Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 1 | #ifndef FRC971_WPILIB_LOOP_OUTPUT_HANDLER_H_ |
| 2 | #define FRC971_WPILIB_LOOP_OUTPUT_HANDLER_H_ |
| 3 | |
Brian Silverman | 425492b | 2015-12-30 15:23:55 -0800 | [diff] [blame] | 4 | #include <atomic> |
Austin Schuh | 61bdc60 | 2016-12-04 19:10:10 -0800 | [diff] [blame] | 5 | #include <chrono> |
Brian Silverman | 425492b | 2015-12-30 15:23:55 -0800 | [diff] [blame] | 6 | |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 7 | #include "aos/events/event-loop.h" |
| 8 | #include "aos/robot_state/robot_state.q.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 9 | #include "aos/scoped/scoped_fd.h" |
| 10 | #include "aos/time/time.h" |
| 11 | #include "aos/util/log_interval.h" |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 12 | |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 13 | namespace frc971 { |
| 14 | namespace wpilib { |
| 15 | |
| 16 | // Handles sending the output from a single control loop to the hardware. |
| 17 | // |
| 18 | // This class implements stopping motors when no new values are received for too |
| 19 | // long efficiently. |
| 20 | // |
| 21 | // The intended use is to have a subclass for each loop which implements the |
| 22 | // pure virtual methods and is then run in a separate thread. The operator() |
| 23 | // loops writing values until Quit() is called. |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 24 | template <typename T> |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 25 | class LoopOutputHandler { |
| 26 | public: |
Austin Schuh | bb227f8 | 2015-09-06 15:27:52 -0700 | [diff] [blame] | 27 | LoopOutputHandler( |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 28 | ::aos::EventLoop *event_loop, const ::std::string &name, |
| 29 | ::std::chrono::nanoseconds timeout = ::std::chrono::milliseconds(100)) |
| 30 | : event_loop_(event_loop), timeout_(timeout) { |
| 31 | event_loop_->SetRuntimeRealtimePriority(30); |
| 32 | // TODO(austin): Name thread. |
| 33 | event_loop_->MakeWatcher(name, [this](const T &t) { |
| 34 | // Push the watchdog out a bit further. |
| 35 | timer_handler_->Setup(event_loop_->monotonic_now() + timeout_); |
| 36 | Write(t); |
| 37 | }); |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 38 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 39 | // TODO(austin): Set name. |
| 40 | timer_handler_ = event_loop_->AddTimer([this]() { Stop(); }); |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 41 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 42 | event_loop_->OnRun([this, timeout]() { |
| 43 | timer_handler_->Setup(event_loop_->monotonic_now() + timeout_); |
| 44 | }); |
| 45 | } |
| 46 | |
| 47 | void Quit() { event_loop_->Exit(); } |
| 48 | |
| 49 | // Note, all subclasses should call Stop. |
| 50 | virtual ~LoopOutputHandler() {} |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 51 | |
| 52 | protected: |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 53 | // Send the output from the appropriate queue to the hardware. |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 54 | virtual void Write(const T &t) = 0; |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 55 | |
| 56 | // Stop all outputs. This will be called in a separate thread (if at all). |
| 57 | // The subclass implementation should handle any appropriate logging. |
| 58 | // This will be called exactly once if Read() blocks for too long and once |
| 59 | // after Quit is called. |
| 60 | virtual void Stop() = 0; |
| 61 | |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 62 | // Returns a pointer to the event loop. |
| 63 | ::aos::EventLoop *event_loop() { return event_loop_; } |
| 64 | |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 65 | private: |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 66 | ::aos::EventLoop *event_loop_; |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 67 | const ::std::chrono::nanoseconds timeout_; |
| 68 | ::aos::TimerHandler *timer_handler_; |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | } // namespace wpilib |
| 72 | } // namespace frc971 |
| 73 | |
| 74 | #endif // FRC971_WPILIB_LOOP_OUTPUT_HANDLER_H_ |