ros2_tracing/tracetools_test/src/test_timer.cpp

49 lines
908 B
C++
Raw Normal View History

2019-06-05 15:35:46 +02:00
#include <memory>
2019-06-03 11:36:16 +02:00
#include <chrono>
#include "rclcpp/rclcpp.hpp"
using namespace std::chrono_literals;
class TimerNode : public rclcpp::Node
{
public:
2019-06-05 15:35:46 +02:00
explicit TimerNode(rclcpp::NodeOptions options)
: Node("timer_node", options)
{
is_done_ = false;
timer_ = this->create_wall_timer(
1ms,
std::bind(&TimerNode::timer_callback, this));
}
2019-06-03 11:36:16 +02:00
private:
2019-06-05 15:35:46 +02:00
void timer_callback()
{
if (is_done_) {
rclcpp::shutdown();
} else {
is_done_ = true;
2019-06-03 11:36:16 +02:00
}
2019-06-05 15:35:46 +02:00
}
2019-06-03 11:36:16 +02:00
2019-06-05 15:35:46 +02:00
rclcpp::TimerBase::SharedPtr timer_;
bool is_done_;
2019-06-03 11:36:16 +02:00
};
2019-06-05 15:35:46 +02:00
int main(int argc, char * argv[])
2019-06-03 11:36:16 +02:00
{
2019-06-05 15:35:46 +02:00
rclcpp::init(argc, argv);
2019-06-03 11:36:16 +02:00
2019-06-05 15:35:46 +02:00
rclcpp::executors::SingleThreadedExecutor exec;
auto timer_node = std::make_shared<TimerNode>(rclcpp::NodeOptions());
exec.add_node(timer_node);
2019-06-03 11:36:16 +02:00
2019-06-05 15:35:46 +02:00
printf("spinning\n");
exec.spin();
2019-06-03 11:36:16 +02:00
2019-06-05 15:35:46 +02:00
// Will actually be called inside the timer's callback
rclcpp::shutdown();
return 0;
2019-06-03 11:36:16 +02:00
}