blob: a23f693d053e12a0836012b04020ce0758f472d8 [file] [log] [blame]
Maxwell Hendersonad312342023-01-10 12:07:47 -08001#include "y2023/autonomous/autonomous_actor.h"
2
3#include <chrono>
4#include <cinttypes>
5#include <cmath>
6
7#include "aos/logging/logging.h"
Maxwell Henderson64f37452023-03-11 13:39:21 -08008#include "aos/util/math.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -08009#include "frc971/control_loops/drivetrain/localizer_generated.h"
Maxwell Henderson64f37452023-03-11 13:39:21 -080010#include "y2023/autonomous/auto_splines.h"
11#include "y2023/constants.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -080012#include "y2023/control_loops/drivetrain/drivetrain_base.h"
Maxwell Henderson64f37452023-03-11 13:39:21 -080013#include "y2023/control_loops/superstructure/arm/generated_graph.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -080014
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -070015DEFINE_bool(spline_auto, false, "Run simple test S-spline auto mode.");
Maxwell Henderson64f37452023-03-11 13:39:21 -080016DEFINE_bool(charged_up, true, "If true run charged up autonomous mode");
Maxwell Henderson25378832023-04-07 14:37:41 -070017DEFINE_bool(charged_up_cable, false, "If true run cable side autonomous mode");
James Kuszmaul6fc7ee22023-09-30 10:23:31 -070018DEFINE_bool(do_balance, true, "If true run the balance.");
James Kuszmaul713c5ce2023-03-04 18:23:24 -080019
Stephan Pleinesf63bde82024-01-13 15:59:33 -080020namespace y2023::autonomous {
Maxwell Hendersonad312342023-01-10 12:07:47 -080021
Maxwell Hendersonad312342023-01-10 12:07:47 -080022using ::frc971::ProfileParametersT;
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -070023
24ProfileParametersT MakeProfileParameters(float max_velocity,
25 float max_acceleration) {
26 ProfileParametersT result;
27 result.max_velocity = max_velocity;
28 result.max_acceleration = max_acceleration;
29 return result;
30}
31
32using ::aos::monotonic_clock;
33using frc971::CreateProfileParameters;
34using ::frc971::ProfileParametersT;
35using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal;
36using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal;
Maxwell Hendersonad312342023-01-10 12:07:47 -080037using frc971::control_loops::drivetrain::LocalizerControl;
38namespace chrono = ::std::chrono;
39
40AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
Maxwell Henderson12b07b92024-01-08 20:17:25 -080041 : frc971::autonomous::UserButtonLocalizedAutonomousActor(
James Kuszmaul713c5ce2023-03-04 18:23:24 -080042 event_loop, control_loops::drivetrain::GetDrivetrainConfig()),
43 localizer_control_sender_(
44 event_loop->MakeSender<
45 ::frc971::control_loops::drivetrain::LocalizerControl>(
46 "/drivetrain")),
Maxwell Henderson64f37452023-03-11 13:39:21 -080047 auto_splines_(),
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -070048 arm_goal_position_(control_loops::superstructure::arm::StartingIndex()),
49 superstructure_goal_sender_(
50 event_loop->MakeSender<::y2023::control_loops::superstructure::Goal>(
51 "/superstructure")),
52 superstructure_status_fetcher_(
53 event_loop
54 ->MakeFetcher<::y2023::control_loops::superstructure::Status>(
55 "/superstructure")),
Maxwell Henderson12b07b92024-01-08 20:17:25 -080056 points_(control_loops::superstructure::arm::PointList()) {}
James Kuszmaul713c5ce2023-03-04 18:23:24 -080057
58void AutonomousActor::Replan() {
James Kuszmaul713c5ce2023-03-04 18:23:24 -080059 if (FLAGS_spline_auto) {
60 test_spline_ =
61 PlanSpline(std::bind(&AutonomousSplines::TestSpline, &auto_splines_,
62 std::placeholders::_1, alliance_),
63 SplineDirection::kForward);
64
65 starting_position_ = test_spline_->starting_position();
Maxwell Henderson64f37452023-03-11 13:39:21 -080066 } else if (FLAGS_charged_up) {
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -070067 AOS_LOG(INFO, "Charged up replanning!");
Maxwell Henderson64f37452023-03-11 13:39:21 -080068 charged_up_splines_ = {
69 PlanSpline(std::bind(&AutonomousSplines::Spline1, &auto_splines_,
70 std::placeholders::_1, alliance_),
71 SplineDirection::kBackward),
72 PlanSpline(std::bind(&AutonomousSplines::Spline2, &auto_splines_,
73 std::placeholders::_1, alliance_),
74 SplineDirection::kForward),
75 PlanSpline(std::bind(&AutonomousSplines::Spline3, &auto_splines_,
76 std::placeholders::_1, alliance_),
77 SplineDirection::kBackward),
78 PlanSpline(std::bind(&AutonomousSplines::Spline4, &auto_splines_,
79 std::placeholders::_1, alliance_),
Maxwell Henderson25378832023-04-07 14:37:41 -070080 SplineDirection::kForward)};
Maxwell Henderson64f37452023-03-11 13:39:21 -080081
82 starting_position_ = charged_up_splines_.value()[0].starting_position();
83 CHECK(starting_position_);
Maxwell Henderson25378832023-04-07 14:37:41 -070084 } else if (FLAGS_charged_up_cable) {
85 charged_up_cable_splines_ = {
86 PlanSpline(std::bind(&AutonomousSplines::SplineCable1, &auto_splines_,
87 std::placeholders::_1, alliance_),
88 SplineDirection::kBackward),
89 PlanSpline(std::bind(&AutonomousSplines::SplineCable2, &auto_splines_,
90 std::placeholders::_1, alliance_),
91 SplineDirection::kForward),
92 PlanSpline(std::bind(&AutonomousSplines::SplineCable3, &auto_splines_,
93 std::placeholders::_1, alliance_),
94 SplineDirection::kBackward),
95 PlanSpline(std::bind(&AutonomousSplines::SplineCable4, &auto_splines_,
96 std::placeholders::_1, alliance_),
97 SplineDirection::kForward)};
98
99 starting_position_ =
100 charged_up_cable_splines_.value()[0].starting_position();
101 CHECK(starting_position_);
James Kuszmaul713c5ce2023-03-04 18:23:24 -0800102 }
103
104 is_planned_ = true;
105
106 MaybeSendStartingPosition();
107}
108
Maxwell Hendersonad312342023-01-10 12:07:47 -0800109void AutonomousActor::Reset() {
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700110 wrist_goal_ = 0.0;
111 roller_goal_ = control_loops::superstructure::RollerGoal::IDLE;
112 arm_goal_position_ = control_loops::superstructure::arm::StartingIndex();
113 preloaded_ = false;
114 SendSuperstructureGoal();
Maxwell Hendersonad312342023-01-10 12:07:47 -0800115}
116
Maxwell Henderson12b07b92024-01-08 20:17:25 -0800117bool AutonomousActor::Run(
Maxwell Hendersonad312342023-01-10 12:07:47 -0800118 const ::frc971::autonomous::AutonomousActionParams *params) {
James Kuszmaul713c5ce2023-03-04 18:23:24 -0800119 AOS_LOG(INFO, "Params are %d\n", params->mode());
120 if (alliance_ == aos::Alliance::kInvalid) {
121 AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection.");
122 return false;
123 }
124 if (FLAGS_spline_auto) {
125 SplineAuto();
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700126 } else if (FLAGS_charged_up) {
127 ChargedUp();
Austin Schuh8c9d2d02023-04-09 20:05:42 -0700128 } else if (FLAGS_charged_up_cable) {
129 ChargedUpCableSide();
James Kuszmaul713c5ce2023-03-04 18:23:24 -0800130 } else {
131 AOS_LOG(WARNING, "No auto mode selected.");
132 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800133 return true;
134}
135
James Kuszmaul713c5ce2023-03-04 18:23:24 -0800136void AutonomousActor::SplineAuto() {
137 CHECK(test_spline_);
138
139 if (!test_spline_->WaitForPlan()) return;
140 test_spline_->Start();
141
142 if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return;
143}
144
145void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) {
146 // Set up the starting position for the blue alliance.
147
148 auto builder = localizer_control_sender_.MakeBuilder();
149
150 LocalizerControl::Builder localizer_control_builder =
151 builder.MakeBuilder<LocalizerControl>();
152 localizer_control_builder.add_x(start(0));
153 localizer_control_builder.add_y(start(1));
154 localizer_control_builder.add_theta(start(2));
155 localizer_control_builder.add_theta_uncertainty(0.00001);
156 AOS_LOG(INFO, "User button pressed, x: %f y: %f theta: %f", start(0),
157 start(1), start(2));
158 if (builder.Send(localizer_control_builder.Finish()) !=
159 aos::RawSender::Error::kOk) {
160 AOS_LOG(ERROR, "Failed to reset localizer.\n");
161 }
162}
163
Maxwell Henderson25378832023-04-07 14:37:41 -0700164// Charged Up 3 Game Object Autonomous (non-cable side)
Maxwell Henderson64f37452023-03-11 13:39:21 -0800165void AutonomousActor::ChargedUp() {
166 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
167
168 CHECK(charged_up_splines_);
169
170 auto &splines = *charged_up_splines_;
171
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700172 AOS_LOG(INFO, "Going to preload");
173
Maxwell Henderson64f37452023-03-11 13:39:21 -0800174 // Tell the superstructure a cone was preloaded
175 if (!WaitForPreloaded()) return;
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700176 AOS_LOG(INFO, "Moving arm");
Maxwell Henderson64f37452023-03-11 13:39:21 -0800177
178 // Place first cone on mid level
179 MidConeScore();
180
181 // Wait until the arm is at the goal to spit
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700182 if (!WaitForArmGoal(0.10)) return;
Maxwell Henderson64f37452023-03-11 13:39:21 -0800183 Spit();
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700184 if (!WaitForArmGoal(0.01)) return;
185
186 std::this_thread::sleep_for(chrono::milliseconds(100));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800187
188 AOS_LOG(
189 INFO, "Placed first cone %lf s\n",
190 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
191
192 // Drive and intake the cube nearest to the starting zone
193 if (!splines[0].WaitForPlan()) return;
194 splines[0].Start();
195
196 // Move arm into position to pickup a cube and start cube intake
197 PickupCube();
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700198
199 std::this_thread::sleep_for(chrono::milliseconds(500));
200
Maxwell Henderson64f37452023-03-11 13:39:21 -0800201 IntakeCube();
202
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700203 AOS_LOG(
204 INFO, "Turning on rollers %lf s\n",
205 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
206
Maxwell Henderson64f37452023-03-11 13:39:21 -0800207 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
208
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700209 AOS_LOG(
210 INFO, "Got there %lf s\n",
211 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
212
Maxwell Henderson64f37452023-03-11 13:39:21 -0800213 // Drive back to grid and place cube on high level
214 if (!splines[1].WaitForPlan()) return;
215 splines[1].Start();
216
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700217 std::this_thread::sleep_for(chrono::milliseconds(300));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800218 HighCubeScore();
219
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700220 if (!splines[1].WaitForSplineDistanceRemaining(0.08)) return;
221 AOS_LOG(
222 INFO, "Back for first cube %lf s\n",
223 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800224
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700225 if (!WaitForArmGoal(0.10)) return;
226
227 AOS_LOG(
228 INFO, "Arm in place for first cube %lf s\n",
229 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
230
Maxwell Henderson64f37452023-03-11 13:39:21 -0800231 Spit();
232
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700233 if (!splines[1].WaitForSplineDistanceRemaining(0.08)) return;
234
235 AOS_LOG(
236 INFO, "Finished spline back %lf s\n",
237 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
238
239 if (!WaitForArmGoal(0.05)) return;
240
Austin Schuha7c3bc62023-04-16 19:46:23 -0700241 std::this_thread::sleep_for(chrono::milliseconds(100));
242
Maxwell Henderson64f37452023-03-11 13:39:21 -0800243 AOS_LOG(
244 INFO, "Placed first cube %lf s\n",
245 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
246
247 // Drive and intake the cube second nearest to the starting zone
248 if (!splines[2].WaitForPlan()) return;
249 splines[2].Start();
250
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700251 std::this_thread::sleep_for(chrono::milliseconds(200));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800252 PickupCube();
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700253
254 std::this_thread::sleep_for(chrono::milliseconds(500));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800255 IntakeCube();
256
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700257 if (!splines[2].WaitForSplineDistanceRemaining(0.05)) return;
258 AOS_LOG(
259 INFO, "Picked up second cube %lf s\n",
260 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800261
262 // Drive back to grid and place object on mid level
263 if (!splines[3].WaitForPlan()) return;
264 splines[3].Start();
265
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700266 AOS_LOG(
267 INFO, "Driving back %lf s\n",
268 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
269
Maxwell Henderson64f37452023-03-11 13:39:21 -0800270 MidCubeScore();
271
Austin Schuh700bfff2023-04-05 19:45:55 -0700272 if (!splines[3].WaitForSplineDistanceRemaining(0.07)) return;
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700273 AOS_LOG(
274 INFO, "Got back from second cube at %lf s\n",
275 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800276
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700277 if (!WaitForArmGoal(0.05)) return;
Maxwell Henderson64f37452023-03-11 13:39:21 -0800278 Spit();
279
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700280 if (!splines[3].WaitForSplineDistanceRemaining(0.02)) return;
281
Maxwell Henderson64f37452023-03-11 13:39:21 -0800282 AOS_LOG(
283 INFO, "Placed second cube %lf s\n",
284 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
James Kuszmaul6fc7ee22023-09-30 10:23:31 -0700285
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700286 InitializeEncoders();
Maxwell Henderson64f37452023-03-11 13:39:21 -0800287
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700288 const ProfileParametersT kDrive = MakeProfileParameters(2.0, 4.0);
289 const ProfileParametersT kTurn = MakeProfileParameters(3.0, 4.5);
290 StartDrive(0.0, 0.0, kDrive, kTurn);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800291
Austin Schuh700bfff2023-04-05 19:45:55 -0700292 std::this_thread::sleep_for(chrono::milliseconds(100));
293
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700294 {
295 double side_scalar = (alliance_ == aos::Alliance::kRed) ? 1.0 : -1.0;
296 StartDrive(6.33 - std::abs(X()), 0.0, kDrive, kTurn);
297 if (!WaitForDriveProfileNear(0.01)) return;
298
299 AOS_LOG(
300 INFO, "Done backing up %lf s\n",
301 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
302
James Kuszmaul6fc7ee22023-09-30 10:23:31 -0700303 if (!FLAGS_do_balance) {
304 StopSpitting();
305 return;
306 }
307
Austin Schuha7c3bc62023-04-16 19:46:23 -0700308 const ProfileParametersT kInPlaceTurn = MakeProfileParameters(2.7, 8.0);
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700309 StartDrive(0.0, aos::math::NormalizeAngle(M_PI / 2.0 - Theta()), kDrive,
310 kInPlaceTurn);
311
312 std::this_thread::sleep_for(chrono::milliseconds(400));
313 StopSpitting();
314
315 AOS_LOG(
316 INFO, "Roller off %lf s\n",
317 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
318
Austin Schuh700bfff2023-04-05 19:45:55 -0700319 if (!WaitForTurnProfileNear(0.6)) return;
320 AOS_LOG(
321 INFO, "Balance arm %lf s\n",
322 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
323
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700324 Balance();
Austin Schuh700bfff2023-04-05 19:45:55 -0700325 if (!WaitForTurnProfileNear(0.001)) return;
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700326
327 AOS_LOG(
328 INFO, "Done turning %lf s\n",
329 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
330
331 const ProfileParametersT kDrive = MakeProfileParameters(1.4, 3.0);
332 const ProfileParametersT kFinalTurn = MakeProfileParameters(3.0, 4.5);
Austin Schuh700bfff2023-04-05 19:45:55 -0700333 const double kDriveDistance = 3.11;
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700334 StartDrive(-kDriveDistance, 0.0, kDrive, kFinalTurn);
335
336 const ProfileParametersT kFastTurn = MakeProfileParameters(5.0, 8.0);
337 if (!WaitForDriveProfileNear(kDriveDistance - 0.4)) return;
338
339 AOS_LOG(
340 INFO, "Turning %lf s\n",
341 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
342 StartDrive(0.0, -side_scalar * M_PI / 2.0, kDrive, kFastTurn);
343 if (!WaitForDriveProfileDone()) return;
344 if (!WaitForTurnProfileDone()) return;
345 AOS_LOG(
346 INFO, "Done %lf s\n",
347 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
348 }
Maxwell Henderson64f37452023-03-11 13:39:21 -0800349}
350
Maxwell Henderson25378832023-04-07 14:37:41 -0700351// Charged Up 3 Game Object Autonomous (cable side)
352void AutonomousActor::ChargedUpCableSide() {
353 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
354
355 CHECK(charged_up_cable_splines_);
356
357 auto &splines = *charged_up_cable_splines_;
358
359 AOS_LOG(INFO, "Going to preload");
360
361 // Tell the superstructure a cone was preloaded
362 if (!WaitForPreloaded()) return;
363 AOS_LOG(INFO, "Moving arm");
364
365 // Place first cone on mid level
366 MidConeScore();
367
368 // Wait until the arm is at the goal to spit
369 if (!WaitForArmGoal(0.10)) return;
370 Spit();
371 if (!WaitForArmGoal(0.01)) return;
372
373 std::this_thread::sleep_for(chrono::milliseconds(100));
374
375 AOS_LOG(
376 INFO, "Placed first cone %lf s\n",
377 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
378
379 // Drive and intake the cube nearest to the starting zone
380 if (!splines[0].WaitForPlan()) return;
381 splines[0].Start();
382
383 // Move arm into position to pickup a cube and start cube intake
384 PickupCube();
385
386 std::this_thread::sleep_for(chrono::milliseconds(500));
387
388 IntakeCube();
389
390 AOS_LOG(
391 INFO, "Turning on rollers %lf s\n",
392 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
393
394 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
395
396 AOS_LOG(
397 INFO, "Got there %lf s\n",
398 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
399
400 // Drive back to grid and place cube on high level
401 if (!splines[1].WaitForPlan()) return;
402 splines[1].Start();
403
404 std::this_thread::sleep_for(chrono::milliseconds(300));
Austin Schuhdd128822023-04-09 22:19:06 -0700405 Neutral();
406
407 if (!splines[1].WaitForSplineDistanceTraveled(3.2)) return;
Maxwell Henderson25378832023-04-07 14:37:41 -0700408 HighCubeScore();
Austin Schuhdd128822023-04-09 22:19:06 -0700409 AOS_LOG(
410 INFO, "Extending arm %lf s\n",
411 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
Maxwell Henderson25378832023-04-07 14:37:41 -0700412
413 if (!splines[1].WaitForSplineDistanceRemaining(0.08)) return;
414 AOS_LOG(
415 INFO, "Back for first cube %lf s\n",
416 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
417
418 if (!WaitForArmGoal(0.10)) return;
419
420 AOS_LOG(
421 INFO, "Arm in place for first cube %lf s\n",
422 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
423
424 Spit();
425
426 if (!splines[1].WaitForSplineDistanceRemaining(0.08)) return;
427
428 AOS_LOG(
429 INFO, "Finished spline back %lf s\n",
430 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
431
432 if (!WaitForArmGoal(0.05)) return;
433
434 AOS_LOG(
435 INFO, "Placed first cube %lf s\n",
436 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
437
438 // Drive and intake the cube second nearest to the starting zone
439 if (!splines[2].WaitForPlan()) return;
440 splines[2].Start();
441
442 std::this_thread::sleep_for(chrono::milliseconds(200));
443 PickupCube();
444
445 std::this_thread::sleep_for(chrono::milliseconds(500));
446 IntakeCube();
447
448 if (!splines[2].WaitForSplineDistanceRemaining(0.05)) return;
449 AOS_LOG(
450 INFO, "Picked up second cube %lf s\n",
451 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
452
453 // Drive back to grid and place object on mid level
454 if (!splines[3].WaitForPlan()) return;
455 splines[3].Start();
456
Austin Schuhdd128822023-04-09 22:19:06 -0700457 std::this_thread::sleep_for(chrono::milliseconds(400));
458 Neutral();
459
Maxwell Henderson25378832023-04-07 14:37:41 -0700460 AOS_LOG(
461 INFO, "Driving back %lf s\n",
462 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
463
Austin Schuhdd128822023-04-09 22:19:06 -0700464 if (!splines[3].WaitForSplineDistanceTraveled(3.5)) return;
465 AOS_LOG(
466 INFO, "Extending arm %lf s\n",
467 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
Maxwell Henderson25378832023-04-07 14:37:41 -0700468 MidCubeScore();
469
470 if (!splines[3].WaitForSplineDistanceRemaining(0.07)) return;
471 AOS_LOG(
472 INFO, "Got back from second cube at %lf s\n",
473 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
474
475 if (!WaitForArmGoal(0.05)) return;
476 Spit();
477
478 if (!splines[3].WaitForSplineDistanceRemaining(0.02)) return;
479
480 AOS_LOG(
481 INFO, "Placed second cube %lf s\n",
482 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
Austin Schuh8c9d2d02023-04-09 20:05:42 -0700483
484 std::this_thread::sleep_for(chrono::milliseconds(200));
485 Neutral();
Austin Schuha7c3bc62023-04-16 19:46:23 -0700486 AOS_LOG(
487 INFO, "Going to neutral %lf s\n",
488 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
489
490 if (!WaitForArmGoal(0.05)) return;
491 AOS_LOG(
492 INFO, "Done at neutral %lf s\n",
493 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
Maxwell Henderson25378832023-04-07 14:37:41 -0700494}
495
Maxwell Henderson64f37452023-03-11 13:39:21 -0800496void AutonomousActor::SendSuperstructureGoal() {
497 auto builder = superstructure_goal_sender_.MakeBuilder();
498
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700499 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
500 wrist_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
501 *builder.fbb(), wrist_goal_,
502 CreateProfileParameters(*builder.fbb(), 12.0, 90.0));
503
Maxwell Henderson64f37452023-03-11 13:39:21 -0800504 control_loops::superstructure::Goal::Builder superstructure_builder =
505 builder.MakeBuilder<control_loops::superstructure::Goal>();
506
507 superstructure_builder.add_arm_goal_position(arm_goal_position_);
508 superstructure_builder.add_preloaded_with_cone(preloaded_);
509 superstructure_builder.add_roller_goal(roller_goal_);
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700510 superstructure_builder.add_wrist(wrist_offset);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800511
512 if (builder.Send(superstructure_builder.Finish()) !=
513 aos::RawSender::Error::kOk) {
514 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
515 }
516}
517
518[[nodiscard]] bool AutonomousActor::WaitForPreloaded() {
519 set_preloaded(true);
520 SendSuperstructureGoal();
521
522 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
523 event_loop()->monotonic_now(),
Stephan Pleines743f83a2024-02-02 18:32:09 -0800524 aos::common::actions::kLoopOffset);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800525
526 bool loaded = false;
527 while (!loaded) {
528 if (ShouldCancel()) {
529 return false;
530 }
531
532 phased_loop.SleepUntilNext();
533 superstructure_status_fetcher_.Fetch();
534 CHECK(superstructure_status_fetcher_.get() != nullptr);
535
536 loaded = (superstructure_status_fetcher_->end_effector_state() ==
537 control_loops::superstructure::EndEffectorState::LOADED);
538 }
539
540 set_preloaded(false);
541 SendSuperstructureGoal();
542
543 return true;
544}
545
546void AutonomousActor::MidConeScore() {
547 set_arm_goal_position(
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700548 control_loops::superstructure::arm::ScoreFrontMidConeUpAutoIndex());
549 set_wrist_goal(0.0);
550 SendSuperstructureGoal();
551}
552
553void AutonomousActor::Neutral() {
554 set_arm_goal_position(control_loops::superstructure::arm::NeutralIndex());
Austin Schuha7c3bc62023-04-16 19:46:23 -0700555 set_wrist_goal(1.0);
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700556 SendSuperstructureGoal();
557}
558
559void AutonomousActor::Balance() {
560 set_arm_goal_position(
Austin Schuh700bfff2023-04-05 19:45:55 -0700561 control_loops::superstructure::arm::ScoreFrontLowCubeIndex());
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700562 set_wrist_goal(0.0);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800563 SendSuperstructureGoal();
564}
565
566void AutonomousActor::HighCubeScore() {
567 set_arm_goal_position(
568 control_loops::superstructure::arm::ScoreFrontHighCubeIndex());
Austin Schuha7c3bc62023-04-16 19:46:23 -0700569 set_wrist_goal(1.0);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800570 SendSuperstructureGoal();
571}
572
573void AutonomousActor::MidCubeScore() {
574 set_arm_goal_position(
575 control_loops::superstructure::arm::ScoreFrontMidCubeIndex());
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700576 set_wrist_goal(1.0);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800577 SendSuperstructureGoal();
578}
579
580void AutonomousActor::PickupCube() {
581 set_arm_goal_position(
582 control_loops::superstructure::arm::GroundPickupBackCubeIndex());
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700583 set_wrist_goal(1.0);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800584 SendSuperstructureGoal();
585}
586
587void AutonomousActor::Spit() {
588 set_roller_goal(control_loops::superstructure::RollerGoal::SPIT);
589 SendSuperstructureGoal();
590}
591
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700592void AutonomousActor::StopSpitting() {
593 set_roller_goal(control_loops::superstructure::RollerGoal::IDLE);
594 SendSuperstructureGoal();
595}
596
Maxwell Henderson64f37452023-03-11 13:39:21 -0800597void AutonomousActor::IntakeCube() {
598 set_roller_goal(control_loops::superstructure::RollerGoal::INTAKE_CUBE);
599 SendSuperstructureGoal();
600}
601
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700602[[nodiscard]] bool AutonomousActor::WaitForArmGoal(double distance_to_go) {
603 constexpr double kEpsTheta = 0.10;
Maxwell Henderson64f37452023-03-11 13:39:21 -0800604
605 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
606 event_loop()->monotonic_now(),
Stephan Pleines743f83a2024-02-02 18:32:09 -0800607 aos::common::actions::kLoopOffset);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800608
609 bool at_goal = false;
610 while (!at_goal) {
611 if (ShouldCancel()) {
612 return false;
613 }
614
615 phased_loop.SleepUntilNext();
616 superstructure_status_fetcher_.Fetch();
617 CHECK(superstructure_status_fetcher_.get() != nullptr);
618
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700619 at_goal = (arm_goal_position_ ==
620 superstructure_status_fetcher_->arm()->current_node() &&
621 superstructure_status_fetcher_->arm()->path_distance_to_go() <
622 distance_to_go) &&
Maxwell Henderson64f37452023-03-11 13:39:21 -0800623 (std::abs(wrist_goal_ -
624 superstructure_status_fetcher_->wrist()->position()) <
625 kEpsTheta);
626 }
627
Maxwell Henderson64f37452023-03-11 13:39:21 -0800628 return true;
629}
630
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800631} // namespace y2023::autonomous