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