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