blob: 89082dfa77fcde4696525d044330169ec15d309c [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
Austin Schuha3c148e2018-03-09 21:04:05 -080011#include "y2018/control_loops/drivetrain/drivetrain_base.h"
12
13namespace y2018 {
14namespace actors {
Alex Perrycb7da4b2019-08-28 19:35:56 -070015using ::frc971::ProfileParametersT;
Austin Schuha3c148e2018-03-09 21:04:05 -080016
Austin Schuha3c148e2018-03-09 21:04:05 -080017using ::aos::monotonic_clock;
18namespace chrono = ::std::chrono;
19namespace this_thread = ::std::this_thread;
20
21namespace {
22
Austin Schuhbd0a40f2019-06-30 14:56:31 -070023namespace arm = ::y2018::control_loops::superstructure::arm;
24
Alex Perrycb7da4b2019-08-28 19:35:56 -070025ProfileParametersT MakeProfileParameters(float max_velocity,
26 float max_acceleration) {
27 ProfileParametersT result;
28 result.max_velocity = max_velocity;
29 result.max_acceleration = max_acceleration;
30 return result;
31}
Austin Schuhf79c0e52018-04-04 20:13:21 -070032
Alex Perrycb7da4b2019-08-28 19:35:56 -070033const ProfileParametersT kFinalSwitchDrive = MakeProfileParameters(0.5, 1.5);
34const ProfileParametersT kDrive = MakeProfileParameters(5.0, 2.5);
35const ProfileParametersT kThirdBoxDrive = MakeProfileParameters(3.0, 2.5);
36const ProfileParametersT kSlowDrive = MakeProfileParameters(1.5, 2.5);
37const ProfileParametersT kScaleTurnDrive = MakeProfileParameters(3.0, 2.5);
38const ProfileParametersT kFarSwitchTurnDrive = MakeProfileParameters(2.0, 2.5);
39const ProfileParametersT kFarScaleFinalTurnDrive = kFarSwitchTurnDrive;
40const ProfileParametersT kTurn = MakeProfileParameters(4.0, 2.0);
41const ProfileParametersT kSweepingTurn = MakeProfileParameters(5.0, 7.0);
42const ProfileParametersT kFarScaleSweepingTurn = kSweepingTurn;
43const ProfileParametersT kFastTurn = MakeProfileParameters(5.0, 7.0);
44const ProfileParametersT kReallyFastTurn = MakeProfileParameters(1.5, 9.0);
Austin Schuhf79c0e52018-04-04 20:13:21 -070045
Alex Perrycb7da4b2019-08-28 19:35:56 -070046const ProfileParametersT kThirdBoxSlowBackup = MakeProfileParameters(0.35, 1.5);
47const ProfileParametersT kThirdBoxSlowTurn = MakeProfileParameters(1.5, 4.0);
Austin Schuhf79c0e52018-04-04 20:13:21 -070048
Alex Perrycb7da4b2019-08-28 19:35:56 -070049const ProfileParametersT kThirdBoxPlaceDrive = MakeProfileParameters(4.0, 2.0);
50
51const ProfileParametersT kFinalFrontFarSwitchDrive =
52 MakeProfileParameters(2.0, 2.0);
Austin Schuha3c148e2018-03-09 21:04:05 -080053
54} // namespace
55
Austin Schuh1bf8a212019-05-26 22:13:14 -070056AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
Austin Schuha3c148e2018-03-09 21:04:05 -080057 : frc971::autonomous::BaseAutonomousActor(
Austin Schuhd845c972019-06-29 21:20:05 -070058 event_loop, control_loops::drivetrain::GetDrivetrainConfig()),
59 superstructure_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070060 event_loop->MakeSender<::y2018::control_loops::superstructure::Goal>(
61 "/superstructure")),
Austin Schuhd845c972019-06-29 21:20:05 -070062 superstructure_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070063 event_loop
64 ->MakeFetcher<::y2018::control_loops::superstructure::Status>(
65 "/superstructure")) {}
Austin Schuha3c148e2018-03-09 21:04:05 -080066
67bool AutonomousActor::RunAction(
Alex Perrycb7da4b2019-08-28 19:35:56 -070068 const ::frc971::autonomous::AutonomousActionParams *params) {
Austin Schuh77ed5432019-07-07 20:41:36 -070069 const monotonic_clock::time_point start_time = monotonic_now();
Austin Schuhf257f3c2019-10-27 21:00:43 -070070 AOS_LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -070071 params->mode());
Austin Schuha3c148e2018-03-09 21:04:05 -080072 Reset();
73
Austin Schuh4dad8fb2018-07-08 16:00:13 -070074 // Switch
75 /*
Alex Perrycb7da4b2019-08-28 19:35:56 -070076 switch (params->mode()) {
Austin Schuh4dad8fb2018-07-08 16:00:13 -070077 case 0:
78 case 2: {
79 if (FarSwitch(start_time, true)) return true;
80 } break;
81
82 case 3:
Austin Schuhc231df42018-03-21 20:43:24 -070083 case 1: {
Austin Schuh4dad8fb2018-07-08 16:00:13 -070084 if (CloseSwitch(start_time)) return true;
Austin Schuhc231df42018-03-21 20:43:24 -070085 } break;
Austin Schuh4dad8fb2018-07-08 16:00:13 -070086 }
87 */
88 // Scale
Alex Perrycb7da4b2019-08-28 19:35:56 -070089 switch (params->mode()) {
Austin Schuh4dad8fb2018-07-08 16:00:13 -070090 case 0:
91 case 1: {
92 if (FarScale(start_time)) return true;
Austin Schuhc231df42018-03-21 20:43:24 -070093 } break;
94
95 case 3:
96 case 2: {
Austin Schuh4dad8fb2018-07-08 16:00:13 -070097 if (ThreeCubeAuto(start_time)) return true;
Austin Schuhc231df42018-03-21 20:43:24 -070098 } break;
Austin Schuha3c148e2018-03-09 21:04:05 -080099 }
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700100 /*
Alex Perrycb7da4b2019-08-28 19:35:56 -0700101 switch (params->mode()) {
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700102 case 1: {
103 if (FarScale(start_time)) return true;
104 //if (CloseSwitch(start_time)) return true;
105 } break;
106 case 0: {
107 if (DriveStraight()) return true;
108 } break;
109 case 200: {
110 if (FarSwitch(start_time)) return true;
111 } break;
112
113 case 3:
114 case 2: {
115 if (ThreeCubeAuto(start_time)) {
116 return true;
117 }
118 } break;
119 }
120 */
Austin Schuha3c148e2018-03-09 21:04:05 -0800121
Austin Schuhf257f3c2019-10-27 21:00:43 -0700122 AOS_LOG(INFO, "Done %f\n",
123 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuha3c148e2018-03-09 21:04:05 -0800124
125 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700126 event_loop()->monotonic_now(),
Austin Schuha3c148e2018-03-09 21:04:05 -0800127 ::std::chrono::milliseconds(5) / 2);
128
129 while (!ShouldCancel()) {
130 phased_loop.SleepUntilNext();
131 }
Austin Schuhf257f3c2019-10-27 21:00:43 -0700132 AOS_LOG(DEBUG, "Done running\n");
Austin Schuha3c148e2018-03-09 21:04:05 -0800133
134 return true;
135}
136
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700137bool AutonomousActor::FarSwitch(monotonic_clock::time_point start_time,
138 bool drive_behind, bool left) {
139 const double turn_scalar = left ? 1.0 : -1.0;
140 if (drive_behind) {
141 // Start on the left. Hit the switch.
142 constexpr double kFullDriveLength = 9.93;
143 constexpr double kTurnDistance = 4.40;
144 StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn);
145 set_arm_goal_position(arm::NeutralIndex());
146 set_grab_box(false);
147 SendSuperstructureGoal();
148 if (!WaitForDriveProfileNear(kFullDriveLength - (kTurnDistance - 1.4)))
149 return true;
150 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn);
151
152 if (!WaitForDriveProfileNear(kFullDriveLength - kTurnDistance)) return true;
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700153 StartDrive(0.0, turn_scalar * (-M_PI / 2.0), kFarSwitchTurnDrive,
154 kSweepingTurn);
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700155 if (!WaitForTurnProfileDone()) return true;
156
157 // Now, close so let's move the arm up.
158 set_arm_goal_position(arm::FrontSwitchAutoIndex());
159 SendSuperstructureGoal();
160
161 StartDrive(0.0, 0.0, kDrive, kTurn);
162 if (!WaitForDriveProfileNear(2.0)) return true;
163 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kFastTurn);
164
165 if (!WaitForDriveProfileNear(1.3)) return true;
166
167 StartDrive(0.0, turn_scalar * M_PI / 2.0, kFarSwitchTurnDrive, kFastTurn);
168 if (!WaitForTurnProfileDone()) return true;
169
170 constexpr double kGentlePushDrive = 0.70;
171 StartDrive(kGentlePushDrive, 0.0, kSlowDrive, kTurn);
172
173 if (!WaitForDriveProfileNear(kGentlePushDrive - 0.25)) return true;
174 // Turn down the peak voltage when we push against the wall so we don't blow
175 // breakers or pull the battery voltage down too far.
176 set_max_drivetrain_voltage(6.0);
177 StartDrive(0.00, 0.0, kFinalSwitchDrive, kTurn);
178
179 if (!WaitForArmTrajectoryClose(0.001)) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700180 AOS_LOG(INFO, "Arm close at %f\n",
181 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700182
183 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
184
185 set_open_claw(true);
186 SendSuperstructureGoal();
187
188 ::std::this_thread::sleep_for(chrono::milliseconds(1500));
189 set_arm_goal_position(arm::NeutralIndex());
190 SendSuperstructureGoal();
191 if (ShouldCancel()) return true;
192 set_max_drivetrain_voltage(12.0);
193 StartDrive(-0.5, 0.0, kDrive, kTurn);
194 if (!WaitForDriveProfileDone()) return true;
195 } else {
196 // Start on the left. Hit the switch.
197 constexpr double kFullDriveLength = 5.55;
198 constexpr double kTurnDistance = 0.35;
199 StartDrive(-kFullDriveLength, 0.0, kFarSwitchTurnDrive, kTurn);
200
201 if (!WaitForDriveProfileNear(kFullDriveLength - kTurnDistance)) return true;
202 StartDrive(0.0, turn_scalar * (-M_PI / 2.0), kFarSwitchTurnDrive,
203 kSweepingTurn);
204 if (!WaitForTurnProfileDone()) return true;
205 StartDrive(0.0, 0.0, kDrive, kTurn);
206 if (!WaitForDriveProfileDone()) return true;
207
208 // Now, close so let's move the arm up.
209 set_arm_goal_position(arm::FrontSwitchIndex());
210 SendSuperstructureGoal();
211
212 StartDrive(0.0, turn_scalar * (-M_PI / 2.0), kDrive, kFastTurn);
213 if (!WaitForTurnProfileDone()) return true;
214
215 set_max_drivetrain_voltage(10.0);
216 StartDrive(1.1, 0.0, kDrive, kTurn);
217 if (!WaitForArmTrajectoryClose(0.001)) return true;
218 if (!WaitForDriveNear(0.6, M_PI / 2.0)) return true;
219 StartDrive(0.0, 0.0, kFinalFrontFarSwitchDrive, kTurn);
220
221 if (!WaitForDriveNear(0.3, M_PI / 2.0)) return true;
222 set_max_drivetrain_voltage(6.0);
223 StartDrive(0.0, 0.0, kFinalFrontFarSwitchDrive, kTurn);
224
225 // if (!WaitForDriveNear(0.2, M_PI / 2.0)) return true;
226 ::std::this_thread::sleep_for(chrono::milliseconds(300));
227
228 set_open_claw(true);
229 SendSuperstructureGoal();
230
231 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
232 set_arm_goal_position(arm::NeutralIndex());
233 SendSuperstructureGoal();
234 if (ShouldCancel()) return true;
235 set_max_drivetrain_voltage(12.0);
236 StartDrive(-0.5, 0.0, kDrive, kTurn);
237 if (!WaitForDriveProfileDone()) return true;
238 }
239 return false;
240}
241
242bool AutonomousActor::FarScale(monotonic_clock::time_point start_time) {
243 // Start on the left. Hit the switch.
244 constexpr double kFullDriveLength = 11.40;
245 constexpr double kFirstTurnDistance = 4.40;
246 constexpr double kSecondTurnDistance = 9.30;
247 StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn);
248 if (!WaitForDriveProfileNear(kFullDriveLength - (kFirstTurnDistance - 1.4)))
249 return true;
250 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn);
251
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700252 if (!WaitForDriveProfileNear(kFullDriveLength - kFirstTurnDistance))
253 return true;
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700254 StartDrive(0.0, -M_PI / 2.0, kFarSwitchTurnDrive, kSweepingTurn);
255 set_arm_goal_position(arm::BackHighBoxIndex());
256 SendSuperstructureGoal();
257 if (!WaitForTurnProfileDone()) return true;
258
259 StartDrive(0.0, 0.0, kDrive, kTurn);
260
261 if (!WaitForDriveProfileNear(kFullDriveLength - (kSecondTurnDistance - 1.4)))
262 return true;
263
264 StartDrive(0.0, 0.0, kFarScaleFinalTurnDrive, kTurn);
265 if (!WaitForDriveProfileNear(kFullDriveLength - (kSecondTurnDistance)))
266 return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700267 AOS_LOG(INFO, "Final turn at %f\n",
268 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700269
270 StartDrive(0.0, M_PI / 2.0, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
271 if (!WaitForDriveProfileNear(0.15)) return true;
272
273 StartDrive(0.0, 0.3, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
274
Austin Schuhf257f3c2019-10-27 21:00:43 -0700275 AOS_LOG(INFO, "Dropping at %f\n",
276 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700277 set_open_claw(true);
278 SendSuperstructureGoal();
279
280 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700281 AOS_LOG(INFO, "Backing up at %f\n",
282 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700283
284 StartDrive(1.5, -0.55, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
285
286 if (!WaitForDriveProfileNear(1.4)) return true;
287 set_arm_goal_position(arm::NeutralIndex());
288 set_open_claw(false);
289 set_grab_box(true);
290 SendSuperstructureGoal();
291
292 if (!WaitForDriveProfileNear(0.3)) return true;
293
294 set_intake_angle(0.15);
295 set_roller_voltage(10.0);
296 SendSuperstructureGoal();
297
298 set_max_drivetrain_voltage(6.0);
299 StartDrive(0.0, 0.0, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn);
300
301 if (!WaitForDriveProfileDone()) return true;
302 if (!WaitForTurnProfileDone()) return true;
303
304 StartDrive(0.2, 0.0, kThirdBoxSlowBackup, kThirdBoxSlowTurn);
305
306 if (!WaitForBoxGrabed()) return true;
307 set_arm_goal_position(arm::BackHighBoxIndex());
308 set_intake_angle(-3.0);
309 set_roller_voltage(0.0);
310 set_grab_box(false);
311 SendSuperstructureGoal();
312
313 StartDrive(-1.40, 0.15, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
314
315 if (!WaitForDriveProfileNear(0.1)) return true;
316
317 StartDrive(0.0, 0.5, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
318
319 if (!WaitForDriveProfileDone()) return true;
320 if (!WaitForTurnProfileDone()) return true;
321 ::std::this_thread::sleep_for(chrono::milliseconds(500));
322
Austin Schuhf257f3c2019-10-27 21:00:43 -0700323 AOS_LOG(INFO, "Dropping second box at %f\n",
324 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700325 set_open_claw(true);
326 SendSuperstructureGoal();
327
328 ::std::this_thread::sleep_for(chrono::milliseconds(1000));
329
330 StartDrive(1.4, -0.7, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
331 ::std::this_thread::sleep_for(chrono::milliseconds(200));
332 set_arm_goal_position(arm::NeutralIndex());
333 set_open_claw(false);
334 SendSuperstructureGoal();
335
336 if (!WaitForDriveProfileNear(1.0)) return true;
337
338 StartDrive(0.0, -0.6, kThirdBoxPlaceDrive, kFarScaleSweepingTurn);
339 return false;
340}
341
342bool AutonomousActor::FarReadyScale(monotonic_clock::time_point start_time) {
343 // Start on the left. Hit the switch.
344 constexpr double kFullDriveLength = 7.5;
345 constexpr double kFirstTurnDistance = 4.40;
346 StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn);
347 if (!WaitForDriveProfileNear(kFullDriveLength - (kFirstTurnDistance - 1.4)))
348 return true;
349 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn);
350
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700351 if (!WaitForDriveProfileNear(kFullDriveLength - kFirstTurnDistance))
352 return true;
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700353 StartDrive(0.0, -M_PI / 2.0, kFarSwitchTurnDrive, kSweepingTurn);
354 set_arm_goal_position(arm::UpIndex());
355 SendSuperstructureGoal();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700356 AOS_LOG(INFO, "Lifting arm at %f\n",
357 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700358 if (!WaitForTurnProfileDone()) return true;
359
360 StartDrive(0.0, 0.0, kDrive, kTurn);
361 return false;
362}
363
364bool AutonomousActor::DriveStraight() {
365 StartDrive(-3.2, 0.0, kDrive, kTurn);
366 if (!WaitForDriveProfileDone()) return true;
367 return false;
368}
369
370bool AutonomousActor::CloseSwitch(monotonic_clock::time_point start_time,
371 bool left) {
372 const double turn_scalar = left ? 1.0 : -1.0;
373
374 constexpr double kDriveDistance = 3.2;
375 // Start on the left. Drive, arc a turn, and drop in the close switch.
376 StartDrive(-kDriveDistance, 0.0, kDrive, kTurn);
377 if (!WaitForDriveNear(2.5, M_PI / 2.0)) return true;
378
379 // Now, close so let's move the arm up.
380 set_arm_goal_position(arm::BackSwitchIndex());
381 SendSuperstructureGoal();
382
383 StartDrive(0.0, 0.0, kSlowDrive, kSweepingTurn);
384 if (!WaitForDriveNear(1.6, M_PI / 2.0)) return true;
385
386 StartDrive(0.0, turn_scalar * (-M_PI / 4.0 - 0.2), kSlowDrive, kSweepingTurn);
387 if (!WaitForDriveNear(0.2, 0.2)) return true;
388 set_max_drivetrain_voltage(6.0);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700389 AOS_LOG(INFO, "Lowered drivetrain voltage %f\n",
390 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700391 ::std::this_thread::sleep_for(chrono::milliseconds(300));
392
393 set_open_claw(true);
394 SendSuperstructureGoal();
395
396 ::std::this_thread::sleep_for(chrono::milliseconds(500));
397 set_max_drivetrain_voltage(12.0);
398 StartDrive(0.7, 0.0, kDrive, kTurn);
399 if (!WaitForTurnProfileDone()) return true;
400
401 set_arm_goal_position(arm::NeutralIndex());
402 SendSuperstructureGoal();
403 return false;
404}
405
406bool AutonomousActor::ThreeCubeAuto(monotonic_clock::time_point start_time) {
407 // Start on the left. Hit the scale.
408 constexpr double kDriveDistance = 6.95;
409 // Distance and angle to do the big drive to the third cube.
410 constexpr double kThirdCubeDrive = 1.57 + 0.05 + 0.05;
411 constexpr double kThirdCubeTurn = M_PI / 4.0 + 0.1;
412 // Angle to do the slow pickup turn on the third box.
413 constexpr double kThirdBoxEndTurnAngle = 0.30;
414
415 // Distance to drive back to the scale with the third cube.
416 constexpr double kThirdCubeDropDrive = kThirdCubeDrive + 0.40;
417
418 // Drive.
419 StartDrive(-kDriveDistance, 0.0, kDrive, kTurn);
420 if (!WaitForDriveNear(kDriveDistance - 1.0, M_PI / 2.0)) return true;
421 // Once we are away from the wall, start the arm.
422 set_arm_goal_position(arm::BackMiddle2BoxIndex());
423 SendSuperstructureGoal();
424
425 // We are starting to get close. Slow down for the turn.
426 if (!WaitForDriveNear(4.0, M_PI / 2.0)) return true;
427 StartDrive(0.0, 0.0, kScaleTurnDrive, kFastTurn);
428
429 // Once we've gotten slowed down a bit, start turning.
430 if (!WaitForDriveNear(3.25, M_PI / 2.0)) return true;
431 StartDrive(0.0, -M_PI / 6.0, kScaleTurnDrive, kFastTurn);
432
433 if (!WaitForDriveNear(1.0, M_PI / 2.0)) return true;
434 StartDrive(0.0, M_PI / 6.0, kScaleTurnDrive, kFastTurn);
435
436 // Get close and open the claw.
437 if (!WaitForDriveNear(0.15, 0.25)) return true;
438 set_open_claw(true);
439 SendSuperstructureGoal();
440 set_intake_angle(-0.60);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700441 AOS_LOG(INFO, "Dropped first box %f\n",
442 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700443
444 ::std::this_thread::sleep_for(chrono::milliseconds(700));
445
446 set_grab_box(true);
447 SendSuperstructureGoal();
448
Austin Schuhf257f3c2019-10-27 21:00:43 -0700449 AOS_LOG(INFO, "Starting second box drive %f\n",
450 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700451 constexpr double kSecondBoxSwerveAngle = 0.35;
452 constexpr double kSecondBoxDrive = 1.38;
453 StartDrive(kSecondBoxDrive, 0.0, kDrive, kFastTurn);
454 if (!WaitForDriveNear(kSecondBoxDrive - 0.2, M_PI / 2.0)) return true;
455
456 StartDrive(0.0, kSecondBoxSwerveAngle, kDrive, kFastTurn);
457 if (!WaitForDriveNear(0.5, M_PI / 2.0)) return true;
458
459 set_open_claw(true);
460 set_disable_box_correct(false);
461 SendSuperstructureGoal();
462
463 StartDrive(0.0, -kSecondBoxSwerveAngle, kDrive, kFastTurn);
464
465 if (!WaitForDriveProfileDone()) return true;
466 set_max_drivetrain_voltage(6.0);
467 StartDrive(0.0, 0.0, kDrive, kFastTurn);
468
469 set_intake_angle(0.15);
470 set_arm_goal_position(arm::BackHighBoxIndex());
471 set_open_claw(false);
472
473 set_roller_voltage(10.0);
474 SendSuperstructureGoal();
475
Austin Schuhf257f3c2019-10-27 21:00:43 -0700476 AOS_LOG(INFO, "Grabbing second box %f\n",
477 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700478 ::std::this_thread::sleep_for(chrono::milliseconds(200));
479 StartDrive(-0.04, 0.0, kThirdBoxSlowBackup, kThirdBoxSlowTurn);
480
481 if (!WaitForBoxGrabed()) return true;
482 set_max_drivetrain_voltage(12.0);
483
Austin Schuhf257f3c2019-10-27 21:00:43 -0700484 AOS_LOG(INFO, "Got second box %f\n",
485 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700486 ::std::this_thread::sleep_for(chrono::milliseconds(500));
487
488 set_grab_box(false);
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700489 // set_arm_goal_position(arm::UpIndex());
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700490 set_arm_goal_position(arm::BackHighBoxIndex());
491 set_roller_voltage(0.0);
492 set_disable_box_correct(false);
493 SendSuperstructureGoal();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700494 AOS_LOG(INFO, "Driving to place second box %f\n",
495 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700496
497 StartDrive(-kSecondBoxDrive + 0.16, kSecondBoxSwerveAngle, kDrive, kFastTurn);
498 if (!WaitForDriveNear(0.4, M_PI / 2.0)) return true;
499
500 constexpr double kSecondBoxEndExtraAngle = 0.3;
501 StartDrive(0.0, -kSecondBoxSwerveAngle - kSecondBoxEndExtraAngle, kDrive,
502 kFastTurn);
503
Austin Schuhf257f3c2019-10-27 21:00:43 -0700504 AOS_LOG(INFO, "Starting throw %f\n",
505 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700506 if (!WaitForDriveNear(0.4, M_PI / 2.0)) return true;
507 if (!WaitForArmTrajectoryClose(0.25)) return true;
508 SendSuperstructureGoal();
509
510 // Throw the box.
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700511 // if (!WaitForArmTrajectoryClose(0.20)) return true;
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700512 if (!WaitForDriveNear(0.2, M_PI / 2.0)) return true;
513
514 set_open_claw(true);
515 set_intake_angle(-M_PI / 4.0);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700516 AOS_LOG(INFO, "Releasing second box %f\n",
517 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700518 SendSuperstructureGoal();
519
520 ::std::this_thread::sleep_for(chrono::milliseconds(700));
521 set_open_claw(false);
522 SendSuperstructureGoal();
523
Austin Schuhf257f3c2019-10-27 21:00:43 -0700524 AOS_LOG(INFO, "Driving to third box %f\n",
525 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700526 StartDrive(kThirdCubeDrive, kSecondBoxEndExtraAngle, kThirdBoxDrive,
527 kFastTurn);
528 if (!WaitForDriveNear(kThirdCubeDrive - 0.1, M_PI / 4.0)) return true;
529 set_grab_box(true);
530 SendSuperstructureGoal();
531
532 StartDrive(0.0, kThirdCubeTurn, kThirdBoxDrive, kFastTurn);
533 if (!WaitForDriveNear(0.05, M_PI / 4.0)) return true;
534
535 set_intake_angle(0.05);
536 set_roller_voltage(9.0);
537 SendSuperstructureGoal();
538
539 if (!WaitForDriveProfileDone()) return true;
540 if (!WaitForTurnProfileDone()) return true;
541 StartDrive(0.35, kThirdBoxEndTurnAngle, kThirdBoxSlowBackup,
542 kThirdBoxSlowTurn);
543 if (!WaitForDriveProfileDone()) return true;
544
Austin Schuhf257f3c2019-10-27 21:00:43 -0700545 AOS_LOG(INFO, "Waiting for third box %f\n",
546 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700547 if (!WaitForBoxGrabed()) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700548 AOS_LOG(INFO, "Third box grabbed %f\n",
549 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700550 const bool too_late =
Austin Schuh77ed5432019-07-07 20:41:36 -0700551 monotonic_now() > start_time + chrono::milliseconds(12500);
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700552 if (too_late) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700553 AOS_LOG(INFO, "Third box too long, going up. %f\n",
554 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700555 set_grab_box(false);
556 set_arm_goal_position(arm::UpIndex());
557 set_roller_voltage(0.0);
558 SendSuperstructureGoal();
559 }
560 ::std::this_thread::sleep_for(chrono::milliseconds(400));
561
562 set_grab_box(false);
563 if (!too_late) {
564 set_arm_goal_position(arm::BackMiddle2BoxIndex());
565 }
566 set_roller_voltage(0.0);
567 SendSuperstructureGoal();
568
569 StartDrive(-kThirdCubeDropDrive, 0.0, kThirdBoxPlaceDrive, kReallyFastTurn);
570
571 if (!WaitForDriveNear(kThirdCubeDropDrive - 0.23, M_PI / 4.0)) return true;
572 StartDrive(0.0, -kThirdCubeTurn - 0.2 - kThirdBoxEndTurnAngle - 0.3,
573 kThirdBoxPlaceDrive, kReallyFastTurn);
574
575 if (!WaitForDriveNear(0.30, M_PI / 4.0 + 0.2)) return true;
576
577 if (!too_late) {
578 set_open_claw(true);
579 set_intake_angle(-M_PI / 4.0);
580 set_roller_voltage(0.0);
581 SendSuperstructureGoal();
582
Austin Schuhf257f3c2019-10-27 21:00:43 -0700583 AOS_LOG(INFO, "Final open %f\n",
584 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700585 }
586
587 if (!WaitForDriveProfileDone()) return true;
588 if (!WaitForTurnProfileDone()) return true;
589
590 ::std::this_thread::sleep_for(chrono::milliseconds(14750) -
Austin Schuh77ed5432019-07-07 20:41:36 -0700591 (monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700592
593 set_arm_goal_position(arm::UpIndex());
594 SendSuperstructureGoal();
595
596 ::std::this_thread::sleep_for(chrono::milliseconds(15000) -
Austin Schuh77ed5432019-07-07 20:41:36 -0700597 (monotonic_now() - start_time));
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700598
599 set_close_claw(true);
600 SendSuperstructureGoal();
601 return false;
602}
603
Austin Schuha3c148e2018-03-09 21:04:05 -0800604} // namespace actors
605} // namespace y2018