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