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