blob: 4789e1dd77df952832468d8cad72b2a9df6cde74 [file] [log] [blame]
joe93778a62014-02-15 13:22:14 -08001#include "frc971/control_loops/shooter/shooter.h"
joe2d92e852014-01-25 14:31:24 -08002
3#include <stdio.h>
4
5#include <algorithm>
6
7#include "aos/common/control_loop/control_loops.q.h"
Ben Fredricksonedf0e092014-02-16 10:46:50 +00008#include "aos/common/control_loop/control_loops.q.h"
joe2d92e852014-01-25 14:31:24 -08009#include "aos/common/logging/logging.h"
10
11#include "frc971/constants.h"
joe93778a62014-02-15 13:22:14 -080012#include "frc971/control_loops/shooter/shooter_motor_plant.h"
joe2d92e852014-01-25 14:31:24 -080013
14namespace frc971 {
15namespace control_loops {
16
Ben Fredricksonedf0e092014-02-16 10:46:50 +000017using ::aos::time::Time;
Ben Fredrickson22c93322014-02-17 05:56:33 +000018
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000019void ZeroedStateFeedbackLoop::CapU() {
20 const double old_voltage = voltage_;
21 voltage_ += U(0, 0);
22
23 uncapped_voltage_ = voltage_;
24
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000025 // Make sure that reality and the observer can't get too far off. There is a
26 // delay by one cycle between the applied voltage and X_hat(2, 0), so compare
27 // against last cycle's voltage.
Austin Schuhbe1401f2014-02-18 03:18:41 -080028 if (X_hat(2, 0) > last_voltage_ + 4.0) {
29 voltage_ -= X_hat(2, 0) - (last_voltage_ + 4.0);
30 LOG(INFO, "Capping due to runawway\n");
31 } else if (X_hat(2, 0) < last_voltage_ - 4.0) {
32 voltage_ += X_hat(2, 0) - (last_voltage_ - 4.0);
33 LOG(INFO, "Capping due to runawway\n");
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000034 }
35
Austin Schuhd34569d2014-02-18 20:26:38 -080036 voltage_ = std::min(max_voltage_, voltage_);
37 voltage_ = std::max(-max_voltage_, voltage_);
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000038 U(0, 0) = voltage_ - old_voltage;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000039
Austin Schuhbe1401f2014-02-18 03:18:41 -080040 LOG(INFO, "X_hat is %f, applied is %f\n", X_hat(2, 0), voltage_);
41
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000042 last_voltage_ = voltage_;
Austin Schuhd34569d2014-02-18 20:26:38 -080043 capped_goal_ = false;
44}
45
46void ZeroedStateFeedbackLoop::CapGoal() {
47 if (uncapped_voltage() > max_voltage_) {
48 double dx;
49 if (controller_index() == 0) {
50 dx = (uncapped_voltage() - max_voltage_) /
51 (K(0, 0) - A(1, 0) * K(0, 2) / A(1, 2));
52 R(0, 0) -= dx;
53 R(2, 0) -= -A(1, 0) / A(1, 2) * dx;
54 } else {
55 dx = (uncapped_voltage() - max_voltage_) / K(0, 0);
56 R(0, 0) -= dx;
57 }
58 capped_goal_ = true;
59 LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx);
60 } else if (uncapped_voltage() < -max_voltage_) {
61 double dx;
62 if (controller_index() == 0) {
63 dx = (uncapped_voltage() + max_voltage_) /
64 (K(0, 0) - A(1, 0) * K(0, 2) / A(1, 2));
65 R(0, 0) -= dx;
66 R(2, 0) -= -A(1, 0) / A(1, 2) * dx;
67 } else {
68 dx = (uncapped_voltage() + max_voltage_) / K(0, 0);
69 R(0, 0) -= dx;
70 }
71 capped_goal_ = true;
72 LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx);
73 } else {
74 capped_goal_ = false;
75 }
76}
77
78void ZeroedStateFeedbackLoop::RecalculatePowerGoal() {
79 if (controller_index() == 0) {
80 R(2, 0) = (-A(1, 0) / A(1, 2) * R(0, 0) - A(1, 1) / A(1, 2) * R(1, 0));
81 } else {
82 R(2, 0) = -A(1, 1) / A(1, 2) * R(1, 0);
83 }
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000084}
85
Austin Schuh30537882014-02-18 01:07:23 -080086void ZeroedStateFeedbackLoop::SetCalibration(double encoder_val,
87 double known_position) {
88 LOG(INFO, "Setting calibration such that %f -> %f\n", encoder_val,
89 known_position);
90 LOG(INFO, "Position was %f\n", absolute_position());
91 double previous_offset = offset_;
92 offset_ = known_position - encoder_val;
93 double doffset = offset_ - previous_offset;
94 LOG(INFO, "Changing offset from %f to %f\n", previous_offset, offset_);
95 X_hat(0, 0) += doffset;
96 // Offset our measurements because the offset is baked into them.
97 Y_(0, 0) += doffset;
98 // Offset the goal so we don't move.
99 R(0, 0) += doffset;
Austin Schuhd34569d2014-02-18 20:26:38 -0800100 if (controller_index() == 0) {
101 R(2, 0) += -A(1, 0) / A(1, 2) * (doffset);
102 }
Austin Schuh30537882014-02-18 01:07:23 -0800103 LOG(INFO, "Validation: position is %f\n", absolute_position());
104}
105
Ben Fredricksonedf0e092014-02-16 10:46:50 +0000106ShooterMotor::ShooterMotor(control_loops::ShooterGroup *my_shooter)
107 : aos::control_loops::ControlLoop<control_loops::ShooterGroup>(my_shooter),
108 shooter_(MakeShooterLoop()),
Ben Fredrickson22c93322014-02-17 05:56:33 +0000109 state_(STATE_INITIALIZE),
110 loading_problem_end_time_(0, 0),
Austin Schuh30537882014-02-18 01:07:23 -0800111 load_timeout_(0, 0),
Ben Fredrickson22c93322014-02-17 05:56:33 +0000112 shooter_brake_set_time_(0, 0),
Austin Schuh30537882014-02-18 01:07:23 -0800113 unload_timeout_(0, 0),
Ben Fredrickson22c93322014-02-17 05:56:33 +0000114 prepare_fire_end_time_(0, 0),
115 shot_end_time_(0, 0),
Austin Schuh30537882014-02-18 01:07:23 -0800116 cycles_not_moved_(0) {}
117
118double ShooterMotor::PowerToPosition(double power) {
119 // LOG(WARNING, "power to position not correctly implemented\n");
120 const frc971::constants::Values &values = constants::GetValues();
121 double new_pos = ::std::min(::std::max(power, values.shooter.lower_limit),
122 values.shooter.upper_limit);
123 return new_pos;
124}
Ben Fredrickson1e512ff2014-02-15 21:36:52 +0000125
126// Positive is out, and positive power is out.
joe2d92e852014-01-25 14:31:24 -0800127void ShooterMotor::RunIteration(
Ben Fredricksonedf0e092014-02-16 10:46:50 +0000128 const control_loops::ShooterGroup::Goal *goal,
129 const control_loops::ShooterGroup::Position *position,
130 control_loops::ShooterGroup::Output *output,
Ben Fredrickson22c93322014-02-17 05:56:33 +0000131 control_loops::ShooterGroup::Status *status) {
Ben Fredrickson1e512ff2014-02-15 21:36:52 +0000132 constexpr double dt = 0.01;
133
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000134 // we must always have these or we have issues.
135 if (goal == NULL || status == NULL) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000136 if (output) output->voltage = 0;
137 LOG(ERROR, "Thought I would just check for null and die.\n");
138 return;
139 }
140
Austin Schuh30537882014-02-18 01:07:23 -0800141 if (reset()) {
142 state_ = STATE_INITIALIZE;
143 }
144 if (position) {
145 shooter_.CorrectPosition(position->position);
Ben Fredrickson1e512ff2014-02-15 21:36:52 +0000146 }
joe2d92e852014-01-25 14:31:24 -0800147
148 // Disable the motors now so that all early returns will return with the
149 // motors disabled.
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000150 if (output) output->voltage = 0;
joe2d92e852014-01-25 14:31:24 -0800151
Ben Fredrickson1e512ff2014-02-15 21:36:52 +0000152 const frc971::constants::Values &values = constants::GetValues();
153
Brian Silvermanaae236a2014-02-17 01:49:39 -0800154 // Don't even let the control loops run.
Ben Fredricksonedf0e092014-02-16 10:46:50 +0000155 bool shooter_loop_disable = false;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000156
Brian Silvermanaae236a2014-02-17 01:49:39 -0800157 // Adds voltage to take up slack in gears before shot.
Ben Fredrickson7d980c22014-02-16 21:39:02 +0000158 bool apply_some_voltage = false;
159
Austin Schuhd34569d2014-02-18 20:26:38 -0800160
Austin Schuh30537882014-02-18 01:07:23 -0800161 const bool disabled = !::aos::robot_state->enabled;
Austin Schuhd34569d2014-02-18 20:26:38 -0800162 // If true, move the goal if we saturate.
163 bool cap_goal = false;
164
165 // TODO(austin): Move the offset if we see or don't see a hall effect when we
166 // expect to see one.
167 // Probably not needed yet.
168
169 if (position) {
170 int last_controller_index = shooter_.controller_index();
171 if (position->plunger && position->latch) {
172 // Use the controller without the spring if the latch is set and the
173 // plunger is back
174 shooter_.set_controller_index(1);
175 } else {
176 // Otherwise use the controller with the spring.
Austin Schuh06cbbf12014-02-22 02:07:31 -0800177 shooter_.set_controller_index(1);
Austin Schuhd34569d2014-02-18 20:26:38 -0800178 }
179 if (shooter_.controller_index() != last_controller_index) {
180 shooter_.RecalculatePowerGoal();
181 }
182 }
Austin Schuh30537882014-02-18 01:07:23 -0800183
Ben Fredrickson1e512ff2014-02-15 21:36:52 +0000184 switch (state_) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000185 case STATE_INITIALIZE:
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000186 if (position) {
Austin Schuh30537882014-02-18 01:07:23 -0800187 // Reinitialize the internal filter state.
188 shooter_.InitializeState(position->position);
Austin Schuh30537882014-02-18 01:07:23 -0800189
190 // Start off with the assumption that we are at the value
191 // futhest back given our sensors.
192 if (position->pusher_distal.current) {
193 shooter_.SetCalibration(position->position,
194 values.shooter.pusher_distal.lower_angle);
195 } else if (position->pusher_proximal.current) {
196 shooter_.SetCalibration(position->position,
Austin Schuhd34569d2014-02-18 20:26:38 -0800197 values.shooter.pusher_proximal.upper_angle);
Austin Schuh30537882014-02-18 01:07:23 -0800198 } else {
199 shooter_.SetCalibration(position->position,
200 values.shooter.upper_limit);
201 }
202
203 state_ = STATE_REQUEST_LOAD;
204
205 // Go to the current position.
206 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
207 // If the plunger is all the way back, we want to be latched.
208 latch_piston_ = position->plunger;
209 brake_piston_ = false;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000210 } else {
Austin Schuh30537882014-02-18 01:07:23 -0800211 // If we can't start yet because we don't know where we are, set the
212 // latch and brake to their defaults.
213 latch_piston_ = true;
214 brake_piston_ = true;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000215 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000216 break;
217 case STATE_REQUEST_LOAD:
Austin Schuh30537882014-02-18 01:07:23 -0800218 if (position) {
Austin Schuh06cbbf12014-02-22 02:07:31 -0800219 if (position->pusher_distal.current) {
Austin Schuh30537882014-02-18 01:07:23 -0800220 // We started on the sensor, back up until we are found.
221 // If the plunger is all the way back and not latched, it won't be
222 // there for long.
223 state_ = STATE_LOAD_BACKTRACK;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800224
225 // The plunger is already back and latched. Don't release it.
226 if (position->plunger && position->latch) {
227 latch_piston_ = true;
228 } else {
229 latch_piston_ = false;
230 }
231 } else if (position->plunger && position->latch) {
232 // The plunger is back and we are latched. We most likely got here
233 // from Initialize, in which case we want to 'load' again anyways to
234 // zero.
235 Load();
236 latch_piston_ = true;
Austin Schuh30537882014-02-18 01:07:23 -0800237 } else {
238 // Off the sensor, start loading.
239 Load();
240 latch_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000241 }
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000242 }
243
Austin Schuh30537882014-02-18 01:07:23 -0800244 // Hold our current position.
245 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
246 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000247 break;
248 case STATE_LOAD_BACKTRACK:
Austin Schuh30537882014-02-18 01:07:23 -0800249 // If we are here, then that means we started past the edge where we want
250 // to zero. Move backwards until we don't see the sensor anymore.
251 // The plunger is contacting the pusher (or will be shortly).
252
Austin Schuh30537882014-02-18 01:07:23 -0800253 if (!disabled) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000254 shooter_.SetGoalPosition(
Austin Schuhfaeee632014-02-18 01:24:05 -0800255 shooter_.goal_position() + values.shooter.zeroing_speed * dt,
256 values.shooter.zeroing_speed);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000257 }
Austin Schuhd34569d2014-02-18 20:26:38 -0800258 cap_goal = true;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800259 shooter_.set_max_voltage(4.0);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000260
Austin Schuh30537882014-02-18 01:07:23 -0800261 if (position) {
262 if (!position->pusher_distal.current) {
263 Load();
264 }
265 }
266
267 latch_piston_ = false;
268 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000269 break;
270 case STATE_LOAD:
Austin Schuh30537882014-02-18 01:07:23 -0800271 // If we are disabled right now, reset the timer.
272 if (disabled) {
273 Load();
274 // Latch defaults to true when disabled. Leave it latched until we have
275 // useful sensor data.
276 latch_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000277 }
Austin Schuh30537882014-02-18 01:07:23 -0800278 // Go to 0, which should be the latch position, or trigger a hall effect
279 // on the way. If we don't see edges where we are supposed to, the
280 // offset will be updated by code above.
281 shooter_.SetGoalPosition(0.0, 0.0);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000282
Austin Schuh30537882014-02-18 01:07:23 -0800283 if (position) {
284 // If we see a posedge on any of the hall effects,
285 if (position->pusher_proximal.posedge_count !=
286 last_proximal_posedge_count_) {
287 LOG(DEBUG, "Setting calibration using proximal sensor\n");
288 shooter_.SetCalibration(position->pusher_proximal.posedge_value,
289 values.shooter.pusher_proximal.upper_angle);
290 }
291 if (position->pusher_distal.posedge_count !=
292 last_distal_posedge_count_) {
293 LOG(DEBUG, "Setting calibration using distal sensor\n");
294 shooter_.SetCalibration(position->pusher_distal.posedge_value,
295 values.shooter.pusher_distal.upper_angle);
296 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000297
Austin Schuh30537882014-02-18 01:07:23 -0800298 // Latch if the plunger is far enough back to trigger the hall effect.
299 // This happens when the distal sensor is triggered.
Austin Schuh06cbbf12014-02-22 02:07:31 -0800300 latch_piston_ = position->pusher_distal.current || position->plunger;
Austin Schuh30537882014-02-18 01:07:23 -0800301
Austin Schuh06cbbf12014-02-22 02:07:31 -0800302 // Check if we are latched and back. Make sure the plunger is all the
303 // way back as well.
304 if (position->plunger && position->latch &&
305 position->pusher_distal.current) {
Austin Schuh30537882014-02-18 01:07:23 -0800306 state_ = STATE_PREPARE_SHOT;
307 } else if (position->plunger &&
308 ::std::abs(shooter_.absolute_position() -
309 shooter_.goal_position()) < 0.001) {
310 // We are at the goal, but not latched.
311 state_ = STATE_LOADING_PROBLEM;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800312 loading_problem_end_time_ = Time::Now() + kLoadProblemEndTimeout;
Austin Schuh30537882014-02-18 01:07:23 -0800313 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000314 }
Austin Schuh30537882014-02-18 01:07:23 -0800315 if (load_timeout_ < Time::Now()) {
316 if (position) {
317 if (!position->pusher_distal.current ||
318 !position->pusher_proximal.current) {
319 state_ = STATE_ESTOP;
320 }
321 }
322 } else if (goal->unload_requested) {
323 Unload();
324 }
325 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000326 break;
327 case STATE_LOADING_PROBLEM:
Austin Schuh30537882014-02-18 01:07:23 -0800328 if (disabled) {
329 Load();
330 }
331 // We got to the goal, but the latch hasn't registered as down. It might
332 // be stuck, or on it's way but not there yet.
Ben Fredrickson22c93322014-02-17 05:56:33 +0000333 if (Time::Now() > loading_problem_end_time_) {
Austin Schuh30537882014-02-18 01:07:23 -0800334 // Timeout by unloading.
335 Unload();
336 } else if (position && position->plunger && position->latch) {
337 // If both trigger, we are latched.
Ben Fredrickson22c93322014-02-17 05:56:33 +0000338 state_ = STATE_PREPARE_SHOT;
339 }
Austin Schuh30537882014-02-18 01:07:23 -0800340 // Move a bit further back to help it trigger.
341 // If the latch is slow due to the air flowing through the tubes or
342 // inertia, but is otherwise free, this won't have much time to do
343 // anything and is safe. Otherwise this gives us a bit more room to free
344 // up the latch.
345 shooter_.SetGoalPosition(values.shooter.lower_limit, 0.0);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000346 LOG(DEBUG, "Waiting on latch: plunger %d, latch: %d\n",
Austin Schuh60c56662014-02-17 14:37:19 -0800347 position->plunger, position->latch);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000348
Austin Schuh30537882014-02-18 01:07:23 -0800349 latch_piston_ = true;
350 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000351 break;
352 case STATE_PREPARE_SHOT:
Austin Schuh30537882014-02-18 01:07:23 -0800353 // Move the shooter to the shot power set point and then lock the brake.
354 // TODO(austin): Timeout. Low priority.
355
Ben Fredrickson22c93322014-02-17 05:56:33 +0000356 shooter_.SetGoalPosition(PowerToPosition(goal->shot_power), 0.0);
Austin Schuh30537882014-02-18 01:07:23 -0800357
358 LOG(DEBUG, "PDIFF: absolute_position: %.2f, pow: %.2f\n",
Austin Schuhbe1401f2014-02-18 03:18:41 -0800359 shooter_.absolute_position(), PowerToPosition(goal->shot_power));
Austin Schuh30537882014-02-18 01:07:23 -0800360 if (::std::abs(shooter_.absolute_position() -
Austin Schuhbe1401f2014-02-18 03:18:41 -0800361 PowerToPosition(goal->shot_power)) +
362 ::std::abs(shooter_.absolute_velocity()) <
363 0.001) {
Austin Schuh30537882014-02-18 01:07:23 -0800364 // We are there, set the brake and move on.
365 latch_piston_ = true;
366 brake_piston_ = true;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800367 shooter_brake_set_time_ = Time::Now() + kShooterBrakeSetTime;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000368 state_ = STATE_READY;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000369 } else {
Austin Schuh30537882014-02-18 01:07:23 -0800370 latch_piston_ = true;
371 brake_piston_ = false;
372 }
373 if (goal->unload_requested) {
374 Unload();
Ben Fredrickson22c93322014-02-17 05:56:33 +0000375 }
376 break;
377 case STATE_READY:
Austin Schuh30537882014-02-18 01:07:23 -0800378 LOG(DEBUG, "In ready\n");
Austin Schuhbe1401f2014-02-18 03:18:41 -0800379 // Wait until the brake is set, and a shot is requested or the shot power
380 // is changed.
381 if (::std::abs(shooter_.absolute_position() -
382 PowerToPosition(goal->shot_power)) > 0.002) {
383 // TODO(austin): Add a state to release the brake.
384
385 // TODO(austin): Do we want to set the brake here or after shooting?
386 // Depends on air usage.
387 LOG(DEBUG, "Preparing shot again.\n");
388 state_ = STATE_PREPARE_SHOT;
389 } else if (Time::Now() > shooter_brake_set_time_) {
390 // We have waited long enough for the brake to set, turn the shooter
391 // control loop off.
Ben Fredrickson22c93322014-02-17 05:56:33 +0000392 shooter_loop_disable = true;
Austin Schuh30537882014-02-18 01:07:23 -0800393 LOG(DEBUG, "Brake is now set\n");
394 if (goal->shot_requested && !disabled) {
395 LOG(DEBUG, "Shooting now\n");
396 shooter_loop_disable = true;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800397 prepare_fire_end_time_ = Time::Now() + kPrepareFireEndTime;
Austin Schuh30537882014-02-18 01:07:23 -0800398 apply_some_voltage = true;
399 state_ = STATE_PREPARE_FIRE;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000400 }
Austin Schuh30537882014-02-18 01:07:23 -0800401 } else {
402 LOG(DEBUG, "Nothing %d %d\n", goal->shot_requested, !disabled);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000403 }
Ben Fredricksona6d77542014-02-17 07:54:43 +0000404 shooter_.SetGoalPosition(PowerToPosition(goal->shot_power), 0.0);
405
Austin Schuh30537882014-02-18 01:07:23 -0800406 latch_piston_ = true;
407 brake_piston_ = true;
408
409 if (goal->unload_requested) {
410 Unload();
411 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000412 break;
Austin Schuh30537882014-02-18 01:07:23 -0800413
414 case STATE_PREPARE_FIRE:
415 // Apply a bit of voltage to bias the gears for a little bit of time, and
416 // then fire.
Ben Fredrickson22c93322014-02-17 05:56:33 +0000417 shooter_loop_disable = true;
Austin Schuh30537882014-02-18 01:07:23 -0800418 if (disabled) {
419 // If we are disabled, reset the backlash bias timer.
Austin Schuh06cbbf12014-02-22 02:07:31 -0800420 prepare_fire_end_time_ = Time::Now() + kPrepareFireEndTime;
Austin Schuh30537882014-02-18 01:07:23 -0800421 break;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000422 }
Austin Schuh30537882014-02-18 01:07:23 -0800423 if (Time::Now() > prepare_fire_end_time_) {
424 cycles_not_moved_ = 0;
425 firing_starting_position_ = shooter_.absolute_position();
Austin Schuh06cbbf12014-02-22 02:07:31 -0800426 shot_end_time_ = Time::Now() + kShotEndTimeout;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000427 state_ = STATE_FIRE;
Austin Schuhf5642a92014-02-18 01:42:32 -0800428 latch_piston_ = false;
Austin Schuh30537882014-02-18 01:07:23 -0800429 } else {
430 apply_some_voltage = true;
Austin Schuhf5642a92014-02-18 01:42:32 -0800431 latch_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000432 }
433
Austin Schuh30537882014-02-18 01:07:23 -0800434 brake_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000435 break;
Austin Schuh30537882014-02-18 01:07:23 -0800436
Ben Fredrickson22c93322014-02-17 05:56:33 +0000437 case STATE_FIRE:
Austin Schuh30537882014-02-18 01:07:23 -0800438 if (disabled) {
439 if (position) {
440 if (position->plunger) {
441 // If disabled and the plunger is still back there, reset the
442 // timeout.
Austin Schuh06cbbf12014-02-22 02:07:31 -0800443 shot_end_time_ = Time::Now() + kShotEndTimeout;
Austin Schuh30537882014-02-18 01:07:23 -0800444 }
445 }
446 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000447 shooter_loop_disable = true;
Austin Schuh30537882014-02-18 01:07:23 -0800448 // Count the number of contiguous cycles during which we haven't moved.
449 if (::std::abs(last_position_.position - shooter_.absolute_position()) <
450 0.0005) {
451 ++cycles_not_moved_;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000452 } else {
453 cycles_not_moved_ = 0;
454 }
Austin Schuh30537882014-02-18 01:07:23 -0800455
456 // If we have moved any amount since the start and the shooter has now
457 // been still for a couple cycles, the shot finished.
458 // Also move on if it times out.
459 if ((::std::abs(firing_starting_position_ -
460 shooter_.absolute_position()) > 0.0005 &&
461 cycles_not_moved_ > 3) ||
462 Time::Now() > shot_end_time_) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000463 state_ = STATE_REQUEST_LOAD;
464 }
Austin Schuh30537882014-02-18 01:07:23 -0800465 latch_piston_ = false;
466 brake_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000467 break;
468 case STATE_UNLOAD:
Austin Schuh30537882014-02-18 01:07:23 -0800469 // Reset the timeouts.
470 if (disabled) Unload();
471
472 // If it is latched and the plunger is back, move the pusher back to catch
473 // the plunger.
Austin Schuhf84a1302014-02-19 00:23:30 -0800474 bool all_back;
475 if (position) {
476 all_back = position->plunger && position->latch;
477 } else {
478 all_back = last_position_.plunger && last_position_.latch;
479 }
480
481 if (all_back) {
Austin Schuh30537882014-02-18 01:07:23 -0800482 // Pull back to 0, 0.
483 shooter_.SetGoalPosition(0.0, 0.0);
484 if (shooter_.absolute_position() < 0.005) {
485 // When we are close enough, 'fire'.
486 latch_piston_ = false;
487 } else {
488 latch_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000489 }
490 } else {
Austin Schuh30537882014-02-18 01:07:23 -0800491 // The plunger isn't all the way back, or it is and it is unlatched, so
492 // we can now unload.
493 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
494 latch_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000495 state_ = STATE_UNLOAD_MOVE;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800496 unload_timeout_ = Time::Now() + kUnloadTimeout;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000497 }
498
Austin Schuh30537882014-02-18 01:07:23 -0800499 if (Time::Now() > unload_timeout_) {
500 // We have been stuck trying to unload for way too long, give up and
501 // turn everything off.
502 state_ = STATE_ESTOP;
503 }
504
505 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000506 break;
Austin Schuh30537882014-02-18 01:07:23 -0800507 case STATE_UNLOAD_MOVE: {
508 if (disabled) {
Austin Schuh06cbbf12014-02-22 02:07:31 -0800509 unload_timeout_ = Time::Now() + kUnloadTimeout;
Austin Schuh30537882014-02-18 01:07:23 -0800510 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
511 }
Austin Schuhd34569d2014-02-18 20:26:38 -0800512 cap_goal = true;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800513 shooter_.set_max_voltage(5.0);
Austin Schuh30537882014-02-18 01:07:23 -0800514
515 // Slowly move back until we hit the upper limit.
Austin Schuhd34569d2014-02-18 20:26:38 -0800516 // If we were at the limit last cycle, we are done unloading.
517 // This is because if we saturate, we might hit the limit before we are
518 // actually there.
519 if (shooter_.goal_position() >= values.shooter.upper_limit) {
Austin Schuh30537882014-02-18 01:07:23 -0800520 shooter_.SetGoalPosition(values.shooter.upper_limit, 0.0);
521 // We don't want the loop fighting the spring when we are unloaded.
522 // Turn it off.
523 shooter_loop_disable = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000524 state_ = STATE_READY_UNLOAD;
525 } else {
Austin Schuhd34569d2014-02-18 20:26:38 -0800526 shooter_.SetGoalPosition(
527 ::std::min(
528 values.shooter.upper_limit,
529 shooter_.goal_position() + values.shooter.zeroing_speed * dt),
530 values.shooter.zeroing_speed);
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000531 }
532
Austin Schuh30537882014-02-18 01:07:23 -0800533 latch_piston_ = false;
534 brake_piston_ = false;
535 } break;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000536 case STATE_READY_UNLOAD:
Austin Schuh30537882014-02-18 01:07:23 -0800537 if (goal->load_requested) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000538 state_ = STATE_REQUEST_LOAD;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000539 }
Austin Schuh30537882014-02-18 01:07:23 -0800540 // If we are ready to load again,
541 shooter_loop_disable = true;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000542
Austin Schuh30537882014-02-18 01:07:23 -0800543 latch_piston_ = false;
544 brake_piston_ = false;
545 break;
546
547 case STATE_ESTOP:
548 // Totally lost, go to a safe state.
549 shooter_loop_disable = true;
550 latch_piston_ = true;
551 brake_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000552 break;
joe2d92e852014-01-25 14:31:24 -0800553 }
554
Ben Fredrickson7d980c22014-02-16 21:39:02 +0000555 if (apply_some_voltage) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000556 shooter_.Update(true);
Austin Schuhd34569d2014-02-18 20:26:38 -0800557 shooter_.ZeroPower();
Ben Fredrickson22c93322014-02-17 05:56:33 +0000558 if (output) output->voltage = 2.0;
Ben Fredrickson7d980c22014-02-16 21:39:02 +0000559 } else if (!shooter_loop_disable) {
Austin Schuh30537882014-02-18 01:07:23 -0800560 LOG(DEBUG, "Running the loop, goal is %f, position is %f\n",
561 shooter_.goal_position(), shooter_.absolute_position());
Austin Schuhd34569d2014-02-18 20:26:38 -0800562 if (!cap_goal) {
563 shooter_.set_max_voltage(12.0);
564 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000565 shooter_.Update(output == NULL);
Austin Schuhd34569d2014-02-18 20:26:38 -0800566 if (cap_goal) {
567 shooter_.CapGoal();
568 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000569 if (output) output->voltage = shooter_.voltage();
Ben Fredrickson7d980c22014-02-16 21:39:02 +0000570 } else {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000571 shooter_.Update(true);
Austin Schuhd34569d2014-02-18 20:26:38 -0800572 shooter_.ZeroPower();
Ben Fredrickson22c93322014-02-17 05:56:33 +0000573 if (output) output->voltage = 0.0;
Ben Fredricksonedf0e092014-02-16 10:46:50 +0000574 }
Ben Fredrickson7d980c22014-02-16 21:39:02 +0000575
Austin Schuh30537882014-02-18 01:07:23 -0800576 if (output) {
577 output->latch_piston = latch_piston_;
578 output->brake_piston = brake_piston_;
579 }
580
Austin Schuhd34569d2014-02-18 20:26:38 -0800581 status->done = ::std::abs(shooter_.absolute_position() -
582 PowerToPosition(goal->shot_power)) < 0.004;
Austin Schuh30537882014-02-18 01:07:23 -0800583
584 if (position) {
585 last_position_ = *position;
586 LOG(DEBUG,
Austin Schuhbe1401f2014-02-18 03:18:41 -0800587 "pos > absolute: %f velocity: %f state= %d\n",
588 shooter_.absolute_position(), shooter_.absolute_velocity(),
Austin Schuh30537882014-02-18 01:07:23 -0800589 state_);
590 }
591 if (position) {
592 last_distal_posedge_count_ = position->pusher_distal.posedge_count;
593 last_proximal_posedge_count_ = position->pusher_proximal.posedge_count;
594 }
joe2d92e852014-01-25 14:31:24 -0800595}
596
597} // namespace control_loops
598} // namespace frc971