blob: 90eaf6eee600d596837ab5d583bb77b39da0436b [file] [log] [blame]
Austin Schuh00558222013-03-03 14:16:16 -08001#include "frc971/control_loops/index/index.h"
Austin Schuhd78ab542013-03-01 22:22:19 -08002
3#include <stdio.h>
4
5#include <algorithm>
6
Austin Schuhd78ab542013-03-01 22:22:19 -08007#include "aos/common/control_loop/control_loops.q.h"
8#include "aos/common/logging/logging.h"
Brian Silverman94195052013-03-09 13:45:05 -08009#include "aos/common/inttypes.h"
Austin Schuhd78ab542013-03-01 22:22:19 -080010
11#include "frc971/constants.h"
Austin Schuh00558222013-03-03 14:16:16 -080012#include "frc971/control_loops/index/index_motor_plant.h"
Austin Schuha3e8e032013-03-10 18:43:14 -070013#include "frc971/control_loops/shooter/shooter_motor.q.h"
Austin Schuhd78ab542013-03-01 22:22:19 -080014
15using ::aos::time::Time;
16
17namespace frc971 {
18namespace control_loops {
19
Austin Schuh6b1ad2f2013-03-11 23:23:00 -070020double IndexMotor::Frisbee::ObserveNoTopDiscSensor(double index_position) {
Austin Schuhdff24e22013-03-06 00:41:21 -080021 // The absolute disc position in meters.
Austin Schuh1b864a12013-03-07 00:46:50 -080022 double disc_position = absolute_position(index_position);
Austin Schuh825bde92013-03-06 00:16:46 -080023 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 Schuhdff24e22013-03-06 00:41:21 -080027 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 Schuh6b1ad2f2013-03-11 23:23:00 -070031 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 Schuh825bde92013-03-06 00:16:46 -080036 } else {
Austin Schuh6b1ad2f2013-03-11 23:23:00 -070037 LOG(INFO, "Moving disc to bottom slow.\n");
38 index_start_position_ += distance_to_below;
39 return distance_to_below;
Austin Schuh825bde92013-03-06 00:16:46 -080040 }
41 }
Austin Schuhdff24e22013-03-06 00:41:21 -080042 return 0.0;
Austin Schuh825bde92013-03-06 00:16:46 -080043}
44
Austin Schuhd78ab542013-03-01 22:22:19 -080045IndexMotor::IndexMotor(control_loops::IndexLoop *my_index)
46 : aos::control_loops::ControlLoop<control_loops::IndexLoop>(my_index),
Austin Schuh93485832013-03-04 00:01:34 -080047 wrist_loop_(new IndexStateFeedbackLoop(MakeIndexLoop())),
Austin Schuhd78ab542013-03-01 22:22:19 -080048 hopper_disc_count_(0),
49 total_disc_count_(0),
Austin Schuh70be1ba2013-03-10 13:37:17 -070050 shot_disc_count_(0),
Austin Schuhf8c52252013-03-03 02:25:49 -080051 safe_goal_(Goal::HOLD),
52 loader_goal_(LoaderGoal::READY),
53 loader_state_(LoaderState::READY),
Austin Schuhd78ab542013-03-01 22:22:19 -080054 loader_up_(false),
55 disc_clamped_(false),
56 disc_ejected_(false),
Austin Schuhbcdb90c2013-03-03 23:24:58 -080057 last_bottom_disc_detect_(false),
Austin Schuh825bde92013-03-06 00:16:46 -080058 last_top_disc_detect_(false),
Austin Schuhbcdb90c2013-03-03 23:24:58 -080059 no_prior_position_(true),
Austin Schuh723770b2013-03-10 13:26:20 -070060 missing_position_count_(0) {
Austin Schuhd78ab542013-03-01 22:22:19 -080061}
62
Austin Schuhf8c52252013-03-03 02:25:49 -080063/*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 Schuh1b864a12013-03-07 00:46:50 -080069/*static*/ const double IndexMotor::kReadyToPreload =
70 kLoaderFreeStopPosition - ConvertDiscAngleToDiscPosition(M_PI / 6.0);
Austin Schuhf8c52252013-03-03 02:25:49 -080071/*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 Schuh6328daf2013-03-05 00:53:15 -080076/*static*/ const double IndexMotor::kGrabberMovementVelocity = 0.7;
Austin Schuhf8c52252013-03-03 02:25:49 -080077/*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 Schuhcc297022013-03-09 23:26:40 -080083/*static*/ const double IndexMotor::kBottomDiscDetectStart = 0.00;
84/*static*/ const double IndexMotor::kBottomDiscDetectStop = 0.13;
85/*static*/ const double IndexMotor::kBottomDiscIndexDelay = 0.032;
Austin Schuhd3d0fbf2013-03-14 00:37:00 -070086/*static*/ const ::aos::time::Time IndexMotor::kTransferOffDelay =
Brian Silvermanb7bcef12013-03-16 13:57:11 -070087 ::aos::time::Time::InSeconds(0.3);
Austin Schuhf8c52252013-03-03 02:25:49 -080088
Austin Schuh7c0e2aa2013-03-09 02:01:16 -080089// TODO(aschuh): Verify these with the sensor actually on.
Austin Schuh825bde92013-03-06 00:16:46 -080090/*static*/ const double IndexMotor::kTopDiscDetectStart =
91 (IndexMotor::kLoaderFreeStopPosition -
Austin Schuh7c0e2aa2013-03-09 02:01:16 -080092 IndexMotor::ConvertDiscAngleToDiscPosition(49 * M_PI / 180));
Austin Schuh825bde92013-03-06 00:16:46 -080093/*static*/ const double IndexMotor::kTopDiscDetectStop =
Austin Schuh7c0e2aa2013-03-09 02:01:16 -080094 (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 Schuhf8c52252013-03-03 02:25:49 -0800103
Austin Schuhd78ab542013-03-01 22:22:19 -0800104const /*static*/ double IndexMotor::kDiscRadius = 10.875 * 0.0254 / 2;
105const /*static*/ double IndexMotor::kRollerRadius = 2.0 * 0.0254 / 2;
Austin Schuhf8c52252013-03-03 02:25:49 -0800106const /*static*/ double IndexMotor::kTransferRollerRadius = 1.25 * 0.0254 / 2;
Austin Schuhd78ab542013-03-01 22:22:19 -0800107
Austin Schuhf8c52252013-03-03 02:25:49 -0800108/*static*/ const int IndexMotor::kGrabbingDelay = 5;
Austin Schuha3e8e032013-03-10 18:43:14 -0700109/*static*/ const int IndexMotor::kLiftingDelay = 30;
Austin Schuhf8c52252013-03-03 02:25:49 -0800110/*static*/ const int IndexMotor::kShootingDelay = 5;
111/*static*/ const int IndexMotor::kLoweringDelay = 20;
Austin Schuhd78ab542013-03-01 22:22:19 -0800112
Austin Schuh93485832013-03-04 00:01:34 -0800113// TODO(aschuh): Tune these.
114/*static*/ const double
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700115 IndexMotor::IndexStateFeedbackLoop::kMinMotionVoltage = 11.0;
Austin Schuh93485832013-03-04 00:01:34 -0800116/*static*/ const double
Austin Schuha3e8e032013-03-10 18:43:14 -0700117 IndexMotor::IndexStateFeedbackLoop::kNoMotionCuttoffCount = 20;
Austin Schuh93485832013-03-04 00:01:34 -0800118
Austin Schuhd78ab542013-03-01 22:22:19 -0800119/*static*/ double IndexMotor::ConvertDiscAngleToIndex(const double angle) {
120 return (angle * (1 + (kDiscRadius * 2 + kRollerRadius) / kRollerRadius));
121}
122
Austin Schuhf8c52252013-03-03 02:25:49 -0800123/*static*/ double IndexMotor::ConvertDiscAngleToDiscPosition(
124 const double angle) {
Austin Schuhd78ab542013-03-01 22:22:19 -0800125 return angle * (kDiscRadius + kRollerRadius);
126}
127
Austin Schuhf8c52252013-03-03 02:25:49 -0800128/*static*/ double IndexMotor::ConvertDiscPositionToDiscAngle(
129 const double position) {
130 return position / (kDiscRadius + kRollerRadius);
131}
132
Austin Schuhd78ab542013-03-01 22:22:19 -0800133/*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 Schuhf8c52252013-03-03 02:25:49 -0800142/*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 Schuh1b864a12013-03-07 00:46:50 -0800155bool IndexMotor::MinDiscPosition(double *disc_position, Frisbee **found_disc) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800156 bool found_start = false;
157 for (unsigned int i = 0; i < frisbees_.size(); ++i) {
Austin Schuh1b864a12013-03-07 00:46:50 -0800158 Frisbee &frisbee = frisbees_[i];
Austin Schuhf8c52252013-03-03 02:25:49 -0800159 if (!found_start) {
160 if (frisbee.has_position()) {
161 *disc_position = frisbee.position();
Austin Schuh1b864a12013-03-07 00:46:50 -0800162 if (found_disc) {
163 *found_disc = &frisbee;
164 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800165 found_start = true;
166 }
167 } else {
Austin Schuh1b864a12013-03-07 00:46:50 -0800168 if (frisbee.position() <= *disc_position) {
169 *disc_position = frisbee.position();
170 if (found_disc) {
171 *found_disc = &frisbee;
172 }
173 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800174 }
175 }
176 return found_start;
177}
178
Austin Schuh1b864a12013-03-07 00:46:50 -0800179bool IndexMotor::MaxDiscPosition(double *disc_position, Frisbee **found_disc) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800180 bool found_start = false;
181 for (unsigned int i = 0; i < frisbees_.size(); ++i) {
Austin Schuh1b864a12013-03-07 00:46:50 -0800182 Frisbee &frisbee = frisbees_[i];
Austin Schuhf8c52252013-03-03 02:25:49 -0800183 if (!found_start) {
184 if (frisbee.has_position()) {
185 *disc_position = frisbee.position();
Austin Schuh1b864a12013-03-07 00:46:50 -0800186 if (found_disc) {
187 *found_disc = &frisbee;
188 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800189 found_start = true;
190 }
191 } else {
Austin Schuh1b864a12013-03-07 00:46:50 -0800192 if (frisbee.position() > *disc_position) {
193 *disc_position = frisbee.position();
194 if (found_disc) {
195 *found_disc = &frisbee;
196 }
197 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800198 }
199 }
200 return found_start;
201}
202
Austin Schuh93485832013-03-04 00:01:34 -0800203void 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 Schuh93485832013-03-04 00:01:34 -0800211 U(0, 0) = 0.0;
212 }
213 } else {
214 low_voltage_count_ = 0;
215 }
216
217 for (int i = 0; i < kNumOutputs; ++i) {
Austin Schuh9644e1c2013-03-12 00:40:36 -0700218 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 Schuh93485832013-03-04 00:01:34 -0800222 }
223 }
224}
225
226
Austin Schuhd78ab542013-03-01 22:22:19 -0800227// Positive angle is towards the shooter, and positive power is towards the
228// shooter.
229void 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 Schuhd3d0fbf2013-03-14 00:37:00 -0700234 Time now = Time::Now();
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800235 // Make goal easy to work with and sanity check it.
Austin Schuhd78ab542013-03-01 22:22:19 -0800236 Goal goal_enum = static_cast<Goal>(goal->goal_state);
Brian Silvermanb8d389f2013-03-19 22:54:06 -0700237 if (goal->goal_state < 0 || goal->goal_state > 5) {
Brian Silverman94195052013-03-09 13:45:05 -0800238 LOG(ERROR, "Goal state is %"PRId32" which is out of range. Going to HOLD.\n",
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800239 goal->goal_state);
240 goal_enum = Goal::HOLD;
241 }
Austin Schuhd78ab542013-03-01 22:22:19 -0800242
243 // Disable the motors now so that all early returns will return with the
244 // motors disabled.
Austin Schuhb6d898b2013-03-03 15:34:35 -0800245 double intake_voltage = 0.0;
Austin Schuhf8c52252013-03-03 02:25:49 -0800246 double transfer_voltage = 0.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800247 if (output) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800248 output->intake_voltage = 0.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800249 output->transfer_voltage = 0.0;
250 output->index_voltage = 0.0;
251 }
252
253 status->ready_to_intake = false;
254
Austin Schuhe3490622013-03-13 01:24:30 -0700255 // 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 Schuhf8c52252013-03-03 02:25:49 -0800260 // Compute a safe index position that we can use.
Austin Schuhd78ab542013-03-01 22:22:19 -0800261 if (position) {
262 wrist_loop_->Y << position->index_position;
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800263 // 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 Silverman43bb73e2013-03-17 13:39:47 -0700266 LOG(INFO, "no prior position; resetting\n");
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800267 wrist_loop_->R << wrist_loop_->Y(0, 0), 0.0;
Austin Schuhc5ef1bb2013-03-10 00:42:05 -0800268 wrist_loop_->X_hat(0, 0) = wrist_loop_->Y(0, 0);
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800269 no_prior_position_ = false;
Austin Schuh6328daf2013-03-05 00:53:15 -0800270 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 Schuh825bde92013-03-06 00:16:46 -0800274 last_top_disc_posedge_count_ = position->top_disc_posedge_count;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800275 last_top_disc_negedge_count_ = position->top_disc_negedge_count;
Brian Silvermance86bac2013-03-31 19:07:24 -0700276 last_top_disc_detect_ = position->top_disc_detect;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800277 // The open positions for the upper is right here and isn't a hard edge.
Austin Schuh723770b2013-03-10 13:26:20 -0700278 upper_open_region_.Restart(wrist_loop_->Y(0, 0));
279 lower_open_region_.Restart(wrist_loop_->Y(0, 0));
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800280 }
281
Austin Schuh1b864a12013-03-07 00:46:50 -0800282 // If the cRIO is gone for over 1/2 of a second, assume that it rebooted.
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800283 if (missing_position_count_ > 50) {
Brian Silverman43bb73e2013-03-17 13:39:47 -0700284 LOG(INFO, "assuming cRIO rebooted\n");
Austin Schuh6328daf2013-03-05 00:53:15 -0800285 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 Schuh825bde92013-03-06 00:16:46 -0800289 last_top_disc_posedge_count_ = position->top_disc_posedge_count;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800290 last_top_disc_negedge_count_ = position->top_disc_negedge_count;
Brian Silvermance86bac2013-03-31 19:07:24 -0700291 last_top_disc_detect_ = position->top_disc_detect;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800292 // We can't really trust the open range any more if the crio rebooted.
Austin Schuh723770b2013-03-10 13:26:20 -0700293 upper_open_region_.Restart(wrist_loop_->Y(0, 0));
294 lower_open_region_.Restart(wrist_loop_->Y(0, 0));
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800295 // 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 Schuhc5ef1bb2013-03-10 00:42:05 -0800302 wrist_loop_->X_hat(0, 0) = wrist_loop_->Y(0, 0);
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800303 }
304 missing_position_count_ = 0;
Austin Schuh81fb1bc2013-04-04 05:52:28 +0000305 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 Silvermance86bac2013-03-31 19:07:24 -0700309 last_top_disc_posedge_count_ = position->top_disc_posedge_count;
310 }
Austin Schuh81fb1bc2013-04-04 05:52:28 +0000311 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 Silvermance86bac2013-03-31 19:07:24 -0700315 last_top_disc_negedge_count_ = position->top_disc_negedge_count;
316 }
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800317 } else {
318 ++missing_position_count_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800319 }
320 const double index_position = wrist_loop_->X_hat(0, 0);
321
Austin Schuh825bde92013-03-06 00:16:46 -0800322 if (position) {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800323 // Reset the open region if we saw a negedge.
Austin Schuh723770b2013-03-10 13:26:20 -0700324 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 Schuh7c0e2aa2013-03-09 02:01:16 -0800330 if (position->top_disc_negedge_count != last_top_disc_negedge_count_) {
331 // Saw a negedge, must be a new region.
Austin Schuh723770b2013-03-10 13:26:20 -0700332 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 Schuh7c0e2aa2013-03-09 02:01:16 -0800338 }
339
340 // No disc. Expand the open region.
341 if (!position->top_disc_detect) {
Austin Schuh723770b2013-03-10 13:26:20 -0700342 upper_open_region_.Expand(index_position);
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800343 }
344
Austin Schuh825bde92013-03-06 00:16:46 -0800345 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 Schuh1b864a12013-03-07 00:46:50 -0800348 // 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 Schuhdff24e22013-03-06 00:41:21 -0800350
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 Schuh6b1ad2f2013-03-11 23:23:00 -0700356 wrist_loop_->X_hat(0, 0));
Austin Schuhdff24e22013-03-06 00:41:21 -0800357 cumulative_offset += amount_moved;
Austin Schuh825bde92013-03-06 00:16:46 -0800358 }
359 }
Austin Schuh1b864a12013-03-07 00:46:50 -0800360
Austin Schuh825bde92013-03-06 00:16:46 -0800361 if (position->top_disc_posedge_count != last_top_disc_posedge_count_) {
Austin Schuh81fb1bc2013-04-04 05:52:28 +0000362 LOG(INFO, "Saw a posedge\n");
Austin Schuh1b864a12013-03-07 00:46:50 -0800363 const double index_position = wrist_loop_->X_hat(0, 0) -
364 position->index_position + position->top_disc_posedge_position;
Austin Schuh825bde92013-03-06 00:16:46 -0800365 // 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 Schuh7c0e2aa2013-03-09 02:01:16 -0800369 // 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 Schuh81fb1bc2013-04-04 05:52:28 +0000372 if (wrist_loop_->X_hat(1, 0) > 100.0) {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800373 disc_direction = 1;
Austin Schuh81fb1bc2013-04-04 05:52:28 +0000374 } else if (wrist_loop_->X_hat(1, 0) < -100.0) {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800375 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 Schuh723770b2013-03-10 13:26:20 -0700382 const double open_width = upper_open_region_.width();
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800383 const double relative_upper_open_precentage =
Austin Schuh723770b2013-03-10 13:26:20 -0700384 (upper_open_region_.upper_bound() - index_position) / open_width;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800385 const double relative_lower_open_precentage =
Austin Schuh723770b2013-03-10 13:26:20 -0700386 (index_position - upper_open_region_.lower_bound()) / open_width;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800387
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 Schuha3e8e032013-03-10 18:43:14 -0700395 LOG(INFO, "Disc edge going down\n");
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800396 } 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 Schuha3e8e032013-03-10 18:43:14 -0700400 LOG(INFO, "Disc edge going up\n");
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800401 } 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 Schuh825bde92013-03-06 00:16:46 -0800410 // Moving up at a reasonable clip.
Austin Schuh1b864a12013-03-07 00:46:50 -0800411 // 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 Schuh7c0e2aa2013-03-09 02:01:16 -0800420 ++hopper_disc_count_;
421 ++total_disc_count_;
Austin Schuh1b864a12013-03-07 00:46:50 -0800422 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 Schuh09e07082013-03-19 10:04:12 +0000443
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 Schuh1b864a12013-03-07 00:46:50 -0800457 if (above_disc_count > 1) {
458 LOG(ERROR, "We have 2 discs above the top sensor.\n");
459 }
Austin Schuh1b864a12013-03-07 00:46:50 -0800460 // 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 Schuh59004d32013-03-21 04:31:45 +0000469 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 Schuh1b864a12013-03-07 00:46:50 -0800473 frisbee->OffsetDisc(disc_delta);
474 }
475 }
Brian Silverman43bb73e2013-03-17 13:39:47 -0700476 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 Schuh7c0e2aa2013-03-09 02:01:16 -0800487 } else if (disc_direction < 0) {
Austin Schuh825bde92013-03-06 00:16:46 -0800488 // Moving down at a reasonable clip.
Austin Schuh1b864a12013-03-07 00:46:50 -0800489 // 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 Silverman94195052013-03-09 13:45:05 -0800491 double min_disc_position = 0;
Austin Schuh1b864a12013-03-07 00:46:50 -0800492 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 Schuha3e8e032013-03-10 18:43:14 -0700504 LOG(INFO, "Posedge going down. Moving top disc down by %f\n",
505 disc_delta_meters);
Austin Schuh1b864a12013-03-07 00:46:50 -0800506 for (auto frisbee = frisbees_.begin(), end = frisbees_.end();
507 frisbee != end; ++frisbee) {
508 frisbee->OffsetDisc(disc_delta);
509 }
510 }
Austin Schuh825bde92013-03-06 00:16:46 -0800511 } else {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800512 LOG(ERROR, "Not sure how to handle the upper posedge, doing nothing\n");
Austin Schuh825bde92013-03-06 00:16:46 -0800513 }
514 }
515 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800516
Austin Schuhf8c52252013-03-03 02:25:49 -0800517 // Bool to track if it is safe for the goal to change yet.
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700518 bool safe_to_change_state = true;
Brian Silvermanf0716e82013-03-17 23:36:11 -0700519 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 Schuhd78ab542013-03-01 22:22:19 -0800527 switch (safe_goal_) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800528 case Goal::HOLD:
Austin Schuhd78ab542013-03-01 22:22:19 -0800529 // The goal should already be good, so sit tight with everything the same
530 // as it was.
Austin Schuhd78ab542013-03-01 22:22:19 -0800531 break;
Austin Schuhf8c52252013-03-03 02:25:49 -0800532 case Goal::READY_LOWER:
533 case Goal::INTAKE:
Austin Schuhd78ab542013-03-01 22:22:19 -0800534 {
Austin Schuhd78ab542013-03-01 22:22:19 -0800535 if (position) {
Austin Schuh6328daf2013-03-05 00:53:15 -0800536 // Posedge of the disc entering the beam break.
537 if (position->bottom_disc_posedge_count !=
538 last_bottom_disc_posedge_count_) {
Austin Schuhd78ab542013-03-01 22:22:19 -0800539 transfer_frisbee_.Reset();
540 transfer_frisbee_.bottom_posedge_time_ = now;
Austin Schuha3e8e032013-03-10 18:43:14 -0700541 LOG(INFO, "Posedge of bottom disc %f\n",
542 transfer_frisbee_.bottom_posedge_time_.ToSeconds());
Austin Schuhd78ab542013-03-01 22:22:19 -0800543 ++hopper_disc_count_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800544 ++total_disc_count_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800545 }
546
547 // Disc exited the beam break now.
Austin Schuh6328daf2013-03-05 00:53:15 -0800548 if (position->bottom_disc_negedge_count !=
549 last_bottom_disc_negedge_count_) {
Austin Schuhd78ab542013-03-01 22:22:19 -0800550 transfer_frisbee_.bottom_negedge_time_ = now;
Austin Schuha3e8e032013-03-10 18:43:14 -0700551 LOG(INFO, "Negedge of bottom disc %f\n",
552 transfer_frisbee_.bottom_negedge_time_.ToSeconds());
Austin Schuhd78ab542013-03-01 22:22:19 -0800553 frisbees_.push_front(transfer_frisbee_);
554 }
555
556 if (position->bottom_disc_detect) {
Brian Silvermanc5277542013-03-22 13:33:07 -0700557 intake_voltage = 0.0;
558 transfer_voltage = 12.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800559 // Must wait until the disc gets out before we can change state.
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700560 safe_to_change_state = false;
Austin Schuhd78ab542013-03-01 22:22:19 -0800561
Austin Schuhf8c52252013-03-03 02:25:49 -0800562 // TODO(aschuh): A disc on the way through needs to start moving
563 // the indexer if it isn't already moving. Maybe?
Austin Schuhd78ab542013-03-01 22:22:19 -0800564
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 Silvermance86bac2013-03-31 19:07:24 -0700570 intake_voltage = -12.0;
Brian Silvermanf0716e82013-03-17 23:36:11 -0700571 transfer_voltage = -12.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800572 }
573 }
574
Austin Schuhf8c52252013-03-03 02:25:49 -0800575 // Check all non-indexed discs and see if they should be indexed.
Austin Schuhb6d898b2013-03-03 15:34:35 -0800576 for (auto frisbee = frisbees_.begin();
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800577 frisbee != frisbees_.end(); ++frisbee) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800578 if (!frisbee->has_been_indexed_) {
Austin Schuh6328daf2013-03-05 00:53:15 -0800579 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 Schuhd78ab542013-03-01 22:22:19 -0800590 LOG(INFO, "Grabbed on the index now at %f\n", index_position);
Austin Schuhb6d898b2013-03-03 15:34:35 -0800591 frisbee->has_been_indexed_ = true;
Austin Schuh6328daf2013-03-05 00:53:15 -0800592 frisbee->index_start_position_ =
593 position->bottom_disc_negedge_wait_position;
Austin Schuhd78ab542013-03-01 22:22:19 -0800594 }
595 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800596 }
Austin Schuhd78ab542013-03-01 22:22:19 -0800597 }
Austin Schuh59004d32013-03-21 04:31:45 +0000598 for (auto frisbee = frisbees_.begin();
599 frisbee != frisbees_.end(); ++frisbee) {
600 if (!frisbee->has_been_indexed_) {
Brian Silvermanc5277542013-03-22 13:33:07 -0700601 intake_voltage = 0.0;
602 transfer_voltage = 12.0;
Austin Schuh59004d32013-03-21 04:31:45 +0000603
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 Schuhd78ab542013-03-01 22:22:19 -0800652 }
Austin Schuh59004d32013-03-21 04:31:45 +0000653 LOG(DEBUG, "INTAKE\n");
Austin Schuhd78ab542013-03-01 22:22:19 -0800654 break;
Brian Silvermanb8d389f2013-03-19 22:54:06 -0700655 case Goal::REINITIALIZE:
656 LOG(WARNING, "Reinitializing the indexer\n");
657 break;
Austin Schuhf8c52252013-03-03 02:25:49 -0800658 case Goal::READY_SHOOTER:
659 case Goal::SHOOT:
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700660 // 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 Schuhf8c52252013-03-03 02:25:49 -0800665 // Check if we have any discs to shoot or load and handle them.
Brian Silverman94195052013-03-09 13:45:05 -0800666 double min_disc_position = 0;
Austin Schuh1b864a12013-03-07 00:46:50 -0800667 if (MinDiscPosition(&min_disc_position, NULL)) {
668 const double ready_disc_position = min_disc_position +
669 ConvertDiscPositionToIndex(kReadyToPreload - kIndexStartPosition);
Austin Schuhf8c52252013-03-03 02:25:49 -0800670
671 const double grabbed_disc_position =
672 min_disc_position +
673 ConvertDiscPositionToIndex(kReadyToLiftPosition -
Austin Schuh59004d32013-03-21 04:31:45 +0000674 kIndexStartPosition + 0.07);
Austin Schuhf8c52252013-03-03 02:25:49 -0800675
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 Schuhbcdb90c2013-03-03 23:24:58 -0800686 // 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 Schuhf8c52252013-03-03 02:25:49 -0800692 // Must wait until it has been grabbed to continue.
693 if (loader_state_ == LoaderState::GRABBING) {
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700694 safe_to_change_state = false;
Austin Schuhf8c52252013-03-03 02:25:49 -0800695 }
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 Schuhae5fc8c2013-03-11 23:23:28 -0700703 safe_to_change_state = false;
Austin Schuhf8c52252013-03-03 02:25:49 -0800704 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 Schuhf8c52252013-03-03 02:25:49 -0800718 }
719 }
720 }
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800721 } 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 Schuh7c0e2aa2013-03-09 02:01:16 -0800731 const double hopper_clear_verification_position =
Austin Schuha3e8e032013-03-10 18:43:14 -0700732 ::std::max(upper_open_region_.lower_bound(),
Austin Schuh723770b2013-03-10 13:26:20 -0700733 lower_open_region_.lower_bound()) +
Austin Schuhf60861b2013-03-14 00:14:10 -0700734 ConvertDiscPositionToIndex(kIndexFreeLength) * 1.5;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800735
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 Schuh7c0e2aa2013-03-09 02:01:16 -0800740 // 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 Schuhae5fc8c2013-03-11 23:23:28 -0700743 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 Schuh7c0e2aa2013-03-09 02:01:16 -0800749 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 Silvermanf0716e82013-03-17 23:36:11 -0700756 hopper_disc_count_ = 0;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800757 }
758 }
759 }
760 }
761
762 {
763 const double hopper_clear_verification_position =
Austin Schuha3e8e032013-03-10 18:43:14 -0700764 ::std::max(upper_open_region_.lower_bound(),
Austin Schuh723770b2013-03-10 13:26:20 -0700765 lower_open_region_.lower_bound()) +
Austin Schuhf60861b2013-03-14 00:14:10 -0700766 ConvertDiscPositionToIndex(kIndexFreeLength) * 1.5;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800767
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 Schuha3e8e032013-03-10 18:43:14 -0700774 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 Schuh7c0e2aa2013-03-09 02:01:16 -0800780 frisbees_.pop_back();
781 --hopper_disc_count_;
782 --total_disc_count_;
783 }
Brian Silvermanf0716e82013-03-17 23:36:11 -0700784 if (hopper_disc_count_ != 0) {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800785 LOG(ERROR,
Brian Silvermanb7bcef12013-03-16 13:57:11 -0700786 "Emptied the hopper out but there are still %"PRId32" discs there\n",
Austin Schuhf60861b2013-03-14 00:14:10 -0700787 hopper_disc_count_);
Brian Silvermanf0716e82013-03-17 23:36:11 -0700788 hopper_disc_count_ = 0;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800789 }
790 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800791 }
792
Austin Schuha3e8e032013-03-10 18:43:14 -0700793 LOG(DEBUG, "READY_SHOOTER or SHOOT\n");
Austin Schuhd78ab542013-03-01 22:22:19 -0800794 break;
Austin Schuhf8c52252013-03-03 02:25:49 -0800795 }
796
Austin Schuhd3d0fbf2013-03-14 00:37:00 -0700797 // 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 Schuhae5fc8c2013-03-11 23:23:28 -0700806 // 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 Silvermana0238ce2013-03-13 17:58:45 -0700814 LOG(INFO, "We have %"PRId32" discs, time to preload automatically\n",
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700815 hopper_disc_count_);
816 break;
817 case Goal::READY_SHOOTER:
818 case Goal::SHOOT:
Brian Silvermanb8d389f2013-03-19 22:54:06 -0700819 case Goal::REINITIALIZE:
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700820 break;
821 }
822 }
823
Austin Schuhf8c52252013-03-03 02:25:49 -0800824 // 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 Schuha3e8e032013-03-10 18:43:14 -0700828 LOG(DEBUG, "Loader READY\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800829 // 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 Silverman9a1081b2013-04-05 13:50:44 -0700834 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 Schuha3e8e032013-03-10 18:43:14 -0700838 LOG(INFO, "Told to GRAB, moving on\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800839 } else {
Austin Schuha3e8e032013-03-10 18:43:14 -0700840 LOG(INFO, "Told to SHOOT_AND_RESET, moving on\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800841 }
842 loader_state_ = LoaderState::GRABBING;
843 loader_countdown_ = kGrabbingDelay;
844 } else {
845 break;
846 }
847 case LoaderState::GRABBING:
Austin Schuha3e8e032013-03-10 18:43:14 -0700848 LOG(DEBUG, "Loader GRABBING %d\n", loader_countdown_);
Austin Schuhf8c52252013-03-03 02:25:49 -0800849 // 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 Schuha3e8e032013-03-10 18:43:14 -0700860 LOG(DEBUG, "Loader GRABBED\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800861 // Grabber closed.
862 loader_up_ = false;
863 disc_clamped_ = true;
864 disc_ejected_ = false;
Brian Silverman9a1081b2013-04-05 13:50:44 -0700865 if (loader_goal_ == LoaderGoal::SHOOT_AND_RESET || goal->force_fire) {
Austin Schuha3e8e032013-03-10 18:43:14 -0700866 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 Schuhf60861b2013-03-14 00:14:10 -0700870 if (shooter.status->average_velocity > 130 && shooter.status->ready) {
Austin Schuha3e8e032013-03-10 18:43:14 -0700871 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 Schuhf8c52252013-03-03 02:25:49 -0800884 } else if (loader_goal_ == LoaderGoal::READY) {
885 LOG(ERROR, "Can't go to ready when we have something grabbed.\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800886 break;
887 } else {
888 break;
889 }
890 case LoaderState::LIFTING:
Austin Schuha3e8e032013-03-10 18:43:14 -0700891 LOG(DEBUG, "Loader LIFTING %d\n", loader_countdown_);
Austin Schuhf8c52252013-03-03 02:25:49 -0800892 // 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 Schuha3e8e032013-03-10 18:43:14 -0700903 LOG(DEBUG, "Loader LIFTED\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800904 // 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 Schuha3e8e032013-03-10 18:43:14 -0700911 LOG(DEBUG, "Loader SHOOTING %d\n", loader_countdown_);
Austin Schuhf8c52252013-03-03 02:25:49 -0800912 // 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 Schuha3e8e032013-03-10 18:43:14 -0700923 LOG(DEBUG, "Loader SHOOT\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800924 // 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 Schuh7c0e2aa2013-03-09 02:01:16 -0800930 --hopper_disc_count_;
Austin Schuh70be1ba2013-03-10 13:37:17 -0700931 ++shot_disc_count_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800932 case LoaderState::LOWERING:
Austin Schuha3e8e032013-03-10 18:43:14 -0700933 LOG(DEBUG, "Loader LOWERING %d\n", loader_countdown_);
Austin Schuhf8c52252013-03-03 02:25:49 -0800934 // 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 Schuha3e8e032013-03-10 18:43:14 -0700945 LOG(DEBUG, "Loader LOWERED\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800946 // 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 Schuhd78ab542013-03-01 22:22:19 -0800954 break;
955 }
956
957 // Update the observer.
958 wrist_loop_->Update(position != NULL, output == NULL);
959
960 if (position) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800961 LOG(DEBUG, "pos=%f\n", position->index_position);
Austin Schuhd78ab542013-03-01 22:22:19 -0800962 last_bottom_disc_detect_ = position->bottom_disc_detect;
Austin Schuh825bde92013-03-06 00:16:46 -0800963 last_top_disc_detect_ = position->top_disc_detect;
Austin Schuh6328daf2013-03-05 00:53:15 -0800964 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 Schuh825bde92013-03-06 00:16:46 -0800968 last_top_disc_posedge_count_ = position->top_disc_posedge_count;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800969 last_top_disc_negedge_count_ = position->top_disc_negedge_count;
Austin Schuhd78ab542013-03-01 22:22:19 -0800970 }
971
Brian Silvermanb8d389f2013-03-19 22:54:06 -0700972 // 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 Schuhd78ab542013-03-01 22:22:19 -0800992 status->hopper_disc_count = hopper_disc_count_;
993 status->total_disc_count = total_disc_count_;
Austin Schuh70be1ba2013-03-10 13:37:17 -0700994 status->shot_disc_count = shot_disc_count_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800995 status->preloaded = (loader_state_ != LoaderState::READY);
Austin Schuhd78ab542013-03-01 22:22:19 -0800996
997 if (output) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800998 output->intake_voltage = intake_voltage;
Austin Schuhf8c52252013-03-03 02:25:49 -0800999 output->transfer_voltage = transfer_voltage;
Brian Silverman180e2b82013-04-08 14:29:56 -07001000 if (goal->override_index) {
1001 output->index_voltage = goal->index_voltage;
1002 } else {
1003 output->index_voltage = wrist_loop_->U(0, 0);
1004 }
Austin Schuhf8c52252013-03-03 02:25:49 -08001005 output->loader_up = loader_up_;
1006 output->disc_clamped = disc_clamped_;
1007 output->disc_ejected = disc_ejected_;
Austin Schuhd78ab542013-03-01 22:22:19 -08001008 }
1009
Austin Schuhae5fc8c2013-03-11 23:23:28 -07001010 if (safe_to_change_state) {
Austin Schuhd78ab542013-03-01 22:22:19 -08001011 safe_goal_ = goal_enum;
1012 }
Austin Schuh7c0e2aa2013-03-09 02:01:16 -08001013 if (hopper_disc_count_ < 0) {
1014 LOG(ERROR, "NEGATIVE DISCS. VERY VERY BAD\n");
1015 }
Austin Schuhd78ab542013-03-01 22:22:19 -08001016}
1017
1018} // namespace control_loops
1019} // namespace frc971