blob: 4e8aa59bb88752259c04dd2e8922ef4377dde58d [file] [log] [blame]
Austin Schuh80ff2e12014-03-08 12:06:19 -08001#include <stdio.h>
2
3#include <memory>
Austin Schuh47017412013-03-10 11:50:46 -07004
Brian3afd6fc2014-04-02 20:41:49 -07005#include "aos/common/util/phased_loop.h"
Austin Schuh47017412013-03-10 11:50:46 -07006#include "aos/common/time.h"
Austin Schuh6be011a2013-03-19 10:07:02 +00007#include "aos/common/util/trapezoid_profile.h"
Brian Silverman598800f2013-05-09 17:08:42 -07008#include "aos/common/logging/logging.h"
Brian Silverman6f621542014-04-06 16:00:41 -07009#include "aos/common/logging/queue_logging.h"
Brian Silverman598800f2013-05-09 17:08:42 -070010
11#include "frc971/autonomous/auto.q.h"
Austin Schuhbb227f82015-09-06 15:27:52 -070012#include "y2015/autonomous/auto.q.h"
Brian Silvermanb691f5e2015-08-02 11:37:55 -070013#include "y2015/constants.h"
14#include "y2015/control_loops/drivetrain/drivetrain.q.h"
15#include "y2015/actors/drivetrain_actor.h"
16#include "y2015/control_loops/claw/claw.q.h"
17#include "y2015/control_loops/fridge/fridge.q.h"
18#include "y2015/actors/pickup_actor.h"
19#include "y2015/actors/stack_actor.h"
20#include "y2015/actors/held_to_lift_actor.h"
Austin Schuh47017412013-03-10 11:50:46 -070021
22using ::aos::time::Time;
Ben Fredrickson0a6ad252015-03-15 18:51:42 -070023using ::frc971::control_loops::claw_queue;
Brian Silverman5bfda6c2015-03-21 23:40:13 -070024using ::frc971::control_loops::fridge_queue;
Brian Silverman93936f72015-03-19 23:38:30 -070025using ::frc971::control_loops::drivetrain_queue;
Austin Schuh47017412013-03-10 11:50:46 -070026
27namespace frc971 {
28namespace autonomous {
29
Ben Fredrickson0a6ad252015-03-15 18:51:42 -070030constexpr double kClawAutoVelocity = 3.00;
31constexpr double kClawAutoAcceleration = 6.0;
32constexpr double kAngleEpsilon = 0.10;
Austin Schuh1f3764e2015-04-18 23:00:09 -070033const double kClawTotePackAngle = 0.90;
34const double kArmRaiseLowerClearance = -0.08;
35const double kClawStackClearance = 0.55;
36const double kStackUpHeight = 0.60;
37const double kStackUpArm = 0.0;
38
39struct ProfileParams {
40 double velocity;
41 double acceleration;
42};
Ben Fredrickson0a6ad252015-03-15 18:51:42 -070043
Austin Schuh80ff2e12014-03-08 12:06:19 -080044namespace time = ::aos::time;
45
Brian Silverman3b89ed82013-03-22 18:59:16 -070046static double left_initial_position, right_initial_position;
47
Austin Schuh6be011a2013-03-19 10:07:02 +000048bool ShouldExitAuto() {
49 ::frc971::autonomous::autonomous.FetchLatest();
50 bool ans = !::frc971::autonomous::autonomous->run_auto;
51 if (ans) {
52 LOG(INFO, "Time to exit auto mode\n");
53 }
54 return ans;
55}
56
Austin Schuh6be011a2013-03-19 10:07:02 +000057void StopDrivetrain() {
58 LOG(INFO, "Stopping the drivetrain\n");
Brian Silvermanada5f2c2015-02-01 02:41:14 -050059 control_loops::drivetrain_queue.goal.MakeWithBuilder()
Brian Silverman3b89ed82013-03-22 18:59:16 -070060 .control_loop_driving(true)
Brian Silvermance86bac2013-03-31 19:07:24 -070061 .left_goal(left_initial_position)
62 .left_velocity_goal(0)
63 .right_goal(right_initial_position)
64 .right_velocity_goal(0)
65 .quickturn(false)
66 .Send();
67}
68
69void ResetDrivetrain() {
70 LOG(INFO, "resetting the drivetrain\n");
Brian Silvermanada5f2c2015-02-01 02:41:14 -050071 control_loops::drivetrain_queue.goal.MakeWithBuilder()
Brian Silvermance86bac2013-03-31 19:07:24 -070072 .control_loop_driving(false)
Brian Silverman93335ae2015-01-26 20:43:39 -050073 //.highgear(false)
Austin Schuh6be011a2013-03-19 10:07:02 +000074 .steering(0.0)
75 .throttle(0.0)
Brian Silverman38ea9bf2014-04-19 22:57:54 -070076 .left_goal(left_initial_position)
77 .left_velocity_goal(0)
78 .right_goal(right_initial_position)
79 .right_velocity_goal(0)
Austin Schuh6be011a2013-03-19 10:07:02 +000080 .Send();
81}
82
Austin Schuh1f3764e2015-04-18 23:00:09 -070083void WaitUntilDoneOrCanceled(
84 ::std::unique_ptr<aos::common::actions::Action> action) {
85 if (!action) {
86 LOG(ERROR, "No action, not waiting\n");
87 return;
Brian Silverman3b89ed82013-03-22 18:59:16 -070088 }
Austin Schuh80ff2e12014-03-08 12:06:19 -080089 while (true) {
90 // Poll the running bit and auto done bits.
Brian Silverman547abb32015-02-16 00:37:48 -050091 ::aos::time::PhasedLoopXMS(5, 2500);
Austin Schuh80ff2e12014-03-08 12:06:19 -080092 if (!action->Running() || ShouldExitAuto()) {
93 return;
94 }
95 }
96}
97
Austin Schuh1f3764e2015-04-18 23:00:09 -070098void StepDrive(double distance, double theta) {
99 double left_goal = (left_initial_position + distance -
100 theta * constants::GetValues().turn_width / 2.0);
101 double right_goal = (right_initial_position + distance +
102 theta * constants::GetValues().turn_width / 2.0);
103 control_loops::drivetrain_queue.goal.MakeWithBuilder()
104 .control_loop_driving(true)
105 .left_goal(left_goal)
106 .right_goal(right_goal)
107 .left_velocity_goal(0.0)
108 .right_velocity_goal(0.0)
109 .Send();
110 left_initial_position = left_goal;
111 right_initial_position = right_goal;
112}
113
114void WaitUntilNear(double distance) {
115 while (true) {
116 if (ShouldExitAuto()) return;
117 control_loops::drivetrain_queue.status.FetchAnother();
118 double left_error = ::std::abs(
119 left_initial_position -
120 control_loops::drivetrain_queue.status->filtered_left_position);
121 double right_error = ::std::abs(
122 right_initial_position -
123 control_loops::drivetrain_queue.status->filtered_right_position);
124 const double kPositionThreshold = 0.05 + distance;
125 if (right_error < kPositionThreshold && left_error < kPositionThreshold) {
126 LOG(INFO, "At the goal\n");
127 return;
128 }
129 }
130}
131
132const ProfileParams kFastDrive = {2.0, 3.5};
133const ProfileParams kFastKnockDrive = {2.0, 3.0};
Austin Schuh9c414f42015-04-19 20:01:56 -0700134const ProfileParams kStackingSecondDrive = {2.0, 1.5};
Austin Schuh1f3764e2015-04-18 23:00:09 -0700135const ProfileParams kFastTurn = {3.0, 10.0};
Austin Schuh9c414f42015-04-19 20:01:56 -0700136const ProfileParams kStackingFirstTurn = {1.0, 1.0};
137const ProfileParams kStackingSecondTurn = {2.0, 6.0};
Austin Schuh1f3764e2015-04-18 23:00:09 -0700138const ProfileParams kComboTurn = {1.2, 8.0};
139const ProfileParams kRaceTurn = {4.0, 10.0};
140const ProfileParams kRaceDrive = {2.0, 2.0};
141const ProfileParams kRaceBackupDrive = {2.0, 5.0};
142
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -0800143::std::unique_ptr<::frc971::actors::DrivetrainAction> SetDriveGoal(
Austin Schuh1f3764e2015-04-18 23:00:09 -0700144 double distance, const ProfileParams drive_params, double theta = 0, const ProfileParams &turn_params = kFastTurn) {
Austin Schuha4faacc2014-03-09 00:50:50 -0800145 LOG(INFO, "Driving to %f\n", distance);
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -0800146
147 ::frc971::actors::DrivetrainActionParams params;
148 params.left_initial_position = left_initial_position;
149 params.right_initial_position = right_initial_position;
150 params.y_offset = distance;
151 params.theta_offset = theta;
Austin Schuh1f3764e2015-04-18 23:00:09 -0700152 params.maximum_turn_acceleration = turn_params.acceleration;
153 params.maximum_turn_velocity = turn_params.velocity;
154 params.maximum_velocity = drive_params.velocity;
155 params.maximum_acceleration = drive_params.acceleration;
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -0800156 auto drivetrain_action = actors::MakeDrivetrainAction(params);
157
Austin Schuh80ff2e12014-03-08 12:06:19 -0800158 drivetrain_action->Start();
Brian Silvermanad9e0002014-04-13 14:55:57 -0700159 left_initial_position +=
160 distance - theta * constants::GetValues().turn_width / 2.0;
161 right_initial_position +=
Daniel Pettib0733be2014-11-14 22:44:03 -0800162 distance + theta * constants::GetValues().turn_width / 2.0;
Austin Schuh80ff2e12014-03-08 12:06:19 -0800163 return ::std::move(drivetrain_action);
164}
165
Austin Schuh1f3764e2015-04-18 23:00:09 -0700166const ProfileParams kFridgeYProfile{1.0, 4.0};
167const ProfileParams kFridgeXProfile{1.0, 2.0};
168const ProfileParams kFridgeFastXProfile{1.2, 5.0};
169
170static double fridge_goal_x = 0.0;
171static double fridge_goal_y = 0.0;
172
173void MoveFridge(double x, double y, bool grabbers, const ProfileParams x_params,
174 const ProfileParams y_params) {
175 auto new_fridge_goal = fridge_queue.goal.MakeMessage();
176 new_fridge_goal->profiling_type = 1;
177
178 new_fridge_goal->max_y_velocity = y_params.velocity;
179 new_fridge_goal->max_y_acceleration = y_params.acceleration;
180 new_fridge_goal->y = y;
181 fridge_goal_y = y;
182 new_fridge_goal->y_velocity = 0.0;
183
184 new_fridge_goal->max_x_velocity = x_params.velocity;
185 new_fridge_goal->max_x_acceleration = x_params.acceleration;
186 new_fridge_goal->x = x;
187 fridge_goal_x = x;
188 new_fridge_goal->x_velocity = 0.0;
189
190 new_fridge_goal->grabbers.top_front = grabbers;
191 new_fridge_goal->grabbers.top_back = grabbers;
192 new_fridge_goal->grabbers.bottom_front = grabbers;
193 new_fridge_goal->grabbers.bottom_back = grabbers;
194
195 if (!new_fridge_goal.Send()) {
196 LOG(ERROR, "Sending fridge goal failed.\n");
197 return;
Brian Silverman3b89ed82013-03-22 18:59:16 -0700198 }
Austin Schuh1f3764e2015-04-18 23:00:09 -0700199}
200
201void WaitForFridge() {
202 while (true) {
203 if (ShouldExitAuto()) return;
204 control_loops::fridge_queue.status.FetchAnother();
205
206 constexpr double kProfileError = 1e-5;
207 constexpr double kXEpsilon = 0.03, kYEpsilon = 0.03;
208
209 if (control_loops::fridge_queue.status->state != 4) {
210 LOG(ERROR, "Fridge no longer running, aborting action\n");
211 return;
212 }
213
214 if (::std::abs(control_loops::fridge_queue.status->goal_x - fridge_goal_x) <
215 kProfileError &&
216 ::std::abs(control_loops::fridge_queue.status->goal_y - fridge_goal_y) <
217 kProfileError &&
218 ::std::abs(control_loops::fridge_queue.status->goal_x_velocity) <
219 kProfileError &&
220 ::std::abs(control_loops::fridge_queue.status->goal_y_velocity) <
221 kProfileError) {
222 LOG(INFO, "Profile done.\n");
223 if (::std::abs(control_loops::fridge_queue.status->x - fridge_goal_x) <
224 kXEpsilon &&
225 ::std::abs(control_loops::fridge_queue.status->y - fridge_goal_y) <
226 kYEpsilon) {
227 LOG(INFO, "Near goal, done.\n");
228 return;
229 }
230 }
231 }
232}
233
234void InitializeEncoders() {
235 control_loops::drivetrain_queue.status.FetchAnother();
Brian Silverman3b89ed82013-03-22 18:59:16 -0700236 left_initial_position =
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500237 control_loops::drivetrain_queue.status->filtered_left_position;
Brian Silverman3b89ed82013-03-22 18:59:16 -0700238 right_initial_position =
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500239 control_loops::drivetrain_queue.status->filtered_right_position;
Austin Schuh80ff2e12014-03-08 12:06:19 -0800240}
241
Austin Schuh1f3764e2015-04-18 23:00:09 -0700242void WaitForClawZero() {
243 LOG(INFO, "Waiting for claw to zero.\n");
244 while (true) {
245 control_loops::claw_queue.status.FetchAnother();
246 LOG_STRUCT(DEBUG, "Got claw status", *control_loops::claw_queue.status);
247 if (control_loops::claw_queue.status->zeroed) {
248 LOG(INFO, "Claw zeroed\n");
249 return;
250 }
251
252 if (ShouldExitAuto()) return;
253 }
254}
255
256void WaitForFridgeZero() {
257 LOG(INFO, "Waiting for claw to zero.\n");
258 while (true) {
259 control_loops::fridge_queue.status.FetchAnother();
260 LOG_STRUCT(DEBUG, "Got fridge status", *control_loops::fridge_queue.status);
261 if (control_loops::fridge_queue.status->zeroed) {
262 LOG(INFO, "Fridge zeroed\n");
263 return;
264 }
265
266 if (ShouldExitAuto()) return;
267 }
268}
269
270constexpr ProfileParams kDefaultClawParams = {kClawAutoVelocity,
271 kClawAutoAcceleration};
272
273// Move the claw in a very small number of cycles.
274constexpr ProfileParams kInstantaneousClaw = {100.0, 100.0};
275constexpr ProfileParams kFastClaw = {8.0, 20.0};
276
277void SetClawStateNoWait(double angle, double intake_voltage,
278 double rollers_closed,
279 const ProfileParams &claw_params = kDefaultClawParams) {
Ben Fredrickson0a6ad252015-03-15 18:51:42 -0700280 auto message = control_loops::claw_queue.goal.MakeMessage();
281 message->angle = angle;
Austin Schuh1f3764e2015-04-18 23:00:09 -0700282 message->max_velocity = claw_params.velocity;
283 message->max_acceleration = claw_params.acceleration;
Ben Fredrickson0a6ad252015-03-15 18:51:42 -0700284 message->angular_velocity = 0.0;
285 message->intake = intake_voltage;
286 message->rollers_closed = rollers_closed;
287
288 LOG_STRUCT(DEBUG, "Sending claw goal", *message);
289 message.Send();
Austin Schuh1f3764e2015-04-18 23:00:09 -0700290}
Ben Fredrickson0a6ad252015-03-15 18:51:42 -0700291
Austin Schuh1f3764e2015-04-18 23:00:09 -0700292void SetClawState(double angle, double intake_voltage, double rollers_closed,
293 const ProfileParams &claw_params = kDefaultClawParams) {
294 SetClawStateNoWait(angle, intake_voltage, rollers_closed, claw_params);
Ben Fredrickson0a6ad252015-03-15 18:51:42 -0700295 while (true) {
296 control_loops::claw_queue.status.FetchAnother();
297 const double current_angle = control_loops::claw_queue.status->angle;
298 LOG_STRUCT(DEBUG, "Got claw status", *control_loops::claw_queue.status);
299
300 // I believe all we can care about here is the angle. Other values will
301 // either be set or not, but there is nothing we can do about it. If it
302 // never gets there we do not care, auto is over for us.
303 if (::std::abs(current_angle - angle) < kAngleEpsilon) {
304 break;
305 }
Austin Schuh1f3764e2015-04-18 23:00:09 -0700306 if (ShouldExitAuto()) return;
Ben Fredrickson0a6ad252015-03-15 18:51:42 -0700307 }
308}
309
Austin Schuhbb227f82015-09-06 15:27:52 -0700310void TripleCanAuto() {
Ben Fredrickson0a6ad252015-03-15 18:51:42 -0700311 ::std::unique_ptr<::frc971::actors::DrivetrainAction> drive;
312 ::std::unique_ptr<::frc971::actors::PickupAction> pickup;
313 ::std::unique_ptr<::frc971::actors::StackAction> stack;
Austin Schuh1f3764e2015-04-18 23:00:09 -0700314 ::std::unique_ptr<::frc971::actors::HeldToLiftAction> lift;
Austin Schuh80ff2e12014-03-08 12:06:19 -0800315
Austin Schuh9c414f42015-04-19 20:01:56 -0700316 actors::PickupParams pickup_params;
317 // Lift to here initially.
318 pickup_params.pickup_angle = 0.9;
319 // Start sucking here
320 pickup_params.suck_angle = 0.8;
321 // Go back down to here to finish sucking.
322 pickup_params.suck_angle_finish = 0.4;
323 // Pack the box back in here.
324 pickup_params.pickup_finish_angle = kClawTotePackAngle;
325 pickup_params.intake_time = 0.70;
326 pickup_params.intake_voltage = 7.0;
327
Austin Schuh80ff2e12014-03-08 12:06:19 -0800328 if (ShouldExitAuto()) return;
329 InitializeEncoders();
Austin Schuh1f3764e2015-04-18 23:00:09 -0700330 ResetDrivetrain();
Ben Fredrickson0a6ad252015-03-15 18:51:42 -0700331
Austin Schuh1f3764e2015-04-18 23:00:09 -0700332 WaitForClawZero();
333 WaitForFridgeZero();
Brian Silverman93936f72015-03-19 23:38:30 -0700334
Austin Schuh1f3764e2015-04-18 23:00:09 -0700335 // Initialize the fridge.
336 MoveFridge(0.0, 0.3, true, kFridgeXProfile, kFridgeYProfile);
Brian Silverman93936f72015-03-19 23:38:30 -0700337
Austin Schuh1f3764e2015-04-18 23:00:09 -0700338 LOG(INFO, "Lowering claw into position.\n");
339 SetClawState(0.0, 2.0, false, kInstantaneousClaw);
Austin Schuh9c414f42015-04-19 20:01:56 -0700340
Austin Schuh1f3764e2015-04-18 23:00:09 -0700341 LOG(INFO, "Sucking in tote.\n");
Austin Schuh9c414f42015-04-19 20:01:56 -0700342 SetClawState(0.0, 6.0, true, kInstantaneousClaw);
Brian Silverman93936f72015-03-19 23:38:30 -0700343
Austin Schuh1f3764e2015-04-18 23:00:09 -0700344 time::SleepFor(time::Time::InSeconds(0.7));
345 LOG(INFO, "Done sucking in tote\n");
Brian Silverman5bfda6c2015-03-21 23:40:13 -0700346
Austin Schuh1f3764e2015-04-18 23:00:09 -0700347 // Now pick it up
Austin Schuh9c414f42015-04-19 20:01:56 -0700348 pickup = actors::MakePickupAction(pickup_params);
349 pickup->Start();
Austin Schuh1f3764e2015-04-18 23:00:09 -0700350
351 time::SleepFor(time::Time::InSeconds(0.9));
352 // Start turning.
353 LOG(INFO, "Turning in place\n");
Austin Schuh9c414f42015-04-19 20:01:56 -0700354 drive = SetDriveGoal(0.0, kFastDrive, -0.23, kStackingFirstTurn);
Austin Schuh1f3764e2015-04-18 23:00:09 -0700355
356 WaitUntilDoneOrCanceled(::std::move(drive));
Brian Silverman5bfda6c2015-03-21 23:40:13 -0700357 if (ShouldExitAuto()) return;
358
Austin Schuh1f3764e2015-04-18 23:00:09 -0700359 LOG(INFO, "Now driving next to the can\n");
360 drive = SetDriveGoal(0.60, kFastDrive);
Brian Silverman5bfda6c2015-03-21 23:40:13 -0700361
Austin Schuh1f3764e2015-04-18 23:00:09 -0700362 WaitUntilDoneOrCanceled(::std::move(pickup));
363 if (ShouldExitAuto()) return;
364
365 // Now grab it in the fridge.
366 {
367 actors::StackParams params;
368
369 params.claw_out_angle = kClawTotePackAngle;
370 params.bottom = 0.020;
371 params.only_place = false;
372 params.arm_clearance = kArmRaiseLowerClearance;
373 params.over_box_before_place_height = 0.39;
374
375 stack = actors::MakeStackAction(params);
376 stack->Start();
377 }
378 WaitUntilDoneOrCanceled(::std::move(stack));
379 if (ShouldExitAuto()) return;
380
381 // Lower the claw to knock the tote.
382 LOG(INFO, "Lowering the claw to knock the tote\n");
383 SetClawStateNoWait(0.0, 0.0, true, kFastClaw);
384
385 time::SleepFor(time::Time::InSeconds(0.1));
386 if (ShouldExitAuto()) return;
387
388 WaitUntilDoneOrCanceled(::std::move(drive));
389 if (ShouldExitAuto()) return;
390
391 LOG(INFO, "Knocking the can over\n");
Austin Schuh9c414f42015-04-19 20:01:56 -0700392 drive = SetDriveGoal(0.40, kFastKnockDrive, 1.05, kComboTurn);
Austin Schuh1f3764e2015-04-18 23:00:09 -0700393 WaitUntilDoneOrCanceled(::std::move(drive));
394 if (ShouldExitAuto()) return;
395 {
396 actors::HeldToLiftParams params;
397 params.arm_clearance = kArmRaiseLowerClearance;
398 params.clamp_pause_time = 0.1;
399 params.before_lift_settle_time = 0.1;
400 params.bottom_height = 0.020;
401 params.claw_out_angle = kClawStackClearance;
402 params.lift_params.lift_height = kStackUpHeight;
403 params.lift_params.lift_arm = kStackUpArm;
Austin Schuh3cba3792015-04-19 20:01:22 -0700404 params.lift_params.second_lift = false;
Austin Schuh1f3764e2015-04-18 23:00:09 -0700405
406 lift = actors::MakeHeldToLiftAction(params);
407 lift->Start();
408 }
409
410 LOG(INFO, "Turning back to aim\n");
411 drive = SetDriveGoal(0.0, kFastDrive, -0.70);
412 WaitUntilDoneOrCanceled(::std::move(drive));
413 if (ShouldExitAuto()) return;
414
415 SetClawStateNoWait(0.0, 4.0, false, kFastClaw);
416 LOG(INFO, "Now driving the second tote\n");
417 drive = SetDriveGoal(1.05, kFastDrive);
418
419 // Wait until we are almost at the tote, and then start intaking.
Austin Schuh9c414f42015-04-19 20:01:56 -0700420 WaitUntilNear(0.35);
Austin Schuh1f3764e2015-04-18 23:00:09 -0700421
422 SetClawState(0.0, 6.0, true, kFastClaw);
423 WaitUntilDoneOrCanceled(::std::move(drive));
424 if (ShouldExitAuto()) return;
425
426 if (ShouldExitAuto()) return;
Austin Schuh9c414f42015-04-19 20:01:56 -0700427 time::SleepFor(time::Time::InSeconds(0.30));
428 if (ShouldExitAuto()) return;
429
430 SetClawStateNoWait(0.0, 4.0, true, kFastClaw);
431 if (ShouldExitAuto()) return;
432 time::SleepFor(time::Time::InSeconds(0.10));
Austin Schuh1f3764e2015-04-18 23:00:09 -0700433
434 WaitUntilDoneOrCanceled(::std::move(lift));
435 if (ShouldExitAuto()) return;
436
437 LOG(INFO, "Done sucking in tote\n");
438 SetClawState(0.0, 0.0, true, kFastClaw);
439 if (ShouldExitAuto()) return;
440
441 // Now pick it up
Austin Schuh9c414f42015-04-19 20:01:56 -0700442 pickup = actors::MakePickupAction(pickup_params);
443 pickup->Start();
Austin Schuh1f3764e2015-04-18 23:00:09 -0700444
Austin Schuh9c414f42015-04-19 20:01:56 -0700445 time::SleepFor(time::Time::InSeconds(1.0));
Austin Schuh1f3764e2015-04-18 23:00:09 -0700446 if (ShouldExitAuto()) return;
447
448 // Start turning.
449 LOG(INFO, "Turning in place\n");
Austin Schuh9c414f42015-04-19 20:01:56 -0700450 drive = SetDriveGoal(0.0, kStackingSecondDrive, -0.40, kStackingSecondTurn);
Austin Schuh1f3764e2015-04-18 23:00:09 -0700451
452 WaitUntilDoneOrCanceled(::std::move(pickup));
453 if (ShouldExitAuto()) return;
454
455 // Now grab it in the fridge.
456 {
457 actors::StackParams params;
458
459 params.claw_out_angle = kClawTotePackAngle;
460 params.bottom = 0.020;
461 params.only_place = false;
462 params.arm_clearance = kArmRaiseLowerClearance;
463 params.over_box_before_place_height = 0.39;
464
465 stack = actors::MakeStackAction(params);
466 stack->Start();
467 }
468
Austin Schuh1f3764e2015-04-18 23:00:09 -0700469 WaitUntilDoneOrCanceled(::std::move(drive));
470 if (ShouldExitAuto()) return;
Austin Schuh9c414f42015-04-19 20:01:56 -0700471 LOG(INFO, "Driving next to the can.\n");
472 drive = SetDriveGoal(0.65, kStackingSecondDrive);
473
474 WaitUntilDoneOrCanceled(::std::move(stack));
475 if (ShouldExitAuto()) return;
Austin Schuh1f3764e2015-04-18 23:00:09 -0700476
477 // Lower the claw to knock the tote.
478 LOG(INFO, "Lowering the claw to knock the tote\n");
479 SetClawStateNoWait(0.0, 0.0, true, kFastClaw);
480
481 // Lift the fridge.
482 MoveFridge(0.0, 0.3, true, kFridgeXProfile, kFridgeYProfile);
483
484 time::SleepFor(time::Time::InSeconds(0.1));
485 if (ShouldExitAuto()) return;
486
Austin Schuh9c414f42015-04-19 20:01:56 -0700487 WaitUntilDoneOrCanceled(::std::move(drive));
488 if (ShouldExitAuto()) return;
489
Austin Schuh1f3764e2015-04-18 23:00:09 -0700490 LOG(INFO, "Knocking the can over\n");
Austin Schuh9c414f42015-04-19 20:01:56 -0700491 drive = SetDriveGoal(0.40, kFastKnockDrive, 1.05, kComboTurn);
Austin Schuh1f3764e2015-04-18 23:00:09 -0700492 WaitUntilDoneOrCanceled(::std::move(drive));
493 if (ShouldExitAuto()) return;
494
495 LOG(INFO, "Turning back to aim\n");
496 drive = SetDriveGoal(0.0, kFastDrive, -0.60);
497 WaitUntilDoneOrCanceled(::std::move(drive));
498 if (ShouldExitAuto()) return;
499
500
501 SetClawStateNoWait(0.0, 4.0, false, kFastClaw);
502 LOG(INFO, "Now driving to the last tote\n");
503 drive = SetDriveGoal(1.05, kFastDrive);
504 WaitUntilNear(0.05);
505
506 SetClawState(0.0, 7.0, true, kFastClaw);
507 if (ShouldExitAuto()) return;
508
509 time::SleepFor(time::Time::InSeconds(0.2));
510 if (ShouldExitAuto()) return;
511
512 WaitUntilDoneOrCanceled(::std::move(drive));
513 if (ShouldExitAuto()) return;
514 SetClawState(0.0, 6.0, true, kFastClaw);
515
516 LOG(INFO, "Racing over\n");
517 //StepDrive(2.5, -1.4);
518 drive = SetDriveGoal(2.5, kRaceDrive, -1.4, kRaceTurn);
519
520 time::SleepFor(time::Time::InSeconds(0.5));
521
522 LOG(INFO, "Moving totes out\n");
523 MoveFridge(0.6, 0.32, true, kFridgeXProfile, kFridgeYProfile);
524
525 WaitForFridge();
526 if (ShouldExitAuto()) return;
527
528 LOG(INFO, "Lowering totes\n");
529 MoveFridge(0.6, 0.15, false, kFridgeXProfile, kFridgeYProfile);
530
531 WaitForFridge();
532 if (ShouldExitAuto()) return;
533
534 time::SleepFor(time::Time::InSeconds(0.1));
Brian Silverman5bfda6c2015-03-21 23:40:13 -0700535
Ben Fredrickson0a6ad252015-03-15 18:51:42 -0700536 if (ShouldExitAuto()) return;
537
Austin Schuh1f3764e2015-04-18 23:00:09 -0700538 LOG(INFO, "Retracting\n");
539 MoveFridge(0.0, 0.10, false, kFridgeFastXProfile, kFridgeYProfile);
Ben Fredrickson0a6ad252015-03-15 18:51:42 -0700540
Austin Schuh1f3764e2015-04-18 23:00:09 -0700541 SetClawState(0.0, 0.0, false, kFastClaw);
Ben Fredrickson0a6ad252015-03-15 18:51:42 -0700542
Austin Schuh1f3764e2015-04-18 23:00:09 -0700543 if (ShouldExitAuto()) return;
Ben Fredrickson0a6ad252015-03-15 18:51:42 -0700544
Austin Schuh1f3764e2015-04-18 23:00:09 -0700545 WaitUntilDoneOrCanceled(::std::move(drive));
546 if (ShouldExitAuto()) return;
Ben Fredrickson0a6ad252015-03-15 18:51:42 -0700547
Austin Schuh1f3764e2015-04-18 23:00:09 -0700548 LOG(INFO, "Backing away to let the stack ago\n");
549 drive = SetDriveGoal(-0.1, kRaceBackupDrive);
550 WaitUntilDoneOrCanceled(::std::move(drive));
Austin Schuh9c414f42015-04-19 20:01:56 -0700551
552 WaitForFridge();
553 if (ShouldExitAuto()) return;
Austin Schuh47017412013-03-10 11:50:46 -0700554}
555
Austin Schuhbb227f82015-09-06 15:27:52 -0700556void GrabberForTime(double voltage, double wait_time) {
557 ::aos::time::Time now = ::aos::time::Time::Now();
558 ::aos::time::Time end_time = now + time::Time::InSeconds(wait_time);
559 LOG(INFO, "Starting to grab at %f for %f seconds\n", voltage, wait_time);
560 while (true) {
561 autonomous::can_control.MakeWithBuilder().can_voltage(voltage).Send();
562 // Poll the running bit and auto done bits.
563 if (ShouldExitAuto()) {
564 return;
565 }
566 if (::aos::time::Time::Now() > end_time) {
567 LOG(INFO, "Done grabbing\n");
568 return;
569 }
570 ::aos::time::PhasedLoopXMS(5, 2500);
571 }
572}
573
574void CanGrabberAuto() {
575 ResetDrivetrain();
576 GrabberForTime(12.0, 0.18);
577 if (ShouldExitAuto()) return;
578
579 //GrabberForTime(4.0, 0.10);
580 if (ShouldExitAuto()) return;
581 InitializeEncoders();
582 ResetDrivetrain();
583 if (ShouldExitAuto()) return;
584 control_loops::drivetrain_queue.goal.MakeWithBuilder()
585 .control_loop_driving(true)
586 //.highgear(false)
587 .steering(0.0)
588 .throttle(0.0)
589 .left_goal(left_initial_position + 1.5)
590 .left_velocity_goal(0)
591 .right_goal(right_initial_position + 1.5)
592 .right_velocity_goal(0)
593 .Send();
594 GrabberForTime(12.0, 0.02);
595
596 GrabberForTime(4.0, 14.0);
597 if (ShouldExitAuto()) return;
598}
599
600void HandleAuto() {
601 ::aos::time::Time start_time = ::aos::time::Time::Now();
602 LOG(INFO, "Starting auto mode at %f\n", start_time.ToSeconds());
603 //TripleCanAuto();
604 CanGrabberAuto();
605}
606
Austin Schuh47017412013-03-10 11:50:46 -0700607} // namespace autonomous
608} // namespace frc971