blob: c38fd231442b408c0e31d34f7bb768aca7c6bf66 [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"
Austin Schuh3bb9a442014-02-02 16:01:45 -08009
10#include "frc971/constants.h"
Austin Schuhcda86af2014-02-16 16:16:39 -080011#include "frc971/control_loops/claw/claw_motor_plant.h"
12
Austin Schuh3bb9a442014-02-02 16:01:45 -080013// Zeroing plan.
14// There are 2 types of zeros. Enabled and disabled ones.
15// Disabled ones are only valid during auto mode, and can be used to speed up
16// the enabled zero process. We need to re-zero during teleop in case the auto
17// zero was poor and causes us to miss all our shots.
18//
19// We need to be able to zero manually while disabled by moving the joint over
20// the zeros.
21// Zero on the down edge when disabled (gravity in the direction of motion)
22//
23// When enabled, zero on the up edge (gravity opposing the direction of motion)
24// The enabled sequence needs to work as follows. We can crash the claw if we
25// bring them too close to each other or too far from each other. The only safe
26// thing to do is to move them in unison.
27//
28// Start by moving them both towards the front of the bot to either find either
29// the middle hall effect on either jaw, or the front hall effect on the bottom
30// jaw. Any edge that isn't the desired edge will provide an approximate edge
31// location that can be used for the fine tuning step.
32// Once an edge is found on the front claw, move back the other way with both
33// claws until an edge is found for the other claw.
34// Now that we have an approximate zero, we can robustify the limits to keep
35// both claws safe. Then, we can move both claws to a position that is the
36// correct side of the zero and go zero.
37
38// Valid region plan.
Ben Fredrickson81ba2d52014-03-02 08:21:46 +000039// Difference between the arms has a range, and the values of each arm has a
40// range.
Austin Schuh3bb9a442014-02-02 16:01:45 -080041// If a claw runs up against a static limit, don't let the goal change outside
42// the limit.
43// If a claw runs up against a movable limit, move both claws outwards to get
44// out of the condition.
45
46namespace frc971 {
47namespace control_loops {
Brian Silverman6dd2c532014-03-29 23:34:39 -070048namespace {
49
50template <typename T> int sign(T val) {
51 if (val > T(0)) {
52 return 1;
53 } else {
54 return -1;
55 }
56}
57
58} // namespace
Austin Schuh3bb9a442014-02-02 16:01:45 -080059
Austin Schuh01c652b2014-02-21 23:13:42 -080060static const double kZeroingVoltage = 4.0;
61static const double kMaxVoltage = 12.0;
Brian Silverman084372e2014-04-10 10:55:53 -070062const double kRezeroThreshold = 0.03;
Austin Schuhf84a1302014-02-19 00:23:30 -080063
Austin Schuh27b8fb12014-02-22 15:10:05 -080064ClawLimitedLoop::ClawLimitedLoop(StateFeedbackLoop<4, 2, 2> loop)
65 : StateFeedbackLoop<4, 2, 2>(loop),
66 uncapped_average_voltage_(0.0),
James Kuszmaulf63b0ae2014-03-25 16:52:11 -070067 is_zeroing_(true),
68 U_Poly_((Eigen::Matrix<double, 4, 2>() << 1, 0,
69 -1, 0,
70 0, 1,
71 0, -1).finished(),
72 (Eigen::Matrix<double, 4, 1>() << kMaxVoltage, kMaxVoltage,
Brian Silverman6dd2c532014-03-29 23:34:39 -070073 kMaxVoltage, kMaxVoltage).finished()),
74 U_Poly_zeroing_((Eigen::Matrix<double, 4, 2>() << 1, 0,
75 -1, 0,
76 0, 1,
77 0, -1).finished(),
78 (Eigen::Matrix<double, 4, 1>() <<
79 kZeroingVoltage, kZeroingVoltage,
80 kZeroingVoltage, kZeroingVoltage).finished()) {
James Kuszmaulf63b0ae2014-03-25 16:52:11 -070081 ::aos::controls::HPolytope<0>::Init();
82}
Austin Schuh27b8fb12014-02-22 15:10:05 -080083
James Kuszmaulf63b0ae2014-03-25 16:52:11 -070084// Caps the voltage prioritizing reducing velocity error over reducing
85// positional error.
86// Uses the polytope libararies which we used to just use for the drivetrain.
87// Uses a region representing the maximum voltage and then transforms it such
88// that the points represent different amounts of positional error and
89// constrains the region such that, if at all possible, it will maintain its
90// current efforts to reduce velocity error.
Austin Schuhcda86af2014-02-16 16:16:39 -080091void ClawLimitedLoop::CapU() {
James Kuszmaulf63b0ae2014-03-25 16:52:11 -070092 Eigen::Matrix<double, 4, 1> error = R - X_hat;
Austin Schuh4cb047f2014-02-16 21:10:19 -080093
James Kuszmauld536a402014-02-18 22:32:12 -080094 double u_top = U(1, 0);
95 double u_bottom = U(0, 0);
Austin Schuhcda86af2014-02-16 16:16:39 -080096
James Kuszmaulf63b0ae2014-03-25 16:52:11 -070097 uncapped_average_voltage_ = (u_top + u_bottom) / 2;
98
99 double max_voltage = is_zeroing_ ? kZeroingVoltage : kMaxVoltage;
100
101 if (::std::abs(u_bottom) > max_voltage or ::std::abs(u_top) > max_voltage) {
Brian Silverman6dd2c532014-03-29 23:34:39 -0700102 LOG_MATRIX(DEBUG, "U at start", U);
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700103 // H * U <= k
104 // U = UPos + UVel
105 // H * (UPos + UVel) <= k
106 // H * UPos <= k - H * UVel
107
108 // Now, we can do a coordinate transformation and say the following.
109
110 // UPos = position_K * position_error
111 // (H * position_K) * position_error <= k - H * UVel
112
113 Eigen::Matrix<double, 2, 2> position_K;
114 position_K << K(0, 0), K(0, 1),
115 K(1, 0), K(1, 1);
116 Eigen::Matrix<double, 2, 2> velocity_K;
117 velocity_K << K(0, 2), K(0, 3),
118 K(1, 2), K(1, 3);
119
120 Eigen::Matrix<double, 2, 1> position_error;
121 position_error << error(0, 0), error(1, 0);
122 Eigen::Matrix<double, 2, 1> velocity_error;
123 velocity_error << error(2, 0), error(3, 0);
Brian Silverman6dd2c532014-03-29 23:34:39 -0700124 LOG_MATRIX(DEBUG, "error", error);
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700125
Brian Silverman6dd2c532014-03-29 23:34:39 -0700126 const auto &poly = is_zeroing_ ? U_Poly_zeroing_ : U_Poly_;
127 const Eigen::Matrix<double, 4, 2> pos_poly_H = poly.H() * position_K;
128 const Eigen::Matrix<double, 4, 1> pos_poly_k =
129 poly.k() - poly.H() * velocity_K * velocity_error;
130 const ::aos::controls::HPolytope<2> pos_poly(pos_poly_H, pos_poly_k);
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700131
Brian Silverman6dd2c532014-03-29 23:34:39 -0700132 Eigen::Matrix<double, 2, 1> adjusted_pos_error;
133 {
134 const auto &P = position_error;
Brian Silvermanb087c0a2014-03-30 12:59:52 -0700135
136 // This line was at 45 degrees but is now at some angle steeper than the
137 // straight one between the points.
138 Eigen::Matrix<double, 1, 2> angle_45;
139 // If the top claw is above its soft upper limit, make the line actually
140 // 45 degrees to avoid smashing it into the limit in an attempt to fix the
141 // separation error faster than the bottom position one.
142 if (X_hat(0, 0) + X_hat(1, 0) >
143 constants::GetValues().claw.upper_claw.upper_limit) {
144 angle_45 << 1, 1;
145 } else {
146 // Fixing separation error half as fast as positional error works well
147 // because it means they both close evenly.
148 angle_45 << ::std::sqrt(3), 1;
149 }
150 Eigen::Matrix<double, 1, 2> L45_quadrant;
151 L45_quadrant << sign(P(1, 0)), -sign(P(0, 0));
152 const auto L45 = L45_quadrant.cwiseProduct(angle_45);
Brian Silverman6dd2c532014-03-29 23:34:39 -0700153 const double w45 = 0;
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700154
Brian Silverman6dd2c532014-03-29 23:34:39 -0700155 Eigen::Matrix<double, 1, 2> LH;
156 if (::std::abs(P(0, 0)) > ::std::abs(P(1, 0))) {
157 LH << 0, 1;
158 } else {
159 LH << 1, 0;
160 }
161 const double wh = LH.dot(P);
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700162
Brian Silverman6dd2c532014-03-29 23:34:39 -0700163 Eigen::Matrix<double, 2, 2> standard;
164 standard << L45, LH;
165 Eigen::Matrix<double, 2, 1> W;
166 W << w45, wh;
167 const Eigen::Matrix<double, 2, 1> intersection = standard.inverse() * W;
168
169 bool is_inside_h;
170 const auto adjusted_pos_error_h =
171 DoCoerceGoal(pos_poly, LH, wh, position_error, &is_inside_h);
172 const auto adjusted_pos_error_45 =
173 DoCoerceGoal(pos_poly, L45, w45, intersection, nullptr);
174 if (pos_poly.IsInside(intersection)) {
175 adjusted_pos_error = adjusted_pos_error_h;
176 } else {
177 if (is_inside_h) {
178 if (adjusted_pos_error_h.norm() > adjusted_pos_error_45.norm()) {
179 adjusted_pos_error = adjusted_pos_error_h;
180 } else {
181 adjusted_pos_error = adjusted_pos_error_45;
182 }
183 } else {
184 adjusted_pos_error = adjusted_pos_error_45;
185 }
186 }
187 }
188
189 LOG_MATRIX(DEBUG, "adjusted_pos_error", adjusted_pos_error);
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700190 U = velocity_K * velocity_error + position_K * adjusted_pos_error;
Brian Silverman6dd2c532014-03-29 23:34:39 -0700191 LOG_MATRIX(DEBUG, "U is now", U);
Brian Silverman7b7c9072014-03-30 13:33:30 -0700192
193 {
194 const auto values = constants::GetValues().claw;
Brian Silvermaned9df2f2014-04-05 07:07:15 -0700195 if (top_known_) {
196 if (X_hat(0, 0) + X_hat(1, 0) > values.upper_claw.upper_limit &&
197 U(1, 0) > 0) {
198 LOG(WARNING, "upper claw too high and moving up\n");
199 U(1, 0) = 0;
200 } else if (X_hat(0, 0) + X_hat(1, 0) < values.upper_claw.lower_limit &&
201 U(1, 0) < 0) {
202 LOG(WARNING, "upper claw too low and moving down\n");
203 U(1, 0) = 0;
204 }
Brian Silverman7b7c9072014-03-30 13:33:30 -0700205 }
Brian Silvermaned9df2f2014-04-05 07:07:15 -0700206 if (bottom_known_) {
207 if (X_hat(0, 0) > values.lower_claw.upper_limit && U(0, 0) > 0) {
208 LOG(WARNING, "lower claw too high and moving up\n");
209 U(0, 0) = 0;
210 } else if (X_hat(0, 0) < values.lower_claw.lower_limit && U(0, 0) < 0) {
211 LOG(WARNING, "lower claw too low and moving down\n");
212 U(0, 0) = 0;
213 }
Brian Silverman7b7c9072014-03-30 13:33:30 -0700214 }
215 }
James Kuszmauld536a402014-02-18 22:32:12 -0800216 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800217}
218
Austin Schuh27b8fb12014-02-22 15:10:05 -0800219ZeroedStateFeedbackLoop::ZeroedStateFeedbackLoop(const char *name,
220 ClawMotor *motor)
221 : offset_(0.0),
222 name_(name),
223 motor_(motor),
224 zeroing_state_(UNKNOWN_POSITION),
225 posedge_value_(0.0),
226 negedge_value_(0.0),
227 encoder_(0.0),
228 last_encoder_(0.0) {}
229
230void ZeroedStateFeedbackLoop::SetPositionValues(const HalfClawPosition &claw) {
231 front_.Update(claw.front);
232 calibration_.Update(claw.calibration);
233 back_.Update(claw.back);
234
235 bool any_sensor_triggered = any_triggered();
236 if (any_sensor_triggered && any_triggered_last_) {
237 // We are still on the hall effect and nothing has changed.
238 min_hall_effect_on_angle_ =
239 ::std::min(min_hall_effect_on_angle_, claw.position);
240 max_hall_effect_on_angle_ =
241 ::std::max(max_hall_effect_on_angle_, claw.position);
242 } else if (!any_sensor_triggered && !any_triggered_last_) {
243 // We are still off the hall effect and nothing has changed.
244 min_hall_effect_off_angle_ =
245 ::std::min(min_hall_effect_off_angle_, claw.position);
246 max_hall_effect_off_angle_ =
247 ::std::max(max_hall_effect_off_angle_, claw.position);
248 } else if (any_sensor_triggered && !any_triggered_last_) {
249 // Saw a posedge on the hall effect. Reset the limits.
250 min_hall_effect_on_angle_ = ::std::min(claw.posedge_value, claw.position);
251 max_hall_effect_on_angle_ = ::std::max(claw.posedge_value, claw.position);
252 } else if (!any_sensor_triggered && any_triggered_last_) {
253 // Saw a negedge on the hall effect. Reset the limits.
254 min_hall_effect_off_angle_ = ::std::min(claw.negedge_value, claw.position);
255 max_hall_effect_off_angle_ = ::std::max(claw.negedge_value, claw.position);
256 }
257
258 posedge_value_ = claw.posedge_value;
259 negedge_value_ = claw.negedge_value;
260 last_encoder_ = encoder_;
261 if (front().value() || calibration().value() || back().value()) {
262 last_on_encoder_ = encoder_;
263 } else {
264 last_off_encoder_ = encoder_;
265 }
266 encoder_ = claw.position;
267 any_triggered_last_ = any_sensor_triggered;
268}
269
270void ZeroedStateFeedbackLoop::Reset(const HalfClawPosition &claw) {
271 set_zeroing_state(ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
272
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000273 front_.Reset(claw.front);
274 calibration_.Reset(claw.calibration);
275 back_.Reset(claw.back);
Austin Schuh27b8fb12014-02-22 15:10:05 -0800276 // close up the min and max edge positions as they are no longer valid and
277 // will be expanded in future iterations
278 min_hall_effect_on_angle_ = claw.position;
279 max_hall_effect_on_angle_ = claw.position;
280 min_hall_effect_off_angle_ = claw.position;
281 max_hall_effect_off_angle_ = claw.position;
282 any_triggered_last_ = any_triggered();
283}
284
285bool TopZeroedStateFeedbackLoop::SetCalibrationOnEdge(
286 const constants::Values::Claws::Claw &claw_values,
287 JointZeroingState zeroing_state) {
288 double edge_encoder;
289 double edge_angle;
290 if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) {
291 LOG(INFO, "Calibration edge edge should be %f.\n", edge_angle);
292 SetCalibration(edge_encoder, edge_angle);
293 set_zeroing_state(zeroing_state);
294 return true;
295 }
296 return false;
297}
298
Brian Silverman084372e2014-04-10 10:55:53 -0700299void TopZeroedStateFeedbackLoop::HandleCalibrationError(
300 const constants::Values::Claws::Claw &claw_values) {
301 double edge_encoder;
302 double edge_angle;
303 if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) {
304 const double calibration_error =
305 ComputeCalibrationChange(edge_encoder, edge_angle);
306 LOG(INFO, "Top calibration error is %f\n", calibration_error);
307 if (::std::abs(calibration_error) > kRezeroThreshold) {
308 SetCalibration(edge_encoder, edge_angle);
309 set_zeroing_state(ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION);
310 }
311 }
312}
313
314
315void BottomZeroedStateFeedbackLoop::HandleCalibrationError(
316 const constants::Values::Claws::Claw &claw_values) {
317 double edge_encoder;
318 double edge_angle;
319 if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) {
320 const double calibration_error =
321 ComputeCalibrationChange(edge_encoder, edge_angle);
322 LOG(INFO, "Bottom calibration error is %f\n", calibration_error);
323 if (::std::abs(calibration_error) > kRezeroThreshold) {
324 SetCalibration(edge_encoder, edge_angle);
325 set_zeroing_state(ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION);
326 }
327 }
328}
329
Austin Schuh27b8fb12014-02-22 15:10:05 -0800330bool BottomZeroedStateFeedbackLoop::SetCalibrationOnEdge(
331 const constants::Values::Claws::Claw &claw_values,
332 JointZeroingState zeroing_state) {
333 double edge_encoder;
334 double edge_angle;
335 if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) {
336 LOG(INFO, "Calibration edge.\n");
337 SetCalibration(edge_encoder, edge_angle);
338 set_zeroing_state(zeroing_state);
339 return true;
340 }
341 return false;
342}
343
Austin Schuhcc0bf312014-02-09 00:39:29 -0800344ClawMotor::ClawMotor(control_loops::ClawGroup *my_claw)
Brian Silverman38111502014-04-10 12:36:26 -0700345 : aos::controls::ControlLoop<control_loops::ClawGroup, true, true,
Brian Silverman71fbee02014-03-13 17:24:54 -0700346 false>(my_claw),
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800347 has_top_claw_goal_(false),
348 top_claw_goal_(0.0),
Austin Schuhcda86af2014-02-16 16:16:39 -0800349 top_claw_(this),
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800350 has_bottom_claw_goal_(false),
351 bottom_claw_goal_(0.0),
Austin Schuhcda86af2014-02-16 16:16:39 -0800352 bottom_claw_(this),
353 claw_(MakeClawLoop()),
Ben Fredrickson9b388422014-02-13 06:15:31 +0000354 was_enabled_(false),
Austin Schuh4cb047f2014-02-16 21:10:19 -0800355 doing_calibration_fine_tune_(false),
Austin Schuhe7f90d12014-02-17 00:48:25 -0800356 capped_goal_(false),
357 mode_(UNKNOWN_LOCATION) {}
Austin Schuh3bb9a442014-02-02 16:01:45 -0800358
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800359const int ZeroedStateFeedbackLoop::kZeroingMaxVoltage;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800360
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000361bool ZeroedStateFeedbackLoop::SawFilteredPosedge(
362 const HallEffectTracker &this_sensor, const HallEffectTracker &sensorA,
363 const HallEffectTracker &sensorB) {
364 if (posedge_filter_ == nullptr && this_sensor.posedge_count_changed() &&
365 !sensorA.posedge_count_changed() && !sensorB.posedge_count_changed() &&
366 this_sensor.value() && !this_sensor.last_value()) {
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000367 posedge_filter_ = &this_sensor;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000368 } else if (posedge_filter_ == &this_sensor &&
369 !this_sensor.posedge_count_changed() &&
370 !sensorA.posedge_count_changed() &&
371 !sensorB.posedge_count_changed() && this_sensor.value()) {
372 posedge_filter_ = nullptr;
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000373 return true;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000374 } else if (posedge_filter_ == &this_sensor) {
375 posedge_filter_ = nullptr;
376 }
377 return false;
378}
379
380bool ZeroedStateFeedbackLoop::SawFilteredNegedge(
381 const HallEffectTracker &this_sensor, const HallEffectTracker &sensorA,
382 const HallEffectTracker &sensorB) {
383 if (negedge_filter_ == nullptr && this_sensor.negedge_count_changed() &&
384 !sensorA.negedge_count_changed() && !sensorB.negedge_count_changed() &&
385 !this_sensor.value() && this_sensor.last_value()) {
386 negedge_filter_ = &this_sensor;
387 } else if (negedge_filter_ == &this_sensor &&
388 !this_sensor.negedge_count_changed() &&
389 !sensorA.negedge_count_changed() &&
390 !sensorB.negedge_count_changed() && !this_sensor.value()) {
391 negedge_filter_ = nullptr;
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000392 return true;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000393 } else if (negedge_filter_ == &this_sensor) {
394 negedge_filter_ = nullptr;
395 }
396 return false;
397}
398
Brian Silvermane0a95462014-02-17 00:41:09 -0800399bool ZeroedStateFeedbackLoop::DoGetPositionOfEdge(
400 const constants::Values::Claws::AnglePair &angles, double *edge_encoder,
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000401 double *edge_angle, const HallEffectTracker &this_sensor,
402 const HallEffectTracker &sensorA, const HallEffectTracker &sensorB,
Brian Silvermane0a95462014-02-17 00:41:09 -0800403 const char *hall_effect_name) {
Austin Schuhf84a1302014-02-19 00:23:30 -0800404 bool found_edge = false;
Austin Schuh27b8fb12014-02-22 15:10:05 -0800405
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000406 if (SawFilteredPosedge(this_sensor, sensorA, sensorB)) {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800407 if (min_hall_effect_off_angle_ == max_hall_effect_off_angle_) {
Brian Silvermanf48fab32014-03-09 14:32:24 -0700408 LOG(WARNING, "%s: Uncertain which side, rejecting posedge\n", name_);
Brian Silvermane0a95462014-02-17 00:41:09 -0800409 } else {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800410 const double average_last_encoder =
411 (min_hall_effect_off_angle_ + max_hall_effect_off_angle_) / 2.0;
412 if (posedge_value_ < average_last_encoder) {
413 *edge_angle = angles.upper_decreasing_angle;
414 LOG(INFO, "%s Posedge upper of %s -> %f posedge: %f avg_encoder: %f\n",
415 name_, hall_effect_name, *edge_angle, posedge_value_,
416 average_last_encoder);
417 } else {
418 *edge_angle = angles.lower_angle;
419 LOG(INFO, "%s Posedge lower of %s -> %f posedge: %f avg_encoder: %f\n",
420 name_, hall_effect_name, *edge_angle, posedge_value_,
421 average_last_encoder);
422 }
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000423 *edge_encoder = posedge_value_;
Austin Schuh27b8fb12014-02-22 15:10:05 -0800424 found_edge = true;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000425 }
426 }
427
428 if (SawFilteredNegedge(this_sensor, sensorA, sensorB)) {
429 if (min_hall_effect_on_angle_ == max_hall_effect_on_angle_) {
Brian Silvermanf48fab32014-03-09 14:32:24 -0700430 LOG(WARNING, "%s: Uncertain which side, rejecting negedge\n", name_);
Brian Silvermane0a95462014-02-17 00:41:09 -0800431 } else {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800432 const double average_last_encoder =
433 (min_hall_effect_on_angle_ + max_hall_effect_on_angle_) / 2.0;
434 if (negedge_value_ > average_last_encoder) {
435 *edge_angle = angles.upper_angle;
436 LOG(INFO, "%s Negedge upper of %s -> %f negedge: %f avg_encoder: %f\n",
437 name_, hall_effect_name, *edge_angle, negedge_value_,
438 average_last_encoder);
439 } else {
440 *edge_angle = angles.lower_decreasing_angle;
441 LOG(INFO, "%s Negedge lower of %s -> %f negedge: %f avg_encoder: %f\n",
442 name_, hall_effect_name, *edge_angle, negedge_value_,
443 average_last_encoder);
444 }
445 *edge_encoder = negedge_value_;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000446 found_edge = true;
Brian Silvermane0a95462014-02-17 00:41:09 -0800447 }
Austin Schuh27b8fb12014-02-22 15:10:05 -0800448 }
449
Austin Schuhf84a1302014-02-19 00:23:30 -0800450 return found_edge;
Brian Silvermane0a95462014-02-17 00:41:09 -0800451}
452
Austin Schuhf9286cd2014-02-11 00:51:09 -0800453bool ZeroedStateFeedbackLoop::GetPositionOfEdge(
Austin Schuhd27931c2014-02-16 19:18:20 -0800454 const constants::Values::Claws::Claw &claw_values, double *edge_encoder,
Austin Schuhf9286cd2014-02-11 00:51:09 -0800455 double *edge_angle) {
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000456 if (DoGetPositionOfEdge(claw_values.front, edge_encoder, edge_angle, front_,
457 calibration_, back_, "front")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800458 return true;
459 }
Brian Silvermane0a95462014-02-17 00:41:09 -0800460 if (DoGetPositionOfEdge(claw_values.calibration, edge_encoder, edge_angle,
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000461 calibration_, front_, back_, "calibration")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800462 return true;
463 }
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000464 if (DoGetPositionOfEdge(claw_values.back, edge_encoder, edge_angle, back_,
465 calibration_, front_, "back")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800466 return true;
467 }
468 return false;
469}
470
Austin Schuhcda86af2014-02-16 16:16:39 -0800471void TopZeroedStateFeedbackLoop::SetCalibration(double edge_encoder,
472 double edge_angle) {
473 double old_offset = offset_;
474 offset_ = edge_angle - edge_encoder;
475 const double doffset = offset_ - old_offset;
476 motor_->ChangeTopOffset(doffset);
477}
478
Brian Silverman084372e2014-04-10 10:55:53 -0700479double TopZeroedStateFeedbackLoop::ComputeCalibrationChange(double edge_encoder,
480 double edge_angle) {
481 const double offset = edge_angle - edge_encoder;
482 const double doffset = offset - offset_;
483 return doffset;
484}
485
Austin Schuhcda86af2014-02-16 16:16:39 -0800486void BottomZeroedStateFeedbackLoop::SetCalibration(double edge_encoder,
487 double edge_angle) {
488 double old_offset = offset_;
489 offset_ = edge_angle - edge_encoder;
490 const double doffset = offset_ - old_offset;
491 motor_->ChangeBottomOffset(doffset);
492}
493
Brian Silverman084372e2014-04-10 10:55:53 -0700494double BottomZeroedStateFeedbackLoop::ComputeCalibrationChange(
495 double edge_encoder, double edge_angle) {
496 const double offset = edge_angle - edge_encoder;
497 const double doffset = offset - offset_;
498 return doffset;
499}
500
Austin Schuhcda86af2014-02-16 16:16:39 -0800501void ClawMotor::ChangeTopOffset(double doffset) {
502 claw_.ChangeTopOffset(doffset);
503 if (has_top_claw_goal_) {
504 top_claw_goal_ += doffset;
505 }
506}
507
508void ClawMotor::ChangeBottomOffset(double doffset) {
509 claw_.ChangeBottomOffset(doffset);
510 if (has_bottom_claw_goal_) {
511 bottom_claw_goal_ += doffset;
512 }
513}
514
515void ClawLimitedLoop::ChangeTopOffset(double doffset) {
516 Y_(1, 0) += doffset;
517 X_hat(1, 0) += doffset;
518 LOG(INFO, "Changing top offset by %f\n", doffset);
519}
520void ClawLimitedLoop::ChangeBottomOffset(double doffset) {
521 Y_(0, 0) += doffset;
522 X_hat(0, 0) += doffset;
523 X_hat(1, 0) -= doffset;
524 LOG(INFO, "Changing bottom offset by %f\n", doffset);
525}
joe7376ff52014-02-16 18:28:42 -0800526
Austin Schuh069143b2014-02-17 02:46:26 -0800527void LimitClawGoal(double *bottom_goal, double *top_goal,
528 const frc971::constants::Values &values) {
529 // first update position based on angle limit
530
531 const double separation = *top_goal - *bottom_goal;
532 if (separation > values.claw.claw_max_separation) {
Austin Schuh069143b2014-02-17 02:46:26 -0800533 const double dsep = (separation - values.claw.claw_max_separation) / 2.0;
534 *bottom_goal += dsep;
535 *top_goal -= dsep;
536 LOG(DEBUG, "Goals now bottom: %f, top: %f\n", *bottom_goal, *top_goal);
537 }
538 if (separation < values.claw.claw_min_separation) {
Austin Schuh069143b2014-02-17 02:46:26 -0800539 const double dsep = (separation - values.claw.claw_min_separation) / 2.0;
540 *bottom_goal += dsep;
541 *top_goal -= dsep;
542 LOG(DEBUG, "Goals now bottom: %f, top: %f\n", *bottom_goal, *top_goal);
543 }
544
545 // now move both goals in unison
546 if (*bottom_goal < values.claw.lower_claw.lower_limit) {
547 *top_goal += values.claw.lower_claw.lower_limit - *bottom_goal;
548 *bottom_goal = values.claw.lower_claw.lower_limit;
549 }
550 if (*bottom_goal > values.claw.lower_claw.upper_limit) {
551 *top_goal -= *bottom_goal - values.claw.lower_claw.upper_limit;
552 *bottom_goal = values.claw.lower_claw.upper_limit;
553 }
554
555 if (*top_goal < values.claw.upper_claw.lower_limit) {
556 *bottom_goal += values.claw.upper_claw.lower_limit - *top_goal;
557 *top_goal = values.claw.upper_claw.lower_limit;
558 }
559 if (*top_goal > values.claw.upper_claw.upper_limit) {
560 *bottom_goal -= *top_goal - values.claw.upper_claw.upper_limit;
561 *top_goal = values.claw.upper_claw.upper_limit;
562 }
563}
Austin Schuhcda86af2014-02-16 16:16:39 -0800564
Austin Schuhe7f90d12014-02-17 00:48:25 -0800565bool ClawMotor::is_ready() const {
566 return (
567 (top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED &&
568 bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) ||
Brian Silverman71fbee02014-03-13 17:24:54 -0700569 (((::aos::robot_state.get() == NULL) ? true
570 : ::aos::robot_state->autonomous) &&
Austin Schuhe7f90d12014-02-17 00:48:25 -0800571 ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
572 top_claw_.zeroing_state() ==
573 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
574 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
575 bottom_claw_.zeroing_state() ==
576 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION))));
577}
578
579bool ClawMotor::is_zeroing() const { return !is_ready(); }
580
Austin Schuh3bb9a442014-02-02 16:01:45 -0800581// Positive angle is up, and positive power is up.
Austin Schuhcc0bf312014-02-09 00:39:29 -0800582void ClawMotor::RunIteration(const control_loops::ClawGroup::Goal *goal,
583 const control_loops::ClawGroup::Position *position,
584 control_loops::ClawGroup::Output *output,
James Kuszmaul9ead1de2014-02-28 21:24:39 -0800585 control_loops::ClawGroup::Status *status) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800586 constexpr double dt = 0.01;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800587
588 // Disable the motors now so that all early returns will return with the
589 // motors disabled.
590 if (output) {
591 output->top_claw_voltage = 0;
592 output->bottom_claw_voltage = 0;
593 output->intake_voltage = 0;
Ben Fredrickson61893d52014-03-02 09:43:23 +0000594 output->tusk_voltage = 0;
595 }
596
Brian Silverman71fbee02014-03-13 17:24:54 -0700597 if (goal) {
598 if (::std::isnan(goal->bottom_angle) ||
599 ::std::isnan(goal->separation_angle) || ::std::isnan(goal->intake) ||
600 ::std::isnan(goal->centering)) {
601 return;
602 }
Austin Schuh3bb9a442014-02-02 16:01:45 -0800603 }
604
Austin Schuh1a499942014-02-17 01:51:58 -0800605 if (reset()) {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800606 top_claw_.Reset(position->top);
607 bottom_claw_.Reset(position->bottom);
Austin Schuh1a499942014-02-17 01:51:58 -0800608 }
609
Austin Schuhf9286cd2014-02-11 00:51:09 -0800610 const frc971::constants::Values &values = constants::GetValues();
611
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800612 if (position) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800613 Eigen::Matrix<double, 2, 1> Y;
614 Y << position->bottom.position + bottom_claw_.offset(),
615 position->top.position + top_claw_.offset();
616 claw_.Correct(Y);
617
Austin Schuhf9286cd2014-02-11 00:51:09 -0800618 top_claw_.SetPositionValues(position->top);
619 bottom_claw_.SetPositionValues(position->bottom);
620
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800621 if (!has_top_claw_goal_) {
622 has_top_claw_goal_ = true;
Austin Schuhcda86af2014-02-16 16:16:39 -0800623 top_claw_goal_ = top_claw_.absolute_position();
Austin Schuhe7f90d12014-02-17 00:48:25 -0800624 initial_separation_ =
Austin Schuhcda86af2014-02-16 16:16:39 -0800625 top_claw_.absolute_position() - bottom_claw_.absolute_position();
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800626 }
627 if (!has_bottom_claw_goal_) {
628 has_bottom_claw_goal_ = true;
Austin Schuhcda86af2014-02-16 16:16:39 -0800629 bottom_claw_goal_ = bottom_claw_.absolute_position();
Austin Schuhe7f90d12014-02-17 00:48:25 -0800630 initial_separation_ =
Austin Schuhcda86af2014-02-16 16:16:39 -0800631 top_claw_.absolute_position() - bottom_claw_.absolute_position();
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800632 }
Brian Silvermanf48fab32014-03-09 14:32:24 -0700633 LOG_STRUCT(DEBUG, "absolute position",
634 ClawPositionToLog(top_claw_.absolute_position(),
635 bottom_claw_.absolute_position()));
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800636 }
637
Brian Silverman71fbee02014-03-13 17:24:54 -0700638 bool autonomous, enabled;
639 if (::aos::robot_state.get() == nullptr) {
640 autonomous = true;
641 enabled = false;
642 } else {
643 autonomous = ::aos::robot_state->autonomous;
644 enabled = ::aos::robot_state->enabled;
645 }
Austin Schuh069143b2014-02-17 02:46:26 -0800646
647 double bottom_claw_velocity_ = 0.0;
648 double top_claw_velocity_ = 0.0;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800649
Brian Silverman71fbee02014-03-13 17:24:54 -0700650 if (goal != NULL &&
651 ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED &&
652 bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) ||
653 (autonomous &&
654 ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
655 top_claw_.zeroing_state() ==
656 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
657 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
658 bottom_claw_.zeroing_state() ==
659 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION))))) {
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800660 // Ready to use the claw.
661 // Limit the goals here.
Austin Schuhf9286cd2014-02-11 00:51:09 -0800662 bottom_claw_goal_ = goal->bottom_angle;
Brian Silverman7c021c42014-02-17 15:15:56 -0800663 top_claw_goal_ = goal->bottom_angle + goal->separation_angle;
Austin Schuhcda86af2014-02-16 16:16:39 -0800664 has_bottom_claw_goal_ = true;
665 has_top_claw_goal_ = true;
666 doing_calibration_fine_tune_ = false;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800667 mode_ = READY;
Brian Silverman084372e2014-04-10 10:55:53 -0700668
669 bottom_claw_.HandleCalibrationError(values.claw.lower_claw);
670 top_claw_.HandleCalibrationError(values.claw.upper_claw);
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800671 } else if (top_claw_.zeroing_state() !=
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000672 ZeroedStateFeedbackLoop::UNKNOWN_POSITION &&
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800673 bottom_claw_.zeroing_state() !=
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000674 ZeroedStateFeedbackLoop::UNKNOWN_POSITION) {
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800675 // Time to fine tune the zero.
676 // Limit the goals here.
Austin Schuh0c733422014-02-17 01:17:12 -0800677 if (!enabled) {
678 // If we are disabled, start the fine tune process over again.
679 doing_calibration_fine_tune_ = false;
680 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800681 if (bottom_claw_.zeroing_state() != ZeroedStateFeedbackLoop::CALIBRATED) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800682 // always get the bottom claw to calibrated first
683 LOG(DEBUG, "Calibrating the bottom of the claw\n");
684 if (!doing_calibration_fine_tune_) {
685 if (::std::abs(bottom_absolute_position() -
Austin Schuhd27931c2014-02-16 19:18:20 -0800686 values.claw.start_fine_tune_pos) <
687 values.claw.claw_unimportant_epsilon) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800688 doing_calibration_fine_tune_ = true;
Austin Schuhd27931c2014-02-16 19:18:20 -0800689 bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800690 top_claw_velocity_ = bottom_claw_velocity_ =
691 values.claw.claw_zeroing_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800692 LOG(DEBUG, "Ready to fine tune the bottom\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800693 mode_ = FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800694 } else {
695 // send bottom to zeroing start
Austin Schuhd27931c2014-02-16 19:18:20 -0800696 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuhcda86af2014-02-16 16:16:39 -0800697 LOG(DEBUG, "Going to the start position for the bottom\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800698 mode_ = PREP_FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800699 }
700 } else {
Austin Schuhe7f90d12014-02-17 00:48:25 -0800701 mode_ = FINE_TUNE_BOTTOM;
Austin Schuhd27931c2014-02-16 19:18:20 -0800702 bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800703 top_claw_velocity_ = bottom_claw_velocity_ =
704 values.claw.claw_zeroing_speed;
Brian Silvermane0a95462014-02-17 00:41:09 -0800705 if (top_claw_.front_or_back_triggered() ||
706 bottom_claw_.front_or_back_triggered()) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800707 // We shouldn't hit a limit, but if we do, go back to the zeroing
708 // point and try again.
709 doing_calibration_fine_tune_ = false;
Austin Schuhd27931c2014-02-16 19:18:20 -0800710 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuh069143b2014-02-17 02:46:26 -0800711 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
Austin Schuhcda86af2014-02-16 16:16:39 -0800712 LOG(DEBUG, "Found a limit, starting over.\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800713 mode_ = PREP_FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800714 }
Austin Schuh288c8c32014-02-16 17:20:17 -0800715
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000716 if (position && bottom_claw_.SawFilteredPosedge(
717 bottom_claw_.calibration(), bottom_claw_.front(),
718 bottom_claw_.back())) {
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000719 // do calibration
720 bottom_claw_.SetCalibration(
721 position->bottom.posedge_value,
722 values.claw.lower_claw.calibration.lower_angle);
723 bottom_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED);
724 // calibrated so we are done fine tuning bottom
725 doing_calibration_fine_tune_ = false;
726 LOG(DEBUG, "Calibrated the bottom correctly!\n");
727 } else if (bottom_claw_.calibration().last_value()) {
728 doing_calibration_fine_tune_ = false;
729 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
730 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
731 mode_ = PREP_FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800732 } else {
733 LOG(DEBUG, "Fine tuning\n");
734 }
735 }
736 // now set the top claw to track
737
Austin Schuhd27931c2014-02-16 19:18:20 -0800738 top_claw_goal_ = bottom_claw_goal_ + values.claw.claw_zeroing_separation;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800739 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800740 // bottom claw must be calibrated, start on the top
741 if (!doing_calibration_fine_tune_) {
Austin Schuhd27931c2014-02-16 19:18:20 -0800742 if (::std::abs(top_absolute_position() -
743 values.claw.start_fine_tune_pos) <
744 values.claw.claw_unimportant_epsilon) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800745 doing_calibration_fine_tune_ = true;
Austin Schuhd27931c2014-02-16 19:18:20 -0800746 top_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800747 top_claw_velocity_ = bottom_claw_velocity_ =
748 values.claw.claw_zeroing_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800749 LOG(DEBUG, "Ready to fine tune the top\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800750 mode_ = FINE_TUNE_TOP;
Austin Schuhcda86af2014-02-16 16:16:39 -0800751 } else {
752 // send top to zeroing start
Austin Schuhd27931c2014-02-16 19:18:20 -0800753 top_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuhcda86af2014-02-16 16:16:39 -0800754 LOG(DEBUG, "Going to the start position for the top\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800755 mode_ = PREP_FINE_TUNE_TOP;
Austin Schuhcda86af2014-02-16 16:16:39 -0800756 }
757 } else {
Austin Schuhe7f90d12014-02-17 00:48:25 -0800758 mode_ = FINE_TUNE_TOP;
Austin Schuhd27931c2014-02-16 19:18:20 -0800759 top_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800760 top_claw_velocity_ = bottom_claw_velocity_ =
761 values.claw.claw_zeroing_speed;
Brian Silvermane0a95462014-02-17 00:41:09 -0800762 if (top_claw_.front_or_back_triggered() ||
763 bottom_claw_.front_or_back_triggered()) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800764 // this should not happen, but now we know it won't
765 doing_calibration_fine_tune_ = false;
Austin Schuhd27931c2014-02-16 19:18:20 -0800766 top_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuh069143b2014-02-17 02:46:26 -0800767 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
Austin Schuhcda86af2014-02-16 16:16:39 -0800768 LOG(DEBUG, "Found a limit, starting over.\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800769 mode_ = PREP_FINE_TUNE_TOP;
Austin Schuhcda86af2014-02-16 16:16:39 -0800770 }
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000771
772 if (position &&
773 top_claw_.SawFilteredPosedge(top_claw_.calibration(),
774 top_claw_.front(), top_claw_.back())) {
775 // do calibration
776 top_claw_.SetCalibration(
777 position->top.posedge_value,
778 values.claw.upper_claw.calibration.lower_angle);
779 top_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED);
780 // calibrated so we are done fine tuning top
781 doing_calibration_fine_tune_ = false;
782 LOG(DEBUG, "Calibrated the top correctly!\n");
783 } else if (top_claw_.calibration().last_value()) {
784 doing_calibration_fine_tune_ = false;
785 top_claw_goal_ = values.claw.start_fine_tune_pos;
786 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
787 mode_ = PREP_FINE_TUNE_TOP;
Austin Schuhcda86af2014-02-16 16:16:39 -0800788 }
789 }
790 // now set the bottom claw to track
Austin Schuhd27931c2014-02-16 19:18:20 -0800791 bottom_claw_goal_ = top_claw_goal_ - values.claw.claw_zeroing_separation;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800792 }
793 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800794 doing_calibration_fine_tune_ = false;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800795 if (!was_enabled_ && enabled) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800796 if (position) {
Brian Silverman72035692014-03-14 10:18:27 -0700797 top_claw_goal_ = position->top.position + top_claw_.offset();
798 bottom_claw_goal_ = position->bottom.position + bottom_claw_.offset();
Austin Schuhe7f90d12014-02-17 00:48:25 -0800799 initial_separation_ =
Austin Schuhcda86af2014-02-16 16:16:39 -0800800 position->top.position - position->bottom.position;
Austin Schuhf9286cd2014-02-11 00:51:09 -0800801 } else {
802 has_top_claw_goal_ = false;
803 has_bottom_claw_goal_ = false;
804 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800805 }
Austin Schuhf9286cd2014-02-11 00:51:09 -0800806
Austin Schuh4cb047f2014-02-16 21:10:19 -0800807 if ((bottom_claw_.zeroing_state() !=
808 ZeroedStateFeedbackLoop::UNKNOWN_POSITION ||
Brian Silvermane0a95462014-02-17 00:41:09 -0800809 bottom_claw_.front().value() || top_claw_.front().value()) &&
810 !top_claw_.back().value() && !bottom_claw_.back().value()) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800811 if (enabled) {
812 // Time to slowly move back up to find any position to narrow down the
813 // zero.
Austin Schuhd27931c2014-02-16 19:18:20 -0800814 top_claw_goal_ += values.claw.claw_zeroing_off_speed * dt;
815 bottom_claw_goal_ += values.claw.claw_zeroing_off_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800816 top_claw_velocity_ = bottom_claw_velocity_ =
817 values.claw.claw_zeroing_off_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800818 LOG(DEBUG, "Bottom is known.\n");
Austin Schuhf9286cd2014-02-11 00:51:09 -0800819 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800820 } else {
821 // We don't know where either claw is. Slowly start moving down to find
822 // any hall effect.
Austin Schuhf9286cd2014-02-11 00:51:09 -0800823 if (enabled) {
Austin Schuhd27931c2014-02-16 19:18:20 -0800824 top_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt;
825 bottom_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800826 top_claw_velocity_ = bottom_claw_velocity_ =
827 -values.claw.claw_zeroing_off_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800828 LOG(DEBUG, "Both are unknown.\n");
Austin Schuhf9286cd2014-02-11 00:51:09 -0800829 }
830 }
831
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000832 if (position) {
833 if (enabled) {
834 top_claw_.SetCalibrationOnEdge(
835 values.claw.upper_claw,
836 ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION);
837 bottom_claw_.SetCalibrationOnEdge(
838 values.claw.lower_claw,
839 ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION);
840 } else {
841 // TODO(austin): Only calibrate on the predetermined edge.
842 // We might be able to just ignore this since the backlash is soooo
843 // low.
844 // :)
845 top_claw_.SetCalibrationOnEdge(
846 values.claw.upper_claw,
847 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
848 bottom_claw_.SetCalibrationOnEdge(
849 values.claw.lower_claw,
850 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
851 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800852 }
Austin Schuhe7f90d12014-02-17 00:48:25 -0800853 mode_ = UNKNOWN_LOCATION;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800854 }
855
Austin Schuh069143b2014-02-17 02:46:26 -0800856 // Limit the goals if both claws have been (mostly) found.
857 if (mode_ != UNKNOWN_LOCATION) {
858 LimitClawGoal(&bottom_claw_goal_, &top_claw_goal_, values);
859 }
860
Brian Silvermaned9df2f2014-04-05 07:07:15 -0700861 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 -0800862 if (has_top_claw_goal_ && has_bottom_claw_goal_) {
Austin Schuh069143b2014-02-17 02:46:26 -0800863 claw_.R << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_,
864 bottom_claw_velocity_, top_claw_velocity_ - bottom_claw_velocity_;
Austin Schuhcda86af2014-02-16 16:16:39 -0800865 double separation = -971;
866 if (position != nullptr) {
867 separation = position->top.position - position->bottom.position;
868 }
Brian Silvermanf48fab32014-03-09 14:32:24 -0700869 LOG_STRUCT(DEBUG, "actual goal",
870 ClawGoalToLog(claw_.R(0, 0), claw_.R(1, 0), separation));
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800871
Austin Schuh01c652b2014-02-21 23:13:42 -0800872 // Only cap power when one of the halves of the claw is moving slowly and
873 // could wind up.
Austin Schuhe7f90d12014-02-17 00:48:25 -0800874 claw_.set_is_zeroing(mode_ == UNKNOWN_LOCATION || mode_ == FINE_TUNE_TOP ||
875 mode_ == FINE_TUNE_BOTTOM);
Austin Schuhcda86af2014-02-16 16:16:39 -0800876 claw_.Update(output == nullptr);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800877 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800878 claw_.Update(true);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800879 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800880
Austin Schuh4cb047f2014-02-16 21:10:19 -0800881 capped_goal_ = false;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800882 switch (mode_) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800883 case READY:
Austin Schuhe7f90d12014-02-17 00:48:25 -0800884 case PREP_FINE_TUNE_TOP:
885 case PREP_FINE_TUNE_BOTTOM:
Austin Schuhcda86af2014-02-16 16:16:39 -0800886 break;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800887 case FINE_TUNE_BOTTOM:
888 case FINE_TUNE_TOP:
Austin Schuh4cb047f2014-02-16 21:10:19 -0800889 case UNKNOWN_LOCATION: {
Brian Silverman7b7c9072014-03-30 13:33:30 -0700890 LOG_MATRIX(DEBUG, "U_uncapped", claw_.U_uncapped);
Austin Schuh4cb047f2014-02-16 21:10:19 -0800891 if (claw_.uncapped_average_voltage() > values.claw.max_zeroing_voltage) {
Brian Silverman6dd2c532014-03-29 23:34:39 -0700892 double dx_bot = (claw_.U_uncapped(0, 0) -
Austin Schuh4cb047f2014-02-16 21:10:19 -0800893 values.claw.max_zeroing_voltage) /
894 claw_.K(0, 0);
Brian Silverman6dd2c532014-03-29 23:34:39 -0700895 double dx_top = (claw_.U_uncapped(1, 0) -
James Kuszmaul0e866512014-02-21 13:12:52 -0800896 values.claw.max_zeroing_voltage) /
897 claw_.K(0, 0);
898 double dx = ::std::max(dx_top, dx_bot);
Austin Schuh4cb047f2014-02-16 21:10:19 -0800899 bottom_claw_goal_ -= dx;
900 top_claw_goal_ -= dx;
James Kuszmaul0e866512014-02-21 13:12:52 -0800901 Eigen::Matrix<double, 4, 1> R;
902 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 -0700903 claw_.U = claw_.K() * (R - claw_.X_hat);
Austin Schuhcda86af2014-02-16 16:16:39 -0800904 capped_goal_ = true;
Brian Silvermanf48fab32014-03-09 14:32:24 -0700905 LOG(DEBUG, "Moving the goal by %f to prevent windup."
906 " Uncapped is %f, max is %f, difference is %f\n",
907 dx,
Austin Schuhe7f90d12014-02-17 00:48:25 -0800908 claw_.uncapped_average_voltage(), values.claw.max_zeroing_voltage,
909 (claw_.uncapped_average_voltage() -
910 values.claw.max_zeroing_voltage));
Austin Schuh4cb047f2014-02-16 21:10:19 -0800911 } else if (claw_.uncapped_average_voltage() <
912 -values.claw.max_zeroing_voltage) {
Brian Silverman6dd2c532014-03-29 23:34:39 -0700913 double dx_bot = (claw_.U_uncapped(0, 0) +
Austin Schuh4cb047f2014-02-16 21:10:19 -0800914 values.claw.max_zeroing_voltage) /
915 claw_.K(0, 0);
Brian Silverman6dd2c532014-03-29 23:34:39 -0700916 double dx_top = (claw_.U_uncapped(1, 0) +
James Kuszmaul0e866512014-02-21 13:12:52 -0800917 values.claw.max_zeroing_voltage) /
918 claw_.K(0, 0);
919 double dx = ::std::min(dx_top, dx_bot);
Austin Schuh4cb047f2014-02-16 21:10:19 -0800920 bottom_claw_goal_ -= dx;
921 top_claw_goal_ -= dx;
James Kuszmaul0e866512014-02-21 13:12:52 -0800922 Eigen::Matrix<double, 4, 1> R;
923 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 -0700924 claw_.U = claw_.K() * (R - claw_.X_hat);
Austin Schuhcda86af2014-02-16 16:16:39 -0800925 capped_goal_ = true;
Austin Schuh4cb047f2014-02-16 21:10:19 -0800926 LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx);
Austin Schuhcda86af2014-02-16 16:16:39 -0800927 }
Austin Schuh4cb047f2014-02-16 21:10:19 -0800928 } break;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800929 }
930
931 if (output) {
Ben Fredrickson2f76ddf2014-02-23 05:58:23 +0000932 if (goal) {
933 //setup the intake
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000934 output->intake_voltage =
935 (goal->intake > 12.0) ? 12 : (goal->intake < -12.0) ? -12.0
936 : goal->intake;
Ben Fredrickson2f76ddf2014-02-23 05:58:23 +0000937 output->tusk_voltage = goal->centering;
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000938 output->tusk_voltage =
939 (goal->centering > 12.0) ? 12 : (goal->centering < -12.0)
940 ? -12.0
941 : goal->centering;
Ben Fredrickson2f76ddf2014-02-23 05:58:23 +0000942 }
James Kuszmauld536a402014-02-18 22:32:12 -0800943 output->top_claw_voltage = claw_.U(1, 0);
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000944 output->bottom_claw_voltage = claw_.U(0, 0);
Austin Schuhf84a1302014-02-19 00:23:30 -0800945
946 if (output->top_claw_voltage > kMaxVoltage) {
947 output->top_claw_voltage = kMaxVoltage;
948 } else if (output->top_claw_voltage < -kMaxVoltage) {
949 output->top_claw_voltage = -kMaxVoltage;
950 }
951
952 if (output->bottom_claw_voltage > kMaxVoltage) {
953 output->bottom_claw_voltage = kMaxVoltage;
954 } else if (output->bottom_claw_voltage < -kMaxVoltage) {
955 output->bottom_claw_voltage = -kMaxVoltage;
956 }
Austin Schuh3bb9a442014-02-02 16:01:45 -0800957 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800958
James Kuszmaul4abaf482014-02-26 21:16:35 -0800959 status->bottom = bottom_absolute_position();
960 status->separation = top_absolute_position() - bottom_absolute_position();
961 status->bottom_velocity = claw_.X_hat(2, 0);
962 status->separation_velocity = claw_.X_hat(3, 0);
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800963
Brian Silverman71fbee02014-03-13 17:24:54 -0700964 if (goal) {
965 bool bottom_done =
966 ::std::abs(bottom_absolute_position() - goal->bottom_angle) < 0.020;
967 bool bottom_velocity_done = ::std::abs(status->bottom_velocity) < 0.2;
968 bool separation_done =
969 ::std::abs((top_absolute_position() - bottom_absolute_position()) -
970 goal->separation_angle) < 0.020;
971 bool separation_done_with_ball =
972 ::std::abs((top_absolute_position() - bottom_absolute_position()) -
973 goal->separation_angle) < 0.06;
974 status->done = is_ready() && separation_done && bottom_done && bottom_velocity_done;
975 status->done_with_ball =
976 is_ready() && separation_done_with_ball && bottom_done && bottom_velocity_done;
977 } else {
978 status->done = status->done_with_ball = false;
979 }
Austin Schuha556b012014-03-02 11:55:52 -0800980
Austin Schuh4f8633f2014-03-02 13:59:46 -0800981 status->zeroed = is_ready();
Brian Silverman80fc94c2014-03-09 16:56:01 -0700982 status->zeroed_for_auto =
983 (top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
984 top_claw_.zeroing_state() ==
985 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
986 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
987 bottom_claw_.zeroing_state() ==
988 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
Austin Schuh4f8633f2014-03-02 13:59:46 -0800989
Brian Silverman71fbee02014-03-13 17:24:54 -0700990 was_enabled_ = enabled;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800991}
992
993} // namespace control_loops
994} // namespace frc971
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000995