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