Austin Schuh | 0055822 | 2013-03-03 14:16:16 -0800 | [diff] [blame] | 1 | #include "frc971/control_loops/index/index.h" |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 2 | |
| 3 | #include <stdio.h> |
| 4 | |
| 5 | #include <algorithm> |
| 6 | |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 7 | #include "aos/common/control_loop/control_loops.q.h" |
| 8 | #include "aos/common/logging/logging.h" |
Brian Silverman | 9419505 | 2013-03-09 13:45:05 -0800 | [diff] [blame] | 9 | #include "aos/common/inttypes.h" |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 10 | |
| 11 | #include "frc971/constants.h" |
Austin Schuh | 0055822 | 2013-03-03 14:16:16 -0800 | [diff] [blame] | 12 | #include "frc971/control_loops/index/index_motor_plant.h" |
Austin Schuh | a3e8e03 | 2013-03-10 18:43:14 -0700 | [diff] [blame] | 13 | #include "frc971/control_loops/shooter/shooter_motor.q.h" |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 14 | |
| 15 | using ::aos::time::Time; |
| 16 | |
| 17 | namespace frc971 { |
| 18 | namespace control_loops { |
| 19 | |
Austin Schuh | 6b1ad2f | 2013-03-11 23:23:00 -0700 | [diff] [blame] | 20 | double IndexMotor::Frisbee::ObserveNoTopDiscSensor(double index_position) { |
Austin Schuh | dff24e2 | 2013-03-06 00:41:21 -0800 | [diff] [blame] | 21 | // The absolute disc position in meters. |
Austin Schuh | 1b864a1 | 2013-03-07 00:46:50 -0800 | [diff] [blame] | 22 | double disc_position = absolute_position(index_position); |
Austin Schuh | 825bde9 | 2013-03-06 00:16:46 -0800 | [diff] [blame] | 23 | if (IndexMotor::kTopDiscDetectStart <= disc_position && |
| 24 | disc_position <= IndexMotor::kTopDiscDetectStop) { |
| 25 | // Whoops, this shouldn't be happening. |
| 26 | // Move the disc off the way that makes most sense. |
Austin Schuh | dff24e2 | 2013-03-06 00:41:21 -0800 | [diff] [blame] | 27 | double distance_to_above = IndexMotor::ConvertDiscPositionToIndex( |
| 28 | ::std::abs(disc_position - IndexMotor::kTopDiscDetectStop)); |
| 29 | double distance_to_below = IndexMotor::ConvertDiscPositionToIndex( |
| 30 | ::std::abs(disc_position - IndexMotor::kTopDiscDetectStart)); |
Austin Schuh | 6b1ad2f | 2013-03-11 23:23:00 -0700 | [diff] [blame] | 31 | if (distance_to_above < distance_to_below) { |
| 32 | LOG(INFO, "Moving disc to top slow.\n"); |
| 33 | // Move it up. |
| 34 | index_start_position_ -= distance_to_above; |
| 35 | return -distance_to_above; |
Austin Schuh | 825bde9 | 2013-03-06 00:16:46 -0800 | [diff] [blame] | 36 | } else { |
Austin Schuh | 6b1ad2f | 2013-03-11 23:23:00 -0700 | [diff] [blame] | 37 | LOG(INFO, "Moving disc to bottom slow.\n"); |
| 38 | index_start_position_ += distance_to_below; |
| 39 | return distance_to_below; |
Austin Schuh | 825bde9 | 2013-03-06 00:16:46 -0800 | [diff] [blame] | 40 | } |
| 41 | } |
Austin Schuh | dff24e2 | 2013-03-06 00:41:21 -0800 | [diff] [blame] | 42 | return 0.0; |
Austin Schuh | 825bde9 | 2013-03-06 00:16:46 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 45 | IndexMotor::IndexMotor(control_loops::IndexLoop *my_index) |
| 46 | : aos::control_loops::ControlLoop<control_loops::IndexLoop>(my_index), |
Austin Schuh | 9348583 | 2013-03-04 00:01:34 -0800 | [diff] [blame] | 47 | wrist_loop_(new IndexStateFeedbackLoop(MakeIndexLoop())), |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 48 | hopper_disc_count_(0), |
| 49 | total_disc_count_(0), |
Austin Schuh | 70be1ba | 2013-03-10 13:37:17 -0700 | [diff] [blame] | 50 | shot_disc_count_(0), |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 51 | safe_goal_(Goal::HOLD), |
| 52 | loader_goal_(LoaderGoal::READY), |
| 53 | loader_state_(LoaderState::READY), |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 54 | loader_up_(false), |
| 55 | disc_clamped_(false), |
| 56 | disc_ejected_(false), |
Brian Silverman | 7ffb138 | 2013-09-29 16:55:12 -0700 | [diff] [blame] | 57 | is_shooting_(false), |
Austin Schuh | bcdb90c | 2013-03-03 23:24:58 -0800 | [diff] [blame] | 58 | last_bottom_disc_detect_(false), |
Austin Schuh | 825bde9 | 2013-03-06 00:16:46 -0800 | [diff] [blame] | 59 | last_top_disc_detect_(false), |
Brian Silverman | 7d39d86 | 2013-09-29 17:00:30 -0700 | [diff] [blame] | 60 | hopper_clear_(true), |
Austin Schuh | bcdb90c | 2013-03-03 23:24:58 -0800 | [diff] [blame] | 61 | no_prior_position_(true), |
Austin Schuh | 723770b | 2013-03-10 13:26:20 -0700 | [diff] [blame] | 62 | missing_position_count_(0) { |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 65 | /*static*/ const double IndexMotor::kTransferStartPosition = 0.0; |
| 66 | /*static*/ const double IndexMotor::kIndexStartPosition = 0.2159; |
| 67 | /*static*/ const double IndexMotor::kIndexFreeLength = |
| 68 | IndexMotor::ConvertDiscAngleToDiscPosition((360 * 2 + 14) * M_PI / 180); |
| 69 | /*static*/ const double IndexMotor::kLoaderFreeStopPosition = |
| 70 | kIndexStartPosition + kIndexFreeLength; |
Austin Schuh | 1b864a1 | 2013-03-07 00:46:50 -0800 | [diff] [blame] | 71 | /*static*/ const double IndexMotor::kReadyToPreload = |
| 72 | kLoaderFreeStopPosition - ConvertDiscAngleToDiscPosition(M_PI / 6.0); |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 73 | /*static*/ const double IndexMotor::kReadyToLiftPosition = |
| 74 | kLoaderFreeStopPosition + 0.2921; |
| 75 | /*static*/ const double IndexMotor::kGrabberLength = 0.03175; |
| 76 | /*static*/ const double IndexMotor::kGrabberStartPosition = |
| 77 | kReadyToLiftPosition - kGrabberLength; |
Austin Schuh | 6328daf | 2013-03-05 00:53:15 -0800 | [diff] [blame] | 78 | /*static*/ const double IndexMotor::kGrabberMovementVelocity = 0.7; |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 79 | /*static*/ const double IndexMotor::kLifterStopPosition = |
| 80 | kReadyToLiftPosition + 0.161925; |
| 81 | /*static*/ const double IndexMotor::kLifterMovementVelocity = 1.0; |
| 82 | /*static*/ const double IndexMotor::kEjectorStopPosition = |
| 83 | kLifterStopPosition + 0.01; |
| 84 | /*static*/ const double IndexMotor::kEjectorMovementVelocity = 1.0; |
Austin Schuh | cc29702 | 2013-03-09 23:26:40 -0800 | [diff] [blame] | 85 | /*static*/ const double IndexMotor::kBottomDiscDetectStart = 0.00; |
| 86 | /*static*/ const double IndexMotor::kBottomDiscDetectStop = 0.13; |
| 87 | /*static*/ const double IndexMotor::kBottomDiscIndexDelay = 0.032; |
Austin Schuh | d3d0fbf | 2013-03-14 00:37:00 -0700 | [diff] [blame] | 88 | /*static*/ const ::aos::time::Time IndexMotor::kTransferOffDelay = |
Brian Silverman | b7bcef1 | 2013-03-16 13:57:11 -0700 | [diff] [blame] | 89 | ::aos::time::Time::InSeconds(0.3); |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 90 | |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 91 | // TODO(aschuh): Verify these with the sensor actually on. |
Austin Schuh | 825bde9 | 2013-03-06 00:16:46 -0800 | [diff] [blame] | 92 | /*static*/ const double IndexMotor::kTopDiscDetectStart = |
| 93 | (IndexMotor::kLoaderFreeStopPosition - |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 94 | IndexMotor::ConvertDiscAngleToDiscPosition(49 * M_PI / 180)); |
Austin Schuh | 825bde9 | 2013-03-06 00:16:46 -0800 | [diff] [blame] | 95 | /*static*/ const double IndexMotor::kTopDiscDetectStop = |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 96 | (IndexMotor::kLoaderFreeStopPosition + |
| 97 | IndexMotor::ConvertDiscAngleToDiscPosition(19 * M_PI / 180)); |
| 98 | |
| 99 | // I measured the angle between 2 discs. That then gives me the distance |
| 100 | // between 2 posedges (or negedges). Then subtract off the width of the |
| 101 | // positive pulse, and that gives the width of the negative pulse. |
| 102 | /*static*/ const double IndexMotor::kTopDiscDetectMinSeperation = |
| 103 | (IndexMotor::ConvertDiscAngleToDiscPosition(120 * M_PI / 180) - |
| 104 | (IndexMotor::kTopDiscDetectStop - IndexMotor::kTopDiscDetectStart)); |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 105 | |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 106 | const /*static*/ double IndexMotor::kDiscRadius = 10.875 * 0.0254 / 2; |
| 107 | const /*static*/ double IndexMotor::kRollerRadius = 2.0 * 0.0254 / 2; |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 108 | const /*static*/ double IndexMotor::kTransferRollerRadius = 1.25 * 0.0254 / 2; |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 109 | |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 110 | /*static*/ const int IndexMotor::kGrabbingDelay = 5; |
Brian Silverman | 25329e6 | 2013-09-21 23:52:10 -0700 | [diff] [blame] | 111 | /*static*/ const int IndexMotor::kLiftingDelay = 2; |
Brian Silverman | 44d8a9b | 2013-09-29 17:03:05 -0700 | [diff] [blame] | 112 | /*static*/ const int IndexMotor::kLiftingTimeout = 100; |
Brian Silverman | 25329e6 | 2013-09-21 23:52:10 -0700 | [diff] [blame] | 113 | /*static*/ const int IndexMotor::kShootingDelay = 10; |
Brian Silverman | 004ec81 | 2013-09-26 15:47:58 -0700 | [diff] [blame] | 114 | /*static*/ const int IndexMotor::kLoweringDelay = 4; |
| 115 | /*static*/ const int IndexMotor::kLoweringTimeout = 120; |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 116 | |
Austin Schuh | 9348583 | 2013-03-04 00:01:34 -0800 | [diff] [blame] | 117 | // TODO(aschuh): Tune these. |
| 118 | /*static*/ const double |
Austin Schuh | ae5fc8c | 2013-03-11 23:23:28 -0700 | [diff] [blame] | 119 | IndexMotor::IndexStateFeedbackLoop::kMinMotionVoltage = 11.0; |
Austin Schuh | 9348583 | 2013-03-04 00:01:34 -0800 | [diff] [blame] | 120 | /*static*/ const double |
Austin Schuh | a3e8e03 | 2013-03-10 18:43:14 -0700 | [diff] [blame] | 121 | IndexMotor::IndexStateFeedbackLoop::kNoMotionCuttoffCount = 20; |
Austin Schuh | 9348583 | 2013-03-04 00:01:34 -0800 | [diff] [blame] | 122 | |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 123 | /*static*/ double IndexMotor::ConvertDiscAngleToIndex(const double angle) { |
| 124 | return (angle * (1 + (kDiscRadius * 2 + kRollerRadius) / kRollerRadius)); |
| 125 | } |
| 126 | |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 127 | /*static*/ double IndexMotor::ConvertDiscAngleToDiscPosition( |
| 128 | const double angle) { |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 129 | return angle * (kDiscRadius + kRollerRadius); |
| 130 | } |
| 131 | |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 132 | /*static*/ double IndexMotor::ConvertDiscPositionToDiscAngle( |
| 133 | const double position) { |
| 134 | return position / (kDiscRadius + kRollerRadius); |
| 135 | } |
| 136 | |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 137 | /*static*/ double IndexMotor::ConvertIndexToDiscAngle(const double angle) { |
| 138 | return (angle / (1 + (kDiscRadius * 2 + kRollerRadius) / kRollerRadius)); |
| 139 | } |
| 140 | |
| 141 | /*static*/ double IndexMotor::ConvertIndexToDiscPosition(const double angle) { |
| 142 | return IndexMotor::ConvertDiscAngleToDiscPosition( |
| 143 | ConvertIndexToDiscAngle(angle)); |
| 144 | } |
| 145 | |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 146 | /*static*/ double IndexMotor::ConvertTransferToDiscPosition( |
| 147 | const double angle) { |
| 148 | const double gear_ratio = (1 + (kDiscRadius * 2 + kTransferRollerRadius) / |
| 149 | kTransferRollerRadius); |
| 150 | return angle / gear_ratio * (kDiscRadius + kTransferRollerRadius); |
| 151 | } |
| 152 | |
| 153 | /*static*/ double IndexMotor::ConvertDiscPositionToIndex( |
| 154 | const double position) { |
| 155 | return IndexMotor::ConvertDiscAngleToIndex( |
| 156 | ConvertDiscPositionToDiscAngle(position)); |
| 157 | } |
| 158 | |
Austin Schuh | 1b864a1 | 2013-03-07 00:46:50 -0800 | [diff] [blame] | 159 | bool IndexMotor::MinDiscPosition(double *disc_position, Frisbee **found_disc) { |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 160 | bool found_start = false; |
| 161 | for (unsigned int i = 0; i < frisbees_.size(); ++i) { |
Austin Schuh | 1b864a1 | 2013-03-07 00:46:50 -0800 | [diff] [blame] | 162 | Frisbee &frisbee = frisbees_[i]; |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 163 | if (!found_start) { |
| 164 | if (frisbee.has_position()) { |
| 165 | *disc_position = frisbee.position(); |
Austin Schuh | 1b864a1 | 2013-03-07 00:46:50 -0800 | [diff] [blame] | 166 | if (found_disc) { |
| 167 | *found_disc = &frisbee; |
| 168 | } |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 169 | found_start = true; |
| 170 | } |
| 171 | } else { |
Austin Schuh | 1b864a1 | 2013-03-07 00:46:50 -0800 | [diff] [blame] | 172 | if (frisbee.position() <= *disc_position) { |
| 173 | *disc_position = frisbee.position(); |
| 174 | if (found_disc) { |
| 175 | *found_disc = &frisbee; |
| 176 | } |
| 177 | } |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | return found_start; |
| 181 | } |
| 182 | |
Austin Schuh | 1b864a1 | 2013-03-07 00:46:50 -0800 | [diff] [blame] | 183 | bool IndexMotor::MaxDiscPosition(double *disc_position, Frisbee **found_disc) { |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 184 | bool found_start = false; |
| 185 | for (unsigned int i = 0; i < frisbees_.size(); ++i) { |
Austin Schuh | 1b864a1 | 2013-03-07 00:46:50 -0800 | [diff] [blame] | 186 | Frisbee &frisbee = frisbees_[i]; |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 187 | if (!found_start) { |
| 188 | if (frisbee.has_position()) { |
| 189 | *disc_position = frisbee.position(); |
Austin Schuh | 1b864a1 | 2013-03-07 00:46:50 -0800 | [diff] [blame] | 190 | if (found_disc) { |
| 191 | *found_disc = &frisbee; |
| 192 | } |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 193 | found_start = true; |
| 194 | } |
| 195 | } else { |
Austin Schuh | 1b864a1 | 2013-03-07 00:46:50 -0800 | [diff] [blame] | 196 | if (frisbee.position() > *disc_position) { |
| 197 | *disc_position = frisbee.position(); |
| 198 | if (found_disc) { |
| 199 | *found_disc = &frisbee; |
| 200 | } |
| 201 | } |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | return found_start; |
| 205 | } |
| 206 | |
Austin Schuh | 9348583 | 2013-03-04 00:01:34 -0800 | [diff] [blame] | 207 | void IndexMotor::IndexStateFeedbackLoop::CapU() { |
| 208 | // If the voltage has been low for a large number of cycles, cut the motor |
| 209 | // power. This is generally very bad controls practice since this isn't LTI, |
| 210 | // but we don't really care about tracking anything other than large step |
| 211 | // inputs, and the loader doesn't need to be that accurate. |
| 212 | if (::std::abs(U(0, 0)) < kMinMotionVoltage) { |
| 213 | ++low_voltage_count_; |
| 214 | if (low_voltage_count_ > kNoMotionCuttoffCount) { |
Austin Schuh | 9348583 | 2013-03-04 00:01:34 -0800 | [diff] [blame] | 215 | U(0, 0) = 0.0; |
| 216 | } |
| 217 | } else { |
| 218 | low_voltage_count_ = 0; |
| 219 | } |
| 220 | |
| 221 | for (int i = 0; i < kNumOutputs; ++i) { |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 222 | if (U(i, 0) > U_max(i, 0)) { |
| 223 | U(i, 0) = U_max(i, 0); |
| 224 | } else if (U(i, 0) < U_min(i, 0)) { |
| 225 | U(i, 0) = U_min(i, 0); |
Austin Schuh | 9348583 | 2013-03-04 00:01:34 -0800 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 231 | // Positive angle is towards the shooter, and positive power is towards the |
| 232 | // shooter. |
| 233 | void IndexMotor::RunIteration( |
| 234 | const control_loops::IndexLoop::Goal *goal, |
| 235 | const control_loops::IndexLoop::Position *position, |
| 236 | control_loops::IndexLoop::Output *output, |
| 237 | control_loops::IndexLoop::Status *status) { |
Austin Schuh | d3d0fbf | 2013-03-14 00:37:00 -0700 | [diff] [blame] | 238 | Time now = Time::Now(); |
Austin Schuh | bcdb90c | 2013-03-03 23:24:58 -0800 | [diff] [blame] | 239 | // Make goal easy to work with and sanity check it. |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 240 | Goal goal_enum = static_cast<Goal>(goal->goal_state); |
Brian Silverman | b8d389f | 2013-03-19 22:54:06 -0700 | [diff] [blame] | 241 | if (goal->goal_state < 0 || goal->goal_state > 5) { |
Brian Silverman | 8efe23e | 2013-07-07 23:31:37 -0700 | [diff] [blame] | 242 | LOG(ERROR, |
| 243 | "Goal state is %" PRId32 " which is out of range. Going to HOLD.\n", |
Austin Schuh | bcdb90c | 2013-03-03 23:24:58 -0800 | [diff] [blame] | 244 | goal->goal_state); |
| 245 | goal_enum = Goal::HOLD; |
| 246 | } |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 247 | |
| 248 | // Disable the motors now so that all early returns will return with the |
| 249 | // motors disabled. |
Austin Schuh | b6d898b | 2013-03-03 15:34:35 -0800 | [diff] [blame] | 250 | double intake_voltage = 0.0; |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 251 | double transfer_voltage = 0.0; |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 252 | if (output) { |
Austin Schuh | b6d898b | 2013-03-03 15:34:35 -0800 | [diff] [blame] | 253 | output->intake_voltage = 0.0; |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 254 | output->transfer_voltage = 0.0; |
| 255 | output->index_voltage = 0.0; |
| 256 | } |
| 257 | |
| 258 | status->ready_to_intake = false; |
| 259 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 260 | // Set the controller to use to be the one designed for the current number of |
| 261 | // discs in the hopper. This is safe since the controller prevents the index |
| 262 | // from being set out of bounds and picks the closest controller. |
| 263 | wrist_loop_->set_controller_index(frisbees_.size()); |
| 264 | |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 265 | // Compute a safe index position that we can use. |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 266 | if (position) { |
| 267 | wrist_loop_->Y << position->index_position; |
Austin Schuh | bcdb90c | 2013-03-03 23:24:58 -0800 | [diff] [blame] | 268 | // Set the goal to be the current position if this is the first time through |
| 269 | // so we don't always spin the indexer to the 0 position before starting. |
| 270 | if (no_prior_position_) { |
Brian Silverman | 43bb73e | 2013-03-17 13:39:47 -0700 | [diff] [blame] | 271 | LOG(INFO, "no prior position; resetting\n"); |
Austin Schuh | bcdb90c | 2013-03-03 23:24:58 -0800 | [diff] [blame] | 272 | wrist_loop_->R << wrist_loop_->Y(0, 0), 0.0; |
Austin Schuh | c5ef1bb | 2013-03-10 00:42:05 -0800 | [diff] [blame] | 273 | wrist_loop_->X_hat(0, 0) = wrist_loop_->Y(0, 0); |
Austin Schuh | bcdb90c | 2013-03-03 23:24:58 -0800 | [diff] [blame] | 274 | no_prior_position_ = false; |
Austin Schuh | 6328daf | 2013-03-05 00:53:15 -0800 | [diff] [blame] | 275 | last_bottom_disc_posedge_count_ = position->bottom_disc_posedge_count; |
| 276 | last_bottom_disc_negedge_count_ = position->bottom_disc_negedge_count; |
| 277 | last_bottom_disc_negedge_wait_count_ = |
| 278 | position->bottom_disc_negedge_wait_count; |
Austin Schuh | 825bde9 | 2013-03-06 00:16:46 -0800 | [diff] [blame] | 279 | last_top_disc_posedge_count_ = position->top_disc_posedge_count; |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 280 | last_top_disc_negedge_count_ = position->top_disc_negedge_count; |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 281 | last_top_disc_detect_ = position->top_disc_detect; |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 282 | // The open positions for the upper is right here and isn't a hard edge. |
Austin Schuh | 723770b | 2013-03-10 13:26:20 -0700 | [diff] [blame] | 283 | upper_open_region_.Restart(wrist_loop_->Y(0, 0)); |
| 284 | lower_open_region_.Restart(wrist_loop_->Y(0, 0)); |
Austin Schuh | bcdb90c | 2013-03-03 23:24:58 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Austin Schuh | 1b864a1 | 2013-03-07 00:46:50 -0800 | [diff] [blame] | 287 | // If the cRIO is gone for over 1/2 of a second, assume that it rebooted. |
Austin Schuh | bcdb90c | 2013-03-03 23:24:58 -0800 | [diff] [blame] | 288 | if (missing_position_count_ > 50) { |
Brian Silverman | 43bb73e | 2013-03-17 13:39:47 -0700 | [diff] [blame] | 289 | LOG(INFO, "assuming cRIO rebooted\n"); |
Austin Schuh | 6328daf | 2013-03-05 00:53:15 -0800 | [diff] [blame] | 290 | last_bottom_disc_posedge_count_ = position->bottom_disc_posedge_count; |
| 291 | last_bottom_disc_negedge_count_ = position->bottom_disc_negedge_count; |
| 292 | last_bottom_disc_negedge_wait_count_ = |
| 293 | position->bottom_disc_negedge_wait_count; |
Austin Schuh | 825bde9 | 2013-03-06 00:16:46 -0800 | [diff] [blame] | 294 | last_top_disc_posedge_count_ = position->top_disc_posedge_count; |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 295 | last_top_disc_negedge_count_ = position->top_disc_negedge_count; |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 296 | last_top_disc_detect_ = position->top_disc_detect; |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 297 | // We can't really trust the open range any more if the crio rebooted. |
Austin Schuh | 723770b | 2013-03-10 13:26:20 -0700 | [diff] [blame] | 298 | upper_open_region_.Restart(wrist_loop_->Y(0, 0)); |
| 299 | lower_open_region_.Restart(wrist_loop_->Y(0, 0)); |
Austin Schuh | bcdb90c | 2013-03-03 23:24:58 -0800 | [diff] [blame] | 300 | // Adjust the disc positions so that they don't have to move. |
| 301 | const double disc_offset = |
| 302 | position->index_position - wrist_loop_->X_hat(0, 0); |
| 303 | for (auto frisbee = frisbees_.begin(); |
| 304 | frisbee != frisbees_.end(); ++frisbee) { |
| 305 | frisbee->OffsetDisc(disc_offset); |
| 306 | } |
Austin Schuh | c5ef1bb | 2013-03-10 00:42:05 -0800 | [diff] [blame] | 307 | wrist_loop_->X_hat(0, 0) = wrist_loop_->Y(0, 0); |
Austin Schuh | bcdb90c | 2013-03-03 23:24:58 -0800 | [diff] [blame] | 308 | } |
| 309 | missing_position_count_ = 0; |
Austin Schuh | 81fb1bc | 2013-04-04 05:52:28 +0000 | [diff] [blame] | 310 | if (last_top_disc_detect_) { |
| 311 | if (last_top_disc_posedge_count_ != position->top_disc_posedge_count) { |
| 312 | LOG(INFO, "Ignoring a top disc posedge\n"); |
| 313 | } |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 314 | last_top_disc_posedge_count_ = position->top_disc_posedge_count; |
| 315 | } |
Austin Schuh | 81fb1bc | 2013-04-04 05:52:28 +0000 | [diff] [blame] | 316 | if (!last_top_disc_detect_) { |
| 317 | if (last_top_disc_negedge_count_ != position->top_disc_negedge_count) { |
| 318 | LOG(INFO, "Ignoring a top disc negedge\n"); |
| 319 | } |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 320 | last_top_disc_negedge_count_ = position->top_disc_negedge_count; |
| 321 | } |
Austin Schuh | bcdb90c | 2013-03-03 23:24:58 -0800 | [diff] [blame] | 322 | } else { |
| 323 | ++missing_position_count_; |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 324 | } |
| 325 | const double index_position = wrist_loop_->X_hat(0, 0); |
| 326 | |
Austin Schuh | 825bde9 | 2013-03-06 00:16:46 -0800 | [diff] [blame] | 327 | if (position) { |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 328 | // Reset the open region if we saw a negedge. |
Austin Schuh | 723770b | 2013-03-10 13:26:20 -0700 | [diff] [blame] | 329 | if (position->bottom_disc_negedge_wait_count != |
| 330 | last_bottom_disc_negedge_wait_count_) { |
| 331 | // Saw a negedge, must be a new region. |
| 332 | lower_open_region_.Restart(position->bottom_disc_negedge_wait_position); |
| 333 | } |
| 334 | // Reset the open region if we saw a negedge. |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 335 | if (position->top_disc_negedge_count != last_top_disc_negedge_count_) { |
| 336 | // Saw a negedge, must be a new region. |
Austin Schuh | 723770b | 2013-03-10 13:26:20 -0700 | [diff] [blame] | 337 | upper_open_region_.Restart(position->top_disc_negedge_position); |
| 338 | } |
| 339 | |
| 340 | // No disc. Expand the open region. |
| 341 | if (!position->bottom_disc_detect) { |
| 342 | lower_open_region_.Expand(index_position); |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | // No disc. Expand the open region. |
| 346 | if (!position->top_disc_detect) { |
Austin Schuh | 723770b | 2013-03-10 13:26:20 -0700 | [diff] [blame] | 347 | upper_open_region_.Expand(index_position); |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 348 | } |
| 349 | |
Austin Schuh | 825bde9 | 2013-03-06 00:16:46 -0800 | [diff] [blame] | 350 | if (!position->top_disc_detect) { |
| 351 | // We don't see a disc. Verify that there are no discs that we should be |
| 352 | // seeing. |
Austin Schuh | 1b864a1 | 2013-03-07 00:46:50 -0800 | [diff] [blame] | 353 | // Assume that discs will move slow enough that we won't miss one as it |
| 354 | // goes by. They will either pile up above or below the sensor. |
Austin Schuh | dff24e2 | 2013-03-06 00:41:21 -0800 | [diff] [blame] | 355 | |
| 356 | double cumulative_offset = 0.0; |
| 357 | for (auto frisbee = frisbees_.rbegin(), rend = frisbees_.rend(); |
| 358 | frisbee != rend; ++frisbee) { |
| 359 | frisbee->OffsetDisc(cumulative_offset); |
| 360 | double amount_moved = frisbee->ObserveNoTopDiscSensor( |
Austin Schuh | 6b1ad2f | 2013-03-11 23:23:00 -0700 | [diff] [blame] | 361 | wrist_loop_->X_hat(0, 0)); |
Austin Schuh | dff24e2 | 2013-03-06 00:41:21 -0800 | [diff] [blame] | 362 | cumulative_offset += amount_moved; |
Austin Schuh | 825bde9 | 2013-03-06 00:16:46 -0800 | [diff] [blame] | 363 | } |
| 364 | } |
Austin Schuh | 1b864a1 | 2013-03-07 00:46:50 -0800 | [diff] [blame] | 365 | |
Austin Schuh | 825bde9 | 2013-03-06 00:16:46 -0800 | [diff] [blame] | 366 | if (position->top_disc_posedge_count != last_top_disc_posedge_count_) { |
Brian Silverman | 967c442 | 2013-09-29 16:58:59 -0700 | [diff] [blame] | 367 | LOG(INFO, "Saw a top posedge\n"); |
Austin Schuh | 1b864a1 | 2013-03-07 00:46:50 -0800 | [diff] [blame] | 368 | const double index_position = wrist_loop_->X_hat(0, 0) - |
| 369 | position->index_position + position->top_disc_posedge_position; |
Austin Schuh | 825bde9 | 2013-03-06 00:16:46 -0800 | [diff] [blame] | 370 | // TODO(aschuh): Sanity check this number... |
| 371 | // Requires storing when the disc was last seen with the sensor off, and |
| 372 | // figuring out what to do if things go south. |
| 373 | |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 374 | // 1 if discs are going up, 0 if we have no clue, and -1 if they are going |
| 375 | // down. |
| 376 | int disc_direction = 0; |
Austin Schuh | 81fb1bc | 2013-04-04 05:52:28 +0000 | [diff] [blame] | 377 | if (wrist_loop_->X_hat(1, 0) > 100.0) { |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 378 | disc_direction = 1; |
Austin Schuh | 81fb1bc | 2013-04-04 05:52:28 +0000 | [diff] [blame] | 379 | } else if (wrist_loop_->X_hat(1, 0) < -100.0) { |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 380 | disc_direction = -1; |
| 381 | } else { |
| 382 | // Save the upper and lower positions that we last saw a disc at. |
| 383 | // If there is a big buffer above, must be a disc from below. |
| 384 | // If there is a big buffer below, must be a disc from above. |
| 385 | // This should work to replace the velocity threshold above. |
| 386 | |
Austin Schuh | 723770b | 2013-03-10 13:26:20 -0700 | [diff] [blame] | 387 | const double open_width = upper_open_region_.width(); |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 388 | const double relative_upper_open_precentage = |
Austin Schuh | 723770b | 2013-03-10 13:26:20 -0700 | [diff] [blame] | 389 | (upper_open_region_.upper_bound() - index_position) / open_width; |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 390 | const double relative_lower_open_precentage = |
Austin Schuh | 723770b | 2013-03-10 13:26:20 -0700 | [diff] [blame] | 391 | (index_position - upper_open_region_.lower_bound()) / open_width; |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 392 | |
| 393 | if (ConvertIndexToDiscPosition(open_width) < |
| 394 | kTopDiscDetectMinSeperation * 0.9) { |
| 395 | LOG(ERROR, "Discs are way too close to each other. Doing nothing\n"); |
| 396 | } else if (relative_upper_open_precentage > 0.75) { |
| 397 | // Looks like it is a disc going down from above since we are near |
| 398 | // the upper edge. |
| 399 | disc_direction = -1; |
Austin Schuh | a3e8e03 | 2013-03-10 18:43:14 -0700 | [diff] [blame] | 400 | LOG(INFO, "Disc edge going down\n"); |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 401 | } else if (relative_lower_open_precentage > 0.75) { |
| 402 | // Looks like it is a disc going up from below since we are near |
| 403 | // the lower edge. |
| 404 | disc_direction = 1; |
Austin Schuh | a3e8e03 | 2013-03-10 18:43:14 -0700 | [diff] [blame] | 405 | LOG(INFO, "Disc edge going up\n"); |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 406 | } else { |
| 407 | LOG(ERROR, |
| 408 | "Got an edge in the middle of what should be an open region.\n"); |
| 409 | LOG(ERROR, "Open width: %f upper precentage %f %%\n", |
| 410 | open_width, relative_upper_open_precentage); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | if (disc_direction > 0) { |
Austin Schuh | 825bde9 | 2013-03-06 00:16:46 -0800 | [diff] [blame] | 415 | // Moving up at a reasonable clip. |
Austin Schuh | 1b864a1 | 2013-03-07 00:46:50 -0800 | [diff] [blame] | 416 | // Find the highest disc that is below the top disc sensor. |
| 417 | // While we are at it, count the number above and log an error if there |
| 418 | // are too many. |
| 419 | if (frisbees_.size() == 0) { |
| 420 | Frisbee new_frisbee; |
| 421 | new_frisbee.has_been_indexed_ = true; |
| 422 | new_frisbee.index_start_position_ = index_position - |
| 423 | ConvertDiscPositionToIndex(kTopDiscDetectStart - |
| 424 | kIndexStartPosition); |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 425 | ++hopper_disc_count_; |
| 426 | ++total_disc_count_; |
Austin Schuh | 1b864a1 | 2013-03-07 00:46:50 -0800 | [diff] [blame] | 427 | frisbees_.push_front(new_frisbee); |
| 428 | LOG(WARNING, "Added a disc to the hopper at the top sensor\n"); |
| 429 | } |
| 430 | |
| 431 | int above_disc_count = 0; |
| 432 | double highest_position = 0; |
| 433 | Frisbee *highest_frisbee_below_sensor = NULL; |
| 434 | for (auto frisbee = frisbees_.rbegin(), rend = frisbees_.rend(); |
| 435 | frisbee != rend; ++frisbee) { |
| 436 | const double disc_position = frisbee->absolute_position( |
| 437 | index_position); |
| 438 | // It is save to use the top position for the cuttoff, since the |
| 439 | // sensor being low will result in discs being pushed off of it. |
| 440 | if (disc_position >= kTopDiscDetectStop) { |
| 441 | ++above_disc_count; |
| 442 | } else if (!highest_frisbee_below_sensor || |
| 443 | disc_position > highest_position) { |
| 444 | highest_frisbee_below_sensor = &*frisbee; |
| 445 | highest_position = disc_position; |
| 446 | } |
| 447 | } |
Austin Schuh | 09e0708 | 2013-03-19 10:04:12 +0000 | [diff] [blame] | 448 | |
| 449 | if (!highest_frisbee_below_sensor) { |
| 450 | Frisbee new_frisbee; |
| 451 | new_frisbee.has_been_indexed_ = true; |
| 452 | new_frisbee.index_start_position_ = index_position - |
| 453 | ConvertDiscPositionToIndex(kTopDiscDetectStart - |
| 454 | kIndexStartPosition); |
| 455 | highest_position = kTopDiscDetectStart; |
| 456 | ++hopper_disc_count_; |
| 457 | ++total_disc_count_; |
| 458 | frisbees_.push_front(new_frisbee); |
| 459 | LOG(WARNING, "Added a disc to the hopper at the top sensor because the one we know about is up top\n"); |
| 460 | } |
| 461 | |
Austin Schuh | 1b864a1 | 2013-03-07 00:46:50 -0800 | [diff] [blame] | 462 | if (above_disc_count > 1) { |
| 463 | LOG(ERROR, "We have 2 discs above the top sensor.\n"); |
| 464 | } |
Austin Schuh | 1b864a1 | 2013-03-07 00:46:50 -0800 | [diff] [blame] | 465 | // We now have the disc. Shift all the ones below the sensor up by the |
| 466 | // computed delta. |
| 467 | const double disc_delta = IndexMotor::ConvertDiscPositionToIndex( |
| 468 | highest_position - kTopDiscDetectStart); |
| 469 | for (auto frisbee = frisbees_.rbegin(), rend = frisbees_.rend(); |
| 470 | frisbee != rend; ++frisbee) { |
| 471 | const double disc_position = frisbee->absolute_position( |
| 472 | index_position); |
| 473 | if (disc_position < kTopDiscDetectStop) { |
Austin Schuh | 59004d3 | 2013-03-21 04:31:45 +0000 | [diff] [blame] | 474 | LOG(INFO, "Moving disc down by %f meters, since it is at %f and top is [%f, %f]\n", |
| 475 | ConvertIndexToDiscPosition(disc_delta), |
| 476 | disc_position, kTopDiscDetectStart, |
| 477 | kTopDiscDetectStop); |
Austin Schuh | 1b864a1 | 2013-03-07 00:46:50 -0800 | [diff] [blame] | 478 | frisbee->OffsetDisc(disc_delta); |
| 479 | } |
| 480 | } |
Brian Silverman | 43bb73e | 2013-03-17 13:39:47 -0700 | [diff] [blame] | 481 | if (highest_frisbee_below_sensor) { |
| 482 | LOG(INFO, "Currently have %d discs, saw posedge moving up. " |
| 483 | "Moving down by %f to %f\n", frisbees_.size(), |
| 484 | ConvertIndexToDiscPosition(disc_delta), |
| 485 | highest_frisbee_below_sensor->absolute_position( |
| 486 | wrist_loop_->X_hat(0, 0))); |
| 487 | } else { |
| 488 | LOG(INFO, "Currently have %d discs, saw posedge moving up. " |
| 489 | "Moving down by %f\n", frisbees_.size(), |
| 490 | ConvertIndexToDiscPosition(disc_delta)); |
| 491 | } |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 492 | } else if (disc_direction < 0) { |
Austin Schuh | 825bde9 | 2013-03-06 00:16:46 -0800 | [diff] [blame] | 493 | // Moving down at a reasonable clip. |
Austin Schuh | 1b864a1 | 2013-03-07 00:46:50 -0800 | [diff] [blame] | 494 | // There can only be 1 disc up top that would give us a posedge. |
| 495 | // Find it and place it at the one spot that it can be. |
Brian Silverman | 9419505 | 2013-03-09 13:45:05 -0800 | [diff] [blame] | 496 | double min_disc_position = 0; |
Austin Schuh | 1b864a1 | 2013-03-07 00:46:50 -0800 | [diff] [blame] | 497 | Frisbee *min_frisbee = NULL; |
| 498 | MinDiscPosition(&min_disc_position, &min_frisbee); |
| 499 | if (!min_frisbee) { |
| 500 | // Uh, oh, we see a disc but there isn't one... |
| 501 | LOG(ERROR, "Saw a disc up top but there isn't one in the hopper\n"); |
| 502 | } else { |
| 503 | const double disc_position = min_frisbee->absolute_position( |
| 504 | index_position); |
| 505 | |
| 506 | const double disc_delta_meters = disc_position - kTopDiscDetectStop; |
| 507 | const double disc_delta = IndexMotor::ConvertDiscPositionToIndex( |
| 508 | disc_delta_meters); |
Austin Schuh | a3e8e03 | 2013-03-10 18:43:14 -0700 | [diff] [blame] | 509 | LOG(INFO, "Posedge going down. Moving top disc down by %f\n", |
| 510 | disc_delta_meters); |
Austin Schuh | 1b864a1 | 2013-03-07 00:46:50 -0800 | [diff] [blame] | 511 | for (auto frisbee = frisbees_.begin(), end = frisbees_.end(); |
| 512 | frisbee != end; ++frisbee) { |
| 513 | frisbee->OffsetDisc(disc_delta); |
| 514 | } |
| 515 | } |
Austin Schuh | 825bde9 | 2013-03-06 00:16:46 -0800 | [diff] [blame] | 516 | } else { |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 517 | LOG(ERROR, "Not sure how to handle the upper posedge, doing nothing\n"); |
Austin Schuh | 825bde9 | 2013-03-06 00:16:46 -0800 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | } |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 521 | |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 522 | // Bool to track if it is safe for the goal to change yet. |
Austin Schuh | ae5fc8c | 2013-03-11 23:23:28 -0700 | [diff] [blame] | 523 | bool safe_to_change_state = true; |
Brian Silverman | f0716e8 | 2013-03-17 23:36:11 -0700 | [diff] [blame] | 524 | if (!position) { |
| 525 | // This fixes a nasty indexer bug. |
| 526 | // If we didn't get a position this cycle, we don't run the code below which |
| 527 | // checks the state of the disc detect sensor and whether all the discs are |
| 528 | // indexed. It is therefore not safe to change state and loose track of |
| 529 | // that disc. |
| 530 | safe_to_change_state = false; |
| 531 | } |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 532 | switch (safe_goal_) { |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 533 | case Goal::HOLD: |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 534 | // The goal should already be good, so sit tight with everything the same |
| 535 | // as it was. |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 536 | break; |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 537 | case Goal::READY_LOWER: |
| 538 | case Goal::INTAKE: |
Brian Silverman | 7d39d86 | 2013-09-29 17:00:30 -0700 | [diff] [blame] | 539 | hopper_clear_ = false; |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 540 | { |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 541 | if (position) { |
Austin Schuh | 6328daf | 2013-03-05 00:53:15 -0800 | [diff] [blame] | 542 | // Posedge of the disc entering the beam break. |
| 543 | if (position->bottom_disc_posedge_count != |
| 544 | last_bottom_disc_posedge_count_) { |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 545 | transfer_frisbee_.Reset(); |
| 546 | transfer_frisbee_.bottom_posedge_time_ = now; |
Austin Schuh | a3e8e03 | 2013-03-10 18:43:14 -0700 | [diff] [blame] | 547 | LOG(INFO, "Posedge of bottom disc %f\n", |
| 548 | transfer_frisbee_.bottom_posedge_time_.ToSeconds()); |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 549 | ++hopper_disc_count_; |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 550 | ++total_disc_count_; |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | // Disc exited the beam break now. |
Austin Schuh | 6328daf | 2013-03-05 00:53:15 -0800 | [diff] [blame] | 554 | if (position->bottom_disc_negedge_count != |
| 555 | last_bottom_disc_negedge_count_) { |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 556 | transfer_frisbee_.bottom_negedge_time_ = now; |
Austin Schuh | a3e8e03 | 2013-03-10 18:43:14 -0700 | [diff] [blame] | 557 | LOG(INFO, "Negedge of bottom disc %f\n", |
| 558 | transfer_frisbee_.bottom_negedge_time_.ToSeconds()); |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 559 | frisbees_.push_front(transfer_frisbee_); |
| 560 | } |
| 561 | |
| 562 | if (position->bottom_disc_detect) { |
Brian Silverman | c527754 | 2013-03-22 13:33:07 -0700 | [diff] [blame] | 563 | intake_voltage = 0.0; |
| 564 | transfer_voltage = 12.0; |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 565 | // Must wait until the disc gets out before we can change state. |
Austin Schuh | ae5fc8c | 2013-03-11 23:23:28 -0700 | [diff] [blame] | 566 | safe_to_change_state = false; |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 567 | |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 568 | // TODO(aschuh): A disc on the way through needs to start moving |
| 569 | // the indexer if it isn't already moving. Maybe? |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 570 | |
| 571 | Time elapsed_posedge_time = now - |
| 572 | transfer_frisbee_.bottom_posedge_time_; |
| 573 | if (elapsed_posedge_time >= Time::InSeconds(0.3)) { |
| 574 | // It has been too long. The disc must be jammed. |
| 575 | LOG(ERROR, "Been way too long. Jammed disc?\n"); |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 576 | intake_voltage = -12.0; |
Brian Silverman | f0716e8 | 2013-03-17 23:36:11 -0700 | [diff] [blame] | 577 | transfer_voltage = -12.0; |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 581 | // Check all non-indexed discs and see if they should be indexed. |
Austin Schuh | b6d898b | 2013-03-03 15:34:35 -0800 | [diff] [blame] | 582 | for (auto frisbee = frisbees_.begin(); |
Austin Schuh | bcdb90c | 2013-03-03 23:24:58 -0800 | [diff] [blame] | 583 | frisbee != frisbees_.end(); ++frisbee) { |
Austin Schuh | b6d898b | 2013-03-03 15:34:35 -0800 | [diff] [blame] | 584 | if (!frisbee->has_been_indexed_) { |
Austin Schuh | 6328daf | 2013-03-05 00:53:15 -0800 | [diff] [blame] | 585 | if (last_bottom_disc_negedge_wait_count_ != |
| 586 | position->bottom_disc_negedge_wait_count) { |
| 587 | // We have an index difference. |
| 588 | // Save the indexer position, and the time. |
| 589 | if (last_bottom_disc_negedge_wait_count_ + 1 != |
| 590 | position->bottom_disc_negedge_wait_count) { |
| 591 | LOG(ERROR, "Funny, we got 2 edges since we last checked.\n"); |
| 592 | } |
| 593 | |
| 594 | // Save the captured position as the position at which the disc |
| 595 | // touched the indexer. |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 596 | LOG(INFO, "Grabbed on the index now at %f\n", index_position); |
Austin Schuh | b6d898b | 2013-03-03 15:34:35 -0800 | [diff] [blame] | 597 | frisbee->has_been_indexed_ = true; |
Austin Schuh | 6328daf | 2013-03-05 00:53:15 -0800 | [diff] [blame] | 598 | frisbee->index_start_position_ = |
| 599 | position->bottom_disc_negedge_wait_position; |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 600 | } |
| 601 | } |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 602 | } |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 603 | } |
Austin Schuh | 59004d3 | 2013-03-21 04:31:45 +0000 | [diff] [blame] | 604 | for (auto frisbee = frisbees_.begin(); |
| 605 | frisbee != frisbees_.end(); ++frisbee) { |
| 606 | if (!frisbee->has_been_indexed_) { |
Brian Silverman | c527754 | 2013-03-22 13:33:07 -0700 | [diff] [blame] | 607 | intake_voltage = 0.0; |
| 608 | transfer_voltage = 12.0; |
Austin Schuh | 59004d3 | 2013-03-21 04:31:45 +0000 | [diff] [blame] | 609 | |
| 610 | // All discs must be indexed before it is safe to stop indexing. |
| 611 | safe_to_change_state = false; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | // Figure out where the indexer should be to move the discs down to |
| 616 | // the right position. |
| 617 | double max_disc_position = 0; |
| 618 | if (MaxDiscPosition(&max_disc_position, NULL)) { |
| 619 | LOG(DEBUG, "There is a disc down here!\n"); |
| 620 | // TODO(aschuh): Figure out what to do if grabbing the next one |
| 621 | // would cause things to jam into the loader. |
| 622 | // Say we aren't ready any more. Undefined behavior will result if |
| 623 | // that isn't observed. |
| 624 | double bottom_disc_position = |
| 625 | max_disc_position + ConvertDiscAngleToIndex(M_PI); |
| 626 | wrist_loop_->R << bottom_disc_position, 0.0; |
| 627 | |
| 628 | // Verify that we are close enough to the goal so that we should be |
| 629 | // fine accepting the next disc. |
| 630 | double disc_error_meters = ConvertIndexToDiscPosition( |
| 631 | wrist_loop_->X_hat(0, 0) - bottom_disc_position); |
| 632 | // We are ready for the next disc if the first one is in the first |
| 633 | // half circle of the indexer. It will take time for the disc to |
| 634 | // come into the indexer, so we will be able to move it out of the |
| 635 | // way in time. |
| 636 | // This choice also makes sure that we don't claim that we aren't |
| 637 | // ready between full speed intaking. |
| 638 | if (-ConvertDiscAngleToIndex(M_PI) < disc_error_meters && |
| 639 | disc_error_meters < 0.04) { |
| 640 | // We are only ready if we aren't being asked to change state or |
| 641 | // are full. |
| 642 | status->ready_to_intake = |
| 643 | (safe_goal_ == goal_enum) && hopper_disc_count_ < 4; |
| 644 | } else { |
| 645 | status->ready_to_intake = false; |
| 646 | } |
| 647 | } else { |
| 648 | // No discs! We are always ready for more if we aren't being |
| 649 | // asked to change state. |
| 650 | status->ready_to_intake = (safe_goal_ == goal_enum); |
| 651 | } |
| 652 | |
| 653 | // Turn on the transfer roller if we are ready. |
| 654 | if (status->ready_to_intake && hopper_disc_count_ < 4 && |
| 655 | safe_goal_ == Goal::INTAKE) { |
| 656 | intake_voltage = transfer_voltage = 12.0; |
| 657 | } |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 658 | } |
Austin Schuh | 59004d3 | 2013-03-21 04:31:45 +0000 | [diff] [blame] | 659 | LOG(DEBUG, "INTAKE\n"); |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 660 | break; |
Brian Silverman | b8d389f | 2013-03-19 22:54:06 -0700 | [diff] [blame] | 661 | case Goal::REINITIALIZE: |
| 662 | LOG(WARNING, "Reinitializing the indexer\n"); |
| 663 | break; |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 664 | case Goal::READY_SHOOTER: |
| 665 | case Goal::SHOOT: |
Austin Schuh | ae5fc8c | 2013-03-11 23:23:28 -0700 | [diff] [blame] | 666 | // Don't let us leave the shoot or preload state if there are 4 discs in |
| 667 | // the hopper. |
| 668 | if (hopper_disc_count_ >= 4 && goal_enum != Goal::SHOOT) { |
| 669 | safe_to_change_state = false; |
| 670 | } |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 671 | // Check if we have any discs to shoot or load and handle them. |
Brian Silverman | 9419505 | 2013-03-09 13:45:05 -0800 | [diff] [blame] | 672 | double min_disc_position = 0; |
Austin Schuh | 1b864a1 | 2013-03-07 00:46:50 -0800 | [diff] [blame] | 673 | if (MinDiscPosition(&min_disc_position, NULL)) { |
| 674 | const double ready_disc_position = min_disc_position + |
| 675 | ConvertDiscPositionToIndex(kReadyToPreload - kIndexStartPosition); |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 676 | |
| 677 | const double grabbed_disc_position = |
| 678 | min_disc_position + |
| 679 | ConvertDiscPositionToIndex(kReadyToLiftPosition - |
Austin Schuh | 59004d3 | 2013-03-21 04:31:45 +0000 | [diff] [blame] | 680 | kIndexStartPosition + 0.07); |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 681 | |
| 682 | // Check the state of the loader FSM. |
| 683 | // If it is ready to load discs, position the disc so that it is ready |
| 684 | // to be grabbed. |
| 685 | // If it isn't ready, there is a disc in there. It needs to finish it's |
| 686 | // cycle first. |
| 687 | if (loader_state_ != LoaderState::READY) { |
| 688 | // We already have a disc in the loader. |
| 689 | // Stage the discs back a bit. |
| 690 | wrist_loop_->R << ready_disc_position, 0.0; |
| 691 | |
Austin Schuh | bcdb90c | 2013-03-03 23:24:58 -0800 | [diff] [blame] | 692 | // Shoot if we are grabbed and being asked to shoot. |
| 693 | if (loader_state_ == LoaderState::GRABBED && |
| 694 | safe_goal_ == Goal::SHOOT) { |
| 695 | loader_goal_ = LoaderGoal::SHOOT_AND_RESET; |
Brian Silverman | 7ffb138 | 2013-09-29 16:55:12 -0700 | [diff] [blame] | 696 | is_shooting_ = true; |
Austin Schuh | bcdb90c | 2013-03-03 23:24:58 -0800 | [diff] [blame] | 697 | } |
| 698 | |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 699 | // Must wait until it has been grabbed to continue. |
| 700 | if (loader_state_ == LoaderState::GRABBING) { |
Austin Schuh | ae5fc8c | 2013-03-11 23:23:28 -0700 | [diff] [blame] | 701 | safe_to_change_state = false; |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 702 | } |
| 703 | } else { |
| 704 | // No disc up top right now. |
| 705 | wrist_loop_->R << grabbed_disc_position, 0.0; |
| 706 | |
| 707 | // See if the disc has gotten pretty far up yet. |
| 708 | if (wrist_loop_->X_hat(0, 0) > ready_disc_position) { |
| 709 | // Point of no return. We are committing to grabbing it now. |
Austin Schuh | ae5fc8c | 2013-03-11 23:23:28 -0700 | [diff] [blame] | 710 | safe_to_change_state = false; |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 711 | const double robust_grabbed_disc_position = |
| 712 | (grabbed_disc_position - |
| 713 | ConvertDiscPositionToIndex(kGrabberLength)); |
| 714 | |
| 715 | // If close, start grabbing and/or shooting. |
| 716 | if (wrist_loop_->X_hat(0, 0) > robust_grabbed_disc_position) { |
| 717 | // Start the state machine. |
| 718 | if (safe_goal_ == Goal::SHOOT) { |
| 719 | loader_goal_ = LoaderGoal::SHOOT_AND_RESET; |
| 720 | } else { |
| 721 | loader_goal_ = LoaderGoal::GRAB; |
| 722 | } |
| 723 | // This frisbee is now gone. Take it out of the queue. |
| 724 | frisbees_.pop_back(); |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | } |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 728 | } else { |
| 729 | if (loader_state_ != LoaderState::READY) { |
| 730 | // Shoot if we are grabbed and being asked to shoot. |
| 731 | if (loader_state_ == LoaderState::GRABBED && |
| 732 | safe_goal_ == Goal::SHOOT) { |
| 733 | loader_goal_ = LoaderGoal::SHOOT_AND_RESET; |
| 734 | } |
| 735 | } else { |
| 736 | // Ok, no discs in sight. Spin the hopper up by 150% of it's full |
| 737 | // range and verify that we don't see anything. |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 738 | const double hopper_clear_verification_position = |
Austin Schuh | a3e8e03 | 2013-03-10 18:43:14 -0700 | [diff] [blame] | 739 | ::std::max(upper_open_region_.lower_bound(), |
Austin Schuh | 723770b | 2013-03-10 13:26:20 -0700 | [diff] [blame] | 740 | lower_open_region_.lower_bound()) + |
Austin Schuh | f60861b | 2013-03-14 00:14:10 -0700 | [diff] [blame] | 741 | ConvertDiscPositionToIndex(kIndexFreeLength) * 1.5; |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 742 | |
| 743 | wrist_loop_->R << hopper_clear_verification_position, 0.0; |
| 744 | if (::std::abs(wrist_loop_->X_hat(0, 0) - |
| 745 | hopper_clear_verification_position) < |
| 746 | ConvertDiscPositionToIndex(0.05)) { |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 747 | // We are at the end of the range. There are no more discs here. |
| 748 | while (frisbees_.size() > 0) { |
| 749 | LOG(ERROR, "Dropping an extra disc since it can't exist\n"); |
Austin Schuh | ae5fc8c | 2013-03-11 23:23:28 -0700 | [diff] [blame] | 750 | LOG(ERROR, "Upper is [%f %f]\n", |
| 751 | upper_open_region_.upper_bound(), |
| 752 | upper_open_region_.lower_bound()); |
| 753 | LOG(ERROR, "Lower is [%f %f]\n", |
| 754 | lower_open_region_.upper_bound(), |
| 755 | lower_open_region_.lower_bound()); |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 756 | frisbees_.pop_back(); |
| 757 | --hopper_disc_count_; |
| 758 | --total_disc_count_; |
| 759 | } |
| 760 | if (hopper_disc_count_ != 0) { |
| 761 | LOG(ERROR, |
| 762 | "Emptied the hopper out but there are still discs there\n"); |
Brian Silverman | f0716e8 | 2013-03-17 23:36:11 -0700 | [diff] [blame] | 763 | hopper_disc_count_ = 0; |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 764 | } |
Brian Silverman | 7d39d86 | 2013-09-29 17:00:30 -0700 | [diff] [blame] | 765 | hopper_clear_ = true; |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 766 | } |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | { |
| 771 | const double hopper_clear_verification_position = |
Austin Schuh | a3e8e03 | 2013-03-10 18:43:14 -0700 | [diff] [blame] | 772 | ::std::max(upper_open_region_.lower_bound(), |
Austin Schuh | 723770b | 2013-03-10 13:26:20 -0700 | [diff] [blame] | 773 | lower_open_region_.lower_bound()) + |
Austin Schuh | f60861b | 2013-03-14 00:14:10 -0700 | [diff] [blame] | 774 | ConvertDiscPositionToIndex(kIndexFreeLength) * 1.5; |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 775 | |
| 776 | if (wrist_loop_->X_hat(0, 0) > |
| 777 | hopper_clear_verification_position + |
| 778 | ConvertDiscPositionToIndex(0.05)) { |
| 779 | // We are at the end of the range. There are no more discs here. |
| 780 | while (frisbees_.size() > 0) { |
| 781 | LOG(ERROR, "Dropping an extra disc since it can't exist\n"); |
Austin Schuh | a3e8e03 | 2013-03-10 18:43:14 -0700 | [diff] [blame] | 782 | LOG(ERROR, "Upper is [%f %f]\n", |
| 783 | upper_open_region_.upper_bound(), |
| 784 | upper_open_region_.lower_bound()); |
| 785 | LOG(ERROR, "Lower is [%f %f]\n", |
| 786 | lower_open_region_.upper_bound(), |
| 787 | lower_open_region_.lower_bound()); |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 788 | frisbees_.pop_back(); |
| 789 | --hopper_disc_count_; |
| 790 | --total_disc_count_; |
| 791 | } |
Brian Silverman | f0716e8 | 2013-03-17 23:36:11 -0700 | [diff] [blame] | 792 | if (hopper_disc_count_ != 0) { |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 793 | LOG(ERROR, |
Brian Silverman | 8efe23e | 2013-07-07 23:31:37 -0700 | [diff] [blame] | 794 | "Emptied the hopper out but there are still %" PRId32 " discs there\n", |
Austin Schuh | f60861b | 2013-03-14 00:14:10 -0700 | [diff] [blame] | 795 | hopper_disc_count_); |
Brian Silverman | f0716e8 | 2013-03-17 23:36:11 -0700 | [diff] [blame] | 796 | hopper_disc_count_ = 0; |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 797 | } |
Brian Silverman | 7d39d86 | 2013-09-29 17:00:30 -0700 | [diff] [blame] | 798 | hopper_clear_ = true; |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 799 | } |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 800 | } |
| 801 | |
Austin Schuh | a3e8e03 | 2013-03-10 18:43:14 -0700 | [diff] [blame] | 802 | LOG(DEBUG, "READY_SHOOTER or SHOOT\n"); |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 803 | break; |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 804 | } |
| 805 | |
Austin Schuh | d3d0fbf | 2013-03-14 00:37:00 -0700 | [diff] [blame] | 806 | // Wait for a period of time to make sure that the disc gets sucked |
| 807 | // in properly. We need to do this regardless of what the indexer is doing. |
| 808 | for (auto frisbee = frisbees_.begin(); |
| 809 | frisbee != frisbees_.end(); ++frisbee) { |
| 810 | if (now - frisbee->bottom_negedge_time_ < kTransferOffDelay) { |
| 811 | transfer_voltage = 12.0; |
| 812 | } |
| 813 | } |
| 814 | |
Austin Schuh | ae5fc8c | 2013-03-11 23:23:28 -0700 | [diff] [blame] | 815 | // If we have 4 discs, it is time to preload. |
| 816 | if (safe_to_change_state && hopper_disc_count_ >= 4) { |
| 817 | switch (safe_goal_) { |
| 818 | case Goal::HOLD: |
| 819 | case Goal::READY_LOWER: |
| 820 | case Goal::INTAKE: |
| 821 | safe_goal_ = Goal::READY_SHOOTER; |
| 822 | safe_to_change_state = false; |
Brian Silverman | 8efe23e | 2013-07-07 23:31:37 -0700 | [diff] [blame] | 823 | LOG(INFO, "We have %" PRId32 " discs, time to preload automatically\n", |
Austin Schuh | ae5fc8c | 2013-03-11 23:23:28 -0700 | [diff] [blame] | 824 | hopper_disc_count_); |
| 825 | break; |
| 826 | case Goal::READY_SHOOTER: |
| 827 | case Goal::SHOOT: |
Brian Silverman | b8d389f | 2013-03-19 22:54:06 -0700 | [diff] [blame] | 828 | case Goal::REINITIALIZE: |
Austin Schuh | ae5fc8c | 2013-03-11 23:23:28 -0700 | [diff] [blame] | 829 | break; |
| 830 | } |
| 831 | } |
| 832 | |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 833 | // The only way out of the loader is to shoot the disc. The FSM can only go |
| 834 | // forwards. |
| 835 | switch (loader_state_) { |
| 836 | case LoaderState::READY: |
Austin Schuh | a3e8e03 | 2013-03-10 18:43:14 -0700 | [diff] [blame] | 837 | LOG(DEBUG, "Loader READY\n"); |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 838 | // Open and down, ready to accept a disc. |
| 839 | loader_up_ = false; |
| 840 | disc_clamped_ = false; |
| 841 | disc_ejected_ = false; |
Brian Silverman | 40f42d9 | 2013-10-19 16:54:22 -0700 | [diff] [blame^] | 842 | disc_stuck_in_loader_ = false; |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 843 | if (loader_goal_ == LoaderGoal::GRAB || |
Brian Silverman | 9a1081b | 2013-04-05 13:50:44 -0700 | [diff] [blame] | 844 | loader_goal_ == LoaderGoal::SHOOT_AND_RESET || goal->force_fire) { |
| 845 | if (goal->force_fire) { |
| 846 | LOG(INFO, "Told to force fire, moving on\n"); |
| 847 | } else if (loader_goal_ == LoaderGoal::GRAB) { |
Austin Schuh | a3e8e03 | 2013-03-10 18:43:14 -0700 | [diff] [blame] | 848 | LOG(INFO, "Told to GRAB, moving on\n"); |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 849 | } else { |
Austin Schuh | a3e8e03 | 2013-03-10 18:43:14 -0700 | [diff] [blame] | 850 | LOG(INFO, "Told to SHOOT_AND_RESET, moving on\n"); |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 851 | } |
| 852 | loader_state_ = LoaderState::GRABBING; |
| 853 | loader_countdown_ = kGrabbingDelay; |
| 854 | } else { |
| 855 | break; |
| 856 | } |
| 857 | case LoaderState::GRABBING: |
Austin Schuh | a3e8e03 | 2013-03-10 18:43:14 -0700 | [diff] [blame] | 858 | LOG(DEBUG, "Loader GRABBING %d\n", loader_countdown_); |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 859 | // Closing the grabber. |
| 860 | loader_up_ = false; |
| 861 | disc_clamped_ = true; |
| 862 | disc_ejected_ = false; |
| 863 | if (loader_countdown_ > 0) { |
| 864 | --loader_countdown_; |
| 865 | break; |
| 866 | } else { |
| 867 | loader_state_ = LoaderState::GRABBED; |
| 868 | } |
| 869 | case LoaderState::GRABBED: |
Austin Schuh | a3e8e03 | 2013-03-10 18:43:14 -0700 | [diff] [blame] | 870 | LOG(DEBUG, "Loader GRABBED\n"); |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 871 | // Grabber closed. |
| 872 | loader_up_ = false; |
| 873 | disc_clamped_ = true; |
| 874 | disc_ejected_ = false; |
Brian Silverman | 9a1081b | 2013-04-05 13:50:44 -0700 | [diff] [blame] | 875 | if (loader_goal_ == LoaderGoal::SHOOT_AND_RESET || goal->force_fire) { |
Brian Silverman | 967c442 | 2013-09-29 16:58:59 -0700 | [diff] [blame] | 876 | if (shooter.status.FetchLatest() || shooter.status.get()) { |
Austin Schuh | a3e8e03 | 2013-03-10 18:43:14 -0700 | [diff] [blame] | 877 | // TODO(aschuh): If we aren't shooting nicely, wait until the shooter |
| 878 | // is up to speed rather than just spinning. |
Austin Schuh | f60861b | 2013-03-14 00:14:10 -0700 | [diff] [blame] | 879 | if (shooter.status->average_velocity > 130 && shooter.status->ready) { |
Austin Schuh | a3e8e03 | 2013-03-10 18:43:14 -0700 | [diff] [blame] | 880 | loader_state_ = LoaderState::LIFTING; |
| 881 | loader_countdown_ = kLiftingDelay; |
Brian Silverman | c540ab8 | 2013-09-22 00:00:28 -0700 | [diff] [blame] | 882 | loader_timeout_ = 0; |
Austin Schuh | a3e8e03 | 2013-03-10 18:43:14 -0700 | [diff] [blame] | 883 | LOG(INFO, "Told to SHOOT_AND_RESET, moving on\n"); |
| 884 | } else { |
| 885 | LOG(WARNING, "Told to SHOOT_AND_RESET, shooter too slow at %f\n", |
| 886 | shooter.status->average_velocity); |
| 887 | break; |
| 888 | } |
| 889 | } else { |
| 890 | LOG(ERROR, "Told to SHOOT_AND_RESET, no shooter data, moving on.\n"); |
| 891 | loader_state_ = LoaderState::LIFTING; |
| 892 | loader_countdown_ = kLiftingDelay; |
Brian Silverman | c540ab8 | 2013-09-22 00:00:28 -0700 | [diff] [blame] | 893 | loader_timeout_ = 0; |
Austin Schuh | a3e8e03 | 2013-03-10 18:43:14 -0700 | [diff] [blame] | 894 | } |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 895 | } else if (loader_goal_ == LoaderGoal::READY) { |
| 896 | LOG(ERROR, "Can't go to ready when we have something grabbed.\n"); |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 897 | break; |
| 898 | } else { |
| 899 | break; |
| 900 | } |
| 901 | case LoaderState::LIFTING: |
Brian Silverman | 674d970 | 2013-10-11 18:00:15 -0700 | [diff] [blame] | 902 | LOG(DEBUG, "Loader LIFTING %d %d\n", loader_countdown_, loader_timeout_); |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 903 | // Lifting the disc. |
| 904 | loader_up_ = true; |
| 905 | disc_clamped_ = true; |
| 906 | disc_ejected_ = false; |
Brian Silverman | 25329e6 | 2013-09-21 23:52:10 -0700 | [diff] [blame] | 907 | if (position->loader_top) { |
| 908 | if (loader_countdown_ > 0) { |
| 909 | --loader_countdown_; |
Brian Silverman | 004ec81 | 2013-09-26 15:47:58 -0700 | [diff] [blame] | 910 | loader_timeout_ = 0; |
| 911 | break; |
Brian Silverman | 25329e6 | 2013-09-21 23:52:10 -0700 | [diff] [blame] | 912 | } else { |
| 913 | loader_state_ = LoaderState::LIFTED; |
| 914 | } |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 915 | } else { |
Brian Silverman | 25329e6 | 2013-09-21 23:52:10 -0700 | [diff] [blame] | 916 | // Restart the countdown if it bounces back down or whatever. |
| 917 | loader_countdown_ = kLiftingDelay; |
Brian Silverman | c540ab8 | 2013-09-22 00:00:28 -0700 | [diff] [blame] | 918 | ++loader_timeout_; |
| 919 | if (loader_timeout_ > kLiftingTimeout) { |
| 920 | LOG(ERROR, "Loader timeout while LIFTING %d\n", loader_timeout_); |
Brian Silverman | 562af3e | 2013-10-19 16:44:23 -0700 | [diff] [blame] | 921 | loader_state_ = LoaderState::LOWERING; |
| 922 | loader_countdown_ = kLoweringDelay; |
| 923 | loader_timeout_ = 0; |
Brian Silverman | 40f42d9 | 2013-10-19 16:54:22 -0700 | [diff] [blame^] | 924 | disc_stuck_in_loader_ = true; |
Brian Silverman | 004ec81 | 2013-09-26 15:47:58 -0700 | [diff] [blame] | 925 | } else { |
| 926 | break; |
Brian Silverman | c540ab8 | 2013-09-22 00:00:28 -0700 | [diff] [blame] | 927 | } |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 928 | } |
| 929 | case LoaderState::LIFTED: |
Austin Schuh | a3e8e03 | 2013-03-10 18:43:14 -0700 | [diff] [blame] | 930 | LOG(DEBUG, "Loader LIFTED\n"); |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 931 | // Disc lifted. Time to eject it out. |
| 932 | loader_up_ = true; |
| 933 | disc_clamped_ = true; |
| 934 | disc_ejected_ = false; |
| 935 | loader_state_ = LoaderState::SHOOTING; |
| 936 | loader_countdown_ = kShootingDelay; |
| 937 | case LoaderState::SHOOTING: |
Austin Schuh | a3e8e03 | 2013-03-10 18:43:14 -0700 | [diff] [blame] | 938 | LOG(DEBUG, "Loader SHOOTING %d\n", loader_countdown_); |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 939 | // Ejecting the disc into the shooter. |
| 940 | loader_up_ = true; |
| 941 | disc_clamped_ = false; |
| 942 | disc_ejected_ = true; |
| 943 | if (loader_countdown_ > 0) { |
| 944 | --loader_countdown_; |
| 945 | break; |
| 946 | } else { |
| 947 | loader_state_ = LoaderState::SHOOT; |
| 948 | } |
| 949 | case LoaderState::SHOOT: |
Austin Schuh | a3e8e03 | 2013-03-10 18:43:14 -0700 | [diff] [blame] | 950 | LOG(DEBUG, "Loader SHOOT\n"); |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 951 | // The disc has been shot. |
| 952 | loader_up_ = true; |
| 953 | disc_clamped_ = false; |
| 954 | disc_ejected_ = true; |
| 955 | loader_state_ = LoaderState::LOWERING; |
| 956 | loader_countdown_ = kLoweringDelay; |
Brian Silverman | 004ec81 | 2013-09-26 15:47:58 -0700 | [diff] [blame] | 957 | loader_timeout_ = 0; |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 958 | case LoaderState::LOWERING: |
Brian Silverman | 674d970 | 2013-10-11 18:00:15 -0700 | [diff] [blame] | 959 | LOG(DEBUG, "Loader LOWERING %d %d\n", loader_countdown_, loader_timeout_); |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 960 | // Lowering the loader back down. |
| 961 | loader_up_ = false; |
| 962 | disc_clamped_ = false; |
Brian Silverman | 562af3e | 2013-10-19 16:44:23 -0700 | [diff] [blame] | 963 | // We don't want to eject if we're stuck because it will force the disc |
| 964 | // into the green loader wheel. |
Brian Silverman | 40f42d9 | 2013-10-19 16:54:22 -0700 | [diff] [blame^] | 965 | disc_ejected_ = disc_stuck_in_loader_ ? false : true; |
Brian Silverman | 004ec81 | 2013-09-26 15:47:58 -0700 | [diff] [blame] | 966 | if (position->loader_bottom) { |
| 967 | if (loader_countdown_ > 0) { |
| 968 | --loader_countdown_; |
| 969 | loader_timeout_ = 0; |
| 970 | break; |
| 971 | } else { |
| 972 | loader_state_ = LoaderState::LOWERED; |
Brian Silverman | 674d970 | 2013-10-11 18:00:15 -0700 | [diff] [blame] | 973 | --hopper_disc_count_; |
| 974 | ++shot_disc_count_; |
Brian Silverman | 004ec81 | 2013-09-26 15:47:58 -0700 | [diff] [blame] | 975 | } |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 976 | } else { |
Brian Silverman | 004ec81 | 2013-09-26 15:47:58 -0700 | [diff] [blame] | 977 | // Restart the countdown if it bounces back up or something. |
| 978 | loader_countdown_ = kLoweringDelay; |
| 979 | ++loader_timeout_; |
| 980 | if (loader_timeout_ > kLoweringTimeout) { |
| 981 | LOG(ERROR, "Loader timeout while LOWERING %d\n", loader_timeout_); |
| 982 | loader_state_ = LoaderState::LOWERED; |
Brian Silverman | 40f42d9 | 2013-10-19 16:54:22 -0700 | [diff] [blame^] | 983 | disc_stuck_in_loader_ = true; |
Brian Silverman | 004ec81 | 2013-09-26 15:47:58 -0700 | [diff] [blame] | 984 | } else { |
| 985 | break; |
| 986 | } |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 987 | } |
| 988 | case LoaderState::LOWERED: |
Austin Schuh | a3e8e03 | 2013-03-10 18:43:14 -0700 | [diff] [blame] | 989 | LOG(DEBUG, "Loader LOWERED\n"); |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 990 | loader_up_ = false; |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 991 | disc_ejected_ = false; |
Brian Silverman | 7ffb138 | 2013-09-29 16:55:12 -0700 | [diff] [blame] | 992 | is_shooting_ = false; |
Brian Silverman | 40f42d9 | 2013-10-19 16:54:22 -0700 | [diff] [blame^] | 993 | if (disc_stuck_in_loader_) { |
| 994 | disc_stuck_in_loader_ = false; |
Brian Silverman | 967c442 | 2013-09-29 16:58:59 -0700 | [diff] [blame] | 995 | disc_clamped_ = true; |
| 996 | loader_state_ = LoaderState::GRABBED; |
| 997 | } else { |
| 998 | disc_clamped_ = false; |
| 999 | loader_state_ = LoaderState::READY; |
| 1000 | // Once we have shot, we need to hang out in READY until otherwise |
| 1001 | // notified. |
| 1002 | loader_goal_ = LoaderGoal::READY; |
| 1003 | } |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 1004 | break; |
| 1005 | } |
| 1006 | |
| 1007 | // Update the observer. |
| 1008 | wrist_loop_->Update(position != NULL, output == NULL); |
| 1009 | |
| 1010 | if (position) { |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 1011 | LOG(DEBUG, "pos=%f\n", position->index_position); |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 1012 | last_bottom_disc_detect_ = position->bottom_disc_detect; |
Austin Schuh | 825bde9 | 2013-03-06 00:16:46 -0800 | [diff] [blame] | 1013 | last_top_disc_detect_ = position->top_disc_detect; |
Austin Schuh | 6328daf | 2013-03-05 00:53:15 -0800 | [diff] [blame] | 1014 | last_bottom_disc_posedge_count_ = position->bottom_disc_posedge_count; |
| 1015 | last_bottom_disc_negedge_count_ = position->bottom_disc_negedge_count; |
| 1016 | last_bottom_disc_negedge_wait_count_ = |
| 1017 | position->bottom_disc_negedge_wait_count; |
Austin Schuh | 825bde9 | 2013-03-06 00:16:46 -0800 | [diff] [blame] | 1018 | last_top_disc_posedge_count_ = position->top_disc_posedge_count; |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 1019 | last_top_disc_negedge_count_ = position->top_disc_negedge_count; |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 1020 | } |
| 1021 | |
Brian Silverman | b8d389f | 2013-03-19 22:54:06 -0700 | [diff] [blame] | 1022 | // Clear everything if we are supposed to re-initialize. |
| 1023 | if (goal_enum == Goal::REINITIALIZE) { |
| 1024 | safe_goal_ = Goal::REINITIALIZE; |
| 1025 | no_prior_position_ = true; |
| 1026 | hopper_disc_count_ = 0; |
| 1027 | total_disc_count_ = 0; |
| 1028 | shot_disc_count_ = 0; |
| 1029 | loader_state_ = LoaderState::READY; |
| 1030 | loader_goal_ = LoaderGoal::READY; |
| 1031 | loader_countdown_ = 0; |
| 1032 | loader_up_ = false; |
| 1033 | disc_clamped_ = false; |
| 1034 | disc_ejected_ = false; |
| 1035 | |
| 1036 | intake_voltage = 0.0; |
| 1037 | transfer_voltage = 0.0; |
| 1038 | wrist_loop_->U(0, 0) = 0.0; |
| 1039 | frisbees_.clear(); |
| 1040 | } |
| 1041 | |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 1042 | status->hopper_disc_count = hopper_disc_count_; |
| 1043 | status->total_disc_count = total_disc_count_; |
Austin Schuh | 70be1ba | 2013-03-10 13:37:17 -0700 | [diff] [blame] | 1044 | status->shot_disc_count = shot_disc_count_; |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 1045 | status->preloaded = (loader_state_ != LoaderState::READY); |
Brian Silverman | 7ffb138 | 2013-09-29 16:55:12 -0700 | [diff] [blame] | 1046 | status->is_shooting = is_shooting_; |
Brian Silverman | 7d39d86 | 2013-09-29 17:00:30 -0700 | [diff] [blame] | 1047 | status->hopper_clear = hopper_clear_; |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 1048 | |
| 1049 | if (output) { |
Austin Schuh | b6d898b | 2013-03-03 15:34:35 -0800 | [diff] [blame] | 1050 | output->intake_voltage = intake_voltage; |
Brian Silverman | 759d263 | 2013-10-11 18:01:21 -0700 | [diff] [blame] | 1051 | if (goal->override_transfer) { |
| 1052 | output->transfer_voltage = goal->transfer_voltage; |
| 1053 | } else { |
| 1054 | output->transfer_voltage = transfer_voltage; |
| 1055 | } |
Brian Silverman | 180e2b8 | 2013-04-08 14:29:56 -0700 | [diff] [blame] | 1056 | if (goal->override_index) { |
| 1057 | output->index_voltage = goal->index_voltage; |
| 1058 | } else { |
| 1059 | output->index_voltage = wrist_loop_->U(0, 0); |
| 1060 | } |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 1061 | output->loader_up = loader_up_; |
| 1062 | output->disc_clamped = disc_clamped_; |
| 1063 | output->disc_ejected = disc_ejected_; |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 1064 | } |
| 1065 | |
Austin Schuh | ae5fc8c | 2013-03-11 23:23:28 -0700 | [diff] [blame] | 1066 | if (safe_to_change_state) { |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 1067 | safe_goal_ = goal_enum; |
| 1068 | } |
Austin Schuh | 7c0e2aa | 2013-03-09 02:01:16 -0800 | [diff] [blame] | 1069 | if (hopper_disc_count_ < 0) { |
| 1070 | LOG(ERROR, "NEGATIVE DISCS. VERY VERY BAD\n"); |
| 1071 | } |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | } // namespace control_loops |
| 1075 | } // namespace frc971 |