blob: e452d212830515649554c41ffaadd89d8b56fbab [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
Maxwell Hendersonad312342023-01-10 12:07:47 -080020namespace y2023 {
Maxwell Henderson64f37452023-03-11 13:39:21 -080021namespace autonomous {
Maxwell Hendersonad312342023-01-10 12:07:47 -080022
Maxwell Hendersonad312342023-01-10 12:07:47 -080023using ::frc971::ProfileParametersT;
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -070024
25ProfileParametersT MakeProfileParameters(float max_velocity,
26 float max_acceleration) {
27 ProfileParametersT result;
28 result.max_velocity = max_velocity;
29 result.max_acceleration = max_acceleration;
30 return result;
31}
32
33using ::aos::monotonic_clock;
34using frc971::CreateProfileParameters;
35using ::frc971::ProfileParametersT;
36using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal;
37using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal;
Maxwell Hendersonad312342023-01-10 12:07:47 -080038using frc971::control_loops::drivetrain::LocalizerControl;
39namespace chrono = ::std::chrono;
40
41AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
Maxwell Henderson12b07b92024-01-08 20:17:25 -080042 : frc971::autonomous::UserButtonLocalizedAutonomousActor(
James Kuszmaul713c5ce2023-03-04 18:23:24 -080043 event_loop, control_loops::drivetrain::GetDrivetrainConfig()),
44 localizer_control_sender_(
45 event_loop->MakeSender<
46 ::frc971::control_loops::drivetrain::LocalizerControl>(
47 "/drivetrain")),
Maxwell Henderson64f37452023-03-11 13:39:21 -080048 auto_splines_(),
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -070049 arm_goal_position_(control_loops::superstructure::arm::StartingIndex()),
50 superstructure_goal_sender_(
51 event_loop->MakeSender<::y2023::control_loops::superstructure::Goal>(
52 "/superstructure")),
53 superstructure_status_fetcher_(
54 event_loop
55 ->MakeFetcher<::y2023::control_loops::superstructure::Status>(
56 "/superstructure")),
Maxwell Henderson12b07b92024-01-08 20:17:25 -080057 points_(control_loops::superstructure::arm::PointList()) {}
James Kuszmaul713c5ce2023-03-04 18:23:24 -080058
59void AutonomousActor::Replan() {
James Kuszmaul713c5ce2023-03-04 18:23:24 -080060 if (FLAGS_spline_auto) {
61 test_spline_ =
62 PlanSpline(std::bind(&AutonomousSplines::TestSpline, &auto_splines_,
63 std::placeholders::_1, alliance_),
64 SplineDirection::kForward);
65
66 starting_position_ = test_spline_->starting_position();
Maxwell Henderson64f37452023-03-11 13:39:21 -080067 } else if (FLAGS_charged_up) {
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -070068 AOS_LOG(INFO, "Charged up replanning!");
Maxwell Henderson64f37452023-03-11 13:39:21 -080069 charged_up_splines_ = {
70 PlanSpline(std::bind(&AutonomousSplines::Spline1, &auto_splines_,
71 std::placeholders::_1, alliance_),
72 SplineDirection::kBackward),
73 PlanSpline(std::bind(&AutonomousSplines::Spline2, &auto_splines_,
74 std::placeholders::_1, alliance_),
75 SplineDirection::kForward),
76 PlanSpline(std::bind(&AutonomousSplines::Spline3, &auto_splines_,
77 std::placeholders::_1, alliance_),
78 SplineDirection::kBackward),
79 PlanSpline(std::bind(&AutonomousSplines::Spline4, &auto_splines_,
80 std::placeholders::_1, alliance_),
Maxwell Henderson25378832023-04-07 14:37:41 -070081 SplineDirection::kForward)};
Maxwell Henderson64f37452023-03-11 13:39:21 -080082
83 starting_position_ = charged_up_splines_.value()[0].starting_position();
84 CHECK(starting_position_);
Maxwell Henderson25378832023-04-07 14:37:41 -070085 } else if (FLAGS_charged_up_cable) {
86 charged_up_cable_splines_ = {
87 PlanSpline(std::bind(&AutonomousSplines::SplineCable1, &auto_splines_,
88 std::placeholders::_1, alliance_),
89 SplineDirection::kBackward),
90 PlanSpline(std::bind(&AutonomousSplines::SplineCable2, &auto_splines_,
91 std::placeholders::_1, alliance_),
92 SplineDirection::kForward),
93 PlanSpline(std::bind(&AutonomousSplines::SplineCable3, &auto_splines_,
94 std::placeholders::_1, alliance_),
95 SplineDirection::kBackward),
96 PlanSpline(std::bind(&AutonomousSplines::SplineCable4, &auto_splines_,
97 std::placeholders::_1, alliance_),
98 SplineDirection::kForward)};
99
100 starting_position_ =
101 charged_up_cable_splines_.value()[0].starting_position();
102 CHECK(starting_position_);
James Kuszmaul713c5ce2023-03-04 18:23:24 -0800103 }
104
105 is_planned_ = true;
106
107 MaybeSendStartingPosition();
108}
109
Maxwell Hendersonad312342023-01-10 12:07:47 -0800110void AutonomousActor::Reset() {
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700111 wrist_goal_ = 0.0;
112 roller_goal_ = control_loops::superstructure::RollerGoal::IDLE;
113 arm_goal_position_ = control_loops::superstructure::arm::StartingIndex();
114 preloaded_ = false;
115 SendSuperstructureGoal();
Maxwell Hendersonad312342023-01-10 12:07:47 -0800116}
117
Maxwell Henderson12b07b92024-01-08 20:17:25 -0800118bool AutonomousActor::Run(
Maxwell Hendersonad312342023-01-10 12:07:47 -0800119 const ::frc971::autonomous::AutonomousActionParams *params) {
James Kuszmaul713c5ce2023-03-04 18:23:24 -0800120 AOS_LOG(INFO, "Params are %d\n", params->mode());
121 if (alliance_ == aos::Alliance::kInvalid) {
122 AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection.");
123 return false;
124 }
125 if (FLAGS_spline_auto) {
126 SplineAuto();
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700127 } else if (FLAGS_charged_up) {
128 ChargedUp();
Austin Schuh8c9d2d02023-04-09 20:05:42 -0700129 } else if (FLAGS_charged_up_cable) {
130 ChargedUpCableSide();
James Kuszmaul713c5ce2023-03-04 18:23:24 -0800131 } else {
132 AOS_LOG(WARNING, "No auto mode selected.");
133 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800134 return true;
135}
136
James Kuszmaul713c5ce2023-03-04 18:23:24 -0800137void AutonomousActor::SplineAuto() {
138 CHECK(test_spline_);
139
140 if (!test_spline_->WaitForPlan()) return;
141 test_spline_->Start();
142
143 if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return;
144}
145
146void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) {
147 // Set up the starting position for the blue alliance.
148
149 auto builder = localizer_control_sender_.MakeBuilder();
150
151 LocalizerControl::Builder localizer_control_builder =
152 builder.MakeBuilder<LocalizerControl>();
153 localizer_control_builder.add_x(start(0));
154 localizer_control_builder.add_y(start(1));
155 localizer_control_builder.add_theta(start(2));
156 localizer_control_builder.add_theta_uncertainty(0.00001);
157 AOS_LOG(INFO, "User button pressed, x: %f y: %f theta: %f", start(0),
158 start(1), start(2));
159 if (builder.Send(localizer_control_builder.Finish()) !=
160 aos::RawSender::Error::kOk) {
161 AOS_LOG(ERROR, "Failed to reset localizer.\n");
162 }
163}
164
Maxwell Henderson25378832023-04-07 14:37:41 -0700165// Charged Up 3 Game Object Autonomous (non-cable side)
Maxwell Henderson64f37452023-03-11 13:39:21 -0800166void AutonomousActor::ChargedUp() {
167 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
168
169 CHECK(charged_up_splines_);
170
171 auto &splines = *charged_up_splines_;
172
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700173 AOS_LOG(INFO, "Going to preload");
174
Maxwell Henderson64f37452023-03-11 13:39:21 -0800175 // Tell the superstructure a cone was preloaded
176 if (!WaitForPreloaded()) return;
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700177 AOS_LOG(INFO, "Moving arm");
Maxwell Henderson64f37452023-03-11 13:39:21 -0800178
179 // Place first cone on mid level
180 MidConeScore();
181
182 // Wait until the arm is at the goal to spit
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700183 if (!WaitForArmGoal(0.10)) return;
Maxwell Henderson64f37452023-03-11 13:39:21 -0800184 Spit();
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700185 if (!WaitForArmGoal(0.01)) return;
186
187 std::this_thread::sleep_for(chrono::milliseconds(100));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800188
189 AOS_LOG(
190 INFO, "Placed first cone %lf s\n",
191 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
192
193 // Drive and intake the cube nearest to the starting zone
194 if (!splines[0].WaitForPlan()) return;
195 splines[0].Start();
196
197 // Move arm into position to pickup a cube and start cube intake
198 PickupCube();
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700199
200 std::this_thread::sleep_for(chrono::milliseconds(500));
201
Maxwell Henderson64f37452023-03-11 13:39:21 -0800202 IntakeCube();
203
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700204 AOS_LOG(
205 INFO, "Turning on rollers %lf s\n",
206 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
207
Maxwell Henderson64f37452023-03-11 13:39:21 -0800208 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
209
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700210 AOS_LOG(
211 INFO, "Got there %lf s\n",
212 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
213
Maxwell Henderson64f37452023-03-11 13:39:21 -0800214 // Drive back to grid and place cube on high level
215 if (!splines[1].WaitForPlan()) return;
216 splines[1].Start();
217
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700218 std::this_thread::sleep_for(chrono::milliseconds(300));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800219 HighCubeScore();
220
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700221 if (!splines[1].WaitForSplineDistanceRemaining(0.08)) return;
222 AOS_LOG(
223 INFO, "Back for first cube %lf s\n",
224 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800225
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700226 if (!WaitForArmGoal(0.10)) return;
227
228 AOS_LOG(
229 INFO, "Arm in place for first cube %lf s\n",
230 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
231
Maxwell Henderson64f37452023-03-11 13:39:21 -0800232 Spit();
233
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700234 if (!splines[1].WaitForSplineDistanceRemaining(0.08)) return;
235
236 AOS_LOG(
237 INFO, "Finished spline back %lf s\n",
238 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
239
240 if (!WaitForArmGoal(0.05)) return;
241
Austin Schuha7c3bc62023-04-16 19:46:23 -0700242 std::this_thread::sleep_for(chrono::milliseconds(100));
243
Maxwell Henderson64f37452023-03-11 13:39:21 -0800244 AOS_LOG(
245 INFO, "Placed first cube %lf s\n",
246 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
247
248 // Drive and intake the cube second nearest to the starting zone
249 if (!splines[2].WaitForPlan()) return;
250 splines[2].Start();
251
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700252 std::this_thread::sleep_for(chrono::milliseconds(200));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800253 PickupCube();
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700254
255 std::this_thread::sleep_for(chrono::milliseconds(500));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800256 IntakeCube();
257
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700258 if (!splines[2].WaitForSplineDistanceRemaining(0.05)) return;
259 AOS_LOG(
260 INFO, "Picked up second cube %lf s\n",
261 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800262
263 // Drive back to grid and place object on mid level
264 if (!splines[3].WaitForPlan()) return;
265 splines[3].Start();
266
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700267 AOS_LOG(
268 INFO, "Driving back %lf s\n",
269 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
270
Maxwell Henderson64f37452023-03-11 13:39:21 -0800271 MidCubeScore();
272
Austin Schuh700bfff2023-04-05 19:45:55 -0700273 if (!splines[3].WaitForSplineDistanceRemaining(0.07)) return;
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700274 AOS_LOG(
275 INFO, "Got back from second cube at %lf s\n",
276 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800277
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700278 if (!WaitForArmGoal(0.05)) return;
Maxwell Henderson64f37452023-03-11 13:39:21 -0800279 Spit();
280
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700281 if (!splines[3].WaitForSplineDistanceRemaining(0.02)) return;
282
Maxwell Henderson64f37452023-03-11 13:39:21 -0800283 AOS_LOG(
284 INFO, "Placed second cube %lf s\n",
285 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
James Kuszmaul6fc7ee22023-09-30 10:23:31 -0700286
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700287 InitializeEncoders();
Maxwell Henderson64f37452023-03-11 13:39:21 -0800288
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700289 const ProfileParametersT kDrive = MakeProfileParameters(2.0, 4.0);
290 const ProfileParametersT kTurn = MakeProfileParameters(3.0, 4.5);
291 StartDrive(0.0, 0.0, kDrive, kTurn);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800292
Austin Schuh700bfff2023-04-05 19:45:55 -0700293 std::this_thread::sleep_for(chrono::milliseconds(100));
294
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700295 {
296 double side_scalar = (alliance_ == aos::Alliance::kRed) ? 1.0 : -1.0;
297 StartDrive(6.33 - std::abs(X()), 0.0, kDrive, kTurn);
298 if (!WaitForDriveProfileNear(0.01)) return;
299
300 AOS_LOG(
301 INFO, "Done backing up %lf s\n",
302 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
303
James Kuszmaul6fc7ee22023-09-30 10:23:31 -0700304 if (!FLAGS_do_balance) {
305 StopSpitting();
306 return;
307 }
308
Austin Schuha7c3bc62023-04-16 19:46:23 -0700309 const ProfileParametersT kInPlaceTurn = MakeProfileParameters(2.7, 8.0);
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700310 StartDrive(0.0, aos::math::NormalizeAngle(M_PI / 2.0 - Theta()), kDrive,
311 kInPlaceTurn);
312
313 std::this_thread::sleep_for(chrono::milliseconds(400));
314 StopSpitting();
315
316 AOS_LOG(
317 INFO, "Roller off %lf s\n",
318 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
319
Austin Schuh700bfff2023-04-05 19:45:55 -0700320 if (!WaitForTurnProfileNear(0.6)) return;
321 AOS_LOG(
322 INFO, "Balance arm %lf s\n",
323 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
324
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700325 Balance();
Austin Schuh700bfff2023-04-05 19:45:55 -0700326 if (!WaitForTurnProfileNear(0.001)) return;
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700327
328 AOS_LOG(
329 INFO, "Done turning %lf s\n",
330 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
331
332 const ProfileParametersT kDrive = MakeProfileParameters(1.4, 3.0);
333 const ProfileParametersT kFinalTurn = MakeProfileParameters(3.0, 4.5);
Austin Schuh700bfff2023-04-05 19:45:55 -0700334 const double kDriveDistance = 3.11;
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700335 StartDrive(-kDriveDistance, 0.0, kDrive, kFinalTurn);
336
337 const ProfileParametersT kFastTurn = MakeProfileParameters(5.0, 8.0);
338 if (!WaitForDriveProfileNear(kDriveDistance - 0.4)) return;
339
340 AOS_LOG(
341 INFO, "Turning %lf s\n",
342 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
343 StartDrive(0.0, -side_scalar * M_PI / 2.0, kDrive, kFastTurn);
344 if (!WaitForDriveProfileDone()) return;
345 if (!WaitForTurnProfileDone()) return;
346 AOS_LOG(
347 INFO, "Done %lf s\n",
348 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
349 }
Maxwell Henderson64f37452023-03-11 13:39:21 -0800350}
351
Maxwell Henderson25378832023-04-07 14:37:41 -0700352// Charged Up 3 Game Object Autonomous (cable side)
353void AutonomousActor::ChargedUpCableSide() {
354 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
355
356 CHECK(charged_up_cable_splines_);
357
358 auto &splines = *charged_up_cable_splines_;
359
360 AOS_LOG(INFO, "Going to preload");
361
362 // Tell the superstructure a cone was preloaded
363 if (!WaitForPreloaded()) return;
364 AOS_LOG(INFO, "Moving arm");
365
366 // Place first cone on mid level
367 MidConeScore();
368
369 // Wait until the arm is at the goal to spit
370 if (!WaitForArmGoal(0.10)) return;
371 Spit();
372 if (!WaitForArmGoal(0.01)) return;
373
374 std::this_thread::sleep_for(chrono::milliseconds(100));
375
376 AOS_LOG(
377 INFO, "Placed first cone %lf s\n",
378 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
379
380 // Drive and intake the cube nearest to the starting zone
381 if (!splines[0].WaitForPlan()) return;
382 splines[0].Start();
383
384 // Move arm into position to pickup a cube and start cube intake
385 PickupCube();
386
387 std::this_thread::sleep_for(chrono::milliseconds(500));
388
389 IntakeCube();
390
391 AOS_LOG(
392 INFO, "Turning on rollers %lf s\n",
393 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
394
395 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
396
397 AOS_LOG(
398 INFO, "Got there %lf s\n",
399 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
400
401 // Drive back to grid and place cube on high level
402 if (!splines[1].WaitForPlan()) return;
403 splines[1].Start();
404
405 std::this_thread::sleep_for(chrono::milliseconds(300));
Austin Schuhdd128822023-04-09 22:19:06 -0700406 Neutral();
407
408 if (!splines[1].WaitForSplineDistanceTraveled(3.2)) return;
Maxwell Henderson25378832023-04-07 14:37:41 -0700409 HighCubeScore();
Austin Schuhdd128822023-04-09 22:19:06 -0700410 AOS_LOG(
411 INFO, "Extending arm %lf s\n",
412 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
Maxwell Henderson25378832023-04-07 14:37:41 -0700413
414 if (!splines[1].WaitForSplineDistanceRemaining(0.08)) return;
415 AOS_LOG(
416 INFO, "Back for first cube %lf s\n",
417 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
418
419 if (!WaitForArmGoal(0.10)) return;
420
421 AOS_LOG(
422 INFO, "Arm in place for first cube %lf s\n",
423 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
424
425 Spit();
426
427 if (!splines[1].WaitForSplineDistanceRemaining(0.08)) return;
428
429 AOS_LOG(
430 INFO, "Finished spline back %lf s\n",
431 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
432
433 if (!WaitForArmGoal(0.05)) return;
434
435 AOS_LOG(
436 INFO, "Placed first cube %lf s\n",
437 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
438
439 // Drive and intake the cube second nearest to the starting zone
440 if (!splines[2].WaitForPlan()) return;
441 splines[2].Start();
442
443 std::this_thread::sleep_for(chrono::milliseconds(200));
444 PickupCube();
445
446 std::this_thread::sleep_for(chrono::milliseconds(500));
447 IntakeCube();
448
449 if (!splines[2].WaitForSplineDistanceRemaining(0.05)) return;
450 AOS_LOG(
451 INFO, "Picked up second cube %lf s\n",
452 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
453
454 // Drive back to grid and place object on mid level
455 if (!splines[3].WaitForPlan()) return;
456 splines[3].Start();
457
Austin Schuhdd128822023-04-09 22:19:06 -0700458 std::this_thread::sleep_for(chrono::milliseconds(400));
459 Neutral();
460
Maxwell Henderson25378832023-04-07 14:37:41 -0700461 AOS_LOG(
462 INFO, "Driving back %lf s\n",
463 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
464
Austin Schuhdd128822023-04-09 22:19:06 -0700465 if (!splines[3].WaitForSplineDistanceTraveled(3.5)) return;
466 AOS_LOG(
467 INFO, "Extending arm %lf s\n",
468 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
Maxwell Henderson25378832023-04-07 14:37:41 -0700469 MidCubeScore();
470
471 if (!splines[3].WaitForSplineDistanceRemaining(0.07)) return;
472 AOS_LOG(
473 INFO, "Got back from second cube at %lf s\n",
474 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
475
476 if (!WaitForArmGoal(0.05)) return;
477 Spit();
478
479 if (!splines[3].WaitForSplineDistanceRemaining(0.02)) return;
480
481 AOS_LOG(
482 INFO, "Placed second cube %lf s\n",
483 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
Austin Schuh8c9d2d02023-04-09 20:05:42 -0700484
485 std::this_thread::sleep_for(chrono::milliseconds(200));
486 Neutral();
Austin Schuha7c3bc62023-04-16 19:46:23 -0700487 AOS_LOG(
488 INFO, "Going to neutral %lf s\n",
489 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
490
491 if (!WaitForArmGoal(0.05)) return;
492 AOS_LOG(
493 INFO, "Done at neutral %lf s\n",
494 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
Maxwell Henderson25378832023-04-07 14:37:41 -0700495}
496
Maxwell Henderson64f37452023-03-11 13:39:21 -0800497void AutonomousActor::SendSuperstructureGoal() {
498 auto builder = superstructure_goal_sender_.MakeBuilder();
499
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700500 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
501 wrist_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
502 *builder.fbb(), wrist_goal_,
503 CreateProfileParameters(*builder.fbb(), 12.0, 90.0));
504
Maxwell Henderson64f37452023-03-11 13:39:21 -0800505 control_loops::superstructure::Goal::Builder superstructure_builder =
506 builder.MakeBuilder<control_loops::superstructure::Goal>();
507
508 superstructure_builder.add_arm_goal_position(arm_goal_position_);
509 superstructure_builder.add_preloaded_with_cone(preloaded_);
510 superstructure_builder.add_roller_goal(roller_goal_);
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700511 superstructure_builder.add_wrist(wrist_offset);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800512
513 if (builder.Send(superstructure_builder.Finish()) !=
514 aos::RawSender::Error::kOk) {
515 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
516 }
517}
518
519[[nodiscard]] bool AutonomousActor::WaitForPreloaded() {
520 set_preloaded(true);
521 SendSuperstructureGoal();
522
523 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
524 event_loop()->monotonic_now(),
525 ActorBase::kLoopOffset);
526
527 bool loaded = false;
528 while (!loaded) {
529 if (ShouldCancel()) {
530 return false;
531 }
532
533 phased_loop.SleepUntilNext();
534 superstructure_status_fetcher_.Fetch();
535 CHECK(superstructure_status_fetcher_.get() != nullptr);
536
537 loaded = (superstructure_status_fetcher_->end_effector_state() ==
538 control_loops::superstructure::EndEffectorState::LOADED);
539 }
540
541 set_preloaded(false);
542 SendSuperstructureGoal();
543
544 return true;
545}
546
547void AutonomousActor::MidConeScore() {
548 set_arm_goal_position(
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700549 control_loops::superstructure::arm::ScoreFrontMidConeUpAutoIndex());
550 set_wrist_goal(0.0);
551 SendSuperstructureGoal();
552}
553
554void AutonomousActor::Neutral() {
555 set_arm_goal_position(control_loops::superstructure::arm::NeutralIndex());
Austin Schuha7c3bc62023-04-16 19:46:23 -0700556 set_wrist_goal(1.0);
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700557 SendSuperstructureGoal();
558}
559
560void AutonomousActor::Balance() {
561 set_arm_goal_position(
Austin Schuh700bfff2023-04-05 19:45:55 -0700562 control_loops::superstructure::arm::ScoreFrontLowCubeIndex());
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700563 set_wrist_goal(0.0);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800564 SendSuperstructureGoal();
565}
566
567void AutonomousActor::HighCubeScore() {
568 set_arm_goal_position(
569 control_loops::superstructure::arm::ScoreFrontHighCubeIndex());
Austin Schuha7c3bc62023-04-16 19:46:23 -0700570 set_wrist_goal(1.0);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800571 SendSuperstructureGoal();
572}
573
574void AutonomousActor::MidCubeScore() {
575 set_arm_goal_position(
576 control_loops::superstructure::arm::ScoreFrontMidCubeIndex());
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700577 set_wrist_goal(1.0);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800578 SendSuperstructureGoal();
579}
580
581void AutonomousActor::PickupCube() {
582 set_arm_goal_position(
583 control_loops::superstructure::arm::GroundPickupBackCubeIndex());
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700584 set_wrist_goal(1.0);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800585 SendSuperstructureGoal();
586}
587
588void AutonomousActor::Spit() {
589 set_roller_goal(control_loops::superstructure::RollerGoal::SPIT);
590 SendSuperstructureGoal();
591}
592
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700593void AutonomousActor::StopSpitting() {
594 set_roller_goal(control_loops::superstructure::RollerGoal::IDLE);
595 SendSuperstructureGoal();
596}
597
Maxwell Henderson64f37452023-03-11 13:39:21 -0800598void AutonomousActor::IntakeCube() {
599 set_roller_goal(control_loops::superstructure::RollerGoal::INTAKE_CUBE);
600 SendSuperstructureGoal();
601}
602
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700603[[nodiscard]] bool AutonomousActor::WaitForArmGoal(double distance_to_go) {
604 constexpr double kEpsTheta = 0.10;
Maxwell Henderson64f37452023-03-11 13:39:21 -0800605
606 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
607 event_loop()->monotonic_now(),
608 ActorBase::kLoopOffset);
609
610 bool at_goal = false;
611 while (!at_goal) {
612 if (ShouldCancel()) {
613 return false;
614 }
615
616 phased_loop.SleepUntilNext();
617 superstructure_status_fetcher_.Fetch();
618 CHECK(superstructure_status_fetcher_.get() != nullptr);
619
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700620 at_goal = (arm_goal_position_ ==
621 superstructure_status_fetcher_->arm()->current_node() &&
622 superstructure_status_fetcher_->arm()->path_distance_to_go() <
623 distance_to_go) &&
Maxwell Henderson64f37452023-03-11 13:39:21 -0800624 (std::abs(wrist_goal_ -
625 superstructure_status_fetcher_->wrist()->position()) <
626 kEpsTheta);
627 }
628
Maxwell Henderson64f37452023-03-11 13:39:21 -0800629 return true;
630}
631
632} // namespace autonomous
Maxwell Hendersonad312342023-01-10 12:07:47 -0800633} // namespace y2023