Skip to content

Commit

Permalink
add time example file
Browse files Browse the repository at this point in the history
  • Loading branch information
epezent committed Feb 26, 2019
1 parent 1e87e6c commit 3804b80
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mel_example(comms_server)
mel_example(virtual_daq)
mel_example(serial)
mel_example(csv)
# mel_example(time)
mel_example(time)

# windows only examples
if(WIN32)
Expand Down
83 changes: 83 additions & 0 deletions examples/ex_time.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// MIT License
//
// MEL - Mechatronics Engine & Library
// Copyright (c) 2019 Mechatronics and Haptic Interfaces Lab - Rice University
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// Author(s): Evan Pezent (epezent@rice.edu)

#include <MEL/Core/Time.hpp>
#include <MEL/Core/Clock.hpp>
#include <MEL/Core/Timer.hpp>
#include <MEL/Core/Console.hpp> // for print
#include <MEL/Utility/System.hpp> // for sleep

using namespace mel;

int main(int argc, char const *argv[])
{

// Time

Time t1 = seconds(10);
Time t2 = milliseconds(10);
Time t3 = t1 + t2;
if (t1 > t2)
print(t3);

double t1_s = t1.as_seconds();
int32 t2_ms = t2.as_milliseconds();
int64 t3_us = t3.as_microseconds();

// Frequency

Frequency f1 = hertz(1000);
Frequency f2 = t2.to_frequency();
print(f1,f2);

// Clock

Clock clock;
sleep(seconds(1));
Time time1 = clock.get_elapsed_time();
sleep(seconds(1));
Time time2 = clock.restart();
sleep(seconds(1));
Time time3 = clock.get_elapsed_time();
print(time1, time2, time3);

// Timer

Timer timer1(seconds(1));
time1 = timer1.wait();
time2 = timer1.wait();
time3 = timer1.wait();
print(time1, time2, time3);

Timer hybrid_timer(milliseconds(10), Timer::WaitMode::Hybrid);
print(hybrid_timer.wait());

// Time loops

Time t = Time::Zero;
Timer timer(hertz(1000));
while (t < seconds(60)) {
// code which executes in less than one millisecond
t = timer.wait();
}





return 0;
}

0 comments on commit 3804b80

Please sign in to comment.