Add timer test
This commit is contained in:
parent
d198de3958
commit
d6fec8171c
3 changed files with 91 additions and 0 deletions
47
tracetools_test/src/test_timer.cpp
Normal file
47
tracetools_test/src/test_timer.cpp
Normal file
|
@ -0,0 +1,47 @@
|
|||
#include <chrono>
|
||||
|
||||
#include "rclcpp/rclcpp.hpp"
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
|
||||
class TimerNode : public rclcpp::Node
|
||||
{
|
||||
public:
|
||||
TimerNode(rclcpp::NodeOptions options) : Node("timer_node", options)
|
||||
{
|
||||
is_done_ = false;
|
||||
timer_ = this->create_wall_timer(
|
||||
1ms,
|
||||
std::bind(&TimerNode::timer_callback, this));
|
||||
}
|
||||
|
||||
private:
|
||||
void timer_callback()
|
||||
{
|
||||
if (is_done_) {
|
||||
rclcpp::shutdown();
|
||||
} else {
|
||||
is_done_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
rclcpp::TimerBase::SharedPtr timer_;
|
||||
bool is_done_;
|
||||
};
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
rclcpp::init(argc, argv);
|
||||
|
||||
rclcpp::executors::SingleThreadedExecutor exec;
|
||||
auto timer_node = std::make_shared<TimerNode>(rclcpp::NodeOptions());
|
||||
exec.add_node(timer_node);
|
||||
|
||||
printf("spinning\n");
|
||||
exec.spin();
|
||||
|
||||
// Will actually be called inside the timer's callback
|
||||
rclcpp::shutdown();
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue