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