0
$\begingroup$

Rosanswers logo

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.


Originally posted by emreay on ROS Answers with karma: 90 on 2014-06-19

Post score: 0


Original comments

Comment by gtm on 2016-03-30:
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.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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......


Originally posted by Wolf with karma: 7555 on 2014-06-19

This answer was ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.