blob: 82ca3a54b2a79900c0b24a99435707b1fe0e5677 [file] [log] [blame]
Brian Silverman17f503e2015-08-02 18:17:18 -07001#include "y2014/control_loops/claw/claw.h"
2
3#include <algorithm>
4
John Park33858a32018-09-28 23:05:48 -07005#include "aos/controls/control_loops.q.h"
6#include "aos/logging/logging.h"
7#include "aos/logging/queue_logging.h"
8#include "aos/logging/matrix_logging.h"
9#include "aos/commonmath.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070010
11#include "y2014/constants.h"
12#include "y2014/control_loops/claw/claw_motor_plant.h"
13
14// 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.
40// Difference between the arms has a range, and the values of each arm has a
41// range.
42// 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
Austin Schuh24957102015-11-28 16:04:40 -080047namespace y2014 {
Brian Silverman17f503e2015-08-02 18:17:18 -070048namespace control_loops {
49
Austin Schuh24957102015-11-28 16:04:40 -080050using ::frc971::HallEffectTracker;
Austin Schuh0e997732015-11-08 15:14:53 -080051using ::y2014::control_loops::claw::kDt;
Austin Schuh24957102015-11-28 16:04:40 -080052using ::frc971::control_loops::DoCoerceGoal;
Brian Silvermanb601d892015-12-20 18:24:38 -050053using ::y2014::control_loops::ClawPositionToLog;
Austin Schuh0e997732015-11-08 15:14:53 -080054
Brian Silverman17f503e2015-08-02 18:17:18 -070055static const double kZeroingVoltage = 4.0;
56static const double kMaxVoltage = 12.0;
57const double kRezeroThreshold = 0.07;
58
59ClawLimitedLoop::ClawLimitedLoop(StateFeedbackLoop<4, 2, 2> &&loop)
60 : StateFeedbackLoop<4, 2, 2>(::std::move(loop)),
61 uncapped_average_voltage_(0.0),
62 is_zeroing_(true),
63 U_Poly_((Eigen::Matrix<double, 4, 2>() << 1, 0,
64 -1, 0,
65 0, 1,
66 0, -1).finished(),
67 (Eigen::Matrix<double, 4, 1>() << kMaxVoltage, kMaxVoltage,
68 kMaxVoltage, kMaxVoltage).finished()),
69 U_Poly_zeroing_((Eigen::Matrix<double, 4, 2>() << 1, 0,
70 -1, 0,
71 0, 1,
72 0, -1).finished(),
73 (Eigen::Matrix<double, 4, 1>() <<
74 kZeroingVoltage, kZeroingVoltage,
75 kZeroingVoltage, kZeroingVoltage).finished()) {
76 ::aos::controls::HPolytope<0>::Init();
77}
78
79// Caps the voltage prioritizing reducing velocity error over reducing
80// positional error.
81// Uses the polytope libararies which we used to just use for the drivetrain.
82// Uses a region representing the maximum voltage and then transforms it such
83// that the points represent different amounts of positional error and
84// constrains the region such that, if at all possible, it will maintain its
85// current efforts to reduce velocity error.
86void ClawLimitedLoop::CapU() {
87 const Eigen::Matrix<double, 4, 1> error = R() - X_hat();
88
89 double u_top = U(1, 0);
90 double u_bottom = U(0, 0);
91
92 uncapped_average_voltage_ = (u_top + u_bottom) / 2;
93
94 double max_voltage = is_zeroing_ ? kZeroingVoltage : kMaxVoltage;
95
96 if (::std::abs(u_bottom) > max_voltage || ::std::abs(u_top) > max_voltage) {
97 LOG_MATRIX(DEBUG, "U at start", U());
98 // H * U <= k
99 // U = UPos + UVel
100 // H * (UPos + UVel) <= k
101 // H * UPos <= k - H * UVel
102
103 // Now, we can do a coordinate transformation and say the following.
104
105 // UPos = position_K * position_error
106 // (H * position_K) * position_error <= k - H * UVel
107
108 Eigen::Matrix<double, 2, 2> position_K;
Austin Schuh32501832017-02-25 18:32:56 -0800109 position_K << controller().K(0, 0), controller().K(0, 1),
110 controller().K(1, 0), controller().K(1, 1);
Brian Silverman17f503e2015-08-02 18:17:18 -0700111 Eigen::Matrix<double, 2, 2> velocity_K;
Austin Schuh32501832017-02-25 18:32:56 -0800112 velocity_K << controller().K(0, 2), controller().K(0, 3),
113 controller().K(1, 2), controller().K(1, 3);
Brian Silverman17f503e2015-08-02 18:17:18 -0700114
115 Eigen::Matrix<double, 2, 1> position_error;
116 position_error << error(0, 0), error(1, 0);
117 Eigen::Matrix<double, 2, 1> velocity_error;
118 velocity_error << error(2, 0), error(3, 0);
119 LOG_MATRIX(DEBUG, "error", error);
120
121 const auto &poly = is_zeroing_ ? U_Poly_zeroing_ : U_Poly_;
122 const Eigen::Matrix<double, 4, 2> pos_poly_H = poly.H() * position_K;
123 const Eigen::Matrix<double, 4, 1> pos_poly_k =
124 poly.k() - poly.H() * velocity_K * velocity_error;
125 const ::aos::controls::HPolytope<2> pos_poly(pos_poly_H, pos_poly_k);
Austin Schuhce8c6cd2016-11-26 15:13:21 -0800126 const ::aos::controls::HVPolytope<2, 4, 4> hv_pos_poly(
127 pos_poly_H, pos_poly_k, pos_poly.Vertices());
Brian Silverman17f503e2015-08-02 18:17:18 -0700128
129 Eigen::Matrix<double, 2, 1> adjusted_pos_error;
130 {
131 const auto &P = position_error;
132
133 // This line was at 45 degrees but is now at some angle steeper than the
134 // straight one between the points.
135 Eigen::Matrix<double, 1, 2> angle_45;
136 // If the top claw is above its soft upper limit, make the line actually
137 // 45 degrees to avoid smashing it into the limit in an attempt to fix the
138 // separation error faster than the bottom position one.
139 if (X_hat(0, 0) + X_hat(1, 0) >
140 constants::GetValues().claw.upper_claw.upper_limit) {
141 angle_45 << 1, 1;
142 } else {
143 // Fixing separation error half as fast as positional error works well
144 // because it means they both close evenly.
145 angle_45 << ::std::sqrt(3), 1;
146 }
147 Eigen::Matrix<double, 1, 2> L45_quadrant;
148 L45_quadrant << ::aos::sign(P(1, 0)), -::aos::sign(P(0, 0));
149 const auto L45 = L45_quadrant.cwiseProduct(angle_45);
150 const double w45 = 0;
151
152 Eigen::Matrix<double, 1, 2> LH;
153 if (::std::abs(P(0, 0)) > ::std::abs(P(1, 0))) {
154 LH << 0, 1;
155 } else {
156 LH << 1, 0;
157 }
158 const double wh = LH.dot(P);
159
160 Eigen::Matrix<double, 2, 2> standard;
161 standard << L45, LH;
162 Eigen::Matrix<double, 2, 1> W;
163 W << w45, wh;
164 const Eigen::Matrix<double, 2, 1> intersection = standard.inverse() * W;
165
166 bool is_inside_h;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700167 const auto adjusted_pos_error_h = DoCoerceGoal<double>(
168 hv_pos_poly, LH, wh, position_error, &is_inside_h);
Brian Silverman17f503e2015-08-02 18:17:18 -0700169 const auto adjusted_pos_error_45 =
Austin Schuhbcce26a2018-03-26 23:41:24 -0700170 DoCoerceGoal<double>(hv_pos_poly, L45, w45, intersection, nullptr);
Brian Silverman17f503e2015-08-02 18:17:18 -0700171 if (pos_poly.IsInside(intersection)) {
172 adjusted_pos_error = adjusted_pos_error_h;
173 } else {
174 if (is_inside_h) {
175 if (adjusted_pos_error_h.norm() > adjusted_pos_error_45.norm()) {
176 adjusted_pos_error = adjusted_pos_error_h;
177 } else {
178 adjusted_pos_error = adjusted_pos_error_45;
179 }
180 } else {
181 adjusted_pos_error = adjusted_pos_error_45;
182 }
183 }
184 }
185
186 LOG_MATRIX(DEBUG, "adjusted_pos_error", adjusted_pos_error);
187 mutable_U() = velocity_K * velocity_error + position_K * adjusted_pos_error;
188 LOG_MATRIX(DEBUG, "U is now", U());
189
190 {
191 const auto values = constants::GetValues().claw;
192 if (top_known_) {
193 if (X_hat(0, 0) + X_hat(1, 0) > values.upper_claw.upper_limit && U(1, 0) > 0) {
194 LOG(WARNING, "upper claw too high and moving up\n");
195 mutable_U(1, 0) = 0;
196 } else if (X_hat(0, 0) + X_hat(1, 0) < values.upper_claw.lower_limit &&
197 U(1, 0) < 0) {
198 LOG(WARNING, "upper claw too low and moving down\n");
199 mutable_U(1, 0) = 0;
200 }
201 }
202 if (bottom_known_) {
203 if (X_hat(0, 0) > values.lower_claw.upper_limit && U(0, 0) > 0) {
204 LOG(WARNING, "lower claw too high and moving up\n");
205 mutable_U(0, 0) = 0;
206 } else if (X_hat(0, 0) < values.lower_claw.lower_limit && U(0, 0) < 0) {
207 LOG(WARNING, "lower claw too low and moving down\n");
208 mutable_U(0, 0) = 0;
209 }
210 }
211 }
212 }
213}
214
215ZeroedStateFeedbackLoop::ZeroedStateFeedbackLoop(const char *name,
216 ClawMotor *motor)
217 : offset_(0.0),
218 name_(name),
219 motor_(motor),
220 zeroing_state_(UNKNOWN_POSITION),
Brian Silverman17f503e2015-08-02 18:17:18 -0700221 encoder_(0.0),
222 last_encoder_(0.0) {}
223
Austin Schuh24957102015-11-28 16:04:40 -0800224void ZeroedStateFeedbackLoop::SetPositionValues(
Brian Silvermanb601d892015-12-20 18:24:38 -0500225 const ::y2014::control_loops::HalfClawPosition &claw) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700226 front_.Update(claw.front);
227 calibration_.Update(claw.calibration);
228 back_.Update(claw.back);
229
230 bool any_sensor_triggered = any_triggered();
231 if (any_sensor_triggered && any_triggered_last_) {
232 // We are still on the hall effect and nothing has changed.
233 min_hall_effect_on_angle_ =
234 ::std::min(min_hall_effect_on_angle_, claw.position);
235 max_hall_effect_on_angle_ =
236 ::std::max(max_hall_effect_on_angle_, claw.position);
237 } else if (!any_sensor_triggered && !any_triggered_last_) {
238 // We are still off the hall effect and nothing has changed.
239 min_hall_effect_off_angle_ =
240 ::std::min(min_hall_effect_off_angle_, claw.position);
241 max_hall_effect_off_angle_ =
242 ::std::max(max_hall_effect_off_angle_, claw.position);
Brian Silverman17f503e2015-08-02 18:17:18 -0700243 }
244
Brian Silvermand3efb182015-05-13 23:04:29 -0400245 if (front_.is_posedge()) {
246 // Saw a posedge on the hall effect. Reset the limits.
247 min_hall_effect_on_angle_ =
248 ::std::min(claw.front.posedge_value, claw.position);
249 max_hall_effect_on_angle_ =
250 ::std::max(claw.front.posedge_value, claw.position);
251 }
252 if (calibration_.is_posedge()) {
253 // Saw a posedge on the hall effect. Reset the limits.
254 min_hall_effect_on_angle_ =
255 ::std::min(claw.calibration.posedge_value, claw.position);
256 max_hall_effect_on_angle_ =
257 ::std::max(claw.calibration.posedge_value, claw.position);
258 }
259 if (back_.is_posedge()) {
260 // Saw a posedge on the hall effect. Reset the limits.
261 min_hall_effect_on_angle_ =
262 ::std::min(claw.back.posedge_value, claw.position);
263 max_hall_effect_on_angle_ =
264 ::std::max(claw.back.posedge_value, claw.position);
265 }
266
267 if (front_.is_negedge()) {
268 // Saw a negedge on the hall effect. Reset the limits.
269 min_hall_effect_off_angle_ =
270 ::std::min(claw.front.negedge_value, claw.position);
271 max_hall_effect_off_angle_ =
272 ::std::max(claw.front.negedge_value, claw.position);
273 }
274 if (calibration_.is_negedge()) {
275 // Saw a negedge on the hall effect. Reset the limits.
276 min_hall_effect_off_angle_ =
277 ::std::min(claw.calibration.negedge_value, claw.position);
278 max_hall_effect_off_angle_ =
279 ::std::max(claw.calibration.negedge_value, claw.position);
280 }
281 if (back_.is_negedge()) {
282 // Saw a negedge on the hall effect. Reset the limits.
283 min_hall_effect_off_angle_ =
284 ::std::min(claw.back.negedge_value, claw.position);
285 max_hall_effect_off_angle_ =
286 ::std::max(claw.back.negedge_value, claw.position);
287 }
288
Brian Silverman17f503e2015-08-02 18:17:18 -0700289 last_encoder_ = encoder_;
290 if (front().value() || calibration().value() || back().value()) {
291 last_on_encoder_ = encoder_;
292 } else {
293 last_off_encoder_ = encoder_;
294 }
295 encoder_ = claw.position;
296 any_triggered_last_ = any_sensor_triggered;
297}
298
Austin Schuh24957102015-11-28 16:04:40 -0800299void ZeroedStateFeedbackLoop::Reset(
Brian Silvermanb601d892015-12-20 18:24:38 -0500300 const ::y2014::control_loops::HalfClawPosition &claw) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700301 set_zeroing_state(ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
302
303 front_.Reset(claw.front);
304 calibration_.Reset(claw.calibration);
305 back_.Reset(claw.back);
306 // close up the min and max edge positions as they are no longer valid and
307 // will be expanded in future iterations
308 min_hall_effect_on_angle_ = claw.position;
309 max_hall_effect_on_angle_ = claw.position;
310 min_hall_effect_off_angle_ = claw.position;
311 max_hall_effect_off_angle_ = claw.position;
312 any_triggered_last_ = any_triggered();
313}
314
315bool TopZeroedStateFeedbackLoop::SetCalibrationOnEdge(
316 const constants::Values::Claws::Claw &claw_values,
317 JointZeroingState zeroing_state) {
318 double edge_encoder;
319 double edge_angle;
320 if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) {
321 LOG(INFO, "Calibration edge edge should be %f.\n", edge_angle);
322 SetCalibration(edge_encoder, edge_angle);
323 set_zeroing_state(zeroing_state);
324 return true;
325 }
326 return false;
327}
328
329void TopZeroedStateFeedbackLoop::HandleCalibrationError(
330 const constants::Values::Claws::Claw &claw_values) {
331 double edge_encoder;
332 double edge_angle;
333 if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) {
334 const double calibration_error =
335 ComputeCalibrationChange(edge_encoder, edge_angle);
336 LOG(INFO, "Top calibration error is %f\n", calibration_error);
337 if (::std::abs(calibration_error) > kRezeroThreshold) {
338 LOG(WARNING, "rezeroing top\n");
339 SetCalibration(edge_encoder, edge_angle);
340 set_zeroing_state(ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
341 }
342 }
343}
344
345
346void BottomZeroedStateFeedbackLoop::HandleCalibrationError(
347 const constants::Values::Claws::Claw &claw_values) {
348 double edge_encoder;
349 double edge_angle;
350 if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) {
351 const double calibration_error =
352 ComputeCalibrationChange(edge_encoder, edge_angle);
353 LOG(INFO, "Bottom calibration error is %f\n", calibration_error);
354 if (::std::abs(calibration_error) > kRezeroThreshold) {
355 LOG(WARNING, "rezeroing bottom\n");
356 SetCalibration(edge_encoder, edge_angle);
357 set_zeroing_state(ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
358 }
359 }
360}
361
362bool BottomZeroedStateFeedbackLoop::SetCalibrationOnEdge(
363 const constants::Values::Claws::Claw &claw_values,
364 JointZeroingState zeroing_state) {
365 double edge_encoder;
366 double edge_angle;
367 if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) {
368 LOG(INFO, "Calibration edge.\n");
369 SetCalibration(edge_encoder, edge_angle);
370 set_zeroing_state(zeroing_state);
371 return true;
372 }
373 return false;
374}
375
Brian Silvermanb601d892015-12-20 18:24:38 -0500376ClawMotor::ClawMotor(::y2014::control_loops::ClawQueue *my_claw)
377 : aos::controls::ControlLoop<::y2014::control_loops::ClawQueue>(my_claw),
Brian Silverman17f503e2015-08-02 18:17:18 -0700378 has_top_claw_goal_(false),
379 top_claw_goal_(0.0),
380 top_claw_(this),
381 has_bottom_claw_goal_(false),
382 bottom_claw_goal_(0.0),
383 bottom_claw_(this),
Austin Schuhedc317c2015-11-08 14:07:42 -0800384 claw_(::y2014::control_loops::claw::MakeClawLoop()),
Brian Silverman17f503e2015-08-02 18:17:18 -0700385 was_enabled_(false),
386 doing_calibration_fine_tune_(false),
387 capped_goal_(false),
388 mode_(UNKNOWN_LOCATION) {}
389
390const int ZeroedStateFeedbackLoop::kZeroingMaxVoltage;
391
392bool ZeroedStateFeedbackLoop::SawFilteredPosedge(
393 const HallEffectTracker &this_sensor, const HallEffectTracker &sensorA,
394 const HallEffectTracker &sensorB) {
395 if (posedge_filter_ == nullptr && this_sensor.posedge_count_changed() &&
396 !sensorA.posedge_count_changed() && !sensorB.posedge_count_changed() &&
397 this_sensor.value() && !this_sensor.last_value()) {
398 posedge_filter_ = &this_sensor;
399 } else if (posedge_filter_ == &this_sensor &&
400 !this_sensor.posedge_count_changed() &&
401 !sensorA.posedge_count_changed() &&
402 !sensorB.posedge_count_changed() && this_sensor.value()) {
403 posedge_filter_ = nullptr;
404 return true;
405 } else if (posedge_filter_ == &this_sensor) {
406 posedge_filter_ = nullptr;
407 }
408 return false;
409}
410
411bool ZeroedStateFeedbackLoop::SawFilteredNegedge(
412 const HallEffectTracker &this_sensor, const HallEffectTracker &sensorA,
413 const HallEffectTracker &sensorB) {
414 if (negedge_filter_ == nullptr && this_sensor.negedge_count_changed() &&
415 !sensorA.negedge_count_changed() && !sensorB.negedge_count_changed() &&
416 !this_sensor.value() && this_sensor.last_value()) {
417 negedge_filter_ = &this_sensor;
418 } else if (negedge_filter_ == &this_sensor &&
419 !this_sensor.negedge_count_changed() &&
420 !sensorA.negedge_count_changed() &&
421 !sensorB.negedge_count_changed() && !this_sensor.value()) {
422 negedge_filter_ = nullptr;
423 return true;
424 } else if (negedge_filter_ == &this_sensor) {
425 negedge_filter_ = nullptr;
426 }
427 return false;
428}
429
430bool ZeroedStateFeedbackLoop::DoGetPositionOfEdge(
431 const constants::Values::Claws::AnglePair &angles, double *edge_encoder,
432 double *edge_angle, const HallEffectTracker &this_sensor,
433 const HallEffectTracker &sensorA, const HallEffectTracker &sensorB,
434 const char *hall_effect_name) {
435 bool found_edge = false;
436
437 if (SawFilteredPosedge(this_sensor, sensorA, sensorB)) {
438 if (min_hall_effect_off_angle_ == max_hall_effect_off_angle_) {
439 LOG(WARNING, "%s: Uncertain which side, rejecting posedge\n", name_);
440 } else {
441 const double average_last_encoder =
442 (min_hall_effect_off_angle_ + max_hall_effect_off_angle_) / 2.0;
Brian Silvermand3efb182015-05-13 23:04:29 -0400443 if (this_sensor.posedge_value() < average_last_encoder) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700444 *edge_angle = angles.upper_decreasing_angle;
445 LOG(INFO, "%s Posedge upper of %s -> %f posedge: %f avg_encoder: %f\n",
Brian Silvermand3efb182015-05-13 23:04:29 -0400446 name_, hall_effect_name, *edge_angle, this_sensor.posedge_value(),
Brian Silverman17f503e2015-08-02 18:17:18 -0700447 average_last_encoder);
448 } else {
449 *edge_angle = angles.lower_angle;
450 LOG(INFO, "%s Posedge lower of %s -> %f posedge: %f avg_encoder: %f\n",
Brian Silvermand3efb182015-05-13 23:04:29 -0400451 name_, hall_effect_name, *edge_angle, this_sensor.posedge_value(),
Brian Silverman17f503e2015-08-02 18:17:18 -0700452 average_last_encoder);
453 }
Brian Silvermand3efb182015-05-13 23:04:29 -0400454 *edge_encoder = this_sensor.posedge_value();
Brian Silverman17f503e2015-08-02 18:17:18 -0700455 found_edge = true;
456 }
457 }
458
459 if (SawFilteredNegedge(this_sensor, sensorA, sensorB)) {
460 if (min_hall_effect_on_angle_ == max_hall_effect_on_angle_) {
461 LOG(WARNING, "%s: Uncertain which side, rejecting negedge\n", name_);
462 } else {
463 const double average_last_encoder =
464 (min_hall_effect_on_angle_ + max_hall_effect_on_angle_) / 2.0;
Brian Silvermand3efb182015-05-13 23:04:29 -0400465 if (this_sensor.negedge_value() > average_last_encoder) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700466 *edge_angle = angles.upper_angle;
467 LOG(INFO, "%s Negedge upper of %s -> %f negedge: %f avg_encoder: %f\n",
Brian Silvermand3efb182015-05-13 23:04:29 -0400468 name_, hall_effect_name, *edge_angle, this_sensor.negedge_value(),
Brian Silverman17f503e2015-08-02 18:17:18 -0700469 average_last_encoder);
470 } else {
471 *edge_angle = angles.lower_decreasing_angle;
472 LOG(INFO, "%s Negedge lower of %s -> %f negedge: %f avg_encoder: %f\n",
Brian Silvermand3efb182015-05-13 23:04:29 -0400473 name_, hall_effect_name, *edge_angle, this_sensor.negedge_value(),
Brian Silverman17f503e2015-08-02 18:17:18 -0700474 average_last_encoder);
475 }
Brian Silvermand3efb182015-05-13 23:04:29 -0400476 *edge_encoder = this_sensor.negedge_value();
Brian Silverman17f503e2015-08-02 18:17:18 -0700477 found_edge = true;
478 }
479 }
480
481 return found_edge;
482}
483
484bool ZeroedStateFeedbackLoop::GetPositionOfEdge(
485 const constants::Values::Claws::Claw &claw_values, double *edge_encoder,
486 double *edge_angle) {
487 if (DoGetPositionOfEdge(claw_values.front, edge_encoder, edge_angle, front_,
488 calibration_, back_, "front")) {
489 return true;
490 }
491 if (DoGetPositionOfEdge(claw_values.calibration, edge_encoder, edge_angle,
492 calibration_, front_, back_, "calibration")) {
493 return true;
494 }
495 if (DoGetPositionOfEdge(claw_values.back, edge_encoder, edge_angle, back_,
496 calibration_, front_, "back")) {
497 return true;
498 }
499 return false;
500}
501
502void TopZeroedStateFeedbackLoop::SetCalibration(double edge_encoder,
503 double edge_angle) {
504 double old_offset = offset_;
505 offset_ = edge_angle - edge_encoder;
506 const double doffset = offset_ - old_offset;
507 motor_->ChangeTopOffset(doffset);
508}
509
510double TopZeroedStateFeedbackLoop::ComputeCalibrationChange(double edge_encoder,
511 double edge_angle) {
512 const double offset = edge_angle - edge_encoder;
513 const double doffset = offset - offset_;
514 return doffset;
515}
516
517void BottomZeroedStateFeedbackLoop::SetCalibration(double edge_encoder,
518 double edge_angle) {
519 double old_offset = offset_;
520 offset_ = edge_angle - edge_encoder;
521 const double doffset = offset_ - old_offset;
522 motor_->ChangeBottomOffset(doffset);
523}
524
525double BottomZeroedStateFeedbackLoop::ComputeCalibrationChange(
526 double edge_encoder, double edge_angle) {
527 const double offset = edge_angle - edge_encoder;
528 const double doffset = offset - offset_;
529 return doffset;
530}
531
532void ClawMotor::ChangeTopOffset(double doffset) {
533 claw_.ChangeTopOffset(doffset);
534 if (has_top_claw_goal_) {
535 top_claw_goal_ += doffset;
536 }
537}
538
539void ClawMotor::ChangeBottomOffset(double doffset) {
540 claw_.ChangeBottomOffset(doffset);
541 if (has_bottom_claw_goal_) {
542 bottom_claw_goal_ += doffset;
543 }
544}
545
546void ClawLimitedLoop::ChangeTopOffset(double doffset) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700547 mutable_X_hat()(1, 0) += doffset;
548 LOG(INFO, "Changing top offset by %f\n", doffset);
549}
550void ClawLimitedLoop::ChangeBottomOffset(double doffset) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700551 mutable_X_hat()(0, 0) += doffset;
552 mutable_X_hat()(1, 0) -= doffset;
553 LOG(INFO, "Changing bottom offset by %f\n", doffset);
554}
555
556void LimitClawGoal(double *bottom_goal, double *top_goal,
Austin Schuh24957102015-11-28 16:04:40 -0800557 const constants::Values &values) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700558 // first update position based on angle limit
559 const double separation = *top_goal - *bottom_goal;
560 if (separation > values.claw.soft_max_separation) {
561 LOG_STRUCT(DEBUG, "before", ClawPositionToLog(*top_goal, *bottom_goal));
562 const double dsep = (separation - values.claw.soft_max_separation) / 2.0;
563 *bottom_goal += dsep;
564 *top_goal -= dsep;
565 LOG_STRUCT(DEBUG, "after", ClawPositionToLog(*top_goal, *bottom_goal));
566 }
567 if (separation < values.claw.soft_min_separation) {
568 LOG_STRUCT(DEBUG, "before", ClawPositionToLog(*top_goal, *bottom_goal));
569 const double dsep = (separation - values.claw.soft_min_separation) / 2.0;
570 *bottom_goal += dsep;
571 *top_goal -= dsep;
572 LOG_STRUCT(DEBUG, "after", ClawPositionToLog(*top_goal, *bottom_goal));
573 }
574
575 // now move both goals in unison
576 if (*bottom_goal < values.claw.lower_claw.lower_limit) {
577 LOG_STRUCT(DEBUG, "before", ClawPositionToLog(*top_goal, *bottom_goal));
578 *top_goal += values.claw.lower_claw.lower_limit - *bottom_goal;
579 *bottom_goal = values.claw.lower_claw.lower_limit;
580 LOG_STRUCT(DEBUG, "after", ClawPositionToLog(*top_goal, *bottom_goal));
581 }
582 if (*bottom_goal > values.claw.lower_claw.upper_limit) {
583 LOG_STRUCT(DEBUG, "before", ClawPositionToLog(*top_goal, *bottom_goal));
584 *top_goal -= *bottom_goal - values.claw.lower_claw.upper_limit;
585 *bottom_goal = values.claw.lower_claw.upper_limit;
586 LOG_STRUCT(DEBUG, "after", ClawPositionToLog(*top_goal, *bottom_goal));
587 }
588
589 if (*top_goal < values.claw.upper_claw.lower_limit) {
590 LOG_STRUCT(DEBUG, "before", ClawPositionToLog(*top_goal, *bottom_goal));
591 *bottom_goal += values.claw.upper_claw.lower_limit - *top_goal;
592 *top_goal = values.claw.upper_claw.lower_limit;
593 LOG_STRUCT(DEBUG, "after", ClawPositionToLog(*top_goal, *bottom_goal));
594 }
595 if (*top_goal > values.claw.upper_claw.upper_limit) {
596 LOG_STRUCT(DEBUG, "before", ClawPositionToLog(*top_goal, *bottom_goal));
597 *bottom_goal -= *top_goal - values.claw.upper_claw.upper_limit;
598 *top_goal = values.claw.upper_claw.upper_limit;
599 LOG_STRUCT(DEBUG, "after", ClawPositionToLog(*top_goal, *bottom_goal));
600 }
601}
602
603bool ClawMotor::is_ready() const {
604 return (
605 (top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED &&
606 bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) ||
Austin Schuheeec74a2019-01-27 20:58:59 -0800607 ((has_joystick_state() ? joystick_state().autonomous : true) &&
Brian Silverman17f503e2015-08-02 18:17:18 -0700608 ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
609 top_claw_.zeroing_state() ==
610 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
611 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
612 bottom_claw_.zeroing_state() ==
613 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION))));
614}
615
616bool ClawMotor::is_zeroing() const { return !is_ready(); }
617
618// Positive angle is up, and positive power is up.
Austin Schuh24957102015-11-28 16:04:40 -0800619void ClawMotor::RunIteration(
Brian Silvermanb601d892015-12-20 18:24:38 -0500620 const ::y2014::control_loops::ClawQueue::Goal *goal,
621 const ::y2014::control_loops::ClawQueue::Position *position,
622 ::y2014::control_loops::ClawQueue::Output *output,
623 ::y2014::control_loops::ClawQueue::Status *status) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700624 // Disable the motors now so that all early returns will return with the
625 // motors disabled.
626 if (output) {
627 output->top_claw_voltage = 0;
628 output->bottom_claw_voltage = 0;
629 output->intake_voltage = 0;
630 output->tusk_voltage = 0;
631 }
632
633 if (goal) {
634 if (::std::isnan(goal->bottom_angle) ||
635 ::std::isnan(goal->separation_angle) || ::std::isnan(goal->intake) ||
636 ::std::isnan(goal->centering)) {
637 return;
638 }
639 }
640
641 if (WasReset()) {
642 top_claw_.Reset(position->top);
643 bottom_claw_.Reset(position->bottom);
644 }
645
Austin Schuh24957102015-11-28 16:04:40 -0800646 const constants::Values &values = constants::GetValues();
Brian Silverman17f503e2015-08-02 18:17:18 -0700647
648 if (position) {
649 Eigen::Matrix<double, 2, 1> Y;
650 Y << position->bottom.position + bottom_claw_.offset(),
651 position->top.position + top_claw_.offset();
652 claw_.Correct(Y);
653
654 top_claw_.SetPositionValues(position->top);
655 bottom_claw_.SetPositionValues(position->bottom);
656
657 if (!has_top_claw_goal_) {
658 has_top_claw_goal_ = true;
659 top_claw_goal_ = top_claw_.absolute_position();
660 initial_separation_ =
661 top_claw_.absolute_position() - bottom_claw_.absolute_position();
662 }
663 if (!has_bottom_claw_goal_) {
664 has_bottom_claw_goal_ = true;
665 bottom_claw_goal_ = bottom_claw_.absolute_position();
666 initial_separation_ =
667 top_claw_.absolute_position() - bottom_claw_.absolute_position();
668 }
669 LOG_STRUCT(DEBUG, "absolute position",
670 ClawPositionToLog(top_claw_.absolute_position(),
671 bottom_claw_.absolute_position()));
672 }
673
674 bool autonomous, enabled;
Austin Schuheeec74a2019-01-27 20:58:59 -0800675 if (has_joystick_state()) {
676 autonomous = joystick_state().autonomous;
677 enabled = joystick_state().enabled;
678 } else {
Brian Silverman17f503e2015-08-02 18:17:18 -0700679 autonomous = true;
680 enabled = false;
Brian Silverman17f503e2015-08-02 18:17:18 -0700681 }
682
683 double bottom_claw_velocity_ = 0.0;
684 double top_claw_velocity_ = 0.0;
685
686 if (goal != NULL &&
687 ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED &&
688 bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) ||
689 (autonomous &&
690 ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
691 top_claw_.zeroing_state() ==
692 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
693 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
694 bottom_claw_.zeroing_state() ==
695 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION))))) {
696 // Ready to use the claw.
697 // Limit the goals here.
698 bottom_claw_goal_ = goal->bottom_angle;
699 top_claw_goal_ = goal->bottom_angle + goal->separation_angle;
700 has_bottom_claw_goal_ = true;
701 has_top_claw_goal_ = true;
702 doing_calibration_fine_tune_ = false;
703 mode_ = READY;
704
705 bottom_claw_.HandleCalibrationError(values.claw.lower_claw);
706 top_claw_.HandleCalibrationError(values.claw.upper_claw);
707 } else if (top_claw_.zeroing_state() !=
708 ZeroedStateFeedbackLoop::UNKNOWN_POSITION &&
709 bottom_claw_.zeroing_state() !=
710 ZeroedStateFeedbackLoop::UNKNOWN_POSITION) {
711 // Time to fine tune the zero.
712 // Limit the goals here.
713 if (!enabled) {
714 // If we are disabled, start the fine tune process over again.
715 doing_calibration_fine_tune_ = false;
716 }
717 if (bottom_claw_.zeroing_state() != ZeroedStateFeedbackLoop::CALIBRATED) {
718 // always get the bottom claw to calibrated first
719 LOG(DEBUG, "Calibrating the bottom of the claw\n");
720 if (!doing_calibration_fine_tune_) {
721 if (::std::abs(bottom_absolute_position() -
722 values.claw.start_fine_tune_pos) <
723 values.claw.claw_unimportant_epsilon) {
724 doing_calibration_fine_tune_ = true;
Austin Schuh0e997732015-11-08 15:14:53 -0800725 bottom_claw_goal_ += values.claw.claw_zeroing_speed * kDt;
Brian Silverman17f503e2015-08-02 18:17:18 -0700726 top_claw_velocity_ = bottom_claw_velocity_ =
727 values.claw.claw_zeroing_speed;
728 LOG(DEBUG, "Ready to fine tune the bottom\n");
729 mode_ = FINE_TUNE_BOTTOM;
730 } else {
731 // send bottom to zeroing start
732 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
733 LOG(DEBUG, "Going to the start position for the bottom\n");
734 mode_ = PREP_FINE_TUNE_BOTTOM;
735 }
736 } else {
737 mode_ = FINE_TUNE_BOTTOM;
Austin Schuh0e997732015-11-08 15:14:53 -0800738 bottom_claw_goal_ += values.claw.claw_zeroing_speed * kDt;
Brian Silverman17f503e2015-08-02 18:17:18 -0700739 top_claw_velocity_ = bottom_claw_velocity_ =
740 values.claw.claw_zeroing_speed;
741 if (top_claw_.front_or_back_triggered() ||
742 bottom_claw_.front_or_back_triggered()) {
743 // We shouldn't hit a limit, but if we do, go back to the zeroing
744 // point and try again.
745 doing_calibration_fine_tune_ = false;
746 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
747 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
748 LOG(DEBUG, "Found a limit, starting over.\n");
749 mode_ = PREP_FINE_TUNE_BOTTOM;
750 }
751
752 if (position && bottom_claw_.SawFilteredPosedge(
753 bottom_claw_.calibration(), bottom_claw_.front(),
754 bottom_claw_.back())) {
755 // do calibration
756 bottom_claw_.SetCalibration(
Brian Silvermand3efb182015-05-13 23:04:29 -0400757 position->bottom.calibration.posedge_value,
Brian Silverman17f503e2015-08-02 18:17:18 -0700758 values.claw.lower_claw.calibration.lower_angle);
759 bottom_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED);
760 // calibrated so we are done fine tuning bottom
761 doing_calibration_fine_tune_ = false;
762 LOG(DEBUG, "Calibrated the bottom correctly!\n");
763 } else if (bottom_claw_.calibration().last_value()) {
764 LOG(DEBUG, "Aborting bottom fine tune because sensor triggered\n");
765 doing_calibration_fine_tune_ = false;
766 bottom_claw_.set_zeroing_state(
767 ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
768 } else {
769 LOG(DEBUG, "Fine tuning\n");
770 }
771 }
772 // now set the top claw to track
773
774 top_claw_goal_ = bottom_claw_goal_ + values.claw.claw_zeroing_separation;
775 } else {
776 // bottom claw must be calibrated, start on the top
777 if (!doing_calibration_fine_tune_) {
778 if (::std::abs(top_absolute_position() -
779 values.claw.start_fine_tune_pos) <
780 values.claw.claw_unimportant_epsilon) {
781 doing_calibration_fine_tune_ = true;
Austin Schuh0e997732015-11-08 15:14:53 -0800782 top_claw_goal_ += values.claw.claw_zeroing_speed * kDt;
Brian Silverman17f503e2015-08-02 18:17:18 -0700783 top_claw_velocity_ = bottom_claw_velocity_ =
784 values.claw.claw_zeroing_speed;
785 LOG(DEBUG, "Ready to fine tune the top\n");
786 mode_ = FINE_TUNE_TOP;
787 } else {
788 // send top to zeroing start
789 top_claw_goal_ = values.claw.start_fine_tune_pos;
790 LOG(DEBUG, "Going to the start position for the top\n");
791 mode_ = PREP_FINE_TUNE_TOP;
792 }
793 } else {
794 mode_ = FINE_TUNE_TOP;
Austin Schuh0e997732015-11-08 15:14:53 -0800795 top_claw_goal_ += values.claw.claw_zeroing_speed * kDt;
Brian Silverman17f503e2015-08-02 18:17:18 -0700796 top_claw_velocity_ = bottom_claw_velocity_ =
797 values.claw.claw_zeroing_speed;
798 if (top_claw_.front_or_back_triggered() ||
799 bottom_claw_.front_or_back_triggered()) {
800 // this should not happen, but now we know it won't
801 doing_calibration_fine_tune_ = false;
802 top_claw_goal_ = values.claw.start_fine_tune_pos;
803 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
804 LOG(DEBUG, "Found a limit, starting over.\n");
805 mode_ = PREP_FINE_TUNE_TOP;
806 }
807
808 if (position &&
809 top_claw_.SawFilteredPosedge(top_claw_.calibration(),
810 top_claw_.front(), top_claw_.back())) {
811 // do calibration
812 top_claw_.SetCalibration(
Brian Silvermand3efb182015-05-13 23:04:29 -0400813 position->top.calibration.posedge_value,
Brian Silverman17f503e2015-08-02 18:17:18 -0700814 values.claw.upper_claw.calibration.lower_angle);
815 top_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED);
816 // calibrated so we are done fine tuning top
817 doing_calibration_fine_tune_ = false;
818 LOG(DEBUG, "Calibrated the top correctly!\n");
819 } else if (top_claw_.calibration().last_value()) {
820 LOG(DEBUG, "Aborting top fine tune because sensor triggered\n");
821 doing_calibration_fine_tune_ = false;
822 top_claw_.set_zeroing_state(
823 ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
824 }
825 }
826 // now set the bottom claw to track
827 bottom_claw_goal_ = top_claw_goal_ - values.claw.claw_zeroing_separation;
828 }
829 } else {
830 doing_calibration_fine_tune_ = false;
831 if (!was_enabled_ && enabled) {
832 if (position) {
833 top_claw_goal_ = position->top.position + top_claw_.offset();
834 bottom_claw_goal_ = position->bottom.position + bottom_claw_.offset();
835 initial_separation_ =
836 position->top.position - position->bottom.position;
837 } else {
838 has_top_claw_goal_ = false;
839 has_bottom_claw_goal_ = false;
840 }
841 }
842
843 if ((bottom_claw_.zeroing_state() !=
844 ZeroedStateFeedbackLoop::UNKNOWN_POSITION ||
845 bottom_claw_.front().value() || top_claw_.front().value()) &&
846 !top_claw_.back().value() && !bottom_claw_.back().value()) {
847 if (enabled) {
848 // Time to slowly move back up to find any position to narrow down the
849 // zero.
Austin Schuh0e997732015-11-08 15:14:53 -0800850 top_claw_goal_ += values.claw.claw_zeroing_off_speed * kDt;
851 bottom_claw_goal_ += values.claw.claw_zeroing_off_speed * kDt;
Brian Silverman17f503e2015-08-02 18:17:18 -0700852 top_claw_velocity_ = bottom_claw_velocity_ =
853 values.claw.claw_zeroing_off_speed;
854 LOG(DEBUG, "Bottom is known.\n");
855 }
856 } else {
857 // We don't know where either claw is. Slowly start moving down to find
858 // any hall effect.
859 if (enabled) {
Austin Schuh0e997732015-11-08 15:14:53 -0800860 top_claw_goal_ -= values.claw.claw_zeroing_off_speed * kDt;
861 bottom_claw_goal_ -= values.claw.claw_zeroing_off_speed * kDt;
Brian Silverman17f503e2015-08-02 18:17:18 -0700862 top_claw_velocity_ = bottom_claw_velocity_ =
863 -values.claw.claw_zeroing_off_speed;
864 LOG(DEBUG, "Both are unknown.\n");
865 }
866 }
867
868 if (position) {
869 if (enabled) {
870 top_claw_.SetCalibrationOnEdge(
871 values.claw.upper_claw,
872 ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION);
873 bottom_claw_.SetCalibrationOnEdge(
874 values.claw.lower_claw,
875 ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION);
876 } else {
877 // TODO(austin): Only calibrate on the predetermined edge.
878 // We might be able to just ignore this since the backlash is soooo
879 // low.
880 // :)
881 top_claw_.SetCalibrationOnEdge(
882 values.claw.upper_claw,
883 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
884 bottom_claw_.SetCalibrationOnEdge(
885 values.claw.lower_claw,
886 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
887 }
888 }
889 mode_ = UNKNOWN_LOCATION;
890 }
891
892 // Limit the goals if both claws have been (mostly) found.
893 if (mode_ != UNKNOWN_LOCATION) {
894 LimitClawGoal(&bottom_claw_goal_, &top_claw_goal_, values);
895 }
896
897 claw_.set_positions_known(
898 top_claw_.zeroing_state() != ZeroedStateFeedbackLoop::UNKNOWN_POSITION,
899 bottom_claw_.zeroing_state() !=
900 ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
901 if (has_top_claw_goal_ && has_bottom_claw_goal_) {
902 claw_.mutable_R() << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_,
903 bottom_claw_velocity_, top_claw_velocity_ - bottom_claw_velocity_;
904 LOG_MATRIX(DEBUG, "actual goal", claw_.R());
905
906 // Only cap power when one of the halves of the claw is moving slowly and
907 // could wind up.
908 claw_.set_is_zeroing(mode_ == UNKNOWN_LOCATION || mode_ == FINE_TUNE_TOP ||
909 mode_ == FINE_TUNE_BOTTOM);
910 claw_.Update(output == nullptr);
911 } else {
912 claw_.Update(true);
913 }
914
915 capped_goal_ = false;
916 switch (mode_) {
917 case READY:
918 case PREP_FINE_TUNE_TOP:
919 case PREP_FINE_TUNE_BOTTOM:
920 break;
921 case FINE_TUNE_BOTTOM:
922 case FINE_TUNE_TOP:
923 case UNKNOWN_LOCATION: {
924 if (claw_.uncapped_average_voltage() > values.claw.max_zeroing_voltage) {
Austin Schuh32501832017-02-25 18:32:56 -0800925 double dx_bot =
926 (claw_.U_uncapped(0, 0) - values.claw.max_zeroing_voltage) /
927 claw_.controller().K(0, 0);
928 double dx_top =
929 (claw_.U_uncapped(1, 0) - values.claw.max_zeroing_voltage) /
930 claw_.controller().K(0, 0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700931 double dx = ::std::max(dx_top, dx_bot);
932 bottom_claw_goal_ -= dx;
933 top_claw_goal_ -= dx;
934 Eigen::Matrix<double, 4, 1> R;
935 R << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_, claw_.R(2, 0),
936 claw_.R(3, 0);
Austin Schuh32501832017-02-25 18:32:56 -0800937 claw_.mutable_U() = claw_.controller().K() * (R - claw_.X_hat());
Brian Silverman17f503e2015-08-02 18:17:18 -0700938 capped_goal_ = true;
939 LOG(DEBUG, "Moving the goal by %f to prevent windup."
940 " Uncapped is %f, max is %f, difference is %f\n",
941 dx,
942 claw_.uncapped_average_voltage(), values.claw.max_zeroing_voltage,
943 (claw_.uncapped_average_voltage() -
944 values.claw.max_zeroing_voltage));
945 } else if (claw_.uncapped_average_voltage() <
946 -values.claw.max_zeroing_voltage) {
Austin Schuh32501832017-02-25 18:32:56 -0800947 double dx_bot =
948 (claw_.U_uncapped(0, 0) + values.claw.max_zeroing_voltage) /
949 claw_.controller().K(0, 0);
950 double dx_top =
951 (claw_.U_uncapped(1, 0) + values.claw.max_zeroing_voltage) /
952 claw_.controller().K(0, 0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700953 double dx = ::std::min(dx_top, dx_bot);
954 bottom_claw_goal_ -= dx;
955 top_claw_goal_ -= dx;
956 Eigen::Matrix<double, 4, 1> R;
957 R << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_, claw_.R(2, 0),
958 claw_.R(3, 0);
Austin Schuh32501832017-02-25 18:32:56 -0800959 claw_.mutable_U() = claw_.controller().K() * (R - claw_.X_hat());
Brian Silverman17f503e2015-08-02 18:17:18 -0700960 capped_goal_ = true;
961 LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx);
962 }
963 } break;
964 }
965
966 if (output) {
967 if (goal) {
968 //setup the intake
969 output->intake_voltage =
970 (goal->intake > 12.0) ? 12 : (goal->intake < -12.0) ? -12.0
971 : goal->intake;
972 output->tusk_voltage = goal->centering;
973 output->tusk_voltage =
974 (goal->centering > 12.0) ? 12 : (goal->centering < -12.0)
975 ? -12.0
976 : goal->centering;
977 }
978 output->top_claw_voltage = claw_.U(1, 0);
979 output->bottom_claw_voltage = claw_.U(0, 0);
980
981 if (output->top_claw_voltage > kMaxVoltage) {
982 output->top_claw_voltage = kMaxVoltage;
983 } else if (output->top_claw_voltage < -kMaxVoltage) {
984 output->top_claw_voltage = -kMaxVoltage;
985 }
986
987 if (output->bottom_claw_voltage > kMaxVoltage) {
988 output->bottom_claw_voltage = kMaxVoltage;
989 } else if (output->bottom_claw_voltage < -kMaxVoltage) {
990 output->bottom_claw_voltage = -kMaxVoltage;
991 }
992 }
993
994 status->bottom = bottom_absolute_position();
995 status->separation = top_absolute_position() - bottom_absolute_position();
996 status->bottom_velocity = claw_.X_hat(2, 0);
997 status->separation_velocity = claw_.X_hat(3, 0);
998
999 if (goal) {
1000 bool bottom_done =
1001 ::std::abs(bottom_absolute_position() - goal->bottom_angle) < 0.020;
1002 bool bottom_velocity_done = ::std::abs(status->bottom_velocity) < 0.2;
1003 bool separation_done =
1004 ::std::abs((top_absolute_position() - bottom_absolute_position()) -
1005 goal->separation_angle) < 0.020;
1006 bool separation_done_with_ball =
1007 ::std::abs((top_absolute_position() - bottom_absolute_position()) -
1008 goal->separation_angle) < 0.06;
1009 status->done = is_ready() && separation_done && bottom_done && bottom_velocity_done;
1010 status->done_with_ball =
1011 is_ready() && separation_done_with_ball && bottom_done && bottom_velocity_done;
1012 } else {
1013 status->done = status->done_with_ball = false;
1014 }
1015
1016 status->zeroed = is_ready();
1017 status->zeroed_for_auto =
1018 (top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
1019 top_claw_.zeroing_state() ==
1020 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
1021 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
1022 bottom_claw_.zeroing_state() ==
1023 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
1024
1025 was_enabled_ = enabled;
1026}
1027
1028} // namespace control_loops
Austin Schuh24957102015-11-28 16:04:40 -08001029} // namespace y2014