blob: bf2e6149bfc3352506b3012761816c05812130cf [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) {
Austin Schuh30537882014-02-18 01:07:23 -0800119 const frc971::constants::Values &values = constants::GetValues();
Ben Fredricksonda334d12014-02-23 09:09:23 +0000120 double maxpower = 0.5 * kSpringConstant *
121 (kMaxExtension * kMaxExtension -
122 (kMaxExtension - values.shooter.upper_limit) *
123 (kMaxExtension - values.shooter.upper_limit));
124 if (power < 0) {
Ben Fredrickson7c9314f2014-02-23 09:26:17 +0000125 //LOG(ERROR, "Power too low giving minimum (%f) (%f).\n", power,
126 // 0.0);
Ben Fredricksonda334d12014-02-23 09:09:23 +0000127 power = 0;
128 } else if (power > maxpower) {
Ben Fredrickson7c9314f2014-02-23 09:26:17 +0000129 //LOG(ERROR, "Power too high giving maximum (%f) (%f).\n", power,
130 // maxpower);
Ben Fredricksonda334d12014-02-23 09:09:23 +0000131 power = maxpower;
132 }
133
134 double mp = kMaxExtension * kMaxExtension - (power + power) / kSpringConstant;
135 double new_pos = 0.10;
136 if (mp < 0) {
137 LOG(ERROR,
138 "Power calculation has negative number before square root (%f).\n", mp);
139 } else {
140 new_pos = kMaxExtension - ::std::sqrt(mp);
141 }
142
143 new_pos = ::std::min(::std::max(new_pos, values.shooter.lower_limit),
Austin Schuh30537882014-02-18 01:07:23 -0800144 values.shooter.upper_limit);
145 return new_pos;
146}
Ben Fredrickson1e512ff2014-02-15 21:36:52 +0000147
148// Positive is out, and positive power is out.
joe2d92e852014-01-25 14:31:24 -0800149void ShooterMotor::RunIteration(
Ben Fredricksonedf0e092014-02-16 10:46:50 +0000150 const control_loops::ShooterGroup::Goal *goal,
151 const control_loops::ShooterGroup::Position *position,
152 control_loops::ShooterGroup::Output *output,
Ben Fredrickson22c93322014-02-17 05:56:33 +0000153 control_loops::ShooterGroup::Status *status) {
Ben Fredrickson1e512ff2014-02-15 21:36:52 +0000154 constexpr double dt = 0.01;
155
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000156 // we must always have these or we have issues.
157 if (goal == NULL || status == NULL) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000158 if (output) output->voltage = 0;
159 LOG(ERROR, "Thought I would just check for null and die.\n");
160 return;
161 }
162
Austin Schuh30537882014-02-18 01:07:23 -0800163 if (reset()) {
164 state_ = STATE_INITIALIZE;
165 }
166 if (position) {
167 shooter_.CorrectPosition(position->position);
Ben Fredrickson1e512ff2014-02-15 21:36:52 +0000168 }
joe2d92e852014-01-25 14:31:24 -0800169
170 // Disable the motors now so that all early returns will return with the
171 // motors disabled.
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000172 if (output) output->voltage = 0;
joe2d92e852014-01-25 14:31:24 -0800173
Ben Fredrickson1e512ff2014-02-15 21:36:52 +0000174 const frc971::constants::Values &values = constants::GetValues();
175
Brian Silvermanaae236a2014-02-17 01:49:39 -0800176 // Don't even let the control loops run.
Ben Fredricksonedf0e092014-02-16 10:46:50 +0000177 bool shooter_loop_disable = false;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000178
Brian Silvermanaae236a2014-02-17 01:49:39 -0800179 // Adds voltage to take up slack in gears before shot.
Ben Fredrickson7d980c22014-02-16 21:39:02 +0000180 bool apply_some_voltage = false;
181
Austin Schuhd34569d2014-02-18 20:26:38 -0800182
Austin Schuh30537882014-02-18 01:07:23 -0800183 const bool disabled = !::aos::robot_state->enabled;
Austin Schuhd34569d2014-02-18 20:26:38 -0800184 // If true, move the goal if we saturate.
185 bool cap_goal = false;
186
187 // TODO(austin): Move the offset if we see or don't see a hall effect when we
188 // expect to see one.
189 // Probably not needed yet.
190
191 if (position) {
192 int last_controller_index = shooter_.controller_index();
193 if (position->plunger && position->latch) {
194 // Use the controller without the spring if the latch is set and the
195 // plunger is back
196 shooter_.set_controller_index(1);
Austin Schuh6439a7a2014-02-22 18:07:25 -0800197 LOG(DEBUG, "Using controller 1\n");
Austin Schuhd34569d2014-02-18 20:26:38 -0800198 } else {
199 // Otherwise use the controller with the spring.
200 shooter_.set_controller_index(0);
Austin Schuh6439a7a2014-02-22 18:07:25 -0800201 LOG(DEBUG, "Using controller 0\n");
Austin Schuhd34569d2014-02-18 20:26:38 -0800202 }
203 if (shooter_.controller_index() != last_controller_index) {
204 shooter_.RecalculatePowerGoal();
205 }
206 }
Austin Schuh30537882014-02-18 01:07:23 -0800207
Ben Fredrickson1e512ff2014-02-15 21:36:52 +0000208 switch (state_) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000209 case STATE_INITIALIZE:
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000210 if (position) {
Austin Schuh30537882014-02-18 01:07:23 -0800211 // Reinitialize the internal filter state.
212 shooter_.InitializeState(position->position);
Austin Schuh30537882014-02-18 01:07:23 -0800213
214 // Start off with the assumption that we are at the value
215 // futhest back given our sensors.
216 if (position->pusher_distal.current) {
217 shooter_.SetCalibration(position->position,
218 values.shooter.pusher_distal.lower_angle);
219 } else if (position->pusher_proximal.current) {
220 shooter_.SetCalibration(position->position,
Austin Schuhd34569d2014-02-18 20:26:38 -0800221 values.shooter.pusher_proximal.upper_angle);
Austin Schuh30537882014-02-18 01:07:23 -0800222 } else {
223 shooter_.SetCalibration(position->position,
224 values.shooter.upper_limit);
225 }
226
227 state_ = STATE_REQUEST_LOAD;
228
229 // Go to the current position.
230 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
231 // If the plunger is all the way back, we want to be latched.
232 latch_piston_ = position->plunger;
233 brake_piston_ = false;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000234 } else {
Austin Schuh30537882014-02-18 01:07:23 -0800235 // If we can't start yet because we don't know where we are, set the
236 // latch and brake to their defaults.
237 latch_piston_ = true;
238 brake_piston_ = true;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000239 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000240 break;
241 case STATE_REQUEST_LOAD:
Austin Schuh30537882014-02-18 01:07:23 -0800242 if (position) {
Ben Fredrickson26a0dee2014-02-23 04:38:23 +0000243 if (position->pusher_distal.current ||
244 position->pusher_proximal.current) {
Austin Schuh30537882014-02-18 01:07:23 -0800245 // We started on the sensor, back up until we are found.
246 // If the plunger is all the way back and not latched, it won't be
247 // there for long.
248 state_ = STATE_LOAD_BACKTRACK;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800249
250 // The plunger is already back and latched. Don't release it.
251 if (position->plunger && position->latch) {
252 latch_piston_ = true;
253 } else {
254 latch_piston_ = false;
255 }
256 } else if (position->plunger && position->latch) {
257 // The plunger is back and we are latched. We most likely got here
258 // from Initialize, in which case we want to 'load' again anyways to
259 // zero.
260 Load();
261 latch_piston_ = true;
Austin Schuh30537882014-02-18 01:07:23 -0800262 } else {
263 // Off the sensor, start loading.
264 Load();
265 latch_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000266 }
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000267 }
268
Austin Schuh30537882014-02-18 01:07:23 -0800269 // Hold our current position.
270 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
271 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000272 break;
273 case STATE_LOAD_BACKTRACK:
Austin Schuh30537882014-02-18 01:07:23 -0800274 // If we are here, then that means we started past the edge where we want
275 // to zero. Move backwards until we don't see the sensor anymore.
276 // The plunger is contacting the pusher (or will be shortly).
277
Austin Schuh30537882014-02-18 01:07:23 -0800278 if (!disabled) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000279 shooter_.SetGoalPosition(
Austin Schuhfaeee632014-02-18 01:24:05 -0800280 shooter_.goal_position() + values.shooter.zeroing_speed * dt,
281 values.shooter.zeroing_speed);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000282 }
Austin Schuhd34569d2014-02-18 20:26:38 -0800283 cap_goal = true;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800284 shooter_.set_max_voltage(4.0);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000285
Austin Schuh30537882014-02-18 01:07:23 -0800286 if (position) {
Ben Fredrickson26a0dee2014-02-23 04:38:23 +0000287 if (!position->pusher_distal.current &&
288 !position->pusher_proximal.current) {
Austin Schuh30537882014-02-18 01:07:23 -0800289 Load();
290 }
Austin Schuh6b428602014-02-22 21:02:00 -0800291 latch_piston_ = position->plunger;
Austin Schuh30537882014-02-18 01:07:23 -0800292 }
293
Austin Schuh30537882014-02-18 01:07:23 -0800294 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000295 break;
296 case STATE_LOAD:
Austin Schuh30537882014-02-18 01:07:23 -0800297 // If we are disabled right now, reset the timer.
298 if (disabled) {
299 Load();
300 // Latch defaults to true when disabled. Leave it latched until we have
301 // useful sensor data.
302 latch_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000303 }
Austin Schuh30537882014-02-18 01:07:23 -0800304 // Go to 0, which should be the latch position, or trigger a hall effect
305 // on the way. If we don't see edges where we are supposed to, the
306 // offset will be updated by code above.
307 shooter_.SetGoalPosition(0.0, 0.0);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000308
Austin Schuh30537882014-02-18 01:07:23 -0800309 if (position) {
310 // If we see a posedge on any of the hall effects,
311 if (position->pusher_proximal.posedge_count !=
312 last_proximal_posedge_count_) {
313 LOG(DEBUG, "Setting calibration using proximal sensor\n");
314 shooter_.SetCalibration(position->pusher_proximal.posedge_value,
315 values.shooter.pusher_proximal.upper_angle);
316 }
317 if (position->pusher_distal.posedge_count !=
318 last_distal_posedge_count_) {
319 LOG(DEBUG, "Setting calibration using distal sensor\n");
320 shooter_.SetCalibration(position->pusher_distal.posedge_value,
321 values.shooter.pusher_distal.upper_angle);
322 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000323
Austin Schuh30537882014-02-18 01:07:23 -0800324 // Latch if the plunger is far enough back to trigger the hall effect.
325 // This happens when the distal sensor is triggered.
Austin Schuh06cbbf12014-02-22 02:07:31 -0800326 latch_piston_ = position->pusher_distal.current || position->plunger;
Austin Schuh30537882014-02-18 01:07:23 -0800327
Austin Schuh06cbbf12014-02-22 02:07:31 -0800328 // Check if we are latched and back. Make sure the plunger is all the
329 // way back as well.
330 if (position->plunger && position->latch &&
331 position->pusher_distal.current) {
Austin Schuh30537882014-02-18 01:07:23 -0800332 state_ = STATE_PREPARE_SHOT;
333 } else if (position->plunger &&
334 ::std::abs(shooter_.absolute_position() -
335 shooter_.goal_position()) < 0.001) {
336 // We are at the goal, but not latched.
337 state_ = STATE_LOADING_PROBLEM;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800338 loading_problem_end_time_ = Time::Now() + kLoadProblemEndTimeout;
Austin Schuh30537882014-02-18 01:07:23 -0800339 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000340 }
Austin Schuh30537882014-02-18 01:07:23 -0800341 if (load_timeout_ < Time::Now()) {
342 if (position) {
343 if (!position->pusher_distal.current ||
344 !position->pusher_proximal.current) {
345 state_ = STATE_ESTOP;
346 }
347 }
348 } else if (goal->unload_requested) {
349 Unload();
350 }
351 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000352 break;
353 case STATE_LOADING_PROBLEM:
Austin Schuh30537882014-02-18 01:07:23 -0800354 if (disabled) {
355 Load();
356 }
357 // We got to the goal, but the latch hasn't registered as down. It might
358 // be stuck, or on it's way but not there yet.
Ben Fredrickson22c93322014-02-17 05:56:33 +0000359 if (Time::Now() > loading_problem_end_time_) {
Austin Schuh30537882014-02-18 01:07:23 -0800360 // Timeout by unloading.
361 Unload();
362 } else if (position && position->plunger && position->latch) {
363 // If both trigger, we are latched.
Ben Fredrickson22c93322014-02-17 05:56:33 +0000364 state_ = STATE_PREPARE_SHOT;
365 }
Austin Schuh30537882014-02-18 01:07:23 -0800366 // Move a bit further back to help it trigger.
367 // If the latch is slow due to the air flowing through the tubes or
368 // inertia, but is otherwise free, this won't have much time to do
369 // anything and is safe. Otherwise this gives us a bit more room to free
370 // up the latch.
371 shooter_.SetGoalPosition(values.shooter.lower_limit, 0.0);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000372 LOG(DEBUG, "Waiting on latch: plunger %d, latch: %d\n",
Austin Schuh60c56662014-02-17 14:37:19 -0800373 position->plunger, position->latch);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000374
Austin Schuh30537882014-02-18 01:07:23 -0800375 latch_piston_ = true;
376 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000377 break;
378 case STATE_PREPARE_SHOT:
Austin Schuh30537882014-02-18 01:07:23 -0800379 // Move the shooter to the shot power set point and then lock the brake.
380 // TODO(austin): Timeout. Low priority.
381
Ben Fredrickson22c93322014-02-17 05:56:33 +0000382 shooter_.SetGoalPosition(PowerToPosition(goal->shot_power), 0.0);
Austin Schuh30537882014-02-18 01:07:23 -0800383
384 LOG(DEBUG, "PDIFF: absolute_position: %.2f, pow: %.2f\n",
Austin Schuhbe1401f2014-02-18 03:18:41 -0800385 shooter_.absolute_position(), PowerToPosition(goal->shot_power));
Austin Schuh30537882014-02-18 01:07:23 -0800386 if (::std::abs(shooter_.absolute_position() -
Austin Schuhbe1401f2014-02-18 03:18:41 -0800387 PowerToPosition(goal->shot_power)) +
388 ::std::abs(shooter_.absolute_velocity()) <
389 0.001) {
Austin Schuh30537882014-02-18 01:07:23 -0800390 // We are there, set the brake and move on.
391 latch_piston_ = true;
392 brake_piston_ = true;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800393 shooter_brake_set_time_ = Time::Now() + kShooterBrakeSetTime;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000394 state_ = STATE_READY;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000395 } else {
Austin Schuh30537882014-02-18 01:07:23 -0800396 latch_piston_ = true;
397 brake_piston_ = false;
398 }
399 if (goal->unload_requested) {
400 Unload();
Ben Fredrickson22c93322014-02-17 05:56:33 +0000401 }
402 break;
403 case STATE_READY:
Austin Schuh30537882014-02-18 01:07:23 -0800404 LOG(DEBUG, "In ready\n");
Austin Schuhbe1401f2014-02-18 03:18:41 -0800405 // Wait until the brake is set, and a shot is requested or the shot power
406 // is changed.
Austin Schuh6b428602014-02-22 21:02:00 -0800407 if (Time::Now() > shooter_brake_set_time_) {
Austin Schuhbe1401f2014-02-18 03:18:41 -0800408 // We have waited long enough for the brake to set, turn the shooter
409 // control loop off.
Ben Fredrickson22c93322014-02-17 05:56:33 +0000410 shooter_loop_disable = true;
Austin Schuh30537882014-02-18 01:07:23 -0800411 LOG(DEBUG, "Brake is now set\n");
412 if (goal->shot_requested && !disabled) {
413 LOG(DEBUG, "Shooting now\n");
414 shooter_loop_disable = true;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800415 prepare_fire_end_time_ = Time::Now() + kPrepareFireEndTime;
Austin Schuh30537882014-02-18 01:07:23 -0800416 apply_some_voltage = true;
417 state_ = STATE_PREPARE_FIRE;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000418 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000419 }
Austin Schuh6b428602014-02-22 21:02:00 -0800420 if (state_ == STATE_READY &&
421 ::std::abs(shooter_.absolute_position() -
422 PowerToPosition(goal->shot_power)) > 0.002) {
423 // TODO(austin): Add a state to release the brake.
424
425 // TODO(austin): Do we want to set the brake here or after shooting?
426 // Depends on air usage.
427 LOG(DEBUG, "Preparing shot again.\n");
428 state_ = STATE_PREPARE_SHOT;
429 }
430
Ben Fredricksona6d77542014-02-17 07:54:43 +0000431 shooter_.SetGoalPosition(PowerToPosition(goal->shot_power), 0.0);
432
Austin Schuh30537882014-02-18 01:07:23 -0800433 latch_piston_ = true;
434 brake_piston_ = true;
435
436 if (goal->unload_requested) {
437 Unload();
438 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000439 break;
Austin Schuh30537882014-02-18 01:07:23 -0800440
441 case STATE_PREPARE_FIRE:
442 // Apply a bit of voltage to bias the gears for a little bit of time, and
443 // then fire.
Ben Fredrickson22c93322014-02-17 05:56:33 +0000444 shooter_loop_disable = true;
Austin Schuh30537882014-02-18 01:07:23 -0800445 if (disabled) {
446 // If we are disabled, reset the backlash bias timer.
Austin Schuh06cbbf12014-02-22 02:07:31 -0800447 prepare_fire_end_time_ = Time::Now() + kPrepareFireEndTime;
Austin Schuh30537882014-02-18 01:07:23 -0800448 break;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000449 }
Austin Schuh30537882014-02-18 01:07:23 -0800450 if (Time::Now() > prepare_fire_end_time_) {
451 cycles_not_moved_ = 0;
452 firing_starting_position_ = shooter_.absolute_position();
Austin Schuh06cbbf12014-02-22 02:07:31 -0800453 shot_end_time_ = Time::Now() + kShotEndTimeout;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000454 state_ = STATE_FIRE;
Austin Schuhf5642a92014-02-18 01:42:32 -0800455 latch_piston_ = false;
Austin Schuh30537882014-02-18 01:07:23 -0800456 } else {
457 apply_some_voltage = true;
Austin Schuhf5642a92014-02-18 01:42:32 -0800458 latch_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000459 }
460
Austin Schuh30537882014-02-18 01:07:23 -0800461 brake_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000462 break;
Austin Schuh30537882014-02-18 01:07:23 -0800463
Ben Fredrickson22c93322014-02-17 05:56:33 +0000464 case STATE_FIRE:
Austin Schuh30537882014-02-18 01:07:23 -0800465 if (disabled) {
466 if (position) {
467 if (position->plunger) {
468 // If disabled and the plunger is still back there, reset the
469 // timeout.
Austin Schuh06cbbf12014-02-22 02:07:31 -0800470 shot_end_time_ = Time::Now() + kShotEndTimeout;
Austin Schuh30537882014-02-18 01:07:23 -0800471 }
472 }
473 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000474 shooter_loop_disable = true;
Austin Schuh30537882014-02-18 01:07:23 -0800475 // Count the number of contiguous cycles during which we haven't moved.
476 if (::std::abs(last_position_.position - shooter_.absolute_position()) <
477 0.0005) {
478 ++cycles_not_moved_;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000479 } else {
480 cycles_not_moved_ = 0;
481 }
Austin Schuh30537882014-02-18 01:07:23 -0800482
483 // If we have moved any amount since the start and the shooter has now
484 // been still for a couple cycles, the shot finished.
485 // Also move on if it times out.
486 if ((::std::abs(firing_starting_position_ -
487 shooter_.absolute_position()) > 0.0005 &&
488 cycles_not_moved_ > 3) ||
489 Time::Now() > shot_end_time_) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000490 state_ = STATE_REQUEST_LOAD;
491 }
Austin Schuh30537882014-02-18 01:07:23 -0800492 latch_piston_ = false;
493 brake_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000494 break;
495 case STATE_UNLOAD:
Austin Schuh30537882014-02-18 01:07:23 -0800496 // Reset the timeouts.
497 if (disabled) Unload();
498
499 // If it is latched and the plunger is back, move the pusher back to catch
500 // the plunger.
Austin Schuhf84a1302014-02-19 00:23:30 -0800501 bool all_back;
502 if (position) {
503 all_back = position->plunger && position->latch;
504 } else {
505 all_back = last_position_.plunger && last_position_.latch;
506 }
507
508 if (all_back) {
Austin Schuh30537882014-02-18 01:07:23 -0800509 // Pull back to 0, 0.
510 shooter_.SetGoalPosition(0.0, 0.0);
511 if (shooter_.absolute_position() < 0.005) {
512 // When we are close enough, 'fire'.
513 latch_piston_ = false;
514 } else {
515 latch_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000516 }
517 } else {
Austin Schuh30537882014-02-18 01:07:23 -0800518 // The plunger isn't all the way back, or it is and it is unlatched, so
519 // we can now unload.
520 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
521 latch_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000522 state_ = STATE_UNLOAD_MOVE;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800523 unload_timeout_ = Time::Now() + kUnloadTimeout;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000524 }
525
Austin Schuh30537882014-02-18 01:07:23 -0800526 if (Time::Now() > unload_timeout_) {
527 // We have been stuck trying to unload for way too long, give up and
528 // turn everything off.
529 state_ = STATE_ESTOP;
530 }
531
532 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000533 break;
Austin Schuh30537882014-02-18 01:07:23 -0800534 case STATE_UNLOAD_MOVE: {
535 if (disabled) {
Austin Schuh06cbbf12014-02-22 02:07:31 -0800536 unload_timeout_ = Time::Now() + kUnloadTimeout;
Austin Schuh30537882014-02-18 01:07:23 -0800537 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
538 }
Austin Schuhd34569d2014-02-18 20:26:38 -0800539 cap_goal = true;
Austin Schuh6b428602014-02-22 21:02:00 -0800540 shooter_.set_max_voltage(6.0);
Austin Schuh30537882014-02-18 01:07:23 -0800541
542 // Slowly move back until we hit the upper limit.
Austin Schuhd34569d2014-02-18 20:26:38 -0800543 // If we were at the limit last cycle, we are done unloading.
544 // This is because if we saturate, we might hit the limit before we are
545 // actually there.
546 if (shooter_.goal_position() >= values.shooter.upper_limit) {
Austin Schuh30537882014-02-18 01:07:23 -0800547 shooter_.SetGoalPosition(values.shooter.upper_limit, 0.0);
548 // We don't want the loop fighting the spring when we are unloaded.
549 // Turn it off.
550 shooter_loop_disable = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000551 state_ = STATE_READY_UNLOAD;
552 } else {
Austin Schuhd34569d2014-02-18 20:26:38 -0800553 shooter_.SetGoalPosition(
554 ::std::min(
555 values.shooter.upper_limit,
Austin Schuh6b428602014-02-22 21:02:00 -0800556 shooter_.goal_position() + values.shooter.unload_speed * dt),
557 values.shooter.unload_speed);
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000558 }
559
Austin Schuh30537882014-02-18 01:07:23 -0800560 latch_piston_ = false;
561 brake_piston_ = false;
562 } break;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000563 case STATE_READY_UNLOAD:
Austin Schuh30537882014-02-18 01:07:23 -0800564 if (goal->load_requested) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000565 state_ = STATE_REQUEST_LOAD;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000566 }
Austin Schuh30537882014-02-18 01:07:23 -0800567 // If we are ready to load again,
568 shooter_loop_disable = true;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000569
Austin Schuh30537882014-02-18 01:07:23 -0800570 latch_piston_ = false;
571 brake_piston_ = false;
572 break;
573
574 case STATE_ESTOP:
575 // Totally lost, go to a safe state.
576 shooter_loop_disable = true;
577 latch_piston_ = true;
578 brake_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000579 break;
joe2d92e852014-01-25 14:31:24 -0800580 }
581
Ben Fredrickson7d980c22014-02-16 21:39:02 +0000582 if (apply_some_voltage) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000583 shooter_.Update(true);
Austin Schuhd34569d2014-02-18 20:26:38 -0800584 shooter_.ZeroPower();
Ben Fredrickson22c93322014-02-17 05:56:33 +0000585 if (output) output->voltage = 2.0;
Ben Fredrickson7d980c22014-02-16 21:39:02 +0000586 } else if (!shooter_loop_disable) {
Austin Schuh30537882014-02-18 01:07:23 -0800587 LOG(DEBUG, "Running the loop, goal is %f, position is %f\n",
588 shooter_.goal_position(), shooter_.absolute_position());
Austin Schuhd34569d2014-02-18 20:26:38 -0800589 if (!cap_goal) {
590 shooter_.set_max_voltage(12.0);
591 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000592 shooter_.Update(output == NULL);
Austin Schuhd34569d2014-02-18 20:26:38 -0800593 if (cap_goal) {
594 shooter_.CapGoal();
595 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000596 if (output) output->voltage = shooter_.voltage();
Ben Fredrickson7d980c22014-02-16 21:39:02 +0000597 } else {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000598 shooter_.Update(true);
Austin Schuhd34569d2014-02-18 20:26:38 -0800599 shooter_.ZeroPower();
Ben Fredrickson22c93322014-02-17 05:56:33 +0000600 if (output) output->voltage = 0.0;
Ben Fredricksonedf0e092014-02-16 10:46:50 +0000601 }
Ben Fredrickson7d980c22014-02-16 21:39:02 +0000602
Austin Schuh30537882014-02-18 01:07:23 -0800603 if (output) {
604 output->latch_piston = latch_piston_;
605 output->brake_piston = brake_piston_;
606 }
607
Austin Schuhd34569d2014-02-18 20:26:38 -0800608 status->done = ::std::abs(shooter_.absolute_position() -
609 PowerToPosition(goal->shot_power)) < 0.004;
Austin Schuh30537882014-02-18 01:07:23 -0800610
611 if (position) {
612 last_position_ = *position;
Ben Fredricksona9dcfa42014-02-23 02:05:59 +0000613 LOG(DEBUG, "pos > absolute: %f velocity: %f state= %d l= %d pp= %d, pd= %d "
614 "p= %d b=%d\n",
Austin Schuhbe1401f2014-02-18 03:18:41 -0800615 shooter_.absolute_position(), shooter_.absolute_velocity(),
Ben Fredricksona9dcfa42014-02-23 02:05:59 +0000616 state_, position->latch, position->pusher_proximal.current,
617 position->pusher_distal.current,
618 position->plunger, brake_piston_);
Austin Schuh30537882014-02-18 01:07:23 -0800619 }
620 if (position) {
621 last_distal_posedge_count_ = position->pusher_distal.posedge_count;
622 last_proximal_posedge_count_ = position->pusher_proximal.posedge_count;
623 }
joe2d92e852014-01-25 14:31:24 -0800624}
625
626} // namespace control_loops
627} // namespace frc971