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> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 5 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 6 | #include "aos/common/type_traits.h" |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 7 | #include "aos/common/queue.h" |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 8 | #include "aos/common/time.h" |
Brian Silverman | 50a9d03 | 2014-02-16 17:20:57 -0800 | [diff] [blame] | 9 | #include "aos/common/util/log_interval.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 10 | |
| 11 | namespace aos { |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 12 | namespace controls { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 13 | |
| 14 | // Interface to describe runnable jobs. |
| 15 | class Runnable { |
| 16 | public: |
| 17 | virtual ~Runnable() {} |
| 18 | // Runs forever. |
| 19 | virtual void Run() = 0; |
| 20 | // Does one quick piece of work and return. Does _not_ block. |
| 21 | virtual void Iterate() = 0; |
| 22 | }; |
| 23 | |
| 24 | class SerializableControlLoop : public Runnable { |
| 25 | public: |
| 26 | // Returns the size of all the data to be sent when serialized. |
| 27 | virtual size_t SeralizedSize() = 0; |
| 28 | // Serialize the current data. |
| 29 | virtual void Serialize(char *buffer) const = 0; |
| 30 | // Serialize zeroed data in case the data is out of date. |
| 31 | virtual void SerializeZeroMessage(char *buffer) const = 0; |
| 32 | // Deserialize data into the control loop. |
| 33 | virtual void Deserialize(const char *buffer) = 0; |
| 34 | // Unique identifier for the control loop. |
| 35 | // Most likely the hash of the queue group. |
| 36 | virtual uint32_t UniqueID() = 0; |
| 37 | }; |
| 38 | |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 39 | // Control loops run this often, "starting" at time 0. |
Brian Silverman | 1c3cfc0 | 2013-10-25 17:02:19 -0700 | [diff] [blame] | 40 | constexpr time::Time kLoopFrequency = time::Time::InSeconds(0.01); |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 41 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 42 | // Provides helper methods to assist in writing control loops. |
| 43 | // This template expects to be constructed with a queue group as an argument |
| 44 | // that has a goal, position, status, and output queue. |
| 45 | // It will then call the RunIteration method every cycle that it has enough |
| 46 | // valid data for the control loop to run. |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 47 | template <class T, bool fail_no_goal = true> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 48 | class ControlLoop : public SerializableControlLoop { |
| 49 | public: |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 50 | // Create some convenient typedefs to reference the Goal, Position, Status, |
| 51 | // and Output structures. |
| 52 | typedef typename std::remove_reference< |
| 53 | decltype(*(static_cast<T *>(NULL)->goal.MakeMessage().get()))>::type |
| 54 | GoalType; |
| 55 | typedef typename std::remove_reference< |
| 56 | decltype(*(static_cast<T *>(NULL)->position.MakeMessage().get()))>::type |
| 57 | PositionType; |
| 58 | typedef typename std::remove_reference< |
| 59 | decltype(*(static_cast<T *>(NULL)->status.MakeMessage().get()))>::type |
| 60 | StatusType; |
| 61 | typedef typename std::remove_reference< |
| 62 | decltype(*(static_cast<T *>(NULL)->output.MakeMessage().get()))>::type |
| 63 | OutputType; |
| 64 | |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 65 | ControlLoop(T *control_loop) : control_loop_(control_loop) {} |
| 66 | |
| 67 | // Returns true if all the counters etc in the sensor data have been reset. |
| 68 | // This will return true only a single time per reset. |
| 69 | bool WasReset() { |
| 70 | if (reset_) { |
| 71 | reset_ = false; |
| 72 | return true; |
| 73 | } else { |
| 74 | return false; |
| 75 | } |
| 76 | } |
| 77 | |
Brian Silverman | d1e65b9 | 2014-03-08 17:07:14 -0800 | [diff] [blame] | 78 | // Constructs and sends a message on the output queue which sets everything to |
| 79 | // a safe state (generally motors off). For some subclasses, this will be a |
| 80 | // bit different (ie pistons). |
| 81 | // The implementation here creates a new Output message, calls Zero() on it, |
| 82 | // and then sends it. |
| 83 | virtual void ZeroOutputs(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 84 | |
| 85 | // Sets the output to zero. |
| 86 | // Over-ride if a value of zero is not "off" for this subsystem. |
| 87 | virtual void Zero(OutputType *output) { output->Zero(); } |
| 88 | |
| 89 | // Runs the loop forever. |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 90 | void Run() override; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 91 | |
| 92 | // Runs one cycle of the loop. |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 93 | void Iterate() override; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 94 | |
| 95 | // Returns the name of the queue group. |
| 96 | const char *name() { return control_loop_->name(); } |
| 97 | |
| 98 | // Methods to serialize all the data that should be sent over the network. |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 99 | size_t SeralizedSize() override { return control_loop_->goal->Size(); } |
| 100 | void Serialize(char *buffer) const override { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 101 | control_loop_->goal->Serialize(buffer); |
| 102 | } |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 103 | void SerializeZeroMessage(char *buffer) const override { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 104 | GoalType zero_goal; |
| 105 | zero_goal.Zero(); |
| 106 | zero_goal.Serialize(buffer); |
| 107 | } |
| 108 | |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 109 | void Deserialize(const char *buffer) override { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 110 | ScopedMessagePtr<GoalType> new_msg = control_loop_->goal.MakeMessage(); |
| 111 | new_msg->Deserialize(buffer); |
| 112 | new_msg.Send(); |
| 113 | } |
| 114 | |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 115 | uint32_t UniqueID() override { return control_loop_->hash(); } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 116 | |
| 117 | protected: |
| 118 | // Runs an iteration of the control loop. |
| 119 | // goal is the last goal that was sent. It might be any number of cycles old. |
| 120 | // position is the current position, or NULL if we didn't get a position this |
| 121 | // cycle. |
| 122 | // output is the values to be sent to the motors. This is NULL if the output |
| 123 | // is going to be ignored and set to 0. |
| 124 | // status is the status of the control loop. |
| 125 | // Both output and status should be filled in by the implementation. |
| 126 | virtual void RunIteration(const GoalType *goal, |
| 127 | const PositionType *position, |
| 128 | OutputType *output, |
| 129 | StatusType *status) = 0; |
| 130 | |
Brian Silverman | d1e65b9 | 2014-03-08 17:07:14 -0800 | [diff] [blame] | 131 | T *queue_group() { return control_loop_; } |
| 132 | const T *queue_group() const { return control_loop_; } |
| 133 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 134 | private: |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 135 | static constexpr ::aos::time::Time kStaleLogInterval = |
| 136 | ::aos::time::Time::InSeconds(0.1); |
| 137 | // The amount of time after the last PWM pulse we consider motors enabled for. |
| 138 | // 100ms is the result of using an oscilliscope to look at the input and |
| 139 | // output of a Talon. The Info Sheet also lists 100ms for Talon SR, Talon SRX, |
| 140 | // and Victor SP. |
| 141 | static constexpr ::aos::time::Time kPwmDisableTime = |
| 142 | ::aos::time::Time::InMS(100); |
| 143 | |
| 144 | // Maximum age of driver station packets before the loop will be disabled. |
| 145 | static const int kDSPacketTimeoutMs = 500; |
| 146 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 147 | // Pointer to the queue group |
| 148 | T *control_loop_; |
Brian Silverman | 50a9d03 | 2014-02-16 17:20:57 -0800 | [diff] [blame] | 149 | |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 150 | bool reset_ = false; |
| 151 | int32_t sensor_reader_pid_ = 0; |
| 152 | |
| 153 | ::aos::time::Time last_pwm_sent_{0, 0}; |
Austin Schuh | 3d6e3df | 2014-02-17 01:51:03 -0800 | [diff] [blame] | 154 | |
Brian Silverman | 50a9d03 | 2014-02-16 17:20:57 -0800 | [diff] [blame] | 155 | typedef ::aos::util::SimpleLogInterval SimpleLogInterval; |
Brian Silverman | 50a9d03 | 2014-02-16 17:20:57 -0800 | [diff] [blame] | 156 | SimpleLogInterval no_driver_station_ = |
| 157 | SimpleLogInterval(kStaleLogInterval, ERROR, |
| 158 | "no driver station packet"); |
| 159 | SimpleLogInterval driver_station_old_ = |
| 160 | SimpleLogInterval(kStaleLogInterval, ERROR, |
| 161 | "driver station packet is too old"); |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 162 | SimpleLogInterval no_sensor_state_ = |
| 163 | SimpleLogInterval(kStaleLogInterval, ERROR, "no sensor state"); |
Brian Silverman | 2704ecf | 2014-04-09 20:24:03 -0700 | [diff] [blame] | 164 | SimpleLogInterval motors_off_log_ = |
| 165 | SimpleLogInterval(kStaleLogInterval, WARNING, "motors disabled"); |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 166 | SimpleLogInterval no_goal_ = |
| 167 | SimpleLogInterval(kStaleLogInterval, ERROR, "no goal"); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 168 | }; |
| 169 | |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 170 | } // namespace controls |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 171 | } // namespace aos |
| 172 | |
Brian | a6553ed | 2014-04-02 21:26:46 -0700 | [diff] [blame] | 173 | #include "aos/common/controls/control_loop-tmpl.h" // IWYU pragma: export |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 174 | |
| 175 | #endif |