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