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 | 10f997b | 2013-10-11 18:01:56 -0700 | [diff] [blame] | 54 | template <class T, bool has_position = true, bool fail_no_position = true> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 55 | class ControlLoop : public SerializableControlLoop { |
| 56 | public: |
| 57 | // Maximum age of position packets before the loop will be disabled due to |
| 58 | // invalid position data. |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 59 | static const int kPositionTimeoutMs = 1000; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 60 | // Maximum age of driver station packets before the loop will be disabled. |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 61 | static const int kDSPacketTimeoutMs = 500; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 62 | |
| 63 | ControlLoop(T *control_loop) : control_loop_(control_loop) {} |
| 64 | |
| 65 | // Create some convenient typedefs to reference the Goal, Position, Status, |
| 66 | // and Output structures. |
| 67 | typedef typename std::remove_reference< |
| 68 | decltype(*(static_cast<T *>(NULL)->goal.MakeMessage().get()))>::type |
| 69 | GoalType; |
| 70 | typedef typename std::remove_reference< |
| 71 | decltype(*(static_cast<T *>(NULL)->position.MakeMessage().get()))>::type |
| 72 | PositionType; |
| 73 | typedef typename std::remove_reference< |
| 74 | decltype(*(static_cast<T *>(NULL)->status.MakeMessage().get()))>::type |
| 75 | StatusType; |
| 76 | typedef typename std::remove_reference< |
| 77 | decltype(*(static_cast<T *>(NULL)->output.MakeMessage().get()))>::type |
| 78 | OutputType; |
| 79 | |
| 80 | // Constructs and sends a message on the output queue which will stop all the |
| 81 | // motors. Calls Zero to clear all the state. |
| 82 | void ZeroOutputs(); |
| 83 | |
| 84 | // Sets the output to zero. |
| 85 | // Over-ride if a value of zero is not "off" for this subsystem. |
| 86 | virtual void Zero(OutputType *output) { output->Zero(); } |
| 87 | |
| 88 | // Runs the loop forever. |
| 89 | virtual void Run(); |
| 90 | |
| 91 | // Runs one cycle of the loop. |
| 92 | virtual void Iterate(); |
| 93 | |
| 94 | // Returns the name of the queue group. |
| 95 | const char *name() { return control_loop_->name(); } |
| 96 | |
| 97 | // Methods to serialize all the data that should be sent over the network. |
| 98 | virtual size_t SeralizedSize() { return control_loop_->goal->Size(); } |
| 99 | virtual void Serialize(char *buffer) const { |
| 100 | control_loop_->goal->Serialize(buffer); |
| 101 | } |
| 102 | virtual void SerializeZeroMessage(char *buffer) const { |
| 103 | GoalType zero_goal; |
| 104 | zero_goal.Zero(); |
| 105 | zero_goal.Serialize(buffer); |
| 106 | } |
| 107 | |
| 108 | virtual void Deserialize(const char *buffer) { |
| 109 | ScopedMessagePtr<GoalType> new_msg = control_loop_->goal.MakeMessage(); |
| 110 | new_msg->Deserialize(buffer); |
| 111 | new_msg.Send(); |
| 112 | } |
| 113 | |
| 114 | virtual uint32_t UniqueID() { return control_loop_->hash(); } |
| 115 | |
| 116 | protected: |
| 117 | // Runs an iteration of the control loop. |
| 118 | // goal is the last goal that was sent. It might be any number of cycles old. |
| 119 | // position is the current position, or NULL if we didn't get a position this |
| 120 | // cycle. |
| 121 | // output is the values to be sent to the motors. This is NULL if the output |
| 122 | // is going to be ignored and set to 0. |
| 123 | // status is the status of the control loop. |
| 124 | // Both output and status should be filled in by the implementation. |
| 125 | virtual void RunIteration(const GoalType *goal, |
| 126 | const PositionType *position, |
| 127 | OutputType *output, |
| 128 | StatusType *status) = 0; |
| 129 | |
| 130 | private: |
| 131 | // Pointer to the queue group |
| 132 | T *control_loop_; |
Brian Silverman | 50a9d03 | 2014-02-16 17:20:57 -0800 | [diff] [blame] | 133 | |
| 134 | typedef ::aos::util::SimpleLogInterval SimpleLogInterval; |
| 135 | static constexpr ::aos::time::Time kStaleLogInterval = |
| 136 | ::aos::time::Time::InSeconds(0.1); |
| 137 | SimpleLogInterval very_stale_position_ = |
| 138 | SimpleLogInterval(kStaleLogInterval, ERROR, |
| 139 | "outputs disabled because position is very stale"); |
| 140 | SimpleLogInterval no_prior_goal_ = |
| 141 | SimpleLogInterval(kStaleLogInterval, ERROR, |
| 142 | "no prior goal"); |
| 143 | SimpleLogInterval no_prior_position_ = |
| 144 | SimpleLogInterval(kStaleLogInterval, ERROR, |
| 145 | "no prior position"); |
| 146 | SimpleLogInterval no_driver_station_ = |
| 147 | SimpleLogInterval(kStaleLogInterval, ERROR, |
| 148 | "no driver station packet"); |
| 149 | SimpleLogInterval driver_station_old_ = |
| 150 | SimpleLogInterval(kStaleLogInterval, ERROR, |
| 151 | "driver station packet is too old"); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | } // namespace control_loops |
| 155 | } // namespace aos |
| 156 | |
| 157 | #include "aos/common/control_loop/ControlLoop-tmpl.h" // IWYU pragma: export |
| 158 | |
| 159 | #endif |