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