blob: 80cdbe9a569138e9940723321668a211b3f4ac74 [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/logging/logging.h"
James Kuszmaul651fc3f2019-05-15 21:14:25 -07009#include "aos/util/phased_loop.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
Austin Schuha3c148e2018-03-09 21:04:05 -080018using ::aos::monotonic_clock;
19namespace chrono = ::std::chrono;
20namespace this_thread = ::std::this_thread;
21
22namespace {
23
Austin Schuhbd0a40f2019-06-30 14:56:31 -070024namespace arm = ::y2018::control_loops::superstructure::arm;
25
Austin Schuh4dad8fb2018-07-08 16:00:13 -070026const ProfileParameters kFinalSwitchDrive = {0.5, 1.5};
Austin Schuhc231df42018-03-21 20:43:24 -070027const ProfileParameters kDrive = {5.0, 2.5};
Austin Schuhf79c0e52018-04-04 20:13:21 -070028const ProfileParameters kThirdBoxDrive = {3.0, 2.5};
Austin Schuhc231df42018-03-21 20:43:24 -070029const ProfileParameters kSlowDrive = {1.5, 2.5};
Austin Schuhf79c0e52018-04-04 20:13:21 -070030const ProfileParameters kScaleTurnDrive = {3.0, 2.5};
Austin Schuhc231df42018-03-21 20:43:24 -070031const ProfileParameters kFarSwitchTurnDrive = {2.0, 2.5};
Austin Schuh4dad8fb2018-07-08 16:00:13 -070032const ProfileParameters kFarScaleFinalTurnDrive = kFarSwitchTurnDrive;
Austin Schuhc231df42018-03-21 20:43:24 -070033const ProfileParameters kTurn = {4.0, 2.0};
34const ProfileParameters kSweepingTurn = {5.0, 7.0};
Austin Schuh4dad8fb2018-07-08 16:00:13 -070035const ProfileParameters kFarScaleSweepingTurn = kSweepingTurn;
Austin Schuhc231df42018-03-21 20:43:24 -070036const ProfileParameters kFastTurn = {5.0, 7.0};
Austin Schuhf79c0e52018-04-04 20:13:21 -070037const ProfileParameters kReallyFastTurn = {1.5, 9.0};
38
39const ProfileParameters kThirdBoxSlowBackup = {0.35, 1.5};
40const ProfileParameters kThirdBoxSlowTurn = {1.5, 4.0};
41
Austin Schuh4dad8fb2018-07-08 16:00:13 -070042const ProfileParameters kThirdBoxPlaceDrive = {4.0, 2.0};
Austin Schuhf79c0e52018-04-04 20:13:21 -070043
44const ProfileParameters kFinalFrontFarSwitchDrive = {2.0, 2.0};
Austin Schuha3c148e2018-03-09 21:04:05 -080045
46} // namespace
47
Austin Schuh1bf8a212019-05-26 22:13:14 -070048AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
Austin Schuha3c148e2018-03-09 21:04:05 -080049 : frc971::autonomous::BaseAutonomousActor(
Austin Schuhd845c972019-06-29 21:20:05 -070050 event_loop, control_loops::drivetrain::GetDrivetrainConfig()),
51 superstructure_goal_sender_(
52 event_loop
53 ->MakeSender<::y2018::control_loops::SuperstructureQueue::Goal>(
54 ".frc971.control_loops.superstructure_queue.goal")),
55 superstructure_status_fetcher_(
56 event_loop->MakeFetcher<
57 ::y2018::control_loops::SuperstructureQueue::Status>(
58 ".frc971.control_loops.superstructure_queue.status")) {}
Austin Schuha3c148e2018-03-09 21:04:05 -080059
60bool AutonomousActor::RunAction(
61 const ::frc971::autonomous::AutonomousActionParams &params) {
62 monotonic_clock::time_point start_time = monotonic_clock::now();
63 LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode);
64 Reset();
65
Austin Schuh4dad8fb2018-07-08 16:00:13 -070066 // Switch
67 /*
Austin Schuhc231df42018-03-21 20:43:24 -070068 switch (params.mode) {
Austin Schuh4dad8fb2018-07-08 16:00:13 -070069 case 0:
70 case 2: {
71 if (FarSwitch(start_time, true)) return true;
72 } break;
73
74 case 3:
Austin Schuhc231df42018-03-21 20:43:24 -070075 case 1: {
Austin Schuh4dad8fb2018-07-08 16:00:13 -070076 if (CloseSwitch(start_time)) return true;
Austin Schuhc231df42018-03-21 20:43:24 -070077 } break;
Austin Schuh4dad8fb2018-07-08 16:00:13 -070078 }
79 */
80 // Scale
81 switch (params.mode) {
82 case 0:
83 case 1: {
84 if (FarScale(start_time)) return true;
Austin Schuhc231df42018-03-21 20:43:24 -070085 } break;
86
87 case 3:
88 case 2: {
Austin Schuh4dad8fb2018-07-08 16:00:13 -070089 if (ThreeCubeAuto(start_time)) return true;
Austin Schuhc231df42018-03-21 20:43:24 -070090 } break;
Austin Schuha3c148e2018-03-09 21:04:05 -080091 }
Austin Schuh4dad8fb2018-07-08 16:00:13 -070092 /*
93 switch (params.mode) {
94 case 1: {
95 if (FarScale(start_time)) return true;
96 //if (CloseSwitch(start_time)) return true;
97 } break;
98 case 0: {
99 if (DriveStraight()) return true;
100 } break;
101 case 200: {
102 if (FarSwitch(start_time)) return true;
103 } break;
104
105 case 3:
106 case 2: {
107 if (ThreeCubeAuto(start_time)) {
108 return true;
109 }
110 } break;
111 }
112 */
Austin Schuha3c148e2018-03-09 21:04:05 -0800113
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700114 LOG(INFO, "Done %f\n",
115 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuha3c148e2018-03-09 21:04:05 -0800116
117 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700118 event_loop()->monotonic_now(),
Austin Schuha3c148e2018-03-09 21:04:05 -0800119 ::std::chrono::milliseconds(5) / 2);
120
121 while (!ShouldCancel()) {
122 phased_loop.SleepUntilNext();
123 }
124 LOG(DEBUG, "Done running\n");
125
126 return true;
127}
128
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700129bool AutonomousActor::FarSwitch(monotonic_clock::time_point start_time,
130 bool drive_behind, bool left) {
131 const double turn_scalar = left ? 1.0 : -1.0;
132 if (drive_behind) {
133 // Start on the left. Hit the switch.
134 constexpr double kFullDriveLength = 9.93;
135 constexpr double kTurnDistance = 4.40;
136 StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn);
137 set_arm_goal_position(arm::NeutralIndex());
138 set_grab_box(false);
139 SendSuperstructureGoal();
140 if (!WaitForDriveProfileNear(kFullDriveLength - (kTurnDistance - 1.4)))
141 return true;
142 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn);
143
144 if (!WaitForDriveProfileNear(kFullDriveLength - kTurnDistance)) return true;
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700145 StartDrive(0.0, turn_scalar * (-M_PI / 2.0), kFarSwitchTurnDrive,
146 kSweepingTurn);
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700147 if (!WaitForTurnProfileDone()) return true;
148
149 // Now, close so let's move the arm up.
150 set_arm_goal_position(arm::FrontSwitchAutoIndex());
151 SendSuperstructureGoal();
152
153 StartDrive(0.0, 0.0, kDrive, kTurn);
154 if (!WaitForDriveProfileNear(2.0)) return true;
155 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kFastTurn);
156
157 if (!WaitForDriveProfileNear(1.3)) return true;
158
159 StartDrive(0.0, turn_scalar * M_PI / 2.0, kFarSwitchTurnDrive, kFastTurn);
160 if (!WaitForTurnProfileDone()) return true;
161
162 constexpr double kGentlePushDrive = 0.70;
163 StartDrive(kGentlePushDrive, 0.0, kSlowDrive, kTurn);
164
165 if (!WaitForDriveProfileNear(kGentlePushDrive - 0.25)) return true;
166 // Turn down the peak voltage when we push against the wall so we don't blow
167 // breakers or pull the battery voltage down too far.
168 set_max_drivetrain_voltage(6.0);
169 StartDrive(0.00, 0.0, kFinalSwitchDrive, kTurn);
170
171 if (!WaitForArmTrajectoryClose(0.001)) return true;
172 LOG(INFO, "Arm close at %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700173 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700174
175 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
176
177 set_open_claw(true);
178 SendSuperstructureGoal();
179
180 ::std::this_thread::sleep_for(chrono::milliseconds(1500));
181 set_arm_goal_position(arm::NeutralIndex());
182 SendSuperstructureGoal();
183 if (ShouldCancel()) return true;
184 set_max_drivetrain_voltage(12.0);
185 StartDrive(-0.5, 0.0, kDrive, kTurn);
186 if (!WaitForDriveProfileDone()) return true;
187 } else {
188 // Start on the left. Hit the switch.
189 constexpr double kFullDriveLength = 5.55;
190 constexpr double kTurnDistance = 0.35;
191 StartDrive(-kFullDriveLength, 0.0, kFarSwitchTurnDrive, kTurn);
192
193 if (!WaitForDriveProfileNear(kFullDriveLength - kTurnDistance)) return true;
194 StartDrive(0.0, turn_scalar * (-M_PI / 2.0), kFarSwitchTurnDrive,
195 kSweepingTurn);
196 if (!WaitForTurnProfileDone()) return true;
197 StartDrive(0.0, 0.0, kDrive, kTurn);
198 if (!WaitForDriveProfileDone()) return true;
199
200 // Now, close so let's move the arm up.
201 set_arm_goal_position(arm::FrontSwitchIndex());
202 SendSuperstructureGoal();
203
204 StartDrive(0.0, turn_scalar * (-M_PI / 2.0), kDrive, kFastTurn);
205 if (!WaitForTurnProfileDone()) return true;
206
207 set_max_drivetrain_voltage(10.0);
208 StartDrive(1.1, 0.0, kDrive, kTurn);
209 if (!WaitForArmTrajectoryClose(0.001)) return true;
210 if (!WaitForDriveNear(0.6, M_PI / 2.0)) return true;
211 StartDrive(0.0, 0.0, kFinalFrontFarSwitchDrive, kTurn);
212
213 if (!WaitForDriveNear(0.3, M_PI / 2.0)) return true;
214 set_max_drivetrain_voltage(6.0);
215 StartDrive(0.0, 0.0, kFinalFrontFarSwitchDrive, kTurn);
216
217 // if (!WaitForDriveNear(0.2, M_PI / 2.0)) return true;
218 ::std::this_thread::sleep_for(chrono::milliseconds(300));
219
220 set_open_claw(true);
221 SendSuperstructureGoal();
222
223 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
224 set_arm_goal_position(arm::NeutralIndex());
225 SendSuperstructureGoal();
226 if (ShouldCancel()) return true;
227 set_max_drivetrain_voltage(12.0);
228 StartDrive(-0.5, 0.0, kDrive, kTurn);
229 if (!WaitForDriveProfileDone()) return true;
230 }
231 return false;
232}
233
234bool AutonomousActor::FarScale(monotonic_clock::time_point start_time) {
235 // Start on the left. Hit the switch.
236 constexpr double kFullDriveLength = 11.40;
237 constexpr double kFirstTurnDistance = 4.40;
238 constexpr double kSecondTurnDistance = 9.30;
239 StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn);
240 if (!WaitForDriveProfileNear(kFullDriveLength - (kFirstTurnDistance - 1.4)))
241 return true;
242 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn);
243
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700244 if (!WaitForDriveProfileNear(kFullDriveLength - kFirstTurnDistance))
245 return true;
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700246 StartDrive(0.0, -M_PI / 2.0, kFarSwitchTurnDrive, kSweepingTurn);
247 set_arm_goal_position(arm::BackHighBoxIndex());
248 SendSuperstructureGoal();
249 if (!WaitForTurnProfileDone()) return true;
250
251 StartDrive(0.0, 0.0, kDrive, kTurn);
252
253 if (!WaitForDriveProfileNear(kFullDriveLength - (kSecondTurnDistance - 1.4)))
254 return true;
255
256 StartDrive(0.0, 0.0, kFarScaleFinalTurnDrive, kTurn);
257 if (!WaitForDriveProfileNear(kFullDriveLength - (kSecondTurnDistance)))
258 return true;
259 LOG(INFO, "Final turn at %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700260 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700261
262 StartDrive(0.0, M_PI / 2.0, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
263 if (!WaitForDriveProfileNear(0.15)) return true;
264
265 StartDrive(0.0, 0.3, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
266
267 LOG(INFO, "Dropping at %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700268 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700269 set_open_claw(true);
270 SendSuperstructureGoal();
271
272 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
273 LOG(INFO, "Backing up at %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700274 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700275
276 StartDrive(1.5, -0.55, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
277
278 if (!WaitForDriveProfileNear(1.4)) return true;
279 set_arm_goal_position(arm::NeutralIndex());
280 set_open_claw(false);
281 set_grab_box(true);
282 SendSuperstructureGoal();
283
284 if (!WaitForDriveProfileNear(0.3)) return true;
285
286 set_intake_angle(0.15);
287 set_roller_voltage(10.0);
288 SendSuperstructureGoal();
289
290 set_max_drivetrain_voltage(6.0);
291 StartDrive(0.0, 0.0, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
292
293 if (!WaitForDriveProfileDone()) return true;
294 if (!WaitForTurnProfileDone()) return true;
295
296 StartDrive(0.2, 0.0, kThirdBoxSlowBackup, kThirdBoxSlowTurn);
297
298 if (!WaitForBoxGrabed()) return true;
299 set_arm_goal_position(arm::BackHighBoxIndex());
300 set_intake_angle(-3.0);
301 set_roller_voltage(0.0);
302 set_grab_box(false);
303 SendSuperstructureGoal();
304
305 StartDrive(-1.40, 0.15, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
306
307 if (!WaitForDriveProfileNear(0.1)) return true;
308
309 StartDrive(0.0, 0.5, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
310
311 if (!WaitForDriveProfileDone()) return true;
312 if (!WaitForTurnProfileDone()) return true;
313 ::std::this_thread::sleep_for(chrono::milliseconds(500));
314
315 LOG(INFO, "Dropping second box at %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700316 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700317 set_open_claw(true);
318 SendSuperstructureGoal();
319
320 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
321
322 StartDrive(1.4, -0.7, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
323 ::std::this_thread::sleep_for(chrono::milliseconds(200));
324 set_arm_goal_position(arm::NeutralIndex());
325 set_open_claw(false);
326 SendSuperstructureGoal();
327
328 if (!WaitForDriveProfileNear(1.0)) return true;
329
330 StartDrive(0.0, -0.6, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
331 return false;
332}
333
334bool AutonomousActor::FarReadyScale(monotonic_clock::time_point start_time) {
335 // Start on the left. Hit the switch.
336 constexpr double kFullDriveLength = 7.5;
337 constexpr double kFirstTurnDistance = 4.40;
338 StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn);
339 if (!WaitForDriveProfileNear(kFullDriveLength - (kFirstTurnDistance - 1.4)))
340 return true;
341 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn);
342
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700343 if (!WaitForDriveProfileNear(kFullDriveLength - kFirstTurnDistance))
344 return true;
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700345 StartDrive(0.0, -M_PI / 2.0, kFarSwitchTurnDrive, kSweepingTurn);
346 set_arm_goal_position(arm::UpIndex());
347 SendSuperstructureGoal();
348 LOG(INFO, "Lifting arm at %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700349 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700350 if (!WaitForTurnProfileDone()) return true;
351
352 StartDrive(0.0, 0.0, kDrive, kTurn);
353 return false;
354}
355
356bool AutonomousActor::DriveStraight() {
357 StartDrive(-3.2, 0.0, kDrive, kTurn);
358 if (!WaitForDriveProfileDone()) return true;
359 return false;
360}
361
362bool AutonomousActor::CloseSwitch(monotonic_clock::time_point start_time,
363 bool left) {
364 const double turn_scalar = left ? 1.0 : -1.0;
365
366 constexpr double kDriveDistance = 3.2;
367 // Start on the left. Drive, arc a turn, and drop in the close switch.
368 StartDrive(-kDriveDistance, 0.0, kDrive, kTurn);
369 if (!WaitForDriveNear(2.5, M_PI / 2.0)) return true;
370
371 // Now, close so let's move the arm up.
372 set_arm_goal_position(arm::BackSwitchIndex());
373 SendSuperstructureGoal();
374
375 StartDrive(0.0, 0.0, kSlowDrive, kSweepingTurn);
376 if (!WaitForDriveNear(1.6, M_PI / 2.0)) return true;
377
378 StartDrive(0.0, turn_scalar * (-M_PI / 4.0 - 0.2), kSlowDrive, kSweepingTurn);
379 if (!WaitForDriveNear(0.2, 0.2)) return true;
380 set_max_drivetrain_voltage(6.0);
381 LOG(INFO, "Lowered drivetrain voltage %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700382 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700383 ::std::this_thread::sleep_for(chrono::milliseconds(300));
384
385 set_open_claw(true);
386 SendSuperstructureGoal();
387
388 ::std::this_thread::sleep_for(chrono::milliseconds(500));
389 set_max_drivetrain_voltage(12.0);
390 StartDrive(0.7, 0.0, kDrive, kTurn);
391 if (!WaitForTurnProfileDone()) return true;
392
393 set_arm_goal_position(arm::NeutralIndex());
394 SendSuperstructureGoal();
395 return false;
396}
397
398bool AutonomousActor::ThreeCubeAuto(monotonic_clock::time_point start_time) {
399 // Start on the left. Hit the scale.
400 constexpr double kDriveDistance = 6.95;
401 // Distance and angle to do the big drive to the third cube.
402 constexpr double kThirdCubeDrive = 1.57 + 0.05 + 0.05;
403 constexpr double kThirdCubeTurn = M_PI / 4.0 + 0.1;
404 // Angle to do the slow pickup turn on the third box.
405 constexpr double kThirdBoxEndTurnAngle = 0.30;
406
407 // Distance to drive back to the scale with the third cube.
408 constexpr double kThirdCubeDropDrive = kThirdCubeDrive + 0.40;
409
410 // Drive.
411 StartDrive(-kDriveDistance, 0.0, kDrive, kTurn);
412 if (!WaitForDriveNear(kDriveDistance - 1.0, M_PI / 2.0)) return true;
413 // Once we are away from the wall, start the arm.
414 set_arm_goal_position(arm::BackMiddle2BoxIndex());
415 SendSuperstructureGoal();
416
417 // We are starting to get close. Slow down for the turn.
418 if (!WaitForDriveNear(4.0, M_PI / 2.0)) return true;
419 StartDrive(0.0, 0.0, kScaleTurnDrive, kFastTurn);
420
421 // Once we've gotten slowed down a bit, start turning.
422 if (!WaitForDriveNear(3.25, M_PI / 2.0)) return true;
423 StartDrive(0.0, -M_PI / 6.0, kScaleTurnDrive, kFastTurn);
424
425 if (!WaitForDriveNear(1.0, M_PI / 2.0)) return true;
426 StartDrive(0.0, M_PI / 6.0, kScaleTurnDrive, kFastTurn);
427
428 // Get close and open the claw.
429 if (!WaitForDriveNear(0.15, 0.25)) return true;
430 set_open_claw(true);
431 SendSuperstructureGoal();
432 set_intake_angle(-0.60);
433 LOG(INFO, "Dropped first box %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700434 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700435
436 ::std::this_thread::sleep_for(chrono::milliseconds(700));
437
438 set_grab_box(true);
439 SendSuperstructureGoal();
440
441 LOG(INFO, "Starting second box drive %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700442 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700443 constexpr double kSecondBoxSwerveAngle = 0.35;
444 constexpr double kSecondBoxDrive = 1.38;
445 StartDrive(kSecondBoxDrive, 0.0, kDrive, kFastTurn);
446 if (!WaitForDriveNear(kSecondBoxDrive - 0.2, M_PI / 2.0)) return true;
447
448 StartDrive(0.0, kSecondBoxSwerveAngle, kDrive, kFastTurn);
449 if (!WaitForDriveNear(0.5, M_PI / 2.0)) return true;
450
451 set_open_claw(true);
452 set_disable_box_correct(false);
453 SendSuperstructureGoal();
454
455 StartDrive(0.0, -kSecondBoxSwerveAngle, kDrive, kFastTurn);
456
457 if (!WaitForDriveProfileDone()) return true;
458 set_max_drivetrain_voltage(6.0);
459 StartDrive(0.0, 0.0, kDrive, kFastTurn);
460
461 set_intake_angle(0.15);
462 set_arm_goal_position(arm::BackHighBoxIndex());
463 set_open_claw(false);
464
465 set_roller_voltage(10.0);
466 SendSuperstructureGoal();
467
468 LOG(INFO, "Grabbing second box %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700469 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700470 ::std::this_thread::sleep_for(chrono::milliseconds(200));
471 StartDrive(-0.04, 0.0, kThirdBoxSlowBackup, kThirdBoxSlowTurn);
472
473 if (!WaitForBoxGrabed()) return true;
474 set_max_drivetrain_voltage(12.0);
475
476 LOG(INFO, "Got second box %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700477 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700478 ::std::this_thread::sleep_for(chrono::milliseconds(500));
479
480 set_grab_box(false);
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700481 // set_arm_goal_position(arm::UpIndex());
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700482 set_arm_goal_position(arm::BackHighBoxIndex());
483 set_roller_voltage(0.0);
484 set_disable_box_correct(false);
485 SendSuperstructureGoal();
486 LOG(INFO, "Driving to place second box %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700487 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700488
489 StartDrive(-kSecondBoxDrive + 0.16, kSecondBoxSwerveAngle, kDrive, kFastTurn);
490 if (!WaitForDriveNear(0.4, M_PI / 2.0)) return true;
491
492 constexpr double kSecondBoxEndExtraAngle = 0.3;
493 StartDrive(0.0, -kSecondBoxSwerveAngle - kSecondBoxEndExtraAngle, kDrive,
494 kFastTurn);
495
496 LOG(INFO, "Starting throw %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700497 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700498 if (!WaitForDriveNear(0.4, M_PI / 2.0)) return true;
499 if (!WaitForArmTrajectoryClose(0.25)) return true;
500 SendSuperstructureGoal();
501
502 // Throw the box.
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700503 // if (!WaitForArmTrajectoryClose(0.20)) return true;
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700504 if (!WaitForDriveNear(0.2, M_PI / 2.0)) return true;
505
506 set_open_claw(true);
507 set_intake_angle(-M_PI / 4.0);
508 LOG(INFO, "Releasing second box %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700509 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700510 SendSuperstructureGoal();
511
512 ::std::this_thread::sleep_for(chrono::milliseconds(700));
513 set_open_claw(false);
514 SendSuperstructureGoal();
515
516 LOG(INFO, "Driving to third box %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700517 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700518 StartDrive(kThirdCubeDrive, kSecondBoxEndExtraAngle, kThirdBoxDrive,
519 kFastTurn);
520 if (!WaitForDriveNear(kThirdCubeDrive - 0.1, M_PI / 4.0)) return true;
521 set_grab_box(true);
522 SendSuperstructureGoal();
523
524 StartDrive(0.0, kThirdCubeTurn, kThirdBoxDrive, kFastTurn);
525 if (!WaitForDriveNear(0.05, M_PI / 4.0)) return true;
526
527 set_intake_angle(0.05);
528 set_roller_voltage(9.0);
529 SendSuperstructureGoal();
530
531 if (!WaitForDriveProfileDone()) return true;
532 if (!WaitForTurnProfileDone()) return true;
533 StartDrive(0.35, kThirdBoxEndTurnAngle, kThirdBoxSlowBackup,
534 kThirdBoxSlowTurn);
535 if (!WaitForDriveProfileDone()) return true;
536
537 LOG(INFO, "Waiting for third box %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700538 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700539 if (!WaitForBoxGrabed()) return true;
540 LOG(INFO, "Third box grabbed %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700541 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700542 const bool too_late =
543 monotonic_clock::now() > start_time + chrono::milliseconds(12500);
544 if (too_late) {
545 LOG(INFO, "Third box too long, going up. %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700546 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700547 set_grab_box(false);
548 set_arm_goal_position(arm::UpIndex());
549 set_roller_voltage(0.0);
550 SendSuperstructureGoal();
551 }
552 ::std::this_thread::sleep_for(chrono::milliseconds(400));
553
554 set_grab_box(false);
555 if (!too_late) {
556 set_arm_goal_position(arm::BackMiddle2BoxIndex());
557 }
558 set_roller_voltage(0.0);
559 SendSuperstructureGoal();
560
561 StartDrive(-kThirdCubeDropDrive, 0.0, kThirdBoxPlaceDrive, kReallyFastTurn);
562
563 if (!WaitForDriveNear(kThirdCubeDropDrive - 0.23, M_PI / 4.0)) return true;
564 StartDrive(0.0, -kThirdCubeTurn - 0.2 - kThirdBoxEndTurnAngle - 0.3,
565 kThirdBoxPlaceDrive, kReallyFastTurn);
566
567 if (!WaitForDriveNear(0.30, M_PI / 4.0 + 0.2)) return true;
568
569 if (!too_late) {
570 set_open_claw(true);
571 set_intake_angle(-M_PI / 4.0);
572 set_roller_voltage(0.0);
573 SendSuperstructureGoal();
574
575 LOG(INFO, "Final open %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700576 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700577 }
578
579 if (!WaitForDriveProfileDone()) return true;
580 if (!WaitForTurnProfileDone()) return true;
581
582 ::std::this_thread::sleep_for(chrono::milliseconds(14750) -
583 (monotonic_clock::now() - start_time));
584
585 set_arm_goal_position(arm::UpIndex());
586 SendSuperstructureGoal();
587
588 ::std::this_thread::sleep_for(chrono::milliseconds(15000) -
589 (monotonic_clock::now() - start_time));
590
591 set_close_claw(true);
592 SendSuperstructureGoal();
593 return false;
594}
595
Austin Schuha3c148e2018-03-09 21:04:05 -0800596} // namespace actors
597} // namespace y2018