Skip to content

ROSSERIAL_Arduino_button

rje1974 edited this page Mar 28, 2021 · 1 revision

Boton..

Ingresamos..

ssh pochito@192.168.10.124

Levantamos el contenedor..

./ROS_A_SIM_MB_VNC.sh
vncviewer 192.168.10.124:5901

Abrimos terminator

Sourceamos...

source /root/tractor_ws/devel/setup.bash

Lanzamos el lawn_tractor_sim

roslaunch lawn_tractor_sim lawn_tractor_sim.launch

1 source /root/tractor_ws/devel/setup.bash | roslaunch lawn_tractor_sim lawn_tractor_sim.launch

Partimos ventana en el terminator y hacemos un..

$ rostopic list

rostopic list

abrimos el arduino y en mi caso para seguir usando todos los pins lo puse en el 7.

Imgur Imgur Imgur Imgur

/* 
 * Button Example for Rosserial
 */

#include <ros.h>
#include <std_msgs/Bool.h>


ros::NodeHandle nh;

std_msgs::Bool pushed_msg;
ros::Publisher pub_button("pushed", &pushed_msg);

const int button_pin = 7;
const int led_pin = 13;

bool last_reading;
long last_debounce_time=0;
long debounce_delay=50;
bool published = true;

void setup()
{
  nh.initNode();
  nh.advertise(pub_button);
  
  //initialize an LED output pin 
  //and a input pin for our push button
  pinMode(led_pin, OUTPUT);
  pinMode(button_pin, INPUT);
  
  //Enable the pullup resistor on the button
  digitalWrite(button_pin, HIGH);
  
  //The button is a normally button
  last_reading = ! digitalRead(button_pin);
 
}

void loop()
{
  
  bool reading = ! digitalRead(button_pin);
  
  if (last_reading!= reading){
      last_debounce_time = millis();
      published = false;
  }
  
  //if the button value has not changed for the debounce delay, we know its stable
  if ( !published && (millis() - last_debounce_time)  > debounce_delay) {
    digitalWrite(led_pin, reading);
    pushed_msg.data = reading;
    pub_button.publish(&pushed_msg);
    published = true;
  }

  last_reading = reading;
  
  nh.spinOnce();
}

Lanzamos

Monitor de arduino. Monitor Serial

comprobamos con un rostopic list

chatter

y vemos el chatter con un rostopic echo /chatter

rostopic echo

Clone this wiki locally