Ask Your Question
0

Compile error creating message_filter with std_msgs/String

asked 2018-02-08 11:27:16 -0600

edamondo gravatar image

updated 2018-02-08 18:48:55 -0600

Hi,

Based on https://wiki.ros.org/message_filters#Time_Synchronizer I am trying to subscribe a node on two topics. I made two of the talker nodes like in https://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28c%2B%2B%29. One talker publishes on chatter and other on chatter1. So below is the code for listenerMultiple.cpp :

#include "ros/ros.h"
#include <message_filters/subscriber.h>
#include <message_filters/synchronizer.h>
#include <message_filters/sync_policies/approximate_time.h>
#include <std_msgs/String.h>

using namespace message_filters;
using namespace std_msgs;
using namespace std;


void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
        ROS_INFO("I heard: [%s]", msg->data.c_str());
}

int main(int argc, char **argv)
{

        ros::init(argc, argv, "listenerMultipleTopics");
        ros::NodeHandle n;

        message_filters::Subscriber<std_msgs::String> camera_sub(n, "chatter",1);
        message_filters::Subscriber<std_msgs::String> imu_sub(n, "chatter2",1);

        typedef sync_policies::ApproximateTime<String, String> MySyncPolicy;
        Synchronizer<MySyncPolicy> sync(MySyncPolicy(10), camera_sub, imu_sub); // adding these lines create problem!!!
        sync.registerCallback(boost::bind(&chatterCallback, _1, _2)); // adding these lines create problem!!!

        ros::spin();
        return 0;
}

edit gvdhorn explained this won't work because there is no header for the String messages. So how can I take the String from the two topics and concatenate them?

This creates a super huge compilation error (how could I solve this?) :

/opt/ros/lunar/include/message_filters/sync_policies/approximate_time.h: In instantiation of ‘void message_filters::sync_policies::ApproximateTime<M0, M1, M2, M3, M4, M5, M6, M7, M8>::checkInterMessageBound() [with int i = 0; M0 = std_msgs::String_<std::allocator<void> >; M1 = std_msgs::String_<std::allocator<void> >; M2 = message_filters::NullType; M3 = message_filters::NullType; M4 = message_filters::NullType; M5 = message_filters::NullType; M6 = message_filters::NullType; M7 = message_filters::NullType; M8 = message_filters::NullType]’:
/opt/ros/lunar/include/message_filters/sync_policies/approximate_time.h:218:32:   required from ‘void message_filters::sync_policies::ApproximateTime<M0, M1, M2, M3, M4, M5, M6, M7, M8>::add(const typename boost::mpl::at_c<typename message_filters::PolicyBase<M0, M1, M2, M3, M4, M5, M6, M7, M8>::Events, i>::type&) [with int i = 0; M0 = std_msgs::String_<std::allocator<void> >; M1 = std_msgs::String_<std::allocator<void> >; M2 = message_filters::NullType; M3 = message_filters::NullType; M4 = message_filters::NullType; M5 = message_filters::NullType; M6 = message_filters::NullType; M7 = message_filters::NullType; M8 = message_filters::NullType; typename boost::mpl::at_c<typename message_filters::PolicyBase<M0, M1, M2, M3, M4, M5, M6, M7, M8>::Events, i>::type = ros::MessageEvent<const std_msgs::String_<std::allocator<void> > >]’
/opt/ros/lunar/include/message_filters/synchronizer.h:358:5:   required from ‘void message_filters::Synchronizer<Policy>::cb(const typename boost::mpl::at_c<typename Policy::Events, i>::type&) [with int i = 0; Policy = message_filters::sync_policies::ApproximateTime<std_msgs::String_<std::allocator<void> >, std_msgs::String_<std::allocator<void> > >; typename boost::mpl::at_c<typename Policy::Events, i>::type = ros::MessageEvent<const std_msgs::String_<std::allocator<void> > >]’
/opt/ros/lunar/include/message_filters/synchronizer.h:290:98:   required from ‘void message_filters::Synchronizer<Policy>::connectInput(F0&, F1&, F2&, F3&, F4&, F5&, F6&, F7&, F8&) [with F0 = message_filters::Subscriber<std_msgs::String_<std::allocator<void> > >; F1 = message_filters::Subscriber<std_msgs::String_<std::allocator<void> > >; F2 = message_filters::NullFilter<message_filters::NullType>; F3 = message_filters::NullFilter<message_filters::NullType>; F4 = message_filters::NullFilter<message_filters::NullType>; F5 = message_filters::NullFilter<message_filters ...
(more)
edit retag flag offensive close merge delete

Comments

I changed the title of your question, as the old title "Have a ros node subscribe to 2 topics" didn't really cover your issue. Subscribing to N topics with a single ROS node is easy. It's the use of message_filters that makes this 'special'.

gvdhoorn gravatar imagegvdhoorn ( 2018-02-08 13:03:08 -0600 )edit

1 Answer

Sort by » oldest newest most voted
2

answered 2018-02-08 12:59:10 -0600

gvdhoorn gravatar image

updated 2018-02-08 13:00:16 -0600

Just about all of the message_filter filters only work with messages with a Header field, as the filters need timestamps to work with.

std_msgs/String doesn't have a header, so that's most likely why you get the compilation error.

Search for header on the wiki/message_filters - Time Synchronizer page you linked.

edit flag offensive delete link more

Comments

Hello gvdhoorn, So how can I use the messages received from the two talkers and stack them into a vector?

edamondo gravatar imageedamondo ( 2018-02-08 16:20:21 -0600 )edit

If you have no requirements for a particular order, I suggest you write a callback that does something like:

void callback(... msg)
{
    my_vector_.push_back(msg->data);
}

provided my_vector_ is a std::vector<std::string> and is in scope, that should work.

gvdhoorn gravatar imagegvdhoorn ( 2018-02-09 02:17:38 -0600 )edit

But my_vector_ would be defined in the main function, so how can it be in the scope of the callback? Sould I pass the my_vector_ by reference to the callback? But then should I bind the callback or something?

edamondo gravatar imageedamondo ( 2018-02-09 07:14:15 -0600 )edit

Either place variables in the global scope, or use a class.

But this is more a C++ question, not a ROS issue.

gvdhoorn gravatar imagegvdhoorn ( 2018-02-09 07:15:30 -0600 )edit

okay, I am going for the class way. Thanks!

edamondo gravatar imageedamondo ( 2018-02-09 10:21:12 -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: 2018-02-08 11:27:16 -0600

Seen: 256 times

Last updated: Feb 08 '18