-
Notifications
You must be signed in to change notification settings - Fork 3
Handling Time
Synchronizing and keeping track of time is critical for real-time mechatronics hardware control and data acquisition. MEL provides three classes to assist you in this process:
#include <MEL/Core/Time.hpp>
#include <MEL/Core/Clock.hpp>
#include <MEL/Core/Timer.hpp>
The Time
class represents a time point or duration. Times
are constructed using the free functions seconds()
, milliseconds()
, and microseconds()
. They can be used with basic arithmetic and logical operations:
Time t1 = seconds(10);
Time t2 = milliseconds(1234);
Time t3 = t1 + t2;
if (t1 > t2)
print(t3); // 11.234 s
The advantage to using a Time
over something like double t3 = 11.234
is that the units are automatically kept track of. If you do need to convert Times
to number types, the class provides conversions for you:
double t1_s = t1.as_seconds();
int32 t2_ms = t2.as_milliseconds();
int64 t3_us = t3.as_microseconds();
A clock is an object that measures elapsed time. It begins counts upward from Time::Zero
until the member function restart()
is call, at which point it restarts counting upward from Time::Zero
:
Clock clock;
sleep(seconds(2));
Time time1 = clock.get_elapsed_time();
sleep(seconds(3));
Time time2 = clock.restart();
sleep(seconds(4));
Time time3 = clock.get_elapsed_time();
print(time1, time2, time3); // 2.0006 s 5.00096 s 4.00012 s