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