blob: ed9115824145337cb5bcf0896c4927564db67ffa [file] [log] [blame]
Austin Schuha3c148e2018-03-09 21:04:05 -08001#include "y2018/actors/autonomous_actor.h"
2
3#include <inttypes.h>
4
5#include <chrono>
6#include <cmath>
7
John Park33858a32018-09-28 23:05:48 -07008#include "aos/util/phased_loop.h"
9#include "aos/logging/logging.h"
Austin Schuha3c148e2018-03-09 21:04:05 -080010
11#include "frc971/control_loops/drivetrain/drivetrain.q.h"
12#include "y2018/control_loops/drivetrain/drivetrain_base.h"
13
14namespace y2018 {
15namespace actors {
16using ::frc971::ProfileParameters;
17
18using ::frc971::control_loops::drivetrain_queue;
19using ::aos::monotonic_clock;
20namespace chrono = ::std::chrono;
21namespace this_thread = ::std::this_thread;
22
23namespace {
24
25double DoubleSeconds(monotonic_clock::duration duration) {
26 return ::std::chrono::duration_cast<::std::chrono::duration<double>>(duration)
27 .count();
28}
29
Austin Schuh4dad8fb2018-07-08 16:00:13 -070030const ProfileParameters kFinalSwitchDrive = {0.5, 1.5};
Austin Schuhc231df42018-03-21 20:43:24 -070031const ProfileParameters kDrive = {5.0, 2.5};
Austin Schuhf79c0e52018-04-04 20:13:21 -070032const ProfileParameters kThirdBoxDrive = {3.0, 2.5};
Austin Schuhc231df42018-03-21 20:43:24 -070033const ProfileParameters kSlowDrive = {1.5, 2.5};
Austin Schuhf79c0e52018-04-04 20:13:21 -070034const ProfileParameters kScaleTurnDrive = {3.0, 2.5};
Austin Schuhc231df42018-03-21 20:43:24 -070035const ProfileParameters kFarSwitchTurnDrive = {2.0, 2.5};
Austin Schuh4dad8fb2018-07-08 16:00:13 -070036const ProfileParameters kFarScaleFinalTurnDrive = kFarSwitchTurnDrive;
Austin Schuhc231df42018-03-21 20:43:24 -070037const ProfileParameters kTurn = {4.0, 2.0};
38const ProfileParameters kSweepingTurn = {5.0, 7.0};
Austin Schuh4dad8fb2018-07-08 16:00:13 -070039const ProfileParameters kFarScaleSweepingTurn = kSweepingTurn;
Austin Schuhc231df42018-03-21 20:43:24 -070040const ProfileParameters kFastTurn = {5.0, 7.0};
Austin Schuhf79c0e52018-04-04 20:13:21 -070041const ProfileParameters kReallyFastTurn = {1.5, 9.0};
42
43const ProfileParameters kThirdBoxSlowBackup = {0.35, 1.5};
44const ProfileParameters kThirdBoxSlowTurn = {1.5, 4.0};
45
Austin Schuh4dad8fb2018-07-08 16:00:13 -070046const ProfileParameters kThirdBoxPlaceDrive = {4.0, 2.0};
Austin Schuhf79c0e52018-04-04 20:13:21 -070047
48const ProfileParameters kFinalFrontFarSwitchDrive = {2.0, 2.0};
Austin Schuha3c148e2018-03-09 21:04:05 -080049
50} // namespace
51
52AutonomousActor::AutonomousActor(
Austin Schuheb99d072019-05-12 21:03:38 -070053 ::aos::EventLoop *event_loop,
Austin Schuha3c148e2018-03-09 21:04:05 -080054 ::frc971::autonomous::AutonomousActionQueueGroup *s)
55 : frc971::autonomous::BaseAutonomousActor(
Austin Schuheb99d072019-05-12 21:03:38 -070056 event_loop, s, control_loops::drivetrain::GetDrivetrainConfig()) {}
Austin Schuha3c148e2018-03-09 21:04:05 -080057
58bool AutonomousActor::RunAction(
59 const ::frc971::autonomous::AutonomousActionParams &params) {
60 monotonic_clock::time_point start_time = monotonic_clock::now();
61 LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode);
62 Reset();
63
Austin Schuh4dad8fb2018-07-08 16:00:13 -070064 // Switch
65 /*
Austin Schuhc231df42018-03-21 20:43:24 -070066 switch (params.mode) {
Austin Schuh4dad8fb2018-07-08 16:00:13 -070067 case 0:
68 case 2: {
69 if (FarSwitch(start_time, true)) return true;
70 } break;
71
72 case 3:
Austin Schuhc231df42018-03-21 20:43:24 -070073 case 1: {
Austin Schuh4dad8fb2018-07-08 16:00:13 -070074 if (CloseSwitch(start_time)) return true;
Austin Schuhc231df42018-03-21 20:43:24 -070075 } break;
Austin Schuh4dad8fb2018-07-08 16:00:13 -070076 }
77 */
78 // Scale
79 switch (params.mode) {
80 case 0:
81 case 1: {
82 if (FarScale(start_time)) return true;
Austin Schuhc231df42018-03-21 20:43:24 -070083 } break;
84
85 case 3:
86 case 2: {
Austin Schuh4dad8fb2018-07-08 16:00:13 -070087 if (ThreeCubeAuto(start_time)) return true;
Austin Schuhc231df42018-03-21 20:43:24 -070088 } break;
Austin Schuha3c148e2018-03-09 21:04:05 -080089 }
Austin Schuh4dad8fb2018-07-08 16:00:13 -070090 /*
91 switch (params.mode) {
92 case 1: {
93 if (FarScale(start_time)) return true;
94 //if (CloseSwitch(start_time)) return true;
95 } break;
96 case 0: {
97 if (DriveStraight()) return true;
98 } break;
99 case 200: {
100 if (FarSwitch(start_time)) return true;
101 } break;
102
103 case 3:
104 case 2: {
105 if (ThreeCubeAuto(start_time)) {
106 return true;
107 }
108 } break;
109 }
110 */
Austin Schuha3c148e2018-03-09 21:04:05 -0800111
112 LOG(INFO, "Done %f\n", DoubleSeconds(monotonic_clock::now() - start_time));
113
114 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
115 ::std::chrono::milliseconds(5) / 2);
116
117 while (!ShouldCancel()) {
118 phased_loop.SleepUntilNext();
119 }
120 LOG(DEBUG, "Done running\n");
121
122 return true;
123}
124
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700125bool AutonomousActor::FarSwitch(monotonic_clock::time_point start_time,
126 bool drive_behind, bool left) {
127 const double turn_scalar = left ? 1.0 : -1.0;
128 if (drive_behind) {
129 // Start on the left. Hit the switch.
130 constexpr double kFullDriveLength = 9.93;
131 constexpr double kTurnDistance = 4.40;
132 StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn);
133 set_arm_goal_position(arm::NeutralIndex());
134 set_grab_box(false);
135 SendSuperstructureGoal();
136 if (!WaitForDriveProfileNear(kFullDriveLength - (kTurnDistance - 1.4)))
137 return true;
138 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn);
139
140 if (!WaitForDriveProfileNear(kFullDriveLength - kTurnDistance)) return true;
141 StartDrive(0.0, turn_scalar * (-M_PI / 2.0), kFarSwitchTurnDrive, kSweepingTurn);
142 if (!WaitForTurnProfileDone()) return true;
143
144 // Now, close so let's move the arm up.
145 set_arm_goal_position(arm::FrontSwitchAutoIndex());
146 SendSuperstructureGoal();
147
148 StartDrive(0.0, 0.0, kDrive, kTurn);
149 if (!WaitForDriveProfileNear(2.0)) return true;
150 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kFastTurn);
151
152 if (!WaitForDriveProfileNear(1.3)) return true;
153
154 StartDrive(0.0, turn_scalar * M_PI / 2.0, kFarSwitchTurnDrive, kFastTurn);
155 if (!WaitForTurnProfileDone()) return true;
156
157 constexpr double kGentlePushDrive = 0.70;
158 StartDrive(kGentlePushDrive, 0.0, kSlowDrive, kTurn);
159
160 if (!WaitForDriveProfileNear(kGentlePushDrive - 0.25)) return true;
161 // Turn down the peak voltage when we push against the wall so we don't blow
162 // breakers or pull the battery voltage down too far.
163 set_max_drivetrain_voltage(6.0);
164 StartDrive(0.00, 0.0, kFinalSwitchDrive, kTurn);
165
166 if (!WaitForArmTrajectoryClose(0.001)) return true;
167 LOG(INFO, "Arm close at %f\n",
168 DoubleSeconds(monotonic_clock::now() - start_time));
169
170 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
171
172 set_open_claw(true);
173 SendSuperstructureGoal();
174
175 ::std::this_thread::sleep_for(chrono::milliseconds(1500));
176 set_arm_goal_position(arm::NeutralIndex());
177 SendSuperstructureGoal();
178 if (ShouldCancel()) return true;
179 set_max_drivetrain_voltage(12.0);
180 StartDrive(-0.5, 0.0, kDrive, kTurn);
181 if (!WaitForDriveProfileDone()) return true;
182 } else {
183 // Start on the left. Hit the switch.
184 constexpr double kFullDriveLength = 5.55;
185 constexpr double kTurnDistance = 0.35;
186 StartDrive(-kFullDriveLength, 0.0, kFarSwitchTurnDrive, kTurn);
187
188 if (!WaitForDriveProfileNear(kFullDriveLength - kTurnDistance)) return true;
189 StartDrive(0.0, turn_scalar * (-M_PI / 2.0), kFarSwitchTurnDrive,
190 kSweepingTurn);
191 if (!WaitForTurnProfileDone()) return true;
192 StartDrive(0.0, 0.0, kDrive, kTurn);
193 if (!WaitForDriveProfileDone()) return true;
194
195 // Now, close so let's move the arm up.
196 set_arm_goal_position(arm::FrontSwitchIndex());
197 SendSuperstructureGoal();
198
199 StartDrive(0.0, turn_scalar * (-M_PI / 2.0), kDrive, kFastTurn);
200 if (!WaitForTurnProfileDone()) return true;
201
202 set_max_drivetrain_voltage(10.0);
203 StartDrive(1.1, 0.0, kDrive, kTurn);
204 if (!WaitForArmTrajectoryClose(0.001)) return true;
205 if (!WaitForDriveNear(0.6, M_PI / 2.0)) return true;
206 StartDrive(0.0, 0.0, kFinalFrontFarSwitchDrive, kTurn);
207
208 if (!WaitForDriveNear(0.3, M_PI / 2.0)) return true;
209 set_max_drivetrain_voltage(6.0);
210 StartDrive(0.0, 0.0, kFinalFrontFarSwitchDrive, kTurn);
211
212 // if (!WaitForDriveNear(0.2, M_PI / 2.0)) return true;
213 ::std::this_thread::sleep_for(chrono::milliseconds(300));
214
215 set_open_claw(true);
216 SendSuperstructureGoal();
217
218 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
219 set_arm_goal_position(arm::NeutralIndex());
220 SendSuperstructureGoal();
221 if (ShouldCancel()) return true;
222 set_max_drivetrain_voltage(12.0);
223 StartDrive(-0.5, 0.0, kDrive, kTurn);
224 if (!WaitForDriveProfileDone()) return true;
225 }
226 return false;
227}
228
229bool AutonomousActor::FarScale(monotonic_clock::time_point start_time) {
230 // Start on the left. Hit the switch.
231 constexpr double kFullDriveLength = 11.40;
232 constexpr double kFirstTurnDistance = 4.40;
233 constexpr double kSecondTurnDistance = 9.30;
234 StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn);
235 if (!WaitForDriveProfileNear(kFullDriveLength - (kFirstTurnDistance - 1.4)))
236 return true;
237 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn);
238
239 if (!WaitForDriveProfileNear(kFullDriveLength - kFirstTurnDistance)) return true;
240 StartDrive(0.0, -M_PI / 2.0, kFarSwitchTurnDrive, kSweepingTurn);
241 set_arm_goal_position(arm::BackHighBoxIndex());
242 SendSuperstructureGoal();
243 if (!WaitForTurnProfileDone()) return true;
244
245 StartDrive(0.0, 0.0, kDrive, kTurn);
246
247 if (!WaitForDriveProfileNear(kFullDriveLength - (kSecondTurnDistance - 1.4)))
248 return true;
249
250 StartDrive(0.0, 0.0, kFarScaleFinalTurnDrive, kTurn);
251 if (!WaitForDriveProfileNear(kFullDriveLength - (kSecondTurnDistance)))
252 return true;
253 LOG(INFO, "Final turn at %f\n",
254 DoubleSeconds(monotonic_clock::now() - start_time));
255
256 StartDrive(0.0, M_PI / 2.0, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
257 if (!WaitForDriveProfileNear(0.15)) return true;
258
259 StartDrive(0.0, 0.3, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
260
261 LOG(INFO, "Dropping at %f\n",
262 DoubleSeconds(monotonic_clock::now() - start_time));
263 set_open_claw(true);
264 SendSuperstructureGoal();
265
266 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
267 LOG(INFO, "Backing up at %f\n",
268 DoubleSeconds(monotonic_clock::now() - start_time));
269
270 StartDrive(1.5, -0.55, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
271
272 if (!WaitForDriveProfileNear(1.4)) return true;
273 set_arm_goal_position(arm::NeutralIndex());
274 set_open_claw(false);
275 set_grab_box(true);
276 SendSuperstructureGoal();
277
278 if (!WaitForDriveProfileNear(0.3)) return true;
279
280 set_intake_angle(0.15);
281 set_roller_voltage(10.0);
282 SendSuperstructureGoal();
283
284 set_max_drivetrain_voltage(6.0);
285 StartDrive(0.0, 0.0, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
286
287 if (!WaitForDriveProfileDone()) return true;
288 if (!WaitForTurnProfileDone()) return true;
289
290 StartDrive(0.2, 0.0, kThirdBoxSlowBackup, kThirdBoxSlowTurn);
291
292 if (!WaitForBoxGrabed()) return true;
293 set_arm_goal_position(arm::BackHighBoxIndex());
294 set_intake_angle(-3.0);
295 set_roller_voltage(0.0);
296 set_grab_box(false);
297 SendSuperstructureGoal();
298
299 StartDrive(-1.40, 0.15, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
300
301 if (!WaitForDriveProfileNear(0.1)) return true;
302
303 StartDrive(0.0, 0.5, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
304
305 if (!WaitForDriveProfileDone()) return true;
306 if (!WaitForTurnProfileDone()) return true;
307 ::std::this_thread::sleep_for(chrono::milliseconds(500));
308
309 LOG(INFO, "Dropping second box at %f\n",
310 DoubleSeconds(monotonic_clock::now() - start_time));
311 set_open_claw(true);
312 SendSuperstructureGoal();
313
314 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
315
316 StartDrive(1.4, -0.7, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
317 ::std::this_thread::sleep_for(chrono::milliseconds(200));
318 set_arm_goal_position(arm::NeutralIndex());
319 set_open_claw(false);
320 SendSuperstructureGoal();
321
322 if (!WaitForDriveProfileNear(1.0)) return true;
323
324 StartDrive(0.0, -0.6, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
325 return false;
326}
327
328bool AutonomousActor::FarReadyScale(monotonic_clock::time_point start_time) {
329 // Start on the left. Hit the switch.
330 constexpr double kFullDriveLength = 7.5;
331 constexpr double kFirstTurnDistance = 4.40;
332 StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn);
333 if (!WaitForDriveProfileNear(kFullDriveLength - (kFirstTurnDistance - 1.4)))
334 return true;
335 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn);
336
337 if (!WaitForDriveProfileNear(kFullDriveLength - kFirstTurnDistance)) return true;
338 StartDrive(0.0, -M_PI / 2.0, kFarSwitchTurnDrive, kSweepingTurn);
339 set_arm_goal_position(arm::UpIndex());
340 SendSuperstructureGoal();
341 LOG(INFO, "Lifting arm at %f\n",
342 DoubleSeconds(monotonic_clock::now() - start_time));
343 if (!WaitForTurnProfileDone()) return true;
344
345 StartDrive(0.0, 0.0, kDrive, kTurn);
346 return false;
347}
348
349bool AutonomousActor::DriveStraight() {
350 StartDrive(-3.2, 0.0, kDrive, kTurn);
351 if (!WaitForDriveProfileDone()) return true;
352 return false;
353}
354
355bool AutonomousActor::CloseSwitch(monotonic_clock::time_point start_time,
356 bool left) {
357 const double turn_scalar = left ? 1.0 : -1.0;
358
359 constexpr double kDriveDistance = 3.2;
360 // Start on the left. Drive, arc a turn, and drop in the close switch.
361 StartDrive(-kDriveDistance, 0.0, kDrive, kTurn);
362 if (!WaitForDriveNear(2.5, M_PI / 2.0)) return true;
363
364 // Now, close so let's move the arm up.
365 set_arm_goal_position(arm::BackSwitchIndex());
366 SendSuperstructureGoal();
367
368 StartDrive(0.0, 0.0, kSlowDrive, kSweepingTurn);
369 if (!WaitForDriveNear(1.6, M_PI / 2.0)) return true;
370
371 StartDrive(0.0, turn_scalar * (-M_PI / 4.0 - 0.2), kSlowDrive, kSweepingTurn);
372 if (!WaitForDriveNear(0.2, 0.2)) return true;
373 set_max_drivetrain_voltage(6.0);
374 LOG(INFO, "Lowered drivetrain voltage %f\n",
375 DoubleSeconds(monotonic_clock::now() - start_time));
376 ::std::this_thread::sleep_for(chrono::milliseconds(300));
377
378 set_open_claw(true);
379 SendSuperstructureGoal();
380
381 ::std::this_thread::sleep_for(chrono::milliseconds(500));
382 set_max_drivetrain_voltage(12.0);
383 StartDrive(0.7, 0.0, kDrive, kTurn);
384 if (!WaitForTurnProfileDone()) return true;
385
386 set_arm_goal_position(arm::NeutralIndex());
387 SendSuperstructureGoal();
388 return false;
389}
390
391bool AutonomousActor::ThreeCubeAuto(monotonic_clock::time_point start_time) {
392 // Start on the left. Hit the scale.
393 constexpr double kDriveDistance = 6.95;
394 // Distance and angle to do the big drive to the third cube.
395 constexpr double kThirdCubeDrive = 1.57 + 0.05 + 0.05;
396 constexpr double kThirdCubeTurn = M_PI / 4.0 + 0.1;
397 // Angle to do the slow pickup turn on the third box.
398 constexpr double kThirdBoxEndTurnAngle = 0.30;
399
400 // Distance to drive back to the scale with the third cube.
401 constexpr double kThirdCubeDropDrive = kThirdCubeDrive + 0.40;
402
403 // Drive.
404 StartDrive(-kDriveDistance, 0.0, kDrive, kTurn);
405 if (!WaitForDriveNear(kDriveDistance - 1.0, M_PI / 2.0)) return true;
406 // Once we are away from the wall, start the arm.
407 set_arm_goal_position(arm::BackMiddle2BoxIndex());
408 SendSuperstructureGoal();
409
410 // We are starting to get close. Slow down for the turn.
411 if (!WaitForDriveNear(4.0, M_PI / 2.0)) return true;
412 StartDrive(0.0, 0.0, kScaleTurnDrive, kFastTurn);
413
414 // Once we've gotten slowed down a bit, start turning.
415 if (!WaitForDriveNear(3.25, M_PI / 2.0)) return true;
416 StartDrive(0.0, -M_PI / 6.0, kScaleTurnDrive, kFastTurn);
417
418 if (!WaitForDriveNear(1.0, M_PI / 2.0)) return true;
419 StartDrive(0.0, M_PI / 6.0, kScaleTurnDrive, kFastTurn);
420
421 // Get close and open the claw.
422 if (!WaitForDriveNear(0.15, 0.25)) return true;
423 set_open_claw(true);
424 SendSuperstructureGoal();
425 set_intake_angle(-0.60);
426 LOG(INFO, "Dropped first box %f\n",
427 DoubleSeconds(monotonic_clock::now() - start_time));
428
429 ::std::this_thread::sleep_for(chrono::milliseconds(700));
430
431 set_grab_box(true);
432 SendSuperstructureGoal();
433
434 LOG(INFO, "Starting second box drive %f\n",
435 DoubleSeconds(monotonic_clock::now() - start_time));
436 constexpr double kSecondBoxSwerveAngle = 0.35;
437 constexpr double kSecondBoxDrive = 1.38;
438 StartDrive(kSecondBoxDrive, 0.0, kDrive, kFastTurn);
439 if (!WaitForDriveNear(kSecondBoxDrive - 0.2, M_PI / 2.0)) return true;
440
441 StartDrive(0.0, kSecondBoxSwerveAngle, kDrive, kFastTurn);
442 if (!WaitForDriveNear(0.5, M_PI / 2.0)) return true;
443
444 set_open_claw(true);
445 set_disable_box_correct(false);
446 SendSuperstructureGoal();
447
448 StartDrive(0.0, -kSecondBoxSwerveAngle, kDrive, kFastTurn);
449
450 if (!WaitForDriveProfileDone()) return true;
451 set_max_drivetrain_voltage(6.0);
452 StartDrive(0.0, 0.0, kDrive, kFastTurn);
453
454 set_intake_angle(0.15);
455 set_arm_goal_position(arm::BackHighBoxIndex());
456 set_open_claw(false);
457
458 set_roller_voltage(10.0);
459 SendSuperstructureGoal();
460
461 LOG(INFO, "Grabbing second box %f\n",
462 DoubleSeconds(monotonic_clock::now() - start_time));
463 ::std::this_thread::sleep_for(chrono::milliseconds(200));
464 StartDrive(-0.04, 0.0, kThirdBoxSlowBackup, kThirdBoxSlowTurn);
465
466 if (!WaitForBoxGrabed()) return true;
467 set_max_drivetrain_voltage(12.0);
468
469 LOG(INFO, "Got second box %f\n",
470 DoubleSeconds(monotonic_clock::now() - start_time));
471 ::std::this_thread::sleep_for(chrono::milliseconds(500));
472
473 set_grab_box(false);
474 //set_arm_goal_position(arm::UpIndex());
475 set_arm_goal_position(arm::BackHighBoxIndex());
476 set_roller_voltage(0.0);
477 set_disable_box_correct(false);
478 SendSuperstructureGoal();
479 LOG(INFO, "Driving to place second box %f\n",
480 DoubleSeconds(monotonic_clock::now() - start_time));
481
482 StartDrive(-kSecondBoxDrive + 0.16, kSecondBoxSwerveAngle, kDrive, kFastTurn);
483 if (!WaitForDriveNear(0.4, M_PI / 2.0)) return true;
484
485 constexpr double kSecondBoxEndExtraAngle = 0.3;
486 StartDrive(0.0, -kSecondBoxSwerveAngle - kSecondBoxEndExtraAngle, kDrive,
487 kFastTurn);
488
489 LOG(INFO, "Starting throw %f\n",
490 DoubleSeconds(monotonic_clock::now() - start_time));
491 if (!WaitForDriveNear(0.4, M_PI / 2.0)) return true;
492 if (!WaitForArmTrajectoryClose(0.25)) return true;
493 SendSuperstructureGoal();
494
495 // Throw the box.
496 //if (!WaitForArmTrajectoryClose(0.20)) return true;
497 if (!WaitForDriveNear(0.2, M_PI / 2.0)) return true;
498
499 set_open_claw(true);
500 set_intake_angle(-M_PI / 4.0);
501 LOG(INFO, "Releasing second box %f\n",
502 DoubleSeconds(monotonic_clock::now() - start_time));
503 SendSuperstructureGoal();
504
505 ::std::this_thread::sleep_for(chrono::milliseconds(700));
506 set_open_claw(false);
507 SendSuperstructureGoal();
508
509 LOG(INFO, "Driving to third box %f\n",
510 DoubleSeconds(monotonic_clock::now() - start_time));
511 StartDrive(kThirdCubeDrive, kSecondBoxEndExtraAngle, kThirdBoxDrive,
512 kFastTurn);
513 if (!WaitForDriveNear(kThirdCubeDrive - 0.1, M_PI / 4.0)) return true;
514 set_grab_box(true);
515 SendSuperstructureGoal();
516
517 StartDrive(0.0, kThirdCubeTurn, kThirdBoxDrive, kFastTurn);
518 if (!WaitForDriveNear(0.05, M_PI / 4.0)) return true;
519
520 set_intake_angle(0.05);
521 set_roller_voltage(9.0);
522 SendSuperstructureGoal();
523
524 if (!WaitForDriveProfileDone()) return true;
525 if (!WaitForTurnProfileDone()) return true;
526 StartDrive(0.35, kThirdBoxEndTurnAngle, kThirdBoxSlowBackup,
527 kThirdBoxSlowTurn);
528 if (!WaitForDriveProfileDone()) return true;
529
530 LOG(INFO, "Waiting for third box %f\n",
531 DoubleSeconds(monotonic_clock::now() - start_time));
532 if (!WaitForBoxGrabed()) return true;
533 LOG(INFO, "Third box grabbed %f\n",
534 DoubleSeconds(monotonic_clock::now() - start_time));
535 const bool too_late =
536 monotonic_clock::now() > start_time + chrono::milliseconds(12500);
537 if (too_late) {
538 LOG(INFO, "Third box too long, going up. %f\n",
539 DoubleSeconds(monotonic_clock::now() - start_time));
540 set_grab_box(false);
541 set_arm_goal_position(arm::UpIndex());
542 set_roller_voltage(0.0);
543 SendSuperstructureGoal();
544 }
545 ::std::this_thread::sleep_for(chrono::milliseconds(400));
546
547 set_grab_box(false);
548 if (!too_late) {
549 set_arm_goal_position(arm::BackMiddle2BoxIndex());
550 }
551 set_roller_voltage(0.0);
552 SendSuperstructureGoal();
553
554 StartDrive(-kThirdCubeDropDrive, 0.0, kThirdBoxPlaceDrive, kReallyFastTurn);
555
556 if (!WaitForDriveNear(kThirdCubeDropDrive - 0.23, M_PI / 4.0)) return true;
557 StartDrive(0.0, -kThirdCubeTurn - 0.2 - kThirdBoxEndTurnAngle - 0.3,
558 kThirdBoxPlaceDrive, kReallyFastTurn);
559
560 if (!WaitForDriveNear(0.30, M_PI / 4.0 + 0.2)) return true;
561
562 if (!too_late) {
563 set_open_claw(true);
564 set_intake_angle(-M_PI / 4.0);
565 set_roller_voltage(0.0);
566 SendSuperstructureGoal();
567
568 LOG(INFO, "Final open %f\n",
569 DoubleSeconds(monotonic_clock::now() - start_time));
570 }
571
572 if (!WaitForDriveProfileDone()) return true;
573 if (!WaitForTurnProfileDone()) return true;
574
575 ::std::this_thread::sleep_for(chrono::milliseconds(14750) -
576 (monotonic_clock::now() - start_time));
577
578 set_arm_goal_position(arm::UpIndex());
579 SendSuperstructureGoal();
580
581 ::std::this_thread::sleep_for(chrono::milliseconds(15000) -
582 (monotonic_clock::now() - start_time));
583
584 set_close_claw(true);
585 SendSuperstructureGoal();
586 return false;
587}
588
Austin Schuha3c148e2018-03-09 21:04:05 -0800589} // namespace actors
590} // namespace y2018