blob: 198cc975c6ddf3c3bde476677d1e26de32157fc1 [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"
Austin Schuh70810b92016-11-26 14:55:34 -080014#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Brian Silvermanb691f5e2015-08-02 11:37:55 -070015#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;
Brian Silverman93936f72015-03-19 23:38:30 -070023using ::frc971::control_loops::drivetrain_queue;
Austin Schuh88af0852016-12-04 20:31:32 -080024using ::y2015::control_loops::claw_queue;
25using ::y2015::control_loops::fridge::fridge_queue;
Austin Schuh47017412013-03-10 11:50:46 -070026
Austin Schuh88af0852016-12-04 20:31:32 -080027namespace y2015 {
Austin Schuh47017412013-03-10 11:50:46 -070028namespace 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");
Austin Schuh88af0852016-12-04 20:31:32 -080059 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");
Austin Schuh88af0852016-12-04 20:31:32 -080071 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);
Austin Schuh88af0852016-12-04 20:31:32 -0800103 drivetrain_queue.goal.MakeWithBuilder()
Austin Schuh1f3764e2015-04-18 23:00:09 -0700104 .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;
Austin Schuh88af0852016-12-04 20:31:32 -0800117 drivetrain_queue.status.FetchAnother();
118 double left_error =
119 ::std::abs(left_initial_position -
120 drivetrain_queue.status->estimated_left_position);
121 double right_error =
122 ::std::abs(right_initial_position -
123 drivetrain_queue.status->estimated_right_position);
Austin Schuh1f3764e2015-04-18 23:00:09 -0700124 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
Austin Schuh88af0852016-12-04 20:31:32 -0800143::std::unique_ptr<::y2015::actors::DrivetrainAction> SetDriveGoal(
144 double distance, const ProfileParams drive_params, double theta = 0,
145 const ProfileParams &turn_params = kFastTurn) {
Austin Schuha4faacc2014-03-09 00:50:50 -0800146 LOG(INFO, "Driving to %f\n", distance);
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -0800147
Austin Schuh88af0852016-12-04 20:31:32 -0800148 ::y2015::actors::DrivetrainActionParams params;
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -0800149 params.left_initial_position = left_initial_position;
150 params.right_initial_position = right_initial_position;
151 params.y_offset = distance;
152 params.theta_offset = theta;
Austin Schuh1f3764e2015-04-18 23:00:09 -0700153 params.maximum_turn_acceleration = turn_params.acceleration;
154 params.maximum_turn_velocity = turn_params.velocity;
155 params.maximum_velocity = drive_params.velocity;
156 params.maximum_acceleration = drive_params.acceleration;
Ben Fredrickson9fb2ab12015-02-16 16:42:08 -0800157 auto drivetrain_action = actors::MakeDrivetrainAction(params);
158
Austin Schuh80ff2e12014-03-08 12:06:19 -0800159 drivetrain_action->Start();
Brian Silvermanad9e0002014-04-13 14:55:57 -0700160 left_initial_position +=
161 distance - theta * constants::GetValues().turn_width / 2.0;
162 right_initial_position +=
Daniel Pettib0733be2014-11-14 22:44:03 -0800163 distance + theta * constants::GetValues().turn_width / 2.0;
Austin Schuh80ff2e12014-03-08 12:06:19 -0800164 return ::std::move(drivetrain_action);
165}
166
Austin Schuh1f3764e2015-04-18 23:00:09 -0700167const ProfileParams kFridgeYProfile{1.0, 4.0};
168const ProfileParams kFridgeXProfile{1.0, 2.0};
169const ProfileParams kFridgeFastXProfile{1.2, 5.0};
170
171static double fridge_goal_x = 0.0;
172static double fridge_goal_y = 0.0;
173
174void MoveFridge(double x, double y, bool grabbers, const ProfileParams x_params,
175 const ProfileParams y_params) {
176 auto new_fridge_goal = fridge_queue.goal.MakeMessage();
177 new_fridge_goal->profiling_type = 1;
178
179 new_fridge_goal->max_y_velocity = y_params.velocity;
180 new_fridge_goal->max_y_acceleration = y_params.acceleration;
181 new_fridge_goal->y = y;
182 fridge_goal_y = y;
183 new_fridge_goal->y_velocity = 0.0;
184
185 new_fridge_goal->max_x_velocity = x_params.velocity;
186 new_fridge_goal->max_x_acceleration = x_params.acceleration;
187 new_fridge_goal->x = x;
188 fridge_goal_x = x;
189 new_fridge_goal->x_velocity = 0.0;
190
191 new_fridge_goal->grabbers.top_front = grabbers;
192 new_fridge_goal->grabbers.top_back = grabbers;
193 new_fridge_goal->grabbers.bottom_front = grabbers;
194 new_fridge_goal->grabbers.bottom_back = grabbers;
195
196 if (!new_fridge_goal.Send()) {
197 LOG(ERROR, "Sending fridge goal failed.\n");
198 return;
Brian Silverman3b89ed82013-03-22 18:59:16 -0700199 }
Austin Schuh1f3764e2015-04-18 23:00:09 -0700200}
201
202void WaitForFridge() {
203 while (true) {
204 if (ShouldExitAuto()) return;
Austin Schuh88af0852016-12-04 20:31:32 -0800205 fridge_queue.status.FetchAnother();
Austin Schuh1f3764e2015-04-18 23:00:09 -0700206
207 constexpr double kProfileError = 1e-5;
208 constexpr double kXEpsilon = 0.03, kYEpsilon = 0.03;
209
Austin Schuh88af0852016-12-04 20:31:32 -0800210 if (fridge_queue.status->state != 4) {
Austin Schuh1f3764e2015-04-18 23:00:09 -0700211 LOG(ERROR, "Fridge no longer running, aborting action\n");
212 return;
213 }
214
Austin Schuh88af0852016-12-04 20:31:32 -0800215 if (::std::abs(fridge_queue.status->goal_x - fridge_goal_x) <
Austin Schuh1f3764e2015-04-18 23:00:09 -0700216 kProfileError &&
Austin Schuh88af0852016-12-04 20:31:32 -0800217 ::std::abs(fridge_queue.status->goal_y - fridge_goal_y) <
Austin Schuh1f3764e2015-04-18 23:00:09 -0700218 kProfileError &&
Austin Schuh88af0852016-12-04 20:31:32 -0800219 ::std::abs(fridge_queue.status->goal_x_velocity) < kProfileError &&
220 ::std::abs(fridge_queue.status->goal_y_velocity) < kProfileError) {
Austin Schuh1f3764e2015-04-18 23:00:09 -0700221 LOG(INFO, "Profile done.\n");
Austin Schuh88af0852016-12-04 20:31:32 -0800222 if (::std::abs(fridge_queue.status->x - fridge_goal_x) <
Austin Schuh1f3764e2015-04-18 23:00:09 -0700223 kXEpsilon &&
Austin Schuh88af0852016-12-04 20:31:32 -0800224 ::std::abs(fridge_queue.status->y - fridge_goal_y) <
Austin Schuh1f3764e2015-04-18 23:00:09 -0700225 kYEpsilon) {
226 LOG(INFO, "Near goal, done.\n");
227 return;
228 }
229 }
230 }
231}
232
233void InitializeEncoders() {
Austin Schuh88af0852016-12-04 20:31:32 -0800234 drivetrain_queue.status.FetchAnother();
235 left_initial_position = drivetrain_queue.status->estimated_left_position;
236 right_initial_position = drivetrain_queue.status->estimated_right_position;
Austin Schuh80ff2e12014-03-08 12:06:19 -0800237}
238
Austin Schuh1f3764e2015-04-18 23:00:09 -0700239void WaitForClawZero() {
240 LOG(INFO, "Waiting for claw to zero.\n");
241 while (true) {
242 control_loops::claw_queue.status.FetchAnother();
243 LOG_STRUCT(DEBUG, "Got claw status", *control_loops::claw_queue.status);
244 if (control_loops::claw_queue.status->zeroed) {
245 LOG(INFO, "Claw zeroed\n");
246 return;
247 }
248
249 if (ShouldExitAuto()) return;
250 }
251}
252
253void WaitForFridgeZero() {
254 LOG(INFO, "Waiting for claw to zero.\n");
255 while (true) {
Austin Schuh88af0852016-12-04 20:31:32 -0800256 fridge_queue.status.FetchAnother();
257 LOG_STRUCT(DEBUG, "Got fridge status", *fridge_queue.status);
258 if (fridge_queue.status->zeroed) {
Austin Schuh1f3764e2015-04-18 23:00:09 -0700259 LOG(INFO, "Fridge zeroed\n");
260 return;
261 }
262
263 if (ShouldExitAuto()) return;
264 }
265}
266
267constexpr ProfileParams kDefaultClawParams = {kClawAutoVelocity,
268 kClawAutoAcceleration};
269
270// Move the claw in a very small number of cycles.
271constexpr ProfileParams kInstantaneousClaw = {100.0, 100.0};
272constexpr ProfileParams kFastClaw = {8.0, 20.0};
273
274void SetClawStateNoWait(double angle, double intake_voltage,
275 double rollers_closed,
276 const ProfileParams &claw_params = kDefaultClawParams) {
Ben Fredrickson0a6ad252015-03-15 18:51:42 -0700277 auto message = control_loops::claw_queue.goal.MakeMessage();
278 message->angle = angle;
Austin Schuh1f3764e2015-04-18 23:00:09 -0700279 message->max_velocity = claw_params.velocity;
280 message->max_acceleration = claw_params.acceleration;
Ben Fredrickson0a6ad252015-03-15 18:51:42 -0700281 message->angular_velocity = 0.0;
282 message->intake = intake_voltage;
283 message->rollers_closed = rollers_closed;
284
285 LOG_STRUCT(DEBUG, "Sending claw goal", *message);
286 message.Send();
Austin Schuh1f3764e2015-04-18 23:00:09 -0700287}
Ben Fredrickson0a6ad252015-03-15 18:51:42 -0700288
Austin Schuh1f3764e2015-04-18 23:00:09 -0700289void SetClawState(double angle, double intake_voltage, double rollers_closed,
290 const ProfileParams &claw_params = kDefaultClawParams) {
291 SetClawStateNoWait(angle, intake_voltage, rollers_closed, claw_params);
Ben Fredrickson0a6ad252015-03-15 18:51:42 -0700292 while (true) {
293 control_loops::claw_queue.status.FetchAnother();
294 const double current_angle = control_loops::claw_queue.status->angle;
295 LOG_STRUCT(DEBUG, "Got claw status", *control_loops::claw_queue.status);
296
297 // I believe all we can care about here is the angle. Other values will
298 // either be set or not, but there is nothing we can do about it. If it
299 // never gets there we do not care, auto is over for us.
300 if (::std::abs(current_angle - angle) < kAngleEpsilon) {
301 break;
302 }
Austin Schuh1f3764e2015-04-18 23:00:09 -0700303 if (ShouldExitAuto()) return;
Ben Fredrickson0a6ad252015-03-15 18:51:42 -0700304 }
305}
306
Austin Schuhbb227f82015-09-06 15:27:52 -0700307void TripleCanAuto() {
Austin Schuh88af0852016-12-04 20:31:32 -0800308 ::std::unique_ptr<::y2015::actors::DrivetrainAction> drive;
309 ::std::unique_ptr<::y2015::actors::PickupAction> pickup;
310 ::std::unique_ptr<::y2015::actors::StackAction> stack;
311 ::std::unique_ptr<::y2015::actors::HeldToLiftAction> lift;
Austin Schuh80ff2e12014-03-08 12:06:19 -0800312
Austin Schuh9c414f42015-04-19 20:01:56 -0700313 actors::PickupParams pickup_params;
314 // Lift to here initially.
315 pickup_params.pickup_angle = 0.9;
316 // Start sucking here
317 pickup_params.suck_angle = 0.8;
318 // Go back down to here to finish sucking.
319 pickup_params.suck_angle_finish = 0.4;
320 // Pack the box back in here.
321 pickup_params.pickup_finish_angle = kClawTotePackAngle;
322 pickup_params.intake_time = 0.70;
323 pickup_params.intake_voltage = 7.0;
324
Austin Schuh80ff2e12014-03-08 12:06:19 -0800325 if (ShouldExitAuto()) return;
326 InitializeEncoders();
Austin Schuh1f3764e2015-04-18 23:00:09 -0700327 ResetDrivetrain();
Ben Fredrickson0a6ad252015-03-15 18:51:42 -0700328
Austin Schuh1f3764e2015-04-18 23:00:09 -0700329 WaitForClawZero();
330 WaitForFridgeZero();
Brian Silverman93936f72015-03-19 23:38:30 -0700331
Austin Schuh1f3764e2015-04-18 23:00:09 -0700332 // Initialize the fridge.
333 MoveFridge(0.0, 0.3, true, kFridgeXProfile, kFridgeYProfile);
Brian Silverman93936f72015-03-19 23:38:30 -0700334
Austin Schuh1f3764e2015-04-18 23:00:09 -0700335 LOG(INFO, "Lowering claw into position.\n");
336 SetClawState(0.0, 2.0, false, kInstantaneousClaw);
Austin Schuh9c414f42015-04-19 20:01:56 -0700337
Austin Schuh1f3764e2015-04-18 23:00:09 -0700338 LOG(INFO, "Sucking in tote.\n");
Austin Schuh9c414f42015-04-19 20:01:56 -0700339 SetClawState(0.0, 6.0, true, kInstantaneousClaw);
Brian Silverman93936f72015-03-19 23:38:30 -0700340
Austin Schuh1f3764e2015-04-18 23:00:09 -0700341 time::SleepFor(time::Time::InSeconds(0.7));
342 LOG(INFO, "Done sucking in tote\n");
Brian Silverman5bfda6c2015-03-21 23:40:13 -0700343
Austin Schuh1f3764e2015-04-18 23:00:09 -0700344 // Now pick it up
Austin Schuh9c414f42015-04-19 20:01:56 -0700345 pickup = actors::MakePickupAction(pickup_params);
346 pickup->Start();
Austin Schuh1f3764e2015-04-18 23:00:09 -0700347
348 time::SleepFor(time::Time::InSeconds(0.9));
349 // Start turning.
350 LOG(INFO, "Turning in place\n");
Austin Schuh9c414f42015-04-19 20:01:56 -0700351 drive = SetDriveGoal(0.0, kFastDrive, -0.23, kStackingFirstTurn);
Austin Schuh1f3764e2015-04-18 23:00:09 -0700352
353 WaitUntilDoneOrCanceled(::std::move(drive));
Brian Silverman5bfda6c2015-03-21 23:40:13 -0700354 if (ShouldExitAuto()) return;
355
Austin Schuh1f3764e2015-04-18 23:00:09 -0700356 LOG(INFO, "Now driving next to the can\n");
357 drive = SetDriveGoal(0.60, kFastDrive);
Brian Silverman5bfda6c2015-03-21 23:40:13 -0700358
Austin Schuh1f3764e2015-04-18 23:00:09 -0700359 WaitUntilDoneOrCanceled(::std::move(pickup));
360 if (ShouldExitAuto()) return;
361
362 // Now grab it in the fridge.
363 {
364 actors::StackParams params;
365
366 params.claw_out_angle = kClawTotePackAngle;
367 params.bottom = 0.020;
368 params.only_place = false;
369 params.arm_clearance = kArmRaiseLowerClearance;
370 params.over_box_before_place_height = 0.39;
371
372 stack = actors::MakeStackAction(params);
373 stack->Start();
374 }
375 WaitUntilDoneOrCanceled(::std::move(stack));
376 if (ShouldExitAuto()) return;
377
378 // Lower the claw to knock the tote.
379 LOG(INFO, "Lowering the claw to knock the tote\n");
380 SetClawStateNoWait(0.0, 0.0, true, kFastClaw);
381
382 time::SleepFor(time::Time::InSeconds(0.1));
383 if (ShouldExitAuto()) return;
384
385 WaitUntilDoneOrCanceled(::std::move(drive));
386 if (ShouldExitAuto()) return;
387
388 LOG(INFO, "Knocking the can over\n");
Austin Schuh9c414f42015-04-19 20:01:56 -0700389 drive = SetDriveGoal(0.40, kFastKnockDrive, 1.05, kComboTurn);
Austin Schuh1f3764e2015-04-18 23:00:09 -0700390 WaitUntilDoneOrCanceled(::std::move(drive));
391 if (ShouldExitAuto()) return;
392 {
393 actors::HeldToLiftParams params;
394 params.arm_clearance = kArmRaiseLowerClearance;
395 params.clamp_pause_time = 0.1;
396 params.before_lift_settle_time = 0.1;
397 params.bottom_height = 0.020;
398 params.claw_out_angle = kClawStackClearance;
399 params.lift_params.lift_height = kStackUpHeight;
400 params.lift_params.lift_arm = kStackUpArm;
Austin Schuh3cba3792015-04-19 20:01:22 -0700401 params.lift_params.second_lift = false;
Austin Schuh1f3764e2015-04-18 23:00:09 -0700402
403 lift = actors::MakeHeldToLiftAction(params);
404 lift->Start();
405 }
406
407 LOG(INFO, "Turning back to aim\n");
408 drive = SetDriveGoal(0.0, kFastDrive, -0.70);
409 WaitUntilDoneOrCanceled(::std::move(drive));
410 if (ShouldExitAuto()) return;
411
412 SetClawStateNoWait(0.0, 4.0, false, kFastClaw);
413 LOG(INFO, "Now driving the second tote\n");
414 drive = SetDriveGoal(1.05, kFastDrive);
415
416 // Wait until we are almost at the tote, and then start intaking.
Austin Schuh9c414f42015-04-19 20:01:56 -0700417 WaitUntilNear(0.35);
Austin Schuh1f3764e2015-04-18 23:00:09 -0700418
419 SetClawState(0.0, 6.0, true, kFastClaw);
420 WaitUntilDoneOrCanceled(::std::move(drive));
421 if (ShouldExitAuto()) return;
422
423 if (ShouldExitAuto()) return;
Austin Schuh9c414f42015-04-19 20:01:56 -0700424 time::SleepFor(time::Time::InSeconds(0.30));
425 if (ShouldExitAuto()) return;
426
427 SetClawStateNoWait(0.0, 4.0, true, kFastClaw);
428 if (ShouldExitAuto()) return;
429 time::SleepFor(time::Time::InSeconds(0.10));
Austin Schuh1f3764e2015-04-18 23:00:09 -0700430
431 WaitUntilDoneOrCanceled(::std::move(lift));
432 if (ShouldExitAuto()) return;
433
434 LOG(INFO, "Done sucking in tote\n");
435 SetClawState(0.0, 0.0, true, kFastClaw);
436 if (ShouldExitAuto()) return;
437
438 // Now pick it up
Austin Schuh9c414f42015-04-19 20:01:56 -0700439 pickup = actors::MakePickupAction(pickup_params);
440 pickup->Start();
Austin Schuh1f3764e2015-04-18 23:00:09 -0700441
Austin Schuh9c414f42015-04-19 20:01:56 -0700442 time::SleepFor(time::Time::InSeconds(1.0));
Austin Schuh1f3764e2015-04-18 23:00:09 -0700443 if (ShouldExitAuto()) return;
444
445 // Start turning.
446 LOG(INFO, "Turning in place\n");
Austin Schuh9c414f42015-04-19 20:01:56 -0700447 drive = SetDriveGoal(0.0, kStackingSecondDrive, -0.40, kStackingSecondTurn);
Austin Schuh1f3764e2015-04-18 23:00:09 -0700448
449 WaitUntilDoneOrCanceled(::std::move(pickup));
450 if (ShouldExitAuto()) return;
451
452 // Now grab it in the fridge.
453 {
454 actors::StackParams params;
455
456 params.claw_out_angle = kClawTotePackAngle;
457 params.bottom = 0.020;
458 params.only_place = false;
459 params.arm_clearance = kArmRaiseLowerClearance;
460 params.over_box_before_place_height = 0.39;
461
462 stack = actors::MakeStackAction(params);
463 stack->Start();
464 }
465
Austin Schuh1f3764e2015-04-18 23:00:09 -0700466 WaitUntilDoneOrCanceled(::std::move(drive));
467 if (ShouldExitAuto()) return;
Austin Schuh9c414f42015-04-19 20:01:56 -0700468 LOG(INFO, "Driving next to the can.\n");
469 drive = SetDriveGoal(0.65, kStackingSecondDrive);
470
471 WaitUntilDoneOrCanceled(::std::move(stack));
472 if (ShouldExitAuto()) return;
Austin Schuh1f3764e2015-04-18 23:00:09 -0700473
474 // Lower the claw to knock the tote.
475 LOG(INFO, "Lowering the claw to knock the tote\n");
476 SetClawStateNoWait(0.0, 0.0, true, kFastClaw);
477
478 // Lift the fridge.
479 MoveFridge(0.0, 0.3, true, kFridgeXProfile, kFridgeYProfile);
480
481 time::SleepFor(time::Time::InSeconds(0.1));
482 if (ShouldExitAuto()) return;
483
Austin Schuh9c414f42015-04-19 20:01:56 -0700484 WaitUntilDoneOrCanceled(::std::move(drive));
485 if (ShouldExitAuto()) return;
486
Austin Schuh1f3764e2015-04-18 23:00:09 -0700487 LOG(INFO, "Knocking the can over\n");
Austin Schuh9c414f42015-04-19 20:01:56 -0700488 drive = SetDriveGoal(0.40, kFastKnockDrive, 1.05, kComboTurn);
Austin Schuh1f3764e2015-04-18 23:00:09 -0700489 WaitUntilDoneOrCanceled(::std::move(drive));
490 if (ShouldExitAuto()) return;
491
492 LOG(INFO, "Turning back to aim\n");
493 drive = SetDriveGoal(0.0, kFastDrive, -0.60);
494 WaitUntilDoneOrCanceled(::std::move(drive));
495 if (ShouldExitAuto()) return;
496
497
498 SetClawStateNoWait(0.0, 4.0, false, kFastClaw);
499 LOG(INFO, "Now driving to the last tote\n");
500 drive = SetDriveGoal(1.05, kFastDrive);
501 WaitUntilNear(0.05);
502
503 SetClawState(0.0, 7.0, true, kFastClaw);
504 if (ShouldExitAuto()) return;
505
506 time::SleepFor(time::Time::InSeconds(0.2));
507 if (ShouldExitAuto()) return;
508
509 WaitUntilDoneOrCanceled(::std::move(drive));
510 if (ShouldExitAuto()) return;
511 SetClawState(0.0, 6.0, true, kFastClaw);
512
513 LOG(INFO, "Racing over\n");
514 //StepDrive(2.5, -1.4);
515 drive = SetDriveGoal(2.5, kRaceDrive, -1.4, kRaceTurn);
516
517 time::SleepFor(time::Time::InSeconds(0.5));
518
519 LOG(INFO, "Moving totes out\n");
520 MoveFridge(0.6, 0.32, true, kFridgeXProfile, kFridgeYProfile);
521
522 WaitForFridge();
523 if (ShouldExitAuto()) return;
524
525 LOG(INFO, "Lowering totes\n");
526 MoveFridge(0.6, 0.15, false, kFridgeXProfile, kFridgeYProfile);
527
528 WaitForFridge();
529 if (ShouldExitAuto()) return;
530
531 time::SleepFor(time::Time::InSeconds(0.1));
Brian Silverman5bfda6c2015-03-21 23:40:13 -0700532
Ben Fredrickson0a6ad252015-03-15 18:51:42 -0700533 if (ShouldExitAuto()) return;
534
Austin Schuh1f3764e2015-04-18 23:00:09 -0700535 LOG(INFO, "Retracting\n");
536 MoveFridge(0.0, 0.10, false, kFridgeFastXProfile, kFridgeYProfile);
Ben Fredrickson0a6ad252015-03-15 18:51:42 -0700537
Austin Schuh1f3764e2015-04-18 23:00:09 -0700538 SetClawState(0.0, 0.0, false, kFastClaw);
Ben Fredrickson0a6ad252015-03-15 18:51:42 -0700539
Austin Schuh1f3764e2015-04-18 23:00:09 -0700540 if (ShouldExitAuto()) return;
Ben Fredrickson0a6ad252015-03-15 18:51:42 -0700541
Austin Schuh1f3764e2015-04-18 23:00:09 -0700542 WaitUntilDoneOrCanceled(::std::move(drive));
543 if (ShouldExitAuto()) return;
Ben Fredrickson0a6ad252015-03-15 18:51:42 -0700544
Austin Schuh1f3764e2015-04-18 23:00:09 -0700545 LOG(INFO, "Backing away to let the stack ago\n");
546 drive = SetDriveGoal(-0.1, kRaceBackupDrive);
547 WaitUntilDoneOrCanceled(::std::move(drive));
Austin Schuh9c414f42015-04-19 20:01:56 -0700548
549 WaitForFridge();
550 if (ShouldExitAuto()) return;
Austin Schuh47017412013-03-10 11:50:46 -0700551}
552
Austin Schuhbb227f82015-09-06 15:27:52 -0700553void GrabberForTime(double voltage, double wait_time) {
554 ::aos::time::Time now = ::aos::time::Time::Now();
555 ::aos::time::Time end_time = now + time::Time::InSeconds(wait_time);
556 LOG(INFO, "Starting to grab at %f for %f seconds\n", voltage, wait_time);
557 while (true) {
558 autonomous::can_control.MakeWithBuilder().can_voltage(voltage).Send();
559 // Poll the running bit and auto done bits.
560 if (ShouldExitAuto()) {
561 return;
562 }
563 if (::aos::time::Time::Now() > end_time) {
564 LOG(INFO, "Done grabbing\n");
565 return;
566 }
567 ::aos::time::PhasedLoopXMS(5, 2500);
568 }
569}
570
571void CanGrabberAuto() {
572 ResetDrivetrain();
573 GrabberForTime(12.0, 0.18);
574 if (ShouldExitAuto()) return;
575
576 //GrabberForTime(4.0, 0.10);
577 if (ShouldExitAuto()) return;
578 InitializeEncoders();
579 ResetDrivetrain();
580 if (ShouldExitAuto()) return;
Austin Schuh88af0852016-12-04 20:31:32 -0800581 drivetrain_queue.goal.MakeWithBuilder()
Austin Schuhbb227f82015-09-06 15:27:52 -0700582 .control_loop_driving(true)
583 //.highgear(false)
584 .steering(0.0)
585 .throttle(0.0)
586 .left_goal(left_initial_position + 1.5)
587 .left_velocity_goal(0)
588 .right_goal(right_initial_position + 1.5)
589 .right_velocity_goal(0)
590 .Send();
591 GrabberForTime(12.0, 0.02);
592
593 GrabberForTime(4.0, 14.0);
594 if (ShouldExitAuto()) return;
595}
596
597void HandleAuto() {
598 ::aos::time::Time start_time = ::aos::time::Time::Now();
599 LOG(INFO, "Starting auto mode at %f\n", start_time.ToSeconds());
600 //TripleCanAuto();
601 CanGrabberAuto();
602}
603
Austin Schuh47017412013-03-10 11:50:46 -0700604} // namespace autonomous
Austin Schuh88af0852016-12-04 20:31:32 -0800605} // namespace y2015