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