blob: b5ff7f84ae2984dbc9fd797cc90c97ba1b2751d6 [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 Schuh7c0e2aa2013-03-09 02:01:16 -080083 missing_position_count_(0),
84 upper_open_index_position_(0.0),
85 upper_open_index_position_was_negedge_(false),
86 lower_open_index_position_(0.0),
87 lower_open_index_position_was_negedge_(false) {
Austin Schuhd78ab542013-03-01 22:22:19 -080088}
89
Austin Schuhf8c52252013-03-03 02:25:49 -080090/*static*/ const double IndexMotor::kTransferStartPosition = 0.0;
91/*static*/ const double IndexMotor::kIndexStartPosition = 0.2159;
92/*static*/ const double IndexMotor::kIndexFreeLength =
93 IndexMotor::ConvertDiscAngleToDiscPosition((360 * 2 + 14) * M_PI / 180);
94/*static*/ const double IndexMotor::kLoaderFreeStopPosition =
95 kIndexStartPosition + kIndexFreeLength;
Austin Schuh1b864a12013-03-07 00:46:50 -080096/*static*/ const double IndexMotor::kReadyToPreload =
97 kLoaderFreeStopPosition - ConvertDiscAngleToDiscPosition(M_PI / 6.0);
Austin Schuhf8c52252013-03-03 02:25:49 -080098/*static*/ const double IndexMotor::kReadyToLiftPosition =
99 kLoaderFreeStopPosition + 0.2921;
100/*static*/ const double IndexMotor::kGrabberLength = 0.03175;
101/*static*/ const double IndexMotor::kGrabberStartPosition =
102 kReadyToLiftPosition - kGrabberLength;
Austin Schuh6328daf2013-03-05 00:53:15 -0800103/*static*/ const double IndexMotor::kGrabberMovementVelocity = 0.7;
Austin Schuhf8c52252013-03-03 02:25:49 -0800104/*static*/ const double IndexMotor::kLifterStopPosition =
105 kReadyToLiftPosition + 0.161925;
106/*static*/ const double IndexMotor::kLifterMovementVelocity = 1.0;
107/*static*/ const double IndexMotor::kEjectorStopPosition =
108 kLifterStopPosition + 0.01;
109/*static*/ const double IndexMotor::kEjectorMovementVelocity = 1.0;
Austin Schuhcc297022013-03-09 23:26:40 -0800110/*static*/ const double IndexMotor::kBottomDiscDetectStart = 0.00;
111/*static*/ const double IndexMotor::kBottomDiscDetectStop = 0.13;
112/*static*/ const double IndexMotor::kBottomDiscIndexDelay = 0.032;
Austin Schuhf8c52252013-03-03 02:25:49 -0800113
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800114// TODO(aschuh): Verify these with the sensor actually on.
Austin Schuh825bde92013-03-06 00:16:46 -0800115/*static*/ const double IndexMotor::kTopDiscDetectStart =
116 (IndexMotor::kLoaderFreeStopPosition -
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800117 IndexMotor::ConvertDiscAngleToDiscPosition(49 * M_PI / 180));
Austin Schuh825bde92013-03-06 00:16:46 -0800118/*static*/ const double IndexMotor::kTopDiscDetectStop =
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800119 (IndexMotor::kLoaderFreeStopPosition +
120 IndexMotor::ConvertDiscAngleToDiscPosition(19 * M_PI / 180));
121
122// I measured the angle between 2 discs. That then gives me the distance
123// between 2 posedges (or negedges). Then subtract off the width of the
124// positive pulse, and that gives the width of the negative pulse.
125/*static*/ const double IndexMotor::kTopDiscDetectMinSeperation =
126 (IndexMotor::ConvertDiscAngleToDiscPosition(120 * M_PI / 180) -
127 (IndexMotor::kTopDiscDetectStop - IndexMotor::kTopDiscDetectStart));
Austin Schuhf8c52252013-03-03 02:25:49 -0800128
Austin Schuhd78ab542013-03-01 22:22:19 -0800129const /*static*/ double IndexMotor::kDiscRadius = 10.875 * 0.0254 / 2;
130const /*static*/ double IndexMotor::kRollerRadius = 2.0 * 0.0254 / 2;
Austin Schuhf8c52252013-03-03 02:25:49 -0800131const /*static*/ double IndexMotor::kTransferRollerRadius = 1.25 * 0.0254 / 2;
Austin Schuhd78ab542013-03-01 22:22:19 -0800132
Austin Schuhf8c52252013-03-03 02:25:49 -0800133/*static*/ const int IndexMotor::kGrabbingDelay = 5;
134/*static*/ const int IndexMotor::kLiftingDelay = 20;
135/*static*/ const int IndexMotor::kShootingDelay = 5;
136/*static*/ const int IndexMotor::kLoweringDelay = 20;
Austin Schuhd78ab542013-03-01 22:22:19 -0800137
Austin Schuh93485832013-03-04 00:01:34 -0800138// TODO(aschuh): Tune these.
139/*static*/ const double
140 IndexMotor::IndexStateFeedbackLoop::kMinMotionVoltage = 5.0;
141/*static*/ const double
142 IndexMotor::IndexStateFeedbackLoop::kNoMotionCuttoffCount = 30;
143
Austin Schuhd78ab542013-03-01 22:22:19 -0800144// Distance to move the indexer when grabbing a disc.
145const double kNextPosition = 10.0;
146
147/*static*/ double IndexMotor::ConvertDiscAngleToIndex(const double angle) {
148 return (angle * (1 + (kDiscRadius * 2 + kRollerRadius) / kRollerRadius));
149}
150
Austin Schuhf8c52252013-03-03 02:25:49 -0800151/*static*/ double IndexMotor::ConvertDiscAngleToDiscPosition(
152 const double angle) {
Austin Schuhd78ab542013-03-01 22:22:19 -0800153 return angle * (kDiscRadius + kRollerRadius);
154}
155
Austin Schuhf8c52252013-03-03 02:25:49 -0800156/*static*/ double IndexMotor::ConvertDiscPositionToDiscAngle(
157 const double position) {
158 return position / (kDiscRadius + kRollerRadius);
159}
160
Austin Schuhd78ab542013-03-01 22:22:19 -0800161/*static*/ double IndexMotor::ConvertIndexToDiscAngle(const double angle) {
162 return (angle / (1 + (kDiscRadius * 2 + kRollerRadius) / kRollerRadius));
163}
164
165/*static*/ double IndexMotor::ConvertIndexToDiscPosition(const double angle) {
166 return IndexMotor::ConvertDiscAngleToDiscPosition(
167 ConvertIndexToDiscAngle(angle));
168}
169
Austin Schuhf8c52252013-03-03 02:25:49 -0800170/*static*/ double IndexMotor::ConvertTransferToDiscPosition(
171 const double angle) {
172 const double gear_ratio = (1 + (kDiscRadius * 2 + kTransferRollerRadius) /
173 kTransferRollerRadius);
174 return angle / gear_ratio * (kDiscRadius + kTransferRollerRadius);
175}
176
177/*static*/ double IndexMotor::ConvertDiscPositionToIndex(
178 const double position) {
179 return IndexMotor::ConvertDiscAngleToIndex(
180 ConvertDiscPositionToDiscAngle(position));
181}
182
Austin Schuh1b864a12013-03-07 00:46:50 -0800183bool IndexMotor::MinDiscPosition(double *disc_position, Frisbee **found_disc) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800184 bool found_start = false;
185 for (unsigned int i = 0; i < frisbees_.size(); ++i) {
Austin Schuh1b864a12013-03-07 00:46:50 -0800186 Frisbee &frisbee = frisbees_[i];
Austin Schuhf8c52252013-03-03 02:25:49 -0800187 if (!found_start) {
188 if (frisbee.has_position()) {
189 *disc_position = frisbee.position();
Austin Schuh1b864a12013-03-07 00:46:50 -0800190 if (found_disc) {
191 *found_disc = &frisbee;
192 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800193 found_start = true;
194 }
195 } else {
Austin Schuh1b864a12013-03-07 00:46:50 -0800196 if (frisbee.position() <= *disc_position) {
197 *disc_position = frisbee.position();
198 if (found_disc) {
199 *found_disc = &frisbee;
200 }
201 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800202 }
203 }
204 return found_start;
205}
206
Austin Schuh1b864a12013-03-07 00:46:50 -0800207bool IndexMotor::MaxDiscPosition(double *disc_position, Frisbee **found_disc) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800208 bool found_start = false;
209 for (unsigned int i = 0; i < frisbees_.size(); ++i) {
Austin Schuh1b864a12013-03-07 00:46:50 -0800210 Frisbee &frisbee = frisbees_[i];
Austin Schuhf8c52252013-03-03 02:25:49 -0800211 if (!found_start) {
212 if (frisbee.has_position()) {
213 *disc_position = frisbee.position();
Austin Schuh1b864a12013-03-07 00:46:50 -0800214 if (found_disc) {
215 *found_disc = &frisbee;
216 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800217 found_start = true;
218 }
219 } else {
Austin Schuh1b864a12013-03-07 00:46:50 -0800220 if (frisbee.position() > *disc_position) {
221 *disc_position = frisbee.position();
222 if (found_disc) {
223 *found_disc = &frisbee;
224 }
225 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800226 }
227 }
228 return found_start;
229}
230
Austin Schuh93485832013-03-04 00:01:34 -0800231void IndexMotor::IndexStateFeedbackLoop::CapU() {
232 // If the voltage has been low for a large number of cycles, cut the motor
233 // power. This is generally very bad controls practice since this isn't LTI,
234 // but we don't really care about tracking anything other than large step
235 // inputs, and the loader doesn't need to be that accurate.
236 if (::std::abs(U(0, 0)) < kMinMotionVoltage) {
237 ++low_voltage_count_;
238 if (low_voltage_count_ > kNoMotionCuttoffCount) {
239 printf("Limiting power from %f to 0\n", U(0, 0));
240 U(0, 0) = 0.0;
241 }
242 } else {
243 low_voltage_count_ = 0;
244 }
245
246 for (int i = 0; i < kNumOutputs; ++i) {
247 if (U[i] > plant.U_max[i]) {
248 U[i] = plant.U_max[i];
249 } else if (U[i] < plant.U_min[i]) {
250 U[i] = plant.U_min[i];
251 }
252 }
253}
254
255
Austin Schuhd78ab542013-03-01 22:22:19 -0800256// Positive angle is towards the shooter, and positive power is towards the
257// shooter.
258void IndexMotor::RunIteration(
259 const control_loops::IndexLoop::Goal *goal,
260 const control_loops::IndexLoop::Position *position,
261 control_loops::IndexLoop::Output *output,
262 control_loops::IndexLoop::Status *status) {
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800263 // Make goal easy to work with and sanity check it.
Austin Schuhd78ab542013-03-01 22:22:19 -0800264 Goal goal_enum = static_cast<Goal>(goal->goal_state);
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800265 if (goal->goal_state < 0 || goal->goal_state > 4) {
Brian Silverman94195052013-03-09 13:45:05 -0800266 LOG(ERROR, "Goal state is %"PRId32" which is out of range. Going to HOLD.\n",
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800267 goal->goal_state);
268 goal_enum = Goal::HOLD;
269 }
Austin Schuhd78ab542013-03-01 22:22:19 -0800270
271 // Disable the motors now so that all early returns will return with the
272 // motors disabled.
Austin Schuhb6d898b2013-03-03 15:34:35 -0800273 double intake_voltage = 0.0;
Austin Schuhf8c52252013-03-03 02:25:49 -0800274 double transfer_voltage = 0.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800275 if (output) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800276 output->intake_voltage = 0.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800277 output->transfer_voltage = 0.0;
278 output->index_voltage = 0.0;
279 }
280
281 status->ready_to_intake = false;
282
Austin Schuhf8c52252013-03-03 02:25:49 -0800283 // Compute a safe index position that we can use.
Austin Schuhd78ab542013-03-01 22:22:19 -0800284 if (position) {
285 wrist_loop_->Y << position->index_position;
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800286 // Set the goal to be the current position if this is the first time through
287 // so we don't always spin the indexer to the 0 position before starting.
288 if (no_prior_position_) {
289 wrist_loop_->R << wrist_loop_->Y(0, 0), 0.0;
290 no_prior_position_ = false;
Austin Schuh6328daf2013-03-05 00:53:15 -0800291 last_bottom_disc_posedge_count_ = position->bottom_disc_posedge_count;
292 last_bottom_disc_negedge_count_ = position->bottom_disc_negedge_count;
293 last_bottom_disc_negedge_wait_count_ =
294 position->bottom_disc_negedge_wait_count;
Austin Schuh825bde92013-03-06 00:16:46 -0800295 last_top_disc_posedge_count_ = position->top_disc_posedge_count;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800296 last_top_disc_negedge_count_ = position->top_disc_negedge_count;
297 // The open positions for the upper is right here and isn't a hard edge.
298 upper_open_index_position_ = wrist_loop_->Y(0, 0);
299 upper_open_index_position_was_negedge_ = false;
300 lower_open_index_position_ = wrist_loop_->Y(0, 0);
301 lower_open_index_position_was_negedge_ = false;
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800302 }
303
Austin Schuh1b864a12013-03-07 00:46:50 -0800304 // If the cRIO is gone for over 1/2 of a second, assume that it rebooted.
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800305 if (missing_position_count_ > 50) {
Austin Schuh6328daf2013-03-05 00:53:15 -0800306 last_bottom_disc_posedge_count_ = position->bottom_disc_posedge_count;
307 last_bottom_disc_negedge_count_ = position->bottom_disc_negedge_count;
308 last_bottom_disc_negedge_wait_count_ =
309 position->bottom_disc_negedge_wait_count;
Austin Schuh825bde92013-03-06 00:16:46 -0800310 last_top_disc_posedge_count_ = position->top_disc_posedge_count;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800311 last_top_disc_negedge_count_ = position->top_disc_negedge_count;
312 // We can't really trust the open range any more if the crio rebooted.
313 upper_open_index_position_ = wrist_loop_->Y(0, 0);
314 upper_open_index_position_was_negedge_ = false;
315 lower_open_index_position_ = wrist_loop_->Y(0, 0);
316 lower_open_index_position_was_negedge_ = false;
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800317 // Adjust the disc positions so that they don't have to move.
318 const double disc_offset =
319 position->index_position - wrist_loop_->X_hat(0, 0);
320 for (auto frisbee = frisbees_.begin();
321 frisbee != frisbees_.end(); ++frisbee) {
322 frisbee->OffsetDisc(disc_offset);
323 }
324 }
325 missing_position_count_ = 0;
326 } else {
327 ++missing_position_count_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800328 }
329 const double index_position = wrist_loop_->X_hat(0, 0);
330
Austin Schuh825bde92013-03-06 00:16:46 -0800331 if (position) {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800332 // Reset the open region if we saw a negedge.
333 if (position->top_disc_negedge_count != last_top_disc_negedge_count_) {
334 // Saw a negedge, must be a new region.
335 upper_open_index_position_ = position->top_disc_negedge_position;
336 lower_open_index_position_ = position->top_disc_negedge_position;
337 upper_open_index_position_was_negedge_ = true;
338 lower_open_index_position_was_negedge_ = true;
339 }
340
341 // No disc. Expand the open region.
342 if (!position->top_disc_detect) {
343 // If it is higher than it was before, the end of the region is no longer
344 // determined by the negedge.
345 if (index_position > upper_open_index_position_) {
346 upper_open_index_position_ = index_position;
347 upper_open_index_position_was_negedge_ = false;
348 }
349 // If it is lower than it was before, the end of the region is no longer
350 // determined by the negedge.
351 if (index_position < lower_open_index_position_) {
352 lower_open_index_position_ = index_position;
353 lower_open_index_position_was_negedge_ = false;
354 }
355 }
356
Austin Schuh825bde92013-03-06 00:16:46 -0800357 if (!position->top_disc_detect) {
358 // We don't see a disc. Verify that there are no discs that we should be
359 // seeing.
Austin Schuh1b864a12013-03-07 00:46:50 -0800360 // Assume that discs will move slow enough that we won't miss one as it
361 // goes by. They will either pile up above or below the sensor.
Austin Schuhdff24e22013-03-06 00:41:21 -0800362
363 double cumulative_offset = 0.0;
364 for (auto frisbee = frisbees_.rbegin(), rend = frisbees_.rend();
365 frisbee != rend; ++frisbee) {
366 frisbee->OffsetDisc(cumulative_offset);
367 double amount_moved = frisbee->ObserveNoTopDiscSensor(
Austin Schuh825bde92013-03-06 00:16:46 -0800368 wrist_loop_->X_hat(0, 0), wrist_loop_->X_hat(1, 0));
Austin Schuhdff24e22013-03-06 00:41:21 -0800369 cumulative_offset += amount_moved;
Austin Schuh825bde92013-03-06 00:16:46 -0800370 }
371 }
Austin Schuh1b864a12013-03-07 00:46:50 -0800372
Austin Schuh825bde92013-03-06 00:16:46 -0800373 if (position->top_disc_posedge_count != last_top_disc_posedge_count_) {
Austin Schuh1b864a12013-03-07 00:46:50 -0800374 const double index_position = wrist_loop_->X_hat(0, 0) -
375 position->index_position + position->top_disc_posedge_position;
Austin Schuh825bde92013-03-06 00:16:46 -0800376 // TODO(aschuh): Sanity check this number...
377 // Requires storing when the disc was last seen with the sensor off, and
378 // figuring out what to do if things go south.
379
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800380 // 1 if discs are going up, 0 if we have no clue, and -1 if they are going
381 // down.
382 int disc_direction = 0;
Austin Schuh825bde92013-03-06 00:16:46 -0800383 if (wrist_loop_->X_hat(1, 0) > 50.0) {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800384 disc_direction = 1;
385 } else if (wrist_loop_->X_hat(1, 0) < -50.0) {
386 disc_direction = -1;
387 } else {
388 // Save the upper and lower positions that we last saw a disc at.
389 // If there is a big buffer above, must be a disc from below.
390 // If there is a big buffer below, must be a disc from above.
391 // This should work to replace the velocity threshold above.
392
393 const double open_width =
394 upper_open_index_position_ - lower_open_index_position_;
395 const double relative_upper_open_precentage =
396 (upper_open_index_position_ - index_position) / open_width;
397 const double relative_lower_open_precentage =
398 (index_position - lower_open_index_position_) / open_width;
399 printf("Width %f upper %f lower %f\n",
400 open_width, relative_upper_open_precentage,
401 relative_lower_open_precentage);
402
403 if (ConvertIndexToDiscPosition(open_width) <
404 kTopDiscDetectMinSeperation * 0.9) {
405 LOG(ERROR, "Discs are way too close to each other. Doing nothing\n");
406 } else if (relative_upper_open_precentage > 0.75) {
407 // Looks like it is a disc going down from above since we are near
408 // the upper edge.
409 disc_direction = -1;
410 printf("Disc edge going down\n");
411 } else if (relative_lower_open_precentage > 0.75) {
412 // Looks like it is a disc going up from below since we are near
413 // the lower edge.
414 disc_direction = 1;
415 printf("Disc edge going up\n");
416 } else {
417 LOG(ERROR,
418 "Got an edge in the middle of what should be an open region.\n");
419 LOG(ERROR, "Open width: %f upper precentage %f %%\n",
420 open_width, relative_upper_open_precentage);
421 }
422 }
423
424 if (disc_direction > 0) {
Austin Schuh825bde92013-03-06 00:16:46 -0800425 // Moving up at a reasonable clip.
Austin Schuh1b864a12013-03-07 00:46:50 -0800426 // Find the highest disc that is below the top disc sensor.
427 // While we are at it, count the number above and log an error if there
428 // are too many.
429 if (frisbees_.size() == 0) {
430 Frisbee new_frisbee;
431 new_frisbee.has_been_indexed_ = true;
432 new_frisbee.index_start_position_ = index_position -
433 ConvertDiscPositionToIndex(kTopDiscDetectStart -
434 kIndexStartPosition);
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800435 ++hopper_disc_count_;
436 ++total_disc_count_;
Austin Schuh1b864a12013-03-07 00:46:50 -0800437 frisbees_.push_front(new_frisbee);
438 LOG(WARNING, "Added a disc to the hopper at the top sensor\n");
439 }
440
441 int above_disc_count = 0;
442 double highest_position = 0;
443 Frisbee *highest_frisbee_below_sensor = NULL;
444 for (auto frisbee = frisbees_.rbegin(), rend = frisbees_.rend();
445 frisbee != rend; ++frisbee) {
446 const double disc_position = frisbee->absolute_position(
447 index_position);
448 // It is save to use the top position for the cuttoff, since the
449 // sensor being low will result in discs being pushed off of it.
450 if (disc_position >= kTopDiscDetectStop) {
451 ++above_disc_count;
452 } else if (!highest_frisbee_below_sensor ||
453 disc_position > highest_position) {
454 highest_frisbee_below_sensor = &*frisbee;
455 highest_position = disc_position;
456 }
457 }
458 if (above_disc_count > 1) {
459 LOG(ERROR, "We have 2 discs above the top sensor.\n");
460 }
461
462 // We now have the disc. Shift all the ones below the sensor up by the
463 // computed delta.
464 const double disc_delta = IndexMotor::ConvertDiscPositionToIndex(
465 highest_position - kTopDiscDetectStart);
466 for (auto frisbee = frisbees_.rbegin(), rend = frisbees_.rend();
467 frisbee != rend; ++frisbee) {
468 const double disc_position = frisbee->absolute_position(
469 index_position);
470 if (disc_position < kTopDiscDetectStop) {
471 frisbee->OffsetDisc(disc_delta);
472 }
473 }
474 printf("Currently have %d discs, saw posedge moving up. "
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800475 "Moving down by %f to %f\n", frisbees_.size(),
Austin Schuh1b864a12013-03-07 00:46:50 -0800476 ConvertIndexToDiscPosition(disc_delta),
477 highest_frisbee_below_sensor->absolute_position(
478 wrist_loop_->X_hat(0, 0)));
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800479 } else if (disc_direction < 0) {
Austin Schuh825bde92013-03-06 00:16:46 -0800480 // Moving down at a reasonable clip.
Austin Schuh1b864a12013-03-07 00:46:50 -0800481 // There can only be 1 disc up top that would give us a posedge.
482 // Find it and place it at the one spot that it can be.
Brian Silverman94195052013-03-09 13:45:05 -0800483 double min_disc_position = 0;
Austin Schuh1b864a12013-03-07 00:46:50 -0800484 Frisbee *min_frisbee = NULL;
485 MinDiscPosition(&min_disc_position, &min_frisbee);
486 if (!min_frisbee) {
487 // Uh, oh, we see a disc but there isn't one...
488 LOG(ERROR, "Saw a disc up top but there isn't one in the hopper\n");
489 } else {
490 const double disc_position = min_frisbee->absolute_position(
491 index_position);
492
493 const double disc_delta_meters = disc_position - kTopDiscDetectStop;
494 const double disc_delta = IndexMotor::ConvertDiscPositionToIndex(
495 disc_delta_meters);
496 printf("Posedge going down. Moving top disc down by %f\n",
497 disc_delta_meters);
498 for (auto frisbee = frisbees_.begin(), end = frisbees_.end();
499 frisbee != end; ++frisbee) {
500 frisbee->OffsetDisc(disc_delta);
501 }
502 }
Austin Schuh825bde92013-03-06 00:16:46 -0800503 } else {
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800504 LOG(ERROR, "Not sure how to handle the upper posedge, doing nothing\n");
Austin Schuh825bde92013-03-06 00:16:46 -0800505 }
506 }
507 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800508
Austin Schuhf8c52252013-03-03 02:25:49 -0800509 // Bool to track if it is safe for the goal to change yet.
Austin Schuhd78ab542013-03-01 22:22:19 -0800510 bool safe_to_change_state_ = true;
511 switch (safe_goal_) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800512 case Goal::HOLD:
Austin Schuhd78ab542013-03-01 22:22:19 -0800513 // The goal should already be good, so sit tight with everything the same
514 // as it was.
Austin Schuhd78ab542013-03-01 22:22:19 -0800515 break;
Austin Schuhf8c52252013-03-03 02:25:49 -0800516 case Goal::READY_LOWER:
517 case Goal::INTAKE:
Austin Schuhd78ab542013-03-01 22:22:19 -0800518 {
519 Time now = Time::Now();
Austin Schuhd78ab542013-03-01 22:22:19 -0800520 if (position) {
Austin Schuh6328daf2013-03-05 00:53:15 -0800521 // Posedge of the disc entering the beam break.
522 if (position->bottom_disc_posedge_count !=
523 last_bottom_disc_posedge_count_) {
Austin Schuhd78ab542013-03-01 22:22:19 -0800524 transfer_frisbee_.Reset();
525 transfer_frisbee_.bottom_posedge_time_ = now;
526 printf("Posedge of bottom disc %f\n",
527 transfer_frisbee_.bottom_posedge_time_.ToSeconds());
528 ++hopper_disc_count_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800529 ++total_disc_count_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800530 }
531
532 // Disc exited the beam break now.
Austin Schuh6328daf2013-03-05 00:53:15 -0800533 if (position->bottom_disc_negedge_count !=
534 last_bottom_disc_negedge_count_) {
Austin Schuhd78ab542013-03-01 22:22:19 -0800535 transfer_frisbee_.bottom_negedge_time_ = now;
536 printf("Negedge of bottom disc %f\n",
537 transfer_frisbee_.bottom_negedge_time_.ToSeconds());
538 frisbees_.push_front(transfer_frisbee_);
539 }
540
541 if (position->bottom_disc_detect) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800542 intake_voltage = transfer_voltage = 12.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800543 // Must wait until the disc gets out before we can change state.
544 safe_to_change_state_ = false;
545
Austin Schuhf8c52252013-03-03 02:25:49 -0800546 // TODO(aschuh): A disc on the way through needs to start moving
547 // the indexer if it isn't already moving. Maybe?
Austin Schuhd78ab542013-03-01 22:22:19 -0800548
549 Time elapsed_posedge_time = now -
550 transfer_frisbee_.bottom_posedge_time_;
551 if (elapsed_posedge_time >= Time::InSeconds(0.3)) {
552 // It has been too long. The disc must be jammed.
553 LOG(ERROR, "Been way too long. Jammed disc?\n");
554 printf("Been way too long. Jammed disc?\n");
555 }
556 }
557
Austin Schuhf8c52252013-03-03 02:25:49 -0800558 // Check all non-indexed discs and see if they should be indexed.
Austin Schuhb6d898b2013-03-03 15:34:35 -0800559 for (auto frisbee = frisbees_.begin();
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800560 frisbee != frisbees_.end(); ++frisbee) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800561 if (!frisbee->has_been_indexed_) {
562 intake_voltage = transfer_voltage = 12.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800563
Austin Schuh6328daf2013-03-05 00:53:15 -0800564 if (last_bottom_disc_negedge_wait_count_ !=
565 position->bottom_disc_negedge_wait_count) {
566 // We have an index difference.
567 // Save the indexer position, and the time.
568 if (last_bottom_disc_negedge_wait_count_ + 1 !=
569 position->bottom_disc_negedge_wait_count) {
570 LOG(ERROR, "Funny, we got 2 edges since we last checked.\n");
571 }
572
573 // Save the captured position as the position at which the disc
574 // touched the indexer.
Austin Schuhd78ab542013-03-01 22:22:19 -0800575 LOG(INFO, "Grabbed on the index now at %f\n", index_position);
576 printf("Grabbed on the index now at %f\n", index_position);
Austin Schuhb6d898b2013-03-03 15:34:35 -0800577 frisbee->has_been_indexed_ = true;
Austin Schuh6328daf2013-03-05 00:53:15 -0800578 frisbee->index_start_position_ =
579 position->bottom_disc_negedge_wait_position;
Austin Schuhd78ab542013-03-01 22:22:19 -0800580 }
581 }
Austin Schuhb6d898b2013-03-03 15:34:35 -0800582 if (!frisbee->has_been_indexed_) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800583 // All discs must be indexed before it is safe to stop indexing.
Austin Schuhd78ab542013-03-01 22:22:19 -0800584 safe_to_change_state_ = false;
585 }
586 }
587
Austin Schuhf8c52252013-03-03 02:25:49 -0800588 // Figure out where the indexer should be to move the discs down to
589 // the right position.
Brian Silverman94195052013-03-09 13:45:05 -0800590 double max_disc_position = 0;
Austin Schuh1b864a12013-03-07 00:46:50 -0800591 if (MaxDiscPosition(&max_disc_position, NULL)) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800592 printf("There is a disc down here!\n");
593 // TODO(aschuh): Figure out what to do if grabbing the next one
594 // would cause things to jam into the loader.
595 // Say we aren't ready any more. Undefined behavior will result if
596 // that isn't observed.
597 double bottom_disc_position =
598 max_disc_position + ConvertDiscAngleToIndex(M_PI);
599 wrist_loop_->R << bottom_disc_position, 0.0;
Austin Schuhd78ab542013-03-01 22:22:19 -0800600
Austin Schuhf8c52252013-03-03 02:25:49 -0800601 // Verify that we are close enough to the goal so that we should be
602 // fine accepting the next disc.
603 double disc_error_meters = ConvertIndexToDiscPosition(
604 wrist_loop_->X_hat(0, 0) - bottom_disc_position);
605 // We are ready for the next disc if the first one is in the first
606 // half circle of the indexer. It will take time for the disc to
607 // come into the indexer, so we will be able to move it out of the
608 // way in time.
609 // This choice also makes sure that we don't claim that we aren't
610 // ready between full speed intaking.
611 if (-ConvertDiscAngleToIndex(M_PI) < disc_error_meters &&
612 disc_error_meters < 0.04) {
613 // We are only ready if we aren't being asked to change state or
614 // are full.
615 status->ready_to_intake =
616 (safe_goal_ == goal_enum) && hopper_disc_count_ < 4;
617 } else {
618 status->ready_to_intake = false;
Austin Schuhd78ab542013-03-01 22:22:19 -0800619 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800620 } else {
621 // No discs! We are always ready for more if we aren't being
622 // asked to change state.
623 status->ready_to_intake = (safe_goal_ == goal_enum);
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800624 printf("Ready to intake, zero discs. %d %d %d\n",
625 status->ready_to_intake, hopper_disc_count_, safe_goal_);
Austin Schuhd78ab542013-03-01 22:22:19 -0800626 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800627
628 // Turn on the transfer roller if we are ready.
629 if (status->ready_to_intake && hopper_disc_count_ < 4 &&
630 safe_goal_ == Goal::INTAKE) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800631 intake_voltage = transfer_voltage = 12.0;
Austin Schuhf8c52252013-03-03 02:25:49 -0800632 }
Austin Schuhd78ab542013-03-01 22:22:19 -0800633 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800634 printf("INTAKE\n");
Austin Schuhd78ab542013-03-01 22:22:19 -0800635 }
636 break;
Austin Schuhf8c52252013-03-03 02:25:49 -0800637 case Goal::READY_SHOOTER:
638 case Goal::SHOOT:
639 // Check if we have any discs to shoot or load and handle them.
Brian Silverman94195052013-03-09 13:45:05 -0800640 double min_disc_position = 0;
Austin Schuh1b864a12013-03-07 00:46:50 -0800641 if (MinDiscPosition(&min_disc_position, NULL)) {
642 const double ready_disc_position = min_disc_position +
643 ConvertDiscPositionToIndex(kReadyToPreload - kIndexStartPosition);
Austin Schuhf8c52252013-03-03 02:25:49 -0800644
645 const double grabbed_disc_position =
646 min_disc_position +
647 ConvertDiscPositionToIndex(kReadyToLiftPosition -
648 kIndexStartPosition + 0.03);
649
650 // Check the state of the loader FSM.
651 // If it is ready to load discs, position the disc so that it is ready
652 // to be grabbed.
653 // If it isn't ready, there is a disc in there. It needs to finish it's
654 // cycle first.
655 if (loader_state_ != LoaderState::READY) {
656 // We already have a disc in the loader.
657 // Stage the discs back a bit.
658 wrist_loop_->R << ready_disc_position, 0.0;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800659 printf("Loader not ready but asked to shoot\n");
Austin Schuhf8c52252013-03-03 02:25:49 -0800660
Austin Schuhbcdb90c2013-03-03 23:24:58 -0800661 // Shoot if we are grabbed and being asked to shoot.
662 if (loader_state_ == LoaderState::GRABBED &&
663 safe_goal_ == Goal::SHOOT) {
664 loader_goal_ = LoaderGoal::SHOOT_AND_RESET;
665 }
666
Austin Schuhf8c52252013-03-03 02:25:49 -0800667 // Must wait until it has been grabbed to continue.
668 if (loader_state_ == LoaderState::GRABBING) {
669 safe_to_change_state_ = false;
670 }
671 } else {
672 // No disc up top right now.
673 wrist_loop_->R << grabbed_disc_position, 0.0;
674
675 // See if the disc has gotten pretty far up yet.
676 if (wrist_loop_->X_hat(0, 0) > ready_disc_position) {
677 // Point of no return. We are committing to grabbing it now.
678 safe_to_change_state_ = false;
679 const double robust_grabbed_disc_position =
680 (grabbed_disc_position -
681 ConvertDiscPositionToIndex(kGrabberLength));
682
683 // If close, start grabbing and/or shooting.
684 if (wrist_loop_->X_hat(0, 0) > robust_grabbed_disc_position) {
685 // Start the state machine.
686 if (safe_goal_ == Goal::SHOOT) {
687 loader_goal_ = LoaderGoal::SHOOT_AND_RESET;
688 } else {
689 loader_goal_ = LoaderGoal::GRAB;
690 }
691 // This frisbee is now gone. Take it out of the queue.
692 frisbees_.pop_back();
Austin Schuhf8c52252013-03-03 02:25:49 -0800693 }
694 }
695 }
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800696 } else {
697 if (loader_state_ != LoaderState::READY) {
698 // Shoot if we are grabbed and being asked to shoot.
699 if (loader_state_ == LoaderState::GRABBED &&
700 safe_goal_ == Goal::SHOOT) {
701 loader_goal_ = LoaderGoal::SHOOT_AND_RESET;
702 }
703 } else {
704 // Ok, no discs in sight. Spin the hopper up by 150% of it's full
705 // range and verify that we don't see anything.
706 printf("Moving the indexer to verify that it is clear\n");
707 const double hopper_clear_verification_position =
708 lower_open_index_position_ +
709 ConvertDiscPositionToIndex(kIndexFreeLength) * 1.5;
710
711 wrist_loop_->R << hopper_clear_verification_position, 0.0;
712 if (::std::abs(wrist_loop_->X_hat(0, 0) -
713 hopper_clear_verification_position) <
714 ConvertDiscPositionToIndex(0.05)) {
715 printf("Should be empty\n");
716 // We are at the end of the range. There are no more discs here.
717 while (frisbees_.size() > 0) {
718 LOG(ERROR, "Dropping an extra disc since it can't exist\n");
719 frisbees_.pop_back();
720 --hopper_disc_count_;
721 --total_disc_count_;
722 }
723 if (hopper_disc_count_ != 0) {
724 LOG(ERROR,
725 "Emptied the hopper out but there are still discs there\n");
726 }
727 }
728 }
729 }
730
731 {
732 const double hopper_clear_verification_position =
733 lower_open_index_position_ +
734 ConvertDiscPositionToIndex(kIndexFreeLength) * 1.5;
735
736 if (wrist_loop_->X_hat(0, 0) >
737 hopper_clear_verification_position +
738 ConvertDiscPositionToIndex(0.05)) {
739 // We are at the end of the range. There are no more discs here.
740 while (frisbees_.size() > 0) {
741 LOG(ERROR, "Dropping an extra disc since it can't exist\n");
742 frisbees_.pop_back();
743 --hopper_disc_count_;
744 --total_disc_count_;
745 }
746 if (hopper_disc_count_ != 0) {
747 LOG(ERROR,
748 "Emptied the hopper out but there are still discs there\n");
749 }
750 }
Austin Schuhf8c52252013-03-03 02:25:49 -0800751 }
752
753 printf("READY_SHOOTER or SHOOT\n");
Austin Schuhd78ab542013-03-01 22:22:19 -0800754 break;
Austin Schuhf8c52252013-03-03 02:25:49 -0800755 }
756
757 // The only way out of the loader is to shoot the disc. The FSM can only go
758 // forwards.
759 switch (loader_state_) {
760 case LoaderState::READY:
761 printf("Loader READY\n");
762 // Open and down, ready to accept a disc.
763 loader_up_ = false;
764 disc_clamped_ = false;
765 disc_ejected_ = false;
766 if (loader_goal_ == LoaderGoal::GRAB ||
767 loader_goal_ == LoaderGoal::SHOOT_AND_RESET) {
768 if (loader_goal_ == LoaderGoal::GRAB) {
769 printf("Told to GRAB, moving on\n");
770 } else {
771 printf("Told to SHOOT_AND_RESET, moving on\n");
772 }
773 loader_state_ = LoaderState::GRABBING;
774 loader_countdown_ = kGrabbingDelay;
775 } else {
776 break;
777 }
778 case LoaderState::GRABBING:
779 printf("Loader GRABBING %d\n", loader_countdown_);
780 // Closing the grabber.
781 loader_up_ = false;
782 disc_clamped_ = true;
783 disc_ejected_ = false;
784 if (loader_countdown_ > 0) {
785 --loader_countdown_;
786 break;
787 } else {
788 loader_state_ = LoaderState::GRABBED;
789 }
790 case LoaderState::GRABBED:
791 printf("Loader GRABBED\n");
792 // Grabber closed.
793 loader_up_ = false;
794 disc_clamped_ = true;
795 disc_ejected_ = false;
796 if (loader_goal_ == LoaderGoal::SHOOT_AND_RESET) {
797 // TODO(aschuh): Only shoot if the shooter is up to speed.
798 // Seems like that would have us shooting a bit later than we could be,
799 // but it also probably spins back up real fast.
800 loader_state_ = LoaderState::LIFTING;
801 loader_countdown_ = kLiftingDelay;
802 printf("Told to SHOOT_AND_RESET, moving on\n");
803 } else if (loader_goal_ == LoaderGoal::READY) {
804 LOG(ERROR, "Can't go to ready when we have something grabbed.\n");
805 printf("Can't go to ready when we have something grabbed.\n");
806 break;
807 } else {
808 break;
809 }
810 case LoaderState::LIFTING:
811 printf("Loader LIFTING %d\n", loader_countdown_);
812 // Lifting the disc.
813 loader_up_ = true;
814 disc_clamped_ = true;
815 disc_ejected_ = false;
816 if (loader_countdown_ > 0) {
817 --loader_countdown_;
818 break;
819 } else {
820 loader_state_ = LoaderState::LIFTED;
821 }
822 case LoaderState::LIFTED:
823 printf("Loader LIFTED\n");
824 // Disc lifted. Time to eject it out.
825 loader_up_ = true;
826 disc_clamped_ = true;
827 disc_ejected_ = false;
828 loader_state_ = LoaderState::SHOOTING;
829 loader_countdown_ = kShootingDelay;
830 case LoaderState::SHOOTING:
831 printf("Loader SHOOTING %d\n", loader_countdown_);
832 // Ejecting the disc into the shooter.
833 loader_up_ = true;
834 disc_clamped_ = false;
835 disc_ejected_ = true;
836 if (loader_countdown_ > 0) {
837 --loader_countdown_;
838 break;
839 } else {
840 loader_state_ = LoaderState::SHOOT;
841 }
842 case LoaderState::SHOOT:
843 printf("Loader SHOOT\n");
844 // The disc has been shot.
845 loader_up_ = true;
846 disc_clamped_ = false;
847 disc_ejected_ = true;
848 loader_state_ = LoaderState::LOWERING;
849 loader_countdown_ = kLoweringDelay;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800850 --hopper_disc_count_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800851 case LoaderState::LOWERING:
852 printf("Loader LOWERING %d\n", loader_countdown_);
853 // Lowering the loader back down.
854 loader_up_ = false;
855 disc_clamped_ = false;
856 disc_ejected_ = true;
857 if (loader_countdown_ > 0) {
858 --loader_countdown_;
859 break;
860 } else {
861 loader_state_ = LoaderState::LOWERED;
862 }
863 case LoaderState::LOWERED:
864 printf("Loader LOWERED\n");
865 // The indexer is lowered.
866 loader_up_ = false;
867 disc_clamped_ = false;
868 disc_ejected_ = false;
869 loader_state_ = LoaderState::READY;
870 // Once we have shot, we need to hang out in READY until otherwise
871 // notified.
872 loader_goal_ = LoaderGoal::READY;
Austin Schuhd78ab542013-03-01 22:22:19 -0800873 break;
874 }
875
876 // Update the observer.
877 wrist_loop_->Update(position != NULL, output == NULL);
878
879 if (position) {
Austin Schuhf8c52252013-03-03 02:25:49 -0800880 LOG(DEBUG, "pos=%f\n", position->index_position);
Austin Schuhd78ab542013-03-01 22:22:19 -0800881 last_bottom_disc_detect_ = position->bottom_disc_detect;
Austin Schuh825bde92013-03-06 00:16:46 -0800882 last_top_disc_detect_ = position->top_disc_detect;
Austin Schuh6328daf2013-03-05 00:53:15 -0800883 last_bottom_disc_posedge_count_ = position->bottom_disc_posedge_count;
884 last_bottom_disc_negedge_count_ = position->bottom_disc_negedge_count;
885 last_bottom_disc_negedge_wait_count_ =
886 position->bottom_disc_negedge_wait_count;
Austin Schuh825bde92013-03-06 00:16:46 -0800887 last_top_disc_posedge_count_ = position->top_disc_posedge_count;
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800888 last_top_disc_negedge_count_ = position->top_disc_negedge_count;
Austin Schuhd78ab542013-03-01 22:22:19 -0800889 }
890
891 status->hopper_disc_count = hopper_disc_count_;
892 status->total_disc_count = total_disc_count_;
Austin Schuhf8c52252013-03-03 02:25:49 -0800893 status->preloaded = (loader_state_ != LoaderState::READY);
Austin Schuhd78ab542013-03-01 22:22:19 -0800894
895 if (output) {
Austin Schuhb6d898b2013-03-03 15:34:35 -0800896 output->intake_voltage = intake_voltage;
Austin Schuhf8c52252013-03-03 02:25:49 -0800897 output->transfer_voltage = transfer_voltage;
Austin Schuhd78ab542013-03-01 22:22:19 -0800898 output->index_voltage = wrist_loop_->U(0, 0);
Austin Schuhf8c52252013-03-03 02:25:49 -0800899 output->loader_up = loader_up_;
900 output->disc_clamped = disc_clamped_;
901 output->disc_ejected = disc_ejected_;
Austin Schuhd78ab542013-03-01 22:22:19 -0800902 }
903
904 if (safe_to_change_state_) {
905 safe_goal_ = goal_enum;
906 }
Austin Schuh7c0e2aa2013-03-09 02:01:16 -0800907 if (hopper_disc_count_ < 0) {
908 LOG(ERROR, "NEGATIVE DISCS. VERY VERY BAD\n");
909 }
Austin Schuhd78ab542013-03-01 22:22:19 -0800910}
911
912} // namespace control_loops
913} // namespace frc971