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