Ask Your Question

Subscriber and Publisher node at once in cpp

asked 2011-03-08 03:29:09 -0600

tfoote gravatar image

updated 2011-03-08 10:28:42 -0600

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);
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);

 return 0;
edit retag flag offensive close merge delete


from ros-users mail
tfoote gravatar image tfoote  ( 2011-03-08 03:29:49 -0600 )edit

@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

ctguell gravatar image ctguell  ( 2013-10-02 05:50:53 -0600 )edit

Please see accepted answer from tfoote below. It perfectly describes why the above code does not work and also how it should be modified.

Dirk Thomas gravatar image Dirk Thomas  ( 2013-10-02 06:07:44 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2011-03-08 03:29:34 -0600

tfoote gravatar image

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 for more info.

edit flag offensive delete link more



@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

ctguell gravatar image ctguell  ( 2013-10-02 06:40:03 -0600 )edit

Can you give me a specific solution to this problem, like a sample code will be helpful. Thank you.

skyhawk gravatar image skyhawk  ( 2014-05-06 23:44:05 -0600 )edit

I'm also trying to do something like this, and some sample code would be really helpful!

Andrew.A gravatar image Andrew.A  ( 2014-08-08 06:31:23 -0600 )edit

answered 2015-05-23 14:03:29 -0600

new_forROS gravatar image

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);

edit flag offensive delete link more


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.

jayess gravatar image jayess  ( 2017-06-27 20:49:05 -0600 )edit

Quality lies in the eye of the observer ....

ct2034 gravatar image ct2034  ( 2019-08-09 11:12:12 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2011-03-08 03:29:09 -0600

Seen: 17,276 times

Last updated: May 23 '15