blob: cffeab38e36d27f8b2b661d8cd2e7f36ba1f89b3 [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;
Brian Silvermanf0716e82013-03-17 23:36:11 -0700490 if (!position) {
491 // This fixes a nasty indexer bug.
492 // If we didn't get a position this cycle, we don't run the code below which
493 // checks the state of the disc detect sensor and whether all the discs are
494 // indexed. It is therefore not safe to change state and loose track of
495 // that disc.
496 safe_to_change_state = false;
497 }
Austin Schuhd78ab542013-03-01 22:22:19 -0800498 switch (safe_goal_) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800499 case Goal::HOLD:
Austin Schuhd78ab542013-03-01 22:22:19 -0800500 // The goal should already be good, so sit tight with everything the same
501 // as it was.
Austin Schuhd78ab542013-03-01 22:22:19 -0800502 break;
Austin Schuhf8c52252013-03-03 02:25:49 -0800503 case Goal::READY_LOWER:
504 case Goal::INTAKE:
Austin Schuhd78ab542013-03-01 22:22:19 -0800505 {
Austin Schuhd78ab542013-03-01 22:22:19 -0800506 if (position) {
Austin Schuh6328daf2013-03-05 00:53:15 -0800507 // Posedge of the disc entering the beam break.
508 if (position->bottom_disc_posedge_count !=
509 last_bottom_disc_posedge_count_) {
Austin Schuhd78ab542013-03-01 22:22:19 -0800510 transfer_frisbee_.Reset();
511 transfer_frisbee_.bottom_posedge_time_ = now;
Austin Schuha3e8e032013-03-10 18:43:14 -0700512 LOG(INFO, "Posedge of bottom disc %f\n",
513 transfer_frisbee_.bottom_posedge_time_.ToSeconds());
Austin Schuhd78ab542013-03-01 22:22:19 -0800514 ++hopper_disc_count_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800515 ++total_disc_count_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800516 }
517
518 // Disc exited the beam break now.
Austin Schuh6328daf2013-03-05 00:53:15 -0800519 if (position->bottom_disc_negedge_count !=
520 last_bottom_disc_negedge_count_) {
Austin Schuhd78ab542013-03-01 22:22:19 -0800521 transfer_frisbee_.bottom_negedge_time_ = now;
Austin Schuha3e8e032013-03-10 18:43:14 -0700522 LOG(INFO, "Negedge of bottom disc %f\n",
523 transfer_frisbee_.bottom_negedge_time_.ToSeconds());
Austin Schuhd78ab542013-03-01 22:22:19 -0800524 frisbees_.push_front(transfer_frisbee_);
525 }
526
527 if (position->bottom_disc_detect) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800528 intake_voltage = transfer_voltage = 12.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800529 // Must wait until the disc gets out before we can change state.
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700530 safe_to_change_state = false;
Austin Schuhd78ab542013-03-01 22:22:19 -0800531
Austin Schuhf8c52252013-03-03 02:25:49 -0800532 // TODO(aschuh): A disc on the way through needs to start moving
533 // the indexer if it isn't already moving. Maybe?
Austin Schuhd78ab542013-03-01 22:22:19 -0800534
535 Time elapsed_posedge_time = now -
536 transfer_frisbee_.bottom_posedge_time_;
537 if (elapsed_posedge_time >= Time::InSeconds(0.3)) {
538 // It has been too long. The disc must be jammed.
539 LOG(ERROR, "Been way too long. Jammed disc?\n");
Brian Silvermanf0716e82013-03-17 23:36:11 -0700540 intake_voltage = 0.0;
541 transfer_voltage = -12.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800542 }
543 }
544
Austin Schuhf8c52252013-03-03 02:25:49 -0800545 // Check all non-indexed discs and see if they should be indexed.
Austin Schuhb6d898b2013-03-03 15:34:35 -0800546 for (auto frisbee = frisbees_.begin();
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800547 frisbee != frisbees_.end(); ++frisbee) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800548 if (!frisbee->has_been_indexed_) {
549 intake_voltage = transfer_voltage = 12.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800550
Austin Schuh6328daf2013-03-05 00:53:15 -0800551 if (last_bottom_disc_negedge_wait_count_ !=
552 position->bottom_disc_negedge_wait_count) {
553 // We have an index difference.
554 // Save the indexer position, and the time.
555 if (last_bottom_disc_negedge_wait_count_ + 1 !=
556 position->bottom_disc_negedge_wait_count) {
557 LOG(ERROR, "Funny, we got 2 edges since we last checked.\n");
558 }
559
560 // Save the captured position as the position at which the disc
561 // touched the indexer.
Austin Schuhd78ab542013-03-01 22:22:19 -0800562 LOG(INFO, "Grabbed on the index now at %f\n", index_position);
Austin Schuhb6d898b2013-03-03 15:34:35 -0800563 frisbee->has_been_indexed_ = true;
Austin Schuh6328daf2013-03-05 00:53:15 -0800564 frisbee->index_start_position_ =
565 position->bottom_disc_negedge_wait_position;
Austin Schuhd78ab542013-03-01 22:22:19 -0800566 }
567 }
Austin Schuhb6d898b2013-03-03 15:34:35 -0800568 if (!frisbee->has_been_indexed_) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800569 // All discs must be indexed before it is safe to stop indexing.
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700570 safe_to_change_state = false;
Austin Schuhd78ab542013-03-01 22:22:19 -0800571 }
572 }
573
Austin Schuhf8c52252013-03-03 02:25:49 -0800574 // Figure out where the indexer should be to move the discs down to
575 // the right position.
Brian Silverman94195052013-03-09 13:45:05 -0800576 double max_disc_position = 0;
Austin Schuh1b864a12013-03-07 00:46:50 -0800577 if (MaxDiscPosition(&max_disc_position, NULL)) {
Austin Schuha3e8e032013-03-10 18:43:14 -0700578 LOG(DEBUG, "There is a disc down here!\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800579 // TODO(aschuh): Figure out what to do if grabbing the next one
580 // would cause things to jam into the loader.
581 // Say we aren't ready any more. Undefined behavior will result if
582 // that isn't observed.
583 double bottom_disc_position =
584 max_disc_position + ConvertDiscAngleToIndex(M_PI);
585 wrist_loop_->R << bottom_disc_position, 0.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800586
Austin Schuhf8c52252013-03-03 02:25:49 -0800587 // Verify that we are close enough to the goal so that we should be
588 // fine accepting the next disc.
589 double disc_error_meters = ConvertIndexToDiscPosition(
590 wrist_loop_->X_hat(0, 0) - bottom_disc_position);
591 // We are ready for the next disc if the first one is in the first
592 // half circle of the indexer. It will take time for the disc to
593 // come into the indexer, so we will be able to move it out of the
594 // way in time.
595 // This choice also makes sure that we don't claim that we aren't
596 // ready between full speed intaking.
597 if (-ConvertDiscAngleToIndex(M_PI) < disc_error_meters &&
598 disc_error_meters < 0.04) {
599 // We are only ready if we aren't being asked to change state or
600 // are full.
601 status->ready_to_intake =
602 (safe_goal_ == goal_enum) && hopper_disc_count_ < 4;
603 } else {
604 status->ready_to_intake = false;
Austin Schuhd78ab542013-03-01 22:22:19 -0800605 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800606 } else {
607 // No discs! We are always ready for more if we aren't being
608 // asked to change state.
609 status->ready_to_intake = (safe_goal_ == goal_enum);
Austin Schuhd78ab542013-03-01 22:22:19 -0800610 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800611
612 // Turn on the transfer roller if we are ready.
613 if (status->ready_to_intake && hopper_disc_count_ < 4 &&
614 safe_goal_ == Goal::INTAKE) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800615 intake_voltage = transfer_voltage = 12.0;
Austin Schuhf8c52252013-03-03 02:25:49 -0800616 }
Austin Schuhd78ab542013-03-01 22:22:19 -0800617 }
Austin Schuha3e8e032013-03-10 18:43:14 -0700618 LOG(DEBUG, "INTAKE\n");
Austin Schuhd78ab542013-03-01 22:22:19 -0800619 }
620 break;
Austin Schuhf8c52252013-03-03 02:25:49 -0800621 case Goal::READY_SHOOTER:
622 case Goal::SHOOT:
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700623 // Don't let us leave the shoot or preload state if there are 4 discs in
624 // the hopper.
625 if (hopper_disc_count_ >= 4 && goal_enum != Goal::SHOOT) {
626 safe_to_change_state = false;
627 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800628 // Check if we have any discs to shoot or load and handle them.
Brian Silverman94195052013-03-09 13:45:05 -0800629 double min_disc_position = 0;
Austin Schuh1b864a12013-03-07 00:46:50 -0800630 if (MinDiscPosition(&min_disc_position, NULL)) {
631 const double ready_disc_position = min_disc_position +
632 ConvertDiscPositionToIndex(kReadyToPreload - kIndexStartPosition);
Austin Schuhf8c52252013-03-03 02:25:49 -0800633
634 const double grabbed_disc_position =
635 min_disc_position +
636 ConvertDiscPositionToIndex(kReadyToLiftPosition -
Brian Silvermanb7bcef12013-03-16 13:57:11 -0700637 kIndexStartPosition + 0.10);
Austin Schuhf8c52252013-03-03 02:25:49 -0800638
639 // Check the state of the loader FSM.
640 // If it is ready to load discs, position the disc so that it is ready
641 // to be grabbed.
642 // If it isn't ready, there is a disc in there. It needs to finish it's
643 // cycle first.
644 if (loader_state_ != LoaderState::READY) {
645 // We already have a disc in the loader.
646 // Stage the discs back a bit.
647 wrist_loop_->R << ready_disc_position, 0.0;
648
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800649 // Shoot if we are grabbed and being asked to shoot.
650 if (loader_state_ == LoaderState::GRABBED &&
651 safe_goal_ == Goal::SHOOT) {
652 loader_goal_ = LoaderGoal::SHOOT_AND_RESET;
653 }
654
Austin Schuhf8c52252013-03-03 02:25:49 -0800655 // Must wait until it has been grabbed to continue.
656 if (loader_state_ == LoaderState::GRABBING) {
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700657 safe_to_change_state = false;
Austin Schuhf8c52252013-03-03 02:25:49 -0800658 }
659 } else {
660 // No disc up top right now.
661 wrist_loop_->R << grabbed_disc_position, 0.0;
662
663 // See if the disc has gotten pretty far up yet.
664 if (wrist_loop_->X_hat(0, 0) > ready_disc_position) {
665 // Point of no return. We are committing to grabbing it now.
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700666 safe_to_change_state = false;
Austin Schuhf8c52252013-03-03 02:25:49 -0800667 const double robust_grabbed_disc_position =
668 (grabbed_disc_position -
669 ConvertDiscPositionToIndex(kGrabberLength));
670
671 // If close, start grabbing and/or shooting.
672 if (wrist_loop_->X_hat(0, 0) > robust_grabbed_disc_position) {
673 // Start the state machine.
674 if (safe_goal_ == Goal::SHOOT) {
675 loader_goal_ = LoaderGoal::SHOOT_AND_RESET;
676 } else {
677 loader_goal_ = LoaderGoal::GRAB;
678 }
679 // This frisbee is now gone. Take it out of the queue.
680 frisbees_.pop_back();
Austin Schuhf8c52252013-03-03 02:25:49 -0800681 }
682 }
683 }
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800684 } else {
685 if (loader_state_ != LoaderState::READY) {
686 // Shoot if we are grabbed and being asked to shoot.
687 if (loader_state_ == LoaderState::GRABBED &&
688 safe_goal_ == Goal::SHOOT) {
689 loader_goal_ = LoaderGoal::SHOOT_AND_RESET;
690 }
691 } else {
692 // Ok, no discs in sight. Spin the hopper up by 150% of it's full
693 // range and verify that we don't see anything.
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800694 const double hopper_clear_verification_position =
Austin Schuha3e8e032013-03-10 18:43:14 -0700695 ::std::max(upper_open_region_.lower_bound(),
Austin Schuh723770b2013-03-10 13:26:20 -0700696 lower_open_region_.lower_bound()) +
Austin Schuhf60861b2013-03-14 00:14:10 -0700697 ConvertDiscPositionToIndex(kIndexFreeLength) * 1.5;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800698
699 wrist_loop_->R << hopper_clear_verification_position, 0.0;
700 if (::std::abs(wrist_loop_->X_hat(0, 0) -
701 hopper_clear_verification_position) <
702 ConvertDiscPositionToIndex(0.05)) {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800703 // We are at the end of the range. There are no more discs here.
704 while (frisbees_.size() > 0) {
705 LOG(ERROR, "Dropping an extra disc since it can't exist\n");
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700706 LOG(ERROR, "Upper is [%f %f]\n",
707 upper_open_region_.upper_bound(),
708 upper_open_region_.lower_bound());
709 LOG(ERROR, "Lower is [%f %f]\n",
710 lower_open_region_.upper_bound(),
711 lower_open_region_.lower_bound());
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800712 frisbees_.pop_back();
713 --hopper_disc_count_;
714 --total_disc_count_;
715 }
716 if (hopper_disc_count_ != 0) {
717 LOG(ERROR,
718 "Emptied the hopper out but there are still discs there\n");
Brian Silvermanf0716e82013-03-17 23:36:11 -0700719 hopper_disc_count_ = 0;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800720 }
721 }
722 }
723 }
724
725 {
726 const double hopper_clear_verification_position =
Austin Schuha3e8e032013-03-10 18:43:14 -0700727 ::std::max(upper_open_region_.lower_bound(),
Austin Schuh723770b2013-03-10 13:26:20 -0700728 lower_open_region_.lower_bound()) +
Austin Schuhf60861b2013-03-14 00:14:10 -0700729 ConvertDiscPositionToIndex(kIndexFreeLength) * 1.5;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800730
731 if (wrist_loop_->X_hat(0, 0) >
732 hopper_clear_verification_position +
733 ConvertDiscPositionToIndex(0.05)) {
734 // We are at the end of the range. There are no more discs here.
735 while (frisbees_.size() > 0) {
736 LOG(ERROR, "Dropping an extra disc since it can't exist\n");
Austin Schuha3e8e032013-03-10 18:43:14 -0700737 LOG(ERROR, "Upper is [%f %f]\n",
738 upper_open_region_.upper_bound(),
739 upper_open_region_.lower_bound());
740 LOG(ERROR, "Lower is [%f %f]\n",
741 lower_open_region_.upper_bound(),
742 lower_open_region_.lower_bound());
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800743 frisbees_.pop_back();
744 --hopper_disc_count_;
745 --total_disc_count_;
746 }
Brian Silvermanf0716e82013-03-17 23:36:11 -0700747 if (hopper_disc_count_ != 0) {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800748 LOG(ERROR,
Brian Silvermanb7bcef12013-03-16 13:57:11 -0700749 "Emptied the hopper out but there are still %"PRId32" discs there\n",
Austin Schuhf60861b2013-03-14 00:14:10 -0700750 hopper_disc_count_);
Brian Silvermanf0716e82013-03-17 23:36:11 -0700751 hopper_disc_count_ = 0;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800752 }
753 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800754 }
755
Austin Schuha3e8e032013-03-10 18:43:14 -0700756 LOG(DEBUG, "READY_SHOOTER or SHOOT\n");
Austin Schuhd78ab542013-03-01 22:22:19 -0800757 break;
Austin Schuhf8c52252013-03-03 02:25:49 -0800758 }
759
Austin Schuhd3d0fbf2013-03-14 00:37:00 -0700760 // Wait for a period of time to make sure that the disc gets sucked
761 // in properly. We need to do this regardless of what the indexer is doing.
762 for (auto frisbee = frisbees_.begin();
763 frisbee != frisbees_.end(); ++frisbee) {
764 if (now - frisbee->bottom_negedge_time_ < kTransferOffDelay) {
765 transfer_voltage = 12.0;
766 }
767 }
768
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700769 // If we have 4 discs, it is time to preload.
770 if (safe_to_change_state && hopper_disc_count_ >= 4) {
771 switch (safe_goal_) {
772 case Goal::HOLD:
773 case Goal::READY_LOWER:
774 case Goal::INTAKE:
775 safe_goal_ = Goal::READY_SHOOTER;
776 safe_to_change_state = false;
Brian Silvermana0238ce2013-03-13 17:58:45 -0700777 LOG(INFO, "We have %"PRId32" discs, time to preload automatically\n",
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700778 hopper_disc_count_);
779 break;
780 case Goal::READY_SHOOTER:
781 case Goal::SHOOT:
782 break;
783 }
784 }
785
Austin Schuhf8c52252013-03-03 02:25:49 -0800786 // The only way out of the loader is to shoot the disc. The FSM can only go
787 // forwards.
788 switch (loader_state_) {
789 case LoaderState::READY:
Austin Schuha3e8e032013-03-10 18:43:14 -0700790 LOG(DEBUG, "Loader READY\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800791 // Open and down, ready to accept a disc.
792 loader_up_ = false;
793 disc_clamped_ = false;
794 disc_ejected_ = false;
795 if (loader_goal_ == LoaderGoal::GRAB ||
796 loader_goal_ == LoaderGoal::SHOOT_AND_RESET) {
797 if (loader_goal_ == LoaderGoal::GRAB) {
Austin Schuha3e8e032013-03-10 18:43:14 -0700798 LOG(INFO, "Told to GRAB, moving on\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800799 } else {
Austin Schuha3e8e032013-03-10 18:43:14 -0700800 LOG(INFO, "Told to SHOOT_AND_RESET, moving on\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800801 }
802 loader_state_ = LoaderState::GRABBING;
803 loader_countdown_ = kGrabbingDelay;
804 } else {
805 break;
806 }
807 case LoaderState::GRABBING:
Austin Schuha3e8e032013-03-10 18:43:14 -0700808 LOG(DEBUG, "Loader GRABBING %d\n", loader_countdown_);
Austin Schuhf8c52252013-03-03 02:25:49 -0800809 // Closing the grabber.
810 loader_up_ = false;
811 disc_clamped_ = true;
812 disc_ejected_ = false;
813 if (loader_countdown_ > 0) {
814 --loader_countdown_;
815 break;
816 } else {
817 loader_state_ = LoaderState::GRABBED;
818 }
819 case LoaderState::GRABBED:
Austin Schuha3e8e032013-03-10 18:43:14 -0700820 LOG(DEBUG, "Loader GRABBED\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800821 // Grabber closed.
822 loader_up_ = false;
823 disc_clamped_ = true;
824 disc_ejected_ = false;
825 if (loader_goal_ == LoaderGoal::SHOOT_AND_RESET) {
Austin Schuha3e8e032013-03-10 18:43:14 -0700826 shooter.status.FetchLatest();
827 if (shooter.status.get()) {
828 // TODO(aschuh): If we aren't shooting nicely, wait until the shooter
829 // is up to speed rather than just spinning.
Austin Schuhf60861b2013-03-14 00:14:10 -0700830 if (shooter.status->average_velocity > 130 && shooter.status->ready) {
Austin Schuha3e8e032013-03-10 18:43:14 -0700831 loader_state_ = LoaderState::LIFTING;
832 loader_countdown_ = kLiftingDelay;
833 LOG(INFO, "Told to SHOOT_AND_RESET, moving on\n");
834 } else {
835 LOG(WARNING, "Told to SHOOT_AND_RESET, shooter too slow at %f\n",
836 shooter.status->average_velocity);
837 break;
838 }
839 } else {
840 LOG(ERROR, "Told to SHOOT_AND_RESET, no shooter data, moving on.\n");
841 loader_state_ = LoaderState::LIFTING;
842 loader_countdown_ = kLiftingDelay;
843 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800844 } else if (loader_goal_ == LoaderGoal::READY) {
845 LOG(ERROR, "Can't go to ready when we have something grabbed.\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800846 break;
847 } else {
848 break;
849 }
850 case LoaderState::LIFTING:
Austin Schuha3e8e032013-03-10 18:43:14 -0700851 LOG(DEBUG, "Loader LIFTING %d\n", loader_countdown_);
Austin Schuhf8c52252013-03-03 02:25:49 -0800852 // Lifting the disc.
853 loader_up_ = true;
854 disc_clamped_ = true;
855 disc_ejected_ = false;
856 if (loader_countdown_ > 0) {
857 --loader_countdown_;
858 break;
859 } else {
860 loader_state_ = LoaderState::LIFTED;
861 }
862 case LoaderState::LIFTED:
Austin Schuha3e8e032013-03-10 18:43:14 -0700863 LOG(DEBUG, "Loader LIFTED\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800864 // Disc lifted. Time to eject it out.
865 loader_up_ = true;
866 disc_clamped_ = true;
867 disc_ejected_ = false;
868 loader_state_ = LoaderState::SHOOTING;
869 loader_countdown_ = kShootingDelay;
870 case LoaderState::SHOOTING:
Austin Schuha3e8e032013-03-10 18:43:14 -0700871 LOG(DEBUG, "Loader SHOOTING %d\n", loader_countdown_);
Austin Schuhf8c52252013-03-03 02:25:49 -0800872 // Ejecting the disc into the shooter.
873 loader_up_ = true;
874 disc_clamped_ = false;
875 disc_ejected_ = true;
876 if (loader_countdown_ > 0) {
877 --loader_countdown_;
878 break;
879 } else {
880 loader_state_ = LoaderState::SHOOT;
881 }
882 case LoaderState::SHOOT:
Austin Schuha3e8e032013-03-10 18:43:14 -0700883 LOG(DEBUG, "Loader SHOOT\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800884 // The disc has been shot.
885 loader_up_ = true;
886 disc_clamped_ = false;
887 disc_ejected_ = true;
888 loader_state_ = LoaderState::LOWERING;
889 loader_countdown_ = kLoweringDelay;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800890 --hopper_disc_count_;
Austin Schuh70be1ba2013-03-10 13:37:17 -0700891 ++shot_disc_count_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800892 case LoaderState::LOWERING:
Austin Schuha3e8e032013-03-10 18:43:14 -0700893 LOG(DEBUG, "Loader LOWERING %d\n", loader_countdown_);
Austin Schuhf8c52252013-03-03 02:25:49 -0800894 // Lowering the loader back down.
895 loader_up_ = false;
896 disc_clamped_ = false;
897 disc_ejected_ = true;
898 if (loader_countdown_ > 0) {
899 --loader_countdown_;
900 break;
901 } else {
902 loader_state_ = LoaderState::LOWERED;
903 }
904 case LoaderState::LOWERED:
Austin Schuha3e8e032013-03-10 18:43:14 -0700905 LOG(DEBUG, "Loader LOWERED\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800906 // The indexer is lowered.
907 loader_up_ = false;
908 disc_clamped_ = false;
909 disc_ejected_ = false;
910 loader_state_ = LoaderState::READY;
911 // Once we have shot, we need to hang out in READY until otherwise
912 // notified.
913 loader_goal_ = LoaderGoal::READY;
Austin Schuhd78ab542013-03-01 22:22:19 -0800914 break;
915 }
916
917 // Update the observer.
918 wrist_loop_->Update(position != NULL, output == NULL);
919
920 if (position) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800921 LOG(DEBUG, "pos=%f\n", position->index_position);
Austin Schuhd78ab542013-03-01 22:22:19 -0800922 last_bottom_disc_detect_ = position->bottom_disc_detect;
Austin Schuh825bde92013-03-06 00:16:46 -0800923 last_top_disc_detect_ = position->top_disc_detect;
Austin Schuh6328daf2013-03-05 00:53:15 -0800924 last_bottom_disc_posedge_count_ = position->bottom_disc_posedge_count;
925 last_bottom_disc_negedge_count_ = position->bottom_disc_negedge_count;
926 last_bottom_disc_negedge_wait_count_ =
927 position->bottom_disc_negedge_wait_count;
Austin Schuh825bde92013-03-06 00:16:46 -0800928 last_top_disc_posedge_count_ = position->top_disc_posedge_count;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800929 last_top_disc_negedge_count_ = position->top_disc_negedge_count;
Austin Schuhd78ab542013-03-01 22:22:19 -0800930 }
931
932 status->hopper_disc_count = hopper_disc_count_;
933 status->total_disc_count = total_disc_count_;
Austin Schuh70be1ba2013-03-10 13:37:17 -0700934 status->shot_disc_count = shot_disc_count_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800935 status->preloaded = (loader_state_ != LoaderState::READY);
Austin Schuhd78ab542013-03-01 22:22:19 -0800936
937 if (output) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800938 output->intake_voltage = intake_voltage;
Austin Schuhf8c52252013-03-03 02:25:49 -0800939 output->transfer_voltage = transfer_voltage;
Austin Schuhd78ab542013-03-01 22:22:19 -0800940 output->index_voltage = wrist_loop_->U(0, 0);
Austin Schuhf8c52252013-03-03 02:25:49 -0800941 output->loader_up = loader_up_;
942 output->disc_clamped = disc_clamped_;
943 output->disc_ejected = disc_ejected_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800944 }
945
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700946 if (safe_to_change_state) {
Austin Schuhd78ab542013-03-01 22:22:19 -0800947 safe_goal_ = goal_enum;
948 }
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800949 if (hopper_disc_count_ < 0) {
950 LOG(ERROR, "NEGATIVE DISCS. VERY VERY BAD\n");
951 }
Austin Schuhd78ab542013-03-01 22:22:19 -0800952}
953
954} // namespace control_loops
955} // namespace frc971