0
$\begingroup$

Rosanswers logo

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]

Originally posted by Tomm on ROS Answers with karma: 59 on 2016-07-08

Post score: 0

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

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.


Originally posted by Icehawk101 with karma: 955 on 2016-07-08

This answer was ACCEPTED on the original site

Post score: 3


Original comments

Comment by Tomm on 2016-07-08:
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.

$\endgroup$
0
$\begingroup$

Rosanswers logo

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


Originally posted by samiSH with karma: 38 on 2017-01-21

This answer was NOT 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 that you have read and understand our privacy policy and code of conduct.