How to publish and subscribe to a boolean message?
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]