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