blob: 4f70ec930051d9cb95ebe6e5b8f9ed507f01d45e [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
Austin Schuh99f7c6a2024-06-25 22:07:44 -07007#include "absl/flags/flag.h"
8
milind-u086d7262022-01-19 20:44:18 -08009#include "aos/logging/logging.h"
Ravago Jones81e50632022-03-11 16:23:51 -080010#include "aos/network/team_number.h"
11#include "aos/util/math.h"
milind-u086d7262022-01-19 20:44:18 -080012#include "frc971/control_loops/drivetrain/localizer_generated.h"
Ravago Jones81e50632022-03-11 16:23:51 -080013#include "y2022/actors/auto_splines.h"
14#include "y2022/constants.h"
milind-u086d7262022-01-19 20:44:18 -080015#include "y2022/control_loops/drivetrain/drivetrain_base.h"
16
Austin Schuh99f7c6a2024-06-25 22:07:44 -070017ABSL_FLAG(bool, spline_auto, false, "If true, define a spline autonomous mode");
18ABSL_FLAG(bool, rapid_react, true,
19 "If true, run the main rapid react autonomous mode");
20ABSL_FLAG(bool, rapid_react_two, false,
21 "If true, run the two ball rapid react autonomous mode");
Ravago Jones81e50632022-03-11 16:23:51 -080022
Stephan Pleinesf63bde82024-01-13 15:59:33 -080023namespace y2022::actors {
Ravago Jones81e50632022-03-11 16:23:51 -080024namespace {
Henry Speisere23d4de2022-04-05 16:47:47 -070025constexpr double kExtendIntakeGoal = -0.10;
Ravago Jones81e50632022-03-11 16:23:51 -080026constexpr double kRetractIntakeGoal = 1.47;
Austin Schuh42d7e5f2022-03-16 23:35:09 -070027constexpr double kIntakeRollerVoltage = 12.0;
Ravago Jones81e50632022-03-11 16:23:51 -080028constexpr double kRollerVoltage = 12.0;
29constexpr double kCatapultReturnPosition = -0.908;
30} // namespace
milind-u086d7262022-01-19 20:44:18 -080031
32using ::aos::monotonic_clock;
Ravago Jones81e50632022-03-11 16:23:51 -080033using frc971::CreateProfileParameters;
milind-u086d7262022-01-19 20:44:18 -080034using ::frc971::ProfileParametersT;
Ravago Jones81e50632022-03-11 16:23:51 -080035using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal;
36using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal;
Nathan Leongdd728002024-02-03 15:26:53 -080037using frc971::control_loops::catapult::CatapultGoal;
milind-u086d7262022-01-19 20:44:18 -080038using frc971::control_loops::drivetrain::LocalizerControl;
Ravago Jones81e50632022-03-11 16:23:51 -080039
milind-u086d7262022-01-19 20:44:18 -080040namespace chrono = ::std::chrono;
41
42AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
43 : frc971::autonomous::BaseAutonomousActor(
Ravago Jones81e50632022-03-11 16:23:51 -080044 event_loop, control_loops::drivetrain::GetDrivetrainConfig()),
45 localizer_control_sender_(
46 event_loop->MakeSender<
47 ::frc971::control_loops::drivetrain::LocalizerControl>(
48 "/drivetrain")),
49 superstructure_goal_sender_(
50 event_loop->MakeSender<control_loops::superstructure::Goal>(
51 "/superstructure")),
52 superstructure_status_fetcher_(
53 event_loop->MakeFetcher<control_loops::superstructure::Status>(
54 "/superstructure")),
55 joystick_state_fetcher_(
56 event_loop->MakeFetcher<aos::JoystickState>("/aos")),
57 robot_state_fetcher_(event_loop->MakeFetcher<aos::RobotState>("/aos")),
58 auto_splines_() {
59 set_max_drivetrain_voltage(12.0);
60 replan_timer_ = event_loop->AddTimer([this]() { Replan(); });
61 event_loop->OnRun([this, event_loop]() {
Philipp Schradera6712522023-07-05 20:25:11 -070062 replan_timer_->Schedule(event_loop->monotonic_now());
63 button_poll_->Schedule(event_loop->monotonic_now(),
64 chrono::milliseconds(50));
Ravago Jones81e50632022-03-11 16:23:51 -080065 });
66
67 button_poll_ = event_loop->AddTimer([this]() {
68 const aos::monotonic_clock::time_point now =
69 this->event_loop()->context().monotonic_event_time;
70 if (robot_state_fetcher_.Fetch()) {
71 if (robot_state_fetcher_->user_button()) {
72 user_indicated_safe_to_reset_ = true;
73 MaybeSendStartingPosition();
74 }
75 }
76 if (joystick_state_fetcher_.Fetch()) {
77 if (joystick_state_fetcher_->has_alliance() &&
78 (joystick_state_fetcher_->alliance() != alliance_)) {
79 alliance_ = joystick_state_fetcher_->alliance();
80 is_planned_ = false;
81 // Only kick the planning out by 2 seconds. If we end up enabled in that
82 // second, then we will kick it out further based on the code below.
Philipp Schradera6712522023-07-05 20:25:11 -070083 replan_timer_->Schedule(now + std::chrono::seconds(2));
Ravago Jones81e50632022-03-11 16:23:51 -080084 }
85 if (joystick_state_fetcher_->enabled()) {
86 if (!is_planned_) {
87 // Only replan once we've been disabled for 5 seconds.
Philipp Schradera6712522023-07-05 20:25:11 -070088 replan_timer_->Schedule(now + std::chrono::seconds(5));
Ravago Jones81e50632022-03-11 16:23:51 -080089 }
90 }
91 }
92 });
93}
94
95void AutonomousActor::Replan() {
96 LOG(INFO) << "Alliance " << static_cast<int>(alliance_);
97 if (alliance_ == aos::Alliance::kInvalid) {
98 return;
99 }
100 sent_starting_position_ = false;
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700101 if (absl::GetFlag(FLAGS_spline_auto)) {
Ravago Jones81e50632022-03-11 16:23:51 -0800102 test_spline_ =
103 PlanSpline(std::bind(&AutonomousSplines::TestSpline, &auto_splines_,
104 std::placeholders::_1, alliance_),
105 SplineDirection::kForward);
106
107 starting_position_ = test_spline_->starting_position();
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700108 } else if (absl::GetFlag(FLAGS_rapid_react)) {
Milind Upadhyaya7793962022-03-11 19:39:36 -0800109 rapid_react_splines_ = {
110 PlanSpline(std::bind(&AutonomousSplines::Spline1, &auto_splines_,
111 std::placeholders::_1, alliance_),
Henry Speiser09e8da92022-03-14 20:58:45 -0700112 SplineDirection::kBackward),
Milind Upadhyaya7793962022-03-11 19:39:36 -0800113 PlanSpline(std::bind(&AutonomousSplines::Spline2, &auto_splines_,
114 std::placeholders::_1, alliance_),
Henry Speisere23d4de2022-04-05 16:47:47 -0700115 SplineDirection::kBackward),
Milind Upadhyaya7793962022-03-11 19:39:36 -0800116 PlanSpline(std::bind(&AutonomousSplines::Spline3, &auto_splines_,
117 std::placeholders::_1, alliance_),
Henry Speisere23d4de2022-04-05 16:47:47 -0700118 SplineDirection::kForward)};
Milind Upadhyaya7793962022-03-11 19:39:36 -0800119 starting_position_ = rapid_react_splines_.value()[0].starting_position();
120 CHECK(starting_position_);
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700121 } else if (absl::GetFlag(FLAGS_rapid_react_two)) {
Henry Speiser5eed1de2022-04-07 21:52:10 -0700122 rapid_react_two_spline_ = {
Austin Schuhf3413b72022-04-16 19:09:33 -0700123 PlanSpline(std::bind(&AutonomousSplines::SplineTwoBall1, &auto_splines_,
Henry Speiser5eed1de2022-04-07 21:52:10 -0700124 std::placeholders::_1, alliance_),
Austin Schuhf3413b72022-04-16 19:09:33 -0700125 SplineDirection::kBackward),
126 PlanSpline(std::bind(&AutonomousSplines::SplineTwoBall2, &auto_splines_,
127 std::placeholders::_1, alliance_),
128 SplineDirection::kForward)};
Henry Speiser5eed1de2022-04-07 21:52:10 -0700129 starting_position_ = rapid_react_two_spline_.value()[0].starting_position();
130 CHECK(starting_position_);
Ravago Jones81e50632022-03-11 16:23:51 -0800131 }
132
133 is_planned_ = true;
134
135 MaybeSendStartingPosition();
136}
137
138void AutonomousActor::MaybeSendStartingPosition() {
139 if (is_planned_ && user_indicated_safe_to_reset_ &&
140 !sent_starting_position_) {
141 CHECK(starting_position_);
142 SendStartingPosition(starting_position_.value());
143 }
144}
milind-u086d7262022-01-19 20:44:18 -0800145
146void AutonomousActor::Reset() {
147 InitializeEncoders();
148 ResetDrivetrain();
Ravago Jones81e50632022-03-11 16:23:51 -0800149 RetractFrontIntake();
150 RetractBackIntake();
151
152 joystick_state_fetcher_.Fetch();
153 CHECK(joystick_state_fetcher_.get() != nullptr)
154 << "Expect at least one JoystickState message before running auto...";
155 alliance_ = joystick_state_fetcher_->alliance();
milind-u086d7262022-01-19 20:44:18 -0800156}
157
158bool AutonomousActor::RunAction(
159 const ::frc971::autonomous::AutonomousActionParams *params) {
160 Reset();
Ravago Jones81e50632022-03-11 16:23:51 -0800161 if (!user_indicated_safe_to_reset_) {
162 AOS_LOG(WARNING, "Didn't send starting position prior to starting auto.");
163 CHECK(starting_position_);
164 SendStartingPosition(starting_position_.value());
165 }
166 // Clear this so that we don't accidentally resend things as soon as we replan
167 // later.
168 user_indicated_safe_to_reset_ = false;
169 is_planned_ = false;
170 starting_position_.reset();
milind-u086d7262022-01-19 20:44:18 -0800171
172 AOS_LOG(INFO, "Params are %d\n", params->mode());
Ravago Jones81e50632022-03-11 16:23:51 -0800173 if (alliance_ == aos::Alliance::kInvalid) {
174 AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection.");
175 return false;
176 }
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700177 if (absl::GetFlag(FLAGS_spline_auto)) {
Ravago Jones81e50632022-03-11 16:23:51 -0800178 SplineAuto();
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700179 } else if (absl::GetFlag(FLAGS_rapid_react)) {
Milind Upadhyaya7793962022-03-11 19:39:36 -0800180 RapidReact();
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700181 } else if (absl::GetFlag(FLAGS_rapid_react_two)) {
Henry Speiser5eed1de2022-04-07 21:52:10 -0700182 RapidReactTwo();
Ravago Jones81e50632022-03-11 16:23:51 -0800183 }
184
milind-u086d7262022-01-19 20:44:18 -0800185 return true;
186}
187
Ravago Jones81e50632022-03-11 16:23:51 -0800188void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) {
189 // Set up the starting position for the blue alliance.
190
191 // TODO(james): Resetting the localizer breaks the left/right statespace
192 // controller. That is a bug, but we can fix that later by not resetting.
193 auto builder = localizer_control_sender_.MakeBuilder();
194
195 LocalizerControl::Builder localizer_control_builder =
196 builder.MakeBuilder<LocalizerControl>();
197 localizer_control_builder.add_x(start(0));
198 localizer_control_builder.add_y(start(1));
199 localizer_control_builder.add_theta(start(2));
200 localizer_control_builder.add_theta_uncertainty(0.00001);
201 LOG(INFO) << "User button pressed, x: " << start(0) << " y: " << start(1)
202 << " theta: " << start(2);
203 if (builder.Send(localizer_control_builder.Finish()) !=
204 aos::RawSender::Error::kOk) {
205 AOS_LOG(ERROR, "Failed to reset localizer.\n");
206 }
207}
208
209void AutonomousActor::SplineAuto() {
210 CHECK(test_spline_);
211
212 if (!test_spline_->WaitForPlan()) return;
213 test_spline_->Start();
214
215 if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return;
216}
217
Milind Upadhyaya7793962022-03-11 19:39:36 -0800218void AutonomousActor::RapidReact() {
219 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
220
221 CHECK(rapid_react_splines_);
222
223 auto &splines = *rapid_react_splines_;
224
225 // Tell the superstructure a ball was preloaded
Milind Upadhyaya7793962022-03-11 19:39:36 -0800226 if (!WaitForPreloaded()) return;
Henry Speiser09e8da92022-03-14 20:58:45 -0700227
Henry Speisere23d4de2022-04-05 16:47:47 -0700228 // Fire preloaded ball while driving
Henry Speiser09e8da92022-03-14 20:58:45 -0700229 set_fire_at_will(true);
230 SendSuperstructureGoal();
Milind Upadhyayf35bb112022-03-16 23:08:04 -0700231 if (!WaitForBallsShot()) return;
Austin Schuh42d7e5f2022-03-16 23:35:09 -0700232 LOG(INFO) << "Shot first ball "
233 << chrono::duration<double>(aos::monotonic_clock::now() -
234 start_time)
235 .count()
236 << 's';
Henry Speiser09e8da92022-03-14 20:58:45 -0700237 set_fire_at_will(false);
238 SendSuperstructureGoal();
239
Henry Speisere23d4de2022-04-05 16:47:47 -0700240 // Drive and intake the ball nearest to the starting zone.
241 // Fire while moving.
Henry Speiser09e8da92022-03-14 20:58:45 -0700242 ExtendBackIntake();
Milind Upadhyaya7793962022-03-11 19:39:36 -0800243 if (!splines[0].WaitForPlan()) return;
244 splines[0].Start();
Henry Speisere23d4de2022-04-05 16:47:47 -0700245 // Distance before we don't shoot while moving.
Austin Schuh9651c5a2022-04-17 19:12:42 -0700246 if (!splines[0].WaitForSplineDistanceRemaining(2.1)) return;
247 LOG(INFO) << "Tring to take the shot";
Milind Upadhyaya7793962022-03-11 19:39:36 -0800248
Milind Upadhyaya7793962022-03-11 19:39:36 -0800249 set_fire_at_will(true);
250 SendSuperstructureGoal();
Henry Speisere23d4de2022-04-05 16:47:47 -0700251
252 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
253
Austin Schuh9651c5a2022-04-17 19:12:42 -0700254 std::this_thread::sleep_for(std::chrono::milliseconds(500));
Henry Speisere23d4de2022-04-05 16:47:47 -0700255
256 // Fire the last ball we picked up when stopped.
257 SendSuperstructureGoal();
258 LOG(INFO) << "Close";
Milind Upadhyayf35bb112022-03-16 23:08:04 -0700259 if (!WaitForBallsShot()) return;
Austin Schuh42d7e5f2022-03-16 23:35:09 -0700260 LOG(INFO) << "Shot first 3 balls "
261 << chrono::duration<double>(aos::monotonic_clock::now() -
262 start_time)
263 .count()
264 << 's';
Milind Upadhyaya7793962022-03-11 19:39:36 -0800265
266 // Drive to the human player station while intaking two balls.
267 // Once is already placed down,
268 // and one will be rolled to the robot by the human player
Henry Speiser09e8da92022-03-14 20:58:45 -0700269 if (!splines[1].WaitForPlan()) return;
270 splines[1].Start();
Henry Speisere23d4de2022-04-05 16:47:47 -0700271
272 std::this_thread::sleep_for(std::chrono::milliseconds(1500));
273
274 set_fire_at_will(false);
275 SendSuperstructureGoal();
276
Henry Speiser09e8da92022-03-14 20:58:45 -0700277 if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return;
Henry Speisere23d4de2022-04-05 16:47:47 -0700278 std::this_thread::sleep_for(std::chrono::milliseconds(500));
Austin Schuh42d7e5f2022-03-16 23:35:09 -0700279 LOG(INFO) << "At balls 4/5 "
280 << chrono::duration<double>(aos::monotonic_clock::now() -
281 start_time)
282 .count()
283 << 's';
Milind Upadhyaya7793962022-03-11 19:39:36 -0800284
285 // Drive to the shooting position
Henry Speiser09e8da92022-03-14 20:58:45 -0700286 if (!splines[2].WaitForPlan()) return;
287 splines[2].Start();
288 if (!splines[2].WaitForSplineDistanceRemaining(2.00)) return;
289 RetractFrontIntake();
290
291 if (!splines[2].WaitForSplineDistanceRemaining(0.02)) return;
Austin Schuh42d7e5f2022-03-16 23:35:09 -0700292 LOG(INFO) << "Shooting last balls "
293 << chrono::duration<double>(aos::monotonic_clock::now() -
294 start_time)
295 .count()
296 << 's';
Milind Upadhyaya7793962022-03-11 19:39:36 -0800297
298 // Fire the two balls once we stopped
299 set_fire_at_will(true);
300 SendSuperstructureGoal();
Milind Upadhyayf35bb112022-03-16 23:08:04 -0700301 if (!WaitForBallsShot()) return;
Milind Upadhyaya7793962022-03-11 19:39:36 -0800302
303 LOG(INFO) << "Took "
304 << chrono::duration<double>(aos::monotonic_clock::now() -
305 start_time)
306 .count()
307 << 's';
Austin Schuh9651c5a2022-04-17 19:12:42 -0700308 std::this_thread::sleep_for(std::chrono::milliseconds(500));
309 set_fire_at_will(false);
310 SendSuperstructureGoal();
Milind Upadhyaya7793962022-03-11 19:39:36 -0800311}
312
Henry Speiser5eed1de2022-04-07 21:52:10 -0700313// Rapid React Two Ball Autonomous.
314void AutonomousActor::RapidReactTwo() {
315 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
316
317 CHECK(rapid_react_two_spline_);
318
319 auto &splines = *rapid_react_two_spline_;
320
321 // Tell the superstructure a ball was preloaded
322 if (!WaitForPreloaded()) return;
323 set_fire_at_will(true);
324 SendSuperstructureGoal();
325 if (!WaitForBallsShot()) return;
326 LOG(INFO) << "Shot first ball "
327 << chrono::duration<double>(aos::monotonic_clock::now() -
328 start_time)
329 .count()
330 << 's';
331 set_fire_at_will(false);
332 SendSuperstructureGoal();
333
334 ExtendBackIntake();
335 if (!splines[0].WaitForPlan()) return;
336 splines[0].Start();
337 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
338
Austin Schuhf3413b72022-04-16 19:09:33 -0700339 std::this_thread::sleep_for(std::chrono::milliseconds(300));
340
341 if (!splines[1].WaitForPlan()) return;
342 splines[1].Start();
343 if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return;
344 std::this_thread::sleep_for(std::chrono::milliseconds(500));
345
Henry Speiser5eed1de2022-04-07 21:52:10 -0700346 // Fire the ball once we stopped
Henry Speiser5eed1de2022-04-07 21:52:10 -0700347 set_fire_at_will(true);
348 SendSuperstructureGoal();
349 if (!WaitForBallsShot()) return;
350 LOG(INFO) << "Shot last ball "
351 << chrono::duration<double>(aos::monotonic_clock::now() -
352 start_time)
353 .count()
354 << 's';
355 set_fire_at_will(false);
Austin Schuhf3413b72022-04-16 19:09:33 -0700356 RetractBackIntake();
Henry Speiser5eed1de2022-04-07 21:52:10 -0700357 SendSuperstructureGoal();
358}
359
Milind Upadhyaya7793962022-03-11 19:39:36 -0800360[[nodiscard]] bool AutonomousActor::WaitForPreloaded() {
Milind Upadhyay803bbf02022-03-11 17:56:26 -0800361 set_preloaded(true);
362 SendSuperstructureGoal();
363
364 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
365 event_loop()->monotonic_now(),
Stephan Pleines743f83a2024-02-02 18:32:09 -0800366 aos::common::actions::kLoopOffset);
Milind Upadhyay803bbf02022-03-11 17:56:26 -0800367
368 bool loaded = false;
369 while (!loaded) {
370 if (ShouldCancel()) {
371 return false;
372 }
373
374 phased_loop.SleepUntilNext();
375 superstructure_status_fetcher_.Fetch();
376 CHECK(superstructure_status_fetcher_.get() != nullptr);
377
378 loaded = (superstructure_status_fetcher_->state() ==
379 control_loops::superstructure::SuperstructureState::LOADED);
380 }
381
382 set_preloaded(false);
383 SendSuperstructureGoal();
384
385 return true;
386}
387
Ravago Jones81e50632022-03-11 16:23:51 -0800388void AutonomousActor::SendSuperstructureGoal() {
389 auto builder = superstructure_goal_sender_.MakeBuilder();
390
391 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
392 intake_front_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
393 *builder.fbb(), intake_front_goal_,
394 CreateProfileParameters(*builder.fbb(), 20.0, 60.0));
395
396 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
397 intake_back_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
398 *builder.fbb(), intake_back_goal_,
399 CreateProfileParameters(*builder.fbb(), 20.0, 60.0));
400
401 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
402 catapult_return_position_offset =
403 CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
404 *builder.fbb(), kCatapultReturnPosition,
405 CreateProfileParameters(*builder.fbb(), 9.0, 50.0));
406
Nathan Leongdd728002024-02-03 15:26:53 -0800407 CatapultGoal::Builder catapult_goal_builder(*builder.fbb());
Ravago Jones81e50632022-03-11 16:23:51 -0800408 catapult_goal_builder.add_shot_position(0.03);
409 catapult_goal_builder.add_shot_velocity(18.0);
410 catapult_goal_builder.add_return_position(catapult_return_position_offset);
Nathan Leongdd728002024-02-03 15:26:53 -0800411 flatbuffers::Offset<CatapultGoal> catapult_goal_offset =
Ravago Jones81e50632022-03-11 16:23:51 -0800412 catapult_goal_builder.Finish();
413
414 superstructure::Goal::Builder superstructure_builder =
415 builder.MakeBuilder<superstructure::Goal>();
416
417 superstructure_builder.add_intake_front(intake_front_offset);
418 superstructure_builder.add_intake_back(intake_back_offset);
Austin Schuh42d7e5f2022-03-16 23:35:09 -0700419 superstructure_builder.add_roller_speed_compensation(0.0);
Ravago Jones81e50632022-03-11 16:23:51 -0800420 superstructure_builder.add_roller_speed_front(roller_front_voltage_);
421 superstructure_builder.add_roller_speed_back(roller_back_voltage_);
Milind Upadhyayd86b02c2022-03-13 20:57:46 -0700422 if (requested_intake_.has_value()) {
423 superstructure_builder.add_turret_intake(*requested_intake_);
424 }
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700425 superstructure_builder.add_transfer_roller_speed(transfer_roller_voltage_);
Ravago Jones81e50632022-03-11 16:23:51 -0800426 superstructure_builder.add_catapult(catapult_goal_offset);
427 superstructure_builder.add_fire(fire_);
Milind Upadhyay803bbf02022-03-11 17:56:26 -0800428 superstructure_builder.add_preloaded(preloaded_);
Austin Schuh42d7e5f2022-03-16 23:35:09 -0700429 superstructure_builder.add_auto_aim(true);
Ravago Jones81e50632022-03-11 16:23:51 -0800430
431 if (builder.Send(superstructure_builder.Finish()) !=
432 aos::RawSender::Error::kOk) {
433 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
434 }
435}
436
437void AutonomousActor::ExtendFrontIntake() {
Milind Upadhyayd86b02c2022-03-13 20:57:46 -0700438 set_requested_intake(RequestedIntake::kFront);
Ravago Jones81e50632022-03-11 16:23:51 -0800439 set_intake_front_goal(kExtendIntakeGoal);
Austin Schuhe1264372022-03-13 20:22:36 -0700440 set_roller_front_voltage(kIntakeRollerVoltage);
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700441 set_transfer_roller_voltage(kRollerVoltage);
Ravago Jones81e50632022-03-11 16:23:51 -0800442 SendSuperstructureGoal();
443}
444
445void AutonomousActor::RetractFrontIntake() {
Milind Upadhyayd86b02c2022-03-13 20:57:46 -0700446 set_requested_intake(std::nullopt);
Ravago Jones81e50632022-03-11 16:23:51 -0800447 set_intake_front_goal(kRetractIntakeGoal);
Austin Schuhe1264372022-03-13 20:22:36 -0700448 set_roller_front_voltage(0.0);
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700449 set_transfer_roller_voltage(0.0);
Ravago Jones81e50632022-03-11 16:23:51 -0800450 SendSuperstructureGoal();
451}
452
453void AutonomousActor::ExtendBackIntake() {
Milind Upadhyayd86b02c2022-03-13 20:57:46 -0700454 set_requested_intake(RequestedIntake::kBack);
Ravago Jones81e50632022-03-11 16:23:51 -0800455 set_intake_back_goal(kExtendIntakeGoal);
Austin Schuhe1264372022-03-13 20:22:36 -0700456 set_roller_back_voltage(kIntakeRollerVoltage);
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700457 set_transfer_roller_voltage(-kRollerVoltage);
Ravago Jones81e50632022-03-11 16:23:51 -0800458 SendSuperstructureGoal();
459}
460
461void AutonomousActor::RetractBackIntake() {
Milind Upadhyayd86b02c2022-03-13 20:57:46 -0700462 set_requested_intake(std::nullopt);
Ravago Jones81e50632022-03-11 16:23:51 -0800463 set_intake_back_goal(kRetractIntakeGoal);
Austin Schuhe1264372022-03-13 20:22:36 -0700464 set_roller_back_voltage(0.0);
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700465 set_transfer_roller_voltage(0.0);
Ravago Jones81e50632022-03-11 16:23:51 -0800466 SendSuperstructureGoal();
467}
468
Milind Upadhyayf35bb112022-03-16 23:08:04 -0700469[[nodiscard]] bool AutonomousActor::WaitForBallsShot() {
Austin Schuh42d7e5f2022-03-16 23:35:09 -0700470 superstructure_status_fetcher_.Fetch();
471 CHECK(superstructure_status_fetcher_.get());
Milind Upadhyayf35bb112022-03-16 23:08:04 -0700472
Ravago Jonesf699d1c2022-03-11 18:39:56 -0800473 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
474 event_loop()->monotonic_now(),
Stephan Pleines743f83a2024-02-02 18:32:09 -0800475 aos::common::actions::kLoopOffset);
Ravago Jonesf699d1c2022-03-11 18:39:56 -0800476 superstructure_status_fetcher_.Fetch();
477 CHECK(superstructure_status_fetcher_.get() != nullptr);
Henry Speisere23d4de2022-04-05 16:47:47 -0700478
Ravago Jonesf699d1c2022-03-11 18:39:56 -0800479 while (true) {
480 if (ShouldCancel()) {
481 return false;
482 }
483 phased_loop.SleepUntilNext();
484 superstructure_status_fetcher_.Fetch();
485 CHECK(superstructure_status_fetcher_.get() != nullptr);
Henry Speisere23d4de2022-04-05 16:47:47 -0700486
487 if (!superstructure_status_fetcher_->front_intake_has_ball() &&
488 !superstructure_status_fetcher_->back_intake_has_ball() &&
489 superstructure_status_fetcher_->state() ==
490 control_loops::superstructure::SuperstructureState::IDLE) {
Ravago Jonesf699d1c2022-03-11 18:39:56 -0800491 return true;
492 }
493 }
494}
495
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800496} // namespace y2022::actors