blob: ea9daab256d8300ed74cc6ae8e7b7326239161df [file] [log] [blame]
Austin Schuha3c148e2018-03-09 21:04:05 -08001#include "y2018/actors/autonomous_actor.h"
2
Austin Schuha3c148e2018-03-09 21:04:05 -08003#include <chrono>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07004#include <cinttypes>
Austin Schuha3c148e2018-03-09 21:04:05 -08005#include <cmath>
6
John Park33858a32018-09-28 23:05:48 -07007#include "aos/logging/logging.h"
James Kuszmaul651fc3f2019-05-15 21:14:25 -07008#include "aos/util/phased_loop.h"
Austin Schuha3c148e2018-03-09 21:04:05 -08009#include "y2018/control_loops/drivetrain/drivetrain_base.h"
10
11namespace y2018 {
12namespace actors {
Alex Perrycb7da4b2019-08-28 19:35:56 -070013using ::frc971::ProfileParametersT;
Austin Schuha3c148e2018-03-09 21:04:05 -080014
Austin Schuha3c148e2018-03-09 21:04:05 -080015using ::aos::monotonic_clock;
16namespace chrono = ::std::chrono;
17namespace this_thread = ::std::this_thread;
18
19namespace {
20
Austin Schuhbd0a40f2019-06-30 14:56:31 -070021namespace arm = ::y2018::control_loops::superstructure::arm;
22
Alex Perrycb7da4b2019-08-28 19:35:56 -070023ProfileParametersT MakeProfileParameters(float max_velocity,
24 float max_acceleration) {
25 ProfileParametersT result;
26 result.max_velocity = max_velocity;
27 result.max_acceleration = max_acceleration;
28 return result;
29}
Austin Schuhf79c0e52018-04-04 20:13:21 -070030
Alex Perrycb7da4b2019-08-28 19:35:56 -070031const ProfileParametersT kFinalSwitchDrive = MakeProfileParameters(0.5, 1.5);
32const ProfileParametersT kDrive = MakeProfileParameters(5.0, 2.5);
33const ProfileParametersT kThirdBoxDrive = MakeProfileParameters(3.0, 2.5);
34const ProfileParametersT kSlowDrive = MakeProfileParameters(1.5, 2.5);
35const ProfileParametersT kScaleTurnDrive = MakeProfileParameters(3.0, 2.5);
36const ProfileParametersT kFarSwitchTurnDrive = MakeProfileParameters(2.0, 2.5);
37const ProfileParametersT kFarScaleFinalTurnDrive = kFarSwitchTurnDrive;
38const ProfileParametersT kTurn = MakeProfileParameters(4.0, 2.0);
39const ProfileParametersT kSweepingTurn = MakeProfileParameters(5.0, 7.0);
40const ProfileParametersT kFarScaleSweepingTurn = kSweepingTurn;
41const ProfileParametersT kFastTurn = MakeProfileParameters(5.0, 7.0);
42const ProfileParametersT kReallyFastTurn = MakeProfileParameters(1.5, 9.0);
Austin Schuhf79c0e52018-04-04 20:13:21 -070043
Alex Perrycb7da4b2019-08-28 19:35:56 -070044const ProfileParametersT kThirdBoxSlowBackup = MakeProfileParameters(0.35, 1.5);
45const ProfileParametersT kThirdBoxSlowTurn = MakeProfileParameters(1.5, 4.0);
Austin Schuhf79c0e52018-04-04 20:13:21 -070046
Alex Perrycb7da4b2019-08-28 19:35:56 -070047const ProfileParametersT kThirdBoxPlaceDrive = MakeProfileParameters(4.0, 2.0);
48
49const ProfileParametersT kFinalFrontFarSwitchDrive =
50 MakeProfileParameters(2.0, 2.0);
Austin Schuha3c148e2018-03-09 21:04:05 -080051
52} // namespace
53
Austin Schuh1bf8a212019-05-26 22:13:14 -070054AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
Austin Schuha3c148e2018-03-09 21:04:05 -080055 : frc971::autonomous::BaseAutonomousActor(
Austin Schuhd845c972019-06-29 21:20:05 -070056 event_loop, control_loops::drivetrain::GetDrivetrainConfig()),
57 superstructure_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070058 event_loop->MakeSender<::y2018::control_loops::superstructure::Goal>(
59 "/superstructure")),
Austin Schuhd845c972019-06-29 21:20:05 -070060 superstructure_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070061 event_loop
62 ->MakeFetcher<::y2018::control_loops::superstructure::Status>(
63 "/superstructure")) {}
Austin Schuha3c148e2018-03-09 21:04:05 -080064
65bool AutonomousActor::RunAction(
Alex Perrycb7da4b2019-08-28 19:35:56 -070066 const ::frc971::autonomous::AutonomousActionParams *params) {
Austin Schuh77ed5432019-07-07 20:41:36 -070067 const monotonic_clock::time_point start_time = monotonic_now();
Austin Schuhf257f3c2019-10-27 21:00:43 -070068 AOS_LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -070069 params->mode());
Austin Schuha3c148e2018-03-09 21:04:05 -080070 Reset();
71
Austin Schuh4dad8fb2018-07-08 16:00:13 -070072 // Switch
73 /*
Alex Perrycb7da4b2019-08-28 19:35:56 -070074 switch (params->mode()) {
Austin Schuh4dad8fb2018-07-08 16:00:13 -070075 case 0:
76 case 2: {
77 if (FarSwitch(start_time, true)) return true;
78 } break;
79
80 case 3:
Austin Schuhc231df42018-03-21 20:43:24 -070081 case 1: {
Austin Schuh4dad8fb2018-07-08 16:00:13 -070082 if (CloseSwitch(start_time)) return true;
Austin Schuhc231df42018-03-21 20:43:24 -070083 } break;
Austin Schuh4dad8fb2018-07-08 16:00:13 -070084 }
85 */
86 // Scale
Alex Perrycb7da4b2019-08-28 19:35:56 -070087 switch (params->mode()) {
Austin Schuh4dad8fb2018-07-08 16:00:13 -070088 case 0:
89 case 1: {
90 if (FarScale(start_time)) return true;
Austin Schuhc231df42018-03-21 20:43:24 -070091 } break;
92
93 case 3:
94 case 2: {
Austin Schuh4dad8fb2018-07-08 16:00:13 -070095 if (ThreeCubeAuto(start_time)) return true;
Austin Schuhc231df42018-03-21 20:43:24 -070096 } break;
Austin Schuha3c148e2018-03-09 21:04:05 -080097 }
Austin Schuh4dad8fb2018-07-08 16:00:13 -070098 /*
Alex Perrycb7da4b2019-08-28 19:35:56 -070099 switch (params->mode()) {
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700100 case 1: {
101 if (FarScale(start_time)) return true;
102 //if (CloseSwitch(start_time)) return true;
103 } break;
104 case 0: {
105 if (DriveStraight()) return true;
106 } break;
107 case 200: {
108 if (FarSwitch(start_time)) return true;
109 } break;
110
111 case 3:
112 case 2: {
113 if (ThreeCubeAuto(start_time)) {
114 return true;
115 }
116 } break;
117 }
118 */
Austin Schuha3c148e2018-03-09 21:04:05 -0800119
Austin Schuhf257f3c2019-10-27 21:00:43 -0700120 AOS_LOG(INFO, "Done %f\n",
121 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuha3c148e2018-03-09 21:04:05 -0800122
Yash Chainania6fe97b2021-12-15 21:01:11 -0800123 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
Austin Schuhd32b3622019-06-23 18:49:06 -0700124 event_loop()->monotonic_now(),
Yash Chainania6fe97b2021-12-15 21:01:11 -0800125 ActorBase::kLoopOffset);
Austin Schuha3c148e2018-03-09 21:04:05 -0800126
127 while (!ShouldCancel()) {
128 phased_loop.SleepUntilNext();
129 }
Austin Schuhf257f3c2019-10-27 21:00:43 -0700130 AOS_LOG(DEBUG, "Done running\n");
Austin Schuha3c148e2018-03-09 21:04:05 -0800131
132 return true;
133}
134
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700135bool AutonomousActor::FarSwitch(monotonic_clock::time_point start_time,
136 bool drive_behind, bool left) {
137 const double turn_scalar = left ? 1.0 : -1.0;
138 if (drive_behind) {
139 // Start on the left. Hit the switch.
140 constexpr double kFullDriveLength = 9.93;
141 constexpr double kTurnDistance = 4.40;
142 StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn);
143 set_arm_goal_position(arm::NeutralIndex());
144 set_grab_box(false);
145 SendSuperstructureGoal();
146 if (!WaitForDriveProfileNear(kFullDriveLength - (kTurnDistance - 1.4)))
147 return true;
148 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn);
149
150 if (!WaitForDriveProfileNear(kFullDriveLength - kTurnDistance)) return true;
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700151 StartDrive(0.0, turn_scalar * (-M_PI / 2.0), kFarSwitchTurnDrive,
152 kSweepingTurn);
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700153 if (!WaitForTurnProfileDone()) return true;
154
155 // Now, close so let's move the arm up.
156 set_arm_goal_position(arm::FrontSwitchAutoIndex());
157 SendSuperstructureGoal();
158
159 StartDrive(0.0, 0.0, kDrive, kTurn);
160 if (!WaitForDriveProfileNear(2.0)) return true;
161 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kFastTurn);
162
163 if (!WaitForDriveProfileNear(1.3)) return true;
164
165 StartDrive(0.0, turn_scalar * M_PI / 2.0, kFarSwitchTurnDrive, kFastTurn);
166 if (!WaitForTurnProfileDone()) return true;
167
168 constexpr double kGentlePushDrive = 0.70;
169 StartDrive(kGentlePushDrive, 0.0, kSlowDrive, kTurn);
170
171 if (!WaitForDriveProfileNear(kGentlePushDrive - 0.25)) return true;
172 // Turn down the peak voltage when we push against the wall so we don't blow
173 // breakers or pull the battery voltage down too far.
174 set_max_drivetrain_voltage(6.0);
175 StartDrive(0.00, 0.0, kFinalSwitchDrive, kTurn);
176
177 if (!WaitForArmTrajectoryClose(0.001)) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700178 AOS_LOG(INFO, "Arm close at %f\n",
179 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700180
181 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
182
183 set_open_claw(true);
184 SendSuperstructureGoal();
185
186 ::std::this_thread::sleep_for(chrono::milliseconds(1500));
187 set_arm_goal_position(arm::NeutralIndex());
188 SendSuperstructureGoal();
189 if (ShouldCancel()) return true;
190 set_max_drivetrain_voltage(12.0);
191 StartDrive(-0.5, 0.0, kDrive, kTurn);
192 if (!WaitForDriveProfileDone()) return true;
193 } else {
194 // Start on the left. Hit the switch.
195 constexpr double kFullDriveLength = 5.55;
196 constexpr double kTurnDistance = 0.35;
197 StartDrive(-kFullDriveLength, 0.0, kFarSwitchTurnDrive, kTurn);
198
199 if (!WaitForDriveProfileNear(kFullDriveLength - kTurnDistance)) return true;
200 StartDrive(0.0, turn_scalar * (-M_PI / 2.0), kFarSwitchTurnDrive,
201 kSweepingTurn);
202 if (!WaitForTurnProfileDone()) return true;
203 StartDrive(0.0, 0.0, kDrive, kTurn);
204 if (!WaitForDriveProfileDone()) return true;
205
206 // Now, close so let's move the arm up.
207 set_arm_goal_position(arm::FrontSwitchIndex());
208 SendSuperstructureGoal();
209
210 StartDrive(0.0, turn_scalar * (-M_PI / 2.0), kDrive, kFastTurn);
211 if (!WaitForTurnProfileDone()) return true;
212
213 set_max_drivetrain_voltage(10.0);
214 StartDrive(1.1, 0.0, kDrive, kTurn);
215 if (!WaitForArmTrajectoryClose(0.001)) return true;
216 if (!WaitForDriveNear(0.6, M_PI / 2.0)) return true;
217 StartDrive(0.0, 0.0, kFinalFrontFarSwitchDrive, kTurn);
218
219 if (!WaitForDriveNear(0.3, M_PI / 2.0)) return true;
220 set_max_drivetrain_voltage(6.0);
221 StartDrive(0.0, 0.0, kFinalFrontFarSwitchDrive, kTurn);
222
223 // if (!WaitForDriveNear(0.2, M_PI / 2.0)) return true;
224 ::std::this_thread::sleep_for(chrono::milliseconds(300));
225
226 set_open_claw(true);
227 SendSuperstructureGoal();
228
229 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
230 set_arm_goal_position(arm::NeutralIndex());
231 SendSuperstructureGoal();
232 if (ShouldCancel()) return true;
233 set_max_drivetrain_voltage(12.0);
234 StartDrive(-0.5, 0.0, kDrive, kTurn);
235 if (!WaitForDriveProfileDone()) return true;
236 }
237 return false;
238}
239
240bool AutonomousActor::FarScale(monotonic_clock::time_point start_time) {
241 // Start on the left. Hit the switch.
242 constexpr double kFullDriveLength = 11.40;
243 constexpr double kFirstTurnDistance = 4.40;
244 constexpr double kSecondTurnDistance = 9.30;
245 StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn);
246 if (!WaitForDriveProfileNear(kFullDriveLength - (kFirstTurnDistance - 1.4)))
247 return true;
248 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn);
249
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700250 if (!WaitForDriveProfileNear(kFullDriveLength - kFirstTurnDistance))
251 return true;
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700252 StartDrive(0.0, -M_PI / 2.0, kFarSwitchTurnDrive, kSweepingTurn);
253 set_arm_goal_position(arm::BackHighBoxIndex());
254 SendSuperstructureGoal();
255 if (!WaitForTurnProfileDone()) return true;
256
257 StartDrive(0.0, 0.0, kDrive, kTurn);
258
259 if (!WaitForDriveProfileNear(kFullDriveLength - (kSecondTurnDistance - 1.4)))
260 return true;
261
262 StartDrive(0.0, 0.0, kFarScaleFinalTurnDrive, kTurn);
263 if (!WaitForDriveProfileNear(kFullDriveLength - (kSecondTurnDistance)))
264 return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700265 AOS_LOG(INFO, "Final turn at %f\n",
266 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700267
268 StartDrive(0.0, M_PI / 2.0, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
269 if (!WaitForDriveProfileNear(0.15)) return true;
270
271 StartDrive(0.0, 0.3, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
272
Austin Schuhf257f3c2019-10-27 21:00:43 -0700273 AOS_LOG(INFO, "Dropping at %f\n",
274 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700275 set_open_claw(true);
276 SendSuperstructureGoal();
277
278 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700279 AOS_LOG(INFO, "Backing up at %f\n",
280 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700281
282 StartDrive(1.5, -0.55, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
283
284 if (!WaitForDriveProfileNear(1.4)) return true;
285 set_arm_goal_position(arm::NeutralIndex());
286 set_open_claw(false);
287 set_grab_box(true);
288 SendSuperstructureGoal();
289
290 if (!WaitForDriveProfileNear(0.3)) return true;
291
292 set_intake_angle(0.15);
293 set_roller_voltage(10.0);
294 SendSuperstructureGoal();
295
296 set_max_drivetrain_voltage(6.0);
297 StartDrive(0.0, 0.0, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
298
299 if (!WaitForDriveProfileDone()) return true;
300 if (!WaitForTurnProfileDone()) return true;
301
302 StartDrive(0.2, 0.0, kThirdBoxSlowBackup, kThirdBoxSlowTurn);
303
304 if (!WaitForBoxGrabed()) return true;
305 set_arm_goal_position(arm::BackHighBoxIndex());
306 set_intake_angle(-3.0);
307 set_roller_voltage(0.0);
308 set_grab_box(false);
309 SendSuperstructureGoal();
310
311 StartDrive(-1.40, 0.15, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
312
313 if (!WaitForDriveProfileNear(0.1)) return true;
314
315 StartDrive(0.0, 0.5, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
316
317 if (!WaitForDriveProfileDone()) return true;
318 if (!WaitForTurnProfileDone()) return true;
319 ::std::this_thread::sleep_for(chrono::milliseconds(500));
320
Austin Schuhf257f3c2019-10-27 21:00:43 -0700321 AOS_LOG(INFO, "Dropping second box at %f\n",
322 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700323 set_open_claw(true);
324 SendSuperstructureGoal();
325
326 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
327
328 StartDrive(1.4, -0.7, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
329 ::std::this_thread::sleep_for(chrono::milliseconds(200));
330 set_arm_goal_position(arm::NeutralIndex());
331 set_open_claw(false);
332 SendSuperstructureGoal();
333
334 if (!WaitForDriveProfileNear(1.0)) return true;
335
336 StartDrive(0.0, -0.6, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
337 return false;
338}
339
340bool AutonomousActor::FarReadyScale(monotonic_clock::time_point start_time) {
341 // Start on the left. Hit the switch.
342 constexpr double kFullDriveLength = 7.5;
343 constexpr double kFirstTurnDistance = 4.40;
344 StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn);
345 if (!WaitForDriveProfileNear(kFullDriveLength - (kFirstTurnDistance - 1.4)))
346 return true;
347 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn);
348
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700349 if (!WaitForDriveProfileNear(kFullDriveLength - kFirstTurnDistance))
350 return true;
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700351 StartDrive(0.0, -M_PI / 2.0, kFarSwitchTurnDrive, kSweepingTurn);
352 set_arm_goal_position(arm::UpIndex());
353 SendSuperstructureGoal();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700354 AOS_LOG(INFO, "Lifting arm at %f\n",
355 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700356 if (!WaitForTurnProfileDone()) return true;
357
358 StartDrive(0.0, 0.0, kDrive, kTurn);
359 return false;
360}
361
362bool AutonomousActor::DriveStraight() {
363 StartDrive(-3.2, 0.0, kDrive, kTurn);
364 if (!WaitForDriveProfileDone()) return true;
365 return false;
366}
367
368bool AutonomousActor::CloseSwitch(monotonic_clock::time_point start_time,
369 bool left) {
370 const double turn_scalar = left ? 1.0 : -1.0;
371
372 constexpr double kDriveDistance = 3.2;
373 // Start on the left. Drive, arc a turn, and drop in the close switch.
374 StartDrive(-kDriveDistance, 0.0, kDrive, kTurn);
375 if (!WaitForDriveNear(2.5, M_PI / 2.0)) return true;
376
377 // Now, close so let's move the arm up.
378 set_arm_goal_position(arm::BackSwitchIndex());
379 SendSuperstructureGoal();
380
381 StartDrive(0.0, 0.0, kSlowDrive, kSweepingTurn);
382 if (!WaitForDriveNear(1.6, M_PI / 2.0)) return true;
383
384 StartDrive(0.0, turn_scalar * (-M_PI / 4.0 - 0.2), kSlowDrive, kSweepingTurn);
385 if (!WaitForDriveNear(0.2, 0.2)) return true;
386 set_max_drivetrain_voltage(6.0);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700387 AOS_LOG(INFO, "Lowered drivetrain voltage %f\n",
388 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700389 ::std::this_thread::sleep_for(chrono::milliseconds(300));
390
391 set_open_claw(true);
392 SendSuperstructureGoal();
393
394 ::std::this_thread::sleep_for(chrono::milliseconds(500));
395 set_max_drivetrain_voltage(12.0);
396 StartDrive(0.7, 0.0, kDrive, kTurn);
397 if (!WaitForTurnProfileDone()) return true;
398
399 set_arm_goal_position(arm::NeutralIndex());
400 SendSuperstructureGoal();
401 return false;
402}
403
404bool AutonomousActor::ThreeCubeAuto(monotonic_clock::time_point start_time) {
405 // Start on the left. Hit the scale.
406 constexpr double kDriveDistance = 6.95;
407 // Distance and angle to do the big drive to the third cube.
408 constexpr double kThirdCubeDrive = 1.57 + 0.05 + 0.05;
409 constexpr double kThirdCubeTurn = M_PI / 4.0 + 0.1;
410 // Angle to do the slow pickup turn on the third box.
411 constexpr double kThirdBoxEndTurnAngle = 0.30;
412
413 // Distance to drive back to the scale with the third cube.
414 constexpr double kThirdCubeDropDrive = kThirdCubeDrive + 0.40;
415
416 // Drive.
417 StartDrive(-kDriveDistance, 0.0, kDrive, kTurn);
418 if (!WaitForDriveNear(kDriveDistance - 1.0, M_PI / 2.0)) return true;
419 // Once we are away from the wall, start the arm.
420 set_arm_goal_position(arm::BackMiddle2BoxIndex());
421 SendSuperstructureGoal();
422
423 // We are starting to get close. Slow down for the turn.
424 if (!WaitForDriveNear(4.0, M_PI / 2.0)) return true;
425 StartDrive(0.0, 0.0, kScaleTurnDrive, kFastTurn);
426
427 // Once we've gotten slowed down a bit, start turning.
428 if (!WaitForDriveNear(3.25, M_PI / 2.0)) return true;
429 StartDrive(0.0, -M_PI / 6.0, kScaleTurnDrive, kFastTurn);
430
431 if (!WaitForDriveNear(1.0, M_PI / 2.0)) return true;
432 StartDrive(0.0, M_PI / 6.0, kScaleTurnDrive, kFastTurn);
433
434 // Get close and open the claw.
435 if (!WaitForDriveNear(0.15, 0.25)) return true;
436 set_open_claw(true);
437 SendSuperstructureGoal();
438 set_intake_angle(-0.60);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700439 AOS_LOG(INFO, "Dropped first box %f\n",
440 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700441
442 ::std::this_thread::sleep_for(chrono::milliseconds(700));
443
444 set_grab_box(true);
445 SendSuperstructureGoal();
446
Austin Schuhf257f3c2019-10-27 21:00:43 -0700447 AOS_LOG(INFO, "Starting second box drive %f\n",
448 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700449 constexpr double kSecondBoxSwerveAngle = 0.35;
450 constexpr double kSecondBoxDrive = 1.38;
451 StartDrive(kSecondBoxDrive, 0.0, kDrive, kFastTurn);
452 if (!WaitForDriveNear(kSecondBoxDrive - 0.2, M_PI / 2.0)) return true;
453
454 StartDrive(0.0, kSecondBoxSwerveAngle, kDrive, kFastTurn);
455 if (!WaitForDriveNear(0.5, M_PI / 2.0)) return true;
456
457 set_open_claw(true);
458 set_disable_box_correct(false);
459 SendSuperstructureGoal();
460
461 StartDrive(0.0, -kSecondBoxSwerveAngle, kDrive, kFastTurn);
462
463 if (!WaitForDriveProfileDone()) return true;
464 set_max_drivetrain_voltage(6.0);
465 StartDrive(0.0, 0.0, kDrive, kFastTurn);
466
467 set_intake_angle(0.15);
468 set_arm_goal_position(arm::BackHighBoxIndex());
469 set_open_claw(false);
470
471 set_roller_voltage(10.0);
472 SendSuperstructureGoal();
473
Austin Schuhf257f3c2019-10-27 21:00:43 -0700474 AOS_LOG(INFO, "Grabbing second box %f\n",
475 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700476 ::std::this_thread::sleep_for(chrono::milliseconds(200));
477 StartDrive(-0.04, 0.0, kThirdBoxSlowBackup, kThirdBoxSlowTurn);
478
479 if (!WaitForBoxGrabed()) return true;
480 set_max_drivetrain_voltage(12.0);
481
Austin Schuhf257f3c2019-10-27 21:00:43 -0700482 AOS_LOG(INFO, "Got second box %f\n",
483 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700484 ::std::this_thread::sleep_for(chrono::milliseconds(500));
485
486 set_grab_box(false);
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700487 // set_arm_goal_position(arm::UpIndex());
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700488 set_arm_goal_position(arm::BackHighBoxIndex());
489 set_roller_voltage(0.0);
490 set_disable_box_correct(false);
491 SendSuperstructureGoal();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700492 AOS_LOG(INFO, "Driving to place second box %f\n",
493 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700494
495 StartDrive(-kSecondBoxDrive + 0.16, kSecondBoxSwerveAngle, kDrive, kFastTurn);
496 if (!WaitForDriveNear(0.4, M_PI / 2.0)) return true;
497
498 constexpr double kSecondBoxEndExtraAngle = 0.3;
499 StartDrive(0.0, -kSecondBoxSwerveAngle - kSecondBoxEndExtraAngle, kDrive,
500 kFastTurn);
501
Austin Schuhf257f3c2019-10-27 21:00:43 -0700502 AOS_LOG(INFO, "Starting throw %f\n",
503 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700504 if (!WaitForDriveNear(0.4, M_PI / 2.0)) return true;
505 if (!WaitForArmTrajectoryClose(0.25)) return true;
506 SendSuperstructureGoal();
507
508 // Throw the box.
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700509 // if (!WaitForArmTrajectoryClose(0.20)) return true;
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700510 if (!WaitForDriveNear(0.2, M_PI / 2.0)) return true;
511
512 set_open_claw(true);
513 set_intake_angle(-M_PI / 4.0);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700514 AOS_LOG(INFO, "Releasing second box %f\n",
515 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700516 SendSuperstructureGoal();
517
518 ::std::this_thread::sleep_for(chrono::milliseconds(700));
519 set_open_claw(false);
520 SendSuperstructureGoal();
521
Austin Schuhf257f3c2019-10-27 21:00:43 -0700522 AOS_LOG(INFO, "Driving to third box %f\n",
523 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700524 StartDrive(kThirdCubeDrive, kSecondBoxEndExtraAngle, kThirdBoxDrive,
525 kFastTurn);
526 if (!WaitForDriveNear(kThirdCubeDrive - 0.1, M_PI / 4.0)) return true;
527 set_grab_box(true);
528 SendSuperstructureGoal();
529
530 StartDrive(0.0, kThirdCubeTurn, kThirdBoxDrive, kFastTurn);
531 if (!WaitForDriveNear(0.05, M_PI / 4.0)) return true;
532
533 set_intake_angle(0.05);
534 set_roller_voltage(9.0);
535 SendSuperstructureGoal();
536
537 if (!WaitForDriveProfileDone()) return true;
538 if (!WaitForTurnProfileDone()) return true;
539 StartDrive(0.35, kThirdBoxEndTurnAngle, kThirdBoxSlowBackup,
540 kThirdBoxSlowTurn);
541 if (!WaitForDriveProfileDone()) return true;
542
Austin Schuhf257f3c2019-10-27 21:00:43 -0700543 AOS_LOG(INFO, "Waiting for third box %f\n",
544 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700545 if (!WaitForBoxGrabed()) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700546 AOS_LOG(INFO, "Third box grabbed %f\n",
547 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700548 const bool too_late =
Austin Schuh77ed5432019-07-07 20:41:36 -0700549 monotonic_now() > start_time + chrono::milliseconds(12500);
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700550 if (too_late) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700551 AOS_LOG(INFO, "Third box too long, going up. %f\n",
552 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700553 set_grab_box(false);
554 set_arm_goal_position(arm::UpIndex());
555 set_roller_voltage(0.0);
556 SendSuperstructureGoal();
557 }
558 ::std::this_thread::sleep_for(chrono::milliseconds(400));
559
560 set_grab_box(false);
561 if (!too_late) {
562 set_arm_goal_position(arm::BackMiddle2BoxIndex());
563 }
564 set_roller_voltage(0.0);
565 SendSuperstructureGoal();
566
567 StartDrive(-kThirdCubeDropDrive, 0.0, kThirdBoxPlaceDrive, kReallyFastTurn);
568
569 if (!WaitForDriveNear(kThirdCubeDropDrive - 0.23, M_PI / 4.0)) return true;
570 StartDrive(0.0, -kThirdCubeTurn - 0.2 - kThirdBoxEndTurnAngle - 0.3,
571 kThirdBoxPlaceDrive, kReallyFastTurn);
572
573 if (!WaitForDriveNear(0.30, M_PI / 4.0 + 0.2)) return true;
574
575 if (!too_late) {
576 set_open_claw(true);
577 set_intake_angle(-M_PI / 4.0);
578 set_roller_voltage(0.0);
579 SendSuperstructureGoal();
580
Austin Schuhf257f3c2019-10-27 21:00:43 -0700581 AOS_LOG(INFO, "Final open %f\n",
582 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700583 }
584
585 if (!WaitForDriveProfileDone()) return true;
586 if (!WaitForTurnProfileDone()) return true;
587
588 ::std::this_thread::sleep_for(chrono::milliseconds(14750) -
Austin Schuh77ed5432019-07-07 20:41:36 -0700589 (monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700590
591 set_arm_goal_position(arm::UpIndex());
592 SendSuperstructureGoal();
593
594 ::std::this_thread::sleep_for(chrono::milliseconds(15000) -
Austin Schuh77ed5432019-07-07 20:41:36 -0700595 (monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700596
597 set_close_claw(true);
598 SendSuperstructureGoal();
599 return false;
600}
601
Austin Schuha3c148e2018-03-09 21:04:05 -0800602} // namespace actors
603} // namespace y2018