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