blob: d1ef2a51a07710b1e8a5e7ec13f38107b8f45b32 [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
Austin Schuh55a13dc2019-01-27 22:39:03 -0800119ShooterMotor::ShooterMotor(::aos::EventLoop *event_loop,
120 const ::std::string &name)
Brian Silvermanb601d892015-12-20 18:24:38 -0500121 : aos::controls::ControlLoop<::y2014::control_loops::ShooterQueue>(
Austin Schuh55a13dc2019-01-27 22:39:03 -0800122 event_loop, name),
Brian Silverman17f503e2015-08-02 18:17:18 -0700123 shooter_(MakeShooterLoop()),
124 state_(STATE_INITIALIZE),
Brian Silverman17f503e2015-08-02 18:17:18 -0700125 cycles_not_moved_(0),
126 shot_count_(0),
127 zeroed_(false),
128 distal_posedge_validation_cycles_left_(0),
129 proximal_posedge_validation_cycles_left_(0),
130 last_distal_current_(true),
131 last_proximal_current_(true) {}
132
133double ShooterMotor::PowerToPosition(double power) {
Austin Schuh24957102015-11-28 16:04:40 -0800134 const constants::Values &values = constants::GetValues();
Brian Silverman17f503e2015-08-02 18:17:18 -0700135 double maxpower = 0.5 * kSpringConstant *
136 (kMaxExtension * kMaxExtension -
137 (kMaxExtension - values.shooter.upper_limit) *
138 (kMaxExtension - values.shooter.upper_limit));
139 if (power < 0) {
Austin Schuh24957102015-11-28 16:04:40 -0800140 LOG_STRUCT(WARNING, "negative power",
Brian Silvermanb601d892015-12-20 18:24:38 -0500141 ::y2014::control_loops::PowerAdjustment(power, 0));
Brian Silverman17f503e2015-08-02 18:17:18 -0700142 power = 0;
143 } else if (power > maxpower) {
Austin Schuh24957102015-11-28 16:04:40 -0800144 LOG_STRUCT(WARNING, "power too high",
Brian Silvermanb601d892015-12-20 18:24:38 -0500145 ::y2014::control_loops::PowerAdjustment(power, maxpower));
Brian Silverman17f503e2015-08-02 18:17:18 -0700146 power = maxpower;
147 }
148
149 double mp = kMaxExtension * kMaxExtension - (power + power) / kSpringConstant;
150 double new_pos = 0.10;
151 if (mp < 0) {
152 LOG(ERROR,
153 "Power calculation has negative number before square root (%f).\n", mp);
154 } else {
155 new_pos = kMaxExtension - ::std::sqrt(mp);
156 }
157
158 new_pos = ::std::min(::std::max(new_pos, values.shooter.lower_limit),
159 values.shooter.upper_limit);
160 return new_pos;
161}
162
163double ShooterMotor::PositionToPower(double position) {
164 double power = kSpringConstant * position * (kMaxExtension - position / 2.0);
165 return power;
166}
167
168void ShooterMotor::CheckCalibrations(
Brian Silvermanb601d892015-12-20 18:24:38 -0500169 const ::y2014::control_loops::ShooterQueue::Position *position) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700170 CHECK_NOTNULL(position);
Austin Schuh24957102015-11-28 16:04:40 -0800171 const constants::Values &values = constants::GetValues();
Brian Silverman17f503e2015-08-02 18:17:18 -0700172
173 // TODO(austin): Validate that this is the right edge.
174 // If we see a posedge on any of the hall effects,
175 if (position->pusher_proximal.posedge_count != last_proximal_posedge_count_ &&
176 !last_proximal_current_) {
177 proximal_posedge_validation_cycles_left_ = 2;
178 }
179 if (proximal_posedge_validation_cycles_left_ > 0) {
180 if (position->pusher_proximal.current) {
181 --proximal_posedge_validation_cycles_left_;
182 if (proximal_posedge_validation_cycles_left_ == 0) {
183 shooter_.SetCalibration(
184 position->pusher_proximal.posedge_value,
185 values.shooter.pusher_proximal.upper_angle);
186
187 LOG(DEBUG, "Setting calibration using proximal sensor\n");
188 zeroed_ = true;
189 }
190 } else {
191 proximal_posedge_validation_cycles_left_ = 0;
192 }
193 }
194
195 if (position->pusher_distal.posedge_count != last_distal_posedge_count_ &&
196 !last_distal_current_) {
197 distal_posedge_validation_cycles_left_ = 2;
198 }
199 if (distal_posedge_validation_cycles_left_ > 0) {
200 if (position->pusher_distal.current) {
201 --distal_posedge_validation_cycles_left_;
202 if (distal_posedge_validation_cycles_left_ == 0) {
203 shooter_.SetCalibration(
204 position->pusher_distal.posedge_value,
205 values.shooter.pusher_distal.upper_angle);
206
207 LOG(DEBUG, "Setting calibration using distal sensor\n");
208 zeroed_ = true;
209 }
210 } else {
211 distal_posedge_validation_cycles_left_ = 0;
212 }
213 }
214}
215
216// Positive is out, and positive power is out.
217void ShooterMotor::RunIteration(
Brian Silvermanb601d892015-12-20 18:24:38 -0500218 const ::y2014::control_loops::ShooterQueue::Goal *goal,
219 const ::y2014::control_loops::ShooterQueue::Position *position,
220 ::y2014::control_loops::ShooterQueue::Output *output,
221 ::y2014::control_loops::ShooterQueue::Status *status) {
Austin Schuh9fe68f72019-08-10 19:32:03 -0700222 const monotonic_clock::time_point monotonic_now = event_loop()->monotonic_now();
Brian Silverman17f503e2015-08-02 18:17:18 -0700223 if (goal && ::std::isnan(goal->shot_power)) {
224 state_ = STATE_ESTOP;
225 LOG(ERROR, "Estopping because got a shot power of NAN.\n");
226 }
227
228 // we must always have these or we have issues.
229 if (status == NULL) {
230 if (output) output->voltage = 0;
231 LOG(ERROR, "Thought I would just check for null and die.\n");
232 return;
233 }
234 status->ready = false;
235
236 if (WasReset()) {
237 state_ = STATE_INITIALIZE;
238 last_distal_current_ = position->pusher_distal.current;
239 last_proximal_current_ = position->pusher_proximal.current;
240 }
241 if (position) {
242 shooter_.CorrectPosition(position->position);
243 }
244
245 // Disable the motors now so that all early returns will return with the
246 // motors disabled.
247 if (output) output->voltage = 0;
248
Austin Schuh24957102015-11-28 16:04:40 -0800249 const constants::Values &values = constants::GetValues();
Brian Silverman17f503e2015-08-02 18:17:18 -0700250
251 // Don't even let the control loops run.
252 bool shooter_loop_disable = false;
253
Austin Schuheeec74a2019-01-27 20:58:59 -0800254 const bool disabled = !has_joystick_state() || !joystick_state().enabled;
Brian Silverman17f503e2015-08-02 18:17:18 -0700255
256 // If true, move the goal if we saturate.
257 bool cap_goal = false;
258
259 // TODO(austin): Move the offset if we see or don't see a hall effect when we
260 // expect to see one.
261 // Probably not needed yet.
262
263 if (position) {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800264 int last_index = shooter_.index();
Brian Silverman17f503e2015-08-02 18:17:18 -0700265 if (position->plunger && position->latch) {
266 // Use the controller without the spring if the latch is set and the
267 // plunger is back
Austin Schuhc5fceb82017-02-25 16:24:12 -0800268 shooter_.set_index(1);
Brian Silverman17f503e2015-08-02 18:17:18 -0700269 } else {
270 // Otherwise use the controller with the spring.
Austin Schuhc5fceb82017-02-25 16:24:12 -0800271 shooter_.set_index(0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700272 }
Austin Schuhc5fceb82017-02-25 16:24:12 -0800273 if (shooter_.index() != last_index) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700274 shooter_.RecalculatePowerGoal();
275 }
276 }
277
278 switch (state_) {
279 case STATE_INITIALIZE:
280 if (position) {
281 // Reinitialize the internal filter state.
282 shooter_.InitializeState(position->position);
283
284 // Start off with the assumption that we are at the value
285 // futhest back given our sensors.
286 if (position->pusher_distal.current) {
287 shooter_.SetCalibration(position->position,
288 values.shooter.pusher_distal.lower_angle);
289 } else if (position->pusher_proximal.current) {
290 shooter_.SetCalibration(position->position,
291 values.shooter.pusher_proximal.upper_angle);
292 } else {
293 shooter_.SetCalibration(position->position,
294 values.shooter.upper_limit);
295 }
296
297 // Go to the current position.
298 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
299 // If the plunger is all the way back, we want to be latched.
300 latch_piston_ = position->plunger;
301 brake_piston_ = false;
302 if (position->latch == latch_piston_) {
303 state_ = STATE_REQUEST_LOAD;
304 } else {
305 shooter_loop_disable = true;
306 LOG(DEBUG,
307 "Not moving on until the latch has moved to avoid a crash\n");
308 }
309 } else {
310 // If we can't start yet because we don't know where we are, set the
311 // latch and brake to their defaults.
312 latch_piston_ = true;
313 brake_piston_ = true;
314 }
315 break;
316 case STATE_REQUEST_LOAD:
317 if (position) {
318 zeroed_ = false;
319 if (position->pusher_distal.current ||
320 position->pusher_proximal.current) {
321 // We started on the sensor, back up until we are found.
322 // If the plunger is all the way back and not latched, it won't be
323 // there for long.
324 state_ = STATE_LOAD_BACKTRACK;
325
326 // The plunger is already back and latched. Don't release it.
327 if (position->plunger && position->latch) {
328 latch_piston_ = true;
329 } else {
330 latch_piston_ = false;
331 }
332 } else if (position->plunger && position->latch) {
333 // The plunger is back and we are latched. We most likely got here
334 // from Initialize, in which case we want to 'load' again anyways to
335 // zero.
Austin Schuh9fe68f72019-08-10 19:32:03 -0700336 Load(monotonic_now);
Brian Silverman17f503e2015-08-02 18:17:18 -0700337 latch_piston_ = true;
338 } else {
339 // Off the sensor, start loading.
Austin Schuh9fe68f72019-08-10 19:32:03 -0700340 Load(monotonic_now);
Brian Silverman17f503e2015-08-02 18:17:18 -0700341 latch_piston_ = false;
342 }
343 }
344
345 // Hold our current position.
346 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
347 brake_piston_ = false;
348 break;
349 case STATE_LOAD_BACKTRACK:
350 // If we are here, then that means we started past the edge where we want
351 // to zero. Move backwards until we don't see the sensor anymore.
352 // The plunger is contacting the pusher (or will be shortly).
353
354 if (!disabled) {
355 shooter_.SetGoalPosition(
Austin Schuh0e997732015-11-08 15:14:53 -0800356 shooter_.goal_position() + values.shooter.zeroing_speed * kDt,
Brian Silverman17f503e2015-08-02 18:17:18 -0700357 values.shooter.zeroing_speed);
358 }
359 cap_goal = true;
360 shooter_.set_max_voltage(4.0);
361
362 if (position) {
363 if (!position->pusher_distal.current &&
364 !position->pusher_proximal.current) {
Austin Schuh9fe68f72019-08-10 19:32:03 -0700365 Load(monotonic_now);
Brian Silverman17f503e2015-08-02 18:17:18 -0700366 }
367 latch_piston_ = position->plunger;
368 }
369
370 brake_piston_ = false;
371 break;
372 case STATE_LOAD:
373 // If we are disabled right now, reset the timer.
374 if (disabled) {
Austin Schuh9fe68f72019-08-10 19:32:03 -0700375 Load(monotonic_now);
Brian Silverman17f503e2015-08-02 18:17:18 -0700376 // Latch defaults to true when disabled. Leave it latched until we have
377 // useful sensor data.
378 latch_piston_ = true;
379 }
380 if (output == nullptr) {
381 load_timeout_ += ::aos::controls::kLoopFrequency;
382 }
383 // Go to 0, which should be the latch position, or trigger a hall effect
384 // on the way. If we don't see edges where we are supposed to, the
385 // offset will be updated by code above.
386 shooter_.SetGoalPosition(0.0, 0.0);
387
388 if (position) {
389 CheckCalibrations(position);
390
391 // Latch if the plunger is far enough back to trigger the hall effect.
392 // This happens when the distal sensor is triggered.
393 latch_piston_ = position->pusher_distal.current || position->plunger;
394
395 // Check if we are latched and back. Make sure the plunger is all the
396 // way back as well.
397 if (position->plunger && position->latch &&
398 position->pusher_distal.current) {
399 if (!zeroed_) {
400 state_ = STATE_REQUEST_LOAD;
401 } else {
402 state_ = STATE_PREPARE_SHOT;
403 }
404 } else if (position->plunger &&
405 ::std::abs(shooter_.absolute_position() -
406 shooter_.goal_position()) < 0.001) {
407 // We are at the goal, but not latched.
408 state_ = STATE_LOADING_PROBLEM;
Austin Schuh214e9c12016-11-25 17:26:20 -0800409 loading_problem_end_time_ =
Austin Schuh9fe68f72019-08-10 19:32:03 -0700410 monotonic_now + kLoadProblemEndTimeout;
Brian Silverman17f503e2015-08-02 18:17:18 -0700411 }
412 }
Austin Schuh9fe68f72019-08-10 19:32:03 -0700413 if (load_timeout_ < monotonic_now) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700414 if (position) {
Austin Schuhadf2cde2015-11-08 20:35:16 -0800415 // If none of the sensors is triggered, estop.
416 // Otherwise, trigger anyways if it has been 0.5 seconds more.
417 if (!(position->pusher_distal.current ||
418 position->pusher_proximal.current) ||
Austin Schuh214e9c12016-11-25 17:26:20 -0800419 (load_timeout_ + chrono::milliseconds(500) <
Austin Schuh9fe68f72019-08-10 19:32:03 -0700420 monotonic_now)) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700421 state_ = STATE_ESTOP;
422 LOG(ERROR, "Estopping because took too long to load.\n");
423 }
424 }
425 }
426 brake_piston_ = false;
427 break;
428 case STATE_LOADING_PROBLEM:
429 if (disabled) {
430 state_ = STATE_REQUEST_LOAD;
431 break;
432 }
433 // We got to the goal, but the latch hasn't registered as down. It might
434 // be stuck, or on it's way but not there yet.
Austin Schuh9fe68f72019-08-10 19:32:03 -0700435 if (monotonic_now > loading_problem_end_time_) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700436 // Timeout by unloading.
Austin Schuh9fe68f72019-08-10 19:32:03 -0700437 Unload(monotonic_now);
Brian Silverman17f503e2015-08-02 18:17:18 -0700438 } else if (position && position->plunger && position->latch) {
439 // If both trigger, we are latched.
440 state_ = STATE_PREPARE_SHOT;
441 }
442 // Move a bit further back to help it trigger.
443 // If the latch is slow due to the air flowing through the tubes or
444 // inertia, but is otherwise free, this won't have much time to do
445 // anything and is safe. Otherwise this gives us a bit more room to free
446 // up the latch.
447 shooter_.SetGoalPosition(values.shooter.lower_limit, 0.0);
448 if (position) {
449 LOG(DEBUG, "Waiting on latch: plunger %d, latch: %d\n",
450 position->plunger, position->latch);
451 }
452
453 latch_piston_ = true;
454 brake_piston_ = false;
455 break;
456 case STATE_PREPARE_SHOT:
457 // Move the shooter to the shot power set point and then lock the brake.
458 // TODO(austin): Timeout. Low priority.
459
460 if (goal) {
461 shooter_.SetGoalPosition(PowerToPosition(goal->shot_power), 0.0);
462 }
463
464 LOG(DEBUG, "PDIFF: absolute_position: %.2f, pow: %.2f\n",
465 shooter_.absolute_position(),
466 goal ? PowerToPosition(goal->shot_power)
467 : ::std::numeric_limits<double>::quiet_NaN());
468 if (goal &&
469 ::std::abs(shooter_.absolute_position() -
470 PowerToPosition(goal->shot_power)) < 0.001 &&
471 ::std::abs(shooter_.absolute_velocity()) < 0.005) {
472 // We are there, set the brake and move on.
473 latch_piston_ = true;
474 brake_piston_ = true;
Austin Schuh9fe68f72019-08-10 19:32:03 -0700475 shooter_brake_set_time_ = monotonic_now + kShooterBrakeSetTime;
Brian Silverman17f503e2015-08-02 18:17:18 -0700476 state_ = STATE_READY;
477 } else {
478 latch_piston_ = true;
479 brake_piston_ = false;
480 }
481 if (goal && goal->unload_requested) {
Austin Schuh9fe68f72019-08-10 19:32:03 -0700482 Unload(monotonic_now);
Brian Silverman17f503e2015-08-02 18:17:18 -0700483 }
484 break;
485 case STATE_READY:
486 LOG(DEBUG, "In ready\n");
487 // Wait until the brake is set, and a shot is requested or the shot power
488 // is changed.
Austin Schuh9fe68f72019-08-10 19:32:03 -0700489 if (monotonic_now > shooter_brake_set_time_) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700490 status->ready = true;
491 // We have waited long enough for the brake to set, turn the shooter
492 // control loop off.
493 shooter_loop_disable = true;
494 LOG(DEBUG, "Brake is now set\n");
495 if (goal && goal->shot_requested && !disabled) {
496 LOG(DEBUG, "Shooting now\n");
497 shooter_loop_disable = true;
Austin Schuh9fe68f72019-08-10 19:32:03 -0700498 shot_end_time_ = monotonic_now + kShotEndTimeout;
Brian Silverman17f503e2015-08-02 18:17:18 -0700499 firing_starting_position_ = shooter_.absolute_position();
500 state_ = STATE_FIRE;
501 }
502 }
503 if (state_ == STATE_READY && goal &&
504 ::std::abs(shooter_.absolute_position() -
505 PowerToPosition(goal->shot_power)) > 0.002) {
506 // TODO(austin): Add a state to release the brake.
507
508 // TODO(austin): Do we want to set the brake here or after shooting?
509 // Depends on air usage.
510 status->ready = false;
511 LOG(DEBUG, "Preparing shot again.\n");
512 state_ = STATE_PREPARE_SHOT;
513 }
514
515 if (goal) {
516 shooter_.SetGoalPosition(PowerToPosition(goal->shot_power), 0.0);
517 }
518
519 latch_piston_ = true;
520 brake_piston_ = true;
521
522 if (goal && goal->unload_requested) {
Austin Schuh9fe68f72019-08-10 19:32:03 -0700523 Unload(monotonic_now);
Brian Silverman17f503e2015-08-02 18:17:18 -0700524 }
525 break;
526
527 case STATE_FIRE:
528 if (disabled) {
529 if (position) {
530 if (position->plunger) {
531 // If disabled and the plunger is still back there, reset the
532 // timeout.
Austin Schuh9fe68f72019-08-10 19:32:03 -0700533 shot_end_time_ = monotonic_now + kShotEndTimeout;
Brian Silverman17f503e2015-08-02 18:17:18 -0700534 }
535 }
536 }
537 shooter_loop_disable = true;
538 // Count the number of contiguous cycles during which we haven't moved.
539 if (::std::abs(last_position_.position - shooter_.absolute_position()) <
540 0.0005) {
541 ++cycles_not_moved_;
542 } else {
543 cycles_not_moved_ = 0;
544 }
545
546 // If we have moved any amount since the start and the shooter has now
547 // been still for a couple cycles, the shot finished.
548 // Also move on if it times out.
Austin Schuh829e6ad2015-11-08 14:10:37 -0800549 if (((::std::abs(firing_starting_position_ -
550 shooter_.absolute_position()) > 0.0005 &&
Austin Schuhadf2cde2015-11-08 20:35:16 -0800551 cycles_not_moved_ > 6) ||
Austin Schuh9fe68f72019-08-10 19:32:03 -0700552 monotonic_now > shot_end_time_) &&
Austin Schuheeec74a2019-01-27 20:58:59 -0800553 robot_state().voltage_battery > 10.5) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700554 state_ = STATE_REQUEST_LOAD;
555 ++shot_count_;
556 }
557 latch_piston_ = false;
558 brake_piston_ = true;
559 break;
560 case STATE_UNLOAD:
561 // Reset the timeouts.
Austin Schuh9fe68f72019-08-10 19:32:03 -0700562 if (disabled) Unload(monotonic_now);
Brian Silverman17f503e2015-08-02 18:17:18 -0700563
564 // If it is latched and the plunger is back, move the pusher back to catch
565 // the plunger.
566 bool all_back;
567 if (position) {
568 all_back = position->plunger && position->latch;
569 } else {
570 all_back = last_position_.plunger && last_position_.latch;
571 }
572
573 if (all_back) {
574 // Pull back to 0, 0.
575 shooter_.SetGoalPosition(0.0, 0.0);
576 if (shooter_.absolute_position() < 0.005) {
577 // When we are close enough, 'fire'.
578 latch_piston_ = false;
579 } else {
580 latch_piston_ = true;
581
582 if (position) {
583 CheckCalibrations(position);
584 }
585 }
586 } else {
587 // The plunger isn't all the way back, or it is and it is unlatched, so
588 // we can now unload.
589 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
590 latch_piston_ = false;
591 state_ = STATE_UNLOAD_MOVE;
Austin Schuh9fe68f72019-08-10 19:32:03 -0700592 unload_timeout_ = monotonic_now + kUnloadTimeout;
Brian Silverman17f503e2015-08-02 18:17:18 -0700593 }
594
Austin Schuh9fe68f72019-08-10 19:32:03 -0700595 if (monotonic_now > unload_timeout_) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700596 // We have been stuck trying to unload for way too long, give up and
597 // turn everything off.
598 state_ = STATE_ESTOP;
599 LOG(ERROR, "Estopping because took too long to unload.\n");
600 }
601
602 brake_piston_ = false;
603 break;
604 case STATE_UNLOAD_MOVE: {
605 if (disabled) {
Austin Schuh9fe68f72019-08-10 19:32:03 -0700606 unload_timeout_ = monotonic_now + kUnloadTimeout;
Brian Silverman17f503e2015-08-02 18:17:18 -0700607 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
608 }
609 cap_goal = true;
610 shooter_.set_max_voltage(6.0);
611
612 // Slowly move back until we hit the upper limit.
613 // If we were at the limit last cycle, we are done unloading.
614 // This is because if we saturate, we might hit the limit before we are
615 // actually there.
616 if (shooter_.goal_position() >= values.shooter.upper_limit) {
617 shooter_.SetGoalPosition(values.shooter.upper_limit, 0.0);
618 // We don't want the loop fighting the spring when we are unloaded.
619 // Turn it off.
620 shooter_loop_disable = true;
621 state_ = STATE_READY_UNLOAD;
622 } else {
623 shooter_.SetGoalPosition(
624 ::std::min(
625 values.shooter.upper_limit,
Austin Schuh0e997732015-11-08 15:14:53 -0800626 shooter_.goal_position() + values.shooter.unload_speed * kDt),
Brian Silverman17f503e2015-08-02 18:17:18 -0700627 values.shooter.unload_speed);
628 }
629
630 latch_piston_ = false;
631 brake_piston_ = false;
632 } break;
633 case STATE_READY_UNLOAD:
634 if (goal && goal->load_requested) {
635 state_ = STATE_REQUEST_LOAD;
636 }
637 // If we are ready to load again,
638 shooter_loop_disable = true;
639
640 latch_piston_ = false;
641 brake_piston_ = false;
642 break;
643
644 case STATE_ESTOP:
645 LOG(WARNING, "estopped\n");
646 // Totally lost, go to a safe state.
647 shooter_loop_disable = true;
648 latch_piston_ = true;
649 brake_piston_ = true;
650 break;
651 }
652
653 if (!shooter_loop_disable) {
654 LOG_STRUCT(DEBUG, "running the loop",
Brian Silvermanb601d892015-12-20 18:24:38 -0500655 ::y2014::control_loops::ShooterStatusToLog(
Austin Schuh24957102015-11-28 16:04:40 -0800656 shooter_.goal_position(), shooter_.absolute_position()));
Brian Silverman17f503e2015-08-02 18:17:18 -0700657 if (!cap_goal) {
658 shooter_.set_max_voltage(12.0);
659 }
660 shooter_.Update(output == NULL);
661 if (cap_goal) {
662 shooter_.CapGoal();
663 }
664 // We don't really want to output anything if we went through everything
665 // assuming the motors weren't working.
666 if (output) output->voltage = shooter_.voltage();
667 } else {
668 shooter_.Update(true);
669 shooter_.ZeroPower();
670 if (output) output->voltage = 0.0;
671 }
672
673 status->hard_stop_power = PositionToPower(shooter_.absolute_position());
674
675 if (output) {
676 output->latch_piston = latch_piston_;
677 output->brake_piston = brake_piston_;
678 }
679
680 if (position) {
681 LOG_STRUCT(DEBUG, "internal state",
Brian Silvermanb601d892015-12-20 18:24:38 -0500682 ::y2014::control_loops::ShooterStateToLog(
Brian Silverman17f503e2015-08-02 18:17:18 -0700683 shooter_.absolute_position(), shooter_.absolute_velocity(),
684 state_, position->latch, position->pusher_proximal.current,
685 position->pusher_distal.current, position->plunger,
686 brake_piston_, latch_piston_));
687
688 last_position_ = *position;
689
690 last_distal_posedge_count_ = position->pusher_distal.posedge_count;
691 last_proximal_posedge_count_ = position->pusher_proximal.posedge_count;
692 last_distal_current_ = position->pusher_distal.current;
693 last_proximal_current_ = position->pusher_proximal.current;
694 }
695
Austin Schuhadf2cde2015-11-08 20:35:16 -0800696 status->absolute_position = shooter_.absolute_position();
697 status->absolute_velocity = shooter_.absolute_velocity();
698 status->state = state_;
699
Brian Silverman17f503e2015-08-02 18:17:18 -0700700 status->shots = shot_count_;
701}
702
Austin Schuhaebfb4f2019-01-27 13:26:38 -0800703void ShooterMotor::Zero(::y2014::control_loops::ShooterQueue::Output *output) {
704 output->voltage = 0.0;
705 output->latch_piston = latch_piston_;
706 output->brake_piston = brake_piston_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700707}
708
709} // namespace control_loops
Austin Schuh24957102015-11-28 16:04:40 -0800710} // namespace y2014