blob: 4afb9e4b63bd78b662f6fb48a28e153fa8c1bd99 [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
Austin Schuh99f7c6a2024-06-25 22:07:44 -07007#include "absl/flags/flag.h"
8
Niko Sohmers3860f8a2024-01-12 21:05:19 -08009#include "aos/logging/logging.h"
10#include "aos/util/math.h"
11#include "frc971/control_loops/drivetrain/localizer_generated.h"
12#include "y2024/autonomous/auto_splines.h"
13#include "y2024/constants.h"
14#include "y2024/control_loops/drivetrain/drivetrain_base.h"
15
Austin Schuh99f7c6a2024-06-25 22:07:44 -070016ABSL_FLAG(bool, spline_auto, false, "Run simple test S-spline auto mode.");
17ABSL_FLAG(bool, do_fifth_piece, true, "");
Niko Sohmers3860f8a2024-01-12 21:05:19 -080018
Stephan Pleinesf63bde82024-01-13 15:59:33 -080019namespace y2024::autonomous {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080020
21using ::frc971::ProfileParametersT;
22
23ProfileParametersT MakeProfileParameters(float max_velocity,
24 float max_acceleration) {
25 ProfileParametersT result;
26 result.max_velocity = max_velocity;
27 result.max_acceleration = max_acceleration;
28 return result;
29}
30
31using ::aos::monotonic_clock;
32using frc971::CreateProfileParameters;
33using ::frc971::ProfileParametersT;
34using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal;
35using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal;
36using frc971::control_loops::drivetrain::LocalizerControl;
37namespace chrono = ::std::chrono;
38
James Kuszmaulb5f11832024-03-15 22:30:59 -070039AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop,
40 const y2024::Constants *robot_constants)
Filip Kujawa58ffbf32024-02-24 18:28:34 -080041 : frc971::autonomous::UserButtonLocalizedAutonomousActor(
James Kuszmaul2549e752024-01-20 17:42:51 -080042 event_loop,
43 control_loops::drivetrain::GetDrivetrainConfig(event_loop)),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080044 localizer_control_sender_(
45 event_loop->MakeSender<
46 ::frc971::control_loops::drivetrain::LocalizerControl>(
47 "/drivetrain")),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080048 superstructure_goal_sender_(
Filip Kujawa58ffbf32024-02-24 18:28:34 -080049 event_loop
50 ->MakeSender<::y2024::control_loops::superstructure::GoalStatic>(
51 "/superstructure")),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080052 superstructure_status_fetcher_(
53 event_loop
54 ->MakeFetcher<::y2024::control_loops::superstructure::Status>(
Filip Kujawa58ffbf32024-02-24 18:28:34 -080055 "/superstructure")),
Austin Schuh6bdcc372024-06-27 14:49:11 -070056 robot_constants_(robot_constants),
57 auto_splines_() {
58 CHECK(robot_constants_ != nullptr);
59}
Niko Sohmers3860f8a2024-01-12 21:05:19 -080060
61void AutonomousActor::Replan() {
James Kuszmaulb5f11832024-03-15 22:30:59 -070062 AutonomousMode mode = robot_constants_->common()->autonomous_mode();
63 switch (mode) {
64 case AutonomousMode::NONE:
65 break;
66 case AutonomousMode::SPLINE_AUTO:
67 test_spline_ =
68 PlanSpline(std::bind(&AutonomousSplines::TestSpline, &auto_splines_,
69 std::placeholders::_1, alliance_),
70 SplineDirection::kForward);
Niko Sohmers3860f8a2024-01-12 21:05:19 -080071
James Kuszmaulb5f11832024-03-15 22:30:59 -070072 starting_position_ = test_spline_->starting_position();
73 break;
74 case AutonomousMode::MOBILITY_AND_SHOOT:
75 AOS_LOG(INFO, "Mobility & shoot replanning!");
76 mobility_and_shoot_splines_ = {PlanSpline(
77 std::bind(&AutonomousSplines::MobilityAndShootSpline, &auto_splines_,
78 std::placeholders::_1, alliance_),
79 SplineDirection::kForward)};
80
81 starting_position_ =
82 mobility_and_shoot_splines_.value()[0].starting_position();
83 CHECK(starting_position_);
84 break;
James Kuszmaul58f51432024-03-24 15:00:06 -070085 case AutonomousMode::FIVE_PIECE:
86 AOS_LOG(INFO, "FIVE_PIECE replanning!");
James Kuszmaulb5f11832024-03-15 22:30:59 -070087 four_piece_splines_ = {
88 PlanSpline(
89 std::bind(&AutonomousSplines::FourPieceSpline1, &auto_splines_,
90 std::placeholders::_1, alliance_),
91 SplineDirection::kForward),
92 PlanSpline(
93 std::bind(&AutonomousSplines::FourPieceSpline2, &auto_splines_,
94 std::placeholders::_1, alliance_),
95 SplineDirection::kBackward),
96 PlanSpline(
97 std::bind(&AutonomousSplines::FourPieceSpline3, &auto_splines_,
98 std::placeholders::_1, alliance_),
99 SplineDirection::kForward),
100 PlanSpline(
101 std::bind(&AutonomousSplines::FourPieceSpline4, &auto_splines_,
102 std::placeholders::_1, alliance_),
103 SplineDirection::kForward),
104 PlanSpline(
105 std::bind(&AutonomousSplines::FourPieceSpline5, &auto_splines_,
106 std::placeholders::_1, alliance_),
107 SplineDirection::kBackward)};
108
109 starting_position_ = four_piece_splines_.value()[0].starting_position();
110 CHECK(starting_position_);
111 break;
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700112 case AutonomousMode::TWO_PIECE_STEAL:
113 AOS_LOG(INFO, "TWO_PIECE_STEAL replanning!");
114 two_piece_steal_splines_ = {
115 PlanSpline(
116 std::bind(&AutonomousSplines::TwoPieceStealSpline1,
117 &auto_splines_, std::placeholders::_1, alliance_),
118 SplineDirection::kForward),
119 PlanSpline(
120 std::bind(&AutonomousSplines::TwoPieceStealSpline2,
121 &auto_splines_, std::placeholders::_1, alliance_),
122 SplineDirection::kBackward),
123 PlanSpline(
124 std::bind(&AutonomousSplines::TwoPieceStealSpline3,
125 &auto_splines_, std::placeholders::_1, alliance_),
126 SplineDirection::kForward),
127 PlanSpline(
128 std::bind(&AutonomousSplines::TwoPieceStealSpline4,
129 &auto_splines_, std::placeholders::_1, alliance_),
James Kuszmaul58f51432024-03-24 15:00:06 -0700130 SplineDirection::kBackward)};
131
132 starting_position_ =
133 two_piece_steal_splines_.value()[0].starting_position();
134 CHECK(starting_position_);
135 break;
James Kuszmaul337e3a52024-05-04 14:06:52 -0700136 case AutonomousMode::TWO_PIECE_VIA_STAGE:
137 AOS_LOG(INFO, "TWO_PIECE_VIA_STAGE replanning!");
138 two_piece_via_stage_splines_ = {
139 PlanSpline(
140 std::bind(&AutonomousSplines::TwoPieceViaStageSpline1,
141 &auto_splines_, std::placeholders::_1, alliance_),
142 SplineDirection::kForward),
143 PlanSpline(
144 std::bind(&AutonomousSplines::TwoPieceViaStageSpline2,
145 &auto_splines_, std::placeholders::_1, alliance_),
146 SplineDirection::kBackward),
147 PlanSpline(
148 std::bind(&AutonomousSplines::TwoPieceViaStageSpline3,
149 &auto_splines_, std::placeholders::_1, alliance_),
150 SplineDirection::kForward),
151 PlanSpline(
152 std::bind(&AutonomousSplines::TwoPieceViaStageSpline4,
153 &auto_splines_, std::placeholders::_1, alliance_),
154 SplineDirection::kBackward)};
155
156 starting_position_ =
157 two_piece_via_stage_splines_.value()[0].starting_position();
158 CHECK(starting_position_);
159 break;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800160 }
161
162 is_planned_ = true;
163
164 MaybeSendStartingPosition();
165}
166
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800167bool AutonomousActor::Run(
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800168 const ::frc971::autonomous::AutonomousActionParams *params) {
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800169 AOS_LOG(INFO, "Params are %d\n", params->mode());
170 if (alliance_ == aos::Alliance::kInvalid) {
171 AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection.");
172 return false;
173 }
James Kuszmaulb5f11832024-03-15 22:30:59 -0700174
175 AutonomousMode mode = robot_constants_->common()->autonomous_mode();
176 switch (mode) {
177 case AutonomousMode::NONE:
178 AOS_LOG(WARNING, "No auto mode selected.");
179 break;
180 case AutonomousMode::SPLINE_AUTO:
181 SplineAuto();
182 break;
183 case AutonomousMode::MOBILITY_AND_SHOOT:
184 MobilityAndShoot();
185 break;
James Kuszmaul58f51432024-03-24 15:00:06 -0700186 case AutonomousMode::FIVE_PIECE:
James Kuszmaulb5f11832024-03-15 22:30:59 -0700187 FourPieceAuto();
188 break;
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700189 case AutonomousMode::TWO_PIECE_STEAL:
190 TwoPieceStealAuto();
191 break;
James Kuszmaul337e3a52024-05-04 14:06:52 -0700192 case AutonomousMode::TWO_PIECE_VIA_STAGE:
193 TwoPieceViaStageAuto();
194 break;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800195 }
196 return true;
197}
198
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800199void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) {
200 // Set up the starting position for the blue alliance.
201
202 auto builder = localizer_control_sender_.MakeBuilder();
203
204 LocalizerControl::Builder localizer_control_builder =
205 builder.MakeBuilder<LocalizerControl>();
206 localizer_control_builder.add_x(start(0));
207 localizer_control_builder.add_y(start(1));
208 localizer_control_builder.add_theta(start(2));
209 localizer_control_builder.add_theta_uncertainty(0.00001);
210 AOS_LOG(INFO, "User button pressed, x: %f y: %f theta: %f", start(0),
211 start(1), start(2));
212 if (builder.Send(localizer_control_builder.Finish()) !=
213 aos::RawSender::Error::kOk) {
214 AOS_LOG(ERROR, "Failed to reset localizer.\n");
215 }
216}
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800217
218void AutonomousActor::Reset() {
219 set_intake_goal(control_loops::superstructure::IntakeGoal::NONE);
220 set_note_goal(control_loops::superstructure::NoteGoal::NONE);
Filip Kujawa1286c012024-03-31 22:53:27 -0700221 set_auto_aim(control_loops::superstructure::AutoAimMode::NONE);
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800222 set_fire(false);
223 set_preloaded(false);
224 SendSuperstructureGoal();
225}
226
227void AutonomousActor::SplineAuto() {
228 CHECK(test_spline_);
229
230 if (!test_spline_->WaitForPlan()) return;
231 test_spline_->Start();
232
233 if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return;
234}
235
James Kuszmaulb5f11832024-03-15 22:30:59 -0700236void AutonomousActor::MobilityAndShoot() {
237 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
238
239 uint32_t initial_shot_count = shot_count();
240
241 CHECK(mobility_and_shoot_splines_);
242
243 auto &splines = *mobility_and_shoot_splines_;
244
245 AOS_LOG(INFO, "Going to preload");
246
247 // Always be auto-aiming.
248 Aim();
249
250 if (!WaitForPreloaded()) return;
251
252 AOS_LOG(INFO, "Starting to Move");
253
254 if (!splines[0].WaitForPlan()) return;
255
256 splines[0].Start();
257
258 if (!splines[0].WaitForSplineDistanceRemaining(0.05)) return;
259
260 AOS_LOG(
261 INFO, "Got there %lf s\nNow Shooting\n",
262 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
263
264 Shoot();
265
266 if (!WaitForNoteFired(initial_shot_count, std::chrono::seconds(5))) return;
267
268 StopFiring();
269
270 AOS_LOG(
271 INFO, "Note fired at %lf seconds\n",
272 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
273}
274
275void AutonomousActor::FourPieceAuto() {
276 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
277
278 CHECK(four_piece_splines_);
279 auto &splines = *four_piece_splines_;
280
281 uint32_t initial_shot_count = shot_count();
282
283 // Always be aiming & firing.
284 Aim();
285 if (!WaitForPreloaded()) return;
286
James Kuszmaulb5f11832024-03-15 22:30:59 -0700287 Shoot();
288
289 AOS_LOG(
290 INFO, "Shooting Preloaded Note %lfs\n",
291 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
292
293 if (!WaitForNoteFired(initial_shot_count, std::chrono::seconds(2))) return;
294
295 AOS_LOG(
296 INFO, "Shot first note %lfs\n",
297 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
298
299 Intake();
300 StopFiring();
301
302 AOS_LOG(
303 INFO, "Starting Spline 1 %lfs\n",
304 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
305
306 if (!splines[0].WaitForPlan()) return;
307
308 splines[0].Start();
309
310 if (!splines[0].WaitForSplineDistanceRemaining(0.01)) return;
311
312 if (!splines[1].WaitForPlan()) return;
313
314 AOS_LOG(
315 INFO, "Starting second spline %lfs\n",
316 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
317
318 splines[1].Start();
319
320 if (!splines[1].WaitForSplineDistanceRemaining(0.01)) return;
321
322 AOS_LOG(
323 INFO, "Finished second spline %lfs\n",
324 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
325
James Kuszmaul58f51432024-03-24 15:00:06 -0700326 std::this_thread::sleep_for(chrono::milliseconds(200));
James Kuszmaulb5f11832024-03-15 22:30:59 -0700327
328 Shoot();
329
330 if (!WaitForNoteFired(initial_shot_count + 1, std::chrono::seconds(2)))
331 return;
332
333 AOS_LOG(
334 INFO, "Shot second note, starting drive %lfs\n",
335 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
336
337 if (!splines[2].WaitForPlan()) return;
338 splines[2].Start();
339
340 if (!splines[2].WaitForSplineDistanceRemaining(0.01)) return;
341
342 if (!WaitForNoteFired(initial_shot_count + 3, std::chrono::seconds(5)))
343 return;
344
345 AOS_LOG(
346 INFO, "Finished 4 notes at %lfs\n",
347 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
348
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700349 if (!absl::GetFlag(FLAGS_do_fifth_piece)) {
James Kuszmaulb5f11832024-03-15 22:30:59 -0700350 AOS_LOG(INFO, "Exitting early due to --nodo_fifth_piece");
351 return;
352 }
353
354 if (!splines[3].WaitForPlan()) return;
355 splines[3].Start();
356
357 if (!splines[3].WaitForSplineDistanceRemaining(0.1)) return;
358
359 AOS_LOG(
360 INFO, "Got to 5th note at %lfs\n",
361 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
362
363 StopFiring();
364
365 if (!splines[4].WaitForPlan()) return;
366 splines[4].Start();
367
James Kuszmaulf2f95b32024-03-23 20:12:05 -0700368 if (!splines[4].WaitForSplineDistanceRemaining(0.01)) return;
James Kuszmaulb5f11832024-03-15 22:30:59 -0700369
370 AOS_LOG(
371 INFO, "Done with spline %lfs\n",
372 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
373
James Kuszmaul58f51432024-03-24 15:00:06 -0700374 std::this_thread::sleep_for(chrono::milliseconds(250));
James Kuszmaulb5f11832024-03-15 22:30:59 -0700375
376 AOS_LOG(
377 INFO, "Shooting last note! %lfs\n",
378 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
379
380 Shoot();
381
382 if (!WaitForNoteFired(initial_shot_count + 4, std::chrono::seconds(5)))
383 return;
384
385 AOS_LOG(
386 INFO, "Done %lfs\n",
387 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
388}
389
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700390void AutonomousActor::TwoPieceStealAuto() {
391 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
392
393 CHECK(two_piece_steal_splines_);
394 auto &splines = *two_piece_steal_splines_;
395
396 uint32_t initial_shot_count = shot_count();
397
398 // Always be aiming & firing.
399 Aim();
400 if (!WaitForPreloaded()) return;
401
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700402 Shoot();
403
404 AOS_LOG(
405 INFO, "Shooting Preloaded Note %lfs\n",
406 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
407
408 if (!WaitForNoteFired(initial_shot_count, std::chrono::seconds(2))) return;
409
410 AOS_LOG(
411 INFO, "Shot first note %lfs\n",
412 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
413
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700414 StopFiring();
415
416 AOS_LOG(
417 INFO, "Starting Spline 1 %lfs\n",
418 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
419
420 if (!splines[0].WaitForPlan()) return;
421
422 splines[0].Start();
423
James Kuszmaul58f51432024-03-24 15:00:06 -0700424 if (!splines[0].WaitForSplineDistanceRemaining(2.0)) return;
425 Intake();
426
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700427 if (!splines[0].WaitForSplineDistanceRemaining(0.01)) return;
428
429 if (!splines[1].WaitForPlan()) return;
430
431 AOS_LOG(
432 INFO, "Starting second spline %lfs\n",
433 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
434
435 splines[1].Start();
436
437 if (!splines[1].WaitForSplineDistanceRemaining(0.01)) return;
438
439 AOS_LOG(
440 INFO, "Finished second spline %lfs\n",
441 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
442
James Kuszmaul58f51432024-03-24 15:00:06 -0700443 std::this_thread::sleep_for(chrono::milliseconds(200));
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700444
445 Shoot();
James Kuszmaul58f51432024-03-24 15:00:06 -0700446 StopIntake();
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700447
448 if (!WaitForNoteFired(initial_shot_count + 1, std::chrono::seconds(2)))
449 return;
450
451 StopFiring();
452
453 AOS_LOG(
454 INFO, "Shot second note, starting drive %lfs\n",
455 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
456
457 if (!splines[2].WaitForPlan()) return;
458
459 AOS_LOG(
460 INFO, "Starting third spline %lfs\n",
461 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
462 splines[2].Start();
463
James Kuszmaul58f51432024-03-24 15:00:06 -0700464 if (!splines[2].WaitForSplineDistanceRemaining(2.0)) return;
465
466 Intake();
467
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700468 if (!splines[2].WaitForSplineDistanceRemaining(0.01)) return;
469
470 if (!splines[3].WaitForPlan()) return;
471
472 AOS_LOG(
473 INFO, "Starting fourth spline %lfs\n",
474 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
475 splines[3].Start();
476
477 if (!splines[3].WaitForSplineDistanceRemaining(0.01)) return;
478
479 Shoot();
480
James Kuszmaul58f51432024-03-24 15:00:06 -0700481 std::this_thread::sleep_for(chrono::milliseconds(400));
482
Maxwell Hendersona2dadd02024-03-24 13:57:09 -0700483 if (!WaitForNoteFired(initial_shot_count + 2, std::chrono::seconds(2)))
484 return;
485
486 AOS_LOG(
487 INFO, "Done %lfs\n",
488 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
489}
490
James Kuszmaul337e3a52024-05-04 14:06:52 -0700491void AutonomousActor::TwoPieceViaStageAuto() {
492 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
493
494 CHECK(two_piece_via_stage_splines_);
495 auto &splines = *two_piece_via_stage_splines_;
496
497 uint32_t initial_shot_count = shot_count();
498
499 // Always be aiming & firing.
500 Aim();
501 if (!WaitForPreloaded()) return;
502
503 Shoot();
504
505 AOS_LOG(
506 INFO, "Shooting Preloaded Note %lfs\n",
507 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
508
509 if (!WaitForNoteFired(initial_shot_count, std::chrono::seconds(2))) return;
510 StopAiming();
511
512 AOS_LOG(
513 INFO, "Shot first note %lfs\n",
514 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
515
516 StopFiring();
517
518 AOS_LOG(
519 INFO, "Starting Spline 1 %lfs\n",
520 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
521
522 if (!splines[0].WaitForPlan()) return;
523
524 splines[0].Start();
525
526 if (!splines[0].WaitForSplineDistanceRemaining(2.0)) return;
527 Intake();
528
529 if (!splines[0].WaitForSplineDistanceRemaining(0.01)) return;
530
531 if (!splines[1].WaitForPlan()) return;
532
533 AOS_LOG(
534 INFO, "Starting second spline %lfs\n",
535 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
536
537 splines[1].Start();
538
539 if (!splines[1].WaitForSplineDistanceRemaining(0.01)) return;
540
541 Aim();
542
543 AOS_LOG(
544 INFO, "Finished second spline %lfs\n",
545 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
546
547 std::this_thread::sleep_for(chrono::milliseconds(1000));
548
549 Shoot();
550 StopIntake();
551
552 if (!WaitForNoteFired(initial_shot_count + 1, std::chrono::seconds(2)))
553 return;
554
555 StopFiring();
556 StopAiming();
557
558 AOS_LOG(
559 INFO, "Shot second note, starting drive %lfs\n",
560 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
561
562 if (!splines[2].WaitForPlan()) return;
563
564 AOS_LOG(
565 INFO, "Starting third spline %lfs\n",
566 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
567 splines[2].Start();
568
569 if (!splines[2].WaitForSplineDistanceRemaining(2.0)) return;
570
571 Intake();
572
573 if (!splines[2].WaitForSplineDistanceRemaining(0.01)) return;
574
575 if (!splines[3].WaitForPlan()) return;
576
577 AOS_LOG(
578 INFO, "Starting fourth spline %lfs\n",
579 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
580 splines[3].Start();
581
582 if (!splines[3].WaitForSplineDistanceRemaining(0.01)) return;
583
584 Aim();
585
586 Shoot();
587
588 std::this_thread::sleep_for(chrono::milliseconds(400));
589
590 if (!WaitForNoteFired(initial_shot_count + 2, std::chrono::seconds(2)))
591 return;
592
593 AOS_LOG(
594 INFO, "Done %lfs\n",
595 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
596}
597
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800598void AutonomousActor::SendSuperstructureGoal() {
599 aos::Sender<control_loops::superstructure::GoalStatic>::StaticBuilder
600 goal_builder = superstructure_goal_sender_.MakeStaticBuilder();
601
602 goal_builder->set_intake_goal(intake_goal_);
James Kuszmaulb5f11832024-03-15 22:30:59 -0700603 if (intake_goal_ == control_loops::superstructure::IntakeGoal::INTAKE) {
604 goal_builder->set_intake_pivot(
605 control_loops::superstructure::IntakePivotGoal::DOWN);
606 } else {
607 goal_builder->set_intake_pivot(
608 control_loops::superstructure::IntakePivotGoal::UP);
609 }
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800610 goal_builder->set_note_goal(note_goal_);
611 goal_builder->set_fire(fire_);
612
613 control_loops::superstructure::ShooterGoalStatic *shooter_goal =
614 goal_builder->add_shooter_goal();
615
616 shooter_goal->set_auto_aim(auto_aim_);
617 shooter_goal->set_preloaded(preloaded_);
618
619 goal_builder.CheckOk(goal_builder.Send());
620}
621
James Kuszmaul58f51432024-03-24 15:00:06 -0700622void AutonomousActor::StopIntake() {
623 set_intake_goal(control_loops::superstructure::IntakeGoal::NONE);
624 set_note_goal(control_loops::superstructure::NoteGoal::CATAPULT);
625 SendSuperstructureGoal();
626}
627
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800628void AutonomousActor::Intake() {
629 set_intake_goal(control_loops::superstructure::IntakeGoal::INTAKE);
630 set_note_goal(control_loops::superstructure::NoteGoal::CATAPULT);
631 SendSuperstructureGoal();
632}
633
James Kuszmaul337e3a52024-05-04 14:06:52 -0700634void AutonomousActor::StopAiming() {
635 set_auto_aim(control_loops::superstructure::AutoAimMode::NONE);
636 SendSuperstructureGoal();
637}
638
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800639void AutonomousActor::Aim() {
Filip Kujawa1286c012024-03-31 22:53:27 -0700640 set_auto_aim(control_loops::superstructure::AutoAimMode::SPEAKER);
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800641 SendSuperstructureGoal();
642}
643
644void AutonomousActor::Shoot() {
645 set_fire(true);
646 SendSuperstructureGoal();
647}
648
James Kuszmaulb5f11832024-03-15 22:30:59 -0700649void AutonomousActor::StopFiring() {
650 set_fire(false);
651 SendSuperstructureGoal();
652}
653
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800654[[nodiscard]] bool AutonomousActor::WaitForPreloaded() {
655 set_preloaded(true);
656 SendSuperstructureGoal();
657
658 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
659 event_loop()->monotonic_now(),
660 aos::common::actions::kLoopOffset);
661
662 bool loaded = false;
663 while (!loaded) {
664 if (ShouldCancel()) {
665 return false;
666 }
667
668 phased_loop.SleepUntilNext();
669 superstructure_status_fetcher_.Fetch();
670 CHECK(superstructure_status_fetcher_.get() != nullptr);
671
672 loaded = (superstructure_status_fetcher_->shooter()->catapult_state() ==
673 control_loops::superstructure::CatapultState::LOADED);
674 }
675
676 set_preloaded(false);
677 SendSuperstructureGoal();
678
679 return true;
680}
681
James Kuszmaulb5f11832024-03-15 22:30:59 -0700682uint32_t AutonomousActor::shot_count() {
683 superstructure_status_fetcher_.Fetch();
684 return superstructure_status_fetcher_->shot_count();
685}
686
687[[nodiscard]] bool AutonomousActor::WaitForNoteFired(
688 uint32_t penultimate_target_shot_count, std::chrono::nanoseconds timeout) {
689 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
690 event_loop()->monotonic_now(),
691 aos::common::actions::kLoopOffset);
692 aos::monotonic_clock::time_point end_time =
693 event_loop()->monotonic_now() + timeout;
694 while (true) {
695 if (ShouldCancel()) {
696 return false;
697 }
698
699 phased_loop.SleepUntilNext();
700
701 if (shot_count() > penultimate_target_shot_count ||
702 event_loop()->monotonic_now() > end_time) {
703 return true;
704 }
705 }
706}
707
708[[nodiscard]] bool AutonomousActor::WaitForCatapultReady() {
709 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
710 event_loop()->monotonic_now(),
711 aos::common::actions::kLoopOffset);
712
713 bool loaded = false;
714 while (!loaded) {
715 if (ShouldCancel()) {
716 return false;
717 }
718
719 phased_loop.SleepUntilNext();
720 superstructure_status_fetcher_.Fetch();
721 CHECK(superstructure_status_fetcher_.get() != nullptr);
722
723 loaded = (superstructure_status_fetcher_->state() ==
724 control_loops::superstructure::SuperstructureState::READY);
725 }
726
727 SendSuperstructureGoal();
728
729 return true;
730}
731
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800732} // namespace y2024::autonomous