blob: 39bf5c08100e86e1f77299d4fba7915857b4b244 [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>
7
8#include "aos/common/controls/control_loops.q.h"
9#include "aos/common/logging/logging.h"
10#include "aos/common/logging/queue_logging.h"
11
12#include "y2014/constants.h"
13#include "y2014/control_loops/shooter/shooter_motor_plant.h"
14
Austin Schuh24957102015-11-28 16:04:40 -080015namespace y2014 {
Brian Silverman17f503e2015-08-02 18:17:18 -070016namespace control_loops {
17
Austin Schuh9d4aca82015-11-08 14:41:31 -080018using ::y2014::control_loops::shooter::kSpringConstant;
19using ::y2014::control_loops::shooter::kMaxExtension;
Austin Schuh0e997732015-11-08 15:14:53 -080020using ::y2014::control_loops::shooter::kDt;
Austin Schuh9d4aca82015-11-08 14:41:31 -080021using ::y2014::control_loops::shooter::MakeShooterLoop;
Brian Silverman17f503e2015-08-02 18:17:18 -070022using ::aos::time::Time;
23
24void ZeroedStateFeedbackLoop::CapU() {
25 const double old_voltage = voltage_;
26 voltage_ += U(0, 0);
27
28 uncapped_voltage_ = voltage_;
29
30 // Make sure that reality and the observer can't get too far off. There is a
31 // delay by one cycle between the applied voltage and X_hat(2, 0), so compare
32 // against last cycle's voltage.
33 if (X_hat(2, 0) > last_voltage_ + 4.0) {
34 voltage_ -= X_hat(2, 0) - (last_voltage_ + 4.0);
35 LOG(DEBUG, "Capping due to runaway\n");
36 } else 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 }
40
41 voltage_ = std::min(max_voltage_, voltage_);
42 voltage_ = std::max(-max_voltage_, voltage_);
43 mutable_U(0, 0) = voltage_ - old_voltage;
44
Austin Schuh24957102015-11-28 16:04:40 -080045 LOG_STRUCT(
46 DEBUG, "shooter_output",
47 ::frc971::control_loops::ShooterVoltageToLog(X_hat(2, 0), voltage_));
Brian Silverman17f503e2015-08-02 18:17:18 -070048
49 last_voltage_ = voltage_;
50 capped_goal_ = false;
51}
52
53void ZeroedStateFeedbackLoop::CapGoal() {
54 if (uncapped_voltage() > max_voltage_) {
55 double dx;
56 if (controller_index() == 0) {
57 dx = (uncapped_voltage() - max_voltage_) /
58 (K(0, 0) - A(1, 0) * K(0, 2) / A(1, 2));
59 mutable_R(0, 0) -= dx;
60 mutable_R(2, 0) -= -A(1, 0) / A(1, 2) * dx;
61 } else {
62 dx = (uncapped_voltage() - max_voltage_) / K(0, 0);
63 mutable_R(0, 0) -= dx;
64 }
65 capped_goal_ = true;
Austin Schuh24957102015-11-28 16:04:40 -080066 LOG_STRUCT(DEBUG, "to prevent windup",
67 ::frc971::control_loops::ShooterMovingGoal(dx));
Brian Silverman17f503e2015-08-02 18:17:18 -070068 } else if (uncapped_voltage() < -max_voltage_) {
69 double dx;
70 if (controller_index() == 0) {
71 dx = (uncapped_voltage() + max_voltage_) /
72 (K(0, 0) - A(1, 0) * K(0, 2) / A(1, 2));
73 mutable_R(0, 0) -= dx;
74 mutable_R(2, 0) -= -A(1, 0) / A(1, 2) * dx;
75 } else {
76 dx = (uncapped_voltage() + max_voltage_) / K(0, 0);
77 mutable_R(0, 0) -= dx;
78 }
79 capped_goal_ = true;
Austin Schuh24957102015-11-28 16:04:40 -080080 LOG_STRUCT(DEBUG, "to prevent windup",
81 ::frc971::control_loops::ShooterMovingGoal(dx));
Brian Silverman17f503e2015-08-02 18:17:18 -070082 } else {
83 capped_goal_ = false;
84 }
85}
86
87void ZeroedStateFeedbackLoop::RecalculatePowerGoal() {
88 if (controller_index() == 0) {
89 mutable_R(2, 0) = (-A(1, 0) / A(1, 2) * R(0, 0) - A(1, 1) / A(1, 2) * R(1, 0));
90 } else {
91 mutable_R(2, 0) = -A(1, 1) / A(1, 2) * R(1, 0);
92 }
93}
94
95void ZeroedStateFeedbackLoop::SetCalibration(double encoder_val,
96 double known_position) {
97 double old_position = absolute_position();
98 double previous_offset = offset_;
99 offset_ = known_position - encoder_val;
100 double doffset = offset_ - previous_offset;
101 mutable_X_hat(0, 0) += doffset;
Brian Silverman17f503e2015-08-02 18:17:18 -0700102 // Offset the goal so we don't move.
103 mutable_R(0, 0) += doffset;
104 if (controller_index() == 0) {
105 mutable_R(2, 0) += -A(1, 0) / A(1, 2) * (doffset);
106 }
Austin Schuh24957102015-11-28 16:04:40 -0800107 LOG_STRUCT(DEBUG, "sensor edge (fake?)",
108 ::frc971::control_loops::ShooterChangeCalibration(
109 encoder_val, known_position, old_position, absolute_position(),
110 previous_offset, offset_));
Brian Silverman17f503e2015-08-02 18:17:18 -0700111}
112
Austin Schuh24957102015-11-28 16:04:40 -0800113ShooterMotor::ShooterMotor(::frc971::control_loops::ShooterQueue *my_shooter)
114 : aos::controls::ControlLoop<::frc971::control_loops::ShooterQueue>(
115 my_shooter),
Brian Silverman17f503e2015-08-02 18:17:18 -0700116 shooter_(MakeShooterLoop()),
117 state_(STATE_INITIALIZE),
118 loading_problem_end_time_(0, 0),
119 load_timeout_(0, 0),
120 shooter_brake_set_time_(0, 0),
121 unload_timeout_(0, 0),
122 shot_end_time_(0, 0),
123 cycles_not_moved_(0),
124 shot_count_(0),
125 zeroed_(false),
126 distal_posedge_validation_cycles_left_(0),
127 proximal_posedge_validation_cycles_left_(0),
128 last_distal_current_(true),
129 last_proximal_current_(true) {}
130
131double ShooterMotor::PowerToPosition(double power) {
Austin Schuh24957102015-11-28 16:04:40 -0800132 const constants::Values &values = constants::GetValues();
Brian Silverman17f503e2015-08-02 18:17:18 -0700133 double maxpower = 0.5 * kSpringConstant *
134 (kMaxExtension * kMaxExtension -
135 (kMaxExtension - values.shooter.upper_limit) *
136 (kMaxExtension - values.shooter.upper_limit));
137 if (power < 0) {
Austin Schuh24957102015-11-28 16:04:40 -0800138 LOG_STRUCT(WARNING, "negative power",
139 ::frc971::control_loops::PowerAdjustment(power, 0));
Brian Silverman17f503e2015-08-02 18:17:18 -0700140 power = 0;
141 } else if (power > maxpower) {
Austin Schuh24957102015-11-28 16:04:40 -0800142 LOG_STRUCT(WARNING, "power too high",
143 ::frc971::control_loops::PowerAdjustment(power, maxpower));
Brian Silverman17f503e2015-08-02 18:17:18 -0700144 power = maxpower;
145 }
146
147 double mp = kMaxExtension * kMaxExtension - (power + power) / kSpringConstant;
148 double new_pos = 0.10;
149 if (mp < 0) {
150 LOG(ERROR,
151 "Power calculation has negative number before square root (%f).\n", mp);
152 } else {
153 new_pos = kMaxExtension - ::std::sqrt(mp);
154 }
155
156 new_pos = ::std::min(::std::max(new_pos, values.shooter.lower_limit),
157 values.shooter.upper_limit);
158 return new_pos;
159}
160
161double ShooterMotor::PositionToPower(double position) {
162 double power = kSpringConstant * position * (kMaxExtension - position / 2.0);
163 return power;
164}
165
166void ShooterMotor::CheckCalibrations(
Austin Schuh24957102015-11-28 16:04:40 -0800167 const ::frc971::control_loops::ShooterQueue::Position *position) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700168 CHECK_NOTNULL(position);
Austin Schuh24957102015-11-28 16:04:40 -0800169 const constants::Values &values = constants::GetValues();
Brian Silverman17f503e2015-08-02 18:17:18 -0700170
171 // TODO(austin): Validate that this is the right edge.
172 // If we see a posedge on any of the hall effects,
173 if (position->pusher_proximal.posedge_count != last_proximal_posedge_count_ &&
174 !last_proximal_current_) {
175 proximal_posedge_validation_cycles_left_ = 2;
176 }
177 if (proximal_posedge_validation_cycles_left_ > 0) {
178 if (position->pusher_proximal.current) {
179 --proximal_posedge_validation_cycles_left_;
180 if (proximal_posedge_validation_cycles_left_ == 0) {
181 shooter_.SetCalibration(
182 position->pusher_proximal.posedge_value,
183 values.shooter.pusher_proximal.upper_angle);
184
185 LOG(DEBUG, "Setting calibration using proximal sensor\n");
186 zeroed_ = true;
187 }
188 } else {
189 proximal_posedge_validation_cycles_left_ = 0;
190 }
191 }
192
193 if (position->pusher_distal.posedge_count != last_distal_posedge_count_ &&
194 !last_distal_current_) {
195 distal_posedge_validation_cycles_left_ = 2;
196 }
197 if (distal_posedge_validation_cycles_left_ > 0) {
198 if (position->pusher_distal.current) {
199 --distal_posedge_validation_cycles_left_;
200 if (distal_posedge_validation_cycles_left_ == 0) {
201 shooter_.SetCalibration(
202 position->pusher_distal.posedge_value,
203 values.shooter.pusher_distal.upper_angle);
204
205 LOG(DEBUG, "Setting calibration using distal sensor\n");
206 zeroed_ = true;
207 }
208 } else {
209 distal_posedge_validation_cycles_left_ = 0;
210 }
211 }
212}
213
214// Positive is out, and positive power is out.
215void ShooterMotor::RunIteration(
Austin Schuh24957102015-11-28 16:04:40 -0800216 const ::frc971::control_loops::ShooterQueue::Goal *goal,
217 const ::frc971::control_loops::ShooterQueue::Position *position,
218 ::frc971::control_loops::ShooterQueue::Output *output,
219 ::frc971::control_loops::ShooterQueue::Status *status) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700220 if (goal && ::std::isnan(goal->shot_power)) {
221 state_ = STATE_ESTOP;
222 LOG(ERROR, "Estopping because got a shot power of NAN.\n");
223 }
224
225 // we must always have these or we have issues.
226 if (status == NULL) {
227 if (output) output->voltage = 0;
228 LOG(ERROR, "Thought I would just check for null and die.\n");
229 return;
230 }
231 status->ready = false;
232
233 if (WasReset()) {
234 state_ = STATE_INITIALIZE;
235 last_distal_current_ = position->pusher_distal.current;
236 last_proximal_current_ = position->pusher_proximal.current;
237 }
238 if (position) {
239 shooter_.CorrectPosition(position->position);
240 }
241
242 // Disable the motors now so that all early returns will return with the
243 // motors disabled.
244 if (output) output->voltage = 0;
245
Austin Schuh24957102015-11-28 16:04:40 -0800246 const constants::Values &values = constants::GetValues();
Brian Silverman17f503e2015-08-02 18:17:18 -0700247
248 // Don't even let the control loops run.
249 bool shooter_loop_disable = false;
250
251 const bool disabled =
252 !::aos::joystick_state.get() || !::aos::joystick_state->enabled;
253
254 // If true, move the goal if we saturate.
255 bool cap_goal = false;
256
257 // TODO(austin): Move the offset if we see or don't see a hall effect when we
258 // expect to see one.
259 // Probably not needed yet.
260
261 if (position) {
262 int last_controller_index = shooter_.controller_index();
263 if (position->plunger && position->latch) {
264 // Use the controller without the spring if the latch is set and the
265 // plunger is back
266 shooter_.set_controller_index(1);
267 } else {
268 // Otherwise use the controller with the spring.
269 shooter_.set_controller_index(0);
270 }
271 if (shooter_.controller_index() != last_controller_index) {
272 shooter_.RecalculatePowerGoal();
273 }
274 }
275
276 switch (state_) {
277 case STATE_INITIALIZE:
278 if (position) {
279 // Reinitialize the internal filter state.
280 shooter_.InitializeState(position->position);
281
282 // Start off with the assumption that we are at the value
283 // futhest back given our sensors.
284 if (position->pusher_distal.current) {
285 shooter_.SetCalibration(position->position,
286 values.shooter.pusher_distal.lower_angle);
287 } else if (position->pusher_proximal.current) {
288 shooter_.SetCalibration(position->position,
289 values.shooter.pusher_proximal.upper_angle);
290 } else {
291 shooter_.SetCalibration(position->position,
292 values.shooter.upper_limit);
293 }
294
295 // Go to the current position.
296 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
297 // If the plunger is all the way back, we want to be latched.
298 latch_piston_ = position->plunger;
299 brake_piston_ = false;
300 if (position->latch == latch_piston_) {
301 state_ = STATE_REQUEST_LOAD;
302 } else {
303 shooter_loop_disable = true;
304 LOG(DEBUG,
305 "Not moving on until the latch has moved to avoid a crash\n");
306 }
307 } else {
308 // If we can't start yet because we don't know where we are, set the
309 // latch and brake to their defaults.
310 latch_piston_ = true;
311 brake_piston_ = true;
312 }
313 break;
314 case STATE_REQUEST_LOAD:
315 if (position) {
316 zeroed_ = false;
317 if (position->pusher_distal.current ||
318 position->pusher_proximal.current) {
319 // We started on the sensor, back up until we are found.
320 // If the plunger is all the way back and not latched, it won't be
321 // there for long.
322 state_ = STATE_LOAD_BACKTRACK;
323
324 // The plunger is already back and latched. Don't release it.
325 if (position->plunger && position->latch) {
326 latch_piston_ = true;
327 } else {
328 latch_piston_ = false;
329 }
330 } else if (position->plunger && position->latch) {
331 // The plunger is back and we are latched. We most likely got here
332 // from Initialize, in which case we want to 'load' again anyways to
333 // zero.
334 Load();
335 latch_piston_ = true;
336 } else {
337 // Off the sensor, start loading.
338 Load();
339 latch_piston_ = false;
340 }
341 }
342
343 // Hold our current position.
344 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
345 brake_piston_ = false;
346 break;
347 case STATE_LOAD_BACKTRACK:
348 // If we are here, then that means we started past the edge where we want
349 // to zero. Move backwards until we don't see the sensor anymore.
350 // The plunger is contacting the pusher (or will be shortly).
351
352 if (!disabled) {
353 shooter_.SetGoalPosition(
Austin Schuh0e997732015-11-08 15:14:53 -0800354 shooter_.goal_position() + values.shooter.zeroing_speed * kDt,
Brian Silverman17f503e2015-08-02 18:17:18 -0700355 values.shooter.zeroing_speed);
356 }
357 cap_goal = true;
358 shooter_.set_max_voltage(4.0);
359
360 if (position) {
361 if (!position->pusher_distal.current &&
362 !position->pusher_proximal.current) {
363 Load();
364 }
365 latch_piston_ = position->plunger;
366 }
367
368 brake_piston_ = false;
369 break;
370 case STATE_LOAD:
371 // If we are disabled right now, reset the timer.
372 if (disabled) {
373 Load();
374 // Latch defaults to true when disabled. Leave it latched until we have
375 // useful sensor data.
376 latch_piston_ = true;
377 }
378 if (output == nullptr) {
379 load_timeout_ += ::aos::controls::kLoopFrequency;
380 }
381 // Go to 0, which should be the latch position, or trigger a hall effect
382 // on the way. If we don't see edges where we are supposed to, the
383 // offset will be updated by code above.
384 shooter_.SetGoalPosition(0.0, 0.0);
385
386 if (position) {
387 CheckCalibrations(position);
388
389 // Latch if the plunger is far enough back to trigger the hall effect.
390 // This happens when the distal sensor is triggered.
391 latch_piston_ = position->pusher_distal.current || position->plunger;
392
393 // Check if we are latched and back. Make sure the plunger is all the
394 // way back as well.
395 if (position->plunger && position->latch &&
396 position->pusher_distal.current) {
397 if (!zeroed_) {
398 state_ = STATE_REQUEST_LOAD;
399 } else {
400 state_ = STATE_PREPARE_SHOT;
401 }
402 } else if (position->plunger &&
403 ::std::abs(shooter_.absolute_position() -
404 shooter_.goal_position()) < 0.001) {
405 // We are at the goal, but not latched.
406 state_ = STATE_LOADING_PROBLEM;
407 loading_problem_end_time_ = Time::Now() + kLoadProblemEndTimeout;
408 }
409 }
410 if (load_timeout_ < Time::Now()) {
411 if (position) {
Austin Schuhadf2cde2015-11-08 20:35:16 -0800412 // If none of the sensors is triggered, estop.
413 // Otherwise, trigger anyways if it has been 0.5 seconds more.
414 if (!(position->pusher_distal.current ||
415 position->pusher_proximal.current) ||
Austin Schuh143bb8f2015-11-08 22:24:34 -0800416 (load_timeout_ + Time::InSeconds(0.5) < Time::Now())) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700417 state_ = STATE_ESTOP;
418 LOG(ERROR, "Estopping because took too long to load.\n");
419 }
420 }
421 }
422 brake_piston_ = false;
423 break;
424 case STATE_LOADING_PROBLEM:
425 if (disabled) {
426 state_ = STATE_REQUEST_LOAD;
427 break;
428 }
429 // We got to the goal, but the latch hasn't registered as down. It might
430 // be stuck, or on it's way but not there yet.
431 if (Time::Now() > loading_problem_end_time_) {
432 // Timeout by unloading.
433 Unload();
434 } else if (position && position->plunger && position->latch) {
435 // If both trigger, we are latched.
436 state_ = STATE_PREPARE_SHOT;
437 }
438 // Move a bit further back to help it trigger.
439 // If the latch is slow due to the air flowing through the tubes or
440 // inertia, but is otherwise free, this won't have much time to do
441 // anything and is safe. Otherwise this gives us a bit more room to free
442 // up the latch.
443 shooter_.SetGoalPosition(values.shooter.lower_limit, 0.0);
444 if (position) {
445 LOG(DEBUG, "Waiting on latch: plunger %d, latch: %d\n",
446 position->plunger, position->latch);
447 }
448
449 latch_piston_ = true;
450 brake_piston_ = false;
451 break;
452 case STATE_PREPARE_SHOT:
453 // Move the shooter to the shot power set point and then lock the brake.
454 // TODO(austin): Timeout. Low priority.
455
456 if (goal) {
457 shooter_.SetGoalPosition(PowerToPosition(goal->shot_power), 0.0);
458 }
459
460 LOG(DEBUG, "PDIFF: absolute_position: %.2f, pow: %.2f\n",
461 shooter_.absolute_position(),
462 goal ? PowerToPosition(goal->shot_power)
463 : ::std::numeric_limits<double>::quiet_NaN());
464 if (goal &&
465 ::std::abs(shooter_.absolute_position() -
466 PowerToPosition(goal->shot_power)) < 0.001 &&
467 ::std::abs(shooter_.absolute_velocity()) < 0.005) {
468 // We are there, set the brake and move on.
469 latch_piston_ = true;
470 brake_piston_ = true;
471 shooter_brake_set_time_ = Time::Now() + kShooterBrakeSetTime;
472 state_ = STATE_READY;
473 } else {
474 latch_piston_ = true;
475 brake_piston_ = false;
476 }
477 if (goal && goal->unload_requested) {
478 Unload();
479 }
480 break;
481 case STATE_READY:
482 LOG(DEBUG, "In ready\n");
483 // Wait until the brake is set, and a shot is requested or the shot power
484 // is changed.
485 if (Time::Now() > shooter_brake_set_time_) {
486 status->ready = true;
487 // We have waited long enough for the brake to set, turn the shooter
488 // control loop off.
489 shooter_loop_disable = true;
490 LOG(DEBUG, "Brake is now set\n");
491 if (goal && goal->shot_requested && !disabled) {
492 LOG(DEBUG, "Shooting now\n");
493 shooter_loop_disable = true;
494 shot_end_time_ = Time::Now() + kShotEndTimeout;
495 firing_starting_position_ = shooter_.absolute_position();
496 state_ = STATE_FIRE;
497 }
498 }
499 if (state_ == STATE_READY && goal &&
500 ::std::abs(shooter_.absolute_position() -
501 PowerToPosition(goal->shot_power)) > 0.002) {
502 // TODO(austin): Add a state to release the brake.
503
504 // TODO(austin): Do we want to set the brake here or after shooting?
505 // Depends on air usage.
506 status->ready = false;
507 LOG(DEBUG, "Preparing shot again.\n");
508 state_ = STATE_PREPARE_SHOT;
509 }
510
511 if (goal) {
512 shooter_.SetGoalPosition(PowerToPosition(goal->shot_power), 0.0);
513 }
514
515 latch_piston_ = true;
516 brake_piston_ = true;
517
518 if (goal && goal->unload_requested) {
519 Unload();
520 }
521 break;
522
523 case STATE_FIRE:
524 if (disabled) {
525 if (position) {
526 if (position->plunger) {
527 // If disabled and the plunger is still back there, reset the
528 // timeout.
529 shot_end_time_ = Time::Now() + kShotEndTimeout;
530 }
531 }
532 }
533 shooter_loop_disable = true;
534 // Count the number of contiguous cycles during which we haven't moved.
535 if (::std::abs(last_position_.position - shooter_.absolute_position()) <
536 0.0005) {
537 ++cycles_not_moved_;
538 } else {
539 cycles_not_moved_ = 0;
540 }
541
542 // If we have moved any amount since the start and the shooter has now
543 // been still for a couple cycles, the shot finished.
544 // Also move on if it times out.
Austin Schuh829e6ad2015-11-08 14:10:37 -0800545 if (((::std::abs(firing_starting_position_ -
546 shooter_.absolute_position()) > 0.0005 &&
Austin Schuhadf2cde2015-11-08 20:35:16 -0800547 cycles_not_moved_ > 6) ||
Austin Schuh829e6ad2015-11-08 14:10:37 -0800548 Time::Now() > shot_end_time_) &&
549 ::aos::robot_state->voltage_battery > 10.5) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700550 state_ = STATE_REQUEST_LOAD;
551 ++shot_count_;
552 }
553 latch_piston_ = false;
554 brake_piston_ = true;
555 break;
556 case STATE_UNLOAD:
557 // Reset the timeouts.
558 if (disabled) Unload();
559
560 // If it is latched and the plunger is back, move the pusher back to catch
561 // the plunger.
562 bool all_back;
563 if (position) {
564 all_back = position->plunger && position->latch;
565 } else {
566 all_back = last_position_.plunger && last_position_.latch;
567 }
568
569 if (all_back) {
570 // Pull back to 0, 0.
571 shooter_.SetGoalPosition(0.0, 0.0);
572 if (shooter_.absolute_position() < 0.005) {
573 // When we are close enough, 'fire'.
574 latch_piston_ = false;
575 } else {
576 latch_piston_ = true;
577
578 if (position) {
579 CheckCalibrations(position);
580 }
581 }
582 } else {
583 // The plunger isn't all the way back, or it is and it is unlatched, so
584 // we can now unload.
585 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
586 latch_piston_ = false;
587 state_ = STATE_UNLOAD_MOVE;
588 unload_timeout_ = Time::Now() + kUnloadTimeout;
589 }
590
591 if (Time::Now() > unload_timeout_) {
592 // We have been stuck trying to unload for way too long, give up and
593 // turn everything off.
594 state_ = STATE_ESTOP;
595 LOG(ERROR, "Estopping because took too long to unload.\n");
596 }
597
598 brake_piston_ = false;
599 break;
600 case STATE_UNLOAD_MOVE: {
601 if (disabled) {
602 unload_timeout_ = Time::Now() + kUnloadTimeout;
603 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
604 }
605 cap_goal = true;
606 shooter_.set_max_voltage(6.0);
607
608 // Slowly move back until we hit the upper limit.
609 // If we were at the limit last cycle, we are done unloading.
610 // This is because if we saturate, we might hit the limit before we are
611 // actually there.
612 if (shooter_.goal_position() >= values.shooter.upper_limit) {
613 shooter_.SetGoalPosition(values.shooter.upper_limit, 0.0);
614 // We don't want the loop fighting the spring when we are unloaded.
615 // Turn it off.
616 shooter_loop_disable = true;
617 state_ = STATE_READY_UNLOAD;
618 } else {
619 shooter_.SetGoalPosition(
620 ::std::min(
621 values.shooter.upper_limit,
Austin Schuh0e997732015-11-08 15:14:53 -0800622 shooter_.goal_position() + values.shooter.unload_speed * kDt),
Brian Silverman17f503e2015-08-02 18:17:18 -0700623 values.shooter.unload_speed);
624 }
625
626 latch_piston_ = false;
627 brake_piston_ = false;
628 } break;
629 case STATE_READY_UNLOAD:
630 if (goal && goal->load_requested) {
631 state_ = STATE_REQUEST_LOAD;
632 }
633 // If we are ready to load again,
634 shooter_loop_disable = true;
635
636 latch_piston_ = false;
637 brake_piston_ = false;
638 break;
639
640 case STATE_ESTOP:
641 LOG(WARNING, "estopped\n");
642 // Totally lost, go to a safe state.
643 shooter_loop_disable = true;
644 latch_piston_ = true;
645 brake_piston_ = true;
646 break;
647 }
648
649 if (!shooter_loop_disable) {
650 LOG_STRUCT(DEBUG, "running the loop",
Austin Schuh24957102015-11-28 16:04:40 -0800651 ::frc971::control_loops::ShooterStatusToLog(
652 shooter_.goal_position(), shooter_.absolute_position()));
Brian Silverman17f503e2015-08-02 18:17:18 -0700653 if (!cap_goal) {
654 shooter_.set_max_voltage(12.0);
655 }
656 shooter_.Update(output == NULL);
657 if (cap_goal) {
658 shooter_.CapGoal();
659 }
660 // We don't really want to output anything if we went through everything
661 // assuming the motors weren't working.
662 if (output) output->voltage = shooter_.voltage();
663 } else {
664 shooter_.Update(true);
665 shooter_.ZeroPower();
666 if (output) output->voltage = 0.0;
667 }
668
669 status->hard_stop_power = PositionToPower(shooter_.absolute_position());
670
671 if (output) {
672 output->latch_piston = latch_piston_;
673 output->brake_piston = brake_piston_;
674 }
675
676 if (position) {
677 LOG_STRUCT(DEBUG, "internal state",
Austin Schuh24957102015-11-28 16:04:40 -0800678 ::frc971::control_loops::ShooterStateToLog(
Brian Silverman17f503e2015-08-02 18:17:18 -0700679 shooter_.absolute_position(), shooter_.absolute_velocity(),
680 state_, position->latch, position->pusher_proximal.current,
681 position->pusher_distal.current, position->plunger,
682 brake_piston_, latch_piston_));
683
684 last_position_ = *position;
685
686 last_distal_posedge_count_ = position->pusher_distal.posedge_count;
687 last_proximal_posedge_count_ = position->pusher_proximal.posedge_count;
688 last_distal_current_ = position->pusher_distal.current;
689 last_proximal_current_ = position->pusher_proximal.current;
690 }
691
Austin Schuhadf2cde2015-11-08 20:35:16 -0800692 status->absolute_position = shooter_.absolute_position();
693 status->absolute_velocity = shooter_.absolute_velocity();
694 status->state = state_;
695
Brian Silverman17f503e2015-08-02 18:17:18 -0700696 status->shots = shot_count_;
697}
698
699void ShooterMotor::ZeroOutputs() {
700 queue_group()->output.MakeWithBuilder()
701 .voltage(0)
702 .latch_piston(latch_piston_)
703 .brake_piston(brake_piston_)
704 .Send();
705}
706
707} // namespace control_loops
Austin Schuh24957102015-11-28 16:04:40 -0800708} // namespace y2014