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 | |
Brian Silverman | b407c67 | 2014-04-09 11:58:37 -0700 | [diff] [blame] | 4 | #include <string.h> |
Austin Schuh | b58ceb6 | 2017-02-05 14:21:57 -0800 | [diff] [blame] | 5 | #include <atomic> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 6 | |
Austin Schuh | a1654ed | 2019-01-27 17:24:54 -0800 | [diff] [blame] | 7 | #include "aos/events/event-loop.h" |
| 8 | #include "aos/events/shm-event-loop.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 9 | #include "aos/queue.h" |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame^] | 10 | #include "aos/robot_state/robot_state.q.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 11 | #include "aos/time/time.h" |
| 12 | #include "aos/type_traits/type_traits.h" |
| 13 | #include "aos/util/log_interval.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 14 | |
| 15 | namespace aos { |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 16 | namespace controls { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 17 | |
| 18 | // Interface to describe runnable jobs. |
| 19 | class Runnable { |
| 20 | public: |
| 21 | virtual ~Runnable() {} |
| 22 | // Runs forever. |
| 23 | virtual void Run() = 0; |
| 24 | // Does one quick piece of work and return. Does _not_ block. |
| 25 | virtual void Iterate() = 0; |
| 26 | }; |
| 27 | |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 28 | // Control loops run this often, "starting" at time 0. |
Austin Schuh | 214e9c1 | 2016-11-25 17:26:20 -0800 | [diff] [blame] | 29 | constexpr ::std::chrono::nanoseconds kLoopFrequency = |
| 30 | ::std::chrono::milliseconds(5); |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 31 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 32 | // Provides helper methods to assist in writing control loops. |
| 33 | // This template expects to be constructed with a queue group as an argument |
| 34 | // that has a goal, position, status, and output queue. |
| 35 | // It will then call the RunIteration method every cycle that it has enough |
| 36 | // valid data for the control loop to run. |
Brian Silverman | 089f581 | 2015-02-15 01:58:19 -0500 | [diff] [blame] | 37 | template <class T> |
Austin Schuh | fd6e16c | 2019-01-27 12:22:47 -0800 | [diff] [blame] | 38 | class ControlLoop : public Runnable { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 39 | public: |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 40 | // Create some convenient typedefs to reference the Goal, Position, Status, |
| 41 | // and Output structures. |
| 42 | typedef typename std::remove_reference< |
| 43 | decltype(*(static_cast<T *>(NULL)->goal.MakeMessage().get()))>::type |
| 44 | GoalType; |
| 45 | typedef typename std::remove_reference< |
| 46 | decltype(*(static_cast<T *>(NULL)->position.MakeMessage().get()))>::type |
| 47 | PositionType; |
| 48 | typedef typename std::remove_reference< |
| 49 | decltype(*(static_cast<T *>(NULL)->status.MakeMessage().get()))>::type |
| 50 | StatusType; |
| 51 | typedef typename std::remove_reference< |
| 52 | decltype(*(static_cast<T *>(NULL)->output.MakeMessage().get()))>::type |
| 53 | OutputType; |
| 54 | |
Austin Schuh | a1654ed | 2019-01-27 17:24:54 -0800 | [diff] [blame] | 55 | ControlLoop(T *control_loop) |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame^] | 56 | : shm_event_loop_(new ::aos::ShmEventLoop()), |
| 57 | event_loop_(shm_event_loop_.get()), |
| 58 | name_(control_loop->name()) { |
| 59 | output_sender_ = event_loop_->MakeSender<OutputType>(name_ + ".output"); |
| 60 | status_sender_ = event_loop_->MakeSender<StatusType>(name_ + ".status"); |
| 61 | goal_fetcher_ = event_loop_->MakeFetcher<GoalType>(name_ + ".goal"); |
| 62 | robot_state_fetcher_ = |
| 63 | event_loop_->MakeFetcher<::aos::RobotState>(".aos.robot_state"); |
| 64 | joystick_state_fetcher_ = |
| 65 | event_loop_->MakeFetcher<::aos::JoystickState>(".aos.joystick_state"); |
| 66 | } |
| 67 | |
| 68 | const ::aos::RobotState &robot_state() const { return *robot_state_fetcher_; } |
| 69 | bool has_joystick_state() const { return joystick_state_fetcher_.get(); } |
| 70 | const ::aos::JoystickState &joystick_state() const { |
| 71 | return *joystick_state_fetcher_; |
Austin Schuh | a1654ed | 2019-01-27 17:24:54 -0800 | [diff] [blame] | 72 | } |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 73 | |
| 74 | // Returns true if all the counters etc in the sensor data have been reset. |
| 75 | // This will return true only a single time per reset. |
| 76 | bool WasReset() { |
| 77 | if (reset_) { |
| 78 | reset_ = false; |
| 79 | return true; |
| 80 | } else { |
| 81 | return false; |
| 82 | } |
| 83 | } |
| 84 | |
Brian Silverman | d1e65b9 | 2014-03-08 17:07:14 -0800 | [diff] [blame] | 85 | // 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] | 86 | // a safe state. Default is to set everything to zero. Override Zero below |
| 87 | // to change that behavior. |
| 88 | void ZeroOutputs(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 89 | |
| 90 | // Sets the output to zero. |
Austin Schuh | aebfb4f | 2019-01-27 13:26:38 -0800 | [diff] [blame] | 91 | // Override this if a value of zero (or false) is not "off" for this |
| 92 | // subsystem. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 93 | virtual void Zero(OutputType *output) { output->Zero(); } |
| 94 | |
| 95 | // Runs the loop forever. |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 96 | void Run() override; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 97 | |
| 98 | // Runs one cycle of the loop. |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 99 | void Iterate() override; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 100 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 101 | protected: |
Austin Schuh | a1654ed | 2019-01-27 17:24:54 -0800 | [diff] [blame] | 102 | void IteratePosition(const PositionType &position); |
| 103 | |
Austin Schuh | b58ceb6 | 2017-02-05 14:21:57 -0800 | [diff] [blame] | 104 | static void Quit(int /*signum*/) { |
| 105 | run_ = false; |
| 106 | } |
| 107 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 108 | // Runs an iteration of the control loop. |
Brian Silverman | 089f581 | 2015-02-15 01:58:19 -0500 | [diff] [blame] | 109 | // goal is the last goal that was sent. It might be any number of cycles old |
| 110 | // or nullptr if we haven't ever received a goal. |
| 111 | // position is the current position, or nullptr if we didn't get a position |
| 112 | // this cycle. |
| 113 | // output is the values to be sent to the motors. This is nullptr if the |
| 114 | // output is going to be ignored and set to 0. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 115 | // status is the status of the control loop. |
| 116 | // Both output and status should be filled in by the implementation. |
| 117 | virtual void RunIteration(const GoalType *goal, |
| 118 | const PositionType *position, |
| 119 | OutputType *output, |
| 120 | StatusType *status) = 0; |
| 121 | |
| 122 | private: |
Austin Schuh | 61bdc60 | 2016-12-04 19:10:10 -0800 | [diff] [blame] | 123 | static constexpr ::std::chrono::milliseconds kStaleLogInterval = |
| 124 | ::std::chrono::milliseconds(100); |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 125 | // The amount of time after the last PWM pulse we consider motors enabled for. |
| 126 | // 100ms is the result of using an oscilliscope to look at the input and |
| 127 | // output of a Talon. The Info Sheet also lists 100ms for Talon SR, Talon SRX, |
| 128 | // and Victor SP. |
Austin Schuh | 61bdc60 | 2016-12-04 19:10:10 -0800 | [diff] [blame] | 129 | static constexpr ::std::chrono::milliseconds kPwmDisableTime = |
| 130 | ::std::chrono::milliseconds(100); |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 131 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 132 | // Pointer to the queue group |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame^] | 133 | ::std::unique_ptr<ShmEventLoop> shm_event_loop_; |
| 134 | EventLoop *event_loop_; |
| 135 | ::std::string name_; |
Brian Silverman | 50a9d03 | 2014-02-16 17:20:57 -0800 | [diff] [blame] | 136 | |
Austin Schuh | a1654ed | 2019-01-27 17:24:54 -0800 | [diff] [blame] | 137 | ::aos::Sender<OutputType> output_sender_; |
| 138 | ::aos::Sender<StatusType> status_sender_; |
| 139 | ::aos::Fetcher<GoalType> goal_fetcher_; |
Austin Schuh | eeec74a | 2019-01-27 20:58:59 -0800 | [diff] [blame^] | 140 | ::aos::Fetcher<::aos::RobotState> robot_state_fetcher_; |
| 141 | ::aos::Fetcher<::aos::JoystickState> joystick_state_fetcher_; |
| 142 | |
| 143 | // Fetcher only to be used for the Iterate method. If Iterate is called, Run |
| 144 | // can't be called. |
| 145 | bool has_iterate_fetcher_ = false; |
| 146 | ::aos::Fetcher<PositionType> iterate_position_fetcher_; |
Austin Schuh | a1654ed | 2019-01-27 17:24:54 -0800 | [diff] [blame] | 147 | |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 148 | bool reset_ = false; |
| 149 | int32_t sensor_reader_pid_ = 0; |
| 150 | |
Austin Schuh | 61bdc60 | 2016-12-04 19:10:10 -0800 | [diff] [blame] | 151 | ::aos::monotonic_clock::time_point last_pwm_sent_ = |
| 152 | ::aos::monotonic_clock::min_time; |
Austin Schuh | 3d6e3df | 2014-02-17 01:51:03 -0800 | [diff] [blame] | 153 | |
Brian Silverman | 50a9d03 | 2014-02-16 17:20:57 -0800 | [diff] [blame] | 154 | typedef ::aos::util::SimpleLogInterval SimpleLogInterval; |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 155 | SimpleLogInterval no_sensor_state_ = |
| 156 | SimpleLogInterval(kStaleLogInterval, ERROR, "no sensor state"); |
Brian Silverman | 2704ecf | 2014-04-09 20:24:03 -0700 | [diff] [blame] | 157 | SimpleLogInterval motors_off_log_ = |
| 158 | SimpleLogInterval(kStaleLogInterval, WARNING, "motors disabled"); |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 159 | SimpleLogInterval no_goal_ = |
| 160 | SimpleLogInterval(kStaleLogInterval, ERROR, "no goal"); |
Austin Schuh | b58ceb6 | 2017-02-05 14:21:57 -0800 | [diff] [blame] | 161 | |
| 162 | static ::std::atomic<bool> run_; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 165 | } // namespace controls |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 166 | } // namespace aos |
| 167 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 168 | #include "aos/controls/control_loop-tmpl.h" // IWYU pragma: export |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 169 | |
| 170 | #endif |