add WallTime và WallTimer
This commit is contained in:
229
examples/walltimer_example.cpp
Normal file
229
examples/walltimer_example.cpp
Normal file
@@ -0,0 +1,229 @@
|
||||
/*********************************************************************
|
||||
* WallTimer Example
|
||||
*
|
||||
* Demonstrates various uses of robot::WallTimer
|
||||
*********************************************************************/
|
||||
|
||||
#include <robot/wall_timer.h>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
|
||||
// Example 1: Simple periodic callback
|
||||
void simpleCallback(const robot::WallTimerEvent& event)
|
||||
{
|
||||
std::cout << "[Simple] Timer fired at wall time: "
|
||||
<< event.current_real.toSec() << std::endl;
|
||||
std::cout << "[Simple] Time since last callback: "
|
||||
<< event.last_duration.toSec() << " seconds" << std::endl;
|
||||
}
|
||||
|
||||
// Example 2: Performance monitoring
|
||||
class PerformanceMonitor
|
||||
{
|
||||
public:
|
||||
void start()
|
||||
{
|
||||
timer_ = std::make_unique<robot::WallTimer>(
|
||||
robot::WallDuration(1.0), // Check every 1 second
|
||||
[this](const robot::WallTimerEvent& event) {
|
||||
this->monitor(event);
|
||||
},
|
||||
false, // Repeating
|
||||
true // Auto-start
|
||||
);
|
||||
}
|
||||
|
||||
void monitor(const robot::WallTimerEvent& event)
|
||||
{
|
||||
static int count = 0;
|
||||
count++;
|
||||
|
||||
double actual_interval = event.last_duration.toSec();
|
||||
double expected_interval = 1.0;
|
||||
|
||||
std::cout << "[Monitor] Check #" << count
|
||||
<< " - Expected: " << expected_interval
|
||||
<< "s, Actual: " << actual_interval << "s";
|
||||
|
||||
if (actual_interval > expected_interval * 1.1)
|
||||
{
|
||||
std::cout << " (DRIFT DETECTED!)";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
void stop()
|
||||
{
|
||||
if (timer_)
|
||||
{
|
||||
timer_->stop();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<robot::WallTimer> timer_;
|
||||
};
|
||||
|
||||
// Example 3: One-shot delayed action
|
||||
void delayedAction(const robot::WallTimerEvent& event)
|
||||
{
|
||||
std::cout << "[Delayed] Action executed after delay!" << std::endl;
|
||||
std::cout << "[Delayed] Executed at wall time: "
|
||||
<< event.current_real.toSec() << std::endl;
|
||||
}
|
||||
|
||||
// Example 4: Dynamic period adjustment
|
||||
class DynamicTimer
|
||||
{
|
||||
public:
|
||||
void start()
|
||||
{
|
||||
timer_ = std::make_unique<robot::WallTimer>(
|
||||
robot::WallDuration(1.0), // Start with 1 second
|
||||
[this](const robot::WallTimerEvent& event) {
|
||||
this->callback(event);
|
||||
},
|
||||
false, // Repeating
|
||||
true // Auto-start
|
||||
);
|
||||
|
||||
// After 3 seconds, change period to 0.5 seconds
|
||||
adjust_thread_ = std::thread([this]() {
|
||||
robot::WallDuration(3.0).sleep();
|
||||
std::cout << "[Dynamic] Changing period to 0.5 seconds..." << std::endl;
|
||||
timer_->setPeriod(robot::WallDuration(0.5), true);
|
||||
});
|
||||
}
|
||||
|
||||
void callback(const robot::WallTimerEvent& event)
|
||||
{
|
||||
static int count = 0;
|
||||
count++;
|
||||
std::cout << "[Dynamic] Callback #" << count
|
||||
<< " at " << event.current_real.toSec() << std::endl;
|
||||
}
|
||||
|
||||
void stop()
|
||||
{
|
||||
if (timer_)
|
||||
{
|
||||
timer_->stop();
|
||||
}
|
||||
if (adjust_thread_.joinable())
|
||||
{
|
||||
adjust_thread_.join();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<robot::WallTimer> timer_;
|
||||
std::thread adjust_thread_;
|
||||
};
|
||||
|
||||
// Example 5: Timer with member function
|
||||
class TimerClass
|
||||
{
|
||||
public:
|
||||
void startTimer()
|
||||
{
|
||||
timer_ = std::make_unique<robot::WallTimer>(
|
||||
robot::WallDuration(0.5), // 2 Hz
|
||||
&TimerClass::timerCallback,
|
||||
this,
|
||||
false, // Repeating
|
||||
true // Auto-start
|
||||
);
|
||||
}
|
||||
|
||||
void timerCallback(const robot::WallTimerEvent& event)
|
||||
{
|
||||
static int count = 0;
|
||||
count++;
|
||||
std::cout << "[Class] Member function callback #" << count
|
||||
<< " at " << event.current_real.toSec() << std::endl;
|
||||
}
|
||||
|
||||
void stopTimer()
|
||||
{
|
||||
if (timer_)
|
||||
{
|
||||
timer_->stop();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<robot::WallTimer> timer_;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << "=== WallTimer Examples ===" << std::endl;
|
||||
std::cout << std::endl;
|
||||
|
||||
// Example 1: Simple timer
|
||||
std::cout << "Example 1: Simple periodic callback" << std::endl;
|
||||
{
|
||||
robot::WallTimer timer(
|
||||
robot::WallDuration(1.0),
|
||||
simpleCallback,
|
||||
false, // Repeating
|
||||
true // Auto-start
|
||||
);
|
||||
|
||||
robot::WallDuration(3.0).sleep(); // Run for 3 seconds
|
||||
timer.stop();
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
// Example 2: Performance monitoring
|
||||
std::cout << "Example 2: Performance monitoring" << std::endl;
|
||||
{
|
||||
PerformanceMonitor monitor;
|
||||
monitor.start();
|
||||
|
||||
robot::WallDuration(5.0).sleep(); // Run for 5 seconds
|
||||
monitor.stop();
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
// Example 3: One-shot timer
|
||||
std::cout << "Example 3: One-shot delayed action" << std::endl;
|
||||
{
|
||||
robot::WallTimer timer(
|
||||
robot::WallDuration(2.0),
|
||||
delayedAction,
|
||||
true, // One-shot
|
||||
true // Auto-start
|
||||
);
|
||||
|
||||
robot::WallDuration(3.0).sleep(); // Wait for it to fire
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
// Example 4: Dynamic period adjustment
|
||||
std::cout << "Example 4: Dynamic period adjustment" << std::endl;
|
||||
{
|
||||
DynamicTimer dynamic;
|
||||
dynamic.start();
|
||||
|
||||
robot::WallDuration(8.0).sleep(); // Run for 8 seconds
|
||||
dynamic.stop();
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
// Example 5: Member function callback
|
||||
std::cout << "Example 5: Member function callback" << std::endl;
|
||||
{
|
||||
TimerClass timer_obj;
|
||||
timer_obj.startTimer();
|
||||
|
||||
robot::WallDuration(3.0).sleep(); // Run for 3 seconds
|
||||
timer_obj.stopTimer();
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "All examples completed!" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user