blob: 8de4dd8cd838ed9b03f6ecf5f6db4643516da022 [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"
Brian Silvermanf48fab32014-03-09 14:32:24 -07009#include "aos/common/logging/queue_logging.h"
Austin Schuh3bb9a442014-02-02 16:01:45 -080010
11#include "frc971/constants.h"
Austin Schuhcda86af2014-02-16 16:16:39 -080012#include "frc971/control_loops/claw/claw_motor_plant.h"
13
Austin Schuh3bb9a442014-02-02 16:01:45 -080014// Zeroing plan.
15// There are 2 types of zeros. Enabled and disabled ones.
16// Disabled ones are only valid during auto mode, and can be used to speed up
17// the enabled zero process. We need to re-zero during teleop in case the auto
18// zero was poor and causes us to miss all our shots.
19//
20// We need to be able to zero manually while disabled by moving the joint over
21// the zeros.
22// Zero on the down edge when disabled (gravity in the direction of motion)
23//
24// When enabled, zero on the up edge (gravity opposing the direction of motion)
25// The enabled sequence needs to work as follows. We can crash the claw if we
26// bring them too close to each other or too far from each other. The only safe
27// thing to do is to move them in unison.
28//
29// Start by moving them both towards the front of the bot to either find either
30// the middle hall effect on either jaw, or the front hall effect on the bottom
31// jaw. Any edge that isn't the desired edge will provide an approximate edge
32// location that can be used for the fine tuning step.
33// Once an edge is found on the front claw, move back the other way with both
34// claws until an edge is found for the other claw.
35// Now that we have an approximate zero, we can robustify the limits to keep
36// both claws safe. Then, we can move both claws to a position that is the
37// correct side of the zero and go zero.
38
39// Valid region plan.
Ben Fredrickson81ba2d52014-03-02 08:21:46 +000040// Difference between the arms has a range, and the values of each arm has a
41// range.
Austin Schuh3bb9a442014-02-02 16:01:45 -080042// If a claw runs up against a static limit, don't let the goal change outside
43// the limit.
44// If a claw runs up against a movable limit, move both claws outwards to get
45// out of the condition.
46
47namespace frc971 {
48namespace control_loops {
49
Austin Schuh01c652b2014-02-21 23:13:42 -080050static const double kZeroingVoltage = 4.0;
51static const double kMaxVoltage = 12.0;
Austin Schuhf84a1302014-02-19 00:23:30 -080052
Austin Schuh27b8fb12014-02-22 15:10:05 -080053ClawLimitedLoop::ClawLimitedLoop(StateFeedbackLoop<4, 2, 2> loop)
54 : StateFeedbackLoop<4, 2, 2>(loop),
55 uncapped_average_voltage_(0.0),
56 is_zeroing_(true) {}
57
Austin Schuhcda86af2014-02-16 16:16:39 -080058void ClawLimitedLoop::CapU() {
Austin Schuh4cb047f2014-02-16 21:10:19 -080059 uncapped_average_voltage_ = U(0, 0) + U(1, 0) / 2.0;
60 if (is_zeroing_) {
Austin Schuhf84a1302014-02-19 00:23:30 -080061 LOG(DEBUG, "zeroing\n");
Austin Schuh4cb047f2014-02-16 21:10:19 -080062 const frc971::constants::Values &values = constants::GetValues();
63 if (uncapped_average_voltage_ > values.claw.max_zeroing_voltage) {
64 const double difference =
65 uncapped_average_voltage_ - values.claw.max_zeroing_voltage;
66 U(0, 0) -= difference;
Austin Schuh4cb047f2014-02-16 21:10:19 -080067 } else if (uncapped_average_voltage_ < -values.claw.max_zeroing_voltage) {
68 const double difference =
69 -uncapped_average_voltage_ - values.claw.max_zeroing_voltage;
70 U(0, 0) += difference;
Austin Schuh4cb047f2014-02-16 21:10:19 -080071 }
72 }
73
74 double max_value =
75 ::std::max(::std::abs(U(0, 0)), ::std::abs(U(1, 0) + U(0, 0)));
76
Austin Schuh01c652b2014-02-21 23:13:42 -080077 const double k_max_voltage = is_zeroing_ ? kZeroingVoltage : kMaxVoltage;
78 if (max_value > k_max_voltage) {
Austin Schuh01c652b2014-02-21 23:13:42 -080079 U = U * k_max_voltage / max_value;
Brian Silvermanf48fab32014-03-09 14:32:24 -070080 LOG(DEBUG, "Capping U is now %f %f (max is %f)\n",
81 U(0, 0), U(1, 0), max_value);
Austin Schuh4b7b5d02014-02-10 21:20:34 -080082 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -080083}
84
Austin Schuh27b8fb12014-02-22 15:10:05 -080085ZeroedStateFeedbackLoop::ZeroedStateFeedbackLoop(const char *name,
86 ClawMotor *motor)
87 : offset_(0.0),
88 name_(name),
89 motor_(motor),
90 zeroing_state_(UNKNOWN_POSITION),
91 posedge_value_(0.0),
92 negedge_value_(0.0),
93 encoder_(0.0),
94 last_encoder_(0.0) {}
95
96void ZeroedStateFeedbackLoop::SetPositionValues(const HalfClawPosition &claw) {
97 front_.Update(claw.front);
98 calibration_.Update(claw.calibration);
99 back_.Update(claw.back);
100
101 bool any_sensor_triggered = any_triggered();
102 if (any_sensor_triggered && any_triggered_last_) {
103 // We are still on the hall effect and nothing has changed.
104 min_hall_effect_on_angle_ =
105 ::std::min(min_hall_effect_on_angle_, claw.position);
106 max_hall_effect_on_angle_ =
107 ::std::max(max_hall_effect_on_angle_, claw.position);
108 } else if (!any_sensor_triggered && !any_triggered_last_) {
109 // We are still off the hall effect and nothing has changed.
110 min_hall_effect_off_angle_ =
111 ::std::min(min_hall_effect_off_angle_, claw.position);
112 max_hall_effect_off_angle_ =
113 ::std::max(max_hall_effect_off_angle_, claw.position);
114 } else if (any_sensor_triggered && !any_triggered_last_) {
115 // Saw a posedge on the hall effect. Reset the limits.
116 min_hall_effect_on_angle_ = ::std::min(claw.posedge_value, claw.position);
117 max_hall_effect_on_angle_ = ::std::max(claw.posedge_value, claw.position);
118 } else if (!any_sensor_triggered && any_triggered_last_) {
119 // Saw a negedge on the hall effect. Reset the limits.
120 min_hall_effect_off_angle_ = ::std::min(claw.negedge_value, claw.position);
121 max_hall_effect_off_angle_ = ::std::max(claw.negedge_value, claw.position);
122 }
123
124 posedge_value_ = claw.posedge_value;
125 negedge_value_ = claw.negedge_value;
126 last_encoder_ = encoder_;
127 if (front().value() || calibration().value() || back().value()) {
128 last_on_encoder_ = encoder_;
129 } else {
130 last_off_encoder_ = encoder_;
131 }
132 encoder_ = claw.position;
133 any_triggered_last_ = any_sensor_triggered;
134}
135
136void ZeroedStateFeedbackLoop::Reset(const HalfClawPosition &claw) {
137 set_zeroing_state(ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
138
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000139 front_.Reset(claw.front);
140 calibration_.Reset(claw.calibration);
141 back_.Reset(claw.back);
Austin Schuh27b8fb12014-02-22 15:10:05 -0800142 // close up the min and max edge positions as they are no longer valid and
143 // will be expanded in future iterations
144 min_hall_effect_on_angle_ = claw.position;
145 max_hall_effect_on_angle_ = claw.position;
146 min_hall_effect_off_angle_ = claw.position;
147 max_hall_effect_off_angle_ = claw.position;
148 any_triggered_last_ = any_triggered();
149}
150
151bool TopZeroedStateFeedbackLoop::SetCalibrationOnEdge(
152 const constants::Values::Claws::Claw &claw_values,
153 JointZeroingState zeroing_state) {
154 double edge_encoder;
155 double edge_angle;
156 if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) {
157 LOG(INFO, "Calibration edge edge should be %f.\n", edge_angle);
158 SetCalibration(edge_encoder, edge_angle);
159 set_zeroing_state(zeroing_state);
160 return true;
161 }
162 return false;
163}
164
165bool BottomZeroedStateFeedbackLoop::SetCalibrationOnEdge(
166 const constants::Values::Claws::Claw &claw_values,
167 JointZeroingState zeroing_state) {
168 double edge_encoder;
169 double edge_angle;
170 if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) {
171 LOG(INFO, "Calibration edge.\n");
172 SetCalibration(edge_encoder, edge_angle);
173 set_zeroing_state(zeroing_state);
174 return true;
175 }
176 return false;
177}
178
Austin Schuhcc0bf312014-02-09 00:39:29 -0800179ClawMotor::ClawMotor(control_loops::ClawGroup *my_claw)
180 : aos::control_loops::ControlLoop<control_loops::ClawGroup>(my_claw),
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800181 has_top_claw_goal_(false),
182 top_claw_goal_(0.0),
Austin Schuhcda86af2014-02-16 16:16:39 -0800183 top_claw_(this),
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800184 has_bottom_claw_goal_(false),
185 bottom_claw_goal_(0.0),
Austin Schuhcda86af2014-02-16 16:16:39 -0800186 bottom_claw_(this),
187 claw_(MakeClawLoop()),
Ben Fredrickson9b388422014-02-13 06:15:31 +0000188 was_enabled_(false),
Austin Schuh4cb047f2014-02-16 21:10:19 -0800189 doing_calibration_fine_tune_(false),
Austin Schuhe7f90d12014-02-17 00:48:25 -0800190 capped_goal_(false),
191 mode_(UNKNOWN_LOCATION) {}
Austin Schuh3bb9a442014-02-02 16:01:45 -0800192
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800193const int ZeroedStateFeedbackLoop::kZeroingMaxVoltage;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800194
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000195bool ZeroedStateFeedbackLoop::SawFilteredPosedge(
196 const HallEffectTracker &this_sensor, const HallEffectTracker &sensorA,
197 const HallEffectTracker &sensorB) {
198 if (posedge_filter_ == nullptr && this_sensor.posedge_count_changed() &&
199 !sensorA.posedge_count_changed() && !sensorB.posedge_count_changed() &&
200 this_sensor.value() && !this_sensor.last_value()) {
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000201 posedge_filter_ = &this_sensor;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000202 } else if (posedge_filter_ == &this_sensor &&
203 !this_sensor.posedge_count_changed() &&
204 !sensorA.posedge_count_changed() &&
205 !sensorB.posedge_count_changed() && this_sensor.value()) {
206 posedge_filter_ = nullptr;
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000207 return true;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000208 } else if (posedge_filter_ == &this_sensor) {
209 posedge_filter_ = nullptr;
210 }
211 return false;
212}
213
214bool ZeroedStateFeedbackLoop::SawFilteredNegedge(
215 const HallEffectTracker &this_sensor, const HallEffectTracker &sensorA,
216 const HallEffectTracker &sensorB) {
217 if (negedge_filter_ == nullptr && this_sensor.negedge_count_changed() &&
218 !sensorA.negedge_count_changed() && !sensorB.negedge_count_changed() &&
219 !this_sensor.value() && this_sensor.last_value()) {
220 negedge_filter_ = &this_sensor;
221 } else if (negedge_filter_ == &this_sensor &&
222 !this_sensor.negedge_count_changed() &&
223 !sensorA.negedge_count_changed() &&
224 !sensorB.negedge_count_changed() && !this_sensor.value()) {
225 negedge_filter_ = nullptr;
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000226 return true;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000227 } else if (negedge_filter_ == &this_sensor) {
228 negedge_filter_ = nullptr;
229 }
230 return false;
231}
232
Brian Silvermane0a95462014-02-17 00:41:09 -0800233bool ZeroedStateFeedbackLoop::DoGetPositionOfEdge(
234 const constants::Values::Claws::AnglePair &angles, double *edge_encoder,
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000235 double *edge_angle, const HallEffectTracker &this_sensor,
236 const HallEffectTracker &sensorA, const HallEffectTracker &sensorB,
Brian Silvermane0a95462014-02-17 00:41:09 -0800237 const char *hall_effect_name) {
Austin Schuhf84a1302014-02-19 00:23:30 -0800238 bool found_edge = false;
Austin Schuh27b8fb12014-02-22 15:10:05 -0800239
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000240 if (SawFilteredPosedge(this_sensor, sensorA, sensorB)) {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800241 if (min_hall_effect_off_angle_ == max_hall_effect_off_angle_) {
Brian Silvermanf48fab32014-03-09 14:32:24 -0700242 LOG(WARNING, "%s: Uncertain which side, rejecting posedge\n", name_);
Brian Silvermane0a95462014-02-17 00:41:09 -0800243 } else {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800244 const double average_last_encoder =
245 (min_hall_effect_off_angle_ + max_hall_effect_off_angle_) / 2.0;
246 if (posedge_value_ < average_last_encoder) {
247 *edge_angle = angles.upper_decreasing_angle;
248 LOG(INFO, "%s Posedge upper of %s -> %f posedge: %f avg_encoder: %f\n",
249 name_, hall_effect_name, *edge_angle, posedge_value_,
250 average_last_encoder);
251 } else {
252 *edge_angle = angles.lower_angle;
253 LOG(INFO, "%s Posedge lower of %s -> %f posedge: %f avg_encoder: %f\n",
254 name_, hall_effect_name, *edge_angle, posedge_value_,
255 average_last_encoder);
256 }
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000257 *edge_encoder = posedge_value_;
Austin Schuh27b8fb12014-02-22 15:10:05 -0800258 found_edge = true;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000259 }
260 }
261
262 if (SawFilteredNegedge(this_sensor, sensorA, sensorB)) {
263 if (min_hall_effect_on_angle_ == max_hall_effect_on_angle_) {
Brian Silvermanf48fab32014-03-09 14:32:24 -0700264 LOG(WARNING, "%s: Uncertain which side, rejecting negedge\n", name_);
Brian Silvermane0a95462014-02-17 00:41:09 -0800265 } else {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800266 const double average_last_encoder =
267 (min_hall_effect_on_angle_ + max_hall_effect_on_angle_) / 2.0;
268 if (negedge_value_ > average_last_encoder) {
269 *edge_angle = angles.upper_angle;
270 LOG(INFO, "%s Negedge upper of %s -> %f negedge: %f avg_encoder: %f\n",
271 name_, hall_effect_name, *edge_angle, negedge_value_,
272 average_last_encoder);
273 } else {
274 *edge_angle = angles.lower_decreasing_angle;
275 LOG(INFO, "%s Negedge lower of %s -> %f negedge: %f avg_encoder: %f\n",
276 name_, hall_effect_name, *edge_angle, negedge_value_,
277 average_last_encoder);
278 }
279 *edge_encoder = negedge_value_;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000280 found_edge = true;
Brian Silvermane0a95462014-02-17 00:41:09 -0800281 }
Austin Schuh27b8fb12014-02-22 15:10:05 -0800282 }
283
Austin Schuhf84a1302014-02-19 00:23:30 -0800284 return found_edge;
Brian Silvermane0a95462014-02-17 00:41:09 -0800285}
286
Austin Schuhf9286cd2014-02-11 00:51:09 -0800287bool ZeroedStateFeedbackLoop::GetPositionOfEdge(
Austin Schuhd27931c2014-02-16 19:18:20 -0800288 const constants::Values::Claws::Claw &claw_values, double *edge_encoder,
Austin Schuhf9286cd2014-02-11 00:51:09 -0800289 double *edge_angle) {
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000290 if (DoGetPositionOfEdge(claw_values.front, edge_encoder, edge_angle, front_,
291 calibration_, back_, "front")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800292 return true;
293 }
Brian Silvermane0a95462014-02-17 00:41:09 -0800294 if (DoGetPositionOfEdge(claw_values.calibration, edge_encoder, edge_angle,
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000295 calibration_, front_, back_, "calibration")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800296 return true;
297 }
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000298 if (DoGetPositionOfEdge(claw_values.back, edge_encoder, edge_angle, back_,
299 calibration_, front_, "back")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800300 return true;
301 }
302 return false;
303}
304
Austin Schuhcda86af2014-02-16 16:16:39 -0800305void TopZeroedStateFeedbackLoop::SetCalibration(double edge_encoder,
306 double edge_angle) {
307 double old_offset = offset_;
308 offset_ = edge_angle - edge_encoder;
309 const double doffset = offset_ - old_offset;
310 motor_->ChangeTopOffset(doffset);
311}
312
313void BottomZeroedStateFeedbackLoop::SetCalibration(double edge_encoder,
314 double edge_angle) {
315 double old_offset = offset_;
316 offset_ = edge_angle - edge_encoder;
317 const double doffset = offset_ - old_offset;
318 motor_->ChangeBottomOffset(doffset);
319}
320
321void ClawMotor::ChangeTopOffset(double doffset) {
322 claw_.ChangeTopOffset(doffset);
323 if (has_top_claw_goal_) {
324 top_claw_goal_ += doffset;
325 }
326}
327
328void ClawMotor::ChangeBottomOffset(double doffset) {
329 claw_.ChangeBottomOffset(doffset);
330 if (has_bottom_claw_goal_) {
331 bottom_claw_goal_ += doffset;
332 }
333}
334
335void ClawLimitedLoop::ChangeTopOffset(double doffset) {
336 Y_(1, 0) += doffset;
337 X_hat(1, 0) += doffset;
338 LOG(INFO, "Changing top offset by %f\n", doffset);
339}
340void ClawLimitedLoop::ChangeBottomOffset(double doffset) {
341 Y_(0, 0) += doffset;
342 X_hat(0, 0) += doffset;
343 X_hat(1, 0) -= doffset;
344 LOG(INFO, "Changing bottom offset by %f\n", doffset);
345}
joe7376ff52014-02-16 18:28:42 -0800346
Austin Schuh069143b2014-02-17 02:46:26 -0800347void LimitClawGoal(double *bottom_goal, double *top_goal,
348 const frc971::constants::Values &values) {
349 // first update position based on angle limit
350
351 const double separation = *top_goal - *bottom_goal;
352 if (separation > values.claw.claw_max_separation) {
Austin Schuh069143b2014-02-17 02:46:26 -0800353 const double dsep = (separation - values.claw.claw_max_separation) / 2.0;
354 *bottom_goal += dsep;
355 *top_goal -= dsep;
356 LOG(DEBUG, "Goals now bottom: %f, top: %f\n", *bottom_goal, *top_goal);
357 }
358 if (separation < values.claw.claw_min_separation) {
Austin Schuh069143b2014-02-17 02:46:26 -0800359 const double dsep = (separation - values.claw.claw_min_separation) / 2.0;
360 *bottom_goal += dsep;
361 *top_goal -= dsep;
362 LOG(DEBUG, "Goals now bottom: %f, top: %f\n", *bottom_goal, *top_goal);
363 }
364
365 // now move both goals in unison
366 if (*bottom_goal < values.claw.lower_claw.lower_limit) {
367 *top_goal += values.claw.lower_claw.lower_limit - *bottom_goal;
368 *bottom_goal = values.claw.lower_claw.lower_limit;
369 }
370 if (*bottom_goal > values.claw.lower_claw.upper_limit) {
371 *top_goal -= *bottom_goal - values.claw.lower_claw.upper_limit;
372 *bottom_goal = values.claw.lower_claw.upper_limit;
373 }
374
375 if (*top_goal < values.claw.upper_claw.lower_limit) {
376 *bottom_goal += values.claw.upper_claw.lower_limit - *top_goal;
377 *top_goal = values.claw.upper_claw.lower_limit;
378 }
379 if (*top_goal > values.claw.upper_claw.upper_limit) {
380 *bottom_goal -= *top_goal - values.claw.upper_claw.upper_limit;
381 *top_goal = values.claw.upper_claw.upper_limit;
382 }
383}
Austin Schuhcda86af2014-02-16 16:16:39 -0800384
Austin Schuhe7f90d12014-02-17 00:48:25 -0800385bool ClawMotor::is_ready() const {
386 return (
387 (top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED &&
388 bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) ||
389 (::aos::robot_state->autonomous &&
390 ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
391 top_claw_.zeroing_state() ==
392 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
393 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
394 bottom_claw_.zeroing_state() ==
395 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION))));
396}
397
398bool ClawMotor::is_zeroing() const { return !is_ready(); }
399
Austin Schuh3bb9a442014-02-02 16:01:45 -0800400// Positive angle is up, and positive power is up.
Austin Schuhcc0bf312014-02-09 00:39:29 -0800401void ClawMotor::RunIteration(const control_loops::ClawGroup::Goal *goal,
402 const control_loops::ClawGroup::Position *position,
403 control_loops::ClawGroup::Output *output,
James Kuszmaul9ead1de2014-02-28 21:24:39 -0800404 control_loops::ClawGroup::Status *status) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800405 constexpr double dt = 0.01;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800406
407 // Disable the motors now so that all early returns will return with the
408 // motors disabled.
409 if (output) {
410 output->top_claw_voltage = 0;
411 output->bottom_claw_voltage = 0;
412 output->intake_voltage = 0;
Ben Fredrickson61893d52014-03-02 09:43:23 +0000413 output->tusk_voltage = 0;
414 }
415
416 if (::std::isnan(goal->bottom_angle) ||
417 ::std::isnan(goal->separation_angle) || ::std::isnan(goal->intake) ||
418 ::std::isnan(goal->centering)) {
419 return;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800420 }
421
Austin Schuh1a499942014-02-17 01:51:58 -0800422 if (reset()) {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800423 top_claw_.Reset(position->top);
424 bottom_claw_.Reset(position->bottom);
Austin Schuh1a499942014-02-17 01:51:58 -0800425 }
426
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800427 if (::aos::robot_state.get() == nullptr) {
428 return;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800429 }
430
Austin Schuhf9286cd2014-02-11 00:51:09 -0800431 const frc971::constants::Values &values = constants::GetValues();
432
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800433 if (position) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800434 Eigen::Matrix<double, 2, 1> Y;
435 Y << position->bottom.position + bottom_claw_.offset(),
436 position->top.position + top_claw_.offset();
437 claw_.Correct(Y);
438
Austin Schuhf9286cd2014-02-11 00:51:09 -0800439 top_claw_.SetPositionValues(position->top);
440 bottom_claw_.SetPositionValues(position->bottom);
441
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800442 if (!has_top_claw_goal_) {
443 has_top_claw_goal_ = true;
Austin Schuhcda86af2014-02-16 16:16:39 -0800444 top_claw_goal_ = top_claw_.absolute_position();
Austin Schuhe7f90d12014-02-17 00:48:25 -0800445 initial_separation_ =
Austin Schuhcda86af2014-02-16 16:16:39 -0800446 top_claw_.absolute_position() - bottom_claw_.absolute_position();
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800447 }
448 if (!has_bottom_claw_goal_) {
449 has_bottom_claw_goal_ = true;
Austin Schuhcda86af2014-02-16 16:16:39 -0800450 bottom_claw_goal_ = bottom_claw_.absolute_position();
Austin Schuhe7f90d12014-02-17 00:48:25 -0800451 initial_separation_ =
Austin Schuhcda86af2014-02-16 16:16:39 -0800452 top_claw_.absolute_position() - bottom_claw_.absolute_position();
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800453 }
Brian Silvermanf48fab32014-03-09 14:32:24 -0700454 LOG_STRUCT(DEBUG, "absolute position",
455 ClawPositionToLog(top_claw_.absolute_position(),
456 bottom_claw_.absolute_position()));
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800457 }
458
Austin Schuh069143b2014-02-17 02:46:26 -0800459 const bool autonomous = ::aos::robot_state->autonomous;
460 const bool enabled = ::aos::robot_state->enabled;
461
462 double bottom_claw_velocity_ = 0.0;
463 double top_claw_velocity_ = 0.0;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800464
465 if ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED &&
466 bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) ||
467 (autonomous &&
468 ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
469 top_claw_.zeroing_state() ==
470 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
471 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
472 bottom_claw_.zeroing_state() ==
473 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION)))) {
474 // Ready to use the claw.
475 // Limit the goals here.
Austin Schuhf9286cd2014-02-11 00:51:09 -0800476 bottom_claw_goal_ = goal->bottom_angle;
Brian Silverman7c021c42014-02-17 15:15:56 -0800477 top_claw_goal_ = goal->bottom_angle + goal->separation_angle;
Austin Schuhcda86af2014-02-16 16:16:39 -0800478 has_bottom_claw_goal_ = true;
479 has_top_claw_goal_ = true;
480 doing_calibration_fine_tune_ = false;
481
Austin Schuhe7f90d12014-02-17 00:48:25 -0800482 mode_ = READY;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800483 } else if (top_claw_.zeroing_state() !=
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000484 ZeroedStateFeedbackLoop::UNKNOWN_POSITION &&
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800485 bottom_claw_.zeroing_state() !=
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000486 ZeroedStateFeedbackLoop::UNKNOWN_POSITION) {
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800487 // Time to fine tune the zero.
488 // Limit the goals here.
Austin Schuh0c733422014-02-17 01:17:12 -0800489 if (!enabled) {
490 // If we are disabled, start the fine tune process over again.
491 doing_calibration_fine_tune_ = false;
492 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800493 if (bottom_claw_.zeroing_state() != ZeroedStateFeedbackLoop::CALIBRATED) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800494 // always get the bottom claw to calibrated first
495 LOG(DEBUG, "Calibrating the bottom of the claw\n");
496 if (!doing_calibration_fine_tune_) {
497 if (::std::abs(bottom_absolute_position() -
Austin Schuhd27931c2014-02-16 19:18:20 -0800498 values.claw.start_fine_tune_pos) <
499 values.claw.claw_unimportant_epsilon) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800500 doing_calibration_fine_tune_ = true;
Austin Schuhd27931c2014-02-16 19:18:20 -0800501 bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800502 top_claw_velocity_ = bottom_claw_velocity_ =
503 values.claw.claw_zeroing_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800504 LOG(DEBUG, "Ready to fine tune the bottom\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800505 mode_ = FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800506 } else {
507 // send bottom to zeroing start
Austin Schuhd27931c2014-02-16 19:18:20 -0800508 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuhcda86af2014-02-16 16:16:39 -0800509 LOG(DEBUG, "Going to the start position for the bottom\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800510 mode_ = PREP_FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800511 }
512 } else {
Austin Schuhe7f90d12014-02-17 00:48:25 -0800513 mode_ = FINE_TUNE_BOTTOM;
Austin Schuhd27931c2014-02-16 19:18:20 -0800514 bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800515 top_claw_velocity_ = bottom_claw_velocity_ =
516 values.claw.claw_zeroing_speed;
Brian Silvermane0a95462014-02-17 00:41:09 -0800517 if (top_claw_.front_or_back_triggered() ||
518 bottom_claw_.front_or_back_triggered()) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800519 // We shouldn't hit a limit, but if we do, go back to the zeroing
520 // point and try again.
521 doing_calibration_fine_tune_ = false;
Austin Schuhd27931c2014-02-16 19:18:20 -0800522 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuh069143b2014-02-17 02:46:26 -0800523 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
Austin Schuhcda86af2014-02-16 16:16:39 -0800524 LOG(DEBUG, "Found a limit, starting over.\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800525 mode_ = PREP_FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800526 }
Austin Schuh288c8c32014-02-16 17:20:17 -0800527
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000528 if (position && bottom_claw_.SawFilteredPosedge(
529 bottom_claw_.calibration(), bottom_claw_.front(),
530 bottom_claw_.back())) {
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000531 // do calibration
532 bottom_claw_.SetCalibration(
533 position->bottom.posedge_value,
534 values.claw.lower_claw.calibration.lower_angle);
535 bottom_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED);
536 // calibrated so we are done fine tuning bottom
537 doing_calibration_fine_tune_ = false;
538 LOG(DEBUG, "Calibrated the bottom correctly!\n");
539 } else if (bottom_claw_.calibration().last_value()) {
540 doing_calibration_fine_tune_ = false;
541 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
542 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
543 mode_ = PREP_FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800544 } else {
545 LOG(DEBUG, "Fine tuning\n");
546 }
547 }
548 // now set the top claw to track
549
Austin Schuhd27931c2014-02-16 19:18:20 -0800550 top_claw_goal_ = bottom_claw_goal_ + values.claw.claw_zeroing_separation;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800551 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800552 // bottom claw must be calibrated, start on the top
553 if (!doing_calibration_fine_tune_) {
Austin Schuhd27931c2014-02-16 19:18:20 -0800554 if (::std::abs(top_absolute_position() -
555 values.claw.start_fine_tune_pos) <
556 values.claw.claw_unimportant_epsilon) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800557 doing_calibration_fine_tune_ = true;
Austin Schuhd27931c2014-02-16 19:18:20 -0800558 top_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800559 top_claw_velocity_ = bottom_claw_velocity_ =
560 values.claw.claw_zeroing_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800561 LOG(DEBUG, "Ready to fine tune the top\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800562 mode_ = FINE_TUNE_TOP;
Austin Schuhcda86af2014-02-16 16:16:39 -0800563 } else {
564 // send top to zeroing start
Austin Schuhd27931c2014-02-16 19:18:20 -0800565 top_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuhcda86af2014-02-16 16:16:39 -0800566 LOG(DEBUG, "Going to the start position for the top\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800567 mode_ = PREP_FINE_TUNE_TOP;
Austin Schuhcda86af2014-02-16 16:16:39 -0800568 }
569 } else {
Austin Schuhe7f90d12014-02-17 00:48:25 -0800570 mode_ = FINE_TUNE_TOP;
Austin Schuhd27931c2014-02-16 19:18:20 -0800571 top_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800572 top_claw_velocity_ = bottom_claw_velocity_ =
573 values.claw.claw_zeroing_speed;
Brian Silvermane0a95462014-02-17 00:41:09 -0800574 if (top_claw_.front_or_back_triggered() ||
575 bottom_claw_.front_or_back_triggered()) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800576 // this should not happen, but now we know it won't
577 doing_calibration_fine_tune_ = false;
Austin Schuhd27931c2014-02-16 19:18:20 -0800578 top_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuh069143b2014-02-17 02:46:26 -0800579 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
Austin Schuhcda86af2014-02-16 16:16:39 -0800580 LOG(DEBUG, "Found a limit, starting over.\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800581 mode_ = PREP_FINE_TUNE_TOP;
Austin Schuhcda86af2014-02-16 16:16:39 -0800582 }
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000583
584 if (position &&
585 top_claw_.SawFilteredPosedge(top_claw_.calibration(),
586 top_claw_.front(), top_claw_.back())) {
587 // do calibration
588 top_claw_.SetCalibration(
589 position->top.posedge_value,
590 values.claw.upper_claw.calibration.lower_angle);
591 top_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED);
592 // calibrated so we are done fine tuning top
593 doing_calibration_fine_tune_ = false;
594 LOG(DEBUG, "Calibrated the top correctly!\n");
595 } else if (top_claw_.calibration().last_value()) {
596 doing_calibration_fine_tune_ = false;
597 top_claw_goal_ = values.claw.start_fine_tune_pos;
598 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
599 mode_ = PREP_FINE_TUNE_TOP;
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000600 }
Austin Schuhcda86af2014-02-16 16:16:39 -0800601 }
602 // now set the bottom claw to track
Austin Schuhd27931c2014-02-16 19:18:20 -0800603 bottom_claw_goal_ = top_claw_goal_ - values.claw.claw_zeroing_separation;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800604 }
605 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800606 doing_calibration_fine_tune_ = false;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800607 if (!was_enabled_ && enabled) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800608 if (position) {
609 top_claw_goal_ = position->top.position;
610 bottom_claw_goal_ = position->bottom.position;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800611 initial_separation_ =
Austin Schuhcda86af2014-02-16 16:16:39 -0800612 position->top.position - position->bottom.position;
Austin Schuhf9286cd2014-02-11 00:51:09 -0800613 } else {
614 has_top_claw_goal_ = false;
615 has_bottom_claw_goal_ = false;
616 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800617 }
Austin Schuhf9286cd2014-02-11 00:51:09 -0800618
Austin Schuh4cb047f2014-02-16 21:10:19 -0800619 if ((bottom_claw_.zeroing_state() !=
620 ZeroedStateFeedbackLoop::UNKNOWN_POSITION ||
Brian Silvermane0a95462014-02-17 00:41:09 -0800621 bottom_claw_.front().value() || top_claw_.front().value()) &&
622 !top_claw_.back().value() && !bottom_claw_.back().value()) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800623 if (enabled) {
624 // Time to slowly move back up to find any position to narrow down the
625 // zero.
Austin Schuhd27931c2014-02-16 19:18:20 -0800626 top_claw_goal_ += values.claw.claw_zeroing_off_speed * dt;
627 bottom_claw_goal_ += values.claw.claw_zeroing_off_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800628 top_claw_velocity_ = bottom_claw_velocity_ =
629 values.claw.claw_zeroing_off_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800630 LOG(DEBUG, "Bottom is known.\n");
Austin Schuhf9286cd2014-02-11 00:51:09 -0800631 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800632 } else {
633 // We don't know where either claw is. Slowly start moving down to find
634 // any hall effect.
Austin Schuhf9286cd2014-02-11 00:51:09 -0800635 if (enabled) {
Austin Schuhd27931c2014-02-16 19:18:20 -0800636 top_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt;
637 bottom_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800638 top_claw_velocity_ = bottom_claw_velocity_ =
639 -values.claw.claw_zeroing_off_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800640 LOG(DEBUG, "Both are unknown.\n");
Austin Schuhf9286cd2014-02-11 00:51:09 -0800641 }
642 }
643
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000644 if (position) {
645 if (enabled) {
646 top_claw_.SetCalibrationOnEdge(
647 values.claw.upper_claw,
648 ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION);
649 bottom_claw_.SetCalibrationOnEdge(
650 values.claw.lower_claw,
651 ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION);
652 } else {
653 // TODO(austin): Only calibrate on the predetermined edge.
654 // We might be able to just ignore this since the backlash is soooo
655 // low.
656 // :)
657 top_claw_.SetCalibrationOnEdge(
658 values.claw.upper_claw,
659 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
660 bottom_claw_.SetCalibrationOnEdge(
661 values.claw.lower_claw,
662 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
663 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800664 }
Austin Schuhe7f90d12014-02-17 00:48:25 -0800665 mode_ = UNKNOWN_LOCATION;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800666 }
667
Austin Schuh069143b2014-02-17 02:46:26 -0800668 // Limit the goals if both claws have been (mostly) found.
669 if (mode_ != UNKNOWN_LOCATION) {
670 LimitClawGoal(&bottom_claw_goal_, &top_claw_goal_, values);
671 }
672
Austin Schuhf9286cd2014-02-11 00:51:09 -0800673 if (has_top_claw_goal_ && has_bottom_claw_goal_) {
Austin Schuh069143b2014-02-17 02:46:26 -0800674 claw_.R << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_,
675 bottom_claw_velocity_, top_claw_velocity_ - bottom_claw_velocity_;
Austin Schuhcda86af2014-02-16 16:16:39 -0800676 double separation = -971;
677 if (position != nullptr) {
678 separation = position->top.position - position->bottom.position;
679 }
Brian Silvermanf48fab32014-03-09 14:32:24 -0700680 LOG_STRUCT(DEBUG, "actual goal",
681 ClawGoalToLog(claw_.R(0, 0), claw_.R(1, 0), separation));
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800682
Austin Schuh01c652b2014-02-21 23:13:42 -0800683 // Only cap power when one of the halves of the claw is moving slowly and
684 // could wind up.
Austin Schuhe7f90d12014-02-17 00:48:25 -0800685 claw_.set_is_zeroing(mode_ == UNKNOWN_LOCATION || mode_ == FINE_TUNE_TOP ||
686 mode_ == FINE_TUNE_BOTTOM);
Austin Schuhcda86af2014-02-16 16:16:39 -0800687 claw_.Update(output == nullptr);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800688 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800689 claw_.Update(true);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800690 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800691
Austin Schuh4cb047f2014-02-16 21:10:19 -0800692 capped_goal_ = false;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800693 switch (mode_) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800694 case READY:
Austin Schuhe7f90d12014-02-17 00:48:25 -0800695 case PREP_FINE_TUNE_TOP:
696 case PREP_FINE_TUNE_BOTTOM:
Austin Schuhcda86af2014-02-16 16:16:39 -0800697 break;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800698 case FINE_TUNE_BOTTOM:
699 case FINE_TUNE_TOP:
Austin Schuh4cb047f2014-02-16 21:10:19 -0800700 case UNKNOWN_LOCATION: {
701 if (claw_.uncapped_average_voltage() > values.claw.max_zeroing_voltage) {
702 double dx = (claw_.uncapped_average_voltage() -
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000703 values.claw.max_zeroing_voltage) / claw_.K(0, 0);
Austin Schuh4cb047f2014-02-16 21:10:19 -0800704 bottom_claw_goal_ -= dx;
705 top_claw_goal_ -= dx;
Austin Schuhcda86af2014-02-16 16:16:39 -0800706 capped_goal_ = true;
Brian Silvermanf48fab32014-03-09 14:32:24 -0700707 LOG(DEBUG, "Moving the goal by %f to prevent windup."
708 " Uncapped is %f, max is %f, difference is %f\n",
709 dx,
Austin Schuhe7f90d12014-02-17 00:48:25 -0800710 claw_.uncapped_average_voltage(), values.claw.max_zeroing_voltage,
711 (claw_.uncapped_average_voltage() -
712 values.claw.max_zeroing_voltage));
Austin Schuh4cb047f2014-02-16 21:10:19 -0800713 } else if (claw_.uncapped_average_voltage() <
714 -values.claw.max_zeroing_voltage) {
715 double dx = (claw_.uncapped_average_voltage() +
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000716 values.claw.max_zeroing_voltage) / claw_.K(0, 0);
Austin Schuh4cb047f2014-02-16 21:10:19 -0800717 bottom_claw_goal_ -= dx;
718 top_claw_goal_ -= dx;
Austin Schuhcda86af2014-02-16 16:16:39 -0800719 capped_goal_ = true;
Austin Schuh4cb047f2014-02-16 21:10:19 -0800720 LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx);
Austin Schuhcda86af2014-02-16 16:16:39 -0800721 }
Austin Schuh4cb047f2014-02-16 21:10:19 -0800722 } break;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800723 }
724
725 if (output) {
Ben Fredrickson2f76ddf2014-02-23 05:58:23 +0000726 if (goal) {
727 //setup the intake
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000728 output->intake_voltage =
729 (goal->intake > 12.0) ? 12 : (goal->intake < -12.0) ? -12.0
730 : goal->intake;
Ben Fredrickson2f76ddf2014-02-23 05:58:23 +0000731 output->tusk_voltage = goal->centering;
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000732 output->tusk_voltage =
733 (goal->centering > 12.0) ? 12 : (goal->centering < -12.0)
734 ? -12.0
735 : goal->centering;
Ben Fredrickson2f76ddf2014-02-23 05:58:23 +0000736 }
Austin Schuhcda86af2014-02-16 16:16:39 -0800737 output->top_claw_voltage = claw_.U(1, 0) + claw_.U(0, 0);
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000738 output->bottom_claw_voltage = claw_.U(0, 0);
Austin Schuhf84a1302014-02-19 00:23:30 -0800739
740 if (output->top_claw_voltage > kMaxVoltage) {
741 output->top_claw_voltage = kMaxVoltage;
742 } else if (output->top_claw_voltage < -kMaxVoltage) {
743 output->top_claw_voltage = -kMaxVoltage;
744 }
745
746 if (output->bottom_claw_voltage > kMaxVoltage) {
747 output->bottom_claw_voltage = kMaxVoltage;
748 } else if (output->bottom_claw_voltage < -kMaxVoltage) {
749 output->bottom_claw_voltage = -kMaxVoltage;
750 }
Austin Schuh3bb9a442014-02-02 16:01:45 -0800751 }
James Kuszmaul4abaf482014-02-26 21:16:35 -0800752
James Kuszmaul4abaf482014-02-26 21:16:35 -0800753 status->bottom = bottom_absolute_position();
754 status->separation = top_absolute_position() - bottom_absolute_position();
755 status->bottom_velocity = claw_.X_hat(2, 0);
756 status->separation_velocity = claw_.X_hat(3, 0);
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800757
Austin Schuha556b012014-03-02 11:55:52 -0800758 bool bottom_done =
759 ::std::abs(bottom_absolute_position() - goal->bottom_angle) < 0.020;
760 bool bottom_velocity_done = ::std::abs(status->bottom_velocity) < 0.2;
761 bool separation_done =
762 ::std::abs((top_absolute_position() - bottom_absolute_position()) -
763 goal->separation_angle) < 0.020;
764 bool separation_done_with_ball =
765 ::std::abs((top_absolute_position() - bottom_absolute_position()) -
766 goal->separation_angle) < 0.06;
767 status->done = is_ready() && separation_done && bottom_done && bottom_velocity_done;
768 status->done_with_ball =
769 is_ready() && separation_done_with_ball && bottom_done && bottom_velocity_done;
770
Austin Schuh4f8633f2014-03-02 13:59:46 -0800771 status->zeroed = is_ready();
Brian Silverman80fc94c2014-03-09 16:56:01 -0700772 status->zeroed_for_auto =
773 (top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
774 top_claw_.zeroing_state() ==
775 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
776 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
777 bottom_claw_.zeroing_state() ==
778 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
Austin Schuh4f8633f2014-03-02 13:59:46 -0800779
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800780 was_enabled_ = ::aos::robot_state->enabled;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800781}
782
783} // namespace control_loops
784} // namespace frc971
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000785