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