Compile error creating message_filter with std_msgs/String
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 ...
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'.