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 { |
| 16 | class ClawTest_NoWindupPositive_Test; |
| 17 | class ClawTest_NoWindupNegative_Test; |
| 18 | }; |
| 19 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 20 | // Note: Everything in this file assumes that there is a 1 cycle delay between |
| 21 | // power being requested and it showing up at the motor. It assumes that |
| 22 | // X_hat(2, 1) is the voltage being applied as well. It will go unstable if |
| 23 | // that isn't true. |
| 24 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 25 | class ClawLimitedLoop : public StateFeedbackLoop<4, 2, 2> { |
| 26 | public: |
| 27 | ClawLimitedLoop(StateFeedbackLoop<4, 2, 2> loop) |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 28 | : StateFeedbackLoop<4, 2, 2>(loop), |
| 29 | uncapped_average_voltage_(0.0), |
| 30 | is_zeroing_(true) {} |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 31 | virtual void CapU(); |
| 32 | |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 33 | void set_is_zeroing(bool is_zeroing) { is_zeroing_ = is_zeroing; } |
| 34 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 35 | void ChangeTopOffset(double doffset); |
| 36 | void ChangeBottomOffset(double doffset); |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 37 | |
| 38 | double uncapped_average_voltage() const { return uncapped_average_voltage_; } |
| 39 | |
| 40 | private: |
| 41 | double uncapped_average_voltage_; |
| 42 | bool is_zeroing_; |
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 | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 53 | ZeroedStateFeedbackLoop(const char *name, ClawMotor *motor) |
| 54 | : offset_(0.0), |
| 55 | name_(name), |
| 56 | motor_(motor), |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 57 | zeroing_state_(UNKNOWN_POSITION), |
| 58 | posedge_value_(0.0), |
| 59 | negedge_value_(0.0), |
| 60 | encoder_(0.0), |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 61 | last_encoder_(0.0) {} |
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 | const static int kZeroingMaxVoltage = 5; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 64 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 65 | enum JointZeroingState { |
| 66 | // We don't know where the joint is at all. |
| 67 | UNKNOWN_POSITION, |
| 68 | // We have an approximate position for where the claw is using. |
| 69 | APPROXIMATE_CALIBRATION, |
| 70 | // We observed the calibration edge while disabled. This is good enough for |
| 71 | // autonomous mode. |
| 72 | DISABLED_CALIBRATION, |
| 73 | // Ready for use during teleop. |
| 74 | CALIBRATED |
| 75 | }; |
| 76 | |
| 77 | void set_zeroing_state(JointZeroingState zeroing_state) { |
| 78 | zeroing_state_ = zeroing_state; |
| 79 | } |
| 80 | JointZeroingState zeroing_state() const { return zeroing_state_; } |
| 81 | |
Austin Schuh | 4339ebb | 2014-02-11 00:56:44 -0800 | [diff] [blame] | 82 | void SetPositionValues(const HalfClawPosition &claw) { |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame^] | 83 | front_.Update(claw.front); |
| 84 | calibration_.Update(claw.calibration); |
| 85 | back_.Update(claw.back); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 86 | |
| 87 | posedge_value_ = claw.posedge_value; |
| 88 | negedge_value_ = claw.negedge_value; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 89 | last_encoder_ = encoder_; |
| 90 | encoder_ = claw.position; |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 93 | double absolute_position() const { return encoder() + offset(); } |
| 94 | |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame^] | 95 | const HallEffectTracker &front() const { return front_; } |
| 96 | const HallEffectTracker &calibration() const { return calibration_; } |
| 97 | const HallEffectTracker &back() const { return back_; } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 98 | |
| 99 | bool any_hall_effect_changed() const { |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame^] | 100 | return front().either_count_changed() || |
| 101 | calibration().either_count_changed() || |
| 102 | back().either_count_changed(); |
| 103 | } |
| 104 | bool front_or_back_triggered() const { |
| 105 | return front().value() || back().value(); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 108 | double encoder() const { return encoder_; } |
| 109 | double last_encoder() const { return last_encoder_; } |
| 110 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 111 | double offset() const { return offset_; } |
| 112 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 113 | // Returns true if an edge was detected in the last cycle and then sets the |
| 114 | // edge_position to the absolute position of the edge. |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 115 | bool GetPositionOfEdge(const constants::Values::Claws::Claw &claw, |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 116 | double *edge_encoder, double *edge_angle); |
| 117 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 118 | #undef COUNT_SETTER_GETTER |
| 119 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 120 | protected: |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 121 | // The accumulated voltage to apply to the motor. |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 122 | double offset_; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 123 | const char *name_; |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 124 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 125 | ClawMotor *motor_; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 126 | |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame^] | 127 | HallEffectTracker front_, calibration_, back_; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 128 | |
| 129 | JointZeroingState zeroing_state_; |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 130 | double posedge_value_; |
| 131 | double negedge_value_; |
| 132 | double encoder_; |
| 133 | double last_encoder_; |
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 | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 152 | JointZeroingState zeroing_state) { |
| 153 | double edge_encoder; |
| 154 | double edge_angle; |
| 155 | if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) { |
| 156 | LOG(INFO, "Calibration edge.\n"); |
| 157 | SetCalibration(edge_encoder, edge_angle); |
| 158 | set_zeroing_state(zeroing_state); |
| 159 | return true; |
| 160 | } |
| 161 | return false; |
| 162 | } |
| 163 | }; |
| 164 | |
| 165 | class BottomZeroedStateFeedbackLoop : public ZeroedStateFeedbackLoop { |
| 166 | public: |
| 167 | BottomZeroedStateFeedbackLoop(ClawMotor *motor) |
| 168 | : ZeroedStateFeedbackLoop("bottom", motor) {} |
| 169 | // Sets the calibration offset given the absolute angle and the corrisponding |
| 170 | // encoder value. |
| 171 | void SetCalibration(double edge_encoder, double edge_angle); |
| 172 | |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 173 | bool SetCalibrationOnEdge(const constants::Values::Claws::Claw &claw_values, |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 174 | JointZeroingState zeroing_state) { |
| 175 | double edge_encoder; |
| 176 | double edge_angle; |
| 177 | if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) { |
| 178 | LOG(INFO, "Calibration edge.\n"); |
| 179 | SetCalibration(edge_encoder, edge_angle); |
| 180 | set_zeroing_state(zeroing_state); |
| 181 | return true; |
| 182 | } |
| 183 | return false; |
| 184 | } |
| 185 | }; |
Ben Fredrickson | 9b38842 | 2014-02-13 06:15:31 +0000 | [diff] [blame] | 186 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 187 | class ClawMotor |
| 188 | : public aos::control_loops::ControlLoop<control_loops::ClawGroup> { |
| 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 | |
| 196 | // True if the state machine is ready. |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 197 | bool is_ready() const { return false; } |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 198 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 199 | void ChangeTopOffset(double doffset); |
| 200 | void ChangeBottomOffset(double doffset); |
| 201 | |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 202 | protected: |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 203 | virtual void RunIteration(const control_loops::ClawGroup::Goal *goal, |
| 204 | const control_loops::ClawGroup::Position *position, |
| 205 | control_loops::ClawGroup::Output *output, |
| 206 | ::aos::control_loops::Status *status); |
| 207 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 208 | double top_absolute_position() const { |
| 209 | return claw_.X_hat(1, 0) + claw_.X_hat(0, 0); |
| 210 | } |
| 211 | double bottom_absolute_position() const { return claw_.X_hat(0, 0); } |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 212 | |
| 213 | private: |
| 214 | // Friend the test classes for acces to the internal state. |
| 215 | friend class testing::ClawTest_NoWindupPositive_Test; |
| 216 | friend class testing::ClawTest_NoWindupNegative_Test; |
| 217 | |
| 218 | // The zeroed joint to use. |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 219 | bool has_top_claw_goal_; |
| 220 | double top_claw_goal_; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 221 | TopZeroedStateFeedbackLoop top_claw_; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 222 | |
| 223 | bool has_bottom_claw_goal_; |
| 224 | double bottom_claw_goal_; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 225 | BottomZeroedStateFeedbackLoop bottom_claw_; |
| 226 | |
| 227 | // The claw loop. |
| 228 | ClawLimitedLoop claw_; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 229 | |
| 230 | bool was_enabled_; |
Ben Fredrickson | 9b38842 | 2014-02-13 06:15:31 +0000 | [diff] [blame] | 231 | bool doing_calibration_fine_tune_; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 232 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 233 | // The initial seperation when disabled. Used as the safe seperation |
| 234 | // distance. |
| 235 | double initial_seperation_; |
| 236 | |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 237 | bool capped_goal_; |
| 238 | |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 239 | DISALLOW_COPY_AND_ASSIGN(ClawMotor); |
| 240 | }; |
| 241 | |
| 242 | } // namespace control_loops |
| 243 | } // namespace frc971 |
| 244 | |
| 245 | #endif // FRC971_CONTROL_LOOPS_CLAW_CLAW_H_ |