blob: 3fcb5ec13a4d01cc743d26c9b64d4850d5b069d2 [file] [log] [blame]
milind-u086d7262022-01-19 20:44:18 -08001#include "y2022/actors/autonomous_actor.h"
2
3#include <chrono>
4#include <cinttypes>
5#include <cmath>
6
7#include "aos/logging/logging.h"
Ravago Jones81e50632022-03-11 16:23:51 -08008#include "aos/network/team_number.h"
9#include "aos/util/math.h"
milind-u086d7262022-01-19 20:44:18 -080010#include "frc971/control_loops/drivetrain/localizer_generated.h"
Ravago Jones81e50632022-03-11 16:23:51 -080011#include "y2022/actors/auto_splines.h"
12#include "y2022/constants.h"
milind-u086d7262022-01-19 20:44:18 -080013#include "y2022/control_loops/drivetrain/drivetrain_base.h"
14
Ravago Jones81e50632022-03-11 16:23:51 -080015DEFINE_bool(spline_auto, false, "If true, define a spline autonomous mode");
Austin Schuh9651c5a2022-04-17 19:12:42 -070016DEFINE_bool(rapid_react, true,
Milind Upadhyaya7793962022-03-11 19:39:36 -080017 "If true, run the main rapid react autonomous mode");
Austin Schuh9651c5a2022-04-17 19:12:42 -070018DEFINE_bool(rapid_react_two, false,
Henry Speiser5eed1de2022-04-07 21:52:10 -070019 "If true, run the two ball rapid react autonomous mode");
Ravago Jones81e50632022-03-11 16:23:51 -080020
Stephan Pleinesf63bde82024-01-13 15:59:33 -080021namespace y2022::actors {
Ravago Jones81e50632022-03-11 16:23:51 -080022namespace {
Henry Speisere23d4de2022-04-05 16:47:47 -070023constexpr double kExtendIntakeGoal = -0.10;
Ravago Jones81e50632022-03-11 16:23:51 -080024constexpr double kRetractIntakeGoal = 1.47;
Austin Schuh42d7e5f2022-03-16 23:35:09 -070025constexpr double kIntakeRollerVoltage = 12.0;
Ravago Jones81e50632022-03-11 16:23:51 -080026constexpr double kRollerVoltage = 12.0;
27constexpr double kCatapultReturnPosition = -0.908;
28} // namespace
milind-u086d7262022-01-19 20:44:18 -080029
30using ::aos::monotonic_clock;
Ravago Jones81e50632022-03-11 16:23:51 -080031using frc971::CreateProfileParameters;
milind-u086d7262022-01-19 20:44:18 -080032using ::frc971::ProfileParametersT;
Ravago Jones81e50632022-03-11 16:23:51 -080033using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal;
34using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal;
Nathan Leongdd728002024-02-03 15:26:53 -080035using frc971::control_loops::catapult::CatapultGoal;
milind-u086d7262022-01-19 20:44:18 -080036using frc971::control_loops::drivetrain::LocalizerControl;
Ravago Jones81e50632022-03-11 16:23:51 -080037
milind-u086d7262022-01-19 20:44:18 -080038namespace chrono = ::std::chrono;
39
40AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
41 : frc971::autonomous::BaseAutonomousActor(
Ravago Jones81e50632022-03-11 16:23:51 -080042 event_loop, control_loops::drivetrain::GetDrivetrainConfig()),
43 localizer_control_sender_(
44 event_loop->MakeSender<
45 ::frc971::control_loops::drivetrain::LocalizerControl>(
46 "/drivetrain")),
47 superstructure_goal_sender_(
48 event_loop->MakeSender<control_loops::superstructure::Goal>(
49 "/superstructure")),
50 superstructure_status_fetcher_(
51 event_loop->MakeFetcher<control_loops::superstructure::Status>(
52 "/superstructure")),
53 joystick_state_fetcher_(
54 event_loop->MakeFetcher<aos::JoystickState>("/aos")),
55 robot_state_fetcher_(event_loop->MakeFetcher<aos::RobotState>("/aos")),
56 auto_splines_() {
57 set_max_drivetrain_voltage(12.0);
58 replan_timer_ = event_loop->AddTimer([this]() { Replan(); });
59 event_loop->OnRun([this, event_loop]() {
Philipp Schradera6712522023-07-05 20:25:11 -070060 replan_timer_->Schedule(event_loop->monotonic_now());
61 button_poll_->Schedule(event_loop->monotonic_now(),
62 chrono::milliseconds(50));
Ravago Jones81e50632022-03-11 16:23:51 -080063 });
64
65 button_poll_ = event_loop->AddTimer([this]() {
66 const aos::monotonic_clock::time_point now =
67 this->event_loop()->context().monotonic_event_time;
68 if (robot_state_fetcher_.Fetch()) {
69 if (robot_state_fetcher_->user_button()) {
70 user_indicated_safe_to_reset_ = true;
71 MaybeSendStartingPosition();
72 }
73 }
74 if (joystick_state_fetcher_.Fetch()) {
75 if (joystick_state_fetcher_->has_alliance() &&
76 (joystick_state_fetcher_->alliance() != alliance_)) {
77 alliance_ = joystick_state_fetcher_->alliance();
78 is_planned_ = false;
79 // Only kick the planning out by 2 seconds. If we end up enabled in that
80 // second, then we will kick it out further based on the code below.
Philipp Schradera6712522023-07-05 20:25:11 -070081 replan_timer_->Schedule(now + std::chrono::seconds(2));
Ravago Jones81e50632022-03-11 16:23:51 -080082 }
83 if (joystick_state_fetcher_->enabled()) {
84 if (!is_planned_) {
85 // Only replan once we've been disabled for 5 seconds.
Philipp Schradera6712522023-07-05 20:25:11 -070086 replan_timer_->Schedule(now + std::chrono::seconds(5));
Ravago Jones81e50632022-03-11 16:23:51 -080087 }
88 }
89 }
90 });
91}
92
93void AutonomousActor::Replan() {
94 LOG(INFO) << "Alliance " << static_cast<int>(alliance_);
95 if (alliance_ == aos::Alliance::kInvalid) {
96 return;
97 }
98 sent_starting_position_ = false;
99 if (FLAGS_spline_auto) {
100 test_spline_ =
101 PlanSpline(std::bind(&AutonomousSplines::TestSpline, &auto_splines_,
102 std::placeholders::_1, alliance_),
103 SplineDirection::kForward);
104
105 starting_position_ = test_spline_->starting_position();
Milind Upadhyaya7793962022-03-11 19:39:36 -0800106 } else if (FLAGS_rapid_react) {
107 rapid_react_splines_ = {
108 PlanSpline(std::bind(&AutonomousSplines::Spline1, &auto_splines_,
109 std::placeholders::_1, alliance_),
Henry Speiser09e8da92022-03-14 20:58:45 -0700110 SplineDirection::kBackward),
Milind Upadhyaya7793962022-03-11 19:39:36 -0800111 PlanSpline(std::bind(&AutonomousSplines::Spline2, &auto_splines_,
112 std::placeholders::_1, alliance_),
Henry Speisere23d4de2022-04-05 16:47:47 -0700113 SplineDirection::kBackward),
Milind Upadhyaya7793962022-03-11 19:39:36 -0800114 PlanSpline(std::bind(&AutonomousSplines::Spline3, &auto_splines_,
115 std::placeholders::_1, alliance_),
Henry Speisere23d4de2022-04-05 16:47:47 -0700116 SplineDirection::kForward)};
Milind Upadhyaya7793962022-03-11 19:39:36 -0800117 starting_position_ = rapid_react_splines_.value()[0].starting_position();
118 CHECK(starting_position_);
Henry Speiser5eed1de2022-04-07 21:52:10 -0700119 } else if (FLAGS_rapid_react_two) {
120 rapid_react_two_spline_ = {
Austin Schuhf3413b72022-04-16 19:09:33 -0700121 PlanSpline(std::bind(&AutonomousSplines::SplineTwoBall1, &auto_splines_,
Henry Speiser5eed1de2022-04-07 21:52:10 -0700122 std::placeholders::_1, alliance_),
Austin Schuhf3413b72022-04-16 19:09:33 -0700123 SplineDirection::kBackward),
124 PlanSpline(std::bind(&AutonomousSplines::SplineTwoBall2, &auto_splines_,
125 std::placeholders::_1, alliance_),
126 SplineDirection::kForward)};
Henry Speiser5eed1de2022-04-07 21:52:10 -0700127 starting_position_ = rapid_react_two_spline_.value()[0].starting_position();
128 CHECK(starting_position_);
Ravago Jones81e50632022-03-11 16:23:51 -0800129 }
130
131 is_planned_ = true;
132
133 MaybeSendStartingPosition();
134}
135
136void AutonomousActor::MaybeSendStartingPosition() {
137 if (is_planned_ && user_indicated_safe_to_reset_ &&
138 !sent_starting_position_) {
139 CHECK(starting_position_);
140 SendStartingPosition(starting_position_.value());
141 }
142}
milind-u086d7262022-01-19 20:44:18 -0800143
144void AutonomousActor::Reset() {
145 InitializeEncoders();
146 ResetDrivetrain();
Ravago Jones81e50632022-03-11 16:23:51 -0800147 RetractFrontIntake();
148 RetractBackIntake();
149
150 joystick_state_fetcher_.Fetch();
151 CHECK(joystick_state_fetcher_.get() != nullptr)
152 << "Expect at least one JoystickState message before running auto...";
153 alliance_ = joystick_state_fetcher_->alliance();
milind-u086d7262022-01-19 20:44:18 -0800154}
155
156bool AutonomousActor::RunAction(
157 const ::frc971::autonomous::AutonomousActionParams *params) {
158 Reset();
Ravago Jones81e50632022-03-11 16:23:51 -0800159 if (!user_indicated_safe_to_reset_) {
160 AOS_LOG(WARNING, "Didn't send starting position prior to starting auto.");
161 CHECK(starting_position_);
162 SendStartingPosition(starting_position_.value());
163 }
164 // Clear this so that we don't accidentally resend things as soon as we replan
165 // later.
166 user_indicated_safe_to_reset_ = false;
167 is_planned_ = false;
168 starting_position_.reset();
milind-u086d7262022-01-19 20:44:18 -0800169
170 AOS_LOG(INFO, "Params are %d\n", params->mode());
Ravago Jones81e50632022-03-11 16:23:51 -0800171 if (alliance_ == aos::Alliance::kInvalid) {
172 AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection.");
173 return false;
174 }
175 if (FLAGS_spline_auto) {
176 SplineAuto();
Milind Upadhyaya7793962022-03-11 19:39:36 -0800177 } else if (FLAGS_rapid_react) {
178 RapidReact();
Henry Speiser5eed1de2022-04-07 21:52:10 -0700179 } else if (FLAGS_rapid_react_two) {
180 RapidReactTwo();
Ravago Jones81e50632022-03-11 16:23:51 -0800181 }
182
milind-u086d7262022-01-19 20:44:18 -0800183 return true;
184}
185
Ravago Jones81e50632022-03-11 16:23:51 -0800186void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) {
187 // Set up the starting position for the blue alliance.
188
189 // TODO(james): Resetting the localizer breaks the left/right statespace
190 // controller. That is a bug, but we can fix that later by not resetting.
191 auto builder = localizer_control_sender_.MakeBuilder();
192
193 LocalizerControl::Builder localizer_control_builder =
194 builder.MakeBuilder<LocalizerControl>();
195 localizer_control_builder.add_x(start(0));
196 localizer_control_builder.add_y(start(1));
197 localizer_control_builder.add_theta(start(2));
198 localizer_control_builder.add_theta_uncertainty(0.00001);
199 LOG(INFO) << "User button pressed, x: " << start(0) << " y: " << start(1)
200 << " theta: " << start(2);
201 if (builder.Send(localizer_control_builder.Finish()) !=
202 aos::RawSender::Error::kOk) {
203 AOS_LOG(ERROR, "Failed to reset localizer.\n");
204 }
205}
206
207void AutonomousActor::SplineAuto() {
208 CHECK(test_spline_);
209
210 if (!test_spline_->WaitForPlan()) return;
211 test_spline_->Start();
212
213 if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return;
214}
215
Milind Upadhyaya7793962022-03-11 19:39:36 -0800216void AutonomousActor::RapidReact() {
217 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
218
219 CHECK(rapid_react_splines_);
220
221 auto &splines = *rapid_react_splines_;
222
223 // Tell the superstructure a ball was preloaded
Milind Upadhyaya7793962022-03-11 19:39:36 -0800224 if (!WaitForPreloaded()) return;
Henry Speiser09e8da92022-03-14 20:58:45 -0700225
Henry Speisere23d4de2022-04-05 16:47:47 -0700226 // Fire preloaded ball while driving
Henry Speiser09e8da92022-03-14 20:58:45 -0700227 set_fire_at_will(true);
228 SendSuperstructureGoal();
Milind Upadhyayf35bb112022-03-16 23:08:04 -0700229 if (!WaitForBallsShot()) return;
Austin Schuh42d7e5f2022-03-16 23:35:09 -0700230 LOG(INFO) << "Shot first ball "
231 << chrono::duration<double>(aos::monotonic_clock::now() -
232 start_time)
233 .count()
234 << 's';
Henry Speiser09e8da92022-03-14 20:58:45 -0700235 set_fire_at_will(false);
236 SendSuperstructureGoal();
237
Henry Speisere23d4de2022-04-05 16:47:47 -0700238 // Drive and intake the ball nearest to the starting zone.
239 // Fire while moving.
Henry Speiser09e8da92022-03-14 20:58:45 -0700240 ExtendBackIntake();
Milind Upadhyaya7793962022-03-11 19:39:36 -0800241 if (!splines[0].WaitForPlan()) return;
242 splines[0].Start();
Henry Speisere23d4de2022-04-05 16:47:47 -0700243 // Distance before we don't shoot while moving.
Austin Schuh9651c5a2022-04-17 19:12:42 -0700244 if (!splines[0].WaitForSplineDistanceRemaining(2.1)) return;
245 LOG(INFO) << "Tring to take the shot";
Milind Upadhyaya7793962022-03-11 19:39:36 -0800246
Milind Upadhyaya7793962022-03-11 19:39:36 -0800247 set_fire_at_will(true);
248 SendSuperstructureGoal();
Henry Speisere23d4de2022-04-05 16:47:47 -0700249
250 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
251
Austin Schuh9651c5a2022-04-17 19:12:42 -0700252 std::this_thread::sleep_for(std::chrono::milliseconds(500));
Henry Speisere23d4de2022-04-05 16:47:47 -0700253
254 // Fire the last ball we picked up when stopped.
255 SendSuperstructureGoal();
256 LOG(INFO) << "Close";
Milind Upadhyayf35bb112022-03-16 23:08:04 -0700257 if (!WaitForBallsShot()) return;
Austin Schuh42d7e5f2022-03-16 23:35:09 -0700258 LOG(INFO) << "Shot first 3 balls "
259 << chrono::duration<double>(aos::monotonic_clock::now() -
260 start_time)
261 .count()
262 << 's';
Milind Upadhyaya7793962022-03-11 19:39:36 -0800263
264 // Drive to the human player station while intaking two balls.
265 // Once is already placed down,
266 // and one will be rolled to the robot by the human player
Henry Speiser09e8da92022-03-14 20:58:45 -0700267 if (!splines[1].WaitForPlan()) return;
268 splines[1].Start();
Henry Speisere23d4de2022-04-05 16:47:47 -0700269
270 std::this_thread::sleep_for(std::chrono::milliseconds(1500));
271
272 set_fire_at_will(false);
273 SendSuperstructureGoal();
274
Henry Speiser09e8da92022-03-14 20:58:45 -0700275 if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return;
Henry Speisere23d4de2022-04-05 16:47:47 -0700276 std::this_thread::sleep_for(std::chrono::milliseconds(500));
Austin Schuh42d7e5f2022-03-16 23:35:09 -0700277 LOG(INFO) << "At balls 4/5 "
278 << chrono::duration<double>(aos::monotonic_clock::now() -
279 start_time)
280 .count()
281 << 's';
Milind Upadhyaya7793962022-03-11 19:39:36 -0800282
283 // Drive to the shooting position
Henry Speiser09e8da92022-03-14 20:58:45 -0700284 if (!splines[2].WaitForPlan()) return;
285 splines[2].Start();
286 if (!splines[2].WaitForSplineDistanceRemaining(2.00)) return;
287 RetractFrontIntake();
288
289 if (!splines[2].WaitForSplineDistanceRemaining(0.02)) return;
Austin Schuh42d7e5f2022-03-16 23:35:09 -0700290 LOG(INFO) << "Shooting last balls "
291 << chrono::duration<double>(aos::monotonic_clock::now() -
292 start_time)
293 .count()
294 << 's';
Milind Upadhyaya7793962022-03-11 19:39:36 -0800295
296 // Fire the two balls once we stopped
297 set_fire_at_will(true);
298 SendSuperstructureGoal();
Milind Upadhyayf35bb112022-03-16 23:08:04 -0700299 if (!WaitForBallsShot()) return;
Milind Upadhyaya7793962022-03-11 19:39:36 -0800300
301 LOG(INFO) << "Took "
302 << chrono::duration<double>(aos::monotonic_clock::now() -
303 start_time)
304 .count()
305 << 's';
Austin Schuh9651c5a2022-04-17 19:12:42 -0700306 std::this_thread::sleep_for(std::chrono::milliseconds(500));
307 set_fire_at_will(false);
308 SendSuperstructureGoal();
Milind Upadhyaya7793962022-03-11 19:39:36 -0800309}
310
Henry Speiser5eed1de2022-04-07 21:52:10 -0700311// Rapid React Two Ball Autonomous.
312void AutonomousActor::RapidReactTwo() {
313 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
314
315 CHECK(rapid_react_two_spline_);
316
317 auto &splines = *rapid_react_two_spline_;
318
319 // Tell the superstructure a ball was preloaded
320 if (!WaitForPreloaded()) return;
321 set_fire_at_will(true);
322 SendSuperstructureGoal();
323 if (!WaitForBallsShot()) return;
324 LOG(INFO) << "Shot first ball "
325 << chrono::duration<double>(aos::monotonic_clock::now() -
326 start_time)
327 .count()
328 << 's';
329 set_fire_at_will(false);
330 SendSuperstructureGoal();
331
332 ExtendBackIntake();
333 if (!splines[0].WaitForPlan()) return;
334 splines[0].Start();
335 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
336
Austin Schuhf3413b72022-04-16 19:09:33 -0700337 std::this_thread::sleep_for(std::chrono::milliseconds(300));
338
339 if (!splines[1].WaitForPlan()) return;
340 splines[1].Start();
341 if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return;
342 std::this_thread::sleep_for(std::chrono::milliseconds(500));
343
Henry Speiser5eed1de2022-04-07 21:52:10 -0700344 // Fire the ball once we stopped
Henry Speiser5eed1de2022-04-07 21:52:10 -0700345 set_fire_at_will(true);
346 SendSuperstructureGoal();
347 if (!WaitForBallsShot()) return;
348 LOG(INFO) << "Shot last ball "
349 << chrono::duration<double>(aos::monotonic_clock::now() -
350 start_time)
351 .count()
352 << 's';
353 set_fire_at_will(false);
Austin Schuhf3413b72022-04-16 19:09:33 -0700354 RetractBackIntake();
Henry Speiser5eed1de2022-04-07 21:52:10 -0700355 SendSuperstructureGoal();
356}
357
Milind Upadhyaya7793962022-03-11 19:39:36 -0800358[[nodiscard]] bool AutonomousActor::WaitForPreloaded() {
Milind Upadhyay803bbf02022-03-11 17:56:26 -0800359 set_preloaded(true);
360 SendSuperstructureGoal();
361
362 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
363 event_loop()->monotonic_now(),
Stephan Pleines743f83a2024-02-02 18:32:09 -0800364 aos::common::actions::kLoopOffset);
Milind Upadhyay803bbf02022-03-11 17:56:26 -0800365
366 bool loaded = false;
367 while (!loaded) {
368 if (ShouldCancel()) {
369 return false;
370 }
371
372 phased_loop.SleepUntilNext();
373 superstructure_status_fetcher_.Fetch();
374 CHECK(superstructure_status_fetcher_.get() != nullptr);
375
376 loaded = (superstructure_status_fetcher_->state() ==
377 control_loops::superstructure::SuperstructureState::LOADED);
378 }
379
380 set_preloaded(false);
381 SendSuperstructureGoal();
382
383 return true;
384}
385
Ravago Jones81e50632022-03-11 16:23:51 -0800386void AutonomousActor::SendSuperstructureGoal() {
387 auto builder = superstructure_goal_sender_.MakeBuilder();
388
389 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
390 intake_front_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
391 *builder.fbb(), intake_front_goal_,
392 CreateProfileParameters(*builder.fbb(), 20.0, 60.0));
393
394 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
395 intake_back_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
396 *builder.fbb(), intake_back_goal_,
397 CreateProfileParameters(*builder.fbb(), 20.0, 60.0));
398
399 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
400 catapult_return_position_offset =
401 CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
402 *builder.fbb(), kCatapultReturnPosition,
403 CreateProfileParameters(*builder.fbb(), 9.0, 50.0));
404
Nathan Leongdd728002024-02-03 15:26:53 -0800405 CatapultGoal::Builder catapult_goal_builder(*builder.fbb());
Ravago Jones81e50632022-03-11 16:23:51 -0800406 catapult_goal_builder.add_shot_position(0.03);
407 catapult_goal_builder.add_shot_velocity(18.0);
408 catapult_goal_builder.add_return_position(catapult_return_position_offset);
Nathan Leongdd728002024-02-03 15:26:53 -0800409 flatbuffers::Offset<CatapultGoal> catapult_goal_offset =
Ravago Jones81e50632022-03-11 16:23:51 -0800410 catapult_goal_builder.Finish();
411
412 superstructure::Goal::Builder superstructure_builder =
413 builder.MakeBuilder<superstructure::Goal>();
414
415 superstructure_builder.add_intake_front(intake_front_offset);
416 superstructure_builder.add_intake_back(intake_back_offset);
Austin Schuh42d7e5f2022-03-16 23:35:09 -0700417 superstructure_builder.add_roller_speed_compensation(0.0);
Ravago Jones81e50632022-03-11 16:23:51 -0800418 superstructure_builder.add_roller_speed_front(roller_front_voltage_);
419 superstructure_builder.add_roller_speed_back(roller_back_voltage_);
Milind Upadhyayd86b02c2022-03-13 20:57:46 -0700420 if (requested_intake_.has_value()) {
421 superstructure_builder.add_turret_intake(*requested_intake_);
422 }
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700423 superstructure_builder.add_transfer_roller_speed(transfer_roller_voltage_);
Ravago Jones81e50632022-03-11 16:23:51 -0800424 superstructure_builder.add_catapult(catapult_goal_offset);
425 superstructure_builder.add_fire(fire_);
Milind Upadhyay803bbf02022-03-11 17:56:26 -0800426 superstructure_builder.add_preloaded(preloaded_);
Austin Schuh42d7e5f2022-03-16 23:35:09 -0700427 superstructure_builder.add_auto_aim(true);
Ravago Jones81e50632022-03-11 16:23:51 -0800428
429 if (builder.Send(superstructure_builder.Finish()) !=
430 aos::RawSender::Error::kOk) {
431 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
432 }
433}
434
435void AutonomousActor::ExtendFrontIntake() {
Milind Upadhyayd86b02c2022-03-13 20:57:46 -0700436 set_requested_intake(RequestedIntake::kFront);
Ravago Jones81e50632022-03-11 16:23:51 -0800437 set_intake_front_goal(kExtendIntakeGoal);
Austin Schuhe1264372022-03-13 20:22:36 -0700438 set_roller_front_voltage(kIntakeRollerVoltage);
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700439 set_transfer_roller_voltage(kRollerVoltage);
Ravago Jones81e50632022-03-11 16:23:51 -0800440 SendSuperstructureGoal();
441}
442
443void AutonomousActor::RetractFrontIntake() {
Milind Upadhyayd86b02c2022-03-13 20:57:46 -0700444 set_requested_intake(std::nullopt);
Ravago Jones81e50632022-03-11 16:23:51 -0800445 set_intake_front_goal(kRetractIntakeGoal);
Austin Schuhe1264372022-03-13 20:22:36 -0700446 set_roller_front_voltage(0.0);
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700447 set_transfer_roller_voltage(0.0);
Ravago Jones81e50632022-03-11 16:23:51 -0800448 SendSuperstructureGoal();
449}
450
451void AutonomousActor::ExtendBackIntake() {
Milind Upadhyayd86b02c2022-03-13 20:57:46 -0700452 set_requested_intake(RequestedIntake::kBack);
Ravago Jones81e50632022-03-11 16:23:51 -0800453 set_intake_back_goal(kExtendIntakeGoal);
Austin Schuhe1264372022-03-13 20:22:36 -0700454 set_roller_back_voltage(kIntakeRollerVoltage);
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700455 set_transfer_roller_voltage(-kRollerVoltage);
Ravago Jones81e50632022-03-11 16:23:51 -0800456 SendSuperstructureGoal();
457}
458
459void AutonomousActor::RetractBackIntake() {
Milind Upadhyayd86b02c2022-03-13 20:57:46 -0700460 set_requested_intake(std::nullopt);
Ravago Jones81e50632022-03-11 16:23:51 -0800461 set_intake_back_goal(kRetractIntakeGoal);
Austin Schuhe1264372022-03-13 20:22:36 -0700462 set_roller_back_voltage(0.0);
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700463 set_transfer_roller_voltage(0.0);
Ravago Jones81e50632022-03-11 16:23:51 -0800464 SendSuperstructureGoal();
465}
466
Milind Upadhyayf35bb112022-03-16 23:08:04 -0700467[[nodiscard]] bool AutonomousActor::WaitForBallsShot() {
Austin Schuh42d7e5f2022-03-16 23:35:09 -0700468 superstructure_status_fetcher_.Fetch();
469 CHECK(superstructure_status_fetcher_.get());
Milind Upadhyayf35bb112022-03-16 23:08:04 -0700470
Ravago Jonesf699d1c2022-03-11 18:39:56 -0800471 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
472 event_loop()->monotonic_now(),
Stephan Pleines743f83a2024-02-02 18:32:09 -0800473 aos::common::actions::kLoopOffset);
Ravago Jonesf699d1c2022-03-11 18:39:56 -0800474 superstructure_status_fetcher_.Fetch();
475 CHECK(superstructure_status_fetcher_.get() != nullptr);
Henry Speisere23d4de2022-04-05 16:47:47 -0700476
Ravago Jonesf699d1c2022-03-11 18:39:56 -0800477 while (true) {
478 if (ShouldCancel()) {
479 return false;
480 }
481 phased_loop.SleepUntilNext();
482 superstructure_status_fetcher_.Fetch();
483 CHECK(superstructure_status_fetcher_.get() != nullptr);
Henry Speisere23d4de2022-04-05 16:47:47 -0700484
485 if (!superstructure_status_fetcher_->front_intake_has_ball() &&
486 !superstructure_status_fetcher_->back_intake_has_ball() &&
487 superstructure_status_fetcher_->state() ==
488 control_loops::superstructure::SuperstructureState::IDLE) {
Ravago Jonesf699d1c2022-03-11 18:39:56 -0800489 return true;
490 }
491 }
492}
493
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800494} // namespace y2022::actors