blob: 0f702920cad1ceb8212fc58da91eb8e045edc7c8 [file] [log] [blame]
Niko Sohmers3860f8a2024-01-12 21:05:19 -08001#include "y2024/autonomous/autonomous_actor.h"
2
3#include <chrono>
4#include <cinttypes>
5#include <cmath>
6
7#include "aos/logging/logging.h"
8#include "aos/util/math.h"
9#include "frc971/control_loops/drivetrain/localizer_generated.h"
10#include "y2024/autonomous/auto_splines.h"
11#include "y2024/constants.h"
12#include "y2024/control_loops/drivetrain/drivetrain_base.h"
13
14DEFINE_bool(spline_auto, false, "Run simple test S-spline auto mode.");
James Kuszmaulb5f11832024-03-15 22:30:59 -070015DEFINE_bool(do_fifth_piece, true, "");
Niko Sohmers3860f8a2024-01-12 21:05:19 -080016
Stephan Pleinesf63bde82024-01-13 15:59:33 -080017namespace y2024::autonomous {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080018
19using ::frc971::ProfileParametersT;
20
21ProfileParametersT MakeProfileParameters(float max_velocity,
22 float max_acceleration) {
23 ProfileParametersT result;
24 result.max_velocity = max_velocity;
25 result.max_acceleration = max_acceleration;
26 return result;
27}
28
29using ::aos::monotonic_clock;
30using frc971::CreateProfileParameters;
31using ::frc971::ProfileParametersT;
32using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal;
33using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal;
34using frc971::control_loops::drivetrain::LocalizerControl;
35namespace chrono = ::std::chrono;
36
James Kuszmaulb5f11832024-03-15 22:30:59 -070037AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop,
38 const y2024::Constants *robot_constants)
Filip Kujawa58ffbf32024-02-24 18:28:34 -080039 : frc971::autonomous::UserButtonLocalizedAutonomousActor(
James Kuszmaul2549e752024-01-20 17:42:51 -080040 event_loop,
41 control_loops::drivetrain::GetDrivetrainConfig(event_loop)),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080042 localizer_control_sender_(
43 event_loop->MakeSender<
44 ::frc971::control_loops::drivetrain::LocalizerControl>(
45 "/drivetrain")),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080046 superstructure_goal_sender_(
Filip Kujawa58ffbf32024-02-24 18:28:34 -080047 event_loop
48 ->MakeSender<::y2024::control_loops::superstructure::GoalStatic>(
49 "/superstructure")),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080050 superstructure_status_fetcher_(
51 event_loop
52 ->MakeFetcher<::y2024::control_loops::superstructure::Status>(
Filip Kujawa58ffbf32024-02-24 18:28:34 -080053 "/superstructure")),
James Kuszmaulb5f11832024-03-15 22:30:59 -070054 robot_constants_(CHECK_NOTNULL(robot_constants)),
Filip Kujawa58ffbf32024-02-24 18:28:34 -080055 auto_splines_() {}
Niko Sohmers3860f8a2024-01-12 21:05:19 -080056
57void AutonomousActor::Replan() {
James Kuszmaulb5f11832024-03-15 22:30:59 -070058 AutonomousMode mode = robot_constants_->common()->autonomous_mode();
59 switch (mode) {
60 case AutonomousMode::NONE:
61 break;
62 case AutonomousMode::SPLINE_AUTO:
63 test_spline_ =
64 PlanSpline(std::bind(&AutonomousSplines::TestSpline, &auto_splines_,
65 std::placeholders::_1, alliance_),
66 SplineDirection::kForward);
Niko Sohmers3860f8a2024-01-12 21:05:19 -080067
James Kuszmaulb5f11832024-03-15 22:30:59 -070068 starting_position_ = test_spline_->starting_position();
69 break;
70 case AutonomousMode::MOBILITY_AND_SHOOT:
71 AOS_LOG(INFO, "Mobility & shoot replanning!");
72 mobility_and_shoot_splines_ = {PlanSpline(
73 std::bind(&AutonomousSplines::MobilityAndShootSpline, &auto_splines_,
74 std::placeholders::_1, alliance_),
75 SplineDirection::kForward)};
76
77 starting_position_ =
78 mobility_and_shoot_splines_.value()[0].starting_position();
79 CHECK(starting_position_);
80 break;
James Kuszmaul58f51432024-03-24 15:00:06 -070081 case AutonomousMode::FIVE_PIECE:
82 AOS_LOG(INFO, "FIVE_PIECE replanning!");
James Kuszmaulb5f11832024-03-15 22:30:59 -070083 four_piece_splines_ = {
84 PlanSpline(
85 std::bind(&AutonomousSplines::FourPieceSpline1, &auto_splines_,
86 std::placeholders::_1, alliance_),
87 SplineDirection::kForward),
88 PlanSpline(
89 std::bind(&AutonomousSplines::FourPieceSpline2, &auto_splines_,
90 std::placeholders::_1, alliance_),
91 SplineDirection::kBackward),
92 PlanSpline(
93 std::bind(&AutonomousSplines::FourPieceSpline3, &auto_splines_,
94 std::placeholders::_1, alliance_),
95 SplineDirection::kForward),
96 PlanSpline(
97 std::bind(&AutonomousSplines::FourPieceSpline4, &auto_splines_,
98 std::placeholders::_1, alliance_),
99 SplineDirection::kForward),
100 PlanSpline(
101 std::bind(&AutonomousSplines::FourPieceSpline5, &auto_splines_,
102 std::placeholders::_1, alliance_),
103 SplineDirection::kBackward)};
104
105 starting_position_ = four_piece_splines_.value()[0].starting_position();
106 CHECK(starting_position_);
107 break;
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700108 case AutonomousMode::TWO_PIECE_STEAL:
109 AOS_LOG(INFO, "TWO_PIECE_STEAL replanning!");
110 two_piece_steal_splines_ = {
111 PlanSpline(
112 std::bind(&AutonomousSplines::TwoPieceStealSpline1,
113 &auto_splines_, std::placeholders::_1, alliance_),
114 SplineDirection::kForward),
115 PlanSpline(
116 std::bind(&AutonomousSplines::TwoPieceStealSpline2,
117 &auto_splines_, std::placeholders::_1, alliance_),
118 SplineDirection::kBackward),
119 PlanSpline(
120 std::bind(&AutonomousSplines::TwoPieceStealSpline3,
121 &auto_splines_, std::placeholders::_1, alliance_),
122 SplineDirection::kForward),
123 PlanSpline(
124 std::bind(&AutonomousSplines::TwoPieceStealSpline4,
125 &auto_splines_, std::placeholders::_1, alliance_),
James Kuszmaul58f51432024-03-24 15:00:06 -0700126 SplineDirection::kBackward)};
127
128 starting_position_ =
129 two_piece_steal_splines_.value()[0].starting_position();
130 CHECK(starting_position_);
131 break;
James Kuszmaul337e3a52024-05-04 14:06:52 -0700132 case AutonomousMode::TWO_PIECE_VIA_STAGE:
133 AOS_LOG(INFO, "TWO_PIECE_VIA_STAGE replanning!");
134 two_piece_via_stage_splines_ = {
135 PlanSpline(
136 std::bind(&AutonomousSplines::TwoPieceViaStageSpline1,
137 &auto_splines_, std::placeholders::_1, alliance_),
138 SplineDirection::kForward),
139 PlanSpline(
140 std::bind(&AutonomousSplines::TwoPieceViaStageSpline2,
141 &auto_splines_, std::placeholders::_1, alliance_),
142 SplineDirection::kBackward),
143 PlanSpline(
144 std::bind(&AutonomousSplines::TwoPieceViaStageSpline3,
145 &auto_splines_, std::placeholders::_1, alliance_),
146 SplineDirection::kForward),
147 PlanSpline(
148 std::bind(&AutonomousSplines::TwoPieceViaStageSpline4,
149 &auto_splines_, std::placeholders::_1, alliance_),
150 SplineDirection::kBackward)};
151
152 starting_position_ =
153 two_piece_via_stage_splines_.value()[0].starting_position();
154 CHECK(starting_position_);
155 break;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800156 }
157
158 is_planned_ = true;
159
160 MaybeSendStartingPosition();
161}
162
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800163bool AutonomousActor::Run(
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800164 const ::frc971::autonomous::AutonomousActionParams *params) {
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800165 AOS_LOG(INFO, "Params are %d\n", params->mode());
166 if (alliance_ == aos::Alliance::kInvalid) {
167 AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection.");
168 return false;
169 }
James Kuszmaulb5f11832024-03-15 22:30:59 -0700170
171 AutonomousMode mode = robot_constants_->common()->autonomous_mode();
172 switch (mode) {
173 case AutonomousMode::NONE:
174 AOS_LOG(WARNING, "No auto mode selected.");
175 break;
176 case AutonomousMode::SPLINE_AUTO:
177 SplineAuto();
178 break;
179 case AutonomousMode::MOBILITY_AND_SHOOT:
180 MobilityAndShoot();
181 break;
James Kuszmaul58f51432024-03-24 15:00:06 -0700182 case AutonomousMode::FIVE_PIECE:
James Kuszmaulb5f11832024-03-15 22:30:59 -0700183 FourPieceAuto();
184 break;
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700185 case AutonomousMode::TWO_PIECE_STEAL:
186 TwoPieceStealAuto();
187 break;
James Kuszmaul337e3a52024-05-04 14:06:52 -0700188 case AutonomousMode::TWO_PIECE_VIA_STAGE:
189 TwoPieceViaStageAuto();
190 break;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800191 }
192 return true;
193}
194
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800195void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) {
196 // Set up the starting position for the blue alliance.
197
198 auto builder = localizer_control_sender_.MakeBuilder();
199
200 LocalizerControl::Builder localizer_control_builder =
201 builder.MakeBuilder<LocalizerControl>();
202 localizer_control_builder.add_x(start(0));
203 localizer_control_builder.add_y(start(1));
204 localizer_control_builder.add_theta(start(2));
205 localizer_control_builder.add_theta_uncertainty(0.00001);
206 AOS_LOG(INFO, "User button pressed, x: %f y: %f theta: %f", start(0),
207 start(1), start(2));
208 if (builder.Send(localizer_control_builder.Finish()) !=
209 aos::RawSender::Error::kOk) {
210 AOS_LOG(ERROR, "Failed to reset localizer.\n");
211 }
212}
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800213
214void AutonomousActor::Reset() {
215 set_intake_goal(control_loops::superstructure::IntakeGoal::NONE);
216 set_note_goal(control_loops::superstructure::NoteGoal::NONE);
Filip Kujawa1286c012024-03-31 22:53:27 -0700217 set_auto_aim(control_loops::superstructure::AutoAimMode::NONE);
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800218 set_fire(false);
219 set_preloaded(false);
220 SendSuperstructureGoal();
221}
222
223void AutonomousActor::SplineAuto() {
224 CHECK(test_spline_);
225
226 if (!test_spline_->WaitForPlan()) return;
227 test_spline_->Start();
228
229 if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return;
230}
231
James Kuszmaulb5f11832024-03-15 22:30:59 -0700232void AutonomousActor::MobilityAndShoot() {
233 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
234
235 uint32_t initial_shot_count = shot_count();
236
237 CHECK(mobility_and_shoot_splines_);
238
239 auto &splines = *mobility_and_shoot_splines_;
240
241 AOS_LOG(INFO, "Going to preload");
242
243 // Always be auto-aiming.
244 Aim();
245
246 if (!WaitForPreloaded()) return;
247
248 AOS_LOG(INFO, "Starting to Move");
249
250 if (!splines[0].WaitForPlan()) return;
251
252 splines[0].Start();
253
254 if (!splines[0].WaitForSplineDistanceRemaining(0.05)) return;
255
256 AOS_LOG(
257 INFO, "Got there %lf s\nNow Shooting\n",
258 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
259
260 Shoot();
261
262 if (!WaitForNoteFired(initial_shot_count, std::chrono::seconds(5))) return;
263
264 StopFiring();
265
266 AOS_LOG(
267 INFO, "Note fired at %lf seconds\n",
268 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
269}
270
271void AutonomousActor::FourPieceAuto() {
272 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
273
274 CHECK(four_piece_splines_);
275 auto &splines = *four_piece_splines_;
276
277 uint32_t initial_shot_count = shot_count();
278
279 // Always be aiming & firing.
280 Aim();
281 if (!WaitForPreloaded()) return;
282
James Kuszmaulb5f11832024-03-15 22:30:59 -0700283 Shoot();
284
285 AOS_LOG(
286 INFO, "Shooting Preloaded Note %lfs\n",
287 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
288
289 if (!WaitForNoteFired(initial_shot_count, std::chrono::seconds(2))) return;
290
291 AOS_LOG(
292 INFO, "Shot first note %lfs\n",
293 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
294
295 Intake();
296 StopFiring();
297
298 AOS_LOG(
299 INFO, "Starting Spline 1 %lfs\n",
300 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
301
302 if (!splines[0].WaitForPlan()) return;
303
304 splines[0].Start();
305
306 if (!splines[0].WaitForSplineDistanceRemaining(0.01)) return;
307
308 if (!splines[1].WaitForPlan()) return;
309
310 AOS_LOG(
311 INFO, "Starting second spline %lfs\n",
312 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
313
314 splines[1].Start();
315
316 if (!splines[1].WaitForSplineDistanceRemaining(0.01)) return;
317
318 AOS_LOG(
319 INFO, "Finished second spline %lfs\n",
320 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
321
James Kuszmaul58f51432024-03-24 15:00:06 -0700322 std::this_thread::sleep_for(chrono::milliseconds(200));
James Kuszmaulb5f11832024-03-15 22:30:59 -0700323
324 Shoot();
325
326 if (!WaitForNoteFired(initial_shot_count + 1, std::chrono::seconds(2)))
327 return;
328
329 AOS_LOG(
330 INFO, "Shot second note, starting drive %lfs\n",
331 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
332
333 if (!splines[2].WaitForPlan()) return;
334 splines[2].Start();
335
336 if (!splines[2].WaitForSplineDistanceRemaining(0.01)) return;
337
338 if (!WaitForNoteFired(initial_shot_count + 3, std::chrono::seconds(5)))
339 return;
340
341 AOS_LOG(
342 INFO, "Finished 4 notes at %lfs\n",
343 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
344
345 if (!FLAGS_do_fifth_piece) {
346 AOS_LOG(INFO, "Exitting early due to --nodo_fifth_piece");
347 return;
348 }
349
350 if (!splines[3].WaitForPlan()) return;
351 splines[3].Start();
352
353 if (!splines[3].WaitForSplineDistanceRemaining(0.1)) return;
354
355 AOS_LOG(
356 INFO, "Got to 5th note at %lfs\n",
357 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
358
359 StopFiring();
360
361 if (!splines[4].WaitForPlan()) return;
362 splines[4].Start();
363
James Kuszmaulf2f95b32024-03-23 20:12:05 -0700364 if (!splines[4].WaitForSplineDistanceRemaining(0.01)) return;
James Kuszmaulb5f11832024-03-15 22:30:59 -0700365
366 AOS_LOG(
367 INFO, "Done with spline %lfs\n",
368 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
369
James Kuszmaul58f51432024-03-24 15:00:06 -0700370 std::this_thread::sleep_for(chrono::milliseconds(250));
James Kuszmaulb5f11832024-03-15 22:30:59 -0700371
372 AOS_LOG(
373 INFO, "Shooting last note! %lfs\n",
374 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
375
376 Shoot();
377
378 if (!WaitForNoteFired(initial_shot_count + 4, std::chrono::seconds(5)))
379 return;
380
381 AOS_LOG(
382 INFO, "Done %lfs\n",
383 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
384}
385
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700386void AutonomousActor::TwoPieceStealAuto() {
387 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
388
389 CHECK(two_piece_steal_splines_);
390 auto &splines = *two_piece_steal_splines_;
391
392 uint32_t initial_shot_count = shot_count();
393
394 // Always be aiming & firing.
395 Aim();
396 if (!WaitForPreloaded()) return;
397
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700398 Shoot();
399
400 AOS_LOG(
401 INFO, "Shooting Preloaded Note %lfs\n",
402 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
403
404 if (!WaitForNoteFired(initial_shot_count, std::chrono::seconds(2))) return;
405
406 AOS_LOG(
407 INFO, "Shot first note %lfs\n",
408 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
409
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700410 StopFiring();
411
412 AOS_LOG(
413 INFO, "Starting Spline 1 %lfs\n",
414 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
415
416 if (!splines[0].WaitForPlan()) return;
417
418 splines[0].Start();
419
James Kuszmaul58f51432024-03-24 15:00:06 -0700420 if (!splines[0].WaitForSplineDistanceRemaining(2.0)) return;
421 Intake();
422
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700423 if (!splines[0].WaitForSplineDistanceRemaining(0.01)) return;
424
425 if (!splines[1].WaitForPlan()) return;
426
427 AOS_LOG(
428 INFO, "Starting second spline %lfs\n",
429 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
430
431 splines[1].Start();
432
433 if (!splines[1].WaitForSplineDistanceRemaining(0.01)) return;
434
435 AOS_LOG(
436 INFO, "Finished second spline %lfs\n",
437 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
438
James Kuszmaul58f51432024-03-24 15:00:06 -0700439 std::this_thread::sleep_for(chrono::milliseconds(200));
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700440
441 Shoot();
James Kuszmaul58f51432024-03-24 15:00:06 -0700442 StopIntake();
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700443
444 if (!WaitForNoteFired(initial_shot_count + 1, std::chrono::seconds(2)))
445 return;
446
447 StopFiring();
448
449 AOS_LOG(
450 INFO, "Shot second note, starting drive %lfs\n",
451 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
452
453 if (!splines[2].WaitForPlan()) return;
454
455 AOS_LOG(
456 INFO, "Starting third spline %lfs\n",
457 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
458 splines[2].Start();
459
James Kuszmaul58f51432024-03-24 15:00:06 -0700460 if (!splines[2].WaitForSplineDistanceRemaining(2.0)) return;
461
462 Intake();
463
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700464 if (!splines[2].WaitForSplineDistanceRemaining(0.01)) return;
465
466 if (!splines[3].WaitForPlan()) return;
467
468 AOS_LOG(
469 INFO, "Starting fourth spline %lfs\n",
470 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
471 splines[3].Start();
472
473 if (!splines[3].WaitForSplineDistanceRemaining(0.01)) return;
474
475 Shoot();
476
James Kuszmaul58f51432024-03-24 15:00:06 -0700477 std::this_thread::sleep_for(chrono::milliseconds(400));
478
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700479 if (!WaitForNoteFired(initial_shot_count + 2, std::chrono::seconds(2)))
480 return;
481
482 AOS_LOG(
483 INFO, "Done %lfs\n",
484 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
485}
486
James Kuszmaul337e3a52024-05-04 14:06:52 -0700487void AutonomousActor::TwoPieceViaStageAuto() {
488 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
489
490 CHECK(two_piece_via_stage_splines_);
491 auto &splines = *two_piece_via_stage_splines_;
492
493 uint32_t initial_shot_count = shot_count();
494
495 // Always be aiming & firing.
496 Aim();
497 if (!WaitForPreloaded()) return;
498
499 Shoot();
500
501 AOS_LOG(
502 INFO, "Shooting Preloaded Note %lfs\n",
503 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
504
505 if (!WaitForNoteFired(initial_shot_count, std::chrono::seconds(2))) return;
506 StopAiming();
507
508 AOS_LOG(
509 INFO, "Shot first note %lfs\n",
510 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
511
512 StopFiring();
513
514 AOS_LOG(
515 INFO, "Starting Spline 1 %lfs\n",
516 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
517
518 if (!splines[0].WaitForPlan()) return;
519
520 splines[0].Start();
521
522 if (!splines[0].WaitForSplineDistanceRemaining(2.0)) return;
523 Intake();
524
525 if (!splines[0].WaitForSplineDistanceRemaining(0.01)) return;
526
527 if (!splines[1].WaitForPlan()) return;
528
529 AOS_LOG(
530 INFO, "Starting second spline %lfs\n",
531 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
532
533 splines[1].Start();
534
535 if (!splines[1].WaitForSplineDistanceRemaining(0.01)) return;
536
537 Aim();
538
539 AOS_LOG(
540 INFO, "Finished second spline %lfs\n",
541 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
542
543 std::this_thread::sleep_for(chrono::milliseconds(1000));
544
545 Shoot();
546 StopIntake();
547
548 if (!WaitForNoteFired(initial_shot_count + 1, std::chrono::seconds(2)))
549 return;
550
551 StopFiring();
552 StopAiming();
553
554 AOS_LOG(
555 INFO, "Shot second note, starting drive %lfs\n",
556 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
557
558 if (!splines[2].WaitForPlan()) return;
559
560 AOS_LOG(
561 INFO, "Starting third spline %lfs\n",
562 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
563 splines[2].Start();
564
565 if (!splines[2].WaitForSplineDistanceRemaining(2.0)) return;
566
567 Intake();
568
569 if (!splines[2].WaitForSplineDistanceRemaining(0.01)) return;
570
571 if (!splines[3].WaitForPlan()) return;
572
573 AOS_LOG(
574 INFO, "Starting fourth spline %lfs\n",
575 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
576 splines[3].Start();
577
578 if (!splines[3].WaitForSplineDistanceRemaining(0.01)) return;
579
580 Aim();
581
582 Shoot();
583
584 std::this_thread::sleep_for(chrono::milliseconds(400));
585
586 if (!WaitForNoteFired(initial_shot_count + 2, std::chrono::seconds(2)))
587 return;
588
589 AOS_LOG(
590 INFO, "Done %lfs\n",
591 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
592}
593
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800594void AutonomousActor::SendSuperstructureGoal() {
595 aos::Sender<control_loops::superstructure::GoalStatic>::StaticBuilder
596 goal_builder = superstructure_goal_sender_.MakeStaticBuilder();
597
598 goal_builder->set_intake_goal(intake_goal_);
James Kuszmaulb5f11832024-03-15 22:30:59 -0700599 if (intake_goal_ == control_loops::superstructure::IntakeGoal::INTAKE) {
600 goal_builder->set_intake_pivot(
601 control_loops::superstructure::IntakePivotGoal::DOWN);
602 } else {
603 goal_builder->set_intake_pivot(
604 control_loops::superstructure::IntakePivotGoal::UP);
605 }
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800606 goal_builder->set_note_goal(note_goal_);
607 goal_builder->set_fire(fire_);
608
609 control_loops::superstructure::ShooterGoalStatic *shooter_goal =
610 goal_builder->add_shooter_goal();
611
612 shooter_goal->set_auto_aim(auto_aim_);
613 shooter_goal->set_preloaded(preloaded_);
614
615 goal_builder.CheckOk(goal_builder.Send());
616}
617
James Kuszmaul58f51432024-03-24 15:00:06 -0700618void AutonomousActor::StopIntake() {
619 set_intake_goal(control_loops::superstructure::IntakeGoal::NONE);
620 set_note_goal(control_loops::superstructure::NoteGoal::CATAPULT);
621 SendSuperstructureGoal();
622}
623
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800624void AutonomousActor::Intake() {
625 set_intake_goal(control_loops::superstructure::IntakeGoal::INTAKE);
626 set_note_goal(control_loops::superstructure::NoteGoal::CATAPULT);
627 SendSuperstructureGoal();
628}
629
James Kuszmaul337e3a52024-05-04 14:06:52 -0700630void AutonomousActor::StopAiming() {
631 set_auto_aim(control_loops::superstructure::AutoAimMode::NONE);
632 SendSuperstructureGoal();
633}
634
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800635void AutonomousActor::Aim() {
Filip Kujawa1286c012024-03-31 22:53:27 -0700636 set_auto_aim(control_loops::superstructure::AutoAimMode::SPEAKER);
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800637 SendSuperstructureGoal();
638}
639
640void AutonomousActor::Shoot() {
641 set_fire(true);
642 SendSuperstructureGoal();
643}
644
James Kuszmaulb5f11832024-03-15 22:30:59 -0700645void AutonomousActor::StopFiring() {
646 set_fire(false);
647 SendSuperstructureGoal();
648}
649
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800650[[nodiscard]] bool AutonomousActor::WaitForPreloaded() {
651 set_preloaded(true);
652 SendSuperstructureGoal();
653
654 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
655 event_loop()->monotonic_now(),
656 aos::common::actions::kLoopOffset);
657
658 bool loaded = false;
659 while (!loaded) {
660 if (ShouldCancel()) {
661 return false;
662 }
663
664 phased_loop.SleepUntilNext();
665 superstructure_status_fetcher_.Fetch();
666 CHECK(superstructure_status_fetcher_.get() != nullptr);
667
668 loaded = (superstructure_status_fetcher_->shooter()->catapult_state() ==
669 control_loops::superstructure::CatapultState::LOADED);
670 }
671
672 set_preloaded(false);
673 SendSuperstructureGoal();
674
675 return true;
676}
677
James Kuszmaulb5f11832024-03-15 22:30:59 -0700678uint32_t AutonomousActor::shot_count() {
679 superstructure_status_fetcher_.Fetch();
680 return superstructure_status_fetcher_->shot_count();
681}
682
683[[nodiscard]] bool AutonomousActor::WaitForNoteFired(
684 uint32_t penultimate_target_shot_count, std::chrono::nanoseconds timeout) {
685 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
686 event_loop()->monotonic_now(),
687 aos::common::actions::kLoopOffset);
688 aos::monotonic_clock::time_point end_time =
689 event_loop()->monotonic_now() + timeout;
690 while (true) {
691 if (ShouldCancel()) {
692 return false;
693 }
694
695 phased_loop.SleepUntilNext();
696
697 if (shot_count() > penultimate_target_shot_count ||
698 event_loop()->monotonic_now() > end_time) {
699 return true;
700 }
701 }
702}
703
704[[nodiscard]] bool AutonomousActor::WaitForCatapultReady() {
705 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
706 event_loop()->monotonic_now(),
707 aos::common::actions::kLoopOffset);
708
709 bool loaded = false;
710 while (!loaded) {
711 if (ShouldCancel()) {
712 return false;
713 }
714
715 phased_loop.SleepUntilNext();
716 superstructure_status_fetcher_.Fetch();
717 CHECK(superstructure_status_fetcher_.get() != nullptr);
718
719 loaded = (superstructure_status_fetcher_->state() ==
720 control_loops::superstructure::SuperstructureState::READY);
721 }
722
723 SendSuperstructureGoal();
724
725 return true;
726}
727
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800728} // namespace y2024::autonomous