diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2016-09-27 15:01:27 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2016-09-27 15:58:31 +0200 |
commit | 4da5809f8901066ab912b84f08b110e55d89b067 (patch) | |
tree | 960ee643831ccb62b3c4010109243be566ad6a98 /logic/tracker.cpp | |
parent | 946d80a09181363ea44b33c6b633a6dd9b8a1a0c (diff) |
logic/tracker: try harder to keep up constant Hz
Now time error is accumulated rather than using only the last
iteration's error.
Diffstat (limited to 'logic/tracker.cpp')
-rw-r--r-- | logic/tracker.cpp | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/logic/tracker.cpp b/logic/tracker.cpp index 84d3afdb..bfc95eed 100644 --- a/logic/tracker.cpp +++ b/logic/tracker.cpp @@ -13,6 +13,7 @@ */ #include "compat/sleep.hpp" +#include "compat/util.hpp" #include "tracker.h" @@ -30,7 +31,8 @@ constexpr double Tracker::d2r; Tracker::Tracker(Mappings &m, SelectedLibraries &libs, TrackLogger &logger) : m(m), libs(libs), - logger(logger) + logger(logger), + backlog_time(0) { set(f_center, s.center_at_startup); } @@ -385,13 +387,21 @@ void Tracker::run() using std::min; const long elapsed_usecs = t.elapsed_usecs(); - const long sleep_us = const_sleep_us * 2 - elapsed_usecs; - const unsigned sleep_time = unsigned(max(1l, min(const_sleep_us * 4, max(1l, (sleep_us + 200l)/1000l)))); + backlog_time += elapsed_usecs - const_sleep_us; + + if (std::fabs(backlog_time) > 10000l * 1000) + { + qDebug() << "tracker: backlog interval overflow" << backlog_time; + backlog_time = 0; + } + + const unsigned sleep_time = unsigned(std::round(clamp((const_sleep_us - backlog_time)/1000., 0., const_sleep_us*3/1000.))); t.start(); - portable::sleep(unsigned(max(1u, sleep_time))); + if (sleep_time > 0) + portable::sleep(sleep_time); } { |