brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #ifndef AOS_CONTROL_LOOP_CONTROL_LOOP_H_ |
| 2 | #define AOS_CONTROL_LOOP_CONTROL_LOOP_H_ |
| 3 | |
Austin Schuh | b58ceb6 | 2017-02-05 14:21:57 -0800 | [diff] [blame] | 4 | #include <atomic> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 5 | #include <cstring> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 6 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 7 | #include "aos/events/event_loop.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 8 | #include "aos/time/time.h" |
| 9 | #include "aos/type_traits/type_traits.h" |
| 10 | #include "aos/util/log_interval.h" |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 11 | #include "frc971/input/joystick_state_generated.h" |
| 12 | #include "frc971/input/robot_state_generated.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 13 | |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 14 | namespace frc971 { |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 15 | namespace controls { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 16 | |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 17 | // Control loops run this often, "starting" at time 0. |
Austin Schuh | 214e9c1 | 2016-11-25 17:26:20 -0800 | [diff] [blame] | 18 | constexpr ::std::chrono::nanoseconds kLoopFrequency = |
| 19 | ::std::chrono::milliseconds(5); |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 20 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 21 | // Provides helper methods to assist in writing control loops. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 22 | // It will then call the RunIteration method every cycle that it has enough |
| 23 | // valid data for the control loop to run. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 24 | template <class GoalType, class PositionType, class StatusType, |
| 25 | class OutputType> |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 26 | class ControlLoop { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 27 | public: |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 28 | ControlLoop(aos::EventLoop *event_loop, const ::std::string &name) |
Austin Schuh | d3cd699 | 2019-01-27 22:44:07 -0800 | [diff] [blame] | 29 | : event_loop_(event_loop), name_(name) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 30 | output_sender_ = event_loop_->MakeSender<OutputType>(name_); |
| 31 | status_sender_ = event_loop_->MakeSender<StatusType>(name_); |
| 32 | goal_fetcher_ = event_loop_->MakeFetcher<GoalType>(name_); |
| 33 | robot_state_fetcher_ = event_loop_->MakeFetcher<::aos::RobotState>("/aos"); |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame] | 34 | joystick_state_fetcher_ = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 35 | event_loop_->MakeFetcher<::aos::JoystickState>("/aos"); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 36 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 37 | event_loop_->MakeWatcher(name_, [this](const PositionType &position) { |
| 38 | this->IteratePosition(position); |
| 39 | }); |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | const ::aos::RobotState &robot_state() const { return *robot_state_fetcher_; } |
| 43 | bool has_joystick_state() const { return joystick_state_fetcher_.get(); } |
| 44 | const ::aos::JoystickState &joystick_state() const { |
| 45 | return *joystick_state_fetcher_; |
Austin Schuh | a1654ed | 2019-01-27 17:24:54 -0800 | [diff] [blame] | 46 | } |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 47 | |
| 48 | // Returns true if all the counters etc in the sensor data have been reset. |
| 49 | // This will return true only a single time per reset. |
| 50 | bool WasReset() { |
| 51 | if (reset_) { |
| 52 | reset_ = false; |
| 53 | return true; |
| 54 | } else { |
| 55 | return false; |
| 56 | } |
| 57 | } |
| 58 | |
Brian Silverman | d1e65b9 | 2014-03-08 17:07:14 -0800 | [diff] [blame] | 59 | // Constructs and sends a message on the output queue which sets everything to |
Austin Schuh | aebfb4f | 2019-01-27 13:26:38 -0800 | [diff] [blame] | 60 | // a safe state. Default is to set everything to zero. Override Zero below |
| 61 | // to change that behavior. |
| 62 | void ZeroOutputs(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 63 | |
| 64 | // Sets the output to zero. |
Austin Schuh | aebfb4f | 2019-01-27 13:26:38 -0800 | [diff] [blame] | 65 | // Override this if a value of zero (or false) is not "off" for this |
| 66 | // subsystem. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 67 | virtual flatbuffers::Offset<OutputType> Zero( |
| 68 | typename ::aos::Sender<OutputType>::Builder *builder) { |
| 69 | return builder->template MakeBuilder<OutputType>().Finish(); |
| 70 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 71 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 72 | protected: |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 73 | // Runs one cycle of the loop. |
Austin Schuh | a1654ed | 2019-01-27 17:24:54 -0800 | [diff] [blame] | 74 | void IteratePosition(const PositionType &position); |
| 75 | |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 76 | aos::EventLoop *event_loop() { return event_loop_; } |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 77 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 78 | // Returns the position context. This is only valid inside the RunIteration |
| 79 | // method. |
| 80 | const aos::Context &position_context() { return event_loop_->context(); } |
| 81 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 82 | // Runs an iteration of the control loop. |
Brian Silverman | 089f581 | 2015-02-15 01:58:19 -0500 | [diff] [blame] | 83 | // goal is the last goal that was sent. It might be any number of cycles old |
| 84 | // or nullptr if we haven't ever received a goal. |
| 85 | // position is the current position, or nullptr if we didn't get a position |
| 86 | // this cycle. |
| 87 | // output is the values to be sent to the motors. This is nullptr if the |
| 88 | // output is going to be ignored and set to 0. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 89 | // status is the status of the control loop. |
| 90 | // Both output and status should be filled in by the implementation. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 91 | virtual void RunIteration( |
| 92 | const GoalType *goal, const PositionType *position, |
| 93 | typename ::aos::Sender<OutputType>::Builder *output, |
| 94 | typename ::aos::Sender<StatusType>::Builder *status) = 0; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 95 | |
| 96 | private: |
Austin Schuh | 61bdc60 | 2016-12-04 19:10:10 -0800 | [diff] [blame] | 97 | static constexpr ::std::chrono::milliseconds kStaleLogInterval = |
| 98 | ::std::chrono::milliseconds(100); |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 99 | // The amount of time after the last PWM pulse we consider motors enabled for. |
| 100 | // 100ms is the result of using an oscilliscope to look at the input and |
| 101 | // output of a Talon. The Info Sheet also lists 100ms for Talon SR, Talon SRX, |
| 102 | // and Victor SP. |
Austin Schuh | 61bdc60 | 2016-12-04 19:10:10 -0800 | [diff] [blame] | 103 | static constexpr ::std::chrono::milliseconds kPwmDisableTime = |
| 104 | ::std::chrono::milliseconds(100); |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 105 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 106 | // Pointer to the queue group |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 107 | aos::EventLoop *event_loop_; |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame] | 108 | ::std::string name_; |
Brian Silverman | 50a9d03 | 2014-02-16 17:20:57 -0800 | [diff] [blame] | 109 | |
Austin Schuh | a1654ed | 2019-01-27 17:24:54 -0800 | [diff] [blame] | 110 | ::aos::Sender<OutputType> output_sender_; |
| 111 | ::aos::Sender<StatusType> status_sender_; |
| 112 | ::aos::Fetcher<GoalType> goal_fetcher_; |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame] | 113 | ::aos::Fetcher<::aos::RobotState> robot_state_fetcher_; |
| 114 | ::aos::Fetcher<::aos::JoystickState> joystick_state_fetcher_; |
| 115 | |
| 116 | // Fetcher only to be used for the Iterate method. If Iterate is called, Run |
| 117 | // can't be called. |
| 118 | bool has_iterate_fetcher_ = false; |
| 119 | ::aos::Fetcher<PositionType> iterate_position_fetcher_; |
Austin Schuh | a1654ed | 2019-01-27 17:24:54 -0800 | [diff] [blame] | 120 | |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 121 | bool reset_ = false; |
| 122 | int32_t sensor_reader_pid_ = 0; |
| 123 | |
Austin Schuh | 61bdc60 | 2016-12-04 19:10:10 -0800 | [diff] [blame] | 124 | ::aos::monotonic_clock::time_point last_pwm_sent_ = |
| 125 | ::aos::monotonic_clock::min_time; |
Austin Schuh | 3d6e3df | 2014-02-17 01:51:03 -0800 | [diff] [blame] | 126 | |
Brian Silverman | 50a9d03 | 2014-02-16 17:20:57 -0800 | [diff] [blame] | 127 | typedef ::aos::util::SimpleLogInterval SimpleLogInterval; |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 128 | SimpleLogInterval no_sensor_state_ = |
| 129 | SimpleLogInterval(kStaleLogInterval, ERROR, "no sensor state"); |
Brian Silverman | 2704ecf | 2014-04-09 20:24:03 -0700 | [diff] [blame] | 130 | SimpleLogInterval motors_off_log_ = |
| 131 | SimpleLogInterval(kStaleLogInterval, WARNING, "motors disabled"); |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 132 | SimpleLogInterval no_goal_ = |
| 133 | SimpleLogInterval(kStaleLogInterval, ERROR, "no goal"); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 136 | } // namespace controls |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 137 | } // namespace frc971 |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 138 | |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 139 | #include "frc971/control_loops/control_loop-tmpl.h" // IWYU pragma: export |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 140 | |
| 141 | #endif |