ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How to publish and subscribe to a boolean message?

asked 2016-07-08 13:49:36 -0500

Tomm gravatar image

updated 2016-07-09 04:24:43 -0500

gvdhoorn gravatar image

Hello everyone, I am moving my robot(pioneer 3dx) from say point A to point B. When my robot reaches point B, I want to publish a boolean message to a topic "PosReached". Then I want to my robotic arm connected to arduino (rosserial_arduino) to subscribe to that message and start working on its task. Here is how I write my publisher:

ros::Publisher BasePos1 = n.advertise<std_msgs::Bool>("PosReached",1000);
std_msgs::Bool Reached;

//some code here

if(linearposx=0.1)
      {
      velocity.linear.x= 0;
      Reached.data = true;
      Velocity_pub.publish(velocity);
      BasePos1.publish(Reached);
      break;
      }

Above code compiles successfully. I just wonder whether it is the right way to do it?

Coming to subscriber here is what I wrote on my Arduino IDE:

void positionreached(const std_msgs::Bool& Reached)
{
Don't know what to write here.
}

ros::Subscriber<std_msgs::Bool> sub("PosReached", &positionreached);

What should I write in the callback function? I don't know how to receive boolean message and the use it in my main loop,to let robotic arm know when to start working on its task.

Can you guys help me with syntax or example codes?

Edit: I did the following changes as suggested:

std_msgs::Bool reached=false;
void positionreached(const std_msgs::Bool::ConstPtr& Reached)
{
  reached=Reached->data;
}

ros::Subscriber<std_msgs::Bool> sub("PosReached", &positionreached);

I get the following errors:

conversion from ‘int’ to non-scalar type ‘std_msgs::Bool’ requested
 #define false 0x0

sketch_jul07a.ino:8:24: note: in expansion of macro ‘false’
sketch_jul07a.ino:10:28: error: ‘ConstPtr’ in ‘class std_msgs::Bool’ does not name a type
sketch_jul07a.ino:10:54: error: ISO C++ forbids declaration of ‘Reached’ with no type [-fpermissive]
sketch_jul07a.ino: In function ‘void positionreached(const int&)’:
sketch_jul07a.ino:12:18: error: base operand of ‘->’ is not a pointer
sketch_jul07a.ino: At global scope:

error: invalid conversion from ‘void (*)(const int&)’ to ‘ros::Subscriber<std_msgs::Bool>::CallbackT {aka void (*)(const std_msgs::Bool&)}’ [-fpermissive]
edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
3

answered 2016-07-08 14:18:56 -0500

updated 2016-07-11 13:31:42 -0500

If you are using move_base to autonomously drive to a location, then using the service provides a return message when it reaches the goal. Otherwise, if you want to use the bool method, it is the same as any other callback. Make a global variable to hold some flag, called reached below, and populate it with the data value from the message.

bool reached = false;

void positionreached(const std_msgs::Bool::ConstPtr& Reached)
{
  reached = Reached->data;
}

[EDIT]

Right, Arduino. Try:

void positionreached(const std_msgs::Bool Reached)
{
  reached = Reached.data;
}

Also, reached only needs to be bool, not std_msgs::Bool.

edit flag offensive delete link more

Comments

I have done the changes you suggested and got errors. I have edited the post and copied my errors there. Please have a look. Please note that I am using Arduino IDE for compiling my sketches. Thanks.

Tomm gravatar image Tomm  ( 2016-07-08 14:48:06 -0500 )edit
1

answered 2017-01-21 00:10:23 -0500

samiSH gravatar image

You need to include: #include "std_msgs/Bool.h"

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2016-07-08 13:49:36 -0500

Seen: 24,795 times

Last updated: Jan 21 '17