我正在尝试修改另一个代码。只是想添加另一个订阅者。这是代码的骨架:
namespace my_server
{
a_server::a_server(ros::NodeHandle n)
: nh()
{
ros::NodeHandle n(n_);
sub1 = new message_filters::Subscriber<sensor_msgs::PointCloud2> (nh(), "/x", 5);
sub2 = new tf::MessageFilter<sensor_msgs::PointCloud2> (*sub1, sub3, xyz, 5);
sub2->registerCallback(boost::bind(&a_server::callback, this, _1));
}
a_server::~a_server()
{
.....
}
void a_server::callback(const sensor_msgs::PointCloud2::ConstPtr& cl)
{
....
}
}
这是头文件:
namespace my_server
{
class a_server
{
public:
a_server(ros::NodeHandle n_ = ros::NodeHandle("~"));
virtual ~a_server();
virtual void callback(const sensor_msgs::PointCloud2::ConstPtr& cl);
protected:
ros::NodeHandle n;
message_filters::Subscriber<sensor_msgs::PointCloud2>* sub1;
tf::MessageFilter<sensor_msgs::PointCloud2>* sub2;
tf::TransformListener sub3;
}
}
现在我想添加另一个订阅者。不需要将其与以前的订阅者绑定。我通过在以前的订阅者之后添加此行来做到这一点。
ros::Subscriber sub4 = m_nh.subscribe<geometry_msgs::Point>("/y", 1, &a_server::callback2);
并在void a_server::callback
后添加了一个回调函数:
void a_server::callback2(geometry_msgs::Point &a)
{
....
}
这到protected
下的头文件:
void callback2(geometry_msgs::Point &a);
这是我收到的错误:
Error:
no match for call to ‘(boost::_mfi::mf1<void, my_server::a_server, geometry_msgs::Point_<std::allocator<void> >&>) (const boost::shared_ptr<const geometry_msgs::Point_<std::allocator<void> > >&)’
BOOST_FUNCTION_RETURN(boost::mem_fn(*f)(BOOST_FUNCTION_ARGS));
我知道这与以前订阅者的boost::bind
使用情况有关。但我不想改变它们。只需要添加另一个订阅者,其回调函数将更新一个全局变量,作为回报,该变量将被以前的回调函数使用。
所以在失去了宝贵的 6 个小时之后。这就是它是如何解决的。订阅者是这样添加的:
ros::Subscriber sub4 = nh.subscribe<geometry_msgs::Point>("/y", 1, boost::bind(&a_server::callback2, this, _1));
回调函数是这样写的:
void a_server::callback2(const geometry_msgs::Point::ConstPtr& a)
{
....
}
头文件已随之添加在Public:
void callback2(const geometry_msgs::Point::ConstPtr& a);