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