blob: 4b0fce1927ab63c263e9eb4e02b66e2779cf8af8 [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
47AutonomousActor::AutonomousActor(
Austin Schuheb99d072019-05-12 21:03:38 -070048 ::aos::EventLoop *event_loop,
Austin Schuha3c148e2018-03-09 21:04:05 -080049 ::frc971::autonomous::AutonomousActionQueueGroup *s)
50 : frc971::autonomous::BaseAutonomousActor(
Austin Schuheb99d072019-05-12 21:03:38 -070051 event_loop, s, control_loops::drivetrain::GetDrivetrainConfig()) {}
Austin Schuha3c148e2018-03-09 21:04:05 -080052
53bool AutonomousActor::RunAction(
54 const ::frc971::autonomous::AutonomousActionParams &params) {
55 monotonic_clock::time_point start_time = monotonic_clock::now();
56 LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode);
57 Reset();
58
Austin Schuh4dad8fb2018-07-08 16:00:13 -070059 // Switch
60 /*
Austin Schuhc231df42018-03-21 20:43:24 -070061 switch (params.mode) {
Austin Schuh4dad8fb2018-07-08 16:00:13 -070062 case 0:
63 case 2: {
64 if (FarSwitch(start_time, true)) return true;
65 } break;
66
67 case 3:
Austin Schuhc231df42018-03-21 20:43:24 -070068 case 1: {
Austin Schuh4dad8fb2018-07-08 16:00:13 -070069 if (CloseSwitch(start_time)) return true;
Austin Schuhc231df42018-03-21 20:43:24 -070070 } break;
Austin Schuh4dad8fb2018-07-08 16:00:13 -070071 }
72 */
73 // Scale
74 switch (params.mode) {
75 case 0:
76 case 1: {
77 if (FarScale(start_time)) return true;
Austin Schuhc231df42018-03-21 20:43:24 -070078 } break;
79
80 case 3:
81 case 2: {
Austin Schuh4dad8fb2018-07-08 16:00:13 -070082 if (ThreeCubeAuto(start_time)) return true;
Austin Schuhc231df42018-03-21 20:43:24 -070083 } break;
Austin Schuha3c148e2018-03-09 21:04:05 -080084 }
Austin Schuh4dad8fb2018-07-08 16:00:13 -070085 /*
86 switch (params.mode) {
87 case 1: {
88 if (FarScale(start_time)) return true;
89 //if (CloseSwitch(start_time)) return true;
90 } break;
91 case 0: {
92 if (DriveStraight()) return true;
93 } break;
94 case 200: {
95 if (FarSwitch(start_time)) return true;
96 } break;
97
98 case 3:
99 case 2: {
100 if (ThreeCubeAuto(start_time)) {
101 return true;
102 }
103 } break;
104 }
105 */
Austin Schuha3c148e2018-03-09 21:04:05 -0800106
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700107 LOG(INFO, "Done %f\n",
108 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuha3c148e2018-03-09 21:04:05 -0800109
110 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
111 ::std::chrono::milliseconds(5) / 2);
112
113 while (!ShouldCancel()) {
114 phased_loop.SleepUntilNext();
115 }
116 LOG(DEBUG, "Done running\n");
117
118 return true;
119}
120
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700121bool AutonomousActor::FarSwitch(monotonic_clock::time_point start_time,
122 bool drive_behind, bool left) {
123 const double turn_scalar = left ? 1.0 : -1.0;
124 if (drive_behind) {
125 // Start on the left. Hit the switch.
126 constexpr double kFullDriveLength = 9.93;
127 constexpr double kTurnDistance = 4.40;
128 StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn);
129 set_arm_goal_position(arm::NeutralIndex());
130 set_grab_box(false);
131 SendSuperstructureGoal();
132 if (!WaitForDriveProfileNear(kFullDriveLength - (kTurnDistance - 1.4)))
133 return true;
134 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn);
135
136 if (!WaitForDriveProfileNear(kFullDriveLength - kTurnDistance)) return true;
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700137 StartDrive(0.0, turn_scalar * (-M_PI / 2.0), kFarSwitchTurnDrive,
138 kSweepingTurn);
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700139 if (!WaitForTurnProfileDone()) return true;
140
141 // Now, close so let's move the arm up.
142 set_arm_goal_position(arm::FrontSwitchAutoIndex());
143 SendSuperstructureGoal();
144
145 StartDrive(0.0, 0.0, kDrive, kTurn);
146 if (!WaitForDriveProfileNear(2.0)) return true;
147 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kFastTurn);
148
149 if (!WaitForDriveProfileNear(1.3)) return true;
150
151 StartDrive(0.0, turn_scalar * M_PI / 2.0, kFarSwitchTurnDrive, kFastTurn);
152 if (!WaitForTurnProfileDone()) return true;
153
154 constexpr double kGentlePushDrive = 0.70;
155 StartDrive(kGentlePushDrive, 0.0, kSlowDrive, kTurn);
156
157 if (!WaitForDriveProfileNear(kGentlePushDrive - 0.25)) return true;
158 // Turn down the peak voltage when we push against the wall so we don't blow
159 // breakers or pull the battery voltage down too far.
160 set_max_drivetrain_voltage(6.0);
161 StartDrive(0.00, 0.0, kFinalSwitchDrive, kTurn);
162
163 if (!WaitForArmTrajectoryClose(0.001)) return true;
164 LOG(INFO, "Arm close at %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700165 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700166
167 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
168
169 set_open_claw(true);
170 SendSuperstructureGoal();
171
172 ::std::this_thread::sleep_for(chrono::milliseconds(1500));
173 set_arm_goal_position(arm::NeutralIndex());
174 SendSuperstructureGoal();
175 if (ShouldCancel()) return true;
176 set_max_drivetrain_voltage(12.0);
177 StartDrive(-0.5, 0.0, kDrive, kTurn);
178 if (!WaitForDriveProfileDone()) return true;
179 } else {
180 // Start on the left. Hit the switch.
181 constexpr double kFullDriveLength = 5.55;
182 constexpr double kTurnDistance = 0.35;
183 StartDrive(-kFullDriveLength, 0.0, kFarSwitchTurnDrive, kTurn);
184
185 if (!WaitForDriveProfileNear(kFullDriveLength - kTurnDistance)) return true;
186 StartDrive(0.0, turn_scalar * (-M_PI / 2.0), kFarSwitchTurnDrive,
187 kSweepingTurn);
188 if (!WaitForTurnProfileDone()) return true;
189 StartDrive(0.0, 0.0, kDrive, kTurn);
190 if (!WaitForDriveProfileDone()) return true;
191
192 // Now, close so let's move the arm up.
193 set_arm_goal_position(arm::FrontSwitchIndex());
194 SendSuperstructureGoal();
195
196 StartDrive(0.0, turn_scalar * (-M_PI / 2.0), kDrive, kFastTurn);
197 if (!WaitForTurnProfileDone()) return true;
198
199 set_max_drivetrain_voltage(10.0);
200 StartDrive(1.1, 0.0, kDrive, kTurn);
201 if (!WaitForArmTrajectoryClose(0.001)) return true;
202 if (!WaitForDriveNear(0.6, M_PI / 2.0)) return true;
203 StartDrive(0.0, 0.0, kFinalFrontFarSwitchDrive, kTurn);
204
205 if (!WaitForDriveNear(0.3, M_PI / 2.0)) return true;
206 set_max_drivetrain_voltage(6.0);
207 StartDrive(0.0, 0.0, kFinalFrontFarSwitchDrive, kTurn);
208
209 // if (!WaitForDriveNear(0.2, M_PI / 2.0)) return true;
210 ::std::this_thread::sleep_for(chrono::milliseconds(300));
211
212 set_open_claw(true);
213 SendSuperstructureGoal();
214
215 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
216 set_arm_goal_position(arm::NeutralIndex());
217 SendSuperstructureGoal();
218 if (ShouldCancel()) return true;
219 set_max_drivetrain_voltage(12.0);
220 StartDrive(-0.5, 0.0, kDrive, kTurn);
221 if (!WaitForDriveProfileDone()) return true;
222 }
223 return false;
224}
225
226bool AutonomousActor::FarScale(monotonic_clock::time_point start_time) {
227 // Start on the left. Hit the switch.
228 constexpr double kFullDriveLength = 11.40;
229 constexpr double kFirstTurnDistance = 4.40;
230 constexpr double kSecondTurnDistance = 9.30;
231 StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn);
232 if (!WaitForDriveProfileNear(kFullDriveLength - (kFirstTurnDistance - 1.4)))
233 return true;
234 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn);
235
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700236 if (!WaitForDriveProfileNear(kFullDriveLength - kFirstTurnDistance))
237 return true;
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700238 StartDrive(0.0, -M_PI / 2.0, kFarSwitchTurnDrive, kSweepingTurn);
239 set_arm_goal_position(arm::BackHighBoxIndex());
240 SendSuperstructureGoal();
241 if (!WaitForTurnProfileDone()) return true;
242
243 StartDrive(0.0, 0.0, kDrive, kTurn);
244
245 if (!WaitForDriveProfileNear(kFullDriveLength - (kSecondTurnDistance - 1.4)))
246 return true;
247
248 StartDrive(0.0, 0.0, kFarScaleFinalTurnDrive, kTurn);
249 if (!WaitForDriveProfileNear(kFullDriveLength - (kSecondTurnDistance)))
250 return true;
251 LOG(INFO, "Final turn at %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700252 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700253
254 StartDrive(0.0, M_PI / 2.0, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
255 if (!WaitForDriveProfileNear(0.15)) return true;
256
257 StartDrive(0.0, 0.3, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
258
259 LOG(INFO, "Dropping 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 set_open_claw(true);
262 SendSuperstructureGoal();
263
264 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
265 LOG(INFO, "Backing up at %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700266 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700267
268 StartDrive(1.5, -0.55, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
269
270 if (!WaitForDriveProfileNear(1.4)) return true;
271 set_arm_goal_position(arm::NeutralIndex());
272 set_open_claw(false);
273 set_grab_box(true);
274 SendSuperstructureGoal();
275
276 if (!WaitForDriveProfileNear(0.3)) return true;
277
278 set_intake_angle(0.15);
279 set_roller_voltage(10.0);
280 SendSuperstructureGoal();
281
282 set_max_drivetrain_voltage(6.0);
283 StartDrive(0.0, 0.0, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
284
285 if (!WaitForDriveProfileDone()) return true;
286 if (!WaitForTurnProfileDone()) return true;
287
288 StartDrive(0.2, 0.0, kThirdBoxSlowBackup, kThirdBoxSlowTurn);
289
290 if (!WaitForBoxGrabed()) return true;
291 set_arm_goal_position(arm::BackHighBoxIndex());
292 set_intake_angle(-3.0);
293 set_roller_voltage(0.0);
294 set_grab_box(false);
295 SendSuperstructureGoal();
296
297 StartDrive(-1.40, 0.15, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
298
299 if (!WaitForDriveProfileNear(0.1)) return true;
300
301 StartDrive(0.0, 0.5, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
302
303 if (!WaitForDriveProfileDone()) return true;
304 if (!WaitForTurnProfileDone()) return true;
305 ::std::this_thread::sleep_for(chrono::milliseconds(500));
306
307 LOG(INFO, "Dropping second box at %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700308 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700309 set_open_claw(true);
310 SendSuperstructureGoal();
311
312 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
313
314 StartDrive(1.4, -0.7, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
315 ::std::this_thread::sleep_for(chrono::milliseconds(200));
316 set_arm_goal_position(arm::NeutralIndex());
317 set_open_claw(false);
318 SendSuperstructureGoal();
319
320 if (!WaitForDriveProfileNear(1.0)) return true;
321
322 StartDrive(0.0, -0.6, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
323 return false;
324}
325
326bool AutonomousActor::FarReadyScale(monotonic_clock::time_point start_time) {
327 // Start on the left. Hit the switch.
328 constexpr double kFullDriveLength = 7.5;
329 constexpr double kFirstTurnDistance = 4.40;
330 StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn);
331 if (!WaitForDriveProfileNear(kFullDriveLength - (kFirstTurnDistance - 1.4)))
332 return true;
333 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn);
334
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700335 if (!WaitForDriveProfileNear(kFullDriveLength - kFirstTurnDistance))
336 return true;
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700337 StartDrive(0.0, -M_PI / 2.0, kFarSwitchTurnDrive, kSweepingTurn);
338 set_arm_goal_position(arm::UpIndex());
339 SendSuperstructureGoal();
340 LOG(INFO, "Lifting arm at %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700341 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700342 if (!WaitForTurnProfileDone()) return true;
343
344 StartDrive(0.0, 0.0, kDrive, kTurn);
345 return false;
346}
347
348bool AutonomousActor::DriveStraight() {
349 StartDrive(-3.2, 0.0, kDrive, kTurn);
350 if (!WaitForDriveProfileDone()) return true;
351 return false;
352}
353
354bool AutonomousActor::CloseSwitch(monotonic_clock::time_point start_time,
355 bool left) {
356 const double turn_scalar = left ? 1.0 : -1.0;
357
358 constexpr double kDriveDistance = 3.2;
359 // Start on the left. Drive, arc a turn, and drop in the close switch.
360 StartDrive(-kDriveDistance, 0.0, kDrive, kTurn);
361 if (!WaitForDriveNear(2.5, M_PI / 2.0)) return true;
362
363 // Now, close so let's move the arm up.
364 set_arm_goal_position(arm::BackSwitchIndex());
365 SendSuperstructureGoal();
366
367 StartDrive(0.0, 0.0, kSlowDrive, kSweepingTurn);
368 if (!WaitForDriveNear(1.6, M_PI / 2.0)) return true;
369
370 StartDrive(0.0, turn_scalar * (-M_PI / 4.0 - 0.2), kSlowDrive, kSweepingTurn);
371 if (!WaitForDriveNear(0.2, 0.2)) return true;
372 set_max_drivetrain_voltage(6.0);
373 LOG(INFO, "Lowered drivetrain voltage %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700374 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700375 ::std::this_thread::sleep_for(chrono::milliseconds(300));
376
377 set_open_claw(true);
378 SendSuperstructureGoal();
379
380 ::std::this_thread::sleep_for(chrono::milliseconds(500));
381 set_max_drivetrain_voltage(12.0);
382 StartDrive(0.7, 0.0, kDrive, kTurn);
383 if (!WaitForTurnProfileDone()) return true;
384
385 set_arm_goal_position(arm::NeutralIndex());
386 SendSuperstructureGoal();
387 return false;
388}
389
390bool AutonomousActor::ThreeCubeAuto(monotonic_clock::time_point start_time) {
391 // Start on the left. Hit the scale.
392 constexpr double kDriveDistance = 6.95;
393 // Distance and angle to do the big drive to the third cube.
394 constexpr double kThirdCubeDrive = 1.57 + 0.05 + 0.05;
395 constexpr double kThirdCubeTurn = M_PI / 4.0 + 0.1;
396 // Angle to do the slow pickup turn on the third box.
397 constexpr double kThirdBoxEndTurnAngle = 0.30;
398
399 // Distance to drive back to the scale with the third cube.
400 constexpr double kThirdCubeDropDrive = kThirdCubeDrive + 0.40;
401
402 // Drive.
403 StartDrive(-kDriveDistance, 0.0, kDrive, kTurn);
404 if (!WaitForDriveNear(kDriveDistance - 1.0, M_PI / 2.0)) return true;
405 // Once we are away from the wall, start the arm.
406 set_arm_goal_position(arm::BackMiddle2BoxIndex());
407 SendSuperstructureGoal();
408
409 // We are starting to get close. Slow down for the turn.
410 if (!WaitForDriveNear(4.0, M_PI / 2.0)) return true;
411 StartDrive(0.0, 0.0, kScaleTurnDrive, kFastTurn);
412
413 // Once we've gotten slowed down a bit, start turning.
414 if (!WaitForDriveNear(3.25, M_PI / 2.0)) return true;
415 StartDrive(0.0, -M_PI / 6.0, kScaleTurnDrive, kFastTurn);
416
417 if (!WaitForDriveNear(1.0, M_PI / 2.0)) return true;
418 StartDrive(0.0, M_PI / 6.0, kScaleTurnDrive, kFastTurn);
419
420 // Get close and open the claw.
421 if (!WaitForDriveNear(0.15, 0.25)) return true;
422 set_open_claw(true);
423 SendSuperstructureGoal();
424 set_intake_angle(-0.60);
425 LOG(INFO, "Dropped first box %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700426 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700427
428 ::std::this_thread::sleep_for(chrono::milliseconds(700));
429
430 set_grab_box(true);
431 SendSuperstructureGoal();
432
433 LOG(INFO, "Starting second box drive %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 constexpr double kSecondBoxSwerveAngle = 0.35;
436 constexpr double kSecondBoxDrive = 1.38;
437 StartDrive(kSecondBoxDrive, 0.0, kDrive, kFastTurn);
438 if (!WaitForDriveNear(kSecondBoxDrive - 0.2, M_PI / 2.0)) return true;
439
440 StartDrive(0.0, kSecondBoxSwerveAngle, kDrive, kFastTurn);
441 if (!WaitForDriveNear(0.5, M_PI / 2.0)) return true;
442
443 set_open_claw(true);
444 set_disable_box_correct(false);
445 SendSuperstructureGoal();
446
447 StartDrive(0.0, -kSecondBoxSwerveAngle, kDrive, kFastTurn);
448
449 if (!WaitForDriveProfileDone()) return true;
450 set_max_drivetrain_voltage(6.0);
451 StartDrive(0.0, 0.0, kDrive, kFastTurn);
452
453 set_intake_angle(0.15);
454 set_arm_goal_position(arm::BackHighBoxIndex());
455 set_open_claw(false);
456
457 set_roller_voltage(10.0);
458 SendSuperstructureGoal();
459
460 LOG(INFO, "Grabbing second box %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700461 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700462 ::std::this_thread::sleep_for(chrono::milliseconds(200));
463 StartDrive(-0.04, 0.0, kThirdBoxSlowBackup, kThirdBoxSlowTurn);
464
465 if (!WaitForBoxGrabed()) return true;
466 set_max_drivetrain_voltage(12.0);
467
468 LOG(INFO, "Got 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(500));
471
472 set_grab_box(false);
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700473 // set_arm_goal_position(arm::UpIndex());
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700474 set_arm_goal_position(arm::BackHighBoxIndex());
475 set_roller_voltage(0.0);
476 set_disable_box_correct(false);
477 SendSuperstructureGoal();
478 LOG(INFO, "Driving to place second box %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700479 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700480
481 StartDrive(-kSecondBoxDrive + 0.16, kSecondBoxSwerveAngle, kDrive, kFastTurn);
482 if (!WaitForDriveNear(0.4, M_PI / 2.0)) return true;
483
484 constexpr double kSecondBoxEndExtraAngle = 0.3;
485 StartDrive(0.0, -kSecondBoxSwerveAngle - kSecondBoxEndExtraAngle, kDrive,
486 kFastTurn);
487
488 LOG(INFO, "Starting throw %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700489 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700490 if (!WaitForDriveNear(0.4, M_PI / 2.0)) return true;
491 if (!WaitForArmTrajectoryClose(0.25)) return true;
492 SendSuperstructureGoal();
493
494 // Throw the box.
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700495 // if (!WaitForArmTrajectoryClose(0.20)) return true;
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700496 if (!WaitForDriveNear(0.2, M_PI / 2.0)) return true;
497
498 set_open_claw(true);
499 set_intake_angle(-M_PI / 4.0);
500 LOG(INFO, "Releasing second box %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700501 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700502 SendSuperstructureGoal();
503
504 ::std::this_thread::sleep_for(chrono::milliseconds(700));
505 set_open_claw(false);
506 SendSuperstructureGoal();
507
508 LOG(INFO, "Driving to third 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 StartDrive(kThirdCubeDrive, kSecondBoxEndExtraAngle, kThirdBoxDrive,
511 kFastTurn);
512 if (!WaitForDriveNear(kThirdCubeDrive - 0.1, M_PI / 4.0)) return true;
513 set_grab_box(true);
514 SendSuperstructureGoal();
515
516 StartDrive(0.0, kThirdCubeTurn, kThirdBoxDrive, kFastTurn);
517 if (!WaitForDriveNear(0.05, M_PI / 4.0)) return true;
518
519 set_intake_angle(0.05);
520 set_roller_voltage(9.0);
521 SendSuperstructureGoal();
522
523 if (!WaitForDriveProfileDone()) return true;
524 if (!WaitForTurnProfileDone()) return true;
525 StartDrive(0.35, kThirdBoxEndTurnAngle, kThirdBoxSlowBackup,
526 kThirdBoxSlowTurn);
527 if (!WaitForDriveProfileDone()) return true;
528
529 LOG(INFO, "Waiting for third box %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700530 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700531 if (!WaitForBoxGrabed()) return true;
532 LOG(INFO, "Third box grabbed %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700533 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700534 const bool too_late =
535 monotonic_clock::now() > start_time + chrono::milliseconds(12500);
536 if (too_late) {
537 LOG(INFO, "Third box too long, going up. %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 set_grab_box(false);
540 set_arm_goal_position(arm::UpIndex());
541 set_roller_voltage(0.0);
542 SendSuperstructureGoal();
543 }
544 ::std::this_thread::sleep_for(chrono::milliseconds(400));
545
546 set_grab_box(false);
547 if (!too_late) {
548 set_arm_goal_position(arm::BackMiddle2BoxIndex());
549 }
550 set_roller_voltage(0.0);
551 SendSuperstructureGoal();
552
553 StartDrive(-kThirdCubeDropDrive, 0.0, kThirdBoxPlaceDrive, kReallyFastTurn);
554
555 if (!WaitForDriveNear(kThirdCubeDropDrive - 0.23, M_PI / 4.0)) return true;
556 StartDrive(0.0, -kThirdCubeTurn - 0.2 - kThirdBoxEndTurnAngle - 0.3,
557 kThirdBoxPlaceDrive, kReallyFastTurn);
558
559 if (!WaitForDriveNear(0.30, M_PI / 4.0 + 0.2)) return true;
560
561 if (!too_late) {
562 set_open_claw(true);
563 set_intake_angle(-M_PI / 4.0);
564 set_roller_voltage(0.0);
565 SendSuperstructureGoal();
566
567 LOG(INFO, "Final open %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700568 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700569 }
570
571 if (!WaitForDriveProfileDone()) return true;
572 if (!WaitForTurnProfileDone()) return true;
573
574 ::std::this_thread::sleep_for(chrono::milliseconds(14750) -
575 (monotonic_clock::now() - start_time));
576
577 set_arm_goal_position(arm::UpIndex());
578 SendSuperstructureGoal();
579
580 ::std::this_thread::sleep_for(chrono::milliseconds(15000) -
581 (monotonic_clock::now() - start_time));
582
583 set_close_claw(true);
584 SendSuperstructureGoal();
585 return false;
586}
587
Austin Schuha3c148e2018-03-09 21:04:05 -0800588} // namespace actors
589} // namespace y2018