blob: c757cd27ee4e5b1504d5618bdc035002075f4634 [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 =
90 ::aos::time::Time::InSeconds(0.1);
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_) {
269 wrist_loop_->R << wrist_loop_->Y(0, 0), 0.0;
Austin Schuhc5ef1bb2013-03-10 00:42:05 -0800270 wrist_loop_->X_hat(0, 0) = wrist_loop_->Y(0, 0);
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800271 no_prior_position_ = false;
Austin Schuh6328daf2013-03-05 00:53:15 -0800272 last_bottom_disc_posedge_count_ = position->bottom_disc_posedge_count;
273 last_bottom_disc_negedge_count_ = position->bottom_disc_negedge_count;
274 last_bottom_disc_negedge_wait_count_ =
275 position->bottom_disc_negedge_wait_count;
Austin Schuh825bde92013-03-06 00:16:46 -0800276 last_top_disc_posedge_count_ = position->top_disc_posedge_count;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800277 last_top_disc_negedge_count_ = position->top_disc_negedge_count;
278 // The open positions for the upper is right here and isn't a hard edge.
Austin Schuh723770b2013-03-10 13:26:20 -0700279 upper_open_region_.Restart(wrist_loop_->Y(0, 0));
280 lower_open_region_.Restart(wrist_loop_->Y(0, 0));
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800281 }
282
Austin Schuh1b864a12013-03-07 00:46:50 -0800283 // If the cRIO is gone for over 1/2 of a second, assume that it rebooted.
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800284 if (missing_position_count_ > 50) {
Austin Schuh6328daf2013-03-05 00:53:15 -0800285 last_bottom_disc_posedge_count_ = position->bottom_disc_posedge_count;
286 last_bottom_disc_negedge_count_ = position->bottom_disc_negedge_count;
287 last_bottom_disc_negedge_wait_count_ =
288 position->bottom_disc_negedge_wait_count;
Austin Schuh825bde92013-03-06 00:16:46 -0800289 last_top_disc_posedge_count_ = position->top_disc_posedge_count;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800290 last_top_disc_negedge_count_ = position->top_disc_negedge_count;
291 // We can't really trust the open range any more if the crio rebooted.
Austin Schuh723770b2013-03-10 13:26:20 -0700292 upper_open_region_.Restart(wrist_loop_->Y(0, 0));
293 lower_open_region_.Restart(wrist_loop_->Y(0, 0));
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800294 // Adjust the disc positions so that they don't have to move.
295 const double disc_offset =
296 position->index_position - wrist_loop_->X_hat(0, 0);
297 for (auto frisbee = frisbees_.begin();
298 frisbee != frisbees_.end(); ++frisbee) {
299 frisbee->OffsetDisc(disc_offset);
300 }
Austin Schuhc5ef1bb2013-03-10 00:42:05 -0800301 wrist_loop_->X_hat(0, 0) = wrist_loop_->Y(0, 0);
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800302 }
303 missing_position_count_ = 0;
304 } else {
305 ++missing_position_count_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800306 }
307 const double index_position = wrist_loop_->X_hat(0, 0);
308
Austin Schuh825bde92013-03-06 00:16:46 -0800309 if (position) {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800310 // Reset the open region if we saw a negedge.
Austin Schuh723770b2013-03-10 13:26:20 -0700311 if (position->bottom_disc_negedge_wait_count !=
312 last_bottom_disc_negedge_wait_count_) {
313 // Saw a negedge, must be a new region.
314 lower_open_region_.Restart(position->bottom_disc_negedge_wait_position);
315 }
316 // Reset the open region if we saw a negedge.
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800317 if (position->top_disc_negedge_count != last_top_disc_negedge_count_) {
318 // Saw a negedge, must be a new region.
Austin Schuh723770b2013-03-10 13:26:20 -0700319 upper_open_region_.Restart(position->top_disc_negedge_position);
320 }
321
322 // No disc. Expand the open region.
323 if (!position->bottom_disc_detect) {
324 lower_open_region_.Expand(index_position);
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800325 }
326
327 // No disc. Expand the open region.
328 if (!position->top_disc_detect) {
Austin Schuh723770b2013-03-10 13:26:20 -0700329 upper_open_region_.Expand(index_position);
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800330 }
331
Austin Schuh825bde92013-03-06 00:16:46 -0800332 if (!position->top_disc_detect) {
333 // We don't see a disc. Verify that there are no discs that we should be
334 // seeing.
Austin Schuh1b864a12013-03-07 00:46:50 -0800335 // Assume that discs will move slow enough that we won't miss one as it
336 // goes by. They will either pile up above or below the sensor.
Austin Schuhdff24e22013-03-06 00:41:21 -0800337
338 double cumulative_offset = 0.0;
339 for (auto frisbee = frisbees_.rbegin(), rend = frisbees_.rend();
340 frisbee != rend; ++frisbee) {
341 frisbee->OffsetDisc(cumulative_offset);
342 double amount_moved = frisbee->ObserveNoTopDiscSensor(
Austin Schuh6b1ad2f2013-03-11 23:23:00 -0700343 wrist_loop_->X_hat(0, 0));
Austin Schuhdff24e22013-03-06 00:41:21 -0800344 cumulative_offset += amount_moved;
Austin Schuh825bde92013-03-06 00:16:46 -0800345 }
346 }
Austin Schuh1b864a12013-03-07 00:46:50 -0800347
Austin Schuh825bde92013-03-06 00:16:46 -0800348 if (position->top_disc_posedge_count != last_top_disc_posedge_count_) {
Austin Schuh1b864a12013-03-07 00:46:50 -0800349 const double index_position = wrist_loop_->X_hat(0, 0) -
350 position->index_position + position->top_disc_posedge_position;
Austin Schuh825bde92013-03-06 00:16:46 -0800351 // TODO(aschuh): Sanity check this number...
352 // Requires storing when the disc was last seen with the sensor off, and
353 // figuring out what to do if things go south.
354
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800355 // 1 if discs are going up, 0 if we have no clue, and -1 if they are going
356 // down.
357 int disc_direction = 0;
Austin Schuh825bde92013-03-06 00:16:46 -0800358 if (wrist_loop_->X_hat(1, 0) > 50.0) {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800359 disc_direction = 1;
360 } else if (wrist_loop_->X_hat(1, 0) < -50.0) {
361 disc_direction = -1;
362 } else {
363 // Save the upper and lower positions that we last saw a disc at.
364 // If there is a big buffer above, must be a disc from below.
365 // If there is a big buffer below, must be a disc from above.
366 // This should work to replace the velocity threshold above.
367
Austin Schuh723770b2013-03-10 13:26:20 -0700368 const double open_width = upper_open_region_.width();
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800369 const double relative_upper_open_precentage =
Austin Schuh723770b2013-03-10 13:26:20 -0700370 (upper_open_region_.upper_bound() - index_position) / open_width;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800371 const double relative_lower_open_precentage =
Austin Schuh723770b2013-03-10 13:26:20 -0700372 (index_position - upper_open_region_.lower_bound()) / open_width;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800373
374 if (ConvertIndexToDiscPosition(open_width) <
375 kTopDiscDetectMinSeperation * 0.9) {
376 LOG(ERROR, "Discs are way too close to each other. Doing nothing\n");
377 } else if (relative_upper_open_precentage > 0.75) {
378 // Looks like it is a disc going down from above since we are near
379 // the upper edge.
380 disc_direction = -1;
Austin Schuha3e8e032013-03-10 18:43:14 -0700381 LOG(INFO, "Disc edge going down\n");
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800382 } else if (relative_lower_open_precentage > 0.75) {
383 // Looks like it is a disc going up from below since we are near
384 // the lower edge.
385 disc_direction = 1;
Austin Schuha3e8e032013-03-10 18:43:14 -0700386 LOG(INFO, "Disc edge going up\n");
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800387 } else {
388 LOG(ERROR,
389 "Got an edge in the middle of what should be an open region.\n");
390 LOG(ERROR, "Open width: %f upper precentage %f %%\n",
391 open_width, relative_upper_open_precentage);
392 }
393 }
394
395 if (disc_direction > 0) {
Austin Schuh825bde92013-03-06 00:16:46 -0800396 // Moving up at a reasonable clip.
Austin Schuh1b864a12013-03-07 00:46:50 -0800397 // Find the highest disc that is below the top disc sensor.
398 // While we are at it, count the number above and log an error if there
399 // are too many.
400 if (frisbees_.size() == 0) {
401 Frisbee new_frisbee;
402 new_frisbee.has_been_indexed_ = true;
403 new_frisbee.index_start_position_ = index_position -
404 ConvertDiscPositionToIndex(kTopDiscDetectStart -
405 kIndexStartPosition);
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800406 ++hopper_disc_count_;
407 ++total_disc_count_;
Austin Schuh1b864a12013-03-07 00:46:50 -0800408 frisbees_.push_front(new_frisbee);
409 LOG(WARNING, "Added a disc to the hopper at the top sensor\n");
410 }
411
412 int above_disc_count = 0;
413 double highest_position = 0;
414 Frisbee *highest_frisbee_below_sensor = NULL;
415 for (auto frisbee = frisbees_.rbegin(), rend = frisbees_.rend();
416 frisbee != rend; ++frisbee) {
417 const double disc_position = frisbee->absolute_position(
418 index_position);
419 // It is save to use the top position for the cuttoff, since the
420 // sensor being low will result in discs being pushed off of it.
421 if (disc_position >= kTopDiscDetectStop) {
422 ++above_disc_count;
423 } else if (!highest_frisbee_below_sensor ||
424 disc_position > highest_position) {
425 highest_frisbee_below_sensor = &*frisbee;
426 highest_position = disc_position;
427 }
428 }
429 if (above_disc_count > 1) {
430 LOG(ERROR, "We have 2 discs above the top sensor.\n");
431 }
432
433 // We now have the disc. Shift all the ones below the sensor up by the
434 // computed delta.
435 const double disc_delta = IndexMotor::ConvertDiscPositionToIndex(
436 highest_position - kTopDiscDetectStart);
437 for (auto frisbee = frisbees_.rbegin(), rend = frisbees_.rend();
438 frisbee != rend; ++frisbee) {
439 const double disc_position = frisbee->absolute_position(
440 index_position);
441 if (disc_position < kTopDiscDetectStop) {
442 frisbee->OffsetDisc(disc_delta);
443 }
444 }
Austin Schuha3e8e032013-03-10 18:43:14 -0700445 LOG(INFO, "Currently have %d discs, saw posedge moving up. "
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800446 "Moving down by %f to %f\n", frisbees_.size(),
Austin Schuh1b864a12013-03-07 00:46:50 -0800447 ConvertIndexToDiscPosition(disc_delta),
448 highest_frisbee_below_sensor->absolute_position(
449 wrist_loop_->X_hat(0, 0)));
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800450 } else if (disc_direction < 0) {
Austin Schuh825bde92013-03-06 00:16:46 -0800451 // Moving down at a reasonable clip.
Austin Schuh1b864a12013-03-07 00:46:50 -0800452 // There can only be 1 disc up top that would give us a posedge.
453 // Find it and place it at the one spot that it can be.
Brian Silverman94195052013-03-09 13:45:05 -0800454 double min_disc_position = 0;
Austin Schuh1b864a12013-03-07 00:46:50 -0800455 Frisbee *min_frisbee = NULL;
456 MinDiscPosition(&min_disc_position, &min_frisbee);
457 if (!min_frisbee) {
458 // Uh, oh, we see a disc but there isn't one...
459 LOG(ERROR, "Saw a disc up top but there isn't one in the hopper\n");
460 } else {
461 const double disc_position = min_frisbee->absolute_position(
462 index_position);
463
464 const double disc_delta_meters = disc_position - kTopDiscDetectStop;
465 const double disc_delta = IndexMotor::ConvertDiscPositionToIndex(
466 disc_delta_meters);
Austin Schuha3e8e032013-03-10 18:43:14 -0700467 LOG(INFO, "Posedge going down. Moving top disc down by %f\n",
468 disc_delta_meters);
Austin Schuh1b864a12013-03-07 00:46:50 -0800469 for (auto frisbee = frisbees_.begin(), end = frisbees_.end();
470 frisbee != end; ++frisbee) {
471 frisbee->OffsetDisc(disc_delta);
472 }
473 }
Austin Schuh825bde92013-03-06 00:16:46 -0800474 } else {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800475 LOG(ERROR, "Not sure how to handle the upper posedge, doing nothing\n");
Austin Schuh825bde92013-03-06 00:16:46 -0800476 }
477 }
478 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800479
Austin Schuhf8c52252013-03-03 02:25:49 -0800480 // Bool to track if it is safe for the goal to change yet.
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700481 bool safe_to_change_state = true;
Austin Schuhd78ab542013-03-01 22:22:19 -0800482 switch (safe_goal_) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800483 case Goal::HOLD:
Austin Schuhd78ab542013-03-01 22:22:19 -0800484 // The goal should already be good, so sit tight with everything the same
485 // as it was.
Austin Schuhd78ab542013-03-01 22:22:19 -0800486 break;
Austin Schuhf8c52252013-03-03 02:25:49 -0800487 case Goal::READY_LOWER:
488 case Goal::INTAKE:
Austin Schuhd78ab542013-03-01 22:22:19 -0800489 {
Austin Schuhd78ab542013-03-01 22:22:19 -0800490 if (position) {
Austin Schuh6328daf2013-03-05 00:53:15 -0800491 // Posedge of the disc entering the beam break.
492 if (position->bottom_disc_posedge_count !=
493 last_bottom_disc_posedge_count_) {
Austin Schuhd78ab542013-03-01 22:22:19 -0800494 transfer_frisbee_.Reset();
495 transfer_frisbee_.bottom_posedge_time_ = now;
Austin Schuha3e8e032013-03-10 18:43:14 -0700496 LOG(INFO, "Posedge of bottom disc %f\n",
497 transfer_frisbee_.bottom_posedge_time_.ToSeconds());
Austin Schuhd78ab542013-03-01 22:22:19 -0800498 ++hopper_disc_count_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800499 ++total_disc_count_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800500 }
501
502 // Disc exited the beam break now.
Austin Schuh6328daf2013-03-05 00:53:15 -0800503 if (position->bottom_disc_negedge_count !=
504 last_bottom_disc_negedge_count_) {
Austin Schuhd78ab542013-03-01 22:22:19 -0800505 transfer_frisbee_.bottom_negedge_time_ = now;
Austin Schuha3e8e032013-03-10 18:43:14 -0700506 LOG(INFO, "Negedge of bottom disc %f\n",
507 transfer_frisbee_.bottom_negedge_time_.ToSeconds());
Austin Schuhd78ab542013-03-01 22:22:19 -0800508 frisbees_.push_front(transfer_frisbee_);
509 }
510
511 if (position->bottom_disc_detect) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800512 intake_voltage = transfer_voltage = 12.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800513 // Must wait until the disc gets out before we can change state.
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700514 safe_to_change_state = false;
Austin Schuhd78ab542013-03-01 22:22:19 -0800515
Austin Schuhf8c52252013-03-03 02:25:49 -0800516 // TODO(aschuh): A disc on the way through needs to start moving
517 // the indexer if it isn't already moving. Maybe?
Austin Schuhd78ab542013-03-01 22:22:19 -0800518
519 Time elapsed_posedge_time = now -
520 transfer_frisbee_.bottom_posedge_time_;
521 if (elapsed_posedge_time >= Time::InSeconds(0.3)) {
522 // It has been too long. The disc must be jammed.
523 LOG(ERROR, "Been way too long. Jammed disc?\n");
Austin Schuhd78ab542013-03-01 22:22:19 -0800524 }
525 }
526
Austin Schuhf8c52252013-03-03 02:25:49 -0800527 // Check all non-indexed discs and see if they should be indexed.
Austin Schuhb6d898b2013-03-03 15:34:35 -0800528 for (auto frisbee = frisbees_.begin();
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800529 frisbee != frisbees_.end(); ++frisbee) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800530 if (!frisbee->has_been_indexed_) {
531 intake_voltage = transfer_voltage = 12.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800532
Austin Schuh6328daf2013-03-05 00:53:15 -0800533 if (last_bottom_disc_negedge_wait_count_ !=
534 position->bottom_disc_negedge_wait_count) {
535 // We have an index difference.
536 // Save the indexer position, and the time.
537 if (last_bottom_disc_negedge_wait_count_ + 1 !=
538 position->bottom_disc_negedge_wait_count) {
539 LOG(ERROR, "Funny, we got 2 edges since we last checked.\n");
540 }
541
542 // Save the captured position as the position at which the disc
543 // touched the indexer.
Austin Schuhd78ab542013-03-01 22:22:19 -0800544 LOG(INFO, "Grabbed on the index now at %f\n", index_position);
Austin Schuhb6d898b2013-03-03 15:34:35 -0800545 frisbee->has_been_indexed_ = true;
Austin Schuh6328daf2013-03-05 00:53:15 -0800546 frisbee->index_start_position_ =
547 position->bottom_disc_negedge_wait_position;
Austin Schuhd78ab542013-03-01 22:22:19 -0800548 }
549 }
Austin Schuhb6d898b2013-03-03 15:34:35 -0800550 if (!frisbee->has_been_indexed_) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800551 // All discs must be indexed before it is safe to stop indexing.
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700552 safe_to_change_state = false;
Austin Schuhd78ab542013-03-01 22:22:19 -0800553 }
554 }
555
Austin Schuhf8c52252013-03-03 02:25:49 -0800556 // Figure out where the indexer should be to move the discs down to
557 // the right position.
Brian Silverman94195052013-03-09 13:45:05 -0800558 double max_disc_position = 0;
Austin Schuh1b864a12013-03-07 00:46:50 -0800559 if (MaxDiscPosition(&max_disc_position, NULL)) {
Austin Schuha3e8e032013-03-10 18:43:14 -0700560 LOG(DEBUG, "There is a disc down here!\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800561 // TODO(aschuh): Figure out what to do if grabbing the next one
562 // would cause things to jam into the loader.
563 // Say we aren't ready any more. Undefined behavior will result if
564 // that isn't observed.
565 double bottom_disc_position =
566 max_disc_position + ConvertDiscAngleToIndex(M_PI);
567 wrist_loop_->R << bottom_disc_position, 0.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800568
Austin Schuhf8c52252013-03-03 02:25:49 -0800569 // Verify that we are close enough to the goal so that we should be
570 // fine accepting the next disc.
571 double disc_error_meters = ConvertIndexToDiscPosition(
572 wrist_loop_->X_hat(0, 0) - bottom_disc_position);
573 // We are ready for the next disc if the first one is in the first
574 // half circle of the indexer. It will take time for the disc to
575 // come into the indexer, so we will be able to move it out of the
576 // way in time.
577 // This choice also makes sure that we don't claim that we aren't
578 // ready between full speed intaking.
579 if (-ConvertDiscAngleToIndex(M_PI) < disc_error_meters &&
580 disc_error_meters < 0.04) {
581 // We are only ready if we aren't being asked to change state or
582 // are full.
583 status->ready_to_intake =
584 (safe_goal_ == goal_enum) && hopper_disc_count_ < 4;
585 } else {
586 status->ready_to_intake = false;
Austin Schuhd78ab542013-03-01 22:22:19 -0800587 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800588 } else {
589 // No discs! We are always ready for more if we aren't being
590 // asked to change state.
591 status->ready_to_intake = (safe_goal_ == goal_enum);
Austin Schuhd78ab542013-03-01 22:22:19 -0800592 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800593
594 // Turn on the transfer roller if we are ready.
595 if (status->ready_to_intake && hopper_disc_count_ < 4 &&
596 safe_goal_ == Goal::INTAKE) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800597 intake_voltage = transfer_voltage = 12.0;
Austin Schuhf8c52252013-03-03 02:25:49 -0800598 }
Austin Schuhd78ab542013-03-01 22:22:19 -0800599 }
Austin Schuha3e8e032013-03-10 18:43:14 -0700600 LOG(DEBUG, "INTAKE\n");
Austin Schuhd78ab542013-03-01 22:22:19 -0800601 }
602 break;
Austin Schuhf8c52252013-03-03 02:25:49 -0800603 case Goal::READY_SHOOTER:
604 case Goal::SHOOT:
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700605 // Don't let us leave the shoot or preload state if there are 4 discs in
606 // the hopper.
607 if (hopper_disc_count_ >= 4 && goal_enum != Goal::SHOOT) {
608 safe_to_change_state = false;
609 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800610 // Check if we have any discs to shoot or load and handle them.
Brian Silverman94195052013-03-09 13:45:05 -0800611 double min_disc_position = 0;
Austin Schuh1b864a12013-03-07 00:46:50 -0800612 if (MinDiscPosition(&min_disc_position, NULL)) {
613 const double ready_disc_position = min_disc_position +
614 ConvertDiscPositionToIndex(kReadyToPreload - kIndexStartPosition);
Austin Schuhf8c52252013-03-03 02:25:49 -0800615
616 const double grabbed_disc_position =
617 min_disc_position +
618 ConvertDiscPositionToIndex(kReadyToLiftPosition -
619 kIndexStartPosition + 0.03);
620
621 // Check the state of the loader FSM.
622 // If it is ready to load discs, position the disc so that it is ready
623 // to be grabbed.
624 // If it isn't ready, there is a disc in there. It needs to finish it's
625 // cycle first.
626 if (loader_state_ != LoaderState::READY) {
627 // We already have a disc in the loader.
628 // Stage the discs back a bit.
629 wrist_loop_->R << ready_disc_position, 0.0;
630
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800631 // Shoot if we are grabbed and being asked to shoot.
632 if (loader_state_ == LoaderState::GRABBED &&
633 safe_goal_ == Goal::SHOOT) {
634 loader_goal_ = LoaderGoal::SHOOT_AND_RESET;
635 }
636
Austin Schuhf8c52252013-03-03 02:25:49 -0800637 // Must wait until it has been grabbed to continue.
638 if (loader_state_ == LoaderState::GRABBING) {
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700639 safe_to_change_state = false;
Austin Schuhf8c52252013-03-03 02:25:49 -0800640 }
641 } else {
642 // No disc up top right now.
643 wrist_loop_->R << grabbed_disc_position, 0.0;
644
645 // See if the disc has gotten pretty far up yet.
646 if (wrist_loop_->X_hat(0, 0) > ready_disc_position) {
647 // Point of no return. We are committing to grabbing it now.
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700648 safe_to_change_state = false;
Austin Schuhf8c52252013-03-03 02:25:49 -0800649 const double robust_grabbed_disc_position =
650 (grabbed_disc_position -
651 ConvertDiscPositionToIndex(kGrabberLength));
652
653 // If close, start grabbing and/or shooting.
654 if (wrist_loop_->X_hat(0, 0) > robust_grabbed_disc_position) {
655 // Start the state machine.
656 if (safe_goal_ == Goal::SHOOT) {
657 loader_goal_ = LoaderGoal::SHOOT_AND_RESET;
658 } else {
659 loader_goal_ = LoaderGoal::GRAB;
660 }
661 // This frisbee is now gone. Take it out of the queue.
662 frisbees_.pop_back();
Austin Schuhf8c52252013-03-03 02:25:49 -0800663 }
664 }
665 }
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800666 } else {
667 if (loader_state_ != LoaderState::READY) {
668 // Shoot if we are grabbed and being asked to shoot.
669 if (loader_state_ == LoaderState::GRABBED &&
670 safe_goal_ == Goal::SHOOT) {
671 loader_goal_ = LoaderGoal::SHOOT_AND_RESET;
672 }
673 } else {
674 // Ok, no discs in sight. Spin the hopper up by 150% of it's full
675 // range and verify that we don't see anything.
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800676 const double hopper_clear_verification_position =
Austin Schuha3e8e032013-03-10 18:43:14 -0700677 ::std::max(upper_open_region_.lower_bound(),
Austin Schuh723770b2013-03-10 13:26:20 -0700678 lower_open_region_.lower_bound()) +
Austin Schuhf60861b2013-03-14 00:14:10 -0700679 ConvertDiscPositionToIndex(kIndexFreeLength) * 1.5;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800680
681 wrist_loop_->R << hopper_clear_verification_position, 0.0;
682 if (::std::abs(wrist_loop_->X_hat(0, 0) -
683 hopper_clear_verification_position) <
684 ConvertDiscPositionToIndex(0.05)) {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800685 // We are at the end of the range. There are no more discs here.
686 while (frisbees_.size() > 0) {
687 LOG(ERROR, "Dropping an extra disc since it can't exist\n");
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700688 LOG(ERROR, "Upper is [%f %f]\n",
689 upper_open_region_.upper_bound(),
690 upper_open_region_.lower_bound());
691 LOG(ERROR, "Lower is [%f %f]\n",
692 lower_open_region_.upper_bound(),
693 lower_open_region_.lower_bound());
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800694 frisbees_.pop_back();
695 --hopper_disc_count_;
696 --total_disc_count_;
697 }
698 if (hopper_disc_count_ != 0) {
699 LOG(ERROR,
700 "Emptied the hopper out but there are still discs there\n");
701 }
702 }
703 }
704 }
705
706 {
707 const double hopper_clear_verification_position =
Austin Schuha3e8e032013-03-10 18:43:14 -0700708 ::std::max(upper_open_region_.lower_bound(),
Austin Schuh723770b2013-03-10 13:26:20 -0700709 lower_open_region_.lower_bound()) +
Austin Schuhf60861b2013-03-14 00:14:10 -0700710 ConvertDiscPositionToIndex(kIndexFreeLength) * 1.5;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800711
712 if (wrist_loop_->X_hat(0, 0) >
713 hopper_clear_verification_position +
714 ConvertDiscPositionToIndex(0.05)) {
715 // We are at the end of the range. There are no more discs here.
716 while (frisbees_.size() > 0) {
717 LOG(ERROR, "Dropping an extra disc since it can't exist\n");
Austin Schuha3e8e032013-03-10 18:43:14 -0700718 LOG(ERROR, "Upper is [%f %f]\n",
719 upper_open_region_.upper_bound(),
720 upper_open_region_.lower_bound());
721 LOG(ERROR, "Lower is [%f %f]\n",
722 lower_open_region_.upper_bound(),
723 lower_open_region_.lower_bound());
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800724 frisbees_.pop_back();
725 --hopper_disc_count_;
726 --total_disc_count_;
727 }
Austin Schuhd3d0fbf2013-03-14 00:37:00 -0700728 if (hopper_disc_count_ > 0) {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800729 LOG(ERROR,
Austin Schuhf60861b2013-03-14 00:14:10 -0700730 "Emptied the hopper out but there are still %d discs there\n",
731 hopper_disc_count_);
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800732 }
733 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800734 }
735
Austin Schuha3e8e032013-03-10 18:43:14 -0700736 LOG(DEBUG, "READY_SHOOTER or SHOOT\n");
Austin Schuhd78ab542013-03-01 22:22:19 -0800737 break;
Austin Schuhf8c52252013-03-03 02:25:49 -0800738 }
739
Austin Schuhd3d0fbf2013-03-14 00:37:00 -0700740 // Wait for a period of time to make sure that the disc gets sucked
741 // in properly. We need to do this regardless of what the indexer is doing.
742 for (auto frisbee = frisbees_.begin();
743 frisbee != frisbees_.end(); ++frisbee) {
744 if (now - frisbee->bottom_negedge_time_ < kTransferOffDelay) {
745 transfer_voltage = 12.0;
746 }
747 }
748
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700749 // If we have 4 discs, it is time to preload.
750 if (safe_to_change_state && hopper_disc_count_ >= 4) {
751 switch (safe_goal_) {
752 case Goal::HOLD:
753 case Goal::READY_LOWER:
754 case Goal::INTAKE:
755 safe_goal_ = Goal::READY_SHOOTER;
756 safe_to_change_state = false;
Brian Silvermana0238ce2013-03-13 17:58:45 -0700757 LOG(INFO, "We have %"PRId32" discs, time to preload automatically\n",
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700758 hopper_disc_count_);
759 break;
760 case Goal::READY_SHOOTER:
761 case Goal::SHOOT:
762 break;
763 }
764 }
765
Austin Schuhf8c52252013-03-03 02:25:49 -0800766 // The only way out of the loader is to shoot the disc. The FSM can only go
767 // forwards.
768 switch (loader_state_) {
769 case LoaderState::READY:
Austin Schuha3e8e032013-03-10 18:43:14 -0700770 LOG(DEBUG, "Loader READY\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800771 // Open and down, ready to accept a disc.
772 loader_up_ = false;
773 disc_clamped_ = false;
774 disc_ejected_ = false;
775 if (loader_goal_ == LoaderGoal::GRAB ||
776 loader_goal_ == LoaderGoal::SHOOT_AND_RESET) {
777 if (loader_goal_ == LoaderGoal::GRAB) {
Austin Schuha3e8e032013-03-10 18:43:14 -0700778 LOG(INFO, "Told to GRAB, moving on\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800779 } else {
Austin Schuha3e8e032013-03-10 18:43:14 -0700780 LOG(INFO, "Told to SHOOT_AND_RESET, moving on\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800781 }
782 loader_state_ = LoaderState::GRABBING;
783 loader_countdown_ = kGrabbingDelay;
784 } else {
785 break;
786 }
787 case LoaderState::GRABBING:
Austin Schuha3e8e032013-03-10 18:43:14 -0700788 LOG(DEBUG, "Loader GRABBING %d\n", loader_countdown_);
Austin Schuhf8c52252013-03-03 02:25:49 -0800789 // Closing the grabber.
790 loader_up_ = false;
791 disc_clamped_ = true;
792 disc_ejected_ = false;
793 if (loader_countdown_ > 0) {
794 --loader_countdown_;
795 break;
796 } else {
797 loader_state_ = LoaderState::GRABBED;
798 }
799 case LoaderState::GRABBED:
Austin Schuha3e8e032013-03-10 18:43:14 -0700800 LOG(DEBUG, "Loader GRABBED\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800801 // Grabber closed.
802 loader_up_ = false;
803 disc_clamped_ = true;
804 disc_ejected_ = false;
805 if (loader_goal_ == LoaderGoal::SHOOT_AND_RESET) {
Austin Schuha3e8e032013-03-10 18:43:14 -0700806 shooter.status.FetchLatest();
807 if (shooter.status.get()) {
808 // TODO(aschuh): If we aren't shooting nicely, wait until the shooter
809 // is up to speed rather than just spinning.
Austin Schuhf60861b2013-03-14 00:14:10 -0700810 if (shooter.status->average_velocity > 130 && shooter.status->ready) {
Austin Schuha3e8e032013-03-10 18:43:14 -0700811 loader_state_ = LoaderState::LIFTING;
812 loader_countdown_ = kLiftingDelay;
813 LOG(INFO, "Told to SHOOT_AND_RESET, moving on\n");
814 } else {
815 LOG(WARNING, "Told to SHOOT_AND_RESET, shooter too slow at %f\n",
816 shooter.status->average_velocity);
817 break;
818 }
819 } else {
820 LOG(ERROR, "Told to SHOOT_AND_RESET, no shooter data, moving on.\n");
821 loader_state_ = LoaderState::LIFTING;
822 loader_countdown_ = kLiftingDelay;
823 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800824 } else if (loader_goal_ == LoaderGoal::READY) {
825 LOG(ERROR, "Can't go to ready when we have something grabbed.\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800826 break;
827 } else {
828 break;
829 }
830 case LoaderState::LIFTING:
Austin Schuha3e8e032013-03-10 18:43:14 -0700831 LOG(DEBUG, "Loader LIFTING %d\n", loader_countdown_);
Austin Schuhf8c52252013-03-03 02:25:49 -0800832 // Lifting the disc.
833 loader_up_ = true;
834 disc_clamped_ = true;
835 disc_ejected_ = false;
836 if (loader_countdown_ > 0) {
837 --loader_countdown_;
838 break;
839 } else {
840 loader_state_ = LoaderState::LIFTED;
841 }
842 case LoaderState::LIFTED:
Austin Schuha3e8e032013-03-10 18:43:14 -0700843 LOG(DEBUG, "Loader LIFTED\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800844 // Disc lifted. Time to eject it out.
845 loader_up_ = true;
846 disc_clamped_ = true;
847 disc_ejected_ = false;
848 loader_state_ = LoaderState::SHOOTING;
849 loader_countdown_ = kShootingDelay;
850 case LoaderState::SHOOTING:
Austin Schuha3e8e032013-03-10 18:43:14 -0700851 LOG(DEBUG, "Loader SHOOTING %d\n", loader_countdown_);
Austin Schuhf8c52252013-03-03 02:25:49 -0800852 // Ejecting the disc into the shooter.
853 loader_up_ = true;
854 disc_clamped_ = false;
855 disc_ejected_ = true;
856 if (loader_countdown_ > 0) {
857 --loader_countdown_;
858 break;
859 } else {
860 loader_state_ = LoaderState::SHOOT;
861 }
862 case LoaderState::SHOOT:
Austin Schuha3e8e032013-03-10 18:43:14 -0700863 LOG(DEBUG, "Loader SHOOT\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800864 // The disc has been shot.
865 loader_up_ = true;
866 disc_clamped_ = false;
867 disc_ejected_ = true;
868 loader_state_ = LoaderState::LOWERING;
869 loader_countdown_ = kLoweringDelay;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800870 --hopper_disc_count_;
Austin Schuh70be1ba2013-03-10 13:37:17 -0700871 ++shot_disc_count_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800872 case LoaderState::LOWERING:
Austin Schuha3e8e032013-03-10 18:43:14 -0700873 LOG(DEBUG, "Loader LOWERING %d\n", loader_countdown_);
Austin Schuhf8c52252013-03-03 02:25:49 -0800874 // Lowering the loader back down.
875 loader_up_ = false;
876 disc_clamped_ = false;
877 disc_ejected_ = true;
878 if (loader_countdown_ > 0) {
879 --loader_countdown_;
880 break;
881 } else {
882 loader_state_ = LoaderState::LOWERED;
883 }
884 case LoaderState::LOWERED:
Austin Schuha3e8e032013-03-10 18:43:14 -0700885 LOG(DEBUG, "Loader LOWERED\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800886 // The indexer is lowered.
887 loader_up_ = false;
888 disc_clamped_ = false;
889 disc_ejected_ = false;
890 loader_state_ = LoaderState::READY;
891 // Once we have shot, we need to hang out in READY until otherwise
892 // notified.
893 loader_goal_ = LoaderGoal::READY;
Austin Schuhd78ab542013-03-01 22:22:19 -0800894 break;
895 }
896
897 // Update the observer.
898 wrist_loop_->Update(position != NULL, output == NULL);
899
900 if (position) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800901 LOG(DEBUG, "pos=%f\n", position->index_position);
Austin Schuhd78ab542013-03-01 22:22:19 -0800902 last_bottom_disc_detect_ = position->bottom_disc_detect;
Austin Schuh825bde92013-03-06 00:16:46 -0800903 last_top_disc_detect_ = position->top_disc_detect;
Austin Schuh6328daf2013-03-05 00:53:15 -0800904 last_bottom_disc_posedge_count_ = position->bottom_disc_posedge_count;
905 last_bottom_disc_negedge_count_ = position->bottom_disc_negedge_count;
906 last_bottom_disc_negedge_wait_count_ =
907 position->bottom_disc_negedge_wait_count;
Austin Schuh825bde92013-03-06 00:16:46 -0800908 last_top_disc_posedge_count_ = position->top_disc_posedge_count;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800909 last_top_disc_negedge_count_ = position->top_disc_negedge_count;
Austin Schuhd78ab542013-03-01 22:22:19 -0800910 }
911
912 status->hopper_disc_count = hopper_disc_count_;
913 status->total_disc_count = total_disc_count_;
Austin Schuh70be1ba2013-03-10 13:37:17 -0700914 status->shot_disc_count = shot_disc_count_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800915 status->preloaded = (loader_state_ != LoaderState::READY);
Austin Schuhd78ab542013-03-01 22:22:19 -0800916
917 if (output) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800918 output->intake_voltage = intake_voltage;
Austin Schuhf8c52252013-03-03 02:25:49 -0800919 output->transfer_voltage = transfer_voltage;
Austin Schuhd78ab542013-03-01 22:22:19 -0800920 output->index_voltage = wrist_loop_->U(0, 0);
Austin Schuhf8c52252013-03-03 02:25:49 -0800921 output->loader_up = loader_up_;
922 output->disc_clamped = disc_clamped_;
923 output->disc_ejected = disc_ejected_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800924 }
925
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700926 if (safe_to_change_state) {
Austin Schuhd78ab542013-03-01 22:22:19 -0800927 safe_goal_ = goal_enum;
928 }
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800929 if (hopper_disc_count_ < 0) {
930 LOG(ERROR, "NEGATIVE DISCS. VERY VERY BAD\n");
931 }
Austin Schuhd78ab542013-03-01 22:22:19 -0800932}
933
934} // namespace control_loops
935} // namespace frc971