blob: bff533d7c2869912e2e5f5f08a5f99827d84958b [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
86 posedge_value_ = claw.posedge_value;
87 negedge_value_ = claw.negedge_value;
Austin Schuhcda86af2014-02-16 16:16:39 -080088 last_encoder_ = encoder_;
Austin Schuhf84a1302014-02-19 00:23:30 -080089 if (front().value() || calibration().value() || back().value()) {
90 last_on_encoder_ = encoder_;
91 } else {
92 last_off_encoder_ = encoder_;
93 }
Austin Schuhcda86af2014-02-16 16:16:39 -080094 encoder_ = claw.position;
Austin Schuhf9286cd2014-02-11 00:51:09 -080095 }
96
Austin Schuhf84a1302014-02-19 00:23:30 -080097 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 Schuhcda86af2014-02-16 16:16:39 -0800107 double absolute_position() const { return encoder() + offset(); }
108
Brian Silvermane0a95462014-02-17 00:41:09 -0800109 const HallEffectTracker &front() const { return front_; }
110 const HallEffectTracker &calibration() const { return calibration_; }
111 const HallEffectTracker &back() const { return back_; }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800112
113 bool any_hall_effect_changed() const {
Brian Silvermane0a95462014-02-17 00:41:09 -0800114 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 Schuh4b7b5d02014-02-10 21:20:34 -0800120 }
121
Austin Schuhf9286cd2014-02-11 00:51:09 -0800122 double encoder() const { return encoder_; }
123 double last_encoder() const { return last_encoder_; }
124
Austin Schuhcda86af2014-02-16 16:16:39 -0800125 double offset() const { return offset_; }
126
Austin Schuhf9286cd2014-02-11 00:51:09 -0800127 // 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 Schuhd27931c2014-02-16 19:18:20 -0800129 bool GetPositionOfEdge(const constants::Values::Claws::Claw &claw,
Austin Schuhf9286cd2014-02-11 00:51:09 -0800130 double *edge_encoder, double *edge_angle);
131
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800132#undef COUNT_SETTER_GETTER
133
Austin Schuhcda86af2014-02-16 16:16:39 -0800134 protected:
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800135 // The accumulated voltage to apply to the motor.
Austin Schuhf9286cd2014-02-11 00:51:09 -0800136 double offset_;
Austin Schuhcda86af2014-02-16 16:16:39 -0800137 const char *name_;
Austin Schuhf9286cd2014-02-11 00:51:09 -0800138
Austin Schuhcda86af2014-02-16 16:16:39 -0800139 ClawMotor *motor_;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800140
Brian Silvermane0a95462014-02-17 00:41:09 -0800141 HallEffectTracker front_, calibration_, back_;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800142
143 JointZeroingState zeroing_state_;
Austin Schuhf9286cd2014-02-11 00:51:09 -0800144 double posedge_value_;
145 double negedge_value_;
146 double encoder_;
147 double last_encoder_;
Austin Schuhf84a1302014-02-19 00:23:30 -0800148 double last_on_encoder_;
149 double last_off_encoder_;
Brian Silvermane0a95462014-02-17 00:41:09 -0800150
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 Schuh4b7b5d02014-02-10 21:20:34 -0800157};
158
Austin Schuhcda86af2014-02-16 16:16:39 -0800159class 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 Schuhd27931c2014-02-16 19:18:20 -0800167 bool SetCalibrationOnEdge(const constants::Values::Claws::Claw &claw_values,
Austin Schuhcda86af2014-02-16 16:16:39 -0800168 JointZeroingState zeroing_state) {
169 double edge_encoder;
170 double edge_angle;
171 if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) {
Austin Schuhf84a1302014-02-19 00:23:30 -0800172 LOG(INFO, "Calibration edge edge should be %f.\n", edge_angle);
Austin Schuhcda86af2014-02-16 16:16:39 -0800173 SetCalibration(edge_encoder, edge_angle);
174 set_zeroing_state(zeroing_state);
175 return true;
176 }
177 return false;
178 }
179};
180
181class 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 Schuhd27931c2014-02-16 19:18:20 -0800189 bool SetCalibrationOnEdge(const constants::Values::Claws::Claw &claw_values,
Austin Schuhcda86af2014-02-16 16:16:39 -0800190 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 Fredrickson9b388422014-02-13 06:15:31 +0000202
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800203class 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 Schuh3bb9a442014-02-02 16:01:45 -0800208
209 // True if the state machine is ready.
Austin Schuh4cb047f2014-02-16 21:10:19 -0800210 bool capped_goal() const { return capped_goal_; }
211
Austin Schuhe7f90d12014-02-17 00:48:25 -0800212 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 Schuh4cb047f2014-02-16 21:10:19 -0800219 // True if the state machine is ready.
Austin Schuhe7f90d12014-02-17 00:48:25 -0800220 bool is_ready() const;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800221
Austin Schuhcda86af2014-02-16 16:16:39 -0800222 void ChangeTopOffset(double doffset);
223 void ChangeBottomOffset(double doffset);
224
Austin Schuhe7f90d12014-02-17 00:48:25 -0800225 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 Schuh3bb9a442014-02-02 16:01:45 -0800236 protected:
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800237 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 Schuhcda86af2014-02-16 16:16:39 -0800242 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 Schuh3bb9a442014-02-02 16:01:45 -0800246
247 private:
248 // Friend the test classes for acces to the internal state.
Austin Schuhe7f90d12014-02-17 00:48:25 -0800249 friend class testing::WindupClawTest;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800250
251 // The zeroed joint to use.
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800252 bool has_top_claw_goal_;
253 double top_claw_goal_;
Austin Schuhcda86af2014-02-16 16:16:39 -0800254 TopZeroedStateFeedbackLoop top_claw_;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800255
256 bool has_bottom_claw_goal_;
257 double bottom_claw_goal_;
Austin Schuhcda86af2014-02-16 16:16:39 -0800258 BottomZeroedStateFeedbackLoop bottom_claw_;
259
260 // The claw loop.
261 ClawLimitedLoop claw_;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800262
263 bool was_enabled_;
Ben Fredrickson9b388422014-02-13 06:15:31 +0000264 bool doing_calibration_fine_tune_;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800265
Brian Silverman7c021c42014-02-17 15:15:56 -0800266 // The initial separation when disabled. Used as the safe separation
Austin Schuhcda86af2014-02-16 16:16:39 -0800267 // distance.
Austin Schuhe7f90d12014-02-17 00:48:25 -0800268 double initial_separation_;
Austin Schuhcda86af2014-02-16 16:16:39 -0800269
Austin Schuh4cb047f2014-02-16 21:10:19 -0800270 bool capped_goal_;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800271 CalibrationMode mode_;
Austin Schuh4cb047f2014-02-16 21:10:19 -0800272
Austin Schuh3bb9a442014-02-02 16:01:45 -0800273 DISALLOW_COPY_AND_ASSIGN(ClawMotor);
274};
275
Austin Schuh069143b2014-02-17 02:46:26 -0800276// Modifies the bottom and top goal such that they are within the limits and
277// their separation isn't too much or little.
278void LimitClawGoal(double *bottom_goal, double *top_goal,
279 const frc971::constants::Values &values);
280
Austin Schuh3bb9a442014-02-02 16:01:45 -0800281} // namespace control_loops
282} // namespace frc971
283
284#endif // FRC971_CONTROL_LOOPS_CLAW_CLAW_H_