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. |
| 24 | class LoopOutputHandler { |
| 25 | public: |
Austin Schuh | bb227f8 | 2015-09-06 15:27:52 -0700 | [diff] [blame] | 26 | LoopOutputHandler( |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame^] | 27 | ::aos::EventLoop *event_loop, |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 28 | ::std::chrono::nanoseconds timeout = ::std::chrono::milliseconds(100)); |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 29 | |
| 30 | void Quit() { run_ = false; } |
| 31 | |
| 32 | void operator()(); |
| 33 | |
| 34 | protected: |
| 35 | // Read a message from the appropriate queue. |
| 36 | // This must block. |
| 37 | virtual void Read() = 0; |
| 38 | |
| 39 | // Send the output from the appropriate queue to the hardware. |
| 40 | // Read() will always be called at least once before per invocation of this. |
| 41 | virtual void Write() = 0; |
| 42 | |
| 43 | // Stop all outputs. This will be called in a separate thread (if at all). |
| 44 | // The subclass implementation should handle any appropriate logging. |
| 45 | // This will be called exactly once if Read() blocks for too long and once |
| 46 | // after Quit is called. |
| 47 | virtual void Stop() = 0; |
| 48 | |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame^] | 49 | // Returns a pointer to the event loop. |
| 50 | ::aos::EventLoop *event_loop() { return event_loop_; } |
| 51 | |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 52 | private: |
| 53 | // The thread that actually contains a timerfd to implement timeouts. The |
| 54 | // idea is to have a timerfd that is repeatedly set to the timeout expiration |
| 55 | // in the future so it will never actually expire unless it is not reset for |
| 56 | // too long. |
| 57 | // |
| 58 | // This class nicely encapsulates the raw fd and manipulating it. Its |
| 59 | // operator() loops until Quit() is called, calling Stop() on its associated |
| 60 | // LoopOutputHandler whenever the timerfd expires. |
| 61 | class Watchdog { |
| 62 | public: |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 63 | Watchdog(LoopOutputHandler *handler, ::std::chrono::nanoseconds timeout); |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 64 | |
| 65 | void operator()(); |
| 66 | |
| 67 | void Reset(); |
| 68 | |
| 69 | void Quit() { run_ = false; } |
| 70 | |
| 71 | private: |
| 72 | LoopOutputHandler *const handler_; |
| 73 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 74 | const ::std::chrono::nanoseconds timeout_; |
Austin Schuh | bb227f8 | 2015-09-06 15:27:52 -0700 | [diff] [blame] | 75 | |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 76 | ::aos::ScopedFD timerfd_; |
| 77 | |
| 78 | ::std::atomic<bool> run_{true}; |
| 79 | }; |
| 80 | |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame^] | 81 | ::aos::EventLoop *event_loop_; |
| 82 | ::aos::Fetcher<::aos::JoystickState> joystick_state_fetcher_; |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 83 | Watchdog watchdog_; |
| 84 | |
| 85 | ::std::atomic<bool> run_{true}; |
| 86 | |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 87 | ::aos::util::SimpleLogInterval no_joystick_state_ = |
Austin Schuh | 61bdc60 | 2016-12-04 19:10:10 -0800 | [diff] [blame] | 88 | ::aos::util::SimpleLogInterval(::std::chrono::milliseconds(500), INFO, |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 89 | "no joystick state -> not outputting"); |
| 90 | ::aos::util::SimpleLogInterval fake_joystick_state_ = |
Austin Schuh | 61bdc60 | 2016-12-04 19:10:10 -0800 | [diff] [blame] | 91 | ::aos::util::SimpleLogInterval(::std::chrono::milliseconds(500), DEBUG, |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 92 | "fake joystick state -> not outputting"); |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | } // namespace wpilib |
| 96 | } // namespace frc971 |
| 97 | |
| 98 | #endif // FRC971_WPILIB_LOOP_OUTPUT_HANDLER_H_ |