blob: 73d8bad7e72393f26bb7e6f2ac92094e9f93c841 [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);
Austin Schuh6439a7a2014-02-22 18:07:25 -0800175 LOG(DEBUG, "Using controller 1\n");
Austin Schuhd34569d2014-02-18 20:26:38 -0800176 } else {
177 // Otherwise use the controller with the spring.
178 shooter_.set_controller_index(0);
Austin Schuh6439a7a2014-02-22 18:07:25 -0800179 LOG(DEBUG, "Using controller 0\n");
Austin Schuhd34569d2014-02-18 20:26:38 -0800180 }
181 if (shooter_.controller_index() != last_controller_index) {
182 shooter_.RecalculatePowerGoal();
183 }
184 }
Austin Schuh30537882014-02-18 01:07:23 -0800185
Ben Fredrickson1e512ff2014-02-15 21:36:52 +0000186 switch (state_) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000187 case STATE_INITIALIZE:
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000188 if (position) {
Austin Schuh30537882014-02-18 01:07:23 -0800189 // Reinitialize the internal filter state.
190 shooter_.InitializeState(position->position);
Austin Schuh30537882014-02-18 01:07:23 -0800191
192 // Start off with the assumption that we are at the value
193 // futhest back given our sensors.
194 if (position->pusher_distal.current) {
195 shooter_.SetCalibration(position->position,
196 values.shooter.pusher_distal.lower_angle);
197 } else if (position->pusher_proximal.current) {
198 shooter_.SetCalibration(position->position,
Austin Schuhd34569d2014-02-18 20:26:38 -0800199 values.shooter.pusher_proximal.upper_angle);
Austin Schuh30537882014-02-18 01:07:23 -0800200 } else {
201 shooter_.SetCalibration(position->position,
202 values.shooter.upper_limit);
203 }
204
205 state_ = STATE_REQUEST_LOAD;
206
207 // Go to the current position.
208 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
209 // If the plunger is all the way back, we want to be latched.
210 latch_piston_ = position->plunger;
211 brake_piston_ = false;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000212 } else {
Austin Schuh30537882014-02-18 01:07:23 -0800213 // If we can't start yet because we don't know where we are, set the
214 // latch and brake to their defaults.
215 latch_piston_ = true;
216 brake_piston_ = true;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000217 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000218 break;
219 case STATE_REQUEST_LOAD:
Austin Schuh30537882014-02-18 01:07:23 -0800220 if (position) {
Ben Fredrickson26a0dee2014-02-23 04:38:23 +0000221 if (position->pusher_distal.current ||
222 position->pusher_proximal.current) {
Austin Schuh30537882014-02-18 01:07:23 -0800223 // We started on the sensor, back up until we are found.
224 // If the plunger is all the way back and not latched, it won't be
225 // there for long.
226 state_ = STATE_LOAD_BACKTRACK;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800227
228 // The plunger is already back and latched. Don't release it.
229 if (position->plunger && position->latch) {
230 latch_piston_ = true;
231 } else {
232 latch_piston_ = false;
233 }
234 } else if (position->plunger && position->latch) {
235 // The plunger is back and we are latched. We most likely got here
236 // from Initialize, in which case we want to 'load' again anyways to
237 // zero.
238 Load();
239 latch_piston_ = true;
Austin Schuh30537882014-02-18 01:07:23 -0800240 } else {
241 // Off the sensor, start loading.
242 Load();
243 latch_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000244 }
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000245 }
246
Austin Schuh30537882014-02-18 01:07:23 -0800247 // Hold our current position.
248 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
249 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000250 break;
251 case STATE_LOAD_BACKTRACK:
Austin Schuh30537882014-02-18 01:07:23 -0800252 // If we are here, then that means we started past the edge where we want
253 // to zero. Move backwards until we don't see the sensor anymore.
254 // The plunger is contacting the pusher (or will be shortly).
255
Austin Schuh30537882014-02-18 01:07:23 -0800256 if (!disabled) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000257 shooter_.SetGoalPosition(
Austin Schuhfaeee632014-02-18 01:24:05 -0800258 shooter_.goal_position() + values.shooter.zeroing_speed * dt,
259 values.shooter.zeroing_speed);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000260 }
Austin Schuhd34569d2014-02-18 20:26:38 -0800261 cap_goal = true;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800262 shooter_.set_max_voltage(4.0);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000263
Austin Schuh30537882014-02-18 01:07:23 -0800264 if (position) {
Ben Fredrickson26a0dee2014-02-23 04:38:23 +0000265 if (!position->pusher_distal.current &&
266 !position->pusher_proximal.current) {
Austin Schuh30537882014-02-18 01:07:23 -0800267 Load();
268 }
269 }
270
271 latch_piston_ = false;
272 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000273 break;
274 case STATE_LOAD:
Austin Schuh30537882014-02-18 01:07:23 -0800275 // If we are disabled right now, reset the timer.
276 if (disabled) {
277 Load();
278 // Latch defaults to true when disabled. Leave it latched until we have
279 // useful sensor data.
280 latch_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000281 }
Austin Schuh30537882014-02-18 01:07:23 -0800282 // Go to 0, which should be the latch position, or trigger a hall effect
283 // on the way. If we don't see edges where we are supposed to, the
284 // offset will be updated by code above.
285 shooter_.SetGoalPosition(0.0, 0.0);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000286
Austin Schuh30537882014-02-18 01:07:23 -0800287 if (position) {
288 // If we see a posedge on any of the hall effects,
289 if (position->pusher_proximal.posedge_count !=
290 last_proximal_posedge_count_) {
291 LOG(DEBUG, "Setting calibration using proximal sensor\n");
292 shooter_.SetCalibration(position->pusher_proximal.posedge_value,
293 values.shooter.pusher_proximal.upper_angle);
294 }
295 if (position->pusher_distal.posedge_count !=
296 last_distal_posedge_count_) {
297 LOG(DEBUG, "Setting calibration using distal sensor\n");
298 shooter_.SetCalibration(position->pusher_distal.posedge_value,
299 values.shooter.pusher_distal.upper_angle);
300 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000301
Austin Schuh30537882014-02-18 01:07:23 -0800302 // Latch if the plunger is far enough back to trigger the hall effect.
303 // This happens when the distal sensor is triggered.
Austin Schuh06cbbf12014-02-22 02:07:31 -0800304 latch_piston_ = position->pusher_distal.current || position->plunger;
Austin Schuh30537882014-02-18 01:07:23 -0800305
Austin Schuh06cbbf12014-02-22 02:07:31 -0800306 // Check if we are latched and back. Make sure the plunger is all the
307 // way back as well.
308 if (position->plunger && position->latch &&
309 position->pusher_distal.current) {
Austin Schuh30537882014-02-18 01:07:23 -0800310 state_ = STATE_PREPARE_SHOT;
311 } else if (position->plunger &&
312 ::std::abs(shooter_.absolute_position() -
313 shooter_.goal_position()) < 0.001) {
314 // We are at the goal, but not latched.
315 state_ = STATE_LOADING_PROBLEM;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800316 loading_problem_end_time_ = Time::Now() + kLoadProblemEndTimeout;
Austin Schuh30537882014-02-18 01:07:23 -0800317 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000318 }
Austin Schuh30537882014-02-18 01:07:23 -0800319 if (load_timeout_ < Time::Now()) {
320 if (position) {
321 if (!position->pusher_distal.current ||
322 !position->pusher_proximal.current) {
323 state_ = STATE_ESTOP;
324 }
325 }
326 } else if (goal->unload_requested) {
327 Unload();
328 }
329 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000330 break;
331 case STATE_LOADING_PROBLEM:
Austin Schuh30537882014-02-18 01:07:23 -0800332 if (disabled) {
333 Load();
334 }
335 // We got to the goal, but the latch hasn't registered as down. It might
336 // be stuck, or on it's way but not there yet.
Ben Fredrickson22c93322014-02-17 05:56:33 +0000337 if (Time::Now() > loading_problem_end_time_) {
Austin Schuh30537882014-02-18 01:07:23 -0800338 // Timeout by unloading.
339 Unload();
340 } else if (position && position->plunger && position->latch) {
341 // If both trigger, we are latched.
Ben Fredrickson22c93322014-02-17 05:56:33 +0000342 state_ = STATE_PREPARE_SHOT;
343 }
Austin Schuh30537882014-02-18 01:07:23 -0800344 // Move a bit further back to help it trigger.
345 // If the latch is slow due to the air flowing through the tubes or
346 // inertia, but is otherwise free, this won't have much time to do
347 // anything and is safe. Otherwise this gives us a bit more room to free
348 // up the latch.
349 shooter_.SetGoalPosition(values.shooter.lower_limit, 0.0);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000350 LOG(DEBUG, "Waiting on latch: plunger %d, latch: %d\n",
Austin Schuh60c56662014-02-17 14:37:19 -0800351 position->plunger, position->latch);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000352
Austin Schuh30537882014-02-18 01:07:23 -0800353 latch_piston_ = true;
354 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000355 break;
356 case STATE_PREPARE_SHOT:
Austin Schuh30537882014-02-18 01:07:23 -0800357 // Move the shooter to the shot power set point and then lock the brake.
358 // TODO(austin): Timeout. Low priority.
359
Ben Fredrickson22c93322014-02-17 05:56:33 +0000360 shooter_.SetGoalPosition(PowerToPosition(goal->shot_power), 0.0);
Austin Schuh30537882014-02-18 01:07:23 -0800361
362 LOG(DEBUG, "PDIFF: absolute_position: %.2f, pow: %.2f\n",
Austin Schuhbe1401f2014-02-18 03:18:41 -0800363 shooter_.absolute_position(), PowerToPosition(goal->shot_power));
Austin Schuh30537882014-02-18 01:07:23 -0800364 if (::std::abs(shooter_.absolute_position() -
Austin Schuhbe1401f2014-02-18 03:18:41 -0800365 PowerToPosition(goal->shot_power)) +
366 ::std::abs(shooter_.absolute_velocity()) <
367 0.001) {
Austin Schuh30537882014-02-18 01:07:23 -0800368 // We are there, set the brake and move on.
369 latch_piston_ = true;
370 brake_piston_ = true;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800371 shooter_brake_set_time_ = Time::Now() + kShooterBrakeSetTime;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000372 state_ = STATE_READY;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000373 } else {
Austin Schuh30537882014-02-18 01:07:23 -0800374 latch_piston_ = true;
375 brake_piston_ = false;
376 }
377 if (goal->unload_requested) {
378 Unload();
Ben Fredrickson22c93322014-02-17 05:56:33 +0000379 }
380 break;
381 case STATE_READY:
Austin Schuh30537882014-02-18 01:07:23 -0800382 LOG(DEBUG, "In ready\n");
Austin Schuhbe1401f2014-02-18 03:18:41 -0800383 // Wait until the brake is set, and a shot is requested or the shot power
384 // is changed.
385 if (::std::abs(shooter_.absolute_position() -
386 PowerToPosition(goal->shot_power)) > 0.002) {
387 // TODO(austin): Add a state to release the brake.
388
389 // TODO(austin): Do we want to set the brake here or after shooting?
390 // Depends on air usage.
391 LOG(DEBUG, "Preparing shot again.\n");
392 state_ = STATE_PREPARE_SHOT;
393 } else if (Time::Now() > shooter_brake_set_time_) {
394 // We have waited long enough for the brake to set, turn the shooter
395 // control loop off.
Ben Fredrickson22c93322014-02-17 05:56:33 +0000396 shooter_loop_disable = true;
Austin Schuh30537882014-02-18 01:07:23 -0800397 LOG(DEBUG, "Brake is now set\n");
398 if (goal->shot_requested && !disabled) {
399 LOG(DEBUG, "Shooting now\n");
400 shooter_loop_disable = true;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800401 prepare_fire_end_time_ = Time::Now() + kPrepareFireEndTime;
Austin Schuh30537882014-02-18 01:07:23 -0800402 apply_some_voltage = true;
403 state_ = STATE_PREPARE_FIRE;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000404 }
Austin Schuh30537882014-02-18 01:07:23 -0800405 } else {
406 LOG(DEBUG, "Nothing %d %d\n", goal->shot_requested, !disabled);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000407 }
Ben Fredricksona6d77542014-02-17 07:54:43 +0000408 shooter_.SetGoalPosition(PowerToPosition(goal->shot_power), 0.0);
409
Austin Schuh30537882014-02-18 01:07:23 -0800410 latch_piston_ = true;
411 brake_piston_ = true;
412
413 if (goal->unload_requested) {
414 Unload();
415 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000416 break;
Austin Schuh30537882014-02-18 01:07:23 -0800417
418 case STATE_PREPARE_FIRE:
419 // Apply a bit of voltage to bias the gears for a little bit of time, and
420 // then fire.
Ben Fredrickson22c93322014-02-17 05:56:33 +0000421 shooter_loop_disable = true;
Austin Schuh30537882014-02-18 01:07:23 -0800422 if (disabled) {
423 // If we are disabled, reset the backlash bias timer.
Austin Schuh06cbbf12014-02-22 02:07:31 -0800424 prepare_fire_end_time_ = Time::Now() + kPrepareFireEndTime;
Austin Schuh30537882014-02-18 01:07:23 -0800425 break;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000426 }
Austin Schuh30537882014-02-18 01:07:23 -0800427 if (Time::Now() > prepare_fire_end_time_) {
428 cycles_not_moved_ = 0;
429 firing_starting_position_ = shooter_.absolute_position();
Austin Schuh06cbbf12014-02-22 02:07:31 -0800430 shot_end_time_ = Time::Now() + kShotEndTimeout;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000431 state_ = STATE_FIRE;
Austin Schuhf5642a92014-02-18 01:42:32 -0800432 latch_piston_ = false;
Austin Schuh30537882014-02-18 01:07:23 -0800433 } else {
434 apply_some_voltage = true;
Austin Schuhf5642a92014-02-18 01:42:32 -0800435 latch_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000436 }
437
Austin Schuh30537882014-02-18 01:07:23 -0800438 brake_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000439 break;
Austin Schuh30537882014-02-18 01:07:23 -0800440
Ben Fredrickson22c93322014-02-17 05:56:33 +0000441 case STATE_FIRE:
Austin Schuh30537882014-02-18 01:07:23 -0800442 if (disabled) {
443 if (position) {
444 if (position->plunger) {
445 // If disabled and the plunger is still back there, reset the
446 // timeout.
Austin Schuh06cbbf12014-02-22 02:07:31 -0800447 shot_end_time_ = Time::Now() + kShotEndTimeout;
Austin Schuh30537882014-02-18 01:07:23 -0800448 }
449 }
450 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000451 shooter_loop_disable = true;
Austin Schuh30537882014-02-18 01:07:23 -0800452 // Count the number of contiguous cycles during which we haven't moved.
453 if (::std::abs(last_position_.position - shooter_.absolute_position()) <
454 0.0005) {
455 ++cycles_not_moved_;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000456 } else {
457 cycles_not_moved_ = 0;
458 }
Austin Schuh30537882014-02-18 01:07:23 -0800459
460 // If we have moved any amount since the start and the shooter has now
461 // been still for a couple cycles, the shot finished.
462 // Also move on if it times out.
463 if ((::std::abs(firing_starting_position_ -
464 shooter_.absolute_position()) > 0.0005 &&
465 cycles_not_moved_ > 3) ||
466 Time::Now() > shot_end_time_) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000467 state_ = STATE_REQUEST_LOAD;
468 }
Austin Schuh30537882014-02-18 01:07:23 -0800469 latch_piston_ = false;
470 brake_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000471 break;
472 case STATE_UNLOAD:
Austin Schuh30537882014-02-18 01:07:23 -0800473 // Reset the timeouts.
474 if (disabled) Unload();
475
476 // If it is latched and the plunger is back, move the pusher back to catch
477 // the plunger.
Austin Schuhf84a1302014-02-19 00:23:30 -0800478 bool all_back;
479 if (position) {
480 all_back = position->plunger && position->latch;
481 } else {
482 all_back = last_position_.plunger && last_position_.latch;
483 }
484
485 if (all_back) {
Austin Schuh30537882014-02-18 01:07:23 -0800486 // Pull back to 0, 0.
487 shooter_.SetGoalPosition(0.0, 0.0);
488 if (shooter_.absolute_position() < 0.005) {
489 // When we are close enough, 'fire'.
490 latch_piston_ = false;
491 } else {
492 latch_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000493 }
494 } else {
Austin Schuh30537882014-02-18 01:07:23 -0800495 // The plunger isn't all the way back, or it is and it is unlatched, so
496 // we can now unload.
497 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
498 latch_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000499 state_ = STATE_UNLOAD_MOVE;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800500 unload_timeout_ = Time::Now() + kUnloadTimeout;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000501 }
502
Austin Schuh30537882014-02-18 01:07:23 -0800503 if (Time::Now() > unload_timeout_) {
504 // We have been stuck trying to unload for way too long, give up and
505 // turn everything off.
506 state_ = STATE_ESTOP;
507 }
508
509 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000510 break;
Austin Schuh30537882014-02-18 01:07:23 -0800511 case STATE_UNLOAD_MOVE: {
512 if (disabled) {
Austin Schuh06cbbf12014-02-22 02:07:31 -0800513 unload_timeout_ = Time::Now() + kUnloadTimeout;
Austin Schuh30537882014-02-18 01:07:23 -0800514 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
515 }
Austin Schuhd34569d2014-02-18 20:26:38 -0800516 cap_goal = true;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800517 shooter_.set_max_voltage(5.0);
Austin Schuh30537882014-02-18 01:07:23 -0800518
519 // Slowly move back until we hit the upper limit.
Austin Schuhd34569d2014-02-18 20:26:38 -0800520 // If we were at the limit last cycle, we are done unloading.
521 // This is because if we saturate, we might hit the limit before we are
522 // actually there.
523 if (shooter_.goal_position() >= values.shooter.upper_limit) {
Austin Schuh30537882014-02-18 01:07:23 -0800524 shooter_.SetGoalPosition(values.shooter.upper_limit, 0.0);
525 // We don't want the loop fighting the spring when we are unloaded.
526 // Turn it off.
527 shooter_loop_disable = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000528 state_ = STATE_READY_UNLOAD;
529 } else {
Austin Schuhd34569d2014-02-18 20:26:38 -0800530 shooter_.SetGoalPosition(
531 ::std::min(
532 values.shooter.upper_limit,
533 shooter_.goal_position() + values.shooter.zeroing_speed * dt),
534 values.shooter.zeroing_speed);
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000535 }
536
Austin Schuh30537882014-02-18 01:07:23 -0800537 latch_piston_ = false;
538 brake_piston_ = false;
539 } break;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000540 case STATE_READY_UNLOAD:
Austin Schuh30537882014-02-18 01:07:23 -0800541 if (goal->load_requested) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000542 state_ = STATE_REQUEST_LOAD;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000543 }
Austin Schuh30537882014-02-18 01:07:23 -0800544 // If we are ready to load again,
545 shooter_loop_disable = true;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000546
Austin Schuh30537882014-02-18 01:07:23 -0800547 latch_piston_ = false;
548 brake_piston_ = false;
549 break;
550
551 case STATE_ESTOP:
552 // Totally lost, go to a safe state.
553 shooter_loop_disable = true;
554 latch_piston_ = true;
555 brake_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000556 break;
joe2d92e852014-01-25 14:31:24 -0800557 }
558
Ben Fredrickson7d980c22014-02-16 21:39:02 +0000559 if (apply_some_voltage) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000560 shooter_.Update(true);
Austin Schuhd34569d2014-02-18 20:26:38 -0800561 shooter_.ZeroPower();
Ben Fredrickson22c93322014-02-17 05:56:33 +0000562 if (output) output->voltage = 2.0;
Ben Fredrickson7d980c22014-02-16 21:39:02 +0000563 } else if (!shooter_loop_disable) {
Austin Schuh30537882014-02-18 01:07:23 -0800564 LOG(DEBUG, "Running the loop, goal is %f, position is %f\n",
565 shooter_.goal_position(), shooter_.absolute_position());
Austin Schuhd34569d2014-02-18 20:26:38 -0800566 if (!cap_goal) {
567 shooter_.set_max_voltage(12.0);
568 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000569 shooter_.Update(output == NULL);
Austin Schuhd34569d2014-02-18 20:26:38 -0800570 if (cap_goal) {
571 shooter_.CapGoal();
572 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000573 if (output) output->voltage = shooter_.voltage();
Ben Fredrickson7d980c22014-02-16 21:39:02 +0000574 } else {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000575 shooter_.Update(true);
Austin Schuhd34569d2014-02-18 20:26:38 -0800576 shooter_.ZeroPower();
Ben Fredrickson22c93322014-02-17 05:56:33 +0000577 if (output) output->voltage = 0.0;
Ben Fredricksonedf0e092014-02-16 10:46:50 +0000578 }
Ben Fredrickson7d980c22014-02-16 21:39:02 +0000579
Austin Schuh30537882014-02-18 01:07:23 -0800580 if (output) {
581 output->latch_piston = latch_piston_;
582 output->brake_piston = brake_piston_;
583 }
584
Austin Schuhd34569d2014-02-18 20:26:38 -0800585 status->done = ::std::abs(shooter_.absolute_position() -
586 PowerToPosition(goal->shot_power)) < 0.004;
Austin Schuh30537882014-02-18 01:07:23 -0800587
588 if (position) {
589 last_position_ = *position;
Ben Fredricksona9dcfa42014-02-23 02:05:59 +0000590 LOG(DEBUG, "pos > absolute: %f velocity: %f state= %d l= %d pp= %d, pd= %d "
591 "p= %d b=%d\n",
Austin Schuhbe1401f2014-02-18 03:18:41 -0800592 shooter_.absolute_position(), shooter_.absolute_velocity(),
Ben Fredricksona9dcfa42014-02-23 02:05:59 +0000593 state_, position->latch, position->pusher_proximal.current,
594 position->pusher_distal.current,
595 position->plunger, brake_piston_);
Austin Schuh30537882014-02-18 01:07:23 -0800596 }
597 if (position) {
598 last_distal_posedge_count_ = position->pusher_distal.posedge_count;
599 last_proximal_posedge_count_ = position->pusher_proximal.posedge_count;
600 }
joe2d92e852014-01-25 14:31:24 -0800601}
602
603} // namespace control_loops
604} // namespace frc971