mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
timer_handler.hpp
1#ifndef MRS_TIMER_HPP
2#define MRS_TIMER_HPP
3
4#ifndef MRS_TIMER_H
5#include <mrs_lib/timer_handler.h>
6#endif
7
8#include <mutex>
9
10namespace mrs_lib
11{
12
13 // | ----------------------- ThreadTimer ---------------------- |
14
15 /* class ThreadTimer::Impl //{ */
16
18 {
19 public:
20 Impl(const rclcpp::Node::SharedPtr& node, const std::function<void()>& callback, const rclcpp::Rate& rate, const bool oneshot);
21
22 ~Impl();
23
24 void start();
25 void stop();
26 void setPeriod(const rclcpp::Duration& duration);
27 void setCallback(const std::function<void()>& callback);
28
29 friend class ThreadTimer;
30
31 // to keep rule of five since we have a custom destructor
32 Impl(const Impl&) = delete;
33 Impl(Impl&&) = delete;
34 Impl& operator=(const Impl&) = delete;
35 Impl& operator=(Impl&&) = delete;
36
37 private:
38 std::thread thread_;
39 std::function<void()> callback_;
40
41 rclcpp::Node::SharedPtr node_;
42
43 bool oneshot_;
44
45 bool breakableSleep(const rclcpp::Time& until);
46 void threadFcn();
47
48 std::mutex mutex_wakeup_;
49 std::condition_variable wakeup_cond_;
50 std::recursive_mutex mutex_state_;
51 bool running_;
52 std::chrono::nanoseconds delay_dur_;
53 bool ending_;
54 rclcpp::Time next_expected_;
55 };
56
57 //}
58
59} // namespace mrs_lib
60
61#endif // MRS_TIMER_HPP
Custom thread-based Timers with the same interface as mrs_lib::ROSTimer.
Definition timer_handler.h:172
Definition timer_handler.hpp:18
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition attitude_converter.h:24