blob: 12ac570043efe7098448f88e33433cb8d01bdee2 [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
5#include "aos/common/control_loop/control_loops.q.h"
6#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;
Austin Schuhf84a1302014-02-19 00:23:30 -080062
Austin Schuh27b8fb12014-02-22 15:10:05 -080063ClawLimitedLoop::ClawLimitedLoop(StateFeedbackLoop<4, 2, 2> loop)
64 : StateFeedbackLoop<4, 2, 2>(loop),
65 uncapped_average_voltage_(0.0),
James Kuszmaulf63b0ae2014-03-25 16:52:11 -070066 is_zeroing_(true),
67 U_Poly_((Eigen::Matrix<double, 4, 2>() << 1, 0,
68 -1, 0,
69 0, 1,
70 0, -1).finished(),
71 (Eigen::Matrix<double, 4, 1>() << kMaxVoltage, kMaxVoltage,
Brian Silverman6dd2c532014-03-29 23:34:39 -070072 kMaxVoltage, kMaxVoltage).finished()),
73 U_Poly_zeroing_((Eigen::Matrix<double, 4, 2>() << 1, 0,
74 -1, 0,
75 0, 1,
76 0, -1).finished(),
77 (Eigen::Matrix<double, 4, 1>() <<
78 kZeroingVoltage, kZeroingVoltage,
79 kZeroingVoltage, kZeroingVoltage).finished()) {
James Kuszmaulf63b0ae2014-03-25 16:52:11 -070080 ::aos::controls::HPolytope<0>::Init();
81}
Austin Schuh27b8fb12014-02-22 15:10:05 -080082
James Kuszmaulf63b0ae2014-03-25 16:52:11 -070083// Caps the voltage prioritizing reducing velocity error over reducing
84// positional error.
85// Uses the polytope libararies which we used to just use for the drivetrain.
86// Uses a region representing the maximum voltage and then transforms it such
87// that the points represent different amounts of positional error and
88// constrains the region such that, if at all possible, it will maintain its
89// current efforts to reduce velocity error.
Austin Schuhcda86af2014-02-16 16:16:39 -080090void ClawLimitedLoop::CapU() {
James Kuszmaulf63b0ae2014-03-25 16:52:11 -070091 Eigen::Matrix<double, 4, 1> error = R - X_hat;
Austin Schuh4cb047f2014-02-16 21:10:19 -080092
James Kuszmauld536a402014-02-18 22:32:12 -080093 double u_top = U(1, 0);
94 double u_bottom = U(0, 0);
Austin Schuhcda86af2014-02-16 16:16:39 -080095
James Kuszmaulf63b0ae2014-03-25 16:52:11 -070096 uncapped_average_voltage_ = (u_top + u_bottom) / 2;
97
98 double max_voltage = is_zeroing_ ? kZeroingVoltage : kMaxVoltage;
99
100 if (::std::abs(u_bottom) > max_voltage or ::std::abs(u_top) > max_voltage) {
Brian Silverman6dd2c532014-03-29 23:34:39 -0700101 LOG_MATRIX(DEBUG, "U at start", U);
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700102 // H * U <= k
103 // U = UPos + UVel
104 // H * (UPos + UVel) <= k
105 // H * UPos <= k - H * UVel
106
107 // Now, we can do a coordinate transformation and say the following.
108
109 // UPos = position_K * position_error
110 // (H * position_K) * position_error <= k - H * UVel
111
112 Eigen::Matrix<double, 2, 2> position_K;
113 position_K << K(0, 0), K(0, 1),
114 K(1, 0), K(1, 1);
115 Eigen::Matrix<double, 2, 2> velocity_K;
116 velocity_K << K(0, 2), K(0, 3),
117 K(1, 2), K(1, 3);
118
119 Eigen::Matrix<double, 2, 1> position_error;
120 position_error << error(0, 0), error(1, 0);
121 Eigen::Matrix<double, 2, 1> velocity_error;
122 velocity_error << error(2, 0), error(3, 0);
Brian Silverman6dd2c532014-03-29 23:34:39 -0700123 LOG_MATRIX(DEBUG, "error", error);
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700124
Brian Silverman6dd2c532014-03-29 23:34:39 -0700125 const auto &poly = is_zeroing_ ? U_Poly_zeroing_ : U_Poly_;
126 const Eigen::Matrix<double, 4, 2> pos_poly_H = poly.H() * position_K;
127 const Eigen::Matrix<double, 4, 1> pos_poly_k =
128 poly.k() - poly.H() * velocity_K * velocity_error;
129 const ::aos::controls::HPolytope<2> pos_poly(pos_poly_H, pos_poly_k);
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700130
Brian Silverman6dd2c532014-03-29 23:34:39 -0700131 Eigen::Matrix<double, 2, 1> adjusted_pos_error;
132 {
133 const auto &P = position_error;
Brian Silvermanb087c0a2014-03-30 12:59:52 -0700134
135 // This line was at 45 degrees but is now at some angle steeper than the
136 // straight one between the points.
137 Eigen::Matrix<double, 1, 2> angle_45;
138 // If the top claw is above its soft upper limit, make the line actually
139 // 45 degrees to avoid smashing it into the limit in an attempt to fix the
140 // separation error faster than the bottom position one.
141 if (X_hat(0, 0) + X_hat(1, 0) >
142 constants::GetValues().claw.upper_claw.upper_limit) {
143 angle_45 << 1, 1;
144 } else {
145 // Fixing separation error half as fast as positional error works well
146 // because it means they both close evenly.
147 angle_45 << ::std::sqrt(3), 1;
148 }
149 Eigen::Matrix<double, 1, 2> L45_quadrant;
150 L45_quadrant << sign(P(1, 0)), -sign(P(0, 0));
151 const auto L45 = L45_quadrant.cwiseProduct(angle_45);
Brian Silverman6dd2c532014-03-29 23:34:39 -0700152 const double w45 = 0;
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700153
Brian Silverman6dd2c532014-03-29 23:34:39 -0700154 Eigen::Matrix<double, 1, 2> LH;
155 if (::std::abs(P(0, 0)) > ::std::abs(P(1, 0))) {
156 LH << 0, 1;
157 } else {
158 LH << 1, 0;
159 }
160 const double wh = LH.dot(P);
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700161
Brian Silverman6dd2c532014-03-29 23:34:39 -0700162 Eigen::Matrix<double, 2, 2> standard;
163 standard << L45, LH;
164 Eigen::Matrix<double, 2, 1> W;
165 W << w45, wh;
166 const Eigen::Matrix<double, 2, 1> intersection = standard.inverse() * W;
167
168 bool is_inside_h;
169 const auto adjusted_pos_error_h =
170 DoCoerceGoal(pos_poly, LH, wh, position_error, &is_inside_h);
171 const auto adjusted_pos_error_45 =
172 DoCoerceGoal(pos_poly, L45, w45, intersection, nullptr);
173 if (pos_poly.IsInside(intersection)) {
174 adjusted_pos_error = adjusted_pos_error_h;
175 } else {
176 if (is_inside_h) {
177 if (adjusted_pos_error_h.norm() > adjusted_pos_error_45.norm()) {
178 adjusted_pos_error = adjusted_pos_error_h;
179 } else {
180 adjusted_pos_error = adjusted_pos_error_45;
181 }
182 } else {
183 adjusted_pos_error = adjusted_pos_error_45;
184 }
185 }
186 }
187
188 LOG_MATRIX(DEBUG, "adjusted_pos_error", adjusted_pos_error);
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700189 U = velocity_K * velocity_error + position_K * adjusted_pos_error;
Brian Silverman6dd2c532014-03-29 23:34:39 -0700190 LOG_MATRIX(DEBUG, "U is now", U);
James Kuszmauld536a402014-02-18 22:32:12 -0800191 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800192}
193
Austin Schuh27b8fb12014-02-22 15:10:05 -0800194ZeroedStateFeedbackLoop::ZeroedStateFeedbackLoop(const char *name,
195 ClawMotor *motor)
196 : offset_(0.0),
197 name_(name),
198 motor_(motor),
199 zeroing_state_(UNKNOWN_POSITION),
200 posedge_value_(0.0),
201 negedge_value_(0.0),
202 encoder_(0.0),
203 last_encoder_(0.0) {}
204
205void ZeroedStateFeedbackLoop::SetPositionValues(const HalfClawPosition &claw) {
206 front_.Update(claw.front);
207 calibration_.Update(claw.calibration);
208 back_.Update(claw.back);
209
210 bool any_sensor_triggered = any_triggered();
211 if (any_sensor_triggered && any_triggered_last_) {
212 // We are still on the hall effect and nothing has changed.
213 min_hall_effect_on_angle_ =
214 ::std::min(min_hall_effect_on_angle_, claw.position);
215 max_hall_effect_on_angle_ =
216 ::std::max(max_hall_effect_on_angle_, claw.position);
217 } else if (!any_sensor_triggered && !any_triggered_last_) {
218 // We are still off the hall effect and nothing has changed.
219 min_hall_effect_off_angle_ =
220 ::std::min(min_hall_effect_off_angle_, claw.position);
221 max_hall_effect_off_angle_ =
222 ::std::max(max_hall_effect_off_angle_, claw.position);
223 } else if (any_sensor_triggered && !any_triggered_last_) {
224 // Saw a posedge on the hall effect. Reset the limits.
225 min_hall_effect_on_angle_ = ::std::min(claw.posedge_value, claw.position);
226 max_hall_effect_on_angle_ = ::std::max(claw.posedge_value, claw.position);
227 } else if (!any_sensor_triggered && any_triggered_last_) {
228 // Saw a negedge on the hall effect. Reset the limits.
229 min_hall_effect_off_angle_ = ::std::min(claw.negedge_value, claw.position);
230 max_hall_effect_off_angle_ = ::std::max(claw.negedge_value, claw.position);
231 }
232
233 posedge_value_ = claw.posedge_value;
234 negedge_value_ = claw.negedge_value;
235 last_encoder_ = encoder_;
236 if (front().value() || calibration().value() || back().value()) {
237 last_on_encoder_ = encoder_;
238 } else {
239 last_off_encoder_ = encoder_;
240 }
241 encoder_ = claw.position;
242 any_triggered_last_ = any_sensor_triggered;
243}
244
245void ZeroedStateFeedbackLoop::Reset(const HalfClawPosition &claw) {
246 set_zeroing_state(ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
247
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000248 front_.Reset(claw.front);
249 calibration_.Reset(claw.calibration);
250 back_.Reset(claw.back);
Austin Schuh27b8fb12014-02-22 15:10:05 -0800251 // close up the min and max edge positions as they are no longer valid and
252 // will be expanded in future iterations
253 min_hall_effect_on_angle_ = claw.position;
254 max_hall_effect_on_angle_ = claw.position;
255 min_hall_effect_off_angle_ = claw.position;
256 max_hall_effect_off_angle_ = claw.position;
257 any_triggered_last_ = any_triggered();
258}
259
260bool TopZeroedStateFeedbackLoop::SetCalibrationOnEdge(
261 const constants::Values::Claws::Claw &claw_values,
262 JointZeroingState zeroing_state) {
263 double edge_encoder;
264 double edge_angle;
265 if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) {
266 LOG(INFO, "Calibration edge edge should be %f.\n", edge_angle);
267 SetCalibration(edge_encoder, edge_angle);
268 set_zeroing_state(zeroing_state);
269 return true;
270 }
271 return false;
272}
273
274bool BottomZeroedStateFeedbackLoop::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.\n");
281 SetCalibration(edge_encoder, edge_angle);
282 set_zeroing_state(zeroing_state);
283 return true;
284 }
285 return false;
286}
287
Austin Schuhcc0bf312014-02-09 00:39:29 -0800288ClawMotor::ClawMotor(control_loops::ClawGroup *my_claw)
Brian Silverman71fbee02014-03-13 17:24:54 -0700289 : aos::control_loops::ControlLoop<control_loops::ClawGroup, true, true,
290 false>(my_claw),
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800291 has_top_claw_goal_(false),
292 top_claw_goal_(0.0),
Austin Schuhcda86af2014-02-16 16:16:39 -0800293 top_claw_(this),
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800294 has_bottom_claw_goal_(false),
295 bottom_claw_goal_(0.0),
Austin Schuhcda86af2014-02-16 16:16:39 -0800296 bottom_claw_(this),
297 claw_(MakeClawLoop()),
Ben Fredrickson9b388422014-02-13 06:15:31 +0000298 was_enabled_(false),
Austin Schuh4cb047f2014-02-16 21:10:19 -0800299 doing_calibration_fine_tune_(false),
Austin Schuhe7f90d12014-02-17 00:48:25 -0800300 capped_goal_(false),
301 mode_(UNKNOWN_LOCATION) {}
Austin Schuh3bb9a442014-02-02 16:01:45 -0800302
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800303const int ZeroedStateFeedbackLoop::kZeroingMaxVoltage;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800304
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000305bool ZeroedStateFeedbackLoop::SawFilteredPosedge(
306 const HallEffectTracker &this_sensor, const HallEffectTracker &sensorA,
307 const HallEffectTracker &sensorB) {
308 if (posedge_filter_ == nullptr && this_sensor.posedge_count_changed() &&
309 !sensorA.posedge_count_changed() && !sensorB.posedge_count_changed() &&
310 this_sensor.value() && !this_sensor.last_value()) {
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000311 posedge_filter_ = &this_sensor;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000312 } else if (posedge_filter_ == &this_sensor &&
313 !this_sensor.posedge_count_changed() &&
314 !sensorA.posedge_count_changed() &&
315 !sensorB.posedge_count_changed() && this_sensor.value()) {
316 posedge_filter_ = nullptr;
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000317 return true;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000318 } else if (posedge_filter_ == &this_sensor) {
319 posedge_filter_ = nullptr;
320 }
321 return false;
322}
323
324bool ZeroedStateFeedbackLoop::SawFilteredNegedge(
325 const HallEffectTracker &this_sensor, const HallEffectTracker &sensorA,
326 const HallEffectTracker &sensorB) {
327 if (negedge_filter_ == nullptr && this_sensor.negedge_count_changed() &&
328 !sensorA.negedge_count_changed() && !sensorB.negedge_count_changed() &&
329 !this_sensor.value() && this_sensor.last_value()) {
330 negedge_filter_ = &this_sensor;
331 } else if (negedge_filter_ == &this_sensor &&
332 !this_sensor.negedge_count_changed() &&
333 !sensorA.negedge_count_changed() &&
334 !sensorB.negedge_count_changed() && !this_sensor.value()) {
335 negedge_filter_ = nullptr;
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000336 return true;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000337 } else if (negedge_filter_ == &this_sensor) {
338 negedge_filter_ = nullptr;
339 }
340 return false;
341}
342
Brian Silvermane0a95462014-02-17 00:41:09 -0800343bool ZeroedStateFeedbackLoop::DoGetPositionOfEdge(
344 const constants::Values::Claws::AnglePair &angles, double *edge_encoder,
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000345 double *edge_angle, const HallEffectTracker &this_sensor,
346 const HallEffectTracker &sensorA, const HallEffectTracker &sensorB,
Brian Silvermane0a95462014-02-17 00:41:09 -0800347 const char *hall_effect_name) {
Austin Schuhf84a1302014-02-19 00:23:30 -0800348 bool found_edge = false;
Austin Schuh27b8fb12014-02-22 15:10:05 -0800349
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000350 if (SawFilteredPosedge(this_sensor, sensorA, sensorB)) {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800351 if (min_hall_effect_off_angle_ == max_hall_effect_off_angle_) {
Brian Silvermanf48fab32014-03-09 14:32:24 -0700352 LOG(WARNING, "%s: Uncertain which side, rejecting posedge\n", name_);
Brian Silvermane0a95462014-02-17 00:41:09 -0800353 } else {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800354 const double average_last_encoder =
355 (min_hall_effect_off_angle_ + max_hall_effect_off_angle_) / 2.0;
356 if (posedge_value_ < average_last_encoder) {
357 *edge_angle = angles.upper_decreasing_angle;
358 LOG(INFO, "%s Posedge upper of %s -> %f posedge: %f avg_encoder: %f\n",
359 name_, hall_effect_name, *edge_angle, posedge_value_,
360 average_last_encoder);
361 } else {
362 *edge_angle = angles.lower_angle;
363 LOG(INFO, "%s Posedge lower of %s -> %f posedge: %f avg_encoder: %f\n",
364 name_, hall_effect_name, *edge_angle, posedge_value_,
365 average_last_encoder);
366 }
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000367 *edge_encoder = posedge_value_;
Austin Schuh27b8fb12014-02-22 15:10:05 -0800368 found_edge = true;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000369 }
370 }
371
372 if (SawFilteredNegedge(this_sensor, sensorA, sensorB)) {
373 if (min_hall_effect_on_angle_ == max_hall_effect_on_angle_) {
Brian Silvermanf48fab32014-03-09 14:32:24 -0700374 LOG(WARNING, "%s: Uncertain which side, rejecting negedge\n", name_);
Brian Silvermane0a95462014-02-17 00:41:09 -0800375 } else {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800376 const double average_last_encoder =
377 (min_hall_effect_on_angle_ + max_hall_effect_on_angle_) / 2.0;
378 if (negedge_value_ > average_last_encoder) {
379 *edge_angle = angles.upper_angle;
380 LOG(INFO, "%s Negedge upper of %s -> %f negedge: %f avg_encoder: %f\n",
381 name_, hall_effect_name, *edge_angle, negedge_value_,
382 average_last_encoder);
383 } else {
384 *edge_angle = angles.lower_decreasing_angle;
385 LOG(INFO, "%s Negedge lower of %s -> %f negedge: %f avg_encoder: %f\n",
386 name_, hall_effect_name, *edge_angle, negedge_value_,
387 average_last_encoder);
388 }
389 *edge_encoder = negedge_value_;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000390 found_edge = true;
Brian Silvermane0a95462014-02-17 00:41:09 -0800391 }
Austin Schuh27b8fb12014-02-22 15:10:05 -0800392 }
393
Austin Schuhf84a1302014-02-19 00:23:30 -0800394 return found_edge;
Brian Silvermane0a95462014-02-17 00:41:09 -0800395}
396
Austin Schuhf9286cd2014-02-11 00:51:09 -0800397bool ZeroedStateFeedbackLoop::GetPositionOfEdge(
Austin Schuhd27931c2014-02-16 19:18:20 -0800398 const constants::Values::Claws::Claw &claw_values, double *edge_encoder,
Austin Schuhf9286cd2014-02-11 00:51:09 -0800399 double *edge_angle) {
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000400 if (DoGetPositionOfEdge(claw_values.front, edge_encoder, edge_angle, front_,
401 calibration_, back_, "front")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800402 return true;
403 }
Brian Silvermane0a95462014-02-17 00:41:09 -0800404 if (DoGetPositionOfEdge(claw_values.calibration, edge_encoder, edge_angle,
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000405 calibration_, front_, back_, "calibration")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800406 return true;
407 }
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000408 if (DoGetPositionOfEdge(claw_values.back, edge_encoder, edge_angle, back_,
409 calibration_, front_, "back")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800410 return true;
411 }
412 return false;
413}
414
Austin Schuhcda86af2014-02-16 16:16:39 -0800415void TopZeroedStateFeedbackLoop::SetCalibration(double edge_encoder,
416 double edge_angle) {
417 double old_offset = offset_;
418 offset_ = edge_angle - edge_encoder;
419 const double doffset = offset_ - old_offset;
420 motor_->ChangeTopOffset(doffset);
421}
422
423void BottomZeroedStateFeedbackLoop::SetCalibration(double edge_encoder,
424 double edge_angle) {
425 double old_offset = offset_;
426 offset_ = edge_angle - edge_encoder;
427 const double doffset = offset_ - old_offset;
428 motor_->ChangeBottomOffset(doffset);
429}
430
431void ClawMotor::ChangeTopOffset(double doffset) {
432 claw_.ChangeTopOffset(doffset);
433 if (has_top_claw_goal_) {
434 top_claw_goal_ += doffset;
435 }
436}
437
438void ClawMotor::ChangeBottomOffset(double doffset) {
439 claw_.ChangeBottomOffset(doffset);
440 if (has_bottom_claw_goal_) {
441 bottom_claw_goal_ += doffset;
442 }
443}
444
445void ClawLimitedLoop::ChangeTopOffset(double doffset) {
446 Y_(1, 0) += doffset;
447 X_hat(1, 0) += doffset;
448 LOG(INFO, "Changing top offset by %f\n", doffset);
449}
450void ClawLimitedLoop::ChangeBottomOffset(double doffset) {
451 Y_(0, 0) += doffset;
452 X_hat(0, 0) += doffset;
453 X_hat(1, 0) -= doffset;
454 LOG(INFO, "Changing bottom offset by %f\n", doffset);
455}
joe7376ff52014-02-16 18:28:42 -0800456
Austin Schuh069143b2014-02-17 02:46:26 -0800457void LimitClawGoal(double *bottom_goal, double *top_goal,
458 const frc971::constants::Values &values) {
459 // first update position based on angle limit
460
461 const double separation = *top_goal - *bottom_goal;
462 if (separation > values.claw.claw_max_separation) {
Austin Schuh069143b2014-02-17 02:46:26 -0800463 const double dsep = (separation - values.claw.claw_max_separation) / 2.0;
464 *bottom_goal += dsep;
465 *top_goal -= dsep;
466 LOG(DEBUG, "Goals now bottom: %f, top: %f\n", *bottom_goal, *top_goal);
467 }
468 if (separation < values.claw.claw_min_separation) {
Austin Schuh069143b2014-02-17 02:46:26 -0800469 const double dsep = (separation - values.claw.claw_min_separation) / 2.0;
470 *bottom_goal += dsep;
471 *top_goal -= dsep;
472 LOG(DEBUG, "Goals now bottom: %f, top: %f\n", *bottom_goal, *top_goal);
473 }
474
475 // now move both goals in unison
476 if (*bottom_goal < values.claw.lower_claw.lower_limit) {
477 *top_goal += values.claw.lower_claw.lower_limit - *bottom_goal;
478 *bottom_goal = values.claw.lower_claw.lower_limit;
479 }
480 if (*bottom_goal > values.claw.lower_claw.upper_limit) {
481 *top_goal -= *bottom_goal - values.claw.lower_claw.upper_limit;
482 *bottom_goal = values.claw.lower_claw.upper_limit;
483 }
484
485 if (*top_goal < values.claw.upper_claw.lower_limit) {
486 *bottom_goal += values.claw.upper_claw.lower_limit - *top_goal;
487 *top_goal = values.claw.upper_claw.lower_limit;
488 }
489 if (*top_goal > values.claw.upper_claw.upper_limit) {
490 *bottom_goal -= *top_goal - values.claw.upper_claw.upper_limit;
491 *top_goal = values.claw.upper_claw.upper_limit;
492 }
493}
Austin Schuhcda86af2014-02-16 16:16:39 -0800494
Austin Schuhe7f90d12014-02-17 00:48:25 -0800495bool ClawMotor::is_ready() const {
496 return (
497 (top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED &&
498 bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) ||
Brian Silverman71fbee02014-03-13 17:24:54 -0700499 (((::aos::robot_state.get() == NULL) ? true
500 : ::aos::robot_state->autonomous) &&
Austin Schuhe7f90d12014-02-17 00:48:25 -0800501 ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
502 top_claw_.zeroing_state() ==
503 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
504 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
505 bottom_claw_.zeroing_state() ==
506 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION))));
507}
508
509bool ClawMotor::is_zeroing() const { return !is_ready(); }
510
Austin Schuh3bb9a442014-02-02 16:01:45 -0800511// Positive angle is up, and positive power is up.
Austin Schuhcc0bf312014-02-09 00:39:29 -0800512void ClawMotor::RunIteration(const control_loops::ClawGroup::Goal *goal,
513 const control_loops::ClawGroup::Position *position,
514 control_loops::ClawGroup::Output *output,
James Kuszmaul9ead1de2014-02-28 21:24:39 -0800515 control_loops::ClawGroup::Status *status) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800516 constexpr double dt = 0.01;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800517
518 // Disable the motors now so that all early returns will return with the
519 // motors disabled.
520 if (output) {
521 output->top_claw_voltage = 0;
522 output->bottom_claw_voltage = 0;
523 output->intake_voltage = 0;
Ben Fredrickson61893d52014-03-02 09:43:23 +0000524 output->tusk_voltage = 0;
525 }
526
Brian Silverman71fbee02014-03-13 17:24:54 -0700527 if (goal) {
528 if (::std::isnan(goal->bottom_angle) ||
529 ::std::isnan(goal->separation_angle) || ::std::isnan(goal->intake) ||
530 ::std::isnan(goal->centering)) {
531 return;
532 }
Austin Schuh3bb9a442014-02-02 16:01:45 -0800533 }
534
Austin Schuh1a499942014-02-17 01:51:58 -0800535 if (reset()) {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800536 top_claw_.Reset(position->top);
537 bottom_claw_.Reset(position->bottom);
Austin Schuh1a499942014-02-17 01:51:58 -0800538 }
539
Austin Schuhf9286cd2014-02-11 00:51:09 -0800540 const frc971::constants::Values &values = constants::GetValues();
541
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800542 if (position) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800543 Eigen::Matrix<double, 2, 1> Y;
544 Y << position->bottom.position + bottom_claw_.offset(),
545 position->top.position + top_claw_.offset();
546 claw_.Correct(Y);
547
Austin Schuhf9286cd2014-02-11 00:51:09 -0800548 top_claw_.SetPositionValues(position->top);
549 bottom_claw_.SetPositionValues(position->bottom);
550
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800551 if (!has_top_claw_goal_) {
552 has_top_claw_goal_ = true;
Austin Schuhcda86af2014-02-16 16:16:39 -0800553 top_claw_goal_ = top_claw_.absolute_position();
Austin Schuhe7f90d12014-02-17 00:48:25 -0800554 initial_separation_ =
Austin Schuhcda86af2014-02-16 16:16:39 -0800555 top_claw_.absolute_position() - bottom_claw_.absolute_position();
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800556 }
557 if (!has_bottom_claw_goal_) {
558 has_bottom_claw_goal_ = true;
Austin Schuhcda86af2014-02-16 16:16:39 -0800559 bottom_claw_goal_ = bottom_claw_.absolute_position();
Austin Schuhe7f90d12014-02-17 00:48:25 -0800560 initial_separation_ =
Austin Schuhcda86af2014-02-16 16:16:39 -0800561 top_claw_.absolute_position() - bottom_claw_.absolute_position();
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800562 }
Brian Silvermanf48fab32014-03-09 14:32:24 -0700563 LOG_STRUCT(DEBUG, "absolute position",
564 ClawPositionToLog(top_claw_.absolute_position(),
565 bottom_claw_.absolute_position()));
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800566 }
567
Brian Silverman71fbee02014-03-13 17:24:54 -0700568 bool autonomous, enabled;
569 if (::aos::robot_state.get() == nullptr) {
570 autonomous = true;
571 enabled = false;
572 } else {
573 autonomous = ::aos::robot_state->autonomous;
574 enabled = ::aos::robot_state->enabled;
575 }
Austin Schuh069143b2014-02-17 02:46:26 -0800576
577 double bottom_claw_velocity_ = 0.0;
578 double top_claw_velocity_ = 0.0;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800579
Brian Silverman71fbee02014-03-13 17:24:54 -0700580 if (goal != NULL &&
581 ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED &&
582 bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) ||
583 (autonomous &&
584 ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
585 top_claw_.zeroing_state() ==
586 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
587 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
588 bottom_claw_.zeroing_state() ==
589 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION))))) {
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800590 // Ready to use the claw.
591 // Limit the goals here.
Austin Schuhf9286cd2014-02-11 00:51:09 -0800592 bottom_claw_goal_ = goal->bottom_angle;
Brian Silverman7c021c42014-02-17 15:15:56 -0800593 top_claw_goal_ = goal->bottom_angle + goal->separation_angle;
Austin Schuhcda86af2014-02-16 16:16:39 -0800594 has_bottom_claw_goal_ = true;
595 has_top_claw_goal_ = true;
596 doing_calibration_fine_tune_ = false;
597
Austin Schuhe7f90d12014-02-17 00:48:25 -0800598 mode_ = READY;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800599 } else if (top_claw_.zeroing_state() !=
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000600 ZeroedStateFeedbackLoop::UNKNOWN_POSITION &&
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800601 bottom_claw_.zeroing_state() !=
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000602 ZeroedStateFeedbackLoop::UNKNOWN_POSITION) {
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800603 // Time to fine tune the zero.
604 // Limit the goals here.
Austin Schuh0c733422014-02-17 01:17:12 -0800605 if (!enabled) {
606 // If we are disabled, start the fine tune process over again.
607 doing_calibration_fine_tune_ = false;
608 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800609 if (bottom_claw_.zeroing_state() != ZeroedStateFeedbackLoop::CALIBRATED) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800610 // always get the bottom claw to calibrated first
611 LOG(DEBUG, "Calibrating the bottom of the claw\n");
612 if (!doing_calibration_fine_tune_) {
613 if (::std::abs(bottom_absolute_position() -
Austin Schuhd27931c2014-02-16 19:18:20 -0800614 values.claw.start_fine_tune_pos) <
615 values.claw.claw_unimportant_epsilon) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800616 doing_calibration_fine_tune_ = true;
Austin Schuhd27931c2014-02-16 19:18:20 -0800617 bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800618 top_claw_velocity_ = bottom_claw_velocity_ =
619 values.claw.claw_zeroing_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800620 LOG(DEBUG, "Ready to fine tune the bottom\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800621 mode_ = FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800622 } else {
623 // send bottom to zeroing start
Austin Schuhd27931c2014-02-16 19:18:20 -0800624 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuhcda86af2014-02-16 16:16:39 -0800625 LOG(DEBUG, "Going to the start position for the bottom\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800626 mode_ = PREP_FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800627 }
628 } else {
Austin Schuhe7f90d12014-02-17 00:48:25 -0800629 mode_ = FINE_TUNE_BOTTOM;
Austin Schuhd27931c2014-02-16 19:18:20 -0800630 bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800631 top_claw_velocity_ = bottom_claw_velocity_ =
632 values.claw.claw_zeroing_speed;
Brian Silvermane0a95462014-02-17 00:41:09 -0800633 if (top_claw_.front_or_back_triggered() ||
634 bottom_claw_.front_or_back_triggered()) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800635 // We shouldn't hit a limit, but if we do, go back to the zeroing
636 // point and try again.
637 doing_calibration_fine_tune_ = false;
Austin Schuhd27931c2014-02-16 19:18:20 -0800638 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuh069143b2014-02-17 02:46:26 -0800639 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
Austin Schuhcda86af2014-02-16 16:16:39 -0800640 LOG(DEBUG, "Found a limit, starting over.\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800641 mode_ = PREP_FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800642 }
Austin Schuh288c8c32014-02-16 17:20:17 -0800643
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000644 if (position && bottom_claw_.SawFilteredPosedge(
645 bottom_claw_.calibration(), bottom_claw_.front(),
646 bottom_claw_.back())) {
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000647 // do calibration
648 bottom_claw_.SetCalibration(
649 position->bottom.posedge_value,
650 values.claw.lower_claw.calibration.lower_angle);
651 bottom_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED);
652 // calibrated so we are done fine tuning bottom
653 doing_calibration_fine_tune_ = false;
654 LOG(DEBUG, "Calibrated the bottom correctly!\n");
655 } else if (bottom_claw_.calibration().last_value()) {
656 doing_calibration_fine_tune_ = false;
657 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
658 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
659 mode_ = PREP_FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800660 } else {
661 LOG(DEBUG, "Fine tuning\n");
662 }
663 }
664 // now set the top claw to track
665
Austin Schuhd27931c2014-02-16 19:18:20 -0800666 top_claw_goal_ = bottom_claw_goal_ + values.claw.claw_zeroing_separation;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800667 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800668 // bottom claw must be calibrated, start on the top
669 if (!doing_calibration_fine_tune_) {
Austin Schuhd27931c2014-02-16 19:18:20 -0800670 if (::std::abs(top_absolute_position() -
671 values.claw.start_fine_tune_pos) <
672 values.claw.claw_unimportant_epsilon) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800673 doing_calibration_fine_tune_ = true;
Austin Schuhd27931c2014-02-16 19:18:20 -0800674 top_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800675 top_claw_velocity_ = bottom_claw_velocity_ =
676 values.claw.claw_zeroing_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800677 LOG(DEBUG, "Ready to fine tune the top\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800678 mode_ = FINE_TUNE_TOP;
Austin Schuhcda86af2014-02-16 16:16:39 -0800679 } else {
680 // send top to zeroing start
Austin Schuhd27931c2014-02-16 19:18:20 -0800681 top_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuhcda86af2014-02-16 16:16:39 -0800682 LOG(DEBUG, "Going to the start position for the top\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800683 mode_ = PREP_FINE_TUNE_TOP;
Austin Schuhcda86af2014-02-16 16:16:39 -0800684 }
685 } else {
Austin Schuhe7f90d12014-02-17 00:48:25 -0800686 mode_ = FINE_TUNE_TOP;
Austin Schuhd27931c2014-02-16 19:18:20 -0800687 top_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800688 top_claw_velocity_ = bottom_claw_velocity_ =
689 values.claw.claw_zeroing_speed;
Brian Silvermane0a95462014-02-17 00:41:09 -0800690 if (top_claw_.front_or_back_triggered() ||
691 bottom_claw_.front_or_back_triggered()) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800692 // this should not happen, but now we know it won't
693 doing_calibration_fine_tune_ = false;
Austin Schuhd27931c2014-02-16 19:18:20 -0800694 top_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuh069143b2014-02-17 02:46:26 -0800695 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
Austin Schuhcda86af2014-02-16 16:16:39 -0800696 LOG(DEBUG, "Found a limit, starting over.\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800697 mode_ = PREP_FINE_TUNE_TOP;
Austin Schuhcda86af2014-02-16 16:16:39 -0800698 }
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000699
700 if (position &&
701 top_claw_.SawFilteredPosedge(top_claw_.calibration(),
702 top_claw_.front(), top_claw_.back())) {
703 // do calibration
704 top_claw_.SetCalibration(
705 position->top.posedge_value,
706 values.claw.upper_claw.calibration.lower_angle);
707 top_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED);
708 // calibrated so we are done fine tuning top
709 doing_calibration_fine_tune_ = false;
710 LOG(DEBUG, "Calibrated the top correctly!\n");
711 } else if (top_claw_.calibration().last_value()) {
712 doing_calibration_fine_tune_ = false;
713 top_claw_goal_ = values.claw.start_fine_tune_pos;
714 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
715 mode_ = PREP_FINE_TUNE_TOP;
Austin Schuhcda86af2014-02-16 16:16:39 -0800716 }
717 }
718 // now set the bottom claw to track
Austin Schuhd27931c2014-02-16 19:18:20 -0800719 bottom_claw_goal_ = top_claw_goal_ - values.claw.claw_zeroing_separation;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800720 }
721 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800722 doing_calibration_fine_tune_ = false;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800723 if (!was_enabled_ && enabled) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800724 if (position) {
Brian Silverman72035692014-03-14 10:18:27 -0700725 top_claw_goal_ = position->top.position + top_claw_.offset();
726 bottom_claw_goal_ = position->bottom.position + bottom_claw_.offset();
Austin Schuhe7f90d12014-02-17 00:48:25 -0800727 initial_separation_ =
Austin Schuhcda86af2014-02-16 16:16:39 -0800728 position->top.position - position->bottom.position;
Austin Schuhf9286cd2014-02-11 00:51:09 -0800729 } else {
730 has_top_claw_goal_ = false;
731 has_bottom_claw_goal_ = false;
732 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800733 }
Austin Schuhf9286cd2014-02-11 00:51:09 -0800734
Austin Schuh4cb047f2014-02-16 21:10:19 -0800735 if ((bottom_claw_.zeroing_state() !=
736 ZeroedStateFeedbackLoop::UNKNOWN_POSITION ||
Brian Silvermane0a95462014-02-17 00:41:09 -0800737 bottom_claw_.front().value() || top_claw_.front().value()) &&
738 !top_claw_.back().value() && !bottom_claw_.back().value()) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800739 if (enabled) {
740 // Time to slowly move back up to find any position to narrow down the
741 // zero.
Austin Schuhd27931c2014-02-16 19:18:20 -0800742 top_claw_goal_ += values.claw.claw_zeroing_off_speed * dt;
743 bottom_claw_goal_ += values.claw.claw_zeroing_off_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800744 top_claw_velocity_ = bottom_claw_velocity_ =
745 values.claw.claw_zeroing_off_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800746 LOG(DEBUG, "Bottom is known.\n");
Austin Schuhf9286cd2014-02-11 00:51:09 -0800747 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800748 } else {
749 // We don't know where either claw is. Slowly start moving down to find
750 // any hall effect.
Austin Schuhf9286cd2014-02-11 00:51:09 -0800751 if (enabled) {
Austin Schuhd27931c2014-02-16 19:18:20 -0800752 top_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt;
753 bottom_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800754 top_claw_velocity_ = bottom_claw_velocity_ =
755 -values.claw.claw_zeroing_off_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800756 LOG(DEBUG, "Both are unknown.\n");
Austin Schuhf9286cd2014-02-11 00:51:09 -0800757 }
758 }
759
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000760 if (position) {
761 if (enabled) {
762 top_claw_.SetCalibrationOnEdge(
763 values.claw.upper_claw,
764 ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION);
765 bottom_claw_.SetCalibrationOnEdge(
766 values.claw.lower_claw,
767 ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION);
768 } else {
769 // TODO(austin): Only calibrate on the predetermined edge.
770 // We might be able to just ignore this since the backlash is soooo
771 // low.
772 // :)
773 top_claw_.SetCalibrationOnEdge(
774 values.claw.upper_claw,
775 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
776 bottom_claw_.SetCalibrationOnEdge(
777 values.claw.lower_claw,
778 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
779 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800780 }
Austin Schuhe7f90d12014-02-17 00:48:25 -0800781 mode_ = UNKNOWN_LOCATION;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800782 }
783
Austin Schuh069143b2014-02-17 02:46:26 -0800784 // Limit the goals if both claws have been (mostly) found.
785 if (mode_ != UNKNOWN_LOCATION) {
786 LimitClawGoal(&bottom_claw_goal_, &top_claw_goal_, values);
787 }
788
Austin Schuhf9286cd2014-02-11 00:51:09 -0800789 if (has_top_claw_goal_ && has_bottom_claw_goal_) {
Austin Schuh069143b2014-02-17 02:46:26 -0800790 claw_.R << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_,
791 bottom_claw_velocity_, top_claw_velocity_ - bottom_claw_velocity_;
Austin Schuhcda86af2014-02-16 16:16:39 -0800792 double separation = -971;
793 if (position != nullptr) {
794 separation = position->top.position - position->bottom.position;
795 }
Brian Silvermanf48fab32014-03-09 14:32:24 -0700796 LOG_STRUCT(DEBUG, "actual goal",
797 ClawGoalToLog(claw_.R(0, 0), claw_.R(1, 0), separation));
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800798
Austin Schuh01c652b2014-02-21 23:13:42 -0800799 // Only cap power when one of the halves of the claw is moving slowly and
800 // could wind up.
Austin Schuhe7f90d12014-02-17 00:48:25 -0800801 claw_.set_is_zeroing(mode_ == UNKNOWN_LOCATION || mode_ == FINE_TUNE_TOP ||
802 mode_ == FINE_TUNE_BOTTOM);
Austin Schuhcda86af2014-02-16 16:16:39 -0800803 claw_.Update(output == nullptr);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800804 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800805 claw_.Update(true);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800806 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800807
Austin Schuh4cb047f2014-02-16 21:10:19 -0800808 capped_goal_ = false;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800809 switch (mode_) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800810 case READY:
Austin Schuhe7f90d12014-02-17 00:48:25 -0800811 case PREP_FINE_TUNE_TOP:
812 case PREP_FINE_TUNE_BOTTOM:
Austin Schuhcda86af2014-02-16 16:16:39 -0800813 break;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800814 case FINE_TUNE_BOTTOM:
815 case FINE_TUNE_TOP:
Austin Schuh4cb047f2014-02-16 21:10:19 -0800816 case UNKNOWN_LOCATION: {
Brian Silverman6dd2c532014-03-29 23:34:39 -0700817 LOG(DEBUG, "Uncapped voltages: Top: %f, Bottom: %f\n", claw_.U_uncapped(1, 0), claw_.U_uncapped(0, 0));
Austin Schuh4cb047f2014-02-16 21:10:19 -0800818 if (claw_.uncapped_average_voltage() > values.claw.max_zeroing_voltage) {
Brian Silverman6dd2c532014-03-29 23:34:39 -0700819 double dx_bot = (claw_.U_uncapped(0, 0) -
Austin Schuh4cb047f2014-02-16 21:10:19 -0800820 values.claw.max_zeroing_voltage) /
821 claw_.K(0, 0);
Brian Silverman6dd2c532014-03-29 23:34:39 -0700822 double dx_top = (claw_.U_uncapped(1, 0) -
James Kuszmaul0e866512014-02-21 13:12:52 -0800823 values.claw.max_zeroing_voltage) /
824 claw_.K(0, 0);
825 double dx = ::std::max(dx_top, dx_bot);
Austin Schuh4cb047f2014-02-16 21:10:19 -0800826 bottom_claw_goal_ -= dx;
827 top_claw_goal_ -= dx;
James Kuszmaul0e866512014-02-21 13:12:52 -0800828 Eigen::Matrix<double, 4, 1> R;
829 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 -0700830 claw_.U = claw_.K() * (R - claw_.X_hat);
Austin Schuhcda86af2014-02-16 16:16:39 -0800831 capped_goal_ = true;
Brian Silvermanf48fab32014-03-09 14:32:24 -0700832 LOG(DEBUG, "Moving the goal by %f to prevent windup."
833 " Uncapped is %f, max is %f, difference is %f\n",
834 dx,
Austin Schuhe7f90d12014-02-17 00:48:25 -0800835 claw_.uncapped_average_voltage(), values.claw.max_zeroing_voltage,
836 (claw_.uncapped_average_voltage() -
837 values.claw.max_zeroing_voltage));
Austin Schuh4cb047f2014-02-16 21:10:19 -0800838 } else if (claw_.uncapped_average_voltage() <
839 -values.claw.max_zeroing_voltage) {
Brian Silverman6dd2c532014-03-29 23:34:39 -0700840 double dx_bot = (claw_.U_uncapped(0, 0) +
Austin Schuh4cb047f2014-02-16 21:10:19 -0800841 values.claw.max_zeroing_voltage) /
842 claw_.K(0, 0);
Brian Silverman6dd2c532014-03-29 23:34:39 -0700843 double dx_top = (claw_.U_uncapped(1, 0) +
James Kuszmaul0e866512014-02-21 13:12:52 -0800844 values.claw.max_zeroing_voltage) /
845 claw_.K(0, 0);
846 double dx = ::std::min(dx_top, dx_bot);
Austin Schuh4cb047f2014-02-16 21:10:19 -0800847 bottom_claw_goal_ -= dx;
848 top_claw_goal_ -= dx;
James Kuszmaul0e866512014-02-21 13:12:52 -0800849 Eigen::Matrix<double, 4, 1> R;
850 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 -0700851 claw_.U = claw_.K() * (R - claw_.X_hat);
Austin Schuhcda86af2014-02-16 16:16:39 -0800852 capped_goal_ = true;
Austin Schuh4cb047f2014-02-16 21:10:19 -0800853 LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx);
Austin Schuhcda86af2014-02-16 16:16:39 -0800854 }
Austin Schuh4cb047f2014-02-16 21:10:19 -0800855 } break;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800856 }
857
858 if (output) {
Ben Fredrickson2f76ddf2014-02-23 05:58:23 +0000859 if (goal) {
860 //setup the intake
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000861 output->intake_voltage =
862 (goal->intake > 12.0) ? 12 : (goal->intake < -12.0) ? -12.0
863 : goal->intake;
Ben Fredrickson2f76ddf2014-02-23 05:58:23 +0000864 output->tusk_voltage = goal->centering;
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000865 output->tusk_voltage =
866 (goal->centering > 12.0) ? 12 : (goal->centering < -12.0)
867 ? -12.0
868 : goal->centering;
Ben Fredrickson2f76ddf2014-02-23 05:58:23 +0000869 }
James Kuszmauld536a402014-02-18 22:32:12 -0800870 output->top_claw_voltage = claw_.U(1, 0);
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000871 output->bottom_claw_voltage = claw_.U(0, 0);
Austin Schuhf84a1302014-02-19 00:23:30 -0800872
873 if (output->top_claw_voltage > kMaxVoltage) {
874 output->top_claw_voltage = kMaxVoltage;
875 } else if (output->top_claw_voltage < -kMaxVoltage) {
876 output->top_claw_voltage = -kMaxVoltage;
877 }
878
879 if (output->bottom_claw_voltage > kMaxVoltage) {
880 output->bottom_claw_voltage = kMaxVoltage;
881 } else if (output->bottom_claw_voltage < -kMaxVoltage) {
882 output->bottom_claw_voltage = -kMaxVoltage;
883 }
Austin Schuh3bb9a442014-02-02 16:01:45 -0800884 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800885
James Kuszmaul4abaf482014-02-26 21:16:35 -0800886 status->bottom = bottom_absolute_position();
887 status->separation = top_absolute_position() - bottom_absolute_position();
888 status->bottom_velocity = claw_.X_hat(2, 0);
889 status->separation_velocity = claw_.X_hat(3, 0);
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800890
Brian Silverman71fbee02014-03-13 17:24:54 -0700891 if (goal) {
892 bool bottom_done =
893 ::std::abs(bottom_absolute_position() - goal->bottom_angle) < 0.020;
894 bool bottom_velocity_done = ::std::abs(status->bottom_velocity) < 0.2;
895 bool separation_done =
896 ::std::abs((top_absolute_position() - bottom_absolute_position()) -
897 goal->separation_angle) < 0.020;
898 bool separation_done_with_ball =
899 ::std::abs((top_absolute_position() - bottom_absolute_position()) -
900 goal->separation_angle) < 0.06;
901 status->done = is_ready() && separation_done && bottom_done && bottom_velocity_done;
902 status->done_with_ball =
903 is_ready() && separation_done_with_ball && bottom_done && bottom_velocity_done;
904 } else {
905 status->done = status->done_with_ball = false;
906 }
Austin Schuha556b012014-03-02 11:55:52 -0800907
Austin Schuh4f8633f2014-03-02 13:59:46 -0800908 status->zeroed = is_ready();
Brian Silverman80fc94c2014-03-09 16:56:01 -0700909 status->zeroed_for_auto =
910 (top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
911 top_claw_.zeroing_state() ==
912 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
913 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
914 bottom_claw_.zeroing_state() ==
915 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
Austin Schuh4f8633f2014-03-02 13:59:46 -0800916
Brian Silverman71fbee02014-03-13 17:24:54 -0700917 was_enabled_ = enabled;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800918}
919
920} // namespace control_loops
921} // namespace frc971
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000922