Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 1 | #ifndef FRC971_CONTROL_LOOPS_CLAW_CLAW_H_ |
| 2 | #define FRC971_CONTROL_LOOPS_CLAW_CLAW_H_ |
| 3 | |
| 4 | #include <memory> |
| 5 | |
Brian | a6553ed | 2014-04-02 21:26:46 -0700 | [diff] [blame] | 6 | #include "aos/common/controls/control_loop.h" |
| 7 | #include "aos/common/controls/polytope.h" |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 8 | #include "frc971/constants.h" |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 9 | #include "frc971/control_loops/state_feedback_loop.h" |
James Kuszmaul | f63b0ae | 2014-03-25 16:52:11 -0700 | [diff] [blame] | 10 | #include "frc971/control_loops/coerce_goal.h" |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 11 | #include "frc971/control_loops/claw/claw.q.h" |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 12 | #include "frc971/control_loops/claw/claw_motor_plant.h" |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 13 | #include "frc971/control_loops/hall_effect_tracker.h" |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 14 | |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 15 | namespace frc971 { |
| 16 | namespace control_loops { |
| 17 | namespace testing { |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 18 | class WindupClawTest; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 19 | }; |
| 20 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 21 | // Note: Everything in this file assumes that there is a 1 cycle delay between |
| 22 | // power being requested and it showing up at the motor. It assumes that |
| 23 | // X_hat(2, 1) is the voltage being applied as well. It will go unstable if |
| 24 | // that isn't true. |
| 25 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 26 | class ClawLimitedLoop : public StateFeedbackLoop<4, 2, 2> { |
| 27 | public: |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 28 | ClawLimitedLoop(StateFeedbackLoop<4, 2, 2> &&loop); |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 29 | virtual void CapU(); |
| 30 | |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 31 | void set_is_zeroing(bool is_zeroing) { is_zeroing_ = is_zeroing; } |
Brian Silverman | ed9df2f | 2014-04-05 07:07:15 -0700 | [diff] [blame] | 32 | void set_positions_known(bool top_known, bool bottom_known) { |
| 33 | top_known_ = top_known; |
| 34 | bottom_known_ = bottom_known; |
| 35 | } |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 36 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 37 | void ChangeTopOffset(double doffset); |
| 38 | void ChangeBottomOffset(double doffset); |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 39 | |
| 40 | double uncapped_average_voltage() const { return uncapped_average_voltage_; } |
| 41 | |
| 42 | private: |
| 43 | double uncapped_average_voltage_; |
| 44 | bool is_zeroing_; |
James Kuszmaul | f63b0ae | 2014-03-25 16:52:11 -0700 | [diff] [blame] | 45 | |
Brian Silverman | ed9df2f | 2014-04-05 07:07:15 -0700 | [diff] [blame] | 46 | bool top_known_ = false, bottom_known_ = false; |
| 47 | |
Brian Silverman | 6dd2c53 | 2014-03-29 23:34:39 -0700 | [diff] [blame] | 48 | const ::aos::controls::HPolytope<2> U_Poly_, U_Poly_zeroing_; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | class ClawMotor; |
| 52 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 53 | // This class implements the CapU function correctly given all the extra |
| 54 | // information that we know about from the wrist motor. |
| 55 | // It does not have any zeroing logic in it, only logic to deal with a delta U |
| 56 | // controller. |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 57 | class ZeroedStateFeedbackLoop { |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 58 | public: |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 59 | ZeroedStateFeedbackLoop(const char *name, ClawMotor *motor); |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 60 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 61 | const static int kZeroingMaxVoltage = 5; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 62 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 63 | enum JointZeroingState { |
| 64 | // We don't know where the joint is at all. |
| 65 | UNKNOWN_POSITION, |
| 66 | // We have an approximate position for where the claw is using. |
| 67 | APPROXIMATE_CALIBRATION, |
| 68 | // We observed the calibration edge while disabled. This is good enough for |
| 69 | // autonomous mode. |
| 70 | DISABLED_CALIBRATION, |
| 71 | // Ready for use during teleop. |
| 72 | CALIBRATED |
| 73 | }; |
| 74 | |
| 75 | void set_zeroing_state(JointZeroingState zeroing_state) { |
| 76 | zeroing_state_ = zeroing_state; |
| 77 | } |
| 78 | JointZeroingState zeroing_state() const { return zeroing_state_; } |
| 79 | |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 80 | void SetPositionValues(const HalfClawPosition &claw); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 81 | |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 82 | void Reset(const HalfClawPosition &claw); |
Ben Fredrickson | ade3eab | 2014-02-22 07:30:53 +0000 | [diff] [blame] | 83 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 84 | double absolute_position() const { return encoder() + offset(); } |
| 85 | |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 86 | const HallEffectTracker &front() const { return front_; } |
| 87 | const HallEffectTracker &calibration() const { return calibration_; } |
| 88 | const HallEffectTracker &back() const { return back_; } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 89 | |
| 90 | bool any_hall_effect_changed() const { |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 91 | return front().either_count_changed() || |
| 92 | calibration().either_count_changed() || |
| 93 | back().either_count_changed(); |
| 94 | } |
| 95 | bool front_or_back_triggered() const { |
| 96 | return front().value() || back().value(); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 97 | } |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 98 | bool any_triggered() const { |
| 99 | return calibration().value() || front().value() || back().value(); |
| 100 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 101 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 102 | double encoder() const { return encoder_; } |
| 103 | double last_encoder() const { return last_encoder_; } |
| 104 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 105 | double offset() const { return offset_; } |
| 106 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 107 | // Returns true if an edge was detected in the last cycle and then sets the |
| 108 | // edge_position to the absolute position of the edge. |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 109 | bool GetPositionOfEdge(const constants::Values::Claws::Claw &claw, |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 110 | double *edge_encoder, double *edge_angle); |
| 111 | |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 112 | bool SawFilteredPosedge(const HallEffectTracker &this_sensor, |
| 113 | const HallEffectTracker &sensorA, |
| 114 | const HallEffectTracker &sensorB); |
| 115 | |
| 116 | bool SawFilteredNegedge(const HallEffectTracker &this_sensor, |
| 117 | const HallEffectTracker &sensorA, |
| 118 | const HallEffectTracker &sensorB); |
| 119 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 120 | #undef COUNT_SETTER_GETTER |
| 121 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 122 | protected: |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 123 | // The accumulated voltage to apply to the motor. |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 124 | double offset_; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 125 | const char *name_; |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 126 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 127 | ClawMotor *motor_; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 128 | |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 129 | HallEffectTracker front_, calibration_, back_; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 130 | |
| 131 | JointZeroingState zeroing_state_; |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 132 | double posedge_value_; |
| 133 | double negedge_value_; |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 134 | double min_hall_effect_on_angle_; |
| 135 | double max_hall_effect_on_angle_; |
| 136 | double min_hall_effect_off_angle_; |
| 137 | double max_hall_effect_off_angle_; |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 138 | double encoder_; |
| 139 | double last_encoder_; |
Austin Schuh | f84a130 | 2014-02-19 00:23:30 -0800 | [diff] [blame] | 140 | double last_on_encoder_; |
| 141 | double last_off_encoder_; |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 142 | bool any_triggered_last_; |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 143 | |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 144 | const HallEffectTracker* posedge_filter_ = nullptr; |
| 145 | const HallEffectTracker* negedge_filter_ = nullptr; |
| 146 | |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 147 | private: |
| 148 | // Does the edges of 1 sensor for GetPositionOfEdge. |
| 149 | bool DoGetPositionOfEdge(const constants::Values::Claws::AnglePair &angles, |
| 150 | double *edge_encoder, double *edge_angle, |
| 151 | const HallEffectTracker &sensor, |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 152 | const HallEffectTracker &sensorA, |
| 153 | const HallEffectTracker &sensorB, |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 154 | const char *hall_effect_name); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 155 | }; |
| 156 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 157 | class TopZeroedStateFeedbackLoop : public ZeroedStateFeedbackLoop { |
| 158 | public: |
| 159 | TopZeroedStateFeedbackLoop(ClawMotor *motor) |
| 160 | : ZeroedStateFeedbackLoop("top", motor) {} |
| 161 | // Sets the calibration offset given the absolute angle and the corrisponding |
| 162 | // encoder value. |
| 163 | void SetCalibration(double edge_encoder, double edge_angle); |
| 164 | |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 165 | bool SetCalibrationOnEdge(const constants::Values::Claws::Claw &claw_values, |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 166 | JointZeroingState zeroing_state); |
Brian Silverman | 084372e | 2014-04-10 10:55:53 -0700 | [diff] [blame] | 167 | double ComputeCalibrationChange(double edge_encoder, double edge_angle); |
| 168 | void HandleCalibrationError( |
| 169 | const constants::Values::Claws::Claw &claw_values); |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | class BottomZeroedStateFeedbackLoop : public ZeroedStateFeedbackLoop { |
| 173 | public: |
| 174 | BottomZeroedStateFeedbackLoop(ClawMotor *motor) |
| 175 | : ZeroedStateFeedbackLoop("bottom", motor) {} |
| 176 | // Sets the calibration offset given the absolute angle and the corrisponding |
| 177 | // encoder value. |
| 178 | void SetCalibration(double edge_encoder, double edge_angle); |
| 179 | |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 180 | bool SetCalibrationOnEdge(const constants::Values::Claws::Claw &claw_values, |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 181 | JointZeroingState zeroing_state); |
Brian Silverman | 084372e | 2014-04-10 10:55:53 -0700 | [diff] [blame] | 182 | double ComputeCalibrationChange(double edge_encoder, double edge_angle); |
| 183 | void HandleCalibrationError( |
| 184 | const constants::Values::Claws::Claw &claw_values); |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 185 | }; |
Ben Fredrickson | 9b38842 | 2014-02-13 06:15:31 +0000 | [diff] [blame] | 186 | |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame] | 187 | class ClawMotor : public aos::controls::ControlLoop< |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 188 | control_loops::ClawGroup, false> { |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 189 | public: |
| 190 | explicit ClawMotor(control_loops::ClawGroup *my_claw = |
| 191 | &control_loops::claw_queue_group); |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 192 | |
| 193 | // True if the state machine is ready. |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 194 | bool capped_goal() const { return capped_goal_; } |
| 195 | |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 196 | double uncapped_average_voltage() const { |
| 197 | return claw_.uncapped_average_voltage(); |
| 198 | } |
| 199 | |
| 200 | // True if the claw is zeroing. |
| 201 | bool is_zeroing() const; |
| 202 | |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 203 | // True if the state machine is ready. |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 204 | bool is_ready() const; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 205 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 206 | void ChangeTopOffset(double doffset); |
| 207 | void ChangeBottomOffset(double doffset); |
| 208 | |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 209 | enum CalibrationMode { |
| 210 | READY, |
| 211 | PREP_FINE_TUNE_TOP, |
| 212 | FINE_TUNE_TOP, |
| 213 | PREP_FINE_TUNE_BOTTOM, |
| 214 | FINE_TUNE_BOTTOM, |
| 215 | UNKNOWN_LOCATION |
| 216 | }; |
| 217 | |
| 218 | CalibrationMode mode() const { return mode_; } |
| 219 | |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 220 | protected: |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 221 | virtual void RunIteration(const control_loops::ClawGroup::Goal *goal, |
| 222 | const control_loops::ClawGroup::Position *position, |
| 223 | control_loops::ClawGroup::Output *output, |
James Kuszmaul | 9ead1de | 2014-02-28 21:24:39 -0800 | [diff] [blame] | 224 | control_loops::ClawGroup::Status *status); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 225 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 226 | double top_absolute_position() const { |
| 227 | return claw_.X_hat(1, 0) + claw_.X_hat(0, 0); |
| 228 | } |
| 229 | double bottom_absolute_position() const { return claw_.X_hat(0, 0); } |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 230 | |
| 231 | private: |
| 232 | // Friend the test classes for acces to the internal state. |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 233 | friend class testing::WindupClawTest; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 234 | |
| 235 | // The zeroed joint to use. |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 236 | bool has_top_claw_goal_; |
| 237 | double top_claw_goal_; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 238 | TopZeroedStateFeedbackLoop top_claw_; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 239 | |
| 240 | bool has_bottom_claw_goal_; |
| 241 | double bottom_claw_goal_; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 242 | BottomZeroedStateFeedbackLoop bottom_claw_; |
| 243 | |
| 244 | // The claw loop. |
| 245 | ClawLimitedLoop claw_; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 246 | |
| 247 | bool was_enabled_; |
Ben Fredrickson | 9b38842 | 2014-02-13 06:15:31 +0000 | [diff] [blame] | 248 | bool doing_calibration_fine_tune_; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 249 | |
Brian Silverman | 7c021c4 | 2014-02-17 15:15:56 -0800 | [diff] [blame] | 250 | // The initial separation when disabled. Used as the safe separation |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 251 | // distance. |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 252 | double initial_separation_; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 253 | |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 254 | bool capped_goal_; |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 255 | CalibrationMode mode_; |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 256 | |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 257 | DISALLOW_COPY_AND_ASSIGN(ClawMotor); |
| 258 | }; |
| 259 | |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 260 | // Modifies the bottom and top goal such that they are within the limits and |
| 261 | // their separation isn't too much or little. |
| 262 | void LimitClawGoal(double *bottom_goal, double *top_goal, |
| 263 | const frc971::constants::Values &values); |
| 264 | |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 265 | } // namespace control_loops |
| 266 | } // namespace frc971 |
| 267 | |
| 268 | #endif // FRC971_CONTROL_LOOPS_CLAW_CLAW_H_ |