blob: cbd4d2ad5843862c9200ed7470c33fe272c8aa64 [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;
134 Eigen::Matrix<double, 1, 2> L45;
135 L45 << sign(P(1, 0)), -sign(P(0, 0));
136 const double w45 = 0;
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700137
Brian Silverman6dd2c532014-03-29 23:34:39 -0700138 Eigen::Matrix<double, 1, 2> LH;
139 if (::std::abs(P(0, 0)) > ::std::abs(P(1, 0))) {
140 LH << 0, 1;
141 } else {
142 LH << 1, 0;
143 }
144 const double wh = LH.dot(P);
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700145
Brian Silverman6dd2c532014-03-29 23:34:39 -0700146 Eigen::Matrix<double, 2, 2> standard;
147 standard << L45, LH;
148 Eigen::Matrix<double, 2, 1> W;
149 W << w45, wh;
150 const Eigen::Matrix<double, 2, 1> intersection = standard.inverse() * W;
151
152 bool is_inside_h;
153 const auto adjusted_pos_error_h =
154 DoCoerceGoal(pos_poly, LH, wh, position_error, &is_inside_h);
155 const auto adjusted_pos_error_45 =
156 DoCoerceGoal(pos_poly, L45, w45, intersection, nullptr);
157 if (pos_poly.IsInside(intersection)) {
158 adjusted_pos_error = adjusted_pos_error_h;
159 } else {
160 if (is_inside_h) {
161 if (adjusted_pos_error_h.norm() > adjusted_pos_error_45.norm()) {
162 adjusted_pos_error = adjusted_pos_error_h;
163 } else {
164 adjusted_pos_error = adjusted_pos_error_45;
165 }
166 } else {
167 adjusted_pos_error = adjusted_pos_error_45;
168 }
169 }
170 }
171
172 LOG_MATRIX(DEBUG, "adjusted_pos_error", adjusted_pos_error);
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700173 U = velocity_K * velocity_error + position_K * adjusted_pos_error;
Brian Silverman6dd2c532014-03-29 23:34:39 -0700174 LOG_MATRIX(DEBUG, "U is now", U);
James Kuszmauld536a402014-02-18 22:32:12 -0800175 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800176}
177
Austin Schuh27b8fb12014-02-22 15:10:05 -0800178ZeroedStateFeedbackLoop::ZeroedStateFeedbackLoop(const char *name,
179 ClawMotor *motor)
180 : offset_(0.0),
181 name_(name),
182 motor_(motor),
183 zeroing_state_(UNKNOWN_POSITION),
184 posedge_value_(0.0),
185 negedge_value_(0.0),
186 encoder_(0.0),
187 last_encoder_(0.0) {}
188
189void ZeroedStateFeedbackLoop::SetPositionValues(const HalfClawPosition &claw) {
190 front_.Update(claw.front);
191 calibration_.Update(claw.calibration);
192 back_.Update(claw.back);
193
194 bool any_sensor_triggered = any_triggered();
195 if (any_sensor_triggered && any_triggered_last_) {
196 // We are still on the hall effect and nothing has changed.
197 min_hall_effect_on_angle_ =
198 ::std::min(min_hall_effect_on_angle_, claw.position);
199 max_hall_effect_on_angle_ =
200 ::std::max(max_hall_effect_on_angle_, claw.position);
201 } else if (!any_sensor_triggered && !any_triggered_last_) {
202 // We are still off the hall effect and nothing has changed.
203 min_hall_effect_off_angle_ =
204 ::std::min(min_hall_effect_off_angle_, claw.position);
205 max_hall_effect_off_angle_ =
206 ::std::max(max_hall_effect_off_angle_, claw.position);
207 } else if (any_sensor_triggered && !any_triggered_last_) {
208 // Saw a posedge on the hall effect. Reset the limits.
209 min_hall_effect_on_angle_ = ::std::min(claw.posedge_value, claw.position);
210 max_hall_effect_on_angle_ = ::std::max(claw.posedge_value, claw.position);
211 } else if (!any_sensor_triggered && any_triggered_last_) {
212 // Saw a negedge on the hall effect. Reset the limits.
213 min_hall_effect_off_angle_ = ::std::min(claw.negedge_value, claw.position);
214 max_hall_effect_off_angle_ = ::std::max(claw.negedge_value, claw.position);
215 }
216
217 posedge_value_ = claw.posedge_value;
218 negedge_value_ = claw.negedge_value;
219 last_encoder_ = encoder_;
220 if (front().value() || calibration().value() || back().value()) {
221 last_on_encoder_ = encoder_;
222 } else {
223 last_off_encoder_ = encoder_;
224 }
225 encoder_ = claw.position;
226 any_triggered_last_ = any_sensor_triggered;
227}
228
229void ZeroedStateFeedbackLoop::Reset(const HalfClawPosition &claw) {
230 set_zeroing_state(ZeroedStateFeedbackLoop::UNKNOWN_POSITION);
231
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000232 front_.Reset(claw.front);
233 calibration_.Reset(claw.calibration);
234 back_.Reset(claw.back);
Austin Schuh27b8fb12014-02-22 15:10:05 -0800235 // close up the min and max edge positions as they are no longer valid and
236 // will be expanded in future iterations
237 min_hall_effect_on_angle_ = claw.position;
238 max_hall_effect_on_angle_ = claw.position;
239 min_hall_effect_off_angle_ = claw.position;
240 max_hall_effect_off_angle_ = claw.position;
241 any_triggered_last_ = any_triggered();
242}
243
244bool TopZeroedStateFeedbackLoop::SetCalibrationOnEdge(
245 const constants::Values::Claws::Claw &claw_values,
246 JointZeroingState zeroing_state) {
247 double edge_encoder;
248 double edge_angle;
249 if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) {
250 LOG(INFO, "Calibration edge edge should be %f.\n", edge_angle);
251 SetCalibration(edge_encoder, edge_angle);
252 set_zeroing_state(zeroing_state);
253 return true;
254 }
255 return false;
256}
257
258bool BottomZeroedStateFeedbackLoop::SetCalibrationOnEdge(
259 const constants::Values::Claws::Claw &claw_values,
260 JointZeroingState zeroing_state) {
261 double edge_encoder;
262 double edge_angle;
263 if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) {
264 LOG(INFO, "Calibration edge.\n");
265 SetCalibration(edge_encoder, edge_angle);
266 set_zeroing_state(zeroing_state);
267 return true;
268 }
269 return false;
270}
271
Austin Schuhcc0bf312014-02-09 00:39:29 -0800272ClawMotor::ClawMotor(control_loops::ClawGroup *my_claw)
Brian Silverman71fbee02014-03-13 17:24:54 -0700273 : aos::control_loops::ControlLoop<control_loops::ClawGroup, true, true,
274 false>(my_claw),
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800275 has_top_claw_goal_(false),
276 top_claw_goal_(0.0),
Austin Schuhcda86af2014-02-16 16:16:39 -0800277 top_claw_(this),
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800278 has_bottom_claw_goal_(false),
279 bottom_claw_goal_(0.0),
Austin Schuhcda86af2014-02-16 16:16:39 -0800280 bottom_claw_(this),
281 claw_(MakeClawLoop()),
Ben Fredrickson9b388422014-02-13 06:15:31 +0000282 was_enabled_(false),
Austin Schuh4cb047f2014-02-16 21:10:19 -0800283 doing_calibration_fine_tune_(false),
Austin Schuhe7f90d12014-02-17 00:48:25 -0800284 capped_goal_(false),
285 mode_(UNKNOWN_LOCATION) {}
Austin Schuh3bb9a442014-02-02 16:01:45 -0800286
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800287const int ZeroedStateFeedbackLoop::kZeroingMaxVoltage;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800288
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000289bool ZeroedStateFeedbackLoop::SawFilteredPosedge(
290 const HallEffectTracker &this_sensor, const HallEffectTracker &sensorA,
291 const HallEffectTracker &sensorB) {
292 if (posedge_filter_ == nullptr && this_sensor.posedge_count_changed() &&
293 !sensorA.posedge_count_changed() && !sensorB.posedge_count_changed() &&
294 this_sensor.value() && !this_sensor.last_value()) {
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000295 posedge_filter_ = &this_sensor;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000296 } else if (posedge_filter_ == &this_sensor &&
297 !this_sensor.posedge_count_changed() &&
298 !sensorA.posedge_count_changed() &&
299 !sensorB.posedge_count_changed() && this_sensor.value()) {
300 posedge_filter_ = nullptr;
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000301 return true;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000302 } else if (posedge_filter_ == &this_sensor) {
303 posedge_filter_ = nullptr;
304 }
305 return false;
306}
307
308bool ZeroedStateFeedbackLoop::SawFilteredNegedge(
309 const HallEffectTracker &this_sensor, const HallEffectTracker &sensorA,
310 const HallEffectTracker &sensorB) {
311 if (negedge_filter_ == nullptr && this_sensor.negedge_count_changed() &&
312 !sensorA.negedge_count_changed() && !sensorB.negedge_count_changed() &&
313 !this_sensor.value() && this_sensor.last_value()) {
314 negedge_filter_ = &this_sensor;
315 } else if (negedge_filter_ == &this_sensor &&
316 !this_sensor.negedge_count_changed() &&
317 !sensorA.negedge_count_changed() &&
318 !sensorB.negedge_count_changed() && !this_sensor.value()) {
319 negedge_filter_ = nullptr;
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000320 return true;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000321 } else if (negedge_filter_ == &this_sensor) {
322 negedge_filter_ = nullptr;
323 }
324 return false;
325}
326
Brian Silvermane0a95462014-02-17 00:41:09 -0800327bool ZeroedStateFeedbackLoop::DoGetPositionOfEdge(
328 const constants::Values::Claws::AnglePair &angles, double *edge_encoder,
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000329 double *edge_angle, const HallEffectTracker &this_sensor,
330 const HallEffectTracker &sensorA, const HallEffectTracker &sensorB,
Brian Silvermane0a95462014-02-17 00:41:09 -0800331 const char *hall_effect_name) {
Austin Schuhf84a1302014-02-19 00:23:30 -0800332 bool found_edge = false;
Austin Schuh27b8fb12014-02-22 15:10:05 -0800333
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000334 if (SawFilteredPosedge(this_sensor, sensorA, sensorB)) {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800335 if (min_hall_effect_off_angle_ == max_hall_effect_off_angle_) {
Brian Silvermanf48fab32014-03-09 14:32:24 -0700336 LOG(WARNING, "%s: Uncertain which side, rejecting posedge\n", name_);
Brian Silvermane0a95462014-02-17 00:41:09 -0800337 } else {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800338 const double average_last_encoder =
339 (min_hall_effect_off_angle_ + max_hall_effect_off_angle_) / 2.0;
340 if (posedge_value_ < average_last_encoder) {
341 *edge_angle = angles.upper_decreasing_angle;
342 LOG(INFO, "%s Posedge upper of %s -> %f posedge: %f avg_encoder: %f\n",
343 name_, hall_effect_name, *edge_angle, posedge_value_,
344 average_last_encoder);
345 } else {
346 *edge_angle = angles.lower_angle;
347 LOG(INFO, "%s Posedge lower of %s -> %f posedge: %f avg_encoder: %f\n",
348 name_, hall_effect_name, *edge_angle, posedge_value_,
349 average_last_encoder);
350 }
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000351 *edge_encoder = posedge_value_;
Austin Schuh27b8fb12014-02-22 15:10:05 -0800352 found_edge = true;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000353 }
354 }
355
356 if (SawFilteredNegedge(this_sensor, sensorA, sensorB)) {
357 if (min_hall_effect_on_angle_ == max_hall_effect_on_angle_) {
Brian Silvermanf48fab32014-03-09 14:32:24 -0700358 LOG(WARNING, "%s: Uncertain which side, rejecting negedge\n", name_);
Brian Silvermane0a95462014-02-17 00:41:09 -0800359 } else {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800360 const double average_last_encoder =
361 (min_hall_effect_on_angle_ + max_hall_effect_on_angle_) / 2.0;
362 if (negedge_value_ > average_last_encoder) {
363 *edge_angle = angles.upper_angle;
364 LOG(INFO, "%s Negedge upper of %s -> %f negedge: %f avg_encoder: %f\n",
365 name_, hall_effect_name, *edge_angle, negedge_value_,
366 average_last_encoder);
367 } else {
368 *edge_angle = angles.lower_decreasing_angle;
369 LOG(INFO, "%s Negedge lower of %s -> %f negedge: %f avg_encoder: %f\n",
370 name_, hall_effect_name, *edge_angle, negedge_value_,
371 average_last_encoder);
372 }
373 *edge_encoder = negedge_value_;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000374 found_edge = true;
Brian Silvermane0a95462014-02-17 00:41:09 -0800375 }
Austin Schuh27b8fb12014-02-22 15:10:05 -0800376 }
377
Austin Schuhf84a1302014-02-19 00:23:30 -0800378 return found_edge;
Brian Silvermane0a95462014-02-17 00:41:09 -0800379}
380
Austin Schuhf9286cd2014-02-11 00:51:09 -0800381bool ZeroedStateFeedbackLoop::GetPositionOfEdge(
Austin Schuhd27931c2014-02-16 19:18:20 -0800382 const constants::Values::Claws::Claw &claw_values, double *edge_encoder,
Austin Schuhf9286cd2014-02-11 00:51:09 -0800383 double *edge_angle) {
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000384 if (DoGetPositionOfEdge(claw_values.front, edge_encoder, edge_angle, front_,
385 calibration_, back_, "front")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800386 return true;
387 }
Brian Silvermane0a95462014-02-17 00:41:09 -0800388 if (DoGetPositionOfEdge(claw_values.calibration, edge_encoder, edge_angle,
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000389 calibration_, front_, back_, "calibration")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800390 return true;
391 }
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000392 if (DoGetPositionOfEdge(claw_values.back, edge_encoder, edge_angle, back_,
393 calibration_, front_, "back")) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800394 return true;
395 }
396 return false;
397}
398
Austin Schuhcda86af2014-02-16 16:16:39 -0800399void TopZeroedStateFeedbackLoop::SetCalibration(double edge_encoder,
400 double edge_angle) {
401 double old_offset = offset_;
402 offset_ = edge_angle - edge_encoder;
403 const double doffset = offset_ - old_offset;
404 motor_->ChangeTopOffset(doffset);
405}
406
407void BottomZeroedStateFeedbackLoop::SetCalibration(double edge_encoder,
408 double edge_angle) {
409 double old_offset = offset_;
410 offset_ = edge_angle - edge_encoder;
411 const double doffset = offset_ - old_offset;
412 motor_->ChangeBottomOffset(doffset);
413}
414
415void ClawMotor::ChangeTopOffset(double doffset) {
416 claw_.ChangeTopOffset(doffset);
417 if (has_top_claw_goal_) {
418 top_claw_goal_ += doffset;
419 }
420}
421
422void ClawMotor::ChangeBottomOffset(double doffset) {
423 claw_.ChangeBottomOffset(doffset);
424 if (has_bottom_claw_goal_) {
425 bottom_claw_goal_ += doffset;
426 }
427}
428
429void ClawLimitedLoop::ChangeTopOffset(double doffset) {
430 Y_(1, 0) += doffset;
431 X_hat(1, 0) += doffset;
432 LOG(INFO, "Changing top offset by %f\n", doffset);
433}
434void ClawLimitedLoop::ChangeBottomOffset(double doffset) {
435 Y_(0, 0) += doffset;
436 X_hat(0, 0) += doffset;
437 X_hat(1, 0) -= doffset;
438 LOG(INFO, "Changing bottom offset by %f\n", doffset);
439}
joe7376ff52014-02-16 18:28:42 -0800440
Austin Schuh069143b2014-02-17 02:46:26 -0800441void LimitClawGoal(double *bottom_goal, double *top_goal,
442 const frc971::constants::Values &values) {
443 // first update position based on angle limit
444
445 const double separation = *top_goal - *bottom_goal;
446 if (separation > values.claw.claw_max_separation) {
Austin Schuh069143b2014-02-17 02:46:26 -0800447 const double dsep = (separation - values.claw.claw_max_separation) / 2.0;
448 *bottom_goal += dsep;
449 *top_goal -= dsep;
450 LOG(DEBUG, "Goals now bottom: %f, top: %f\n", *bottom_goal, *top_goal);
451 }
452 if (separation < values.claw.claw_min_separation) {
Austin Schuh069143b2014-02-17 02:46:26 -0800453 const double dsep = (separation - values.claw.claw_min_separation) / 2.0;
454 *bottom_goal += dsep;
455 *top_goal -= dsep;
456 LOG(DEBUG, "Goals now bottom: %f, top: %f\n", *bottom_goal, *top_goal);
457 }
458
459 // now move both goals in unison
460 if (*bottom_goal < values.claw.lower_claw.lower_limit) {
461 *top_goal += values.claw.lower_claw.lower_limit - *bottom_goal;
462 *bottom_goal = values.claw.lower_claw.lower_limit;
463 }
464 if (*bottom_goal > values.claw.lower_claw.upper_limit) {
465 *top_goal -= *bottom_goal - values.claw.lower_claw.upper_limit;
466 *bottom_goal = values.claw.lower_claw.upper_limit;
467 }
468
469 if (*top_goal < values.claw.upper_claw.lower_limit) {
470 *bottom_goal += values.claw.upper_claw.lower_limit - *top_goal;
471 *top_goal = values.claw.upper_claw.lower_limit;
472 }
473 if (*top_goal > values.claw.upper_claw.upper_limit) {
474 *bottom_goal -= *top_goal - values.claw.upper_claw.upper_limit;
475 *top_goal = values.claw.upper_claw.upper_limit;
476 }
477}
Austin Schuhcda86af2014-02-16 16:16:39 -0800478
Austin Schuhe7f90d12014-02-17 00:48:25 -0800479bool ClawMotor::is_ready() const {
480 return (
481 (top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED &&
482 bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) ||
Brian Silverman71fbee02014-03-13 17:24:54 -0700483 (((::aos::robot_state.get() == NULL) ? true
484 : ::aos::robot_state->autonomous) &&
Austin Schuhe7f90d12014-02-17 00:48:25 -0800485 ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
486 top_claw_.zeroing_state() ==
487 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
488 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
489 bottom_claw_.zeroing_state() ==
490 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION))));
491}
492
493bool ClawMotor::is_zeroing() const { return !is_ready(); }
494
Austin Schuh3bb9a442014-02-02 16:01:45 -0800495// Positive angle is up, and positive power is up.
Austin Schuhcc0bf312014-02-09 00:39:29 -0800496void ClawMotor::RunIteration(const control_loops::ClawGroup::Goal *goal,
497 const control_loops::ClawGroup::Position *position,
498 control_loops::ClawGroup::Output *output,
James Kuszmaul9ead1de2014-02-28 21:24:39 -0800499 control_loops::ClawGroup::Status *status) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800500 constexpr double dt = 0.01;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800501
502 // Disable the motors now so that all early returns will return with the
503 // motors disabled.
504 if (output) {
505 output->top_claw_voltage = 0;
506 output->bottom_claw_voltage = 0;
507 output->intake_voltage = 0;
Ben Fredrickson61893d52014-03-02 09:43:23 +0000508 output->tusk_voltage = 0;
509 }
510
Brian Silverman71fbee02014-03-13 17:24:54 -0700511 if (goal) {
512 if (::std::isnan(goal->bottom_angle) ||
513 ::std::isnan(goal->separation_angle) || ::std::isnan(goal->intake) ||
514 ::std::isnan(goal->centering)) {
515 return;
516 }
Austin Schuh3bb9a442014-02-02 16:01:45 -0800517 }
518
Austin Schuh1a499942014-02-17 01:51:58 -0800519 if (reset()) {
Austin Schuh27b8fb12014-02-22 15:10:05 -0800520 top_claw_.Reset(position->top);
521 bottom_claw_.Reset(position->bottom);
Austin Schuh1a499942014-02-17 01:51:58 -0800522 }
523
Austin Schuhf9286cd2014-02-11 00:51:09 -0800524 const frc971::constants::Values &values = constants::GetValues();
525
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800526 if (position) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800527 Eigen::Matrix<double, 2, 1> Y;
528 Y << position->bottom.position + bottom_claw_.offset(),
529 position->top.position + top_claw_.offset();
530 claw_.Correct(Y);
531
Austin Schuhf9286cd2014-02-11 00:51:09 -0800532 top_claw_.SetPositionValues(position->top);
533 bottom_claw_.SetPositionValues(position->bottom);
534
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800535 if (!has_top_claw_goal_) {
536 has_top_claw_goal_ = true;
Austin Schuhcda86af2014-02-16 16:16:39 -0800537 top_claw_goal_ = top_claw_.absolute_position();
Austin Schuhe7f90d12014-02-17 00:48:25 -0800538 initial_separation_ =
Austin Schuhcda86af2014-02-16 16:16:39 -0800539 top_claw_.absolute_position() - bottom_claw_.absolute_position();
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800540 }
541 if (!has_bottom_claw_goal_) {
542 has_bottom_claw_goal_ = true;
Austin Schuhcda86af2014-02-16 16:16:39 -0800543 bottom_claw_goal_ = bottom_claw_.absolute_position();
Austin Schuhe7f90d12014-02-17 00:48:25 -0800544 initial_separation_ =
Austin Schuhcda86af2014-02-16 16:16:39 -0800545 top_claw_.absolute_position() - bottom_claw_.absolute_position();
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800546 }
Brian Silvermanf48fab32014-03-09 14:32:24 -0700547 LOG_STRUCT(DEBUG, "absolute position",
548 ClawPositionToLog(top_claw_.absolute_position(),
549 bottom_claw_.absolute_position()));
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800550 }
551
Brian Silverman71fbee02014-03-13 17:24:54 -0700552 bool autonomous, enabled;
553 if (::aos::robot_state.get() == nullptr) {
554 autonomous = true;
555 enabled = false;
556 } else {
557 autonomous = ::aos::robot_state->autonomous;
558 enabled = ::aos::robot_state->enabled;
559 }
Austin Schuh069143b2014-02-17 02:46:26 -0800560
561 double bottom_claw_velocity_ = 0.0;
562 double top_claw_velocity_ = 0.0;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800563
Brian Silverman71fbee02014-03-13 17:24:54 -0700564 if (goal != NULL &&
565 ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED &&
566 bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) ||
567 (autonomous &&
568 ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
569 top_claw_.zeroing_state() ==
570 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
571 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
572 bottom_claw_.zeroing_state() ==
573 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION))))) {
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800574 // Ready to use the claw.
575 // Limit the goals here.
Austin Schuhf9286cd2014-02-11 00:51:09 -0800576 bottom_claw_goal_ = goal->bottom_angle;
Brian Silverman7c021c42014-02-17 15:15:56 -0800577 top_claw_goal_ = goal->bottom_angle + goal->separation_angle;
Austin Schuhcda86af2014-02-16 16:16:39 -0800578 has_bottom_claw_goal_ = true;
579 has_top_claw_goal_ = true;
580 doing_calibration_fine_tune_ = false;
581
Austin Schuhe7f90d12014-02-17 00:48:25 -0800582 mode_ = READY;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800583 } else if (top_claw_.zeroing_state() !=
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000584 ZeroedStateFeedbackLoop::UNKNOWN_POSITION &&
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800585 bottom_claw_.zeroing_state() !=
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000586 ZeroedStateFeedbackLoop::UNKNOWN_POSITION) {
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800587 // Time to fine tune the zero.
588 // Limit the goals here.
Austin Schuh0c733422014-02-17 01:17:12 -0800589 if (!enabled) {
590 // If we are disabled, start the fine tune process over again.
591 doing_calibration_fine_tune_ = false;
592 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800593 if (bottom_claw_.zeroing_state() != ZeroedStateFeedbackLoop::CALIBRATED) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800594 // always get the bottom claw to calibrated first
595 LOG(DEBUG, "Calibrating the bottom of the claw\n");
596 if (!doing_calibration_fine_tune_) {
597 if (::std::abs(bottom_absolute_position() -
Austin Schuhd27931c2014-02-16 19:18:20 -0800598 values.claw.start_fine_tune_pos) <
599 values.claw.claw_unimportant_epsilon) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800600 doing_calibration_fine_tune_ = true;
Austin Schuhd27931c2014-02-16 19:18:20 -0800601 bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800602 top_claw_velocity_ = bottom_claw_velocity_ =
603 values.claw.claw_zeroing_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800604 LOG(DEBUG, "Ready to fine tune the bottom\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800605 mode_ = FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800606 } else {
607 // send bottom to zeroing start
Austin Schuhd27931c2014-02-16 19:18:20 -0800608 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuhcda86af2014-02-16 16:16:39 -0800609 LOG(DEBUG, "Going to the start position for the bottom\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800610 mode_ = PREP_FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800611 }
612 } else {
Austin Schuhe7f90d12014-02-17 00:48:25 -0800613 mode_ = FINE_TUNE_BOTTOM;
Austin Schuhd27931c2014-02-16 19:18:20 -0800614 bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800615 top_claw_velocity_ = bottom_claw_velocity_ =
616 values.claw.claw_zeroing_speed;
Brian Silvermane0a95462014-02-17 00:41:09 -0800617 if (top_claw_.front_or_back_triggered() ||
618 bottom_claw_.front_or_back_triggered()) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800619 // We shouldn't hit a limit, but if we do, go back to the zeroing
620 // point and try again.
621 doing_calibration_fine_tune_ = false;
Austin Schuhd27931c2014-02-16 19:18:20 -0800622 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuh069143b2014-02-17 02:46:26 -0800623 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
Austin Schuhcda86af2014-02-16 16:16:39 -0800624 LOG(DEBUG, "Found a limit, starting over.\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800625 mode_ = PREP_FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800626 }
Austin Schuh288c8c32014-02-16 17:20:17 -0800627
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000628 if (position && bottom_claw_.SawFilteredPosedge(
629 bottom_claw_.calibration(), bottom_claw_.front(),
630 bottom_claw_.back())) {
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000631 // do calibration
632 bottom_claw_.SetCalibration(
633 position->bottom.posedge_value,
634 values.claw.lower_claw.calibration.lower_angle);
635 bottom_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED);
636 // calibrated so we are done fine tuning bottom
637 doing_calibration_fine_tune_ = false;
638 LOG(DEBUG, "Calibrated the bottom correctly!\n");
639 } else if (bottom_claw_.calibration().last_value()) {
640 doing_calibration_fine_tune_ = false;
641 bottom_claw_goal_ = values.claw.start_fine_tune_pos;
642 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
643 mode_ = PREP_FINE_TUNE_BOTTOM;
Austin Schuhcda86af2014-02-16 16:16:39 -0800644 } else {
645 LOG(DEBUG, "Fine tuning\n");
646 }
647 }
648 // now set the top claw to track
649
Austin Schuhd27931c2014-02-16 19:18:20 -0800650 top_claw_goal_ = bottom_claw_goal_ + values.claw.claw_zeroing_separation;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800651 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800652 // bottom claw must be calibrated, start on the top
653 if (!doing_calibration_fine_tune_) {
Austin Schuhd27931c2014-02-16 19:18:20 -0800654 if (::std::abs(top_absolute_position() -
655 values.claw.start_fine_tune_pos) <
656 values.claw.claw_unimportant_epsilon) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800657 doing_calibration_fine_tune_ = true;
Austin Schuhd27931c2014-02-16 19:18:20 -0800658 top_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800659 top_claw_velocity_ = bottom_claw_velocity_ =
660 values.claw.claw_zeroing_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800661 LOG(DEBUG, "Ready to fine tune the top\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800662 mode_ = FINE_TUNE_TOP;
Austin Schuhcda86af2014-02-16 16:16:39 -0800663 } else {
664 // send top to zeroing start
Austin Schuhd27931c2014-02-16 19:18:20 -0800665 top_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuhcda86af2014-02-16 16:16:39 -0800666 LOG(DEBUG, "Going to the start position for the top\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800667 mode_ = PREP_FINE_TUNE_TOP;
Austin Schuhcda86af2014-02-16 16:16:39 -0800668 }
669 } else {
Austin Schuhe7f90d12014-02-17 00:48:25 -0800670 mode_ = FINE_TUNE_TOP;
Austin Schuhd27931c2014-02-16 19:18:20 -0800671 top_claw_goal_ += values.claw.claw_zeroing_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800672 top_claw_velocity_ = bottom_claw_velocity_ =
673 values.claw.claw_zeroing_speed;
Brian Silvermane0a95462014-02-17 00:41:09 -0800674 if (top_claw_.front_or_back_triggered() ||
675 bottom_claw_.front_or_back_triggered()) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800676 // this should not happen, but now we know it won't
677 doing_calibration_fine_tune_ = false;
Austin Schuhd27931c2014-02-16 19:18:20 -0800678 top_claw_goal_ = values.claw.start_fine_tune_pos;
Austin Schuh069143b2014-02-17 02:46:26 -0800679 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
Austin Schuhcda86af2014-02-16 16:16:39 -0800680 LOG(DEBUG, "Found a limit, starting over.\n");
Austin Schuhe7f90d12014-02-17 00:48:25 -0800681 mode_ = PREP_FINE_TUNE_TOP;
Austin Schuhcda86af2014-02-16 16:16:39 -0800682 }
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000683
684 if (position &&
685 top_claw_.SawFilteredPosedge(top_claw_.calibration(),
686 top_claw_.front(), top_claw_.back())) {
687 // do calibration
688 top_claw_.SetCalibration(
689 position->top.posedge_value,
690 values.claw.upper_claw.calibration.lower_angle);
691 top_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED);
692 // calibrated so we are done fine tuning top
693 doing_calibration_fine_tune_ = false;
694 LOG(DEBUG, "Calibrated the top correctly!\n");
695 } else if (top_claw_.calibration().last_value()) {
696 doing_calibration_fine_tune_ = false;
697 top_claw_goal_ = values.claw.start_fine_tune_pos;
698 top_claw_velocity_ = bottom_claw_velocity_ = 0.0;
699 mode_ = PREP_FINE_TUNE_TOP;
Austin Schuhcda86af2014-02-16 16:16:39 -0800700 }
701 }
702 // now set the bottom claw to track
Austin Schuhd27931c2014-02-16 19:18:20 -0800703 bottom_claw_goal_ = top_claw_goal_ - values.claw.claw_zeroing_separation;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800704 }
705 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800706 doing_calibration_fine_tune_ = false;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800707 if (!was_enabled_ && enabled) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800708 if (position) {
Brian Silverman72035692014-03-14 10:18:27 -0700709 top_claw_goal_ = position->top.position + top_claw_.offset();
710 bottom_claw_goal_ = position->bottom.position + bottom_claw_.offset();
Austin Schuhe7f90d12014-02-17 00:48:25 -0800711 initial_separation_ =
Austin Schuhcda86af2014-02-16 16:16:39 -0800712 position->top.position - position->bottom.position;
Austin Schuhf9286cd2014-02-11 00:51:09 -0800713 } else {
714 has_top_claw_goal_ = false;
715 has_bottom_claw_goal_ = false;
716 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800717 }
Austin Schuhf9286cd2014-02-11 00:51:09 -0800718
Austin Schuh4cb047f2014-02-16 21:10:19 -0800719 if ((bottom_claw_.zeroing_state() !=
720 ZeroedStateFeedbackLoop::UNKNOWN_POSITION ||
Brian Silvermane0a95462014-02-17 00:41:09 -0800721 bottom_claw_.front().value() || top_claw_.front().value()) &&
722 !top_claw_.back().value() && !bottom_claw_.back().value()) {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800723 if (enabled) {
724 // Time to slowly move back up to find any position to narrow down the
725 // zero.
Austin Schuhd27931c2014-02-16 19:18:20 -0800726 top_claw_goal_ += values.claw.claw_zeroing_off_speed * dt;
727 bottom_claw_goal_ += values.claw.claw_zeroing_off_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800728 top_claw_velocity_ = bottom_claw_velocity_ =
729 values.claw.claw_zeroing_off_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800730 LOG(DEBUG, "Bottom is known.\n");
Austin Schuhf9286cd2014-02-11 00:51:09 -0800731 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800732 } else {
733 // We don't know where either claw is. Slowly start moving down to find
734 // any hall effect.
Austin Schuhf9286cd2014-02-11 00:51:09 -0800735 if (enabled) {
Austin Schuhd27931c2014-02-16 19:18:20 -0800736 top_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt;
737 bottom_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt;
Austin Schuh069143b2014-02-17 02:46:26 -0800738 top_claw_velocity_ = bottom_claw_velocity_ =
739 -values.claw.claw_zeroing_off_speed;
Austin Schuhcda86af2014-02-16 16:16:39 -0800740 LOG(DEBUG, "Both are unknown.\n");
Austin Schuhf9286cd2014-02-11 00:51:09 -0800741 }
742 }
743
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +0000744 if (position) {
745 if (enabled) {
746 top_claw_.SetCalibrationOnEdge(
747 values.claw.upper_claw,
748 ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION);
749 bottom_claw_.SetCalibrationOnEdge(
750 values.claw.lower_claw,
751 ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION);
752 } else {
753 // TODO(austin): Only calibrate on the predetermined edge.
754 // We might be able to just ignore this since the backlash is soooo
755 // low.
756 // :)
757 top_claw_.SetCalibrationOnEdge(
758 values.claw.upper_claw,
759 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
760 bottom_claw_.SetCalibrationOnEdge(
761 values.claw.lower_claw,
762 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
763 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800764 }
Austin Schuhe7f90d12014-02-17 00:48:25 -0800765 mode_ = UNKNOWN_LOCATION;
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800766 }
767
Austin Schuh069143b2014-02-17 02:46:26 -0800768 // Limit the goals if both claws have been (mostly) found.
769 if (mode_ != UNKNOWN_LOCATION) {
770 LimitClawGoal(&bottom_claw_goal_, &top_claw_goal_, values);
771 }
772
Austin Schuhf9286cd2014-02-11 00:51:09 -0800773 if (has_top_claw_goal_ && has_bottom_claw_goal_) {
Austin Schuh069143b2014-02-17 02:46:26 -0800774 claw_.R << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_,
775 bottom_claw_velocity_, top_claw_velocity_ - bottom_claw_velocity_;
Austin Schuhcda86af2014-02-16 16:16:39 -0800776 double separation = -971;
777 if (position != nullptr) {
778 separation = position->top.position - position->bottom.position;
779 }
Brian Silvermanf48fab32014-03-09 14:32:24 -0700780 LOG_STRUCT(DEBUG, "actual goal",
781 ClawGoalToLog(claw_.R(0, 0), claw_.R(1, 0), separation));
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800782
Austin Schuh01c652b2014-02-21 23:13:42 -0800783 // Only cap power when one of the halves of the claw is moving slowly and
784 // could wind up.
Austin Schuhe7f90d12014-02-17 00:48:25 -0800785 claw_.set_is_zeroing(mode_ == UNKNOWN_LOCATION || mode_ == FINE_TUNE_TOP ||
786 mode_ == FINE_TUNE_BOTTOM);
Austin Schuhcda86af2014-02-16 16:16:39 -0800787 claw_.Update(output == nullptr);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800788 } else {
Austin Schuhcda86af2014-02-16 16:16:39 -0800789 claw_.Update(true);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800790 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800791
Austin Schuh4cb047f2014-02-16 21:10:19 -0800792 capped_goal_ = false;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800793 switch (mode_) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800794 case READY:
Austin Schuhe7f90d12014-02-17 00:48:25 -0800795 case PREP_FINE_TUNE_TOP:
796 case PREP_FINE_TUNE_BOTTOM:
Austin Schuhcda86af2014-02-16 16:16:39 -0800797 break;
Austin Schuhe7f90d12014-02-17 00:48:25 -0800798 case FINE_TUNE_BOTTOM:
799 case FINE_TUNE_TOP:
Austin Schuh4cb047f2014-02-16 21:10:19 -0800800 case UNKNOWN_LOCATION: {
Brian Silverman6dd2c532014-03-29 23:34:39 -0700801 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 -0800802 if (claw_.uncapped_average_voltage() > values.claw.max_zeroing_voltage) {
Brian Silverman6dd2c532014-03-29 23:34:39 -0700803 double dx_bot = (claw_.U_uncapped(0, 0) -
Austin Schuh4cb047f2014-02-16 21:10:19 -0800804 values.claw.max_zeroing_voltage) /
805 claw_.K(0, 0);
Brian Silverman6dd2c532014-03-29 23:34:39 -0700806 double dx_top = (claw_.U_uncapped(1, 0) -
James Kuszmaul0e866512014-02-21 13:12:52 -0800807 values.claw.max_zeroing_voltage) /
808 claw_.K(0, 0);
809 double dx = ::std::max(dx_top, dx_bot);
Austin Schuh4cb047f2014-02-16 21:10:19 -0800810 bottom_claw_goal_ -= dx;
811 top_claw_goal_ -= dx;
James Kuszmaul0e866512014-02-21 13:12:52 -0800812 Eigen::Matrix<double, 4, 1> R;
813 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 -0700814 claw_.U = claw_.K() * (R - claw_.X_hat);
Austin Schuhcda86af2014-02-16 16:16:39 -0800815 capped_goal_ = true;
Brian Silvermanf48fab32014-03-09 14:32:24 -0700816 LOG(DEBUG, "Moving the goal by %f to prevent windup."
817 " Uncapped is %f, max is %f, difference is %f\n",
818 dx,
Austin Schuhe7f90d12014-02-17 00:48:25 -0800819 claw_.uncapped_average_voltage(), values.claw.max_zeroing_voltage,
820 (claw_.uncapped_average_voltage() -
821 values.claw.max_zeroing_voltage));
Austin Schuh4cb047f2014-02-16 21:10:19 -0800822 } else if (claw_.uncapped_average_voltage() <
823 -values.claw.max_zeroing_voltage) {
Brian Silverman6dd2c532014-03-29 23:34:39 -0700824 double dx_bot = (claw_.U_uncapped(0, 0) +
Austin Schuh4cb047f2014-02-16 21:10:19 -0800825 values.claw.max_zeroing_voltage) /
826 claw_.K(0, 0);
Brian Silverman6dd2c532014-03-29 23:34:39 -0700827 double dx_top = (claw_.U_uncapped(1, 0) +
James Kuszmaul0e866512014-02-21 13:12:52 -0800828 values.claw.max_zeroing_voltage) /
829 claw_.K(0, 0);
830 double dx = ::std::min(dx_top, dx_bot);
Austin Schuh4cb047f2014-02-16 21:10:19 -0800831 bottom_claw_goal_ -= dx;
832 top_claw_goal_ -= dx;
James Kuszmaul0e866512014-02-21 13:12:52 -0800833 Eigen::Matrix<double, 4, 1> R;
834 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 -0700835 claw_.U = claw_.K() * (R - claw_.X_hat);
Austin Schuhcda86af2014-02-16 16:16:39 -0800836 capped_goal_ = true;
Austin Schuh4cb047f2014-02-16 21:10:19 -0800837 LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx);
Austin Schuhcda86af2014-02-16 16:16:39 -0800838 }
Austin Schuh4cb047f2014-02-16 21:10:19 -0800839 } break;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800840 }
841
842 if (output) {
Ben Fredrickson2f76ddf2014-02-23 05:58:23 +0000843 if (goal) {
844 //setup the intake
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000845 output->intake_voltage =
846 (goal->intake > 12.0) ? 12 : (goal->intake < -12.0) ? -12.0
847 : goal->intake;
Ben Fredrickson2f76ddf2014-02-23 05:58:23 +0000848 output->tusk_voltage = goal->centering;
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000849 output->tusk_voltage =
850 (goal->centering > 12.0) ? 12 : (goal->centering < -12.0)
851 ? -12.0
852 : goal->centering;
Ben Fredrickson2f76ddf2014-02-23 05:58:23 +0000853 }
James Kuszmauld536a402014-02-18 22:32:12 -0800854 output->top_claw_voltage = claw_.U(1, 0);
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000855 output->bottom_claw_voltage = claw_.U(0, 0);
Austin Schuhf84a1302014-02-19 00:23:30 -0800856
857 if (output->top_claw_voltage > kMaxVoltage) {
858 output->top_claw_voltage = kMaxVoltage;
859 } else if (output->top_claw_voltage < -kMaxVoltage) {
860 output->top_claw_voltage = -kMaxVoltage;
861 }
862
863 if (output->bottom_claw_voltage > kMaxVoltage) {
864 output->bottom_claw_voltage = kMaxVoltage;
865 } else if (output->bottom_claw_voltage < -kMaxVoltage) {
866 output->bottom_claw_voltage = -kMaxVoltage;
867 }
Austin Schuh3bb9a442014-02-02 16:01:45 -0800868 }
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800869
James Kuszmaul4abaf482014-02-26 21:16:35 -0800870 status->bottom = bottom_absolute_position();
871 status->separation = top_absolute_position() - bottom_absolute_position();
872 status->bottom_velocity = claw_.X_hat(2, 0);
873 status->separation_velocity = claw_.X_hat(3, 0);
Austin Schuh4b7b5d02014-02-10 21:20:34 -0800874
Brian Silverman71fbee02014-03-13 17:24:54 -0700875 if (goal) {
876 bool bottom_done =
877 ::std::abs(bottom_absolute_position() - goal->bottom_angle) < 0.020;
878 bool bottom_velocity_done = ::std::abs(status->bottom_velocity) < 0.2;
879 bool separation_done =
880 ::std::abs((top_absolute_position() - bottom_absolute_position()) -
881 goal->separation_angle) < 0.020;
882 bool separation_done_with_ball =
883 ::std::abs((top_absolute_position() - bottom_absolute_position()) -
884 goal->separation_angle) < 0.06;
885 status->done = is_ready() && separation_done && bottom_done && bottom_velocity_done;
886 status->done_with_ball =
887 is_ready() && separation_done_with_ball && bottom_done && bottom_velocity_done;
888 } else {
889 status->done = status->done_with_ball = false;
890 }
Austin Schuha556b012014-03-02 11:55:52 -0800891
Austin Schuh4f8633f2014-03-02 13:59:46 -0800892 status->zeroed = is_ready();
Brian Silverman80fc94c2014-03-09 16:56:01 -0700893 status->zeroed_for_auto =
894 (top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
895 top_claw_.zeroing_state() ==
896 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) &&
897 (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED ||
898 bottom_claw_.zeroing_state() ==
899 ZeroedStateFeedbackLoop::DISABLED_CALIBRATION);
Austin Schuh4f8633f2014-03-02 13:59:46 -0800900
Brian Silverman71fbee02014-03-13 17:24:54 -0700901 was_enabled_ = enabled;
Austin Schuh3bb9a442014-02-02 16:01:45 -0800902}
903
904} // namespace control_loops
905} // namespace frc971
Ben Fredrickson81ba2d52014-03-02 08:21:46 +0000906