joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame] | 1 | #include "frc971/control_loops/shooter/shooter.h" |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 2 | |
| 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" |
Brian Silverman | f48fab3 | 2014-03-09 14:32:24 -0700 | [diff] [blame^] | 9 | #include "aos/common/logging/queue_logging.h" |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 10 | |
| 11 | #include "frc971/constants.h" |
joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame] | 12 | #include "frc971/control_loops/shooter/shooter_motor_plant.h" |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 13 | |
| 14 | namespace frc971 { |
| 15 | namespace control_loops { |
| 16 | |
Ben Fredrickson | edf0e09 | 2014-02-16 10:46:50 +0000 | [diff] [blame] | 17 | using ::aos::time::Time; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 18 | |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 19 | void ZeroedStateFeedbackLoop::CapU() { |
| 20 | const double old_voltage = voltage_; |
| 21 | voltage_ += U(0, 0); |
| 22 | |
| 23 | uncapped_voltage_ = voltage_; |
| 24 | |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 25 | // 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 Schuh | be1401f | 2014-02-18 03:18:41 -0800 | [diff] [blame] | 28 | if (X_hat(2, 0) > last_voltage_ + 4.0) { |
| 29 | voltage_ -= X_hat(2, 0) - (last_voltage_ + 4.0); |
Brian Silverman | 101b964 | 2014-03-08 12:45:16 -0800 | [diff] [blame] | 30 | LOG(DEBUG, "Capping due to runaway\n"); |
Austin Schuh | be1401f | 2014-02-18 03:18:41 -0800 | [diff] [blame] | 31 | } else if (X_hat(2, 0) < last_voltage_ - 4.0) { |
| 32 | voltage_ += X_hat(2, 0) - (last_voltage_ - 4.0); |
Brian Silverman | 101b964 | 2014-03-08 12:45:16 -0800 | [diff] [blame] | 33 | LOG(DEBUG, "Capping due to runaway\n"); |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame] | 36 | voltage_ = std::min(max_voltage_, voltage_); |
| 37 | voltage_ = std::max(-max_voltage_, voltage_); |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 38 | U(0, 0) = voltage_ - old_voltage; |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 39 | |
Brian Silverman | f48fab3 | 2014-03-09 14:32:24 -0700 | [diff] [blame^] | 40 | LOG_STRUCT(DEBUG, "output", ShooterVoltageToLog(X_hat(2, 0), voltage_)); |
Austin Schuh | be1401f | 2014-02-18 03:18:41 -0800 | [diff] [blame] | 41 | |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 42 | last_voltage_ = voltage_; |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame] | 43 | capped_goal_ = false; |
| 44 | } |
| 45 | |
| 46 | void 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; |
Brian Silverman | f48fab3 | 2014-03-09 14:32:24 -0700 | [diff] [blame^] | 59 | LOG_STRUCT(DEBUG, "to prevent windup", ShooterMovingGoal(dx)); |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame] | 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; |
Brian Silverman | f48fab3 | 2014-03-09 14:32:24 -0700 | [diff] [blame^] | 72 | LOG_STRUCT(DEBUG, "to prevent windup", ShooterMovingGoal(dx)); |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame] | 73 | } else { |
| 74 | capped_goal_ = false; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void 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 Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 86 | void ZeroedStateFeedbackLoop::SetCalibration(double encoder_val, |
| 87 | double known_position) { |
Brian Silverman | f48fab3 | 2014-03-09 14:32:24 -0700 | [diff] [blame^] | 88 | double old_position = absolute_position(); |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 89 | double previous_offset = offset_; |
| 90 | offset_ = known_position - encoder_val; |
| 91 | double doffset = offset_ - previous_offset; |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 92 | X_hat(0, 0) += doffset; |
| 93 | // Offset our measurements because the offset is baked into them. |
| 94 | Y_(0, 0) += doffset; |
| 95 | // Offset the goal so we don't move. |
| 96 | R(0, 0) += doffset; |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame] | 97 | if (controller_index() == 0) { |
| 98 | R(2, 0) += -A(1, 0) / A(1, 2) * (doffset); |
| 99 | } |
Brian Silverman | f48fab3 | 2014-03-09 14:32:24 -0700 | [diff] [blame^] | 100 | LOG_STRUCT( |
| 101 | INFO, "sensor edge", |
| 102 | ShooterChangeCalibration(encoder_val, known_position, old_position, |
| 103 | absolute_position(), previous_offset, offset_)); |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Ben Fredrickson | edf0e09 | 2014-02-16 10:46:50 +0000 | [diff] [blame] | 106 | ShooterMotor::ShooterMotor(control_loops::ShooterGroup *my_shooter) |
| 107 | : aos::control_loops::ControlLoop<control_loops::ShooterGroup>(my_shooter), |
| 108 | shooter_(MakeShooterLoop()), |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 109 | state_(STATE_INITIALIZE), |
| 110 | loading_problem_end_time_(0, 0), |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 111 | load_timeout_(0, 0), |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 112 | shooter_brake_set_time_(0, 0), |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 113 | unload_timeout_(0, 0), |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 114 | shot_end_time_(0, 0), |
Austin Schuh | 4edad87 | 2014-03-02 11:56:12 -0800 | [diff] [blame] | 115 | cycles_not_moved_(0), |
Austin Schuh | d30d738 | 2014-03-02 21:15:38 -0800 | [diff] [blame] | 116 | shot_count_(0), |
| 117 | zeroed_(false), |
| 118 | distal_posedge_validation_cycles_left_(0), |
Austin Schuh | 2e97681 | 2014-03-05 19:56:58 -0800 | [diff] [blame] | 119 | proximal_posedge_validation_cycles_left_(0), |
| 120 | last_distal_current_(true), |
| 121 | last_proximal_current_(true) {} |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 122 | |
| 123 | double ShooterMotor::PowerToPosition(double power) { |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 124 | const frc971::constants::Values &values = constants::GetValues(); |
Ben Fredrickson | da334d1 | 2014-02-23 09:09:23 +0000 | [diff] [blame] | 125 | double maxpower = 0.5 * kSpringConstant * |
| 126 | (kMaxExtension * kMaxExtension - |
| 127 | (kMaxExtension - values.shooter.upper_limit) * |
| 128 | (kMaxExtension - values.shooter.upper_limit)); |
| 129 | if (power < 0) { |
Ben Fredrickson | 7c9314f | 2014-02-23 09:26:17 +0000 | [diff] [blame] | 130 | //LOG(ERROR, "Power too low giving minimum (%f) (%f).\n", power, |
| 131 | // 0.0); |
Ben Fredrickson | da334d1 | 2014-02-23 09:09:23 +0000 | [diff] [blame] | 132 | power = 0; |
| 133 | } else if (power > maxpower) { |
Ben Fredrickson | 7c9314f | 2014-02-23 09:26:17 +0000 | [diff] [blame] | 134 | //LOG(ERROR, "Power too high giving maximum (%f) (%f).\n", power, |
| 135 | // maxpower); |
Ben Fredrickson | da334d1 | 2014-02-23 09:09:23 +0000 | [diff] [blame] | 136 | 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 Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 149 | values.shooter.upper_limit); |
| 150 | return new_pos; |
| 151 | } |
Ben Fredrickson | 1e512ff | 2014-02-15 21:36:52 +0000 | [diff] [blame] | 152 | |
James Kuszmaul | d29689f | 2014-03-02 13:06:54 -0800 | [diff] [blame] | 153 | double ShooterMotor::PositionToPower(double position) { |
| 154 | double power = kSpringConstant * position * (kMaxExtension - position / 2.0); |
| 155 | return power; |
| 156 | } |
| 157 | |
Ben Fredrickson | 1e512ff | 2014-02-15 21:36:52 +0000 | [diff] [blame] | 158 | // Positive is out, and positive power is out. |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 159 | void ShooterMotor::RunIteration( |
Ben Fredrickson | edf0e09 | 2014-02-16 10:46:50 +0000 | [diff] [blame] | 160 | const control_loops::ShooterGroup::Goal *goal, |
| 161 | const control_loops::ShooterGroup::Position *position, |
| 162 | control_loops::ShooterGroup::Output *output, |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 163 | control_loops::ShooterGroup::Status *status) { |
Ben Fredrickson | 1e512ff | 2014-02-15 21:36:52 +0000 | [diff] [blame] | 164 | constexpr double dt = 0.01; |
| 165 | |
Ben Fredrickson | 61893d5 | 2014-03-02 09:43:23 +0000 | [diff] [blame] | 166 | if (::std::isnan(goal->shot_power)) { |
| 167 | state_ = STATE_ESTOP; |
Brian Silverman | f48fab3 | 2014-03-09 14:32:24 -0700 | [diff] [blame^] | 168 | LOG(ERROR, "Estopping because got a shot power of NAN.\n"); |
Ben Fredrickson | 61893d5 | 2014-03-02 09:43:23 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 171 | // we must always have these or we have issues. |
| 172 | if (goal == NULL || status == NULL) { |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 173 | if (output) output->voltage = 0; |
| 174 | LOG(ERROR, "Thought I would just check for null and die.\n"); |
| 175 | return; |
| 176 | } |
James Kuszmaul | 4abaf48 | 2014-02-26 21:16:35 -0800 | [diff] [blame] | 177 | status->ready = false; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 178 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 179 | if (reset()) { |
| 180 | state_ = STATE_INITIALIZE; |
Austin Schuh | 2e97681 | 2014-03-05 19:56:58 -0800 | [diff] [blame] | 181 | last_distal_current_ = position->pusher_distal.current; |
| 182 | last_proximal_current_ = position->pusher_proximal.current; |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 183 | } |
| 184 | if (position) { |
| 185 | shooter_.CorrectPosition(position->position); |
Ben Fredrickson | 1e512ff | 2014-02-15 21:36:52 +0000 | [diff] [blame] | 186 | } |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 187 | |
| 188 | // Disable the motors now so that all early returns will return with the |
| 189 | // motors disabled. |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 190 | if (output) output->voltage = 0; |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 191 | |
Ben Fredrickson | 1e512ff | 2014-02-15 21:36:52 +0000 | [diff] [blame] | 192 | const frc971::constants::Values &values = constants::GetValues(); |
| 193 | |
Brian Silverman | aae236a | 2014-02-17 01:49:39 -0800 | [diff] [blame] | 194 | // Don't even let the control loops run. |
Ben Fredrickson | edf0e09 | 2014-02-16 10:46:50 +0000 | [diff] [blame] | 195 | bool shooter_loop_disable = false; |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 196 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 197 | const bool disabled = !::aos::robot_state->enabled; |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame] | 198 | // If true, move the goal if we saturate. |
| 199 | bool cap_goal = false; |
| 200 | |
| 201 | // TODO(austin): Move the offset if we see or don't see a hall effect when we |
| 202 | // expect to see one. |
| 203 | // Probably not needed yet. |
| 204 | |
| 205 | if (position) { |
| 206 | int last_controller_index = shooter_.controller_index(); |
| 207 | if (position->plunger && position->latch) { |
| 208 | // Use the controller without the spring if the latch is set and the |
| 209 | // plunger is back |
| 210 | shooter_.set_controller_index(1); |
| 211 | } else { |
| 212 | // Otherwise use the controller with the spring. |
| 213 | shooter_.set_controller_index(0); |
| 214 | } |
| 215 | if (shooter_.controller_index() != last_controller_index) { |
| 216 | shooter_.RecalculatePowerGoal(); |
| 217 | } |
| 218 | } |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 219 | |
Ben Fredrickson | 1e512ff | 2014-02-15 21:36:52 +0000 | [diff] [blame] | 220 | switch (state_) { |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 221 | case STATE_INITIALIZE: |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 222 | if (position) { |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 223 | // Reinitialize the internal filter state. |
| 224 | shooter_.InitializeState(position->position); |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 225 | |
| 226 | // Start off with the assumption that we are at the value |
| 227 | // futhest back given our sensors. |
| 228 | if (position->pusher_distal.current) { |
| 229 | shooter_.SetCalibration(position->position, |
| 230 | values.shooter.pusher_distal.lower_angle); |
| 231 | } else if (position->pusher_proximal.current) { |
| 232 | shooter_.SetCalibration(position->position, |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame] | 233 | values.shooter.pusher_proximal.upper_angle); |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 234 | } else { |
| 235 | shooter_.SetCalibration(position->position, |
| 236 | values.shooter.upper_limit); |
| 237 | } |
| 238 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 239 | // Go to the current position. |
| 240 | shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0); |
| 241 | // If the plunger is all the way back, we want to be latched. |
| 242 | latch_piston_ = position->plunger; |
| 243 | brake_piston_ = false; |
Austin Schuh | 1a08bd8 | 2014-03-09 00:47:11 -0800 | [diff] [blame] | 244 | if (position->latch == latch_piston_) { |
| 245 | state_ = STATE_REQUEST_LOAD; |
| 246 | } else { |
| 247 | shooter_loop_disable = true; |
Brian Silverman | f48fab3 | 2014-03-09 14:32:24 -0700 | [diff] [blame^] | 248 | LOG(DEBUG, |
| 249 | "Not moving on until the latch has moved to avoid a crash\n"); |
Austin Schuh | 1a08bd8 | 2014-03-09 00:47:11 -0800 | [diff] [blame] | 250 | } |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 251 | } else { |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 252 | // If we can't start yet because we don't know where we are, set the |
| 253 | // latch and brake to their defaults. |
| 254 | latch_piston_ = true; |
| 255 | brake_piston_ = true; |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 256 | } |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 257 | break; |
| 258 | case STATE_REQUEST_LOAD: |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 259 | if (position) { |
Austin Schuh | d30d738 | 2014-03-02 21:15:38 -0800 | [diff] [blame] | 260 | zeroed_ = false; |
Ben Fredrickson | 26a0dee | 2014-02-23 04:38:23 +0000 | [diff] [blame] | 261 | if (position->pusher_distal.current || |
| 262 | position->pusher_proximal.current) { |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 263 | // We started on the sensor, back up until we are found. |
| 264 | // If the plunger is all the way back and not latched, it won't be |
| 265 | // there for long. |
| 266 | state_ = STATE_LOAD_BACKTRACK; |
Austin Schuh | 06cbbf1 | 2014-02-22 02:07:31 -0800 | [diff] [blame] | 267 | |
| 268 | // The plunger is already back and latched. Don't release it. |
| 269 | if (position->plunger && position->latch) { |
| 270 | latch_piston_ = true; |
| 271 | } else { |
| 272 | latch_piston_ = false; |
| 273 | } |
| 274 | } else if (position->plunger && position->latch) { |
| 275 | // The plunger is back and we are latched. We most likely got here |
| 276 | // from Initialize, in which case we want to 'load' again anyways to |
| 277 | // zero. |
| 278 | Load(); |
| 279 | latch_piston_ = true; |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 280 | } else { |
| 281 | // Off the sensor, start loading. |
| 282 | Load(); |
| 283 | latch_piston_ = false; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 284 | } |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 287 | // Hold our current position. |
| 288 | shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0); |
| 289 | brake_piston_ = false; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 290 | break; |
| 291 | case STATE_LOAD_BACKTRACK: |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 292 | // If we are here, then that means we started past the edge where we want |
| 293 | // to zero. Move backwards until we don't see the sensor anymore. |
| 294 | // The plunger is contacting the pusher (or will be shortly). |
| 295 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 296 | if (!disabled) { |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 297 | shooter_.SetGoalPosition( |
Austin Schuh | faeee63 | 2014-02-18 01:24:05 -0800 | [diff] [blame] | 298 | shooter_.goal_position() + values.shooter.zeroing_speed * dt, |
| 299 | values.shooter.zeroing_speed); |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 300 | } |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame] | 301 | cap_goal = true; |
Austin Schuh | 06cbbf1 | 2014-02-22 02:07:31 -0800 | [diff] [blame] | 302 | shooter_.set_max_voltage(4.0); |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 303 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 304 | if (position) { |
Ben Fredrickson | 26a0dee | 2014-02-23 04:38:23 +0000 | [diff] [blame] | 305 | if (!position->pusher_distal.current && |
| 306 | !position->pusher_proximal.current) { |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 307 | Load(); |
| 308 | } |
Austin Schuh | 6b42860 | 2014-02-22 21:02:00 -0800 | [diff] [blame] | 309 | latch_piston_ = position->plunger; |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 312 | brake_piston_ = false; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 313 | break; |
| 314 | case STATE_LOAD: |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 315 | // If we are disabled right now, reset the timer. |
| 316 | if (disabled) { |
| 317 | Load(); |
| 318 | // Latch defaults to true when disabled. Leave it latched until we have |
| 319 | // useful sensor data. |
| 320 | latch_piston_ = true; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 321 | } |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 322 | // Go to 0, which should be the latch position, or trigger a hall effect |
| 323 | // on the way. If we don't see edges where we are supposed to, the |
| 324 | // offset will be updated by code above. |
| 325 | shooter_.SetGoalPosition(0.0, 0.0); |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 326 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 327 | if (position) { |
Austin Schuh | d30d738 | 2014-03-02 21:15:38 -0800 | [diff] [blame] | 328 | // TODO(austin): Validate that this is the right edge. |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 329 | // If we see a posedge on any of the hall effects, |
| 330 | if (position->pusher_proximal.posedge_count != |
Austin Schuh | 2e97681 | 2014-03-05 19:56:58 -0800 | [diff] [blame] | 331 | last_proximal_posedge_count_ && |
| 332 | !last_proximal_current_) { |
Austin Schuh | d30d738 | 2014-03-02 21:15:38 -0800 | [diff] [blame] | 333 | proximal_posedge_validation_cycles_left_ = 2; |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 334 | } |
Austin Schuh | d30d738 | 2014-03-02 21:15:38 -0800 | [diff] [blame] | 335 | if (proximal_posedge_validation_cycles_left_ > 0) { |
| 336 | if (position->pusher_proximal.current) { |
| 337 | --proximal_posedge_validation_cycles_left_; |
| 338 | if (proximal_posedge_validation_cycles_left_ == 0) { |
| 339 | shooter_.SetCalibration( |
| 340 | position->pusher_proximal.posedge_value, |
| 341 | values.shooter.pusher_proximal.upper_angle); |
| 342 | |
| 343 | LOG(DEBUG, "Setting calibration using proximal sensor\n"); |
| 344 | zeroed_ = true; |
| 345 | } |
| 346 | } else { |
| 347 | proximal_posedge_validation_cycles_left_ = 0; |
| 348 | } |
| 349 | } |
| 350 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 351 | if (position->pusher_distal.posedge_count != |
Austin Schuh | 2e97681 | 2014-03-05 19:56:58 -0800 | [diff] [blame] | 352 | last_distal_posedge_count_ && |
| 353 | !last_distal_current_) { |
Austin Schuh | d30d738 | 2014-03-02 21:15:38 -0800 | [diff] [blame] | 354 | distal_posedge_validation_cycles_left_ = 2; |
| 355 | } |
| 356 | if (distal_posedge_validation_cycles_left_ > 0) { |
| 357 | if (position->pusher_distal.current) { |
| 358 | --distal_posedge_validation_cycles_left_; |
| 359 | if (distal_posedge_validation_cycles_left_ == 0) { |
| 360 | shooter_.SetCalibration( |
| 361 | position->pusher_distal.posedge_value, |
| 362 | values.shooter.pusher_distal.upper_angle); |
| 363 | |
| 364 | LOG(DEBUG, "Setting calibration using distal sensor\n"); |
| 365 | zeroed_ = true; |
| 366 | } |
| 367 | } else { |
| 368 | distal_posedge_validation_cycles_left_ = 0; |
| 369 | } |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 370 | } |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 371 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 372 | // Latch if the plunger is far enough back to trigger the hall effect. |
| 373 | // This happens when the distal sensor is triggered. |
Austin Schuh | 06cbbf1 | 2014-02-22 02:07:31 -0800 | [diff] [blame] | 374 | latch_piston_ = position->pusher_distal.current || position->plunger; |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 375 | |
Austin Schuh | 06cbbf1 | 2014-02-22 02:07:31 -0800 | [diff] [blame] | 376 | // Check if we are latched and back. Make sure the plunger is all the |
| 377 | // way back as well. |
| 378 | if (position->plunger && position->latch && |
| 379 | position->pusher_distal.current) { |
Austin Schuh | d30d738 | 2014-03-02 21:15:38 -0800 | [diff] [blame] | 380 | if (!zeroed_) { |
| 381 | state_ = STATE_REQUEST_LOAD; |
| 382 | } else { |
| 383 | state_ = STATE_PREPARE_SHOT; |
| 384 | } |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 385 | } else if (position->plunger && |
| 386 | ::std::abs(shooter_.absolute_position() - |
| 387 | shooter_.goal_position()) < 0.001) { |
| 388 | // We are at the goal, but not latched. |
| 389 | state_ = STATE_LOADING_PROBLEM; |
Austin Schuh | 06cbbf1 | 2014-02-22 02:07:31 -0800 | [diff] [blame] | 390 | loading_problem_end_time_ = Time::Now() + kLoadProblemEndTimeout; |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 391 | } |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 392 | } |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 393 | if (load_timeout_ < Time::Now()) { |
| 394 | if (position) { |
| 395 | if (!position->pusher_distal.current || |
| 396 | !position->pusher_proximal.current) { |
| 397 | state_ = STATE_ESTOP; |
Brian Silverman | f48fab3 | 2014-03-09 14:32:24 -0700 | [diff] [blame^] | 398 | LOG(ERROR, "Estopping because took too long to load.\n"); |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | } else if (goal->unload_requested) { |
| 402 | Unload(); |
| 403 | } |
| 404 | brake_piston_ = false; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 405 | break; |
| 406 | case STATE_LOADING_PROBLEM: |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 407 | if (disabled) { |
Austin Schuh | d30d738 | 2014-03-02 21:15:38 -0800 | [diff] [blame] | 408 | state_ = STATE_REQUEST_LOAD; |
| 409 | break; |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 410 | } |
| 411 | // We got to the goal, but the latch hasn't registered as down. It might |
| 412 | // be stuck, or on it's way but not there yet. |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 413 | if (Time::Now() > loading_problem_end_time_) { |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 414 | // Timeout by unloading. |
| 415 | Unload(); |
| 416 | } else if (position && position->plunger && position->latch) { |
| 417 | // If both trigger, we are latched. |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 418 | state_ = STATE_PREPARE_SHOT; |
| 419 | } |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 420 | // Move a bit further back to help it trigger. |
| 421 | // If the latch is slow due to the air flowing through the tubes or |
| 422 | // inertia, but is otherwise free, this won't have much time to do |
| 423 | // anything and is safe. Otherwise this gives us a bit more room to free |
| 424 | // up the latch. |
| 425 | shooter_.SetGoalPosition(values.shooter.lower_limit, 0.0); |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 426 | LOG(DEBUG, "Waiting on latch: plunger %d, latch: %d\n", |
Austin Schuh | 60c5666 | 2014-02-17 14:37:19 -0800 | [diff] [blame] | 427 | position->plunger, position->latch); |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 428 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 429 | latch_piston_ = true; |
| 430 | brake_piston_ = false; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 431 | break; |
| 432 | case STATE_PREPARE_SHOT: |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 433 | // Move the shooter to the shot power set point and then lock the brake. |
| 434 | // TODO(austin): Timeout. Low priority. |
| 435 | |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 436 | shooter_.SetGoalPosition(PowerToPosition(goal->shot_power), 0.0); |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 437 | |
| 438 | LOG(DEBUG, "PDIFF: absolute_position: %.2f, pow: %.2f\n", |
Austin Schuh | be1401f | 2014-02-18 03:18:41 -0800 | [diff] [blame] | 439 | shooter_.absolute_position(), PowerToPosition(goal->shot_power)); |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 440 | if (::std::abs(shooter_.absolute_position() - |
Austin Schuh | be1401f | 2014-02-18 03:18:41 -0800 | [diff] [blame] | 441 | PowerToPosition(goal->shot_power)) + |
| 442 | ::std::abs(shooter_.absolute_velocity()) < |
| 443 | 0.001) { |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 444 | // We are there, set the brake and move on. |
| 445 | latch_piston_ = true; |
| 446 | brake_piston_ = true; |
Austin Schuh | 06cbbf1 | 2014-02-22 02:07:31 -0800 | [diff] [blame] | 447 | shooter_brake_set_time_ = Time::Now() + kShooterBrakeSetTime; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 448 | state_ = STATE_READY; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 449 | } else { |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 450 | latch_piston_ = true; |
| 451 | brake_piston_ = false; |
| 452 | } |
| 453 | if (goal->unload_requested) { |
| 454 | Unload(); |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 455 | } |
| 456 | break; |
| 457 | case STATE_READY: |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 458 | LOG(DEBUG, "In ready\n"); |
Austin Schuh | be1401f | 2014-02-18 03:18:41 -0800 | [diff] [blame] | 459 | // Wait until the brake is set, and a shot is requested or the shot power |
| 460 | // is changed. |
Austin Schuh | 6b42860 | 2014-02-22 21:02:00 -0800 | [diff] [blame] | 461 | if (Time::Now() > shooter_brake_set_time_) { |
James Kuszmaul | 4abaf48 | 2014-02-26 21:16:35 -0800 | [diff] [blame] | 462 | status->ready = true; |
Austin Schuh | be1401f | 2014-02-18 03:18:41 -0800 | [diff] [blame] | 463 | // We have waited long enough for the brake to set, turn the shooter |
| 464 | // control loop off. |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 465 | shooter_loop_disable = true; |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 466 | LOG(DEBUG, "Brake is now set\n"); |
| 467 | if (goal->shot_requested && !disabled) { |
| 468 | LOG(DEBUG, "Shooting now\n"); |
| 469 | shooter_loop_disable = true; |
James Kuszmaul | 7290a94 | 2014-02-26 20:04:13 -0800 | [diff] [blame] | 470 | shot_end_time_ = Time::Now() + kShotEndTimeout; |
| 471 | firing_starting_position_ = shooter_.absolute_position(); |
| 472 | state_ = STATE_FIRE; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 473 | } |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 474 | } |
Austin Schuh | 6b42860 | 2014-02-22 21:02:00 -0800 | [diff] [blame] | 475 | if (state_ == STATE_READY && |
| 476 | ::std::abs(shooter_.absolute_position() - |
| 477 | PowerToPosition(goal->shot_power)) > 0.002) { |
| 478 | // TODO(austin): Add a state to release the brake. |
| 479 | |
| 480 | // TODO(austin): Do we want to set the brake here or after shooting? |
| 481 | // Depends on air usage. |
James Kuszmaul | 4abaf48 | 2014-02-26 21:16:35 -0800 | [diff] [blame] | 482 | status->ready = false; |
Austin Schuh | 6b42860 | 2014-02-22 21:02:00 -0800 | [diff] [blame] | 483 | LOG(DEBUG, "Preparing shot again.\n"); |
| 484 | state_ = STATE_PREPARE_SHOT; |
| 485 | } |
| 486 | |
Ben Fredrickson | a6d7754 | 2014-02-17 07:54:43 +0000 | [diff] [blame] | 487 | shooter_.SetGoalPosition(PowerToPosition(goal->shot_power), 0.0); |
| 488 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 489 | latch_piston_ = true; |
| 490 | brake_piston_ = true; |
| 491 | |
| 492 | if (goal->unload_requested) { |
| 493 | Unload(); |
| 494 | } |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 495 | break; |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 496 | |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 497 | case STATE_FIRE: |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 498 | if (disabled) { |
| 499 | if (position) { |
| 500 | if (position->plunger) { |
| 501 | // If disabled and the plunger is still back there, reset the |
| 502 | // timeout. |
Austin Schuh | 06cbbf1 | 2014-02-22 02:07:31 -0800 | [diff] [blame] | 503 | shot_end_time_ = Time::Now() + kShotEndTimeout; |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | } |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 507 | shooter_loop_disable = true; |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 508 | // Count the number of contiguous cycles during which we haven't moved. |
| 509 | if (::std::abs(last_position_.position - shooter_.absolute_position()) < |
| 510 | 0.0005) { |
| 511 | ++cycles_not_moved_; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 512 | } else { |
| 513 | cycles_not_moved_ = 0; |
| 514 | } |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 515 | |
| 516 | // If we have moved any amount since the start and the shooter has now |
| 517 | // been still for a couple cycles, the shot finished. |
| 518 | // Also move on if it times out. |
| 519 | if ((::std::abs(firing_starting_position_ - |
| 520 | shooter_.absolute_position()) > 0.0005 && |
| 521 | cycles_not_moved_ > 3) || |
| 522 | Time::Now() > shot_end_time_) { |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 523 | state_ = STATE_REQUEST_LOAD; |
Austin Schuh | 4edad87 | 2014-03-02 11:56:12 -0800 | [diff] [blame] | 524 | ++shot_count_; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 525 | } |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 526 | latch_piston_ = false; |
| 527 | brake_piston_ = true; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 528 | break; |
| 529 | case STATE_UNLOAD: |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 530 | // Reset the timeouts. |
| 531 | if (disabled) Unload(); |
| 532 | |
| 533 | // If it is latched and the plunger is back, move the pusher back to catch |
| 534 | // the plunger. |
Austin Schuh | f84a130 | 2014-02-19 00:23:30 -0800 | [diff] [blame] | 535 | bool all_back; |
| 536 | if (position) { |
| 537 | all_back = position->plunger && position->latch; |
| 538 | } else { |
| 539 | all_back = last_position_.plunger && last_position_.latch; |
| 540 | } |
| 541 | |
| 542 | if (all_back) { |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 543 | // Pull back to 0, 0. |
| 544 | shooter_.SetGoalPosition(0.0, 0.0); |
| 545 | if (shooter_.absolute_position() < 0.005) { |
| 546 | // When we are close enough, 'fire'. |
| 547 | latch_piston_ = false; |
| 548 | } else { |
| 549 | latch_piston_ = true; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 550 | } |
| 551 | } else { |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 552 | // The plunger isn't all the way back, or it is and it is unlatched, so |
| 553 | // we can now unload. |
| 554 | shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0); |
| 555 | latch_piston_ = false; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 556 | state_ = STATE_UNLOAD_MOVE; |
Austin Schuh | 06cbbf1 | 2014-02-22 02:07:31 -0800 | [diff] [blame] | 557 | unload_timeout_ = Time::Now() + kUnloadTimeout; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 560 | if (Time::Now() > unload_timeout_) { |
| 561 | // We have been stuck trying to unload for way too long, give up and |
| 562 | // turn everything off. |
| 563 | state_ = STATE_ESTOP; |
Brian Silverman | f48fab3 | 2014-03-09 14:32:24 -0700 | [diff] [blame^] | 564 | LOG(ERROR, "Estopping because took too long to unload.\n"); |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | brake_piston_ = false; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 568 | break; |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 569 | case STATE_UNLOAD_MOVE: { |
| 570 | if (disabled) { |
Austin Schuh | 06cbbf1 | 2014-02-22 02:07:31 -0800 | [diff] [blame] | 571 | unload_timeout_ = Time::Now() + kUnloadTimeout; |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 572 | shooter_.SetGoalPosition(shooter_.absolute_position(), 0.0); |
| 573 | } |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame] | 574 | cap_goal = true; |
Austin Schuh | 6b42860 | 2014-02-22 21:02:00 -0800 | [diff] [blame] | 575 | shooter_.set_max_voltage(6.0); |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 576 | |
| 577 | // Slowly move back until we hit the upper limit. |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame] | 578 | // If we were at the limit last cycle, we are done unloading. |
| 579 | // This is because if we saturate, we might hit the limit before we are |
| 580 | // actually there. |
| 581 | if (shooter_.goal_position() >= values.shooter.upper_limit) { |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 582 | shooter_.SetGoalPosition(values.shooter.upper_limit, 0.0); |
| 583 | // We don't want the loop fighting the spring when we are unloaded. |
| 584 | // Turn it off. |
| 585 | shooter_loop_disable = true; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 586 | state_ = STATE_READY_UNLOAD; |
| 587 | } else { |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame] | 588 | shooter_.SetGoalPosition( |
| 589 | ::std::min( |
| 590 | values.shooter.upper_limit, |
Austin Schuh | 6b42860 | 2014-02-22 21:02:00 -0800 | [diff] [blame] | 591 | shooter_.goal_position() + values.shooter.unload_speed * dt), |
| 592 | values.shooter.unload_speed); |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 595 | latch_piston_ = false; |
| 596 | brake_piston_ = false; |
| 597 | } break; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 598 | case STATE_READY_UNLOAD: |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 599 | if (goal->load_requested) { |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 600 | state_ = STATE_REQUEST_LOAD; |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 601 | } |
James Kuszmaul | 7290a94 | 2014-02-26 20:04:13 -0800 | [diff] [blame] | 602 | // If we are ready to load again, |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 603 | shooter_loop_disable = true; |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 604 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 605 | latch_piston_ = false; |
| 606 | brake_piston_ = false; |
| 607 | break; |
| 608 | |
| 609 | case STATE_ESTOP: |
Brian Silverman | f48fab3 | 2014-03-09 14:32:24 -0700 | [diff] [blame^] | 610 | LOG(DEBUG, "estopped\n"); |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 611 | // Totally lost, go to a safe state. |
| 612 | shooter_loop_disable = true; |
| 613 | latch_piston_ = true; |
| 614 | brake_piston_ = true; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 615 | break; |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 616 | } |
| 617 | |
James Kuszmaul | 7290a94 | 2014-02-26 20:04:13 -0800 | [diff] [blame] | 618 | if (!shooter_loop_disable) { |
Brian Silverman | f48fab3 | 2014-03-09 14:32:24 -0700 | [diff] [blame^] | 619 | LOG_STRUCT(DEBUG, "running the loop", |
| 620 | ShooterStatusToLog(shooter_.goal_position(), |
| 621 | shooter_.absolute_position())); |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame] | 622 | if (!cap_goal) { |
| 623 | shooter_.set_max_voltage(12.0); |
| 624 | } |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 625 | shooter_.Update(output == NULL); |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame] | 626 | if (cap_goal) { |
| 627 | shooter_.CapGoal(); |
| 628 | } |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 629 | if (output) output->voltage = shooter_.voltage(); |
Ben Fredrickson | 7d980c2 | 2014-02-16 21:39:02 +0000 | [diff] [blame] | 630 | } else { |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 631 | shooter_.Update(true); |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame] | 632 | shooter_.ZeroPower(); |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 633 | if (output) output->voltage = 0.0; |
Ben Fredrickson | edf0e09 | 2014-02-16 10:46:50 +0000 | [diff] [blame] | 634 | } |
Ben Fredrickson | 7d980c2 | 2014-02-16 21:39:02 +0000 | [diff] [blame] | 635 | |
Austin Schuh | f4392d4 | 2014-03-02 14:00:37 -0800 | [diff] [blame] | 636 | status->hard_stop_power = PositionToPower(shooter_.absolute_position()); |
James Kuszmaul | d29689f | 2014-03-02 13:06:54 -0800 | [diff] [blame] | 637 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 638 | if (output) { |
| 639 | output->latch_piston = latch_piston_; |
| 640 | output->brake_piston = brake_piston_; |
| 641 | } |
| 642 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 643 | if (position) { |
Brian Silverman | f48fab3 | 2014-03-09 14:32:24 -0700 | [diff] [blame^] | 644 | LOG_STRUCT(DEBUG, "internal state", |
| 645 | ShooterStateToLog( |
| 646 | shooter_.absolute_position(), shooter_.absolute_velocity(), |
| 647 | state_, position->latch, position->pusher_proximal.current, |
| 648 | position->pusher_distal.current, position->plunger, |
| 649 | brake_piston_, latch_piston_)); |
| 650 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 651 | last_position_ = *position; |
Brian Silverman | f48fab3 | 2014-03-09 14:32:24 -0700 | [diff] [blame^] | 652 | |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 653 | last_distal_posedge_count_ = position->pusher_distal.posedge_count; |
| 654 | last_proximal_posedge_count_ = position->pusher_proximal.posedge_count; |
Austin Schuh | 2e97681 | 2014-03-05 19:56:58 -0800 | [diff] [blame] | 655 | last_distal_current_ = position->pusher_distal.current; |
| 656 | last_proximal_current_ = position->pusher_proximal.current; |
Austin Schuh | 3053788 | 2014-02-18 01:07:23 -0800 | [diff] [blame] | 657 | } |
Austin Schuh | 4edad87 | 2014-03-02 11:56:12 -0800 | [diff] [blame] | 658 | |
| 659 | status->shots = shot_count_; |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 660 | } |
| 661 | |
Brian Silverman | d1e65b9 | 2014-03-08 17:07:14 -0800 | [diff] [blame] | 662 | void ShooterMotor::ZeroOutputs() { |
| 663 | queue_group()->output.MakeWithBuilder() |
| 664 | .voltage(0) |
| 665 | .latch_piston(latch_piston_) |
| 666 | .brake_piston(brake_piston_) |
| 667 | .Send(); |
| 668 | } |
| 669 | |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 670 | } // namespace control_loops |
| 671 | } // namespace frc971 |