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