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

TimeSync handler never called

asked 2013-09-28 05:26:13 -0600

rnunziata gravatar image

updated 2013-10-01 03:38:11 -0600

The following code handler is never called. This should work yes?

    #include <string>
    #include <ros/ros.h>
    #include <geometry_msgs/PoseStamped.h>
    #include <message_filters/subscriber.h>
    #include <message_filters/time_synchronizer.h>
    #include <message_filters/sync_policies/approximate_time.h>

    using namespace message_filters;

    class SynchronizerTest

    void handelerSynchronizerTest(const geometry_msgs::PoseStampedConstPtr& msg1, const geometry_msgs::PoseStampedConstPtr& msg2)
        ROS_INFO("HERE1 made it to handeler [%e]", msg1->header.stamp.toSec());
        ROS_INFO("HERE2                     [%e]", msg2->header.stamp.toSec());

void setTest()
     j1_sub = n.subscribe("/SynchronizerTest1", 10, &SynchronizerTest::handelerSynchronizerTest, this);
     j2_sub = n.subscribe("/SynchronizerTest2", 10, &SynchronizerTest::handelerSynchronizerTest, this);
    // TimeSynchronizer<geometry_msgs::PoseStamped, geometry_msgs::PoseStamped> sync(j1_sub, j2_sub, 10);
    // sync.registerCallback(boost::bind(&SynchronizerTest::handelerSynchronizerTest, this, _1, _2));
     ROS_INFO("Test set");


 ros::NodeHandle n;
 Subscriber<geometry_msgs::PoseStamped> j1_sub;
 Subscriber<geometry_msgs::PoseStamped> j2_sub;

};  // Enodof Class

    int main(int argc, char **argv)
      ros::init(argc, argv, "SynchronizerTest");
      ros::NodeHandle n;
      ros::Rate loop_rate(10);
      int count = 0;

      SynchronizerTest sp;
      ros::Publisher pub1 = n.advertise<geometry_msgs::PoseStamped>("SynchronizerTest1", 10);
      ros::Publisher pub2 = n.advertise<geometry_msgs::PoseStamped>("SynchronizerTest2", 10);
      while (ros::ok())
         geometry_msgs::PoseStamped msg;
         msg.header.stamp = ros::Time::now();

      return 0;
edit retag flag offensive close merge delete


How do you publish to `/SynchronizerTest1` and `/SynchronizerTest2`? Do your messages satisfy the conditions from here: ?

Boris gravatar image Boris  ( 2013-09-28 08:29:24 -0600 )edit

If you can see where this does not meet requirements then please point it out. Really all I want is a blocking read I can correlate time. I did look over this document and did not see anything that should stop this code from working.

rnunziata gravatar image rnunziata  ( 2013-09-28 08:42:55 -0600 )edit

Oh, sorry... I confused this questions with another yours. Somehow several of them looks very similar.

Boris gravatar image Boris  ( 2013-09-28 20:14:56 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2013-09-28 20:16:20 -0600

updated 2013-10-01 03:55:42 -0600

The problem is that you have defined subscribers in a local namespace. Thus j1_sub and j2_sub are destroyed immediately after setTest() function returns. So make them members of the class to make it work. Same with the TimeSynchronizer.


Here is one of the possible implementations:

class SynchronizerTest
  boost::shared_ptr<Subscriber<geometry_msgs::PoseStamped> > j1_sub;
  boost::shared_ptr<Subscriber<geometry_msgs::PoseStamped> > j2_sub;
  boost::shared_ptr<TimeSynchronizer<geometry_msgs::PoseStamped, geometry_msgs::PoseStamped> > sync;


  void setTest()
    j1_sub.reset(new Subscriber<geometry_msgs::PoseStamped>(n, "/SynchronizerTest1", 10));
    j2_sub.reset(new Subscriber<geometry_msgs::PoseStamped>(n, "/SynchronizerTest2", 10));
    sync.reset(new TimeSynchronizer<geometry_msgs::PoseStamped, geometry_msgs::PoseStamped>(*j1_sub, *j2_sub, 10));
    sync->registerCallback(boost::bind(&SynchronizerTest::handelerSynchronizerTest, this, _1, _2));
    ROS_INFO("Test set");


edit flag offensive delete link more


I have made the changes you recommend by now I get compilation errors. Can you supply the correct code for this short program please. I do recommend that the documentation be updated with solution for class functions.

rnunziata gravatar image rnunziata  ( 2013-09-29 04:37:28 -0600 )edit

Well, you did some very strange modifications, but definitely not what was recommended. I will update the answer with a code snippet in a bit.

Boris gravatar image Boris  ( 2013-09-29 04:47:57 -0600 )edit

Thank you..I look forward to the solution.

rnunziata gravatar image rnunziata  ( 2013-09-29 05:23:33 -0600 )edit

Do you think you will have time this week to post the solution. Thank you.

rnunziata gravatar image rnunziata  ( 2013-09-30 15:21:49 -0600 )edit

It has been posted couple of days ago. Please see EDIT section in my answer.

Boris gravatar image Boris  ( 2013-09-30 18:32:25 -0600 )edit

Sorry Boris...did not see Edit. The *n_ was giving me problems but it compiled if I changed it to just n. Will be testing shortly.

rnunziata gravatar image rnunziata  ( 2013-10-01 03:39:30 -0600 )edit

My mistake, sorry... That is actually exerpt from a working program, I just have a habit to keep things as shared pointers including `ros::NodeHandle` and forgot to change it back to your notation.

Boris gravatar image Boris  ( 2013-10-01 04:01:28 -0600 )edit

Much thanks...that worked..:-) I would like to see the tutorial updated to reflex member usage as it is not obvious. I feel the two approaches present a inconsistent view of the interface and that it would be better to just have one that worked for both.

rnunziata gravatar image rnunziata  ( 2013-10-01 04:03:28 -0600 )edit

Question Tools

1 follower


Asked: 2013-09-28 05:26:13 -0600

Seen: 350 times

Last updated: Oct 01 '13