blob: 7a28a2366117cf79e4cf150c959447c844f9ae73 [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
Austin Schuh55a13dc2019-01-27 22:39:03 -0800376ClawMotor::ClawMotor(::aos::EventLoop *event_loop, const ::std::string &name)
377 : aos::controls::ControlLoop<::y2014::control_loops::ClawQueue>(event_loop,
378 name),
Brian Silverman17f503e2015-08-02 18:17:18 -0700379 has_top_claw_goal_(false),
380 top_claw_goal_(0.0),
381 top_claw_(this),
382 has_bottom_claw_goal_(false),
383 bottom_claw_goal_(0.0),
384 bottom_claw_(this),
Austin Schuhedc317c2015-11-08 14:07:42 -0800385 claw_(::y2014::control_loops::claw::MakeClawLoop()),
Brian Silverman17f503e2015-08-02 18:17:18 -0700386 was_enabled_(false),
387 doing_calibration_fine_tune_(false),
388 capped_goal_(false),
389 mode_(UNKNOWN_LOCATION) {}
390
391const int ZeroedStateFeedbackLoop::kZeroingMaxVoltage;
392
393bool ZeroedStateFeedbackLoop::SawFilteredPosedge(
394 const HallEffectTracker &this_sensor, const HallEffectTracker &sensorA,
395 const HallEffectTracker &sensorB) {
396 if (posedge_filter_ == nullptr && this_sensor.posedge_count_changed() &&
397 !sensorA.posedge_count_changed() && !sensorB.posedge_count_changed() &&
398 this_sensor.value() && !this_sensor.last_value()) {
399 posedge_filter_ = &this_sensor;
400 } else if (posedge_filter_ == &this_sensor &&
401 !this_sensor.posedge_count_changed() &&
402 !sensorA.posedge_count_changed() &&
403 !sensorB.posedge_count_changed() && this_sensor.value()) {
404 posedge_filter_ = nullptr;
405 return true;
406 } else if (posedge_filter_ == &this_sensor) {
407 posedge_filter_ = nullptr;
408 }
409 return false;
410}
411
412bool ZeroedStateFeedbackLoop::SawFilteredNegedge(
413 const HallEffectTracker &this_sensor, const HallEffectTracker &sensorA,
414 const HallEffectTracker &sensorB) {
415 if (negedge_filter_ == nullptr && this_sensor.negedge_count_changed() &&
416 !sensorA.negedge_count_changed() && !sensorB.negedge_count_changed() &&
417 !this_sensor.value() && this_sensor.last_value()) {
418 negedge_filter_ = &this_sensor;
419 } else if (negedge_filter_ == &this_sensor &&
420 !this_sensor.negedge_count_changed() &&
421 !sensorA.negedge_count_changed() &&
422 !sensorB.negedge_count_changed() && !this_sensor.value()) {
423 negedge_filter_ = nullptr;
424 return true;
425 } else if (negedge_filter_ == &this_sensor) {
426 negedge_filter_ = nullptr;
427 }
428 return false;
429}
430
431bool ZeroedStateFeedbackLoop::DoGetPositionOfEdge(
432 const constants::Values::Claws::AnglePair &angles, double *edge_encoder,
433 double *edge_angle, const HallEffectTracker &this_sensor,
434 const HallEffectTracker &sensorA, const HallEffectTracker &sensorB,
435 const char *hall_effect_name) {
436 bool found_edge = false;
437
438 if (SawFilteredPosedge(this_sensor, sensorA, sensorB)) {
439 if (min_hall_effect_off_angle_ == max_hall_effect_off_angle_) {
440 LOG(WARNING, "%s: Uncertain which side, rejecting posedge\n", name_);
441 } else {
442 const double average_last_encoder =
443 (min_hall_effect_off_angle_ + max_hall_effect_off_angle_) / 2.0;
Brian Silvermand3efb182015-05-13 23:04:29 -0400444 if (this_sensor.posedge_value() < average_last_encoder) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700445 *edge_angle = angles.upper_decreasing_angle;
446 LOG(INFO, "%s Posedge upper of %s -> %f posedge: %f avg_encoder: %f\n",
Brian Silvermand3efb182015-05-13 23:04:29 -0400447 name_, hall_effect_name, *edge_angle, this_sensor.posedge_value(),
Brian Silverman17f503e2015-08-02 18:17:18 -0700448 average_last_encoder);
449 } else {
450 *edge_angle = angles.lower_angle;
451 LOG(INFO, "%s Posedge lower of %s -> %f posedge: %f avg_encoder: %f\n",
Brian Silvermand3efb182015-05-13 23:04:29 -0400452 name_, hall_effect_name, *edge_angle, this_sensor.posedge_value(),
Brian Silverman17f503e2015-08-02 18:17:18 -0700453 average_last_encoder);
454 }
Brian Silvermand3efb182015-05-13 23:04:29 -0400455 *edge_encoder = this_sensor.posedge_value();
Brian Silverman17f503e2015-08-02 18:17:18 -0700456 found_edge = true;
457 }
458 }
459
460 if (SawFilteredNegedge(this_sensor, sensorA, sensorB)) {
461 if (min_hall_effect_on_angle_ == max_hall_effect_on_angle_) {
462 LOG(WARNING, "%s: Uncertain which side, rejecting negedge\n", name_);
463 } else {
464 const double average_last_encoder =
465 (min_hall_effect_on_angle_ + max_hall_effect_on_angle_) / 2.0;
Brian Silvermand3efb182015-05-13 23:04:29 -0400466 if (this_sensor.negedge_value() > average_last_encoder) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700467 *edge_angle = angles.upper_angle;
468 LOG(INFO, "%s Negedge upper of %s -> %f negedge: %f avg_encoder: %f\n",
Brian Silvermand3efb182015-05-13 23:04:29 -0400469 name_, hall_effect_name, *edge_angle, this_sensor.negedge_value(),
Brian Silverman17f503e2015-08-02 18:17:18 -0700470 average_last_encoder);
471 } else {
472 *edge_angle = angles.lower_decreasing_angle;
473 LOG(INFO, "%s Negedge lower of %s -> %f negedge: %f avg_encoder: %f\n",
Brian Silvermand3efb182015-05-13 23:04:29 -0400474 name_, hall_effect_name, *edge_angle, this_sensor.negedge_value(),
Brian Silverman17f503e2015-08-02 18:17:18 -0700475 average_last_encoder);
476 }
Brian Silvermand3efb182015-05-13 23:04:29 -0400477 *edge_encoder = this_sensor.negedge_value();
Brian Silverman17f503e2015-08-02 18:17:18 -0700478 found_edge = true;
479 }
480 }
481
482 return found_edge;
483}
484
485bool ZeroedStateFeedbackLoop::GetPositionOfEdge(
486 const constants::Values::Claws::Claw &claw_values, double *edge_encoder,
487 double *edge_angle) {
488 if (DoGetPositionOfEdge(claw_values.front, edge_encoder, edge_angle, front_,
489 calibration_, back_, "front")) {
490 return true;
491 }
492 if (DoGetPositionOfEdge(claw_values.calibration, edge_encoder, edge_angle,
493 calibration_, front_, back_, "calibration")) {
494 return true;
495 }
496 if (DoGetPositionOfEdge(claw_values.back, edge_encoder, edge_angle, back_,
497 calibration_, front_, "back")) {
498 return true;
499 }
500 return false;
501}
502
503void TopZeroedStateFeedbackLoop::SetCalibration(double edge_encoder,
504 double edge_angle) {
505 double old_offset = offset_;
506 offset_ = edge_angle - edge_encoder;
507 const double doffset = offset_ - old_offset;
508 motor_->ChangeTopOffset(doffset);
509}
510
511double TopZeroedStateFeedbackLoop::ComputeCalibrationChange(double edge_encoder,
512 double edge_angle) {
513 const double offset = edge_angle - edge_encoder;
514 const double doffset = offset - offset_;
515 return doffset;
516}
517
518void BottomZeroedStateFeedbackLoop::SetCalibration(double edge_encoder,
519 double edge_angle) {
520 double old_offset = offset_;
521 offset_ = edge_angle - edge_encoder;
522 const double doffset = offset_ - old_offset;
523 motor_->ChangeBottomOffset(doffset);
524}
525
526double BottomZeroedStateFeedbackLoop::ComputeCalibrationChange(
527 double edge_encoder, double edge_angle) {
528 const double offset = edge_angle - edge_encoder;
529 const double doffset = offset - offset_;
530 return doffset;
531}
532
533void ClawMotor::ChangeTopOffset(double doffset) {
534 claw_.ChangeTopOffset(doffset);
535 if (has_top_claw_goal_) {
536 top_claw_goal_ += doffset;
537 }
538}
539
540void ClawMotor::ChangeBottomOffset(double doffset) {
541 claw_.ChangeBottomOffset(doffset);
542 if (has_bottom_claw_goal_) {
543 bottom_claw_goal_ += doffset;
544 }
545}
546
547void ClawLimitedLoop::ChangeTopOffset(double doffset) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700548 mutable_X_hat()(1, 0) += doffset;
549 LOG(INFO, "Changing top offset by %f\n", doffset);
550}
551void ClawLimitedLoop::ChangeBottomOffset(double doffset) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700552 mutable_X_hat()(0, 0) += doffset;
553 mutable_X_hat()(1, 0) -= doffset;
554 LOG(INFO, "Changing bottom offset by %f\n", doffset);
555}
556
557void LimitClawGoal(double *bottom_goal, double *top_goal,
Austin Schuh24957102015-11-28 16:04:40 -0800558 const constants::Values &values) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700559 // first update position based on angle limit
560 const double separation = *top_goal - *bottom_goal;
561 if (separation > values.claw.soft_max_separation) {
562 LOG_STRUCT(DEBUG, "before", ClawPositionToLog(*top_goal, *bottom_goal));
563 const double dsep = (separation - values.claw.soft_max_separation) / 2.0;
564 *bottom_goal += dsep;
565 *top_goal -= dsep;
566 LOG_STRUCT(DEBUG, "after", ClawPositionToLog(*top_goal, *bottom_goal));
567 }
568 if (separation < values.claw.soft_min_separation) {
569 LOG_STRUCT(DEBUG, "before", ClawPositionToLog(*top_goal, *bottom_goal));
570 const double dsep = (separation - values.claw.soft_min_separation) / 2.0;
571 *bottom_goal += dsep;
572 *top_goal -= dsep;
573 LOG_STRUCT(DEBUG, "after", ClawPositionToLog(*top_goal, *bottom_goal));
574 }
575
576 // now move both goals in unison
577 if (*bottom_goal < values.claw.lower_claw.lower_limit) {
578 LOG_STRUCT(DEBUG, "before", ClawPositionToLog(*top_goal, *bottom_goal));
579 *top_goal += values.claw.lower_claw.lower_limit - *bottom_goal;
580 *bottom_goal = values.claw.lower_claw.lower_limit;
581 LOG_STRUCT(DEBUG, "after", ClawPositionToLog(*top_goal, *bottom_goal));
582 }
583 if (*bottom_goal > values.claw.lower_claw.upper_limit) {
584 LOG_STRUCT(DEBUG, "before", ClawPositionToLog(*top_goal, *bottom_goal));
585 *top_goal -= *bottom_goal - values.claw.lower_claw.upper_limit;
586 *bottom_goal = values.claw.lower_claw.upper_limit;
587 LOG_STRUCT(DEBUG, "after", ClawPositionToLog(*top_goal, *bottom_goal));
588 }
589
590 if (*top_goal < values.claw.upper_claw.lower_limit) {
591 LOG_STRUCT(DEBUG, "before", ClawPositionToLog(*top_goal, *bottom_goal));
592 *bottom_goal += values.claw.upper_claw.lower_limit - *top_goal;
593 *top_goal = values.claw.upper_claw.lower_limit;
594 LOG_STRUCT(DEBUG, "after", ClawPositionToLog(*top_goal, *bottom_goal));
595 }
596 if (*top_goal > values.claw.upper_claw.upper_limit) {
597 LOG_STRUCT(DEBUG, "before", ClawPositionToLog(*top_goal, *bottom_goal));
598 *bottom_goal -= *top_goal - values.claw.upper_claw.upper_limit;
599 *top_goal = values.claw.upper_claw.upper_limit;
600 LOG_STRUCT(DEBUG, "after", ClawPositionToLog(*top_goal, *bottom_goal));
601 }
602}
603
604bool ClawMotor::is_ready() const {
605 return (
606 (top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED &&
607 bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) ||
Austin Schuheeec74a2019-01-27 20:58:59 -0800608 ((has_joystick_state() ? joystick_state().autonomous : true) &&
Brian Silverman17f503e2015-08-02 18:17:18 -0700609 ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
610 top_claw_.zeroing_state() ==
611 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
612 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
613 bottom_claw_.zeroing_state() ==
614 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION))));
615}
616
617bool ClawMotor::is_zeroing() const { return !is_ready(); }
618
619// Positive angle is up, and positive power is up.
Austin Schuh24957102015-11-28 16:04:40 -0800620void ClawMotor::RunIteration(
Brian Silvermanb601d892015-12-20 18:24:38 -0500621 const ::y2014::control_loops::ClawQueue::Goal *goal,
622 const ::y2014::control_loops::ClawQueue::Position *position,
623 ::y2014::control_loops::ClawQueue::Output *output,
624 ::y2014::control_loops::ClawQueue::Status *status) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700625 // Disable the motors now so that all early returns will return with the
626 // motors disabled.
627 if (output) {
628 output->top_claw_voltage = 0;
629 output->bottom_claw_voltage = 0;
630 output->intake_voltage = 0;
631 output->tusk_voltage = 0;
632 }
633
634 if (goal) {
635 if (::std::isnan(goal->bottom_angle) ||
636 ::std::isnan(goal->separation_angle) || ::std::isnan(goal->intake) ||
637 ::std::isnan(goal->centering)) {
638 return;
639 }
640 }
641
642 if (WasReset()) {
643 top_claw_.Reset(position->top);
644 bottom_claw_.Reset(position->bottom);
645 }
646
Austin Schuh24957102015-11-28 16:04:40 -0800647 const constants::Values &values = constants::GetValues();
Brian Silverman17f503e2015-08-02 18:17:18 -0700648
649 if (position) {
650 Eigen::Matrix<double, 2, 1> Y;
651 Y << position->bottom.position + bottom_claw_.offset(),
652 position->top.position + top_claw_.offset();
653 claw_.Correct(Y);
654
655 top_claw_.SetPositionValues(position->top);
656 bottom_claw_.SetPositionValues(position->bottom);
657
658 if (!has_top_claw_goal_) {
659 has_top_claw_goal_ = true;
660 top_claw_goal_ = top_claw_.absolute_position();
661 initial_separation_ =
662 top_claw_.absolute_position() - bottom_claw_.absolute_position();
663 }
664 if (!has_bottom_claw_goal_) {
665 has_bottom_claw_goal_ = true;
666 bottom_claw_goal_ = bottom_claw_.absolute_position();
667 initial_separation_ =
668 top_claw_.absolute_position() - bottom_claw_.absolute_position();
669 }
670 LOG_STRUCT(DEBUG, "absolute position",
671 ClawPositionToLog(top_claw_.absolute_position(),
672 bottom_claw_.absolute_position()));
673 }
674
675 bool autonomous, enabled;
Austin Schuheeec74a2019-01-27 20:58:59 -0800676 if (has_joystick_state()) {
677 autonomous = joystick_state().autonomous;
678 enabled = joystick_state().enabled;
679 } else {
Brian Silverman17f503e2015-08-02 18:17:18 -0700680 autonomous = true;
681 enabled = false;
Brian Silverman17f503e2015-08-02 18:17:18 -0700682 }
683
684 double bottom_claw_velocity_ = 0.0;
685 double top_claw_velocity_ = 0.0;
686
687 if (goal != NULL &&
688 ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED &&
689 bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) ||
690 (autonomous &&
691 ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
692 top_claw_.zeroing_state() ==
693 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
694 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
695 bottom_claw_.zeroing_state() ==
696 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION))))) {
697 // Ready to use the claw.
698 // Limit the goals here.
699 bottom_claw_goal_ = goal->bottom_angle;
700 top_claw_goal_ = goal->bottom_angle + goal->separation_angle;
701 has_bottom_claw_goal_ = true;
702 has_top_claw_goal_ = true;
703 doing_calibration_fine_tune_ = false;
704 mode_ = READY;
705
706 bottom_claw_.HandleCalibrationError(values.claw.lower_claw);
707 top_claw_.HandleCalibrationError(values.claw.upper_claw);
708 } else if (top_claw_.zeroing_state() !=
709 ZeroedStateFeedbackLoop::UNKNOWN_POSITION &&
710 bottom_claw_.zeroing_state() !=
711 ZeroedStateFeedbackLoop::UNKNOWN_POSITION) {
712 // Time to fine tune the zero.
713 // Limit the goals here.
714 if (!enabled) {
715 // If we are disabled, start the fine tune process over again.
716 doing_calibration_fine_tune_ = false;
717 }
718 if (bottom_claw_.zeroing_state() != ZeroedStateFeedbackLoop::CALIBRATED) {
719 // always get the bottom claw to calibrated first
720 LOG(DEBUG, "Calibrating the bottom of the claw\n");
721 if (!doing_calibration_fine_tune_) {
722 if (::std::abs(bottom_absolute_position() -
723 values.claw.start_fine_tune_pos) <
724 values.claw.claw_unimportant_epsilon) {
725 doing_calibration_fine_tune_ = true;
Austin Schuh0e997732015-11-08 15:14:53 -0800726 bottom_claw_goal_ += values.claw.claw_zeroing_speed * kDt;
Brian Silverman17f503e2015-08-02 18:17:18 -0700727 top_claw_velocity_ = bottom_claw_velocity_ =
728 values.claw.claw_zeroing_speed;
729 LOG(DEBUG, "Ready to fine tune the bottom\n");
730 mode_ = FINE_TUNE_BOTTOM;
731 } else {
732 // send bottom to zeroing start
733 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
734 LOG(DEBUG, "Going to the start position for the bottom\n");
735 mode_ = PREP_FINE_TUNE_BOTTOM;
736 }
737 } else {
738 mode_ = FINE_TUNE_BOTTOM;
Austin Schuh0e997732015-11-08 15:14:53 -0800739 bottom_claw_goal_ += values.claw.claw_zeroing_speed * kDt;
Brian Silverman17f503e2015-08-02 18:17:18 -0700740 top_claw_velocity_ = bottom_claw_velocity_ =
741 values.claw.claw_zeroing_speed;
742 if (top_claw_.front_or_back_triggered() ||
743 bottom_claw_.front_or_back_triggered()) {
744 // We shouldn't hit a limit, but if we do, go back to the zeroing
745 // point and try again.
746 doing_calibration_fine_tune_ = false;
747 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
748 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
749 LOG(DEBUG, "Found a limit, starting over.\n");
750 mode_ = PREP_FINE_TUNE_BOTTOM;
751 }
752
753 if (position && bottom_claw_.SawFilteredPosedge(
754 bottom_claw_.calibration(), bottom_claw_.front(),
755 bottom_claw_.back())) {
756 // do calibration
757 bottom_claw_.SetCalibration(
Brian Silvermand3efb182015-05-13 23:04:29 -0400758 position->bottom.calibration.posedge_value,
Brian Silverman17f503e2015-08-02 18:17:18 -0700759 values.claw.lower_claw.calibration.lower_angle);
760 bottom_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED);
761 // calibrated so we are done fine tuning bottom
762 doing_calibration_fine_tune_ = false;
763 LOG(DEBUG, "Calibrated the bottom correctly!\n");
764 } else if (bottom_claw_.calibration().last_value()) {
765 LOG(DEBUG, "Aborting bottom fine tune because sensor triggered\n");
766 doing_calibration_fine_tune_ = false;
767 bottom_claw_.set_zeroing_state(
768 ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
769 } else {
770 LOG(DEBUG, "Fine tuning\n");
771 }
772 }
773 // now set the top claw to track
774
775 top_claw_goal_ = bottom_claw_goal_ + values.claw.claw_zeroing_separation;
776 } else {
777 // bottom claw must be calibrated, start on the top
778 if (!doing_calibration_fine_tune_) {
779 if (::std::abs(top_absolute_position() -
780 values.claw.start_fine_tune_pos) <
781 values.claw.claw_unimportant_epsilon) {
782 doing_calibration_fine_tune_ = true;
Austin Schuh0e997732015-11-08 15:14:53 -0800783 top_claw_goal_ += values.claw.claw_zeroing_speed * kDt;
Brian Silverman17f503e2015-08-02 18:17:18 -0700784 top_claw_velocity_ = bottom_claw_velocity_ =
785 values.claw.claw_zeroing_speed;
786 LOG(DEBUG, "Ready to fine tune the top\n");
787 mode_ = FINE_TUNE_TOP;
788 } else {
789 // send top to zeroing start
790 top_claw_goal_ = values.claw.start_fine_tune_pos;
791 LOG(DEBUG, "Going to the start position for the top\n");
792 mode_ = PREP_FINE_TUNE_TOP;
793 }
794 } else {
795 mode_ = FINE_TUNE_TOP;
Austin Schuh0e997732015-11-08 15:14:53 -0800796 top_claw_goal_ += values.claw.claw_zeroing_speed * kDt;
Brian Silverman17f503e2015-08-02 18:17:18 -0700797 top_claw_velocity_ = bottom_claw_velocity_ =
798 values.claw.claw_zeroing_speed;
799 if (top_claw_.front_or_back_triggered() ||
800 bottom_claw_.front_or_back_triggered()) {
801 // this should not happen, but now we know it won't
802 doing_calibration_fine_tune_ = false;
803 top_claw_goal_ = values.claw.start_fine_tune_pos;
804 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
805 LOG(DEBUG, "Found a limit, starting over.\n");
806 mode_ = PREP_FINE_TUNE_TOP;
807 }
808
809 if (position &&
810 top_claw_.SawFilteredPosedge(top_claw_.calibration(),
811 top_claw_.front(), top_claw_.back())) {
812 // do calibration
813 top_claw_.SetCalibration(
Brian Silvermand3efb182015-05-13 23:04:29 -0400814 position->top.calibration.posedge_value,
Brian Silverman17f503e2015-08-02 18:17:18 -0700815 values.claw.upper_claw.calibration.lower_angle);
816 top_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED);
817 // calibrated so we are done fine tuning top
818 doing_calibration_fine_tune_ = false;
819 LOG(DEBUG, "Calibrated the top correctly!\n");
820 } else if (top_claw_.calibration().last_value()) {
821 LOG(DEBUG, "Aborting top fine tune because sensor triggered\n");
822 doing_calibration_fine_tune_ = false;
823 top_claw_.set_zeroing_state(
824 ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
825 }
826 }
827 // now set the bottom claw to track
828 bottom_claw_goal_ = top_claw_goal_ - values.claw.claw_zeroing_separation;
829 }
830 } else {
831 doing_calibration_fine_tune_ = false;
832 if (!was_enabled_ && enabled) {
833 if (position) {
834 top_claw_goal_ = position->top.position + top_claw_.offset();
835 bottom_claw_goal_ = position->bottom.position + bottom_claw_.offset();
836 initial_separation_ =
837 position->top.position - position->bottom.position;
838 } else {
839 has_top_claw_goal_ = false;
840 has_bottom_claw_goal_ = false;
841 }
842 }
843
844 if ((bottom_claw_.zeroing_state() !=
845 ZeroedStateFeedbackLoop::UNKNOWN_POSITION ||
846 bottom_claw_.front().value() || top_claw_.front().value()) &&
847 !top_claw_.back().value() && !bottom_claw_.back().value()) {
848 if (enabled) {
849 // Time to slowly move back up to find any position to narrow down the
850 // zero.
Austin Schuh0e997732015-11-08 15:14:53 -0800851 top_claw_goal_ += values.claw.claw_zeroing_off_speed * kDt;
852 bottom_claw_goal_ += values.claw.claw_zeroing_off_speed * kDt;
Brian Silverman17f503e2015-08-02 18:17:18 -0700853 top_claw_velocity_ = bottom_claw_velocity_ =
854 values.claw.claw_zeroing_off_speed;
855 LOG(DEBUG, "Bottom is known.\n");
856 }
857 } else {
858 // We don't know where either claw is. Slowly start moving down to find
859 // any hall effect.
860 if (enabled) {
Austin Schuh0e997732015-11-08 15:14:53 -0800861 top_claw_goal_ -= values.claw.claw_zeroing_off_speed * kDt;
862 bottom_claw_goal_ -= values.claw.claw_zeroing_off_speed * kDt;
Brian Silverman17f503e2015-08-02 18:17:18 -0700863 top_claw_velocity_ = bottom_claw_velocity_ =
864 -values.claw.claw_zeroing_off_speed;
865 LOG(DEBUG, "Both are unknown.\n");
866 }
867 }
868
869 if (position) {
870 if (enabled) {
871 top_claw_.SetCalibrationOnEdge(
872 values.claw.upper_claw,
873 ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION);
874 bottom_claw_.SetCalibrationOnEdge(
875 values.claw.lower_claw,
876 ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION);
877 } else {
878 // TODO(austin): Only calibrate on the predetermined edge.
879 // We might be able to just ignore this since the backlash is soooo
880 // low.
881 // :)
882 top_claw_.SetCalibrationOnEdge(
883 values.claw.upper_claw,
884 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
885 bottom_claw_.SetCalibrationOnEdge(
886 values.claw.lower_claw,
887 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
888 }
889 }
890 mode_ = UNKNOWN_LOCATION;
891 }
892
893 // Limit the goals if both claws have been (mostly) found.
894 if (mode_ != UNKNOWN_LOCATION) {
895 LimitClawGoal(&bottom_claw_goal_, &top_claw_goal_, values);
896 }
897
898 claw_.set_positions_known(
899 top_claw_.zeroing_state() != ZeroedStateFeedbackLoop::UNKNOWN_POSITION,
900 bottom_claw_.zeroing_state() !=
901 ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
902 if (has_top_claw_goal_ && has_bottom_claw_goal_) {
903 claw_.mutable_R() << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_,
904 bottom_claw_velocity_, top_claw_velocity_ - bottom_claw_velocity_;
905 LOG_MATRIX(DEBUG, "actual goal", claw_.R());
906
907 // Only cap power when one of the halves of the claw is moving slowly and
908 // could wind up.
909 claw_.set_is_zeroing(mode_ == UNKNOWN_LOCATION || mode_ == FINE_TUNE_TOP ||
910 mode_ == FINE_TUNE_BOTTOM);
911 claw_.Update(output == nullptr);
912 } else {
913 claw_.Update(true);
914 }
915
916 capped_goal_ = false;
917 switch (mode_) {
918 case READY:
919 case PREP_FINE_TUNE_TOP:
920 case PREP_FINE_TUNE_BOTTOM:
921 break;
922 case FINE_TUNE_BOTTOM:
923 case FINE_TUNE_TOP:
924 case UNKNOWN_LOCATION: {
925 if (claw_.uncapped_average_voltage() > values.claw.max_zeroing_voltage) {
Austin Schuh32501832017-02-25 18:32:56 -0800926 double dx_bot =
927 (claw_.U_uncapped(0, 0) - values.claw.max_zeroing_voltage) /
928 claw_.controller().K(0, 0);
929 double dx_top =
930 (claw_.U_uncapped(1, 0) - values.claw.max_zeroing_voltage) /
931 claw_.controller().K(0, 0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700932 double dx = ::std::max(dx_top, dx_bot);
933 bottom_claw_goal_ -= dx;
934 top_claw_goal_ -= dx;
935 Eigen::Matrix<double, 4, 1> R;
936 R << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_, claw_.R(2, 0),
937 claw_.R(3, 0);
Austin Schuh32501832017-02-25 18:32:56 -0800938 claw_.mutable_U() = claw_.controller().K() * (R - claw_.X_hat());
Brian Silverman17f503e2015-08-02 18:17:18 -0700939 capped_goal_ = true;
940 LOG(DEBUG, "Moving the goal by %f to prevent windup."
941 " Uncapped is %f, max is %f, difference is %f\n",
942 dx,
943 claw_.uncapped_average_voltage(), values.claw.max_zeroing_voltage,
944 (claw_.uncapped_average_voltage() -
945 values.claw.max_zeroing_voltage));
946 } else if (claw_.uncapped_average_voltage() <
947 -values.claw.max_zeroing_voltage) {
Austin Schuh32501832017-02-25 18:32:56 -0800948 double dx_bot =
949 (claw_.U_uncapped(0, 0) + values.claw.max_zeroing_voltage) /
950 claw_.controller().K(0, 0);
951 double dx_top =
952 (claw_.U_uncapped(1, 0) + values.claw.max_zeroing_voltage) /
953 claw_.controller().K(0, 0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700954 double dx = ::std::min(dx_top, dx_bot);
955 bottom_claw_goal_ -= dx;
956 top_claw_goal_ -= dx;
957 Eigen::Matrix<double, 4, 1> R;
958 R << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_, claw_.R(2, 0),
959 claw_.R(3, 0);
Austin Schuh32501832017-02-25 18:32:56 -0800960 claw_.mutable_U() = claw_.controller().K() * (R - claw_.X_hat());
Brian Silverman17f503e2015-08-02 18:17:18 -0700961 capped_goal_ = true;
962 LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx);
963 }
964 } break;
965 }
966
967 if (output) {
968 if (goal) {
969 //setup the intake
970 output->intake_voltage =
971 (goal->intake > 12.0) ? 12 : (goal->intake < -12.0) ? -12.0
972 : goal->intake;
973 output->tusk_voltage = goal->centering;
974 output->tusk_voltage =
975 (goal->centering > 12.0) ? 12 : (goal->centering < -12.0)
976 ? -12.0
977 : goal->centering;
978 }
979 output->top_claw_voltage = claw_.U(1, 0);
980 output->bottom_claw_voltage = claw_.U(0, 0);
981
982 if (output->top_claw_voltage > kMaxVoltage) {
983 output->top_claw_voltage = kMaxVoltage;
984 } else if (output->top_claw_voltage < -kMaxVoltage) {
985 output->top_claw_voltage = -kMaxVoltage;
986 }
987
988 if (output->bottom_claw_voltage > kMaxVoltage) {
989 output->bottom_claw_voltage = kMaxVoltage;
990 } else if (output->bottom_claw_voltage < -kMaxVoltage) {
991 output->bottom_claw_voltage = -kMaxVoltage;
992 }
993 }
994
995 status->bottom = bottom_absolute_position();
996 status->separation = top_absolute_position() - bottom_absolute_position();
997 status->bottom_velocity = claw_.X_hat(2, 0);
998 status->separation_velocity = claw_.X_hat(3, 0);
999
1000 if (goal) {
1001 bool bottom_done =
1002 ::std::abs(bottom_absolute_position() - goal->bottom_angle) < 0.020;
1003 bool bottom_velocity_done = ::std::abs(status->bottom_velocity) < 0.2;
1004 bool separation_done =
1005 ::std::abs((top_absolute_position() - bottom_absolute_position()) -
1006 goal->separation_angle) < 0.020;
1007 bool separation_done_with_ball =
1008 ::std::abs((top_absolute_position() - bottom_absolute_position()) -
1009 goal->separation_angle) < 0.06;
1010 status->done = is_ready() && separation_done && bottom_done && bottom_velocity_done;
1011 status->done_with_ball =
1012 is_ready() && separation_done_with_ball && bottom_done && bottom_velocity_done;
1013 } else {
1014 status->done = status->done_with_ball = false;
1015 }
1016
1017 status->zeroed = is_ready();
1018 status->zeroed_for_auto =
1019 (top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
1020 top_claw_.zeroing_state() ==
1021 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
1022 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
1023 bottom_claw_.zeroing_state() ==
1024 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
1025
1026 was_enabled_ = enabled;
1027}
1028
1029} // namespace control_loops
Austin Schuh24957102015-11-28 16:04:40 -08001030} // namespace y2014