blob: 949b0f5ee51ab37490934d3636fdc41e56660c22 [file] [log] [blame]
Austin Schuh3bb9a442014-02-02 16:01:45 -08001#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 Schuhf9286cd2014-02-11 00:51:09 -08007#include "frc971/constants.h"
Austin Schuh3bb9a442014-02-02 16:01:45 -08008#include "frc971/control_loops/state_feedback_loop.h"
9#include "frc971/control_loops/claw/claw.q.h"
Austin Schuhcda86af2014-02-16 16:16:39 -080010#include "frc971/control_loops/claw/claw_motor_plant.h"
Brian Silvermane0a95462014-02-17 00:41:09 -080011#include "frc971/control_loops/hall_effect_tracker.h"
Austin Schuh3bb9a442014-02-02 16:01:45 -080012
Austin Schuh3bb9a442014-02-02 16:01:45 -080013namespace frc971 {
14namespace control_loops {
15namespace testing {
Austin Schuhe7f90d12014-02-17 00:48:25 -080016class WindupClawTest;
Austin Schuh3bb9a442014-02-02 16:01:45 -080017};
18
Austin Schuh4b7b5d02014-02-10 21:20:34 -080019// 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 Schuhcda86af2014-02-16 16:16:39 -080024class ClawLimitedLoop : public StateFeedbackLoop<4, 2, 2> {
25 public:
26 ClawLimitedLoop(StateFeedbackLoop<4, 2, 2> loop)
Austin Schuh4cb047f2014-02-16 21:10:19 -080027 : StateFeedbackLoop<4, 2, 2>(loop),
28 uncapped_average_voltage_(0.0),
29 is_zeroing_(true) {}
Austin Schuhcda86af2014-02-16 16:16:39 -080030 virtual void CapU();
31
Austin Schuh4cb047f2014-02-16 21:10:19 -080032 void set_is_zeroing(bool is_zeroing) { is_zeroing_ = is_zeroing; }
33
Austin Schuhcda86af2014-02-16 16:16:39 -080034 void ChangeTopOffset(double doffset);
35 void ChangeBottomOffset(double doffset);
Austin Schuh4cb047f2014-02-16 21:10:19 -080036
37 double uncapped_average_voltage() const { return uncapped_average_voltage_; }
38
39 private:
40 double uncapped_average_voltage_;
41 bool is_zeroing_;
Austin Schuhcda86af2014-02-16 16:16:39 -080042};
43
44class ClawMotor;
45
Austin Schuh4b7b5d02014-02-10 21:20:34 -080046// 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 Schuhcda86af2014-02-16 16:16:39 -080050class ZeroedStateFeedbackLoop {
Austin Schuh3bb9a442014-02-02 16:01:45 -080051 public:
Austin Schuhcda86af2014-02-16 16:16:39 -080052 ZeroedStateFeedbackLoop(const char *name, ClawMotor *motor)
53 : offset_(0.0),
54 name_(name),
55 motor_(motor),
Austin Schuhf9286cd2014-02-11 00:51:09 -080056 zeroing_state_(UNKNOWN_POSITION),
57 posedge_value_(0.0),
58 negedge_value_(0.0),
59 encoder_(0.0),
Austin Schuhcda86af2014-02-16 16:16:39 -080060 last_encoder_(0.0) {}
Austin Schuh3bb9a442014-02-02 16:01:45 -080061
Austin Schuh4b7b5d02014-02-10 21:20:34 -080062 const static int kZeroingMaxVoltage = 5;
Austin Schuh3bb9a442014-02-02 16:01:45 -080063
Austin Schuh4b7b5d02014-02-10 21:20:34 -080064 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 Schuh4339ebb2014-02-11 00:56:44 -080081 void SetPositionValues(const HalfClawPosition &claw) {
Brian Silvermane0a95462014-02-17 00:41:09 -080082 front_.Update(claw.front);
83 calibration_.Update(claw.calibration);
84 back_.Update(claw.back);
Austin Schuhf9286cd2014-02-11 00:51:09 -080085
Ben Fredricksonade3eab2014-02-22 07:30:53 +000086 if (any_hall_effect_changed()) {
87 min_current_hall_edge_ = claw.position;
88 max_current_hall_edge_ = claw.position;
89 } else if (claw.position > max_current_hall_effect_edge_) {
90 max_current_hall_effect_edge_ = claw.position;
91 } else if (claw.position < min_current_hall_effect_edge_) {
92 min_current_hall_effect_edge_ = claw.position;
93 }
94
Austin Schuhf9286cd2014-02-11 00:51:09 -080095 posedge_value_ = claw.posedge_value;
96 negedge_value_ = claw.negedge_value;
Austin Schuhcda86af2014-02-16 16:16:39 -080097 last_encoder_ = encoder_;
98 encoder_ = claw.position;
Austin Schuhf9286cd2014-02-11 00:51:09 -080099 }
100
Austin Schuhcda86af2014-02-16 16:16:39 -0800101 double absolute_position() const { return encoder() + offset(); }
102
Brian Silvermane0a95462014-02-17 00:41:09 -0800103 const HallEffectTracker &front() const { return front_; }
104 const HallEffectTracker &calibration() const { return calibration_; }
105 const HallEffectTracker &back() const { return back_; }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800106
107 bool any_hall_effect_changed() const {
Brian Silvermane0a95462014-02-17 00:41:09 -0800108 return front().either_count_changed() ||
109 calibration().either_count_changed() ||
110 back().either_count_changed();
111 }
112 bool front_or_back_triggered() const {
113 return front().value() || back().value();
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800114 }
115
Austin Schuhf9286cd2014-02-11 00:51:09 -0800116 double encoder() const { return encoder_; }
117 double last_encoder() const { return last_encoder_; }
118
Austin Schuhcda86af2014-02-16 16:16:39 -0800119 double offset() const { return offset_; }
120
Austin Schuhf9286cd2014-02-11 00:51:09 -0800121 // Returns true if an edge was detected in the last cycle and then sets the
122 // edge_position to the absolute position of the edge.
Austin Schuhd27931c2014-02-16 19:18:20 -0800123 bool GetPositionOfEdge(const constants::Values::Claws::Claw &claw,
Austin Schuhf9286cd2014-02-11 00:51:09 -0800124 double *edge_encoder, double *edge_angle);
125
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800126#undef COUNT_SETTER_GETTER
127
Austin Schuhcda86af2014-02-16 16:16:39 -0800128 protected:
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800129 // The accumulated voltage to apply to the motor.
Austin Schuhf9286cd2014-02-11 00:51:09 -0800130 double offset_;
Austin Schuhcda86af2014-02-16 16:16:39 -0800131 const char *name_;
Austin Schuhf9286cd2014-02-11 00:51:09 -0800132
Austin Schuhcda86af2014-02-16 16:16:39 -0800133 ClawMotor *motor_;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800134
Brian Silvermane0a95462014-02-17 00:41:09 -0800135 HallEffectTracker front_, calibration_, back_;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800136
137 JointZeroingState zeroing_state_;
Austin Schuhf9286cd2014-02-11 00:51:09 -0800138 double posedge_value_;
139 double negedge_value_;
140 double encoder_;
141 double last_encoder_;
Brian Silvermane0a95462014-02-17 00:41:09 -0800142
143 private:
144 // Does the edges of 1 sensor for GetPositionOfEdge.
145 bool DoGetPositionOfEdge(const constants::Values::Claws::AnglePair &angles,
146 double *edge_encoder, double *edge_angle,
147 const HallEffectTracker &sensor,
148 const char *hall_effect_name);
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800149};
150
Austin Schuhcda86af2014-02-16 16:16:39 -0800151class TopZeroedStateFeedbackLoop : public ZeroedStateFeedbackLoop {
152 public:
153 TopZeroedStateFeedbackLoop(ClawMotor *motor)
154 : ZeroedStateFeedbackLoop("top", motor) {}
155 // Sets the calibration offset given the absolute angle and the corrisponding
156 // encoder value.
157 void SetCalibration(double edge_encoder, double edge_angle);
158
Austin Schuhd27931c2014-02-16 19:18:20 -0800159 bool SetCalibrationOnEdge(const constants::Values::Claws::Claw &claw_values,
Austin Schuhcda86af2014-02-16 16:16:39 -0800160 JointZeroingState zeroing_state) {
161 double edge_encoder;
162 double edge_angle;
163 if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) {
164 LOG(INFO, "Calibration edge.\n");
165 SetCalibration(edge_encoder, edge_angle);
166 set_zeroing_state(zeroing_state);
167 return true;
168 }
169 return false;
170 }
171};
172
173class BottomZeroedStateFeedbackLoop : public ZeroedStateFeedbackLoop {
174 public:
175 BottomZeroedStateFeedbackLoop(ClawMotor *motor)
176 : ZeroedStateFeedbackLoop("bottom", motor) {}
177 // Sets the calibration offset given the absolute angle and the corrisponding
178 // encoder value.
179 void SetCalibration(double edge_encoder, double edge_angle);
180
Austin Schuhd27931c2014-02-16 19:18:20 -0800181 bool SetCalibrationOnEdge(const constants::Values::Claws::Claw &claw_values,
Austin Schuhcda86af2014-02-16 16:16:39 -0800182 JointZeroingState zeroing_state) {
183 double edge_encoder;
184 double edge_angle;
185 if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) {
186 LOG(INFO, "Calibration edge.\n");
187 SetCalibration(edge_encoder, edge_angle);
188 set_zeroing_state(zeroing_state);
189 return true;
190 }
191 return false;
192 }
193};
Ben Fredrickson9b388422014-02-13 06:15:31 +0000194
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800195class ClawMotor
196 : public aos::control_loops::ControlLoop<control_loops::ClawGroup> {
197 public:
198 explicit ClawMotor(control_loops::ClawGroup *my_claw =
199 &control_loops::claw_queue_group);
Austin Schuh3bb9a442014-02-02 16:01:45 -0800200
201 // True if the state machine is ready.
Austin Schuh4cb047f2014-02-16 21:10:19 -0800202 bool capped_goal() const { return capped_goal_; }
203
Austin Schuhe7f90d12014-02-17 00:48:25 -0800204 double uncapped_average_voltage() const {
205 return claw_.uncapped_average_voltage();
206 }
207
208 // True if the claw is zeroing.
209 bool is_zeroing() const;
210
Austin Schuh4cb047f2014-02-16 21:10:19 -0800211 // True if the state machine is ready.
Austin Schuhe7f90d12014-02-17 00:48:25 -0800212 bool is_ready() const;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800213
Austin Schuhcda86af2014-02-16 16:16:39 -0800214 void ChangeTopOffset(double doffset);
215 void ChangeBottomOffset(double doffset);
216
Austin Schuhe7f90d12014-02-17 00:48:25 -0800217 enum CalibrationMode {
218 READY,
219 PREP_FINE_TUNE_TOP,
220 FINE_TUNE_TOP,
221 PREP_FINE_TUNE_BOTTOM,
222 FINE_TUNE_BOTTOM,
223 UNKNOWN_LOCATION
224 };
225
226 CalibrationMode mode() const { return mode_; }
227
Austin Schuh3bb9a442014-02-02 16:01:45 -0800228 protected:
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800229 virtual void RunIteration(const control_loops::ClawGroup::Goal *goal,
230 const control_loops::ClawGroup::Position *position,
231 control_loops::ClawGroup::Output *output,
232 ::aos::control_loops::Status *status);
233
Austin Schuhcda86af2014-02-16 16:16:39 -0800234 double top_absolute_position() const {
235 return claw_.X_hat(1, 0) + claw_.X_hat(0, 0);
236 }
237 double bottom_absolute_position() const { return claw_.X_hat(0, 0); }
Austin Schuh3bb9a442014-02-02 16:01:45 -0800238
239 private:
240 // Friend the test classes for acces to the internal state.
Austin Schuhe7f90d12014-02-17 00:48:25 -0800241 friend class testing::WindupClawTest;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800242
243 // The zeroed joint to use.
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800244 bool has_top_claw_goal_;
245 double top_claw_goal_;
Austin Schuhcda86af2014-02-16 16:16:39 -0800246 TopZeroedStateFeedbackLoop top_claw_;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800247
248 bool has_bottom_claw_goal_;
249 double bottom_claw_goal_;
Austin Schuhcda86af2014-02-16 16:16:39 -0800250 BottomZeroedStateFeedbackLoop bottom_claw_;
251
252 // The claw loop.
253 ClawLimitedLoop claw_;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800254
255 bool was_enabled_;
Ben Fredrickson9b388422014-02-13 06:15:31 +0000256 bool doing_calibration_fine_tune_;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800257
Brian Silverman7c021c42014-02-17 15:15:56 -0800258 // The initial separation when disabled. Used as the safe separation
Austin Schuhcda86af2014-02-16 16:16:39 -0800259 // distance.
Austin Schuhe7f90d12014-02-17 00:48:25 -0800260 double initial_separation_;
Austin Schuhcda86af2014-02-16 16:16:39 -0800261
Austin Schuh4cb047f2014-02-16 21:10:19 -0800262 bool capped_goal_;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800263 CalibrationMode mode_;
Austin Schuh4cb047f2014-02-16 21:10:19 -0800264
Austin Schuh3bb9a442014-02-02 16:01:45 -0800265 DISALLOW_COPY_AND_ASSIGN(ClawMotor);
266};
267
Austin Schuh069143b2014-02-17 02:46:26 -0800268// Modifies the bottom and top goal such that they are within the limits and
269// their separation isn't too much or little.
270void LimitClawGoal(double *bottom_goal, double *top_goal,
271 const frc971::constants::Values &values);
272
Austin Schuh3bb9a442014-02-02 16:01:45 -0800273} // namespace control_loops
274} // namespace frc971
275
276#endif // FRC971_CONTROL_LOOPS_CLAW_CLAW_H_