blob: abff616492ba89ad06691fb022b6c03a80e59f3a [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;
56 U(1, 0) -= difference;
57 } else if (uncapped_average_voltage_ < -values.claw.max_zeroing_voltage) {
58 const double difference =
59 -uncapped_average_voltage_ - values.claw.max_zeroing_voltage;
60 U(0, 0) += difference;
61 U(1, 0) += difference;
62 }
63 }
64
65 double max_value =
66 ::std::max(::std::abs(U(0, 0)), ::std::abs(U(1, 0) + U(0, 0)));
67
Austin Schuhcda86af2014-02-16 16:16:39 -080068 if (max_value > 12.0) {
69 LOG(DEBUG, "Capping U because max is %f\n", max_value);
70 U = U * 12.0 / max_value;
71 LOG(DEBUG, "Capping U is now %f %f\n", U(0, 0), U(1, 0));
Austin Schuh4b7b5d02014-02-10 21:20:34 -080072 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -080073}
74
Austin Schuhcc0bf312014-02-09 00:39:29 -080075ClawMotor::ClawMotor(control_loops::ClawGroup *my_claw)
76 : aos::control_loops::ControlLoop<control_loops::ClawGroup>(my_claw),
Austin Schuh4b7b5d02014-02-10 21:20:34 -080077 has_top_claw_goal_(false),
78 top_claw_goal_(0.0),
Austin Schuhcda86af2014-02-16 16:16:39 -080079 top_claw_(this),
Austin Schuh4b7b5d02014-02-10 21:20:34 -080080 has_bottom_claw_goal_(false),
81 bottom_claw_goal_(0.0),
Austin Schuhcda86af2014-02-16 16:16:39 -080082 bottom_claw_(this),
83 claw_(MakeClawLoop()),
Ben Fredrickson9b388422014-02-13 06:15:31 +000084 was_enabled_(false),
Austin Schuh4cb047f2014-02-16 21:10:19 -080085 doing_calibration_fine_tune_(false),
86 capped_goal_(false) {}
Austin Schuh3bb9a442014-02-02 16:01:45 -080087
Austin Schuh4b7b5d02014-02-10 21:20:34 -080088const int ZeroedStateFeedbackLoop::kZeroingMaxVoltage;
Austin Schuh3bb9a442014-02-02 16:01:45 -080089
Brian Silvermane0a95462014-02-17 00:41:09 -080090bool ZeroedStateFeedbackLoop::DoGetPositionOfEdge(
91 const constants::Values::Claws::AnglePair &angles, double *edge_encoder,
92 double *edge_angle, const HallEffectTracker &sensor,
93 const char *hall_effect_name) {
94 if (sensor.posedge_count_changed()) {
95 if (posedge_value_ < last_encoder()) {
96 *edge_angle = angles.upper_angle;
97 LOG(INFO, "%s Posedge upper of %s -> %f\n", name_,
98 hall_effect_name, *edge_angle);
99 } else {
100 *edge_angle = angles.lower_angle;
101 LOG(INFO, "%s Posedge lower of %s -> %f\n", name_,
102 hall_effect_name, *edge_angle);
103 }
104 *edge_encoder = posedge_value_;
105 return true;
106 }
107 if (sensor.negedge_count_changed()) {
108 if (negedge_value_ > last_encoder()) {
109 *edge_angle = angles.upper_angle;
110 LOG(INFO, "%s Negedge upper of %s -> %f\n", name_,
111 hall_effect_name, *edge_angle);
112 } else {
113 *edge_angle = angles.lower_angle;
114 LOG(INFO, "%s Negedge lower of %s -> %f\n", name_,
115 hall_effect_name, *edge_angle);
116 }
117 *edge_encoder = negedge_value_;
118 return true;
119 }
120
121 return false;
122}
123
Austin Schuhf9286cd2014-02-11 00:51:09 -0800124bool ZeroedStateFeedbackLoop::GetPositionOfEdge(
Austin Schuhd27931c2014-02-16 19:18:20 -0800125 const constants::Values::Claws::Claw &claw_values, double *edge_encoder,
Austin Schuhf9286cd2014-02-11 00:51:09 -0800126 double *edge_angle) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800127 // TODO(austin): Validate that the hall effect edge makes sense.
128 // We must now be on the side of the edge that we expect to be, and the
129 // encoder must have been on either side of the edge before and after.
130
Austin Schuhcda86af2014-02-16 16:16:39 -0800131 // TODO(austin): Compute the last off range min and max and compare the edge
132 // value to the middle of the range. This will be quite a bit more reliable.
133
Brian Silvermane0a95462014-02-17 00:41:09 -0800134 if (DoGetPositionOfEdge(claw_values.front, edge_encoder, edge_angle,
135 front_, "front")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800136 return true;
137 }
Brian Silvermane0a95462014-02-17 00:41:09 -0800138 if (DoGetPositionOfEdge(claw_values.calibration, edge_encoder, edge_angle,
139 calibration_, "calibration")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800140 return true;
141 }
Brian Silvermane0a95462014-02-17 00:41:09 -0800142 if (DoGetPositionOfEdge(claw_values.back, edge_encoder, edge_angle,
143 back_, "back")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800144 return true;
145 }
146 return false;
147}
148
Austin Schuhcda86af2014-02-16 16:16:39 -0800149void TopZeroedStateFeedbackLoop::SetCalibration(double edge_encoder,
150 double edge_angle) {
151 double old_offset = offset_;
152 offset_ = edge_angle - edge_encoder;
153 const double doffset = offset_ - old_offset;
154 motor_->ChangeTopOffset(doffset);
155}
156
157void BottomZeroedStateFeedbackLoop::SetCalibration(double edge_encoder,
158 double edge_angle) {
159 double old_offset = offset_;
160 offset_ = edge_angle - edge_encoder;
161 const double doffset = offset_ - old_offset;
162 motor_->ChangeBottomOffset(doffset);
163}
164
165void ClawMotor::ChangeTopOffset(double doffset) {
166 claw_.ChangeTopOffset(doffset);
167 if (has_top_claw_goal_) {
168 top_claw_goal_ += doffset;
169 }
170}
171
172void ClawMotor::ChangeBottomOffset(double doffset) {
173 claw_.ChangeBottomOffset(doffset);
174 if (has_bottom_claw_goal_) {
175 bottom_claw_goal_ += doffset;
176 }
177}
178
179void ClawLimitedLoop::ChangeTopOffset(double doffset) {
180 Y_(1, 0) += doffset;
181 X_hat(1, 0) += doffset;
182 LOG(INFO, "Changing top offset by %f\n", doffset);
183}
184void ClawLimitedLoop::ChangeBottomOffset(double doffset) {
185 Y_(0, 0) += doffset;
186 X_hat(0, 0) += doffset;
187 X_hat(1, 0) -= doffset;
188 LOG(INFO, "Changing bottom offset by %f\n", doffset);
189}
190
Austin Schuh3bb9a442014-02-02 16:01:45 -0800191// Positive angle is up, and positive power is up.
Austin Schuhcc0bf312014-02-09 00:39:29 -0800192void ClawMotor::RunIteration(const control_loops::ClawGroup::Goal *goal,
193 const control_loops::ClawGroup::Position *position,
194 control_loops::ClawGroup::Output *output,
Austin Schuh3bb9a442014-02-02 16:01:45 -0800195 ::aos::control_loops::Status *status) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800196 constexpr double dt = 0.01;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800197
198 // Disable the motors now so that all early returns will return with the
199 // motors disabled.
200 if (output) {
201 output->top_claw_voltage = 0;
202 output->bottom_claw_voltage = 0;
203 output->intake_voltage = 0;
204 }
205
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800206 // TODO(austin): Handle the disabled state and the disabled -> enabled
Austin Schuhcda86af2014-02-16 16:16:39 -0800207 // transition in all of these states.
208 // TODO(austin): Handle zeroing while disabled correctly (only use a single
209 // edge and direction when zeroing.)
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800210
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800211 if (::aos::robot_state.get() == nullptr) {
212 return;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800213 }
214
Austin Schuhf9286cd2014-02-11 00:51:09 -0800215 const frc971::constants::Values &values = constants::GetValues();
216
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800217 if (position) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800218 Eigen::Matrix<double, 2, 1> Y;
219 Y << position->bottom.position + bottom_claw_.offset(),
220 position->top.position + top_claw_.offset();
221 claw_.Correct(Y);
222
Austin Schuhf9286cd2014-02-11 00:51:09 -0800223 top_claw_.SetPositionValues(position->top);
224 bottom_claw_.SetPositionValues(position->bottom);
225
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800226 if (!has_top_claw_goal_) {
227 has_top_claw_goal_ = true;
Austin Schuhcda86af2014-02-16 16:16:39 -0800228 top_claw_goal_ = top_claw_.absolute_position();
229 initial_seperation_ =
230 top_claw_.absolute_position() - bottom_claw_.absolute_position();
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800231 }
232 if (!has_bottom_claw_goal_) {
233 has_bottom_claw_goal_ = true;
Austin Schuhcda86af2014-02-16 16:16:39 -0800234 bottom_claw_goal_ = bottom_claw_.absolute_position();
235 initial_seperation_ =
236 top_claw_.absolute_position() - bottom_claw_.absolute_position();
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800237 }
Austin Schuhcda86af2014-02-16 16:16:39 -0800238 LOG(DEBUG, "Claw position is (top: %f bottom: %f\n",
239 top_claw_.absolute_position(), bottom_claw_.absolute_position());
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800240 }
241
242 bool autonomous = ::aos::robot_state->autonomous;
Austin Schuhf9286cd2014-02-11 00:51:09 -0800243 bool enabled = ::aos::robot_state->enabled;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800244
Austin Schuhcda86af2014-02-16 16:16:39 -0800245 enum CalibrationMode {
246 READY,
247 FINE_TUNE,
248 UNKNOWN_LOCATION
249 };
250
251 CalibrationMode mode;
252
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800253 if ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED &&
254 bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) ||
255 (autonomous &&
256 ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
257 top_claw_.zeroing_state() ==
258 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
259 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
260 bottom_claw_.zeroing_state() ==
261 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION)))) {
262 // Ready to use the claw.
263 // Limit the goals here.
Austin Schuhf9286cd2014-02-11 00:51:09 -0800264 bottom_claw_goal_ = goal->bottom_angle;
265 top_claw_goal_ = goal->bottom_angle + goal->seperation_angle;
Austin Schuhcda86af2014-02-16 16:16:39 -0800266 has_bottom_claw_goal_ = true;
267 has_top_claw_goal_ = true;
268 doing_calibration_fine_tune_ = false;
269
270 mode = READY;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800271 } else if (top_claw_.zeroing_state() !=
272 ZeroedStateFeedbackLoop::UNKNOWN_POSITION &&
273 bottom_claw_.zeroing_state() !=
274 ZeroedStateFeedbackLoop::UNKNOWN_POSITION) {
275 // Time to fine tune the zero.
276 // Limit the goals here.
277 if (bottom_claw_.zeroing_state() != ZeroedStateFeedbackLoop::CALIBRATED) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800278 // always get the bottom claw to calibrated first
279 LOG(DEBUG, "Calibrating the bottom of the claw\n");
280 if (!doing_calibration_fine_tune_) {
281 if (::std::abs(bottom_absolute_position() -
Austin Schuhd27931c2014-02-16 19:18:20 -0800282 values.claw.start_fine_tune_pos) <
283 values.claw.claw_unimportant_epsilon) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800284 doing_calibration_fine_tune_ = true;
Austin Schuhd27931c2014-02-16 19:18:20 -0800285 bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuhcda86af2014-02-16 16:16:39 -0800286 LOG(DEBUG, "Ready to fine tune the bottom\n");
287 } else {
288 // send bottom to zeroing start
Austin Schuhd27931c2014-02-16 19:18:20 -0800289 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuhcda86af2014-02-16 16:16:39 -0800290 LOG(DEBUG, "Going to the start position for the bottom\n");
291 }
292 } else {
Austin Schuhd27931c2014-02-16 19:18:20 -0800293 bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Brian Silvermane0a95462014-02-17 00:41:09 -0800294 if (top_claw_.front_or_back_triggered() ||
295 bottom_claw_.front_or_back_triggered()) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800296 // We shouldn't hit a limit, but if we do, go back to the zeroing
297 // point and try again.
298 doing_calibration_fine_tune_ = false;
Austin Schuhd27931c2014-02-16 19:18:20 -0800299 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuhcda86af2014-02-16 16:16:39 -0800300 LOG(DEBUG, "Found a limit, starting over.\n");
301 }
Austin Schuh288c8c32014-02-16 17:20:17 -0800302
Brian Silvermane0a95462014-02-17 00:41:09 -0800303 if (bottom_claw_.calibration().value()) {
304 if (bottom_claw_.calibration().posedge_count_changed() &&
Austin Schuh288c8c32014-02-16 17:20:17 -0800305 position) {
306 // do calibration
307 bottom_claw_.SetCalibration(
308 position->bottom.posedge_value,
Austin Schuhd27931c2014-02-16 19:18:20 -0800309 values.claw.lower_claw.calibration.lower_angle);
Austin Schuh288c8c32014-02-16 17:20:17 -0800310 bottom_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED);
311 // calibrated so we are done fine tuning bottom
312 doing_calibration_fine_tune_ = false;
313 LOG(DEBUG, "Calibrated the bottom correctly!\n");
314 } else {
315 doing_calibration_fine_tune_ = false;
Austin Schuhd27931c2014-02-16 19:18:20 -0800316 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuh288c8c32014-02-16 17:20:17 -0800317 }
Austin Schuhcda86af2014-02-16 16:16:39 -0800318 } else {
319 LOG(DEBUG, "Fine tuning\n");
320 }
321 }
322 // now set the top claw to track
323
Austin Schuhd27931c2014-02-16 19:18:20 -0800324 top_claw_goal_ = bottom_claw_goal_ + values.claw.claw_zeroing_separation;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800325 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800326 // bottom claw must be calibrated, start on the top
327 if (!doing_calibration_fine_tune_) {
Austin Schuhd27931c2014-02-16 19:18:20 -0800328 if (::std::abs(top_absolute_position() -
329 values.claw.start_fine_tune_pos) <
330 values.claw.claw_unimportant_epsilon) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800331 doing_calibration_fine_tune_ = true;
Austin Schuhd27931c2014-02-16 19:18:20 -0800332 top_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuhcda86af2014-02-16 16:16:39 -0800333 LOG(DEBUG, "Ready to fine tune the top\n");
334 } else {
335 // send top to zeroing start
Austin Schuhd27931c2014-02-16 19:18:20 -0800336 top_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuhcda86af2014-02-16 16:16:39 -0800337 LOG(DEBUG, "Going to the start position for the top\n");
338 }
339 } else {
Austin Schuhd27931c2014-02-16 19:18:20 -0800340 top_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Brian Silvermane0a95462014-02-17 00:41:09 -0800341 if (top_claw_.front_or_back_triggered() ||
342 bottom_claw_.front_or_back_triggered()) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800343 // this should not happen, but now we know it won't
344 doing_calibration_fine_tune_ = false;
Austin Schuhd27931c2014-02-16 19:18:20 -0800345 top_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuhcda86af2014-02-16 16:16:39 -0800346 LOG(DEBUG, "Found a limit, starting over.\n");
347 }
Brian Silvermane0a95462014-02-17 00:41:09 -0800348 if (top_claw_.calibration().value()) {
349 if (top_claw_.calibration().posedge_count_changed() &&
Austin Schuh288c8c32014-02-16 17:20:17 -0800350 position) {
351 // do calibration
Austin Schuhd27931c2014-02-16 19:18:20 -0800352 top_claw_.SetCalibration(
353 position->top.posedge_value,
354 values.claw.upper_claw.calibration.lower_angle);
Austin Schuh288c8c32014-02-16 17:20:17 -0800355 top_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED);
356 // calinrated so we are done fine tuning top
357 doing_calibration_fine_tune_ = false;
358 LOG(DEBUG, "Calibrated the top correctly!\n");
359 } else {
360 doing_calibration_fine_tune_ = false;
Austin Schuhd27931c2014-02-16 19:18:20 -0800361 top_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuh288c8c32014-02-16 17:20:17 -0800362 }
Austin Schuhcda86af2014-02-16 16:16:39 -0800363 }
364 }
365 // now set the bottom claw to track
Austin Schuhd27931c2014-02-16 19:18:20 -0800366 bottom_claw_goal_ = top_claw_goal_ - values.claw.claw_zeroing_separation;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800367 }
Austin Schuhcda86af2014-02-16 16:16:39 -0800368 mode = FINE_TUNE;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800369 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800370 doing_calibration_fine_tune_ = false;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800371 if (!was_enabled_ && enabled) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800372 if (position) {
373 top_claw_goal_ = position->top.position;
374 bottom_claw_goal_ = position->bottom.position;
Austin Schuhcda86af2014-02-16 16:16:39 -0800375 initial_seperation_ =
376 position->top.position - position->bottom.position;
Austin Schuhf9286cd2014-02-11 00:51:09 -0800377 } else {
378 has_top_claw_goal_ = false;
379 has_bottom_claw_goal_ = false;
380 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800381 }
Austin Schuhf9286cd2014-02-11 00:51:09 -0800382
383 // TODO(austin): Limit the goals here.
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800384
Austin Schuh4cb047f2014-02-16 21:10:19 -0800385 if ((bottom_claw_.zeroing_state() !=
386 ZeroedStateFeedbackLoop::UNKNOWN_POSITION ||
Brian Silvermane0a95462014-02-17 00:41:09 -0800387 bottom_claw_.front().value() || top_claw_.front().value()) &&
388 !top_claw_.back().value() && !bottom_claw_.back().value()) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800389 if (enabled) {
390 // Time to slowly move back up to find any position to narrow down the
391 // zero.
Austin Schuhd27931c2014-02-16 19:18:20 -0800392 top_claw_goal_ += values.claw.claw_zeroing_off_speed * dt;
393 bottom_claw_goal_ += values.claw.claw_zeroing_off_speed * dt;
Austin Schuhf9286cd2014-02-11 00:51:09 -0800394 // TODO(austin): Goal velocity too!
Austin Schuhcda86af2014-02-16 16:16:39 -0800395 LOG(DEBUG, "Bottom is known.\n");
Austin Schuhf9286cd2014-02-11 00:51:09 -0800396 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800397 } else {
398 // We don't know where either claw is. Slowly start moving down to find
399 // any hall effect.
Austin Schuhf9286cd2014-02-11 00:51:09 -0800400 if (enabled) {
Austin Schuhd27931c2014-02-16 19:18:20 -0800401 top_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt;
402 bottom_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt;
Austin Schuhf9286cd2014-02-11 00:51:09 -0800403 // TODO(austin): Goal velocity too!
Austin Schuhcda86af2014-02-16 16:16:39 -0800404 LOG(DEBUG, "Both are unknown.\n");
Austin Schuhf9286cd2014-02-11 00:51:09 -0800405 }
406 }
407
408 if (enabled) {
409 top_claw_.SetCalibrationOnEdge(
Austin Schuhd27931c2014-02-16 19:18:20 -0800410 values.claw.upper_claw, ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800411 bottom_claw_.SetCalibrationOnEdge(
Austin Schuhd27931c2014-02-16 19:18:20 -0800412 values.claw.lower_claw, ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800413 } else {
414 top_claw_.SetCalibrationOnEdge(
Austin Schuhd27931c2014-02-16 19:18:20 -0800415 values.claw.upper_claw, ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800416 bottom_claw_.SetCalibrationOnEdge(
Austin Schuhd27931c2014-02-16 19:18:20 -0800417 values.claw.lower_claw, ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800418 }
Austin Schuhcda86af2014-02-16 16:16:39 -0800419 mode = UNKNOWN_LOCATION;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800420 }
421
Austin Schuh288c8c32014-02-16 17:20:17 -0800422 // TODO(austin): Handle disabled properly everwhere... Restart and all that
423 // jazz.
424
Austin Schuhf9286cd2014-02-11 00:51:09 -0800425 if (has_top_claw_goal_ && has_bottom_claw_goal_) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800426 claw_.R << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_, 0, 0;
427 double separation = -971;
428 if (position != nullptr) {
429 separation = position->top.position - position->bottom.position;
430 }
431 LOG(DEBUG, "Goal is %f (bottom) %f, separation is %f\n", claw_.R(0, 0),
432 claw_.R(1, 0), separation);
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800433
Austin Schuh4cb047f2014-02-16 21:10:19 -0800434 // Only cap power when one of the halves of the claw is unknown.
435 claw_.set_is_zeroing(mode == UNKNOWN_LOCATION);
Austin Schuhcda86af2014-02-16 16:16:39 -0800436 claw_.Update(output == nullptr);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800437 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800438 claw_.Update(true);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800439 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800440
Austin Schuh4cb047f2014-02-16 21:10:19 -0800441 capped_goal_ = false;
Austin Schuhcda86af2014-02-16 16:16:39 -0800442 switch (mode) {
443 case READY:
444 break;
445 case FINE_TUNE:
446 break;
Austin Schuh4cb047f2014-02-16 21:10:19 -0800447 case UNKNOWN_LOCATION: {
448 if (claw_.uncapped_average_voltage() > values.claw.max_zeroing_voltage) {
449 double dx = (claw_.uncapped_average_voltage() -
450 values.claw.max_zeroing_voltage) /
451 claw_.K(0, 0);
452 bottom_claw_goal_ -= dx;
453 top_claw_goal_ -= dx;
Austin Schuhcda86af2014-02-16 16:16:39 -0800454 capped_goal_ = true;
Austin Schuh4cb047f2014-02-16 21:10:19 -0800455 LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx);
456 } else if (claw_.uncapped_average_voltage() <
457 -values.claw.max_zeroing_voltage) {
458 double dx = (claw_.uncapped_average_voltage() +
459 values.claw.max_zeroing_voltage) /
460 claw_.K(0, 0);
461 bottom_claw_goal_ -= dx;
462 top_claw_goal_ -= dx;
Austin Schuhcda86af2014-02-16 16:16:39 -0800463 capped_goal_ = true;
Austin Schuh4cb047f2014-02-16 21:10:19 -0800464 LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx);
Austin Schuhcda86af2014-02-16 16:16:39 -0800465 }
Austin Schuh4cb047f2014-02-16 21:10:19 -0800466 } break;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800467 }
468
469 if (output) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800470 output->top_claw_voltage = claw_.U(1, 0) + claw_.U(0, 0);
471 output->bottom_claw_voltage = claw_.U(0, 0);
Austin Schuh3bb9a442014-02-02 16:01:45 -0800472 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800473 status->done = false;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800474
475 was_enabled_ = ::aos::robot_state->enabled;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800476}
477
478} // namespace control_loops
479} // namespace frc971