0
$\begingroup$

Rosanswers logo

i need some an example how to wrtie a node, wich is subscribing and publishing at the same time. I want to publish a message when i got i message, beacuse of that i try to publish in the Callback function, but it doesn't work.

My code is following:

void pointCallback(const nxt_msgs::Range  &msg)
{
         sensor_msgs::LaserScan pub;
         ros::NodeHandle r;
         ros::Publisher laser_scan_pub = r.advertise<sensor_msgs::LaserScan>("laser_scan", 50);
         laser_scan_pub.publish(pub);
}
int main(int argc, char **argv)
{

 ros::init(argc, argv, "range_to_laser");
 ros::NodeHandle n;
 ros::Subscriber sub = n.subscribe("point_cloud", 50, pointCallback);
 ros::spin();

 return 0;
}

Originally posted by tfoote on ROS Answers with karma: 58457 on 2011-03-08

Post score: 10


Original comments

Comment by tfoote on 2011-03-08:
from ros-users mail

Comment by ctguell on 2013-10-02:
@tfoote could you solve this after all? Im trying to do the same and im having trouble accomplishing it, any help would be great , thanks

Comment by Dirk Thomas on 2013-10-02:
Please see accepted answer from tfoote below. It perfectly describes why the above code does not work and also how it should be modified.

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

I wouldn't create the Publisher in the callback. Usually I have a class where I create the publisher in the constructor and store it in a member variable. Publishing can then be done from a callback method, that uses the publisher member to call .publish().

You could also use a global variable to store the publisher object if you do not want classes. Or you bind the publisher boost::bind to the callback.

See http://www.ros.org/wiki/roscpp/Overview/Publishers%20and%20Subscribers for more info.


Originally posted by tfoote with karma: 58457 on 2011-03-08

This answer was ACCEPTED on the original site

Post score: 13


Original comments

Comment by ctguell on 2013-10-02:
@tfoote after understanding the answer I still cant manage to make it work, could you post the code that finally worked, i would really appreciate it ? Thanks

Comment by skyhawk on 2014-05-06:
Can you give me a specific solution to this problem, like a sample code will be helpful. Thank you.

Comment by Andrew.A on 2014-08-08:
I'm also trying to do something like this, and some sample code would be really helpful!

$\endgroup$
0
$\begingroup$

Rosanswers logo

Better solution: ros::Publisher //declare this globally

laser_scan_pub.publish(pub); //put this in same function

//change the following to main function ros::NodeHandle r; laser_scan_pub = r.advertise<sensor_msgs::LaserScan>("laser_scan", 50);


Originally posted by new_forROS with karma: 31 on 2015-05-23

This answer was NOT ACCEPTED on the original site

Post score: 2


Original comments

Comment by jayess on 2017-06-27:
This is part of @tfoote's solution. This was the second of his three proposed solutions. Also, using globals is usually not the best solution and works "good enough" if your node is short. Otherwise, it can become quite confusing.

Comment by ct2034 on 2019-08-09:
Quality lies in the eye of the observer ....

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.