ROS2 同步器寄存器回调类内问题

ROS2 synchronizer register callback inside a class problem

提问人:JanSam 提问时间:6/28/2023 最后编辑:JanSam 更新时间:6/28/2023 访问量:296

问:

我正在使用 ROS2-HUMBLE,并尝试在使用 NEARIMATE TIME POLICY 时利用message_filters SYNCHRONIZER。但是在注册同步回调时,会出现问题。当不在类中使用时,回调工作正常,但是在类中使用时,registerCallback 函数需要静态方法作为回调(不适用于我的意图解决方案)。


如何将回调注册为非静态函数。


这是我的回调函数:

    void performNDT(const sensor_msgs::msg::LaserScan &lidar_msg, const nav_msgs::msg::Odometry &odom_msg)
    {
      std::cout << "Hello sync world" << std::endl;
    }

我定义了同步策略和同步器,如下所示:

    typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::msg::LaserScan, nav_msgs::msg::Odometry> approximate_time_sync_policy;
    message_filters::Synchronizer<approximate_time_sync_policy> synchronizer;

初始化同步器并尝试注册回调的类构造函数如下所示:

    LandmarkExtractor()
    : Node("landmark_extractor"),
    synchronizer(approximate_time_sync_policy(10), lidar_sub, odom_sub)
    {      
      /******* QOS SETTINGS *******/
      custom_qos_profile.depth = 1;
      custom_qos_profile.reliability = rmw_qos_reliability_policy_t::RMW_QOS_POLICY_RELIABILITY_BEST_EFFORT;
      custom_qos_profile.history = rmw_qos_history_policy_t::RMW_QOS_POLICY_HISTORY_KEEP_LAST;
      custom_qos_profile.durability = rmw_qos_durability_policy_t::RMW_QOS_POLICY_DURABILITY_SYSTEM_DEFAULT;

      // Subscribing to Odometry and LaserScan msg topics
      lidar_sub.subscribe(this, "scan", custom_qos_profile);
      odom_sub.subscribe(this, "odom", custom_qos_profile);

      // Registering synced callback
      synchronizer.registerCallback(std::bind(&LandmarkExtractor::performNDT,this, _1, _2));

...
}

订阅者定义如下: ̈

    // Subscribers
    message_filters::Subscriber<sensor_msgs::msg::LaserScan> lidar_sub;
    message_filters::Subscriber<nav_msgs::msg::Odometry> odom_sub;

使用构建此解决方案时,我收到这个可怕的错误:colcon build

error: no matching function for call to ‘message_filters::Signal9<std::shared_ptr<sensor_msgs::msg::LaserScan_<std::allocator<void> > >, std::shared_ptr<nav_msgs::msg::Odometry_<std::allocator<void> > >, message_filters::NullType, message_filters::NullType, message_filters::NullType, message_filters::NullType, message_filters::NullType, message_filters::NullType, message_filters::NullType>::addCallback<const M0ConstPtr&, const M1ConstPtr&, const M2ConstPtr&, const M3ConstPtr&, const M4ConstPtr&, const M5ConstPtr&, const M6ConstPtr&, const M7ConstPtr&, const M8ConstPtr&>(std::_Bind_helper<false, const std::_Bind<void (LandmarkExtractor::*(LandmarkExtractor*, std::_Placeholder<1>, std::_Placeholder<2>))(const std::shared_ptr<sensor_msgs::msg::LaserScan_<std::allocator<void> > >&, const std::shared_ptr<nav_msgs::msg::Odometry_<std::allocator<void> > >&)>&, const std::_Placeholder<1>&, const std::_Placeholder<2>&, const std::_Placeholder<3>&, const std::_Placeholder<4>&, const std::_Placeholder<5>&, const std::_Placeholder<6>&, const std::_Placeholder<7>&, const std::_Placeholder<8>&, const std::_Placeholder<9>&>::type)’
  272 |     return addCallback<const M0ConstPtr&,
      |            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  273 |                      const M1ConstPtr&,
      |                      ~~~~~~~~~~~~~~~~~~ 
  274 |                      const M2ConstPtr&,
      |                      ~~~~~~~~~~~~~~~~~~ 
  275 |                      const M3ConstPtr&,
      |                      ~~~~~~~~~~~~~~~~~~ 
  276 |                      const M4ConstPtr&,
      |                      ~~~~~~~~~~~~~~~~~~ 
  277 |                      const M5ConstPtr&,
      |                      ~~~~~~~~~~~~~~~~~~ 
  278 |                      const M6ConstPtr&,
      |                      ~~~~~~~~~~~~~~~~~~ 
  279 |                      const M7ConstPtr&,
      |                      ~~~~~~~~~~~~~~~~~~ 
  280 |                      const M8ConstPtr&>(std::bind(callback, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5, std::placeholders::_6, std::placeholders::_7, std::placeholders::_8, std::placeholders::_9));
      |                      ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /opt/ros/humble/include/message_filters/message_filters/synchronizer.h:47,
                 from /opt/ros/humble/include/message_filters/message_filters/time_synchronizer.h:41,
                 from /home/jan/ros_ws/src/shamanbot/src/landmark_extraction.cpp:8:
/opt/ros/humble/include/message_filters/message_filters/signal9.h:170:14: note: candidate: ‘message_filters::Connection message_filters::Signal9<M0, M1, M2, M3, M4, M5, M6, M7, M8>::addCallback(const std::function<void(P0, P1, P2, P3, P4, P5, P6, P7, P8)>&) [with P0 = const std::shared_ptr<const std::shared_ptr<sensor_msgs::msg::LaserScan_<std::allocator<void> > > >&; P1 = const std::shared_ptr<const std::shared_ptr<nav_msgs::msg::Odometry_<std::allocator<void> > > >&; P2 = const std::shared_ptr<const message_filters::NullType>&; P3 = const std::shared_ptr<const message_filters::NullType>&; P4 = const std::shared_ptr<const message_filters::NullType>&; P5 = const std::shared_ptr<const message_filters::NullType>&; P6 = const std::shared_ptr<const message_filters::NullType>&; P7 = const std::shared_ptr<const message_filters::NullType>&; P8 = const std::shared_ptr<const message_filters::NullType>&; M0 = std::shared_ptr<sensor_msgs::msg::LaserScan_<std::allocator<void> > >; M1 = std::shared_ptr<nav_msgs::msg::Odometry_<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]’
  170 |   Connection addCallback(const std::function<void(P0, P1, P2, P3, P4, P5, P6, P7, P8)>& callback)
      |              ^~~~~~~~~~~
/opt/ros/humble/include/message_filters/message_filters/signal9.h:170:89: note:   no known conversion for argument 1 from ‘std::_Bind_helper<false, const std::_Bind<void (LandmarkExtractor::*(LandmarkExtractor*, std::_Placeholder<1>, std::_Placeholder<2>))(const std::shared_ptr<sensor_msgs::msg::LaserScan_<std::allocator<void> > >&, const std::shared_ptr<nav_msgs::msg::Odometry_<std::allocator<void> > >&)>&, const std::_Placeholder<1>&, const std::_Placeholder<2>&, const std::_Placeholder<3>&, const std::_Placeholder<4>&, const std::_Placeholder<5>&, const std::_Placeholder<6>&, const std::_Placeholder<7>&, const std::_Placeholder<8>&, const std::_Placeholder<9>&>::type’ to ‘const std::function<void(const std::shared_ptr<const std::shared_ptr<sensor_msgs::msg::LaserScan_<std::allocator<void> > > >&, const std::shared_ptr<const std::shared_ptr<nav_msgs::msg::Odometry_<std::allocator<void> > > >&, const std::shared_ptr<const message_filters::NullType>&, const std::shared_ptr<const message_filters::NullType>&, const std::shared_ptr<const message_filters::NullType>&, const std::shared_ptr<const message_filters::NullType>&, const std::shared_ptr<const message_filters::NullType>&, const std::shared_ptr<const message_filters::NullType>&, const std::shared_ptr<const message_filters::NullType>&)>&’
  170 |   Connection addCallback(const std::function<void(P0, P1, P2, P3, P4, P5, P6, P7, P8)>& callback)
      |                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
/opt/ros/humble/include/message_filters/message_filters/signal9.h:180:14: note: candidate: ‘template<class P0, class P1> message_filters::Connection message_filters::Signal9<M0, M1, M2, M3, M4, M5, M6, M7, M8>::addCallback(void (*)(P0, P1)) [with P0 = P0; P1 = P1; M0 = std::shared_ptr<sensor_msgs::msg::LaserScan_<std::allocator<void> > >; M1 = std::shared_ptr<nav_msgs::msg::Odometry_<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]’
  180 |   Connection addCallback(void(*callback)(P0, P1))

C++ 回调 同步 ROS

评论

0赞 Ted Lyngmo 6/28/2023
我对该库完全一无所知,但也许使用 lambda 添加回调至少会使错误输出更易于阅读 - 也许。无论如何,它比占位符更容易理解。synchronizer.registerCallback([this](const sensor_msgs::msg::LaserScan &lidar_msg, const nav_msgs::msg::Odometry &odom_msg) { performNDT(lidar_msg, odom_msg); });
0赞 JanSam 6/28/2023
非常感谢您对此问题的贡献。可悲的是,当我用 lambda 表达式替换有问题的部分时,我收到一个类似的错误,指出我使用了错误数量的模板参数 (9) 应该是 (1)。
0赞 JanSam 7/1/2023
当我像这样调用registerCallback时,我最终解决了这个问题:synchronizer.registerCallback(&LandmarkExtractor::performNDT, this);
0赞 Ted Lyngmo 7/1/2023
明!这比错误消息所传达的要简单(至少对我来说是这样):-)

答: 暂无答案