blob: c3ba65bf3c9a11f7a39f002740d79304f6fd17ae [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");
James Kuszmaul713c5ce2023-03-04 18:23:24 -080017
Maxwell Hendersonad312342023-01-10 12:07:47 -080018namespace y2023 {
Maxwell Henderson64f37452023-03-11 13:39:21 -080019namespace autonomous {
Maxwell Hendersonad312342023-01-10 12:07:47 -080020
Maxwell Hendersonad312342023-01-10 12:07:47 -080021using ::frc971::ProfileParametersT;
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -070022
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;
Maxwell Hendersonad312342023-01-10 12:07:47 -080036using frc971::control_loops::drivetrain::LocalizerControl;
37namespace chrono = ::std::chrono;
38
39AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
40 : frc971::autonomous::BaseAutonomousActor(
James Kuszmaul713c5ce2023-03-04 18:23:24 -080041 event_loop, control_loops::drivetrain::GetDrivetrainConfig()),
42 localizer_control_sender_(
43 event_loop->MakeSender<
44 ::frc971::control_loops::drivetrain::LocalizerControl>(
45 "/drivetrain")),
46 joystick_state_fetcher_(
47 event_loop->MakeFetcher<aos::JoystickState>("/aos")),
48 robot_state_fetcher_(event_loop->MakeFetcher<aos::RobotState>("/aos")),
Maxwell Henderson64f37452023-03-11 13:39:21 -080049 auto_splines_(),
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -070050 arm_goal_position_(control_loops::superstructure::arm::StartingIndex()),
51 superstructure_goal_sender_(
52 event_loop->MakeSender<::y2023::control_loops::superstructure::Goal>(
53 "/superstructure")),
54 superstructure_status_fetcher_(
55 event_loop
56 ->MakeFetcher<::y2023::control_loops::superstructure::Status>(
57 "/superstructure")),
Maxwell Henderson64f37452023-03-11 13:39:21 -080058 points_(control_loops::superstructure::arm::PointList()) {
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -070059 drivetrain_status_fetcher_.Fetch();
James Kuszmaul713c5ce2023-03-04 18:23:24 -080060 replan_timer_ = event_loop->AddTimer([this]() { Replan(); });
61
62 event_loop->OnRun([this, event_loop]() {
63 replan_timer_->Setup(event_loop->monotonic_now());
64 button_poll_->Setup(event_loop->monotonic_now(), chrono::milliseconds(50));
65 });
66
67 // TODO(james): Really need to refactor this code since we keep using it.
68 button_poll_ = event_loop->AddTimer([this]() {
69 const aos::monotonic_clock::time_point now =
70 this->event_loop()->context().monotonic_event_time;
71 if (robot_state_fetcher_.Fetch()) {
72 if (robot_state_fetcher_->user_button()) {
73 user_indicated_safe_to_reset_ = true;
74 MaybeSendStartingPosition();
75 }
76 }
77 if (joystick_state_fetcher_.Fetch()) {
78 if (joystick_state_fetcher_->has_alliance() &&
79 (joystick_state_fetcher_->alliance() != alliance_)) {
80 alliance_ = joystick_state_fetcher_->alliance();
81 is_planned_ = false;
Maxwell Henderson64f37452023-03-11 13:39:21 -080082 // Only kick the planning out by 2 seconds. If we end up enabled in
83 // that second, then we will kick it out further based on the code
84 // below.
James Kuszmaul713c5ce2023-03-04 18:23:24 -080085 replan_timer_->Setup(now + std::chrono::seconds(2));
86 }
87 if (joystick_state_fetcher_->enabled()) {
88 if (!is_planned_) {
89 // Only replan once we've been disabled for 5 seconds.
90 replan_timer_->Setup(now + std::chrono::seconds(5));
91 }
92 }
93 }
94 });
95}
96
97void AutonomousActor::Replan() {
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -070098 if (!drivetrain_status_fetcher_.Fetch()) {
99 replan_timer_->Setup(event_loop()->monotonic_now() + chrono::seconds(1));
100 AOS_LOG(INFO, "Drivetrain not up, replanning in 1 second");
101 return;
102 }
103
James Kuszmaul713c5ce2023-03-04 18:23:24 -0800104 if (alliance_ == aos::Alliance::kInvalid) {
105 return;
106 }
107 sent_starting_position_ = false;
108 if (FLAGS_spline_auto) {
109 test_spline_ =
110 PlanSpline(std::bind(&AutonomousSplines::TestSpline, &auto_splines_,
111 std::placeholders::_1, alliance_),
112 SplineDirection::kForward);
113
114 starting_position_ = test_spline_->starting_position();
Maxwell Henderson64f37452023-03-11 13:39:21 -0800115 } else if (FLAGS_charged_up) {
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700116 AOS_LOG(INFO, "Charged up replanning!");
Maxwell Henderson64f37452023-03-11 13:39:21 -0800117 charged_up_splines_ = {
118 PlanSpline(std::bind(&AutonomousSplines::Spline1, &auto_splines_,
119 std::placeholders::_1, alliance_),
120 SplineDirection::kBackward),
121 PlanSpline(std::bind(&AutonomousSplines::Spline2, &auto_splines_,
122 std::placeholders::_1, alliance_),
123 SplineDirection::kForward),
124 PlanSpline(std::bind(&AutonomousSplines::Spline3, &auto_splines_,
125 std::placeholders::_1, alliance_),
126 SplineDirection::kBackward),
127 PlanSpline(std::bind(&AutonomousSplines::Spline4, &auto_splines_,
128 std::placeholders::_1, alliance_),
129 SplineDirection::kForward),
Maxwell Henderson64f37452023-03-11 13:39:21 -0800130 };
131
132 starting_position_ = charged_up_splines_.value()[0].starting_position();
133 CHECK(starting_position_);
James Kuszmaul713c5ce2023-03-04 18:23:24 -0800134 }
135
136 is_planned_ = true;
137
138 MaybeSendStartingPosition();
139}
140
141void AutonomousActor::MaybeSendStartingPosition() {
142 if (is_planned_ && user_indicated_safe_to_reset_ &&
143 !sent_starting_position_) {
144 CHECK(starting_position_);
145 SendStartingPosition(starting_position_.value());
146 }
147}
Maxwell Hendersonad312342023-01-10 12:07:47 -0800148
149void AutonomousActor::Reset() {
150 InitializeEncoders();
151 ResetDrivetrain();
James Kuszmaul713c5ce2023-03-04 18:23:24 -0800152
153 joystick_state_fetcher_.Fetch();
154 CHECK(joystick_state_fetcher_.get() != nullptr)
155 << "Expect at least one JoystickState message before running auto...";
156 alliance_ = joystick_state_fetcher_->alliance();
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700157
158 wrist_goal_ = 0.0;
159 roller_goal_ = control_loops::superstructure::RollerGoal::IDLE;
160 arm_goal_position_ = control_loops::superstructure::arm::StartingIndex();
161 preloaded_ = false;
162 SendSuperstructureGoal();
Maxwell Hendersonad312342023-01-10 12:07:47 -0800163}
164
165bool AutonomousActor::RunAction(
166 const ::frc971::autonomous::AutonomousActionParams *params) {
167 Reset();
168
169 AOS_LOG(INFO, "Params are %d\n", params->mode());
James Kuszmaul713c5ce2023-03-04 18:23:24 -0800170
171 if (!user_indicated_safe_to_reset_) {
172 AOS_LOG(WARNING, "Didn't send starting position prior to starting auto.");
173 CHECK(starting_position_);
174 SendStartingPosition(starting_position_.value());
175 }
Maxwell Henderson64f37452023-03-11 13:39:21 -0800176 // Clear this so that we don't accidentally resend things as soon as we
177 // replan later.
James Kuszmaul713c5ce2023-03-04 18:23:24 -0800178 user_indicated_safe_to_reset_ = false;
179 is_planned_ = false;
180 starting_position_.reset();
181
182 AOS_LOG(INFO, "Params are %d\n", params->mode());
183 if (alliance_ == aos::Alliance::kInvalid) {
184 AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection.");
185 return false;
186 }
187 if (FLAGS_spline_auto) {
188 SplineAuto();
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700189 } else if (FLAGS_charged_up) {
190 ChargedUp();
James Kuszmaul713c5ce2023-03-04 18:23:24 -0800191 } else {
192 AOS_LOG(WARNING, "No auto mode selected.");
193 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800194 return true;
195}
196
James Kuszmaul713c5ce2023-03-04 18:23:24 -0800197void AutonomousActor::SplineAuto() {
198 CHECK(test_spline_);
199
200 if (!test_spline_->WaitForPlan()) return;
201 test_spline_->Start();
202
203 if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return;
204}
205
206void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) {
207 // Set up the starting position for the blue alliance.
208
209 auto builder = localizer_control_sender_.MakeBuilder();
210
211 LocalizerControl::Builder localizer_control_builder =
212 builder.MakeBuilder<LocalizerControl>();
213 localizer_control_builder.add_x(start(0));
214 localizer_control_builder.add_y(start(1));
215 localizer_control_builder.add_theta(start(2));
216 localizer_control_builder.add_theta_uncertainty(0.00001);
217 AOS_LOG(INFO, "User button pressed, x: %f y: %f theta: %f", start(0),
218 start(1), start(2));
219 if (builder.Send(localizer_control_builder.Finish()) !=
220 aos::RawSender::Error::kOk) {
221 AOS_LOG(ERROR, "Failed to reset localizer.\n");
222 }
223}
224
Maxwell Henderson64f37452023-03-11 13:39:21 -0800225// Charged Up 3 Game Object Autonomous.
226void AutonomousActor::ChargedUp() {
227 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
228
229 CHECK(charged_up_splines_);
230
231 auto &splines = *charged_up_splines_;
232
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700233 AOS_LOG(INFO, "Going to preload");
234
Maxwell Henderson64f37452023-03-11 13:39:21 -0800235 // Tell the superstructure a cone was preloaded
236 if (!WaitForPreloaded()) return;
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700237 AOS_LOG(INFO, "Moving arm");
Maxwell Henderson64f37452023-03-11 13:39:21 -0800238
239 // Place first cone on mid level
240 MidConeScore();
241
242 // Wait until the arm is at the goal to spit
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700243 if (!WaitForArmGoal(0.10)) return;
Maxwell Henderson64f37452023-03-11 13:39:21 -0800244 Spit();
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700245 if (!WaitForArmGoal(0.01)) return;
246
247 std::this_thread::sleep_for(chrono::milliseconds(100));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800248
249 AOS_LOG(
250 INFO, "Placed first cone %lf s\n",
251 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
252
253 // Drive and intake the cube nearest to the starting zone
254 if (!splines[0].WaitForPlan()) return;
255 splines[0].Start();
256
257 // Move arm into position to pickup a cube and start cube intake
258 PickupCube();
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700259
260 std::this_thread::sleep_for(chrono::milliseconds(500));
261
Maxwell Henderson64f37452023-03-11 13:39:21 -0800262 IntakeCube();
263
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700264 AOS_LOG(
265 INFO, "Turning on rollers %lf s\n",
266 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
267
Maxwell Henderson64f37452023-03-11 13:39:21 -0800268 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
269
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700270 AOS_LOG(
271 INFO, "Got there %lf s\n",
272 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
273
Maxwell Henderson64f37452023-03-11 13:39:21 -0800274 // Drive back to grid and place cube on high level
275 if (!splines[1].WaitForPlan()) return;
276 splines[1].Start();
277
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700278 std::this_thread::sleep_for(chrono::milliseconds(300));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800279 HighCubeScore();
280
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700281 if (!splines[1].WaitForSplineDistanceRemaining(0.08)) return;
282 AOS_LOG(
283 INFO, "Back for first cube %lf s\n",
284 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800285
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700286 if (!WaitForArmGoal(0.10)) return;
287
288 AOS_LOG(
289 INFO, "Arm in place for first cube %lf s\n",
290 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
291
Maxwell Henderson64f37452023-03-11 13:39:21 -0800292 Spit();
293
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700294 if (!splines[1].WaitForSplineDistanceRemaining(0.08)) return;
295
296 AOS_LOG(
297 INFO, "Finished spline back %lf s\n",
298 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
299
300 if (!WaitForArmGoal(0.05)) return;
301
Maxwell Henderson64f37452023-03-11 13:39:21 -0800302 AOS_LOG(
303 INFO, "Placed first cube %lf s\n",
304 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
305
306 // Drive and intake the cube second nearest to the starting zone
307 if (!splines[2].WaitForPlan()) return;
308 splines[2].Start();
309
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700310 std::this_thread::sleep_for(chrono::milliseconds(200));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800311 PickupCube();
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700312
313 std::this_thread::sleep_for(chrono::milliseconds(500));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800314 IntakeCube();
315
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700316 if (!splines[2].WaitForSplineDistanceRemaining(0.05)) return;
317 AOS_LOG(
318 INFO, "Picked up second cube %lf s\n",
319 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800320
321 // Drive back to grid and place object on mid level
322 if (!splines[3].WaitForPlan()) return;
323 splines[3].Start();
324
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700325 AOS_LOG(
326 INFO, "Driving back %lf s\n",
327 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
328
Maxwell Henderson64f37452023-03-11 13:39:21 -0800329 MidCubeScore();
330
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700331 if (!splines[3].WaitForSplineDistanceRemaining(0.05)) return;
332 AOS_LOG(
333 INFO, "Got back from second cube at %lf s\n",
334 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
Maxwell Henderson64f37452023-03-11 13:39:21 -0800335
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700336 if (!WaitForArmGoal(0.05)) return;
337
338 if (!splines[3].WaitForSplineDistanceRemaining(0.05)) return;
Maxwell Henderson64f37452023-03-11 13:39:21 -0800339 Spit();
340
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700341 if (!splines[3].WaitForSplineDistanceRemaining(0.02)) return;
342
Maxwell Henderson64f37452023-03-11 13:39:21 -0800343 AOS_LOG(
344 INFO, "Placed second cube %lf s\n",
345 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700346 InitializeEncoders();
Maxwell Henderson64f37452023-03-11 13:39:21 -0800347
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700348 const ProfileParametersT kDrive = MakeProfileParameters(2.0, 4.0);
349 const ProfileParametersT kTurn = MakeProfileParameters(3.0, 4.5);
350 StartDrive(0.0, 0.0, kDrive, kTurn);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800351
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700352 {
353 double side_scalar = (alliance_ == aos::Alliance::kRed) ? 1.0 : -1.0;
354 StartDrive(6.33 - std::abs(X()), 0.0, kDrive, kTurn);
355 if (!WaitForDriveProfileNear(0.01)) return;
356
357 AOS_LOG(
358 INFO, "Done backing up %lf s\n",
359 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
360
361 const ProfileParametersT kInPlaceTurn = MakeProfileParameters(2.0, 4.5);
362 StartDrive(0.0, aos::math::NormalizeAngle(M_PI / 2.0 - Theta()), kDrive,
363 kInPlaceTurn);
364
365 std::this_thread::sleep_for(chrono::milliseconds(400));
366 StopSpitting();
367
368 AOS_LOG(
369 INFO, "Roller off %lf s\n",
370 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
371
372 Balance();
373 if (!WaitForTurnProfileNear(0.03)) return;
374
375 AOS_LOG(
376 INFO, "Done turning %lf s\n",
377 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
378
379 const ProfileParametersT kDrive = MakeProfileParameters(1.4, 3.0);
380 const ProfileParametersT kFinalTurn = MakeProfileParameters(3.0, 4.5);
381 const double kDriveDistance = 3.12;
382 StartDrive(-kDriveDistance, 0.0, kDrive, kFinalTurn);
383
384 const ProfileParametersT kFastTurn = MakeProfileParameters(5.0, 8.0);
385 if (!WaitForDriveProfileNear(kDriveDistance - 0.4)) return;
386
387 AOS_LOG(
388 INFO, "Turning %lf s\n",
389 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
390 StartDrive(0.0, -side_scalar * M_PI / 2.0, kDrive, kFastTurn);
391 if (!WaitForDriveProfileDone()) return;
392 if (!WaitForTurnProfileDone()) return;
393 AOS_LOG(
394 INFO, "Done %lf s\n",
395 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
396 }
Maxwell Henderson64f37452023-03-11 13:39:21 -0800397}
398
399void AutonomousActor::SendSuperstructureGoal() {
400 auto builder = superstructure_goal_sender_.MakeBuilder();
401
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700402 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
403 wrist_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
404 *builder.fbb(), wrist_goal_,
405 CreateProfileParameters(*builder.fbb(), 12.0, 90.0));
406
Maxwell Henderson64f37452023-03-11 13:39:21 -0800407 control_loops::superstructure::Goal::Builder superstructure_builder =
408 builder.MakeBuilder<control_loops::superstructure::Goal>();
409
410 superstructure_builder.add_arm_goal_position(arm_goal_position_);
411 superstructure_builder.add_preloaded_with_cone(preloaded_);
412 superstructure_builder.add_roller_goal(roller_goal_);
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700413 superstructure_builder.add_wrist(wrist_offset);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800414
415 if (builder.Send(superstructure_builder.Finish()) !=
416 aos::RawSender::Error::kOk) {
417 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
418 }
419}
420
421[[nodiscard]] bool AutonomousActor::WaitForPreloaded() {
422 set_preloaded(true);
423 SendSuperstructureGoal();
424
425 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
426 event_loop()->monotonic_now(),
427 ActorBase::kLoopOffset);
428
429 bool loaded = false;
430 while (!loaded) {
431 if (ShouldCancel()) {
432 return false;
433 }
434
435 phased_loop.SleepUntilNext();
436 superstructure_status_fetcher_.Fetch();
437 CHECK(superstructure_status_fetcher_.get() != nullptr);
438
439 loaded = (superstructure_status_fetcher_->end_effector_state() ==
440 control_loops::superstructure::EndEffectorState::LOADED);
441 }
442
443 set_preloaded(false);
444 SendSuperstructureGoal();
445
446 return true;
447}
448
449void AutonomousActor::MidConeScore() {
450 set_arm_goal_position(
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700451 control_loops::superstructure::arm::ScoreFrontMidConeUpAutoIndex());
452 set_wrist_goal(0.0);
453 SendSuperstructureGoal();
454}
455
456void AutonomousActor::Neutral() {
457 set_arm_goal_position(control_loops::superstructure::arm::NeutralIndex());
458 set_wrist_goal(0.0);
459 SendSuperstructureGoal();
460}
461
462void AutonomousActor::Balance() {
463 set_arm_goal_position(
464 control_loops::superstructure::arm::GroundPickupFrontConeUpIndex());
465 set_wrist_goal(0.0);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800466 SendSuperstructureGoal();
467}
468
469void AutonomousActor::HighCubeScore() {
470 set_arm_goal_position(
471 control_loops::superstructure::arm::ScoreFrontHighCubeIndex());
472 set_wrist_goal(0.6);
473 SendSuperstructureGoal();
474}
475
476void AutonomousActor::MidCubeScore() {
477 set_arm_goal_position(
478 control_loops::superstructure::arm::ScoreFrontMidCubeIndex());
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700479 set_wrist_goal(1.0);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800480 SendSuperstructureGoal();
481}
482
483void AutonomousActor::PickupCube() {
484 set_arm_goal_position(
485 control_loops::superstructure::arm::GroundPickupBackCubeIndex());
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700486 set_wrist_goal(1.0);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800487 SendSuperstructureGoal();
488}
489
490void AutonomousActor::Spit() {
491 set_roller_goal(control_loops::superstructure::RollerGoal::SPIT);
492 SendSuperstructureGoal();
493}
494
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700495void AutonomousActor::StopSpitting() {
496 set_roller_goal(control_loops::superstructure::RollerGoal::IDLE);
497 SendSuperstructureGoal();
498}
499
Maxwell Henderson64f37452023-03-11 13:39:21 -0800500void AutonomousActor::IntakeCube() {
501 set_roller_goal(control_loops::superstructure::RollerGoal::INTAKE_CUBE);
502 SendSuperstructureGoal();
503}
504
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700505[[nodiscard]] bool AutonomousActor::WaitForArmGoal(double distance_to_go) {
506 constexpr double kEpsTheta = 0.10;
Maxwell Henderson64f37452023-03-11 13:39:21 -0800507
508 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
509 event_loop()->monotonic_now(),
510 ActorBase::kLoopOffset);
511
512 bool at_goal = false;
513 while (!at_goal) {
514 if (ShouldCancel()) {
515 return false;
516 }
517
518 phased_loop.SleepUntilNext();
519 superstructure_status_fetcher_.Fetch();
520 CHECK(superstructure_status_fetcher_.get() != nullptr);
521
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700522 at_goal = (arm_goal_position_ ==
523 superstructure_status_fetcher_->arm()->current_node() &&
524 superstructure_status_fetcher_->arm()->path_distance_to_go() <
525 distance_to_go) &&
Maxwell Henderson64f37452023-03-11 13:39:21 -0800526 (std::abs(wrist_goal_ -
527 superstructure_status_fetcher_->wrist()->position()) <
528 kEpsTheta);
529 }
530
Maxwell Henderson64f37452023-03-11 13:39:21 -0800531 return true;
532}
533
534} // namespace autonomous
Maxwell Hendersonad312342023-01-10 12:07:47 -0800535} // namespace y2023