blob: 32fe56772e1410dbcb4a80e808b8790c09c385ca [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;
Austin Schuh3ba10f022014-12-14 14:19:51 -080052const double kRezeroThreshold = 0.07;
Austin Schuhf84a1302014-02-19 00:23:30 -080053
Brian Silverman0a151c92014-05-02 15:28:44 -070054ClawLimitedLoop::ClawLimitedLoop(StateFeedbackLoop<4, 2, 2> &&loop)
55 : StateFeedbackLoop<4, 2, 2>(::std::move(loop)),
Austin Schuh27b8fb12014-02-22 15:10:05 -080056 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 Silverman273d8a32014-05-10 22:19:09 -070082 const Eigen::Matrix<double, 4, 1> error = R() - X_hat();
Austin Schuh4cb047f2014-02-16 21:10:19 -080083
Brian Silvermana21c3a22014-06-12 21:49:15 -070084 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 Silverman273d8a32014-05-10 22:19:09 -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.
Brian Silvermana21c3a22014-06-12 21:49:15 -0700132 if (X_hat(0, 0) + X_hat(1, 0) >
Brian Silvermanb087c0a2014-03-30 12:59:52 -0700133 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);
Brian Silverman0ca790b2014-06-12 21:33:08 -0700180 mutable_U() = velocity_K * velocity_error + position_K * adjusted_pos_error;
Brian Silverman273d8a32014-05-10 22:19:09 -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_) {
Brian Silvermana21c3a22014-06-12 21:49:15 -0700186 if (X_hat(0, 0) + X_hat(1, 0) > values.upper_claw.upper_limit && U(1, 0) > 0) {
Brian Silvermaned9df2f2014-04-05 07:07:15 -0700187 LOG(WARNING, "upper claw too high and moving up\n");
Brian Silvermana21c3a22014-06-12 21:49:15 -0700188 mutable_U(1, 0) = 0;
189 } else if (X_hat(0, 0) + X_hat(1, 0) < values.upper_claw.lower_limit &&
190 U(1, 0) < 0) {
Brian Silvermaned9df2f2014-04-05 07:07:15 -0700191 LOG(WARNING, "upper claw too low and moving down\n");
Brian Silvermana21c3a22014-06-12 21:49:15 -0700192 mutable_U(1, 0) = 0;
Brian Silvermaned9df2f2014-04-05 07:07:15 -0700193 }
Brian Silverman7b7c9072014-03-30 13:33:30 -0700194 }
Brian Silvermaned9df2f2014-04-05 07:07:15 -0700195 if (bottom_known_) {
Brian Silvermana21c3a22014-06-12 21:49:15 -0700196 if (X_hat(0, 0) > values.lower_claw.upper_limit && U(0, 0) > 0) {
Brian Silvermaned9df2f2014-04-05 07:07:15 -0700197 LOG(WARNING, "lower claw too high and moving up\n");
Brian Silvermana21c3a22014-06-12 21:49:15 -0700198 mutable_U(0, 0) = 0;
199 } else if (X_hat(0, 0) < values.lower_claw.lower_limit && U(0, 0) < 0) {
Brian Silvermaned9df2f2014-04-05 07:07:15 -0700200 LOG(WARNING, "lower claw too low and moving down\n");
Brian Silvermana21c3a22014-06-12 21:49:15 -0700201 mutable_U(0, 0) = 0;
Brian Silvermaned9df2f2014-04-05 07:07:15 -0700202 }
Brian Silverman7b7c9072014-03-30 13:33:30 -0700203 }
204 }
James Kuszmauld536a402014-02-18 22:32:12 -0800205 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800206}
207
Austin Schuh27b8fb12014-02-22 15:10:05 -0800208ZeroedStateFeedbackLoop::ZeroedStateFeedbackLoop(const char *name,
209 ClawMotor *motor)
210 : offset_(0.0),
211 name_(name),
212 motor_(motor),
213 zeroing_state_(UNKNOWN_POSITION),
214 posedge_value_(0.0),
215 negedge_value_(0.0),
216 encoder_(0.0),
217 last_encoder_(0.0) {}
218
219void ZeroedStateFeedbackLoop::SetPositionValues(const HalfClawPosition &claw) {
220 front_.Update(claw.front);
221 calibration_.Update(claw.calibration);
222 back_.Update(claw.back);
223
224 bool any_sensor_triggered = any_triggered();
225 if (any_sensor_triggered && any_triggered_last_) {
226 // We are still on the hall effect and nothing has changed.
227 min_hall_effect_on_angle_ =
228 ::std::min(min_hall_effect_on_angle_, claw.position);
229 max_hall_effect_on_angle_ =
230 ::std::max(max_hall_effect_on_angle_, claw.position);
231 } else if (!any_sensor_triggered && !any_triggered_last_) {
232 // We are still off the hall effect and nothing has changed.
233 min_hall_effect_off_angle_ =
234 ::std::min(min_hall_effect_off_angle_, claw.position);
235 max_hall_effect_off_angle_ =
236 ::std::max(max_hall_effect_off_angle_, claw.position);
237 } else if (any_sensor_triggered && !any_triggered_last_) {
238 // Saw a posedge on the hall effect. Reset the limits.
239 min_hall_effect_on_angle_ = ::std::min(claw.posedge_value, claw.position);
240 max_hall_effect_on_angle_ = ::std::max(claw.posedge_value, claw.position);
241 } else if (!any_sensor_triggered && any_triggered_last_) {
242 // Saw a negedge on the hall effect. Reset the limits.
243 min_hall_effect_off_angle_ = ::std::min(claw.negedge_value, claw.position);
244 max_hall_effect_off_angle_ = ::std::max(claw.negedge_value, claw.position);
245 }
246
247 posedge_value_ = claw.posedge_value;
248 negedge_value_ = claw.negedge_value;
249 last_encoder_ = encoder_;
250 if (front().value() || calibration().value() || back().value()) {
251 last_on_encoder_ = encoder_;
252 } else {
253 last_off_encoder_ = encoder_;
254 }
255 encoder_ = claw.position;
256 any_triggered_last_ = any_sensor_triggered;
257}
258
259void ZeroedStateFeedbackLoop::Reset(const HalfClawPosition &claw) {
260 set_zeroing_state(ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
261
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000262 front_.Reset(claw.front);
263 calibration_.Reset(claw.calibration);
264 back_.Reset(claw.back);
Austin Schuh27b8fb12014-02-22 15:10:05 -0800265 // close up the min and max edge positions as they are no longer valid and
266 // will be expanded in future iterations
267 min_hall_effect_on_angle_ = claw.position;
268 max_hall_effect_on_angle_ = claw.position;
269 min_hall_effect_off_angle_ = claw.position;
270 max_hall_effect_off_angle_ = claw.position;
271 any_triggered_last_ = any_triggered();
272}
273
274bool TopZeroedStateFeedbackLoop::SetCalibrationOnEdge(
275 const constants::Values::Claws::Claw &claw_values,
276 JointZeroingState zeroing_state) {
277 double edge_encoder;
278 double edge_angle;
279 if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) {
280 LOG(INFO, "Calibration edge edge should be %f.\n", edge_angle);
281 SetCalibration(edge_encoder, edge_angle);
282 set_zeroing_state(zeroing_state);
283 return true;
284 }
285 return false;
286}
287
Brian Silverman084372e2014-04-10 10:55:53 -0700288void TopZeroedStateFeedbackLoop::HandleCalibrationError(
289 const constants::Values::Claws::Claw &claw_values) {
290 double edge_encoder;
291 double edge_angle;
292 if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) {
293 const double calibration_error =
294 ComputeCalibrationChange(edge_encoder, edge_angle);
295 LOG(INFO, "Top calibration error is %f\n", calibration_error);
296 if (::std::abs(calibration_error) > kRezeroThreshold) {
Brian Silvermane4c701d2014-04-10 19:29:25 -0700297 LOG(WARNING, "rezeroing top\n");
Brian Silverman084372e2014-04-10 10:55:53 -0700298 SetCalibration(edge_encoder, edge_angle);
Brian Silverman640f97a2014-04-19 16:07:55 -0700299 set_zeroing_state(ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
Brian Silverman084372e2014-04-10 10:55:53 -0700300 }
301 }
302}
303
304
305void BottomZeroedStateFeedbackLoop::HandleCalibrationError(
306 const constants::Values::Claws::Claw &claw_values) {
307 double edge_encoder;
308 double edge_angle;
309 if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) {
310 const double calibration_error =
311 ComputeCalibrationChange(edge_encoder, edge_angle);
312 LOG(INFO, "Bottom calibration error is %f\n", calibration_error);
313 if (::std::abs(calibration_error) > kRezeroThreshold) {
Brian Silvermane4c701d2014-04-10 19:29:25 -0700314 LOG(WARNING, "rezeroing bottom\n");
Brian Silverman084372e2014-04-10 10:55:53 -0700315 SetCalibration(edge_encoder, edge_angle);
Brian Silverman640f97a2014-04-19 16:07:55 -0700316 set_zeroing_state(ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
Brian Silverman084372e2014-04-10 10:55:53 -0700317 }
318 }
319}
320
Austin Schuh27b8fb12014-02-22 15:10:05 -0800321bool BottomZeroedStateFeedbackLoop::SetCalibrationOnEdge(
322 const constants::Values::Claws::Claw &claw_values,
323 JointZeroingState zeroing_state) {
324 double edge_encoder;
325 double edge_angle;
326 if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) {
327 LOG(INFO, "Calibration edge.\n");
328 SetCalibration(edge_encoder, edge_angle);
329 set_zeroing_state(zeroing_state);
330 return true;
331 }
332 return false;
333}
334
Austin Schuhcc0bf312014-02-09 00:39:29 -0800335ClawMotor::ClawMotor(control_loops::ClawGroup *my_claw)
Brian Silverman38111502014-04-10 12:36:26 -0700336 : aos::controls::ControlLoop<control_loops::ClawGroup, true, true,
Brian Silverman71fbee02014-03-13 17:24:54 -0700337 false>(my_claw),
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800338 has_top_claw_goal_(false),
339 top_claw_goal_(0.0),
Austin Schuhcda86af2014-02-16 16:16:39 -0800340 top_claw_(this),
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800341 has_bottom_claw_goal_(false),
342 bottom_claw_goal_(0.0),
Austin Schuhcda86af2014-02-16 16:16:39 -0800343 bottom_claw_(this),
344 claw_(MakeClawLoop()),
Ben Fredrickson9b388422014-02-13 06:15:31 +0000345 was_enabled_(false),
Austin Schuh4cb047f2014-02-16 21:10:19 -0800346 doing_calibration_fine_tune_(false),
Austin Schuhe7f90d12014-02-17 00:48:25 -0800347 capped_goal_(false),
348 mode_(UNKNOWN_LOCATION) {}
Austin Schuh3bb9a442014-02-02 16:01:45 -0800349
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800350const int ZeroedStateFeedbackLoop::kZeroingMaxVoltage;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800351
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000352bool ZeroedStateFeedbackLoop::SawFilteredPosedge(
353 const HallEffectTracker &this_sensor, const HallEffectTracker &sensorA,
354 const HallEffectTracker &sensorB) {
355 if (posedge_filter_ == nullptr && this_sensor.posedge_count_changed() &&
356 !sensorA.posedge_count_changed() && !sensorB.posedge_count_changed() &&
357 this_sensor.value() && !this_sensor.last_value()) {
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000358 posedge_filter_ = &this_sensor;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000359 } else if (posedge_filter_ == &this_sensor &&
360 !this_sensor.posedge_count_changed() &&
361 !sensorA.posedge_count_changed() &&
362 !sensorB.posedge_count_changed() && this_sensor.value()) {
363 posedge_filter_ = nullptr;
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000364 return true;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000365 } else if (posedge_filter_ == &this_sensor) {
366 posedge_filter_ = nullptr;
367 }
368 return false;
369}
370
371bool ZeroedStateFeedbackLoop::SawFilteredNegedge(
372 const HallEffectTracker &this_sensor, const HallEffectTracker &sensorA,
373 const HallEffectTracker &sensorB) {
374 if (negedge_filter_ == nullptr && this_sensor.negedge_count_changed() &&
375 !sensorA.negedge_count_changed() && !sensorB.negedge_count_changed() &&
376 !this_sensor.value() && this_sensor.last_value()) {
377 negedge_filter_ = &this_sensor;
378 } else if (negedge_filter_ == &this_sensor &&
379 !this_sensor.negedge_count_changed() &&
380 !sensorA.negedge_count_changed() &&
381 !sensorB.negedge_count_changed() && !this_sensor.value()) {
382 negedge_filter_ = nullptr;
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000383 return true;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000384 } else if (negedge_filter_ == &this_sensor) {
385 negedge_filter_ = nullptr;
386 }
387 return false;
388}
389
Brian Silvermane0a95462014-02-17 00:41:09 -0800390bool ZeroedStateFeedbackLoop::DoGetPositionOfEdge(
391 const constants::Values::Claws::AnglePair &angles, double *edge_encoder,
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000392 double *edge_angle, const HallEffectTracker &this_sensor,
393 const HallEffectTracker &sensorA, const HallEffectTracker &sensorB,
Brian Silvermane0a95462014-02-17 00:41:09 -0800394 const char *hall_effect_name) {
Austin Schuhf84a1302014-02-19 00:23:30 -0800395 bool found_edge = false;
Austin Schuh27b8fb12014-02-22 15:10:05 -0800396
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000397 if (SawFilteredPosedge(this_sensor, sensorA, sensorB)) {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800398 if (min_hall_effect_off_angle_ == max_hall_effect_off_angle_) {
Brian Silvermanf48fab32014-03-09 14:32:24 -0700399 LOG(WARNING, "%s: Uncertain which side, rejecting posedge\n", name_);
Brian Silvermane0a95462014-02-17 00:41:09 -0800400 } else {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800401 const double average_last_encoder =
402 (min_hall_effect_off_angle_ + max_hall_effect_off_angle_) / 2.0;
403 if (posedge_value_ < average_last_encoder) {
404 *edge_angle = angles.upper_decreasing_angle;
405 LOG(INFO, "%s Posedge upper of %s -> %f posedge: %f avg_encoder: %f\n",
406 name_, hall_effect_name, *edge_angle, posedge_value_,
407 average_last_encoder);
408 } else {
409 *edge_angle = angles.lower_angle;
410 LOG(INFO, "%s Posedge lower of %s -> %f posedge: %f avg_encoder: %f\n",
411 name_, hall_effect_name, *edge_angle, posedge_value_,
412 average_last_encoder);
413 }
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000414 *edge_encoder = posedge_value_;
Austin Schuh27b8fb12014-02-22 15:10:05 -0800415 found_edge = true;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000416 }
417 }
418
419 if (SawFilteredNegedge(this_sensor, sensorA, sensorB)) {
420 if (min_hall_effect_on_angle_ == max_hall_effect_on_angle_) {
Brian Silvermanf48fab32014-03-09 14:32:24 -0700421 LOG(WARNING, "%s: Uncertain which side, rejecting negedge\n", name_);
Brian Silvermane0a95462014-02-17 00:41:09 -0800422 } else {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800423 const double average_last_encoder =
424 (min_hall_effect_on_angle_ + max_hall_effect_on_angle_) / 2.0;
425 if (negedge_value_ > average_last_encoder) {
426 *edge_angle = angles.upper_angle;
427 LOG(INFO, "%s Negedge upper of %s -> %f negedge: %f avg_encoder: %f\n",
428 name_, hall_effect_name, *edge_angle, negedge_value_,
429 average_last_encoder);
430 } else {
431 *edge_angle = angles.lower_decreasing_angle;
432 LOG(INFO, "%s Negedge lower of %s -> %f negedge: %f avg_encoder: %f\n",
433 name_, hall_effect_name, *edge_angle, negedge_value_,
434 average_last_encoder);
435 }
436 *edge_encoder = negedge_value_;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000437 found_edge = true;
Brian Silvermane0a95462014-02-17 00:41:09 -0800438 }
Austin Schuh27b8fb12014-02-22 15:10:05 -0800439 }
440
Austin Schuhf84a1302014-02-19 00:23:30 -0800441 return found_edge;
Brian Silvermane0a95462014-02-17 00:41:09 -0800442}
443
Austin Schuhf9286cd2014-02-11 00:51:09 -0800444bool ZeroedStateFeedbackLoop::GetPositionOfEdge(
Austin Schuhd27931c2014-02-16 19:18:20 -0800445 const constants::Values::Claws::Claw &claw_values, double *edge_encoder,
Austin Schuhf9286cd2014-02-11 00:51:09 -0800446 double *edge_angle) {
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000447 if (DoGetPositionOfEdge(claw_values.front, edge_encoder, edge_angle, front_,
448 calibration_, back_, "front")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800449 return true;
450 }
Brian Silvermane0a95462014-02-17 00:41:09 -0800451 if (DoGetPositionOfEdge(claw_values.calibration, edge_encoder, edge_angle,
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000452 calibration_, front_, back_, "calibration")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800453 return true;
454 }
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000455 if (DoGetPositionOfEdge(claw_values.back, edge_encoder, edge_angle, back_,
456 calibration_, front_, "back")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800457 return true;
458 }
459 return false;
460}
461
Austin Schuhcda86af2014-02-16 16:16:39 -0800462void TopZeroedStateFeedbackLoop::SetCalibration(double edge_encoder,
463 double edge_angle) {
464 double old_offset = offset_;
465 offset_ = edge_angle - edge_encoder;
466 const double doffset = offset_ - old_offset;
467 motor_->ChangeTopOffset(doffset);
468}
469
Brian Silverman084372e2014-04-10 10:55:53 -0700470double TopZeroedStateFeedbackLoop::ComputeCalibrationChange(double edge_encoder,
471 double edge_angle) {
472 const double offset = edge_angle - edge_encoder;
473 const double doffset = offset - offset_;
474 return doffset;
475}
476
Austin Schuhcda86af2014-02-16 16:16:39 -0800477void BottomZeroedStateFeedbackLoop::SetCalibration(double edge_encoder,
478 double edge_angle) {
479 double old_offset = offset_;
480 offset_ = edge_angle - edge_encoder;
481 const double doffset = offset_ - old_offset;
482 motor_->ChangeBottomOffset(doffset);
483}
484
Brian Silverman084372e2014-04-10 10:55:53 -0700485double BottomZeroedStateFeedbackLoop::ComputeCalibrationChange(
486 double edge_encoder, double edge_angle) {
487 const double offset = edge_angle - edge_encoder;
488 const double doffset = offset - offset_;
489 return doffset;
490}
491
Austin Schuhcda86af2014-02-16 16:16:39 -0800492void ClawMotor::ChangeTopOffset(double doffset) {
493 claw_.ChangeTopOffset(doffset);
494 if (has_top_claw_goal_) {
495 top_claw_goal_ += doffset;
496 }
497}
498
499void ClawMotor::ChangeBottomOffset(double doffset) {
500 claw_.ChangeBottomOffset(doffset);
501 if (has_bottom_claw_goal_) {
502 bottom_claw_goal_ += doffset;
503 }
504}
505
506void ClawLimitedLoop::ChangeTopOffset(double doffset) {
Brian Silverman73df9342014-06-12 23:33:16 -0700507 mutable_Y()(1, 0) += doffset;
508 mutable_X_hat()(1, 0) += doffset;
Austin Schuhcda86af2014-02-16 16:16:39 -0800509 LOG(INFO, "Changing top offset by %f\n", doffset);
510}
511void ClawLimitedLoop::ChangeBottomOffset(double doffset) {
Brian Silverman73df9342014-06-12 23:33:16 -0700512 mutable_Y()(0, 0) += doffset;
513 mutable_X_hat()(0, 0) += doffset;
514 mutable_X_hat()(1, 0) -= doffset;
Austin Schuhcda86af2014-02-16 16:16:39 -0800515 LOG(INFO, "Changing bottom offset by %f\n", doffset);
516}
joe7376ff52014-02-16 18:28:42 -0800517
Austin Schuh069143b2014-02-17 02:46:26 -0800518void LimitClawGoal(double *bottom_goal, double *top_goal,
519 const frc971::constants::Values &values) {
520 // first update position based on angle limit
Austin Schuh069143b2014-02-17 02:46:26 -0800521 const double separation = *top_goal - *bottom_goal;
Brian Silverman06374312014-04-12 16:09:28 -0700522 if (separation > values.claw.soft_max_separation) {
Brian Silverman11c6f7f2014-04-30 17:38:21 -0700523 LOG_STRUCT(DEBUG, "before", ClawPositionToLog(*top_goal, *bottom_goal));
Brian Silverman06374312014-04-12 16:09:28 -0700524 const double dsep = (separation - values.claw.soft_max_separation) / 2.0;
Austin Schuh069143b2014-02-17 02:46:26 -0800525 *bottom_goal += dsep;
526 *top_goal -= dsep;
Brian Silverman11c6f7f2014-04-30 17:38:21 -0700527 LOG_STRUCT(DEBUG, "after", ClawPositionToLog(*top_goal, *bottom_goal));
Austin Schuh069143b2014-02-17 02:46:26 -0800528 }
Brian Silverman06374312014-04-12 16:09:28 -0700529 if (separation < values.claw.soft_min_separation) {
Brian Silverman11c6f7f2014-04-30 17:38:21 -0700530 LOG_STRUCT(DEBUG, "before", ClawPositionToLog(*top_goal, *bottom_goal));
Brian Silverman06374312014-04-12 16:09:28 -0700531 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;
Brian Silverman11c6f7f2014-04-30 17:38:21 -0700534 LOG_STRUCT(DEBUG, "after", ClawPositionToLog(*top_goal, *bottom_goal));
Austin Schuh069143b2014-02-17 02:46:26 -0800535 }
536
537 // now move both goals in unison
538 if (*bottom_goal < values.claw.lower_claw.lower_limit) {
Brian Silverman11c6f7f2014-04-30 17:38:21 -0700539 LOG_STRUCT(DEBUG, "before", ClawPositionToLog(*top_goal, *bottom_goal));
Austin Schuh069143b2014-02-17 02:46:26 -0800540 *top_goal += values.claw.lower_claw.lower_limit - *bottom_goal;
541 *bottom_goal = values.claw.lower_claw.lower_limit;
Brian Silverman11c6f7f2014-04-30 17:38:21 -0700542 LOG_STRUCT(DEBUG, "after", ClawPositionToLog(*top_goal, *bottom_goal));
Austin Schuh069143b2014-02-17 02:46:26 -0800543 }
544 if (*bottom_goal > values.claw.lower_claw.upper_limit) {
Brian Silverman11c6f7f2014-04-30 17:38:21 -0700545 LOG_STRUCT(DEBUG, "before", ClawPositionToLog(*top_goal, *bottom_goal));
Austin Schuh069143b2014-02-17 02:46:26 -0800546 *top_goal -= *bottom_goal - values.claw.lower_claw.upper_limit;
547 *bottom_goal = values.claw.lower_claw.upper_limit;
Brian Silverman11c6f7f2014-04-30 17:38:21 -0700548 LOG_STRUCT(DEBUG, "after", ClawPositionToLog(*top_goal, *bottom_goal));
Austin Schuh069143b2014-02-17 02:46:26 -0800549 }
550
551 if (*top_goal < values.claw.upper_claw.lower_limit) {
Brian Silverman11c6f7f2014-04-30 17:38:21 -0700552 LOG_STRUCT(DEBUG, "before", ClawPositionToLog(*top_goal, *bottom_goal));
Austin Schuh069143b2014-02-17 02:46:26 -0800553 *bottom_goal += values.claw.upper_claw.lower_limit - *top_goal;
554 *top_goal = values.claw.upper_claw.lower_limit;
Brian Silverman11c6f7f2014-04-30 17:38:21 -0700555 LOG_STRUCT(DEBUG, "after", ClawPositionToLog(*top_goal, *bottom_goal));
Austin Schuh069143b2014-02-17 02:46:26 -0800556 }
557 if (*top_goal > values.claw.upper_claw.upper_limit) {
Brian Silverman11c6f7f2014-04-30 17:38:21 -0700558 LOG_STRUCT(DEBUG, "before", ClawPositionToLog(*top_goal, *bottom_goal));
Austin Schuh069143b2014-02-17 02:46:26 -0800559 *bottom_goal -= *top_goal - values.claw.upper_claw.upper_limit;
560 *top_goal = values.claw.upper_claw.upper_limit;
Brian Silverman11c6f7f2014-04-30 17:38:21 -0700561 LOG_STRUCT(DEBUG, "after", ClawPositionToLog(*top_goal, *bottom_goal));
Austin Schuh069143b2014-02-17 02:46:26 -0800562 }
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()) {
Brian Silvermane4c701d2014-04-10 19:29:25 -0700728 LOG(DEBUG, "Aborting bottom fine tune because sensor triggered\n");
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000729 doing_calibration_fine_tune_ = false;
Brian Silvermancc3844a2014-04-10 21:15:07 -0700730 bottom_claw_.set_zeroing_state(
731 ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
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()) {
Brian Silvermane4c701d2014-04-10 19:29:25 -0700784 LOG(DEBUG, "Aborting top fine tune because sensor triggered\n");
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000785 doing_calibration_fine_tune_ = false;
Brian Silvermancc3844a2014-04-10 21:15:07 -0700786 top_claw_.set_zeroing_state(
787 ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
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 Silverman273d8a32014-05-10 22:19:09 -0700861 claw_.set_positions_known(
862 top_claw_.zeroing_state() != ZeroedStateFeedbackLoop::UNKNOWN_POSITION,
863 bottom_claw_.zeroing_state() !=
864 ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800865 if (has_top_claw_goal_ && has_bottom_claw_goal_) {
Brian Silverman0ca790b2014-06-12 21:33:08 -0700866 claw_.mutable_R() << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_,
Austin Schuh069143b2014-02-17 02:46:26 -0800867 bottom_claw_velocity_, top_claw_velocity_ - bottom_claw_velocity_;
Brian Silverman273d8a32014-05-10 22:19:09 -0700868 LOG_MATRIX(DEBUG, "actual goal", claw_.R());
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800869
Austin Schuh01c652b2014-02-21 23:13:42 -0800870 // Only cap power when one of the halves of the claw is moving slowly and
871 // could wind up.
Austin Schuhe7f90d12014-02-17 00:48:25 -0800872 claw_.set_is_zeroing(mode_ == UNKNOWN_LOCATION || mode_ == FINE_TUNE_TOP ||
873 mode_ == FINE_TUNE_BOTTOM);
Austin Schuhcda86af2014-02-16 16:16:39 -0800874 claw_.Update(output == nullptr);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800875 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800876 claw_.Update(true);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800877 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800878
Austin Schuh4cb047f2014-02-16 21:10:19 -0800879 capped_goal_ = false;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800880 switch (mode_) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800881 case READY:
Austin Schuhe7f90d12014-02-17 00:48:25 -0800882 case PREP_FINE_TUNE_TOP:
883 case PREP_FINE_TUNE_BOTTOM:
Austin Schuhcda86af2014-02-16 16:16:39 -0800884 break;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800885 case FINE_TUNE_BOTTOM:
886 case FINE_TUNE_TOP:
Austin Schuh4cb047f2014-02-16 21:10:19 -0800887 case UNKNOWN_LOCATION: {
888 if (claw_.uncapped_average_voltage() > values.claw.max_zeroing_voltage) {
Brian Silvermana21c3a22014-06-12 21:49:15 -0700889 double dx_bot = (claw_.U_uncapped(0, 0) -
Austin Schuh4cb047f2014-02-16 21:10:19 -0800890 values.claw.max_zeroing_voltage) /
891 claw_.K(0, 0);
Brian Silvermana21c3a22014-06-12 21:49:15 -0700892 double dx_top = (claw_.U_uncapped(1, 0) -
James Kuszmaul0e866512014-02-21 13:12:52 -0800893 values.claw.max_zeroing_voltage) /
894 claw_.K(0, 0);
895 double dx = ::std::max(dx_top, dx_bot);
Austin Schuh4cb047f2014-02-16 21:10:19 -0800896 bottom_claw_goal_ -= dx;
897 top_claw_goal_ -= dx;
James Kuszmaul0e866512014-02-21 13:12:52 -0800898 Eigen::Matrix<double, 4, 1> R;
Brian Silvermana21c3a22014-06-12 21:49:15 -0700899 R << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_, claw_.R(2, 0),
900 claw_.R(3, 0);
Brian Silverman0ca790b2014-06-12 21:33:08 -0700901 claw_.mutable_U() = claw_.K() * (R - claw_.X_hat());
Austin Schuhcda86af2014-02-16 16:16:39 -0800902 capped_goal_ = true;
Brian Silvermanf48fab32014-03-09 14:32:24 -0700903 LOG(DEBUG, "Moving the goal by %f to prevent windup."
904 " Uncapped is %f, max is %f, difference is %f\n",
905 dx,
Austin Schuhe7f90d12014-02-17 00:48:25 -0800906 claw_.uncapped_average_voltage(), values.claw.max_zeroing_voltage,
907 (claw_.uncapped_average_voltage() -
908 values.claw.max_zeroing_voltage));
Austin Schuh4cb047f2014-02-16 21:10:19 -0800909 } else if (claw_.uncapped_average_voltage() <
910 -values.claw.max_zeroing_voltage) {
Brian Silvermana21c3a22014-06-12 21:49:15 -0700911 double dx_bot = (claw_.U_uncapped(0, 0) +
Austin Schuh4cb047f2014-02-16 21:10:19 -0800912 values.claw.max_zeroing_voltage) /
913 claw_.K(0, 0);
Brian Silvermana21c3a22014-06-12 21:49:15 -0700914 double dx_top = (claw_.U_uncapped(1, 0) +
James Kuszmaul0e866512014-02-21 13:12:52 -0800915 values.claw.max_zeroing_voltage) /
916 claw_.K(0, 0);
917 double dx = ::std::min(dx_top, dx_bot);
Austin Schuh4cb047f2014-02-16 21:10:19 -0800918 bottom_claw_goal_ -= dx;
919 top_claw_goal_ -= dx;
James Kuszmaul0e866512014-02-21 13:12:52 -0800920 Eigen::Matrix<double, 4, 1> R;
Brian Silvermana21c3a22014-06-12 21:49:15 -0700921 R << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_, claw_.R(2, 0),
922 claw_.R(3, 0);
Brian Silverman0ca790b2014-06-12 21:33:08 -0700923 claw_.mutable_U() = claw_.K() * (R - claw_.X_hat());
Austin Schuhcda86af2014-02-16 16:16:39 -0800924 capped_goal_ = true;
Austin Schuh4cb047f2014-02-16 21:10:19 -0800925 LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx);
Austin Schuhcda86af2014-02-16 16:16:39 -0800926 }
Austin Schuh4cb047f2014-02-16 21:10:19 -0800927 } break;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800928 }
929
930 if (output) {
Ben Fredrickson2f76ddf2014-02-23 05:58:23 +0000931 if (goal) {
932 //setup the intake
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000933 output->intake_voltage =
934 (goal->intake > 12.0) ? 12 : (goal->intake < -12.0) ? -12.0
935 : goal->intake;
Ben Fredrickson2f76ddf2014-02-23 05:58:23 +0000936 output->tusk_voltage = goal->centering;
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000937 output->tusk_voltage =
938 (goal->centering > 12.0) ? 12 : (goal->centering < -12.0)
939 ? -12.0
940 : goal->centering;
Ben Fredrickson2f76ddf2014-02-23 05:58:23 +0000941 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700942 output->top_claw_voltage = claw_.U(1, 0);
943 output->bottom_claw_voltage = claw_.U(0, 0);
Austin Schuhf84a1302014-02-19 00:23:30 -0800944
945 if (output->top_claw_voltage > kMaxVoltage) {
946 output->top_claw_voltage = kMaxVoltage;
947 } else if (output->top_claw_voltage < -kMaxVoltage) {
948 output->top_claw_voltage = -kMaxVoltage;
949 }
950
951 if (output->bottom_claw_voltage > kMaxVoltage) {
952 output->bottom_claw_voltage = kMaxVoltage;
953 } else if (output->bottom_claw_voltage < -kMaxVoltage) {
954 output->bottom_claw_voltage = -kMaxVoltage;
955 }
Austin Schuh3bb9a442014-02-02 16:01:45 -0800956 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800957
James Kuszmaul4abaf482014-02-26 21:16:35 -0800958 status->bottom = bottom_absolute_position();
959 status->separation = top_absolute_position() - bottom_absolute_position();
Brian Silvermana21c3a22014-06-12 21:49:15 -0700960 status->bottom_velocity = claw_.X_hat(2, 0);
961 status->separation_velocity = claw_.X_hat(3, 0);
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800962
Brian Silverman71fbee02014-03-13 17:24:54 -0700963 if (goal) {
964 bool bottom_done =
965 ::std::abs(bottom_absolute_position() - goal->bottom_angle) < 0.020;
966 bool bottom_velocity_done = ::std::abs(status->bottom_velocity) < 0.2;
967 bool separation_done =
968 ::std::abs((top_absolute_position() - bottom_absolute_position()) -
969 goal->separation_angle) < 0.020;
970 bool separation_done_with_ball =
971 ::std::abs((top_absolute_position() - bottom_absolute_position()) -
972 goal->separation_angle) < 0.06;
973 status->done = is_ready() && separation_done && bottom_done && bottom_velocity_done;
974 status->done_with_ball =
975 is_ready() && separation_done_with_ball && bottom_done && bottom_velocity_done;
976 } else {
977 status->done = status->done_with_ball = false;
978 }
Austin Schuha556b012014-03-02 11:55:52 -0800979
Austin Schuh4f8633f2014-03-02 13:59:46 -0800980 status->zeroed = is_ready();
Brian Silverman80fc94c2014-03-09 16:56:01 -0700981 status->zeroed_for_auto =
982 (top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
983 top_claw_.zeroing_state() ==
984 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
985 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
986 bottom_claw_.zeroing_state() ==
987 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
Austin Schuh4f8633f2014-03-02 13:59:46 -0800988
Brian Silverman71fbee02014-03-13 17:24:54 -0700989 was_enabled_ = enabled;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800990}
991
992} // namespace control_loops
993} // namespace frc971
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000994