blob: 2d13d3783be09bc533dea687fe0ac88f461b6e7e [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_;
89 encoder_ = claw.position;
Austin Schuhf9286cd2014-02-11 00:51:09 -080090 }
91
Austin Schuhcda86af2014-02-16 16:16:39 -080092 double absolute_position() const { return encoder() + offset(); }
93
Brian Silvermane0a95462014-02-17 00:41:09 -080094 const HallEffectTracker &front() const { return front_; }
95 const HallEffectTracker &calibration() const { return calibration_; }
96 const HallEffectTracker &back() const { return back_; }
Austin Schuh4b7b5d02014-02-10 21:20:34 -080097
98 bool any_hall_effect_changed() const {
Brian Silvermane0a95462014-02-17 00:41:09 -080099 return front().either_count_changed() ||
100 calibration().either_count_changed() ||
101 back().either_count_changed();
102 }
103 bool front_or_back_triggered() const {
104 return front().value() || back().value();
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800105 }
106
Austin Schuhf9286cd2014-02-11 00:51:09 -0800107 double encoder() const { return encoder_; }
108 double last_encoder() const { return last_encoder_; }
109
Austin Schuhcda86af2014-02-16 16:16:39 -0800110 double offset() const { return offset_; }
111
Austin Schuhf9286cd2014-02-11 00:51:09 -0800112 // Returns true if an edge was detected in the last cycle and then sets the
113 // edge_position to the absolute position of the edge.
Austin Schuhd27931c2014-02-16 19:18:20 -0800114 bool GetPositionOfEdge(const constants::Values::Claws::Claw &claw,
Austin Schuhf9286cd2014-02-11 00:51:09 -0800115 double *edge_encoder, double *edge_angle);
116
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800117#undef COUNT_SETTER_GETTER
118
Austin Schuhcda86af2014-02-16 16:16:39 -0800119 protected:
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800120 // The accumulated voltage to apply to the motor.
Austin Schuhf9286cd2014-02-11 00:51:09 -0800121 double offset_;
Austin Schuhcda86af2014-02-16 16:16:39 -0800122 const char *name_;
Austin Schuhf9286cd2014-02-11 00:51:09 -0800123
Austin Schuhcda86af2014-02-16 16:16:39 -0800124 ClawMotor *motor_;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800125
Brian Silvermane0a95462014-02-17 00:41:09 -0800126 HallEffectTracker front_, calibration_, back_;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800127
128 JointZeroingState zeroing_state_;
Austin Schuhf9286cd2014-02-11 00:51:09 -0800129 double posedge_value_;
130 double negedge_value_;
131 double encoder_;
132 double last_encoder_;
Brian Silvermane0a95462014-02-17 00:41:09 -0800133
134 private:
135 // Does the edges of 1 sensor for GetPositionOfEdge.
136 bool DoGetPositionOfEdge(const constants::Values::Claws::AnglePair &angles,
137 double *edge_encoder, double *edge_angle,
138 const HallEffectTracker &sensor,
139 const char *hall_effect_name);
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800140};
141
Austin Schuhcda86af2014-02-16 16:16:39 -0800142class TopZeroedStateFeedbackLoop : public ZeroedStateFeedbackLoop {
143 public:
144 TopZeroedStateFeedbackLoop(ClawMotor *motor)
145 : ZeroedStateFeedbackLoop("top", motor) {}
146 // Sets the calibration offset given the absolute angle and the corrisponding
147 // encoder value.
148 void SetCalibration(double edge_encoder, double edge_angle);
149
Austin Schuhd27931c2014-02-16 19:18:20 -0800150 bool SetCalibrationOnEdge(const constants::Values::Claws::Claw &claw_values,
Austin Schuhcda86af2014-02-16 16:16:39 -0800151 JointZeroingState zeroing_state) {
152 double edge_encoder;
153 double edge_angle;
154 if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) {
155 LOG(INFO, "Calibration edge.\n");
156 SetCalibration(edge_encoder, edge_angle);
157 set_zeroing_state(zeroing_state);
158 return true;
159 }
160 return false;
161 }
162};
163
164class BottomZeroedStateFeedbackLoop : public ZeroedStateFeedbackLoop {
165 public:
166 BottomZeroedStateFeedbackLoop(ClawMotor *motor)
167 : ZeroedStateFeedbackLoop("bottom", motor) {}
168 // Sets the calibration offset given the absolute angle and the corrisponding
169 // encoder value.
170 void SetCalibration(double edge_encoder, double edge_angle);
171
Austin Schuhd27931c2014-02-16 19:18:20 -0800172 bool SetCalibrationOnEdge(const constants::Values::Claws::Claw &claw_values,
Austin Schuhcda86af2014-02-16 16:16:39 -0800173 JointZeroingState zeroing_state) {
174 double edge_encoder;
175 double edge_angle;
176 if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) {
177 LOG(INFO, "Calibration edge.\n");
178 SetCalibration(edge_encoder, edge_angle);
179 set_zeroing_state(zeroing_state);
180 return true;
181 }
182 return false;
183 }
184};
Ben Fredrickson9b388422014-02-13 06:15:31 +0000185
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800186class ClawMotor
187 : public aos::control_loops::ControlLoop<control_loops::ClawGroup> {
188 public:
189 explicit ClawMotor(control_loops::ClawGroup *my_claw =
190 &control_loops::claw_queue_group);
Austin Schuh3bb9a442014-02-02 16:01:45 -0800191
192 // True if the state machine is ready.
Austin Schuh4cb047f2014-02-16 21:10:19 -0800193 bool capped_goal() const { return capped_goal_; }
194
Austin Schuhe7f90d12014-02-17 00:48:25 -0800195 double uncapped_average_voltage() const {
196 return claw_.uncapped_average_voltage();
197 }
198
199 // True if the claw is zeroing.
200 bool is_zeroing() const;
201
Austin Schuh4cb047f2014-02-16 21:10:19 -0800202 // True if the state machine is ready.
Austin Schuhe7f90d12014-02-17 00:48:25 -0800203 bool is_ready() const;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800204
Austin Schuhcda86af2014-02-16 16:16:39 -0800205 void ChangeTopOffset(double doffset);
206 void ChangeBottomOffset(double doffset);
207
Austin Schuhe7f90d12014-02-17 00:48:25 -0800208 enum CalibrationMode {
209 READY,
210 PREP_FINE_TUNE_TOP,
211 FINE_TUNE_TOP,
212 PREP_FINE_TUNE_BOTTOM,
213 FINE_TUNE_BOTTOM,
214 UNKNOWN_LOCATION
215 };
216
217 CalibrationMode mode() const { return mode_; }
218
Austin Schuh3bb9a442014-02-02 16:01:45 -0800219 protected:
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800220 virtual void RunIteration(const control_loops::ClawGroup::Goal *goal,
221 const control_loops::ClawGroup::Position *position,
222 control_loops::ClawGroup::Output *output,
223 ::aos::control_loops::Status *status);
224
Austin Schuhcda86af2014-02-16 16:16:39 -0800225 double top_absolute_position() const {
226 return claw_.X_hat(1, 0) + claw_.X_hat(0, 0);
227 }
228 double bottom_absolute_position() const { return claw_.X_hat(0, 0); }
Austin Schuh3bb9a442014-02-02 16:01:45 -0800229
230 private:
231 // Friend the test classes for acces to the internal state.
Austin Schuhe7f90d12014-02-17 00:48:25 -0800232 friend class testing::WindupClawTest;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800233
234 // The zeroed joint to use.
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800235 bool has_top_claw_goal_;
236 double top_claw_goal_;
Austin Schuhcda86af2014-02-16 16:16:39 -0800237 TopZeroedStateFeedbackLoop top_claw_;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800238
239 bool has_bottom_claw_goal_;
240 double bottom_claw_goal_;
Austin Schuhcda86af2014-02-16 16:16:39 -0800241 BottomZeroedStateFeedbackLoop bottom_claw_;
242
243 // The claw loop.
244 ClawLimitedLoop claw_;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800245
246 bool was_enabled_;
Ben Fredrickson9b388422014-02-13 06:15:31 +0000247 bool doing_calibration_fine_tune_;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800248
Brian Silverman7c021c42014-02-17 15:15:56 -0800249 // The initial separation when disabled. Used as the safe separation
Austin Schuhcda86af2014-02-16 16:16:39 -0800250 // distance.
Austin Schuhe7f90d12014-02-17 00:48:25 -0800251 double initial_separation_;
Austin Schuhcda86af2014-02-16 16:16:39 -0800252
Austin Schuh4cb047f2014-02-16 21:10:19 -0800253 bool capped_goal_;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800254 CalibrationMode mode_;
Austin Schuh4cb047f2014-02-16 21:10:19 -0800255
Austin Schuh3bb9a442014-02-02 16:01:45 -0800256 DISALLOW_COPY_AND_ASSIGN(ClawMotor);
257};
258
Austin Schuh069143b2014-02-17 02:46:26 -0800259// Modifies the bottom and top goal such that they are within the limits and
260// their separation isn't too much or little.
261void LimitClawGoal(double *bottom_goal, double *top_goal,
262 const frc971::constants::Values &values);
263
Austin Schuh3bb9a442014-02-02 16:01:45 -0800264} // namespace control_loops
265} // namespace frc971
266
267#endif // FRC971_CONTROL_LOOPS_CLAW_CLAW_H_