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