Thursday, April 27, 2017

ROS Subscriber may be immediately unsubscribed

A caveat for programming in C++ for ROS (Robot Operating System).  The core ROS architecture is of the pub-sub messaging framework, where you can publish on a topic to send messages and subscribe on a topic to receive messages, among ROS nodes.

Both publishing:
ros::NodeHandle nh;
ros::Publisher pub = nh.advertise("some_topic", ..);pub.publish(message);
and subscribing:
ros::Subscriber sub = nh.subscribe("some_topic", .., callback);
are fairly simple to write.  (The callback can be either a function or method, to handle the message received.)

You must hold the returned subscriber object till the end of running your application.  It is a pitfall you can simply ignore it as follows:
nh.subscribe("some_topic", .., callback);
This might result in the deletion of the subscriber object, calling its destructor to unsubscribe from the topic...  You won't have any callback, then.  ;-<

No comments: