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