[Documentation] [TitleIndex] [WordIndex

(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Sleep

Description: Classes that put the thread to sleep for fixed or periodic intervals.

Keywords: sleep

Tutorial Level: INTERMEDIATE

Contents

  1. Sleep
  2. Snooze

Sleep

Some simple sleep classes. They can be used directly or preconfigured.

   1 Duration duration(1,300000000);
   2 Sleep sleep;
   3 MilliSleep sleep_ms;
   4 MicroSleep sleep_us;
   5 NanoSleep sleep_ns;
   6 
   7 // Direct sleep commands
   8 sleep(duration);
   9 sleep(3);
  10 sleep_ms(3500);
  11 sleep_us(500000);
  12 sleep_ns(500000000);
  13 
  14 // Preconfigured (i.e. use last configured command)
  15 MilliSleep pc_sleep_ms(duration);
  16 sleep_ms(); // uses the last configuration
  17 pc_sleep_ms(); // uses the constructor configuration
  18 

Snooze

Snooze is a periodic sleeper, useful in control loops where you want to exactly control the time taken by each loop. Using a regular sleep function to do this can cause some time drift problems (refer to the snooze class documentation for more detail). The snooze class gets around this by setting its periodic timestamps off the absolute clock time rather than calculating relative time splits.

   1 Snooze snooze(Duration(0,20000000); // 20ms snooze
   2 // Some preliminaries
   3 snooze.initialise(); // make sure the snoozer is sync'ed with the current time.
   4 while (1) {
   5     // do some work
   6     snooze();
   7 }

Note, this can be problematic if the periodic timestamps get behind the current time. To remedy this, by default the class validates the timestamp at each check and syncs it with the current time if it starts to lag behind (can easily be caused by the system scheduler knocking it out of whack in favour of another process). This can be manually turned off in the constructor if you're confident of the timing processes and this will marginally reduce the snoozer's latency.


2020-01-25 12:37