问题
I'm trying to delay processing of some input received via ros topics and as I was reading over the message_filters package information, TimeSequencer seemed to do just this.
They even include some example code which I copied, but cannot get it to compile whatsoever. I have pasted the small test program that produces errors here:
#include "ros/ros.h"
#include <message_filters/subscriber.h>
#include <message_filters/time_sequencer.h>
#include <std_msgs/String.h>
void callback(const std_msgs::String& info)
{
//do some processing
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "test");
ros::NodeHandle n;
message_filters::Subscriber<std_msgs::String> sub(n, "my_topic", 1);
message_filters::TimeSequencer<std_msgs::String> seq(sub, ros::Duration(0.1), ros::Duration(0.01), 10);
seq.registerCallback(callback);
ros::spin();
return 0;
}
The error occurs from seq.registerCallback(callback);
line
In file included from /opt/ros/hydro/include/message_filters/subscriber.h:44:0,
from /home/j/workspace/src/test.cpp:17:
/opt/ros/hydro/include/message_filters/simple_filter.h: In member function message_filters::Connection message_filters::SimpleFilter<M>::registerCallback(void (*)(P)) [with P = const std_msgs::String_<std::allocator<void> >&, M = std_msgs::String_<std::allocator<void> >]:
/home/j/workspace/src/test.cpp:39:32: instantiated from here
/opt/ros/hydro/include/message_filters/simple_filter.h:96:100: error: no matching function for call to message_filters::Signal1<std_msgs::String_<std::allocator<void> > >::addCallback(void (*&)(const std_msgs::String_<std::allocator<void> >&))
/opt/ros/hydro/include/message_filters/simple_filter.h:96:100: note: candidate is:
/opt/ros/hydro/include/message_filters/signal1.h:91:22: note: template<class P> message_filters::Signal1<M>::CallbackHelper1Ptr message_filters::Signal1::addCallback(const boost::function<void(P)>&) [with P = P, M = std_msgs::String_<std::allocator<void> >, message_filters::Signal1<M>::CallbackHelper1Ptr = boost::shared_ptr<message_filters::CallbackHelper1<std_msgs::String_<std::allocator<void> > > >]
I've done a lot of searching for examples with TimeSequencer and can't really find much. Any help is much appreciated.
回答1:
Unlike ros::NodeHandle::subscribe(), message_filters classes require the callback take a boost::shared_ptr.
See documentation here: http://wiki.ros.org/message_filters#Time_Sequencer
void callback(const boost::shared_ptr<std_msgs::String>& info);
Or you can use the ConstPtr typedef:
void callback(const std_msgs::StringConstPtr& info);
回答2:
#include <ros/ros.h>
#include <geometry_msgs/PoseStamped.h>
#include "image_transport/image_transport.h"
#include "cv_bridge/cv_bridge.h"
#include "message_filters/subscriber.h"
#include "message_filters/synchronizer.h"
#include "message_filters/time_synchronizer.h"
#include "message_filters/sync_policies/exact_time.h"
#include <message_filters/sync_policies/approximate_time.h>
using namespace sensor_msgs;
using namespace message_filters;
class SubscribeAndPublish
{
public:
void callback(const sensor_msgs::ImageConstPtr& msg,const sensor_msgs::ImageConstPtr& msg2)
{}
public:
SubscribeAndPublish():sync2(image_sub,image_sub2,500){
//https://gist.github.com/tdenewiler/e2172f628e49ab633ef2786207793336
image_sub.subscribe(n, "/camera/color/image_raw", 1);
image_sub2.subscribe(n, "/camera/aligned_depth_to_color/image_raw", 1);
sync2.registerCallback(boost::bind(&SubscribeAndPublish::callback,this, _1, _2));
odom_pub = n.advertise<nav_msgs::Odometry>("RGBDodom", 50);
}
private:
ros::NodeHandle n;
ros::Publisher odom_pub;
message_filters::Subscriber<Image> image_sub;
message_filters::Subscriber<Image> image_sub2;
TimeSynchronizer<Image, Image> sync2;
};
Try this code above
来源:https://stackoverflow.com/questions/34322075/ros-message-filterstimesequencer