Ros timer

ROS introduced in time, the time interval, the definition and application timers.

A, Time

1, and the time interval

ROS has two types of time and duration, with a corresponding ros :: Time and ros :: Duration class.

  • time represents the time
  • duration represents the time interval

Its unified representation as:

int32 sec
int32 nsec

ROS may be provided a start node to an analog clock. Unlike plateau time, you can use time routine roscpp to get the current time, which can be seamlessly connected and the simulation time, wall-clock time.

1.1 to get the current time

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

Time calculation starting point

When analog time, when /clockthe node receives the first message, now () returns time 0, then the client does not know the clock time.

1.2 create time and space

Floating-point number

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

Represented by two integers

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

 

1.3 Transformation time interval

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

1.4 the time interval four operations

Examples of four arithmetic operations and a time period as follows:

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;

 

2、Sleeping and Rates

bool ros::Duration::sleep()

Sleep 0.5s:

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

ros::Rate

Frequency 10Hz:

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

Rate Timer and the effect, it is best to use a timing Timer.

3、Wall Time

In the simulation, if you want to run into the actual wall-clock time, you can use ros :: WallTime, ros :: WallDuration, and ros :: WallRate, similar to ros :: Time, ros :: Duration, and ros :: Rate

Two, Timer

Timers can not replace the real-time threads / core, they are not only useful for hard real-time requirements of things.

1, defined timer

method: ros::NodeHandle::createTimer()

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

Full definition

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

 

period: the time interval between the timer callback function

: Timer callback function, or a method of class subobject

oneshot: whether the timing only once. false, the timing is continuous.

2, the callback feature

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;
    };

3, the callback type

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

In the simulation is the use of ROS Clock.

void callback(const ros::WallTimerEvent& event)
{
  ...
}
 
...
ros::WallTimer timer = nh.createWallTimer(ros::WallDuration(0.1), callback);

reference:

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

Guess you like

Origin blog.csdn.net/lxlong89940101/article/details/90705117