Ask Your Question
0

Publisher/Subscriber Node for Arduino Sensor Data

asked 2014-06-19 07:24:00 -0600

emreay gravatar image

I'm trying to experiment on ROS to improve myself. I'm running ROS Groovy on Ubuntu 12.04 LTS. I have an analog Sharp sensor (4-30 cm range) connected to an Arduino Mega 2560. I'm using rosserial_arduino library, and the code in the arduino converts sensor data to cm and publishes on topic "/range_data".

I'm trying to write a node that is subscribed to the topic /range_data and publishes a boolean data to another topic "/order" with respect to the value of sensor data. I want to publish "0" when the sensor data is lower than 15 cm and "1" when higher.

My sharp listener code:

#include "ros/ros.h"
#include "sensor_msgs/Range.h"
#include "std_msgs/Bool.h"

float range = -1;

void rangeCallback(const sensor_msgs::Range::ConstPtr& range_data)
{
  range = range_data->range;
  ROS_INFO("I heard: [%f]", range);
}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "sharp_listener");
  ros::NodeHandle n;
  ros::Subscriber sub = n.subscribe("range_data", 1000, rangeCallback);
  ros::Publisher pub = n.advertise<std_msgs::Bool>("order", 1000);
  std_msgs::Bool order;

  while (ros::ok())
  {
   while(range =! -1)
   { 
    if(range>=4 && range<=15)
    {
      order.data = 0;
      pub.publish(order);
    }
    else if(range>15)
    {
      order.data = 1;
      pub.publish(order);
    }
    range = -1;
   }
   ros::spin();
  }
  return 0;
}

Order_listener code:

#include "ros/ros.h"
#include "std_msgs/Bool.h"


void orderCallback(const std_msgs::Bool::ConstPtr& msg)
{
  ROS_INFO("%d", msg->data);
}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "order_listener");
  ros::NodeHandle n;
  ros::Subscriber sub = n.subscribe("order", 1000, orderCallback);
  ros::spin();

  return 0;
}

Sharp_listener node is succesfully subscribed to range_data. But it doesn't seem to publish on topic order. I can see that the rqt_graph is true but nothing happens when I start order_listener node or directly rostopic echo.

edit retag flag offensive close merge delete

Comments

HI emreay! I have used your code and was trying to subscribe "order" topic on another arduino. i was not able to blink the led on arduino based on the massage received. I am new to ros programming can you please help me.

gtm gravatar imagegtm ( 2016-03-30 14:29:10 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2014-06-19 07:37:16 -0600

Wolf gravatar image

You must change ros::spin() for ros::spinOnce() in your sharp listener node. ros::spin() blocks until the node is shut down, i. e. your loop is processed only once......

edit flag offensive delete link more
0

answered 2016-03-30 15:23:27 -0600

gtm gravatar image

I was trying to suscribe "order" topic on new arduino but i was not able to do so. i have successfully published "order" topic and both of my arduino are working. I have following code on my second arduino. However i am not able to switch on LED based on the message received. I am new to ROS arduino programming. Can someone please help me to correct the code i have.

#include <ros.h> #include <std_msgs bool.h=""> ros::NodeHandle nh; const int ledPin = 13; boolean state; void messageCb( const std_msgs::Bool& pushed_msg){ if (state ==true) { digitalWrite(ledPin, HIGH); delayMicroseconds(100); digitalWrite(ledPin, LOW); } } ros::Subscriber<std_msgs::bool> sub("order", &messageCb ); void setup() { pinMode(ledPin, OUTPUT); nh.initNode(); nh.subscribe(sub); } void loop() {
nh.spinOnce(); delay(1); }

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

3 followers

Stats

Asked: 2014-06-19 07:24:00 -0600

Seen: 2,645 times

Last updated: Mar 30 '16