blob: 3191729171988e4b304836f1f8efdd375594189c [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 Schuhd78ab542013-03-01 22:22:19 -080016
17using ::aos::time::Time;
18
19namespace frc971 {
20namespace control_loops {
21
Austin Schuhdff24e22013-03-06 00:41:21 -080022double IndexMotor::Frisbee::ObserveNoTopDiscSensor(
Austin Schuh825bde92013-03-06 00:16:46 -080023 double index_position, double index_velocity) {
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 Schuh825bde92013-03-06 00:16:46 -080034 if (::std::abs(index_velocity) < 100) {
35 if (distance_to_above < distance_to_below) {
Austin Schuhdff24e22013-03-06 00:41:21 -080036 printf("Moving disc to top slow.\n");
Austin Schuh825bde92013-03-06 00:16:46 -080037 // Move it up.
Austin Schuhdff24e22013-03-06 00:41:21 -080038 index_start_position_ -= distance_to_above;
39 return -distance_to_above;
Austin Schuh825bde92013-03-06 00:16:46 -080040 } else {
Austin Schuhdff24e22013-03-06 00:41:21 -080041 printf("Moving disc to bottom slow.\n");
42 index_start_position_ += distance_to_below;
43 return distance_to_below;
Austin Schuh825bde92013-03-06 00:16:46 -080044 }
45 } else {
46 if (index_velocity > 0) {
47 // Now going up. If we didn't see it before, and we don't see it
48 // now but it should be in view, it must still be below. If it were
49 // above, it would be going further away from us.
Austin Schuh1b864a12013-03-07 00:46:50 -080050 printf("Moving fast up, shifting disc down. Disc was at %f\n",
51 absolute_position(index_position));
Austin Schuhdff24e22013-03-06 00:41:21 -080052 index_start_position_ += distance_to_below;
Austin Schuh1b864a12013-03-07 00:46:50 -080053 printf("Moving fast up, shifting disc down. Disc now at %f\n",
54 absolute_position(index_position));
Austin Schuhdff24e22013-03-06 00:41:21 -080055 return distance_to_below;
Austin Schuh825bde92013-03-06 00:16:46 -080056 } else {
Austin Schuh1b864a12013-03-07 00:46:50 -080057 printf("Moving fast down, shifting disc up. Disc was at %f\n",
58 absolute_position(index_position));
Austin Schuhdff24e22013-03-06 00:41:21 -080059 index_start_position_ -= distance_to_above;
Austin Schuh1b864a12013-03-07 00:46:50 -080060 printf("Moving fast down, shifting disc up. Disc now at %f\n",
61 absolute_position(index_position));
Austin Schuhdff24e22013-03-06 00:41:21 -080062 return -distance_to_above;
Austin Schuh825bde92013-03-06 00:16:46 -080063 }
64 }
65 }
Austin Schuhdff24e22013-03-06 00:41:21 -080066 return 0.0;
Austin Schuh825bde92013-03-06 00:16:46 -080067}
68
Austin Schuhd78ab542013-03-01 22:22:19 -080069IndexMotor::IndexMotor(control_loops::IndexLoop *my_index)
70 : aos::control_loops::ControlLoop<control_loops::IndexLoop>(my_index),
Austin Schuh93485832013-03-04 00:01:34 -080071 wrist_loop_(new IndexStateFeedbackLoop(MakeIndexLoop())),
Austin Schuhd78ab542013-03-01 22:22:19 -080072 hopper_disc_count_(0),
73 total_disc_count_(0),
Austin Schuhf8c52252013-03-03 02:25:49 -080074 safe_goal_(Goal::HOLD),
75 loader_goal_(LoaderGoal::READY),
76 loader_state_(LoaderState::READY),
Austin Schuhd78ab542013-03-01 22:22:19 -080077 loader_up_(false),
78 disc_clamped_(false),
79 disc_ejected_(false),
Austin Schuhbcdb90c2013-03-03 23:24:58 -080080 last_bottom_disc_detect_(false),
Austin Schuh825bde92013-03-06 00:16:46 -080081 last_top_disc_detect_(false),
Austin Schuhbcdb90c2013-03-03 23:24:58 -080082 no_prior_position_(true),
Austin Schuh723770b2013-03-10 13:26:20 -070083 missing_position_count_(0) {
Austin Schuhd78ab542013-03-01 22:22:19 -080084}
85
Austin Schuhf8c52252013-03-03 02:25:49 -080086/*static*/ const double IndexMotor::kTransferStartPosition = 0.0;
87/*static*/ const double IndexMotor::kIndexStartPosition = 0.2159;
88/*static*/ const double IndexMotor::kIndexFreeLength =
89 IndexMotor::ConvertDiscAngleToDiscPosition((360 * 2 + 14) * M_PI / 180);
90/*static*/ const double IndexMotor::kLoaderFreeStopPosition =
91 kIndexStartPosition + kIndexFreeLength;
Austin Schuh1b864a12013-03-07 00:46:50 -080092/*static*/ const double IndexMotor::kReadyToPreload =
93 kLoaderFreeStopPosition - ConvertDiscAngleToDiscPosition(M_PI / 6.0);
Austin Schuhf8c52252013-03-03 02:25:49 -080094/*static*/ const double IndexMotor::kReadyToLiftPosition =
95 kLoaderFreeStopPosition + 0.2921;
96/*static*/ const double IndexMotor::kGrabberLength = 0.03175;
97/*static*/ const double IndexMotor::kGrabberStartPosition =
98 kReadyToLiftPosition - kGrabberLength;
Austin Schuh6328daf2013-03-05 00:53:15 -080099/*static*/ const double IndexMotor::kGrabberMovementVelocity = 0.7;
Austin Schuhf8c52252013-03-03 02:25:49 -0800100/*static*/ const double IndexMotor::kLifterStopPosition =
101 kReadyToLiftPosition + 0.161925;
102/*static*/ const double IndexMotor::kLifterMovementVelocity = 1.0;
103/*static*/ const double IndexMotor::kEjectorStopPosition =
104 kLifterStopPosition + 0.01;
105/*static*/ const double IndexMotor::kEjectorMovementVelocity = 1.0;
Austin Schuhcc297022013-03-09 23:26:40 -0800106/*static*/ const double IndexMotor::kBottomDiscDetectStart = 0.00;
107/*static*/ const double IndexMotor::kBottomDiscDetectStop = 0.13;
108/*static*/ const double IndexMotor::kBottomDiscIndexDelay = 0.032;
Austin Schuhf8c52252013-03-03 02:25:49 -0800109
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800110// TODO(aschuh): Verify these with the sensor actually on.
Austin Schuh825bde92013-03-06 00:16:46 -0800111/*static*/ const double IndexMotor::kTopDiscDetectStart =
112 (IndexMotor::kLoaderFreeStopPosition -
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800113 IndexMotor::ConvertDiscAngleToDiscPosition(49 * M_PI / 180));
Austin Schuh825bde92013-03-06 00:16:46 -0800114/*static*/ const double IndexMotor::kTopDiscDetectStop =
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800115 (IndexMotor::kLoaderFreeStopPosition +
116 IndexMotor::ConvertDiscAngleToDiscPosition(19 * M_PI / 180));
117
118// I measured the angle between 2 discs. That then gives me the distance
119// between 2 posedges (or negedges). Then subtract off the width of the
120// positive pulse, and that gives the width of the negative pulse.
121/*static*/ const double IndexMotor::kTopDiscDetectMinSeperation =
122 (IndexMotor::ConvertDiscAngleToDiscPosition(120 * M_PI / 180) -
123 (IndexMotor::kTopDiscDetectStop - IndexMotor::kTopDiscDetectStart));
Austin Schuhf8c52252013-03-03 02:25:49 -0800124
Austin Schuhd78ab542013-03-01 22:22:19 -0800125const /*static*/ double IndexMotor::kDiscRadius = 10.875 * 0.0254 / 2;
126const /*static*/ double IndexMotor::kRollerRadius = 2.0 * 0.0254 / 2;
Austin Schuhf8c52252013-03-03 02:25:49 -0800127const /*static*/ double IndexMotor::kTransferRollerRadius = 1.25 * 0.0254 / 2;
Austin Schuhd78ab542013-03-01 22:22:19 -0800128
Austin Schuhf8c52252013-03-03 02:25:49 -0800129/*static*/ const int IndexMotor::kGrabbingDelay = 5;
130/*static*/ const int IndexMotor::kLiftingDelay = 20;
131/*static*/ const int IndexMotor::kShootingDelay = 5;
132/*static*/ const int IndexMotor::kLoweringDelay = 20;
Austin Schuhd78ab542013-03-01 22:22:19 -0800133
Austin Schuh93485832013-03-04 00:01:34 -0800134// TODO(aschuh): Tune these.
135/*static*/ const double
136 IndexMotor::IndexStateFeedbackLoop::kMinMotionVoltage = 5.0;
137/*static*/ const double
138 IndexMotor::IndexStateFeedbackLoop::kNoMotionCuttoffCount = 30;
139
Austin Schuhd78ab542013-03-01 22:22:19 -0800140// Distance to move the indexer when grabbing a disc.
141const double kNextPosition = 10.0;
142
143/*static*/ double IndexMotor::ConvertDiscAngleToIndex(const double angle) {
144 return (angle * (1 + (kDiscRadius * 2 + kRollerRadius) / kRollerRadius));
145}
146
Austin Schuhf8c52252013-03-03 02:25:49 -0800147/*static*/ double IndexMotor::ConvertDiscAngleToDiscPosition(
148 const double angle) {
Austin Schuhd78ab542013-03-01 22:22:19 -0800149 return angle * (kDiscRadius + kRollerRadius);
150}
151
Austin Schuhf8c52252013-03-03 02:25:49 -0800152/*static*/ double IndexMotor::ConvertDiscPositionToDiscAngle(
153 const double position) {
154 return position / (kDiscRadius + kRollerRadius);
155}
156
Austin Schuhd78ab542013-03-01 22:22:19 -0800157/*static*/ double IndexMotor::ConvertIndexToDiscAngle(const double angle) {
158 return (angle / (1 + (kDiscRadius * 2 + kRollerRadius) / kRollerRadius));
159}
160
161/*static*/ double IndexMotor::ConvertIndexToDiscPosition(const double angle) {
162 return IndexMotor::ConvertDiscAngleToDiscPosition(
163 ConvertIndexToDiscAngle(angle));
164}
165
Austin Schuhf8c52252013-03-03 02:25:49 -0800166/*static*/ double IndexMotor::ConvertTransferToDiscPosition(
167 const double angle) {
168 const double gear_ratio = (1 + (kDiscRadius * 2 + kTransferRollerRadius) /
169 kTransferRollerRadius);
170 return angle / gear_ratio * (kDiscRadius + kTransferRollerRadius);
171}
172
173/*static*/ double IndexMotor::ConvertDiscPositionToIndex(
174 const double position) {
175 return IndexMotor::ConvertDiscAngleToIndex(
176 ConvertDiscPositionToDiscAngle(position));
177}
178
Austin Schuh1b864a12013-03-07 00:46:50 -0800179bool IndexMotor::MinDiscPosition(double *disc_position, Frisbee **found_disc) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800180 bool found_start = false;
181 for (unsigned int i = 0; i < frisbees_.size(); ++i) {
Austin Schuh1b864a12013-03-07 00:46:50 -0800182 Frisbee &frisbee = frisbees_[i];
Austin Schuhf8c52252013-03-03 02:25:49 -0800183 if (!found_start) {
184 if (frisbee.has_position()) {
185 *disc_position = frisbee.position();
Austin Schuh1b864a12013-03-07 00:46:50 -0800186 if (found_disc) {
187 *found_disc = &frisbee;
188 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800189 found_start = true;
190 }
191 } else {
Austin Schuh1b864a12013-03-07 00:46:50 -0800192 if (frisbee.position() <= *disc_position) {
193 *disc_position = frisbee.position();
194 if (found_disc) {
195 *found_disc = &frisbee;
196 }
197 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800198 }
199 }
200 return found_start;
201}
202
Austin Schuh1b864a12013-03-07 00:46:50 -0800203bool IndexMotor::MaxDiscPosition(double *disc_position, Frisbee **found_disc) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800204 bool found_start = false;
205 for (unsigned int i = 0; i < frisbees_.size(); ++i) {
Austin Schuh1b864a12013-03-07 00:46:50 -0800206 Frisbee &frisbee = frisbees_[i];
Austin Schuhf8c52252013-03-03 02:25:49 -0800207 if (!found_start) {
208 if (frisbee.has_position()) {
209 *disc_position = frisbee.position();
Austin Schuh1b864a12013-03-07 00:46:50 -0800210 if (found_disc) {
211 *found_disc = &frisbee;
212 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800213 found_start = true;
214 }
215 } else {
Austin Schuh1b864a12013-03-07 00:46:50 -0800216 if (frisbee.position() > *disc_position) {
217 *disc_position = frisbee.position();
218 if (found_disc) {
219 *found_disc = &frisbee;
220 }
221 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800222 }
223 }
224 return found_start;
225}
226
Austin Schuh93485832013-03-04 00:01:34 -0800227void IndexMotor::IndexStateFeedbackLoop::CapU() {
228 // If the voltage has been low for a large number of cycles, cut the motor
229 // power. This is generally very bad controls practice since this isn't LTI,
230 // but we don't really care about tracking anything other than large step
231 // inputs, and the loader doesn't need to be that accurate.
232 if (::std::abs(U(0, 0)) < kMinMotionVoltage) {
233 ++low_voltage_count_;
234 if (low_voltage_count_ > kNoMotionCuttoffCount) {
235 printf("Limiting power from %f to 0\n", U(0, 0));
236 U(0, 0) = 0.0;
237 }
238 } else {
239 low_voltage_count_ = 0;
240 }
241
242 for (int i = 0; i < kNumOutputs; ++i) {
243 if (U[i] > plant.U_max[i]) {
244 U[i] = plant.U_max[i];
245 } else if (U[i] < plant.U_min[i]) {
246 U[i] = plant.U_min[i];
247 }
248 }
249}
250
251
Austin Schuhd78ab542013-03-01 22:22:19 -0800252// Positive angle is towards the shooter, and positive power is towards the
253// shooter.
254void IndexMotor::RunIteration(
255 const control_loops::IndexLoop::Goal *goal,
256 const control_loops::IndexLoop::Position *position,
257 control_loops::IndexLoop::Output *output,
258 control_loops::IndexLoop::Status *status) {
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800259 // Make goal easy to work with and sanity check it.
Austin Schuhd78ab542013-03-01 22:22:19 -0800260 Goal goal_enum = static_cast<Goal>(goal->goal_state);
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800261 if (goal->goal_state < 0 || goal->goal_state > 4) {
Brian Silverman94195052013-03-09 13:45:05 -0800262 LOG(ERROR, "Goal state is %"PRId32" which is out of range. Going to HOLD.\n",
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800263 goal->goal_state);
264 goal_enum = Goal::HOLD;
265 }
Austin Schuhd78ab542013-03-01 22:22:19 -0800266
267 // Disable the motors now so that all early returns will return with the
268 // motors disabled.
Austin Schuhb6d898b2013-03-03 15:34:35 -0800269 double intake_voltage = 0.0;
Austin Schuhf8c52252013-03-03 02:25:49 -0800270 double transfer_voltage = 0.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800271 if (output) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800272 output->intake_voltage = 0.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800273 output->transfer_voltage = 0.0;
274 output->index_voltage = 0.0;
275 }
276
277 status->ready_to_intake = false;
278
Austin Schuhf8c52252013-03-03 02:25:49 -0800279 // Compute a safe index position that we can use.
Austin Schuhd78ab542013-03-01 22:22:19 -0800280 if (position) {
281 wrist_loop_->Y << position->index_position;
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800282 // Set the goal to be the current position if this is the first time through
283 // so we don't always spin the indexer to the 0 position before starting.
284 if (no_prior_position_) {
285 wrist_loop_->R << wrist_loop_->Y(0, 0), 0.0;
Austin Schuhc5ef1bb2013-03-10 00:42:05 -0800286 wrist_loop_->X_hat(0, 0) = wrist_loop_->Y(0, 0);
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800287 no_prior_position_ = false;
Austin Schuh6328daf2013-03-05 00:53:15 -0800288 last_bottom_disc_posedge_count_ = position->bottom_disc_posedge_count;
289 last_bottom_disc_negedge_count_ = position->bottom_disc_negedge_count;
290 last_bottom_disc_negedge_wait_count_ =
291 position->bottom_disc_negedge_wait_count;
Austin Schuh825bde92013-03-06 00:16:46 -0800292 last_top_disc_posedge_count_ = position->top_disc_posedge_count;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800293 last_top_disc_negedge_count_ = position->top_disc_negedge_count;
294 // The open positions for the upper is right here and isn't a hard edge.
Austin Schuh723770b2013-03-10 13:26:20 -0700295 upper_open_region_.Restart(wrist_loop_->Y(0, 0));
296 lower_open_region_.Restart(wrist_loop_->Y(0, 0));
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800297 }
298
Austin Schuh1b864a12013-03-07 00:46:50 -0800299 // If the cRIO is gone for over 1/2 of a second, assume that it rebooted.
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800300 if (missing_position_count_ > 50) {
Austin Schuh6328daf2013-03-05 00:53:15 -0800301 last_bottom_disc_posedge_count_ = position->bottom_disc_posedge_count;
302 last_bottom_disc_negedge_count_ = position->bottom_disc_negedge_count;
303 last_bottom_disc_negedge_wait_count_ =
304 position->bottom_disc_negedge_wait_count;
Austin Schuh825bde92013-03-06 00:16:46 -0800305 last_top_disc_posedge_count_ = position->top_disc_posedge_count;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800306 last_top_disc_negedge_count_ = position->top_disc_negedge_count;
307 // We can't really trust the open range any more if the crio rebooted.
Austin Schuh723770b2013-03-10 13:26:20 -0700308 upper_open_region_.Restart(wrist_loop_->Y(0, 0));
309 lower_open_region_.Restart(wrist_loop_->Y(0, 0));
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800310 // Adjust the disc positions so that they don't have to move.
311 const double disc_offset =
312 position->index_position - wrist_loop_->X_hat(0, 0);
313 for (auto frisbee = frisbees_.begin();
314 frisbee != frisbees_.end(); ++frisbee) {
315 frisbee->OffsetDisc(disc_offset);
316 }
Austin Schuhc5ef1bb2013-03-10 00:42:05 -0800317 wrist_loop_->X_hat(0, 0) = wrist_loop_->Y(0, 0);
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800318 }
319 missing_position_count_ = 0;
320 } else {
321 ++missing_position_count_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800322 }
323 const double index_position = wrist_loop_->X_hat(0, 0);
324
Austin Schuh825bde92013-03-06 00:16:46 -0800325 if (position) {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800326 // Reset the open region if we saw a negedge.
Austin Schuh723770b2013-03-10 13:26:20 -0700327 if (position->bottom_disc_negedge_wait_count !=
328 last_bottom_disc_negedge_wait_count_) {
329 // Saw a negedge, must be a new region.
330 lower_open_region_.Restart(position->bottom_disc_negedge_wait_position);
331 }
332 // Reset the open region if we saw a negedge.
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800333 if (position->top_disc_negedge_count != last_top_disc_negedge_count_) {
334 // Saw a negedge, must be a new region.
Austin Schuh723770b2013-03-10 13:26:20 -0700335 upper_open_region_.Restart(position->top_disc_negedge_position);
336 }
337
338 // No disc. Expand the open region.
339 if (!position->bottom_disc_detect) {
340 lower_open_region_.Expand(index_position);
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800341 }
342
343 // No disc. Expand the open region.
344 if (!position->top_disc_detect) {
Austin Schuh723770b2013-03-10 13:26:20 -0700345 upper_open_region_.Expand(index_position);
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800346 }
347
Austin Schuh825bde92013-03-06 00:16:46 -0800348 if (!position->top_disc_detect) {
349 // We don't see a disc. Verify that there are no discs that we should be
350 // seeing.
Austin Schuh1b864a12013-03-07 00:46:50 -0800351 // Assume that discs will move slow enough that we won't miss one as it
352 // goes by. They will either pile up above or below the sensor.
Austin Schuhdff24e22013-03-06 00:41:21 -0800353
354 double cumulative_offset = 0.0;
355 for (auto frisbee = frisbees_.rbegin(), rend = frisbees_.rend();
356 frisbee != rend; ++frisbee) {
357 frisbee->OffsetDisc(cumulative_offset);
358 double amount_moved = frisbee->ObserveNoTopDiscSensor(
Austin Schuh825bde92013-03-06 00:16:46 -0800359 wrist_loop_->X_hat(0, 0), wrist_loop_->X_hat(1, 0));
Austin Schuhdff24e22013-03-06 00:41:21 -0800360 cumulative_offset += amount_moved;
Austin Schuh825bde92013-03-06 00:16:46 -0800361 }
362 }
Austin Schuh1b864a12013-03-07 00:46:50 -0800363
Austin Schuh825bde92013-03-06 00:16:46 -0800364 if (position->top_disc_posedge_count != last_top_disc_posedge_count_) {
Austin Schuh1b864a12013-03-07 00:46:50 -0800365 const double index_position = wrist_loop_->X_hat(0, 0) -
366 position->index_position + position->top_disc_posedge_position;
Austin Schuh825bde92013-03-06 00:16:46 -0800367 // TODO(aschuh): Sanity check this number...
368 // Requires storing when the disc was last seen with the sensor off, and
369 // figuring out what to do if things go south.
370
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800371 // 1 if discs are going up, 0 if we have no clue, and -1 if they are going
372 // down.
373 int disc_direction = 0;
Austin Schuh825bde92013-03-06 00:16:46 -0800374 if (wrist_loop_->X_hat(1, 0) > 50.0) {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800375 disc_direction = 1;
376 } else if (wrist_loop_->X_hat(1, 0) < -50.0) {
377 disc_direction = -1;
378 } else {
379 // Save the upper and lower positions that we last saw a disc at.
380 // If there is a big buffer above, must be a disc from below.
381 // If there is a big buffer below, must be a disc from above.
382 // This should work to replace the velocity threshold above.
383
Austin Schuh723770b2013-03-10 13:26:20 -0700384 const double open_width = upper_open_region_.width();
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800385 const double relative_upper_open_precentage =
Austin Schuh723770b2013-03-10 13:26:20 -0700386 (upper_open_region_.upper_bound() - index_position) / open_width;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800387 const double relative_lower_open_precentage =
Austin Schuh723770b2013-03-10 13:26:20 -0700388 (index_position - upper_open_region_.lower_bound()) / open_width;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800389 printf("Width %f upper %f lower %f\n",
390 open_width, relative_upper_open_precentage,
391 relative_lower_open_precentage);
392
393 if (ConvertIndexToDiscPosition(open_width) <
394 kTopDiscDetectMinSeperation * 0.9) {
395 LOG(ERROR, "Discs are way too close to each other. Doing nothing\n");
396 } else if (relative_upper_open_precentage > 0.75) {
397 // Looks like it is a disc going down from above since we are near
398 // the upper edge.
399 disc_direction = -1;
400 printf("Disc edge going down\n");
401 } else if (relative_lower_open_precentage > 0.75) {
402 // Looks like it is a disc going up from below since we are near
403 // the lower edge.
404 disc_direction = 1;
405 printf("Disc edge going up\n");
406 } else {
407 LOG(ERROR,
408 "Got an edge in the middle of what should be an open region.\n");
409 LOG(ERROR, "Open width: %f upper precentage %f %%\n",
410 open_width, relative_upper_open_precentage);
411 }
412 }
413
414 if (disc_direction > 0) {
Austin Schuh825bde92013-03-06 00:16:46 -0800415 // Moving up at a reasonable clip.
Austin Schuh1b864a12013-03-07 00:46:50 -0800416 // Find the highest disc that is below the top disc sensor.
417 // While we are at it, count the number above and log an error if there
418 // are too many.
419 if (frisbees_.size() == 0) {
420 Frisbee new_frisbee;
421 new_frisbee.has_been_indexed_ = true;
422 new_frisbee.index_start_position_ = index_position -
423 ConvertDiscPositionToIndex(kTopDiscDetectStart -
424 kIndexStartPosition);
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800425 ++hopper_disc_count_;
426 ++total_disc_count_;
Austin Schuh1b864a12013-03-07 00:46:50 -0800427 frisbees_.push_front(new_frisbee);
428 LOG(WARNING, "Added a disc to the hopper at the top sensor\n");
429 }
430
431 int above_disc_count = 0;
432 double highest_position = 0;
433 Frisbee *highest_frisbee_below_sensor = NULL;
434 for (auto frisbee = frisbees_.rbegin(), rend = frisbees_.rend();
435 frisbee != rend; ++frisbee) {
436 const double disc_position = frisbee->absolute_position(
437 index_position);
438 // It is save to use the top position for the cuttoff, since the
439 // sensor being low will result in discs being pushed off of it.
440 if (disc_position >= kTopDiscDetectStop) {
441 ++above_disc_count;
442 } else if (!highest_frisbee_below_sensor ||
443 disc_position > highest_position) {
444 highest_frisbee_below_sensor = &*frisbee;
445 highest_position = disc_position;
446 }
447 }
448 if (above_disc_count > 1) {
449 LOG(ERROR, "We have 2 discs above the top sensor.\n");
450 }
451
452 // We now have the disc. Shift all the ones below the sensor up by the
453 // computed delta.
454 const double disc_delta = IndexMotor::ConvertDiscPositionToIndex(
455 highest_position - kTopDiscDetectStart);
456 for (auto frisbee = frisbees_.rbegin(), rend = frisbees_.rend();
457 frisbee != rend; ++frisbee) {
458 const double disc_position = frisbee->absolute_position(
459 index_position);
460 if (disc_position < kTopDiscDetectStop) {
461 frisbee->OffsetDisc(disc_delta);
462 }
463 }
464 printf("Currently have %d discs, saw posedge moving up. "
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800465 "Moving down by %f to %f\n", frisbees_.size(),
Austin Schuh1b864a12013-03-07 00:46:50 -0800466 ConvertIndexToDiscPosition(disc_delta),
467 highest_frisbee_below_sensor->absolute_position(
468 wrist_loop_->X_hat(0, 0)));
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800469 } else if (disc_direction < 0) {
Austin Schuh825bde92013-03-06 00:16:46 -0800470 // Moving down at a reasonable clip.
Austin Schuh1b864a12013-03-07 00:46:50 -0800471 // There can only be 1 disc up top that would give us a posedge.
472 // Find it and place it at the one spot that it can be.
Brian Silverman94195052013-03-09 13:45:05 -0800473 double min_disc_position = 0;
Austin Schuh1b864a12013-03-07 00:46:50 -0800474 Frisbee *min_frisbee = NULL;
475 MinDiscPosition(&min_disc_position, &min_frisbee);
476 if (!min_frisbee) {
477 // Uh, oh, we see a disc but there isn't one...
478 LOG(ERROR, "Saw a disc up top but there isn't one in the hopper\n");
479 } else {
480 const double disc_position = min_frisbee->absolute_position(
481 index_position);
482
483 const double disc_delta_meters = disc_position - kTopDiscDetectStop;
484 const double disc_delta = IndexMotor::ConvertDiscPositionToIndex(
485 disc_delta_meters);
486 printf("Posedge going down. Moving top disc down by %f\n",
487 disc_delta_meters);
488 for (auto frisbee = frisbees_.begin(), end = frisbees_.end();
489 frisbee != end; ++frisbee) {
490 frisbee->OffsetDisc(disc_delta);
491 }
492 }
Austin Schuh825bde92013-03-06 00:16:46 -0800493 } else {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800494 LOG(ERROR, "Not sure how to handle the upper posedge, doing nothing\n");
Austin Schuh825bde92013-03-06 00:16:46 -0800495 }
496 }
497 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800498
Austin Schuhf8c52252013-03-03 02:25:49 -0800499 // Bool to track if it is safe for the goal to change yet.
Austin Schuhd78ab542013-03-01 22:22:19 -0800500 bool safe_to_change_state_ = true;
501 switch (safe_goal_) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800502 case Goal::HOLD:
Austin Schuhd78ab542013-03-01 22:22:19 -0800503 // The goal should already be good, so sit tight with everything the same
504 // as it was.
Austin Schuhd78ab542013-03-01 22:22:19 -0800505 break;
Austin Schuhf8c52252013-03-03 02:25:49 -0800506 case Goal::READY_LOWER:
507 case Goal::INTAKE:
Austin Schuhd78ab542013-03-01 22:22:19 -0800508 {
509 Time now = Time::Now();
Austin Schuhd78ab542013-03-01 22:22:19 -0800510 if (position) {
Austin Schuh6328daf2013-03-05 00:53:15 -0800511 // Posedge of the disc entering the beam break.
512 if (position->bottom_disc_posedge_count !=
513 last_bottom_disc_posedge_count_) {
Austin Schuhd78ab542013-03-01 22:22:19 -0800514 transfer_frisbee_.Reset();
515 transfer_frisbee_.bottom_posedge_time_ = now;
516 printf("Posedge of bottom disc %f\n",
517 transfer_frisbee_.bottom_posedge_time_.ToSeconds());
518 ++hopper_disc_count_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800519 ++total_disc_count_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800520 }
521
522 // Disc exited the beam break now.
Austin Schuh6328daf2013-03-05 00:53:15 -0800523 if (position->bottom_disc_negedge_count !=
524 last_bottom_disc_negedge_count_) {
Austin Schuhd78ab542013-03-01 22:22:19 -0800525 transfer_frisbee_.bottom_negedge_time_ = now;
526 printf("Negedge of bottom disc %f\n",
527 transfer_frisbee_.bottom_negedge_time_.ToSeconds());
528 frisbees_.push_front(transfer_frisbee_);
529 }
530
531 if (position->bottom_disc_detect) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800532 intake_voltage = transfer_voltage = 12.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800533 // Must wait until the disc gets out before we can change state.
534 safe_to_change_state_ = false;
535
Austin Schuhf8c52252013-03-03 02:25:49 -0800536 // TODO(aschuh): A disc on the way through needs to start moving
537 // the indexer if it isn't already moving. Maybe?
Austin Schuhd78ab542013-03-01 22:22:19 -0800538
539 Time elapsed_posedge_time = now -
540 transfer_frisbee_.bottom_posedge_time_;
541 if (elapsed_posedge_time >= Time::InSeconds(0.3)) {
542 // It has been too long. The disc must be jammed.
543 LOG(ERROR, "Been way too long. Jammed disc?\n");
544 printf("Been way too long. Jammed disc?\n");
545 }
546 }
547
Austin Schuhf8c52252013-03-03 02:25:49 -0800548 // Check all non-indexed discs and see if they should be indexed.
Austin Schuhb6d898b2013-03-03 15:34:35 -0800549 for (auto frisbee = frisbees_.begin();
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800550 frisbee != frisbees_.end(); ++frisbee) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800551 if (!frisbee->has_been_indexed_) {
552 intake_voltage = transfer_voltage = 12.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800553
Austin Schuh6328daf2013-03-05 00:53:15 -0800554 if (last_bottom_disc_negedge_wait_count_ !=
555 position->bottom_disc_negedge_wait_count) {
556 // We have an index difference.
557 // Save the indexer position, and the time.
558 if (last_bottom_disc_negedge_wait_count_ + 1 !=
559 position->bottom_disc_negedge_wait_count) {
560 LOG(ERROR, "Funny, we got 2 edges since we last checked.\n");
561 }
562
563 // Save the captured position as the position at which the disc
564 // touched the indexer.
Austin Schuhd78ab542013-03-01 22:22:19 -0800565 LOG(INFO, "Grabbed on the index now at %f\n", index_position);
566 printf("Grabbed on the index now at %f\n", index_position);
Austin Schuhb6d898b2013-03-03 15:34:35 -0800567 frisbee->has_been_indexed_ = true;
Austin Schuh6328daf2013-03-05 00:53:15 -0800568 frisbee->index_start_position_ =
569 position->bottom_disc_negedge_wait_position;
Austin Schuhd78ab542013-03-01 22:22:19 -0800570 }
571 }
Austin Schuhb6d898b2013-03-03 15:34:35 -0800572 if (!frisbee->has_been_indexed_) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800573 // All discs must be indexed before it is safe to stop indexing.
Austin Schuhd78ab542013-03-01 22:22:19 -0800574 safe_to_change_state_ = false;
575 }
576 }
577
Austin Schuhf8c52252013-03-03 02:25:49 -0800578 // Figure out where the indexer should be to move the discs down to
579 // the right position.
Brian Silverman94195052013-03-09 13:45:05 -0800580 double max_disc_position = 0;
Austin Schuh1b864a12013-03-07 00:46:50 -0800581 if (MaxDiscPosition(&max_disc_position, NULL)) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800582 printf("There is a disc down here!\n");
583 // TODO(aschuh): Figure out what to do if grabbing the next one
584 // would cause things to jam into the loader.
585 // Say we aren't ready any more. Undefined behavior will result if
586 // that isn't observed.
587 double bottom_disc_position =
588 max_disc_position + ConvertDiscAngleToIndex(M_PI);
589 wrist_loop_->R << bottom_disc_position, 0.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800590
Austin Schuhf8c52252013-03-03 02:25:49 -0800591 // Verify that we are close enough to the goal so that we should be
592 // fine accepting the next disc.
593 double disc_error_meters = ConvertIndexToDiscPosition(
594 wrist_loop_->X_hat(0, 0) - bottom_disc_position);
595 // We are ready for the next disc if the first one is in the first
596 // half circle of the indexer. It will take time for the disc to
597 // come into the indexer, so we will be able to move it out of the
598 // way in time.
599 // This choice also makes sure that we don't claim that we aren't
600 // ready between full speed intaking.
601 if (-ConvertDiscAngleToIndex(M_PI) < disc_error_meters &&
602 disc_error_meters < 0.04) {
603 // We are only ready if we aren't being asked to change state or
604 // are full.
605 status->ready_to_intake =
606 (safe_goal_ == goal_enum) && hopper_disc_count_ < 4;
607 } else {
608 status->ready_to_intake = false;
Austin Schuhd78ab542013-03-01 22:22:19 -0800609 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800610 } else {
611 // No discs! We are always ready for more if we aren't being
612 // asked to change state.
613 status->ready_to_intake = (safe_goal_ == goal_enum);
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800614 printf("Ready to intake, zero discs. %d %d %d\n",
615 status->ready_to_intake, hopper_disc_count_, safe_goal_);
Austin Schuhd78ab542013-03-01 22:22:19 -0800616 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800617
618 // Turn on the transfer roller if we are ready.
619 if (status->ready_to_intake && hopper_disc_count_ < 4 &&
620 safe_goal_ == Goal::INTAKE) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800621 intake_voltage = transfer_voltage = 12.0;
Austin Schuhf8c52252013-03-03 02:25:49 -0800622 }
Austin Schuhd78ab542013-03-01 22:22:19 -0800623 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800624 printf("INTAKE\n");
Austin Schuhd78ab542013-03-01 22:22:19 -0800625 }
626 break;
Austin Schuhf8c52252013-03-03 02:25:49 -0800627 case Goal::READY_SHOOTER:
628 case Goal::SHOOT:
629 // Check if we have any discs to shoot or load and handle them.
Brian Silverman94195052013-03-09 13:45:05 -0800630 double min_disc_position = 0;
Austin Schuh1b864a12013-03-07 00:46:50 -0800631 if (MinDiscPosition(&min_disc_position, NULL)) {
632 const double ready_disc_position = min_disc_position +
633 ConvertDiscPositionToIndex(kReadyToPreload - kIndexStartPosition);
Austin Schuhf8c52252013-03-03 02:25:49 -0800634
635 const double grabbed_disc_position =
636 min_disc_position +
637 ConvertDiscPositionToIndex(kReadyToLiftPosition -
638 kIndexStartPosition + 0.03);
639
640 // Check the state of the loader FSM.
641 // If it is ready to load discs, position the disc so that it is ready
642 // to be grabbed.
643 // If it isn't ready, there is a disc in there. It needs to finish it's
644 // cycle first.
645 if (loader_state_ != LoaderState::READY) {
646 // We already have a disc in the loader.
647 // Stage the discs back a bit.
648 wrist_loop_->R << ready_disc_position, 0.0;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800649 printf("Loader not ready but asked to shoot\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800650
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800651 // Shoot if we are grabbed and being asked to shoot.
652 if (loader_state_ == LoaderState::GRABBED &&
653 safe_goal_ == Goal::SHOOT) {
654 loader_goal_ = LoaderGoal::SHOOT_AND_RESET;
655 }
656
Austin Schuhf8c52252013-03-03 02:25:49 -0800657 // Must wait until it has been grabbed to continue.
658 if (loader_state_ == LoaderState::GRABBING) {
659 safe_to_change_state_ = false;
660 }
661 } else {
662 // No disc up top right now.
663 wrist_loop_->R << grabbed_disc_position, 0.0;
664
665 // See if the disc has gotten pretty far up yet.
666 if (wrist_loop_->X_hat(0, 0) > ready_disc_position) {
667 // Point of no return. We are committing to grabbing it now.
668 safe_to_change_state_ = false;
669 const double robust_grabbed_disc_position =
670 (grabbed_disc_position -
671 ConvertDiscPositionToIndex(kGrabberLength));
672
673 // If close, start grabbing and/or shooting.
674 if (wrist_loop_->X_hat(0, 0) > robust_grabbed_disc_position) {
675 // Start the state machine.
676 if (safe_goal_ == Goal::SHOOT) {
677 loader_goal_ = LoaderGoal::SHOOT_AND_RESET;
678 } else {
679 loader_goal_ = LoaderGoal::GRAB;
680 }
681 // This frisbee is now gone. Take it out of the queue.
682 frisbees_.pop_back();
Austin Schuhf8c52252013-03-03 02:25:49 -0800683 }
684 }
685 }
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800686 } else {
687 if (loader_state_ != LoaderState::READY) {
688 // Shoot if we are grabbed and being asked to shoot.
689 if (loader_state_ == LoaderState::GRABBED &&
690 safe_goal_ == Goal::SHOOT) {
691 loader_goal_ = LoaderGoal::SHOOT_AND_RESET;
692 }
693 } else {
694 // Ok, no discs in sight. Spin the hopper up by 150% of it's full
695 // range and verify that we don't see anything.
696 printf("Moving the indexer to verify that it is clear\n");
697 const double hopper_clear_verification_position =
Austin Schuh723770b2013-03-10 13:26:20 -0700698 ::std::min(upper_open_region_.lower_bound(),
699 lower_open_region_.lower_bound()) +
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800700 ConvertDiscPositionToIndex(kIndexFreeLength) * 1.5;
701
702 wrist_loop_->R << hopper_clear_verification_position, 0.0;
703 if (::std::abs(wrist_loop_->X_hat(0, 0) -
704 hopper_clear_verification_position) <
705 ConvertDiscPositionToIndex(0.05)) {
706 printf("Should be empty\n");
707 // We are at the end of the range. There are no more discs here.
708 while (frisbees_.size() > 0) {
709 LOG(ERROR, "Dropping an extra disc since it can't exist\n");
710 frisbees_.pop_back();
711 --hopper_disc_count_;
712 --total_disc_count_;
713 }
714 if (hopper_disc_count_ != 0) {
715 LOG(ERROR,
716 "Emptied the hopper out but there are still discs there\n");
717 }
718 }
719 }
720 }
721
722 {
723 const double hopper_clear_verification_position =
Austin Schuh723770b2013-03-10 13:26:20 -0700724 ::std::min(upper_open_region_.lower_bound(),
725 lower_open_region_.lower_bound()) +
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800726 ConvertDiscPositionToIndex(kIndexFreeLength) * 1.5;
727
728 if (wrist_loop_->X_hat(0, 0) >
729 hopper_clear_verification_position +
730 ConvertDiscPositionToIndex(0.05)) {
731 // We are at the end of the range. There are no more discs here.
732 while (frisbees_.size() > 0) {
733 LOG(ERROR, "Dropping an extra disc since it can't exist\n");
734 frisbees_.pop_back();
735 --hopper_disc_count_;
736 --total_disc_count_;
737 }
738 if (hopper_disc_count_ != 0) {
739 LOG(ERROR,
740 "Emptied the hopper out but there are still discs there\n");
741 }
742 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800743 }
744
745 printf("READY_SHOOTER or SHOOT\n");
Austin Schuhd78ab542013-03-01 22:22:19 -0800746 break;
Austin Schuhf8c52252013-03-03 02:25:49 -0800747 }
748
749 // The only way out of the loader is to shoot the disc. The FSM can only go
750 // forwards.
751 switch (loader_state_) {
752 case LoaderState::READY:
753 printf("Loader READY\n");
754 // Open and down, ready to accept a disc.
755 loader_up_ = false;
756 disc_clamped_ = false;
757 disc_ejected_ = false;
758 if (loader_goal_ == LoaderGoal::GRAB ||
759 loader_goal_ == LoaderGoal::SHOOT_AND_RESET) {
760 if (loader_goal_ == LoaderGoal::GRAB) {
761 printf("Told to GRAB, moving on\n");
762 } else {
763 printf("Told to SHOOT_AND_RESET, moving on\n");
764 }
765 loader_state_ = LoaderState::GRABBING;
766 loader_countdown_ = kGrabbingDelay;
767 } else {
768 break;
769 }
770 case LoaderState::GRABBING:
771 printf("Loader GRABBING %d\n", loader_countdown_);
772 // Closing the grabber.
773 loader_up_ = false;
774 disc_clamped_ = true;
775 disc_ejected_ = false;
776 if (loader_countdown_ > 0) {
777 --loader_countdown_;
778 break;
779 } else {
780 loader_state_ = LoaderState::GRABBED;
781 }
782 case LoaderState::GRABBED:
783 printf("Loader GRABBED\n");
784 // Grabber closed.
785 loader_up_ = false;
786 disc_clamped_ = true;
787 disc_ejected_ = false;
788 if (loader_goal_ == LoaderGoal::SHOOT_AND_RESET) {
789 // TODO(aschuh): Only shoot if the shooter is up to speed.
790 // Seems like that would have us shooting a bit later than we could be,
791 // but it also probably spins back up real fast.
792 loader_state_ = LoaderState::LIFTING;
793 loader_countdown_ = kLiftingDelay;
794 printf("Told to SHOOT_AND_RESET, moving on\n");
795 } else if (loader_goal_ == LoaderGoal::READY) {
796 LOG(ERROR, "Can't go to ready when we have something grabbed.\n");
797 printf("Can't go to ready when we have something grabbed.\n");
798 break;
799 } else {
800 break;
801 }
802 case LoaderState::LIFTING:
803 printf("Loader LIFTING %d\n", loader_countdown_);
804 // Lifting the disc.
805 loader_up_ = true;
806 disc_clamped_ = true;
807 disc_ejected_ = false;
808 if (loader_countdown_ > 0) {
809 --loader_countdown_;
810 break;
811 } else {
812 loader_state_ = LoaderState::LIFTED;
813 }
814 case LoaderState::LIFTED:
815 printf("Loader LIFTED\n");
816 // Disc lifted. Time to eject it out.
817 loader_up_ = true;
818 disc_clamped_ = true;
819 disc_ejected_ = false;
820 loader_state_ = LoaderState::SHOOTING;
821 loader_countdown_ = kShootingDelay;
822 case LoaderState::SHOOTING:
823 printf("Loader SHOOTING %d\n", loader_countdown_);
824 // Ejecting the disc into the shooter.
825 loader_up_ = true;
826 disc_clamped_ = false;
827 disc_ejected_ = true;
828 if (loader_countdown_ > 0) {
829 --loader_countdown_;
830 break;
831 } else {
832 loader_state_ = LoaderState::SHOOT;
833 }
834 case LoaderState::SHOOT:
835 printf("Loader SHOOT\n");
836 // The disc has been shot.
837 loader_up_ = true;
838 disc_clamped_ = false;
839 disc_ejected_ = true;
840 loader_state_ = LoaderState::LOWERING;
841 loader_countdown_ = kLoweringDelay;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800842 --hopper_disc_count_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800843 case LoaderState::LOWERING:
844 printf("Loader LOWERING %d\n", loader_countdown_);
845 // Lowering the loader back down.
846 loader_up_ = false;
847 disc_clamped_ = false;
848 disc_ejected_ = true;
849 if (loader_countdown_ > 0) {
850 --loader_countdown_;
851 break;
852 } else {
853 loader_state_ = LoaderState::LOWERED;
854 }
855 case LoaderState::LOWERED:
856 printf("Loader LOWERED\n");
857 // The indexer is lowered.
858 loader_up_ = false;
859 disc_clamped_ = false;
860 disc_ejected_ = false;
861 loader_state_ = LoaderState::READY;
862 // Once we have shot, we need to hang out in READY until otherwise
863 // notified.
864 loader_goal_ = LoaderGoal::READY;
Austin Schuhd78ab542013-03-01 22:22:19 -0800865 break;
866 }
867
868 // Update the observer.
869 wrist_loop_->Update(position != NULL, output == NULL);
870
871 if (position) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800872 LOG(DEBUG, "pos=%f\n", position->index_position);
Austin Schuhd78ab542013-03-01 22:22:19 -0800873 last_bottom_disc_detect_ = position->bottom_disc_detect;
Austin Schuh825bde92013-03-06 00:16:46 -0800874 last_top_disc_detect_ = position->top_disc_detect;
Austin Schuh6328daf2013-03-05 00:53:15 -0800875 last_bottom_disc_posedge_count_ = position->bottom_disc_posedge_count;
876 last_bottom_disc_negedge_count_ = position->bottom_disc_negedge_count;
877 last_bottom_disc_negedge_wait_count_ =
878 position->bottom_disc_negedge_wait_count;
Austin Schuh825bde92013-03-06 00:16:46 -0800879 last_top_disc_posedge_count_ = position->top_disc_posedge_count;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800880 last_top_disc_negedge_count_ = position->top_disc_negedge_count;
Austin Schuhd78ab542013-03-01 22:22:19 -0800881 }
882
883 status->hopper_disc_count = hopper_disc_count_;
884 status->total_disc_count = total_disc_count_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800885 status->preloaded = (loader_state_ != LoaderState::READY);
Austin Schuhd78ab542013-03-01 22:22:19 -0800886
887 if (output) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800888 output->intake_voltage = intake_voltage;
Austin Schuhf8c52252013-03-03 02:25:49 -0800889 output->transfer_voltage = transfer_voltage;
Austin Schuhd78ab542013-03-01 22:22:19 -0800890 output->index_voltage = wrist_loop_->U(0, 0);
Austin Schuhf8c52252013-03-03 02:25:49 -0800891 output->loader_up = loader_up_;
892 output->disc_clamped = disc_clamped_;
893 output->disc_ejected = disc_ejected_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800894 }
895
896 if (safe_to_change_state_) {
897 safe_goal_ = goal_enum;
898 }
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800899 if (hopper_disc_count_ < 0) {
900 LOG(ERROR, "NEGATIVE DISCS. VERY VERY BAD\n");
901 }
Austin Schuhd78ab542013-03-01 22:22:19 -0800902}
903
904} // namespace control_loops
905} // namespace frc971