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