ROS 中的时刻、时间间隔、定时器的定义和应用。

介绍 ROS 中的时刻、时间间隔、定时器的定义和应用。ruby

1、Time

一、时刻和间隔

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

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

其统一表示形式为:函数

int32 sec
int32 nsec

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

1.1 得到当前时间

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

时间计算起点spa

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

1.2 建立时间和间隔

浮点数形式code

ros::Time a_little_after_the_beginning(0.001);
ros::Duration five_seconds(5.0);

用两个整数表示regexp

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

1.3 时间与间隔的转化

double secs =ros::Time::now().toSec();

ros::Duration d(0.5);
secs = d.toSec();

1.4 时间与间隔的四则运算

时间和时刻的四则运算实例以下:对象

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

二、Sleeping and Rates

bool ros::Duration::sleep()get

睡眠 0.5s:

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

ros::Rate

频率 10Hz:

ros::Rate r(10); // 10 hz
while (ros::ok())
{
  ... do some work ...
  r.sleep();
}

Rate 和 Timer 的做用同样,最好用 Timer 来定时。

三、Wall Time

在模拟时,若是想要进入实际运行 wall-clock time ,能够用 ros::WallTime, ros::WallDuration, 和ros::WallRate,相似于 ros::Time, ros::Duration, 和 ros::Rate

2、Timer

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

一、定义定时器

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

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

完整定义:

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

period:定时器回调函数之间的时间间隔

:定时器回调,函数、类方法或者函数子对象

oneshot:是否只定时一次。false,就是连续定时。

二、回调特征

void callback(const ros::TimerEvent&);
struct TimerEvent
    {
      Time last_expected;                     ///< 上一回调函数应该发生的时刻
      Time last_real;                         ///< 上一回调函数实际发生的时刻

      Time current_expected;                  ///< 当前回调函数应该发生的时刻
      Time current_real;                      ///< 当前回调函数实际发生的时刻

      struct
      {
        WallDuration last_duration;           ///<包含上一回调的时间间隔(结束时间-开始时间),它始终在 `wall-clock time`
      } profile;
    };

三、回调类型

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

3.1 Functions

void callback(const ros::TimerEvent& event)
{
...
}

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

3.2 Class Methods

void Foo::callback(const ros::TimerEvent& event)
{
...
}

...
Foo foo_object;
ros::Timer timer = nh.createTimer(ros::Duration(0.1), &Foo::callback, &foo_object);

3.3 Functor Objects

class Foo
{
public:
  void operator()(const ros::TimerEvent& event)
  {
    ...
  }
};

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

3.4 Wall-clock Timers

在仿真是利用 ROS Clock。

void callback(const ros::WallTimerEvent& event)
{
  ...
}

...
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