blob: 00dca6f5e27a08ff28d5f588a28824652210ca37 [file] [log] [blame]
Austin Schuh3bb9a442014-02-02 16:01:45 -08001#include "frc971/control_loops/claw/claw.h"
2
3#include <stdio.h>
4
5#include <algorithm>
6
7#include "aos/common/control_loop/control_loops.q.h"
8#include "aos/common/logging/logging.h"
9
10#include "frc971/constants.h"
Austin Schuhcda86af2014-02-16 16:16:39 -080011#include "frc971/control_loops/claw/claw_motor_plant.h"
12
Austin Schuh3bb9a442014-02-02 16:01:45 -080013// Zeroing plan.
14// There are 2 types of zeros. Enabled and disabled ones.
15// Disabled ones are only valid during auto mode, and can be used to speed up
16// the enabled zero process. We need to re-zero during teleop in case the auto
17// zero was poor and causes us to miss all our shots.
18//
19// We need to be able to zero manually while disabled by moving the joint over
20// the zeros.
21// Zero on the down edge when disabled (gravity in the direction of motion)
22//
23// When enabled, zero on the up edge (gravity opposing the direction of motion)
24// The enabled sequence needs to work as follows. We can crash the claw if we
25// bring them too close to each other or too far from each other. The only safe
26// thing to do is to move them in unison.
27//
28// Start by moving them both towards the front of the bot to either find either
29// the middle hall effect on either jaw, or the front hall effect on the bottom
30// jaw. Any edge that isn't the desired edge will provide an approximate edge
31// location that can be used for the fine tuning step.
32// Once an edge is found on the front claw, move back the other way with both
33// claws until an edge is found for the other claw.
34// Now that we have an approximate zero, we can robustify the limits to keep
35// both claws safe. Then, we can move both claws to a position that is the
36// correct side of the zero and go zero.
37
38// Valid region plan.
39// Difference between the arms has a range, and the values of each arm has a range.
40// If a claw runs up against a static limit, don't let the goal change outside
41// the limit.
42// If a claw runs up against a movable limit, move both claws outwards to get
43// out of the condition.
44
45namespace frc971 {
46namespace control_loops {
47
Austin Schuh01c652b2014-02-21 23:13:42 -080048static const double kZeroingVoltage = 4.0;
49static const double kMaxVoltage = 12.0;
Austin Schuhf84a1302014-02-19 00:23:30 -080050
Austin Schuh27b8fb12014-02-22 15:10:05 -080051ClawLimitedLoop::ClawLimitedLoop(StateFeedbackLoop<4, 2, 2> loop)
52 : StateFeedbackLoop<4, 2, 2>(loop),
53 uncapped_average_voltage_(0.0),
54 is_zeroing_(true) {}
55
Austin Schuhcda86af2014-02-16 16:16:39 -080056void ClawLimitedLoop::CapU() {
Austin Schuh4cb047f2014-02-16 21:10:19 -080057 uncapped_average_voltage_ = U(0, 0) + U(1, 0) / 2.0;
58 if (is_zeroing_) {
Austin Schuhf84a1302014-02-19 00:23:30 -080059 LOG(DEBUG, "zeroing\n");
Austin Schuh4cb047f2014-02-16 21:10:19 -080060 const frc971::constants::Values &values = constants::GetValues();
61 if (uncapped_average_voltage_ > values.claw.max_zeroing_voltage) {
62 const double difference =
63 uncapped_average_voltage_ - values.claw.max_zeroing_voltage;
64 U(0, 0) -= difference;
Austin Schuh4cb047f2014-02-16 21:10:19 -080065 } else if (uncapped_average_voltage_ < -values.claw.max_zeroing_voltage) {
66 const double difference =
67 -uncapped_average_voltage_ - values.claw.max_zeroing_voltage;
68 U(0, 0) += difference;
Austin Schuh4cb047f2014-02-16 21:10:19 -080069 }
70 }
71
72 double max_value =
73 ::std::max(::std::abs(U(0, 0)), ::std::abs(U(1, 0) + U(0, 0)));
74
Austin Schuh01c652b2014-02-21 23:13:42 -080075 const double k_max_voltage = is_zeroing_ ? kZeroingVoltage : kMaxVoltage;
76 if (max_value > k_max_voltage) {
Austin Schuhcda86af2014-02-16 16:16:39 -080077 LOG(DEBUG, "Capping U because max is %f\n", max_value);
Austin Schuh01c652b2014-02-21 23:13:42 -080078 U = U * k_max_voltage / max_value;
Austin Schuhcda86af2014-02-16 16:16:39 -080079 LOG(DEBUG, "Capping U is now %f %f\n", U(0, 0), U(1, 0));
Austin Schuh4b7b5d02014-02-10 21:20:34 -080080 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -080081}
82
Austin Schuh27b8fb12014-02-22 15:10:05 -080083ZeroedStateFeedbackLoop::ZeroedStateFeedbackLoop(const char *name,
84 ClawMotor *motor)
85 : offset_(0.0),
86 name_(name),
87 motor_(motor),
88 zeroing_state_(UNKNOWN_POSITION),
89 posedge_value_(0.0),
90 negedge_value_(0.0),
91 encoder_(0.0),
92 last_encoder_(0.0) {}
93
94void ZeroedStateFeedbackLoop::SetPositionValues(const HalfClawPosition &claw) {
95 front_.Update(claw.front);
96 calibration_.Update(claw.calibration);
97 back_.Update(claw.back);
98
99 bool any_sensor_triggered = any_triggered();
100 if (any_sensor_triggered && any_triggered_last_) {
101 // We are still on the hall effect and nothing has changed.
102 min_hall_effect_on_angle_ =
103 ::std::min(min_hall_effect_on_angle_, claw.position);
104 max_hall_effect_on_angle_ =
105 ::std::max(max_hall_effect_on_angle_, claw.position);
106 } else if (!any_sensor_triggered && !any_triggered_last_) {
107 // We are still off the hall effect and nothing has changed.
108 min_hall_effect_off_angle_ =
109 ::std::min(min_hall_effect_off_angle_, claw.position);
110 max_hall_effect_off_angle_ =
111 ::std::max(max_hall_effect_off_angle_, claw.position);
112 } else if (any_sensor_triggered && !any_triggered_last_) {
113 // Saw a posedge on the hall effect. Reset the limits.
114 min_hall_effect_on_angle_ = ::std::min(claw.posedge_value, claw.position);
115 max_hall_effect_on_angle_ = ::std::max(claw.posedge_value, claw.position);
116 } else if (!any_sensor_triggered && any_triggered_last_) {
117 // Saw a negedge on the hall effect. Reset the limits.
118 min_hall_effect_off_angle_ = ::std::min(claw.negedge_value, claw.position);
119 max_hall_effect_off_angle_ = ::std::max(claw.negedge_value, claw.position);
120 }
121
122 posedge_value_ = claw.posedge_value;
123 negedge_value_ = claw.negedge_value;
124 last_encoder_ = encoder_;
125 if (front().value() || calibration().value() || back().value()) {
126 last_on_encoder_ = encoder_;
127 } else {
128 last_off_encoder_ = encoder_;
129 }
130 encoder_ = claw.position;
131 any_triggered_last_ = any_sensor_triggered;
132}
133
134void ZeroedStateFeedbackLoop::Reset(const HalfClawPosition &claw) {
135 set_zeroing_state(ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
136
137 front_.Reset();
138 calibration_.Reset();
139 back_.Reset();
140 // close up the min and max edge positions as they are no longer valid and
141 // will be expanded in future iterations
142 min_hall_effect_on_angle_ = claw.position;
143 max_hall_effect_on_angle_ = claw.position;
144 min_hall_effect_off_angle_ = claw.position;
145 max_hall_effect_off_angle_ = claw.position;
146 any_triggered_last_ = any_triggered();
147}
148
149bool TopZeroedStateFeedbackLoop::SetCalibrationOnEdge(
150 const constants::Values::Claws::Claw &claw_values,
151 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 edge should be %f.\n", edge_angle);
156 SetCalibration(edge_encoder, edge_angle);
157 set_zeroing_state(zeroing_state);
158 return true;
159 }
160 return false;
161}
162
163bool BottomZeroedStateFeedbackLoop::SetCalibrationOnEdge(
164 const constants::Values::Claws::Claw &claw_values,
165 JointZeroingState zeroing_state) {
166 double edge_encoder;
167 double edge_angle;
168 if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) {
169 LOG(INFO, "Calibration edge.\n");
170 SetCalibration(edge_encoder, edge_angle);
171 set_zeroing_state(zeroing_state);
172 return true;
173 }
174 return false;
175}
176
Austin Schuhcc0bf312014-02-09 00:39:29 -0800177ClawMotor::ClawMotor(control_loops::ClawGroup *my_claw)
178 : aos::control_loops::ControlLoop<control_loops::ClawGroup>(my_claw),
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800179 has_top_claw_goal_(false),
180 top_claw_goal_(0.0),
Austin Schuhcda86af2014-02-16 16:16:39 -0800181 top_claw_(this),
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800182 has_bottom_claw_goal_(false),
183 bottom_claw_goal_(0.0),
Austin Schuhcda86af2014-02-16 16:16:39 -0800184 bottom_claw_(this),
185 claw_(MakeClawLoop()),
Ben Fredrickson9b388422014-02-13 06:15:31 +0000186 was_enabled_(false),
Austin Schuh4cb047f2014-02-16 21:10:19 -0800187 doing_calibration_fine_tune_(false),
Austin Schuhe7f90d12014-02-17 00:48:25 -0800188 capped_goal_(false),
189 mode_(UNKNOWN_LOCATION) {}
Austin Schuh3bb9a442014-02-02 16:01:45 -0800190
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800191const int ZeroedStateFeedbackLoop::kZeroingMaxVoltage;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800192
Brian Silvermane0a95462014-02-17 00:41:09 -0800193bool ZeroedStateFeedbackLoop::DoGetPositionOfEdge(
194 const constants::Values::Claws::AnglePair &angles, double *edge_encoder,
195 double *edge_angle, const HallEffectTracker &sensor,
196 const char *hall_effect_name) {
Austin Schuhf84a1302014-02-19 00:23:30 -0800197 bool found_edge = false;
Austin Schuh27b8fb12014-02-22 15:10:05 -0800198
Brian Silvermane0a95462014-02-17 00:41:09 -0800199 if (sensor.posedge_count_changed()) {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800200 if (min_hall_effect_off_angle_ == max_hall_effect_off_angle_) {
Ben Fredricksonade3eab2014-02-22 07:30:53 +0000201 // we oddly got two of the same edge.
202 *edge_angle = last_edge_value_;
Austin Schuh27b8fb12014-02-22 15:10:05 -0800203 found_edge = true;
Brian Silvermane0a95462014-02-17 00:41:09 -0800204 } else {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800205 const double average_last_encoder =
206 (min_hall_effect_off_angle_ + max_hall_effect_off_angle_) / 2.0;
207 if (posedge_value_ < average_last_encoder) {
208 *edge_angle = angles.upper_decreasing_angle;
209 LOG(INFO, "%s Posedge upper of %s -> %f posedge: %f avg_encoder: %f\n",
210 name_, hall_effect_name, *edge_angle, posedge_value_,
211 average_last_encoder);
212 } else {
213 *edge_angle = angles.lower_angle;
214 LOG(INFO, "%s Posedge lower of %s -> %f posedge: %f avg_encoder: %f\n",
215 name_, hall_effect_name, *edge_angle, posedge_value_,
216 average_last_encoder);
217 }
Brian Silvermane0a95462014-02-17 00:41:09 -0800218 }
219 *edge_encoder = posedge_value_;
Austin Schuhf84a1302014-02-19 00:23:30 -0800220 found_edge = true;
Brian Silvermane0a95462014-02-17 00:41:09 -0800221 }
222 if (sensor.negedge_count_changed()) {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800223 if (min_hall_effect_on_angle_ == max_hall_effect_on_angle_) {
Ben Fredricksonade3eab2014-02-22 07:30:53 +0000224 *edge_angle = last_edge_value_;
Austin Schuh27b8fb12014-02-22 15:10:05 -0800225 found_edge = true;
Brian Silvermane0a95462014-02-17 00:41:09 -0800226 } else {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800227 const double average_last_encoder =
228 (min_hall_effect_on_angle_ + max_hall_effect_on_angle_) / 2.0;
229 if (negedge_value_ > average_last_encoder) {
230 *edge_angle = angles.upper_angle;
231 LOG(INFO, "%s Negedge upper of %s -> %f negedge: %f avg_encoder: %f\n",
232 name_, hall_effect_name, *edge_angle, negedge_value_,
233 average_last_encoder);
234 } else {
235 *edge_angle = angles.lower_decreasing_angle;
236 LOG(INFO, "%s Negedge lower of %s -> %f negedge: %f avg_encoder: %f\n",
237 name_, hall_effect_name, *edge_angle, negedge_value_,
238 average_last_encoder);
239 }
240 *edge_encoder = negedge_value_;
Brian Silvermane0a95462014-02-17 00:41:09 -0800241 }
Austin Schuhf84a1302014-02-19 00:23:30 -0800242 found_edge = true;
Brian Silvermane0a95462014-02-17 00:41:09 -0800243 }
244
Austin Schuh27b8fb12014-02-22 15:10:05 -0800245 if (found_edge) {
246 last_edge_value_ = *edge_angle;
247 }
248
Austin Schuhf84a1302014-02-19 00:23:30 -0800249 return found_edge;
Brian Silvermane0a95462014-02-17 00:41:09 -0800250}
251
Austin Schuhf9286cd2014-02-11 00:51:09 -0800252bool ZeroedStateFeedbackLoop::GetPositionOfEdge(
Austin Schuhd27931c2014-02-16 19:18:20 -0800253 const constants::Values::Claws::Claw &claw_values, double *edge_encoder,
Austin Schuhf9286cd2014-02-11 00:51:09 -0800254 double *edge_angle) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800255 // TODO(austin): Validate that the hall effect edge makes sense.
256 // We must now be on the side of the edge that we expect to be, and the
257 // encoder must have been on either side of the edge before and after.
258
Austin Schuhcda86af2014-02-16 16:16:39 -0800259 // TODO(austin): Compute the last off range min and max and compare the edge
260 // value to the middle of the range. This will be quite a bit more reliable.
261
Brian Silvermane0a95462014-02-17 00:41:09 -0800262 if (DoGetPositionOfEdge(claw_values.front, edge_encoder, edge_angle,
263 front_, "front")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800264 return true;
265 }
Brian Silvermane0a95462014-02-17 00:41:09 -0800266 if (DoGetPositionOfEdge(claw_values.calibration, edge_encoder, edge_angle,
267 calibration_, "calibration")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800268 return true;
269 }
Brian Silvermane0a95462014-02-17 00:41:09 -0800270 if (DoGetPositionOfEdge(claw_values.back, edge_encoder, edge_angle,
271 back_, "back")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800272 return true;
273 }
274 return false;
275}
276
Austin Schuhcda86af2014-02-16 16:16:39 -0800277void TopZeroedStateFeedbackLoop::SetCalibration(double edge_encoder,
278 double edge_angle) {
279 double old_offset = offset_;
280 offset_ = edge_angle - edge_encoder;
281 const double doffset = offset_ - old_offset;
282 motor_->ChangeTopOffset(doffset);
283}
284
285void BottomZeroedStateFeedbackLoop::SetCalibration(double edge_encoder,
286 double edge_angle) {
287 double old_offset = offset_;
288 offset_ = edge_angle - edge_encoder;
289 const double doffset = offset_ - old_offset;
290 motor_->ChangeBottomOffset(doffset);
291}
292
293void ClawMotor::ChangeTopOffset(double doffset) {
294 claw_.ChangeTopOffset(doffset);
295 if (has_top_claw_goal_) {
296 top_claw_goal_ += doffset;
297 }
298}
299
300void ClawMotor::ChangeBottomOffset(double doffset) {
301 claw_.ChangeBottomOffset(doffset);
302 if (has_bottom_claw_goal_) {
303 bottom_claw_goal_ += doffset;
304 }
305}
306
307void ClawLimitedLoop::ChangeTopOffset(double doffset) {
308 Y_(1, 0) += doffset;
309 X_hat(1, 0) += doffset;
310 LOG(INFO, "Changing top offset by %f\n", doffset);
311}
312void ClawLimitedLoop::ChangeBottomOffset(double doffset) {
313 Y_(0, 0) += doffset;
314 X_hat(0, 0) += doffset;
315 X_hat(1, 0) -= doffset;
316 LOG(INFO, "Changing bottom offset by %f\n", doffset);
317}
joe7376ff52014-02-16 18:28:42 -0800318
Austin Schuh069143b2014-02-17 02:46:26 -0800319void LimitClawGoal(double *bottom_goal, double *top_goal,
320 const frc971::constants::Values &values) {
321 // first update position based on angle limit
322
323 const double separation = *top_goal - *bottom_goal;
324 if (separation > values.claw.claw_max_separation) {
325 LOG(DEBUG, "Greater than\n");
326 const double dsep = (separation - values.claw.claw_max_separation) / 2.0;
327 *bottom_goal += dsep;
328 *top_goal -= dsep;
329 LOG(DEBUG, "Goals now bottom: %f, top: %f\n", *bottom_goal, *top_goal);
330 }
331 if (separation < values.claw.claw_min_separation) {
332 LOG(DEBUG, "Less than\n");
333 const double dsep = (separation - values.claw.claw_min_separation) / 2.0;
334 *bottom_goal += dsep;
335 *top_goal -= dsep;
336 LOG(DEBUG, "Goals now bottom: %f, top: %f\n", *bottom_goal, *top_goal);
337 }
338
339 // now move both goals in unison
340 if (*bottom_goal < values.claw.lower_claw.lower_limit) {
341 *top_goal += values.claw.lower_claw.lower_limit - *bottom_goal;
342 *bottom_goal = values.claw.lower_claw.lower_limit;
343 }
344 if (*bottom_goal > values.claw.lower_claw.upper_limit) {
345 *top_goal -= *bottom_goal - values.claw.lower_claw.upper_limit;
346 *bottom_goal = values.claw.lower_claw.upper_limit;
347 }
348
349 if (*top_goal < values.claw.upper_claw.lower_limit) {
350 *bottom_goal += values.claw.upper_claw.lower_limit - *top_goal;
351 *top_goal = values.claw.upper_claw.lower_limit;
352 }
353 if (*top_goal > values.claw.upper_claw.upper_limit) {
354 *bottom_goal -= *top_goal - values.claw.upper_claw.upper_limit;
355 *top_goal = values.claw.upper_claw.upper_limit;
356 }
357}
Austin Schuhcda86af2014-02-16 16:16:39 -0800358
Austin Schuhe7f90d12014-02-17 00:48:25 -0800359bool ClawMotor::is_ready() const {
360 return (
361 (top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED &&
362 bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) ||
363 (::aos::robot_state->autonomous &&
364 ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
365 top_claw_.zeroing_state() ==
366 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
367 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
368 bottom_claw_.zeroing_state() ==
369 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION))));
370}
371
372bool ClawMotor::is_zeroing() const { return !is_ready(); }
373
Austin Schuh3bb9a442014-02-02 16:01:45 -0800374// Positive angle is up, and positive power is up.
Austin Schuhcc0bf312014-02-09 00:39:29 -0800375void ClawMotor::RunIteration(const control_loops::ClawGroup::Goal *goal,
376 const control_loops::ClawGroup::Position *position,
377 control_loops::ClawGroup::Output *output,
Austin Schuh3bb9a442014-02-02 16:01:45 -0800378 ::aos::control_loops::Status *status) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800379 constexpr double dt = 0.01;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800380
381 // Disable the motors now so that all early returns will return with the
382 // motors disabled.
383 if (output) {
384 output->top_claw_voltage = 0;
385 output->bottom_claw_voltage = 0;
386 output->intake_voltage = 0;
387 }
388
Austin Schuh1a499942014-02-17 01:51:58 -0800389 if (reset()) {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800390 top_claw_.Reset(position->top);
391 bottom_claw_.Reset(position->bottom);
Austin Schuh1a499942014-02-17 01:51:58 -0800392 }
393
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800394 if (::aos::robot_state.get() == nullptr) {
395 return;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800396 }
397
Austin Schuhf9286cd2014-02-11 00:51:09 -0800398 const frc971::constants::Values &values = constants::GetValues();
399
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800400 if (position) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800401 Eigen::Matrix<double, 2, 1> Y;
402 Y << position->bottom.position + bottom_claw_.offset(),
403 position->top.position + top_claw_.offset();
404 claw_.Correct(Y);
405
Austin Schuhf9286cd2014-02-11 00:51:09 -0800406 top_claw_.SetPositionValues(position->top);
407 bottom_claw_.SetPositionValues(position->bottom);
408
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800409 if (!has_top_claw_goal_) {
410 has_top_claw_goal_ = true;
Austin Schuhcda86af2014-02-16 16:16:39 -0800411 top_claw_goal_ = top_claw_.absolute_position();
Austin Schuhe7f90d12014-02-17 00:48:25 -0800412 initial_separation_ =
Austin Schuhcda86af2014-02-16 16:16:39 -0800413 top_claw_.absolute_position() - bottom_claw_.absolute_position();
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800414 }
415 if (!has_bottom_claw_goal_) {
416 has_bottom_claw_goal_ = true;
Austin Schuhcda86af2014-02-16 16:16:39 -0800417 bottom_claw_goal_ = bottom_claw_.absolute_position();
Austin Schuhe7f90d12014-02-17 00:48:25 -0800418 initial_separation_ =
Austin Schuhcda86af2014-02-16 16:16:39 -0800419 top_claw_.absolute_position() - bottom_claw_.absolute_position();
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800420 }
Austin Schuhcda86af2014-02-16 16:16:39 -0800421 LOG(DEBUG, "Claw position is (top: %f bottom: %f\n",
422 top_claw_.absolute_position(), bottom_claw_.absolute_position());
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800423 }
424
Austin Schuh069143b2014-02-17 02:46:26 -0800425 const bool autonomous = ::aos::robot_state->autonomous;
426 const bool enabled = ::aos::robot_state->enabled;
427
428 double bottom_claw_velocity_ = 0.0;
429 double top_claw_velocity_ = 0.0;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800430
431 if ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED &&
432 bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) ||
433 (autonomous &&
434 ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
435 top_claw_.zeroing_state() ==
436 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
437 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
438 bottom_claw_.zeroing_state() ==
439 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION)))) {
440 // Ready to use the claw.
441 // Limit the goals here.
Austin Schuhf9286cd2014-02-11 00:51:09 -0800442 bottom_claw_goal_ = goal->bottom_angle;
Brian Silverman7c021c42014-02-17 15:15:56 -0800443 top_claw_goal_ = goal->bottom_angle + goal->separation_angle;
Austin Schuhcda86af2014-02-16 16:16:39 -0800444 has_bottom_claw_goal_ = true;
445 has_top_claw_goal_ = true;
446 doing_calibration_fine_tune_ = false;
447
Austin Schuhe7f90d12014-02-17 00:48:25 -0800448 mode_ = READY;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800449 } else if (top_claw_.zeroing_state() !=
450 ZeroedStateFeedbackLoop::UNKNOWN_POSITION &&
451 bottom_claw_.zeroing_state() !=
452 ZeroedStateFeedbackLoop::UNKNOWN_POSITION) {
453 // Time to fine tune the zero.
454 // Limit the goals here.
Austin Schuh0c733422014-02-17 01:17:12 -0800455 if (!enabled) {
456 // If we are disabled, start the fine tune process over again.
457 doing_calibration_fine_tune_ = false;
458 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800459 if (bottom_claw_.zeroing_state() != ZeroedStateFeedbackLoop::CALIBRATED) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800460 // always get the bottom claw to calibrated first
461 LOG(DEBUG, "Calibrating the bottom of the claw\n");
462 if (!doing_calibration_fine_tune_) {
463 if (::std::abs(bottom_absolute_position() -
Austin Schuhd27931c2014-02-16 19:18:20 -0800464 values.claw.start_fine_tune_pos) <
465 values.claw.claw_unimportant_epsilon) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800466 doing_calibration_fine_tune_ = true;
Austin Schuhd27931c2014-02-16 19:18:20 -0800467 bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800468 top_claw_velocity_ = bottom_claw_velocity_ =
469 values.claw.claw_zeroing_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800470 LOG(DEBUG, "Ready to fine tune the bottom\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800471 mode_ = FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800472 } else {
473 // send bottom to zeroing start
Austin Schuhd27931c2014-02-16 19:18:20 -0800474 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuhcda86af2014-02-16 16:16:39 -0800475 LOG(DEBUG, "Going to the start position for the bottom\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800476 mode_ = PREP_FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800477 }
478 } else {
Austin Schuhe7f90d12014-02-17 00:48:25 -0800479 mode_ = FINE_TUNE_BOTTOM;
Austin Schuhd27931c2014-02-16 19:18:20 -0800480 bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800481 top_claw_velocity_ = bottom_claw_velocity_ =
482 values.claw.claw_zeroing_speed;
Brian Silvermane0a95462014-02-17 00:41:09 -0800483 if (top_claw_.front_or_back_triggered() ||
484 bottom_claw_.front_or_back_triggered()) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800485 // We shouldn't hit a limit, but if we do, go back to the zeroing
486 // point and try again.
487 doing_calibration_fine_tune_ = false;
Austin Schuhd27931c2014-02-16 19:18:20 -0800488 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuh069143b2014-02-17 02:46:26 -0800489 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
Austin Schuhcda86af2014-02-16 16:16:39 -0800490 LOG(DEBUG, "Found a limit, starting over.\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800491 mode_ = PREP_FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800492 }
Austin Schuh288c8c32014-02-16 17:20:17 -0800493
Brian Silvermane0a95462014-02-17 00:41:09 -0800494 if (bottom_claw_.calibration().value()) {
495 if (bottom_claw_.calibration().posedge_count_changed() &&
Austin Schuh288c8c32014-02-16 17:20:17 -0800496 position) {
497 // do calibration
498 bottom_claw_.SetCalibration(
499 position->bottom.posedge_value,
Austin Schuhd27931c2014-02-16 19:18:20 -0800500 values.claw.lower_claw.calibration.lower_angle);
Austin Schuh288c8c32014-02-16 17:20:17 -0800501 bottom_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED);
502 // calibrated so we are done fine tuning bottom
503 doing_calibration_fine_tune_ = false;
504 LOG(DEBUG, "Calibrated the bottom correctly!\n");
505 } else {
506 doing_calibration_fine_tune_ = false;
Austin Schuhd27931c2014-02-16 19:18:20 -0800507 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuh069143b2014-02-17 02:46:26 -0800508 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800509 mode_ = PREP_FINE_TUNE_BOTTOM;
Austin Schuh288c8c32014-02-16 17:20:17 -0800510 }
Austin Schuhcda86af2014-02-16 16:16:39 -0800511 } else {
512 LOG(DEBUG, "Fine tuning\n");
513 }
514 }
515 // now set the top claw to track
516
Austin Schuhd27931c2014-02-16 19:18:20 -0800517 top_claw_goal_ = bottom_claw_goal_ + values.claw.claw_zeroing_separation;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800518 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800519 // bottom claw must be calibrated, start on the top
520 if (!doing_calibration_fine_tune_) {
Austin Schuhd27931c2014-02-16 19:18:20 -0800521 if (::std::abs(top_absolute_position() -
522 values.claw.start_fine_tune_pos) <
523 values.claw.claw_unimportant_epsilon) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800524 doing_calibration_fine_tune_ = true;
Austin Schuhd27931c2014-02-16 19:18:20 -0800525 top_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800526 top_claw_velocity_ = bottom_claw_velocity_ =
527 values.claw.claw_zeroing_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800528 LOG(DEBUG, "Ready to fine tune the top\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800529 mode_ = FINE_TUNE_TOP;
Austin Schuhcda86af2014-02-16 16:16:39 -0800530 } else {
531 // send top to zeroing start
Austin Schuhd27931c2014-02-16 19:18:20 -0800532 top_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuhcda86af2014-02-16 16:16:39 -0800533 LOG(DEBUG, "Going to the start position for the top\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800534 mode_ = PREP_FINE_TUNE_TOP;
Austin Schuhcda86af2014-02-16 16:16:39 -0800535 }
536 } else {
Austin Schuhe7f90d12014-02-17 00:48:25 -0800537 mode_ = FINE_TUNE_TOP;
Austin Schuhd27931c2014-02-16 19:18:20 -0800538 top_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800539 top_claw_velocity_ = bottom_claw_velocity_ =
540 values.claw.claw_zeroing_speed;
Brian Silvermane0a95462014-02-17 00:41:09 -0800541 if (top_claw_.front_or_back_triggered() ||
542 bottom_claw_.front_or_back_triggered()) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800543 // this should not happen, but now we know it won't
544 doing_calibration_fine_tune_ = false;
Austin Schuhd27931c2014-02-16 19:18:20 -0800545 top_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuh069143b2014-02-17 02:46:26 -0800546 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
Austin Schuhcda86af2014-02-16 16:16:39 -0800547 LOG(DEBUG, "Found a limit, starting over.\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800548 mode_ = PREP_FINE_TUNE_TOP;
Austin Schuhcda86af2014-02-16 16:16:39 -0800549 }
Brian Silvermane0a95462014-02-17 00:41:09 -0800550 if (top_claw_.calibration().value()) {
551 if (top_claw_.calibration().posedge_count_changed() &&
Austin Schuh288c8c32014-02-16 17:20:17 -0800552 position) {
553 // do calibration
Austin Schuhd27931c2014-02-16 19:18:20 -0800554 top_claw_.SetCalibration(
555 position->top.posedge_value,
556 values.claw.upper_claw.calibration.lower_angle);
Austin Schuh288c8c32014-02-16 17:20:17 -0800557 top_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED);
Austin Schuhe7f90d12014-02-17 00:48:25 -0800558 // calibrated so we are done fine tuning top
Austin Schuh288c8c32014-02-16 17:20:17 -0800559 doing_calibration_fine_tune_ = false;
560 LOG(DEBUG, "Calibrated the top correctly!\n");
561 } else {
562 doing_calibration_fine_tune_ = false;
Austin Schuhd27931c2014-02-16 19:18:20 -0800563 top_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuh069143b2014-02-17 02:46:26 -0800564 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800565 mode_ = PREP_FINE_TUNE_TOP;
Austin Schuh288c8c32014-02-16 17:20:17 -0800566 }
Austin Schuhcda86af2014-02-16 16:16:39 -0800567 }
568 }
569 // now set the bottom claw to track
Austin Schuhd27931c2014-02-16 19:18:20 -0800570 bottom_claw_goal_ = top_claw_goal_ - values.claw.claw_zeroing_separation;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800571 }
572 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800573 doing_calibration_fine_tune_ = false;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800574 if (!was_enabled_ && enabled) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800575 if (position) {
576 top_claw_goal_ = position->top.position;
577 bottom_claw_goal_ = position->bottom.position;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800578 initial_separation_ =
Austin Schuhcda86af2014-02-16 16:16:39 -0800579 position->top.position - position->bottom.position;
Austin Schuhf9286cd2014-02-11 00:51:09 -0800580 } else {
581 has_top_claw_goal_ = false;
582 has_bottom_claw_goal_ = false;
583 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800584 }
Austin Schuhf9286cd2014-02-11 00:51:09 -0800585
Austin Schuh4cb047f2014-02-16 21:10:19 -0800586 if ((bottom_claw_.zeroing_state() !=
587 ZeroedStateFeedbackLoop::UNKNOWN_POSITION ||
Brian Silvermane0a95462014-02-17 00:41:09 -0800588 bottom_claw_.front().value() || top_claw_.front().value()) &&
589 !top_claw_.back().value() && !bottom_claw_.back().value()) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800590 if (enabled) {
591 // Time to slowly move back up to find any position to narrow down the
592 // zero.
Austin Schuhd27931c2014-02-16 19:18:20 -0800593 top_claw_goal_ += values.claw.claw_zeroing_off_speed * dt;
594 bottom_claw_goal_ += values.claw.claw_zeroing_off_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800595 top_claw_velocity_ = bottom_claw_velocity_ =
596 values.claw.claw_zeroing_off_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800597 LOG(DEBUG, "Bottom is known.\n");
Austin Schuhf9286cd2014-02-11 00:51:09 -0800598 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800599 } else {
600 // We don't know where either claw is. Slowly start moving down to find
601 // any hall effect.
Austin Schuhf9286cd2014-02-11 00:51:09 -0800602 if (enabled) {
Austin Schuhd27931c2014-02-16 19:18:20 -0800603 top_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt;
604 bottom_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800605 top_claw_velocity_ = bottom_claw_velocity_ =
606 -values.claw.claw_zeroing_off_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800607 LOG(DEBUG, "Both are unknown.\n");
Austin Schuhf9286cd2014-02-11 00:51:09 -0800608 }
609 }
610
611 if (enabled) {
612 top_claw_.SetCalibrationOnEdge(
Austin Schuhd27931c2014-02-16 19:18:20 -0800613 values.claw.upper_claw, ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800614 bottom_claw_.SetCalibrationOnEdge(
Austin Schuhd27931c2014-02-16 19:18:20 -0800615 values.claw.lower_claw, ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800616 } else {
Austin Schuh069143b2014-02-17 02:46:26 -0800617 // TODO(austin): Only calibrate on the predetermined edge.
618 // We might be able to just ignore this since the backlash is soooo low. :)
Austin Schuhf9286cd2014-02-11 00:51:09 -0800619 top_claw_.SetCalibrationOnEdge(
Austin Schuhd27931c2014-02-16 19:18:20 -0800620 values.claw.upper_claw, ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800621 bottom_claw_.SetCalibrationOnEdge(
Austin Schuhd27931c2014-02-16 19:18:20 -0800622 values.claw.lower_claw, ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800623 }
Austin Schuhe7f90d12014-02-17 00:48:25 -0800624 mode_ = UNKNOWN_LOCATION;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800625 }
626
Austin Schuh069143b2014-02-17 02:46:26 -0800627 // Limit the goals if both claws have been (mostly) found.
628 if (mode_ != UNKNOWN_LOCATION) {
629 LimitClawGoal(&bottom_claw_goal_, &top_claw_goal_, values);
630 }
631
Austin Schuhf9286cd2014-02-11 00:51:09 -0800632 if (has_top_claw_goal_ && has_bottom_claw_goal_) {
Austin Schuh069143b2014-02-17 02:46:26 -0800633 claw_.R << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_,
634 bottom_claw_velocity_, top_claw_velocity_ - bottom_claw_velocity_;
Austin Schuhcda86af2014-02-16 16:16:39 -0800635 double separation = -971;
636 if (position != nullptr) {
637 separation = position->top.position - position->bottom.position;
638 }
639 LOG(DEBUG, "Goal is %f (bottom) %f, separation is %f\n", claw_.R(0, 0),
640 claw_.R(1, 0), separation);
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800641
Austin Schuh01c652b2014-02-21 23:13:42 -0800642 // Only cap power when one of the halves of the claw is moving slowly and
643 // could wind up.
Austin Schuhe7f90d12014-02-17 00:48:25 -0800644 claw_.set_is_zeroing(mode_ == UNKNOWN_LOCATION || mode_ == FINE_TUNE_TOP ||
645 mode_ == FINE_TUNE_BOTTOM);
Austin Schuhcda86af2014-02-16 16:16:39 -0800646 claw_.Update(output == nullptr);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800647 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800648 claw_.Update(true);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800649 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800650
Austin Schuh4cb047f2014-02-16 21:10:19 -0800651 capped_goal_ = false;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800652 switch (mode_) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800653 case READY:
Austin Schuhe7f90d12014-02-17 00:48:25 -0800654 case PREP_FINE_TUNE_TOP:
655 case PREP_FINE_TUNE_BOTTOM:
Austin Schuhcda86af2014-02-16 16:16:39 -0800656 break;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800657 case FINE_TUNE_BOTTOM:
658 case FINE_TUNE_TOP:
Austin Schuh4cb047f2014-02-16 21:10:19 -0800659 case UNKNOWN_LOCATION: {
660 if (claw_.uncapped_average_voltage() > values.claw.max_zeroing_voltage) {
661 double dx = (claw_.uncapped_average_voltage() -
662 values.claw.max_zeroing_voltage) /
663 claw_.K(0, 0);
664 bottom_claw_goal_ -= dx;
665 top_claw_goal_ -= dx;
Austin Schuhcda86af2014-02-16 16:16:39 -0800666 capped_goal_ = true;
Austin Schuh4cb047f2014-02-16 21:10:19 -0800667 LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx);
Austin Schuhe7f90d12014-02-17 00:48:25 -0800668 LOG(DEBUG, "Uncapped is %f, max is %f, difference is %f\n",
669 claw_.uncapped_average_voltage(), values.claw.max_zeroing_voltage,
670 (claw_.uncapped_average_voltage() -
671 values.claw.max_zeroing_voltage));
Austin Schuh4cb047f2014-02-16 21:10:19 -0800672 } else if (claw_.uncapped_average_voltage() <
673 -values.claw.max_zeroing_voltage) {
674 double dx = (claw_.uncapped_average_voltage() +
675 values.claw.max_zeroing_voltage) /
676 claw_.K(0, 0);
677 bottom_claw_goal_ -= dx;
678 top_claw_goal_ -= dx;
Austin Schuhcda86af2014-02-16 16:16:39 -0800679 capped_goal_ = true;
Austin Schuh4cb047f2014-02-16 21:10:19 -0800680 LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx);
Austin Schuhcda86af2014-02-16 16:16:39 -0800681 }
Austin Schuh4cb047f2014-02-16 21:10:19 -0800682 } break;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800683 }
684
685 if (output) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800686 output->top_claw_voltage = claw_.U(1, 0) + claw_.U(0, 0);
687 output->bottom_claw_voltage = claw_.U(0, 0);
Austin Schuhf84a1302014-02-19 00:23:30 -0800688
689 if (output->top_claw_voltage > kMaxVoltage) {
690 output->top_claw_voltage = kMaxVoltage;
691 } else if (output->top_claw_voltage < -kMaxVoltage) {
692 output->top_claw_voltage = -kMaxVoltage;
693 }
694
695 if (output->bottom_claw_voltage > kMaxVoltage) {
696 output->bottom_claw_voltage = kMaxVoltage;
697 } else if (output->bottom_claw_voltage < -kMaxVoltage) {
698 output->bottom_claw_voltage = -kMaxVoltage;
699 }
Austin Schuh3bb9a442014-02-02 16:01:45 -0800700 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800701 status->done = false;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800702
703 was_enabled_ = ::aos::robot_state->enabled;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800704}
705
706} // namespace control_loops
707} // namespace frc971