blob: e2f104c9ee1811466025086c0eaa9cbcec160dd8 [file] [log] [blame]
Brian Silverman17f503e2015-08-02 18:17:18 -07001#include "y2014/control_loops/shooter/shooter.h"
2
3#include <stdio.h>
4
5#include <algorithm>
6#include <limits>
Austin Schuh214e9c12016-11-25 17:26:20 -08007#include <chrono>
Brian Silverman17f503e2015-08-02 18:17:18 -07008
John Park33858a32018-09-28 23:05:48 -07009#include "aos/controls/control_loops.q.h"
10#include "aos/logging/logging.h"
11#include "aos/logging/queue_logging.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070012
13#include "y2014/constants.h"
14#include "y2014/control_loops/shooter/shooter_motor_plant.h"
15
Austin Schuh24957102015-11-28 16:04:40 -080016namespace y2014 {
Brian Silverman17f503e2015-08-02 18:17:18 -070017namespace control_loops {
18
Austin Schuh214e9c12016-11-25 17:26:20 -080019namespace chrono = ::std::chrono;
20using ::aos::monotonic_clock;
21
Austin Schuh9d4aca82015-11-08 14:41:31 -080022using ::y2014::control_loops::shooter::kSpringConstant;
23using ::y2014::control_loops::shooter::kMaxExtension;
Austin Schuh0e997732015-11-08 15:14:53 -080024using ::y2014::control_loops::shooter::kDt;
Austin Schuh9d4aca82015-11-08 14:41:31 -080025using ::y2014::control_loops::shooter::MakeShooterLoop;
Brian Silverman17f503e2015-08-02 18:17:18 -070026
27void ZeroedStateFeedbackLoop::CapU() {
28 const double old_voltage = voltage_;
29 voltage_ += U(0, 0);
30
31 uncapped_voltage_ = voltage_;
32
33 // Make sure that reality and the observer can't get too far off. There is a
34 // delay by one cycle between the applied voltage and X_hat(2, 0), so compare
35 // against last cycle's voltage.
36 if (X_hat(2, 0) > last_voltage_ + 4.0) {
37 voltage_ -= X_hat(2, 0) - (last_voltage_ + 4.0);
38 LOG(DEBUG, "Capping due to runaway\n");
39 } else if (X_hat(2, 0) < last_voltage_ - 4.0) {
40 voltage_ += X_hat(2, 0) - (last_voltage_ - 4.0);
41 LOG(DEBUG, "Capping due to runaway\n");
42 }
43
44 voltage_ = std::min(max_voltage_, voltage_);
45 voltage_ = std::max(-max_voltage_, voltage_);
46 mutable_U(0, 0) = voltage_ - old_voltage;
47
Austin Schuh24957102015-11-28 16:04:40 -080048 LOG_STRUCT(
49 DEBUG, "shooter_output",
Brian Silvermanb601d892015-12-20 18:24:38 -050050 ::y2014::control_loops::ShooterVoltageToLog(X_hat(2, 0), voltage_));
Brian Silverman17f503e2015-08-02 18:17:18 -070051
52 last_voltage_ = voltage_;
53 capped_goal_ = false;
54}
55
56void ZeroedStateFeedbackLoop::CapGoal() {
57 if (uncapped_voltage() > max_voltage_) {
58 double dx;
Austin Schuhc5fceb82017-02-25 16:24:12 -080059 if (index() == 0) {
Brian Silverman17f503e2015-08-02 18:17:18 -070060 dx = (uncapped_voltage() - max_voltage_) /
Austin Schuh32501832017-02-25 18:32:56 -080061 (controller().K(0, 0) -
62 plant().A(1, 0) * controller().K(0, 2) / plant().A(1, 2));
Brian Silverman17f503e2015-08-02 18:17:18 -070063 mutable_R(0, 0) -= dx;
Austin Schuhc5fceb82017-02-25 16:24:12 -080064 mutable_R(2, 0) -= -plant().A(1, 0) / plant().A(1, 2) * dx;
Brian Silverman17f503e2015-08-02 18:17:18 -070065 } else {
Austin Schuh32501832017-02-25 18:32:56 -080066 dx = (uncapped_voltage() - max_voltage_) / controller().K(0, 0);
Brian Silverman17f503e2015-08-02 18:17:18 -070067 mutable_R(0, 0) -= dx;
68 }
69 capped_goal_ = true;
Austin Schuh24957102015-11-28 16:04:40 -080070 LOG_STRUCT(DEBUG, "to prevent windup",
Brian Silvermanb601d892015-12-20 18:24:38 -050071 ::y2014::control_loops::ShooterMovingGoal(dx));
Brian Silverman17f503e2015-08-02 18:17:18 -070072 } else if (uncapped_voltage() < -max_voltage_) {
73 double dx;
Austin Schuhc5fceb82017-02-25 16:24:12 -080074 if (index() == 0) {
Brian Silverman17f503e2015-08-02 18:17:18 -070075 dx = (uncapped_voltage() + max_voltage_) /
Austin Schuh32501832017-02-25 18:32:56 -080076 (controller().K(0, 0) -
77 plant().A(1, 0) * controller().K(0, 2) / plant().A(1, 2));
Brian Silverman17f503e2015-08-02 18:17:18 -070078 mutable_R(0, 0) -= dx;
Austin Schuhc5fceb82017-02-25 16:24:12 -080079 mutable_R(2, 0) -= -plant().A(1, 0) / plant().A(1, 2) * dx;
Brian Silverman17f503e2015-08-02 18:17:18 -070080 } else {
Austin Schuh32501832017-02-25 18:32:56 -080081 dx = (uncapped_voltage() + max_voltage_) / controller().K(0, 0);
Brian Silverman17f503e2015-08-02 18:17:18 -070082 mutable_R(0, 0) -= dx;
83 }
84 capped_goal_ = true;
Austin Schuh24957102015-11-28 16:04:40 -080085 LOG_STRUCT(DEBUG, "to prevent windup",
Brian Silvermanb601d892015-12-20 18:24:38 -050086 ::y2014::control_loops::ShooterMovingGoal(dx));
Brian Silverman17f503e2015-08-02 18:17:18 -070087 } else {
88 capped_goal_ = false;
89 }
90}
91
92void ZeroedStateFeedbackLoop::RecalculatePowerGoal() {
Austin Schuhc5fceb82017-02-25 16:24:12 -080093 if (index() == 0) {
94 mutable_R(2, 0) = (-plant().A(1, 0) / plant().A(1, 2) * R(0, 0) -
95 plant().A(1, 1) / plant().A(1, 2) * R(1, 0));
Brian Silverman17f503e2015-08-02 18:17:18 -070096 } else {
Austin Schuhc5fceb82017-02-25 16:24:12 -080097 mutable_R(2, 0) = -plant().A(1, 1) / plant().A(1, 2) * R(1, 0);
Brian Silverman17f503e2015-08-02 18:17:18 -070098 }
99}
100
101void ZeroedStateFeedbackLoop::SetCalibration(double encoder_val,
102 double known_position) {
103 double old_position = absolute_position();
104 double previous_offset = offset_;
105 offset_ = known_position - encoder_val;
106 double doffset = offset_ - previous_offset;
107 mutable_X_hat(0, 0) += doffset;
Brian Silverman17f503e2015-08-02 18:17:18 -0700108 // Offset the goal so we don't move.
109 mutable_R(0, 0) += doffset;
Austin Schuhc5fceb82017-02-25 16:24:12 -0800110 if (index() == 0) {
111 mutable_R(2, 0) += -plant().A(1, 0) / plant().A(1, 2) * (doffset);
Brian Silverman17f503e2015-08-02 18:17:18 -0700112 }
Austin Schuh24957102015-11-28 16:04:40 -0800113 LOG_STRUCT(DEBUG, "sensor edge (fake?)",
Brian Silvermanb601d892015-12-20 18:24:38 -0500114 ::y2014::control_loops::ShooterChangeCalibration(
Austin Schuh24957102015-11-28 16:04:40 -0800115 encoder_val, known_position, old_position, absolute_position(),
116 previous_offset, offset_));
Brian Silverman17f503e2015-08-02 18:17:18 -0700117}
118
Brian Silvermanb601d892015-12-20 18:24:38 -0500119ShooterMotor::ShooterMotor(::y2014::control_loops::ShooterQueue *my_shooter)
120 : aos::controls::ControlLoop<::y2014::control_loops::ShooterQueue>(
Austin Schuh24957102015-11-28 16:04:40 -0800121 my_shooter),
Brian Silverman17f503e2015-08-02 18:17:18 -0700122 shooter_(MakeShooterLoop()),
123 state_(STATE_INITIALIZE),
Brian Silverman17f503e2015-08-02 18:17:18 -0700124 cycles_not_moved_(0),
125 shot_count_(0),
126 zeroed_(false),
127 distal_posedge_validation_cycles_left_(0),
128 proximal_posedge_validation_cycles_left_(0),
129 last_distal_current_(true),
130 last_proximal_current_(true) {}
131
132double ShooterMotor::PowerToPosition(double power) {
Austin Schuh24957102015-11-28 16:04:40 -0800133 const constants::Values &values = constants::GetValues();
Brian Silverman17f503e2015-08-02 18:17:18 -0700134 double maxpower = 0.5 * kSpringConstant *
135 (kMaxExtension * kMaxExtension -
136 (kMaxExtension - values.shooter.upper_limit) *
137 (kMaxExtension - values.shooter.upper_limit));
138 if (power < 0) {
Austin Schuh24957102015-11-28 16:04:40 -0800139 LOG_STRUCT(WARNING, "negative power",
Brian Silvermanb601d892015-12-20 18:24:38 -0500140 ::y2014::control_loops::PowerAdjustment(power, 0));
Brian Silverman17f503e2015-08-02 18:17:18 -0700141 power = 0;
142 } else if (power > maxpower) {
Austin Schuh24957102015-11-28 16:04:40 -0800143 LOG_STRUCT(WARNING, "power too high",
Brian Silvermanb601d892015-12-20 18:24:38 -0500144 ::y2014::control_loops::PowerAdjustment(power, maxpower));
Brian Silverman17f503e2015-08-02 18:17:18 -0700145 power = maxpower;
146 }
147
148 double mp = kMaxExtension * kMaxExtension - (power + power) / kSpringConstant;
149 double new_pos = 0.10;
150 if (mp < 0) {
151 LOG(ERROR,
152 "Power calculation has negative number before square root (%f).\n", mp);
153 } else {
154 new_pos = kMaxExtension - ::std::sqrt(mp);
155 }
156
157 new_pos = ::std::min(::std::max(new_pos, values.shooter.lower_limit),
158 values.shooter.upper_limit);
159 return new_pos;
160}
161
162double ShooterMotor::PositionToPower(double position) {
163 double power = kSpringConstant * position * (kMaxExtension - position / 2.0);
164 return power;
165}
166
167void ShooterMotor::CheckCalibrations(
Brian Silvermanb601d892015-12-20 18:24:38 -0500168 const ::y2014::control_loops::ShooterQueue::Position *position) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700169 CHECK_NOTNULL(position);
Austin Schuh24957102015-11-28 16:04:40 -0800170 const constants::Values &values = constants::GetValues();
Brian Silverman17f503e2015-08-02 18:17:18 -0700171
172 // TODO(austin): Validate that this is the right edge.
173 // If we see a posedge on any of the hall effects,
174 if (position->pusher_proximal.posedge_count != last_proximal_posedge_count_ &&
175 !last_proximal_current_) {
176 proximal_posedge_validation_cycles_left_ = 2;
177 }
178 if (proximal_posedge_validation_cycles_left_ > 0) {
179 if (position->pusher_proximal.current) {
180 --proximal_posedge_validation_cycles_left_;
181 if (proximal_posedge_validation_cycles_left_ == 0) {
182 shooter_.SetCalibration(
183 position->pusher_proximal.posedge_value,
184 values.shooter.pusher_proximal.upper_angle);
185
186 LOG(DEBUG, "Setting calibration using proximal sensor\n");
187 zeroed_ = true;
188 }
189 } else {
190 proximal_posedge_validation_cycles_left_ = 0;
191 }
192 }
193
194 if (position->pusher_distal.posedge_count != last_distal_posedge_count_ &&
195 !last_distal_current_) {
196 distal_posedge_validation_cycles_left_ = 2;
197 }
198 if (distal_posedge_validation_cycles_left_ > 0) {
199 if (position->pusher_distal.current) {
200 --distal_posedge_validation_cycles_left_;
201 if (distal_posedge_validation_cycles_left_ == 0) {
202 shooter_.SetCalibration(
203 position->pusher_distal.posedge_value,
204 values.shooter.pusher_distal.upper_angle);
205
206 LOG(DEBUG, "Setting calibration using distal sensor\n");
207 zeroed_ = true;
208 }
209 } else {
210 distal_posedge_validation_cycles_left_ = 0;
211 }
212 }
213}
214
215// Positive is out, and positive power is out.
216void ShooterMotor::RunIteration(
Brian Silvermanb601d892015-12-20 18:24:38 -0500217 const ::y2014::control_loops::ShooterQueue::Goal *goal,
218 const ::y2014::control_loops::ShooterQueue::Position *position,
219 ::y2014::control_loops::ShooterQueue::Output *output,
220 ::y2014::control_loops::ShooterQueue::Status *status) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700221 if (goal && ::std::isnan(goal->shot_power)) {
222 state_ = STATE_ESTOP;
223 LOG(ERROR, "Estopping because got a shot power of NAN.\n");
224 }
225
226 // we must always have these or we have issues.
227 if (status == NULL) {
228 if (output) output->voltage = 0;
229 LOG(ERROR, "Thought I would just check for null and die.\n");
230 return;
231 }
232 status->ready = false;
233
234 if (WasReset()) {
235 state_ = STATE_INITIALIZE;
236 last_distal_current_ = position->pusher_distal.current;
237 last_proximal_current_ = position->pusher_proximal.current;
238 }
239 if (position) {
240 shooter_.CorrectPosition(position->position);
241 }
242
243 // Disable the motors now so that all early returns will return with the
244 // motors disabled.
245 if (output) output->voltage = 0;
246
Austin Schuh24957102015-11-28 16:04:40 -0800247 const constants::Values &values = constants::GetValues();
Brian Silverman17f503e2015-08-02 18:17:18 -0700248
249 // Don't even let the control loops run.
250 bool shooter_loop_disable = false;
251
252 const bool disabled =
253 !::aos::joystick_state.get() || !::aos::joystick_state->enabled;
254
255 // If true, move the goal if we saturate.
256 bool cap_goal = false;
257
258 // TODO(austin): Move the offset if we see or don't see a hall effect when we
259 // expect to see one.
260 // Probably not needed yet.
261
262 if (position) {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800263 int last_index = shooter_.index();
Brian Silverman17f503e2015-08-02 18:17:18 -0700264 if (position->plunger && position->latch) {
265 // Use the controller without the spring if the latch is set and the
266 // plunger is back
Austin Schuhc5fceb82017-02-25 16:24:12 -0800267 shooter_.set_index(1);
Brian Silverman17f503e2015-08-02 18:17:18 -0700268 } else {
269 // Otherwise use the controller with the spring.
Austin Schuhc5fceb82017-02-25 16:24:12 -0800270 shooter_.set_index(0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700271 }
Austin Schuhc5fceb82017-02-25 16:24:12 -0800272 if (shooter_.index() != last_index) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700273 shooter_.RecalculatePowerGoal();
274 }
275 }
276
277 switch (state_) {
278 case STATE_INITIALIZE:
279 if (position) {
280 // Reinitialize the internal filter state.
281 shooter_.InitializeState(position->position);
282
283 // Start off with the assumption that we are at the value
284 // futhest back given our sensors.
285 if (position->pusher_distal.current) {
286 shooter_.SetCalibration(position->position,
287 values.shooter.pusher_distal.lower_angle);
288 } else if (position->pusher_proximal.current) {
289 shooter_.SetCalibration(position->position,
290 values.shooter.pusher_proximal.upper_angle);
291 } else {
292 shooter_.SetCalibration(position->position,
293 values.shooter.upper_limit);
294 }
295
296 // Go to the current position.
297 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
298 // If the plunger is all the way back, we want to be latched.
299 latch_piston_ = position->plunger;
300 brake_piston_ = false;
301 if (position->latch == latch_piston_) {
302 state_ = STATE_REQUEST_LOAD;
303 } else {
304 shooter_loop_disable = true;
305 LOG(DEBUG,
306 "Not moving on until the latch has moved to avoid a crash\n");
307 }
308 } else {
309 // If we can't start yet because we don't know where we are, set the
310 // latch and brake to their defaults.
311 latch_piston_ = true;
312 brake_piston_ = true;
313 }
314 break;
315 case STATE_REQUEST_LOAD:
316 if (position) {
317 zeroed_ = false;
318 if (position->pusher_distal.current ||
319 position->pusher_proximal.current) {
320 // We started on the sensor, back up until we are found.
321 // If the plunger is all the way back and not latched, it won't be
322 // there for long.
323 state_ = STATE_LOAD_BACKTRACK;
324
325 // The plunger is already back and latched. Don't release it.
326 if (position->plunger && position->latch) {
327 latch_piston_ = true;
328 } else {
329 latch_piston_ = false;
330 }
331 } else if (position->plunger && position->latch) {
332 // The plunger is back and we are latched. We most likely got here
333 // from Initialize, in which case we want to 'load' again anyways to
334 // zero.
335 Load();
336 latch_piston_ = true;
337 } else {
338 // Off the sensor, start loading.
339 Load();
340 latch_piston_ = false;
341 }
342 }
343
344 // Hold our current position.
345 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
346 brake_piston_ = false;
347 break;
348 case STATE_LOAD_BACKTRACK:
349 // If we are here, then that means we started past the edge where we want
350 // to zero. Move backwards until we don't see the sensor anymore.
351 // The plunger is contacting the pusher (or will be shortly).
352
353 if (!disabled) {
354 shooter_.SetGoalPosition(
Austin Schuh0e997732015-11-08 15:14:53 -0800355 shooter_.goal_position() + values.shooter.zeroing_speed * kDt,
Brian Silverman17f503e2015-08-02 18:17:18 -0700356 values.shooter.zeroing_speed);
357 }
358 cap_goal = true;
359 shooter_.set_max_voltage(4.0);
360
361 if (position) {
362 if (!position->pusher_distal.current &&
363 !position->pusher_proximal.current) {
364 Load();
365 }
366 latch_piston_ = position->plunger;
367 }
368
369 brake_piston_ = false;
370 break;
371 case STATE_LOAD:
372 // If we are disabled right now, reset the timer.
373 if (disabled) {
374 Load();
375 // Latch defaults to true when disabled. Leave it latched until we have
376 // useful sensor data.
377 latch_piston_ = true;
378 }
379 if (output == nullptr) {
380 load_timeout_ += ::aos::controls::kLoopFrequency;
381 }
382 // Go to 0, which should be the latch position, or trigger a hall effect
383 // on the way. If we don't see edges where we are supposed to, the
384 // offset will be updated by code above.
385 shooter_.SetGoalPosition(0.0, 0.0);
386
387 if (position) {
388 CheckCalibrations(position);
389
390 // Latch if the plunger is far enough back to trigger the hall effect.
391 // This happens when the distal sensor is triggered.
392 latch_piston_ = position->pusher_distal.current || position->plunger;
393
394 // Check if we are latched and back. Make sure the plunger is all the
395 // way back as well.
396 if (position->plunger && position->latch &&
397 position->pusher_distal.current) {
398 if (!zeroed_) {
399 state_ = STATE_REQUEST_LOAD;
400 } else {
401 state_ = STATE_PREPARE_SHOT;
402 }
403 } else if (position->plunger &&
404 ::std::abs(shooter_.absolute_position() -
405 shooter_.goal_position()) < 0.001) {
406 // We are at the goal, but not latched.
407 state_ = STATE_LOADING_PROBLEM;
Austin Schuh214e9c12016-11-25 17:26:20 -0800408 loading_problem_end_time_ =
409 monotonic_clock::now() + kLoadProblemEndTimeout;
Brian Silverman17f503e2015-08-02 18:17:18 -0700410 }
411 }
Austin Schuh214e9c12016-11-25 17:26:20 -0800412 if (load_timeout_ < monotonic_clock::now()) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700413 if (position) {
Austin Schuhadf2cde2015-11-08 20:35:16 -0800414 // If none of the sensors is triggered, estop.
415 // Otherwise, trigger anyways if it has been 0.5 seconds more.
416 if (!(position->pusher_distal.current ||
417 position->pusher_proximal.current) ||
Austin Schuh214e9c12016-11-25 17:26:20 -0800418 (load_timeout_ + chrono::milliseconds(500) <
419 monotonic_clock::now())) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700420 state_ = STATE_ESTOP;
421 LOG(ERROR, "Estopping because took too long to load.\n");
422 }
423 }
424 }
425 brake_piston_ = false;
426 break;
427 case STATE_LOADING_PROBLEM:
428 if (disabled) {
429 state_ = STATE_REQUEST_LOAD;
430 break;
431 }
432 // We got to the goal, but the latch hasn't registered as down. It might
433 // be stuck, or on it's way but not there yet.
Austin Schuh214e9c12016-11-25 17:26:20 -0800434 if (monotonic_clock::now() > loading_problem_end_time_) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700435 // Timeout by unloading.
436 Unload();
437 } else if (position && position->plunger && position->latch) {
438 // If both trigger, we are latched.
439 state_ = STATE_PREPARE_SHOT;
440 }
441 // Move a bit further back to help it trigger.
442 // If the latch is slow due to the air flowing through the tubes or
443 // inertia, but is otherwise free, this won't have much time to do
444 // anything and is safe. Otherwise this gives us a bit more room to free
445 // up the latch.
446 shooter_.SetGoalPosition(values.shooter.lower_limit, 0.0);
447 if (position) {
448 LOG(DEBUG, "Waiting on latch: plunger %d, latch: %d\n",
449 position->plunger, position->latch);
450 }
451
452 latch_piston_ = true;
453 brake_piston_ = false;
454 break;
455 case STATE_PREPARE_SHOT:
456 // Move the shooter to the shot power set point and then lock the brake.
457 // TODO(austin): Timeout. Low priority.
458
459 if (goal) {
460 shooter_.SetGoalPosition(PowerToPosition(goal->shot_power), 0.0);
461 }
462
463 LOG(DEBUG, "PDIFF: absolute_position: %.2f, pow: %.2f\n",
464 shooter_.absolute_position(),
465 goal ? PowerToPosition(goal->shot_power)
466 : ::std::numeric_limits<double>::quiet_NaN());
467 if (goal &&
468 ::std::abs(shooter_.absolute_position() -
469 PowerToPosition(goal->shot_power)) < 0.001 &&
470 ::std::abs(shooter_.absolute_velocity()) < 0.005) {
471 // We are there, set the brake and move on.
472 latch_piston_ = true;
473 brake_piston_ = true;
Austin Schuh214e9c12016-11-25 17:26:20 -0800474 shooter_brake_set_time_ = monotonic_clock::now() + kShooterBrakeSetTime;
Brian Silverman17f503e2015-08-02 18:17:18 -0700475 state_ = STATE_READY;
476 } else {
477 latch_piston_ = true;
478 brake_piston_ = false;
479 }
480 if (goal && goal->unload_requested) {
481 Unload();
482 }
483 break;
484 case STATE_READY:
485 LOG(DEBUG, "In ready\n");
486 // Wait until the brake is set, and a shot is requested or the shot power
487 // is changed.
Austin Schuh214e9c12016-11-25 17:26:20 -0800488 if (monotonic_clock::now() > shooter_brake_set_time_) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700489 status->ready = true;
490 // We have waited long enough for the brake to set, turn the shooter
491 // control loop off.
492 shooter_loop_disable = true;
493 LOG(DEBUG, "Brake is now set\n");
494 if (goal && goal->shot_requested && !disabled) {
495 LOG(DEBUG, "Shooting now\n");
496 shooter_loop_disable = true;
Austin Schuh214e9c12016-11-25 17:26:20 -0800497 shot_end_time_ = monotonic_clock::now() + kShotEndTimeout;
Brian Silverman17f503e2015-08-02 18:17:18 -0700498 firing_starting_position_ = shooter_.absolute_position();
499 state_ = STATE_FIRE;
500 }
501 }
502 if (state_ == STATE_READY && goal &&
503 ::std::abs(shooter_.absolute_position() -
504 PowerToPosition(goal->shot_power)) > 0.002) {
505 // TODO(austin): Add a state to release the brake.
506
507 // TODO(austin): Do we want to set the brake here or after shooting?
508 // Depends on air usage.
509 status->ready = false;
510 LOG(DEBUG, "Preparing shot again.\n");
511 state_ = STATE_PREPARE_SHOT;
512 }
513
514 if (goal) {
515 shooter_.SetGoalPosition(PowerToPosition(goal->shot_power), 0.0);
516 }
517
518 latch_piston_ = true;
519 brake_piston_ = true;
520
521 if (goal && goal->unload_requested) {
522 Unload();
523 }
524 break;
525
526 case STATE_FIRE:
527 if (disabled) {
528 if (position) {
529 if (position->plunger) {
530 // If disabled and the plunger is still back there, reset the
531 // timeout.
Austin Schuh214e9c12016-11-25 17:26:20 -0800532 shot_end_time_ = monotonic_clock::now() + kShotEndTimeout;
Brian Silverman17f503e2015-08-02 18:17:18 -0700533 }
534 }
535 }
536 shooter_loop_disable = true;
537 // Count the number of contiguous cycles during which we haven't moved.
538 if (::std::abs(last_position_.position - shooter_.absolute_position()) <
539 0.0005) {
540 ++cycles_not_moved_;
541 } else {
542 cycles_not_moved_ = 0;
543 }
544
545 // If we have moved any amount since the start and the shooter has now
546 // been still for a couple cycles, the shot finished.
547 // Also move on if it times out.
Austin Schuh829e6ad2015-11-08 14:10:37 -0800548 if (((::std::abs(firing_starting_position_ -
549 shooter_.absolute_position()) > 0.0005 &&
Austin Schuhadf2cde2015-11-08 20:35:16 -0800550 cycles_not_moved_ > 6) ||
Austin Schuh214e9c12016-11-25 17:26:20 -0800551 monotonic_clock::now() > shot_end_time_) &&
Austin Schuh829e6ad2015-11-08 14:10:37 -0800552 ::aos::robot_state->voltage_battery > 10.5) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700553 state_ = STATE_REQUEST_LOAD;
554 ++shot_count_;
555 }
556 latch_piston_ = false;
557 brake_piston_ = true;
558 break;
559 case STATE_UNLOAD:
560 // Reset the timeouts.
561 if (disabled) Unload();
562
563 // If it is latched and the plunger is back, move the pusher back to catch
564 // the plunger.
565 bool all_back;
566 if (position) {
567 all_back = position->plunger && position->latch;
568 } else {
569 all_back = last_position_.plunger && last_position_.latch;
570 }
571
572 if (all_back) {
573 // Pull back to 0, 0.
574 shooter_.SetGoalPosition(0.0, 0.0);
575 if (shooter_.absolute_position() < 0.005) {
576 // When we are close enough, 'fire'.
577 latch_piston_ = false;
578 } else {
579 latch_piston_ = true;
580
581 if (position) {
582 CheckCalibrations(position);
583 }
584 }
585 } else {
586 // The plunger isn't all the way back, or it is and it is unlatched, so
587 // we can now unload.
588 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
589 latch_piston_ = false;
590 state_ = STATE_UNLOAD_MOVE;
Austin Schuh214e9c12016-11-25 17:26:20 -0800591 unload_timeout_ = monotonic_clock::now() + kUnloadTimeout;
Brian Silverman17f503e2015-08-02 18:17:18 -0700592 }
593
Austin Schuh214e9c12016-11-25 17:26:20 -0800594 if (monotonic_clock::now() > unload_timeout_) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700595 // We have been stuck trying to unload for way too long, give up and
596 // turn everything off.
597 state_ = STATE_ESTOP;
598 LOG(ERROR, "Estopping because took too long to unload.\n");
599 }
600
601 brake_piston_ = false;
602 break;
603 case STATE_UNLOAD_MOVE: {
604 if (disabled) {
Austin Schuh214e9c12016-11-25 17:26:20 -0800605 unload_timeout_ = monotonic_clock::now() + kUnloadTimeout;
Brian Silverman17f503e2015-08-02 18:17:18 -0700606 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
607 }
608 cap_goal = true;
609 shooter_.set_max_voltage(6.0);
610
611 // Slowly move back until we hit the upper limit.
612 // If we were at the limit last cycle, we are done unloading.
613 // This is because if we saturate, we might hit the limit before we are
614 // actually there.
615 if (shooter_.goal_position() >= values.shooter.upper_limit) {
616 shooter_.SetGoalPosition(values.shooter.upper_limit, 0.0);
617 // We don't want the loop fighting the spring when we are unloaded.
618 // Turn it off.
619 shooter_loop_disable = true;
620 state_ = STATE_READY_UNLOAD;
621 } else {
622 shooter_.SetGoalPosition(
623 ::std::min(
624 values.shooter.upper_limit,
Austin Schuh0e997732015-11-08 15:14:53 -0800625 shooter_.goal_position() + values.shooter.unload_speed * kDt),
Brian Silverman17f503e2015-08-02 18:17:18 -0700626 values.shooter.unload_speed);
627 }
628
629 latch_piston_ = false;
630 brake_piston_ = false;
631 } break;
632 case STATE_READY_UNLOAD:
633 if (goal && goal->load_requested) {
634 state_ = STATE_REQUEST_LOAD;
635 }
636 // If we are ready to load again,
637 shooter_loop_disable = true;
638
639 latch_piston_ = false;
640 brake_piston_ = false;
641 break;
642
643 case STATE_ESTOP:
644 LOG(WARNING, "estopped\n");
645 // Totally lost, go to a safe state.
646 shooter_loop_disable = true;
647 latch_piston_ = true;
648 brake_piston_ = true;
649 break;
650 }
651
652 if (!shooter_loop_disable) {
653 LOG_STRUCT(DEBUG, "running the loop",
Brian Silvermanb601d892015-12-20 18:24:38 -0500654 ::y2014::control_loops::ShooterStatusToLog(
Austin Schuh24957102015-11-28 16:04:40 -0800655 shooter_.goal_position(), shooter_.absolute_position()));
Brian Silverman17f503e2015-08-02 18:17:18 -0700656 if (!cap_goal) {
657 shooter_.set_max_voltage(12.0);
658 }
659 shooter_.Update(output == NULL);
660 if (cap_goal) {
661 shooter_.CapGoal();
662 }
663 // We don't really want to output anything if we went through everything
664 // assuming the motors weren't working.
665 if (output) output->voltage = shooter_.voltage();
666 } else {
667 shooter_.Update(true);
668 shooter_.ZeroPower();
669 if (output) output->voltage = 0.0;
670 }
671
672 status->hard_stop_power = PositionToPower(shooter_.absolute_position());
673
674 if (output) {
675 output->latch_piston = latch_piston_;
676 output->brake_piston = brake_piston_;
677 }
678
679 if (position) {
680 LOG_STRUCT(DEBUG, "internal state",
Brian Silvermanb601d892015-12-20 18:24:38 -0500681 ::y2014::control_loops::ShooterStateToLog(
Brian Silverman17f503e2015-08-02 18:17:18 -0700682 shooter_.absolute_position(), shooter_.absolute_velocity(),
683 state_, position->latch, position->pusher_proximal.current,
684 position->pusher_distal.current, position->plunger,
685 brake_piston_, latch_piston_));
686
687 last_position_ = *position;
688
689 last_distal_posedge_count_ = position->pusher_distal.posedge_count;
690 last_proximal_posedge_count_ = position->pusher_proximal.posedge_count;
691 last_distal_current_ = position->pusher_distal.current;
692 last_proximal_current_ = position->pusher_proximal.current;
693 }
694
Austin Schuhadf2cde2015-11-08 20:35:16 -0800695 status->absolute_position = shooter_.absolute_position();
696 status->absolute_velocity = shooter_.absolute_velocity();
697 status->state = state_;
698
Brian Silverman17f503e2015-08-02 18:17:18 -0700699 status->shots = shot_count_;
700}
701
702void ShooterMotor::ZeroOutputs() {
703 queue_group()->output.MakeWithBuilder()
704 .voltage(0)
705 .latch_piston(latch_piston_)
706 .brake_piston(brake_piston_)
707 .Send();
708}
709
710} // namespace control_loops
Austin Schuh24957102015-11-28 16:04:40 -0800711} // namespace y2014