【ROS】使用ros::Timer周期性調用函數遇到bug


背景

在類A內,聲明了一個ros::timer變量,然后使用該定時器周期性調用函數。遇到了報錯。

類A代碼如下,

A.h 類的聲明

class A
{
void initA(ros::NodeHandle& nh);
ros::NodeHandle node_;
        ros::Timer xxxx_timer_;
    
        bool radarMapUpdate(const ros::TimerEvent& /*event*/);
};

 

A.cpp 類的定義

void A::initA(ros::NodeHandle& nh)
{
    node_ = nh;

    xxxx_timer_ = node_.createTimer(ros::Duration(0.05), &A::radarMapUpdate, this);
}

bool A::radarMapUpdate()
{
    ....;
}

 

報錯內容如下,

/home/gordon/ros_ws/src/xxxxxxxx/radar_grid_map.cc:1402:93:   required from here
/usr/include/boost/function/function_template.hpp:231:11: error: no match for call to ‘(boost::_mfi::mf1<bool, xag_nav::planning::RadarGridMap, const ros::TimerEvent&>) (const ros::TimerEvent&)’
           BOOST_FUNCTION_RETURN(boost::mem_fn(*f)(BOOST_FUNCTION_ARGS));
           ^
In file included from /usr/include/boost/bind/mem_fn.hpp:215:0,
                 from /usr/include/boost/mem_fn.hpp:22,
                 from /usr/include/boost/function/detail/prologue.hpp:18,
                 from /usr/include/boost/function.hpp:24,
                 from /opt/ros/kinetic/include/ros/forwards.h:40,
                 from /opt/ros/kinetic/include/ros/common.h:37,
                 from /opt/ros/kinetic/include/ros/ros.h:43,
                 from /home/gordon/ros_ws/src/xxxxxx/radar_grid_map.h:13,
                 from /home/gordon/ros_ws/src/xxxxxx/radar_grid_map.cc:3:
/usr/include/boost/bind/mem_fn_template.hpp:163:7: note: candidate: R boost::_mfi::mf1<R, T, A1>::operator()(T*, A1) const [with R = bool; T = xag_nav::planning::RadarGridMap; A1 = const ros::TimerEvent&]
     R operator()(T * p, A1 a1) const
       ^
/usr/include/boost/bind/mem_fn_template.hpp:163:7: note:   candidate expects 2 arguments, 1 provided
/usr/include/boost/bind/mem_fn_template.hpp:168:25: note: candidate: template<class U> R boost::_mfi::mf1<R, T, A1>::operator()(U&, A1) const [with U = U; R = bool; T = xag_nav::planning::RadarGridMap; A1 = const ros::TimerEvent&]
     template<class U> R operator()(U & u, A1 a1) const
                         ^
/usr/include/boost/bind/mem_fn_template.hpp:168:25: note:   template argument deduction/substitution failed:
In file included from /usr/include/boost/function/detail/maybe_include.hpp:18:0,
                 from /usr/include/boost/function/detail/function_iterate.hpp:14,
                 from /usr/include/boost/preprocessor/iteration/detail/iter/forward1.hpp:52,
                 from /usr/include/boost/function.hpp:64,
                 from /opt/ros/kinetic/include/ros/forwards.h:40,
                 from /opt/ros/kinetic/include/ros/common.h:37,
                 from /opt/ros/kinetic/include/ros/ros.h:43,
                 from /home/gordon/ros_ws/src/xxxxxxx/radar_grid_map.h:13,
                 from /home/gordon/ros_ws/src/xxxxxxx/radar_grid_map.cc:3:
/usr/include/boost/function/function_template.hpp:231:11: note:   candidate expects 2 arguments, 1 provided
           BOOST_FUNCTION_RETURN(boost::mem_fn(*f)(BOOST_FUNCTION_ARGS));
           ^
In file included from /usr/include/boost/bind/mem_fn.hpp:215:0,
                 from /usr/include/boost/mem_fn.hpp:22,
                 from /usr/include/boost/function/detail/prologue.hpp:18,
                 from /usr/include/boost/function.hpp:24,
                 from /opt/ros/kinetic/include/ros/forwards.h:40,
                 from /opt/ros/kinetic/include/ros/common.h:37,
                 from /opt/ros/kinetic/include/ros/ros.h:43,
                 from /home/gordon/ros_ws/src/xxxxxxx/radar_grid_map.h:13,
                 from /home/gordon/ros_ws/src/xxxxxxx/radar_grid_map.cc:3:
/usr/include/boost/bind/mem_fn_template.hpp:176:25: note: candidate: template<class U> R boost::_mfi::mf1<R, T, A1>::operator()(const U&, A1) const [with U = U; R = bool; T = xag_nav::planning::RadarGridMap; A1 = const ros::TimerEvent&]
     template<class U> R operator()(U const & u, A1 a1) const

 

解決方案

報錯的意思,大致是找到了boost庫中的候選函數,但是實參和形參對應不上!

據此,把函數 bool radarMapUpdate() {} 改成 void radarMapUpdate() {} 即可。

 

寫在最后

由於對ros timer的理解不深,使用過程中會存在一些玄學的錯誤。比如,timer沒有成功地周期性調用函數!

長個心眼。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM