blob: 732b8823cb9f7e5b432a92db28c27a5afb6417d6 [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"
8#include "aos/common/logging/logging.h"
9
10#include "frc971/constants.h"
joe93778a62014-02-15 13:22:14 -080011#include "frc971/control_loops/shooter/shooter_motor_plant.h"
joe2d92e852014-01-25 14:31:24 -080012
13namespace frc971 {
14namespace control_loops {
15
Ben Fredricksonedf0e092014-02-16 10:46:50 +000016using ::aos::time::Time;
Ben Fredrickson22c93322014-02-17 05:56:33 +000017
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000018void ZeroedStateFeedbackLoop::CapU() {
19 const double old_voltage = voltage_;
20 voltage_ += U(0, 0);
21
22 uncapped_voltage_ = voltage_;
23
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000024 // Make sure that reality and the observer can't get too far off. There is a
25 // delay by one cycle between the applied voltage and X_hat(2, 0), so compare
26 // against last cycle's voltage.
Austin Schuhbe1401f2014-02-18 03:18:41 -080027 if (X_hat(2, 0) > last_voltage_ + 4.0) {
28 voltage_ -= X_hat(2, 0) - (last_voltage_ + 4.0);
Brian Silverman101b9642014-03-08 12:45:16 -080029 LOG(DEBUG, "Capping due to runaway\n");
Austin Schuhbe1401f2014-02-18 03:18:41 -080030 } else if (X_hat(2, 0) < last_voltage_ - 4.0) {
31 voltage_ += X_hat(2, 0) - (last_voltage_ - 4.0);
Brian Silverman101b9642014-03-08 12:45:16 -080032 LOG(DEBUG, "Capping due to runaway\n");
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000033 }
34
Austin Schuhd34569d2014-02-18 20:26:38 -080035 voltage_ = std::min(max_voltage_, voltage_);
36 voltage_ = std::max(-max_voltage_, voltage_);
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000037 U(0, 0) = voltage_ - old_voltage;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000038
Brian Silverman101b9642014-03-08 12:45:16 -080039 LOG(DEBUG, "X_hat is %f, applied is %f\n", X_hat(2, 0), voltage_);
Austin Schuhbe1401f2014-02-18 03:18:41 -080040
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000041 last_voltage_ = voltage_;
Austin Schuhd34569d2014-02-18 20:26:38 -080042 capped_goal_ = false;
43}
44
45void ZeroedStateFeedbackLoop::CapGoal() {
46 if (uncapped_voltage() > max_voltage_) {
47 double dx;
48 if (controller_index() == 0) {
49 dx = (uncapped_voltage() - max_voltage_) /
50 (K(0, 0) - A(1, 0) * K(0, 2) / A(1, 2));
51 R(0, 0) -= dx;
52 R(2, 0) -= -A(1, 0) / A(1, 2) * dx;
53 } else {
54 dx = (uncapped_voltage() - max_voltage_) / K(0, 0);
55 R(0, 0) -= dx;
56 }
57 capped_goal_ = true;
58 LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx);
59 } else if (uncapped_voltage() < -max_voltage_) {
60 double dx;
61 if (controller_index() == 0) {
62 dx = (uncapped_voltage() + max_voltage_) /
63 (K(0, 0) - A(1, 0) * K(0, 2) / A(1, 2));
64 R(0, 0) -= dx;
65 R(2, 0) -= -A(1, 0) / A(1, 2) * dx;
66 } else {
67 dx = (uncapped_voltage() + max_voltage_) / K(0, 0);
68 R(0, 0) -= dx;
69 }
70 capped_goal_ = true;
71 LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx);
72 } else {
73 capped_goal_ = false;
74 }
75}
76
77void ZeroedStateFeedbackLoop::RecalculatePowerGoal() {
78 if (controller_index() == 0) {
79 R(2, 0) = (-A(1, 0) / A(1, 2) * R(0, 0) - A(1, 1) / A(1, 2) * R(1, 0));
80 } else {
81 R(2, 0) = -A(1, 1) / A(1, 2) * R(1, 0);
82 }
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000083}
84
Austin Schuh30537882014-02-18 01:07:23 -080085void ZeroedStateFeedbackLoop::SetCalibration(double encoder_val,
86 double known_position) {
87 LOG(INFO, "Setting calibration such that %f -> %f\n", encoder_val,
88 known_position);
89 LOG(INFO, "Position was %f\n", absolute_position());
90 double previous_offset = offset_;
91 offset_ = known_position - encoder_val;
92 double doffset = offset_ - previous_offset;
93 LOG(INFO, "Changing offset from %f to %f\n", previous_offset, offset_);
94 X_hat(0, 0) += doffset;
95 // Offset our measurements because the offset is baked into them.
96 Y_(0, 0) += doffset;
97 // Offset the goal so we don't move.
98 R(0, 0) += doffset;
Austin Schuhd34569d2014-02-18 20:26:38 -080099 if (controller_index() == 0) {
100 R(2, 0) += -A(1, 0) / A(1, 2) * (doffset);
101 }
Austin Schuh30537882014-02-18 01:07:23 -0800102 LOG(INFO, "Validation: position is %f\n", absolute_position());
103}
104
Ben Fredricksonedf0e092014-02-16 10:46:50 +0000105ShooterMotor::ShooterMotor(control_loops::ShooterGroup *my_shooter)
106 : aos::control_loops::ControlLoop<control_loops::ShooterGroup>(my_shooter),
107 shooter_(MakeShooterLoop()),
Ben Fredrickson22c93322014-02-17 05:56:33 +0000108 state_(STATE_INITIALIZE),
109 loading_problem_end_time_(0, 0),
Austin Schuh30537882014-02-18 01:07:23 -0800110 load_timeout_(0, 0),
Ben Fredrickson22c93322014-02-17 05:56:33 +0000111 shooter_brake_set_time_(0, 0),
Austin Schuh30537882014-02-18 01:07:23 -0800112 unload_timeout_(0, 0),
Ben Fredrickson22c93322014-02-17 05:56:33 +0000113 shot_end_time_(0, 0),
Austin Schuh4edad872014-03-02 11:56:12 -0800114 cycles_not_moved_(0),
Austin Schuhd30d7382014-03-02 21:15:38 -0800115 shot_count_(0),
116 zeroed_(false),
117 distal_posedge_validation_cycles_left_(0),
Austin Schuh2e976812014-03-05 19:56:58 -0800118 proximal_posedge_validation_cycles_left_(0),
119 last_distal_current_(true),
120 last_proximal_current_(true) {}
Austin Schuh30537882014-02-18 01:07:23 -0800121
122double ShooterMotor::PowerToPosition(double power) {
Austin Schuh30537882014-02-18 01:07:23 -0800123 const frc971::constants::Values &values = constants::GetValues();
Ben Fredricksonda334d12014-02-23 09:09:23 +0000124 double maxpower = 0.5 * kSpringConstant *
125 (kMaxExtension * kMaxExtension -
126 (kMaxExtension - values.shooter.upper_limit) *
127 (kMaxExtension - values.shooter.upper_limit));
128 if (power < 0) {
Ben Fredrickson7c9314f2014-02-23 09:26:17 +0000129 //LOG(ERROR, "Power too low giving minimum (%f) (%f).\n", power,
130 // 0.0);
Ben Fredricksonda334d12014-02-23 09:09:23 +0000131 power = 0;
132 } else if (power > maxpower) {
Ben Fredrickson7c9314f2014-02-23 09:26:17 +0000133 //LOG(ERROR, "Power too high giving maximum (%f) (%f).\n", power,
134 // maxpower);
Ben Fredricksonda334d12014-02-23 09:09:23 +0000135 power = maxpower;
136 }
137
138 double mp = kMaxExtension * kMaxExtension - (power + power) / kSpringConstant;
139 double new_pos = 0.10;
140 if (mp < 0) {
141 LOG(ERROR,
142 "Power calculation has negative number before square root (%f).\n", mp);
143 } else {
144 new_pos = kMaxExtension - ::std::sqrt(mp);
145 }
146
147 new_pos = ::std::min(::std::max(new_pos, values.shooter.lower_limit),
Austin Schuh30537882014-02-18 01:07:23 -0800148 values.shooter.upper_limit);
149 return new_pos;
150}
Ben Fredrickson1e512ff2014-02-15 21:36:52 +0000151
James Kuszmauld29689f2014-03-02 13:06:54 -0800152double ShooterMotor::PositionToPower(double position) {
153 double power = kSpringConstant * position * (kMaxExtension - position / 2.0);
154 return power;
155}
156
Ben Fredrickson1e512ff2014-02-15 21:36:52 +0000157// Positive is out, and positive power is out.
joe2d92e852014-01-25 14:31:24 -0800158void ShooterMotor::RunIteration(
Ben Fredricksonedf0e092014-02-16 10:46:50 +0000159 const control_loops::ShooterGroup::Goal *goal,
160 const control_loops::ShooterGroup::Position *position,
161 control_loops::ShooterGroup::Output *output,
Ben Fredrickson22c93322014-02-17 05:56:33 +0000162 control_loops::ShooterGroup::Status *status) {
Ben Fredrickson1e512ff2014-02-15 21:36:52 +0000163 constexpr double dt = 0.01;
164
Ben Fredrickson61893d52014-03-02 09:43:23 +0000165 if (::std::isnan(goal->shot_power)) {
166 state_ = STATE_ESTOP;
167 LOG(ERROR, "Got a shot power of NAN.\n");
168 }
169
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000170 // we must always have these or we have issues.
171 if (goal == NULL || status == NULL) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000172 if (output) output->voltage = 0;
173 LOG(ERROR, "Thought I would just check for null and die.\n");
174 return;
175 }
James Kuszmaul4abaf482014-02-26 21:16:35 -0800176 status->ready = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000177
Austin Schuh30537882014-02-18 01:07:23 -0800178 if (reset()) {
179 state_ = STATE_INITIALIZE;
Austin Schuh2e976812014-03-05 19:56:58 -0800180 last_distal_current_ = position->pusher_distal.current;
181 last_proximal_current_ = position->pusher_proximal.current;
Austin Schuh30537882014-02-18 01:07:23 -0800182 }
183 if (position) {
184 shooter_.CorrectPosition(position->position);
Ben Fredrickson1e512ff2014-02-15 21:36:52 +0000185 }
joe2d92e852014-01-25 14:31:24 -0800186
187 // Disable the motors now so that all early returns will return with the
188 // motors disabled.
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000189 if (output) output->voltage = 0;
joe2d92e852014-01-25 14:31:24 -0800190
Ben Fredrickson1e512ff2014-02-15 21:36:52 +0000191 const frc971::constants::Values &values = constants::GetValues();
192
Brian Silvermanaae236a2014-02-17 01:49:39 -0800193 // Don't even let the control loops run.
Ben Fredricksonedf0e092014-02-16 10:46:50 +0000194 bool shooter_loop_disable = false;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000195
Austin Schuh30537882014-02-18 01:07:23 -0800196 const bool disabled = !::aos::robot_state->enabled;
Austin Schuhd34569d2014-02-18 20:26:38 -0800197 // If true, move the goal if we saturate.
198 bool cap_goal = false;
199
200 // TODO(austin): Move the offset if we see or don't see a hall effect when we
201 // expect to see one.
202 // Probably not needed yet.
203
204 if (position) {
205 int last_controller_index = shooter_.controller_index();
206 if (position->plunger && position->latch) {
207 // Use the controller without the spring if the latch is set and the
208 // plunger is back
209 shooter_.set_controller_index(1);
210 } else {
211 // Otherwise use the controller with the spring.
212 shooter_.set_controller_index(0);
213 }
214 if (shooter_.controller_index() != last_controller_index) {
215 shooter_.RecalculatePowerGoal();
216 }
217 }
Austin Schuh30537882014-02-18 01:07:23 -0800218
Ben Fredrickson1e512ff2014-02-15 21:36:52 +0000219 switch (state_) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000220 case STATE_INITIALIZE:
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000221 if (position) {
Austin Schuh30537882014-02-18 01:07:23 -0800222 // Reinitialize the internal filter state.
223 shooter_.InitializeState(position->position);
Austin Schuh30537882014-02-18 01:07:23 -0800224
225 // Start off with the assumption that we are at the value
226 // futhest back given our sensors.
227 if (position->pusher_distal.current) {
228 shooter_.SetCalibration(position->position,
229 values.shooter.pusher_distal.lower_angle);
230 } else if (position->pusher_proximal.current) {
231 shooter_.SetCalibration(position->position,
Austin Schuhd34569d2014-02-18 20:26:38 -0800232 values.shooter.pusher_proximal.upper_angle);
Austin Schuh30537882014-02-18 01:07:23 -0800233 } else {
234 shooter_.SetCalibration(position->position,
235 values.shooter.upper_limit);
236 }
237
238 state_ = STATE_REQUEST_LOAD;
239
240 // Go to the current position.
241 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
242 // If the plunger is all the way back, we want to be latched.
243 latch_piston_ = position->plunger;
244 brake_piston_ = false;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000245 } else {
Austin Schuh30537882014-02-18 01:07:23 -0800246 // If we can't start yet because we don't know where we are, set the
247 // latch and brake to their defaults.
248 latch_piston_ = true;
249 brake_piston_ = true;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000250 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000251 break;
252 case STATE_REQUEST_LOAD:
Austin Schuh30537882014-02-18 01:07:23 -0800253 if (position) {
Austin Schuhd30d7382014-03-02 21:15:38 -0800254 zeroed_ = false;
Ben Fredrickson26a0dee2014-02-23 04:38:23 +0000255 if (position->pusher_distal.current ||
256 position->pusher_proximal.current) {
Austin Schuh30537882014-02-18 01:07:23 -0800257 // We started on the sensor, back up until we are found.
258 // If the plunger is all the way back and not latched, it won't be
259 // there for long.
260 state_ = STATE_LOAD_BACKTRACK;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800261
262 // The plunger is already back and latched. Don't release it.
263 if (position->plunger && position->latch) {
264 latch_piston_ = true;
265 } else {
266 latch_piston_ = false;
267 }
268 } else if (position->plunger && position->latch) {
269 // The plunger is back and we are latched. We most likely got here
270 // from Initialize, in which case we want to 'load' again anyways to
271 // zero.
272 Load();
273 latch_piston_ = true;
Austin Schuh30537882014-02-18 01:07:23 -0800274 } else {
275 // Off the sensor, start loading.
276 Load();
277 latch_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000278 }
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000279 }
280
Austin Schuh30537882014-02-18 01:07:23 -0800281 // Hold our current position.
282 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
283 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000284 break;
285 case STATE_LOAD_BACKTRACK:
Austin Schuh30537882014-02-18 01:07:23 -0800286 // If we are here, then that means we started past the edge where we want
287 // to zero. Move backwards until we don't see the sensor anymore.
288 // The plunger is contacting the pusher (or will be shortly).
289
Austin Schuh30537882014-02-18 01:07:23 -0800290 if (!disabled) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000291 shooter_.SetGoalPosition(
Austin Schuhfaeee632014-02-18 01:24:05 -0800292 shooter_.goal_position() + values.shooter.zeroing_speed * dt,
293 values.shooter.zeroing_speed);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000294 }
Austin Schuhd34569d2014-02-18 20:26:38 -0800295 cap_goal = true;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800296 shooter_.set_max_voltage(4.0);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000297
Austin Schuh30537882014-02-18 01:07:23 -0800298 if (position) {
Ben Fredrickson26a0dee2014-02-23 04:38:23 +0000299 if (!position->pusher_distal.current &&
300 !position->pusher_proximal.current) {
Austin Schuh30537882014-02-18 01:07:23 -0800301 Load();
302 }
Austin Schuh6b428602014-02-22 21:02:00 -0800303 latch_piston_ = position->plunger;
Austin Schuh30537882014-02-18 01:07:23 -0800304 }
305
Austin Schuh30537882014-02-18 01:07:23 -0800306 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000307 break;
308 case STATE_LOAD:
Austin Schuh30537882014-02-18 01:07:23 -0800309 // If we are disabled right now, reset the timer.
310 if (disabled) {
311 Load();
312 // Latch defaults to true when disabled. Leave it latched until we have
313 // useful sensor data.
314 latch_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000315 }
Austin Schuh30537882014-02-18 01:07:23 -0800316 // Go to 0, which should be the latch position, or trigger a hall effect
317 // on the way. If we don't see edges where we are supposed to, the
318 // offset will be updated by code above.
319 shooter_.SetGoalPosition(0.0, 0.0);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000320
Austin Schuh30537882014-02-18 01:07:23 -0800321 if (position) {
Austin Schuhd30d7382014-03-02 21:15:38 -0800322 // TODO(austin): Validate that this is the right edge.
Austin Schuh30537882014-02-18 01:07:23 -0800323 // If we see a posedge on any of the hall effects,
324 if (position->pusher_proximal.posedge_count !=
Austin Schuh2e976812014-03-05 19:56:58 -0800325 last_proximal_posedge_count_ &&
326 !last_proximal_current_) {
Austin Schuhd30d7382014-03-02 21:15:38 -0800327 proximal_posedge_validation_cycles_left_ = 2;
Austin Schuh30537882014-02-18 01:07:23 -0800328 }
Austin Schuhd30d7382014-03-02 21:15:38 -0800329 if (proximal_posedge_validation_cycles_left_ > 0) {
330 if (position->pusher_proximal.current) {
331 --proximal_posedge_validation_cycles_left_;
332 if (proximal_posedge_validation_cycles_left_ == 0) {
333 shooter_.SetCalibration(
334 position->pusher_proximal.posedge_value,
335 values.shooter.pusher_proximal.upper_angle);
336
337 LOG(DEBUG, "Setting calibration using proximal sensor\n");
338 zeroed_ = true;
339 }
340 } else {
341 proximal_posedge_validation_cycles_left_ = 0;
342 }
343 }
344
Austin Schuh30537882014-02-18 01:07:23 -0800345 if (position->pusher_distal.posedge_count !=
Austin Schuh2e976812014-03-05 19:56:58 -0800346 last_distal_posedge_count_ &&
347 !last_distal_current_) {
Austin Schuhd30d7382014-03-02 21:15:38 -0800348 distal_posedge_validation_cycles_left_ = 2;
349 }
350 if (distal_posedge_validation_cycles_left_ > 0) {
351 if (position->pusher_distal.current) {
352 --distal_posedge_validation_cycles_left_;
353 if (distal_posedge_validation_cycles_left_ == 0) {
354 shooter_.SetCalibration(
355 position->pusher_distal.posedge_value,
356 values.shooter.pusher_distal.upper_angle);
357
358 LOG(DEBUG, "Setting calibration using distal sensor\n");
359 zeroed_ = true;
360 }
361 } else {
362 distal_posedge_validation_cycles_left_ = 0;
363 }
Austin Schuh30537882014-02-18 01:07:23 -0800364 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000365
Austin Schuh30537882014-02-18 01:07:23 -0800366 // Latch if the plunger is far enough back to trigger the hall effect.
367 // This happens when the distal sensor is triggered.
Austin Schuh06cbbf12014-02-22 02:07:31 -0800368 latch_piston_ = position->pusher_distal.current || position->plunger;
Austin Schuh30537882014-02-18 01:07:23 -0800369
Austin Schuh06cbbf12014-02-22 02:07:31 -0800370 // Check if we are latched and back. Make sure the plunger is all the
371 // way back as well.
372 if (position->plunger && position->latch &&
373 position->pusher_distal.current) {
Austin Schuhd30d7382014-03-02 21:15:38 -0800374 if (!zeroed_) {
375 state_ = STATE_REQUEST_LOAD;
376 } else {
377 state_ = STATE_PREPARE_SHOT;
378 }
Austin Schuh30537882014-02-18 01:07:23 -0800379 } else if (position->plunger &&
380 ::std::abs(shooter_.absolute_position() -
381 shooter_.goal_position()) < 0.001) {
382 // We are at the goal, but not latched.
383 state_ = STATE_LOADING_PROBLEM;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800384 loading_problem_end_time_ = Time::Now() + kLoadProblemEndTimeout;
Austin Schuh30537882014-02-18 01:07:23 -0800385 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000386 }
Austin Schuh30537882014-02-18 01:07:23 -0800387 if (load_timeout_ < Time::Now()) {
388 if (position) {
389 if (!position->pusher_distal.current ||
390 !position->pusher_proximal.current) {
391 state_ = STATE_ESTOP;
392 }
393 }
394 } else if (goal->unload_requested) {
395 Unload();
396 }
397 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000398 break;
399 case STATE_LOADING_PROBLEM:
Austin Schuh30537882014-02-18 01:07:23 -0800400 if (disabled) {
Austin Schuhd30d7382014-03-02 21:15:38 -0800401 state_ = STATE_REQUEST_LOAD;
402 break;
Austin Schuh30537882014-02-18 01:07:23 -0800403 }
404 // We got to the goal, but the latch hasn't registered as down. It might
405 // be stuck, or on it's way but not there yet.
Ben Fredrickson22c93322014-02-17 05:56:33 +0000406 if (Time::Now() > loading_problem_end_time_) {
Austin Schuh30537882014-02-18 01:07:23 -0800407 // Timeout by unloading.
408 Unload();
409 } else if (position && position->plunger && position->latch) {
410 // If both trigger, we are latched.
Ben Fredrickson22c93322014-02-17 05:56:33 +0000411 state_ = STATE_PREPARE_SHOT;
412 }
Austin Schuh30537882014-02-18 01:07:23 -0800413 // Move a bit further back to help it trigger.
414 // If the latch is slow due to the air flowing through the tubes or
415 // inertia, but is otherwise free, this won't have much time to do
416 // anything and is safe. Otherwise this gives us a bit more room to free
417 // up the latch.
418 shooter_.SetGoalPosition(values.shooter.lower_limit, 0.0);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000419 LOG(DEBUG, "Waiting on latch: plunger %d, latch: %d\n",
Austin Schuh60c56662014-02-17 14:37:19 -0800420 position->plunger, position->latch);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000421
Austin Schuh30537882014-02-18 01:07:23 -0800422 latch_piston_ = true;
423 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000424 break;
425 case STATE_PREPARE_SHOT:
Austin Schuh30537882014-02-18 01:07:23 -0800426 // Move the shooter to the shot power set point and then lock the brake.
427 // TODO(austin): Timeout. Low priority.
428
Ben Fredrickson22c93322014-02-17 05:56:33 +0000429 shooter_.SetGoalPosition(PowerToPosition(goal->shot_power), 0.0);
Austin Schuh30537882014-02-18 01:07:23 -0800430
431 LOG(DEBUG, "PDIFF: absolute_position: %.2f, pow: %.2f\n",
Austin Schuhbe1401f2014-02-18 03:18:41 -0800432 shooter_.absolute_position(), PowerToPosition(goal->shot_power));
Austin Schuh30537882014-02-18 01:07:23 -0800433 if (::std::abs(shooter_.absolute_position() -
Austin Schuhbe1401f2014-02-18 03:18:41 -0800434 PowerToPosition(goal->shot_power)) +
435 ::std::abs(shooter_.absolute_velocity()) <
436 0.001) {
Austin Schuh30537882014-02-18 01:07:23 -0800437 // We are there, set the brake and move on.
438 latch_piston_ = true;
439 brake_piston_ = true;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800440 shooter_brake_set_time_ = Time::Now() + kShooterBrakeSetTime;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000441 state_ = STATE_READY;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000442 } else {
Austin Schuh30537882014-02-18 01:07:23 -0800443 latch_piston_ = true;
444 brake_piston_ = false;
445 }
446 if (goal->unload_requested) {
447 Unload();
Ben Fredrickson22c93322014-02-17 05:56:33 +0000448 }
449 break;
450 case STATE_READY:
Austin Schuh30537882014-02-18 01:07:23 -0800451 LOG(DEBUG, "In ready\n");
Austin Schuhbe1401f2014-02-18 03:18:41 -0800452 // Wait until the brake is set, and a shot is requested or the shot power
453 // is changed.
Austin Schuh6b428602014-02-22 21:02:00 -0800454 if (Time::Now() > shooter_brake_set_time_) {
James Kuszmaul4abaf482014-02-26 21:16:35 -0800455 status->ready = true;
Austin Schuhbe1401f2014-02-18 03:18:41 -0800456 // We have waited long enough for the brake to set, turn the shooter
457 // control loop off.
Ben Fredrickson22c93322014-02-17 05:56:33 +0000458 shooter_loop_disable = true;
Austin Schuh30537882014-02-18 01:07:23 -0800459 LOG(DEBUG, "Brake is now set\n");
460 if (goal->shot_requested && !disabled) {
461 LOG(DEBUG, "Shooting now\n");
462 shooter_loop_disable = true;
James Kuszmaul7290a942014-02-26 20:04:13 -0800463 shot_end_time_ = Time::Now() + kShotEndTimeout;
464 firing_starting_position_ = shooter_.absolute_position();
465 state_ = STATE_FIRE;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000466 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000467 }
Austin Schuh6b428602014-02-22 21:02:00 -0800468 if (state_ == STATE_READY &&
469 ::std::abs(shooter_.absolute_position() -
470 PowerToPosition(goal->shot_power)) > 0.002) {
471 // TODO(austin): Add a state to release the brake.
472
473 // TODO(austin): Do we want to set the brake here or after shooting?
474 // Depends on air usage.
James Kuszmaul4abaf482014-02-26 21:16:35 -0800475 status->ready = false;
Austin Schuh6b428602014-02-22 21:02:00 -0800476 LOG(DEBUG, "Preparing shot again.\n");
477 state_ = STATE_PREPARE_SHOT;
478 }
479
Ben Fredricksona6d77542014-02-17 07:54:43 +0000480 shooter_.SetGoalPosition(PowerToPosition(goal->shot_power), 0.0);
481
Austin Schuh30537882014-02-18 01:07:23 -0800482 latch_piston_ = true;
483 brake_piston_ = true;
484
485 if (goal->unload_requested) {
486 Unload();
487 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000488 break;
Austin Schuh30537882014-02-18 01:07:23 -0800489
Ben Fredrickson22c93322014-02-17 05:56:33 +0000490 case STATE_FIRE:
Austin Schuh30537882014-02-18 01:07:23 -0800491 if (disabled) {
492 if (position) {
493 if (position->plunger) {
494 // If disabled and the plunger is still back there, reset the
495 // timeout.
Austin Schuh06cbbf12014-02-22 02:07:31 -0800496 shot_end_time_ = Time::Now() + kShotEndTimeout;
Austin Schuh30537882014-02-18 01:07:23 -0800497 }
498 }
499 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000500 shooter_loop_disable = true;
Austin Schuh30537882014-02-18 01:07:23 -0800501 // Count the number of contiguous cycles during which we haven't moved.
502 if (::std::abs(last_position_.position - shooter_.absolute_position()) <
503 0.0005) {
504 ++cycles_not_moved_;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000505 } else {
506 cycles_not_moved_ = 0;
507 }
Austin Schuh30537882014-02-18 01:07:23 -0800508
509 // If we have moved any amount since the start and the shooter has now
510 // been still for a couple cycles, the shot finished.
511 // Also move on if it times out.
512 if ((::std::abs(firing_starting_position_ -
513 shooter_.absolute_position()) > 0.0005 &&
514 cycles_not_moved_ > 3) ||
515 Time::Now() > shot_end_time_) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000516 state_ = STATE_REQUEST_LOAD;
Austin Schuh4edad872014-03-02 11:56:12 -0800517 ++shot_count_;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000518 }
Austin Schuh30537882014-02-18 01:07:23 -0800519 latch_piston_ = false;
520 brake_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000521 break;
522 case STATE_UNLOAD:
Austin Schuh30537882014-02-18 01:07:23 -0800523 // Reset the timeouts.
524 if (disabled) Unload();
525
526 // If it is latched and the plunger is back, move the pusher back to catch
527 // the plunger.
Austin Schuhf84a1302014-02-19 00:23:30 -0800528 bool all_back;
529 if (position) {
530 all_back = position->plunger && position->latch;
531 } else {
532 all_back = last_position_.plunger && last_position_.latch;
533 }
534
535 if (all_back) {
Austin Schuh30537882014-02-18 01:07:23 -0800536 // Pull back to 0, 0.
537 shooter_.SetGoalPosition(0.0, 0.0);
538 if (shooter_.absolute_position() < 0.005) {
539 // When we are close enough, 'fire'.
540 latch_piston_ = false;
541 } else {
542 latch_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000543 }
544 } else {
Austin Schuh30537882014-02-18 01:07:23 -0800545 // The plunger isn't all the way back, or it is and it is unlatched, so
546 // we can now unload.
547 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
548 latch_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000549 state_ = STATE_UNLOAD_MOVE;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800550 unload_timeout_ = Time::Now() + kUnloadTimeout;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000551 }
552
Austin Schuh30537882014-02-18 01:07:23 -0800553 if (Time::Now() > unload_timeout_) {
554 // We have been stuck trying to unload for way too long, give up and
555 // turn everything off.
556 state_ = STATE_ESTOP;
557 }
558
559 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000560 break;
Austin Schuh30537882014-02-18 01:07:23 -0800561 case STATE_UNLOAD_MOVE: {
562 if (disabled) {
Austin Schuh06cbbf12014-02-22 02:07:31 -0800563 unload_timeout_ = Time::Now() + kUnloadTimeout;
Austin Schuh30537882014-02-18 01:07:23 -0800564 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
565 }
Austin Schuhd34569d2014-02-18 20:26:38 -0800566 cap_goal = true;
Austin Schuh6b428602014-02-22 21:02:00 -0800567 shooter_.set_max_voltage(6.0);
Austin Schuh30537882014-02-18 01:07:23 -0800568
569 // Slowly move back until we hit the upper limit.
Austin Schuhd34569d2014-02-18 20:26:38 -0800570 // If we were at the limit last cycle, we are done unloading.
571 // This is because if we saturate, we might hit the limit before we are
572 // actually there.
573 if (shooter_.goal_position() >= values.shooter.upper_limit) {
Austin Schuh30537882014-02-18 01:07:23 -0800574 shooter_.SetGoalPosition(values.shooter.upper_limit, 0.0);
575 // We don't want the loop fighting the spring when we are unloaded.
576 // Turn it off.
577 shooter_loop_disable = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000578 state_ = STATE_READY_UNLOAD;
579 } else {
Austin Schuhd34569d2014-02-18 20:26:38 -0800580 shooter_.SetGoalPosition(
581 ::std::min(
582 values.shooter.upper_limit,
Austin Schuh6b428602014-02-22 21:02:00 -0800583 shooter_.goal_position() + values.shooter.unload_speed * dt),
584 values.shooter.unload_speed);
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000585 }
586
Austin Schuh30537882014-02-18 01:07:23 -0800587 latch_piston_ = false;
588 brake_piston_ = false;
589 } break;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000590 case STATE_READY_UNLOAD:
Austin Schuh30537882014-02-18 01:07:23 -0800591 if (goal->load_requested) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000592 state_ = STATE_REQUEST_LOAD;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000593 }
James Kuszmaul7290a942014-02-26 20:04:13 -0800594 // If we are ready to load again,
Austin Schuh30537882014-02-18 01:07:23 -0800595 shooter_loop_disable = true;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000596
Austin Schuh30537882014-02-18 01:07:23 -0800597 latch_piston_ = false;
598 brake_piston_ = false;
599 break;
600
601 case STATE_ESTOP:
602 // Totally lost, go to a safe state.
603 shooter_loop_disable = true;
604 latch_piston_ = true;
605 brake_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000606 break;
joe2d92e852014-01-25 14:31:24 -0800607 }
608
James Kuszmaul7290a942014-02-26 20:04:13 -0800609 if (!shooter_loop_disable) {
Austin Schuh30537882014-02-18 01:07:23 -0800610 LOG(DEBUG, "Running the loop, goal is %f, position is %f\n",
611 shooter_.goal_position(), shooter_.absolute_position());
Austin Schuhd34569d2014-02-18 20:26:38 -0800612 if (!cap_goal) {
613 shooter_.set_max_voltage(12.0);
614 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000615 shooter_.Update(output == NULL);
Austin Schuhd34569d2014-02-18 20:26:38 -0800616 if (cap_goal) {
617 shooter_.CapGoal();
618 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000619 if (output) output->voltage = shooter_.voltage();
Ben Fredrickson7d980c22014-02-16 21:39:02 +0000620 } else {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000621 shooter_.Update(true);
Austin Schuhd34569d2014-02-18 20:26:38 -0800622 shooter_.ZeroPower();
Ben Fredrickson22c93322014-02-17 05:56:33 +0000623 if (output) output->voltage = 0.0;
Ben Fredricksonedf0e092014-02-16 10:46:50 +0000624 }
Ben Fredrickson7d980c22014-02-16 21:39:02 +0000625
Austin Schuhf4392d42014-03-02 14:00:37 -0800626 status->hard_stop_power = PositionToPower(shooter_.absolute_position());
James Kuszmauld29689f2014-03-02 13:06:54 -0800627
Austin Schuh30537882014-02-18 01:07:23 -0800628 if (output) {
629 output->latch_piston = latch_piston_;
630 output->brake_piston = brake_piston_;
631 }
632
Austin Schuh30537882014-02-18 01:07:23 -0800633 if (position) {
634 last_position_ = *position;
Ben Fredricksona9dcfa42014-02-23 02:05:59 +0000635 LOG(DEBUG, "pos > absolute: %f velocity: %f state= %d l= %d pp= %d, pd= %d "
636 "p= %d b=%d\n",
Austin Schuhbe1401f2014-02-18 03:18:41 -0800637 shooter_.absolute_position(), shooter_.absolute_velocity(),
James Kuszmaul7290a942014-02-26 20:04:13 -0800638 state_, position->latch, position->pusher_proximal.current,
639 position->pusher_distal.current,
640 position->plunger, brake_piston_);
Austin Schuh30537882014-02-18 01:07:23 -0800641 }
642 if (position) {
643 last_distal_posedge_count_ = position->pusher_distal.posedge_count;
644 last_proximal_posedge_count_ = position->pusher_proximal.posedge_count;
Austin Schuh2e976812014-03-05 19:56:58 -0800645 last_distal_current_ = position->pusher_distal.current;
646 last_proximal_current_ = position->pusher_proximal.current;
Austin Schuh30537882014-02-18 01:07:23 -0800647 }
Austin Schuh4edad872014-03-02 11:56:12 -0800648
649 status->shots = shot_count_;
joe2d92e852014-01-25 14:31:24 -0800650}
651
652} // namespace control_loops
653} // namespace frc971