blob: ae017b5a66402e2b19aa6f757cc482ef345100fb [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
Stephan Pleinesf63bde82024-01-13 15:59:33 -080011namespace y2018::actors {
Alex Perrycb7da4b2019-08-28 19:35:56 -070012using ::frc971::ProfileParametersT;
Austin Schuha3c148e2018-03-09 21:04:05 -080013
Austin Schuha3c148e2018-03-09 21:04:05 -080014using ::aos::monotonic_clock;
15namespace chrono = ::std::chrono;
16namespace this_thread = ::std::this_thread;
17
18namespace {
19
Austin Schuhbd0a40f2019-06-30 14:56:31 -070020namespace arm = ::y2018::control_loops::superstructure::arm;
21
Alex Perrycb7da4b2019-08-28 19:35:56 -070022ProfileParametersT MakeProfileParameters(float max_velocity,
23 float max_acceleration) {
24 ProfileParametersT result;
25 result.max_velocity = max_velocity;
26 result.max_acceleration = max_acceleration;
27 return result;
28}
Austin Schuhf79c0e52018-04-04 20:13:21 -070029
Alex Perrycb7da4b2019-08-28 19:35:56 -070030const ProfileParametersT kFinalSwitchDrive = MakeProfileParameters(0.5, 1.5);
31const ProfileParametersT kDrive = MakeProfileParameters(5.0, 2.5);
32const ProfileParametersT kThirdBoxDrive = MakeProfileParameters(3.0, 2.5);
33const ProfileParametersT kSlowDrive = MakeProfileParameters(1.5, 2.5);
34const ProfileParametersT kScaleTurnDrive = MakeProfileParameters(3.0, 2.5);
35const ProfileParametersT kFarSwitchTurnDrive = MakeProfileParameters(2.0, 2.5);
36const ProfileParametersT kFarScaleFinalTurnDrive = kFarSwitchTurnDrive;
37const ProfileParametersT kTurn = MakeProfileParameters(4.0, 2.0);
38const ProfileParametersT kSweepingTurn = MakeProfileParameters(5.0, 7.0);
39const ProfileParametersT kFarScaleSweepingTurn = kSweepingTurn;
40const ProfileParametersT kFastTurn = MakeProfileParameters(5.0, 7.0);
41const ProfileParametersT kReallyFastTurn = MakeProfileParameters(1.5, 9.0);
Austin Schuhf79c0e52018-04-04 20:13:21 -070042
Alex Perrycb7da4b2019-08-28 19:35:56 -070043const ProfileParametersT kThirdBoxSlowBackup = MakeProfileParameters(0.35, 1.5);
44const ProfileParametersT kThirdBoxSlowTurn = MakeProfileParameters(1.5, 4.0);
Austin Schuhf79c0e52018-04-04 20:13:21 -070045
Alex Perrycb7da4b2019-08-28 19:35:56 -070046const ProfileParametersT kThirdBoxPlaceDrive = MakeProfileParameters(4.0, 2.0);
47
48const ProfileParametersT kFinalFrontFarSwitchDrive =
49 MakeProfileParameters(2.0, 2.0);
Austin Schuha3c148e2018-03-09 21:04:05 -080050
51} // namespace
52
Austin Schuh1bf8a212019-05-26 22:13:14 -070053AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
Austin Schuha3c148e2018-03-09 21:04:05 -080054 : frc971::autonomous::BaseAutonomousActor(
Austin Schuhd845c972019-06-29 21:20:05 -070055 event_loop, control_loops::drivetrain::GetDrivetrainConfig()),
56 superstructure_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070057 event_loop->MakeSender<::y2018::control_loops::superstructure::Goal>(
58 "/superstructure")),
Austin Schuhd845c972019-06-29 21:20:05 -070059 superstructure_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070060 event_loop
61 ->MakeFetcher<::y2018::control_loops::superstructure::Status>(
62 "/superstructure")) {}
Austin Schuha3c148e2018-03-09 21:04:05 -080063
64bool AutonomousActor::RunAction(
Alex Perrycb7da4b2019-08-28 19:35:56 -070065 const ::frc971::autonomous::AutonomousActionParams *params) {
Austin Schuh77ed5432019-07-07 20:41:36 -070066 const monotonic_clock::time_point start_time = monotonic_now();
Austin Schuhf257f3c2019-10-27 21:00:43 -070067 AOS_LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -070068 params->mode());
Austin Schuha3c148e2018-03-09 21:04:05 -080069 Reset();
70
Austin Schuh4dad8fb2018-07-08 16:00:13 -070071 // Switch
72 /*
Alex Perrycb7da4b2019-08-28 19:35:56 -070073 switch (params->mode()) {
Austin Schuh4dad8fb2018-07-08 16:00:13 -070074 case 0:
75 case 2: {
76 if (FarSwitch(start_time, true)) return true;
77 } break;
78
79 case 3:
Austin Schuhc231df42018-03-21 20:43:24 -070080 case 1: {
Austin Schuh4dad8fb2018-07-08 16:00:13 -070081 if (CloseSwitch(start_time)) return true;
Austin Schuhc231df42018-03-21 20:43:24 -070082 } break;
Austin Schuh4dad8fb2018-07-08 16:00:13 -070083 }
84 */
85 // Scale
Alex Perrycb7da4b2019-08-28 19:35:56 -070086 switch (params->mode()) {
Austin Schuh4dad8fb2018-07-08 16:00:13 -070087 case 0:
88 case 1: {
89 if (FarScale(start_time)) return true;
Austin Schuhc231df42018-03-21 20:43:24 -070090 } break;
91
92 case 3:
93 case 2: {
Austin Schuh4dad8fb2018-07-08 16:00:13 -070094 if (ThreeCubeAuto(start_time)) return true;
Austin Schuhc231df42018-03-21 20:43:24 -070095 } break;
Austin Schuha3c148e2018-03-09 21:04:05 -080096 }
Austin Schuh4dad8fb2018-07-08 16:00:13 -070097 /*
Alex Perrycb7da4b2019-08-28 19:35:56 -070098 switch (params->mode()) {
Austin Schuh4dad8fb2018-07-08 16:00:13 -070099 case 1: {
100 if (FarScale(start_time)) return true;
101 //if (CloseSwitch(start_time)) return true;
102 } break;
103 case 0: {
104 if (DriveStraight()) return true;
105 } break;
106 case 200: {
107 if (FarSwitch(start_time)) return true;
108 } break;
109
110 case 3:
111 case 2: {
112 if (ThreeCubeAuto(start_time)) {
113 return true;
114 }
115 } break;
116 }
117 */
Austin Schuha3c148e2018-03-09 21:04:05 -0800118
Austin Schuhf257f3c2019-10-27 21:00:43 -0700119 AOS_LOG(INFO, "Done %f\n",
120 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuha3c148e2018-03-09 21:04:05 -0800121
Yash Chainania6fe97b2021-12-15 21:01:11 -0800122 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
Austin Schuhd32b3622019-06-23 18:49:06 -0700123 event_loop()->monotonic_now(),
Yash Chainania6fe97b2021-12-15 21:01:11 -0800124 ActorBase::kLoopOffset);
Austin Schuha3c148e2018-03-09 21:04:05 -0800125
126 while (!ShouldCancel()) {
127 phased_loop.SleepUntilNext();
128 }
Austin Schuhf257f3c2019-10-27 21:00:43 -0700129 AOS_LOG(DEBUG, "Done running\n");
Austin Schuha3c148e2018-03-09 21:04:05 -0800130
131 return true;
132}
133
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700134bool AutonomousActor::FarSwitch(monotonic_clock::time_point start_time,
135 bool drive_behind, bool left) {
136 const double turn_scalar = left ? 1.0 : -1.0;
137 if (drive_behind) {
138 // Start on the left. Hit the switch.
139 constexpr double kFullDriveLength = 9.93;
140 constexpr double kTurnDistance = 4.40;
141 StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn);
142 set_arm_goal_position(arm::NeutralIndex());
143 set_grab_box(false);
144 SendSuperstructureGoal();
145 if (!WaitForDriveProfileNear(kFullDriveLength - (kTurnDistance - 1.4)))
146 return true;
147 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn);
148
149 if (!WaitForDriveProfileNear(kFullDriveLength - kTurnDistance)) return true;
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700150 StartDrive(0.0, turn_scalar * (-M_PI / 2.0), kFarSwitchTurnDrive,
151 kSweepingTurn);
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700152 if (!WaitForTurnProfileDone()) return true;
153
154 // Now, close so let's move the arm up.
155 set_arm_goal_position(arm::FrontSwitchAutoIndex());
156 SendSuperstructureGoal();
157
158 StartDrive(0.0, 0.0, kDrive, kTurn);
159 if (!WaitForDriveProfileNear(2.0)) return true;
160 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kFastTurn);
161
162 if (!WaitForDriveProfileNear(1.3)) return true;
163
164 StartDrive(0.0, turn_scalar * M_PI / 2.0, kFarSwitchTurnDrive, kFastTurn);
165 if (!WaitForTurnProfileDone()) return true;
166
167 constexpr double kGentlePushDrive = 0.70;
168 StartDrive(kGentlePushDrive, 0.0, kSlowDrive, kTurn);
169
170 if (!WaitForDriveProfileNear(kGentlePushDrive - 0.25)) return true;
171 // Turn down the peak voltage when we push against the wall so we don't blow
172 // breakers or pull the battery voltage down too far.
173 set_max_drivetrain_voltage(6.0);
174 StartDrive(0.00, 0.0, kFinalSwitchDrive, kTurn);
175
176 if (!WaitForArmTrajectoryClose(0.001)) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700177 AOS_LOG(INFO, "Arm close at %f\n",
178 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700179
180 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
181
182 set_open_claw(true);
183 SendSuperstructureGoal();
184
185 ::std::this_thread::sleep_for(chrono::milliseconds(1500));
186 set_arm_goal_position(arm::NeutralIndex());
187 SendSuperstructureGoal();
188 if (ShouldCancel()) return true;
189 set_max_drivetrain_voltage(12.0);
190 StartDrive(-0.5, 0.0, kDrive, kTurn);
191 if (!WaitForDriveProfileDone()) return true;
192 } else {
193 // Start on the left. Hit the switch.
194 constexpr double kFullDriveLength = 5.55;
195 constexpr double kTurnDistance = 0.35;
196 StartDrive(-kFullDriveLength, 0.0, kFarSwitchTurnDrive, kTurn);
197
198 if (!WaitForDriveProfileNear(kFullDriveLength - kTurnDistance)) return true;
199 StartDrive(0.0, turn_scalar * (-M_PI / 2.0), kFarSwitchTurnDrive,
200 kSweepingTurn);
201 if (!WaitForTurnProfileDone()) return true;
202 StartDrive(0.0, 0.0, kDrive, kTurn);
203 if (!WaitForDriveProfileDone()) return true;
204
205 // Now, close so let's move the arm up.
206 set_arm_goal_position(arm::FrontSwitchIndex());
207 SendSuperstructureGoal();
208
209 StartDrive(0.0, turn_scalar * (-M_PI / 2.0), kDrive, kFastTurn);
210 if (!WaitForTurnProfileDone()) return true;
211
212 set_max_drivetrain_voltage(10.0);
213 StartDrive(1.1, 0.0, kDrive, kTurn);
214 if (!WaitForArmTrajectoryClose(0.001)) return true;
215 if (!WaitForDriveNear(0.6, M_PI / 2.0)) return true;
216 StartDrive(0.0, 0.0, kFinalFrontFarSwitchDrive, kTurn);
217
218 if (!WaitForDriveNear(0.3, M_PI / 2.0)) return true;
219 set_max_drivetrain_voltage(6.0);
220 StartDrive(0.0, 0.0, kFinalFrontFarSwitchDrive, kTurn);
221
222 // if (!WaitForDriveNear(0.2, M_PI / 2.0)) return true;
223 ::std::this_thread::sleep_for(chrono::milliseconds(300));
224
225 set_open_claw(true);
226 SendSuperstructureGoal();
227
228 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
229 set_arm_goal_position(arm::NeutralIndex());
230 SendSuperstructureGoal();
231 if (ShouldCancel()) return true;
232 set_max_drivetrain_voltage(12.0);
233 StartDrive(-0.5, 0.0, kDrive, kTurn);
234 if (!WaitForDriveProfileDone()) return true;
235 }
236 return false;
237}
238
239bool AutonomousActor::FarScale(monotonic_clock::time_point start_time) {
240 // Start on the left. Hit the switch.
241 constexpr double kFullDriveLength = 11.40;
242 constexpr double kFirstTurnDistance = 4.40;
243 constexpr double kSecondTurnDistance = 9.30;
244 StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn);
245 if (!WaitForDriveProfileNear(kFullDriveLength - (kFirstTurnDistance - 1.4)))
246 return true;
247 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn);
248
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700249 if (!WaitForDriveProfileNear(kFullDriveLength - kFirstTurnDistance))
250 return true;
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700251 StartDrive(0.0, -M_PI / 2.0, kFarSwitchTurnDrive, kSweepingTurn);
252 set_arm_goal_position(arm::BackHighBoxIndex());
253 SendSuperstructureGoal();
254 if (!WaitForTurnProfileDone()) return true;
255
256 StartDrive(0.0, 0.0, kDrive, kTurn);
257
258 if (!WaitForDriveProfileNear(kFullDriveLength - (kSecondTurnDistance - 1.4)))
259 return true;
260
261 StartDrive(0.0, 0.0, kFarScaleFinalTurnDrive, kTurn);
262 if (!WaitForDriveProfileNear(kFullDriveLength - (kSecondTurnDistance)))
263 return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700264 AOS_LOG(INFO, "Final turn at %f\n",
265 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700266
267 StartDrive(0.0, M_PI / 2.0, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
268 if (!WaitForDriveProfileNear(0.15)) return true;
269
270 StartDrive(0.0, 0.3, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
271
Austin Schuhf257f3c2019-10-27 21:00:43 -0700272 AOS_LOG(INFO, "Dropping at %f\n",
273 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700274 set_open_claw(true);
275 SendSuperstructureGoal();
276
277 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700278 AOS_LOG(INFO, "Backing up at %f\n",
279 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700280
281 StartDrive(1.5, -0.55, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
282
283 if (!WaitForDriveProfileNear(1.4)) return true;
284 set_arm_goal_position(arm::NeutralIndex());
285 set_open_claw(false);
286 set_grab_box(true);
287 SendSuperstructureGoal();
288
289 if (!WaitForDriveProfileNear(0.3)) return true;
290
291 set_intake_angle(0.15);
292 set_roller_voltage(10.0);
293 SendSuperstructureGoal();
294
295 set_max_drivetrain_voltage(6.0);
296 StartDrive(0.0, 0.0, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
297
298 if (!WaitForDriveProfileDone()) return true;
299 if (!WaitForTurnProfileDone()) return true;
300
301 StartDrive(0.2, 0.0, kThirdBoxSlowBackup, kThirdBoxSlowTurn);
302
303 if (!WaitForBoxGrabed()) return true;
304 set_arm_goal_position(arm::BackHighBoxIndex());
305 set_intake_angle(-3.0);
306 set_roller_voltage(0.0);
307 set_grab_box(false);
308 SendSuperstructureGoal();
309
310 StartDrive(-1.40, 0.15, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
311
312 if (!WaitForDriveProfileNear(0.1)) return true;
313
314 StartDrive(0.0, 0.5, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
315
316 if (!WaitForDriveProfileDone()) return true;
317 if (!WaitForTurnProfileDone()) return true;
318 ::std::this_thread::sleep_for(chrono::milliseconds(500));
319
Austin Schuhf257f3c2019-10-27 21:00:43 -0700320 AOS_LOG(INFO, "Dropping second box at %f\n",
321 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700322 set_open_claw(true);
323 SendSuperstructureGoal();
324
325 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
326
327 StartDrive(1.4, -0.7, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
328 ::std::this_thread::sleep_for(chrono::milliseconds(200));
329 set_arm_goal_position(arm::NeutralIndex());
330 set_open_claw(false);
331 SendSuperstructureGoal();
332
333 if (!WaitForDriveProfileNear(1.0)) return true;
334
335 StartDrive(0.0, -0.6, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
336 return false;
337}
338
339bool AutonomousActor::FarReadyScale(monotonic_clock::time_point start_time) {
340 // Start on the left. Hit the switch.
341 constexpr double kFullDriveLength = 7.5;
342 constexpr double kFirstTurnDistance = 4.40;
343 StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn);
344 if (!WaitForDriveProfileNear(kFullDriveLength - (kFirstTurnDistance - 1.4)))
345 return true;
346 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn);
347
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700348 if (!WaitForDriveProfileNear(kFullDriveLength - kFirstTurnDistance))
349 return true;
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700350 StartDrive(0.0, -M_PI / 2.0, kFarSwitchTurnDrive, kSweepingTurn);
351 set_arm_goal_position(arm::UpIndex());
352 SendSuperstructureGoal();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700353 AOS_LOG(INFO, "Lifting arm at %f\n",
354 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700355 if (!WaitForTurnProfileDone()) return true;
356
357 StartDrive(0.0, 0.0, kDrive, kTurn);
358 return false;
359}
360
361bool AutonomousActor::DriveStraight() {
362 StartDrive(-3.2, 0.0, kDrive, kTurn);
363 if (!WaitForDriveProfileDone()) return true;
364 return false;
365}
366
367bool AutonomousActor::CloseSwitch(monotonic_clock::time_point start_time,
368 bool left) {
369 const double turn_scalar = left ? 1.0 : -1.0;
370
371 constexpr double kDriveDistance = 3.2;
372 // Start on the left. Drive, arc a turn, and drop in the close switch.
373 StartDrive(-kDriveDistance, 0.0, kDrive, kTurn);
374 if (!WaitForDriveNear(2.5, M_PI / 2.0)) return true;
375
376 // Now, close so let's move the arm up.
377 set_arm_goal_position(arm::BackSwitchIndex());
378 SendSuperstructureGoal();
379
380 StartDrive(0.0, 0.0, kSlowDrive, kSweepingTurn);
381 if (!WaitForDriveNear(1.6, M_PI / 2.0)) return true;
382
383 StartDrive(0.0, turn_scalar * (-M_PI / 4.0 - 0.2), kSlowDrive, kSweepingTurn);
384 if (!WaitForDriveNear(0.2, 0.2)) return true;
385 set_max_drivetrain_voltage(6.0);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700386 AOS_LOG(INFO, "Lowered drivetrain voltage %f\n",
387 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700388 ::std::this_thread::sleep_for(chrono::milliseconds(300));
389
390 set_open_claw(true);
391 SendSuperstructureGoal();
392
393 ::std::this_thread::sleep_for(chrono::milliseconds(500));
394 set_max_drivetrain_voltage(12.0);
395 StartDrive(0.7, 0.0, kDrive, kTurn);
396 if (!WaitForTurnProfileDone()) return true;
397
398 set_arm_goal_position(arm::NeutralIndex());
399 SendSuperstructureGoal();
400 return false;
401}
402
403bool AutonomousActor::ThreeCubeAuto(monotonic_clock::time_point start_time) {
404 // Start on the left. Hit the scale.
405 constexpr double kDriveDistance = 6.95;
406 // Distance and angle to do the big drive to the third cube.
407 constexpr double kThirdCubeDrive = 1.57 + 0.05 + 0.05;
408 constexpr double kThirdCubeTurn = M_PI / 4.0 + 0.1;
409 // Angle to do the slow pickup turn on the third box.
410 constexpr double kThirdBoxEndTurnAngle = 0.30;
411
412 // Distance to drive back to the scale with the third cube.
413 constexpr double kThirdCubeDropDrive = kThirdCubeDrive + 0.40;
414
415 // Drive.
416 StartDrive(-kDriveDistance, 0.0, kDrive, kTurn);
417 if (!WaitForDriveNear(kDriveDistance - 1.0, M_PI / 2.0)) return true;
418 // Once we are away from the wall, start the arm.
419 set_arm_goal_position(arm::BackMiddle2BoxIndex());
420 SendSuperstructureGoal();
421
422 // We are starting to get close. Slow down for the turn.
423 if (!WaitForDriveNear(4.0, M_PI / 2.0)) return true;
424 StartDrive(0.0, 0.0, kScaleTurnDrive, kFastTurn);
425
426 // Once we've gotten slowed down a bit, start turning.
427 if (!WaitForDriveNear(3.25, M_PI / 2.0)) return true;
428 StartDrive(0.0, -M_PI / 6.0, kScaleTurnDrive, kFastTurn);
429
430 if (!WaitForDriveNear(1.0, M_PI / 2.0)) return true;
431 StartDrive(0.0, M_PI / 6.0, kScaleTurnDrive, kFastTurn);
432
433 // Get close and open the claw.
434 if (!WaitForDriveNear(0.15, 0.25)) return true;
435 set_open_claw(true);
436 SendSuperstructureGoal();
437 set_intake_angle(-0.60);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700438 AOS_LOG(INFO, "Dropped first box %f\n",
439 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700440
441 ::std::this_thread::sleep_for(chrono::milliseconds(700));
442
443 set_grab_box(true);
444 SendSuperstructureGoal();
445
Austin Schuhf257f3c2019-10-27 21:00:43 -0700446 AOS_LOG(INFO, "Starting second box drive %f\n",
447 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700448 constexpr double kSecondBoxSwerveAngle = 0.35;
449 constexpr double kSecondBoxDrive = 1.38;
450 StartDrive(kSecondBoxDrive, 0.0, kDrive, kFastTurn);
451 if (!WaitForDriveNear(kSecondBoxDrive - 0.2, M_PI / 2.0)) return true;
452
453 StartDrive(0.0, kSecondBoxSwerveAngle, kDrive, kFastTurn);
454 if (!WaitForDriveNear(0.5, M_PI / 2.0)) return true;
455
456 set_open_claw(true);
457 set_disable_box_correct(false);
458 SendSuperstructureGoal();
459
460 StartDrive(0.0, -kSecondBoxSwerveAngle, kDrive, kFastTurn);
461
462 if (!WaitForDriveProfileDone()) return true;
463 set_max_drivetrain_voltage(6.0);
464 StartDrive(0.0, 0.0, kDrive, kFastTurn);
465
466 set_intake_angle(0.15);
467 set_arm_goal_position(arm::BackHighBoxIndex());
468 set_open_claw(false);
469
470 set_roller_voltage(10.0);
471 SendSuperstructureGoal();
472
Austin Schuhf257f3c2019-10-27 21:00:43 -0700473 AOS_LOG(INFO, "Grabbing second box %f\n",
474 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700475 ::std::this_thread::sleep_for(chrono::milliseconds(200));
476 StartDrive(-0.04, 0.0, kThirdBoxSlowBackup, kThirdBoxSlowTurn);
477
478 if (!WaitForBoxGrabed()) return true;
479 set_max_drivetrain_voltage(12.0);
480
Austin Schuhf257f3c2019-10-27 21:00:43 -0700481 AOS_LOG(INFO, "Got second box %f\n",
482 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700483 ::std::this_thread::sleep_for(chrono::milliseconds(500));
484
485 set_grab_box(false);
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700486 // set_arm_goal_position(arm::UpIndex());
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700487 set_arm_goal_position(arm::BackHighBoxIndex());
488 set_roller_voltage(0.0);
489 set_disable_box_correct(false);
490 SendSuperstructureGoal();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700491 AOS_LOG(INFO, "Driving to place second box %f\n",
492 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700493
494 StartDrive(-kSecondBoxDrive + 0.16, kSecondBoxSwerveAngle, kDrive, kFastTurn);
495 if (!WaitForDriveNear(0.4, M_PI / 2.0)) return true;
496
497 constexpr double kSecondBoxEndExtraAngle = 0.3;
498 StartDrive(0.0, -kSecondBoxSwerveAngle - kSecondBoxEndExtraAngle, kDrive,
499 kFastTurn);
500
Austin Schuhf257f3c2019-10-27 21:00:43 -0700501 AOS_LOG(INFO, "Starting throw %f\n",
502 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700503 if (!WaitForDriveNear(0.4, M_PI / 2.0)) return true;
504 if (!WaitForArmTrajectoryClose(0.25)) return true;
505 SendSuperstructureGoal();
506
507 // Throw the box.
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700508 // if (!WaitForArmTrajectoryClose(0.20)) return true;
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700509 if (!WaitForDriveNear(0.2, M_PI / 2.0)) return true;
510
511 set_open_claw(true);
512 set_intake_angle(-M_PI / 4.0);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700513 AOS_LOG(INFO, "Releasing second box %f\n",
514 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700515 SendSuperstructureGoal();
516
517 ::std::this_thread::sleep_for(chrono::milliseconds(700));
518 set_open_claw(false);
519 SendSuperstructureGoal();
520
Austin Schuhf257f3c2019-10-27 21:00:43 -0700521 AOS_LOG(INFO, "Driving to third box %f\n",
522 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700523 StartDrive(kThirdCubeDrive, kSecondBoxEndExtraAngle, kThirdBoxDrive,
524 kFastTurn);
525 if (!WaitForDriveNear(kThirdCubeDrive - 0.1, M_PI / 4.0)) return true;
526 set_grab_box(true);
527 SendSuperstructureGoal();
528
529 StartDrive(0.0, kThirdCubeTurn, kThirdBoxDrive, kFastTurn);
530 if (!WaitForDriveNear(0.05, M_PI / 4.0)) return true;
531
532 set_intake_angle(0.05);
533 set_roller_voltage(9.0);
534 SendSuperstructureGoal();
535
536 if (!WaitForDriveProfileDone()) return true;
537 if (!WaitForTurnProfileDone()) return true;
538 StartDrive(0.35, kThirdBoxEndTurnAngle, kThirdBoxSlowBackup,
539 kThirdBoxSlowTurn);
540 if (!WaitForDriveProfileDone()) return true;
541
Austin Schuhf257f3c2019-10-27 21:00:43 -0700542 AOS_LOG(INFO, "Waiting for third box %f\n",
543 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700544 if (!WaitForBoxGrabed()) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700545 AOS_LOG(INFO, "Third box grabbed %f\n",
546 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700547 const bool too_late =
Austin Schuh77ed5432019-07-07 20:41:36 -0700548 monotonic_now() > start_time + chrono::milliseconds(12500);
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700549 if (too_late) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700550 AOS_LOG(INFO, "Third box too long, going up. %f\n",
551 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700552 set_grab_box(false);
553 set_arm_goal_position(arm::UpIndex());
554 set_roller_voltage(0.0);
555 SendSuperstructureGoal();
556 }
557 ::std::this_thread::sleep_for(chrono::milliseconds(400));
558
559 set_grab_box(false);
560 if (!too_late) {
561 set_arm_goal_position(arm::BackMiddle2BoxIndex());
562 }
563 set_roller_voltage(0.0);
564 SendSuperstructureGoal();
565
566 StartDrive(-kThirdCubeDropDrive, 0.0, kThirdBoxPlaceDrive, kReallyFastTurn);
567
568 if (!WaitForDriveNear(kThirdCubeDropDrive - 0.23, M_PI / 4.0)) return true;
569 StartDrive(0.0, -kThirdCubeTurn - 0.2 - kThirdBoxEndTurnAngle - 0.3,
570 kThirdBoxPlaceDrive, kReallyFastTurn);
571
572 if (!WaitForDriveNear(0.30, M_PI / 4.0 + 0.2)) return true;
573
574 if (!too_late) {
575 set_open_claw(true);
576 set_intake_angle(-M_PI / 4.0);
577 set_roller_voltage(0.0);
578 SendSuperstructureGoal();
579
Austin Schuhf257f3c2019-10-27 21:00:43 -0700580 AOS_LOG(INFO, "Final open %f\n",
581 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700582 }
583
584 if (!WaitForDriveProfileDone()) return true;
585 if (!WaitForTurnProfileDone()) return true;
586
587 ::std::this_thread::sleep_for(chrono::milliseconds(14750) -
Austin Schuh77ed5432019-07-07 20:41:36 -0700588 (monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700589
590 set_arm_goal_position(arm::UpIndex());
591 SendSuperstructureGoal();
592
593 ::std::this_thread::sleep_for(chrono::milliseconds(15000) -
Austin Schuh77ed5432019-07-07 20:41:36 -0700594 (monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700595
596 set_close_claw(true);
597 SendSuperstructureGoal();
598 return false;
599}
600
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800601} // namespace y2018::actors