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