blob: f68c7f7308f28737e337e91b28bad38cef6ea089 [file] [log] [blame]
Stephan Massaltd021f972020-01-05 20:41:23 -08001#include "y2020/actors/autonomous_actor.h"
2
Stephan Massaltd021f972020-01-05 20:41:23 -08003#include <chrono>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07004#include <cinttypes>
Stephan Massaltd021f972020-01-05 20:41:23 -08005#include <cmath>
6
Austin Schuh99f7c6a2024-06-25 22:07:44 -07007#include "absl/flags/flag.h"
8
Stephan Massaltd021f972020-01-05 20:41:23 -08009#include "aos/logging/logging.h"
Austin Schuh3653cf22021-11-12 11:54:51 -080010#include "aos/network/team_number.h"
James Kuszmaulddd2ba62020-03-08 22:17:13 -070011#include "aos/util/math.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080012#include "frc971/control_loops/drivetrain/localizer_generated.h"
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080013#include "y2020/actors/auto_splines.h"
Austin Schuh3653cf22021-11-12 11:54:51 -080014#include "y2020/constants.h"
Ravago Jonesc2a08022021-02-06 17:40:54 -080015#include "y2020/control_loops/drivetrain/drivetrain_base.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080016
Austin Schuh99f7c6a2024-06-25 22:07:44 -070017ABSL_FLAG(bool, spline_auto, false, "If true, define a spline autonomous mode");
18ABSL_FLAG(bool, just_shoot, false,
19 "If true, run the autonomous that just shoots balls.");
milind upadhyay47a0ab32020-11-25 19:34:41 -080020
Stephan Pleinesf63bde82024-01-13 15:59:33 -080021namespace y2020::actors {
Stephan Massaltd021f972020-01-05 20:41:23 -080022
23using ::aos::monotonic_clock;
24using ::frc971::ProfileParametersT;
25using frc971::control_loops::drivetrain::LocalizerControl;
26namespace chrono = ::std::chrono;
27
28AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
29 : frc971::autonomous::BaseAutonomousActor(
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080030 event_loop, control_loops::drivetrain::GetDrivetrainConfig()),
Ravago Jonesc2a08022021-02-06 17:40:54 -080031 localizer_control_sender_(
32 event_loop->MakeSender<
33 ::frc971::control_loops::drivetrain::LocalizerControl>(
34 "/drivetrain")),
Austin Schuh67e127e2021-03-27 13:25:23 -070035 superstructure_goal_sender_(
36 event_loop->MakeSender<control_loops::superstructure::Goal>(
37 "/superstructure")),
Ravago Jones1f32d622021-08-26 12:20:36 -070038 superstructure_status_fetcher_(
39 event_loop->MakeFetcher<y2020::control_loops::superstructure::Status>(
40 "/superstructure")),
Austin Schuhba77b7e2021-10-25 21:58:54 -070041 joystick_state_fetcher_(
42 event_loop->MakeFetcher<aos::JoystickState>("/aos")),
43 robot_state_fetcher_(event_loop->MakeFetcher<aos::RobotState>("/aos")),
Ravago Jonesc2a08022021-02-06 17:40:54 -080044 auto_splines_() {
Austin Schuh3653cf22021-11-12 11:54:51 -080045 practice_robot_ =
46 ::aos::network::GetTeamNumber() == constants::Values::kPracticeTeamNumber;
47
Austin Schuhd0e9e062021-10-24 17:40:58 -070048 set_max_drivetrain_voltage(12.0);
James Kuszmaul99af8b52021-03-28 10:50:15 -070049 replan_timer_ = event_loop->AddTimer([this]() { Replan(); });
50 event_loop->OnRun([this, event_loop]() {
Philipp Schradera6712522023-07-05 20:25:11 -070051 replan_timer_->Schedule(event_loop->monotonic_now());
52 button_poll_->Schedule(event_loop->monotonic_now(),
53 chrono::milliseconds(50));
James Kuszmaul99af8b52021-03-28 10:50:15 -070054 });
Austin Schuhba77b7e2021-10-25 21:58:54 -070055
56 button_poll_ = event_loop->AddTimer([this]() {
James Kuszmaulead4da62021-10-24 17:36:54 -070057 const aos::monotonic_clock::time_point now =
58 this->event_loop()->context().monotonic_event_time;
Austin Schuhba77b7e2021-10-25 21:58:54 -070059 if (robot_state_fetcher_.Fetch()) {
60 if (robot_state_fetcher_->user_button()) {
61 user_indicated_safe_to_reset_ = true;
62 MaybeSendStartingPosition();
63 }
James Kuszmaul4303a5d2021-10-23 19:58:48 -070064 }
Austin Schuhba77b7e2021-10-25 21:58:54 -070065 if (joystick_state_fetcher_.Fetch()) {
66 if (joystick_state_fetcher_->has_alliance() &&
67 (joystick_state_fetcher_->alliance() != alliance_)) {
68 alliance_ = joystick_state_fetcher_->alliance();
James Kuszmaulead4da62021-10-24 17:36:54 -070069 is_planned_ = false;
70 // Only kick the planning out by 2 seconds. If we end up enabled in that
71 // second, then we will kick it out further based on the code below.
Philipp Schradera6712522023-07-05 20:25:11 -070072 replan_timer_->Schedule(now + std::chrono::seconds(2));
James Kuszmaulead4da62021-10-24 17:36:54 -070073 }
74 if (joystick_state_fetcher_->enabled()) {
75 if (!is_planned_) {
76 // Only replan once we've been disabled for 5 seconds.
Philipp Schradera6712522023-07-05 20:25:11 -070077 replan_timer_->Schedule(now + std::chrono::seconds(5));
James Kuszmaulead4da62021-10-24 17:36:54 -070078 }
Austin Schuhba77b7e2021-10-25 21:58:54 -070079 }
James Kuszmaul4303a5d2021-10-23 19:58:48 -070080 }
81 });
82}
83
84void AutonomousActor::MaybeSendStartingPosition() {
James Kuszmaulead4da62021-10-24 17:36:54 -070085 if (is_planned_ && user_indicated_safe_to_reset_ &&
86 !sent_starting_position_) {
James Kuszmaul4303a5d2021-10-23 19:58:48 -070087 CHECK(starting_position_);
88 SendStartingPosition(starting_position_.value());
89 }
James Kuszmaul99af8b52021-03-28 10:50:15 -070090}
91
92void AutonomousActor::Replan() {
milind-u0e203782021-10-30 21:57:20 -070093 LOG(INFO) << "Alliance " << static_cast<int>(alliance_);
Austin Schuhd0e9e062021-10-24 17:40:58 -070094 if (alliance_ == aos::Alliance::kInvalid) {
95 return;
96 }
milind-u0e203782021-10-30 21:57:20 -070097 sent_starting_position_ = false;
Austin Schuh99f7c6a2024-06-25 22:07:44 -070098 if (absl::GetFlag(FLAGS_spline_auto)) {
Tyler Chatowbf0609c2021-07-31 16:13:27 -070099 test_spline_ = PlanSpline(std::bind(&AutonomousSplines::TestSpline,
100 &auto_splines_, std::placeholders::_1),
101 SplineDirection::kForward);
James Kuszmaul4303a5d2021-10-23 19:58:48 -0700102 starting_position_ = test_spline_->starting_position();
James Kuszmaul4303a5d2021-10-23 19:58:48 -0700103 } else {
Austin Schuh3653cf22021-11-12 11:54:51 -0800104 if (practice_robot_) {
105 fender_splines_ = {PlanSpline(
106 std::bind(&AutonomousSplines::FarSideFender, &auto_splines_,
107 std::placeholders::_1, alliance_),
108 SplineDirection::kForward)};
109 starting_position_ = fender_splines_.value()[0].starting_position();
110 CHECK(starting_position_);
111 } else {
112 target_aligned_splines_ = {
113 PlanSpline(
114 std::bind(&AutonomousSplines::TargetAligned1, &auto_splines_,
115 std::placeholders::_1, alliance_),
116 SplineDirection::kForward),
117 PlanSpline(
118 std::bind(&AutonomousSplines::TargetAligned2, &auto_splines_,
119 std::placeholders::_1, alliance_),
120 SplineDirection::kBackward),
121 PlanSpline(
122 std::bind(&AutonomousSplines::TargetAligned3, &auto_splines_,
123 std::placeholders::_1, alliance_),
124 SplineDirection::kForward)};
125 starting_position_ =
126 target_aligned_splines_.value()[0].starting_position();
127 CHECK(starting_position_);
128 }
James Kuszmaul99af8b52021-03-28 10:50:15 -0700129 }
James Kuszmaulead4da62021-10-24 17:36:54 -0700130
131 is_planned_ = true;
132
James Kuszmaul4303a5d2021-10-23 19:58:48 -0700133 MaybeSendStartingPosition();
milind upadhyay47a0ab32020-11-25 19:34:41 -0800134}
Stephan Massaltd021f972020-01-05 20:41:23 -0800135
136void AutonomousActor::Reset() {
137 InitializeEncoders();
138 ResetDrivetrain();
milind upadhyayb2e840a2021-03-27 13:54:49 -0700139 RetractIntake();
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800140
James Kuszmaulddd2ba62020-03-08 22:17:13 -0700141 joystick_state_fetcher_.Fetch();
142 CHECK(joystick_state_fetcher_.get() != nullptr)
143 << "Expect at least one JoystickState message before running auto...";
144 alliance_ = joystick_state_fetcher_->alliance();
Stephan Massaltd021f972020-01-05 20:41:23 -0800145}
146
147bool AutonomousActor::RunAction(
Austin Schuh6fb0a6d2021-01-23 15:43:17 -0800148 const ::frc971::autonomous::AutonomousActionParams *params) {
Stephan Massaltd021f972020-01-05 20:41:23 -0800149 Reset();
James Kuszmaul4303a5d2021-10-23 19:58:48 -0700150 if (!user_indicated_safe_to_reset_) {
151 AOS_LOG(WARNING, "Didn't send starting position prior to starting auto.");
milind-u0e203782021-10-30 21:57:20 -0700152 CHECK(starting_position_);
James Kuszmaul4303a5d2021-10-23 19:58:48 -0700153 SendStartingPosition(starting_position_.value());
154 }
James Kuszmaulead4da62021-10-24 17:36:54 -0700155 // Clear this so that we don't accidentally resend things as soon as we replan
156 // later.
157 user_indicated_safe_to_reset_ = false;
158 is_planned_ = false;
159 starting_position_.reset();
James Kuszmaul99af8b52021-03-28 10:50:15 -0700160
milind upadhyay47a0ab32020-11-25 19:34:41 -0800161 AOS_LOG(INFO, "Params are %d\n", params->mode());
James Kuszmaulddd2ba62020-03-08 22:17:13 -0700162 if (alliance_ == aos::Alliance::kInvalid) {
163 AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection.");
164 return false;
165 }
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700166 if (absl::GetFlag(FLAGS_spline_auto)) {
Austin Schuhd0e9e062021-10-24 17:40:58 -0700167 SplineAuto();
milind upadhyay47a0ab32020-11-25 19:34:41 -0800168 } else {
Austin Schuh3653cf22021-11-12 11:54:51 -0800169 if (practice_robot_) {
170 Fender();
171 } else {
172 TargetAligned();
173 }
milind upadhyay47a0ab32020-11-25 19:34:41 -0800174 }
Austin Schuh3653cf22021-11-12 11:54:51 -0800175
milind upadhyay47a0ab32020-11-25 19:34:41 -0800176 return true;
177}
Stephan Massaltd021f972020-01-05 20:41:23 -0800178
James Kuszmaul99af8b52021-03-28 10:50:15 -0700179void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) {
kyle96c406e2021-02-27 14:07:22 -0800180 // Set up the starting position for the blue alliance.
kyle96c406e2021-02-27 14:07:22 -0800181
182 // TODO(james): Resetting the localizer breaks the left/right statespace
183 // controller. That is a bug, but we can fix that later by not resetting.
184 auto builder = localizer_control_sender_.MakeBuilder();
185
186 LocalizerControl::Builder localizer_control_builder =
187 builder.MakeBuilder<LocalizerControl>();
James Kuszmaul99af8b52021-03-28 10:50:15 -0700188 localizer_control_builder.add_x(start(0));
189 localizer_control_builder.add_y(start(1));
190 localizer_control_builder.add_theta(start(2));
kyle96c406e2021-02-27 14:07:22 -0800191 localizer_control_builder.add_theta_uncertainty(0.00001);
milind-u0e203782021-10-30 21:57:20 -0700192 LOG(INFO) << "User button pressed, x: " << start(0) << " y: " << start(1)
193 << " theta: " << start(2);
milind1f1dca32021-07-03 13:50:07 -0700194 if (builder.Send(localizer_control_builder.Finish()) !=
195 aos::RawSender::Error::kOk) {
kyle96c406e2021-02-27 14:07:22 -0800196 AOS_LOG(ERROR, "Failed to reset localizer.\n");
197 }
198}
199
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700200void AutonomousActor::TargetAligned() {
Austin Schuhd0e9e062021-10-24 17:40:58 -0700201 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700202 CHECK(target_aligned_splines_);
203 auto &splines = *target_aligned_splines_;
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700204
Austin Schuhd0e9e062021-10-24 17:40:58 -0700205 // Spin up.
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700206 set_shooting(true);
milind-u0e203782021-10-30 21:57:20 -0700207 set_preloading(true);
208 set_shooter_tracking(true);
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700209 SendSuperstructureGoal();
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700210 if (!WaitForBallsShot(3)) return;
milind-u0e203782021-10-30 21:57:20 -0700211 LOG(INFO) << "Shot balls";
212 set_shooter_tracking(false);
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700213
milind-u0e203782021-10-30 21:57:20 -0700214 // Drive and intake 3 balls in front of the trench run
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700215 set_shooting(false);
Austin Schuhd0e9e062021-10-24 17:40:58 -0700216 ExtendIntake();
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700217 SendSuperstructureGoal();
218
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700219 if (!splines[0].WaitForPlan()) return;
220 splines[0].Start();
221
222 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
milind-u0e203782021-10-30 21:57:20 -0700223
Austin Schuhd0e9e062021-10-24 17:40:58 -0700224 std::this_thread::sleep_for(chrono::milliseconds(200));
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700225 RetractIntake();
226
milind-u0e203782021-10-30 21:57:20 -0700227 // Drive back to shooting position
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700228 if (!splines[1].WaitForPlan()) return;
229 splines[1].Start();
230
Austin Schuhd0e9e062021-10-24 17:40:58 -0700231 if (!splines[1].WaitForSplineDistanceRemaining(2.0)) return;
232 // Reverse the rollers for a moment to try to unjam any jammed balls. Since
233 // we are moving here, this is free to try.
234 set_roller_voltage(-12.0);
235 std::this_thread::sleep_for(chrono::milliseconds(300));
236 set_roller_voltage(0.0);
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700237
Austin Schuhd0e9e062021-10-24 17:40:58 -0700238 // Once we come to a stop, give the robot a moment to settle down. This makes
239 // the shot more accurate.
240 if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return;
milind-u0e203782021-10-30 21:57:20 -0700241 set_shooter_tracking(true);
Austin Schuhd0e9e062021-10-24 17:40:58 -0700242 std::this_thread::sleep_for(chrono::milliseconds(1500));
243 set_shooting(true);
244 const int balls = Balls();
245
246 SendSuperstructureGoal();
247
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700248 SendSuperstructureGoal();
249
Austin Schuhd0e9e062021-10-24 17:40:58 -0700250 if (!WaitUntilAbsoluteBallsShot(3 + balls)) return;
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700251
milind-u0e203782021-10-30 21:57:20 -0700252 set_shooting(false);
253 set_roller_voltage(0.0);
254 set_shooter_tracking(false);
255 set_preloading(false);
256 SendSuperstructureGoal();
257
258 // Drive close to the rendezvous point in the center of the field so that the
259 // driver can intake balls there right after auto ends.
260 if (!splines[2].WaitForPlan()) return;
261 splines[2].Start();
262
263 if (!splines[2].WaitForSplineDistanceRemaining(0.02)) return;
264
Austin Schuhd0e9e062021-10-24 17:40:58 -0700265 LOG(INFO) << "Took "
266 << chrono::duration<double>(aos::monotonic_clock::now() -
267 start_time)
268 .count();
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700269}
270
Austin Schuh3653cf22021-11-12 11:54:51 -0800271void AutonomousActor::Fender() {
272 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
273 CHECK(fender_splines_);
274 auto &splines = *fender_splines_;
275
276 // Spin up.
277 set_shooting(false);
278 set_preloading(true);
279 set_shooter_tracking(false);
280 SendSuperstructureGoal();
281
282 if (!splines[0].WaitForPlan()) return;
283 splines[0].Start();
284
285 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
286
287 SendSuperstructureGoal();
288 std::this_thread::sleep_for(chrono::milliseconds(500));
289 ApplyThrottle(0.2);
290 set_shooting(true);
291 SendSuperstructureGoal();
292 LOG(INFO) << "Took "
293 << chrono::duration<double>(aos::monotonic_clock::now() -
294 start_time)
295 .count();
296}
297
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700298void AutonomousActor::JustShoot() {
299 // shoot pre-loaded balls
300 set_shooter_tracking(true);
301 set_shooting(true);
302 SendSuperstructureGoal();
303
304 if (!WaitForBallsShot(3)) return;
305
306 set_shooting(false);
307 set_shooter_tracking(true);
308 SendSuperstructureGoal();
309}
310
311void AutonomousActor::TargetOffset() {
312 CHECK(target_offset_splines_);
313 auto &splines = *target_offset_splines_;
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700314
315 // spin up shooter
316 set_shooter_tracking(true);
317 SendSuperstructureGoal();
318 ExtendIntake();
319
320 // pickup 2 more balls in front of the trench run
321 if (!splines[0].WaitForPlan()) return;
322 splines[0].Start();
323
324 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
325 RetractIntake();
326
327 if (!splines[1].WaitForPlan()) return;
328 splines[1].Start();
329
330 if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return;
331
332 // shoot the balls from in front of the goal.
333 set_shooting(true);
334 SendSuperstructureGoal();
335
336 if (!WaitForBallsShot(5)) return;
337
338 set_shooting(false);
339 set_shooter_tracking(false);
340 SendSuperstructureGoal();
341}
342
milind upadhyay47a0ab32020-11-25 19:34:41 -0800343void AutonomousActor::SplineAuto() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700344 CHECK(test_spline_);
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800345
James Kuszmaul99af8b52021-03-28 10:50:15 -0700346 if (!test_spline_->WaitForPlan()) return;
347 test_spline_->Start();
Stephan Massaltd021f972020-01-05 20:41:23 -0800348
James Kuszmaul99af8b52021-03-28 10:50:15 -0700349 if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return;
kyle96c406e2021-02-27 14:07:22 -0800350}
351
milind upadhyay47a0ab32020-11-25 19:34:41 -0800352ProfileParametersT MakeProfileParametersT(const float max_velocity,
353 const float max_acceleration) {
354 ProfileParametersT params;
355 params.max_velocity = max_velocity;
356 params.max_acceleration = max_acceleration;
357 return params;
358}
359
360bool AutonomousActor::DriveFwd() {
Austin Schuhfd1715f2021-01-30 16:58:24 -0800361 const ProfileParametersT kDrive = MakeProfileParametersT(0.3f, 1.0f);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800362 const ProfileParametersT kTurn = MakeProfileParametersT(5.0f, 15.0f);
Austin Schuhfd1715f2021-01-30 16:58:24 -0800363 StartDrive(1.0, 0.0, kDrive, kTurn);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800364 return WaitForDriveDone();
365}
Sabina Leavera0b43b42021-03-03 20:30:04 -0800366
367void AutonomousActor::SendSuperstructureGoal() {
Sabina Leavera0b43b42021-03-03 20:30:04 -0800368 auto builder = superstructure_goal_sender_.MakeBuilder();
369
370 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
371 intake_offset;
milind upadhyayb9dec712021-03-20 15:47:51 -0700372
Sabina Leavera0b43b42021-03-03 20:30:04 -0800373 {
374 StaticZeroingSingleDOFProfiledSubsystemGoal::Builder intake_builder =
375 builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>();
376
milind upadhyayb9dec712021-03-20 15:47:51 -0700377 frc971::ProfileParameters::Builder profile_params_builder =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800378 builder.MakeBuilder<frc971::ProfileParameters>();
Austin Schuhb39a21c2021-03-31 20:12:18 -0700379 profile_params_builder.add_max_velocity(20.0);
380 profile_params_builder.add_max_acceleration(60.0);
milind upadhyayb9dec712021-03-20 15:47:51 -0700381 flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800382 profile_params_builder.Finish();
383 intake_builder.add_unsafe_goal(intake_goal_);
384 intake_builder.add_profile_params(profile_params_offset);
385 intake_offset = intake_builder.Finish();
386 }
387
Austin Schuh3653cf22021-11-12 11:54:51 -0800388 flatbuffers::Offset<superstructure::ShooterGoal> shooter_offset =
389 superstructure::CreateShooterGoal(*builder.fbb(), 400.0, 200.0);
390
Sabina Leavera0b43b42021-03-03 20:30:04 -0800391 superstructure::Goal::Builder superstructure_builder =
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700392 builder.MakeBuilder<superstructure::Goal>();
milind upadhyayb9dec712021-03-20 15:47:51 -0700393
Sabina Leavera0b43b42021-03-03 20:30:04 -0800394 superstructure_builder.add_intake(intake_offset);
milind-u0e203782021-10-30 21:57:20 -0700395 superstructure_builder.add_intake_preloading(preloading_);
Austin Schuh3653cf22021-11-12 11:54:51 -0800396 if (!shooter_tracking_ && shooting_) {
397 superstructure_builder.add_shooter(shooter_offset);
398 }
Sabina Leavera0b43b42021-03-03 20:30:04 -0800399 superstructure_builder.add_roller_voltage(roller_voltage_);
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700400 superstructure_builder.add_roller_speed_compensation(
401 kRollerSpeedCompensation);
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700402 superstructure_builder.add_hood_tracking(shooter_tracking_);
403 superstructure_builder.add_turret_tracking(shooter_tracking_);
404 superstructure_builder.add_shooter_tracking(shooter_tracking_);
405 superstructure_builder.add_shooting(shooting_);
Sabina Leavera0b43b42021-03-03 20:30:04 -0800406
milind1f1dca32021-07-03 13:50:07 -0700407 if (builder.Send(superstructure_builder.Finish()) !=
408 aos::RawSender::Error::kOk) {
Sabina Leavera0b43b42021-03-03 20:30:04 -0800409 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
410 }
Sabina Leavera0b43b42021-03-03 20:30:04 -0800411}
milind upadhyay5e589d72021-03-27 13:47:18 -0700412
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700413void AutonomousActor::ExtendIntake() {
Austin Schuhd0e9e062021-10-24 17:40:58 -0700414 set_intake_goal(1.30);
415 set_roller_voltage(6.0);
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700416 SendSuperstructureGoal();
417}
418
milind upadhyay5e589d72021-03-27 13:47:18 -0700419void AutonomousActor::RetractIntake() {
420 set_intake_goal(-0.89);
Austin Schuhd0e9e062021-10-24 17:40:58 -0700421 set_roller_voltage(6.0);
milind upadhyay5e589d72021-03-27 13:47:18 -0700422 SendSuperstructureGoal();
423}
424
Austin Schuhd0e9e062021-10-24 17:40:58 -0700425int AutonomousActor::Balls() {
Ravago Jones1f32d622021-08-26 12:20:36 -0700426 superstructure_status_fetcher_.Fetch();
427 CHECK(superstructure_status_fetcher_.get() != nullptr);
Austin Schuhd0e9e062021-10-24 17:40:58 -0700428 return superstructure_status_fetcher_->shooter()->balls_shot();
429}
Ravago Jones1f32d622021-08-26 12:20:36 -0700430
Austin Schuhd0e9e062021-10-24 17:40:58 -0700431bool AutonomousActor::WaitUntilAbsoluteBallsShot(int absolute_balls) {
Ravago Jones1f32d622021-08-26 12:20:36 -0700432 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
433 event_loop()->monotonic_now(),
Stephan Pleines743f83a2024-02-02 18:32:09 -0800434 aos::common::actions::kLoopOffset);
milind-u0e203782021-10-30 21:57:20 -0700435 superstructure_status_fetcher_.Fetch();
436 CHECK(superstructure_status_fetcher_.get() != nullptr);
437 int last_balls = superstructure_status_fetcher_->shooter()->balls_shot();
438 LOG(INFO) << "Waiting for balls, started with " << absolute_balls;
Ravago Jones1f32d622021-08-26 12:20:36 -0700439 while (true) {
440 if (ShouldCancel()) {
441 return false;
442 }
443 phased_loop.SleepUntilNext();
444 superstructure_status_fetcher_.Fetch();
Austin Schuhd0e9e062021-10-24 17:40:58 -0700445 CHECK(superstructure_status_fetcher_.get() != nullptr);
milind-u0e203782021-10-30 21:57:20 -0700446 if (superstructure_status_fetcher_->shooter()->balls_shot() != last_balls) {
447 LOG(INFO) << "Shot "
448 << superstructure_status_fetcher_->shooter()->balls_shot() -
449 last_balls
450 << " balls, now at "
451 << superstructure_status_fetcher_->shooter()->balls_shot();
452 }
Austin Schuhd0e9e062021-10-24 17:40:58 -0700453 if (superstructure_status_fetcher_->shooter()->balls_shot() >=
454 absolute_balls) {
Ravago Jones1f32d622021-08-26 12:20:36 -0700455 return true;
456 }
milind-u0e203782021-10-30 21:57:20 -0700457
458 last_balls = superstructure_status_fetcher_->shooter()->balls_shot();
Ravago Jones1f32d622021-08-26 12:20:36 -0700459 }
460}
461
Austin Schuhd0e9e062021-10-24 17:40:58 -0700462bool AutonomousActor::WaitForBallsShot(int num_wanted) {
463 return WaitUntilAbsoluteBallsShot(Balls() + num_wanted);
464}
465
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800466} // namespace y2020::actors