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