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 | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 25 | // TODO(ben): Limit the voltage if we are ever not certain that things are |
| 26 | // working. |
| 27 | double limit = 12.0; |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 28 | |
| 29 | // Make sure that reality and the observer can't get too far off. There is a |
| 30 | // delay by one cycle between the applied voltage and X_hat(2, 0), so compare |
| 31 | // against last cycle's voltage. |
| 32 | if (X_hat(2, 0) > last_voltage_ + 2.0) { |
| 33 | voltage_ -= X_hat(2, 0) - (last_voltage_ + 2.0); |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 34 | //LOG(DEBUG, "X_hat(2, 0) = %f\n", X_hat(2, 0)); |
| 35 | } else if (X_hat(2, 0) < last_voltage_ - 2.0) { |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 36 | voltage_ += X_hat(2, 0) - (last_voltage_ - 2.0); |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 37 | //LOG(DEBUG, "X_hat(2, 0) = %f\n", X_hat(2, 0)); |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | voltage_ = std::min(limit, voltage_); |
| 41 | voltage_ = std::max(-limit, voltage_); |
| 42 | U(0, 0) = voltage_ - old_voltage; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 43 | //LOG(DEBUG, "abc %f\n", X_hat(2, 0) - voltage_); |
| 44 | //LOG(DEBUG, "error %f\n", X_hat(0, 0) - R(0, 0)); |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 45 | |
| 46 | last_voltage_ = voltage_; |
| 47 | } |
| 48 | |
Ben Fredrickson | edf0e09 | 2014-02-16 10:46:50 +0000 | [diff] [blame] | 49 | ShooterMotor::ShooterMotor(control_loops::ShooterGroup *my_shooter) |
| 50 | : aos::control_loops::ControlLoop<control_loops::ShooterGroup>(my_shooter), |
| 51 | shooter_(MakeShooterLoop()), |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 52 | calibration_position_(0.0), |
| 53 | state_(STATE_INITIALIZE), |
| 54 | loading_problem_end_time_(0, 0), |
| 55 | shooter_brake_set_time_(0, 0), |
| 56 | prepare_fire_end_time_(0, 0), |
| 57 | shot_end_time_(0, 0), |
| 58 | cycles_not_moved_(0), |
| 59 | initial_loop_(true) {} |
Ben Fredrickson | 1e512ff | 2014-02-15 21:36:52 +0000 | [diff] [blame] | 60 | |
| 61 | // Positive is out, and positive power is out. |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 62 | void ShooterMotor::RunIteration( |
Ben Fredrickson | edf0e09 | 2014-02-16 10:46:50 +0000 | [diff] [blame] | 63 | const control_loops::ShooterGroup::Goal *goal, |
| 64 | const control_loops::ShooterGroup::Position *position, |
| 65 | control_loops::ShooterGroup::Output *output, |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 66 | control_loops::ShooterGroup::Status *status) { |
Ben Fredrickson | 1e512ff | 2014-02-15 21:36:52 +0000 | [diff] [blame] | 67 | constexpr double dt = 0.01; |
| 68 | |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 69 | // we must always have these or we have issues. |
| 70 | if (goal == NULL || status == NULL) { |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 71 | if (output) output->voltage = 0; |
| 72 | LOG(ERROR, "Thought I would just check for null and die.\n"); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | if (initial_loop_) { |
Austin Schuh | 60c5666 | 2014-02-17 14:37:19 -0800 | [diff] [blame^] | 77 | // TODO(austin): If 'reset()', we are lost, start over. |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 78 | initial_loop_ = false; |
| 79 | shooter_.SetPositionDirectly(position->position); |
| 80 | } else { |
| 81 | shooter_.SetPositionValues(position->position); |
Ben Fredrickson | 1e512ff | 2014-02-15 21:36:52 +0000 | [diff] [blame] | 82 | } |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 83 | |
| 84 | // Disable the motors now so that all early returns will return with the |
| 85 | // motors disabled. |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 86 | if (output) output->voltage = 0; |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 87 | |
Ben Fredrickson | 1e512ff | 2014-02-15 21:36:52 +0000 | [diff] [blame] | 88 | const frc971::constants::Values &values = constants::GetValues(); |
| 89 | |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 90 | double real_position = shooter_.position(); |
| 91 | double adjusted_position = shooter_.position() - calibration_position_; |
| 92 | |
| 93 | if (position) { |
| 94 | last_position_ = *position; |
| 95 | LOG(DEBUG, |
| 96 | "pos > real: %.2f adjusted: %.2f raw: %.2f calib: %.2f state= %d\n", |
| 97 | real_position, adjusted_position, position->position, |
| 98 | calibration_position_, state_); |
| 99 | } |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 100 | |
Brian Silverman | aae236a | 2014-02-17 01:49:39 -0800 | [diff] [blame] | 101 | // Don't even let the control loops run. |
Ben Fredrickson | edf0e09 | 2014-02-16 10:46:50 +0000 | [diff] [blame] | 102 | bool shooter_loop_disable = false; |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 103 | |
Brian Silverman | aae236a | 2014-02-17 01:49:39 -0800 | [diff] [blame] | 104 | // Adds voltage to take up slack in gears before shot. |
Ben Fredrickson | 7d980c2 | 2014-02-16 21:39:02 +0000 | [diff] [blame] | 105 | bool apply_some_voltage = false; |
| 106 | |
Ben Fredrickson | 1e512ff | 2014-02-15 21:36:52 +0000 | [diff] [blame] | 107 | switch (state_) { |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 108 | case STATE_INITIALIZE: |
Brian Silverman | aae236a | 2014-02-17 01:49:39 -0800 | [diff] [blame] | 109 | // Start off with the assumption that we are at the value |
| 110 | // futhest back given our sensors. |
| 111 | if (position && position->pusher_distal.current) { |
Austin Schuh | 60c5666 | 2014-02-17 14:37:19 -0800 | [diff] [blame^] | 112 | //TODO(ben): use posedge |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 113 | calibration_position_ = |
Brian Silverman | aae236a | 2014-02-17 01:49:39 -0800 | [diff] [blame] | 114 | position->position - values.shooter.pusher_distal.lower_angle; |
| 115 | } else if (position && position->pusher_proximal.current) { |
Austin Schuh | 60c5666 | 2014-02-17 14:37:19 -0800 | [diff] [blame^] | 116 | //TODO(ben): use posedge |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 117 | calibration_position_ = |
Brian Silverman | aae236a | 2014-02-17 01:49:39 -0800 | [diff] [blame] | 118 | position->position - values.shooter.pusher_proximal.lower_angle; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 119 | } |
Ben Fredrickson | 1e512ff | 2014-02-15 21:36:52 +0000 | [diff] [blame] | 120 | |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 121 | state_ = STATE_REQUEST_LOAD; |
Ben Fredrickson | 1e512ff | 2014-02-15 21:36:52 +0000 | [diff] [blame] | 122 | |
Brian Silverman | aae236a | 2014-02-17 01:49:39 -0800 | [diff] [blame] | 123 | // Zero out initial goal. |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 124 | shooter_.SetGoalPosition(real_position, 0.0); |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 125 | if (position) { |
Austin Schuh | 60c5666 | 2014-02-17 14:37:19 -0800 | [diff] [blame^] | 126 | output->latch_piston = position->plunger; |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 127 | } else { |
Brian Silverman | aae236a | 2014-02-17 01:49:39 -0800 | [diff] [blame] | 128 | // We don't know what is going on so just close the latch to be safe. |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 129 | output->latch_piston = true; |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 130 | } |
| 131 | output->brake_piston = false; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 132 | break; |
| 133 | case STATE_REQUEST_LOAD: |
Austin Schuh | 60c5666 | 2014-02-17 14:37:19 -0800 | [diff] [blame^] | 134 | if (position->plunger && position->latch) { |
Brian Silverman | aae236a | 2014-02-17 01:49:39 -0800 | [diff] [blame] | 135 | // Already latched. |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 136 | state_ = STATE_PREPARE_SHOT; |
Austin Schuh | 60c5666 | 2014-02-17 14:37:19 -0800 | [diff] [blame^] | 137 | } else if (position->pusher_distal.current || (adjusted_position) < 0) { |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 138 | state_ = STATE_LOAD_BACKTRACK; |
Austin Schuh | 60c5666 | 2014-02-17 14:37:19 -0800 | [diff] [blame^] | 139 | // TODO(ben): double check that rezero is the right thing to do here |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 140 | if (position) { |
| 141 | calibration_position_ = position->position; |
| 142 | } |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 143 | } else { |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 144 | state_ = STATE_LOAD; |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 147 | shooter_.SetGoalPosition(0.0, 0.0); |
Austin Schuh | 60c5666 | 2014-02-17 14:37:19 -0800 | [diff] [blame^] | 148 | if (position && output) { |
| 149 | output->latch_piston = position->plunger; |
| 150 | } |
| 151 | if (output) { |
| 152 | output->brake_piston = false; |
| 153 | } |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 154 | break; |
| 155 | case STATE_LOAD_BACKTRACK: |
Brian Silverman | aae236a | 2014-02-17 01:49:39 -0800 | [diff] [blame] | 156 | if (adjusted_position > values.shooter.pusher_distal.upper_angle + 0.01) { |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 157 | shooter_.SetGoalPosition( |
Austin Schuh | 60c5666 | 2014-02-17 14:37:19 -0800 | [diff] [blame^] | 158 | real_position - values.shooter.zeroing_speed * dt, |
| 159 | values.shooter.zeroing_speed); |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 160 | } else { |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 161 | state_ = STATE_LOAD; |
| 162 | } |
| 163 | |
| 164 | if (output) output->latch_piston = false; |
| 165 | if (output) output->brake_piston = true; |
| 166 | break; |
| 167 | case STATE_LOAD: |
Brian Silverman | aae236a | 2014-02-17 01:49:39 -0800 | [diff] [blame] | 168 | if (position && position->pusher_proximal.current && |
| 169 | !last_position_.pusher_proximal.current) { |
Austin Schuh | 60c5666 | 2014-02-17 14:37:19 -0800 | [diff] [blame^] | 170 | //TODO(ben): use posedge |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 171 | calibration_position_ = |
Brian Silverman | aae236a | 2014-02-17 01:49:39 -0800 | [diff] [blame] | 172 | position->position - values.shooter.pusher_proximal.upper_angle; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 173 | } |
Brian Silverman | aae236a | 2014-02-17 01:49:39 -0800 | [diff] [blame] | 174 | if (position && position->pusher_distal.current && |
| 175 | !last_position_.pusher_distal.current) { |
Austin Schuh | 60c5666 | 2014-02-17 14:37:19 -0800 | [diff] [blame^] | 176 | //TODO(ben): use posedge |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 177 | calibration_position_ = |
Brian Silverman | aae236a | 2014-02-17 01:49:39 -0800 | [diff] [blame] | 178 | position->position - values.shooter.pusher_distal.lower_angle; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | shooter_.SetGoalPosition(calibration_position_, 0.0); |
| 182 | if (position && output) { |
Austin Schuh | 60c5666 | 2014-02-17 14:37:19 -0800 | [diff] [blame^] | 183 | output->latch_piston = position->plunger; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Austin Schuh | 60c5666 | 2014-02-17 14:37:19 -0800 | [diff] [blame^] | 186 | if (position->plunger && position->latch) { |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 187 | state_ = STATE_PREPARE_SHOT; |
Austin Schuh | 60c5666 | 2014-02-17 14:37:19 -0800 | [diff] [blame^] | 188 | } else if (position->plunger && |
Ben Fredrickson | a6d7754 | 2014-02-17 07:54:43 +0000 | [diff] [blame] | 189 | fabs(adjusted_position - PowerToPosition(goal->shot_power)) < |
Austin Schuh | 60c5666 | 2014-02-17 14:37:19 -0800 | [diff] [blame^] | 190 | 0.05) { |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 191 | state_ = STATE_LOADING_PROBLEM; |
| 192 | loading_problem_end_time_ = |
| 193 | Time::Now(Time::kDefaultClock) + Time::InSeconds(3.0); |
| 194 | } |
| 195 | if (output) output->brake_piston = false; |
| 196 | break; |
| 197 | case STATE_LOADING_PROBLEM: |
| 198 | if (Time::Now() > loading_problem_end_time_) { |
| 199 | state_ = STATE_UNLOAD; |
Austin Schuh | 60c5666 | 2014-02-17 14:37:19 -0800 | [diff] [blame^] | 200 | } else if (position->plunger && position->latch) { |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 201 | state_ = STATE_PREPARE_SHOT; |
| 202 | } |
| 203 | shooter_.SetGoalPosition(calibration_position_, 0.0); |
| 204 | LOG(DEBUG, "Waiting on latch: plunger %d, latch: %d\n", |
Austin Schuh | 60c5666 | 2014-02-17 14:37:19 -0800 | [diff] [blame^] | 205 | position->plunger, position->latch); |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 206 | |
| 207 | if (output) output->latch_piston = true; |
| 208 | if (output) output->brake_piston = false; |
| 209 | break; |
| 210 | case STATE_PREPARE_SHOT: |
| 211 | shooter_.SetGoalPosition(PowerToPosition(goal->shot_power), 0.0); |
Ben Fredrickson | a6d7754 | 2014-02-17 07:54:43 +0000 | [diff] [blame] | 212 | LOG(DEBUG, "PDIFF: adjusted_position: %.2f, pow: %.2f\n", |
| 213 | adjusted_position, PowerToPosition(goal->shot_power)); |
| 214 | if (fabs(adjusted_position - PowerToPosition(goal->shot_power)) < 0.05) { |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 215 | state_ = STATE_READY; |
| 216 | output->latch_piston = true; |
| 217 | output->brake_piston = true; |
| 218 | shooter_brake_set_time_ = |
Ben Fredrickson | a6d7754 | 2014-02-17 07:54:43 +0000 | [diff] [blame] | 219 | Time::Now(Time::kDefaultClock) + Time::InSeconds(0.03); |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 220 | } else { |
| 221 | output->latch_piston = true; |
| 222 | output->brake_piston = false; |
| 223 | } |
| 224 | break; |
| 225 | case STATE_READY: |
| 226 | if (Time::Now() > shooter_brake_set_time_) { |
| 227 | shooter_loop_disable = true; |
| 228 | if (goal->unload_requested) { |
Ben Fredrickson | a6d7754 | 2014-02-17 07:54:43 +0000 | [diff] [blame] | 229 | printf("GHA\n"); |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 230 | state_ = STATE_UNLOAD; |
Ben Fredrickson | a6d7754 | 2014-02-17 07:54:43 +0000 | [diff] [blame] | 231 | } else if (fabs(adjusted_position - PowerToPosition(goal->shot_power)) > |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 232 | 0.05) { |
Ben Fredrickson | a6d7754 | 2014-02-17 07:54:43 +0000 | [diff] [blame] | 233 | printf("GHB\n"); |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 234 | state_ = STATE_PREPARE_SHOT; |
| 235 | } else if (goal->shot_requested) { |
Ben Fredrickson | a6d7754 | 2014-02-17 07:54:43 +0000 | [diff] [blame] | 236 | printf("GHC\n"); |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 237 | state_ = STATE_REQUEST_FIRE; |
| 238 | } |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 239 | } |
Ben Fredrickson | a6d7754 | 2014-02-17 07:54:43 +0000 | [diff] [blame] | 240 | shooter_.SetGoalPosition(PowerToPosition(goal->shot_power), 0.0); |
| 241 | |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 242 | output->latch_piston = true; |
| 243 | output->brake_piston = true; |
| 244 | break; |
| 245 | case STATE_REQUEST_FIRE: |
| 246 | shooter_loop_disable = true; |
Austin Schuh | 60c5666 | 2014-02-17 14:37:19 -0800 | [diff] [blame^] | 247 | if (position->plunger) { |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 248 | prepare_fire_end_time_ = |
| 249 | Time::Now(Time::kDefaultClock) + Time::InMS(40.0); |
| 250 | apply_some_voltage = true; |
| 251 | state_ = STATE_PREPARE_FIRE; |
| 252 | } else { |
| 253 | state_ = STATE_REQUEST_LOAD; |
| 254 | } |
| 255 | break; |
| 256 | case STATE_PREPARE_FIRE: |
| 257 | shooter_loop_disable = true; |
| 258 | if (Time::Now(Time::kDefaultClock) < prepare_fire_end_time_) { |
| 259 | apply_some_voltage = true; |
| 260 | } else { |
| 261 | state_ = STATE_FIRE; |
| 262 | cycles_not_moved_ = 0; |
| 263 | shot_end_time_ = Time::Now(Time::kDefaultClock) + Time::InMS(500); |
| 264 | } |
| 265 | |
| 266 | output->latch_piston = true; |
| 267 | output->brake_piston = true; |
| 268 | |
| 269 | break; |
| 270 | case STATE_FIRE: |
| 271 | shooter_loop_disable = true; |
Austin Schuh | 60c5666 | 2014-02-17 14:37:19 -0800 | [diff] [blame^] | 272 | //TODO(ben): need approamately equal |
Ben Fredrickson | a6d7754 | 2014-02-17 07:54:43 +0000 | [diff] [blame] | 273 | if (fabs(last_position_.position - adjusted_position) < 0.07) { |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 274 | cycles_not_moved_++; |
| 275 | } else { |
| 276 | cycles_not_moved_ = 0; |
| 277 | } |
| 278 | if ((adjusted_position < 0.10 && cycles_not_moved_ > 5) || |
| 279 | Time::Now(Time::kDefaultClock) > shot_end_time_) { |
| 280 | state_ = STATE_REQUEST_LOAD; |
| 281 | } |
| 282 | output->latch_piston = true; |
| 283 | output->brake_piston = true; |
| 284 | break; |
| 285 | case STATE_UNLOAD: |
Austin Schuh | 60c5666 | 2014-02-17 14:37:19 -0800 | [diff] [blame^] | 286 | if (position->plunger && position->latch) { |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 287 | shooter_.SetGoalPosition(0.02, 0.0); |
| 288 | if (adjusted_position < 0.04) { |
| 289 | output->latch_piston = false; |
| 290 | } |
| 291 | } else { |
| 292 | output->latch_piston = false; |
| 293 | state_ = STATE_UNLOAD_MOVE; |
| 294 | } |
| 295 | |
| 296 | output->brake_piston = false; |
| 297 | break; |
| 298 | case STATE_UNLOAD_MOVE: |
Austin Schuh | 60c5666 | 2014-02-17 14:37:19 -0800 | [diff] [blame^] | 299 | if (adjusted_position > values.shooter.upper_limit - 0.03) { |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 300 | shooter_.SetGoalPosition(real_position, 0.0); |
| 301 | state_ = STATE_READY_UNLOAD; |
| 302 | } else { |
| 303 | shooter_.SetGoalPosition( |
Austin Schuh | 60c5666 | 2014-02-17 14:37:19 -0800 | [diff] [blame^] | 304 | real_position + values.shooter.zeroing_speed * dt, |
| 305 | values.shooter.zeroing_speed); |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | output->latch_piston = false; |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 309 | output->brake_piston = false; |
| 310 | break; |
| 311 | case STATE_READY_UNLOAD: |
| 312 | if (!goal->unload_requested) { |
| 313 | state_ = STATE_REQUEST_LOAD; |
Ben Fredrickson | 1f633ef | 2014-02-16 05:35:45 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 316 | output->latch_piston = false; |
| 317 | output->brake_piston = false; |
| 318 | break; |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Ben Fredrickson | 7d980c2 | 2014-02-16 21:39:02 +0000 | [diff] [blame] | 321 | if (apply_some_voltage) { |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 322 | shooter_.Update(true); |
| 323 | if (output) output->voltage = 2.0; |
Ben Fredrickson | 7d980c2 | 2014-02-16 21:39:02 +0000 | [diff] [blame] | 324 | } else if (!shooter_loop_disable) { |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 325 | LOG(DEBUG, "Running the loop, goal is %f\n", shooter_.R(0, 0)); |
| 326 | shooter_.Update(output == NULL); |
| 327 | if (output) output->voltage = shooter_.voltage(); |
Ben Fredrickson | 7d980c2 | 2014-02-16 21:39:02 +0000 | [diff] [blame] | 328 | } else { |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 329 | shooter_.Update(true); |
| 330 | if (output) output->voltage = 0.0; |
Ben Fredrickson | edf0e09 | 2014-02-16 10:46:50 +0000 | [diff] [blame] | 331 | } |
Ben Fredrickson | 7d980c2 | 2014-02-16 21:39:02 +0000 | [diff] [blame] | 332 | |
Ben Fredrickson | 22c9332 | 2014-02-17 05:56:33 +0000 | [diff] [blame] | 333 | status->done = |
Ben Fredrickson | a6d7754 | 2014-02-17 07:54:43 +0000 | [diff] [blame] | 334 | ::std::fabs(adjusted_position - PowerToPosition(goal->shot_power)) < 0.004; |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | } // namespace control_loops |
| 338 | } // namespace frc971 |