- An accurate timer to execute code as real-time as the operating system allows.
- Your experience may vary, but it will try its best. Test it out!
- When running it on a regular OS, it will benefit from giving the process and the thread the highest possible priority.
- Can be used to simulate hard real-time execution verifiably, so you can terminate the computation at the first detected instance of real-time violation.
- Whether this library can realize hard real-time depends on the hardware, the OS, and the task.
- If you must have reliable results, use a real-time microprocessor or a Linux system with PREEMPT_RT kernel.
- If the timing requirements are less than hard real-time, you can easily quantify the timer's performance for design.
This is a header-only library. You can include or copy the include/
folder to your project. You should use CMake and FetchContent
:
Include(FetchContent)
FetchContent_Declare(
rt_timer
URL https://github.com/cinaral/rt_timer/releases/download/v${rt_timer_VERSION}/src.zip
)
FetchContent_MakeAvailable(rt_timer)
If you want to call a function periodically until you stop it, then create a timer and a timer thread, and start the thread:
/** create a timer thread to call the action periodically */
rt_timer::Timer<Action_T> action_timer(timer_period, action, &Action_T::fun);
rt_timer::TimerThread<Action_T> action_thread(action_timer);
/** start the timer thread */
action_thread.start();
If you want to run the thread only for a specified duration, you can use:
/** start the timer thread for timed duration */
action_thread.run_for(std::chrono::seconds(duration));
If you do not want to use a timer thread, you can instead create a timer object and call it periodically:
/** create a timer thread to call the action periodically */
rt_timer::Timer<Action_T> action_timer(timer_period, action, &Action::fun);
while(running) {
action_timer.check();
}
Sample the timer's performance periodically using another time thread:
/** create a second timer thread to sample the previous timer periodically */
Sampler sampler(action_timer);
rt_timer::Timer<Sampler> sampler_timer(sample_period, sampler, &Sampler::sample);
rt_timer::TimerThread<Sampler> sampler_thread(sampler_timer);
sampler_thread.start();
action_thread.stop();
sampler_thread.stop();
You can find the complete example in rt_timer-example.cpp.
- 100 hz does not work??
- Add/improve
set_process_priority()
for more platforms - A benchmark to identify the highest possible call frequency for a given task, hardware, and platform.
- Test it out on a real-time microprocessor.