Ask Your Question
0

Problem using callback with multiple arguments via ApproximateTime synchronizer

asked 2014-07-12 05:43:30 -0600

alfa_80 gravatar image

updated 2014-07-14 04:47:25 -0600

sai gravatar image

I have always no success in using callback with multiple arguments (in my use case here is three arguments) be it using exact time synchronizer or approximate one.

My implementation is like below:

void callback(const std_msgs::Float32::ConstPtr& msg1, const std_msgs::Float32::ConstPtr& msg2, const std_msgs::Float32::ConstPtr& msg3)
{
      // some processing
}

message_filters::Subscriber<std_msgs::Float32> sub1(n, "chatter", 1);
message_filters::Subscriber<std_msgs::Float32> sub2(n, "chatter", 1);
message_filters::Subscriber<std_msgs::Float32> sub3(n, "chatter", 1);

typedef sync_policies::ApproximateTime<std_msgs::Float32, std_msgs::Float32, std_msgs::Float32> MySyncPolicy;
// ApproximateTime takes a queue size as its constructor argument, hence MySyncPolicy(10)
Synchronizer<MySyncPolicy> sync(MySyncPolicy(10), sub1, sub2, sub3);
sync.registerCallback(boost::bind(&callback, _1, _2, _3));

Updated complete error:

{-------------------------------------------------------------------------------           
/opt/ros/fuerte/include/message_filters/synchronizer.h:268:5:   instantiated from ‘void message_filters::Synchronizer<Policy>::connectInput(F0&, F1&, F2&, F3&, F4&, F5&) [with F0 = message_filters::Subscriber<std_msgs::Float32_<std::allocator<void> > >, F1 = message_filters::Subscriber<std_msgs::Float32_<std::allocator<void> > >, F2 = message_filters::Subscriber<std_msgs::Float32_<std::allocator<void> > >, F3 = message_filters::NullFilter<message_filters::NullType>, F4 = message_filters::NullFilter<message_filters::NullType>, F5 = message_filters::NullFilter<message_filters::NullType>, Policy = message_filters::sync_policies::ApproximateTime<std_msgs::Float32_<std::allocator<void> >, std_msgs::Float32_<std::allocator<void> >, std_msgs::Float32_<std::allocator<void> > >]’/opt/ros/fuerte/include/message_filters/synchronizer.h:261:5:   instantiated from ‘void message_filters::Synchronizer<Policy>::connectInput(F0&, F1&, F2&, F3&, F4&) [with F0 = message_filters::Subscriber<std_msgs::Float32_<std::allocator<void> > >, F1 = message_filters::Subscriber<std_msgs::Float32_<std::allocator<void> > >, F2 = message_filters::Subscriber<std_msgs::Float32_<std::allocator<void> > >, F3 = message_filters::NullFilter<message_filters::NullType>, F4 = message_filters::NullFilter<message_filters::NullType>, Policy = message_filters::sync_policies::ApproximateTime<std_msgs::Float32_<std::allocator<void> >, std_msgs::Float32_<std::allocator<void> >, std_msgs::Float32_<std::allocator<void> > >]’/opt/ros/fuerte/include/message_filters/synchronizer.h:254:5:   instantiated from ‘void message_filters::Synchronizer<Policy>::connectInput(F0&, F1&, F2&, F3&) [with F0 = message_filters::Subscriber<std_msgs::Float32_<std::allocator<void> > >, F1 = message_filters::Subscriber<std_msgs::Float32_<std::allocator<void> > >, F2 = message_filters::Subscriber<std_msgs::Float32_<std::allocator<void> > >, F3 = message_filters::NullFilter<message_filters::NullType>, Policy = message_filters::sync_policies::ApproximateTime<std_msgs::Float32_<std::allocator<void> >, std_msgs::Float32_<std::allocator<void> >, std_msgs::Float32_<std::allocator<void> > >]’/opt/ros/fuerte/include/message_filters/synchronizer.h:247:5:   instantiated from ‘void message_filters::Synchronizer<Policy>::connectInput(F0&, F1&, F2&) [with F0 = message_filters::Subscriber<std_msgs::Float32_<std::allocator<void> > >, F1 = message_filters::Subscriber<std_msgs::Float32_<std::allocator<void> > >, F2 = message_filters::Subscriber<std_msgs::Float32_<std::allocator<void> > >, Policy = message_filters::sync_policies::ApproximateTime<std_msgs::Float32_<std::allocator<void> >, std_msgs::Float32_<std::allocator<void> >, std_msgs::Float32_<std::allocator<void> > >]’/opt/ros/fuerte/include/message_filters/synchronizer.h:168:5:   instantiated from ‘message_filters::Synchronizer<Policy>::Synchronizer(const Policy&, F0&, F1&, F2&) [with F0 = message_filters::Subscriber<std_msgs::Float32_<std::allocator<void> > >, F1 = message_filters::Subscriber<std_msgs::Float32_<std::allocator<void> > >, F2 = message_filters::Subscriber<std_msgs::Float32_<std::allocator<void> > >, Policy = message_filters::sync_policies::ApproximateTime<std_msgs::Float32_<std::allocator<void> >, std_msgs::Float32_<std::allocator<void> >, std_msgs::Float32_<std::allocator<void> > >]’/home/abc/ros_workspace/prototype/src/prototype_node.cpp:141:68:   instantiated from here /opt/ros/fuerte/include/message_filters/sync_policies/approximate_time.h:620:119: error: ‘value’ is not a member of ‘ros::message_traits ...
(more)
edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
1

answered 2014-08-13 22:29:34 -0600

Murilo F. M. gravatar image

The std_msgs/Float32 message type has no header and hence no time stamp. As a result, time synchroniser will not work. See the documentation: http://docs.ros.org/api/std_msgs/html...

The reason why the code from the previous answer works is because sensor_msgs/PointCloud2 does have a header (and time stamp - http://docs.ros.org/api/sensor_msgs/h... ).

It's a bit of a late answer, but I hope it helps.

edit flag offensive delete link more

Comments

And what is the solution for this kind of problem with a message type without header like float or bool ?

rasoo gravatar imagerasoo ( 2016-11-26 04:51:08 -0600 )edit

You can either use a message type which has a header, or create a custom message with a header and the primitive data type, e.g., FloatStamped or BoolStamped.

Murilo F. M. gravatar imageMurilo F. M. ( 2017-06-27 09:35:55 -0600 )edit

Although its again a bit late ;)... but I am really glad to get answere from you :) thanks :)

rasoo gravatar imagerasoo ( 2017-06-27 09:50:56 -0600 )edit
1

answered 2014-07-12 22:54:03 -0600

sai gravatar image

updated 2014-08-07 11:13:51 -0600

Ok...

Here is the code that is working for me...and I hope that it will come for you.

Add these lines in CMakeLists.txt

FIND_PACKAGE(PCL 1.5)

include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

rosbuild_add_executable(test_file src/test_file.cpp)
rosbuild_link_boost(test_file signals)
target_link_libraries(test_file ${PCL_LIBRARIES} ${BOOST_LIBRARIES})

I dont think that there is need to add PCL..Just added to test if my file is working here...

Next in create a new package and try this thing..

    /*********************************   Header Files   ***************************/   

#include <ros/ros.h>
#include <sensor_msgs/PointCloud2.h>

// PCL specific includes

#include <pcl/ros/conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>

#include <pcl/visualization/cloud_viewer.h>
#include <iostream>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/common/transforms.h>

#include <message_filters/subscriber.h>
#include <message_filters/time_synchronizer.h>

#include <message_filters/synchronizer.h>
#include <message_filters/sync_policies/approximate_time.h>

 /************************************   End of Header Files   **************************/

    using namespace sensor_msgs;
    using namespace message_filters;

    ros::Publisher pub_sub1_sub2;

    void cloud_cb ( const sensor_msgs::PointCloud2ConstPtr& sub2_input,  
                   const sensor_msgs::PointCloud2ConstPtr& sub1_input)
 {
     //processing
     std::cout<< " I am here! " <<std::endl;
      pub_sub1_sub2.publish (*sub2_input);

 }


         int main (int argc, char** argv)
    {
              // Initialize ROS
              ros::init (argc, argv, "testing");
              ros::NodeHandle nh;
              ROS_INFO("Starting ");

              message_filters::Subscriber<sensor_msgs::PointCloud2> sub2(nh, "topic1", 5);
              message_filters::Subscriber<sensor_msgs::PointCloud2> sub1(nh, "topic2", 5);

typedef sync_policies::ApproximateTime<sensor_msgs::PointCloud2, sensor_msgs::PointCloud2> MySyncPolicy;

      // ApproximateTime takes a queue size as its constructor argument, hence MySyncPolicy(10)
              Synchronizer<MySyncPolicy> sync(MySyncPolicy(10), sub2, sub1);
              sync.registerCallback(boost::bind(&cloud_cb, _1, _2));
               pub_sub1_sub2 = nh.advertise<sensor_msgs::PointCloud2> ("/cloud", 1);    

              // Spin
              ros::spin ();
               return 0;

       }

UPDATE

Two suggestions to try.

  1. i think you have to declare it in a different way, try something like this in declaring subscribers." message_filters::Subscriber<std_msgs::Float32<std::allocator<void> > > sub1

  2. Look at this http://wiki.ros.org/roscpp/Overview/M...

edit flag offensive delete link more

Comments

I've just tried your test code and it works perfectly. I guess it could be something wrong somewhere with the data type used, as in my case I use Float32 and in your case you use PointCloud2. Could you please spot anything that I have overlooked regarding the data type used? Thanks.

alfa_80 gravatar imagealfa_80 ( 2014-07-14 04:17:21 -0600 )edit

I cannot find any but by replacing the variables in the test code, were you able to solve your problem and move ahead ? I think its because of adding this in CMakeLists.txt `rosbuild_link_boost(test_file signals)`

sai gravatar imagesai ( 2014-07-14 04:23:40 -0600 )edit

Your test code is actually working perfectly on my system. I am still stuck by the way with my own code. I am guessing it is something to do with Float32 that I am using.

alfa_80 gravatar imagealfa_80 ( 2014-07-14 04:28:35 -0600 )edit

did you try changing the subscribing topic names to different topics ?

sai gravatar imagesai ( 2014-07-14 04:31:11 -0600 )edit

I think that way is similar to testing your test code in that I have already subscribed to a different data type of PointCloud2.

alfa_80 gravatar imagealfa_80 ( 2014-07-14 04:35:20 -0600 )edit

@alfa_80 look at the updated answer

sai gravatar imagesai ( 2014-07-14 04:43:00 -0600 )edit

I've tried different way declaring the subscriber, but it the first place it gives an error in eclipse editor while editing. It says "invalid template argument". In your code, there is an extra closing ">", i guess but I tried both with and without extra one, none is functional.

alfa_80 gravatar imagealfa_80 ( 2014-07-14 04:47:41 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2014-07-12 05:43:30 -0600

Seen: 1,568 times

Last updated: Aug 13 '14