blob: 84939a9d2f698150e940d0197f3a345206e45933 [file] [log] [blame]
Austin Schuh3bb9a442014-02-02 16:01:45 -08001#include "frc971/control_loops/claw/claw.h"
2
Austin Schuh3bb9a442014-02-02 16:01:45 -08003#include <algorithm>
4
Briana6553ed2014-04-02 21:26:46 -07005#include "aos/common/controls/control_loops.q.h"
Austin Schuh3bb9a442014-02-02 16:01:45 -08006#include "aos/common/logging/logging.h"
Brian Silvermanf48fab32014-03-09 14:32:24 -07007#include "aos/common/logging/queue_logging.h"
Brian Silverman6dd2c532014-03-29 23:34:39 -07008#include "aos/common/logging/matrix_logging.h"
Brian Silvermanad9e0002014-04-13 14:55:57 -07009#include "aos/common/commonmath.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;
Brian Silverman084372e2014-04-10 10:55:53 -070052const double kRezeroThreshold = 0.03;
Austin Schuhf84a1302014-02-19 00:23:30 -080053
Austin Schuh27b8fb12014-02-22 15:10:05 -080054ClawLimitedLoop::ClawLimitedLoop(StateFeedbackLoop<4, 2, 2> loop)
55 : StateFeedbackLoop<4, 2, 2>(loop),
56 uncapped_average_voltage_(0.0),
James Kuszmaulf63b0ae2014-03-25 16:52:11 -070057 is_zeroing_(true),
58 U_Poly_((Eigen::Matrix<double, 4, 2>() << 1, 0,
59 -1, 0,
60 0, 1,
61 0, -1).finished(),
62 (Eigen::Matrix<double, 4, 1>() << kMaxVoltage, kMaxVoltage,
Brian Silverman6dd2c532014-03-29 23:34:39 -070063 kMaxVoltage, kMaxVoltage).finished()),
64 U_Poly_zeroing_((Eigen::Matrix<double, 4, 2>() << 1, 0,
65 -1, 0,
66 0, 1,
67 0, -1).finished(),
68 (Eigen::Matrix<double, 4, 1>() <<
69 kZeroingVoltage, kZeroingVoltage,
70 kZeroingVoltage, kZeroingVoltage).finished()) {
James Kuszmaulf63b0ae2014-03-25 16:52:11 -070071 ::aos::controls::HPolytope<0>::Init();
72}
Austin Schuh27b8fb12014-02-22 15:10:05 -080073
James Kuszmaulf63b0ae2014-03-25 16:52:11 -070074// Caps the voltage prioritizing reducing velocity error over reducing
75// positional error.
76// Uses the polytope libararies which we used to just use for the drivetrain.
77// Uses a region representing the maximum voltage and then transforms it such
78// that the points represent different amounts of positional error and
79// constrains the region such that, if at all possible, it will maintain its
80// current efforts to reduce velocity error.
Austin Schuhcda86af2014-02-16 16:16:39 -080081void ClawLimitedLoop::CapU() {
Brian Silvermanad9e0002014-04-13 14:55:57 -070082 const Eigen::Matrix<double, 4, 1> error = R - X_hat;
Austin Schuh4cb047f2014-02-16 21:10:19 -080083
James Kuszmauld536a402014-02-18 22:32:12 -080084 double u_top = U(1, 0);
85 double u_bottom = U(0, 0);
Austin Schuhcda86af2014-02-16 16:16:39 -080086
James Kuszmaulf63b0ae2014-03-25 16:52:11 -070087 uncapped_average_voltage_ = (u_top + u_bottom) / 2;
88
89 double max_voltage = is_zeroing_ ? kZeroingVoltage : kMaxVoltage;
90
Brian Silvermanad9e0002014-04-13 14:55:57 -070091 if (::std::abs(u_bottom) > max_voltage || ::std::abs(u_top) > max_voltage) {
Brian Silverman6dd2c532014-03-29 23:34:39 -070092 LOG_MATRIX(DEBUG, "U at start", U);
James Kuszmaulf63b0ae2014-03-25 16:52:11 -070093 // H * U <= k
94 // U = UPos + UVel
95 // H * (UPos + UVel) <= k
96 // H * UPos <= k - H * UVel
97
98 // Now, we can do a coordinate transformation and say the following.
99
100 // UPos = position_K * position_error
101 // (H * position_K) * position_error <= k - H * UVel
102
103 Eigen::Matrix<double, 2, 2> position_K;
104 position_K << K(0, 0), K(0, 1),
105 K(1, 0), K(1, 1);
106 Eigen::Matrix<double, 2, 2> velocity_K;
107 velocity_K << K(0, 2), K(0, 3),
108 K(1, 2), K(1, 3);
109
110 Eigen::Matrix<double, 2, 1> position_error;
111 position_error << error(0, 0), error(1, 0);
112 Eigen::Matrix<double, 2, 1> velocity_error;
113 velocity_error << error(2, 0), error(3, 0);
Brian Silverman6dd2c532014-03-29 23:34:39 -0700114 LOG_MATRIX(DEBUG, "error", error);
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700115
Brian Silverman6dd2c532014-03-29 23:34:39 -0700116 const auto &poly = is_zeroing_ ? U_Poly_zeroing_ : U_Poly_;
117 const Eigen::Matrix<double, 4, 2> pos_poly_H = poly.H() * position_K;
118 const Eigen::Matrix<double, 4, 1> pos_poly_k =
119 poly.k() - poly.H() * velocity_K * velocity_error;
120 const ::aos::controls::HPolytope<2> pos_poly(pos_poly_H, pos_poly_k);
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700121
Brian Silverman6dd2c532014-03-29 23:34:39 -0700122 Eigen::Matrix<double, 2, 1> adjusted_pos_error;
123 {
124 const auto &P = position_error;
Brian Silvermanb087c0a2014-03-30 12:59:52 -0700125
126 // This line was at 45 degrees but is now at some angle steeper than the
127 // straight one between the points.
128 Eigen::Matrix<double, 1, 2> angle_45;
129 // If the top claw is above its soft upper limit, make the line actually
130 // 45 degrees to avoid smashing it into the limit in an attempt to fix the
131 // separation error faster than the bottom position one.
132 if (X_hat(0, 0) + X_hat(1, 0) >
133 constants::GetValues().claw.upper_claw.upper_limit) {
134 angle_45 << 1, 1;
135 } else {
136 // Fixing separation error half as fast as positional error works well
137 // because it means they both close evenly.
138 angle_45 << ::std::sqrt(3), 1;
139 }
140 Eigen::Matrix<double, 1, 2> L45_quadrant;
Brian Silvermanad9e0002014-04-13 14:55:57 -0700141 L45_quadrant << ::aos::sign(P(1, 0)), -::aos::sign(P(0, 0));
Brian Silvermanb087c0a2014-03-30 12:59:52 -0700142 const auto L45 = L45_quadrant.cwiseProduct(angle_45);
Brian Silverman6dd2c532014-03-29 23:34:39 -0700143 const double w45 = 0;
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700144
Brian Silverman6dd2c532014-03-29 23:34:39 -0700145 Eigen::Matrix<double, 1, 2> LH;
146 if (::std::abs(P(0, 0)) > ::std::abs(P(1, 0))) {
147 LH << 0, 1;
148 } else {
149 LH << 1, 0;
150 }
151 const double wh = LH.dot(P);
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700152
Brian Silverman6dd2c532014-03-29 23:34:39 -0700153 Eigen::Matrix<double, 2, 2> standard;
154 standard << L45, LH;
155 Eigen::Matrix<double, 2, 1> W;
156 W << w45, wh;
157 const Eigen::Matrix<double, 2, 1> intersection = standard.inverse() * W;
158
159 bool is_inside_h;
160 const auto adjusted_pos_error_h =
161 DoCoerceGoal(pos_poly, LH, wh, position_error, &is_inside_h);
162 const auto adjusted_pos_error_45 =
163 DoCoerceGoal(pos_poly, L45, w45, intersection, nullptr);
164 if (pos_poly.IsInside(intersection)) {
165 adjusted_pos_error = adjusted_pos_error_h;
166 } else {
167 if (is_inside_h) {
168 if (adjusted_pos_error_h.norm() > adjusted_pos_error_45.norm()) {
169 adjusted_pos_error = adjusted_pos_error_h;
170 } else {
171 adjusted_pos_error = adjusted_pos_error_45;
172 }
173 } else {
174 adjusted_pos_error = adjusted_pos_error_45;
175 }
176 }
177 }
178
179 LOG_MATRIX(DEBUG, "adjusted_pos_error", adjusted_pos_error);
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700180 U = velocity_K * velocity_error + position_K * adjusted_pos_error;
Brian Silverman6dd2c532014-03-29 23:34:39 -0700181 LOG_MATRIX(DEBUG, "U is now", U);
Brian Silverman7b7c9072014-03-30 13:33:30 -0700182
183 {
184 const auto values = constants::GetValues().claw;
Brian Silvermaned9df2f2014-04-05 07:07:15 -0700185 if (top_known_) {
186 if (X_hat(0, 0) + X_hat(1, 0) > values.upper_claw.upper_limit &&
187 U(1, 0) > 0) {
188 LOG(WARNING, "upper claw too high and moving up\n");
189 U(1, 0) = 0;
190 } else if (X_hat(0, 0) + X_hat(1, 0) < values.upper_claw.lower_limit &&
191 U(1, 0) < 0) {
192 LOG(WARNING, "upper claw too low and moving down\n");
193 U(1, 0) = 0;
194 }
Brian Silverman7b7c9072014-03-30 13:33:30 -0700195 }
Brian Silvermaned9df2f2014-04-05 07:07:15 -0700196 if (bottom_known_) {
197 if (X_hat(0, 0) > values.lower_claw.upper_limit && U(0, 0) > 0) {
198 LOG(WARNING, "lower claw too high and moving up\n");
199 U(0, 0) = 0;
200 } else if (X_hat(0, 0) < values.lower_claw.lower_limit && U(0, 0) < 0) {
201 LOG(WARNING, "lower claw too low and moving down\n");
202 U(0, 0) = 0;
203 }
Brian Silverman7b7c9072014-03-30 13:33:30 -0700204 }
205 }
James Kuszmauld536a402014-02-18 22:32:12 -0800206 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800207}
208
Austin Schuh27b8fb12014-02-22 15:10:05 -0800209ZeroedStateFeedbackLoop::ZeroedStateFeedbackLoop(const char *name,
210 ClawMotor *motor)
211 : offset_(0.0),
212 name_(name),
213 motor_(motor),
214 zeroing_state_(UNKNOWN_POSITION),
215 posedge_value_(0.0),
216 negedge_value_(0.0),
217 encoder_(0.0),
218 last_encoder_(0.0) {}
219
220void ZeroedStateFeedbackLoop::SetPositionValues(const HalfClawPosition &claw) {
221 front_.Update(claw.front);
222 calibration_.Update(claw.calibration);
223 back_.Update(claw.back);
224
225 bool any_sensor_triggered = any_triggered();
226 if (any_sensor_triggered && any_triggered_last_) {
227 // We are still on the hall effect and nothing has changed.
228 min_hall_effect_on_angle_ =
229 ::std::min(min_hall_effect_on_angle_, claw.position);
230 max_hall_effect_on_angle_ =
231 ::std::max(max_hall_effect_on_angle_, claw.position);
232 } else if (!any_sensor_triggered && !any_triggered_last_) {
233 // We are still off the hall effect and nothing has changed.
234 min_hall_effect_off_angle_ =
235 ::std::min(min_hall_effect_off_angle_, claw.position);
236 max_hall_effect_off_angle_ =
237 ::std::max(max_hall_effect_off_angle_, claw.position);
238 } else if (any_sensor_triggered && !any_triggered_last_) {
239 // Saw a posedge on the hall effect. Reset the limits.
240 min_hall_effect_on_angle_ = ::std::min(claw.posedge_value, claw.position);
241 max_hall_effect_on_angle_ = ::std::max(claw.posedge_value, claw.position);
242 } else if (!any_sensor_triggered && any_triggered_last_) {
243 // Saw a negedge on the hall effect. Reset the limits.
244 min_hall_effect_off_angle_ = ::std::min(claw.negedge_value, claw.position);
245 max_hall_effect_off_angle_ = ::std::max(claw.negedge_value, claw.position);
246 }
247
248 posedge_value_ = claw.posedge_value;
249 negedge_value_ = claw.negedge_value;
250 last_encoder_ = encoder_;
251 if (front().value() || calibration().value() || back().value()) {
252 last_on_encoder_ = encoder_;
253 } else {
254 last_off_encoder_ = encoder_;
255 }
256 encoder_ = claw.position;
257 any_triggered_last_ = any_sensor_triggered;
258}
259
260void ZeroedStateFeedbackLoop::Reset(const HalfClawPosition &claw) {
261 set_zeroing_state(ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
262
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000263 front_.Reset(claw.front);
264 calibration_.Reset(claw.calibration);
265 back_.Reset(claw.back);
Austin Schuh27b8fb12014-02-22 15:10:05 -0800266 // close up the min and max edge positions as they are no longer valid and
267 // will be expanded in future iterations
268 min_hall_effect_on_angle_ = claw.position;
269 max_hall_effect_on_angle_ = claw.position;
270 min_hall_effect_off_angle_ = claw.position;
271 max_hall_effect_off_angle_ = claw.position;
272 any_triggered_last_ = any_triggered();
273}
274
275bool TopZeroedStateFeedbackLoop::SetCalibrationOnEdge(
276 const constants::Values::Claws::Claw &claw_values,
277 JointZeroingState zeroing_state) {
278 double edge_encoder;
279 double edge_angle;
280 if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) {
281 LOG(INFO, "Calibration edge edge should be %f.\n", edge_angle);
282 SetCalibration(edge_encoder, edge_angle);
283 set_zeroing_state(zeroing_state);
284 return true;
285 }
286 return false;
287}
288
Brian Silverman084372e2014-04-10 10:55:53 -0700289void TopZeroedStateFeedbackLoop::HandleCalibrationError(
290 const constants::Values::Claws::Claw &claw_values) {
291 double edge_encoder;
292 double edge_angle;
293 if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) {
294 const double calibration_error =
295 ComputeCalibrationChange(edge_encoder, edge_angle);
296 LOG(INFO, "Top calibration error is %f\n", calibration_error);
297 if (::std::abs(calibration_error) > kRezeroThreshold) {
Brian Silvermane4c701d2014-04-10 19:29:25 -0700298 LOG(WARNING, "rezeroing top\n");
Brian Silverman084372e2014-04-10 10:55:53 -0700299 SetCalibration(edge_encoder, edge_angle);
300 set_zeroing_state(ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION);
301 }
302 }
303}
304
305
306void BottomZeroedStateFeedbackLoop::HandleCalibrationError(
307 const constants::Values::Claws::Claw &claw_values) {
308 double edge_encoder;
309 double edge_angle;
310 if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) {
311 const double calibration_error =
312 ComputeCalibrationChange(edge_encoder, edge_angle);
313 LOG(INFO, "Bottom calibration error is %f\n", calibration_error);
314 if (::std::abs(calibration_error) > kRezeroThreshold) {
Brian Silvermane4c701d2014-04-10 19:29:25 -0700315 LOG(WARNING, "rezeroing bottom\n");
Brian Silverman084372e2014-04-10 10:55:53 -0700316 SetCalibration(edge_encoder, edge_angle);
317 set_zeroing_state(ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION);
318 }
319 }
320}
321
Austin Schuh27b8fb12014-02-22 15:10:05 -0800322bool BottomZeroedStateFeedbackLoop::SetCalibrationOnEdge(
323 const constants::Values::Claws::Claw &claw_values,
324 JointZeroingState zeroing_state) {
325 double edge_encoder;
326 double edge_angle;
327 if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) {
328 LOG(INFO, "Calibration edge.\n");
329 SetCalibration(edge_encoder, edge_angle);
330 set_zeroing_state(zeroing_state);
331 return true;
332 }
333 return false;
334}
335
Austin Schuhcc0bf312014-02-09 00:39:29 -0800336ClawMotor::ClawMotor(control_loops::ClawGroup *my_claw)
Brian Silverman38111502014-04-10 12:36:26 -0700337 : aos::controls::ControlLoop<control_loops::ClawGroup, true, true,
Brian Silverman71fbee02014-03-13 17:24:54 -0700338 false>(my_claw),
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800339 has_top_claw_goal_(false),
340 top_claw_goal_(0.0),
Austin Schuhcda86af2014-02-16 16:16:39 -0800341 top_claw_(this),
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800342 has_bottom_claw_goal_(false),
343 bottom_claw_goal_(0.0),
Austin Schuhcda86af2014-02-16 16:16:39 -0800344 bottom_claw_(this),
345 claw_(MakeClawLoop()),
Ben Fredrickson9b388422014-02-13 06:15:31 +0000346 was_enabled_(false),
Austin Schuh4cb047f2014-02-16 21:10:19 -0800347 doing_calibration_fine_tune_(false),
Austin Schuhe7f90d12014-02-17 00:48:25 -0800348 capped_goal_(false),
349 mode_(UNKNOWN_LOCATION) {}
Austin Schuh3bb9a442014-02-02 16:01:45 -0800350
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800351const int ZeroedStateFeedbackLoop::kZeroingMaxVoltage;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800352
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000353bool ZeroedStateFeedbackLoop::SawFilteredPosedge(
354 const HallEffectTracker &this_sensor, const HallEffectTracker &sensorA,
355 const HallEffectTracker &sensorB) {
356 if (posedge_filter_ == nullptr && this_sensor.posedge_count_changed() &&
357 !sensorA.posedge_count_changed() && !sensorB.posedge_count_changed() &&
358 this_sensor.value() && !this_sensor.last_value()) {
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000359 posedge_filter_ = &this_sensor;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000360 } else if (posedge_filter_ == &this_sensor &&
361 !this_sensor.posedge_count_changed() &&
362 !sensorA.posedge_count_changed() &&
363 !sensorB.posedge_count_changed() && this_sensor.value()) {
364 posedge_filter_ = nullptr;
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000365 return true;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000366 } else if (posedge_filter_ == &this_sensor) {
367 posedge_filter_ = nullptr;
368 }
369 return false;
370}
371
372bool ZeroedStateFeedbackLoop::SawFilteredNegedge(
373 const HallEffectTracker &this_sensor, const HallEffectTracker &sensorA,
374 const HallEffectTracker &sensorB) {
375 if (negedge_filter_ == nullptr && this_sensor.negedge_count_changed() &&
376 !sensorA.negedge_count_changed() && !sensorB.negedge_count_changed() &&
377 !this_sensor.value() && this_sensor.last_value()) {
378 negedge_filter_ = &this_sensor;
379 } else if (negedge_filter_ == &this_sensor &&
380 !this_sensor.negedge_count_changed() &&
381 !sensorA.negedge_count_changed() &&
382 !sensorB.negedge_count_changed() && !this_sensor.value()) {
383 negedge_filter_ = nullptr;
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000384 return true;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000385 } else if (negedge_filter_ == &this_sensor) {
386 negedge_filter_ = nullptr;
387 }
388 return false;
389}
390
Brian Silvermane0a95462014-02-17 00:41:09 -0800391bool ZeroedStateFeedbackLoop::DoGetPositionOfEdge(
392 const constants::Values::Claws::AnglePair &angles, double *edge_encoder,
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000393 double *edge_angle, const HallEffectTracker &this_sensor,
394 const HallEffectTracker &sensorA, const HallEffectTracker &sensorB,
Brian Silvermane0a95462014-02-17 00:41:09 -0800395 const char *hall_effect_name) {
Austin Schuhf84a1302014-02-19 00:23:30 -0800396 bool found_edge = false;
Austin Schuh27b8fb12014-02-22 15:10:05 -0800397
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000398 if (SawFilteredPosedge(this_sensor, sensorA, sensorB)) {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800399 if (min_hall_effect_off_angle_ == max_hall_effect_off_angle_) {
Brian Silvermanf48fab32014-03-09 14:32:24 -0700400 LOG(WARNING, "%s: Uncertain which side, rejecting posedge\n", name_);
Brian Silvermane0a95462014-02-17 00:41:09 -0800401 } else {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800402 const double average_last_encoder =
403 (min_hall_effect_off_angle_ + max_hall_effect_off_angle_) / 2.0;
404 if (posedge_value_ < average_last_encoder) {
405 *edge_angle = angles.upper_decreasing_angle;
406 LOG(INFO, "%s Posedge upper of %s -> %f posedge: %f avg_encoder: %f\n",
407 name_, hall_effect_name, *edge_angle, posedge_value_,
408 average_last_encoder);
409 } else {
410 *edge_angle = angles.lower_angle;
411 LOG(INFO, "%s Posedge lower of %s -> %f posedge: %f avg_encoder: %f\n",
412 name_, hall_effect_name, *edge_angle, posedge_value_,
413 average_last_encoder);
414 }
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000415 *edge_encoder = posedge_value_;
Austin Schuh27b8fb12014-02-22 15:10:05 -0800416 found_edge = true;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000417 }
418 }
419
420 if (SawFilteredNegedge(this_sensor, sensorA, sensorB)) {
421 if (min_hall_effect_on_angle_ == max_hall_effect_on_angle_) {
Brian Silvermanf48fab32014-03-09 14:32:24 -0700422 LOG(WARNING, "%s: Uncertain which side, rejecting negedge\n", name_);
Brian Silvermane0a95462014-02-17 00:41:09 -0800423 } else {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800424 const double average_last_encoder =
425 (min_hall_effect_on_angle_ + max_hall_effect_on_angle_) / 2.0;
426 if (negedge_value_ > average_last_encoder) {
427 *edge_angle = angles.upper_angle;
428 LOG(INFO, "%s Negedge upper of %s -> %f negedge: %f avg_encoder: %f\n",
429 name_, hall_effect_name, *edge_angle, negedge_value_,
430 average_last_encoder);
431 } else {
432 *edge_angle = angles.lower_decreasing_angle;
433 LOG(INFO, "%s Negedge lower of %s -> %f negedge: %f avg_encoder: %f\n",
434 name_, hall_effect_name, *edge_angle, negedge_value_,
435 average_last_encoder);
436 }
437 *edge_encoder = negedge_value_;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000438 found_edge = true;
Brian Silvermane0a95462014-02-17 00:41:09 -0800439 }
Austin Schuh27b8fb12014-02-22 15:10:05 -0800440 }
441
Austin Schuhf84a1302014-02-19 00:23:30 -0800442 return found_edge;
Brian Silvermane0a95462014-02-17 00:41:09 -0800443}
444
Austin Schuhf9286cd2014-02-11 00:51:09 -0800445bool ZeroedStateFeedbackLoop::GetPositionOfEdge(
Austin Schuhd27931c2014-02-16 19:18:20 -0800446 const constants::Values::Claws::Claw &claw_values, double *edge_encoder,
Austin Schuhf9286cd2014-02-11 00:51:09 -0800447 double *edge_angle) {
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000448 if (DoGetPositionOfEdge(claw_values.front, edge_encoder, edge_angle, front_,
449 calibration_, back_, "front")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800450 return true;
451 }
Brian Silvermane0a95462014-02-17 00:41:09 -0800452 if (DoGetPositionOfEdge(claw_values.calibration, edge_encoder, edge_angle,
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000453 calibration_, front_, back_, "calibration")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800454 return true;
455 }
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000456 if (DoGetPositionOfEdge(claw_values.back, edge_encoder, edge_angle, back_,
457 calibration_, front_, "back")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800458 return true;
459 }
460 return false;
461}
462
Austin Schuhcda86af2014-02-16 16:16:39 -0800463void TopZeroedStateFeedbackLoop::SetCalibration(double edge_encoder,
464 double edge_angle) {
465 double old_offset = offset_;
466 offset_ = edge_angle - edge_encoder;
467 const double doffset = offset_ - old_offset;
468 motor_->ChangeTopOffset(doffset);
469}
470
Brian Silverman084372e2014-04-10 10:55:53 -0700471double TopZeroedStateFeedbackLoop::ComputeCalibrationChange(double edge_encoder,
472 double edge_angle) {
473 const double offset = edge_angle - edge_encoder;
474 const double doffset = offset - offset_;
475 return doffset;
476}
477
Austin Schuhcda86af2014-02-16 16:16:39 -0800478void BottomZeroedStateFeedbackLoop::SetCalibration(double edge_encoder,
479 double edge_angle) {
480 double old_offset = offset_;
481 offset_ = edge_angle - edge_encoder;
482 const double doffset = offset_ - old_offset;
483 motor_->ChangeBottomOffset(doffset);
484}
485
Brian Silverman084372e2014-04-10 10:55:53 -0700486double BottomZeroedStateFeedbackLoop::ComputeCalibrationChange(
487 double edge_encoder, double edge_angle) {
488 const double offset = edge_angle - edge_encoder;
489 const double doffset = offset - offset_;
490 return doffset;
491}
492
Austin Schuhcda86af2014-02-16 16:16:39 -0800493void ClawMotor::ChangeTopOffset(double doffset) {
494 claw_.ChangeTopOffset(doffset);
495 if (has_top_claw_goal_) {
496 top_claw_goal_ += doffset;
497 }
498}
499
500void ClawMotor::ChangeBottomOffset(double doffset) {
501 claw_.ChangeBottomOffset(doffset);
502 if (has_bottom_claw_goal_) {
503 bottom_claw_goal_ += doffset;
504 }
505}
506
507void ClawLimitedLoop::ChangeTopOffset(double doffset) {
508 Y_(1, 0) += doffset;
509 X_hat(1, 0) += doffset;
510 LOG(INFO, "Changing top offset by %f\n", doffset);
511}
512void ClawLimitedLoop::ChangeBottomOffset(double doffset) {
513 Y_(0, 0) += doffset;
514 X_hat(0, 0) += doffset;
515 X_hat(1, 0) -= doffset;
516 LOG(INFO, "Changing bottom offset by %f\n", doffset);
517}
joe7376ff52014-02-16 18:28:42 -0800518
Austin Schuh069143b2014-02-17 02:46:26 -0800519void LimitClawGoal(double *bottom_goal, double *top_goal,
520 const frc971::constants::Values &values) {
521 // first update position based on angle limit
522
523 const double separation = *top_goal - *bottom_goal;
Brian Silverman06374312014-04-12 16:09:28 -0700524 if (separation > values.claw.soft_max_separation) {
525 const double dsep = (separation - values.claw.soft_max_separation) / 2.0;
Austin Schuh069143b2014-02-17 02:46:26 -0800526 *bottom_goal += dsep;
527 *top_goal -= dsep;
528 LOG(DEBUG, "Goals now bottom: %f, top: %f\n", *bottom_goal, *top_goal);
529 }
Brian Silverman06374312014-04-12 16:09:28 -0700530 if (separation < values.claw.soft_min_separation) {
531 const double dsep = (separation - values.claw.soft_min_separation) / 2.0;
Austin Schuh069143b2014-02-17 02:46:26 -0800532 *bottom_goal += dsep;
533 *top_goal -= dsep;
534 LOG(DEBUG, "Goals now bottom: %f, top: %f\n", *bottom_goal, *top_goal);
535 }
536
537 // now move both goals in unison
538 if (*bottom_goal < values.claw.lower_claw.lower_limit) {
539 *top_goal += values.claw.lower_claw.lower_limit - *bottom_goal;
540 *bottom_goal = values.claw.lower_claw.lower_limit;
541 }
542 if (*bottom_goal > values.claw.lower_claw.upper_limit) {
543 *top_goal -= *bottom_goal - values.claw.lower_claw.upper_limit;
544 *bottom_goal = values.claw.lower_claw.upper_limit;
545 }
546
547 if (*top_goal < values.claw.upper_claw.lower_limit) {
548 *bottom_goal += values.claw.upper_claw.lower_limit - *top_goal;
549 *top_goal = values.claw.upper_claw.lower_limit;
550 }
551 if (*top_goal > values.claw.upper_claw.upper_limit) {
552 *bottom_goal -= *top_goal - values.claw.upper_claw.upper_limit;
553 *top_goal = values.claw.upper_claw.upper_limit;
554 }
555}
Austin Schuhcda86af2014-02-16 16:16:39 -0800556
Austin Schuhe7f90d12014-02-17 00:48:25 -0800557bool ClawMotor::is_ready() const {
558 return (
559 (top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED &&
560 bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) ||
Brian Silverman71fbee02014-03-13 17:24:54 -0700561 (((::aos::robot_state.get() == NULL) ? true
562 : ::aos::robot_state->autonomous) &&
Austin Schuhe7f90d12014-02-17 00:48:25 -0800563 ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
564 top_claw_.zeroing_state() ==
565 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
566 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
567 bottom_claw_.zeroing_state() ==
568 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION))));
569}
570
571bool ClawMotor::is_zeroing() const { return !is_ready(); }
572
Austin Schuh3bb9a442014-02-02 16:01:45 -0800573// Positive angle is up, and positive power is up.
Austin Schuhcc0bf312014-02-09 00:39:29 -0800574void ClawMotor::RunIteration(const control_loops::ClawGroup::Goal *goal,
575 const control_loops::ClawGroup::Position *position,
576 control_loops::ClawGroup::Output *output,
James Kuszmaul9ead1de2014-02-28 21:24:39 -0800577 control_loops::ClawGroup::Status *status) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800578 constexpr double dt = 0.01;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800579
580 // Disable the motors now so that all early returns will return with the
581 // motors disabled.
582 if (output) {
583 output->top_claw_voltage = 0;
584 output->bottom_claw_voltage = 0;
585 output->intake_voltage = 0;
Ben Fredrickson61893d52014-03-02 09:43:23 +0000586 output->tusk_voltage = 0;
587 }
588
Brian Silverman71fbee02014-03-13 17:24:54 -0700589 if (goal) {
590 if (::std::isnan(goal->bottom_angle) ||
591 ::std::isnan(goal->separation_angle) || ::std::isnan(goal->intake) ||
592 ::std::isnan(goal->centering)) {
593 return;
594 }
Austin Schuh3bb9a442014-02-02 16:01:45 -0800595 }
596
Austin Schuh1a499942014-02-17 01:51:58 -0800597 if (reset()) {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800598 top_claw_.Reset(position->top);
599 bottom_claw_.Reset(position->bottom);
Austin Schuh1a499942014-02-17 01:51:58 -0800600 }
601
Austin Schuhf9286cd2014-02-11 00:51:09 -0800602 const frc971::constants::Values &values = constants::GetValues();
603
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800604 if (position) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800605 Eigen::Matrix<double, 2, 1> Y;
606 Y << position->bottom.position + bottom_claw_.offset(),
607 position->top.position + top_claw_.offset();
608 claw_.Correct(Y);
609
Austin Schuhf9286cd2014-02-11 00:51:09 -0800610 top_claw_.SetPositionValues(position->top);
611 bottom_claw_.SetPositionValues(position->bottom);
612
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800613 if (!has_top_claw_goal_) {
614 has_top_claw_goal_ = true;
Austin Schuhcda86af2014-02-16 16:16:39 -0800615 top_claw_goal_ = top_claw_.absolute_position();
Austin Schuhe7f90d12014-02-17 00:48:25 -0800616 initial_separation_ =
Austin Schuhcda86af2014-02-16 16:16:39 -0800617 top_claw_.absolute_position() - bottom_claw_.absolute_position();
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800618 }
619 if (!has_bottom_claw_goal_) {
620 has_bottom_claw_goal_ = true;
Austin Schuhcda86af2014-02-16 16:16:39 -0800621 bottom_claw_goal_ = bottom_claw_.absolute_position();
Austin Schuhe7f90d12014-02-17 00:48:25 -0800622 initial_separation_ =
Austin Schuhcda86af2014-02-16 16:16:39 -0800623 top_claw_.absolute_position() - bottom_claw_.absolute_position();
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800624 }
Brian Silvermanf48fab32014-03-09 14:32:24 -0700625 LOG_STRUCT(DEBUG, "absolute position",
626 ClawPositionToLog(top_claw_.absolute_position(),
627 bottom_claw_.absolute_position()));
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800628 }
629
Brian Silverman71fbee02014-03-13 17:24:54 -0700630 bool autonomous, enabled;
631 if (::aos::robot_state.get() == nullptr) {
632 autonomous = true;
633 enabled = false;
634 } else {
635 autonomous = ::aos::robot_state->autonomous;
636 enabled = ::aos::robot_state->enabled;
637 }
Austin Schuh069143b2014-02-17 02:46:26 -0800638
639 double bottom_claw_velocity_ = 0.0;
640 double top_claw_velocity_ = 0.0;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800641
Brian Silverman71fbee02014-03-13 17:24:54 -0700642 if (goal != NULL &&
643 ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED &&
644 bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) ||
645 (autonomous &&
646 ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
647 top_claw_.zeroing_state() ==
648 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
649 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
650 bottom_claw_.zeroing_state() ==
651 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION))))) {
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800652 // Ready to use the claw.
653 // Limit the goals here.
Austin Schuhf9286cd2014-02-11 00:51:09 -0800654 bottom_claw_goal_ = goal->bottom_angle;
Brian Silverman7c021c42014-02-17 15:15:56 -0800655 top_claw_goal_ = goal->bottom_angle + goal->separation_angle;
Austin Schuhcda86af2014-02-16 16:16:39 -0800656 has_bottom_claw_goal_ = true;
657 has_top_claw_goal_ = true;
658 doing_calibration_fine_tune_ = false;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800659 mode_ = READY;
Brian Silverman084372e2014-04-10 10:55:53 -0700660
661 bottom_claw_.HandleCalibrationError(values.claw.lower_claw);
662 top_claw_.HandleCalibrationError(values.claw.upper_claw);
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800663 } else if (top_claw_.zeroing_state() !=
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000664 ZeroedStateFeedbackLoop::UNKNOWN_POSITION &&
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800665 bottom_claw_.zeroing_state() !=
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000666 ZeroedStateFeedbackLoop::UNKNOWN_POSITION) {
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800667 // Time to fine tune the zero.
668 // Limit the goals here.
Austin Schuh0c733422014-02-17 01:17:12 -0800669 if (!enabled) {
670 // If we are disabled, start the fine tune process over again.
671 doing_calibration_fine_tune_ = false;
672 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800673 if (bottom_claw_.zeroing_state() != ZeroedStateFeedbackLoop::CALIBRATED) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800674 // always get the bottom claw to calibrated first
675 LOG(DEBUG, "Calibrating the bottom of the claw\n");
676 if (!doing_calibration_fine_tune_) {
677 if (::std::abs(bottom_absolute_position() -
Austin Schuhd27931c2014-02-16 19:18:20 -0800678 values.claw.start_fine_tune_pos) <
679 values.claw.claw_unimportant_epsilon) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800680 doing_calibration_fine_tune_ = true;
Austin Schuhd27931c2014-02-16 19:18:20 -0800681 bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800682 top_claw_velocity_ = bottom_claw_velocity_ =
683 values.claw.claw_zeroing_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800684 LOG(DEBUG, "Ready to fine tune the bottom\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800685 mode_ = FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800686 } else {
687 // send bottom to zeroing start
Austin Schuhd27931c2014-02-16 19:18:20 -0800688 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuhcda86af2014-02-16 16:16:39 -0800689 LOG(DEBUG, "Going to the start position for the bottom\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800690 mode_ = PREP_FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800691 }
692 } else {
Austin Schuhe7f90d12014-02-17 00:48:25 -0800693 mode_ = FINE_TUNE_BOTTOM;
Austin Schuhd27931c2014-02-16 19:18:20 -0800694 bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800695 top_claw_velocity_ = bottom_claw_velocity_ =
696 values.claw.claw_zeroing_speed;
Brian Silvermane0a95462014-02-17 00:41:09 -0800697 if (top_claw_.front_or_back_triggered() ||
698 bottom_claw_.front_or_back_triggered()) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800699 // We shouldn't hit a limit, but if we do, go back to the zeroing
700 // point and try again.
701 doing_calibration_fine_tune_ = false;
Austin Schuhd27931c2014-02-16 19:18:20 -0800702 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuh069143b2014-02-17 02:46:26 -0800703 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
Austin Schuhcda86af2014-02-16 16:16:39 -0800704 LOG(DEBUG, "Found a limit, starting over.\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800705 mode_ = PREP_FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800706 }
Austin Schuh288c8c32014-02-16 17:20:17 -0800707
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000708 if (position && bottom_claw_.SawFilteredPosedge(
709 bottom_claw_.calibration(), bottom_claw_.front(),
710 bottom_claw_.back())) {
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000711 // do calibration
712 bottom_claw_.SetCalibration(
713 position->bottom.posedge_value,
714 values.claw.lower_claw.calibration.lower_angle);
715 bottom_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED);
716 // calibrated so we are done fine tuning bottom
717 doing_calibration_fine_tune_ = false;
718 LOG(DEBUG, "Calibrated the bottom correctly!\n");
719 } else if (bottom_claw_.calibration().last_value()) {
Brian Silvermane4c701d2014-04-10 19:29:25 -0700720 LOG(DEBUG, "Aborting bottom fine tune because sensor triggered\n");
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000721 doing_calibration_fine_tune_ = false;
Brian Silvermancc3844a2014-04-10 21:15:07 -0700722 bottom_claw_.set_zeroing_state(
723 ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
Austin Schuhcda86af2014-02-16 16:16:39 -0800724 } else {
725 LOG(DEBUG, "Fine tuning\n");
726 }
727 }
728 // now set the top claw to track
729
Austin Schuhd27931c2014-02-16 19:18:20 -0800730 top_claw_goal_ = bottom_claw_goal_ + values.claw.claw_zeroing_separation;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800731 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800732 // bottom claw must be calibrated, start on the top
733 if (!doing_calibration_fine_tune_) {
Austin Schuhd27931c2014-02-16 19:18:20 -0800734 if (::std::abs(top_absolute_position() -
735 values.claw.start_fine_tune_pos) <
736 values.claw.claw_unimportant_epsilon) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800737 doing_calibration_fine_tune_ = true;
Austin Schuhd27931c2014-02-16 19:18:20 -0800738 top_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800739 top_claw_velocity_ = bottom_claw_velocity_ =
740 values.claw.claw_zeroing_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800741 LOG(DEBUG, "Ready to fine tune the top\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800742 mode_ = FINE_TUNE_TOP;
Austin Schuhcda86af2014-02-16 16:16:39 -0800743 } else {
744 // send top to zeroing start
Austin Schuhd27931c2014-02-16 19:18:20 -0800745 top_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuhcda86af2014-02-16 16:16:39 -0800746 LOG(DEBUG, "Going to the start position for the top\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800747 mode_ = PREP_FINE_TUNE_TOP;
Austin Schuhcda86af2014-02-16 16:16:39 -0800748 }
749 } else {
Austin Schuhe7f90d12014-02-17 00:48:25 -0800750 mode_ = FINE_TUNE_TOP;
Austin Schuhd27931c2014-02-16 19:18:20 -0800751 top_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800752 top_claw_velocity_ = bottom_claw_velocity_ =
753 values.claw.claw_zeroing_speed;
Brian Silvermane0a95462014-02-17 00:41:09 -0800754 if (top_claw_.front_or_back_triggered() ||
755 bottom_claw_.front_or_back_triggered()) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800756 // this should not happen, but now we know it won't
757 doing_calibration_fine_tune_ = false;
Austin Schuhd27931c2014-02-16 19:18:20 -0800758 top_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuh069143b2014-02-17 02:46:26 -0800759 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
Austin Schuhcda86af2014-02-16 16:16:39 -0800760 LOG(DEBUG, "Found a limit, starting over.\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800761 mode_ = PREP_FINE_TUNE_TOP;
Austin Schuhcda86af2014-02-16 16:16:39 -0800762 }
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000763
764 if (position &&
765 top_claw_.SawFilteredPosedge(top_claw_.calibration(),
766 top_claw_.front(), top_claw_.back())) {
767 // do calibration
768 top_claw_.SetCalibration(
769 position->top.posedge_value,
770 values.claw.upper_claw.calibration.lower_angle);
771 top_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED);
772 // calibrated so we are done fine tuning top
773 doing_calibration_fine_tune_ = false;
774 LOG(DEBUG, "Calibrated the top correctly!\n");
775 } else if (top_claw_.calibration().last_value()) {
Brian Silvermane4c701d2014-04-10 19:29:25 -0700776 LOG(DEBUG, "Aborting top fine tune because sensor triggered\n");
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000777 doing_calibration_fine_tune_ = false;
Brian Silvermancc3844a2014-04-10 21:15:07 -0700778 top_claw_.set_zeroing_state(
779 ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
Austin Schuhcda86af2014-02-16 16:16:39 -0800780 }
781 }
782 // now set the bottom claw to track
Austin Schuhd27931c2014-02-16 19:18:20 -0800783 bottom_claw_goal_ = top_claw_goal_ - values.claw.claw_zeroing_separation;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800784 }
785 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800786 doing_calibration_fine_tune_ = false;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800787 if (!was_enabled_ && enabled) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800788 if (position) {
Brian Silverman72035692014-03-14 10:18:27 -0700789 top_claw_goal_ = position->top.position + top_claw_.offset();
790 bottom_claw_goal_ = position->bottom.position + bottom_claw_.offset();
Austin Schuhe7f90d12014-02-17 00:48:25 -0800791 initial_separation_ =
Austin Schuhcda86af2014-02-16 16:16:39 -0800792 position->top.position - position->bottom.position;
Austin Schuhf9286cd2014-02-11 00:51:09 -0800793 } else {
794 has_top_claw_goal_ = false;
795 has_bottom_claw_goal_ = false;
796 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800797 }
Austin Schuhf9286cd2014-02-11 00:51:09 -0800798
Austin Schuh4cb047f2014-02-16 21:10:19 -0800799 if ((bottom_claw_.zeroing_state() !=
800 ZeroedStateFeedbackLoop::UNKNOWN_POSITION ||
Brian Silvermane0a95462014-02-17 00:41:09 -0800801 bottom_claw_.front().value() || top_claw_.front().value()) &&
802 !top_claw_.back().value() && !bottom_claw_.back().value()) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800803 if (enabled) {
804 // Time to slowly move back up to find any position to narrow down the
805 // zero.
Austin Schuhd27931c2014-02-16 19:18:20 -0800806 top_claw_goal_ += values.claw.claw_zeroing_off_speed * dt;
807 bottom_claw_goal_ += values.claw.claw_zeroing_off_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800808 top_claw_velocity_ = bottom_claw_velocity_ =
809 values.claw.claw_zeroing_off_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800810 LOG(DEBUG, "Bottom is known.\n");
Austin Schuhf9286cd2014-02-11 00:51:09 -0800811 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800812 } else {
813 // We don't know where either claw is. Slowly start moving down to find
814 // any hall effect.
Austin Schuhf9286cd2014-02-11 00:51:09 -0800815 if (enabled) {
Austin Schuhd27931c2014-02-16 19:18:20 -0800816 top_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt;
817 bottom_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800818 top_claw_velocity_ = bottom_claw_velocity_ =
819 -values.claw.claw_zeroing_off_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800820 LOG(DEBUG, "Both are unknown.\n");
Austin Schuhf9286cd2014-02-11 00:51:09 -0800821 }
822 }
823
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000824 if (position) {
825 if (enabled) {
826 top_claw_.SetCalibrationOnEdge(
827 values.claw.upper_claw,
828 ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION);
829 bottom_claw_.SetCalibrationOnEdge(
830 values.claw.lower_claw,
831 ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION);
832 } else {
833 // TODO(austin): Only calibrate on the predetermined edge.
834 // We might be able to just ignore this since the backlash is soooo
835 // low.
836 // :)
837 top_claw_.SetCalibrationOnEdge(
838 values.claw.upper_claw,
839 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
840 bottom_claw_.SetCalibrationOnEdge(
841 values.claw.lower_claw,
842 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
843 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800844 }
Austin Schuhe7f90d12014-02-17 00:48:25 -0800845 mode_ = UNKNOWN_LOCATION;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800846 }
847
Austin Schuh069143b2014-02-17 02:46:26 -0800848 // Limit the goals if both claws have been (mostly) found.
849 if (mode_ != UNKNOWN_LOCATION) {
850 LimitClawGoal(&bottom_claw_goal_, &top_claw_goal_, values);
851 }
852
Brian Silvermaned9df2f2014-04-05 07:07:15 -0700853 claw_.set_positions_known(top_claw_.zeroing_state() != ZeroedStateFeedbackLoop::UNKNOWN_POSITION, bottom_claw_.zeroing_state() != ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800854 if (has_top_claw_goal_ && has_bottom_claw_goal_) {
Austin Schuh069143b2014-02-17 02:46:26 -0800855 claw_.R << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_,
856 bottom_claw_velocity_, top_claw_velocity_ - bottom_claw_velocity_;
Austin Schuhcda86af2014-02-16 16:16:39 -0800857 double separation = -971;
858 if (position != nullptr) {
859 separation = position->top.position - position->bottom.position;
860 }
Brian Silvermanf48fab32014-03-09 14:32:24 -0700861 LOG_STRUCT(DEBUG, "actual goal",
862 ClawGoalToLog(claw_.R(0, 0), claw_.R(1, 0), separation));
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800863
Austin Schuh01c652b2014-02-21 23:13:42 -0800864 // Only cap power when one of the halves of the claw is moving slowly and
865 // could wind up.
Austin Schuhe7f90d12014-02-17 00:48:25 -0800866 claw_.set_is_zeroing(mode_ == UNKNOWN_LOCATION || mode_ == FINE_TUNE_TOP ||
867 mode_ == FINE_TUNE_BOTTOM);
Austin Schuhcda86af2014-02-16 16:16:39 -0800868 claw_.Update(output == nullptr);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800869 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800870 claw_.Update(true);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800871 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800872
Austin Schuh4cb047f2014-02-16 21:10:19 -0800873 capped_goal_ = false;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800874 switch (mode_) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800875 case READY:
Austin Schuhe7f90d12014-02-17 00:48:25 -0800876 case PREP_FINE_TUNE_TOP:
877 case PREP_FINE_TUNE_BOTTOM:
Austin Schuhcda86af2014-02-16 16:16:39 -0800878 break;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800879 case FINE_TUNE_BOTTOM:
880 case FINE_TUNE_TOP:
Austin Schuh4cb047f2014-02-16 21:10:19 -0800881 case UNKNOWN_LOCATION: {
Brian Silverman7b7c9072014-03-30 13:33:30 -0700882 LOG_MATRIX(DEBUG, "U_uncapped", claw_.U_uncapped);
Austin Schuh4cb047f2014-02-16 21:10:19 -0800883 if (claw_.uncapped_average_voltage() > values.claw.max_zeroing_voltage) {
Brian Silverman6dd2c532014-03-29 23:34:39 -0700884 double dx_bot = (claw_.U_uncapped(0, 0) -
Austin Schuh4cb047f2014-02-16 21:10:19 -0800885 values.claw.max_zeroing_voltage) /
886 claw_.K(0, 0);
Brian Silverman6dd2c532014-03-29 23:34:39 -0700887 double dx_top = (claw_.U_uncapped(1, 0) -
James Kuszmaul0e866512014-02-21 13:12:52 -0800888 values.claw.max_zeroing_voltage) /
889 claw_.K(0, 0);
890 double dx = ::std::max(dx_top, dx_bot);
Austin Schuh4cb047f2014-02-16 21:10:19 -0800891 bottom_claw_goal_ -= dx;
892 top_claw_goal_ -= dx;
James Kuszmaul0e866512014-02-21 13:12:52 -0800893 Eigen::Matrix<double, 4, 1> R;
894 R << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_, claw_.R(2, 0), claw_.R(3, 0);
Brian Silverman6dd2c532014-03-29 23:34:39 -0700895 claw_.U = claw_.K() * (R - claw_.X_hat);
Austin Schuhcda86af2014-02-16 16:16:39 -0800896 capped_goal_ = true;
Brian Silvermanf48fab32014-03-09 14:32:24 -0700897 LOG(DEBUG, "Moving the goal by %f to prevent windup."
898 " Uncapped is %f, max is %f, difference is %f\n",
899 dx,
Austin Schuhe7f90d12014-02-17 00:48:25 -0800900 claw_.uncapped_average_voltage(), values.claw.max_zeroing_voltage,
901 (claw_.uncapped_average_voltage() -
902 values.claw.max_zeroing_voltage));
Austin Schuh4cb047f2014-02-16 21:10:19 -0800903 } else if (claw_.uncapped_average_voltage() <
904 -values.claw.max_zeroing_voltage) {
Brian Silverman6dd2c532014-03-29 23:34:39 -0700905 double dx_bot = (claw_.U_uncapped(0, 0) +
Austin Schuh4cb047f2014-02-16 21:10:19 -0800906 values.claw.max_zeroing_voltage) /
907 claw_.K(0, 0);
Brian Silverman6dd2c532014-03-29 23:34:39 -0700908 double dx_top = (claw_.U_uncapped(1, 0) +
James Kuszmaul0e866512014-02-21 13:12:52 -0800909 values.claw.max_zeroing_voltage) /
910 claw_.K(0, 0);
911 double dx = ::std::min(dx_top, dx_bot);
Austin Schuh4cb047f2014-02-16 21:10:19 -0800912 bottom_claw_goal_ -= dx;
913 top_claw_goal_ -= dx;
James Kuszmaul0e866512014-02-21 13:12:52 -0800914 Eigen::Matrix<double, 4, 1> R;
915 R << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_, claw_.R(2, 0), claw_.R(3, 0);
Brian Silverman6dd2c532014-03-29 23:34:39 -0700916 claw_.U = claw_.K() * (R - claw_.X_hat);
Austin Schuhcda86af2014-02-16 16:16:39 -0800917 capped_goal_ = true;
Austin Schuh4cb047f2014-02-16 21:10:19 -0800918 LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx);
Austin Schuhcda86af2014-02-16 16:16:39 -0800919 }
Austin Schuh4cb047f2014-02-16 21:10:19 -0800920 } break;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800921 }
922
923 if (output) {
Ben Fredrickson2f76ddf2014-02-23 05:58:23 +0000924 if (goal) {
925 //setup the intake
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000926 output->intake_voltage =
927 (goal->intake > 12.0) ? 12 : (goal->intake < -12.0) ? -12.0
928 : goal->intake;
Ben Fredrickson2f76ddf2014-02-23 05:58:23 +0000929 output->tusk_voltage = goal->centering;
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000930 output->tusk_voltage =
931 (goal->centering > 12.0) ? 12 : (goal->centering < -12.0)
932 ? -12.0
933 : goal->centering;
Ben Fredrickson2f76ddf2014-02-23 05:58:23 +0000934 }
James Kuszmauld536a402014-02-18 22:32:12 -0800935 output->top_claw_voltage = claw_.U(1, 0);
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000936 output->bottom_claw_voltage = claw_.U(0, 0);
Austin Schuhf84a1302014-02-19 00:23:30 -0800937
938 if (output->top_claw_voltage > kMaxVoltage) {
939 output->top_claw_voltage = kMaxVoltage;
940 } else if (output->top_claw_voltage < -kMaxVoltage) {
941 output->top_claw_voltage = -kMaxVoltage;
942 }
943
944 if (output->bottom_claw_voltage > kMaxVoltage) {
945 output->bottom_claw_voltage = kMaxVoltage;
946 } else if (output->bottom_claw_voltage < -kMaxVoltage) {
947 output->bottom_claw_voltage = -kMaxVoltage;
948 }
Austin Schuh3bb9a442014-02-02 16:01:45 -0800949 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800950
James Kuszmaul4abaf482014-02-26 21:16:35 -0800951 status->bottom = bottom_absolute_position();
952 status->separation = top_absolute_position() - bottom_absolute_position();
953 status->bottom_velocity = claw_.X_hat(2, 0);
954 status->separation_velocity = claw_.X_hat(3, 0);
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800955
Brian Silverman71fbee02014-03-13 17:24:54 -0700956 if (goal) {
957 bool bottom_done =
958 ::std::abs(bottom_absolute_position() - goal->bottom_angle) < 0.020;
959 bool bottom_velocity_done = ::std::abs(status->bottom_velocity) < 0.2;
960 bool separation_done =
961 ::std::abs((top_absolute_position() - bottom_absolute_position()) -
962 goal->separation_angle) < 0.020;
963 bool separation_done_with_ball =
964 ::std::abs((top_absolute_position() - bottom_absolute_position()) -
965 goal->separation_angle) < 0.06;
966 status->done = is_ready() && separation_done && bottom_done && bottom_velocity_done;
967 status->done_with_ball =
968 is_ready() && separation_done_with_ball && bottom_done && bottom_velocity_done;
969 } else {
970 status->done = status->done_with_ball = false;
971 }
Austin Schuha556b012014-03-02 11:55:52 -0800972
Austin Schuh4f8633f2014-03-02 13:59:46 -0800973 status->zeroed = is_ready();
Brian Silverman80fc94c2014-03-09 16:56:01 -0700974 status->zeroed_for_auto =
975 (top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
976 top_claw_.zeroing_state() ==
977 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
978 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
979 bottom_claw_.zeroing_state() ==
980 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
Austin Schuh4f8633f2014-03-02 13:59:46 -0800981
Brian Silverman71fbee02014-03-13 17:24:54 -0700982 was_enabled_ = enabled;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800983}
984
985} // namespace control_loops
986} // namespace frc971
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000987