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 | |
| 4 | #include <cstring> |
| 5 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 6 | #include "aos/common/control_loop/Timing.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 7 | #include "aos/common/type_traits.h" |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 8 | #include "aos/common/queue.h" |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 9 | #include "aos/common/time.h" |
Brian Silverman | 50a9d03 | 2014-02-16 17:20:57 -0800 | [diff] [blame] | 10 | #include "aos/common/util/log_interval.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 11 | |
| 12 | namespace aos { |
| 13 | namespace control_loops { |
| 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 | |
| 25 | class SerializableControlLoop : public Runnable { |
| 26 | public: |
| 27 | // Returns the size of all the data to be sent when serialized. |
| 28 | virtual size_t SeralizedSize() = 0; |
| 29 | // Serialize the current data. |
| 30 | virtual void Serialize(char *buffer) const = 0; |
| 31 | // Serialize zeroed data in case the data is out of date. |
| 32 | virtual void SerializeZeroMessage(char *buffer) const = 0; |
| 33 | // Deserialize data into the control loop. |
| 34 | virtual void Deserialize(const char *buffer) = 0; |
| 35 | // Unique identifier for the control loop. |
| 36 | // Most likely the hash of the queue group. |
| 37 | virtual uint32_t UniqueID() = 0; |
| 38 | }; |
| 39 | |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 40 | // Control loops run this often, "starting" at time 0. |
Brian Silverman | 1c3cfc0 | 2013-10-25 17:02:19 -0700 | [diff] [blame] | 41 | constexpr time::Time kLoopFrequency = time::Time::InSeconds(0.01); |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 42 | |
Brian Silverman | 15ca985 | 2013-03-17 18:24:15 -0700 | [diff] [blame] | 43 | // Calculates the next time to run control loops after start. |
| 44 | time::Time NextLoopTime(time::Time start = time::Time::Now()); |
| 45 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 46 | // Provides helper methods to assist in writing control loops. |
| 47 | // This template expects to be constructed with a queue group as an argument |
| 48 | // that has a goal, position, status, and output queue. |
| 49 | // It will then call the RunIteration method every cycle that it has enough |
| 50 | // valid data for the control loop to run. |
| 51 | // If has_position is false, the control loop will always use NULL as the |
| 52 | // position and not check the queue. This is used for "loops" that control |
| 53 | // motors open loop. |
Brian Silverman | 71fbee0 | 2014-03-13 17:24:54 -0700 | [diff] [blame^] | 54 | template <class T, bool has_position = true, bool fail_no_position = true, |
| 55 | bool fail_no_goal = true> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 56 | class ControlLoop : public SerializableControlLoop { |
| 57 | public: |
| 58 | // Maximum age of position packets before the loop will be disabled due to |
| 59 | // invalid position data. |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 60 | static const int kPositionTimeoutMs = 1000; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 61 | // Maximum age of driver station packets before the loop will be disabled. |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 62 | static const int kDSPacketTimeoutMs = 500; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 63 | |
Austin Schuh | 3d6e3df | 2014-02-17 01:51:03 -0800 | [diff] [blame] | 64 | ControlLoop(T *control_loop) |
| 65 | : control_loop_(control_loop), |
| 66 | reset_(true), |
| 67 | has_sensor_reset_counters_(false) {} |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 68 | // Create some convenient typedefs to reference the Goal, Position, Status, |
| 69 | // and Output structures. |
| 70 | typedef typename std::remove_reference< |
| 71 | decltype(*(static_cast<T *>(NULL)->goal.MakeMessage().get()))>::type |
| 72 | GoalType; |
| 73 | typedef typename std::remove_reference< |
| 74 | decltype(*(static_cast<T *>(NULL)->position.MakeMessage().get()))>::type |
| 75 | PositionType; |
| 76 | typedef typename std::remove_reference< |
| 77 | decltype(*(static_cast<T *>(NULL)->status.MakeMessage().get()))>::type |
| 78 | StatusType; |
| 79 | typedef typename std::remove_reference< |
| 80 | decltype(*(static_cast<T *>(NULL)->output.MakeMessage().get()))>::type |
| 81 | OutputType; |
| 82 | |
Brian Silverman | d1e65b9 | 2014-03-08 17:07:14 -0800 | [diff] [blame] | 83 | // Constructs and sends a message on the output queue which sets everything to |
| 84 | // a safe state (generally motors off). For some subclasses, this will be a |
| 85 | // bit different (ie pistons). |
| 86 | // The implementation here creates a new Output message, calls Zero() on it, |
| 87 | // and then sends it. |
| 88 | virtual void ZeroOutputs(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 89 | |
Austin Schuh | 3d6e3df | 2014-02-17 01:51:03 -0800 | [diff] [blame] | 90 | // Returns true if the device reading the sensors reset and potentially lost |
Ben Fredrickson | 4283bb4 | 2014-02-22 08:31:50 +0000 | [diff] [blame] | 91 | // track of encoder counts. Calling this read method clears the flag. After |
| 92 | // a reset, RunIteration will not be called until there is a valid position |
| 93 | // message. |
Austin Schuh | 3d6e3df | 2014-02-17 01:51:03 -0800 | [diff] [blame] | 94 | bool reset() { |
| 95 | bool ans = reset_; |
| 96 | reset_ = false; |
| 97 | return ans; |
| 98 | } |
| 99 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 100 | // Sets the output to zero. |
| 101 | // Over-ride if a value of zero is not "off" for this subsystem. |
| 102 | virtual void Zero(OutputType *output) { output->Zero(); } |
| 103 | |
| 104 | // Runs the loop forever. |
| 105 | virtual void Run(); |
| 106 | |
| 107 | // Runs one cycle of the loop. |
| 108 | virtual void Iterate(); |
| 109 | |
| 110 | // Returns the name of the queue group. |
| 111 | const char *name() { return control_loop_->name(); } |
| 112 | |
| 113 | // Methods to serialize all the data that should be sent over the network. |
| 114 | virtual size_t SeralizedSize() { return control_loop_->goal->Size(); } |
| 115 | virtual void Serialize(char *buffer) const { |
| 116 | control_loop_->goal->Serialize(buffer); |
| 117 | } |
| 118 | virtual void SerializeZeroMessage(char *buffer) const { |
| 119 | GoalType zero_goal; |
| 120 | zero_goal.Zero(); |
| 121 | zero_goal.Serialize(buffer); |
| 122 | } |
| 123 | |
| 124 | virtual void Deserialize(const char *buffer) { |
| 125 | ScopedMessagePtr<GoalType> new_msg = control_loop_->goal.MakeMessage(); |
| 126 | new_msg->Deserialize(buffer); |
| 127 | new_msg.Send(); |
| 128 | } |
| 129 | |
| 130 | virtual uint32_t UniqueID() { return control_loop_->hash(); } |
| 131 | |
| 132 | protected: |
| 133 | // Runs an iteration of the control loop. |
| 134 | // goal is the last goal that was sent. It might be any number of cycles old. |
| 135 | // position is the current position, or NULL if we didn't get a position this |
| 136 | // cycle. |
| 137 | // output is the values to be sent to the motors. This is NULL if the output |
| 138 | // is going to be ignored and set to 0. |
| 139 | // status is the status of the control loop. |
| 140 | // Both output and status should be filled in by the implementation. |
| 141 | virtual void RunIteration(const GoalType *goal, |
| 142 | const PositionType *position, |
| 143 | OutputType *output, |
| 144 | StatusType *status) = 0; |
| 145 | |
Brian Silverman | d1e65b9 | 2014-03-08 17:07:14 -0800 | [diff] [blame] | 146 | T *queue_group() { return control_loop_; } |
| 147 | const T *queue_group() const { return control_loop_; } |
| 148 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 149 | private: |
| 150 | // Pointer to the queue group |
| 151 | T *control_loop_; |
Brian Silverman | 50a9d03 | 2014-02-16 17:20:57 -0800 | [diff] [blame] | 152 | |
Austin Schuh | 3d6e3df | 2014-02-17 01:51:03 -0800 | [diff] [blame] | 153 | bool reset_; |
| 154 | bool has_sensor_reset_counters_; |
| 155 | int32_t reader_pid_; |
| 156 | uint32_t cape_resets_; |
| 157 | |
Brian Silverman | 50a9d03 | 2014-02-16 17:20:57 -0800 | [diff] [blame] | 158 | typedef ::aos::util::SimpleLogInterval SimpleLogInterval; |
| 159 | static constexpr ::aos::time::Time kStaleLogInterval = |
| 160 | ::aos::time::Time::InSeconds(0.1); |
| 161 | SimpleLogInterval very_stale_position_ = |
| 162 | SimpleLogInterval(kStaleLogInterval, ERROR, |
| 163 | "outputs disabled because position is very stale"); |
| 164 | SimpleLogInterval no_prior_goal_ = |
| 165 | SimpleLogInterval(kStaleLogInterval, ERROR, |
| 166 | "no prior goal"); |
| 167 | SimpleLogInterval no_prior_position_ = |
| 168 | SimpleLogInterval(kStaleLogInterval, ERROR, |
| 169 | "no prior position"); |
| 170 | SimpleLogInterval no_driver_station_ = |
| 171 | SimpleLogInterval(kStaleLogInterval, ERROR, |
| 172 | "no driver station packet"); |
| 173 | SimpleLogInterval driver_station_old_ = |
| 174 | SimpleLogInterval(kStaleLogInterval, ERROR, |
| 175 | "driver station packet is too old"); |
Brian Silverman | 4910efb | 2014-02-17 11:10:10 -0800 | [diff] [blame] | 176 | SimpleLogInterval no_sensor_generation_ = |
| 177 | SimpleLogInterval(kStaleLogInterval, ERROR, |
| 178 | "no sensor_generation message"); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | } // namespace control_loops |
| 182 | } // namespace aos |
| 183 | |
| 184 | #include "aos/common/control_loop/ControlLoop-tmpl.h" // IWYU pragma: export |
| 185 | |
| 186 | #endif |