blob: 26197e32cfcf507656cbe0d75cefd2e54012b70e [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
Austin Schuh700bfff2023-04-05 19:45:55 -0700331 if (!splines[3].WaitForSplineDistanceRemaining(0.07)) return;
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700332 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;
Maxwell Henderson64f37452023-03-11 13:39:21 -0800337 Spit();
338
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700339 if (!splines[3].WaitForSplineDistanceRemaining(0.02)) return;
340
Maxwell Henderson64f37452023-03-11 13:39:21 -0800341 AOS_LOG(
342 INFO, "Placed second cube %lf s\n",
343 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700344 InitializeEncoders();
Maxwell Henderson64f37452023-03-11 13:39:21 -0800345
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700346 const ProfileParametersT kDrive = MakeProfileParameters(2.0, 4.0);
347 const ProfileParametersT kTurn = MakeProfileParameters(3.0, 4.5);
348 StartDrive(0.0, 0.0, kDrive, kTurn);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800349
Austin Schuh700bfff2023-04-05 19:45:55 -0700350 std::this_thread::sleep_for(chrono::milliseconds(100));
351
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
Austin Schuh700bfff2023-04-05 19:45:55 -0700361 const ProfileParametersT kInPlaceTurn = MakeProfileParameters(2.5, 7.0);
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700362 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
Austin Schuh700bfff2023-04-05 19:45:55 -0700372 if (!WaitForTurnProfileNear(0.6)) return;
373 AOS_LOG(
374 INFO, "Balance arm %lf s\n",
375 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
376
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700377 Balance();
Austin Schuh700bfff2023-04-05 19:45:55 -0700378 if (!WaitForTurnProfileNear(0.001)) return;
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700379
380 AOS_LOG(
381 INFO, "Done turning %lf s\n",
382 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
383
384 const ProfileParametersT kDrive = MakeProfileParameters(1.4, 3.0);
385 const ProfileParametersT kFinalTurn = MakeProfileParameters(3.0, 4.5);
Austin Schuh700bfff2023-04-05 19:45:55 -0700386 const double kDriveDistance = 3.11;
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700387 StartDrive(-kDriveDistance, 0.0, kDrive, kFinalTurn);
388
389 const ProfileParametersT kFastTurn = MakeProfileParameters(5.0, 8.0);
390 if (!WaitForDriveProfileNear(kDriveDistance - 0.4)) return;
391
392 AOS_LOG(
393 INFO, "Turning %lf s\n",
394 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
395 StartDrive(0.0, -side_scalar * M_PI / 2.0, kDrive, kFastTurn);
396 if (!WaitForDriveProfileDone()) return;
397 if (!WaitForTurnProfileDone()) return;
398 AOS_LOG(
399 INFO, "Done %lf s\n",
400 aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time));
401 }
Maxwell Henderson64f37452023-03-11 13:39:21 -0800402}
403
404void AutonomousActor::SendSuperstructureGoal() {
405 auto builder = superstructure_goal_sender_.MakeBuilder();
406
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700407 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
408 wrist_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
409 *builder.fbb(), wrist_goal_,
410 CreateProfileParameters(*builder.fbb(), 12.0, 90.0));
411
Maxwell Henderson64f37452023-03-11 13:39:21 -0800412 control_loops::superstructure::Goal::Builder superstructure_builder =
413 builder.MakeBuilder<control_loops::superstructure::Goal>();
414
415 superstructure_builder.add_arm_goal_position(arm_goal_position_);
416 superstructure_builder.add_preloaded_with_cone(preloaded_);
417 superstructure_builder.add_roller_goal(roller_goal_);
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700418 superstructure_builder.add_wrist(wrist_offset);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800419
420 if (builder.Send(superstructure_builder.Finish()) !=
421 aos::RawSender::Error::kOk) {
422 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
423 }
424}
425
426[[nodiscard]] bool AutonomousActor::WaitForPreloaded() {
427 set_preloaded(true);
428 SendSuperstructureGoal();
429
430 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
431 event_loop()->monotonic_now(),
432 ActorBase::kLoopOffset);
433
434 bool loaded = false;
435 while (!loaded) {
436 if (ShouldCancel()) {
437 return false;
438 }
439
440 phased_loop.SleepUntilNext();
441 superstructure_status_fetcher_.Fetch();
442 CHECK(superstructure_status_fetcher_.get() != nullptr);
443
444 loaded = (superstructure_status_fetcher_->end_effector_state() ==
445 control_loops::superstructure::EndEffectorState::LOADED);
446 }
447
448 set_preloaded(false);
449 SendSuperstructureGoal();
450
451 return true;
452}
453
454void AutonomousActor::MidConeScore() {
455 set_arm_goal_position(
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700456 control_loops::superstructure::arm::ScoreFrontMidConeUpAutoIndex());
457 set_wrist_goal(0.0);
458 SendSuperstructureGoal();
459}
460
461void AutonomousActor::Neutral() {
462 set_arm_goal_position(control_loops::superstructure::arm::NeutralIndex());
463 set_wrist_goal(0.0);
464 SendSuperstructureGoal();
465}
466
467void AutonomousActor::Balance() {
468 set_arm_goal_position(
Austin Schuh700bfff2023-04-05 19:45:55 -0700469 control_loops::superstructure::arm::ScoreFrontLowCubeIndex());
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700470 set_wrist_goal(0.0);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800471 SendSuperstructureGoal();
472}
473
474void AutonomousActor::HighCubeScore() {
475 set_arm_goal_position(
476 control_loops::superstructure::arm::ScoreFrontHighCubeIndex());
477 set_wrist_goal(0.6);
478 SendSuperstructureGoal();
479}
480
481void AutonomousActor::MidCubeScore() {
482 set_arm_goal_position(
483 control_loops::superstructure::arm::ScoreFrontMidCubeIndex());
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700484 set_wrist_goal(1.0);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800485 SendSuperstructureGoal();
486}
487
488void AutonomousActor::PickupCube() {
489 set_arm_goal_position(
490 control_loops::superstructure::arm::GroundPickupBackCubeIndex());
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700491 set_wrist_goal(1.0);
Maxwell Henderson64f37452023-03-11 13:39:21 -0800492 SendSuperstructureGoal();
493}
494
495void AutonomousActor::Spit() {
496 set_roller_goal(control_loops::superstructure::RollerGoal::SPIT);
497 SendSuperstructureGoal();
498}
499
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700500void AutonomousActor::StopSpitting() {
501 set_roller_goal(control_loops::superstructure::RollerGoal::IDLE);
502 SendSuperstructureGoal();
503}
504
Maxwell Henderson64f37452023-03-11 13:39:21 -0800505void AutonomousActor::IntakeCube() {
506 set_roller_goal(control_loops::superstructure::RollerGoal::INTAKE_CUBE);
507 SendSuperstructureGoal();
508}
509
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700510[[nodiscard]] bool AutonomousActor::WaitForArmGoal(double distance_to_go) {
511 constexpr double kEpsTheta = 0.10;
Maxwell Henderson64f37452023-03-11 13:39:21 -0800512
513 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
514 event_loop()->monotonic_now(),
515 ActorBase::kLoopOffset);
516
517 bool at_goal = false;
518 while (!at_goal) {
519 if (ShouldCancel()) {
520 return false;
521 }
522
523 phased_loop.SleepUntilNext();
524 superstructure_status_fetcher_.Fetch();
525 CHECK(superstructure_status_fetcher_.get() != nullptr);
526
Maxwell Henderson3d0beaf2023-03-23 11:32:44 -0700527 at_goal = (arm_goal_position_ ==
528 superstructure_status_fetcher_->arm()->current_node() &&
529 superstructure_status_fetcher_->arm()->path_distance_to_go() <
530 distance_to_go) &&
Maxwell Henderson64f37452023-03-11 13:39:21 -0800531 (std::abs(wrist_goal_ -
532 superstructure_status_fetcher_->wrist()->position()) <
533 kEpsTheta);
534 }
535
Maxwell Henderson64f37452023-03-11 13:39:21 -0800536 return true;
537}
538
539} // namespace autonomous
Maxwell Hendersonad312342023-01-10 12:07:47 -0800540} // namespace y2023