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

Revision history [back]

Generally, in C++ you create a class that contains subscribers and publishers which are initialized in the constructor. You can subscribe to topics and read ROS parameters either in your plugin's constructor or in the (virtual) method initialize.

Generally, in C++ you create a class that contains subscribers and publishers which are initialized in the constructor. You can subscribe to topics and read ROS parameters either in your plugin's constructor or in the (virtual) method initialize.

Edit: The problem with your code is that the NodeHandle and the subscribers and publishers are local to your init method. As soon as this method exits, the stack is unwound and the objects destructed. Make them member variables of your class and everything should work.

Generally, in C++ you create a class that contains subscribers and publishers which are initialized in the constructor. You can subscribe to topics and read ROS parameters either in your plugin's constructor or in the (virtual) method initialize.

Edit: The problem with your code is that the NodeHandle and the subscribers and publishers are local to your init method. As soon as this method exits, the stack is unwound and the objects destructed. Make them member variables of your class and everything should work.

I actually don't think your subscriber works as expected. The compiler should complain unless you have a global method my_plan somewhere. To use my_plan of your class, you need to pass a class instance:

sub1 = private_nh.subscribe("/map", 1, &MyNavigator::my_plan, this);

Alternatively you can use boost::bind. You can find more information on callbacks here.