blob: c4d183a2b57b26163dd2cac124f8a1f8a32ee3f0 [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
Briana6553ed2014-04-02 21:26:46 -07007#include "aos/common/controls/control_loops.q.h"
joe2d92e852014-01-25 14:31:24 -08008#include "aos/common/logging/logging.h"
Brian Silvermanf48fab32014-03-09 14:32:24 -07009#include "aos/common/logging/queue_logging.h"
joe2d92e852014-01-25 14:31:24 -080010
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_;
Brian Silverman273d8a32014-05-10 22:19:09 -070021 voltage_ += U(0);
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000022
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
Brian Silverman273d8a32014-05-10 22:19:09 -070026 // delay by one cycle between the applied voltage and X_hat(2), so compare
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000027 // against last cycle's voltage.
Brian Silverman273d8a32014-05-10 22:19:09 -070028 if (X_hat(2) > last_voltage_ + 4.0) {
29 voltage_ -= X_hat(2) - (last_voltage_ + 4.0);
Brian Silverman101b9642014-03-08 12:45:16 -080030 LOG(DEBUG, "Capping due to runaway\n");
Brian Silverman273d8a32014-05-10 22:19:09 -070031 } else if (X_hat(2) < last_voltage_ - 4.0) {
32 voltage_ += X_hat(2) - (last_voltage_ - 4.0);
Brian Silverman101b9642014-03-08 12:45:16 -080033 LOG(DEBUG, "Capping due to runaway\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_);
Brian Silverman273d8a32014-05-10 22:19:09 -070038 change_U(0) = voltage_ - old_voltage;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +000039
Brian Silverman273d8a32014-05-10 22:19:09 -070040 LOG_STRUCT(DEBUG, "output", ShooterVoltageToLog(X_hat(2), voltage_));
Austin Schuhbe1401f2014-02-18 03:18:41 -080041
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));
Brian Silverman273d8a32014-05-10 22:19:09 -070052 change_R(0) -= dx;
53 change_R(2) -= -A(1, 0) / A(1, 2) * dx;
Austin Schuhd34569d2014-02-18 20:26:38 -080054 } else {
55 dx = (uncapped_voltage() - max_voltage_) / K(0, 0);
Brian Silverman273d8a32014-05-10 22:19:09 -070056 change_R(0) -= dx;
Austin Schuhd34569d2014-02-18 20:26:38 -080057 }
58 capped_goal_ = true;
Brian Silvermanf48fab32014-03-09 14:32:24 -070059 LOG_STRUCT(DEBUG, "to prevent windup", ShooterMovingGoal(dx));
Austin Schuhd34569d2014-02-18 20:26:38 -080060 } 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));
Brian Silverman273d8a32014-05-10 22:19:09 -070065 change_R(0) -= dx;
66 change_R(2) -= -A(1, 0) / A(1, 2) * dx;
Austin Schuhd34569d2014-02-18 20:26:38 -080067 } else {
68 dx = (uncapped_voltage() + max_voltage_) / K(0, 0);
Brian Silverman273d8a32014-05-10 22:19:09 -070069 change_R(0) -= dx;
Austin Schuhd34569d2014-02-18 20:26:38 -080070 }
71 capped_goal_ = true;
Brian Silvermanf48fab32014-03-09 14:32:24 -070072 LOG_STRUCT(DEBUG, "to prevent windup", ShooterMovingGoal(dx));
Austin Schuhd34569d2014-02-18 20:26:38 -080073 } else {
74 capped_goal_ = false;
75 }
76}
77
78void ZeroedStateFeedbackLoop::RecalculatePowerGoal() {
79 if (controller_index() == 0) {
Brian Silverman273d8a32014-05-10 22:19:09 -070080 change_R(2) = (-A(1, 0) / A(1, 2) * R(0) - A(1, 1) / A(1, 2) * R(1));
Austin Schuhd34569d2014-02-18 20:26:38 -080081 } else {
Brian Silverman273d8a32014-05-10 22:19:09 -070082 change_R(2) = -A(1, 1) / A(1, 2) * R(1);
Austin Schuhd34569d2014-02-18 20:26:38 -080083 }
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) {
Brian Silvermanf48fab32014-03-09 14:32:24 -070088 double old_position = absolute_position();
Austin Schuh30537882014-02-18 01:07:23 -080089 double previous_offset = offset_;
90 offset_ = known_position - encoder_val;
91 double doffset = offset_ - previous_offset;
Brian Silverman273d8a32014-05-10 22:19:09 -070092 change_X_hat(0) += doffset;
Austin Schuh30537882014-02-18 01:07:23 -080093 // Offset our measurements because the offset is baked into them.
Brian Silverman273d8a32014-05-10 22:19:09 -070094 // This is safe because if we got here, it means position != nullptr, which
95 // means we already set Y to something and it won't just get overwritten.
96 change_Y(0) += doffset;
Austin Schuh30537882014-02-18 01:07:23 -080097 // Offset the goal so we don't move.
Brian Silverman273d8a32014-05-10 22:19:09 -070098 change_R(0) += doffset;
Austin Schuhd34569d2014-02-18 20:26:38 -080099 if (controller_index() == 0) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700100 change_R(2) += -A(1, 0) / A(1, 2) * (doffset);
Austin Schuhd34569d2014-02-18 20:26:38 -0800101 }
Brian Silvermanf48fab32014-03-09 14:32:24 -0700102 LOG_STRUCT(
Brian Silvermand6337412014-03-14 18:51:26 -0700103 DEBUG, "sensor edge (fake?)",
Brian Silvermanf48fab32014-03-09 14:32:24 -0700104 ShooterChangeCalibration(encoder_val, known_position, old_position,
105 absolute_position(), previous_offset, offset_));
Austin Schuh30537882014-02-18 01:07:23 -0800106}
107
Ben Fredricksonedf0e092014-02-16 10:46:50 +0000108ShooterMotor::ShooterMotor(control_loops::ShooterGroup *my_shooter)
Brian Silverman38111502014-04-10 12:36:26 -0700109 : aos::controls::ControlLoop<control_loops::ShooterGroup>(my_shooter),
Ben Fredricksonedf0e092014-02-16 10:46:50 +0000110 shooter_(MakeShooterLoop()),
Ben Fredrickson22c93322014-02-17 05:56:33 +0000111 state_(STATE_INITIALIZE),
112 loading_problem_end_time_(0, 0),
Austin Schuh30537882014-02-18 01:07:23 -0800113 load_timeout_(0, 0),
Ben Fredrickson22c93322014-02-17 05:56:33 +0000114 shooter_brake_set_time_(0, 0),
Austin Schuh30537882014-02-18 01:07:23 -0800115 unload_timeout_(0, 0),
Ben Fredrickson22c93322014-02-17 05:56:33 +0000116 shot_end_time_(0, 0),
Austin Schuh4edad872014-03-02 11:56:12 -0800117 cycles_not_moved_(0),
Austin Schuhd30d7382014-03-02 21:15:38 -0800118 shot_count_(0),
119 zeroed_(false),
120 distal_posedge_validation_cycles_left_(0),
Austin Schuh2e976812014-03-05 19:56:58 -0800121 proximal_posedge_validation_cycles_left_(0),
122 last_distal_current_(true),
123 last_proximal_current_(true) {}
Austin Schuh30537882014-02-18 01:07:23 -0800124
125double ShooterMotor::PowerToPosition(double power) {
Austin Schuh30537882014-02-18 01:07:23 -0800126 const frc971::constants::Values &values = constants::GetValues();
Ben Fredricksonda334d12014-02-23 09:09:23 +0000127 double maxpower = 0.5 * kSpringConstant *
128 (kMaxExtension * kMaxExtension -
129 (kMaxExtension - values.shooter.upper_limit) *
130 (kMaxExtension - values.shooter.upper_limit));
131 if (power < 0) {
Brian Silverman0ebe9442014-03-22 13:57:35 -0700132 LOG_STRUCT(WARNING, "negative power", PowerAdjustment(power, 0));
Ben Fredricksonda334d12014-02-23 09:09:23 +0000133 power = 0;
134 } else if (power > maxpower) {
Brian Silverman0ebe9442014-03-22 13:57:35 -0700135 LOG_STRUCT(WARNING, "power too high", PowerAdjustment(power, maxpower));
Ben Fredricksonda334d12014-02-23 09:09:23 +0000136 power = maxpower;
137 }
138
139 double mp = kMaxExtension * kMaxExtension - (power + power) / kSpringConstant;
140 double new_pos = 0.10;
141 if (mp < 0) {
142 LOG(ERROR,
143 "Power calculation has negative number before square root (%f).\n", mp);
144 } else {
145 new_pos = kMaxExtension - ::std::sqrt(mp);
146 }
147
148 new_pos = ::std::min(::std::max(new_pos, values.shooter.lower_limit),
Austin Schuh30537882014-02-18 01:07:23 -0800149 values.shooter.upper_limit);
150 return new_pos;
151}
Ben Fredrickson1e512ff2014-02-15 21:36:52 +0000152
James Kuszmauld29689f2014-03-02 13:06:54 -0800153double ShooterMotor::PositionToPower(double position) {
154 double power = kSpringConstant * position * (kMaxExtension - position / 2.0);
155 return power;
156}
157
Ben Fredrickson1e512ff2014-02-15 21:36:52 +0000158// Positive is out, and positive power is out.
joe2d92e852014-01-25 14:31:24 -0800159void ShooterMotor::RunIteration(
Ben Fredricksonedf0e092014-02-16 10:46:50 +0000160 const control_loops::ShooterGroup::Goal *goal,
161 const control_loops::ShooterGroup::Position *position,
162 control_loops::ShooterGroup::Output *output,
Ben Fredrickson22c93322014-02-17 05:56:33 +0000163 control_loops::ShooterGroup::Status *status) {
Ben Fredrickson1e512ff2014-02-15 21:36:52 +0000164 constexpr double dt = 0.01;
165
Ben Fredrickson61893d52014-03-02 09:43:23 +0000166 if (::std::isnan(goal->shot_power)) {
167 state_ = STATE_ESTOP;
Brian Silvermanf48fab32014-03-09 14:32:24 -0700168 LOG(ERROR, "Estopping because got a shot power of NAN.\n");
Ben Fredrickson61893d52014-03-02 09:43:23 +0000169 }
170
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000171 // we must always have these or we have issues.
172 if (goal == NULL || status == NULL) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000173 if (output) output->voltage = 0;
174 LOG(ERROR, "Thought I would just check for null and die.\n");
175 return;
176 }
James Kuszmaul4abaf482014-02-26 21:16:35 -0800177 status->ready = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000178
Austin Schuh30537882014-02-18 01:07:23 -0800179 if (reset()) {
180 state_ = STATE_INITIALIZE;
Austin Schuh2e976812014-03-05 19:56:58 -0800181 last_distal_current_ = position->pusher_distal.current;
182 last_proximal_current_ = position->pusher_proximal.current;
Austin Schuh30537882014-02-18 01:07:23 -0800183 }
184 if (position) {
185 shooter_.CorrectPosition(position->position);
Ben Fredrickson1e512ff2014-02-15 21:36:52 +0000186 }
joe2d92e852014-01-25 14:31:24 -0800187
188 // Disable the motors now so that all early returns will return with the
189 // motors disabled.
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000190 if (output) output->voltage = 0;
joe2d92e852014-01-25 14:31:24 -0800191
Ben Fredrickson1e512ff2014-02-15 21:36:52 +0000192 const frc971::constants::Values &values = constants::GetValues();
193
Brian Silvermanaae236a2014-02-17 01:49:39 -0800194 // Don't even let the control loops run.
Ben Fredricksonedf0e092014-02-16 10:46:50 +0000195 bool shooter_loop_disable = false;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000196
Brian Silvermand8a7cf02014-03-22 17:15:23 -0700197 const bool disabled =
198 !::aos::robot_state.get() || !::aos::robot_state->enabled;
Brian Silverman36407c92014-04-01 15:54:02 -0700199
Austin Schuhd34569d2014-02-18 20:26:38 -0800200 // If true, move the goal if we saturate.
201 bool cap_goal = false;
202
203 // TODO(austin): Move the offset if we see or don't see a hall effect when we
204 // expect to see one.
205 // Probably not needed yet.
206
207 if (position) {
208 int last_controller_index = shooter_.controller_index();
209 if (position->plunger && position->latch) {
210 // Use the controller without the spring if the latch is set and the
211 // plunger is back
212 shooter_.set_controller_index(1);
213 } else {
214 // Otherwise use the controller with the spring.
215 shooter_.set_controller_index(0);
216 }
217 if (shooter_.controller_index() != last_controller_index) {
218 shooter_.RecalculatePowerGoal();
219 }
220 }
Austin Schuh30537882014-02-18 01:07:23 -0800221
Ben Fredrickson1e512ff2014-02-15 21:36:52 +0000222 switch (state_) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000223 case STATE_INITIALIZE:
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000224 if (position) {
Austin Schuh30537882014-02-18 01:07:23 -0800225 // Reinitialize the internal filter state.
226 shooter_.InitializeState(position->position);
Austin Schuh30537882014-02-18 01:07:23 -0800227
228 // Start off with the assumption that we are at the value
229 // futhest back given our sensors.
230 if (position->pusher_distal.current) {
231 shooter_.SetCalibration(position->position,
232 values.shooter.pusher_distal.lower_angle);
233 } else if (position->pusher_proximal.current) {
234 shooter_.SetCalibration(position->position,
Austin Schuhd34569d2014-02-18 20:26:38 -0800235 values.shooter.pusher_proximal.upper_angle);
Austin Schuh30537882014-02-18 01:07:23 -0800236 } else {
237 shooter_.SetCalibration(position->position,
238 values.shooter.upper_limit);
239 }
240
Austin Schuh30537882014-02-18 01:07:23 -0800241 // Go to the current position.
242 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
243 // If the plunger is all the way back, we want to be latched.
244 latch_piston_ = position->plunger;
245 brake_piston_ = false;
Austin Schuh1a08bd82014-03-09 00:47:11 -0800246 if (position->latch == latch_piston_) {
247 state_ = STATE_REQUEST_LOAD;
248 } else {
249 shooter_loop_disable = true;
Brian Silvermanf48fab32014-03-09 14:32:24 -0700250 LOG(DEBUG,
251 "Not moving on until the latch has moved to avoid a crash\n");
Austin Schuh1a08bd82014-03-09 00:47:11 -0800252 }
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000253 } else {
Austin Schuh30537882014-02-18 01:07:23 -0800254 // If we can't start yet because we don't know where we are, set the
255 // latch and brake to their defaults.
256 latch_piston_ = true;
257 brake_piston_ = true;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000258 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000259 break;
260 case STATE_REQUEST_LOAD:
Austin Schuh30537882014-02-18 01:07:23 -0800261 if (position) {
Austin Schuhd30d7382014-03-02 21:15:38 -0800262 zeroed_ = false;
Ben Fredrickson26a0dee2014-02-23 04:38:23 +0000263 if (position->pusher_distal.current ||
264 position->pusher_proximal.current) {
Austin Schuh30537882014-02-18 01:07:23 -0800265 // We started on the sensor, back up until we are found.
266 // If the plunger is all the way back and not latched, it won't be
267 // there for long.
268 state_ = STATE_LOAD_BACKTRACK;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800269
270 // The plunger is already back and latched. Don't release it.
271 if (position->plunger && position->latch) {
272 latch_piston_ = true;
273 } else {
274 latch_piston_ = false;
275 }
276 } else if (position->plunger && position->latch) {
277 // The plunger is back and we are latched. We most likely got here
278 // from Initialize, in which case we want to 'load' again anyways to
279 // zero.
280 Load();
281 latch_piston_ = true;
Austin Schuh30537882014-02-18 01:07:23 -0800282 } else {
283 // Off the sensor, start loading.
284 Load();
285 latch_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000286 }
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000287 }
288
Austin Schuh30537882014-02-18 01:07:23 -0800289 // Hold our current position.
290 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
291 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000292 break;
293 case STATE_LOAD_BACKTRACK:
Austin Schuh30537882014-02-18 01:07:23 -0800294 // If we are here, then that means we started past the edge where we want
295 // to zero. Move backwards until we don't see the sensor anymore.
296 // The plunger is contacting the pusher (or will be shortly).
297
Austin Schuh30537882014-02-18 01:07:23 -0800298 if (!disabled) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000299 shooter_.SetGoalPosition(
Austin Schuhfaeee632014-02-18 01:24:05 -0800300 shooter_.goal_position() + values.shooter.zeroing_speed * dt,
301 values.shooter.zeroing_speed);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000302 }
Austin Schuhd34569d2014-02-18 20:26:38 -0800303 cap_goal = true;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800304 shooter_.set_max_voltage(4.0);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000305
Austin Schuh30537882014-02-18 01:07:23 -0800306 if (position) {
Ben Fredrickson26a0dee2014-02-23 04:38:23 +0000307 if (!position->pusher_distal.current &&
308 !position->pusher_proximal.current) {
Austin Schuh30537882014-02-18 01:07:23 -0800309 Load();
310 }
Austin Schuh6b428602014-02-22 21:02:00 -0800311 latch_piston_ = position->plunger;
Austin Schuh30537882014-02-18 01:07:23 -0800312 }
313
Austin Schuh30537882014-02-18 01:07:23 -0800314 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000315 break;
316 case STATE_LOAD:
Austin Schuh30537882014-02-18 01:07:23 -0800317 // If we are disabled right now, reset the timer.
318 if (disabled) {
319 Load();
320 // Latch defaults to true when disabled. Leave it latched until we have
321 // useful sensor data.
322 latch_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000323 }
Brian Silverman2704ecf2014-04-09 20:24:03 -0700324 if (output == nullptr) {
Brian Silverman38111502014-04-10 12:36:26 -0700325 load_timeout_ += ::aos::controls::kLoopFrequency;
Brian Silverman36407c92014-04-01 15:54:02 -0700326 }
Austin Schuh30537882014-02-18 01:07:23 -0800327 // Go to 0, which should be the latch position, or trigger a hall effect
328 // on the way. If we don't see edges where we are supposed to, the
329 // offset will be updated by code above.
330 shooter_.SetGoalPosition(0.0, 0.0);
Ben Fredrickson22c93322014-02-17 05:56:33 +0000331
Austin Schuh30537882014-02-18 01:07:23 -0800332 if (position) {
Austin Schuhd30d7382014-03-02 21:15:38 -0800333 // TODO(austin): Validate that this is the right edge.
Austin Schuh30537882014-02-18 01:07:23 -0800334 // If we see a posedge on any of the hall effects,
335 if (position->pusher_proximal.posedge_count !=
Austin Schuh2e976812014-03-05 19:56:58 -0800336 last_proximal_posedge_count_ &&
337 !last_proximal_current_) {
Austin Schuhd30d7382014-03-02 21:15:38 -0800338 proximal_posedge_validation_cycles_left_ = 2;
Austin Schuh30537882014-02-18 01:07:23 -0800339 }
Austin Schuhd30d7382014-03-02 21:15:38 -0800340 if (proximal_posedge_validation_cycles_left_ > 0) {
341 if (position->pusher_proximal.current) {
342 --proximal_posedge_validation_cycles_left_;
343 if (proximal_posedge_validation_cycles_left_ == 0) {
344 shooter_.SetCalibration(
345 position->pusher_proximal.posedge_value,
346 values.shooter.pusher_proximal.upper_angle);
347
348 LOG(DEBUG, "Setting calibration using proximal sensor\n");
349 zeroed_ = true;
350 }
351 } else {
352 proximal_posedge_validation_cycles_left_ = 0;
353 }
354 }
355
Austin Schuh30537882014-02-18 01:07:23 -0800356 if (position->pusher_distal.posedge_count !=
Austin Schuh2e976812014-03-05 19:56:58 -0800357 last_distal_posedge_count_ &&
358 !last_distal_current_) {
Austin Schuhd30d7382014-03-02 21:15:38 -0800359 distal_posedge_validation_cycles_left_ = 2;
360 }
361 if (distal_posedge_validation_cycles_left_ > 0) {
362 if (position->pusher_distal.current) {
363 --distal_posedge_validation_cycles_left_;
364 if (distal_posedge_validation_cycles_left_ == 0) {
365 shooter_.SetCalibration(
366 position->pusher_distal.posedge_value,
367 values.shooter.pusher_distal.upper_angle);
368
369 LOG(DEBUG, "Setting calibration using distal sensor\n");
370 zeroed_ = true;
371 }
372 } else {
373 distal_posedge_validation_cycles_left_ = 0;
374 }
Austin Schuh30537882014-02-18 01:07:23 -0800375 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000376
Austin Schuh30537882014-02-18 01:07:23 -0800377 // Latch if the plunger is far enough back to trigger the hall effect.
378 // This happens when the distal sensor is triggered.
Austin Schuh06cbbf12014-02-22 02:07:31 -0800379 latch_piston_ = position->pusher_distal.current || position->plunger;
Austin Schuh30537882014-02-18 01:07:23 -0800380
Austin Schuh06cbbf12014-02-22 02:07:31 -0800381 // Check if we are latched and back. Make sure the plunger is all the
382 // way back as well.
383 if (position->plunger && position->latch &&
384 position->pusher_distal.current) {
Austin Schuhd30d7382014-03-02 21:15:38 -0800385 if (!zeroed_) {
386 state_ = STATE_REQUEST_LOAD;
387 } else {
388 state_ = STATE_PREPARE_SHOT;
389 }
Austin Schuh30537882014-02-18 01:07:23 -0800390 } else if (position->plunger &&
391 ::std::abs(shooter_.absolute_position() -
392 shooter_.goal_position()) < 0.001) {
393 // We are at the goal, but not latched.
394 state_ = STATE_LOADING_PROBLEM;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800395 loading_problem_end_time_ = Time::Now() + kLoadProblemEndTimeout;
Austin Schuh30537882014-02-18 01:07:23 -0800396 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000397 }
Austin Schuh30537882014-02-18 01:07:23 -0800398 if (load_timeout_ < Time::Now()) {
399 if (position) {
400 if (!position->pusher_distal.current ||
401 !position->pusher_proximal.current) {
402 state_ = STATE_ESTOP;
Brian Silvermanf48fab32014-03-09 14:32:24 -0700403 LOG(ERROR, "Estopping because took too long to load.\n");
Austin Schuh30537882014-02-18 01:07:23 -0800404 }
405 }
Austin Schuh30537882014-02-18 01:07:23 -0800406 }
407 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000408 break;
409 case STATE_LOADING_PROBLEM:
Austin Schuh30537882014-02-18 01:07:23 -0800410 if (disabled) {
Austin Schuhd30d7382014-03-02 21:15:38 -0800411 state_ = STATE_REQUEST_LOAD;
412 break;
Austin Schuh30537882014-02-18 01:07:23 -0800413 }
414 // We got to the goal, but the latch hasn't registered as down. It might
415 // be stuck, or on it's way but not there yet.
Ben Fredrickson22c93322014-02-17 05:56:33 +0000416 if (Time::Now() > loading_problem_end_time_) {
Austin Schuh30537882014-02-18 01:07:23 -0800417 // Timeout by unloading.
418 Unload();
419 } else if (position && position->plunger && position->latch) {
420 // If both trigger, we are latched.
Ben Fredrickson22c93322014-02-17 05:56:33 +0000421 state_ = STATE_PREPARE_SHOT;
422 }
Austin Schuh30537882014-02-18 01:07:23 -0800423 // Move a bit further back to help it trigger.
424 // If the latch is slow due to the air flowing through the tubes or
425 // inertia, but is otherwise free, this won't have much time to do
426 // anything and is safe. Otherwise this gives us a bit more room to free
427 // up the latch.
428 shooter_.SetGoalPosition(values.shooter.lower_limit, 0.0);
Brian Silverman3693f9a2014-03-30 17:54:15 -0700429 if (position) {
430 LOG(DEBUG, "Waiting on latch: plunger %d, latch: %d\n",
431 position->plunger, position->latch);
432 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000433
Austin Schuh30537882014-02-18 01:07:23 -0800434 latch_piston_ = true;
435 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000436 break;
437 case STATE_PREPARE_SHOT:
Austin Schuh30537882014-02-18 01:07:23 -0800438 // Move the shooter to the shot power set point and then lock the brake.
439 // TODO(austin): Timeout. Low priority.
440
Ben Fredrickson22c93322014-02-17 05:56:33 +0000441 shooter_.SetGoalPosition(PowerToPosition(goal->shot_power), 0.0);
Austin Schuh30537882014-02-18 01:07:23 -0800442
443 LOG(DEBUG, "PDIFF: absolute_position: %.2f, pow: %.2f\n",
Austin Schuhbe1401f2014-02-18 03:18:41 -0800444 shooter_.absolute_position(), PowerToPosition(goal->shot_power));
Austin Schuh30537882014-02-18 01:07:23 -0800445 if (::std::abs(shooter_.absolute_position() -
Austin Schuhbe1401f2014-02-18 03:18:41 -0800446 PowerToPosition(goal->shot_power)) +
447 ::std::abs(shooter_.absolute_velocity()) <
448 0.001) {
Austin Schuh30537882014-02-18 01:07:23 -0800449 // We are there, set the brake and move on.
450 latch_piston_ = true;
451 brake_piston_ = true;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800452 shooter_brake_set_time_ = Time::Now() + kShooterBrakeSetTime;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000453 state_ = STATE_READY;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000454 } else {
Austin Schuh30537882014-02-18 01:07:23 -0800455 latch_piston_ = true;
456 brake_piston_ = false;
457 }
458 if (goal->unload_requested) {
459 Unload();
Ben Fredrickson22c93322014-02-17 05:56:33 +0000460 }
461 break;
462 case STATE_READY:
Austin Schuh30537882014-02-18 01:07:23 -0800463 LOG(DEBUG, "In ready\n");
Austin Schuhbe1401f2014-02-18 03:18:41 -0800464 // Wait until the brake is set, and a shot is requested or the shot power
465 // is changed.
Austin Schuh6b428602014-02-22 21:02:00 -0800466 if (Time::Now() > shooter_brake_set_time_) {
James Kuszmaul4abaf482014-02-26 21:16:35 -0800467 status->ready = true;
Austin Schuhbe1401f2014-02-18 03:18:41 -0800468 // We have waited long enough for the brake to set, turn the shooter
469 // control loop off.
Ben Fredrickson22c93322014-02-17 05:56:33 +0000470 shooter_loop_disable = true;
Austin Schuh30537882014-02-18 01:07:23 -0800471 LOG(DEBUG, "Brake is now set\n");
472 if (goal->shot_requested && !disabled) {
473 LOG(DEBUG, "Shooting now\n");
474 shooter_loop_disable = true;
James Kuszmaul7290a942014-02-26 20:04:13 -0800475 shot_end_time_ = Time::Now() + kShotEndTimeout;
476 firing_starting_position_ = shooter_.absolute_position();
477 state_ = STATE_FIRE;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000478 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000479 }
Austin Schuh6b428602014-02-22 21:02:00 -0800480 if (state_ == STATE_READY &&
481 ::std::abs(shooter_.absolute_position() -
482 PowerToPosition(goal->shot_power)) > 0.002) {
483 // TODO(austin): Add a state to release the brake.
484
485 // TODO(austin): Do we want to set the brake here or after shooting?
486 // Depends on air usage.
James Kuszmaul4abaf482014-02-26 21:16:35 -0800487 status->ready = false;
Austin Schuh6b428602014-02-22 21:02:00 -0800488 LOG(DEBUG, "Preparing shot again.\n");
489 state_ = STATE_PREPARE_SHOT;
490 }
491
Ben Fredricksona6d77542014-02-17 07:54:43 +0000492 shooter_.SetGoalPosition(PowerToPosition(goal->shot_power), 0.0);
493
Austin Schuh30537882014-02-18 01:07:23 -0800494 latch_piston_ = true;
495 brake_piston_ = true;
496
497 if (goal->unload_requested) {
498 Unload();
499 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000500 break;
Austin Schuh30537882014-02-18 01:07:23 -0800501
Ben Fredrickson22c93322014-02-17 05:56:33 +0000502 case STATE_FIRE:
Austin Schuh30537882014-02-18 01:07:23 -0800503 if (disabled) {
504 if (position) {
505 if (position->plunger) {
506 // If disabled and the plunger is still back there, reset the
507 // timeout.
Austin Schuh06cbbf12014-02-22 02:07:31 -0800508 shot_end_time_ = Time::Now() + kShotEndTimeout;
Austin Schuh30537882014-02-18 01:07:23 -0800509 }
510 }
511 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000512 shooter_loop_disable = true;
Austin Schuh30537882014-02-18 01:07:23 -0800513 // Count the number of contiguous cycles during which we haven't moved.
514 if (::std::abs(last_position_.position - shooter_.absolute_position()) <
515 0.0005) {
516 ++cycles_not_moved_;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000517 } else {
518 cycles_not_moved_ = 0;
519 }
Austin Schuh30537882014-02-18 01:07:23 -0800520
521 // If we have moved any amount since the start and the shooter has now
522 // been still for a couple cycles, the shot finished.
523 // Also move on if it times out.
524 if ((::std::abs(firing_starting_position_ -
525 shooter_.absolute_position()) > 0.0005 &&
526 cycles_not_moved_ > 3) ||
527 Time::Now() > shot_end_time_) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000528 state_ = STATE_REQUEST_LOAD;
Austin Schuh4edad872014-03-02 11:56:12 -0800529 ++shot_count_;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000530 }
Austin Schuh30537882014-02-18 01:07:23 -0800531 latch_piston_ = false;
532 brake_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000533 break;
534 case STATE_UNLOAD:
Austin Schuh30537882014-02-18 01:07:23 -0800535 // Reset the timeouts.
536 if (disabled) Unload();
537
538 // If it is latched and the plunger is back, move the pusher back to catch
539 // the plunger.
Austin Schuhf84a1302014-02-19 00:23:30 -0800540 bool all_back;
541 if (position) {
542 all_back = position->plunger && position->latch;
543 } else {
544 all_back = last_position_.plunger && last_position_.latch;
545 }
546
547 if (all_back) {
Austin Schuh30537882014-02-18 01:07:23 -0800548 // Pull back to 0, 0.
549 shooter_.SetGoalPosition(0.0, 0.0);
550 if (shooter_.absolute_position() < 0.005) {
551 // When we are close enough, 'fire'.
552 latch_piston_ = false;
553 } else {
554 latch_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000555 }
556 } else {
Austin Schuh30537882014-02-18 01:07:23 -0800557 // The plunger isn't all the way back, or it is and it is unlatched, so
558 // we can now unload.
559 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
560 latch_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000561 state_ = STATE_UNLOAD_MOVE;
Austin Schuh06cbbf12014-02-22 02:07:31 -0800562 unload_timeout_ = Time::Now() + kUnloadTimeout;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000563 }
564
Austin Schuh30537882014-02-18 01:07:23 -0800565 if (Time::Now() > unload_timeout_) {
566 // We have been stuck trying to unload for way too long, give up and
567 // turn everything off.
568 state_ = STATE_ESTOP;
Brian Silvermanf48fab32014-03-09 14:32:24 -0700569 LOG(ERROR, "Estopping because took too long to unload.\n");
Austin Schuh30537882014-02-18 01:07:23 -0800570 }
571
572 brake_piston_ = false;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000573 break;
Austin Schuh30537882014-02-18 01:07:23 -0800574 case STATE_UNLOAD_MOVE: {
575 if (disabled) {
Austin Schuh06cbbf12014-02-22 02:07:31 -0800576 unload_timeout_ = Time::Now() + kUnloadTimeout;
Austin Schuh30537882014-02-18 01:07:23 -0800577 shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0);
578 }
Austin Schuhd34569d2014-02-18 20:26:38 -0800579 cap_goal = true;
Austin Schuh6b428602014-02-22 21:02:00 -0800580 shooter_.set_max_voltage(6.0);
Austin Schuh30537882014-02-18 01:07:23 -0800581
582 // Slowly move back until we hit the upper limit.
Austin Schuhd34569d2014-02-18 20:26:38 -0800583 // If we were at the limit last cycle, we are done unloading.
584 // This is because if we saturate, we might hit the limit before we are
585 // actually there.
586 if (shooter_.goal_position() >= values.shooter.upper_limit) {
Austin Schuh30537882014-02-18 01:07:23 -0800587 shooter_.SetGoalPosition(values.shooter.upper_limit, 0.0);
588 // We don't want the loop fighting the spring when we are unloaded.
589 // Turn it off.
590 shooter_loop_disable = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000591 state_ = STATE_READY_UNLOAD;
592 } else {
Austin Schuhd34569d2014-02-18 20:26:38 -0800593 shooter_.SetGoalPosition(
594 ::std::min(
595 values.shooter.upper_limit,
Austin Schuh6b428602014-02-22 21:02:00 -0800596 shooter_.goal_position() + values.shooter.unload_speed * dt),
597 values.shooter.unload_speed);
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000598 }
599
Austin Schuh30537882014-02-18 01:07:23 -0800600 latch_piston_ = false;
601 brake_piston_ = false;
602 } break;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000603 case STATE_READY_UNLOAD:
Austin Schuh30537882014-02-18 01:07:23 -0800604 if (goal->load_requested) {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000605 state_ = STATE_REQUEST_LOAD;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000606 }
James Kuszmaul7290a942014-02-26 20:04:13 -0800607 // If we are ready to load again,
Austin Schuh30537882014-02-18 01:07:23 -0800608 shooter_loop_disable = true;
Ben Fredrickson1f633ef2014-02-16 05:35:45 +0000609
Austin Schuh30537882014-02-18 01:07:23 -0800610 latch_piston_ = false;
611 brake_piston_ = false;
612 break;
613
614 case STATE_ESTOP:
Brian Silverman0ebe9442014-03-22 13:57:35 -0700615 LOG(WARNING, "estopped\n");
Austin Schuh30537882014-02-18 01:07:23 -0800616 // Totally lost, go to a safe state.
617 shooter_loop_disable = true;
618 latch_piston_ = true;
619 brake_piston_ = true;
Ben Fredrickson22c93322014-02-17 05:56:33 +0000620 break;
joe2d92e852014-01-25 14:31:24 -0800621 }
622
James Kuszmaul7290a942014-02-26 20:04:13 -0800623 if (!shooter_loop_disable) {
Brian Silvermanf48fab32014-03-09 14:32:24 -0700624 LOG_STRUCT(DEBUG, "running the loop",
625 ShooterStatusToLog(shooter_.goal_position(),
626 shooter_.absolute_position()));
Austin Schuhd34569d2014-02-18 20:26:38 -0800627 if (!cap_goal) {
628 shooter_.set_max_voltage(12.0);
629 }
Ben Fredrickson22c93322014-02-17 05:56:33 +0000630 shooter_.Update(output == NULL);
Austin Schuhd34569d2014-02-18 20:26:38 -0800631 if (cap_goal) {
632 shooter_.CapGoal();
633 }
Brian Silverman36407c92014-04-01 15:54:02 -0700634 // We don't really want to output anything if we went through everything
635 // assuming the motors weren't working.
Brian Silverman2704ecf2014-04-09 20:24:03 -0700636 if (output) output->voltage = shooter_.voltage();
Ben Fredrickson7d980c22014-02-16 21:39:02 +0000637 } else {
Ben Fredrickson22c93322014-02-17 05:56:33 +0000638 shooter_.Update(true);
Austin Schuhd34569d2014-02-18 20:26:38 -0800639 shooter_.ZeroPower();
Ben Fredrickson22c93322014-02-17 05:56:33 +0000640 if (output) output->voltage = 0.0;
Ben Fredricksonedf0e092014-02-16 10:46:50 +0000641 }
Ben Fredrickson7d980c22014-02-16 21:39:02 +0000642
Austin Schuhf4392d42014-03-02 14:00:37 -0800643 status->hard_stop_power = PositionToPower(shooter_.absolute_position());
James Kuszmauld29689f2014-03-02 13:06:54 -0800644
Austin Schuh30537882014-02-18 01:07:23 -0800645 if (output) {
646 output->latch_piston = latch_piston_;
647 output->brake_piston = brake_piston_;
648 }
649
Austin Schuh30537882014-02-18 01:07:23 -0800650 if (position) {
Brian Silvermanf48fab32014-03-09 14:32:24 -0700651 LOG_STRUCT(DEBUG, "internal state",
652 ShooterStateToLog(
653 shooter_.absolute_position(), shooter_.absolute_velocity(),
654 state_, position->latch, position->pusher_proximal.current,
655 position->pusher_distal.current, position->plunger,
656 brake_piston_, latch_piston_));
657
Austin Schuh30537882014-02-18 01:07:23 -0800658 last_position_ = *position;
Brian Silvermanf48fab32014-03-09 14:32:24 -0700659
Austin Schuh30537882014-02-18 01:07:23 -0800660 last_distal_posedge_count_ = position->pusher_distal.posedge_count;
661 last_proximal_posedge_count_ = position->pusher_proximal.posedge_count;
Austin Schuh2e976812014-03-05 19:56:58 -0800662 last_distal_current_ = position->pusher_distal.current;
663 last_proximal_current_ = position->pusher_proximal.current;
Austin Schuh30537882014-02-18 01:07:23 -0800664 }
Austin Schuh4edad872014-03-02 11:56:12 -0800665
666 status->shots = shot_count_;
joe2d92e852014-01-25 14:31:24 -0800667}
668
Brian Silvermand1e65b92014-03-08 17:07:14 -0800669void ShooterMotor::ZeroOutputs() {
670 queue_group()->output.MakeWithBuilder()
671 .voltage(0)
672 .latch_piston(latch_piston_)
673 .brake_piston(brake_piston_)
674 .Send();
675}
676
joe2d92e852014-01-25 14:31:24 -0800677} // namespace control_loops
678} // namespace frc971