Skip to content

Commit 68eb500

Browse files
committed
Timer get_elapsed_time() now returns ACTUAL time. Added time example
1 parent bbb7dbf commit 68eb500

File tree

9 files changed

+37
-28
lines changed

9 files changed

+37
-28
lines changed

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mel_example(comms_server)
2020
mel_example(virtual_daq)
2121
mel_example(serial)
2222
mel_example(csv)
23+
mel_example(time)
2324

2425
# windows only examples
2526
if(WIN32)

examples/ex_ctrl_c_handling.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919

2020
using namespace mel;
2121

22-
ctrl_bool flag(false);
22+
ctrl_bool g_stop_flag(false);
23+
2324
bool my_handler(CtrlEvent event) {
2425
if (event == CtrlEvent::CtrlC)
2526
print("Ctrl-C Pressed");
2627
else if (event == CtrlEvent::CtrlQuit) {
2728
print("Ctrl-Break or Ctrl-\\ Pressed");
28-
flag = true;
29+
g_stop_flag = true;
2930
}
3031
// ... check other Ctrl values as needed
3132
return 1;
@@ -34,7 +35,7 @@ bool my_handler(CtrlEvent event) {
3435
int main() {
3536
print("Press Ctrl+C to continue, or Ctrl+Break to exit");
3637
register_ctrl_handler(my_handler);
37-
while (!flag) {
38+
while (!g_stop_flag) {
3839
}
3940
return 0;
4041
}

examples/ex_haptic_paddle.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ int main(int argc, char* argv[]) {
353353

354354
// compute torque
355355
if (input.count("t") > 0)
356-
torque = tunnel(hp[0].get_position(), filtered_vel, timer.get_elapsed_time_actual());
356+
torque = tunnel(hp[0].get_position(), filtered_vel, timer.get_elapsed_time());
357357
else if (input.count("w") > 0)
358358
torque = wall(hp[0].get_position(), filtered_vel);
359359
else if (input.count("n") > 0)

examples/ex_math.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int main() {
4141
std::vector<double> data(6);
4242

4343
// loop
44-
while (timer.get_elapsed_time_actual() < seconds(5)) {
44+
while (timer.get_elapsed_time() < seconds(5)) {
4545
// get current time in seconds
4646
double t = timer.get_elapsed_time().as_seconds();
4747
data[0] = t;

examples/ex_options.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ int main(int argc, char* argv[]) {
3030
// add options
3131
// options can be short, long, or both (short must come first)
3232
// for example: options.exe -e OR options.exe --evan
33-
options.add_options()("e,evan", "Prints a message from Evan.")(
34-
"c,craig", "Prints a message from Craig.")(
35-
"h,help", "Prints helpful information.")(
36-
"i,integer", "Gets an integer from user.", value<int>());
33+
options.add_options()
34+
("e,evan", "Prints a message from Evan.")
35+
("c,craig", "Prints a message from Craig.")
36+
("h,help", "Prints helpful information.")
37+
("i,integer", "Gets an integer from user.", value<int>())
38+
("d,double", "Gets a double from user.", value<double>());
3739

3840
// parse options
3941
auto result = options.parse(argc, argv);
@@ -43,7 +45,7 @@ int main(int argc, char* argv[]) {
4345
if (result.count("evan") > 0)
4446
print("Hello, my name is Evan!");
4547

46-
if (result.count("craig") > 0)
48+
if (result.count("c") > 0)
4749
print("Hello, my name is Craig!");
4850

4951
if (result.count("help") > 0)
@@ -54,5 +56,10 @@ int main(int argc, char* argv[]) {
5456
print("You entered " + stringify(i));
5557
}
5658

59+
if (result.count("d") > 0) {
60+
double d = result["d"].as<double>();
61+
print("You entered " + stringify(d));
62+
}
63+
5764
return 0;
5865
}

examples/ex_q2usb.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ int main() {
6666
// create 10 Hz Timer
6767
Timer timer(milliseconds(100));
6868
// start encoder loop
69-
while (timer.get_elapsed_time_actual() < seconds(5) && !stop) {
69+
while (timer.get_elapsed_time() < seconds(5) && !stop) {
7070
q2.update_input();
7171
print(q2.encoder.get_value(0));
7272
timer.wait();
@@ -82,10 +82,10 @@ int main() {
8282
Waveform wave(Waveform::Sin, seconds(0.25), 5.0);
8383
// start analog loopback loop
8484
timer.restart();
85-
while (timer.get_elapsed_time_actual() < seconds(5) && !stop) {
85+
while (timer.get_elapsed_time() < seconds(5) && !stop) {
8686
q2.update_input();
8787
print(q2.AI.get_value(0));
88-
double voltage = wave.evaluate(timer.get_elapsed_time_actual());
88+
double voltage = wave.evaluate(timer.get_elapsed_time());
8989
q2.AO.set_value(0, voltage);
9090
q2.update_output();
9191
timer.wait();
@@ -102,7 +102,7 @@ int main() {
102102
Logic signal = High;
103103
// start analog loopback loop
104104
timer.restart();
105-
while (timer.get_elapsed_time_actual() < seconds(5) && !stop) {
105+
while (timer.get_elapsed_time() < seconds(5) && !stop) {
106106
q2.DIO.update();
107107
print((int)q2.DIO[0].get_value());
108108
signal = (Logic)(High - signal);
@@ -122,7 +122,7 @@ int main() {
122122
options.set_led_mode(QuanserOptions::LedMode::User);
123123
q2.set_options(options);
124124
timer.restart();
125-
while (timer.get_elapsed_time_actual() < seconds(10) && !stop) {
125+
while (timer.get_elapsed_time() < seconds(10) && !stop) {
126126
if (Keyboard::is_key_pressed(Key::L))
127127
q2.set_led(High);
128128
else

examples/ex_q8usb.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ int main() {
7777
// create 10 Hz Timer
7878
Timer timer(milliseconds(100));
7979
// start encoder loop
80-
while (timer.get_elapsed_time_actual() < seconds(5) && !stop) {
80+
while (timer.get_elapsed_time() < seconds(5) && !stop) {
8181
q8.update_input();
8282
print(q8.encoder[0].get_value());
8383
timer.wait();
@@ -94,10 +94,10 @@ int main() {
9494
Waveform wave(Waveform::Sin, seconds(0.25), 5.0);
9595
// start analog loopback loop
9696
timer.restart();
97-
while (timer.get_elapsed_time_actual() < seconds(5) && !stop) {
97+
while (timer.get_elapsed_time() < seconds(5) && !stop) {
9898
q8.update_input();
9999
print(q8.AI.get_value(0));
100-
double voltage = wave(timer.get_elapsed_time_actual());
100+
double voltage = wave(timer.get_elapsed_time());
101101
q8.AO.set_value(0, voltage);
102102
q8.update_output();
103103
timer.wait();
@@ -113,7 +113,7 @@ int main() {
113113
Logic signal = High;
114114
// start analog loopback loop
115115
timer.restart();
116-
while (timer.get_elapsed_time_actual() < seconds(5) && !stop) {
116+
while (timer.get_elapsed_time() < seconds(5) && !stop) {
117117
q8.update_input();
118118
print((bool)q8.DI.get_value(0));
119119
signal = (Logic)(High - signal);
@@ -139,7 +139,7 @@ int main() {
139139
// start watchdog
140140
q8.watchdog.start();
141141
timer.restart();
142-
while (timer.get_elapsed_time_actual() < seconds(5) && !stop) {
142+
while (timer.get_elapsed_time() < seconds(5) && !stop) {
143143
// simulate a missed deadline if W pressed
144144
if (Keyboard::is_key_pressed(Key::W)) {
145145
print("The program missed it's deadline!");

include/MEL/Core/Timer.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ class Timer {
5858
/// Restarts the Timer and returns the elapsed time
5959
Time restart();
6060

61-
/// Waits the Timer and returns the ideal elapsed time after the wait completes
61+
/// Waits the Timer and returns the actual elapsed time after the wait completes
6262
Time wait();
6363

64-
/// Gets the ideal elapsed time since construction or last call to
65-
/// restart(). Equal to the tick count times the Timer period
64+
/// Gets the actual elapsed time since construction or last call to restart().
6665
Time get_elapsed_time() const;
6766

68-
/// Gets the actual elapsed time since construction or last call to restart().
69-
Time get_elapsed_time_actual() const;
67+
/// Gets the ideal elapsed time since construction or last call to
68+
/// restart(). Equal to the tick count times the Timer period
69+
Time get_elapsed_time_ideal() const;
7070

7171
/// Gets the elapsed number of ticks since construction or the last call to restart().
7272
int64 get_elapsed_ticks() const;

src/MEL/Core/Timer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ Time Timer::wait() {
6969
return get_elapsed_time();
7070
}
7171

72-
Time Timer::get_elapsed_time_actual() const {
72+
Time Timer::get_elapsed_time() const {
7373
return clock_.get_elapsed_time();
7474
}
7575

76-
Time Timer::get_elapsed_time() const {
76+
Time Timer::get_elapsed_time_ideal() const {
7777
return period_ * ticks_;
7878
}
7979

@@ -98,7 +98,7 @@ Time Timer::get_period() const {
9898
}
9999

100100
double Timer::get_wait_ratio() const {
101-
return waited_.as_seconds() / get_elapsed_time_actual().as_seconds();
101+
return waited_.as_seconds() / get_elapsed_time().as_seconds();
102102
}
103103

104104
void Timer::set_acceptable_miss_rate(double rate) {

0 commit comments

Comments
 (0)