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_; |
Austin Schuh | f84a130 | 2014-02-19 00:23:30 -0800 | [diff] [blame] | 89 | if (front().value() || calibration().value() || back().value()) { |
| 90 | last_on_encoder_ = encoder_; |
| 91 | } else { |
| 92 | last_off_encoder_ = encoder_; |
| 93 | } |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 94 | encoder_ = claw.position; |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Austin Schuh | f84a130 | 2014-02-19 00:23:30 -0800 | [diff] [blame] | 97 | void Reset() { |
| 98 | front_.Reset(); |
| 99 | calibration_.Reset(); |
| 100 | back_.Reset(); |
| 101 | } |
| 102 | |
| 103 | bool ready() { |
| 104 | return front_.ready() && calibration_.ready() && back_.ready(); |
| 105 | } |
| 106 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 107 | double absolute_position() const { return encoder() + offset(); } |
| 108 | |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 109 | const HallEffectTracker &front() const { return front_; } |
| 110 | const HallEffectTracker &calibration() const { return calibration_; } |
| 111 | const HallEffectTracker &back() const { return back_; } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 112 | |
| 113 | bool any_hall_effect_changed() const { |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 114 | return front().either_count_changed() || |
| 115 | calibration().either_count_changed() || |
| 116 | back().either_count_changed(); |
| 117 | } |
| 118 | bool front_or_back_triggered() const { |
| 119 | return front().value() || back().value(); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 122 | double encoder() const { return encoder_; } |
| 123 | double last_encoder() const { return last_encoder_; } |
| 124 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 125 | double offset() const { return offset_; } |
| 126 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 127 | // Returns true if an edge was detected in the last cycle and then sets the |
| 128 | // edge_position to the absolute position of the edge. |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 129 | bool GetPositionOfEdge(const constants::Values::Claws::Claw &claw, |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 130 | double *edge_encoder, double *edge_angle); |
| 131 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 132 | #undef COUNT_SETTER_GETTER |
| 133 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 134 | protected: |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 135 | // The accumulated voltage to apply to the motor. |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 136 | double offset_; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 137 | const char *name_; |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 138 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 139 | ClawMotor *motor_; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 140 | |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 141 | HallEffectTracker front_, calibration_, back_; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 142 | |
| 143 | JointZeroingState zeroing_state_; |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 144 | double posedge_value_; |
| 145 | double negedge_value_; |
| 146 | double encoder_; |
| 147 | double last_encoder_; |
Austin Schuh | f84a130 | 2014-02-19 00:23:30 -0800 | [diff] [blame] | 148 | double last_on_encoder_; |
| 149 | double last_off_encoder_; |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 150 | |
| 151 | private: |
| 152 | // Does the edges of 1 sensor for GetPositionOfEdge. |
| 153 | bool DoGetPositionOfEdge(const constants::Values::Claws::AnglePair &angles, |
| 154 | double *edge_encoder, double *edge_angle, |
| 155 | const HallEffectTracker &sensor, |
| 156 | const char *hall_effect_name); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 157 | }; |
| 158 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 159 | class TopZeroedStateFeedbackLoop : public ZeroedStateFeedbackLoop { |
| 160 | public: |
| 161 | TopZeroedStateFeedbackLoop(ClawMotor *motor) |
| 162 | : ZeroedStateFeedbackLoop("top", motor) {} |
| 163 | // Sets the calibration offset given the absolute angle and the corrisponding |
| 164 | // encoder value. |
| 165 | void SetCalibration(double edge_encoder, double edge_angle); |
| 166 | |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 167 | bool SetCalibrationOnEdge(const constants::Values::Claws::Claw &claw_values, |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 168 | JointZeroingState zeroing_state) { |
| 169 | double edge_encoder; |
| 170 | double edge_angle; |
| 171 | if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) { |
Austin Schuh | f84a130 | 2014-02-19 00:23:30 -0800 | [diff] [blame] | 172 | LOG(INFO, "Calibration edge edge should be %f.\n", edge_angle); |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 173 | SetCalibration(edge_encoder, edge_angle); |
| 174 | set_zeroing_state(zeroing_state); |
| 175 | return true; |
| 176 | } |
| 177 | return false; |
| 178 | } |
| 179 | }; |
| 180 | |
| 181 | class BottomZeroedStateFeedbackLoop : public ZeroedStateFeedbackLoop { |
| 182 | public: |
| 183 | BottomZeroedStateFeedbackLoop(ClawMotor *motor) |
| 184 | : ZeroedStateFeedbackLoop("bottom", motor) {} |
| 185 | // Sets the calibration offset given the absolute angle and the corrisponding |
| 186 | // encoder value. |
| 187 | void SetCalibration(double edge_encoder, double edge_angle); |
| 188 | |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 189 | bool SetCalibrationOnEdge(const constants::Values::Claws::Claw &claw_values, |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 190 | JointZeroingState zeroing_state) { |
| 191 | double edge_encoder; |
| 192 | double edge_angle; |
| 193 | if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) { |
| 194 | LOG(INFO, "Calibration edge.\n"); |
| 195 | SetCalibration(edge_encoder, edge_angle); |
| 196 | set_zeroing_state(zeroing_state); |
| 197 | return true; |
| 198 | } |
| 199 | return false; |
| 200 | } |
| 201 | }; |
Ben Fredrickson | 9b38842 | 2014-02-13 06:15:31 +0000 | [diff] [blame] | 202 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 203 | class ClawMotor |
| 204 | : public aos::control_loops::ControlLoop<control_loops::ClawGroup> { |
| 205 | public: |
| 206 | explicit ClawMotor(control_loops::ClawGroup *my_claw = |
| 207 | &control_loops::claw_queue_group); |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 208 | |
| 209 | // True if the state machine is ready. |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 210 | bool capped_goal() const { return capped_goal_; } |
| 211 | |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 212 | double uncapped_average_voltage() const { |
| 213 | return claw_.uncapped_average_voltage(); |
| 214 | } |
| 215 | |
| 216 | // True if the claw is zeroing. |
| 217 | bool is_zeroing() const; |
| 218 | |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 219 | // True if the state machine is ready. |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 220 | bool is_ready() const; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 221 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 222 | void ChangeTopOffset(double doffset); |
| 223 | void ChangeBottomOffset(double doffset); |
| 224 | |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 225 | enum CalibrationMode { |
| 226 | READY, |
| 227 | PREP_FINE_TUNE_TOP, |
| 228 | FINE_TUNE_TOP, |
| 229 | PREP_FINE_TUNE_BOTTOM, |
| 230 | FINE_TUNE_BOTTOM, |
| 231 | UNKNOWN_LOCATION |
| 232 | }; |
| 233 | |
| 234 | CalibrationMode mode() const { return mode_; } |
| 235 | |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 236 | protected: |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 237 | virtual void RunIteration(const control_loops::ClawGroup::Goal *goal, |
| 238 | const control_loops::ClawGroup::Position *position, |
| 239 | control_loops::ClawGroup::Output *output, |
| 240 | ::aos::control_loops::Status *status); |
| 241 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 242 | double top_absolute_position() const { |
| 243 | return claw_.X_hat(1, 0) + claw_.X_hat(0, 0); |
| 244 | } |
| 245 | double bottom_absolute_position() const { return claw_.X_hat(0, 0); } |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 246 | |
| 247 | private: |
| 248 | // Friend the test classes for acces to the internal state. |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 249 | friend class testing::WindupClawTest; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 250 | |
| 251 | // The zeroed joint to use. |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 252 | bool has_top_claw_goal_; |
| 253 | double top_claw_goal_; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 254 | TopZeroedStateFeedbackLoop top_claw_; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 255 | |
| 256 | bool has_bottom_claw_goal_; |
| 257 | double bottom_claw_goal_; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 258 | BottomZeroedStateFeedbackLoop bottom_claw_; |
| 259 | |
| 260 | // The claw loop. |
| 261 | ClawLimitedLoop claw_; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 262 | |
| 263 | bool was_enabled_; |
Ben Fredrickson | 9b38842 | 2014-02-13 06:15:31 +0000 | [diff] [blame] | 264 | bool doing_calibration_fine_tune_; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 265 | |
Brian Silverman | 7c021c4 | 2014-02-17 15:15:56 -0800 | [diff] [blame] | 266 | // The initial separation when disabled. Used as the safe separation |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 267 | // distance. |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 268 | double initial_separation_; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 269 | |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 270 | bool capped_goal_; |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 271 | CalibrationMode mode_; |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 272 | |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 273 | DISALLOW_COPY_AND_ASSIGN(ClawMotor); |
| 274 | }; |
| 275 | |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 276 | // Modifies the bottom and top goal such that they are within the limits and |
| 277 | // their separation isn't too much or little. |
| 278 | void LimitClawGoal(double *bottom_goal, double *top_goal, |
| 279 | const frc971::constants::Values &values); |
| 280 | |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 281 | } // namespace control_loops |
| 282 | } // namespace frc971 |
| 283 | |
| 284 | #endif // FRC971_CONTROL_LOOPS_CLAW_CLAW_H_ |