blob: fb115c7018b606c8f51ef7c98b2ddc3dfc0fd774 [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/logging/logging.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070010#include "y2014/constants.h"
11#include "y2014/control_loops/shooter/shooter_motor_plant.h"
12
Austin Schuh24957102015-11-28 16:04:40 -080013namespace y2014 {
Brian Silverman17f503e2015-08-02 18:17:18 -070014namespace control_loops {
Alex Perrycb7da4b2019-08-28 19:35:56 -070015namespace shooter {
Brian Silverman17f503e2015-08-02 18:17:18 -070016
Austin Schuh214e9c12016-11-25 17:26:20 -080017namespace chrono = ::std::chrono;
18using ::aos::monotonic_clock;
19
Austin Schuh9d4aca82015-11-08 14:41:31 -080020using ::y2014::control_loops::shooter::kSpringConstant;
21using ::y2014::control_loops::shooter::kMaxExtension;
Austin Schuh0e997732015-11-08 15:14:53 -080022using ::y2014::control_loops::shooter::kDt;
Austin Schuh9d4aca82015-11-08 14:41:31 -080023using ::y2014::control_loops::shooter::MakeShooterLoop;
Brian Silverman17f503e2015-08-02 18:17:18 -070024
25void ZeroedStateFeedbackLoop::CapU() {
26 const double old_voltage = voltage_;
27 voltage_ += U(0, 0);
28
29 uncapped_voltage_ = voltage_;
30
31 // Make sure that reality and the observer can't get too far off. There is a
32 // delay by one cycle between the applied voltage and X_hat(2, 0), so compare
33 // against last cycle's voltage.
34 if (X_hat(2, 0) > last_voltage_ + 4.0) {
35 voltage_ -= X_hat(2, 0) - (last_voltage_ + 4.0);
Austin Schuhf257f3c2019-10-27 21:00:43 -070036 AOS_LOG(DEBUG, "Capping due to runaway\n");
Brian Silverman17f503e2015-08-02 18:17:18 -070037 } else if (X_hat(2, 0) < last_voltage_ - 4.0) {
38 voltage_ += X_hat(2, 0) - (last_voltage_ - 4.0);
Austin Schuhf257f3c2019-10-27 21:00:43 -070039 AOS_LOG(DEBUG, "Capping due to runaway\n");
Brian Silverman17f503e2015-08-02 18:17:18 -070040 }
41
42 voltage_ = std::min(max_voltage_, voltage_);
43 voltage_ = std::max(-max_voltage_, voltage_);
44 mutable_U(0, 0) = voltage_ - old_voltage;
45
Brian Silverman17f503e2015-08-02 18:17:18 -070046 last_voltage_ = voltage_;
47 capped_goal_ = false;
48}
49
50void ZeroedStateFeedbackLoop::CapGoal() {
51 if (uncapped_voltage() > max_voltage_) {
52 double dx;
Austin Schuhc5fceb82017-02-25 16:24:12 -080053 if (index() == 0) {
Brian Silverman17f503e2015-08-02 18:17:18 -070054 dx = (uncapped_voltage() - max_voltage_) /
Austin Schuh32501832017-02-25 18:32:56 -080055 (controller().K(0, 0) -
56 plant().A(1, 0) * controller().K(0, 2) / plant().A(1, 2));
Brian Silverman17f503e2015-08-02 18:17:18 -070057 mutable_R(0, 0) -= dx;
Austin Schuhc5fceb82017-02-25 16:24:12 -080058 mutable_R(2, 0) -= -plant().A(1, 0) / plant().A(1, 2) * dx;
Brian Silverman17f503e2015-08-02 18:17:18 -070059 } else {
Austin Schuh32501832017-02-25 18:32:56 -080060 dx = (uncapped_voltage() - max_voltage_) / controller().K(0, 0);
Brian Silverman17f503e2015-08-02 18:17:18 -070061 mutable_R(0, 0) -= dx;
62 }
63 capped_goal_ = true;
Brian Silverman17f503e2015-08-02 18:17:18 -070064 } else if (uncapped_voltage() < -max_voltage_) {
65 double dx;
Austin Schuhc5fceb82017-02-25 16:24:12 -080066 if (index() == 0) {
Brian Silverman17f503e2015-08-02 18:17:18 -070067 dx = (uncapped_voltage() + max_voltage_) /
Austin Schuh32501832017-02-25 18:32:56 -080068 (controller().K(0, 0) -
69 plant().A(1, 0) * controller().K(0, 2) / plant().A(1, 2));
Brian Silverman17f503e2015-08-02 18:17:18 -070070 mutable_R(0, 0) -= dx;
Austin Schuhc5fceb82017-02-25 16:24:12 -080071 mutable_R(2, 0) -= -plant().A(1, 0) / plant().A(1, 2) * dx;
Brian Silverman17f503e2015-08-02 18:17:18 -070072 } else {
Austin Schuh32501832017-02-25 18:32:56 -080073 dx = (uncapped_voltage() + max_voltage_) / controller().K(0, 0);
Brian Silverman17f503e2015-08-02 18:17:18 -070074 mutable_R(0, 0) -= dx;
75 }
76 capped_goal_ = true;
Brian Silverman17f503e2015-08-02 18:17:18 -070077 } else {
78 capped_goal_ = false;
79 }
80}
81
82void ZeroedStateFeedbackLoop::RecalculatePowerGoal() {
Austin Schuhc5fceb82017-02-25 16:24:12 -080083 if (index() == 0) {
84 mutable_R(2, 0) = (-plant().A(1, 0) / plant().A(1, 2) * R(0, 0) -
85 plant().A(1, 1) / plant().A(1, 2) * R(1, 0));
Brian Silverman17f503e2015-08-02 18:17:18 -070086 } else {
Austin Schuhc5fceb82017-02-25 16:24:12 -080087 mutable_R(2, 0) = -plant().A(1, 1) / plant().A(1, 2) * R(1, 0);
Brian Silverman17f503e2015-08-02 18:17:18 -070088 }
89}
90
91void ZeroedStateFeedbackLoop::SetCalibration(double encoder_val,
92 double known_position) {
Brian Silverman17f503e2015-08-02 18:17:18 -070093 double previous_offset = offset_;
94 offset_ = known_position - encoder_val;
95 double doffset = offset_ - previous_offset;
96 mutable_X_hat(0, 0) += doffset;
Brian Silverman17f503e2015-08-02 18:17:18 -070097 // Offset the goal so we don't move.
98 mutable_R(0, 0) += doffset;
Austin Schuhc5fceb82017-02-25 16:24:12 -080099 if (index() == 0) {
100 mutable_R(2, 0) += -plant().A(1, 0) / plant().A(1, 2) * (doffset);
Brian Silverman17f503e2015-08-02 18:17:18 -0700101 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700102}
103
Austin Schuh55a13dc2019-01-27 22:39:03 -0800104ShooterMotor::ShooterMotor(::aos::EventLoop *event_loop,
105 const ::std::string &name)
Alex Perrycb7da4b2019-08-28 19:35:56 -0700106 : aos::controls::ControlLoop<Goal, Position, Status, Output>(event_loop,
107 name),
Brian Silverman17f503e2015-08-02 18:17:18 -0700108 shooter_(MakeShooterLoop()),
109 state_(STATE_INITIALIZE),
Brian Silverman17f503e2015-08-02 18:17:18 -0700110 cycles_not_moved_(0),
111 shot_count_(0),
112 zeroed_(false),
113 distal_posedge_validation_cycles_left_(0),
114 proximal_posedge_validation_cycles_left_(0),
115 last_distal_current_(true),
116 last_proximal_current_(true) {}
117
118double ShooterMotor::PowerToPosition(double power) {
Austin Schuh24957102015-11-28 16:04:40 -0800119 const constants::Values &values = constants::GetValues();
Brian Silverman17f503e2015-08-02 18:17:18 -0700120 double maxpower = 0.5 * kSpringConstant *
121 (kMaxExtension * kMaxExtension -
122 (kMaxExtension - values.shooter.upper_limit) *
123 (kMaxExtension - values.shooter.upper_limit));
124 if (power < 0) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700125 power = 0;
126 } else if (power > maxpower) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700127 power = maxpower;
128 }
129
130 double mp = kMaxExtension * kMaxExtension - (power + power) / kSpringConstant;
131 double new_pos = 0.10;
132 if (mp < 0) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700133 AOS_LOG(ERROR,
134 "Power calculation has negative number before square root (%f).\n",
135 mp);
Brian Silverman17f503e2015-08-02 18:17:18 -0700136 } else {
137 new_pos = kMaxExtension - ::std::sqrt(mp);
138 }
139
140 new_pos = ::std::min(::std::max(new_pos, values.shooter.lower_limit),
141 values.shooter.upper_limit);
142 return new_pos;
143}
144
145double ShooterMotor::PositionToPower(double position) {
146 double power = kSpringConstant * position * (kMaxExtension - position / 2.0);
147 return power;
148}
149
Alex Perrycb7da4b2019-08-28 19:35:56 -0700150void ShooterMotor::CheckCalibrations(const Position *position) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700151 AOS_CHECK_NOTNULL(position);
Austin Schuh24957102015-11-28 16:04:40 -0800152 const constants::Values &values = constants::GetValues();
Brian Silverman17f503e2015-08-02 18:17:18 -0700153
154 // TODO(austin): Validate that this is the right edge.
155 // If we see a posedge on any of the hall effects,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700156 if (position->pusher_proximal()->posedge_count() !=
157 last_proximal_posedge_count_ &&
Brian Silverman17f503e2015-08-02 18:17:18 -0700158 !last_proximal_current_) {
159 proximal_posedge_validation_cycles_left_ = 2;
160 }
161 if (proximal_posedge_validation_cycles_left_ > 0) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700162 if (position->pusher_proximal()->current()) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700163 --proximal_posedge_validation_cycles_left_;
164 if (proximal_posedge_validation_cycles_left_ == 0) {
165 shooter_.SetCalibration(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700166 position->pusher_proximal()->posedge_value(),
Brian Silverman17f503e2015-08-02 18:17:18 -0700167 values.shooter.pusher_proximal.upper_angle);
168
Austin Schuhf257f3c2019-10-27 21:00:43 -0700169 AOS_LOG(DEBUG, "Setting calibration using proximal sensor\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700170 zeroed_ = true;
171 }
172 } else {
173 proximal_posedge_validation_cycles_left_ = 0;
174 }
175 }
176
Alex Perrycb7da4b2019-08-28 19:35:56 -0700177 if (position->pusher_distal()->posedge_count() !=
178 last_distal_posedge_count_ &&
Brian Silverman17f503e2015-08-02 18:17:18 -0700179 !last_distal_current_) {
180 distal_posedge_validation_cycles_left_ = 2;
181 }
182 if (distal_posedge_validation_cycles_left_ > 0) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700183 if (position->pusher_distal()->current()) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700184 --distal_posedge_validation_cycles_left_;
185 if (distal_posedge_validation_cycles_left_ == 0) {
186 shooter_.SetCalibration(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700187 position->pusher_distal()->posedge_value(),
Brian Silverman17f503e2015-08-02 18:17:18 -0700188 values.shooter.pusher_distal.upper_angle);
189
Austin Schuhf257f3c2019-10-27 21:00:43 -0700190 AOS_LOG(DEBUG, "Setting calibration using distal sensor\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700191 zeroed_ = true;
192 }
193 } else {
194 distal_posedge_validation_cycles_left_ = 0;
195 }
196 }
197}
198
199// Positive is out, and positive power is out.
200void ShooterMotor::RunIteration(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700201 const Goal *goal,
202 const Position *position,
203 aos::Sender<Output>::Builder *output,
204 aos::Sender<Status>::Builder *status) {
205 OutputT output_struct;
Austin Schuh9fe68f72019-08-10 19:32:03 -0700206 const monotonic_clock::time_point monotonic_now = event_loop()->monotonic_now();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700207 if (goal && ::std::isnan(goal->shot_power())) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700208 state_ = STATE_ESTOP;
209 AOS_LOG(ERROR, "Estopping because got a shot power of NAN.\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700210 }
211
212 // we must always have these or we have issues.
213 if (status == NULL) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700214 if (output) output_struct.voltage = 0;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700215 AOS_LOG(ERROR, "Thought I would just check for null and die.\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700216 return;
217 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700218 bool status_ready = false;
Brian Silverman17f503e2015-08-02 18:17:18 -0700219
220 if (WasReset()) {
221 state_ = STATE_INITIALIZE;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700222 last_distal_current_ = position->pusher_distal()->current();
223 last_proximal_current_ = position->pusher_proximal()->current();
Brian Silverman17f503e2015-08-02 18:17:18 -0700224 }
225 if (position) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700226 shooter_.CorrectPosition(position->position());
Brian Silverman17f503e2015-08-02 18:17:18 -0700227 }
228
229 // Disable the motors now so that all early returns will return with the
230 // motors disabled.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700231 if (output) output_struct.voltage = 0;
Brian Silverman17f503e2015-08-02 18:17:18 -0700232
Austin Schuh24957102015-11-28 16:04:40 -0800233 const constants::Values &values = constants::GetValues();
Brian Silverman17f503e2015-08-02 18:17:18 -0700234
235 // Don't even let the control loops run.
236 bool shooter_loop_disable = false;
237
Alex Perrycb7da4b2019-08-28 19:35:56 -0700238 const bool disabled = !has_joystick_state() || !joystick_state().enabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700239
240 // If true, move the goal if we saturate.
241 bool cap_goal = false;
242
243 // TODO(austin): Move the offset if we see or don't see a hall effect when we
244 // expect to see one.
245 // Probably not needed yet.
246
247 if (position) {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800248 int last_index = shooter_.index();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700249 if (position->plunger() && position->latch()) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700250 // Use the controller without the spring if the latch is set and the
251 // plunger is back
Austin Schuhc5fceb82017-02-25 16:24:12 -0800252 shooter_.set_index(1);
Brian Silverman17f503e2015-08-02 18:17:18 -0700253 } else {
254 // Otherwise use the controller with the spring.
Austin Schuhc5fceb82017-02-25 16:24:12 -0800255 shooter_.set_index(0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700256 }
Austin Schuhc5fceb82017-02-25 16:24:12 -0800257 if (shooter_.index() != last_index) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700258 shooter_.RecalculatePowerGoal();
259 }
260 }
261
262 switch (state_) {
263 case STATE_INITIALIZE:
264 if (position) {
265 // Reinitialize the internal filter state.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700266 shooter_.InitializeState(position->position());
Brian Silverman17f503e2015-08-02 18:17:18 -0700267
268 // Start off with the assumption that we are at the value
269 // futhest back given our sensors.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700270 if (position->pusher_distal()->current()) {
271 shooter_.SetCalibration(position->position(),
Brian Silverman17f503e2015-08-02 18:17:18 -0700272 values.shooter.pusher_distal.lower_angle);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700273 } else if (position->pusher_proximal()->current()) {
274 shooter_.SetCalibration(position->position(),
Brian Silverman17f503e2015-08-02 18:17:18 -0700275 values.shooter.pusher_proximal.upper_angle);
276 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700277 shooter_.SetCalibration(position->position(),
Brian Silverman17f503e2015-08-02 18:17:18 -0700278 values.shooter.upper_limit);
279 }
280
281 // Go to the current position.
282 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
283 // If the plunger is all the way back, we want to be latched.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700284 latch_piston_ = position->plunger();
Brian Silverman17f503e2015-08-02 18:17:18 -0700285 brake_piston_ = false;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700286 if (position->latch() == latch_piston_) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700287 state_ = STATE_REQUEST_LOAD;
288 } else {
289 shooter_loop_disable = true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700290 AOS_LOG(DEBUG,
291 "Not moving on until the latch has moved to avoid a crash\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700292 }
293 } else {
294 // If we can't start yet because we don't know where we are, set the
295 // latch and brake to their defaults.
296 latch_piston_ = true;
297 brake_piston_ = true;
298 }
299 break;
300 case STATE_REQUEST_LOAD:
301 if (position) {
302 zeroed_ = false;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700303 if (position->pusher_distal()->current() ||
304 position->pusher_proximal()->current()) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700305 // We started on the sensor, back up until we are found.
306 // If the plunger is all the way back and not latched, it won't be
307 // there for long.
308 state_ = STATE_LOAD_BACKTRACK;
309
310 // The plunger is already back and latched. Don't release it.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700311 if (position->plunger() && position->latch()) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700312 latch_piston_ = true;
313 } else {
314 latch_piston_ = false;
315 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700316 } else if (position->plunger() && position->latch()) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700317 // The plunger is back and we are latched. We most likely got here
318 // from Initialize, in which case we want to 'load' again anyways to
319 // zero.
Austin Schuh9fe68f72019-08-10 19:32:03 -0700320 Load(monotonic_now);
Brian Silverman17f503e2015-08-02 18:17:18 -0700321 latch_piston_ = true;
322 } else {
323 // Off the sensor, start loading.
Austin Schuh9fe68f72019-08-10 19:32:03 -0700324 Load(monotonic_now);
Brian Silverman17f503e2015-08-02 18:17:18 -0700325 latch_piston_ = false;
326 }
327 }
328
329 // Hold our current position.
330 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
331 brake_piston_ = false;
332 break;
333 case STATE_LOAD_BACKTRACK:
334 // If we are here, then that means we started past the edge where we want
335 // to zero. Move backwards until we don't see the sensor anymore.
336 // The plunger is contacting the pusher (or will be shortly).
337
338 if (!disabled) {
339 shooter_.SetGoalPosition(
Austin Schuh0e997732015-11-08 15:14:53 -0800340 shooter_.goal_position() + values.shooter.zeroing_speed * kDt,
Brian Silverman17f503e2015-08-02 18:17:18 -0700341 values.shooter.zeroing_speed);
342 }
343 cap_goal = true;
344 shooter_.set_max_voltage(4.0);
345
346 if (position) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700347 if (!position->pusher_distal()->current() &&
348 !position->pusher_proximal()->current()) {
Austin Schuh9fe68f72019-08-10 19:32:03 -0700349 Load(monotonic_now);
Brian Silverman17f503e2015-08-02 18:17:18 -0700350 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700351 latch_piston_ = position->plunger();
Brian Silverman17f503e2015-08-02 18:17:18 -0700352 }
353
354 brake_piston_ = false;
355 break;
356 case STATE_LOAD:
357 // If we are disabled right now, reset the timer.
358 if (disabled) {
Austin Schuh9fe68f72019-08-10 19:32:03 -0700359 Load(monotonic_now);
Brian Silverman17f503e2015-08-02 18:17:18 -0700360 // Latch defaults to true when disabled. Leave it latched until we have
361 // useful sensor data.
362 latch_piston_ = true;
363 }
364 if (output == nullptr) {
365 load_timeout_ += ::aos::controls::kLoopFrequency;
366 }
367 // Go to 0, which should be the latch position, or trigger a hall effect
368 // on the way. If we don't see edges where we are supposed to, the
369 // offset will be updated by code above.
370 shooter_.SetGoalPosition(0.0, 0.0);
371
372 if (position) {
373 CheckCalibrations(position);
374
375 // Latch if the plunger is far enough back to trigger the hall effect.
376 // This happens when the distal sensor is triggered.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700377 latch_piston_ =
378 position->pusher_distal()->current() || position->plunger();
Brian Silverman17f503e2015-08-02 18:17:18 -0700379
380 // Check if we are latched and back. Make sure the plunger is all the
381 // way back as well.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700382 if (position->plunger() && position->latch() &&
383 position->pusher_distal()->current()) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700384 if (!zeroed_) {
385 state_ = STATE_REQUEST_LOAD;
386 } else {
387 state_ = STATE_PREPARE_SHOT;
388 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700389 } else if (position->plunger() &&
Brian Silverman17f503e2015-08-02 18:17:18 -0700390 ::std::abs(shooter_.absolute_position() -
391 shooter_.goal_position()) < 0.001) {
392 // We are at the goal, but not latched.
393 state_ = STATE_LOADING_PROBLEM;
Austin Schuh214e9c12016-11-25 17:26:20 -0800394 loading_problem_end_time_ =
Austin Schuh9fe68f72019-08-10 19:32:03 -0700395 monotonic_now + kLoadProblemEndTimeout;
Brian Silverman17f503e2015-08-02 18:17:18 -0700396 }
397 }
Austin Schuh9fe68f72019-08-10 19:32:03 -0700398 if (load_timeout_ < monotonic_now) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700399 if (position) {
Austin Schuhadf2cde2015-11-08 20:35:16 -0800400 // If none of the sensors is triggered, estop.
401 // Otherwise, trigger anyways if it has been 0.5 seconds more.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700402 if (!(position->pusher_distal()->current() ||
403 position->pusher_proximal()->current()) ||
404 (load_timeout_ + chrono::milliseconds(500) < monotonic_now)) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700405 state_ = STATE_ESTOP;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700406 AOS_LOG(ERROR, "Estopping because took too long to load.\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700407 }
408 }
409 }
410 brake_piston_ = false;
411 break;
412 case STATE_LOADING_PROBLEM:
413 if (disabled) {
414 state_ = STATE_REQUEST_LOAD;
415 break;
416 }
417 // We got to the goal, but the latch hasn't registered as down. It might
418 // be stuck, or on it's way but not there yet.
Austin Schuh9fe68f72019-08-10 19:32:03 -0700419 if (monotonic_now > loading_problem_end_time_) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700420 // Timeout by unloading.
Austin Schuh9fe68f72019-08-10 19:32:03 -0700421 Unload(monotonic_now);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700422 } else if (position && position->plunger() && position->latch()) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700423 // If both trigger, we are latched.
424 state_ = STATE_PREPARE_SHOT;
425 }
426 // Move a bit further back to help it trigger.
427 // If the latch is slow due to the air flowing through the tubes or
428 // inertia, but is otherwise free, this won't have much time to do
429 // anything and is safe. Otherwise this gives us a bit more room to free
430 // up the latch.
431 shooter_.SetGoalPosition(values.shooter.lower_limit, 0.0);
432 if (position) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700433 AOS_LOG(DEBUG, "Waiting on latch: plunger %d, latch: %d\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -0700434 position->plunger(), position->latch());
Brian Silverman17f503e2015-08-02 18:17:18 -0700435 }
436
437 latch_piston_ = true;
438 brake_piston_ = false;
439 break;
440 case STATE_PREPARE_SHOT:
441 // Move the shooter to the shot power set point and then lock the brake.
442 // TODO(austin): Timeout. Low priority.
443
444 if (goal) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700445 shooter_.SetGoalPosition(PowerToPosition(goal->shot_power()), 0.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700446 }
447
Austin Schuhf257f3c2019-10-27 21:00:43 -0700448 AOS_LOG(DEBUG, "PDIFF: absolute_position: %.2f, pow: %.2f\n",
449 shooter_.absolute_position(),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700450 goal ? PowerToPosition(goal->shot_power())
Austin Schuhf257f3c2019-10-27 21:00:43 -0700451 : ::std::numeric_limits<double>::quiet_NaN());
Brian Silverman17f503e2015-08-02 18:17:18 -0700452 if (goal &&
453 ::std::abs(shooter_.absolute_position() -
Alex Perrycb7da4b2019-08-28 19:35:56 -0700454 PowerToPosition(goal->shot_power())) < 0.001 &&
Brian Silverman17f503e2015-08-02 18:17:18 -0700455 ::std::abs(shooter_.absolute_velocity()) < 0.005) {
456 // We are there, set the brake and move on.
457 latch_piston_ = true;
458 brake_piston_ = true;
Austin Schuh9fe68f72019-08-10 19:32:03 -0700459 shooter_brake_set_time_ = monotonic_now + kShooterBrakeSetTime;
Brian Silverman17f503e2015-08-02 18:17:18 -0700460 state_ = STATE_READY;
461 } else {
462 latch_piston_ = true;
463 brake_piston_ = false;
464 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700465 if (goal && goal->unload_requested()) {
Austin Schuh9fe68f72019-08-10 19:32:03 -0700466 Unload(monotonic_now);
Brian Silverman17f503e2015-08-02 18:17:18 -0700467 }
468 break;
469 case STATE_READY:
Austin Schuhf257f3c2019-10-27 21:00:43 -0700470 AOS_LOG(DEBUG, "In ready\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700471 // Wait until the brake is set, and a shot is requested or the shot power
472 // is changed.
Austin Schuh9fe68f72019-08-10 19:32:03 -0700473 if (monotonic_now > shooter_brake_set_time_) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700474 status_ready = true;
Brian Silverman17f503e2015-08-02 18:17:18 -0700475 // We have waited long enough for the brake to set, turn the shooter
476 // control loop off.
477 shooter_loop_disable = true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700478 AOS_LOG(DEBUG, "Brake is now set\n");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700479 if (goal && goal->shot_requested() && !disabled) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700480 AOS_LOG(DEBUG, "Shooting now\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700481 shooter_loop_disable = true;
Austin Schuh9fe68f72019-08-10 19:32:03 -0700482 shot_end_time_ = monotonic_now + kShotEndTimeout;
Brian Silverman17f503e2015-08-02 18:17:18 -0700483 firing_starting_position_ = shooter_.absolute_position();
484 state_ = STATE_FIRE;
485 }
486 }
487 if (state_ == STATE_READY && goal &&
488 ::std::abs(shooter_.absolute_position() -
Alex Perrycb7da4b2019-08-28 19:35:56 -0700489 PowerToPosition(goal->shot_power())) > 0.002) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700490 // TODO(austin): Add a state to release the brake.
491
492 // TODO(austin): Do we want to set the brake here or after shooting?
493 // Depends on air usage.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700494 status_ready = false;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700495 AOS_LOG(DEBUG, "Preparing shot again.\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700496 state_ = STATE_PREPARE_SHOT;
497 }
498
499 if (goal) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700500 shooter_.SetGoalPosition(PowerToPosition(goal->shot_power()), 0.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700501 }
502
503 latch_piston_ = true;
504 brake_piston_ = true;
505
Alex Perrycb7da4b2019-08-28 19:35:56 -0700506 if (goal && goal->unload_requested()) {
Austin Schuh9fe68f72019-08-10 19:32:03 -0700507 Unload(monotonic_now);
Brian Silverman17f503e2015-08-02 18:17:18 -0700508 }
509 break;
510
511 case STATE_FIRE:
512 if (disabled) {
513 if (position) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700514 if (position->plunger()) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700515 // If disabled and the plunger is still back there, reset the
516 // timeout.
Austin Schuh9fe68f72019-08-10 19:32:03 -0700517 shot_end_time_ = monotonic_now + kShotEndTimeout;
Brian Silverman17f503e2015-08-02 18:17:18 -0700518 }
519 }
520 }
521 shooter_loop_disable = true;
522 // Count the number of contiguous cycles during which we haven't moved.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700523 if (::std::abs(last_position_position_ - shooter_.absolute_position()) <
Brian Silverman17f503e2015-08-02 18:17:18 -0700524 0.0005) {
525 ++cycles_not_moved_;
526 } else {
527 cycles_not_moved_ = 0;
528 }
529
530 // If we have moved any amount since the start and the shooter has now
531 // been still for a couple cycles, the shot finished.
532 // Also move on if it times out.
Austin Schuh829e6ad2015-11-08 14:10:37 -0800533 if (((::std::abs(firing_starting_position_ -
534 shooter_.absolute_position()) > 0.0005 &&
Austin Schuhadf2cde2015-11-08 20:35:16 -0800535 cycles_not_moved_ > 6) ||
Austin Schuh9fe68f72019-08-10 19:32:03 -0700536 monotonic_now > shot_end_time_) &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700537 robot_state().voltage_battery() > 10.5) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700538 state_ = STATE_REQUEST_LOAD;
539 ++shot_count_;
540 }
541 latch_piston_ = false;
542 brake_piston_ = true;
543 break;
544 case STATE_UNLOAD:
545 // Reset the timeouts.
Austin Schuh9fe68f72019-08-10 19:32:03 -0700546 if (disabled) Unload(monotonic_now);
Brian Silverman17f503e2015-08-02 18:17:18 -0700547
548 // If it is latched and the plunger is back, move the pusher back to catch
549 // the plunger.
550 bool all_back;
551 if (position) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700552 all_back = position->plunger() && position->latch();
Brian Silverman17f503e2015-08-02 18:17:18 -0700553 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700554 all_back = last_position_plunger_ && last_position_latch_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700555 }
556
557 if (all_back) {
558 // Pull back to 0, 0.
559 shooter_.SetGoalPosition(0.0, 0.0);
560 if (shooter_.absolute_position() < 0.005) {
561 // When we are close enough, 'fire'.
562 latch_piston_ = false;
563 } else {
564 latch_piston_ = true;
565
566 if (position) {
567 CheckCalibrations(position);
568 }
569 }
570 } else {
571 // The plunger isn't all the way back, or it is and it is unlatched, so
572 // we can now unload.
573 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
574 latch_piston_ = false;
575 state_ = STATE_UNLOAD_MOVE;
Austin Schuh9fe68f72019-08-10 19:32:03 -0700576 unload_timeout_ = monotonic_now + kUnloadTimeout;
Brian Silverman17f503e2015-08-02 18:17:18 -0700577 }
578
Austin Schuh9fe68f72019-08-10 19:32:03 -0700579 if (monotonic_now > unload_timeout_) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700580 // We have been stuck trying to unload for way too long, give up and
581 // turn everything off.
582 state_ = STATE_ESTOP;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700583 AOS_LOG(ERROR, "Estopping because took too long to unload.\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700584 }
585
586 brake_piston_ = false;
587 break;
588 case STATE_UNLOAD_MOVE: {
589 if (disabled) {
Austin Schuh9fe68f72019-08-10 19:32:03 -0700590 unload_timeout_ = monotonic_now + kUnloadTimeout;
Brian Silverman17f503e2015-08-02 18:17:18 -0700591 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
592 }
593 cap_goal = true;
594 shooter_.set_max_voltage(6.0);
595
596 // Slowly move back until we hit the upper limit.
597 // If we were at the limit last cycle, we are done unloading.
598 // This is because if we saturate, we might hit the limit before we are
599 // actually there.
600 if (shooter_.goal_position() >= values.shooter.upper_limit) {
601 shooter_.SetGoalPosition(values.shooter.upper_limit, 0.0);
602 // We don't want the loop fighting the spring when we are unloaded.
603 // Turn it off.
604 shooter_loop_disable = true;
605 state_ = STATE_READY_UNLOAD;
606 } else {
607 shooter_.SetGoalPosition(
608 ::std::min(
609 values.shooter.upper_limit,
Austin Schuh0e997732015-11-08 15:14:53 -0800610 shooter_.goal_position() + values.shooter.unload_speed * kDt),
Brian Silverman17f503e2015-08-02 18:17:18 -0700611 values.shooter.unload_speed);
612 }
613
614 latch_piston_ = false;
615 brake_piston_ = false;
616 } break;
617 case STATE_READY_UNLOAD:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700618 if (goal && goal->load_requested()) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700619 state_ = STATE_REQUEST_LOAD;
620 }
621 // If we are ready to load again,
622 shooter_loop_disable = true;
623
624 latch_piston_ = false;
625 brake_piston_ = false;
626 break;
627
628 case STATE_ESTOP:
Austin Schuhf257f3c2019-10-27 21:00:43 -0700629 AOS_LOG(WARNING, "estopped\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700630 // Totally lost, go to a safe state.
631 shooter_loop_disable = true;
632 latch_piston_ = true;
633 brake_piston_ = true;
634 break;
635 }
636
637 if (!shooter_loop_disable) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700638 if (!cap_goal) {
639 shooter_.set_max_voltage(12.0);
640 }
641 shooter_.Update(output == NULL);
642 if (cap_goal) {
643 shooter_.CapGoal();
644 }
645 // We don't really want to output anything if we went through everything
646 // assuming the motors weren't working.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700647 if (output) output_struct.voltage = shooter_.voltage();
Brian Silverman17f503e2015-08-02 18:17:18 -0700648 } else {
649 shooter_.Update(true);
650 shooter_.ZeroPower();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700651 if (output) output_struct.voltage = 0.0;
Brian Silverman17f503e2015-08-02 18:17:18 -0700652 }
653
Alex Perrycb7da4b2019-08-28 19:35:56 -0700654 Status::Builder status_builder = status->MakeBuilder<Status>();
655
656 status_builder.add_ready(status_ready);
657 status_builder.add_hard_stop_power(
658 PositionToPower(shooter_.absolute_position()));
Brian Silverman17f503e2015-08-02 18:17:18 -0700659
660 if (output) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700661 output_struct.latch_piston = latch_piston_;
662 output_struct.brake_piston = brake_piston_;
663
664 output->Send(Output::Pack(*output->fbb(), &output_struct));
Brian Silverman17f503e2015-08-02 18:17:18 -0700665 }
666
667 if (position) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700668 last_position_latch_ = position->latch();
669 last_position_plunger_ = position->plunger();
670 last_position_position_ = position->position();
Brian Silverman17f503e2015-08-02 18:17:18 -0700671
Alex Perrycb7da4b2019-08-28 19:35:56 -0700672 last_distal_posedge_count_ = position->pusher_distal()->posedge_count();
673 last_proximal_posedge_count_ = position->pusher_proximal()->posedge_count();
674 last_distal_current_ = position->pusher_distal()->current();
675 last_proximal_current_ = position->pusher_proximal()->current();
Brian Silverman17f503e2015-08-02 18:17:18 -0700676 }
677
Alex Perrycb7da4b2019-08-28 19:35:56 -0700678 status_builder.add_absolute_position(shooter_.absolute_position());
679 status_builder.add_absolute_velocity(shooter_.absolute_velocity());
680 status_builder.add_state(state_);
Austin Schuhadf2cde2015-11-08 20:35:16 -0800681
Alex Perrycb7da4b2019-08-28 19:35:56 -0700682 status_builder.add_shots(shot_count_);
683
684 status->Send(status_builder.Finish());
Brian Silverman17f503e2015-08-02 18:17:18 -0700685}
686
Alex Perrycb7da4b2019-08-28 19:35:56 -0700687flatbuffers::Offset<Output> ShooterMotor::Zero(
688 aos::Sender<Output>::Builder *output) {
689 Output::Builder output_builder = output->MakeBuilder<Output>();
690 output_builder.add_voltage(0.0);
691 output_builder.add_latch_piston(latch_piston_);
692 output_builder.add_brake_piston(brake_piston_);
693 return output_builder.Finish();
Brian Silverman17f503e2015-08-02 18:17:18 -0700694}
695
Alex Perrycb7da4b2019-08-28 19:35:56 -0700696} // namespace shooter
Brian Silverman17f503e2015-08-02 18:17:18 -0700697} // namespace control_loops
Austin Schuh24957102015-11-28 16:04:40 -0800698} // namespace y2014