machines-in-motion / real_time_tools

BSD 3-Clause "New" or "Revised" License
9 stars 6 forks source link

Spinner class is drifting in time #14

Open jviereck opened 4 years ago

jviereck commented 4 years ago

The current version of the spinner class 1 can have a slight time drift between ticks. That means, if you setup 1000 hz frequency and you call the spinner 1000 times, then you might need significantly more than 1 seconds.

This is problematic as it means we might not getting stable 1 kHz loop when using dynamic graph manager (which uses the spinner classs).

The current implementation looks like this

  void Spinner::spin() {
    Timer::sleep_until_sec(next_date_sec_);   // (1)
    next_date_sec_ = Timer::get_current_time_sec() + period_sec_; // (2)
  }

The drift happens because Timer::get_current_time_sec() is not necessarly directly after Timer::sleep_until_sec. Also, Timer::sleep_until_sec might take a bit longer than desired.

Proposal is to fix the current implementation and avoid the drift.

inputs? @vincentberenz @MaximilienNaveau @righetti @mwuethri @luator