blob: 4f49f81a79dc779d2bbac8d2a46171f0a37d74e5 [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 Schuhf3413b72022-04-16 19:09:33 -070016DEFINE_bool(rapid_react, false,
Milind Upadhyaya7793962022-03-11 19:39:36 -080017 "If true, run the main rapid react autonomous mode");
Austin Schuhf3413b72022-04-16 19:09:33 -070018DEFINE_bool(rapid_react_two, true,
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
milind-u086d7262022-01-19 20:44:18 -080021namespace y2022 {
22namespace actors {
Ravago Jones81e50632022-03-11 16:23:51 -080023namespace {
Henry Speisere23d4de2022-04-05 16:47:47 -070024constexpr double kExtendIntakeGoal = -0.10;
Ravago Jones81e50632022-03-11 16:23:51 -080025constexpr double kRetractIntakeGoal = 1.47;
Austin Schuh42d7e5f2022-03-16 23:35:09 -070026constexpr double kIntakeRollerVoltage = 12.0;
Ravago Jones81e50632022-03-11 16:23:51 -080027constexpr double kRollerVoltage = 12.0;
28constexpr double kCatapultReturnPosition = -0.908;
29} // namespace
milind-u086d7262022-01-19 20:44:18 -080030
31using ::aos::monotonic_clock;
Ravago Jones81e50632022-03-11 16:23:51 -080032using frc971::CreateProfileParameters;
milind-u086d7262022-01-19 20:44:18 -080033using ::frc971::ProfileParametersT;
Ravago Jones81e50632022-03-11 16:23:51 -080034using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal;
35using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal;
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]() {
60 replan_timer_->Setup(event_loop->monotonic_now());
61 button_poll_->Setup(event_loop->monotonic_now(), chrono::milliseconds(50));
62 });
63
64 button_poll_ = event_loop->AddTimer([this]() {
65 const aos::monotonic_clock::time_point now =
66 this->event_loop()->context().monotonic_event_time;
67 if (robot_state_fetcher_.Fetch()) {
68 if (robot_state_fetcher_->user_button()) {
69 user_indicated_safe_to_reset_ = true;
70 MaybeSendStartingPosition();
71 }
72 }
73 if (joystick_state_fetcher_.Fetch()) {
74 if (joystick_state_fetcher_->has_alliance() &&
75 (joystick_state_fetcher_->alliance() != alliance_)) {
76 alliance_ = joystick_state_fetcher_->alliance();
77 is_planned_ = false;
78 // Only kick the planning out by 2 seconds. If we end up enabled in that
79 // second, then we will kick it out further based on the code below.
80 replan_timer_->Setup(now + std::chrono::seconds(2));
81 }
82 if (joystick_state_fetcher_->enabled()) {
83 if (!is_planned_) {
84 // Only replan once we've been disabled for 5 seconds.
85 replan_timer_->Setup(now + std::chrono::seconds(5));
86 }
87 }
88 }
89 });
90}
91
92void AutonomousActor::Replan() {
93 LOG(INFO) << "Alliance " << static_cast<int>(alliance_);
94 if (alliance_ == aos::Alliance::kInvalid) {
95 return;
96 }
97 sent_starting_position_ = false;
98 if (FLAGS_spline_auto) {
99 test_spline_ =
100 PlanSpline(std::bind(&AutonomousSplines::TestSpline, &auto_splines_,
101 std::placeholders::_1, alliance_),
102 SplineDirection::kForward);
103
104 starting_position_ = test_spline_->starting_position();
Milind Upadhyaya7793962022-03-11 19:39:36 -0800105 } else if (FLAGS_rapid_react) {
106 rapid_react_splines_ = {
107 PlanSpline(std::bind(&AutonomousSplines::Spline1, &auto_splines_,
108 std::placeholders::_1, alliance_),
Henry Speiser09e8da92022-03-14 20:58:45 -0700109 SplineDirection::kBackward),
Milind Upadhyaya7793962022-03-11 19:39:36 -0800110 PlanSpline(std::bind(&AutonomousSplines::Spline2, &auto_splines_,
111 std::placeholders::_1, alliance_),
Henry Speisere23d4de2022-04-05 16:47:47 -0700112 SplineDirection::kBackward),
Milind Upadhyaya7793962022-03-11 19:39:36 -0800113 PlanSpline(std::bind(&AutonomousSplines::Spline3, &auto_splines_,
114 std::placeholders::_1, alliance_),
Henry Speisere23d4de2022-04-05 16:47:47 -0700115 SplineDirection::kForward)};
Milind Upadhyaya7793962022-03-11 19:39:36 -0800116 starting_position_ = rapid_react_splines_.value()[0].starting_position();
117 CHECK(starting_position_);
Henry Speiser5eed1de2022-04-07 21:52:10 -0700118 } else if (FLAGS_rapid_react_two) {
119 rapid_react_two_spline_ = {
Austin Schuhf3413b72022-04-16 19:09:33 -0700120 PlanSpline(std::bind(&AutonomousSplines::SplineTwoBall1, &auto_splines_,
Henry Speiser5eed1de2022-04-07 21:52:10 -0700121 std::placeholders::_1, alliance_),
Austin Schuhf3413b72022-04-16 19:09:33 -0700122 SplineDirection::kBackward),
123 PlanSpline(std::bind(&AutonomousSplines::SplineTwoBall2, &auto_splines_,
124 std::placeholders::_1, alliance_),
125 SplineDirection::kForward)};
Henry Speiser5eed1de2022-04-07 21:52:10 -0700126 starting_position_ = rapid_react_two_spline_.value()[0].starting_position();
127 CHECK(starting_position_);
Ravago Jones81e50632022-03-11 16:23:51 -0800128 }
129
130 is_planned_ = true;
131
132 MaybeSendStartingPosition();
133}
134
135void AutonomousActor::MaybeSendStartingPosition() {
136 if (is_planned_ && user_indicated_safe_to_reset_ &&
137 !sent_starting_position_) {
138 CHECK(starting_position_);
139 SendStartingPosition(starting_position_.value());
140 }
141}
milind-u086d7262022-01-19 20:44:18 -0800142
143void AutonomousActor::Reset() {
144 InitializeEncoders();
145 ResetDrivetrain();
Ravago Jones81e50632022-03-11 16:23:51 -0800146 RetractFrontIntake();
147 RetractBackIntake();
148
149 joystick_state_fetcher_.Fetch();
150 CHECK(joystick_state_fetcher_.get() != nullptr)
151 << "Expect at least one JoystickState message before running auto...";
152 alliance_ = joystick_state_fetcher_->alliance();
milind-u086d7262022-01-19 20:44:18 -0800153}
154
155bool AutonomousActor::RunAction(
156 const ::frc971::autonomous::AutonomousActionParams *params) {
157 Reset();
Ravago Jones81e50632022-03-11 16:23:51 -0800158 if (!user_indicated_safe_to_reset_) {
159 AOS_LOG(WARNING, "Didn't send starting position prior to starting auto.");
160 CHECK(starting_position_);
161 SendStartingPosition(starting_position_.value());
162 }
163 // Clear this so that we don't accidentally resend things as soon as we replan
164 // later.
165 user_indicated_safe_to_reset_ = false;
166 is_planned_ = false;
167 starting_position_.reset();
milind-u086d7262022-01-19 20:44:18 -0800168
169 AOS_LOG(INFO, "Params are %d\n", params->mode());
Ravago Jones81e50632022-03-11 16:23:51 -0800170 if (alliance_ == aos::Alliance::kInvalid) {
171 AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection.");
172 return false;
173 }
174 if (FLAGS_spline_auto) {
175 SplineAuto();
Milind Upadhyaya7793962022-03-11 19:39:36 -0800176 } else if (FLAGS_rapid_react) {
177 RapidReact();
Henry Speiser5eed1de2022-04-07 21:52:10 -0700178 } else if (FLAGS_rapid_react_two) {
179 RapidReactTwo();
Ravago Jones81e50632022-03-11 16:23:51 -0800180 }
181
milind-u086d7262022-01-19 20:44:18 -0800182 return true;
183}
184
Ravago Jones81e50632022-03-11 16:23:51 -0800185void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) {
186 // Set up the starting position for the blue alliance.
187
188 // TODO(james): Resetting the localizer breaks the left/right statespace
189 // controller. That is a bug, but we can fix that later by not resetting.
190 auto builder = localizer_control_sender_.MakeBuilder();
191
192 LocalizerControl::Builder localizer_control_builder =
193 builder.MakeBuilder<LocalizerControl>();
194 localizer_control_builder.add_x(start(0));
195 localizer_control_builder.add_y(start(1));
196 localizer_control_builder.add_theta(start(2));
197 localizer_control_builder.add_theta_uncertainty(0.00001);
198 LOG(INFO) << "User button pressed, x: " << start(0) << " y: " << start(1)
199 << " theta: " << start(2);
200 if (builder.Send(localizer_control_builder.Finish()) !=
201 aos::RawSender::Error::kOk) {
202 AOS_LOG(ERROR, "Failed to reset localizer.\n");
203 }
204}
205
206void AutonomousActor::SplineAuto() {
207 CHECK(test_spline_);
208
209 if (!test_spline_->WaitForPlan()) return;
210 test_spline_->Start();
211
212 if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return;
213}
214
Milind Upadhyaya7793962022-03-11 19:39:36 -0800215void AutonomousActor::RapidReact() {
216 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
217
218 CHECK(rapid_react_splines_);
219
220 auto &splines = *rapid_react_splines_;
221
222 // Tell the superstructure a ball was preloaded
Milind Upadhyaya7793962022-03-11 19:39:36 -0800223 if (!WaitForPreloaded()) return;
Henry Speiser09e8da92022-03-14 20:58:45 -0700224
Henry Speisere23d4de2022-04-05 16:47:47 -0700225 // Fire preloaded ball while driving
Henry Speiser09e8da92022-03-14 20:58:45 -0700226 set_fire_at_will(true);
227 SendSuperstructureGoal();
Milind Upadhyayf35bb112022-03-16 23:08:04 -0700228 if (!WaitForBallsShot()) return;
Austin Schuh42d7e5f2022-03-16 23:35:09 -0700229 LOG(INFO) << "Shot first ball "
230 << chrono::duration<double>(aos::monotonic_clock::now() -
231 start_time)
232 .count()
233 << 's';
Henry Speiser09e8da92022-03-14 20:58:45 -0700234 set_fire_at_will(false);
235 SendSuperstructureGoal();
236
Henry Speisere23d4de2022-04-05 16:47:47 -0700237 // Drive and intake the ball nearest to the starting zone.
238 // Fire while moving.
Henry Speiser09e8da92022-03-14 20:58:45 -0700239 ExtendBackIntake();
Milind Upadhyaya7793962022-03-11 19:39:36 -0800240 if (!splines[0].WaitForPlan()) return;
241 splines[0].Start();
Henry Speisere23d4de2022-04-05 16:47:47 -0700242 // Distance before we don't shoot while moving.
243 if (!splines[0].WaitForSplineDistanceRemaining(0.25)) return;
Milind Upadhyaya7793962022-03-11 19:39:36 -0800244
Milind Upadhyaya7793962022-03-11 19:39:36 -0800245 set_fire_at_will(true);
246 SendSuperstructureGoal();
Henry Speisere23d4de2022-04-05 16:47:47 -0700247
248 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
249
250 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
251
252 // Fire the last ball we picked up when stopped.
253 SendSuperstructureGoal();
254 LOG(INFO) << "Close";
Milind Upadhyayf35bb112022-03-16 23:08:04 -0700255 if (!WaitForBallsShot()) return;
Austin Schuh42d7e5f2022-03-16 23:35:09 -0700256 LOG(INFO) << "Shot first 3 balls "
257 << chrono::duration<double>(aos::monotonic_clock::now() -
258 start_time)
259 .count()
260 << 's';
Milind Upadhyaya7793962022-03-11 19:39:36 -0800261
262 // Drive to the human player station while intaking two balls.
263 // Once is already placed down,
264 // and one will be rolled to the robot by the human player
Henry Speiser09e8da92022-03-14 20:58:45 -0700265 if (!splines[1].WaitForPlan()) return;
266 splines[1].Start();
Henry Speisere23d4de2022-04-05 16:47:47 -0700267
268 std::this_thread::sleep_for(std::chrono::milliseconds(1500));
269
270 set_fire_at_will(false);
271 SendSuperstructureGoal();
272
Henry Speiser09e8da92022-03-14 20:58:45 -0700273 if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return;
Henry Speisere23d4de2022-04-05 16:47:47 -0700274 std::this_thread::sleep_for(std::chrono::milliseconds(500));
Austin Schuh42d7e5f2022-03-16 23:35:09 -0700275 LOG(INFO) << "At balls 4/5 "
276 << chrono::duration<double>(aos::monotonic_clock::now() -
277 start_time)
278 .count()
279 << 's';
Milind Upadhyaya7793962022-03-11 19:39:36 -0800280
281 // Drive to the shooting position
Henry Speiser09e8da92022-03-14 20:58:45 -0700282 if (!splines[2].WaitForPlan()) return;
283 splines[2].Start();
284 if (!splines[2].WaitForSplineDistanceRemaining(2.00)) return;
285 RetractFrontIntake();
286
287 if (!splines[2].WaitForSplineDistanceRemaining(0.02)) return;
Austin Schuh42d7e5f2022-03-16 23:35:09 -0700288 LOG(INFO) << "Shooting last balls "
289 << chrono::duration<double>(aos::monotonic_clock::now() -
290 start_time)
291 .count()
292 << 's';
Milind Upadhyaya7793962022-03-11 19:39:36 -0800293
294 // Fire the two balls once we stopped
295 set_fire_at_will(true);
296 SendSuperstructureGoal();
Milind Upadhyayf35bb112022-03-16 23:08:04 -0700297 if (!WaitForBallsShot()) return;
Milind Upadhyaya7793962022-03-11 19:39:36 -0800298 set_fire_at_will(false);
Henry Speiser09e8da92022-03-14 20:58:45 -0700299 SendSuperstructureGoal();
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';
306}
307
Henry Speiser5eed1de2022-04-07 21:52:10 -0700308// Rapid React Two Ball Autonomous.
309void AutonomousActor::RapidReactTwo() {
310 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
311
312 CHECK(rapid_react_two_spline_);
313
314 auto &splines = *rapid_react_two_spline_;
315
316 // Tell the superstructure a ball was preloaded
317 if (!WaitForPreloaded()) return;
318 set_fire_at_will(true);
319 SendSuperstructureGoal();
320 if (!WaitForBallsShot()) return;
321 LOG(INFO) << "Shot first ball "
322 << chrono::duration<double>(aos::monotonic_clock::now() -
323 start_time)
324 .count()
325 << 's';
326 set_fire_at_will(false);
327 SendSuperstructureGoal();
328
329 ExtendBackIntake();
330 if (!splines[0].WaitForPlan()) return;
331 splines[0].Start();
332 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
333
Austin Schuhf3413b72022-04-16 19:09:33 -0700334 std::this_thread::sleep_for(std::chrono::milliseconds(300));
335
336 if (!splines[1].WaitForPlan()) return;
337 splines[1].Start();
338 if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return;
339 std::this_thread::sleep_for(std::chrono::milliseconds(500));
340
Henry Speiser5eed1de2022-04-07 21:52:10 -0700341 // Fire the ball once we stopped
Henry Speiser5eed1de2022-04-07 21:52:10 -0700342 set_fire_at_will(true);
343 SendSuperstructureGoal();
344 if (!WaitForBallsShot()) return;
345 LOG(INFO) << "Shot last ball "
346 << chrono::duration<double>(aos::monotonic_clock::now() -
347 start_time)
348 .count()
349 << 's';
350 set_fire_at_will(false);
Austin Schuhf3413b72022-04-16 19:09:33 -0700351 RetractBackIntake();
Henry Speiser5eed1de2022-04-07 21:52:10 -0700352 SendSuperstructureGoal();
353}
354
Milind Upadhyaya7793962022-03-11 19:39:36 -0800355[[nodiscard]] bool AutonomousActor::WaitForPreloaded() {
Milind Upadhyay803bbf02022-03-11 17:56:26 -0800356 set_preloaded(true);
357 SendSuperstructureGoal();
358
359 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
360 event_loop()->monotonic_now(),
361 ActorBase::kLoopOffset);
362
363 bool loaded = false;
364 while (!loaded) {
365 if (ShouldCancel()) {
366 return false;
367 }
368
369 phased_loop.SleepUntilNext();
370 superstructure_status_fetcher_.Fetch();
371 CHECK(superstructure_status_fetcher_.get() != nullptr);
372
373 loaded = (superstructure_status_fetcher_->state() ==
374 control_loops::superstructure::SuperstructureState::LOADED);
375 }
376
377 set_preloaded(false);
378 SendSuperstructureGoal();
379
380 return true;
381}
382
Ravago Jones81e50632022-03-11 16:23:51 -0800383void AutonomousActor::SendSuperstructureGoal() {
384 auto builder = superstructure_goal_sender_.MakeBuilder();
385
386 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
387 intake_front_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
388 *builder.fbb(), intake_front_goal_,
389 CreateProfileParameters(*builder.fbb(), 20.0, 60.0));
390
391 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
392 intake_back_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
393 *builder.fbb(), intake_back_goal_,
394 CreateProfileParameters(*builder.fbb(), 20.0, 60.0));
395
396 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
397 catapult_return_position_offset =
398 CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
399 *builder.fbb(), kCatapultReturnPosition,
400 CreateProfileParameters(*builder.fbb(), 9.0, 50.0));
401
402 superstructure::CatapultGoal::Builder catapult_goal_builder(*builder.fbb());
403 catapult_goal_builder.add_shot_position(0.03);
404 catapult_goal_builder.add_shot_velocity(18.0);
405 catapult_goal_builder.add_return_position(catapult_return_position_offset);
406 flatbuffers::Offset<superstructure::CatapultGoal> catapult_goal_offset =
407 catapult_goal_builder.Finish();
408
409 superstructure::Goal::Builder superstructure_builder =
410 builder.MakeBuilder<superstructure::Goal>();
411
412 superstructure_builder.add_intake_front(intake_front_offset);
413 superstructure_builder.add_intake_back(intake_back_offset);
Austin Schuh42d7e5f2022-03-16 23:35:09 -0700414 superstructure_builder.add_roller_speed_compensation(0.0);
Ravago Jones81e50632022-03-11 16:23:51 -0800415 superstructure_builder.add_roller_speed_front(roller_front_voltage_);
416 superstructure_builder.add_roller_speed_back(roller_back_voltage_);
Milind Upadhyayd86b02c2022-03-13 20:57:46 -0700417 if (requested_intake_.has_value()) {
418 superstructure_builder.add_turret_intake(*requested_intake_);
419 }
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700420 superstructure_builder.add_transfer_roller_speed(transfer_roller_voltage_);
Ravago Jones81e50632022-03-11 16:23:51 -0800421 superstructure_builder.add_catapult(catapult_goal_offset);
422 superstructure_builder.add_fire(fire_);
Milind Upadhyay803bbf02022-03-11 17:56:26 -0800423 superstructure_builder.add_preloaded(preloaded_);
Austin Schuh42d7e5f2022-03-16 23:35:09 -0700424 superstructure_builder.add_auto_aim(true);
Ravago Jones81e50632022-03-11 16:23:51 -0800425
426 if (builder.Send(superstructure_builder.Finish()) !=
427 aos::RawSender::Error::kOk) {
428 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
429 }
430}
431
432void AutonomousActor::ExtendFrontIntake() {
Milind Upadhyayd86b02c2022-03-13 20:57:46 -0700433 set_requested_intake(RequestedIntake::kFront);
Ravago Jones81e50632022-03-11 16:23:51 -0800434 set_intake_front_goal(kExtendIntakeGoal);
Austin Schuhe1264372022-03-13 20:22:36 -0700435 set_roller_front_voltage(kIntakeRollerVoltage);
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700436 set_transfer_roller_voltage(kRollerVoltage);
Ravago Jones81e50632022-03-11 16:23:51 -0800437 SendSuperstructureGoal();
438}
439
440void AutonomousActor::RetractFrontIntake() {
Milind Upadhyayd86b02c2022-03-13 20:57:46 -0700441 set_requested_intake(std::nullopt);
Ravago Jones81e50632022-03-11 16:23:51 -0800442 set_intake_front_goal(kRetractIntakeGoal);
Austin Schuhe1264372022-03-13 20:22:36 -0700443 set_roller_front_voltage(0.0);
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700444 set_transfer_roller_voltage(0.0);
Ravago Jones81e50632022-03-11 16:23:51 -0800445 SendSuperstructureGoal();
446}
447
448void AutonomousActor::ExtendBackIntake() {
Milind Upadhyayd86b02c2022-03-13 20:57:46 -0700449 set_requested_intake(RequestedIntake::kBack);
Ravago Jones81e50632022-03-11 16:23:51 -0800450 set_intake_back_goal(kExtendIntakeGoal);
Austin Schuhe1264372022-03-13 20:22:36 -0700451 set_roller_back_voltage(kIntakeRollerVoltage);
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700452 set_transfer_roller_voltage(-kRollerVoltage);
Ravago Jones81e50632022-03-11 16:23:51 -0800453 SendSuperstructureGoal();
454}
455
456void AutonomousActor::RetractBackIntake() {
Milind Upadhyayd86b02c2022-03-13 20:57:46 -0700457 set_requested_intake(std::nullopt);
Ravago Jones81e50632022-03-11 16:23:51 -0800458 set_intake_back_goal(kRetractIntakeGoal);
Austin Schuhe1264372022-03-13 20:22:36 -0700459 set_roller_back_voltage(0.0);
Milind Upadhyay29dcc172022-04-02 19:21:30 -0700460 set_transfer_roller_voltage(0.0);
Ravago Jones81e50632022-03-11 16:23:51 -0800461 SendSuperstructureGoal();
462}
463
Milind Upadhyayf35bb112022-03-16 23:08:04 -0700464[[nodiscard]] bool AutonomousActor::WaitForBallsShot() {
Austin Schuh42d7e5f2022-03-16 23:35:09 -0700465 superstructure_status_fetcher_.Fetch();
466 CHECK(superstructure_status_fetcher_.get());
Milind Upadhyayf35bb112022-03-16 23:08:04 -0700467
Ravago Jonesf699d1c2022-03-11 18:39:56 -0800468 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
469 event_loop()->monotonic_now(),
470 ActorBase::kLoopOffset);
471 superstructure_status_fetcher_.Fetch();
472 CHECK(superstructure_status_fetcher_.get() != nullptr);
Henry Speisere23d4de2022-04-05 16:47:47 -0700473
Ravago Jonesf699d1c2022-03-11 18:39:56 -0800474 while (true) {
475 if (ShouldCancel()) {
476 return false;
477 }
478 phased_loop.SleepUntilNext();
479 superstructure_status_fetcher_.Fetch();
480 CHECK(superstructure_status_fetcher_.get() != nullptr);
Henry Speisere23d4de2022-04-05 16:47:47 -0700481
482 if (!superstructure_status_fetcher_->front_intake_has_ball() &&
483 !superstructure_status_fetcher_->back_intake_has_ball() &&
484 superstructure_status_fetcher_->state() ==
485 control_loops::superstructure::SuperstructureState::IDLE) {
Ravago Jonesf699d1c2022-03-11 18:39:56 -0800486 return true;
487 }
488 }
489}
490
milind-u086d7262022-01-19 20:44:18 -0800491} // namespace actors
492} // namespace y2022