blob: 8c56697a812dac4b8eb721a87b4134852f7cc385 [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) {
Austin Schuh06cbbf12014-02-22 02:07:31 -0800221 if (position->pusher_distal.current) {
Austin Schuh30537882014-02-18 01:07:23 -0800222 // We started on the sensor, back up until we are found.
223 // If the plunger is all the way back and not latched, it won't be
224 // there for long.
225 state_ = STATE_LOAD_BACKTRACK;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800226
227 // The plunger is already back and latched. Don't release it.
228 if (position->plunger && position->latch) {
229 latch_piston_ = true;
230 } else {
231 latch_piston_ = false;
232 }
233 } else if (position->plunger && position->latch) {
234 // The plunger is back and we are latched. We most likely got here
235 // from Initialize, in which case we want to 'load' again anyways to
236 // zero.
237 Load();
238 latch_piston_ = true;
Austin Schuh30537882014-02-18 01:07:23 -0800239 } else {
240 // Off the sensor, start loading.
241 Load();
242 latch_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000243 }
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000244 }
245
Austin Schuh30537882014-02-18 01:07:23 -0800246 // Hold our current position.
247 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
248 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000249 break;
250 case STATE_LOAD_BACKTRACK:
Austin Schuh30537882014-02-18 01:07:23 -0800251 // If we are here, then that means we started past the edge where we want
252 // to zero. Move backwards until we don't see the sensor anymore.
253 // The plunger is contacting the pusher (or will be shortly).
254
Austin Schuh30537882014-02-18 01:07:23 -0800255 if (!disabled) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000256 shooter_.SetGoalPosition(
Austin Schuhfaeee632014-02-18 01:24:05 -0800257 shooter_.goal_position() + values.shooter.zeroing_speed * dt,
258 values.shooter.zeroing_speed);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000259 }
Austin Schuhd34569d2014-02-18 20:26:38 -0800260 cap_goal = true;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800261 shooter_.set_max_voltage(4.0);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000262
Austin Schuh30537882014-02-18 01:07:23 -0800263 if (position) {
264 if (!position->pusher_distal.current) {
265 Load();
266 }
267 }
268
269 latch_piston_ = false;
270 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000271 break;
272 case STATE_LOAD:
Austin Schuh30537882014-02-18 01:07:23 -0800273 // If we are disabled right now, reset the timer.
274 if (disabled) {
275 Load();
276 // Latch defaults to true when disabled. Leave it latched until we have
277 // useful sensor data.
278 latch_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000279 }
Austin Schuh30537882014-02-18 01:07:23 -0800280 // Go to 0, which should be the latch position, or trigger a hall effect
281 // on the way. If we don't see edges where we are supposed to, the
282 // offset will be updated by code above.
283 shooter_.SetGoalPosition(0.0, 0.0);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000284
Austin Schuh30537882014-02-18 01:07:23 -0800285 if (position) {
286 // If we see a posedge on any of the hall effects,
287 if (position->pusher_proximal.posedge_count !=
288 last_proximal_posedge_count_) {
289 LOG(DEBUG, "Setting calibration using proximal sensor\n");
290 shooter_.SetCalibration(position->pusher_proximal.posedge_value,
291 values.shooter.pusher_proximal.upper_angle);
292 }
293 if (position->pusher_distal.posedge_count !=
294 last_distal_posedge_count_) {
295 LOG(DEBUG, "Setting calibration using distal sensor\n");
296 shooter_.SetCalibration(position->pusher_distal.posedge_value,
297 values.shooter.pusher_distal.upper_angle);
298 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000299
Austin Schuh30537882014-02-18 01:07:23 -0800300 // Latch if the plunger is far enough back to trigger the hall effect.
301 // This happens when the distal sensor is triggered.
Austin Schuh06cbbf12014-02-22 02:07:31 -0800302 latch_piston_ = position->pusher_distal.current || position->plunger;
Austin Schuh30537882014-02-18 01:07:23 -0800303
Austin Schuh06cbbf12014-02-22 02:07:31 -0800304 // Check if we are latched and back. Make sure the plunger is all the
305 // way back as well.
306 if (position->plunger && position->latch &&
307 position->pusher_distal.current) {
Austin Schuh30537882014-02-18 01:07:23 -0800308 state_ = STATE_PREPARE_SHOT;
309 } else if (position->plunger &&
310 ::std::abs(shooter_.absolute_position() -
311 shooter_.goal_position()) < 0.001) {
312 // We are at the goal, but not latched.
313 state_ = STATE_LOADING_PROBLEM;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800314 loading_problem_end_time_ = Time::Now() + kLoadProblemEndTimeout;
Austin Schuh30537882014-02-18 01:07:23 -0800315 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000316 }
Austin Schuh30537882014-02-18 01:07:23 -0800317 if (load_timeout_ < Time::Now()) {
318 if (position) {
319 if (!position->pusher_distal.current ||
320 !position->pusher_proximal.current) {
321 state_ = STATE_ESTOP;
322 }
323 }
324 } else if (goal->unload_requested) {
325 Unload();
326 }
327 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000328 break;
329 case STATE_LOADING_PROBLEM:
Austin Schuh30537882014-02-18 01:07:23 -0800330 if (disabled) {
331 Load();
332 }
333 // We got to the goal, but the latch hasn't registered as down. It might
334 // be stuck, or on it's way but not there yet.
Ben Fredrickson22c93322014-02-17 05:56:33 +0000335 if (Time::Now() > loading_problem_end_time_) {
Austin Schuh30537882014-02-18 01:07:23 -0800336 // Timeout by unloading.
337 Unload();
338 } else if (position && position->plunger && position->latch) {
339 // If both trigger, we are latched.
Ben Fredrickson22c93322014-02-17 05:56:33 +0000340 state_ = STATE_PREPARE_SHOT;
341 }
Austin Schuh30537882014-02-18 01:07:23 -0800342 // Move a bit further back to help it trigger.
343 // If the latch is slow due to the air flowing through the tubes or
344 // inertia, but is otherwise free, this won't have much time to do
345 // anything and is safe. Otherwise this gives us a bit more room to free
346 // up the latch.
347 shooter_.SetGoalPosition(values.shooter.lower_limit, 0.0);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000348 LOG(DEBUG, "Waiting on latch: plunger %d, latch: %d\n",
Austin Schuh60c56662014-02-17 14:37:19 -0800349 position->plunger, position->latch);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000350
Austin Schuh30537882014-02-18 01:07:23 -0800351 latch_piston_ = true;
352 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000353 break;
354 case STATE_PREPARE_SHOT:
Austin Schuh30537882014-02-18 01:07:23 -0800355 // Move the shooter to the shot power set point and then lock the brake.
356 // TODO(austin): Timeout. Low priority.
357
Ben Fredrickson22c93322014-02-17 05:56:33 +0000358 shooter_.SetGoalPosition(PowerToPosition(goal->shot_power), 0.0);
Austin Schuh30537882014-02-18 01:07:23 -0800359
360 LOG(DEBUG, "PDIFF: absolute_position: %.2f, pow: %.2f\n",
Austin Schuhbe1401f2014-02-18 03:18:41 -0800361 shooter_.absolute_position(), PowerToPosition(goal->shot_power));
Austin Schuh30537882014-02-18 01:07:23 -0800362 if (::std::abs(shooter_.absolute_position() -
Austin Schuhbe1401f2014-02-18 03:18:41 -0800363 PowerToPosition(goal->shot_power)) +
364 ::std::abs(shooter_.absolute_velocity()) <
365 0.001) {
Austin Schuh30537882014-02-18 01:07:23 -0800366 // We are there, set the brake and move on.
367 latch_piston_ = true;
368 brake_piston_ = true;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800369 shooter_brake_set_time_ = Time::Now() + kShooterBrakeSetTime;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000370 state_ = STATE_READY;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000371 } else {
Austin Schuh30537882014-02-18 01:07:23 -0800372 latch_piston_ = true;
373 brake_piston_ = false;
374 }
375 if (goal->unload_requested) {
376 Unload();
Ben Fredrickson22c93322014-02-17 05:56:33 +0000377 }
378 break;
379 case STATE_READY:
Austin Schuh30537882014-02-18 01:07:23 -0800380 LOG(DEBUG, "In ready\n");
Austin Schuhbe1401f2014-02-18 03:18:41 -0800381 // Wait until the brake is set, and a shot is requested or the shot power
382 // is changed.
383 if (::std::abs(shooter_.absolute_position() -
384 PowerToPosition(goal->shot_power)) > 0.002) {
385 // TODO(austin): Add a state to release the brake.
386
387 // TODO(austin): Do we want to set the brake here or after shooting?
388 // Depends on air usage.
389 LOG(DEBUG, "Preparing shot again.\n");
390 state_ = STATE_PREPARE_SHOT;
391 } else if (Time::Now() > shooter_brake_set_time_) {
392 // We have waited long enough for the brake to set, turn the shooter
393 // control loop off.
Ben Fredrickson22c93322014-02-17 05:56:33 +0000394 shooter_loop_disable = true;
Austin Schuh30537882014-02-18 01:07:23 -0800395 LOG(DEBUG, "Brake is now set\n");
396 if (goal->shot_requested && !disabled) {
397 LOG(DEBUG, "Shooting now\n");
398 shooter_loop_disable = true;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800399 prepare_fire_end_time_ = Time::Now() + kPrepareFireEndTime;
Austin Schuh30537882014-02-18 01:07:23 -0800400 apply_some_voltage = true;
401 state_ = STATE_PREPARE_FIRE;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000402 }
Austin Schuh30537882014-02-18 01:07:23 -0800403 } else {
404 LOG(DEBUG, "Nothing %d %d\n", goal->shot_requested, !disabled);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000405 }
Ben Fredricksona6d77542014-02-17 07:54:43 +0000406 shooter_.SetGoalPosition(PowerToPosition(goal->shot_power), 0.0);
407
Austin Schuh30537882014-02-18 01:07:23 -0800408 latch_piston_ = true;
409 brake_piston_ = true;
410
411 if (goal->unload_requested) {
412 Unload();
413 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000414 break;
Austin Schuh30537882014-02-18 01:07:23 -0800415
416 case STATE_PREPARE_FIRE:
417 // Apply a bit of voltage to bias the gears for a little bit of time, and
418 // then fire.
Ben Fredrickson22c93322014-02-17 05:56:33 +0000419 shooter_loop_disable = true;
Austin Schuh30537882014-02-18 01:07:23 -0800420 if (disabled) {
421 // If we are disabled, reset the backlash bias timer.
Austin Schuh06cbbf12014-02-22 02:07:31 -0800422 prepare_fire_end_time_ = Time::Now() + kPrepareFireEndTime;
Austin Schuh30537882014-02-18 01:07:23 -0800423 break;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000424 }
Austin Schuh30537882014-02-18 01:07:23 -0800425 if (Time::Now() > prepare_fire_end_time_) {
426 cycles_not_moved_ = 0;
427 firing_starting_position_ = shooter_.absolute_position();
Austin Schuh06cbbf12014-02-22 02:07:31 -0800428 shot_end_time_ = Time::Now() + kShotEndTimeout;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000429 state_ = STATE_FIRE;
Austin Schuhf5642a92014-02-18 01:42:32 -0800430 latch_piston_ = false;
Austin Schuh30537882014-02-18 01:07:23 -0800431 } else {
432 apply_some_voltage = true;
Austin Schuhf5642a92014-02-18 01:42:32 -0800433 latch_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000434 }
435
Austin Schuh30537882014-02-18 01:07:23 -0800436 brake_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000437 break;
Austin Schuh30537882014-02-18 01:07:23 -0800438
Ben Fredrickson22c93322014-02-17 05:56:33 +0000439 case STATE_FIRE:
Austin Schuh30537882014-02-18 01:07:23 -0800440 if (disabled) {
441 if (position) {
442 if (position->plunger) {
443 // If disabled and the plunger is still back there, reset the
444 // timeout.
Austin Schuh06cbbf12014-02-22 02:07:31 -0800445 shot_end_time_ = Time::Now() + kShotEndTimeout;
Austin Schuh30537882014-02-18 01:07:23 -0800446 }
447 }
448 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000449 shooter_loop_disable = true;
Austin Schuh30537882014-02-18 01:07:23 -0800450 // Count the number of contiguous cycles during which we haven't moved.
451 if (::std::abs(last_position_.position - shooter_.absolute_position()) <
452 0.0005) {
453 ++cycles_not_moved_;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000454 } else {
455 cycles_not_moved_ = 0;
456 }
Austin Schuh30537882014-02-18 01:07:23 -0800457
458 // If we have moved any amount since the start and the shooter has now
459 // been still for a couple cycles, the shot finished.
460 // Also move on if it times out.
461 if ((::std::abs(firing_starting_position_ -
462 shooter_.absolute_position()) > 0.0005 &&
463 cycles_not_moved_ > 3) ||
464 Time::Now() > shot_end_time_) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000465 state_ = STATE_REQUEST_LOAD;
466 }
Austin Schuh30537882014-02-18 01:07:23 -0800467 latch_piston_ = false;
468 brake_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000469 break;
470 case STATE_UNLOAD:
Austin Schuh30537882014-02-18 01:07:23 -0800471 // Reset the timeouts.
472 if (disabled) Unload();
473
474 // If it is latched and the plunger is back, move the pusher back to catch
475 // the plunger.
Austin Schuhf84a1302014-02-19 00:23:30 -0800476 bool all_back;
477 if (position) {
478 all_back = position->plunger && position->latch;
479 } else {
480 all_back = last_position_.plunger && last_position_.latch;
481 }
482
483 if (all_back) {
Austin Schuh30537882014-02-18 01:07:23 -0800484 // Pull back to 0, 0.
485 shooter_.SetGoalPosition(0.0, 0.0);
486 if (shooter_.absolute_position() < 0.005) {
487 // When we are close enough, 'fire'.
488 latch_piston_ = false;
489 } else {
490 latch_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000491 }
492 } else {
Austin Schuh30537882014-02-18 01:07:23 -0800493 // The plunger isn't all the way back, or it is and it is unlatched, so
494 // we can now unload.
495 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
496 latch_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000497 state_ = STATE_UNLOAD_MOVE;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800498 unload_timeout_ = Time::Now() + kUnloadTimeout;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000499 }
500
Austin Schuh30537882014-02-18 01:07:23 -0800501 if (Time::Now() > unload_timeout_) {
502 // We have been stuck trying to unload for way too long, give up and
503 // turn everything off.
504 state_ = STATE_ESTOP;
505 }
506
507 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000508 break;
Austin Schuh30537882014-02-18 01:07:23 -0800509 case STATE_UNLOAD_MOVE: {
510 if (disabled) {
Austin Schuh06cbbf12014-02-22 02:07:31 -0800511 unload_timeout_ = Time::Now() + kUnloadTimeout;
Austin Schuh30537882014-02-18 01:07:23 -0800512 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
513 }
Austin Schuhd34569d2014-02-18 20:26:38 -0800514 cap_goal = true;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800515 shooter_.set_max_voltage(5.0);
Austin Schuh30537882014-02-18 01:07:23 -0800516
517 // Slowly move back until we hit the upper limit.
Austin Schuhd34569d2014-02-18 20:26:38 -0800518 // If we were at the limit last cycle, we are done unloading.
519 // This is because if we saturate, we might hit the limit before we are
520 // actually there.
521 if (shooter_.goal_position() >= values.shooter.upper_limit) {
Austin Schuh30537882014-02-18 01:07:23 -0800522 shooter_.SetGoalPosition(values.shooter.upper_limit, 0.0);
523 // We don't want the loop fighting the spring when we are unloaded.
524 // Turn it off.
525 shooter_loop_disable = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000526 state_ = STATE_READY_UNLOAD;
527 } else {
Austin Schuhd34569d2014-02-18 20:26:38 -0800528 shooter_.SetGoalPosition(
529 ::std::min(
530 values.shooter.upper_limit,
531 shooter_.goal_position() + values.shooter.zeroing_speed * dt),
532 values.shooter.zeroing_speed);
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000533 }
534
Austin Schuh30537882014-02-18 01:07:23 -0800535 latch_piston_ = false;
536 brake_piston_ = false;
537 } break;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000538 case STATE_READY_UNLOAD:
Austin Schuh30537882014-02-18 01:07:23 -0800539 if (goal->load_requested) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000540 state_ = STATE_REQUEST_LOAD;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000541 }
Austin Schuh30537882014-02-18 01:07:23 -0800542 // If we are ready to load again,
543 shooter_loop_disable = true;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000544
Austin Schuh30537882014-02-18 01:07:23 -0800545 latch_piston_ = false;
546 brake_piston_ = false;
547 break;
548
549 case STATE_ESTOP:
550 // Totally lost, go to a safe state.
551 shooter_loop_disable = true;
552 latch_piston_ = true;
553 brake_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000554 break;
joe2d92e852014-01-25 14:31:24 -0800555 }
556
Ben Fredrickson7d980c22014-02-16 21:39:02 +0000557 if (apply_some_voltage) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000558 shooter_.Update(true);
Austin Schuhd34569d2014-02-18 20:26:38 -0800559 shooter_.ZeroPower();
Ben Fredrickson22c93322014-02-17 05:56:33 +0000560 if (output) output->voltage = 2.0;
Ben Fredrickson7d980c22014-02-16 21:39:02 +0000561 } else if (!shooter_loop_disable) {
Austin Schuh30537882014-02-18 01:07:23 -0800562 LOG(DEBUG, "Running the loop, goal is %f, position is %f\n",
563 shooter_.goal_position(), shooter_.absolute_position());
Austin Schuhd34569d2014-02-18 20:26:38 -0800564 if (!cap_goal) {
565 shooter_.set_max_voltage(12.0);
566 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000567 shooter_.Update(output == NULL);
Austin Schuhd34569d2014-02-18 20:26:38 -0800568 if (cap_goal) {
569 shooter_.CapGoal();
570 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000571 if (output) output->voltage = shooter_.voltage();
Ben Fredrickson7d980c22014-02-16 21:39:02 +0000572 } else {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000573 shooter_.Update(true);
Austin Schuhd34569d2014-02-18 20:26:38 -0800574 shooter_.ZeroPower();
Ben Fredrickson22c93322014-02-17 05:56:33 +0000575 if (output) output->voltage = 0.0;
Ben Fredricksonedf0e092014-02-16 10:46:50 +0000576 }
Ben Fredrickson7d980c22014-02-16 21:39:02 +0000577
Austin Schuh30537882014-02-18 01:07:23 -0800578 if (output) {
579 output->latch_piston = latch_piston_;
580 output->brake_piston = brake_piston_;
581 }
582
Austin Schuhd34569d2014-02-18 20:26:38 -0800583 status->done = ::std::abs(shooter_.absolute_position() -
584 PowerToPosition(goal->shot_power)) < 0.004;
Austin Schuh30537882014-02-18 01:07:23 -0800585
586 if (position) {
587 last_position_ = *position;
Ben Fredricksona9dcfa42014-02-23 02:05:59 +0000588 LOG(DEBUG, "pos > absolute: %f velocity: %f state= %d l= %d pp= %d, pd= %d "
589 "p= %d b=%d\n",
Austin Schuhbe1401f2014-02-18 03:18:41 -0800590 shooter_.absolute_position(), shooter_.absolute_velocity(),
Ben Fredricksona9dcfa42014-02-23 02:05:59 +0000591 state_, position->latch, position->pusher_proximal.current,
592 position->pusher_distal.current,
593 position->plunger, brake_piston_);
Austin Schuh30537882014-02-18 01:07:23 -0800594 }
595 if (position) {
596 last_distal_posedge_count_ = position->pusher_distal.posedge_count;
597 last_proximal_posedge_count_ = position->pusher_proximal.posedge_count;
598 }
joe2d92e852014-01-25 14:31:24 -0800599}
600
601} // namespace control_loops
602} // namespace frc971