介绍 ROS 中的时刻、时间间隔、定时器的定义和应用。
FROM:https://blog.csdn.net/xuehuafeiwu123/article/details/68060377

一、Time

1、时刻和间隔

ROS 中有 time 和 duration 两种类型,相应的有 ros::Time 和 ros::Duration 类。

  • time 表示的是时刻
  • duration 表示的是时间间隔

其统一表示形式为:

  1. int32 sec
  2. int32 nsec

ROS 可以给节点提供一个模拟始时钟。不同于平台时间,你可以利用 roscpp 的时间例程来得到当前的时间,此时间能够和模拟时间、wall-clock 时间进行无缝连接。

1.1 获得当前时间

  1. ros::Time::now()
  1. ros::Time begin = ros::Time::now();

时间计算起点
使用模拟时间时,当 /clock节点接受到第一条消息时,now() 返回时刻 0,此时客户端还不知道时钟时间。

1.2 创建时间和间隔

浮点数形式

  1. ros::Time a_little_after_the_beginning(0.001);
  2. ros::Duration five_seconds(5.0);

用两个整数表示

  1. ros::Time a_little_after_the_beginning(0, 1000000);
  2. ros::Duration five_seconds(5, 0);

1.3 时间与间隔的转化

  1. double secs =ros::Time::now().toSec();
  2. ros::Duration d(0.5);
  3. secs = d.toSec();

1.4 时间与间隔的四则运算

时间和时刻的四则运算实例如下:

  1. ros::Duration two_hours = ros::Duration(60*60) + ros::Duration(60*60);
  2. ros::Duration one_hour = ros::Duration(2*60*60) - ros::Duration(60*60);
  3. ros::Time tomorrow = ros::Time::now() + ros::Duration(24*60*60);
  4. ros::Duration negative_one_day = ros::Time::now() - tomorrow;

2、Sleeping and Rates

bool ros::Duration::sleep()

睡眠 0.5s:

  1. ros::Duration(0.5).sleep(); // sleep for half a second

ros::Rate

频率 10Hz:

  1. ros::Rate r(10); // 10 hz
  2. while (ros::ok())
  3. {
  4. ... do some work ...
  5. r.sleep();
  6. }

Rate 和 Timer 的作用一样,最好用 Timer 来定时。

3、Wall Time

在模拟时,如果想要进入实际运行 wall-clock time ,可以用 ros::WallTime, ros::WallDuration, 和ros::WallRate,类似于 ros::Time, ros::Duration, 和 ros::Rate

二、Timer

定时器不能代替实时线程/内核,它们仅对没有硬实时要求的事物有用。

1、定义定时器

方法: ros::NodeHandle::createTimer()

  1. ros::Timer timer = nh.createTimer(ros::Duration(0.1), timerCallback);

完整定义:

  1. ros::Timer ros::NodeHandle::createTimer(ros::Duration period, <callback>, bool oneshot = false);

period:定时器回调函数之间的时间间隔
:定时器回调,函数、类方法或者函数子对象
oneshot:是否只定时一次。false,就是连续定时。
2、回调特征

  1. void callback(const ros::TimerEvent&);
  1. struct TimerEvent
  2. {
  3. Time last_expected; ///< 上一回调函数应该发生的时刻
  4. Time last_real; ///< 上一回调函数实际发生的时刻
  5. Time current_expected; ///< 当前回调函数应该发生的时刻
  6. Time current_real; ///< 当前回调函数实际发生的时刻
  7. struct
  8. {
  9. WallDuration last_duration; ///<包含上一回调的时间间隔(结束时间-开始时间),它始终在 `wall-clock time`
  10. } profile;
  11. };

3、回调类型

functions
class methods
functor objects (including boost::function)

3.1 Functions

  1. void callback(const ros::TimerEvent& event)
  2. {
  3. ...
  4. }
  5. ...
  6. ros::Timer timer = nh.createTimer(ros::Duration(0.1), callback);

3.2 Class Methods

  1. void Foo::callback(const ros::TimerEvent& event)
  2. {
  3. ...
  4. }
  5. ...
  6. Foo foo_object;
  7. ros::Timer timer = nh.createTimer(ros::Duration(0.1), &Foo::callback, &foo_object);

3.3 Functor Objects

  1. class Foo
  2. {
  3. public:
  4. void operator()(const ros::TimerEvent& event)
  5. {
  6. ...
  7. }
  8. };
  9. ...
  10. ros::Timer timer = nh.createTimer(ros::Duration(0.1), Foo());

3.4 Wall-clock Timers

在仿真是利用 ROS Clock。

  1. void callback(const ros::WallTimerEvent& event)
  2. {
  3. ...
  4. }
  5. ...
  6. ros::WallTimer timer = nh.createWallTimer(ros::WallDuration(0.1), callback);

参考:

http://wiki.ros.org/roscpp/Overview/Time
http://wiki.ros.org/roscpp/Overview/Timers
http://wiki.ros.org/roscpp_tutorials/Tutorials/Timers