blob: 25774d26ed935ba336d8e57a09588977ba5a5651 [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 Schuhf8c52252013-03-03 02:25:49 -080089
Austin Schuh7c0e2aa2013-03-09 02:01:16 -080090// TODO(aschuh): Verify these with the sensor actually on.
Austin Schuh825bde92013-03-06 00:16:46 -080091/*static*/ const double IndexMotor::kTopDiscDetectStart =
92 (IndexMotor::kLoaderFreeStopPosition -
Austin Schuh7c0e2aa2013-03-09 02:01:16 -080093 IndexMotor::ConvertDiscAngleToDiscPosition(49 * M_PI / 180));
Austin Schuh825bde92013-03-06 00:16:46 -080094/*static*/ const double IndexMotor::kTopDiscDetectStop =
Austin Schuh7c0e2aa2013-03-09 02:01:16 -080095 (IndexMotor::kLoaderFreeStopPosition +
96 IndexMotor::ConvertDiscAngleToDiscPosition(19 * M_PI / 180));
97
98// I measured the angle between 2 discs. That then gives me the distance
99// between 2 posedges (or negedges). Then subtract off the width of the
100// positive pulse, and that gives the width of the negative pulse.
101/*static*/ const double IndexMotor::kTopDiscDetectMinSeperation =
102 (IndexMotor::ConvertDiscAngleToDiscPosition(120 * M_PI / 180) -
103 (IndexMotor::kTopDiscDetectStop - IndexMotor::kTopDiscDetectStart));
Austin Schuhf8c52252013-03-03 02:25:49 -0800104
Austin Schuhd78ab542013-03-01 22:22:19 -0800105const /*static*/ double IndexMotor::kDiscRadius = 10.875 * 0.0254 / 2;
106const /*static*/ double IndexMotor::kRollerRadius = 2.0 * 0.0254 / 2;
Austin Schuhf8c52252013-03-03 02:25:49 -0800107const /*static*/ double IndexMotor::kTransferRollerRadius = 1.25 * 0.0254 / 2;
Austin Schuhd78ab542013-03-01 22:22:19 -0800108
Austin Schuhf8c52252013-03-03 02:25:49 -0800109/*static*/ const int IndexMotor::kGrabbingDelay = 5;
Austin Schuha3e8e032013-03-10 18:43:14 -0700110/*static*/ const int IndexMotor::kLiftingDelay = 30;
Austin Schuhf8c52252013-03-03 02:25:49 -0800111/*static*/ const int IndexMotor::kShootingDelay = 5;
112/*static*/ const int IndexMotor::kLoweringDelay = 20;
Austin Schuhd78ab542013-03-01 22:22:19 -0800113
Austin Schuh93485832013-03-04 00:01:34 -0800114// TODO(aschuh): Tune these.
115/*static*/ const double
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700116 IndexMotor::IndexStateFeedbackLoop::kMinMotionVoltage = 11.0;
Austin Schuh93485832013-03-04 00:01:34 -0800117/*static*/ const double
Austin Schuha3e8e032013-03-10 18:43:14 -0700118 IndexMotor::IndexStateFeedbackLoop::kNoMotionCuttoffCount = 20;
Austin Schuh93485832013-03-04 00:01:34 -0800119
Austin Schuhd78ab542013-03-01 22:22:19 -0800120/*static*/ double IndexMotor::ConvertDiscAngleToIndex(const double angle) {
121 return (angle * (1 + (kDiscRadius * 2 + kRollerRadius) / kRollerRadius));
122}
123
Austin Schuhf8c52252013-03-03 02:25:49 -0800124/*static*/ double IndexMotor::ConvertDiscAngleToDiscPosition(
125 const double angle) {
Austin Schuhd78ab542013-03-01 22:22:19 -0800126 return angle * (kDiscRadius + kRollerRadius);
127}
128
Austin Schuhf8c52252013-03-03 02:25:49 -0800129/*static*/ double IndexMotor::ConvertDiscPositionToDiscAngle(
130 const double position) {
131 return position / (kDiscRadius + kRollerRadius);
132}
133
Austin Schuhd78ab542013-03-01 22:22:19 -0800134/*static*/ double IndexMotor::ConvertIndexToDiscAngle(const double angle) {
135 return (angle / (1 + (kDiscRadius * 2 + kRollerRadius) / kRollerRadius));
136}
137
138/*static*/ double IndexMotor::ConvertIndexToDiscPosition(const double angle) {
139 return IndexMotor::ConvertDiscAngleToDiscPosition(
140 ConvertIndexToDiscAngle(angle));
141}
142
Austin Schuhf8c52252013-03-03 02:25:49 -0800143/*static*/ double IndexMotor::ConvertTransferToDiscPosition(
144 const double angle) {
145 const double gear_ratio = (1 + (kDiscRadius * 2 + kTransferRollerRadius) /
146 kTransferRollerRadius);
147 return angle / gear_ratio * (kDiscRadius + kTransferRollerRadius);
148}
149
150/*static*/ double IndexMotor::ConvertDiscPositionToIndex(
151 const double position) {
152 return IndexMotor::ConvertDiscAngleToIndex(
153 ConvertDiscPositionToDiscAngle(position));
154}
155
Austin Schuh1b864a12013-03-07 00:46:50 -0800156bool IndexMotor::MinDiscPosition(double *disc_position, Frisbee **found_disc) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800157 bool found_start = false;
158 for (unsigned int i = 0; i < frisbees_.size(); ++i) {
Austin Schuh1b864a12013-03-07 00:46:50 -0800159 Frisbee &frisbee = frisbees_[i];
Austin Schuhf8c52252013-03-03 02:25:49 -0800160 if (!found_start) {
161 if (frisbee.has_position()) {
162 *disc_position = frisbee.position();
Austin Schuh1b864a12013-03-07 00:46:50 -0800163 if (found_disc) {
164 *found_disc = &frisbee;
165 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800166 found_start = true;
167 }
168 } else {
Austin Schuh1b864a12013-03-07 00:46:50 -0800169 if (frisbee.position() <= *disc_position) {
170 *disc_position = frisbee.position();
171 if (found_disc) {
172 *found_disc = &frisbee;
173 }
174 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800175 }
176 }
177 return found_start;
178}
179
Austin Schuh1b864a12013-03-07 00:46:50 -0800180bool IndexMotor::MaxDiscPosition(double *disc_position, Frisbee **found_disc) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800181 bool found_start = false;
182 for (unsigned int i = 0; i < frisbees_.size(); ++i) {
Austin Schuh1b864a12013-03-07 00:46:50 -0800183 Frisbee &frisbee = frisbees_[i];
Austin Schuhf8c52252013-03-03 02:25:49 -0800184 if (!found_start) {
185 if (frisbee.has_position()) {
186 *disc_position = frisbee.position();
Austin Schuh1b864a12013-03-07 00:46:50 -0800187 if (found_disc) {
188 *found_disc = &frisbee;
189 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800190 found_start = true;
191 }
192 } else {
Austin Schuh1b864a12013-03-07 00:46:50 -0800193 if (frisbee.position() > *disc_position) {
194 *disc_position = frisbee.position();
195 if (found_disc) {
196 *found_disc = &frisbee;
197 }
198 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800199 }
200 }
201 return found_start;
202}
203
Austin Schuh93485832013-03-04 00:01:34 -0800204void IndexMotor::IndexStateFeedbackLoop::CapU() {
205 // If the voltage has been low for a large number of cycles, cut the motor
206 // power. This is generally very bad controls practice since this isn't LTI,
207 // but we don't really care about tracking anything other than large step
208 // inputs, and the loader doesn't need to be that accurate.
209 if (::std::abs(U(0, 0)) < kMinMotionVoltage) {
210 ++low_voltage_count_;
211 if (low_voltage_count_ > kNoMotionCuttoffCount) {
Austin Schuh93485832013-03-04 00:01:34 -0800212 U(0, 0) = 0.0;
213 }
214 } else {
215 low_voltage_count_ = 0;
216 }
217
218 for (int i = 0; i < kNumOutputs; ++i) {
Austin Schuh9644e1c2013-03-12 00:40:36 -0700219 if (U(i, 0) > U_max(i, 0)) {
220 U(i, 0) = U_max(i, 0);
221 } else if (U(i, 0) < U_min(i, 0)) {
222 U(i, 0) = U_min(i, 0);
Austin Schuh93485832013-03-04 00:01:34 -0800223 }
224 }
225}
226
227
Austin Schuhd78ab542013-03-01 22:22:19 -0800228// Positive angle is towards the shooter, and positive power is towards the
229// shooter.
230void IndexMotor::RunIteration(
231 const control_loops::IndexLoop::Goal *goal,
232 const control_loops::IndexLoop::Position *position,
233 control_loops::IndexLoop::Output *output,
234 control_loops::IndexLoop::Status *status) {
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800235 // Make goal easy to work with and sanity check it.
Austin Schuhd78ab542013-03-01 22:22:19 -0800236 Goal goal_enum = static_cast<Goal>(goal->goal_state);
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800237 if (goal->goal_state < 0 || goal->goal_state > 4) {
Brian Silverman94195052013-03-09 13:45:05 -0800238 LOG(ERROR, "Goal state is %"PRId32" which is out of range. Going to HOLD.\n",
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800239 goal->goal_state);
240 goal_enum = Goal::HOLD;
241 }
Austin Schuhd78ab542013-03-01 22:22:19 -0800242
243 // Disable the motors now so that all early returns will return with the
244 // motors disabled.
Austin Schuhb6d898b2013-03-03 15:34:35 -0800245 double intake_voltage = 0.0;
Austin Schuhf8c52252013-03-03 02:25:49 -0800246 double transfer_voltage = 0.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800247 if (output) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800248 output->intake_voltage = 0.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800249 output->transfer_voltage = 0.0;
250 output->index_voltage = 0.0;
251 }
252
253 status->ready_to_intake = false;
254
Austin Schuhe3490622013-03-13 01:24:30 -0700255 // Set the controller to use to be the one designed for the current number of
256 // discs in the hopper. This is safe since the controller prevents the index
257 // from being set out of bounds and picks the closest controller.
258 wrist_loop_->set_controller_index(frisbees_.size());
259
Austin Schuhf8c52252013-03-03 02:25:49 -0800260 // Compute a safe index position that we can use.
Austin Schuhd78ab542013-03-01 22:22:19 -0800261 if (position) {
262 wrist_loop_->Y << position->index_position;
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800263 // Set the goal to be the current position if this is the first time through
264 // so we don't always spin the indexer to the 0 position before starting.
265 if (no_prior_position_) {
266 wrist_loop_->R << wrist_loop_->Y(0, 0), 0.0;
Austin Schuhc5ef1bb2013-03-10 00:42:05 -0800267 wrist_loop_->X_hat(0, 0) = wrist_loop_->Y(0, 0);
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800268 no_prior_position_ = false;
Austin Schuh6328daf2013-03-05 00:53:15 -0800269 last_bottom_disc_posedge_count_ = position->bottom_disc_posedge_count;
270 last_bottom_disc_negedge_count_ = position->bottom_disc_negedge_count;
271 last_bottom_disc_negedge_wait_count_ =
272 position->bottom_disc_negedge_wait_count;
Austin Schuh825bde92013-03-06 00:16:46 -0800273 last_top_disc_posedge_count_ = position->top_disc_posedge_count;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800274 last_top_disc_negedge_count_ = position->top_disc_negedge_count;
275 // The open positions for the upper is right here and isn't a hard edge.
Austin Schuh723770b2013-03-10 13:26:20 -0700276 upper_open_region_.Restart(wrist_loop_->Y(0, 0));
277 lower_open_region_.Restart(wrist_loop_->Y(0, 0));
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800278 }
279
Austin Schuh1b864a12013-03-07 00:46:50 -0800280 // If the cRIO is gone for over 1/2 of a second, assume that it rebooted.
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800281 if (missing_position_count_ > 50) {
Austin Schuh6328daf2013-03-05 00:53:15 -0800282 last_bottom_disc_posedge_count_ = position->bottom_disc_posedge_count;
283 last_bottom_disc_negedge_count_ = position->bottom_disc_negedge_count;
284 last_bottom_disc_negedge_wait_count_ =
285 position->bottom_disc_negedge_wait_count;
Austin Schuh825bde92013-03-06 00:16:46 -0800286 last_top_disc_posedge_count_ = position->top_disc_posedge_count;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800287 last_top_disc_negedge_count_ = position->top_disc_negedge_count;
288 // We can't really trust the open range any more if the crio rebooted.
Austin Schuh723770b2013-03-10 13:26:20 -0700289 upper_open_region_.Restart(wrist_loop_->Y(0, 0));
290 lower_open_region_.Restart(wrist_loop_->Y(0, 0));
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800291 // Adjust the disc positions so that they don't have to move.
292 const double disc_offset =
293 position->index_position - wrist_loop_->X_hat(0, 0);
294 for (auto frisbee = frisbees_.begin();
295 frisbee != frisbees_.end(); ++frisbee) {
296 frisbee->OffsetDisc(disc_offset);
297 }
Austin Schuhc5ef1bb2013-03-10 00:42:05 -0800298 wrist_loop_->X_hat(0, 0) = wrist_loop_->Y(0, 0);
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800299 }
300 missing_position_count_ = 0;
301 } else {
302 ++missing_position_count_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800303 }
304 const double index_position = wrist_loop_->X_hat(0, 0);
305
Austin Schuh825bde92013-03-06 00:16:46 -0800306 if (position) {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800307 // Reset the open region if we saw a negedge.
Austin Schuh723770b2013-03-10 13:26:20 -0700308 if (position->bottom_disc_negedge_wait_count !=
309 last_bottom_disc_negedge_wait_count_) {
310 // Saw a negedge, must be a new region.
311 lower_open_region_.Restart(position->bottom_disc_negedge_wait_position);
312 }
313 // Reset the open region if we saw a negedge.
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800314 if (position->top_disc_negedge_count != last_top_disc_negedge_count_) {
315 // Saw a negedge, must be a new region.
Austin Schuh723770b2013-03-10 13:26:20 -0700316 upper_open_region_.Restart(position->top_disc_negedge_position);
317 }
318
319 // No disc. Expand the open region.
320 if (!position->bottom_disc_detect) {
321 lower_open_region_.Expand(index_position);
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800322 }
323
324 // No disc. Expand the open region.
325 if (!position->top_disc_detect) {
Austin Schuh723770b2013-03-10 13:26:20 -0700326 upper_open_region_.Expand(index_position);
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800327 }
328
Austin Schuh825bde92013-03-06 00:16:46 -0800329 if (!position->top_disc_detect) {
330 // We don't see a disc. Verify that there are no discs that we should be
331 // seeing.
Austin Schuh1b864a12013-03-07 00:46:50 -0800332 // Assume that discs will move slow enough that we won't miss one as it
333 // goes by. They will either pile up above or below the sensor.
Austin Schuhdff24e22013-03-06 00:41:21 -0800334
335 double cumulative_offset = 0.0;
336 for (auto frisbee = frisbees_.rbegin(), rend = frisbees_.rend();
337 frisbee != rend; ++frisbee) {
338 frisbee->OffsetDisc(cumulative_offset);
339 double amount_moved = frisbee->ObserveNoTopDiscSensor(
Austin Schuh6b1ad2f2013-03-11 23:23:00 -0700340 wrist_loop_->X_hat(0, 0));
Austin Schuhdff24e22013-03-06 00:41:21 -0800341 cumulative_offset += amount_moved;
Austin Schuh825bde92013-03-06 00:16:46 -0800342 }
343 }
Austin Schuh1b864a12013-03-07 00:46:50 -0800344
Austin Schuh825bde92013-03-06 00:16:46 -0800345 if (position->top_disc_posedge_count != last_top_disc_posedge_count_) {
Austin Schuh1b864a12013-03-07 00:46:50 -0800346 const double index_position = wrist_loop_->X_hat(0, 0) -
347 position->index_position + position->top_disc_posedge_position;
Austin Schuh825bde92013-03-06 00:16:46 -0800348 // TODO(aschuh): Sanity check this number...
349 // Requires storing when the disc was last seen with the sensor off, and
350 // figuring out what to do if things go south.
351
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800352 // 1 if discs are going up, 0 if we have no clue, and -1 if they are going
353 // down.
354 int disc_direction = 0;
Austin Schuh825bde92013-03-06 00:16:46 -0800355 if (wrist_loop_->X_hat(1, 0) > 50.0) {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800356 disc_direction = 1;
357 } else if (wrist_loop_->X_hat(1, 0) < -50.0) {
358 disc_direction = -1;
359 } else {
360 // Save the upper and lower positions that we last saw a disc at.
361 // If there is a big buffer above, must be a disc from below.
362 // If there is a big buffer below, must be a disc from above.
363 // This should work to replace the velocity threshold above.
364
Austin Schuh723770b2013-03-10 13:26:20 -0700365 const double open_width = upper_open_region_.width();
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800366 const double relative_upper_open_precentage =
Austin Schuh723770b2013-03-10 13:26:20 -0700367 (upper_open_region_.upper_bound() - index_position) / open_width;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800368 const double relative_lower_open_precentage =
Austin Schuh723770b2013-03-10 13:26:20 -0700369 (index_position - upper_open_region_.lower_bound()) / open_width;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800370
371 if (ConvertIndexToDiscPosition(open_width) <
372 kTopDiscDetectMinSeperation * 0.9) {
373 LOG(ERROR, "Discs are way too close to each other. Doing nothing\n");
374 } else if (relative_upper_open_precentage > 0.75) {
375 // Looks like it is a disc going down from above since we are near
376 // the upper edge.
377 disc_direction = -1;
Austin Schuha3e8e032013-03-10 18:43:14 -0700378 LOG(INFO, "Disc edge going down\n");
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800379 } else if (relative_lower_open_precentage > 0.75) {
380 // Looks like it is a disc going up from below since we are near
381 // the lower edge.
382 disc_direction = 1;
Austin Schuha3e8e032013-03-10 18:43:14 -0700383 LOG(INFO, "Disc edge going up\n");
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800384 } else {
385 LOG(ERROR,
386 "Got an edge in the middle of what should be an open region.\n");
387 LOG(ERROR, "Open width: %f upper precentage %f %%\n",
388 open_width, relative_upper_open_precentage);
389 }
390 }
391
392 if (disc_direction > 0) {
Austin Schuh825bde92013-03-06 00:16:46 -0800393 // Moving up at a reasonable clip.
Austin Schuh1b864a12013-03-07 00:46:50 -0800394 // Find the highest disc that is below the top disc sensor.
395 // While we are at it, count the number above and log an error if there
396 // are too many.
397 if (frisbees_.size() == 0) {
398 Frisbee new_frisbee;
399 new_frisbee.has_been_indexed_ = true;
400 new_frisbee.index_start_position_ = index_position -
401 ConvertDiscPositionToIndex(kTopDiscDetectStart -
402 kIndexStartPosition);
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800403 ++hopper_disc_count_;
404 ++total_disc_count_;
Austin Schuh1b864a12013-03-07 00:46:50 -0800405 frisbees_.push_front(new_frisbee);
406 LOG(WARNING, "Added a disc to the hopper at the top sensor\n");
407 }
408
409 int above_disc_count = 0;
410 double highest_position = 0;
411 Frisbee *highest_frisbee_below_sensor = NULL;
412 for (auto frisbee = frisbees_.rbegin(), rend = frisbees_.rend();
413 frisbee != rend; ++frisbee) {
414 const double disc_position = frisbee->absolute_position(
415 index_position);
416 // It is save to use the top position for the cuttoff, since the
417 // sensor being low will result in discs being pushed off of it.
418 if (disc_position >= kTopDiscDetectStop) {
419 ++above_disc_count;
420 } else if (!highest_frisbee_below_sensor ||
421 disc_position > highest_position) {
422 highest_frisbee_below_sensor = &*frisbee;
423 highest_position = disc_position;
424 }
425 }
426 if (above_disc_count > 1) {
427 LOG(ERROR, "We have 2 discs above the top sensor.\n");
428 }
429
430 // We now have the disc. Shift all the ones below the sensor up by the
431 // computed delta.
432 const double disc_delta = IndexMotor::ConvertDiscPositionToIndex(
433 highest_position - kTopDiscDetectStart);
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 if (disc_position < kTopDiscDetectStop) {
439 frisbee->OffsetDisc(disc_delta);
440 }
441 }
Austin Schuha3e8e032013-03-10 18:43:14 -0700442 LOG(INFO, "Currently have %d discs, saw posedge moving up. "
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800443 "Moving down by %f to %f\n", frisbees_.size(),
Austin Schuh1b864a12013-03-07 00:46:50 -0800444 ConvertIndexToDiscPosition(disc_delta),
445 highest_frisbee_below_sensor->absolute_position(
446 wrist_loop_->X_hat(0, 0)));
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800447 } else if (disc_direction < 0) {
Austin Schuh825bde92013-03-06 00:16:46 -0800448 // Moving down at a reasonable clip.
Austin Schuh1b864a12013-03-07 00:46:50 -0800449 // There can only be 1 disc up top that would give us a posedge.
450 // Find it and place it at the one spot that it can be.
Brian Silverman94195052013-03-09 13:45:05 -0800451 double min_disc_position = 0;
Austin Schuh1b864a12013-03-07 00:46:50 -0800452 Frisbee *min_frisbee = NULL;
453 MinDiscPosition(&min_disc_position, &min_frisbee);
454 if (!min_frisbee) {
455 // Uh, oh, we see a disc but there isn't one...
456 LOG(ERROR, "Saw a disc up top but there isn't one in the hopper\n");
457 } else {
458 const double disc_position = min_frisbee->absolute_position(
459 index_position);
460
461 const double disc_delta_meters = disc_position - kTopDiscDetectStop;
462 const double disc_delta = IndexMotor::ConvertDiscPositionToIndex(
463 disc_delta_meters);
Austin Schuha3e8e032013-03-10 18:43:14 -0700464 LOG(INFO, "Posedge going down. Moving top disc down by %f\n",
465 disc_delta_meters);
Austin Schuh1b864a12013-03-07 00:46:50 -0800466 for (auto frisbee = frisbees_.begin(), end = frisbees_.end();
467 frisbee != end; ++frisbee) {
468 frisbee->OffsetDisc(disc_delta);
469 }
470 }
Austin Schuh825bde92013-03-06 00:16:46 -0800471 } else {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800472 LOG(ERROR, "Not sure how to handle the upper posedge, doing nothing\n");
Austin Schuh825bde92013-03-06 00:16:46 -0800473 }
474 }
475 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800476
Austin Schuhf8c52252013-03-03 02:25:49 -0800477 // Bool to track if it is safe for the goal to change yet.
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700478 bool safe_to_change_state = true;
Austin Schuhd78ab542013-03-01 22:22:19 -0800479 switch (safe_goal_) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800480 case Goal::HOLD:
Austin Schuhd78ab542013-03-01 22:22:19 -0800481 // The goal should already be good, so sit tight with everything the same
482 // as it was.
Austin Schuhd78ab542013-03-01 22:22:19 -0800483 break;
Austin Schuhf8c52252013-03-03 02:25:49 -0800484 case Goal::READY_LOWER:
485 case Goal::INTAKE:
Austin Schuhd78ab542013-03-01 22:22:19 -0800486 {
487 Time now = Time::Now();
Austin Schuhd78ab542013-03-01 22:22:19 -0800488 if (position) {
Austin Schuh6328daf2013-03-05 00:53:15 -0800489 // Posedge of the disc entering the beam break.
490 if (position->bottom_disc_posedge_count !=
491 last_bottom_disc_posedge_count_) {
Austin Schuhd78ab542013-03-01 22:22:19 -0800492 transfer_frisbee_.Reset();
493 transfer_frisbee_.bottom_posedge_time_ = now;
Austin Schuha3e8e032013-03-10 18:43:14 -0700494 LOG(INFO, "Posedge of bottom disc %f\n",
495 transfer_frisbee_.bottom_posedge_time_.ToSeconds());
Austin Schuhd78ab542013-03-01 22:22:19 -0800496 ++hopper_disc_count_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800497 ++total_disc_count_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800498 }
499
500 // Disc exited the beam break now.
Austin Schuh6328daf2013-03-05 00:53:15 -0800501 if (position->bottom_disc_negedge_count !=
502 last_bottom_disc_negedge_count_) {
Austin Schuhd78ab542013-03-01 22:22:19 -0800503 transfer_frisbee_.bottom_negedge_time_ = now;
Austin Schuha3e8e032013-03-10 18:43:14 -0700504 LOG(INFO, "Negedge of bottom disc %f\n",
505 transfer_frisbee_.bottom_negedge_time_.ToSeconds());
Austin Schuhd78ab542013-03-01 22:22:19 -0800506 frisbees_.push_front(transfer_frisbee_);
507 }
508
509 if (position->bottom_disc_detect) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800510 intake_voltage = transfer_voltage = 12.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800511 // Must wait until the disc gets out before we can change state.
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700512 safe_to_change_state = false;
Austin Schuhd78ab542013-03-01 22:22:19 -0800513
Austin Schuhf8c52252013-03-03 02:25:49 -0800514 // TODO(aschuh): A disc on the way through needs to start moving
515 // the indexer if it isn't already moving. Maybe?
Austin Schuhd78ab542013-03-01 22:22:19 -0800516
517 Time elapsed_posedge_time = now -
518 transfer_frisbee_.bottom_posedge_time_;
519 if (elapsed_posedge_time >= Time::InSeconds(0.3)) {
520 // It has been too long. The disc must be jammed.
521 LOG(ERROR, "Been way too long. Jammed disc?\n");
Austin Schuhd78ab542013-03-01 22:22:19 -0800522 }
523 }
524
Austin Schuhf8c52252013-03-03 02:25:49 -0800525 // Check all non-indexed discs and see if they should be indexed.
Austin Schuhb6d898b2013-03-03 15:34:35 -0800526 for (auto frisbee = frisbees_.begin();
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800527 frisbee != frisbees_.end(); ++frisbee) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800528 if (!frisbee->has_been_indexed_) {
529 intake_voltage = transfer_voltage = 12.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800530
Austin Schuh6328daf2013-03-05 00:53:15 -0800531 if (last_bottom_disc_negedge_wait_count_ !=
532 position->bottom_disc_negedge_wait_count) {
533 // We have an index difference.
534 // Save the indexer position, and the time.
535 if (last_bottom_disc_negedge_wait_count_ + 1 !=
536 position->bottom_disc_negedge_wait_count) {
537 LOG(ERROR, "Funny, we got 2 edges since we last checked.\n");
538 }
539
540 // Save the captured position as the position at which the disc
541 // touched the indexer.
Austin Schuhd78ab542013-03-01 22:22:19 -0800542 LOG(INFO, "Grabbed on the index now at %f\n", index_position);
Austin Schuhb6d898b2013-03-03 15:34:35 -0800543 frisbee->has_been_indexed_ = true;
Austin Schuh6328daf2013-03-05 00:53:15 -0800544 frisbee->index_start_position_ =
545 position->bottom_disc_negedge_wait_position;
Austin Schuhd78ab542013-03-01 22:22:19 -0800546 }
547 }
Austin Schuhb6d898b2013-03-03 15:34:35 -0800548 if (!frisbee->has_been_indexed_) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800549 // All discs must be indexed before it is safe to stop indexing.
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700550 safe_to_change_state = false;
Austin Schuhd78ab542013-03-01 22:22:19 -0800551 }
552 }
553
Austin Schuhf8c52252013-03-03 02:25:49 -0800554 // Figure out where the indexer should be to move the discs down to
555 // the right position.
Brian Silverman94195052013-03-09 13:45:05 -0800556 double max_disc_position = 0;
Austin Schuh1b864a12013-03-07 00:46:50 -0800557 if (MaxDiscPosition(&max_disc_position, NULL)) {
Austin Schuha3e8e032013-03-10 18:43:14 -0700558 LOG(DEBUG, "There is a disc down here!\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800559 // TODO(aschuh): Figure out what to do if grabbing the next one
560 // would cause things to jam into the loader.
561 // Say we aren't ready any more. Undefined behavior will result if
562 // that isn't observed.
563 double bottom_disc_position =
564 max_disc_position + ConvertDiscAngleToIndex(M_PI);
565 wrist_loop_->R << bottom_disc_position, 0.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800566
Austin Schuhf8c52252013-03-03 02:25:49 -0800567 // Verify that we are close enough to the goal so that we should be
568 // fine accepting the next disc.
569 double disc_error_meters = ConvertIndexToDiscPosition(
570 wrist_loop_->X_hat(0, 0) - bottom_disc_position);
571 // We are ready for the next disc if the first one is in the first
572 // half circle of the indexer. It will take time for the disc to
573 // come into the indexer, so we will be able to move it out of the
574 // way in time.
575 // This choice also makes sure that we don't claim that we aren't
576 // ready between full speed intaking.
577 if (-ConvertDiscAngleToIndex(M_PI) < disc_error_meters &&
578 disc_error_meters < 0.04) {
579 // We are only ready if we aren't being asked to change state or
580 // are full.
581 status->ready_to_intake =
582 (safe_goal_ == goal_enum) && hopper_disc_count_ < 4;
583 } else {
584 status->ready_to_intake = false;
Austin Schuhd78ab542013-03-01 22:22:19 -0800585 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800586 } else {
587 // No discs! We are always ready for more if we aren't being
588 // asked to change state.
589 status->ready_to_intake = (safe_goal_ == goal_enum);
Austin Schuhd78ab542013-03-01 22:22:19 -0800590 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800591
592 // Turn on the transfer roller if we are ready.
593 if (status->ready_to_intake && hopper_disc_count_ < 4 &&
594 safe_goal_ == Goal::INTAKE) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800595 intake_voltage = transfer_voltage = 12.0;
Austin Schuhf8c52252013-03-03 02:25:49 -0800596 }
Austin Schuhd78ab542013-03-01 22:22:19 -0800597 }
Austin Schuha3e8e032013-03-10 18:43:14 -0700598 LOG(DEBUG, "INTAKE\n");
Austin Schuhd78ab542013-03-01 22:22:19 -0800599 }
600 break;
Austin Schuhf8c52252013-03-03 02:25:49 -0800601 case Goal::READY_SHOOTER:
602 case Goal::SHOOT:
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700603 // Don't let us leave the shoot or preload state if there are 4 discs in
604 // the hopper.
605 if (hopper_disc_count_ >= 4 && goal_enum != Goal::SHOOT) {
606 safe_to_change_state = false;
607 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800608 // Check if we have any discs to shoot or load and handle them.
Brian Silverman94195052013-03-09 13:45:05 -0800609 double min_disc_position = 0;
Austin Schuh1b864a12013-03-07 00:46:50 -0800610 if (MinDiscPosition(&min_disc_position, NULL)) {
611 const double ready_disc_position = min_disc_position +
612 ConvertDiscPositionToIndex(kReadyToPreload - kIndexStartPosition);
Austin Schuhf8c52252013-03-03 02:25:49 -0800613
614 const double grabbed_disc_position =
615 min_disc_position +
616 ConvertDiscPositionToIndex(kReadyToLiftPosition -
617 kIndexStartPosition + 0.03);
618
619 // Check the state of the loader FSM.
620 // If it is ready to load discs, position the disc so that it is ready
621 // to be grabbed.
622 // If it isn't ready, there is a disc in there. It needs to finish it's
623 // cycle first.
624 if (loader_state_ != LoaderState::READY) {
625 // We already have a disc in the loader.
626 // Stage the discs back a bit.
627 wrist_loop_->R << ready_disc_position, 0.0;
628
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800629 // Shoot if we are grabbed and being asked to shoot.
630 if (loader_state_ == LoaderState::GRABBED &&
631 safe_goal_ == Goal::SHOOT) {
632 loader_goal_ = LoaderGoal::SHOOT_AND_RESET;
633 }
634
Austin Schuhf8c52252013-03-03 02:25:49 -0800635 // Must wait until it has been grabbed to continue.
636 if (loader_state_ == LoaderState::GRABBING) {
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700637 safe_to_change_state = false;
Austin Schuhf8c52252013-03-03 02:25:49 -0800638 }
639 } else {
640 // No disc up top right now.
641 wrist_loop_->R << grabbed_disc_position, 0.0;
642
643 // See if the disc has gotten pretty far up yet.
644 if (wrist_loop_->X_hat(0, 0) > ready_disc_position) {
645 // Point of no return. We are committing to grabbing it now.
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700646 safe_to_change_state = false;
Austin Schuhf8c52252013-03-03 02:25:49 -0800647 const double robust_grabbed_disc_position =
648 (grabbed_disc_position -
649 ConvertDiscPositionToIndex(kGrabberLength));
650
651 // If close, start grabbing and/or shooting.
652 if (wrist_loop_->X_hat(0, 0) > robust_grabbed_disc_position) {
653 // Start the state machine.
654 if (safe_goal_ == Goal::SHOOT) {
655 loader_goal_ = LoaderGoal::SHOOT_AND_RESET;
656 } else {
657 loader_goal_ = LoaderGoal::GRAB;
658 }
659 // This frisbee is now gone. Take it out of the queue.
660 frisbees_.pop_back();
Austin Schuhf8c52252013-03-03 02:25:49 -0800661 }
662 }
663 }
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800664 } else {
665 if (loader_state_ != LoaderState::READY) {
666 // Shoot if we are grabbed and being asked to shoot.
667 if (loader_state_ == LoaderState::GRABBED &&
668 safe_goal_ == Goal::SHOOT) {
669 loader_goal_ = LoaderGoal::SHOOT_AND_RESET;
670 }
671 } else {
672 // Ok, no discs in sight. Spin the hopper up by 150% of it's full
673 // range and verify that we don't see anything.
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800674 const double hopper_clear_verification_position =
Austin Schuha3e8e032013-03-10 18:43:14 -0700675 ::std::max(upper_open_region_.lower_bound(),
Austin Schuh723770b2013-03-10 13:26:20 -0700676 lower_open_region_.lower_bound()) +
Austin Schuhf60861b2013-03-14 00:14:10 -0700677 ConvertDiscPositionToIndex(kIndexFreeLength) * 1.5;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800678
679 wrist_loop_->R << hopper_clear_verification_position, 0.0;
680 if (::std::abs(wrist_loop_->X_hat(0, 0) -
681 hopper_clear_verification_position) <
682 ConvertDiscPositionToIndex(0.05)) {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800683 // We are at the end of the range. There are no more discs here.
684 while (frisbees_.size() > 0) {
685 LOG(ERROR, "Dropping an extra disc since it can't exist\n");
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700686 LOG(ERROR, "Upper is [%f %f]\n",
687 upper_open_region_.upper_bound(),
688 upper_open_region_.lower_bound());
689 LOG(ERROR, "Lower is [%f %f]\n",
690 lower_open_region_.upper_bound(),
691 lower_open_region_.lower_bound());
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800692 frisbees_.pop_back();
693 --hopper_disc_count_;
694 --total_disc_count_;
695 }
696 if (hopper_disc_count_ != 0) {
697 LOG(ERROR,
698 "Emptied the hopper out but there are still discs there\n");
699 }
700 }
701 }
702 }
703
704 {
705 const double hopper_clear_verification_position =
Austin Schuha3e8e032013-03-10 18:43:14 -0700706 ::std::max(upper_open_region_.lower_bound(),
Austin Schuh723770b2013-03-10 13:26:20 -0700707 lower_open_region_.lower_bound()) +
Austin Schuhf60861b2013-03-14 00:14:10 -0700708 ConvertDiscPositionToIndex(kIndexFreeLength) * 1.5;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800709
710 if (wrist_loop_->X_hat(0, 0) >
711 hopper_clear_verification_position +
712 ConvertDiscPositionToIndex(0.05)) {
713 // We are at the end of the range. There are no more discs here.
714 while (frisbees_.size() > 0) {
715 LOG(ERROR, "Dropping an extra disc since it can't exist\n");
Austin Schuha3e8e032013-03-10 18:43:14 -0700716 LOG(ERROR, "Upper is [%f %f]\n",
717 upper_open_region_.upper_bound(),
718 upper_open_region_.lower_bound());
719 LOG(ERROR, "Lower is [%f %f]\n",
720 lower_open_region_.upper_bound(),
721 lower_open_region_.lower_bound());
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800722 frisbees_.pop_back();
723 --hopper_disc_count_;
724 --total_disc_count_;
725 }
Austin Schuhf60861b2013-03-14 00:14:10 -0700726 if (hopper_disc_count_ >= 0) {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800727 LOG(ERROR,
Austin Schuhf60861b2013-03-14 00:14:10 -0700728 "Emptied the hopper out but there are still %d discs there\n",
729 hopper_disc_count_);
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800730 }
731 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800732 }
733
Austin Schuha3e8e032013-03-10 18:43:14 -0700734 LOG(DEBUG, "READY_SHOOTER or SHOOT\n");
Austin Schuhd78ab542013-03-01 22:22:19 -0800735 break;
Austin Schuhf8c52252013-03-03 02:25:49 -0800736 }
737
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700738 // If we have 4 discs, it is time to preload.
739 if (safe_to_change_state && hopper_disc_count_ >= 4) {
740 switch (safe_goal_) {
741 case Goal::HOLD:
742 case Goal::READY_LOWER:
743 case Goal::INTAKE:
744 safe_goal_ = Goal::READY_SHOOTER;
745 safe_to_change_state = false;
Brian Silvermana0238ce2013-03-13 17:58:45 -0700746 LOG(INFO, "We have %"PRId32" discs, time to preload automatically\n",
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700747 hopper_disc_count_);
748 break;
749 case Goal::READY_SHOOTER:
750 case Goal::SHOOT:
751 break;
752 }
753 }
754
Austin Schuhf8c52252013-03-03 02:25:49 -0800755 // The only way out of the loader is to shoot the disc. The FSM can only go
756 // forwards.
757 switch (loader_state_) {
758 case LoaderState::READY:
Austin Schuha3e8e032013-03-10 18:43:14 -0700759 LOG(DEBUG, "Loader READY\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800760 // Open and down, ready to accept a disc.
761 loader_up_ = false;
762 disc_clamped_ = false;
763 disc_ejected_ = false;
764 if (loader_goal_ == LoaderGoal::GRAB ||
765 loader_goal_ == LoaderGoal::SHOOT_AND_RESET) {
766 if (loader_goal_ == LoaderGoal::GRAB) {
Austin Schuha3e8e032013-03-10 18:43:14 -0700767 LOG(INFO, "Told to GRAB, moving on\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800768 } else {
Austin Schuha3e8e032013-03-10 18:43:14 -0700769 LOG(INFO, "Told to SHOOT_AND_RESET, moving on\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800770 }
771 loader_state_ = LoaderState::GRABBING;
772 loader_countdown_ = kGrabbingDelay;
773 } else {
774 break;
775 }
776 case LoaderState::GRABBING:
Austin Schuha3e8e032013-03-10 18:43:14 -0700777 LOG(DEBUG, "Loader GRABBING %d\n", loader_countdown_);
Austin Schuhf8c52252013-03-03 02:25:49 -0800778 // Closing the grabber.
779 loader_up_ = false;
780 disc_clamped_ = true;
781 disc_ejected_ = false;
782 if (loader_countdown_ > 0) {
783 --loader_countdown_;
784 break;
785 } else {
786 loader_state_ = LoaderState::GRABBED;
787 }
788 case LoaderState::GRABBED:
Austin Schuha3e8e032013-03-10 18:43:14 -0700789 LOG(DEBUG, "Loader GRABBED\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800790 // Grabber closed.
791 loader_up_ = false;
792 disc_clamped_ = true;
793 disc_ejected_ = false;
794 if (loader_goal_ == LoaderGoal::SHOOT_AND_RESET) {
Austin Schuha3e8e032013-03-10 18:43:14 -0700795 shooter.status.FetchLatest();
796 if (shooter.status.get()) {
797 // TODO(aschuh): If we aren't shooting nicely, wait until the shooter
798 // is up to speed rather than just spinning.
Austin Schuhf60861b2013-03-14 00:14:10 -0700799 if (shooter.status->average_velocity > 130 && shooter.status->ready) {
Austin Schuha3e8e032013-03-10 18:43:14 -0700800 loader_state_ = LoaderState::LIFTING;
801 loader_countdown_ = kLiftingDelay;
802 LOG(INFO, "Told to SHOOT_AND_RESET, moving on\n");
803 } else {
804 LOG(WARNING, "Told to SHOOT_AND_RESET, shooter too slow at %f\n",
805 shooter.status->average_velocity);
806 break;
807 }
808 } else {
809 LOG(ERROR, "Told to SHOOT_AND_RESET, no shooter data, moving on.\n");
810 loader_state_ = LoaderState::LIFTING;
811 loader_countdown_ = kLiftingDelay;
812 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800813 } else if (loader_goal_ == LoaderGoal::READY) {
814 LOG(ERROR, "Can't go to ready when we have something grabbed.\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800815 break;
816 } else {
817 break;
818 }
819 case LoaderState::LIFTING:
Austin Schuha3e8e032013-03-10 18:43:14 -0700820 LOG(DEBUG, "Loader LIFTING %d\n", loader_countdown_);
Austin Schuhf8c52252013-03-03 02:25:49 -0800821 // Lifting the disc.
822 loader_up_ = true;
823 disc_clamped_ = true;
824 disc_ejected_ = false;
825 if (loader_countdown_ > 0) {
826 --loader_countdown_;
827 break;
828 } else {
829 loader_state_ = LoaderState::LIFTED;
830 }
831 case LoaderState::LIFTED:
Austin Schuha3e8e032013-03-10 18:43:14 -0700832 LOG(DEBUG, "Loader LIFTED\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800833 // Disc lifted. Time to eject it out.
834 loader_up_ = true;
835 disc_clamped_ = true;
836 disc_ejected_ = false;
837 loader_state_ = LoaderState::SHOOTING;
838 loader_countdown_ = kShootingDelay;
839 case LoaderState::SHOOTING:
Austin Schuha3e8e032013-03-10 18:43:14 -0700840 LOG(DEBUG, "Loader SHOOTING %d\n", loader_countdown_);
Austin Schuhf8c52252013-03-03 02:25:49 -0800841 // Ejecting the disc into the shooter.
842 loader_up_ = true;
843 disc_clamped_ = false;
844 disc_ejected_ = true;
845 if (loader_countdown_ > 0) {
846 --loader_countdown_;
847 break;
848 } else {
849 loader_state_ = LoaderState::SHOOT;
850 }
851 case LoaderState::SHOOT:
Austin Schuha3e8e032013-03-10 18:43:14 -0700852 LOG(DEBUG, "Loader SHOOT\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800853 // The disc has been shot.
854 loader_up_ = true;
855 disc_clamped_ = false;
856 disc_ejected_ = true;
857 loader_state_ = LoaderState::LOWERING;
858 loader_countdown_ = kLoweringDelay;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800859 --hopper_disc_count_;
Austin Schuh70be1ba2013-03-10 13:37:17 -0700860 ++shot_disc_count_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800861 case LoaderState::LOWERING:
Austin Schuha3e8e032013-03-10 18:43:14 -0700862 LOG(DEBUG, "Loader LOWERING %d\n", loader_countdown_);
Austin Schuhf8c52252013-03-03 02:25:49 -0800863 // Lowering the loader back down.
864 loader_up_ = false;
865 disc_clamped_ = false;
866 disc_ejected_ = true;
867 if (loader_countdown_ > 0) {
868 --loader_countdown_;
869 break;
870 } else {
871 loader_state_ = LoaderState::LOWERED;
872 }
873 case LoaderState::LOWERED:
Austin Schuha3e8e032013-03-10 18:43:14 -0700874 LOG(DEBUG, "Loader LOWERED\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800875 // The indexer is lowered.
876 loader_up_ = false;
877 disc_clamped_ = false;
878 disc_ejected_ = false;
879 loader_state_ = LoaderState::READY;
880 // Once we have shot, we need to hang out in READY until otherwise
881 // notified.
882 loader_goal_ = LoaderGoal::READY;
Austin Schuhd78ab542013-03-01 22:22:19 -0800883 break;
884 }
885
886 // Update the observer.
887 wrist_loop_->Update(position != NULL, output == NULL);
888
889 if (position) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800890 LOG(DEBUG, "pos=%f\n", position->index_position);
Austin Schuhd78ab542013-03-01 22:22:19 -0800891 last_bottom_disc_detect_ = position->bottom_disc_detect;
Austin Schuh825bde92013-03-06 00:16:46 -0800892 last_top_disc_detect_ = position->top_disc_detect;
Austin Schuh6328daf2013-03-05 00:53:15 -0800893 last_bottom_disc_posedge_count_ = position->bottom_disc_posedge_count;
894 last_bottom_disc_negedge_count_ = position->bottom_disc_negedge_count;
895 last_bottom_disc_negedge_wait_count_ =
896 position->bottom_disc_negedge_wait_count;
Austin Schuh825bde92013-03-06 00:16:46 -0800897 last_top_disc_posedge_count_ = position->top_disc_posedge_count;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800898 last_top_disc_negedge_count_ = position->top_disc_negedge_count;
Austin Schuhd78ab542013-03-01 22:22:19 -0800899 }
900
901 status->hopper_disc_count = hopper_disc_count_;
902 status->total_disc_count = total_disc_count_;
Austin Schuh70be1ba2013-03-10 13:37:17 -0700903 status->shot_disc_count = shot_disc_count_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800904 status->preloaded = (loader_state_ != LoaderState::READY);
Austin Schuhd78ab542013-03-01 22:22:19 -0800905
906 if (output) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800907 output->intake_voltage = intake_voltage;
Austin Schuhf8c52252013-03-03 02:25:49 -0800908 output->transfer_voltage = transfer_voltage;
Austin Schuhd78ab542013-03-01 22:22:19 -0800909 output->index_voltage = wrist_loop_->U(0, 0);
Austin Schuhf8c52252013-03-03 02:25:49 -0800910 output->loader_up = loader_up_;
911 output->disc_clamped = disc_clamped_;
912 output->disc_ejected = disc_ejected_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800913 }
914
Austin Schuhae5fc8c2013-03-11 23:23:28 -0700915 if (safe_to_change_state) {
Austin Schuhd78ab542013-03-01 22:22:19 -0800916 safe_goal_ = goal_enum;
917 }
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800918 if (hopper_disc_count_ < 0) {
919 LOG(ERROR, "NEGATIVE DISCS. VERY VERY BAD\n");
920 }
Austin Schuhd78ab542013-03-01 22:22:19 -0800921}
922
923} // namespace control_loops
924} // namespace frc971