How to keep a class "alive" (...and passing the same NodeHandle) ?
Hi, here the minimal example I could create to explain a problem with my program which is splitted into 2 classes. One class creates an istance of the second one:
Let's start with the main file:
#include <ros/ros.h>
#include "include/foo.h"
#include "include/bar.h"
int main ( int argc, char **argv ) {
ros::init( argc, argv, "main_node" );
ros::NodeHandle nh;
ros::NodeHandle nh_private( "~" );
boost::scoped_ptr<Foo> trajectory( new Foo( nh, nh_private ) );
ROS_INFO( "Waiting some seconds..." ); // Just for fun
ros::Duration(3.0).sleep();
return 0;
}
The class Foo has a constructor which initialize the class and later, thtough an internal function, create an istance of the second class.
class Foo {
private:
/* private variables here */
private:
void initParam();
/* of course here there are much more functions already implemented.... */
}
in initParam()
I have the following creation of the last class:
void initParam() // A method defined in Foo
{
/* code code code...*/
boost::scoped_ptr<Bar> way( new Bar( nh_ ) ); // <- !!!! Here is the same NodeHdandle of the master class pased by VALUE
way->amIAlive(); // <- Yes it is....the call is working
ROS_INFO( "Getting a random value: %6.4f", way->generateValues() ) // <-working perfectly here too
/*...*/
}
and lately the class Bar in which I have defined a timer to publish some data regularly:
class Bar {
Bar::Bar( ros::NodeHandle nh ): nh_(nh) {
marker_pub_ = nh_.advertise<visualization_msgs::MarkerArray>( "visualization_marker_array", 10 ); // To publish data on RViz
ros::Timer timeloop_ = nh_.createTimer( ros::Duration( set_duration_time_ ), &Bar::publishPoints, this ); // Nothing...here doesn't publish anything... Why!?!?
void Bar::publishPoints( const ros::TimerEvent& event ) const {
/* publish the markers */
marker_pub_.publish( marker_array_ );
}
}
So guys...the problem. The problem is the second class (Bar) gets the node passed by value but doesn't publish anything. Doing:
$ rostopic echo /visualization_marker_array
stucks all the time. Other functions implemented in the class are working correctly and I get the calculation for the points in the right way. What could be the reason?
I put some comments in the constrctors and destructors of both classes to see where could be the problem and here is the output:
me@YE-166:~/workspace_ros$ rosrun my_pkg my_prog
[ INFO] [1417875069.502609530]: Constructor of the Foo class has been called
[ INFO] [1417875069.505375417]: Constructor of the Bar class has been called
[ INFO] [1417875069.505837985]: Valid number of requested waypoints
[ INFO] [1417875072.506157010]: Number of points: 7
[ WARN] [1417875072.506273683]: Calling Destructor of the Bar's class...
[ INFO] [1417875072.508900594]: Waiting some seconds...
[ WARN] [1417875075.509383455]: Calling Destructor of the Foo's Class...
me@YE-166:~/workspace_ros$
Seems to working correctly but I suppose (and it is my opinion) that the class Bar doesn't have the time to publish data on the topic. Fpor that reasons I put a waiting function inside of it to give it time. But it doesn't help. The class runs the function Bar::publishPoints() which is time event function and it is suppose to run all the time. But get destroy with the class
2 things I have:
- I want to be able to publish data ...
Is there a
spin
orspinOnce
anywhere in your code?