ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
10

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

2 Answers

Sort by » oldest newest most voted
13

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 image ctguell  ( 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 image skyhawk  ( 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 image Andrew.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 image jayess  ( 2017-06-27 20:49:05 -0500 )edit
1

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

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

Question Tools

4 followers

Stats

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

Seen: 24,781 times

Last updated: May 23 '15