blob: a8ef5bfdb55e95260bb161fd0e42bd529197fbe0 [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
7#include "aos/aos_core.h"
8
9#include "aos/common/messages/RobotState.q.h"
10#include "aos/common/control_loop/control_loops.q.h"
11#include "aos/common/logging/logging.h"
Brian Silverman94195052013-03-09 13:45:05 -080012#include "aos/common/inttypes.h"
Austin Schuhd78ab542013-03-01 22:22:19 -080013
14#include "frc971/constants.h"
Austin Schuh00558222013-03-03 14:16:16 -080015#include "frc971/control_loops/index/index_motor_plant.h"
Austin Schuha3e8e032013-03-10 18:43:14 -070016#include "frc971/control_loops/shooter/shooter_motor.q.h"
Austin Schuhd78ab542013-03-01 22:22:19 -080017
18using ::aos::time::Time;
19
20namespace frc971 {
21namespace control_loops {
22
Austin Schuh6b1ad2f2013-03-11 23:23:00 -070023double IndexMotor::Frisbee::ObserveNoTopDiscSensor(double index_position) {
Austin Schuhdff24e22013-03-06 00:41:21 -080024 // The absolute disc position in meters.
Austin Schuh1b864a12013-03-07 00:46:50 -080025 double disc_position = absolute_position(index_position);
Austin Schuh825bde92013-03-06 00:16:46 -080026 if (IndexMotor::kTopDiscDetectStart <= disc_position &&
27 disc_position <= IndexMotor::kTopDiscDetectStop) {
28 // Whoops, this shouldn't be happening.
29 // Move the disc off the way that makes most sense.
Austin Schuhdff24e22013-03-06 00:41:21 -080030 double distance_to_above = IndexMotor::ConvertDiscPositionToIndex(
31 ::std::abs(disc_position - IndexMotor::kTopDiscDetectStop));
32 double distance_to_below = IndexMotor::ConvertDiscPositionToIndex(
33 ::std::abs(disc_position - IndexMotor::kTopDiscDetectStart));
Austin Schuh6b1ad2f2013-03-11 23:23:00 -070034 if (distance_to_above < distance_to_below) {
35 LOG(INFO, "Moving disc to top slow.\n");
36 // Move it up.
37 index_start_position_ -= distance_to_above;
38 return -distance_to_above;
Austin Schuh825bde92013-03-06 00:16:46 -080039 } else {
Austin Schuh6b1ad2f2013-03-11 23:23:00 -070040 LOG(INFO, "Moving disc to bottom slow.\n");
41 index_start_position_ += distance_to_below;
42 return distance_to_below;
Austin Schuh825bde92013-03-06 00:16:46 -080043 }
44 }
Austin Schuhdff24e22013-03-06 00:41:21 -080045 return 0.0;
Austin Schuh825bde92013-03-06 00:16:46 -080046}
47
Austin Schuhd78ab542013-03-01 22:22:19 -080048IndexMotor::IndexMotor(control_loops::IndexLoop *my_index)
49 : aos::control_loops::ControlLoop<control_loops::IndexLoop>(my_index),
Austin Schuh93485832013-03-04 00:01:34 -080050 wrist_loop_(new IndexStateFeedbackLoop(MakeIndexLoop())),
Austin Schuhd78ab542013-03-01 22:22:19 -080051 hopper_disc_count_(0),
52 total_disc_count_(0),
Austin Schuh70be1ba2013-03-10 13:37:17 -070053 shot_disc_count_(0),
Austin Schuhf8c52252013-03-03 02:25:49 -080054 safe_goal_(Goal::HOLD),
55 loader_goal_(LoaderGoal::READY),
56 loader_state_(LoaderState::READY),
Austin Schuhd78ab542013-03-01 22:22:19 -080057 loader_up_(false),
58 disc_clamped_(false),
59 disc_ejected_(false),
Austin Schuhbcdb90c2013-03-03 23:24:58 -080060 last_bottom_disc_detect_(false),
Austin Schuh825bde92013-03-06 00:16:46 -080061 last_top_disc_detect_(false),
Austin Schuhbcdb90c2013-03-03 23:24:58 -080062 no_prior_position_(true),
Austin Schuh723770b2013-03-10 13:26:20 -070063 missing_position_count_(0) {
Austin Schuhd78ab542013-03-01 22:22:19 -080064}
65
Austin Schuhf8c52252013-03-03 02:25:49 -080066/*static*/ const double IndexMotor::kTransferStartPosition = 0.0;
67/*static*/ const double IndexMotor::kIndexStartPosition = 0.2159;
68/*static*/ const double IndexMotor::kIndexFreeLength =
69 IndexMotor::ConvertDiscAngleToDiscPosition((360 * 2 + 14) * M_PI / 180);
70/*static*/ const double IndexMotor::kLoaderFreeStopPosition =
71 kIndexStartPosition + kIndexFreeLength;
Austin Schuh1b864a12013-03-07 00:46:50 -080072/*static*/ const double IndexMotor::kReadyToPreload =
73 kLoaderFreeStopPosition - ConvertDiscAngleToDiscPosition(M_PI / 6.0);
Austin Schuhf8c52252013-03-03 02:25:49 -080074/*static*/ const double IndexMotor::kReadyToLiftPosition =
75 kLoaderFreeStopPosition + 0.2921;
76/*static*/ const double IndexMotor::kGrabberLength = 0.03175;
77/*static*/ const double IndexMotor::kGrabberStartPosition =
78 kReadyToLiftPosition - kGrabberLength;
Austin Schuh6328daf2013-03-05 00:53:15 -080079/*static*/ const double IndexMotor::kGrabberMovementVelocity = 0.7;
Austin Schuhf8c52252013-03-03 02:25:49 -080080/*static*/ const double IndexMotor::kLifterStopPosition =
81 kReadyToLiftPosition + 0.161925;
82/*static*/ const double IndexMotor::kLifterMovementVelocity = 1.0;
83/*static*/ const double IndexMotor::kEjectorStopPosition =
84 kLifterStopPosition + 0.01;
85/*static*/ const double IndexMotor::kEjectorMovementVelocity = 1.0;
Austin Schuhcc297022013-03-09 23:26:40 -080086/*static*/ const double IndexMotor::kBottomDiscDetectStart = 0.00;
87/*static*/ const double IndexMotor::kBottomDiscDetectStop = 0.13;
88/*static*/ const double IndexMotor::kBottomDiscIndexDelay = 0.032;
Austin Schuhd3d0fbf2013-03-14 00:37:00 -070089/*static*/ const ::aos::time::Time IndexMotor::kTransferOffDelay =
Brian Silvermanb7bcef12013-03-16 13:57:11 -070090 ::aos::time::Time::InSeconds(0.3);
Austin Schuhf8c52252013-03-03 02:25:49 -080091
Austin Schuh7c0e2aa2013-03-09 02:01:16 -080092// TODO(aschuh): Verify these with the sensor actually on.
Austin Schuh825bde92013-03-06 00:16:46 -080093/*static*/ const double IndexMotor::kTopDiscDetectStart =
94 (IndexMotor::kLoaderFreeStopPosition -
Austin Schuh7c0e2aa2013-03-09 02:01:16 -080095 IndexMotor::ConvertDiscAngleToDiscPosition(49 * M_PI / 180));
Austin Schuh825bde92013-03-06 00:16:46 -080096/*static*/ const double IndexMotor::kTopDiscDetectStop =
Austin Schuh7c0e2aa2013-03-09 02:01:16 -080097 (IndexMotor::kLoaderFreeStopPosition +
98 IndexMotor::ConvertDiscAngleToDiscPosition(19 * M_PI / 180));
99
100// I measured the angle between 2 discs. That then gives me the distance
101// between 2 posedges (or negedges). Then subtract off the width of the
102// positive pulse, and that gives the width of the negative pulse.
103/*static*/ const double IndexMotor::kTopDiscDetectMinSeperation =
104 (IndexMotor::ConvertDiscAngleToDiscPosition(120 * M_PI / 180) -
105 (IndexMotor::kTopDiscDetectStop - IndexMotor::kTopDiscDetectStart));
Austin Schuhf8c52252013-03-03 02:25:49 -0800106
Austin Schuhd78ab542013-03-01 22:22:19 -0800107const /*static*/ double IndexMotor::kDiscRadius = 10.875 * 0.0254 / 2;
108const /*static*/ double IndexMotor::kRollerRadius = 2.0 * 0.0254 / 2;
Austin Schuhf8c52252013-03-03 02:25:49 -0800109const /*static*/ double IndexMotor::kTransferRollerRadius = 1.25 * 0.0254 / 2;
Austin Schuhd78ab542013-03-01 22:22:19 -0800110
Austin Schuhf8c52252013-03-03 02:25:49 -0800111/*static*/ const int IndexMotor::kGrabbingDelay = 5;
Austin Schuha3e8e032013-03-10 18:43:14 -0700112/*static*/ const int IndexMotor::kLiftingDelay = 30;
Austin Schuhf8c52252013-03-03 02:25:49 -0800113/*static*/ const int IndexMotor::kShootingDelay = 5;
114/*static*/ const int IndexMotor::kLoweringDelay = 20;
Austin Schuhd78ab542013-03-01 22:22:19 -0800115
Austin Schuh93485832013-03-04 00:01:34 -0800116// TODO(aschuh): Tune these.
117/*static*/ const double
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700118 IndexMotor::IndexStateFeedbackLoop::kMinMotionVoltage = 11.0;
Austin Schuh93485832013-03-04 00:01:34 -0800119/*static*/ const double
Austin Schuha3e8e032013-03-10 18:43:14 -0700120 IndexMotor::IndexStateFeedbackLoop::kNoMotionCuttoffCount = 20;
Austin Schuh93485832013-03-04 00:01:34 -0800121
Austin Schuhd78ab542013-03-01 22:22:19 -0800122/*static*/ double IndexMotor::ConvertDiscAngleToIndex(const double angle) {
123 return (angle * (1 + (kDiscRadius * 2 + kRollerRadius) / kRollerRadius));
124}
125
Austin Schuhf8c52252013-03-03 02:25:49 -0800126/*static*/ double IndexMotor::ConvertDiscAngleToDiscPosition(
127 const double angle) {
Austin Schuhd78ab542013-03-01 22:22:19 -0800128 return angle * (kDiscRadius + kRollerRadius);
129}
130
Austin Schuhf8c52252013-03-03 02:25:49 -0800131/*static*/ double IndexMotor::ConvertDiscPositionToDiscAngle(
132 const double position) {
133 return position / (kDiscRadius + kRollerRadius);
134}
135
Austin Schuhd78ab542013-03-01 22:22:19 -0800136/*static*/ double IndexMotor::ConvertIndexToDiscAngle(const double angle) {
137 return (angle / (1 + (kDiscRadius * 2 + kRollerRadius) / kRollerRadius));
138}
139
140/*static*/ double IndexMotor::ConvertIndexToDiscPosition(const double angle) {
141 return IndexMotor::ConvertDiscAngleToDiscPosition(
142 ConvertIndexToDiscAngle(angle));
143}
144
Austin Schuhf8c52252013-03-03 02:25:49 -0800145/*static*/ double IndexMotor::ConvertTransferToDiscPosition(
146 const double angle) {
147 const double gear_ratio = (1 + (kDiscRadius * 2 + kTransferRollerRadius) /
148 kTransferRollerRadius);
149 return angle / gear_ratio * (kDiscRadius + kTransferRollerRadius);
150}
151
152/*static*/ double IndexMotor::ConvertDiscPositionToIndex(
153 const double position) {
154 return IndexMotor::ConvertDiscAngleToIndex(
155 ConvertDiscPositionToDiscAngle(position));
156}
157
Austin Schuh1b864a12013-03-07 00:46:50 -0800158bool IndexMotor::MinDiscPosition(double *disc_position, Frisbee **found_disc) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800159 bool found_start = false;
160 for (unsigned int i = 0; i < frisbees_.size(); ++i) {
Austin Schuh1b864a12013-03-07 00:46:50 -0800161 Frisbee &frisbee = frisbees_[i];
Austin Schuhf8c52252013-03-03 02:25:49 -0800162 if (!found_start) {
163 if (frisbee.has_position()) {
164 *disc_position = frisbee.position();
Austin Schuh1b864a12013-03-07 00:46:50 -0800165 if (found_disc) {
166 *found_disc = &frisbee;
167 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800168 found_start = true;
169 }
170 } else {
Austin Schuh1b864a12013-03-07 00:46:50 -0800171 if (frisbee.position() <= *disc_position) {
172 *disc_position = frisbee.position();
173 if (found_disc) {
174 *found_disc = &frisbee;
175 }
176 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800177 }
178 }
179 return found_start;
180}
181
Austin Schuh1b864a12013-03-07 00:46:50 -0800182bool IndexMotor::MaxDiscPosition(double *disc_position, Frisbee **found_disc) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800183 bool found_start = false;
184 for (unsigned int i = 0; i < frisbees_.size(); ++i) {
Austin Schuh1b864a12013-03-07 00:46:50 -0800185 Frisbee &frisbee = frisbees_[i];
Austin Schuhf8c52252013-03-03 02:25:49 -0800186 if (!found_start) {
187 if (frisbee.has_position()) {
188 *disc_position = frisbee.position();
Austin Schuh1b864a12013-03-07 00:46:50 -0800189 if (found_disc) {
190 *found_disc = &frisbee;
191 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800192 found_start = true;
193 }
194 } else {
Austin Schuh1b864a12013-03-07 00:46:50 -0800195 if (frisbee.position() > *disc_position) {
196 *disc_position = frisbee.position();
197 if (found_disc) {
198 *found_disc = &frisbee;
199 }
200 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800201 }
202 }
203 return found_start;
204}
205
Austin Schuh93485832013-03-04 00:01:34 -0800206void IndexMotor::IndexStateFeedbackLoop::CapU() {
207 // If the voltage has been low for a large number of cycles, cut the motor
208 // power. This is generally very bad controls practice since this isn't LTI,
209 // but we don't really care about tracking anything other than large step
210 // inputs, and the loader doesn't need to be that accurate.
211 if (::std::abs(U(0, 0)) < kMinMotionVoltage) {
212 ++low_voltage_count_;
213 if (low_voltage_count_ > kNoMotionCuttoffCount) {
Austin Schuh93485832013-03-04 00:01:34 -0800214 U(0, 0) = 0.0;
215 }
216 } else {
217 low_voltage_count_ = 0;
218 }
219
220 for (int i = 0; i < kNumOutputs; ++i) {
Austin Schuh9644e1c2013-03-12 00:40:36 -0700221 if (U(i, 0) > U_max(i, 0)) {
222 U(i, 0) = U_max(i, 0);
223 } else if (U(i, 0) < U_min(i, 0)) {
224 U(i, 0) = U_min(i, 0);
Austin Schuh93485832013-03-04 00:01:34 -0800225 }
226 }
227}
228
229
Austin Schuhd78ab542013-03-01 22:22:19 -0800230// Positive angle is towards the shooter, and positive power is towards the
231// shooter.
232void IndexMotor::RunIteration(
233 const control_loops::IndexLoop::Goal *goal,
234 const control_loops::IndexLoop::Position *position,
235 control_loops::IndexLoop::Output *output,
236 control_loops::IndexLoop::Status *status) {
Austin Schuhd3d0fbf2013-03-14 00:37:00 -0700237 Time now = Time::Now();
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800238 // Make goal easy to work with and sanity check it.
Austin Schuhd78ab542013-03-01 22:22:19 -0800239 Goal goal_enum = static_cast<Goal>(goal->goal_state);
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800240 if (goal->goal_state < 0 || goal->goal_state > 4) {
Brian Silverman94195052013-03-09 13:45:05 -0800241 LOG(ERROR, "Goal state is %"PRId32" which is out of range. Going to HOLD.\n",
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800242 goal->goal_state);
243 goal_enum = Goal::HOLD;
244 }
Austin Schuhd78ab542013-03-01 22:22:19 -0800245
246 // Disable the motors now so that all early returns will return with the
247 // motors disabled.
Austin Schuhb6d898b2013-03-03 15:34:35 -0800248 double intake_voltage = 0.0;
Austin Schuhf8c52252013-03-03 02:25:49 -0800249 double transfer_voltage = 0.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800250 if (output) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800251 output->intake_voltage = 0.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800252 output->transfer_voltage = 0.0;
253 output->index_voltage = 0.0;
254 }
255
256 status->ready_to_intake = false;
257
Austin Schuhe3490622013-03-13 01:24:30 -0700258 // Set the controller to use to be the one designed for the current number of
259 // discs in the hopper. This is safe since the controller prevents the index
260 // from being set out of bounds and picks the closest controller.
261 wrist_loop_->set_controller_index(frisbees_.size());
262
Austin Schuhf8c52252013-03-03 02:25:49 -0800263 // Compute a safe index position that we can use.
Austin Schuhd78ab542013-03-01 22:22:19 -0800264 if (position) {
265 wrist_loop_->Y << position->index_position;
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800266 // Set the goal to be the current position if this is the first time through
267 // so we don't always spin the indexer to the 0 position before starting.
268 if (no_prior_position_) {
Brian Silverman43bb73e2013-03-17 13:39:47 -0700269 LOG(INFO, "no prior position; resetting\n");
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800270 wrist_loop_->R << wrist_loop_->Y(0, 0), 0.0;
Austin Schuhc5ef1bb2013-03-10 00:42:05 -0800271 wrist_loop_->X_hat(0, 0) = wrist_loop_->Y(0, 0);
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800272 no_prior_position_ = false;
Austin Schuh6328daf2013-03-05 00:53:15 -0800273 last_bottom_disc_posedge_count_ = position->bottom_disc_posedge_count;
274 last_bottom_disc_negedge_count_ = position->bottom_disc_negedge_count;
275 last_bottom_disc_negedge_wait_count_ =
276 position->bottom_disc_negedge_wait_count;
Austin Schuh825bde92013-03-06 00:16:46 -0800277 last_top_disc_posedge_count_ = position->top_disc_posedge_count;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800278 last_top_disc_negedge_count_ = position->top_disc_negedge_count;
279 // The open positions for the upper is right here and isn't a hard edge.
Austin Schuh723770b2013-03-10 13:26:20 -0700280 upper_open_region_.Restart(wrist_loop_->Y(0, 0));
281 lower_open_region_.Restart(wrist_loop_->Y(0, 0));
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800282 }
283
Austin Schuh1b864a12013-03-07 00:46:50 -0800284 // If the cRIO is gone for over 1/2 of a second, assume that it rebooted.
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800285 if (missing_position_count_ > 50) {
Brian Silverman43bb73e2013-03-17 13:39:47 -0700286 LOG(INFO, "assuming cRIO rebooted\n");
Austin Schuh6328daf2013-03-05 00:53:15 -0800287 last_bottom_disc_posedge_count_ = position->bottom_disc_posedge_count;
288 last_bottom_disc_negedge_count_ = position->bottom_disc_negedge_count;
289 last_bottom_disc_negedge_wait_count_ =
290 position->bottom_disc_negedge_wait_count;
Austin Schuh825bde92013-03-06 00:16:46 -0800291 last_top_disc_posedge_count_ = position->top_disc_posedge_count;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800292 last_top_disc_negedge_count_ = position->top_disc_negedge_count;
293 // We can't really trust the open range any more if the crio rebooted.
Austin Schuh723770b2013-03-10 13:26:20 -0700294 upper_open_region_.Restart(wrist_loop_->Y(0, 0));
295 lower_open_region_.Restart(wrist_loop_->Y(0, 0));
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800296 // Adjust the disc positions so that they don't have to move.
297 const double disc_offset =
298 position->index_position - wrist_loop_->X_hat(0, 0);
299 for (auto frisbee = frisbees_.begin();
300 frisbee != frisbees_.end(); ++frisbee) {
301 frisbee->OffsetDisc(disc_offset);
302 }
Austin Schuhc5ef1bb2013-03-10 00:42:05 -0800303 wrist_loop_->X_hat(0, 0) = wrist_loop_->Y(0, 0);
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800304 }
305 missing_position_count_ = 0;
306 } else {
307 ++missing_position_count_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800308 }
309 const double index_position = wrist_loop_->X_hat(0, 0);
310
Austin Schuh825bde92013-03-06 00:16:46 -0800311 if (position) {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800312 // Reset the open region if we saw a negedge.
Austin Schuh723770b2013-03-10 13:26:20 -0700313 if (position->bottom_disc_negedge_wait_count !=
314 last_bottom_disc_negedge_wait_count_) {
315 // Saw a negedge, must be a new region.
316 lower_open_region_.Restart(position->bottom_disc_negedge_wait_position);
317 }
318 // Reset the open region if we saw a negedge.
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800319 if (position->top_disc_negedge_count != last_top_disc_negedge_count_) {
320 // Saw a negedge, must be a new region.
Austin Schuh723770b2013-03-10 13:26:20 -0700321 upper_open_region_.Restart(position->top_disc_negedge_position);
322 }
323
324 // No disc. Expand the open region.
325 if (!position->bottom_disc_detect) {
326 lower_open_region_.Expand(index_position);
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800327 }
328
329 // No disc. Expand the open region.
330 if (!position->top_disc_detect) {
Austin Schuh723770b2013-03-10 13:26:20 -0700331 upper_open_region_.Expand(index_position);
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800332 }
333
Austin Schuh825bde92013-03-06 00:16:46 -0800334 if (!position->top_disc_detect) {
335 // We don't see a disc. Verify that there are no discs that we should be
336 // seeing.
Austin Schuh1b864a12013-03-07 00:46:50 -0800337 // Assume that discs will move slow enough that we won't miss one as it
338 // goes by. They will either pile up above or below the sensor.
Austin Schuhdff24e22013-03-06 00:41:21 -0800339
340 double cumulative_offset = 0.0;
341 for (auto frisbee = frisbees_.rbegin(), rend = frisbees_.rend();
342 frisbee != rend; ++frisbee) {
343 frisbee->OffsetDisc(cumulative_offset);
344 double amount_moved = frisbee->ObserveNoTopDiscSensor(
Austin Schuh6b1ad2f2013-03-11 23:23:00 -0700345 wrist_loop_->X_hat(0, 0));
Austin Schuhdff24e22013-03-06 00:41:21 -0800346 cumulative_offset += amount_moved;
Austin Schuh825bde92013-03-06 00:16:46 -0800347 }
348 }
Austin Schuh1b864a12013-03-07 00:46:50 -0800349
Austin Schuh825bde92013-03-06 00:16:46 -0800350 if (position->top_disc_posedge_count != last_top_disc_posedge_count_) {
Austin Schuh1b864a12013-03-07 00:46:50 -0800351 const double index_position = wrist_loop_->X_hat(0, 0) -
352 position->index_position + position->top_disc_posedge_position;
Austin Schuh825bde92013-03-06 00:16:46 -0800353 // TODO(aschuh): Sanity check this number...
354 // Requires storing when the disc was last seen with the sensor off, and
355 // figuring out what to do if things go south.
356
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800357 // 1 if discs are going up, 0 if we have no clue, and -1 if they are going
358 // down.
359 int disc_direction = 0;
Austin Schuh825bde92013-03-06 00:16:46 -0800360 if (wrist_loop_->X_hat(1, 0) > 50.0) {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800361 disc_direction = 1;
362 } else if (wrist_loop_->X_hat(1, 0) < -50.0) {
363 disc_direction = -1;
364 } else {
365 // Save the upper and lower positions that we last saw a disc at.
366 // If there is a big buffer above, must be a disc from below.
367 // If there is a big buffer below, must be a disc from above.
368 // This should work to replace the velocity threshold above.
369
Austin Schuh723770b2013-03-10 13:26:20 -0700370 const double open_width = upper_open_region_.width();
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800371 const double relative_upper_open_precentage =
Austin Schuh723770b2013-03-10 13:26:20 -0700372 (upper_open_region_.upper_bound() - index_position) / open_width;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800373 const double relative_lower_open_precentage =
Austin Schuh723770b2013-03-10 13:26:20 -0700374 (index_position - upper_open_region_.lower_bound()) / open_width;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800375
376 if (ConvertIndexToDiscPosition(open_width) <
377 kTopDiscDetectMinSeperation * 0.9) {
378 LOG(ERROR, "Discs are way too close to each other. Doing nothing\n");
379 } else if (relative_upper_open_precentage > 0.75) {
380 // Looks like it is a disc going down from above since we are near
381 // the upper edge.
382 disc_direction = -1;
Austin Schuha3e8e032013-03-10 18:43:14 -0700383 LOG(INFO, "Disc edge going down\n");
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800384 } else if (relative_lower_open_precentage > 0.75) {
385 // Looks like it is a disc going up from below since we are near
386 // the lower edge.
387 disc_direction = 1;
Austin Schuha3e8e032013-03-10 18:43:14 -0700388 LOG(INFO, "Disc edge going up\n");
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800389 } else {
390 LOG(ERROR,
391 "Got an edge in the middle of what should be an open region.\n");
392 LOG(ERROR, "Open width: %f upper precentage %f %%\n",
393 open_width, relative_upper_open_precentage);
394 }
395 }
396
397 if (disc_direction > 0) {
Austin Schuh825bde92013-03-06 00:16:46 -0800398 // Moving up at a reasonable clip.
Austin Schuh1b864a12013-03-07 00:46:50 -0800399 // Find the highest disc that is below the top disc sensor.
400 // While we are at it, count the number above and log an error if there
401 // are too many.
402 if (frisbees_.size() == 0) {
403 Frisbee new_frisbee;
404 new_frisbee.has_been_indexed_ = true;
405 new_frisbee.index_start_position_ = index_position -
406 ConvertDiscPositionToIndex(kTopDiscDetectStart -
407 kIndexStartPosition);
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800408 ++hopper_disc_count_;
409 ++total_disc_count_;
Austin Schuh1b864a12013-03-07 00:46:50 -0800410 frisbees_.push_front(new_frisbee);
411 LOG(WARNING, "Added a disc to the hopper at the top sensor\n");
412 }
413
414 int above_disc_count = 0;
415 double highest_position = 0;
416 Frisbee *highest_frisbee_below_sensor = NULL;
417 for (auto frisbee = frisbees_.rbegin(), rend = frisbees_.rend();
418 frisbee != rend; ++frisbee) {
419 const double disc_position = frisbee->absolute_position(
420 index_position);
421 // It is save to use the top position for the cuttoff, since the
422 // sensor being low will result in discs being pushed off of it.
423 if (disc_position >= kTopDiscDetectStop) {
424 ++above_disc_count;
425 } else if (!highest_frisbee_below_sensor ||
426 disc_position > highest_position) {
427 highest_frisbee_below_sensor = &*frisbee;
428 highest_position = disc_position;
429 }
430 }
431 if (above_disc_count > 1) {
432 LOG(ERROR, "We have 2 discs above the top sensor.\n");
433 }
434
435 // We now have the disc. Shift all the ones below the sensor up by the
436 // computed delta.
437 const double disc_delta = IndexMotor::ConvertDiscPositionToIndex(
438 highest_position - kTopDiscDetectStart);
439 for (auto frisbee = frisbees_.rbegin(), rend = frisbees_.rend();
440 frisbee != rend; ++frisbee) {
441 const double disc_position = frisbee->absolute_position(
442 index_position);
443 if (disc_position < kTopDiscDetectStop) {
444 frisbee->OffsetDisc(disc_delta);
445 }
446 }
Brian Silverman43bb73e2013-03-17 13:39:47 -0700447 if (highest_frisbee_below_sensor) {
448 LOG(INFO, "Currently have %d discs, saw posedge moving up. "
449 "Moving down by %f to %f\n", frisbees_.size(),
450 ConvertIndexToDiscPosition(disc_delta),
451 highest_frisbee_below_sensor->absolute_position(
452 wrist_loop_->X_hat(0, 0)));
453 } else {
454 LOG(INFO, "Currently have %d discs, saw posedge moving up. "
455 "Moving down by %f\n", frisbees_.size(),
456 ConvertIndexToDiscPosition(disc_delta));
457 }
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800458 } else if (disc_direction < 0) {
Austin Schuh825bde92013-03-06 00:16:46 -0800459 // Moving down at a reasonable clip.
Austin Schuh1b864a12013-03-07 00:46:50 -0800460 // There can only be 1 disc up top that would give us a posedge.
461 // Find it and place it at the one spot that it can be.
Brian Silverman94195052013-03-09 13:45:05 -0800462 double min_disc_position = 0;
Austin Schuh1b864a12013-03-07 00:46:50 -0800463 Frisbee *min_frisbee = NULL;
464 MinDiscPosition(&min_disc_position, &min_frisbee);
465 if (!min_frisbee) {
466 // Uh, oh, we see a disc but there isn't one...
467 LOG(ERROR, "Saw a disc up top but there isn't one in the hopper\n");
468 } else {
469 const double disc_position = min_frisbee->absolute_position(
470 index_position);
471
472 const double disc_delta_meters = disc_position - kTopDiscDetectStop;
473 const double disc_delta = IndexMotor::ConvertDiscPositionToIndex(
474 disc_delta_meters);
Austin Schuha3e8e032013-03-10 18:43:14 -0700475 LOG(INFO, "Posedge going down. Moving top disc down by %f\n",
476 disc_delta_meters);
Austin Schuh1b864a12013-03-07 00:46:50 -0800477 for (auto frisbee = frisbees_.begin(), end = frisbees_.end();
478 frisbee != end; ++frisbee) {
479 frisbee->OffsetDisc(disc_delta);
480 }
481 }
Austin Schuh825bde92013-03-06 00:16:46 -0800482 } else {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800483 LOG(ERROR, "Not sure how to handle the upper posedge, doing nothing\n");
Austin Schuh825bde92013-03-06 00:16:46 -0800484 }
485 }
486 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800487
Austin Schuhf8c52252013-03-03 02:25:49 -0800488 // Bool to track if it is safe for the goal to change yet.
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700489 bool safe_to_change_state = true;
Austin Schuhd78ab542013-03-01 22:22:19 -0800490 switch (safe_goal_) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800491 case Goal::HOLD:
Austin Schuhd78ab542013-03-01 22:22:19 -0800492 // The goal should already be good, so sit tight with everything the same
493 // as it was.
Austin Schuhd78ab542013-03-01 22:22:19 -0800494 break;
Austin Schuhf8c52252013-03-03 02:25:49 -0800495 case Goal::READY_LOWER:
496 case Goal::INTAKE:
Austin Schuhd78ab542013-03-01 22:22:19 -0800497 {
Austin Schuhd78ab542013-03-01 22:22:19 -0800498 if (position) {
Austin Schuh6328daf2013-03-05 00:53:15 -0800499 // Posedge of the disc entering the beam break.
500 if (position->bottom_disc_posedge_count !=
501 last_bottom_disc_posedge_count_) {
Austin Schuhd78ab542013-03-01 22:22:19 -0800502 transfer_frisbee_.Reset();
503 transfer_frisbee_.bottom_posedge_time_ = now;
Austin Schuha3e8e032013-03-10 18:43:14 -0700504 LOG(INFO, "Posedge of bottom disc %f\n",
505 transfer_frisbee_.bottom_posedge_time_.ToSeconds());
Austin Schuhd78ab542013-03-01 22:22:19 -0800506 ++hopper_disc_count_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800507 ++total_disc_count_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800508 }
509
510 // Disc exited the beam break now.
Austin Schuh6328daf2013-03-05 00:53:15 -0800511 if (position->bottom_disc_negedge_count !=
512 last_bottom_disc_negedge_count_) {
Austin Schuhd78ab542013-03-01 22:22:19 -0800513 transfer_frisbee_.bottom_negedge_time_ = now;
Austin Schuha3e8e032013-03-10 18:43:14 -0700514 LOG(INFO, "Negedge of bottom disc %f\n",
515 transfer_frisbee_.bottom_negedge_time_.ToSeconds());
Austin Schuhd78ab542013-03-01 22:22:19 -0800516 frisbees_.push_front(transfer_frisbee_);
517 }
518
519 if (position->bottom_disc_detect) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800520 intake_voltage = transfer_voltage = 12.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800521 // Must wait until the disc gets out before we can change state.
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700522 safe_to_change_state = false;
Austin Schuhd78ab542013-03-01 22:22:19 -0800523
Austin Schuhf8c52252013-03-03 02:25:49 -0800524 // TODO(aschuh): A disc on the way through needs to start moving
525 // the indexer if it isn't already moving. Maybe?
Austin Schuhd78ab542013-03-01 22:22:19 -0800526
527 Time elapsed_posedge_time = now -
528 transfer_frisbee_.bottom_posedge_time_;
529 if (elapsed_posedge_time >= Time::InSeconds(0.3)) {
530 // It has been too long. The disc must be jammed.
531 LOG(ERROR, "Been way too long. Jammed disc?\n");
Austin Schuhd78ab542013-03-01 22:22:19 -0800532 }
533 }
534
Austin Schuhf8c52252013-03-03 02:25:49 -0800535 // Check all non-indexed discs and see if they should be indexed.
Austin Schuhb6d898b2013-03-03 15:34:35 -0800536 for (auto frisbee = frisbees_.begin();
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800537 frisbee != frisbees_.end(); ++frisbee) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800538 if (!frisbee->has_been_indexed_) {
539 intake_voltage = transfer_voltage = 12.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800540
Austin Schuh6328daf2013-03-05 00:53:15 -0800541 if (last_bottom_disc_negedge_wait_count_ !=
542 position->bottom_disc_negedge_wait_count) {
543 // We have an index difference.
544 // Save the indexer position, and the time.
545 if (last_bottom_disc_negedge_wait_count_ + 1 !=
546 position->bottom_disc_negedge_wait_count) {
547 LOG(ERROR, "Funny, we got 2 edges since we last checked.\n");
548 }
549
550 // Save the captured position as the position at which the disc
551 // touched the indexer.
Austin Schuhd78ab542013-03-01 22:22:19 -0800552 LOG(INFO, "Grabbed on the index now at %f\n", index_position);
Austin Schuhb6d898b2013-03-03 15:34:35 -0800553 frisbee->has_been_indexed_ = true;
Austin Schuh6328daf2013-03-05 00:53:15 -0800554 frisbee->index_start_position_ =
555 position->bottom_disc_negedge_wait_position;
Austin Schuhd78ab542013-03-01 22:22:19 -0800556 }
557 }
Austin Schuhb6d898b2013-03-03 15:34:35 -0800558 if (!frisbee->has_been_indexed_) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800559 // All discs must be indexed before it is safe to stop indexing.
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700560 safe_to_change_state = false;
Austin Schuhd78ab542013-03-01 22:22:19 -0800561 }
562 }
563
Austin Schuhf8c52252013-03-03 02:25:49 -0800564 // Figure out where the indexer should be to move the discs down to
565 // the right position.
Brian Silverman94195052013-03-09 13:45:05 -0800566 double max_disc_position = 0;
Austin Schuh1b864a12013-03-07 00:46:50 -0800567 if (MaxDiscPosition(&max_disc_position, NULL)) {
Austin Schuha3e8e032013-03-10 18:43:14 -0700568 LOG(DEBUG, "There is a disc down here!\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800569 // TODO(aschuh): Figure out what to do if grabbing the next one
570 // would cause things to jam into the loader.
571 // Say we aren't ready any more. Undefined behavior will result if
572 // that isn't observed.
573 double bottom_disc_position =
574 max_disc_position + ConvertDiscAngleToIndex(M_PI);
575 wrist_loop_->R << bottom_disc_position, 0.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800576
Austin Schuhf8c52252013-03-03 02:25:49 -0800577 // Verify that we are close enough to the goal so that we should be
578 // fine accepting the next disc.
579 double disc_error_meters = ConvertIndexToDiscPosition(
580 wrist_loop_->X_hat(0, 0) - bottom_disc_position);
581 // We are ready for the next disc if the first one is in the first
582 // half circle of the indexer. It will take time for the disc to
583 // come into the indexer, so we will be able to move it out of the
584 // way in time.
585 // This choice also makes sure that we don't claim that we aren't
586 // ready between full speed intaking.
587 if (-ConvertDiscAngleToIndex(M_PI) < disc_error_meters &&
588 disc_error_meters < 0.04) {
589 // We are only ready if we aren't being asked to change state or
590 // are full.
591 status->ready_to_intake =
592 (safe_goal_ == goal_enum) && hopper_disc_count_ < 4;
593 } else {
594 status->ready_to_intake = false;
Austin Schuhd78ab542013-03-01 22:22:19 -0800595 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800596 } else {
597 // No discs! We are always ready for more if we aren't being
598 // asked to change state.
599 status->ready_to_intake = (safe_goal_ == goal_enum);
Austin Schuhd78ab542013-03-01 22:22:19 -0800600 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800601
602 // Turn on the transfer roller if we are ready.
603 if (status->ready_to_intake && hopper_disc_count_ < 4 &&
604 safe_goal_ == Goal::INTAKE) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800605 intake_voltage = transfer_voltage = 12.0;
Austin Schuhf8c52252013-03-03 02:25:49 -0800606 }
Austin Schuhd78ab542013-03-01 22:22:19 -0800607 }
Austin Schuha3e8e032013-03-10 18:43:14 -0700608 LOG(DEBUG, "INTAKE\n");
Austin Schuhd78ab542013-03-01 22:22:19 -0800609 }
610 break;
Austin Schuhf8c52252013-03-03 02:25:49 -0800611 case Goal::READY_SHOOTER:
612 case Goal::SHOOT:
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700613 // Don't let us leave the shoot or preload state if there are 4 discs in
614 // the hopper.
615 if (hopper_disc_count_ >= 4 && goal_enum != Goal::SHOOT) {
616 safe_to_change_state = false;
617 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800618 // Check if we have any discs to shoot or load and handle them.
Brian Silverman94195052013-03-09 13:45:05 -0800619 double min_disc_position = 0;
Austin Schuh1b864a12013-03-07 00:46:50 -0800620 if (MinDiscPosition(&min_disc_position, NULL)) {
621 const double ready_disc_position = min_disc_position +
622 ConvertDiscPositionToIndex(kReadyToPreload - kIndexStartPosition);
Austin Schuhf8c52252013-03-03 02:25:49 -0800623
624 const double grabbed_disc_position =
625 min_disc_position +
626 ConvertDiscPositionToIndex(kReadyToLiftPosition -
Brian Silvermanb7bcef12013-03-16 13:57:11 -0700627 kIndexStartPosition + 0.10);
Austin Schuhf8c52252013-03-03 02:25:49 -0800628
629 // Check the state of the loader FSM.
630 // If it is ready to load discs, position the disc so that it is ready
631 // to be grabbed.
632 // If it isn't ready, there is a disc in there. It needs to finish it's
633 // cycle first.
634 if (loader_state_ != LoaderState::READY) {
635 // We already have a disc in the loader.
636 // Stage the discs back a bit.
637 wrist_loop_->R << ready_disc_position, 0.0;
638
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800639 // Shoot if we are grabbed and being asked to shoot.
640 if (loader_state_ == LoaderState::GRABBED &&
641 safe_goal_ == Goal::SHOOT) {
642 loader_goal_ = LoaderGoal::SHOOT_AND_RESET;
643 }
644
Austin Schuhf8c52252013-03-03 02:25:49 -0800645 // Must wait until it has been grabbed to continue.
646 if (loader_state_ == LoaderState::GRABBING) {
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700647 safe_to_change_state = false;
Austin Schuhf8c52252013-03-03 02:25:49 -0800648 }
649 } else {
650 // No disc up top right now.
651 wrist_loop_->R << grabbed_disc_position, 0.0;
652
653 // See if the disc has gotten pretty far up yet.
654 if (wrist_loop_->X_hat(0, 0) > ready_disc_position) {
655 // Point of no return. We are committing to grabbing it now.
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700656 safe_to_change_state = false;
Austin Schuhf8c52252013-03-03 02:25:49 -0800657 const double robust_grabbed_disc_position =
658 (grabbed_disc_position -
659 ConvertDiscPositionToIndex(kGrabberLength));
660
661 // If close, start grabbing and/or shooting.
662 if (wrist_loop_->X_hat(0, 0) > robust_grabbed_disc_position) {
663 // Start the state machine.
664 if (safe_goal_ == Goal::SHOOT) {
665 loader_goal_ = LoaderGoal::SHOOT_AND_RESET;
666 } else {
667 loader_goal_ = LoaderGoal::GRAB;
668 }
669 // This frisbee is now gone. Take it out of the queue.
670 frisbees_.pop_back();
Austin Schuhf8c52252013-03-03 02:25:49 -0800671 }
672 }
673 }
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800674 } else {
675 if (loader_state_ != LoaderState::READY) {
676 // Shoot if we are grabbed and being asked to shoot.
677 if (loader_state_ == LoaderState::GRABBED &&
678 safe_goal_ == Goal::SHOOT) {
679 loader_goal_ = LoaderGoal::SHOOT_AND_RESET;
680 }
681 } else {
682 // Ok, no discs in sight. Spin the hopper up by 150% of it's full
683 // range and verify that we don't see anything.
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800684 const double hopper_clear_verification_position =
Austin Schuha3e8e032013-03-10 18:43:14 -0700685 ::std::max(upper_open_region_.lower_bound(),
Austin Schuh723770b2013-03-10 13:26:20 -0700686 lower_open_region_.lower_bound()) +
Austin Schuhf60861b2013-03-14 00:14:10 -0700687 ConvertDiscPositionToIndex(kIndexFreeLength) * 1.5;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800688
689 wrist_loop_->R << hopper_clear_verification_position, 0.0;
690 if (::std::abs(wrist_loop_->X_hat(0, 0) -
691 hopper_clear_verification_position) <
692 ConvertDiscPositionToIndex(0.05)) {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800693 // We are at the end of the range. There are no more discs here.
694 while (frisbees_.size() > 0) {
695 LOG(ERROR, "Dropping an extra disc since it can't exist\n");
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700696 LOG(ERROR, "Upper is [%f %f]\n",
697 upper_open_region_.upper_bound(),
698 upper_open_region_.lower_bound());
699 LOG(ERROR, "Lower is [%f %f]\n",
700 lower_open_region_.upper_bound(),
701 lower_open_region_.lower_bound());
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800702 frisbees_.pop_back();
703 --hopper_disc_count_;
704 --total_disc_count_;
705 }
706 if (hopper_disc_count_ != 0) {
707 LOG(ERROR,
708 "Emptied the hopper out but there are still discs there\n");
709 }
710 }
711 }
712 }
713
714 {
715 const double hopper_clear_verification_position =
Austin Schuha3e8e032013-03-10 18:43:14 -0700716 ::std::max(upper_open_region_.lower_bound(),
Austin Schuh723770b2013-03-10 13:26:20 -0700717 lower_open_region_.lower_bound()) +
Austin Schuhf60861b2013-03-14 00:14:10 -0700718 ConvertDiscPositionToIndex(kIndexFreeLength) * 1.5;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800719
720 if (wrist_loop_->X_hat(0, 0) >
721 hopper_clear_verification_position +
722 ConvertDiscPositionToIndex(0.05)) {
723 // We are at the end of the range. There are no more discs here.
724 while (frisbees_.size() > 0) {
725 LOG(ERROR, "Dropping an extra disc since it can't exist\n");
Austin Schuha3e8e032013-03-10 18:43:14 -0700726 LOG(ERROR, "Upper is [%f %f]\n",
727 upper_open_region_.upper_bound(),
728 upper_open_region_.lower_bound());
729 LOG(ERROR, "Lower is [%f %f]\n",
730 lower_open_region_.upper_bound(),
731 lower_open_region_.lower_bound());
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800732 frisbees_.pop_back();
733 --hopper_disc_count_;
734 --total_disc_count_;
735 }
Austin Schuhd3d0fbf2013-03-14 00:37:00 -0700736 if (hopper_disc_count_ > 0) {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800737 LOG(ERROR,
Brian Silvermanb7bcef12013-03-16 13:57:11 -0700738 "Emptied the hopper out but there are still %"PRId32" discs there\n",
Austin Schuhf60861b2013-03-14 00:14:10 -0700739 hopper_disc_count_);
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800740 }
741 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800742 }
743
Austin Schuha3e8e032013-03-10 18:43:14 -0700744 LOG(DEBUG, "READY_SHOOTER or SHOOT\n");
Austin Schuhd78ab542013-03-01 22:22:19 -0800745 break;
Austin Schuhf8c52252013-03-03 02:25:49 -0800746 }
747
Austin Schuhd3d0fbf2013-03-14 00:37:00 -0700748 // Wait for a period of time to make sure that the disc gets sucked
749 // in properly. We need to do this regardless of what the indexer is doing.
750 for (auto frisbee = frisbees_.begin();
751 frisbee != frisbees_.end(); ++frisbee) {
752 if (now - frisbee->bottom_negedge_time_ < kTransferOffDelay) {
753 transfer_voltage = 12.0;
754 }
755 }
756
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700757 // If we have 4 discs, it is time to preload.
758 if (safe_to_change_state && hopper_disc_count_ >= 4) {
759 switch (safe_goal_) {
760 case Goal::HOLD:
761 case Goal::READY_LOWER:
762 case Goal::INTAKE:
763 safe_goal_ = Goal::READY_SHOOTER;
764 safe_to_change_state = false;
Brian Silvermana0238ce2013-03-13 17:58:45 -0700765 LOG(INFO, "We have %"PRId32" discs, time to preload automatically\n",
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700766 hopper_disc_count_);
767 break;
768 case Goal::READY_SHOOTER:
769 case Goal::SHOOT:
770 break;
771 }
772 }
773
Austin Schuhf8c52252013-03-03 02:25:49 -0800774 // The only way out of the loader is to shoot the disc. The FSM can only go
775 // forwards.
776 switch (loader_state_) {
777 case LoaderState::READY:
Austin Schuha3e8e032013-03-10 18:43:14 -0700778 LOG(DEBUG, "Loader READY\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800779 // Open and down, ready to accept a disc.
780 loader_up_ = false;
781 disc_clamped_ = false;
782 disc_ejected_ = false;
783 if (loader_goal_ == LoaderGoal::GRAB ||
784 loader_goal_ == LoaderGoal::SHOOT_AND_RESET) {
785 if (loader_goal_ == LoaderGoal::GRAB) {
Austin Schuha3e8e032013-03-10 18:43:14 -0700786 LOG(INFO, "Told to GRAB, moving on\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800787 } else {
Austin Schuha3e8e032013-03-10 18:43:14 -0700788 LOG(INFO, "Told to SHOOT_AND_RESET, moving on\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800789 }
790 loader_state_ = LoaderState::GRABBING;
791 loader_countdown_ = kGrabbingDelay;
792 } else {
793 break;
794 }
795 case LoaderState::GRABBING:
Austin Schuha3e8e032013-03-10 18:43:14 -0700796 LOG(DEBUG, "Loader GRABBING %d\n", loader_countdown_);
Austin Schuhf8c52252013-03-03 02:25:49 -0800797 // Closing the grabber.
798 loader_up_ = false;
799 disc_clamped_ = true;
800 disc_ejected_ = false;
801 if (loader_countdown_ > 0) {
802 --loader_countdown_;
803 break;
804 } else {
805 loader_state_ = LoaderState::GRABBED;
806 }
807 case LoaderState::GRABBED:
Austin Schuha3e8e032013-03-10 18:43:14 -0700808 LOG(DEBUG, "Loader GRABBED\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800809 // Grabber closed.
810 loader_up_ = false;
811 disc_clamped_ = true;
812 disc_ejected_ = false;
813 if (loader_goal_ == LoaderGoal::SHOOT_AND_RESET) {
Austin Schuha3e8e032013-03-10 18:43:14 -0700814 shooter.status.FetchLatest();
815 if (shooter.status.get()) {
816 // TODO(aschuh): If we aren't shooting nicely, wait until the shooter
817 // is up to speed rather than just spinning.
Austin Schuhf60861b2013-03-14 00:14:10 -0700818 if (shooter.status->average_velocity > 130 && shooter.status->ready) {
Austin Schuha3e8e032013-03-10 18:43:14 -0700819 loader_state_ = LoaderState::LIFTING;
820 loader_countdown_ = kLiftingDelay;
821 LOG(INFO, "Told to SHOOT_AND_RESET, moving on\n");
822 } else {
823 LOG(WARNING, "Told to SHOOT_AND_RESET, shooter too slow at %f\n",
824 shooter.status->average_velocity);
825 break;
826 }
827 } else {
828 LOG(ERROR, "Told to SHOOT_AND_RESET, no shooter data, moving on.\n");
829 loader_state_ = LoaderState::LIFTING;
830 loader_countdown_ = kLiftingDelay;
831 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800832 } else if (loader_goal_ == LoaderGoal::READY) {
833 LOG(ERROR, "Can't go to ready when we have something grabbed.\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800834 break;
835 } else {
836 break;
837 }
838 case LoaderState::LIFTING:
Austin Schuha3e8e032013-03-10 18:43:14 -0700839 LOG(DEBUG, "Loader LIFTING %d\n", loader_countdown_);
Austin Schuhf8c52252013-03-03 02:25:49 -0800840 // Lifting the disc.
841 loader_up_ = true;
842 disc_clamped_ = true;
843 disc_ejected_ = false;
844 if (loader_countdown_ > 0) {
845 --loader_countdown_;
846 break;
847 } else {
848 loader_state_ = LoaderState::LIFTED;
849 }
850 case LoaderState::LIFTED:
Austin Schuha3e8e032013-03-10 18:43:14 -0700851 LOG(DEBUG, "Loader LIFTED\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800852 // Disc lifted. Time to eject it out.
853 loader_up_ = true;
854 disc_clamped_ = true;
855 disc_ejected_ = false;
856 loader_state_ = LoaderState::SHOOTING;
857 loader_countdown_ = kShootingDelay;
858 case LoaderState::SHOOTING:
Austin Schuha3e8e032013-03-10 18:43:14 -0700859 LOG(DEBUG, "Loader SHOOTING %d\n", loader_countdown_);
Austin Schuhf8c52252013-03-03 02:25:49 -0800860 // Ejecting the disc into the shooter.
861 loader_up_ = true;
862 disc_clamped_ = false;
863 disc_ejected_ = true;
864 if (loader_countdown_ > 0) {
865 --loader_countdown_;
866 break;
867 } else {
868 loader_state_ = LoaderState::SHOOT;
869 }
870 case LoaderState::SHOOT:
Austin Schuha3e8e032013-03-10 18:43:14 -0700871 LOG(DEBUG, "Loader SHOOT\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800872 // The disc has been shot.
873 loader_up_ = true;
874 disc_clamped_ = false;
875 disc_ejected_ = true;
876 loader_state_ = LoaderState::LOWERING;
877 loader_countdown_ = kLoweringDelay;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800878 --hopper_disc_count_;
Austin Schuh70be1ba2013-03-10 13:37:17 -0700879 ++shot_disc_count_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800880 case LoaderState::LOWERING:
Austin Schuha3e8e032013-03-10 18:43:14 -0700881 LOG(DEBUG, "Loader LOWERING %d\n", loader_countdown_);
Austin Schuhf8c52252013-03-03 02:25:49 -0800882 // Lowering the loader back down.
883 loader_up_ = false;
884 disc_clamped_ = false;
885 disc_ejected_ = true;
886 if (loader_countdown_ > 0) {
887 --loader_countdown_;
888 break;
889 } else {
890 loader_state_ = LoaderState::LOWERED;
891 }
892 case LoaderState::LOWERED:
Austin Schuha3e8e032013-03-10 18:43:14 -0700893 LOG(DEBUG, "Loader LOWERED\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800894 // The indexer is lowered.
895 loader_up_ = false;
896 disc_clamped_ = false;
897 disc_ejected_ = false;
898 loader_state_ = LoaderState::READY;
899 // Once we have shot, we need to hang out in READY until otherwise
900 // notified.
901 loader_goal_ = LoaderGoal::READY;
Austin Schuhd78ab542013-03-01 22:22:19 -0800902 break;
903 }
904
905 // Update the observer.
906 wrist_loop_->Update(position != NULL, output == NULL);
907
908 if (position) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800909 LOG(DEBUG, "pos=%f\n", position->index_position);
Austin Schuhd78ab542013-03-01 22:22:19 -0800910 last_bottom_disc_detect_ = position->bottom_disc_detect;
Austin Schuh825bde92013-03-06 00:16:46 -0800911 last_top_disc_detect_ = position->top_disc_detect;
Austin Schuh6328daf2013-03-05 00:53:15 -0800912 last_bottom_disc_posedge_count_ = position->bottom_disc_posedge_count;
913 last_bottom_disc_negedge_count_ = position->bottom_disc_negedge_count;
914 last_bottom_disc_negedge_wait_count_ =
915 position->bottom_disc_negedge_wait_count;
Austin Schuh825bde92013-03-06 00:16:46 -0800916 last_top_disc_posedge_count_ = position->top_disc_posedge_count;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800917 last_top_disc_negedge_count_ = position->top_disc_negedge_count;
Austin Schuhd78ab542013-03-01 22:22:19 -0800918 }
919
920 status->hopper_disc_count = hopper_disc_count_;
921 status->total_disc_count = total_disc_count_;
Austin Schuh70be1ba2013-03-10 13:37:17 -0700922 status->shot_disc_count = shot_disc_count_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800923 status->preloaded = (loader_state_ != LoaderState::READY);
Austin Schuhd78ab542013-03-01 22:22:19 -0800924
925 if (output) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800926 output->intake_voltage = intake_voltage;
Austin Schuhf8c52252013-03-03 02:25:49 -0800927 output->transfer_voltage = transfer_voltage;
Austin Schuhd78ab542013-03-01 22:22:19 -0800928 output->index_voltage = wrist_loop_->U(0, 0);
Austin Schuhf8c52252013-03-03 02:25:49 -0800929 output->loader_up = loader_up_;
930 output->disc_clamped = disc_clamped_;
931 output->disc_ejected = disc_ejected_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800932 }
933
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700934 if (safe_to_change_state) {
Austin Schuhd78ab542013-03-01 22:22:19 -0800935 safe_goal_ = goal_enum;
936 }
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800937 if (hopper_disc_count_ < 0) {
938 LOG(ERROR, "NEGATIVE DISCS. VERY VERY BAD\n");
939 }
Austin Schuhd78ab542013-03-01 22:22:19 -0800940}
941
942} // namespace control_loops
943} // namespace frc971