blob: 83cf5b111b7bb1d55470588e0290b9c91323e9f3 [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")),
Austin Schuh6bdcc372024-06-27 14:49:11 -070054 robot_constants_(robot_constants),
55 auto_splines_() {
56 CHECK(robot_constants_ != nullptr);
57}
Niko Sohmers3860f8a2024-01-12 21:05:19 -080058
59void AutonomousActor::Replan() {
James Kuszmaulb5f11832024-03-15 22:30:59 -070060 AutonomousMode mode = robot_constants_->common()->autonomous_mode();
61 switch (mode) {
62 case AutonomousMode::NONE:
63 break;
64 case AutonomousMode::SPLINE_AUTO:
65 test_spline_ =
66 PlanSpline(std::bind(&AutonomousSplines::TestSpline, &auto_splines_,
67 std::placeholders::_1, alliance_),
68 SplineDirection::kForward);
Niko Sohmers3860f8a2024-01-12 21:05:19 -080069
James Kuszmaulb5f11832024-03-15 22:30:59 -070070 starting_position_ = test_spline_->starting_position();
71 break;
72 case AutonomousMode::MOBILITY_AND_SHOOT:
73 AOS_LOG(INFO, "Mobility & shoot replanning!");
74 mobility_and_shoot_splines_ = {PlanSpline(
75 std::bind(&AutonomousSplines::MobilityAndShootSpline, &auto_splines_,
76 std::placeholders::_1, alliance_),
77 SplineDirection::kForward)};
78
79 starting_position_ =
80 mobility_and_shoot_splines_.value()[0].starting_position();
81 CHECK(starting_position_);
82 break;
James Kuszmaul58f51432024-03-24 15:00:06 -070083 case AutonomousMode::FIVE_PIECE:
84 AOS_LOG(INFO, "FIVE_PIECE replanning!");
James Kuszmaulb5f11832024-03-15 22:30:59 -070085 four_piece_splines_ = {
86 PlanSpline(
87 std::bind(&AutonomousSplines::FourPieceSpline1, &auto_splines_,
88 std::placeholders::_1, alliance_),
89 SplineDirection::kForward),
90 PlanSpline(
91 std::bind(&AutonomousSplines::FourPieceSpline2, &auto_splines_,
92 std::placeholders::_1, alliance_),
93 SplineDirection::kBackward),
94 PlanSpline(
95 std::bind(&AutonomousSplines::FourPieceSpline3, &auto_splines_,
96 std::placeholders::_1, alliance_),
97 SplineDirection::kForward),
98 PlanSpline(
99 std::bind(&AutonomousSplines::FourPieceSpline4, &auto_splines_,
100 std::placeholders::_1, alliance_),
101 SplineDirection::kForward),
102 PlanSpline(
103 std::bind(&AutonomousSplines::FourPieceSpline5, &auto_splines_,
104 std::placeholders::_1, alliance_),
105 SplineDirection::kBackward)};
106
107 starting_position_ = four_piece_splines_.value()[0].starting_position();
108 CHECK(starting_position_);
109 break;
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700110 case AutonomousMode::TWO_PIECE_STEAL:
111 AOS_LOG(INFO, "TWO_PIECE_STEAL replanning!");
112 two_piece_steal_splines_ = {
113 PlanSpline(
114 std::bind(&AutonomousSplines::TwoPieceStealSpline1,
115 &auto_splines_, std::placeholders::_1, alliance_),
116 SplineDirection::kForward),
117 PlanSpline(
118 std::bind(&AutonomousSplines::TwoPieceStealSpline2,
119 &auto_splines_, std::placeholders::_1, alliance_),
120 SplineDirection::kBackward),
121 PlanSpline(
122 std::bind(&AutonomousSplines::TwoPieceStealSpline3,
123 &auto_splines_, std::placeholders::_1, alliance_),
124 SplineDirection::kForward),
125 PlanSpline(
126 std::bind(&AutonomousSplines::TwoPieceStealSpline4,
127 &auto_splines_, std::placeholders::_1, alliance_),
James Kuszmaul58f51432024-03-24 15:00:06 -0700128 SplineDirection::kBackward)};
129
130 starting_position_ =
131 two_piece_steal_splines_.value()[0].starting_position();
132 CHECK(starting_position_);
133 break;
James Kuszmaul337e3a52024-05-04 14:06:52 -0700134 case AutonomousMode::TWO_PIECE_VIA_STAGE:
135 AOS_LOG(INFO, "TWO_PIECE_VIA_STAGE replanning!");
136 two_piece_via_stage_splines_ = {
137 PlanSpline(
138 std::bind(&AutonomousSplines::TwoPieceViaStageSpline1,
139 &auto_splines_, std::placeholders::_1, alliance_),
140 SplineDirection::kForward),
141 PlanSpline(
142 std::bind(&AutonomousSplines::TwoPieceViaStageSpline2,
143 &auto_splines_, std::placeholders::_1, alliance_),
144 SplineDirection::kBackward),
145 PlanSpline(
146 std::bind(&AutonomousSplines::TwoPieceViaStageSpline3,
147 &auto_splines_, std::placeholders::_1, alliance_),
148 SplineDirection::kForward),
149 PlanSpline(
150 std::bind(&AutonomousSplines::TwoPieceViaStageSpline4,
151 &auto_splines_, std::placeholders::_1, alliance_),
152 SplineDirection::kBackward)};
153
154 starting_position_ =
155 two_piece_via_stage_splines_.value()[0].starting_position();
156 CHECK(starting_position_);
157 break;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800158 }
159
160 is_planned_ = true;
161
162 MaybeSendStartingPosition();
163}
164
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800165bool AutonomousActor::Run(
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800166 const ::frc971::autonomous::AutonomousActionParams *params) {
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800167 AOS_LOG(INFO, "Params are %d\n", params->mode());
168 if (alliance_ == aos::Alliance::kInvalid) {
169 AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection.");
170 return false;
171 }
James Kuszmaulb5f11832024-03-15 22:30:59 -0700172
173 AutonomousMode mode = robot_constants_->common()->autonomous_mode();
174 switch (mode) {
175 case AutonomousMode::NONE:
176 AOS_LOG(WARNING, "No auto mode selected.");
177 break;
178 case AutonomousMode::SPLINE_AUTO:
179 SplineAuto();
180 break;
181 case AutonomousMode::MOBILITY_AND_SHOOT:
182 MobilityAndShoot();
183 break;
James Kuszmaul58f51432024-03-24 15:00:06 -0700184 case AutonomousMode::FIVE_PIECE:
James Kuszmaulb5f11832024-03-15 22:30:59 -0700185 FourPieceAuto();
186 break;
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700187 case AutonomousMode::TWO_PIECE_STEAL:
188 TwoPieceStealAuto();
189 break;
James Kuszmaul337e3a52024-05-04 14:06:52 -0700190 case AutonomousMode::TWO_PIECE_VIA_STAGE:
191 TwoPieceViaStageAuto();
192 break;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800193 }
194 return true;
195}
196
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800197void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) {
198 // Set up the starting position for the blue alliance.
199
200 auto builder = localizer_control_sender_.MakeBuilder();
201
202 LocalizerControl::Builder localizer_control_builder =
203 builder.MakeBuilder<LocalizerControl>();
204 localizer_control_builder.add_x(start(0));
205 localizer_control_builder.add_y(start(1));
206 localizer_control_builder.add_theta(start(2));
207 localizer_control_builder.add_theta_uncertainty(0.00001);
208 AOS_LOG(INFO, "User button pressed, x: %f y: %f theta: %f", start(0),
209 start(1), start(2));
210 if (builder.Send(localizer_control_builder.Finish()) !=
211 aos::RawSender::Error::kOk) {
212 AOS_LOG(ERROR, "Failed to reset localizer.\n");
213 }
214}
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800215
216void AutonomousActor::Reset() {
217 set_intake_goal(control_loops::superstructure::IntakeGoal::NONE);
218 set_note_goal(control_loops::superstructure::NoteGoal::NONE);
Filip Kujawa1286c012024-03-31 22:53:27 -0700219 set_auto_aim(control_loops::superstructure::AutoAimMode::NONE);
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800220 set_fire(false);
221 set_preloaded(false);
222 SendSuperstructureGoal();
223}
224
225void AutonomousActor::SplineAuto() {
226 CHECK(test_spline_);
227
228 if (!test_spline_->WaitForPlan()) return;
229 test_spline_->Start();
230
231 if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return;
232}
233
James Kuszmaulb5f11832024-03-15 22:30:59 -0700234void AutonomousActor::MobilityAndShoot() {
235 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
236
237 uint32_t initial_shot_count = shot_count();
238
239 CHECK(mobility_and_shoot_splines_);
240
241 auto &splines = *mobility_and_shoot_splines_;
242
243 AOS_LOG(INFO, "Going to preload");
244
245 // Always be auto-aiming.
246 Aim();
247
248 if (!WaitForPreloaded()) return;
249
250 AOS_LOG(INFO, "Starting to Move");
251
252 if (!splines[0].WaitForPlan()) return;
253
254 splines[0].Start();
255
256 if (!splines[0].WaitForSplineDistanceRemaining(0.05)) return;
257
258 AOS_LOG(
259 INFO, "Got there %lf s\nNow Shooting\n",
260 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
261
262 Shoot();
263
264 if (!WaitForNoteFired(initial_shot_count, std::chrono::seconds(5))) return;
265
266 StopFiring();
267
268 AOS_LOG(
269 INFO, "Note fired at %lf seconds\n",
270 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
271}
272
273void AutonomousActor::FourPieceAuto() {
274 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
275
276 CHECK(four_piece_splines_);
277 auto &splines = *four_piece_splines_;
278
279 uint32_t initial_shot_count = shot_count();
280
281 // Always be aiming & firing.
282 Aim();
283 if (!WaitForPreloaded()) return;
284
James Kuszmaulb5f11832024-03-15 22:30:59 -0700285 Shoot();
286
287 AOS_LOG(
288 INFO, "Shooting Preloaded Note %lfs\n",
289 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
290
291 if (!WaitForNoteFired(initial_shot_count, std::chrono::seconds(2))) return;
292
293 AOS_LOG(
294 INFO, "Shot first note %lfs\n",
295 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
296
297 Intake();
298 StopFiring();
299
300 AOS_LOG(
301 INFO, "Starting Spline 1 %lfs\n",
302 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
303
304 if (!splines[0].WaitForPlan()) return;
305
306 splines[0].Start();
307
308 if (!splines[0].WaitForSplineDistanceRemaining(0.01)) return;
309
310 if (!splines[1].WaitForPlan()) return;
311
312 AOS_LOG(
313 INFO, "Starting second spline %lfs\n",
314 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
315
316 splines[1].Start();
317
318 if (!splines[1].WaitForSplineDistanceRemaining(0.01)) return;
319
320 AOS_LOG(
321 INFO, "Finished second spline %lfs\n",
322 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
323
James Kuszmaul58f51432024-03-24 15:00:06 -0700324 std::this_thread::sleep_for(chrono::milliseconds(200));
James Kuszmaulb5f11832024-03-15 22:30:59 -0700325
326 Shoot();
327
328 if (!WaitForNoteFired(initial_shot_count + 1, std::chrono::seconds(2)))
329 return;
330
331 AOS_LOG(
332 INFO, "Shot second note, starting drive %lfs\n",
333 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
334
335 if (!splines[2].WaitForPlan()) return;
336 splines[2].Start();
337
338 if (!splines[2].WaitForSplineDistanceRemaining(0.01)) return;
339
340 if (!WaitForNoteFired(initial_shot_count + 3, std::chrono::seconds(5)))
341 return;
342
343 AOS_LOG(
344 INFO, "Finished 4 notes at %lfs\n",
345 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
346
347 if (!FLAGS_do_fifth_piece) {
348 AOS_LOG(INFO, "Exitting early due to --nodo_fifth_piece");
349 return;
350 }
351
352 if (!splines[3].WaitForPlan()) return;
353 splines[3].Start();
354
355 if (!splines[3].WaitForSplineDistanceRemaining(0.1)) return;
356
357 AOS_LOG(
358 INFO, "Got to 5th note at %lfs\n",
359 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
360
361 StopFiring();
362
363 if (!splines[4].WaitForPlan()) return;
364 splines[4].Start();
365
James Kuszmaulf2f95b32024-03-23 20:12:05 -0700366 if (!splines[4].WaitForSplineDistanceRemaining(0.01)) return;
James Kuszmaulb5f11832024-03-15 22:30:59 -0700367
368 AOS_LOG(
369 INFO, "Done with spline %lfs\n",
370 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
371
James Kuszmaul58f51432024-03-24 15:00:06 -0700372 std::this_thread::sleep_for(chrono::milliseconds(250));
James Kuszmaulb5f11832024-03-15 22:30:59 -0700373
374 AOS_LOG(
375 INFO, "Shooting last note! %lfs\n",
376 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
377
378 Shoot();
379
380 if (!WaitForNoteFired(initial_shot_count + 4, std::chrono::seconds(5)))
381 return;
382
383 AOS_LOG(
384 INFO, "Done %lfs\n",
385 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
386}
387
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700388void AutonomousActor::TwoPieceStealAuto() {
389 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
390
391 CHECK(two_piece_steal_splines_);
392 auto &splines = *two_piece_steal_splines_;
393
394 uint32_t initial_shot_count = shot_count();
395
396 // Always be aiming & firing.
397 Aim();
398 if (!WaitForPreloaded()) return;
399
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700400 Shoot();
401
402 AOS_LOG(
403 INFO, "Shooting Preloaded Note %lfs\n",
404 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
405
406 if (!WaitForNoteFired(initial_shot_count, std::chrono::seconds(2))) return;
407
408 AOS_LOG(
409 INFO, "Shot first note %lfs\n",
410 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
411
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700412 StopFiring();
413
414 AOS_LOG(
415 INFO, "Starting Spline 1 %lfs\n",
416 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
417
418 if (!splines[0].WaitForPlan()) return;
419
420 splines[0].Start();
421
James Kuszmaul58f51432024-03-24 15:00:06 -0700422 if (!splines[0].WaitForSplineDistanceRemaining(2.0)) return;
423 Intake();
424
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700425 if (!splines[0].WaitForSplineDistanceRemaining(0.01)) return;
426
427 if (!splines[1].WaitForPlan()) return;
428
429 AOS_LOG(
430 INFO, "Starting second spline %lfs\n",
431 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
432
433 splines[1].Start();
434
435 if (!splines[1].WaitForSplineDistanceRemaining(0.01)) return;
436
437 AOS_LOG(
438 INFO, "Finished second spline %lfs\n",
439 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
440
James Kuszmaul58f51432024-03-24 15:00:06 -0700441 std::this_thread::sleep_for(chrono::milliseconds(200));
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700442
443 Shoot();
James Kuszmaul58f51432024-03-24 15:00:06 -0700444 StopIntake();
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700445
446 if (!WaitForNoteFired(initial_shot_count + 1, std::chrono::seconds(2)))
447 return;
448
449 StopFiring();
450
451 AOS_LOG(
452 INFO, "Shot second note, starting drive %lfs\n",
453 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
454
455 if (!splines[2].WaitForPlan()) return;
456
457 AOS_LOG(
458 INFO, "Starting third spline %lfs\n",
459 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
460 splines[2].Start();
461
James Kuszmaul58f51432024-03-24 15:00:06 -0700462 if (!splines[2].WaitForSplineDistanceRemaining(2.0)) return;
463
464 Intake();
465
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700466 if (!splines[2].WaitForSplineDistanceRemaining(0.01)) return;
467
468 if (!splines[3].WaitForPlan()) return;
469
470 AOS_LOG(
471 INFO, "Starting fourth spline %lfs\n",
472 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
473 splines[3].Start();
474
475 if (!splines[3].WaitForSplineDistanceRemaining(0.01)) return;
476
477 Shoot();
478
James Kuszmaul58f51432024-03-24 15:00:06 -0700479 std::this_thread::sleep_for(chrono::milliseconds(400));
480
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700481 if (!WaitForNoteFired(initial_shot_count + 2, std::chrono::seconds(2)))
482 return;
483
484 AOS_LOG(
485 INFO, "Done %lfs\n",
486 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
487}
488
James Kuszmaul337e3a52024-05-04 14:06:52 -0700489void AutonomousActor::TwoPieceViaStageAuto() {
490 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
491
492 CHECK(two_piece_via_stage_splines_);
493 auto &splines = *two_piece_via_stage_splines_;
494
495 uint32_t initial_shot_count = shot_count();
496
497 // Always be aiming & firing.
498 Aim();
499 if (!WaitForPreloaded()) return;
500
501 Shoot();
502
503 AOS_LOG(
504 INFO, "Shooting Preloaded Note %lfs\n",
505 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
506
507 if (!WaitForNoteFired(initial_shot_count, std::chrono::seconds(2))) return;
508 StopAiming();
509
510 AOS_LOG(
511 INFO, "Shot first note %lfs\n",
512 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
513
514 StopFiring();
515
516 AOS_LOG(
517 INFO, "Starting Spline 1 %lfs\n",
518 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
519
520 if (!splines[0].WaitForPlan()) return;
521
522 splines[0].Start();
523
524 if (!splines[0].WaitForSplineDistanceRemaining(2.0)) return;
525 Intake();
526
527 if (!splines[0].WaitForSplineDistanceRemaining(0.01)) return;
528
529 if (!splines[1].WaitForPlan()) return;
530
531 AOS_LOG(
532 INFO, "Starting second spline %lfs\n",
533 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
534
535 splines[1].Start();
536
537 if (!splines[1].WaitForSplineDistanceRemaining(0.01)) return;
538
539 Aim();
540
541 AOS_LOG(
542 INFO, "Finished second spline %lfs\n",
543 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
544
545 std::this_thread::sleep_for(chrono::milliseconds(1000));
546
547 Shoot();
548 StopIntake();
549
550 if (!WaitForNoteFired(initial_shot_count + 1, std::chrono::seconds(2)))
551 return;
552
553 StopFiring();
554 StopAiming();
555
556 AOS_LOG(
557 INFO, "Shot second note, starting drive %lfs\n",
558 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
559
560 if (!splines[2].WaitForPlan()) return;
561
562 AOS_LOG(
563 INFO, "Starting third spline %lfs\n",
564 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
565 splines[2].Start();
566
567 if (!splines[2].WaitForSplineDistanceRemaining(2.0)) return;
568
569 Intake();
570
571 if (!splines[2].WaitForSplineDistanceRemaining(0.01)) return;
572
573 if (!splines[3].WaitForPlan()) return;
574
575 AOS_LOG(
576 INFO, "Starting fourth spline %lfs\n",
577 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
578 splines[3].Start();
579
580 if (!splines[3].WaitForSplineDistanceRemaining(0.01)) return;
581
582 Aim();
583
584 Shoot();
585
586 std::this_thread::sleep_for(chrono::milliseconds(400));
587
588 if (!WaitForNoteFired(initial_shot_count + 2, std::chrono::seconds(2)))
589 return;
590
591 AOS_LOG(
592 INFO, "Done %lfs\n",
593 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
594}
595
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800596void AutonomousActor::SendSuperstructureGoal() {
597 aos::Sender<control_loops::superstructure::GoalStatic>::StaticBuilder
598 goal_builder = superstructure_goal_sender_.MakeStaticBuilder();
599
600 goal_builder->set_intake_goal(intake_goal_);
James Kuszmaulb5f11832024-03-15 22:30:59 -0700601 if (intake_goal_ == control_loops::superstructure::IntakeGoal::INTAKE) {
602 goal_builder->set_intake_pivot(
603 control_loops::superstructure::IntakePivotGoal::DOWN);
604 } else {
605 goal_builder->set_intake_pivot(
606 control_loops::superstructure::IntakePivotGoal::UP);
607 }
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800608 goal_builder->set_note_goal(note_goal_);
609 goal_builder->set_fire(fire_);
610
611 control_loops::superstructure::ShooterGoalStatic *shooter_goal =
612 goal_builder->add_shooter_goal();
613
614 shooter_goal->set_auto_aim(auto_aim_);
615 shooter_goal->set_preloaded(preloaded_);
616
617 goal_builder.CheckOk(goal_builder.Send());
618}
619
James Kuszmaul58f51432024-03-24 15:00:06 -0700620void AutonomousActor::StopIntake() {
621 set_intake_goal(control_loops::superstructure::IntakeGoal::NONE);
622 set_note_goal(control_loops::superstructure::NoteGoal::CATAPULT);
623 SendSuperstructureGoal();
624}
625
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800626void AutonomousActor::Intake() {
627 set_intake_goal(control_loops::superstructure::IntakeGoal::INTAKE);
628 set_note_goal(control_loops::superstructure::NoteGoal::CATAPULT);
629 SendSuperstructureGoal();
630}
631
James Kuszmaul337e3a52024-05-04 14:06:52 -0700632void AutonomousActor::StopAiming() {
633 set_auto_aim(control_loops::superstructure::AutoAimMode::NONE);
634 SendSuperstructureGoal();
635}
636
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800637void AutonomousActor::Aim() {
Filip Kujawa1286c012024-03-31 22:53:27 -0700638 set_auto_aim(control_loops::superstructure::AutoAimMode::SPEAKER);
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800639 SendSuperstructureGoal();
640}
641
642void AutonomousActor::Shoot() {
643 set_fire(true);
644 SendSuperstructureGoal();
645}
646
James Kuszmaulb5f11832024-03-15 22:30:59 -0700647void AutonomousActor::StopFiring() {
648 set_fire(false);
649 SendSuperstructureGoal();
650}
651
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800652[[nodiscard]] bool AutonomousActor::WaitForPreloaded() {
653 set_preloaded(true);
654 SendSuperstructureGoal();
655
656 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
657 event_loop()->monotonic_now(),
658 aos::common::actions::kLoopOffset);
659
660 bool loaded = false;
661 while (!loaded) {
662 if (ShouldCancel()) {
663 return false;
664 }
665
666 phased_loop.SleepUntilNext();
667 superstructure_status_fetcher_.Fetch();
668 CHECK(superstructure_status_fetcher_.get() != nullptr);
669
670 loaded = (superstructure_status_fetcher_->shooter()->catapult_state() ==
671 control_loops::superstructure::CatapultState::LOADED);
672 }
673
674 set_preloaded(false);
675 SendSuperstructureGoal();
676
677 return true;
678}
679
James Kuszmaulb5f11832024-03-15 22:30:59 -0700680uint32_t AutonomousActor::shot_count() {
681 superstructure_status_fetcher_.Fetch();
682 return superstructure_status_fetcher_->shot_count();
683}
684
685[[nodiscard]] bool AutonomousActor::WaitForNoteFired(
686 uint32_t penultimate_target_shot_count, std::chrono::nanoseconds timeout) {
687 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
688 event_loop()->monotonic_now(),
689 aos::common::actions::kLoopOffset);
690 aos::monotonic_clock::time_point end_time =
691 event_loop()->monotonic_now() + timeout;
692 while (true) {
693 if (ShouldCancel()) {
694 return false;
695 }
696
697 phased_loop.SleepUntilNext();
698
699 if (shot_count() > penultimate_target_shot_count ||
700 event_loop()->monotonic_now() > end_time) {
701 return true;
702 }
703 }
704}
705
706[[nodiscard]] bool AutonomousActor::WaitForCatapultReady() {
707 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
708 event_loop()->monotonic_now(),
709 aos::common::actions::kLoopOffset);
710
711 bool loaded = false;
712 while (!loaded) {
713 if (ShouldCancel()) {
714 return false;
715 }
716
717 phased_loop.SleepUntilNext();
718 superstructure_status_fetcher_.Fetch();
719 CHECK(superstructure_status_fetcher_.get() != nullptr);
720
721 loaded = (superstructure_status_fetcher_->state() ==
722 control_loops::superstructure::SuperstructureState::READY);
723 }
724
725 SendSuperstructureGoal();
726
727 return true;
728}
729
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800730} // namespace y2024::autonomous