blob: 410d6c6183ad79a1fdaeba44a2ad61f6d96cceea [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()) {
Ben Fredricksonade3eab2014-02-22 07:30:53 +000094 if (min_current_hall_effect_edge_ == max_curent_hall_effect_edge_) {
95 // we oddly got two of the same edge.
96 *edge_angle = last_edge_value_;
97 return true;
98 } else if (posedge_value_ <
99 (min_current_hall_effect_edge_ + max_current_hall_effect_edge_) / 2) {
Brian Silvermane0a95462014-02-17 00:41:09 -0800100 *edge_angle = angles.upper_angle;
Ben Fredricksonade3eab2014-02-22 07:30:53 +0000101 LOG(INFO, "%s Posedge upper of %s -> %f\n", name_, hall_effect_name,
102 *edge_angle);
Brian Silvermane0a95462014-02-17 00:41:09 -0800103 } else {
104 *edge_angle = angles.lower_angle;
Ben Fredricksonade3eab2014-02-22 07:30:53 +0000105 LOG(INFO, "%s Posedge lower of %s -> %f\n", name_, hall_effect_name,
106 *edge_angle);
Brian Silvermane0a95462014-02-17 00:41:09 -0800107 }
108 *edge_encoder = posedge_value_;
Ben Fredricksonade3eab2014-02-22 07:30:53 +0000109 last_edge_value_ = posedge_value_;
Brian Silvermane0a95462014-02-17 00:41:09 -0800110 return true;
111 }
112 if (sensor.negedge_count_changed()) {
Ben Fredricksonade3eab2014-02-22 07:30:53 +0000113 // we oddly got two of the same edge.
114 if (min_current_hall_effect_edge_ == max_curent_hall_effect_edge_) {
115 *edge_angle = last_edge_value_;
116 return true;
117 } else if (negedge_value_ > (min_current_hall_effect_edge_ +
118 max_current_hall_effect_edge_) / 2) {
Brian Silvermane0a95462014-02-17 00:41:09 -0800119 *edge_angle = angles.upper_angle;
Ben Fredricksonade3eab2014-02-22 07:30:53 +0000120 LOG(INFO, "%s Negedge upper of %s -> %f\n", name_, hall_effect_name,
121 *edge_angle);
Brian Silvermane0a95462014-02-17 00:41:09 -0800122 } else {
123 *edge_angle = angles.lower_angle;
Ben Fredricksonade3eab2014-02-22 07:30:53 +0000124 LOG(INFO, "%s Negedge lower of %s -> %f\n", name_, hall_effect_name,
125 *edge_angle);
Brian Silvermane0a95462014-02-17 00:41:09 -0800126 }
127 *edge_encoder = negedge_value_;
Ben Fredricksonade3eab2014-02-22 07:30:53 +0000128 last_edge_value_ = posedge_value_;
Brian Silvermane0a95462014-02-17 00:41:09 -0800129 return true;
130 }
131
132 return false;
133}
134
Austin Schuhf9286cd2014-02-11 00:51:09 -0800135bool ZeroedStateFeedbackLoop::GetPositionOfEdge(
Austin Schuhd27931c2014-02-16 19:18:20 -0800136 const constants::Values::Claws::Claw &claw_values, double *edge_encoder,
Austin Schuhf9286cd2014-02-11 00:51:09 -0800137 double *edge_angle) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800138 // TODO(austin): Validate that the hall effect edge makes sense.
139 // We must now be on the side of the edge that we expect to be, and the
140 // encoder must have been on either side of the edge before and after.
141
Austin Schuhcda86af2014-02-16 16:16:39 -0800142 // TODO(austin): Compute the last off range min and max and compare the edge
143 // value to the middle of the range. This will be quite a bit more reliable.
144
Brian Silvermane0a95462014-02-17 00:41:09 -0800145 if (DoGetPositionOfEdge(claw_values.front, edge_encoder, edge_angle,
146 front_, "front")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800147 return true;
148 }
Brian Silvermane0a95462014-02-17 00:41:09 -0800149 if (DoGetPositionOfEdge(claw_values.calibration, edge_encoder, edge_angle,
150 calibration_, "calibration")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800151 return true;
152 }
Brian Silvermane0a95462014-02-17 00:41:09 -0800153 if (DoGetPositionOfEdge(claw_values.back, edge_encoder, edge_angle,
154 back_, "back")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800155 return true;
156 }
157 return false;
158}
159
Austin Schuhcda86af2014-02-16 16:16:39 -0800160void TopZeroedStateFeedbackLoop::SetCalibration(double edge_encoder,
161 double edge_angle) {
162 double old_offset = offset_;
163 offset_ = edge_angle - edge_encoder;
164 const double doffset = offset_ - old_offset;
165 motor_->ChangeTopOffset(doffset);
166}
167
168void BottomZeroedStateFeedbackLoop::SetCalibration(double edge_encoder,
169 double edge_angle) {
170 double old_offset = offset_;
171 offset_ = edge_angle - edge_encoder;
172 const double doffset = offset_ - old_offset;
173 motor_->ChangeBottomOffset(doffset);
174}
175
176void ClawMotor::ChangeTopOffset(double doffset) {
177 claw_.ChangeTopOffset(doffset);
178 if (has_top_claw_goal_) {
179 top_claw_goal_ += doffset;
180 }
181}
182
183void ClawMotor::ChangeBottomOffset(double doffset) {
184 claw_.ChangeBottomOffset(doffset);
185 if (has_bottom_claw_goal_) {
186 bottom_claw_goal_ += doffset;
187 }
188}
189
190void ClawLimitedLoop::ChangeTopOffset(double doffset) {
191 Y_(1, 0) += doffset;
192 X_hat(1, 0) += doffset;
193 LOG(INFO, "Changing top offset by %f\n", doffset);
194}
195void ClawLimitedLoop::ChangeBottomOffset(double doffset) {
196 Y_(0, 0) += doffset;
197 X_hat(0, 0) += doffset;
198 X_hat(1, 0) -= doffset;
199 LOG(INFO, "Changing bottom offset by %f\n", doffset);
200}
joe7376ff52014-02-16 18:28:42 -0800201
Austin Schuh069143b2014-02-17 02:46:26 -0800202void LimitClawGoal(double *bottom_goal, double *top_goal,
203 const frc971::constants::Values &values) {
204 // first update position based on angle limit
205
206 const double separation = *top_goal - *bottom_goal;
207 if (separation > values.claw.claw_max_separation) {
208 LOG(DEBUG, "Greater than\n");
209 const double dsep = (separation - values.claw.claw_max_separation) / 2.0;
210 *bottom_goal += dsep;
211 *top_goal -= dsep;
212 LOG(DEBUG, "Goals now bottom: %f, top: %f\n", *bottom_goal, *top_goal);
213 }
214 if (separation < values.claw.claw_min_separation) {
215 LOG(DEBUG, "Less than\n");
216 const double dsep = (separation - values.claw.claw_min_separation) / 2.0;
217 *bottom_goal += dsep;
218 *top_goal -= dsep;
219 LOG(DEBUG, "Goals now bottom: %f, top: %f\n", *bottom_goal, *top_goal);
220 }
221
222 // now move both goals in unison
223 if (*bottom_goal < values.claw.lower_claw.lower_limit) {
224 *top_goal += values.claw.lower_claw.lower_limit - *bottom_goal;
225 *bottom_goal = values.claw.lower_claw.lower_limit;
226 }
227 if (*bottom_goal > values.claw.lower_claw.upper_limit) {
228 *top_goal -= *bottom_goal - values.claw.lower_claw.upper_limit;
229 *bottom_goal = values.claw.lower_claw.upper_limit;
230 }
231
232 if (*top_goal < values.claw.upper_claw.lower_limit) {
233 *bottom_goal += values.claw.upper_claw.lower_limit - *top_goal;
234 *top_goal = values.claw.upper_claw.lower_limit;
235 }
236 if (*top_goal > values.claw.upper_claw.upper_limit) {
237 *bottom_goal -= *top_goal - values.claw.upper_claw.upper_limit;
238 *top_goal = values.claw.upper_claw.upper_limit;
239 }
240}
Austin Schuhcda86af2014-02-16 16:16:39 -0800241
Austin Schuhe7f90d12014-02-17 00:48:25 -0800242bool ClawMotor::is_ready() const {
243 return (
244 (top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED &&
245 bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) ||
246 (::aos::robot_state->autonomous &&
247 ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
248 top_claw_.zeroing_state() ==
249 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
250 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
251 bottom_claw_.zeroing_state() ==
252 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION))));
253}
254
255bool ClawMotor::is_zeroing() const { return !is_ready(); }
256
Austin Schuh3bb9a442014-02-02 16:01:45 -0800257// Positive angle is up, and positive power is up.
Austin Schuhcc0bf312014-02-09 00:39:29 -0800258void ClawMotor::RunIteration(const control_loops::ClawGroup::Goal *goal,
259 const control_loops::ClawGroup::Position *position,
260 control_loops::ClawGroup::Output *output,
Austin Schuh3bb9a442014-02-02 16:01:45 -0800261 ::aos::control_loops::Status *status) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800262 constexpr double dt = 0.01;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800263
264 // Disable the motors now so that all early returns will return with the
265 // motors disabled.
266 if (output) {
267 output->top_claw_voltage = 0;
268 output->bottom_claw_voltage = 0;
269 output->intake_voltage = 0;
270 }
271
Austin Schuh1a499942014-02-17 01:51:58 -0800272 if (reset()) {
273 bottom_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
274 top_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
275 }
276
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800277 if (::aos::robot_state.get() == nullptr) {
278 return;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800279 }
280
Austin Schuhf9286cd2014-02-11 00:51:09 -0800281 const frc971::constants::Values &values = constants::GetValues();
282
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800283 if (position) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800284 Eigen::Matrix<double, 2, 1> Y;
285 Y << position->bottom.position + bottom_claw_.offset(),
286 position->top.position + top_claw_.offset();
287 claw_.Correct(Y);
288
Austin Schuhf9286cd2014-02-11 00:51:09 -0800289 top_claw_.SetPositionValues(position->top);
290 bottom_claw_.SetPositionValues(position->bottom);
291
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800292 if (!has_top_claw_goal_) {
293 has_top_claw_goal_ = true;
Austin Schuhcda86af2014-02-16 16:16:39 -0800294 top_claw_goal_ = top_claw_.absolute_position();
Austin Schuhe7f90d12014-02-17 00:48:25 -0800295 initial_separation_ =
Austin Schuhcda86af2014-02-16 16:16:39 -0800296 top_claw_.absolute_position() - bottom_claw_.absolute_position();
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800297 }
298 if (!has_bottom_claw_goal_) {
299 has_bottom_claw_goal_ = true;
Austin Schuhcda86af2014-02-16 16:16:39 -0800300 bottom_claw_goal_ = bottom_claw_.absolute_position();
Austin Schuhe7f90d12014-02-17 00:48:25 -0800301 initial_separation_ =
Austin Schuhcda86af2014-02-16 16:16:39 -0800302 top_claw_.absolute_position() - bottom_claw_.absolute_position();
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800303 }
Austin Schuhcda86af2014-02-16 16:16:39 -0800304 LOG(DEBUG, "Claw position is (top: %f bottom: %f\n",
305 top_claw_.absolute_position(), bottom_claw_.absolute_position());
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800306 }
307
Austin Schuh069143b2014-02-17 02:46:26 -0800308 const bool autonomous = ::aos::robot_state->autonomous;
309 const bool enabled = ::aos::robot_state->enabled;
310
311 double bottom_claw_velocity_ = 0.0;
312 double top_claw_velocity_ = 0.0;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800313
314 if ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED &&
315 bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) ||
316 (autonomous &&
317 ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
318 top_claw_.zeroing_state() ==
319 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
320 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
321 bottom_claw_.zeroing_state() ==
322 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION)))) {
323 // Ready to use the claw.
324 // Limit the goals here.
Austin Schuhf9286cd2014-02-11 00:51:09 -0800325 bottom_claw_goal_ = goal->bottom_angle;
Brian Silverman7c021c42014-02-17 15:15:56 -0800326 top_claw_goal_ = goal->bottom_angle + goal->separation_angle;
Austin Schuhcda86af2014-02-16 16:16:39 -0800327 has_bottom_claw_goal_ = true;
328 has_top_claw_goal_ = true;
329 doing_calibration_fine_tune_ = false;
330
Austin Schuhe7f90d12014-02-17 00:48:25 -0800331 mode_ = READY;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800332 } else if (top_claw_.zeroing_state() !=
333 ZeroedStateFeedbackLoop::UNKNOWN_POSITION &&
334 bottom_claw_.zeroing_state() !=
335 ZeroedStateFeedbackLoop::UNKNOWN_POSITION) {
336 // Time to fine tune the zero.
337 // Limit the goals here.
Austin Schuh0c733422014-02-17 01:17:12 -0800338 if (!enabled) {
339 // If we are disabled, start the fine tune process over again.
340 doing_calibration_fine_tune_ = false;
341 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800342 if (bottom_claw_.zeroing_state() != ZeroedStateFeedbackLoop::CALIBRATED) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800343 // always get the bottom claw to calibrated first
344 LOG(DEBUG, "Calibrating the bottom of the claw\n");
345 if (!doing_calibration_fine_tune_) {
346 if (::std::abs(bottom_absolute_position() -
Austin Schuhd27931c2014-02-16 19:18:20 -0800347 values.claw.start_fine_tune_pos) <
348 values.claw.claw_unimportant_epsilon) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800349 doing_calibration_fine_tune_ = true;
Austin Schuhd27931c2014-02-16 19:18:20 -0800350 bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800351 top_claw_velocity_ = bottom_claw_velocity_ =
352 values.claw.claw_zeroing_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800353 LOG(DEBUG, "Ready to fine tune the bottom\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800354 mode_ = FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800355 } else {
356 // send bottom to zeroing start
Austin Schuhd27931c2014-02-16 19:18:20 -0800357 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuhcda86af2014-02-16 16:16:39 -0800358 LOG(DEBUG, "Going to the start position for the bottom\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800359 mode_ = PREP_FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800360 }
361 } else {
Austin Schuhe7f90d12014-02-17 00:48:25 -0800362 mode_ = FINE_TUNE_BOTTOM;
Austin Schuhd27931c2014-02-16 19:18:20 -0800363 bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800364 top_claw_velocity_ = bottom_claw_velocity_ =
365 values.claw.claw_zeroing_speed;
Brian Silvermane0a95462014-02-17 00:41:09 -0800366 if (top_claw_.front_or_back_triggered() ||
367 bottom_claw_.front_or_back_triggered()) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800368 // We shouldn't hit a limit, but if we do, go back to the zeroing
369 // point and try again.
370 doing_calibration_fine_tune_ = false;
Austin Schuhd27931c2014-02-16 19:18:20 -0800371 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuh069143b2014-02-17 02:46:26 -0800372 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
Austin Schuhcda86af2014-02-16 16:16:39 -0800373 LOG(DEBUG, "Found a limit, starting over.\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800374 mode_ = PREP_FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800375 }
Austin Schuh288c8c32014-02-16 17:20:17 -0800376
Brian Silvermane0a95462014-02-17 00:41:09 -0800377 if (bottom_claw_.calibration().value()) {
378 if (bottom_claw_.calibration().posedge_count_changed() &&
Austin Schuh288c8c32014-02-16 17:20:17 -0800379 position) {
380 // do calibration
381 bottom_claw_.SetCalibration(
382 position->bottom.posedge_value,
Austin Schuhd27931c2014-02-16 19:18:20 -0800383 values.claw.lower_claw.calibration.lower_angle);
Austin Schuh288c8c32014-02-16 17:20:17 -0800384 bottom_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED);
385 // calibrated so we are done fine tuning bottom
386 doing_calibration_fine_tune_ = false;
387 LOG(DEBUG, "Calibrated the bottom correctly!\n");
388 } else {
389 doing_calibration_fine_tune_ = false;
Austin Schuhd27931c2014-02-16 19:18:20 -0800390 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuh069143b2014-02-17 02:46:26 -0800391 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800392 mode_ = PREP_FINE_TUNE_BOTTOM;
Austin Schuh288c8c32014-02-16 17:20:17 -0800393 }
Austin Schuhcda86af2014-02-16 16:16:39 -0800394 } else {
395 LOG(DEBUG, "Fine tuning\n");
396 }
397 }
398 // now set the top claw to track
399
Austin Schuhd27931c2014-02-16 19:18:20 -0800400 top_claw_goal_ = bottom_claw_goal_ + values.claw.claw_zeroing_separation;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800401 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800402 // bottom claw must be calibrated, start on the top
403 if (!doing_calibration_fine_tune_) {
Austin Schuhd27931c2014-02-16 19:18:20 -0800404 if (::std::abs(top_absolute_position() -
405 values.claw.start_fine_tune_pos) <
406 values.claw.claw_unimportant_epsilon) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800407 doing_calibration_fine_tune_ = true;
Austin Schuhd27931c2014-02-16 19:18:20 -0800408 top_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800409 top_claw_velocity_ = bottom_claw_velocity_ =
410 values.claw.claw_zeroing_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800411 LOG(DEBUG, "Ready to fine tune the top\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800412 mode_ = FINE_TUNE_TOP;
Austin Schuhcda86af2014-02-16 16:16:39 -0800413 } else {
414 // send top to zeroing start
Austin Schuhd27931c2014-02-16 19:18:20 -0800415 top_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuhcda86af2014-02-16 16:16:39 -0800416 LOG(DEBUG, "Going to the start position for the top\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800417 mode_ = PREP_FINE_TUNE_TOP;
Austin Schuhcda86af2014-02-16 16:16:39 -0800418 }
419 } else {
Austin Schuhe7f90d12014-02-17 00:48:25 -0800420 mode_ = FINE_TUNE_TOP;
Austin Schuhd27931c2014-02-16 19:18:20 -0800421 top_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800422 top_claw_velocity_ = bottom_claw_velocity_ =
423 values.claw.claw_zeroing_speed;
Brian Silvermane0a95462014-02-17 00:41:09 -0800424 if (top_claw_.front_or_back_triggered() ||
425 bottom_claw_.front_or_back_triggered()) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800426 // this should not happen, but now we know it won't
427 doing_calibration_fine_tune_ = false;
Austin Schuhd27931c2014-02-16 19:18:20 -0800428 top_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuh069143b2014-02-17 02:46:26 -0800429 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
Austin Schuhcda86af2014-02-16 16:16:39 -0800430 LOG(DEBUG, "Found a limit, starting over.\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800431 mode_ = PREP_FINE_TUNE_TOP;
Austin Schuhcda86af2014-02-16 16:16:39 -0800432 }
Brian Silvermane0a95462014-02-17 00:41:09 -0800433 if (top_claw_.calibration().value()) {
434 if (top_claw_.calibration().posedge_count_changed() &&
Austin Schuh288c8c32014-02-16 17:20:17 -0800435 position) {
436 // do calibration
Austin Schuhd27931c2014-02-16 19:18:20 -0800437 top_claw_.SetCalibration(
438 position->top.posedge_value,
439 values.claw.upper_claw.calibration.lower_angle);
Austin Schuh288c8c32014-02-16 17:20:17 -0800440 top_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED);
Austin Schuhe7f90d12014-02-17 00:48:25 -0800441 // calibrated so we are done fine tuning top
Austin Schuh288c8c32014-02-16 17:20:17 -0800442 doing_calibration_fine_tune_ = false;
443 LOG(DEBUG, "Calibrated the top correctly!\n");
444 } else {
445 doing_calibration_fine_tune_ = false;
Austin Schuhd27931c2014-02-16 19:18:20 -0800446 top_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuh069143b2014-02-17 02:46:26 -0800447 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800448 mode_ = PREP_FINE_TUNE_TOP;
Austin Schuh288c8c32014-02-16 17:20:17 -0800449 }
Austin Schuhcda86af2014-02-16 16:16:39 -0800450 }
451 }
452 // now set the bottom claw to track
Austin Schuhd27931c2014-02-16 19:18:20 -0800453 bottom_claw_goal_ = top_claw_goal_ - values.claw.claw_zeroing_separation;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800454 }
455 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800456 doing_calibration_fine_tune_ = false;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800457 if (!was_enabled_ && enabled) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800458 if (position) {
459 top_claw_goal_ = position->top.position;
460 bottom_claw_goal_ = position->bottom.position;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800461 initial_separation_ =
Austin Schuhcda86af2014-02-16 16:16:39 -0800462 position->top.position - position->bottom.position;
Austin Schuhf9286cd2014-02-11 00:51:09 -0800463 } else {
464 has_top_claw_goal_ = false;
465 has_bottom_claw_goal_ = false;
466 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800467 }
Austin Schuhf9286cd2014-02-11 00:51:09 -0800468
Austin Schuh4cb047f2014-02-16 21:10:19 -0800469 if ((bottom_claw_.zeroing_state() !=
470 ZeroedStateFeedbackLoop::UNKNOWN_POSITION ||
Brian Silvermane0a95462014-02-17 00:41:09 -0800471 bottom_claw_.front().value() || top_claw_.front().value()) &&
472 !top_claw_.back().value() && !bottom_claw_.back().value()) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800473 if (enabled) {
474 // Time to slowly move back up to find any position to narrow down the
475 // zero.
Austin Schuhd27931c2014-02-16 19:18:20 -0800476 top_claw_goal_ += values.claw.claw_zeroing_off_speed * dt;
477 bottom_claw_goal_ += values.claw.claw_zeroing_off_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800478 top_claw_velocity_ = bottom_claw_velocity_ =
479 values.claw.claw_zeroing_off_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800480 LOG(DEBUG, "Bottom is known.\n");
Austin Schuhf9286cd2014-02-11 00:51:09 -0800481 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800482 } else {
483 // We don't know where either claw is. Slowly start moving down to find
484 // any hall effect.
Austin Schuhf9286cd2014-02-11 00:51:09 -0800485 if (enabled) {
Austin Schuhd27931c2014-02-16 19:18:20 -0800486 top_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt;
487 bottom_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800488 top_claw_velocity_ = bottom_claw_velocity_ =
489 -values.claw.claw_zeroing_off_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800490 LOG(DEBUG, "Both are unknown.\n");
Austin Schuhf9286cd2014-02-11 00:51:09 -0800491 }
492 }
493
494 if (enabled) {
495 top_claw_.SetCalibrationOnEdge(
Austin Schuhd27931c2014-02-16 19:18:20 -0800496 values.claw.upper_claw, ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800497 bottom_claw_.SetCalibrationOnEdge(
Austin Schuhd27931c2014-02-16 19:18:20 -0800498 values.claw.lower_claw, ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800499 } else {
Austin Schuh069143b2014-02-17 02:46:26 -0800500 // TODO(austin): Only calibrate on the predetermined edge.
501 // We might be able to just ignore this since the backlash is soooo low. :)
Austin Schuhf9286cd2014-02-11 00:51:09 -0800502 top_claw_.SetCalibrationOnEdge(
Austin Schuhd27931c2014-02-16 19:18:20 -0800503 values.claw.upper_claw, ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800504 bottom_claw_.SetCalibrationOnEdge(
Austin Schuhd27931c2014-02-16 19:18:20 -0800505 values.claw.lower_claw, ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800506 }
Austin Schuhe7f90d12014-02-17 00:48:25 -0800507 mode_ = UNKNOWN_LOCATION;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800508 }
509
Austin Schuh069143b2014-02-17 02:46:26 -0800510 // Limit the goals if both claws have been (mostly) found.
511 if (mode_ != UNKNOWN_LOCATION) {
512 LimitClawGoal(&bottom_claw_goal_, &top_claw_goal_, values);
513 }
514
Austin Schuhf9286cd2014-02-11 00:51:09 -0800515 if (has_top_claw_goal_ && has_bottom_claw_goal_) {
Austin Schuh069143b2014-02-17 02:46:26 -0800516 claw_.R << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_,
517 bottom_claw_velocity_, top_claw_velocity_ - bottom_claw_velocity_;
Austin Schuhcda86af2014-02-16 16:16:39 -0800518 double separation = -971;
519 if (position != nullptr) {
520 separation = position->top.position - position->bottom.position;
521 }
522 LOG(DEBUG, "Goal is %f (bottom) %f, separation is %f\n", claw_.R(0, 0),
523 claw_.R(1, 0), separation);
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800524
Austin Schuh4cb047f2014-02-16 21:10:19 -0800525 // Only cap power when one of the halves of the claw is unknown.
Austin Schuhe7f90d12014-02-17 00:48:25 -0800526 claw_.set_is_zeroing(mode_ == UNKNOWN_LOCATION || mode_ == FINE_TUNE_TOP ||
527 mode_ == FINE_TUNE_BOTTOM);
Austin Schuhcda86af2014-02-16 16:16:39 -0800528 claw_.Update(output == nullptr);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800529 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800530 claw_.Update(true);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800531 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800532
Austin Schuh4cb047f2014-02-16 21:10:19 -0800533 capped_goal_ = false;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800534 switch (mode_) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800535 case READY:
Austin Schuhe7f90d12014-02-17 00:48:25 -0800536 case PREP_FINE_TUNE_TOP:
537 case PREP_FINE_TUNE_BOTTOM:
Austin Schuhcda86af2014-02-16 16:16:39 -0800538 break;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800539 case FINE_TUNE_BOTTOM:
540 case FINE_TUNE_TOP:
Austin Schuh4cb047f2014-02-16 21:10:19 -0800541 case UNKNOWN_LOCATION: {
542 if (claw_.uncapped_average_voltage() > values.claw.max_zeroing_voltage) {
543 double dx = (claw_.uncapped_average_voltage() -
544 values.claw.max_zeroing_voltage) /
545 claw_.K(0, 0);
546 bottom_claw_goal_ -= dx;
547 top_claw_goal_ -= dx;
Austin Schuhcda86af2014-02-16 16:16:39 -0800548 capped_goal_ = true;
Austin Schuh4cb047f2014-02-16 21:10:19 -0800549 LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx);
Austin Schuhe7f90d12014-02-17 00:48:25 -0800550 LOG(DEBUG, "Uncapped is %f, max is %f, difference is %f\n",
551 claw_.uncapped_average_voltage(), values.claw.max_zeroing_voltage,
552 (claw_.uncapped_average_voltage() -
553 values.claw.max_zeroing_voltage));
Austin Schuh4cb047f2014-02-16 21:10:19 -0800554 } else if (claw_.uncapped_average_voltage() <
555 -values.claw.max_zeroing_voltage) {
556 double dx = (claw_.uncapped_average_voltage() +
557 values.claw.max_zeroing_voltage) /
558 claw_.K(0, 0);
559 bottom_claw_goal_ -= dx;
560 top_claw_goal_ -= dx;
Austin Schuhcda86af2014-02-16 16:16:39 -0800561 capped_goal_ = true;
Austin Schuh4cb047f2014-02-16 21:10:19 -0800562 LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx);
Austin Schuhcda86af2014-02-16 16:16:39 -0800563 }
Austin Schuh4cb047f2014-02-16 21:10:19 -0800564 } break;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800565 }
566
567 if (output) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800568 output->top_claw_voltage = claw_.U(1, 0) + claw_.U(0, 0);
569 output->bottom_claw_voltage = claw_.U(0, 0);
Austin Schuh3bb9a442014-02-02 16:01:45 -0800570 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800571 status->done = false;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800572
573 was_enabled_ = ::aos::robot_state->enabled;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800574}
575
576} // namespace control_loops
577} // namespace frc971