blob: dd94083ca41b8ac7007bb18c0a783adeb18c1f2b [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) {
Austin Schuh77ed5432019-07-07 20:41:36 -070062 const monotonic_clock::time_point start_time = monotonic_now();
Austin Schuhf257f3c2019-10-27 21:00:43 -070063 AOS_LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n",
64 params.mode);
Austin Schuha3c148e2018-03-09 21:04:05 -080065 Reset();
66
Austin Schuh4dad8fb2018-07-08 16:00:13 -070067 // Switch
68 /*
Austin Schuhc231df42018-03-21 20:43:24 -070069 switch (params.mode) {
Austin Schuh4dad8fb2018-07-08 16:00:13 -070070 case 0:
71 case 2: {
72 if (FarSwitch(start_time, true)) return true;
73 } break;
74
75 case 3:
Austin Schuhc231df42018-03-21 20:43:24 -070076 case 1: {
Austin Schuh4dad8fb2018-07-08 16:00:13 -070077 if (CloseSwitch(start_time)) return true;
Austin Schuhc231df42018-03-21 20:43:24 -070078 } break;
Austin Schuh4dad8fb2018-07-08 16:00:13 -070079 }
80 */
81 // Scale
82 switch (params.mode) {
83 case 0:
84 case 1: {
85 if (FarScale(start_time)) return true;
Austin Schuhc231df42018-03-21 20:43:24 -070086 } break;
87
88 case 3:
89 case 2: {
Austin Schuh4dad8fb2018-07-08 16:00:13 -070090 if (ThreeCubeAuto(start_time)) return true;
Austin Schuhc231df42018-03-21 20:43:24 -070091 } break;
Austin Schuha3c148e2018-03-09 21:04:05 -080092 }
Austin Schuh4dad8fb2018-07-08 16:00:13 -070093 /*
94 switch (params.mode) {
95 case 1: {
96 if (FarScale(start_time)) return true;
97 //if (CloseSwitch(start_time)) return true;
98 } break;
99 case 0: {
100 if (DriveStraight()) return true;
101 } break;
102 case 200: {
103 if (FarSwitch(start_time)) return true;
104 } break;
105
106 case 3:
107 case 2: {
108 if (ThreeCubeAuto(start_time)) {
109 return true;
110 }
111 } break;
112 }
113 */
Austin Schuha3c148e2018-03-09 21:04:05 -0800114
Austin Schuhf257f3c2019-10-27 21:00:43 -0700115 AOS_LOG(INFO, "Done %f\n",
116 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuha3c148e2018-03-09 21:04:05 -0800117
118 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700119 event_loop()->monotonic_now(),
Austin Schuha3c148e2018-03-09 21:04:05 -0800120 ::std::chrono::milliseconds(5) / 2);
121
122 while (!ShouldCancel()) {
123 phased_loop.SleepUntilNext();
124 }
Austin Schuhf257f3c2019-10-27 21:00:43 -0700125 AOS_LOG(DEBUG, "Done running\n");
Austin Schuha3c148e2018-03-09 21:04:05 -0800126
127 return true;
128}
129
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700130bool AutonomousActor::FarSwitch(monotonic_clock::time_point start_time,
131 bool drive_behind, bool left) {
132 const double turn_scalar = left ? 1.0 : -1.0;
133 if (drive_behind) {
134 // Start on the left. Hit the switch.
135 constexpr double kFullDriveLength = 9.93;
136 constexpr double kTurnDistance = 4.40;
137 StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn);
138 set_arm_goal_position(arm::NeutralIndex());
139 set_grab_box(false);
140 SendSuperstructureGoal();
141 if (!WaitForDriveProfileNear(kFullDriveLength - (kTurnDistance - 1.4)))
142 return true;
143 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn);
144
145 if (!WaitForDriveProfileNear(kFullDriveLength - kTurnDistance)) return true;
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700146 StartDrive(0.0, turn_scalar * (-M_PI / 2.0), kFarSwitchTurnDrive,
147 kSweepingTurn);
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700148 if (!WaitForTurnProfileDone()) return true;
149
150 // Now, close so let's move the arm up.
151 set_arm_goal_position(arm::FrontSwitchAutoIndex());
152 SendSuperstructureGoal();
153
154 StartDrive(0.0, 0.0, kDrive, kTurn);
155 if (!WaitForDriveProfileNear(2.0)) return true;
156 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kFastTurn);
157
158 if (!WaitForDriveProfileNear(1.3)) return true;
159
160 StartDrive(0.0, turn_scalar * M_PI / 2.0, kFarSwitchTurnDrive, kFastTurn);
161 if (!WaitForTurnProfileDone()) return true;
162
163 constexpr double kGentlePushDrive = 0.70;
164 StartDrive(kGentlePushDrive, 0.0, kSlowDrive, kTurn);
165
166 if (!WaitForDriveProfileNear(kGentlePushDrive - 0.25)) return true;
167 // Turn down the peak voltage when we push against the wall so we don't blow
168 // breakers or pull the battery voltage down too far.
169 set_max_drivetrain_voltage(6.0);
170 StartDrive(0.00, 0.0, kFinalSwitchDrive, kTurn);
171
172 if (!WaitForArmTrajectoryClose(0.001)) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700173 AOS_LOG(INFO, "Arm close at %f\n",
174 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700175
176 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
177
178 set_open_claw(true);
179 SendSuperstructureGoal();
180
181 ::std::this_thread::sleep_for(chrono::milliseconds(1500));
182 set_arm_goal_position(arm::NeutralIndex());
183 SendSuperstructureGoal();
184 if (ShouldCancel()) return true;
185 set_max_drivetrain_voltage(12.0);
186 StartDrive(-0.5, 0.0, kDrive, kTurn);
187 if (!WaitForDriveProfileDone()) return true;
188 } else {
189 // Start on the left. Hit the switch.
190 constexpr double kFullDriveLength = 5.55;
191 constexpr double kTurnDistance = 0.35;
192 StartDrive(-kFullDriveLength, 0.0, kFarSwitchTurnDrive, kTurn);
193
194 if (!WaitForDriveProfileNear(kFullDriveLength - kTurnDistance)) return true;
195 StartDrive(0.0, turn_scalar * (-M_PI / 2.0), kFarSwitchTurnDrive,
196 kSweepingTurn);
197 if (!WaitForTurnProfileDone()) return true;
198 StartDrive(0.0, 0.0, kDrive, kTurn);
199 if (!WaitForDriveProfileDone()) return true;
200
201 // Now, close so let's move the arm up.
202 set_arm_goal_position(arm::FrontSwitchIndex());
203 SendSuperstructureGoal();
204
205 StartDrive(0.0, turn_scalar * (-M_PI / 2.0), kDrive, kFastTurn);
206 if (!WaitForTurnProfileDone()) return true;
207
208 set_max_drivetrain_voltage(10.0);
209 StartDrive(1.1, 0.0, kDrive, kTurn);
210 if (!WaitForArmTrajectoryClose(0.001)) return true;
211 if (!WaitForDriveNear(0.6, M_PI / 2.0)) return true;
212 StartDrive(0.0, 0.0, kFinalFrontFarSwitchDrive, kTurn);
213
214 if (!WaitForDriveNear(0.3, M_PI / 2.0)) return true;
215 set_max_drivetrain_voltage(6.0);
216 StartDrive(0.0, 0.0, kFinalFrontFarSwitchDrive, kTurn);
217
218 // if (!WaitForDriveNear(0.2, M_PI / 2.0)) return true;
219 ::std::this_thread::sleep_for(chrono::milliseconds(300));
220
221 set_open_claw(true);
222 SendSuperstructureGoal();
223
224 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
225 set_arm_goal_position(arm::NeutralIndex());
226 SendSuperstructureGoal();
227 if (ShouldCancel()) return true;
228 set_max_drivetrain_voltage(12.0);
229 StartDrive(-0.5, 0.0, kDrive, kTurn);
230 if (!WaitForDriveProfileDone()) return true;
231 }
232 return false;
233}
234
235bool AutonomousActor::FarScale(monotonic_clock::time_point start_time) {
236 // Start on the left. Hit the switch.
237 constexpr double kFullDriveLength = 11.40;
238 constexpr double kFirstTurnDistance = 4.40;
239 constexpr double kSecondTurnDistance = 9.30;
240 StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn);
241 if (!WaitForDriveProfileNear(kFullDriveLength - (kFirstTurnDistance - 1.4)))
242 return true;
243 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn);
244
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700245 if (!WaitForDriveProfileNear(kFullDriveLength - kFirstTurnDistance))
246 return true;
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700247 StartDrive(0.0, -M_PI / 2.0, kFarSwitchTurnDrive, kSweepingTurn);
248 set_arm_goal_position(arm::BackHighBoxIndex());
249 SendSuperstructureGoal();
250 if (!WaitForTurnProfileDone()) return true;
251
252 StartDrive(0.0, 0.0, kDrive, kTurn);
253
254 if (!WaitForDriveProfileNear(kFullDriveLength - (kSecondTurnDistance - 1.4)))
255 return true;
256
257 StartDrive(0.0, 0.0, kFarScaleFinalTurnDrive, kTurn);
258 if (!WaitForDriveProfileNear(kFullDriveLength - (kSecondTurnDistance)))
259 return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700260 AOS_LOG(INFO, "Final turn at %f\n",
261 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700262
263 StartDrive(0.0, M_PI / 2.0, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
264 if (!WaitForDriveProfileNear(0.15)) return true;
265
266 StartDrive(0.0, 0.3, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
267
Austin Schuhf257f3c2019-10-27 21:00:43 -0700268 AOS_LOG(INFO, "Dropping at %f\n",
269 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700270 set_open_claw(true);
271 SendSuperstructureGoal();
272
273 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700274 AOS_LOG(INFO, "Backing up at %f\n",
275 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700276
277 StartDrive(1.5, -0.55, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
278
279 if (!WaitForDriveProfileNear(1.4)) return true;
280 set_arm_goal_position(arm::NeutralIndex());
281 set_open_claw(false);
282 set_grab_box(true);
283 SendSuperstructureGoal();
284
285 if (!WaitForDriveProfileNear(0.3)) return true;
286
287 set_intake_angle(0.15);
288 set_roller_voltage(10.0);
289 SendSuperstructureGoal();
290
291 set_max_drivetrain_voltage(6.0);
292 StartDrive(0.0, 0.0, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
293
294 if (!WaitForDriveProfileDone()) return true;
295 if (!WaitForTurnProfileDone()) return true;
296
297 StartDrive(0.2, 0.0, kThirdBoxSlowBackup, kThirdBoxSlowTurn);
298
299 if (!WaitForBoxGrabed()) return true;
300 set_arm_goal_position(arm::BackHighBoxIndex());
301 set_intake_angle(-3.0);
302 set_roller_voltage(0.0);
303 set_grab_box(false);
304 SendSuperstructureGoal();
305
306 StartDrive(-1.40, 0.15, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
307
308 if (!WaitForDriveProfileNear(0.1)) return true;
309
310 StartDrive(0.0, 0.5, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
311
312 if (!WaitForDriveProfileDone()) return true;
313 if (!WaitForTurnProfileDone()) return true;
314 ::std::this_thread::sleep_for(chrono::milliseconds(500));
315
Austin Schuhf257f3c2019-10-27 21:00:43 -0700316 AOS_LOG(INFO, "Dropping second box at %f\n",
317 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700318 set_open_claw(true);
319 SendSuperstructureGoal();
320
321 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
322
323 StartDrive(1.4, -0.7, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
324 ::std::this_thread::sleep_for(chrono::milliseconds(200));
325 set_arm_goal_position(arm::NeutralIndex());
326 set_open_claw(false);
327 SendSuperstructureGoal();
328
329 if (!WaitForDriveProfileNear(1.0)) return true;
330
331 StartDrive(0.0, -0.6, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
332 return false;
333}
334
335bool AutonomousActor::FarReadyScale(monotonic_clock::time_point start_time) {
336 // Start on the left. Hit the switch.
337 constexpr double kFullDriveLength = 7.5;
338 constexpr double kFirstTurnDistance = 4.40;
339 StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn);
340 if (!WaitForDriveProfileNear(kFullDriveLength - (kFirstTurnDistance - 1.4)))
341 return true;
342 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn);
343
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700344 if (!WaitForDriveProfileNear(kFullDriveLength - kFirstTurnDistance))
345 return true;
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700346 StartDrive(0.0, -M_PI / 2.0, kFarSwitchTurnDrive, kSweepingTurn);
347 set_arm_goal_position(arm::UpIndex());
348 SendSuperstructureGoal();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700349 AOS_LOG(INFO, "Lifting arm at %f\n",
350 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700351 if (!WaitForTurnProfileDone()) return true;
352
353 StartDrive(0.0, 0.0, kDrive, kTurn);
354 return false;
355}
356
357bool AutonomousActor::DriveStraight() {
358 StartDrive(-3.2, 0.0, kDrive, kTurn);
359 if (!WaitForDriveProfileDone()) return true;
360 return false;
361}
362
363bool AutonomousActor::CloseSwitch(monotonic_clock::time_point start_time,
364 bool left) {
365 const double turn_scalar = left ? 1.0 : -1.0;
366
367 constexpr double kDriveDistance = 3.2;
368 // Start on the left. Drive, arc a turn, and drop in the close switch.
369 StartDrive(-kDriveDistance, 0.0, kDrive, kTurn);
370 if (!WaitForDriveNear(2.5, M_PI / 2.0)) return true;
371
372 // Now, close so let's move the arm up.
373 set_arm_goal_position(arm::BackSwitchIndex());
374 SendSuperstructureGoal();
375
376 StartDrive(0.0, 0.0, kSlowDrive, kSweepingTurn);
377 if (!WaitForDriveNear(1.6, M_PI / 2.0)) return true;
378
379 StartDrive(0.0, turn_scalar * (-M_PI / 4.0 - 0.2), kSlowDrive, kSweepingTurn);
380 if (!WaitForDriveNear(0.2, 0.2)) return true;
381 set_max_drivetrain_voltage(6.0);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700382 AOS_LOG(INFO, "Lowered drivetrain voltage %f\n",
383 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700384 ::std::this_thread::sleep_for(chrono::milliseconds(300));
385
386 set_open_claw(true);
387 SendSuperstructureGoal();
388
389 ::std::this_thread::sleep_for(chrono::milliseconds(500));
390 set_max_drivetrain_voltage(12.0);
391 StartDrive(0.7, 0.0, kDrive, kTurn);
392 if (!WaitForTurnProfileDone()) return true;
393
394 set_arm_goal_position(arm::NeutralIndex());
395 SendSuperstructureGoal();
396 return false;
397}
398
399bool AutonomousActor::ThreeCubeAuto(monotonic_clock::time_point start_time) {
400 // Start on the left. Hit the scale.
401 constexpr double kDriveDistance = 6.95;
402 // Distance and angle to do the big drive to the third cube.
403 constexpr double kThirdCubeDrive = 1.57 + 0.05 + 0.05;
404 constexpr double kThirdCubeTurn = M_PI / 4.0 + 0.1;
405 // Angle to do the slow pickup turn on the third box.
406 constexpr double kThirdBoxEndTurnAngle = 0.30;
407
408 // Distance to drive back to the scale with the third cube.
409 constexpr double kThirdCubeDropDrive = kThirdCubeDrive + 0.40;
410
411 // Drive.
412 StartDrive(-kDriveDistance, 0.0, kDrive, kTurn);
413 if (!WaitForDriveNear(kDriveDistance - 1.0, M_PI / 2.0)) return true;
414 // Once we are away from the wall, start the arm.
415 set_arm_goal_position(arm::BackMiddle2BoxIndex());
416 SendSuperstructureGoal();
417
418 // We are starting to get close. Slow down for the turn.
419 if (!WaitForDriveNear(4.0, M_PI / 2.0)) return true;
420 StartDrive(0.0, 0.0, kScaleTurnDrive, kFastTurn);
421
422 // Once we've gotten slowed down a bit, start turning.
423 if (!WaitForDriveNear(3.25, M_PI / 2.0)) return true;
424 StartDrive(0.0, -M_PI / 6.0, kScaleTurnDrive, kFastTurn);
425
426 if (!WaitForDriveNear(1.0, M_PI / 2.0)) return true;
427 StartDrive(0.0, M_PI / 6.0, kScaleTurnDrive, kFastTurn);
428
429 // Get close and open the claw.
430 if (!WaitForDriveNear(0.15, 0.25)) return true;
431 set_open_claw(true);
432 SendSuperstructureGoal();
433 set_intake_angle(-0.60);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700434 AOS_LOG(INFO, "Dropped first box %f\n",
435 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700436
437 ::std::this_thread::sleep_for(chrono::milliseconds(700));
438
439 set_grab_box(true);
440 SendSuperstructureGoal();
441
Austin Schuhf257f3c2019-10-27 21:00:43 -0700442 AOS_LOG(INFO, "Starting second box drive %f\n",
443 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700444 constexpr double kSecondBoxSwerveAngle = 0.35;
445 constexpr double kSecondBoxDrive = 1.38;
446 StartDrive(kSecondBoxDrive, 0.0, kDrive, kFastTurn);
447 if (!WaitForDriveNear(kSecondBoxDrive - 0.2, M_PI / 2.0)) return true;
448
449 StartDrive(0.0, kSecondBoxSwerveAngle, kDrive, kFastTurn);
450 if (!WaitForDriveNear(0.5, M_PI / 2.0)) return true;
451
452 set_open_claw(true);
453 set_disable_box_correct(false);
454 SendSuperstructureGoal();
455
456 StartDrive(0.0, -kSecondBoxSwerveAngle, kDrive, kFastTurn);
457
458 if (!WaitForDriveProfileDone()) return true;
459 set_max_drivetrain_voltage(6.0);
460 StartDrive(0.0, 0.0, kDrive, kFastTurn);
461
462 set_intake_angle(0.15);
463 set_arm_goal_position(arm::BackHighBoxIndex());
464 set_open_claw(false);
465
466 set_roller_voltage(10.0);
467 SendSuperstructureGoal();
468
Austin Schuhf257f3c2019-10-27 21:00:43 -0700469 AOS_LOG(INFO, "Grabbing second box %f\n",
470 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700471 ::std::this_thread::sleep_for(chrono::milliseconds(200));
472 StartDrive(-0.04, 0.0, kThirdBoxSlowBackup, kThirdBoxSlowTurn);
473
474 if (!WaitForBoxGrabed()) return true;
475 set_max_drivetrain_voltage(12.0);
476
Austin Schuhf257f3c2019-10-27 21:00:43 -0700477 AOS_LOG(INFO, "Got second box %f\n",
478 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700479 ::std::this_thread::sleep_for(chrono::milliseconds(500));
480
481 set_grab_box(false);
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700482 // set_arm_goal_position(arm::UpIndex());
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700483 set_arm_goal_position(arm::BackHighBoxIndex());
484 set_roller_voltage(0.0);
485 set_disable_box_correct(false);
486 SendSuperstructureGoal();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700487 AOS_LOG(INFO, "Driving to place second box %f\n",
488 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700489
490 StartDrive(-kSecondBoxDrive + 0.16, kSecondBoxSwerveAngle, kDrive, kFastTurn);
491 if (!WaitForDriveNear(0.4, M_PI / 2.0)) return true;
492
493 constexpr double kSecondBoxEndExtraAngle = 0.3;
494 StartDrive(0.0, -kSecondBoxSwerveAngle - kSecondBoxEndExtraAngle, kDrive,
495 kFastTurn);
496
Austin Schuhf257f3c2019-10-27 21:00:43 -0700497 AOS_LOG(INFO, "Starting throw %f\n",
498 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700499 if (!WaitForDriveNear(0.4, M_PI / 2.0)) return true;
500 if (!WaitForArmTrajectoryClose(0.25)) return true;
501 SendSuperstructureGoal();
502
503 // Throw the box.
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700504 // if (!WaitForArmTrajectoryClose(0.20)) return true;
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700505 if (!WaitForDriveNear(0.2, M_PI / 2.0)) return true;
506
507 set_open_claw(true);
508 set_intake_angle(-M_PI / 4.0);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700509 AOS_LOG(INFO, "Releasing second box %f\n",
510 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700511 SendSuperstructureGoal();
512
513 ::std::this_thread::sleep_for(chrono::milliseconds(700));
514 set_open_claw(false);
515 SendSuperstructureGoal();
516
Austin Schuhf257f3c2019-10-27 21:00:43 -0700517 AOS_LOG(INFO, "Driving to third box %f\n",
518 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700519 StartDrive(kThirdCubeDrive, kSecondBoxEndExtraAngle, kThirdBoxDrive,
520 kFastTurn);
521 if (!WaitForDriveNear(kThirdCubeDrive - 0.1, M_PI / 4.0)) return true;
522 set_grab_box(true);
523 SendSuperstructureGoal();
524
525 StartDrive(0.0, kThirdCubeTurn, kThirdBoxDrive, kFastTurn);
526 if (!WaitForDriveNear(0.05, M_PI / 4.0)) return true;
527
528 set_intake_angle(0.05);
529 set_roller_voltage(9.0);
530 SendSuperstructureGoal();
531
532 if (!WaitForDriveProfileDone()) return true;
533 if (!WaitForTurnProfileDone()) return true;
534 StartDrive(0.35, kThirdBoxEndTurnAngle, kThirdBoxSlowBackup,
535 kThirdBoxSlowTurn);
536 if (!WaitForDriveProfileDone()) return true;
537
Austin Schuhf257f3c2019-10-27 21:00:43 -0700538 AOS_LOG(INFO, "Waiting for third box %f\n",
539 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700540 if (!WaitForBoxGrabed()) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700541 AOS_LOG(INFO, "Third box grabbed %f\n",
542 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700543 const bool too_late =
Austin Schuh77ed5432019-07-07 20:41:36 -0700544 monotonic_now() > start_time + chrono::milliseconds(12500);
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700545 if (too_late) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700546 AOS_LOG(INFO, "Third box too long, going up. %f\n",
547 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700548 set_grab_box(false);
549 set_arm_goal_position(arm::UpIndex());
550 set_roller_voltage(0.0);
551 SendSuperstructureGoal();
552 }
553 ::std::this_thread::sleep_for(chrono::milliseconds(400));
554
555 set_grab_box(false);
556 if (!too_late) {
557 set_arm_goal_position(arm::BackMiddle2BoxIndex());
558 }
559 set_roller_voltage(0.0);
560 SendSuperstructureGoal();
561
562 StartDrive(-kThirdCubeDropDrive, 0.0, kThirdBoxPlaceDrive, kReallyFastTurn);
563
564 if (!WaitForDriveNear(kThirdCubeDropDrive - 0.23, M_PI / 4.0)) return true;
565 StartDrive(0.0, -kThirdCubeTurn - 0.2 - kThirdBoxEndTurnAngle - 0.3,
566 kThirdBoxPlaceDrive, kReallyFastTurn);
567
568 if (!WaitForDriveNear(0.30, M_PI / 4.0 + 0.2)) return true;
569
570 if (!too_late) {
571 set_open_claw(true);
572 set_intake_angle(-M_PI / 4.0);
573 set_roller_voltage(0.0);
574 SendSuperstructureGoal();
575
Austin Schuhf257f3c2019-10-27 21:00:43 -0700576 AOS_LOG(INFO, "Final open %f\n",
577 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700578 }
579
580 if (!WaitForDriveProfileDone()) return true;
581 if (!WaitForTurnProfileDone()) return true;
582
583 ::std::this_thread::sleep_for(chrono::milliseconds(14750) -
Austin Schuh77ed5432019-07-07 20:41:36 -0700584 (monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700585
586 set_arm_goal_position(arm::UpIndex());
587 SendSuperstructureGoal();
588
589 ::std::this_thread::sleep_for(chrono::milliseconds(15000) -
Austin Schuh77ed5432019-07-07 20:41:36 -0700590 (monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700591
592 set_close_claw(true);
593 SendSuperstructureGoal();
594 return false;
595}
596
Austin Schuha3c148e2018-03-09 21:04:05 -0800597} // namespace actors
598} // namespace y2018