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