Ask Your Question
9

Subscriber and Publisher node at once in cpp

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

tfoote gravatar image

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

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;
}
edit retag flag offensive close merge delete

Comments

from ros-users mail
tfoote gravatar imagetfoote ( 2011-03-08 03:29:49 -0500 )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 imagectguell ( 2013-10-02 05:50:53 -0500 )edit
1

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 imageDirk Thomas ( 2013-10-02 06:07:44 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
11

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

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 http://www.ros.org/wiki/roscpp/Overview/Publishers%20and%20Subscribers for more info.

edit flag offensive delete link more

Comments

3

@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 imagectguell ( 2013-10-02 06:40:03 -0500 )edit
2

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

skyhawk gravatar imageskyhawk ( 2014-05-06 23:44:05 -0500 )edit
1

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

Andrew.A gravatar imageAndrew.A ( 2014-08-08 06:31:23 -0500 )edit
2

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

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

Comments

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 imagejayess ( 2017-06-27 20:49:05 -0500 )edit

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

ct2034 gravatar imagect2034 ( 2019-08-09 11:12:12 -0500 )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

4 followers

Stats

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

Seen: 13,616 times

Last updated: May 23 '15