blob: b6e3024208a8814e294a8ebe4ee55cc60aefb991 [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 Schuhcda86af2014-02-16 16:16:39 -080048void ClawLimitedLoop::CapU() {
Austin Schuh4cb047f2014-02-16 21:10:19 -080049 uncapped_average_voltage_ = U(0, 0) + U(1, 0) / 2.0;
50 if (is_zeroing_) {
51 const frc971::constants::Values &values = constants::GetValues();
52 if (uncapped_average_voltage_ > values.claw.max_zeroing_voltage) {
53 const double difference =
54 uncapped_average_voltage_ - values.claw.max_zeroing_voltage;
55 U(0, 0) -= difference;
Austin Schuh4cb047f2014-02-16 21:10:19 -080056 } else if (uncapped_average_voltage_ < -values.claw.max_zeroing_voltage) {
57 const double difference =
58 -uncapped_average_voltage_ - values.claw.max_zeroing_voltage;
59 U(0, 0) += difference;
Austin Schuh4cb047f2014-02-16 21:10:19 -080060 }
61 }
62
63 double max_value =
64 ::std::max(::std::abs(U(0, 0)), ::std::abs(U(1, 0) + U(0, 0)));
65
Austin Schuhcda86af2014-02-16 16:16:39 -080066 if (max_value > 12.0) {
67 LOG(DEBUG, "Capping U because max is %f\n", max_value);
68 U = U * 12.0 / max_value;
69 LOG(DEBUG, "Capping U is now %f %f\n", U(0, 0), U(1, 0));
Austin Schuh4b7b5d02014-02-10 21:20:34 -080070 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -080071}
72
Austin Schuhcc0bf312014-02-09 00:39:29 -080073ClawMotor::ClawMotor(control_loops::ClawGroup *my_claw)
74 : aos::control_loops::ControlLoop<control_loops::ClawGroup>(my_claw),
Austin Schuh4b7b5d02014-02-10 21:20:34 -080075 has_top_claw_goal_(false),
76 top_claw_goal_(0.0),
Austin Schuhcda86af2014-02-16 16:16:39 -080077 top_claw_(this),
Austin Schuh4b7b5d02014-02-10 21:20:34 -080078 has_bottom_claw_goal_(false),
79 bottom_claw_goal_(0.0),
Austin Schuhcda86af2014-02-16 16:16:39 -080080 bottom_claw_(this),
81 claw_(MakeClawLoop()),
Ben Fredrickson9b388422014-02-13 06:15:31 +000082 was_enabled_(false),
Austin Schuh4cb047f2014-02-16 21:10:19 -080083 doing_calibration_fine_tune_(false),
Austin Schuhe7f90d12014-02-17 00:48:25 -080084 capped_goal_(false),
85 mode_(UNKNOWN_LOCATION) {}
Austin Schuh3bb9a442014-02-02 16:01:45 -080086
Austin Schuh4b7b5d02014-02-10 21:20:34 -080087const int ZeroedStateFeedbackLoop::kZeroingMaxVoltage;
Austin Schuh3bb9a442014-02-02 16:01:45 -080088
Brian Silvermane0a95462014-02-17 00:41:09 -080089bool ZeroedStateFeedbackLoop::DoGetPositionOfEdge(
90 const constants::Values::Claws::AnglePair &angles, double *edge_encoder,
91 double *edge_angle, const HallEffectTracker &sensor,
92 const char *hall_effect_name) {
93 if (sensor.posedge_count_changed()) {
94 if (posedge_value_ < last_encoder()) {
95 *edge_angle = angles.upper_angle;
96 LOG(INFO, "%s Posedge upper of %s -> %f\n", name_,
97 hall_effect_name, *edge_angle);
98 } else {
99 *edge_angle = angles.lower_angle;
100 LOG(INFO, "%s Posedge lower of %s -> %f\n", name_,
101 hall_effect_name, *edge_angle);
102 }
103 *edge_encoder = posedge_value_;
104 return true;
105 }
106 if (sensor.negedge_count_changed()) {
107 if (negedge_value_ > last_encoder()) {
108 *edge_angle = angles.upper_angle;
109 LOG(INFO, "%s Negedge upper of %s -> %f\n", name_,
110 hall_effect_name, *edge_angle);
111 } else {
112 *edge_angle = angles.lower_angle;
113 LOG(INFO, "%s Negedge lower of %s -> %f\n", name_,
114 hall_effect_name, *edge_angle);
115 }
116 *edge_encoder = negedge_value_;
117 return true;
118 }
119
120 return false;
121}
122
Austin Schuhf9286cd2014-02-11 00:51:09 -0800123bool ZeroedStateFeedbackLoop::GetPositionOfEdge(
Austin Schuhd27931c2014-02-16 19:18:20 -0800124 const constants::Values::Claws::Claw &claw_values, double *edge_encoder,
Austin Schuhf9286cd2014-02-11 00:51:09 -0800125 double *edge_angle) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800126 // TODO(austin): Validate that the hall effect edge makes sense.
127 // We must now be on the side of the edge that we expect to be, and the
128 // encoder must have been on either side of the edge before and after.
129
Austin Schuhcda86af2014-02-16 16:16:39 -0800130 // TODO(austin): Compute the last off range min and max and compare the edge
131 // value to the middle of the range. This will be quite a bit more reliable.
132
Brian Silvermane0a95462014-02-17 00:41:09 -0800133 if (DoGetPositionOfEdge(claw_values.front, edge_encoder, edge_angle,
134 front_, "front")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800135 return true;
136 }
Brian Silvermane0a95462014-02-17 00:41:09 -0800137 if (DoGetPositionOfEdge(claw_values.calibration, edge_encoder, edge_angle,
138 calibration_, "calibration")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800139 return true;
140 }
Brian Silvermane0a95462014-02-17 00:41:09 -0800141 if (DoGetPositionOfEdge(claw_values.back, edge_encoder, edge_angle,
142 back_, "back")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800143 return true;
144 }
145 return false;
146}
147
Austin Schuhcda86af2014-02-16 16:16:39 -0800148void TopZeroedStateFeedbackLoop::SetCalibration(double edge_encoder,
149 double edge_angle) {
150 double old_offset = offset_;
151 offset_ = edge_angle - edge_encoder;
152 const double doffset = offset_ - old_offset;
153 motor_->ChangeTopOffset(doffset);
154}
155
156void BottomZeroedStateFeedbackLoop::SetCalibration(double edge_encoder,
157 double edge_angle) {
158 double old_offset = offset_;
159 offset_ = edge_angle - edge_encoder;
160 const double doffset = offset_ - old_offset;
161 motor_->ChangeBottomOffset(doffset);
162}
163
164void ClawMotor::ChangeTopOffset(double doffset) {
165 claw_.ChangeTopOffset(doffset);
166 if (has_top_claw_goal_) {
167 top_claw_goal_ += doffset;
168 }
169}
170
171void ClawMotor::ChangeBottomOffset(double doffset) {
172 claw_.ChangeBottomOffset(doffset);
173 if (has_bottom_claw_goal_) {
174 bottom_claw_goal_ += doffset;
175 }
176}
177
178void ClawLimitedLoop::ChangeTopOffset(double doffset) {
179 Y_(1, 0) += doffset;
180 X_hat(1, 0) += doffset;
181 LOG(INFO, "Changing top offset by %f\n", doffset);
182}
183void ClawLimitedLoop::ChangeBottomOffset(double doffset) {
184 Y_(0, 0) += doffset;
185 X_hat(0, 0) += doffset;
186 X_hat(1, 0) -= doffset;
187 LOG(INFO, "Changing bottom offset by %f\n", doffset);
188}
joe7376ff52014-02-16 18:28:42 -0800189
Austin Schuh069143b2014-02-17 02:46:26 -0800190void LimitClawGoal(double *bottom_goal, double *top_goal,
191 const frc971::constants::Values &values) {
192 // first update position based on angle limit
193
194 const double separation = *top_goal - *bottom_goal;
195 if (separation > values.claw.claw_max_separation) {
196 LOG(DEBUG, "Greater than\n");
197 const double dsep = (separation - values.claw.claw_max_separation) / 2.0;
198 *bottom_goal += dsep;
199 *top_goal -= dsep;
200 LOG(DEBUG, "Goals now bottom: %f, top: %f\n", *bottom_goal, *top_goal);
201 }
202 if (separation < values.claw.claw_min_separation) {
203 LOG(DEBUG, "Less than\n");
204 const double dsep = (separation - values.claw.claw_min_separation) / 2.0;
205 *bottom_goal += dsep;
206 *top_goal -= dsep;
207 LOG(DEBUG, "Goals now bottom: %f, top: %f\n", *bottom_goal, *top_goal);
208 }
209
210 // now move both goals in unison
211 if (*bottom_goal < values.claw.lower_claw.lower_limit) {
212 *top_goal += values.claw.lower_claw.lower_limit - *bottom_goal;
213 *bottom_goal = values.claw.lower_claw.lower_limit;
214 }
215 if (*bottom_goal > values.claw.lower_claw.upper_limit) {
216 *top_goal -= *bottom_goal - values.claw.lower_claw.upper_limit;
217 *bottom_goal = values.claw.lower_claw.upper_limit;
218 }
219
220 if (*top_goal < values.claw.upper_claw.lower_limit) {
221 *bottom_goal += values.claw.upper_claw.lower_limit - *top_goal;
222 *top_goal = values.claw.upper_claw.lower_limit;
223 }
224 if (*top_goal > values.claw.upper_claw.upper_limit) {
225 *bottom_goal -= *top_goal - values.claw.upper_claw.upper_limit;
226 *top_goal = values.claw.upper_claw.upper_limit;
227 }
228}
Austin Schuhcda86af2014-02-16 16:16:39 -0800229
Austin Schuhe7f90d12014-02-17 00:48:25 -0800230bool ClawMotor::is_ready() const {
231 return (
232 (top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED &&
233 bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) ||
234 (::aos::robot_state->autonomous &&
235 ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
236 top_claw_.zeroing_state() ==
237 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
238 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
239 bottom_claw_.zeroing_state() ==
240 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION))));
241}
242
243bool ClawMotor::is_zeroing() const { return !is_ready(); }
244
Austin Schuh3bb9a442014-02-02 16:01:45 -0800245// Positive angle is up, and positive power is up.
Austin Schuhcc0bf312014-02-09 00:39:29 -0800246void ClawMotor::RunIteration(const control_loops::ClawGroup::Goal *goal,
247 const control_loops::ClawGroup::Position *position,
248 control_loops::ClawGroup::Output *output,
Austin Schuh3bb9a442014-02-02 16:01:45 -0800249 ::aos::control_loops::Status *status) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800250 constexpr double dt = 0.01;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800251
252 // Disable the motors now so that all early returns will return with the
253 // motors disabled.
254 if (output) {
255 output->top_claw_voltage = 0;
256 output->bottom_claw_voltage = 0;
257 output->intake_voltage = 0;
258 }
259
Austin Schuh1a499942014-02-17 01:51:58 -0800260 if (reset()) {
261 bottom_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
262 top_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
263 }
264
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800265 if (::aos::robot_state.get() == nullptr) {
266 return;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800267 }
268
Austin Schuhf9286cd2014-02-11 00:51:09 -0800269 const frc971::constants::Values &values = constants::GetValues();
270
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800271 if (position) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800272 Eigen::Matrix<double, 2, 1> Y;
273 Y << position->bottom.position + bottom_claw_.offset(),
274 position->top.position + top_claw_.offset();
275 claw_.Correct(Y);
276
Austin Schuhf9286cd2014-02-11 00:51:09 -0800277 top_claw_.SetPositionValues(position->top);
278 bottom_claw_.SetPositionValues(position->bottom);
279
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800280 if (!has_top_claw_goal_) {
281 has_top_claw_goal_ = true;
Austin Schuhcda86af2014-02-16 16:16:39 -0800282 top_claw_goal_ = top_claw_.absolute_position();
Austin Schuhe7f90d12014-02-17 00:48:25 -0800283 initial_separation_ =
Austin Schuhcda86af2014-02-16 16:16:39 -0800284 top_claw_.absolute_position() - bottom_claw_.absolute_position();
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800285 }
286 if (!has_bottom_claw_goal_) {
287 has_bottom_claw_goal_ = true;
Austin Schuhcda86af2014-02-16 16:16:39 -0800288 bottom_claw_goal_ = bottom_claw_.absolute_position();
Austin Schuhe7f90d12014-02-17 00:48:25 -0800289 initial_separation_ =
Austin Schuhcda86af2014-02-16 16:16:39 -0800290 top_claw_.absolute_position() - bottom_claw_.absolute_position();
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800291 }
Austin Schuhcda86af2014-02-16 16:16:39 -0800292 LOG(DEBUG, "Claw position is (top: %f bottom: %f\n",
293 top_claw_.absolute_position(), bottom_claw_.absolute_position());
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800294 }
295
Austin Schuh069143b2014-02-17 02:46:26 -0800296 const bool autonomous = ::aos::robot_state->autonomous;
297 const bool enabled = ::aos::robot_state->enabled;
298
299 double bottom_claw_velocity_ = 0.0;
300 double top_claw_velocity_ = 0.0;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800301
302 if ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED &&
303 bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) ||
304 (autonomous &&
305 ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
306 top_claw_.zeroing_state() ==
307 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
308 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
309 bottom_claw_.zeroing_state() ==
310 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION)))) {
311 // Ready to use the claw.
312 // Limit the goals here.
Austin Schuhf9286cd2014-02-11 00:51:09 -0800313 bottom_claw_goal_ = goal->bottom_angle;
314 top_claw_goal_ = goal->bottom_angle + goal->seperation_angle;
Austin Schuhcda86af2014-02-16 16:16:39 -0800315 has_bottom_claw_goal_ = true;
316 has_top_claw_goal_ = true;
317 doing_calibration_fine_tune_ = false;
318
Austin Schuhe7f90d12014-02-17 00:48:25 -0800319 mode_ = READY;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800320 } else if (top_claw_.zeroing_state() !=
321 ZeroedStateFeedbackLoop::UNKNOWN_POSITION &&
322 bottom_claw_.zeroing_state() !=
323 ZeroedStateFeedbackLoop::UNKNOWN_POSITION) {
324 // Time to fine tune the zero.
325 // Limit the goals here.
Austin Schuh0c733422014-02-17 01:17:12 -0800326 if (!enabled) {
327 // If we are disabled, start the fine tune process over again.
328 doing_calibration_fine_tune_ = false;
329 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800330 if (bottom_claw_.zeroing_state() != ZeroedStateFeedbackLoop::CALIBRATED) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800331 // always get the bottom claw to calibrated first
332 LOG(DEBUG, "Calibrating the bottom of the claw\n");
333 if (!doing_calibration_fine_tune_) {
334 if (::std::abs(bottom_absolute_position() -
Austin Schuhd27931c2014-02-16 19:18:20 -0800335 values.claw.start_fine_tune_pos) <
336 values.claw.claw_unimportant_epsilon) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800337 doing_calibration_fine_tune_ = true;
Austin Schuhd27931c2014-02-16 19:18:20 -0800338 bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800339 top_claw_velocity_ = bottom_claw_velocity_ =
340 values.claw.claw_zeroing_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800341 LOG(DEBUG, "Ready to fine tune the bottom\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800342 mode_ = FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800343 } else {
344 // send bottom to zeroing start
Austin Schuhd27931c2014-02-16 19:18:20 -0800345 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuhcda86af2014-02-16 16:16:39 -0800346 LOG(DEBUG, "Going to the start position for the bottom\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800347 mode_ = PREP_FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800348 }
349 } else {
Austin Schuhe7f90d12014-02-17 00:48:25 -0800350 mode_ = FINE_TUNE_BOTTOM;
Austin Schuhd27931c2014-02-16 19:18:20 -0800351 bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800352 top_claw_velocity_ = bottom_claw_velocity_ =
353 values.claw.claw_zeroing_speed;
Brian Silvermane0a95462014-02-17 00:41:09 -0800354 if (top_claw_.front_or_back_triggered() ||
355 bottom_claw_.front_or_back_triggered()) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800356 // We shouldn't hit a limit, but if we do, go back to the zeroing
357 // point and try again.
358 doing_calibration_fine_tune_ = false;
Austin Schuhd27931c2014-02-16 19:18:20 -0800359 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuh069143b2014-02-17 02:46:26 -0800360 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
Austin Schuhcda86af2014-02-16 16:16:39 -0800361 LOG(DEBUG, "Found a limit, starting over.\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800362 mode_ = PREP_FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800363 }
Austin Schuh288c8c32014-02-16 17:20:17 -0800364
Brian Silvermane0a95462014-02-17 00:41:09 -0800365 if (bottom_claw_.calibration().value()) {
366 if (bottom_claw_.calibration().posedge_count_changed() &&
Austin Schuh288c8c32014-02-16 17:20:17 -0800367 position) {
368 // do calibration
369 bottom_claw_.SetCalibration(
370 position->bottom.posedge_value,
Austin Schuhd27931c2014-02-16 19:18:20 -0800371 values.claw.lower_claw.calibration.lower_angle);
Austin Schuh288c8c32014-02-16 17:20:17 -0800372 bottom_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED);
373 // calibrated so we are done fine tuning bottom
374 doing_calibration_fine_tune_ = false;
375 LOG(DEBUG, "Calibrated the bottom correctly!\n");
376 } else {
377 doing_calibration_fine_tune_ = false;
Austin Schuhd27931c2014-02-16 19:18:20 -0800378 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuh069143b2014-02-17 02:46:26 -0800379 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800380 mode_ = PREP_FINE_TUNE_BOTTOM;
Austin Schuh288c8c32014-02-16 17:20:17 -0800381 }
Austin Schuhcda86af2014-02-16 16:16:39 -0800382 } else {
383 LOG(DEBUG, "Fine tuning\n");
384 }
385 }
386 // now set the top claw to track
387
Austin Schuhd27931c2014-02-16 19:18:20 -0800388 top_claw_goal_ = bottom_claw_goal_ + values.claw.claw_zeroing_separation;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800389 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800390 // bottom claw must be calibrated, start on the top
391 if (!doing_calibration_fine_tune_) {
Austin Schuhd27931c2014-02-16 19:18:20 -0800392 if (::std::abs(top_absolute_position() -
393 values.claw.start_fine_tune_pos) <
394 values.claw.claw_unimportant_epsilon) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800395 doing_calibration_fine_tune_ = true;
Austin Schuhd27931c2014-02-16 19:18:20 -0800396 top_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800397 top_claw_velocity_ = bottom_claw_velocity_ =
398 values.claw.claw_zeroing_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800399 LOG(DEBUG, "Ready to fine tune the top\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800400 mode_ = FINE_TUNE_TOP;
Austin Schuhcda86af2014-02-16 16:16:39 -0800401 } else {
402 // send top to zeroing start
Austin Schuhd27931c2014-02-16 19:18:20 -0800403 top_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuhcda86af2014-02-16 16:16:39 -0800404 LOG(DEBUG, "Going to the start position for the top\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800405 mode_ = PREP_FINE_TUNE_TOP;
Austin Schuhcda86af2014-02-16 16:16:39 -0800406 }
407 } else {
Austin Schuhe7f90d12014-02-17 00:48:25 -0800408 mode_ = FINE_TUNE_TOP;
Austin Schuhd27931c2014-02-16 19:18:20 -0800409 top_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800410 top_claw_velocity_ = bottom_claw_velocity_ =
411 values.claw.claw_zeroing_speed;
Brian Silvermane0a95462014-02-17 00:41:09 -0800412 if (top_claw_.front_or_back_triggered() ||
413 bottom_claw_.front_or_back_triggered()) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800414 // this should not happen, but now we know it won't
415 doing_calibration_fine_tune_ = false;
Austin Schuhd27931c2014-02-16 19:18:20 -0800416 top_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuh069143b2014-02-17 02:46:26 -0800417 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
Austin Schuhcda86af2014-02-16 16:16:39 -0800418 LOG(DEBUG, "Found a limit, starting over.\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800419 mode_ = PREP_FINE_TUNE_TOP;
Austin Schuhcda86af2014-02-16 16:16:39 -0800420 }
Brian Silvermane0a95462014-02-17 00:41:09 -0800421 if (top_claw_.calibration().value()) {
422 if (top_claw_.calibration().posedge_count_changed() &&
Austin Schuh288c8c32014-02-16 17:20:17 -0800423 position) {
424 // do calibration
Austin Schuhd27931c2014-02-16 19:18:20 -0800425 top_claw_.SetCalibration(
426 position->top.posedge_value,
427 values.claw.upper_claw.calibration.lower_angle);
Austin Schuh288c8c32014-02-16 17:20:17 -0800428 top_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED);
Austin Schuhe7f90d12014-02-17 00:48:25 -0800429 // calibrated so we are done fine tuning top
Austin Schuh288c8c32014-02-16 17:20:17 -0800430 doing_calibration_fine_tune_ = false;
431 LOG(DEBUG, "Calibrated the top correctly!\n");
432 } else {
433 doing_calibration_fine_tune_ = false;
Austin Schuhd27931c2014-02-16 19:18:20 -0800434 top_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuh069143b2014-02-17 02:46:26 -0800435 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800436 mode_ = PREP_FINE_TUNE_TOP;
Austin Schuh288c8c32014-02-16 17:20:17 -0800437 }
Austin Schuhcda86af2014-02-16 16:16:39 -0800438 }
439 }
440 // now set the bottom claw to track
Austin Schuhd27931c2014-02-16 19:18:20 -0800441 bottom_claw_goal_ = top_claw_goal_ - values.claw.claw_zeroing_separation;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800442 }
443 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800444 doing_calibration_fine_tune_ = false;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800445 if (!was_enabled_ && enabled) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800446 if (position) {
447 top_claw_goal_ = position->top.position;
448 bottom_claw_goal_ = position->bottom.position;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800449 initial_separation_ =
Austin Schuhcda86af2014-02-16 16:16:39 -0800450 position->top.position - position->bottom.position;
Austin Schuhf9286cd2014-02-11 00:51:09 -0800451 } else {
452 has_top_claw_goal_ = false;
453 has_bottom_claw_goal_ = false;
454 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800455 }
Austin Schuhf9286cd2014-02-11 00:51:09 -0800456
Austin Schuh4cb047f2014-02-16 21:10:19 -0800457 if ((bottom_claw_.zeroing_state() !=
458 ZeroedStateFeedbackLoop::UNKNOWN_POSITION ||
Brian Silvermane0a95462014-02-17 00:41:09 -0800459 bottom_claw_.front().value() || top_claw_.front().value()) &&
460 !top_claw_.back().value() && !bottom_claw_.back().value()) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800461 if (enabled) {
462 // Time to slowly move back up to find any position to narrow down the
463 // zero.
Austin Schuhd27931c2014-02-16 19:18:20 -0800464 top_claw_goal_ += values.claw.claw_zeroing_off_speed * dt;
465 bottom_claw_goal_ += values.claw.claw_zeroing_off_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800466 top_claw_velocity_ = bottom_claw_velocity_ =
467 values.claw.claw_zeroing_off_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800468 LOG(DEBUG, "Bottom is known.\n");
Austin Schuhf9286cd2014-02-11 00:51:09 -0800469 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800470 } else {
471 // We don't know where either claw is. Slowly start moving down to find
472 // any hall effect.
Austin Schuhf9286cd2014-02-11 00:51:09 -0800473 if (enabled) {
Austin Schuhd27931c2014-02-16 19:18:20 -0800474 top_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt;
475 bottom_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800476 top_claw_velocity_ = bottom_claw_velocity_ =
477 -values.claw.claw_zeroing_off_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800478 LOG(DEBUG, "Both are unknown.\n");
Austin Schuhf9286cd2014-02-11 00:51:09 -0800479 }
480 }
481
482 if (enabled) {
483 top_claw_.SetCalibrationOnEdge(
Austin Schuhd27931c2014-02-16 19:18:20 -0800484 values.claw.upper_claw, ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800485 bottom_claw_.SetCalibrationOnEdge(
Austin Schuhd27931c2014-02-16 19:18:20 -0800486 values.claw.lower_claw, ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800487 } else {
Austin Schuh069143b2014-02-17 02:46:26 -0800488 // TODO(austin): Only calibrate on the predetermined edge.
489 // We might be able to just ignore this since the backlash is soooo low. :)
Austin Schuhf9286cd2014-02-11 00:51:09 -0800490 top_claw_.SetCalibrationOnEdge(
Austin Schuhd27931c2014-02-16 19:18:20 -0800491 values.claw.upper_claw, ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800492 bottom_claw_.SetCalibrationOnEdge(
Austin Schuhd27931c2014-02-16 19:18:20 -0800493 values.claw.lower_claw, ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800494 }
Austin Schuhe7f90d12014-02-17 00:48:25 -0800495 mode_ = UNKNOWN_LOCATION;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800496 }
497
Austin Schuh069143b2014-02-17 02:46:26 -0800498 // Limit the goals if both claws have been (mostly) found.
499 if (mode_ != UNKNOWN_LOCATION) {
500 LimitClawGoal(&bottom_claw_goal_, &top_claw_goal_, values);
501 }
502
Austin Schuhf9286cd2014-02-11 00:51:09 -0800503 if (has_top_claw_goal_ && has_bottom_claw_goal_) {
Austin Schuh069143b2014-02-17 02:46:26 -0800504 claw_.R << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_,
505 bottom_claw_velocity_, top_claw_velocity_ - bottom_claw_velocity_;
Austin Schuhcda86af2014-02-16 16:16:39 -0800506 double separation = -971;
507 if (position != nullptr) {
508 separation = position->top.position - position->bottom.position;
509 }
510 LOG(DEBUG, "Goal is %f (bottom) %f, separation is %f\n", claw_.R(0, 0),
511 claw_.R(1, 0), separation);
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800512
Austin Schuh4cb047f2014-02-16 21:10:19 -0800513 // Only cap power when one of the halves of the claw is unknown.
Austin Schuhe7f90d12014-02-17 00:48:25 -0800514 claw_.set_is_zeroing(mode_ == UNKNOWN_LOCATION || mode_ == FINE_TUNE_TOP ||
515 mode_ == FINE_TUNE_BOTTOM);
Austin Schuhcda86af2014-02-16 16:16:39 -0800516 claw_.Update(output == nullptr);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800517 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800518 claw_.Update(true);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800519 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800520
Austin Schuh4cb047f2014-02-16 21:10:19 -0800521 capped_goal_ = false;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800522 switch (mode_) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800523 case READY:
Austin Schuhe7f90d12014-02-17 00:48:25 -0800524 case PREP_FINE_TUNE_TOP:
525 case PREP_FINE_TUNE_BOTTOM:
Austin Schuhcda86af2014-02-16 16:16:39 -0800526 break;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800527 case FINE_TUNE_BOTTOM:
528 case FINE_TUNE_TOP:
Austin Schuh4cb047f2014-02-16 21:10:19 -0800529 case UNKNOWN_LOCATION: {
530 if (claw_.uncapped_average_voltage() > values.claw.max_zeroing_voltage) {
531 double dx = (claw_.uncapped_average_voltage() -
532 values.claw.max_zeroing_voltage) /
533 claw_.K(0, 0);
534 bottom_claw_goal_ -= dx;
535 top_claw_goal_ -= dx;
Austin Schuhcda86af2014-02-16 16:16:39 -0800536 capped_goal_ = true;
Austin Schuh4cb047f2014-02-16 21:10:19 -0800537 LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx);
Austin Schuhe7f90d12014-02-17 00:48:25 -0800538 LOG(DEBUG, "Uncapped is %f, max is %f, difference is %f\n",
539 claw_.uncapped_average_voltage(), values.claw.max_zeroing_voltage,
540 (claw_.uncapped_average_voltage() -
541 values.claw.max_zeroing_voltage));
Austin Schuh4cb047f2014-02-16 21:10:19 -0800542 } else if (claw_.uncapped_average_voltage() <
543 -values.claw.max_zeroing_voltage) {
544 double dx = (claw_.uncapped_average_voltage() +
545 values.claw.max_zeroing_voltage) /
546 claw_.K(0, 0);
547 bottom_claw_goal_ -= dx;
548 top_claw_goal_ -= dx;
Austin Schuhcda86af2014-02-16 16:16:39 -0800549 capped_goal_ = true;
Austin Schuh4cb047f2014-02-16 21:10:19 -0800550 LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx);
Austin Schuhcda86af2014-02-16 16:16:39 -0800551 }
Austin Schuh4cb047f2014-02-16 21:10:19 -0800552 } break;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800553 }
554
555 if (output) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800556 output->top_claw_voltage = claw_.U(1, 0) + claw_.U(0, 0);
557 output->bottom_claw_voltage = claw_.U(0, 0);
Austin Schuh3bb9a442014-02-02 16:01:45 -0800558 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800559 status->done = false;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800560
561 was_enabled_ = ::aos::robot_state->enabled;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800562}
563
564} // namespace control_loops
565} // namespace frc971