blob: 4583c6cc3cdb47525a077329b07a80d8373dc8a7 [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
Austin Schuh99f7c6a2024-06-25 22:07:44 -07007#include "absl/flags/flag.h"
8
Maxwell Hendersonad312342023-01-10 12:07:47 -08009#include "aos/logging/logging.h"
Maxwell Henderson64f37452023-03-11 13:39:21 -080010#include "aos/util/math.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -080011#include "frc971/control_loops/drivetrain/localizer_generated.h"
Maxwell Henderson64f37452023-03-11 13:39:21 -080012#include "y2023/autonomous/auto_splines.h"
13#include "y2023/constants.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -080014#include "y2023/control_loops/drivetrain/drivetrain_base.h"
Maxwell Henderson64f37452023-03-11 13:39:21 -080015#include "y2023/control_loops/superstructure/arm/generated_graph.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -080016
Austin Schuh99f7c6a2024-06-25 22:07:44 -070017ABSL_FLAG(bool, spline_auto, false, "Run simple test S-spline auto mode.");
18ABSL_FLAG(bool, charged_up, true, "If true run charged up autonomous mode");
19ABSL_FLAG(bool, charged_up_cable, false,
20 "If true run cable side autonomous mode");
21ABSL_FLAG(bool, do_balance, true, "If true run the balance.");
James Kuszmaul713c5ce2023-03-04 18:23:24 -080022
Stephan Pleinesf63bde82024-01-13 15:59:33 -080023namespace y2023::autonomous {
Maxwell Hendersonad312342023-01-10 12:07:47 -080024
Maxwell Hendersonad312342023-01-10 12:07:47 -080025using ::frc971::ProfileParametersT;
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -070026
27ProfileParametersT MakeProfileParameters(float max_velocity,
28 float max_acceleration) {
29 ProfileParametersT result;
30 result.max_velocity = max_velocity;
31 result.max_acceleration = max_acceleration;
32 return result;
33}
34
35using ::aos::monotonic_clock;
36using frc971::CreateProfileParameters;
37using ::frc971::ProfileParametersT;
38using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal;
39using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal;
Maxwell Hendersonad312342023-01-10 12:07:47 -080040using frc971::control_loops::drivetrain::LocalizerControl;
41namespace chrono = ::std::chrono;
42
43AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
Maxwell Henderson12b07b92024-01-08 20:17:25 -080044 : frc971::autonomous::UserButtonLocalizedAutonomousActor(
James Kuszmaul713c5ce2023-03-04 18:23:24 -080045 event_loop, control_loops::drivetrain::GetDrivetrainConfig()),
46 localizer_control_sender_(
47 event_loop->MakeSender<
48 ::frc971::control_loops::drivetrain::LocalizerControl>(
49 "/drivetrain")),
Maxwell Henderson64f37452023-03-11 13:39:21 -080050 auto_splines_(),
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -070051 arm_goal_position_(control_loops::superstructure::arm::StartingIndex()),
52 superstructure_goal_sender_(
53 event_loop->MakeSender<::y2023::control_loops::superstructure::Goal>(
54 "/superstructure")),
55 superstructure_status_fetcher_(
56 event_loop
57 ->MakeFetcher<::y2023::control_loops::superstructure::Status>(
58 "/superstructure")),
Maxwell Henderson12b07b92024-01-08 20:17:25 -080059 points_(control_loops::superstructure::arm::PointList()) {}
James Kuszmaul713c5ce2023-03-04 18:23:24 -080060
61void AutonomousActor::Replan() {
Austin Schuh99f7c6a2024-06-25 22:07:44 -070062 if (absl::GetFlag(FLAGS_spline_auto)) {
James Kuszmaul713c5ce2023-03-04 18:23:24 -080063 test_spline_ =
64 PlanSpline(std::bind(&AutonomousSplines::TestSpline, &auto_splines_,
65 std::placeholders::_1, alliance_),
66 SplineDirection::kForward);
67
68 starting_position_ = test_spline_->starting_position();
Austin Schuh99f7c6a2024-06-25 22:07:44 -070069 } else if (absl::GetFlag(FLAGS_charged_up)) {
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -070070 AOS_LOG(INFO, "Charged up replanning!");
Maxwell Henderson64f37452023-03-11 13:39:21 -080071 charged_up_splines_ = {
72 PlanSpline(std::bind(&AutonomousSplines::Spline1, &auto_splines_,
73 std::placeholders::_1, alliance_),
74 SplineDirection::kBackward),
75 PlanSpline(std::bind(&AutonomousSplines::Spline2, &auto_splines_,
76 std::placeholders::_1, alliance_),
77 SplineDirection::kForward),
78 PlanSpline(std::bind(&AutonomousSplines::Spline3, &auto_splines_,
79 std::placeholders::_1, alliance_),
80 SplineDirection::kBackward),
81 PlanSpline(std::bind(&AutonomousSplines::Spline4, &auto_splines_,
82 std::placeholders::_1, alliance_),
Maxwell Henderson25378832023-04-07 14:37:41 -070083 SplineDirection::kForward)};
Maxwell Henderson64f37452023-03-11 13:39:21 -080084
85 starting_position_ = charged_up_splines_.value()[0].starting_position();
86 CHECK(starting_position_);
Austin Schuh99f7c6a2024-06-25 22:07:44 -070087 } else if (absl::GetFlag(FLAGS_charged_up_cable)) {
Maxwell Henderson25378832023-04-07 14:37:41 -070088 charged_up_cable_splines_ = {
89 PlanSpline(std::bind(&AutonomousSplines::SplineCable1, &auto_splines_,
90 std::placeholders::_1, alliance_),
91 SplineDirection::kBackward),
92 PlanSpline(std::bind(&AutonomousSplines::SplineCable2, &auto_splines_,
93 std::placeholders::_1, alliance_),
94 SplineDirection::kForward),
95 PlanSpline(std::bind(&AutonomousSplines::SplineCable3, &auto_splines_,
96 std::placeholders::_1, alliance_),
97 SplineDirection::kBackward),
98 PlanSpline(std::bind(&AutonomousSplines::SplineCable4, &auto_splines_,
99 std::placeholders::_1, alliance_),
100 SplineDirection::kForward)};
101
102 starting_position_ =
103 charged_up_cable_splines_.value()[0].starting_position();
104 CHECK(starting_position_);
James Kuszmaul713c5ce2023-03-04 18:23:24 -0800105 }
106
107 is_planned_ = true;
108
109 MaybeSendStartingPosition();
110}
111
Maxwell Hendersonad312342023-01-10 12:07:47 -0800112void AutonomousActor::Reset() {
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700113 wrist_goal_ = 0.0;
114 roller_goal_ = control_loops::superstructure::RollerGoal::IDLE;
115 arm_goal_position_ = control_loops::superstructure::arm::StartingIndex();
116 preloaded_ = false;
117 SendSuperstructureGoal();
Maxwell Hendersonad312342023-01-10 12:07:47 -0800118}
119
Maxwell Henderson12b07b92024-01-08 20:17:25 -0800120bool AutonomousActor::Run(
Maxwell Hendersonad312342023-01-10 12:07:47 -0800121 const ::frc971::autonomous::AutonomousActionParams *params) {
James Kuszmaul713c5ce2023-03-04 18:23:24 -0800122 AOS_LOG(INFO, "Params are %d\n", params->mode());
123 if (alliance_ == aos::Alliance::kInvalid) {
124 AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection.");
125 return false;
126 }
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700127 if (absl::GetFlag(FLAGS_spline_auto)) {
James Kuszmaul713c5ce2023-03-04 18:23:24 -0800128 SplineAuto();
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700129 } else if (absl::GetFlag(FLAGS_charged_up)) {
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700130 ChargedUp();
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700131 } else if (absl::GetFlag(FLAGS_charged_up_cable)) {
Austin Schuh8c9d2d02023-04-09 20:05:42 -0700132 ChargedUpCableSide();
James Kuszmaul713c5ce2023-03-04 18:23:24 -0800133 } else {
134 AOS_LOG(WARNING, "No auto mode selected.");
135 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800136 return true;
137}
138
James Kuszmaul713c5ce2023-03-04 18:23:24 -0800139void AutonomousActor::SplineAuto() {
140 CHECK(test_spline_);
141
142 if (!test_spline_->WaitForPlan()) return;
143 test_spline_->Start();
144
145 if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return;
146}
147
148void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) {
149 // Set up the starting position for the blue alliance.
150
151 auto builder = localizer_control_sender_.MakeBuilder();
152
153 LocalizerControl::Builder localizer_control_builder =
154 builder.MakeBuilder<LocalizerControl>();
155 localizer_control_builder.add_x(start(0));
156 localizer_control_builder.add_y(start(1));
157 localizer_control_builder.add_theta(start(2));
158 localizer_control_builder.add_theta_uncertainty(0.00001);
159 AOS_LOG(INFO, "User button pressed, x: %f y: %f theta: %f", start(0),
160 start(1), start(2));
161 if (builder.Send(localizer_control_builder.Finish()) !=
162 aos::RawSender::Error::kOk) {
163 AOS_LOG(ERROR, "Failed to reset localizer.\n");
164 }
165}
166
Maxwell Henderson25378832023-04-07 14:37:41 -0700167// Charged Up 3 Game Object Autonomous (non-cable side)
Maxwell Henderson64f37452023-03-11 13:39:21 -0800168void AutonomousActor::ChargedUp() {
169 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
170
171 CHECK(charged_up_splines_);
172
173 auto &splines = *charged_up_splines_;
174
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700175 AOS_LOG(INFO, "Going to preload");
176
Maxwell Henderson64f37452023-03-11 13:39:21 -0800177 // Tell the superstructure a cone was preloaded
178 if (!WaitForPreloaded()) return;
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700179 AOS_LOG(INFO, "Moving arm");
Maxwell Henderson64f37452023-03-11 13:39:21 -0800180
181 // Place first cone on mid level
182 MidConeScore();
183
184 // Wait until the arm is at the goal to spit
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700185 if (!WaitForArmGoal(0.10)) return;
Maxwell Henderson64f37452023-03-11 13:39:21 -0800186 Spit();
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700187 if (!WaitForArmGoal(0.01)) return;
188
189 std::this_thread::sleep_for(chrono::milliseconds(100));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800190
191 AOS_LOG(
192 INFO, "Placed first cone %lf s\n",
193 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
194
195 // Drive and intake the cube nearest to the starting zone
196 if (!splines[0].WaitForPlan()) return;
197 splines[0].Start();
198
199 // Move arm into position to pickup a cube and start cube intake
200 PickupCube();
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700201
202 std::this_thread::sleep_for(chrono::milliseconds(500));
203
Maxwell Henderson64f37452023-03-11 13:39:21 -0800204 IntakeCube();
205
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700206 AOS_LOG(
207 INFO, "Turning on rollers %lf s\n",
208 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
209
Maxwell Henderson64f37452023-03-11 13:39:21 -0800210 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
211
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700212 AOS_LOG(
213 INFO, "Got there %lf s\n",
214 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
215
Maxwell Henderson64f37452023-03-11 13:39:21 -0800216 // Drive back to grid and place cube on high level
217 if (!splines[1].WaitForPlan()) return;
218 splines[1].Start();
219
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700220 std::this_thread::sleep_for(chrono::milliseconds(300));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800221 HighCubeScore();
222
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700223 if (!splines[1].WaitForSplineDistanceRemaining(0.08)) return;
224 AOS_LOG(
225 INFO, "Back for first cube %lf s\n",
226 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800227
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700228 if (!WaitForArmGoal(0.10)) return;
229
230 AOS_LOG(
231 INFO, "Arm in place for first cube %lf s\n",
232 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
233
Maxwell Henderson64f37452023-03-11 13:39:21 -0800234 Spit();
235
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700236 if (!splines[1].WaitForSplineDistanceRemaining(0.08)) return;
237
238 AOS_LOG(
239 INFO, "Finished spline back %lf s\n",
240 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
241
242 if (!WaitForArmGoal(0.05)) return;
243
Austin Schuha7c3bc62023-04-16 19:46:23 -0700244 std::this_thread::sleep_for(chrono::milliseconds(100));
245
Maxwell Henderson64f37452023-03-11 13:39:21 -0800246 AOS_LOG(
247 INFO, "Placed first cube %lf s\n",
248 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
249
250 // Drive and intake the cube second nearest to the starting zone
251 if (!splines[2].WaitForPlan()) return;
252 splines[2].Start();
253
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700254 std::this_thread::sleep_for(chrono::milliseconds(200));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800255 PickupCube();
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700256
257 std::this_thread::sleep_for(chrono::milliseconds(500));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800258 IntakeCube();
259
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700260 if (!splines[2].WaitForSplineDistanceRemaining(0.05)) return;
261 AOS_LOG(
262 INFO, "Picked up second cube %lf s\n",
263 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800264
265 // Drive back to grid and place object on mid level
266 if (!splines[3].WaitForPlan()) return;
267 splines[3].Start();
268
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700269 AOS_LOG(
270 INFO, "Driving back %lf s\n",
271 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
272
Maxwell Henderson64f37452023-03-11 13:39:21 -0800273 MidCubeScore();
274
Austin Schuh700bfff2023-04-05 19:45:55 -0700275 if (!splines[3].WaitForSplineDistanceRemaining(0.07)) return;
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700276 AOS_LOG(
277 INFO, "Got back from second cube at %lf s\n",
278 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800279
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700280 if (!WaitForArmGoal(0.05)) return;
Maxwell Henderson64f37452023-03-11 13:39:21 -0800281 Spit();
282
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700283 if (!splines[3].WaitForSplineDistanceRemaining(0.02)) return;
284
Maxwell Henderson64f37452023-03-11 13:39:21 -0800285 AOS_LOG(
286 INFO, "Placed second cube %lf s\n",
287 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
James Kuszmaul6fc7ee22023-09-30 10:23:31 -0700288
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700289 InitializeEncoders();
Maxwell Henderson64f37452023-03-11 13:39:21 -0800290
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700291 const ProfileParametersT kDrive = MakeProfileParameters(2.0, 4.0);
292 const ProfileParametersT kTurn = MakeProfileParameters(3.0, 4.5);
293 StartDrive(0.0, 0.0, kDrive, kTurn);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800294
Austin Schuh700bfff2023-04-05 19:45:55 -0700295 std::this_thread::sleep_for(chrono::milliseconds(100));
296
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700297 {
298 double side_scalar = (alliance_ == aos::Alliance::kRed) ? 1.0 : -1.0;
299 StartDrive(6.33 - std::abs(X()), 0.0, kDrive, kTurn);
300 if (!WaitForDriveProfileNear(0.01)) return;
301
302 AOS_LOG(
303 INFO, "Done backing up %lf s\n",
304 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
305
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700306 if (!absl::GetFlag(FLAGS_do_balance)) {
James Kuszmaul6fc7ee22023-09-30 10:23:31 -0700307 StopSpitting();
308 return;
309 }
310
Austin Schuha7c3bc62023-04-16 19:46:23 -0700311 const ProfileParametersT kInPlaceTurn = MakeProfileParameters(2.7, 8.0);
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700312 StartDrive(0.0, aos::math::NormalizeAngle(M_PI / 2.0 - Theta()), kDrive,
313 kInPlaceTurn);
314
315 std::this_thread::sleep_for(chrono::milliseconds(400));
316 StopSpitting();
317
318 AOS_LOG(
319 INFO, "Roller off %lf s\n",
320 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
321
Austin Schuh700bfff2023-04-05 19:45:55 -0700322 if (!WaitForTurnProfileNear(0.6)) return;
323 AOS_LOG(
324 INFO, "Balance arm %lf s\n",
325 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
326
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700327 Balance();
Austin Schuh700bfff2023-04-05 19:45:55 -0700328 if (!WaitForTurnProfileNear(0.001)) return;
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700329
330 AOS_LOG(
331 INFO, "Done turning %lf s\n",
332 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
333
334 const ProfileParametersT kDrive = MakeProfileParameters(1.4, 3.0);
335 const ProfileParametersT kFinalTurn = MakeProfileParameters(3.0, 4.5);
Austin Schuh700bfff2023-04-05 19:45:55 -0700336 const double kDriveDistance = 3.11;
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700337 StartDrive(-kDriveDistance, 0.0, kDrive, kFinalTurn);
338
339 const ProfileParametersT kFastTurn = MakeProfileParameters(5.0, 8.0);
340 if (!WaitForDriveProfileNear(kDriveDistance - 0.4)) return;
341
342 AOS_LOG(
343 INFO, "Turning %lf s\n",
344 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
345 StartDrive(0.0, -side_scalar * M_PI / 2.0, kDrive, kFastTurn);
346 if (!WaitForDriveProfileDone()) return;
347 if (!WaitForTurnProfileDone()) return;
348 AOS_LOG(
349 INFO, "Done %lf s\n",
350 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
351 }
Maxwell Henderson64f37452023-03-11 13:39:21 -0800352}
353
Maxwell Henderson25378832023-04-07 14:37:41 -0700354// Charged Up 3 Game Object Autonomous (cable side)
355void AutonomousActor::ChargedUpCableSide() {
356 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
357
358 CHECK(charged_up_cable_splines_);
359
360 auto &splines = *charged_up_cable_splines_;
361
362 AOS_LOG(INFO, "Going to preload");
363
364 // Tell the superstructure a cone was preloaded
365 if (!WaitForPreloaded()) return;
366 AOS_LOG(INFO, "Moving arm");
367
368 // Place first cone on mid level
369 MidConeScore();
370
371 // Wait until the arm is at the goal to spit
372 if (!WaitForArmGoal(0.10)) return;
373 Spit();
374 if (!WaitForArmGoal(0.01)) return;
375
376 std::this_thread::sleep_for(chrono::milliseconds(100));
377
378 AOS_LOG(
379 INFO, "Placed first cone %lf s\n",
380 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
381
382 // Drive and intake the cube nearest to the starting zone
383 if (!splines[0].WaitForPlan()) return;
384 splines[0].Start();
385
386 // Move arm into position to pickup a cube and start cube intake
387 PickupCube();
388
389 std::this_thread::sleep_for(chrono::milliseconds(500));
390
391 IntakeCube();
392
393 AOS_LOG(
394 INFO, "Turning on rollers %lf s\n",
395 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
396
397 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
398
399 AOS_LOG(
400 INFO, "Got there %lf s\n",
401 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
402
403 // Drive back to grid and place cube on high level
404 if (!splines[1].WaitForPlan()) return;
405 splines[1].Start();
406
407 std::this_thread::sleep_for(chrono::milliseconds(300));
Austin Schuhdd128822023-04-09 22:19:06 -0700408 Neutral();
409
410 if (!splines[1].WaitForSplineDistanceTraveled(3.2)) return;
Maxwell Henderson25378832023-04-07 14:37:41 -0700411 HighCubeScore();
Austin Schuhdd128822023-04-09 22:19:06 -0700412 AOS_LOG(
413 INFO, "Extending arm %lf s\n",
414 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
Maxwell Henderson25378832023-04-07 14:37:41 -0700415
416 if (!splines[1].WaitForSplineDistanceRemaining(0.08)) return;
417 AOS_LOG(
418 INFO, "Back for first cube %lf s\n",
419 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
420
421 if (!WaitForArmGoal(0.10)) return;
422
423 AOS_LOG(
424 INFO, "Arm in place for first cube %lf s\n",
425 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
426
427 Spit();
428
429 if (!splines[1].WaitForSplineDistanceRemaining(0.08)) return;
430
431 AOS_LOG(
432 INFO, "Finished spline back %lf s\n",
433 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
434
435 if (!WaitForArmGoal(0.05)) return;
436
437 AOS_LOG(
438 INFO, "Placed first cube %lf s\n",
439 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
440
441 // Drive and intake the cube second nearest to the starting zone
442 if (!splines[2].WaitForPlan()) return;
443 splines[2].Start();
444
445 std::this_thread::sleep_for(chrono::milliseconds(200));
446 PickupCube();
447
448 std::this_thread::sleep_for(chrono::milliseconds(500));
449 IntakeCube();
450
451 if (!splines[2].WaitForSplineDistanceRemaining(0.05)) return;
452 AOS_LOG(
453 INFO, "Picked up second cube %lf s\n",
454 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
455
456 // Drive back to grid and place object on mid level
457 if (!splines[3].WaitForPlan()) return;
458 splines[3].Start();
459
Austin Schuhdd128822023-04-09 22:19:06 -0700460 std::this_thread::sleep_for(chrono::milliseconds(400));
461 Neutral();
462
Maxwell Henderson25378832023-04-07 14:37:41 -0700463 AOS_LOG(
464 INFO, "Driving back %lf s\n",
465 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
466
Austin Schuhdd128822023-04-09 22:19:06 -0700467 if (!splines[3].WaitForSplineDistanceTraveled(3.5)) return;
468 AOS_LOG(
469 INFO, "Extending arm %lf s\n",
470 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
Maxwell Henderson25378832023-04-07 14:37:41 -0700471 MidCubeScore();
472
473 if (!splines[3].WaitForSplineDistanceRemaining(0.07)) return;
474 AOS_LOG(
475 INFO, "Got back from second cube at %lf s\n",
476 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
477
478 if (!WaitForArmGoal(0.05)) return;
479 Spit();
480
481 if (!splines[3].WaitForSplineDistanceRemaining(0.02)) return;
482
483 AOS_LOG(
484 INFO, "Placed second cube %lf s\n",
485 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
Austin Schuh8c9d2d02023-04-09 20:05:42 -0700486
487 std::this_thread::sleep_for(chrono::milliseconds(200));
488 Neutral();
Austin Schuha7c3bc62023-04-16 19:46:23 -0700489 AOS_LOG(
490 INFO, "Going to neutral %lf s\n",
491 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
492
493 if (!WaitForArmGoal(0.05)) return;
494 AOS_LOG(
495 INFO, "Done at neutral %lf s\n",
496 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
Maxwell Henderson25378832023-04-07 14:37:41 -0700497}
498
Maxwell Henderson64f37452023-03-11 13:39:21 -0800499void AutonomousActor::SendSuperstructureGoal() {
500 auto builder = superstructure_goal_sender_.MakeBuilder();
501
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700502 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
503 wrist_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
504 *builder.fbb(), wrist_goal_,
505 CreateProfileParameters(*builder.fbb(), 12.0, 90.0));
506
Maxwell Henderson64f37452023-03-11 13:39:21 -0800507 control_loops::superstructure::Goal::Builder superstructure_builder =
508 builder.MakeBuilder<control_loops::superstructure::Goal>();
509
510 superstructure_builder.add_arm_goal_position(arm_goal_position_);
511 superstructure_builder.add_preloaded_with_cone(preloaded_);
512 superstructure_builder.add_roller_goal(roller_goal_);
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700513 superstructure_builder.add_wrist(wrist_offset);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800514
515 if (builder.Send(superstructure_builder.Finish()) !=
516 aos::RawSender::Error::kOk) {
517 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
518 }
519}
520
521[[nodiscard]] bool AutonomousActor::WaitForPreloaded() {
522 set_preloaded(true);
523 SendSuperstructureGoal();
524
525 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
526 event_loop()->monotonic_now(),
Stephan Pleines743f83a2024-02-02 18:32:09 -0800527 aos::common::actions::kLoopOffset);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800528
529 bool loaded = false;
530 while (!loaded) {
531 if (ShouldCancel()) {
532 return false;
533 }
534
535 phased_loop.SleepUntilNext();
536 superstructure_status_fetcher_.Fetch();
537 CHECK(superstructure_status_fetcher_.get() != nullptr);
538
539 loaded = (superstructure_status_fetcher_->end_effector_state() ==
540 control_loops::superstructure::EndEffectorState::LOADED);
541 }
542
543 set_preloaded(false);
544 SendSuperstructureGoal();
545
546 return true;
547}
548
549void AutonomousActor::MidConeScore() {
550 set_arm_goal_position(
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700551 control_loops::superstructure::arm::ScoreFrontMidConeUpAutoIndex());
552 set_wrist_goal(0.0);
553 SendSuperstructureGoal();
554}
555
556void AutonomousActor::Neutral() {
557 set_arm_goal_position(control_loops::superstructure::arm::NeutralIndex());
Austin Schuha7c3bc62023-04-16 19:46:23 -0700558 set_wrist_goal(1.0);
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700559 SendSuperstructureGoal();
560}
561
562void AutonomousActor::Balance() {
563 set_arm_goal_position(
Austin Schuh700bfff2023-04-05 19:45:55 -0700564 control_loops::superstructure::arm::ScoreFrontLowCubeIndex());
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700565 set_wrist_goal(0.0);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800566 SendSuperstructureGoal();
567}
568
569void AutonomousActor::HighCubeScore() {
570 set_arm_goal_position(
571 control_loops::superstructure::arm::ScoreFrontHighCubeIndex());
Austin Schuha7c3bc62023-04-16 19:46:23 -0700572 set_wrist_goal(1.0);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800573 SendSuperstructureGoal();
574}
575
576void AutonomousActor::MidCubeScore() {
577 set_arm_goal_position(
578 control_loops::superstructure::arm::ScoreFrontMidCubeIndex());
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700579 set_wrist_goal(1.0);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800580 SendSuperstructureGoal();
581}
582
583void AutonomousActor::PickupCube() {
584 set_arm_goal_position(
585 control_loops::superstructure::arm::GroundPickupBackCubeIndex());
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700586 set_wrist_goal(1.0);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800587 SendSuperstructureGoal();
588}
589
590void AutonomousActor::Spit() {
591 set_roller_goal(control_loops::superstructure::RollerGoal::SPIT);
592 SendSuperstructureGoal();
593}
594
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700595void AutonomousActor::StopSpitting() {
596 set_roller_goal(control_loops::superstructure::RollerGoal::IDLE);
597 SendSuperstructureGoal();
598}
599
Maxwell Henderson64f37452023-03-11 13:39:21 -0800600void AutonomousActor::IntakeCube() {
601 set_roller_goal(control_loops::superstructure::RollerGoal::INTAKE_CUBE);
602 SendSuperstructureGoal();
603}
604
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700605[[nodiscard]] bool AutonomousActor::WaitForArmGoal(double distance_to_go) {
606 constexpr double kEpsTheta = 0.10;
Maxwell Henderson64f37452023-03-11 13:39:21 -0800607
608 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
609 event_loop()->monotonic_now(),
Stephan Pleines743f83a2024-02-02 18:32:09 -0800610 aos::common::actions::kLoopOffset);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800611
612 bool at_goal = false;
613 while (!at_goal) {
614 if (ShouldCancel()) {
615 return false;
616 }
617
618 phased_loop.SleepUntilNext();
619 superstructure_status_fetcher_.Fetch();
620 CHECK(superstructure_status_fetcher_.get() != nullptr);
621
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700622 at_goal = (arm_goal_position_ ==
623 superstructure_status_fetcher_->arm()->current_node() &&
624 superstructure_status_fetcher_->arm()->path_distance_to_go() <
625 distance_to_go) &&
Maxwell Henderson64f37452023-03-11 13:39:21 -0800626 (std::abs(wrist_goal_ -
627 superstructure_status_fetcher_->wrist()->position()) <
628 kEpsTheta);
629 }
630
Maxwell Henderson64f37452023-03-11 13:39:21 -0800631 return true;
632}
633
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800634} // namespace y2023::autonomous