blob: 404372de2dea5ef5e2c72e448954e2746abd3b85 [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
7#include "aos/logging/logging.h"
Austin Schuh3653cf22021-11-12 11:54:51 -08008#include "aos/network/team_number.h"
James Kuszmaulddd2ba62020-03-08 22:17:13 -07009#include "aos/util/math.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080010#include "frc971/control_loops/drivetrain/localizer_generated.h"
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080011#include "y2020/actors/auto_splines.h"
Austin Schuh3653cf22021-11-12 11:54:51 -080012#include "y2020/constants.h"
Ravago Jonesc2a08022021-02-06 17:40:54 -080013#include "y2020/control_loops/drivetrain/drivetrain_base.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080014
Austin Schuh3fb9e422021-03-31 20:11:32 -070015DEFINE_bool(spline_auto, false, "If true, define a spline autonomous mode");
Ravago Jonesa7b3c822021-08-26 12:36:03 -070016DEFINE_bool(just_shoot, false,
17 "If true, run the autonomous that just shoots balls.");
milind upadhyay47a0ab32020-11-25 19:34:41 -080018
Stephan Pleinesf63bde82024-01-13 15:59:33 -080019namespace y2020::actors {
Stephan Massaltd021f972020-01-05 20:41:23 -080020
21using ::aos::monotonic_clock;
22using ::frc971::ProfileParametersT;
23using frc971::control_loops::drivetrain::LocalizerControl;
24namespace chrono = ::std::chrono;
25
26AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
27 : frc971::autonomous::BaseAutonomousActor(
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080028 event_loop, control_loops::drivetrain::GetDrivetrainConfig()),
Ravago Jonesc2a08022021-02-06 17:40:54 -080029 localizer_control_sender_(
30 event_loop->MakeSender<
31 ::frc971::control_loops::drivetrain::LocalizerControl>(
32 "/drivetrain")),
Austin Schuh67e127e2021-03-27 13:25:23 -070033 superstructure_goal_sender_(
34 event_loop->MakeSender<control_loops::superstructure::Goal>(
35 "/superstructure")),
Ravago Jones1f32d622021-08-26 12:20:36 -070036 superstructure_status_fetcher_(
37 event_loop->MakeFetcher<y2020::control_loops::superstructure::Status>(
38 "/superstructure")),
Austin Schuhba77b7e2021-10-25 21:58:54 -070039 joystick_state_fetcher_(
40 event_loop->MakeFetcher<aos::JoystickState>("/aos")),
41 robot_state_fetcher_(event_loop->MakeFetcher<aos::RobotState>("/aos")),
Ravago Jonesc2a08022021-02-06 17:40:54 -080042 auto_splines_() {
Austin Schuh3653cf22021-11-12 11:54:51 -080043 practice_robot_ =
44 ::aos::network::GetTeamNumber() == constants::Values::kPracticeTeamNumber;
45
Austin Schuhd0e9e062021-10-24 17:40:58 -070046 set_max_drivetrain_voltage(12.0);
James Kuszmaul99af8b52021-03-28 10:50:15 -070047 replan_timer_ = event_loop->AddTimer([this]() { Replan(); });
48 event_loop->OnRun([this, event_loop]() {
Philipp Schradera6712522023-07-05 20:25:11 -070049 replan_timer_->Schedule(event_loop->monotonic_now());
50 button_poll_->Schedule(event_loop->monotonic_now(),
51 chrono::milliseconds(50));
James Kuszmaul99af8b52021-03-28 10:50:15 -070052 });
Austin Schuhba77b7e2021-10-25 21:58:54 -070053
54 button_poll_ = event_loop->AddTimer([this]() {
James Kuszmaulead4da62021-10-24 17:36:54 -070055 const aos::monotonic_clock::time_point now =
56 this->event_loop()->context().monotonic_event_time;
Austin Schuhba77b7e2021-10-25 21:58:54 -070057 if (robot_state_fetcher_.Fetch()) {
58 if (robot_state_fetcher_->user_button()) {
59 user_indicated_safe_to_reset_ = true;
60 MaybeSendStartingPosition();
61 }
James Kuszmaul4303a5d2021-10-23 19:58:48 -070062 }
Austin Schuhba77b7e2021-10-25 21:58:54 -070063 if (joystick_state_fetcher_.Fetch()) {
64 if (joystick_state_fetcher_->has_alliance() &&
65 (joystick_state_fetcher_->alliance() != alliance_)) {
66 alliance_ = joystick_state_fetcher_->alliance();
James Kuszmaulead4da62021-10-24 17:36:54 -070067 is_planned_ = false;
68 // Only kick the planning out by 2 seconds. If we end up enabled in that
69 // second, then we will kick it out further based on the code below.
Philipp Schradera6712522023-07-05 20:25:11 -070070 replan_timer_->Schedule(now + std::chrono::seconds(2));
James Kuszmaulead4da62021-10-24 17:36:54 -070071 }
72 if (joystick_state_fetcher_->enabled()) {
73 if (!is_planned_) {
74 // Only replan once we've been disabled for 5 seconds.
Philipp Schradera6712522023-07-05 20:25:11 -070075 replan_timer_->Schedule(now + std::chrono::seconds(5));
James Kuszmaulead4da62021-10-24 17:36:54 -070076 }
Austin Schuhba77b7e2021-10-25 21:58:54 -070077 }
James Kuszmaul4303a5d2021-10-23 19:58:48 -070078 }
79 });
80}
81
82void AutonomousActor::MaybeSendStartingPosition() {
James Kuszmaulead4da62021-10-24 17:36:54 -070083 if (is_planned_ && user_indicated_safe_to_reset_ &&
84 !sent_starting_position_) {
James Kuszmaul4303a5d2021-10-23 19:58:48 -070085 CHECK(starting_position_);
86 SendStartingPosition(starting_position_.value());
87 }
James Kuszmaul99af8b52021-03-28 10:50:15 -070088}
89
90void AutonomousActor::Replan() {
milind-u0e203782021-10-30 21:57:20 -070091 LOG(INFO) << "Alliance " << static_cast<int>(alliance_);
Austin Schuhd0e9e062021-10-24 17:40:58 -070092 if (alliance_ == aos::Alliance::kInvalid) {
93 return;
94 }
milind-u0e203782021-10-30 21:57:20 -070095 sent_starting_position_ = false;
Ravago Jones8c5737e2021-10-16 15:36:12 -070096 if (FLAGS_spline_auto) {
Tyler Chatowbf0609c2021-07-31 16:13:27 -070097 test_spline_ = PlanSpline(std::bind(&AutonomousSplines::TestSpline,
98 &auto_splines_, std::placeholders::_1),
99 SplineDirection::kForward);
James Kuszmaul4303a5d2021-10-23 19:58:48 -0700100 starting_position_ = test_spline_->starting_position();
James Kuszmaul4303a5d2021-10-23 19:58:48 -0700101 } else {
Austin Schuh3653cf22021-11-12 11:54:51 -0800102 if (practice_robot_) {
103 fender_splines_ = {PlanSpline(
104 std::bind(&AutonomousSplines::FarSideFender, &auto_splines_,
105 std::placeholders::_1, alliance_),
106 SplineDirection::kForward)};
107 starting_position_ = fender_splines_.value()[0].starting_position();
108 CHECK(starting_position_);
109 } else {
110 target_aligned_splines_ = {
111 PlanSpline(
112 std::bind(&AutonomousSplines::TargetAligned1, &auto_splines_,
113 std::placeholders::_1, alliance_),
114 SplineDirection::kForward),
115 PlanSpline(
116 std::bind(&AutonomousSplines::TargetAligned2, &auto_splines_,
117 std::placeholders::_1, alliance_),
118 SplineDirection::kBackward),
119 PlanSpline(
120 std::bind(&AutonomousSplines::TargetAligned3, &auto_splines_,
121 std::placeholders::_1, alliance_),
122 SplineDirection::kForward)};
123 starting_position_ =
124 target_aligned_splines_.value()[0].starting_position();
125 CHECK(starting_position_);
126 }
James Kuszmaul99af8b52021-03-28 10:50:15 -0700127 }
James Kuszmaulead4da62021-10-24 17:36:54 -0700128
129 is_planned_ = true;
130
James Kuszmaul4303a5d2021-10-23 19:58:48 -0700131 MaybeSendStartingPosition();
milind upadhyay47a0ab32020-11-25 19:34:41 -0800132}
Stephan Massaltd021f972020-01-05 20:41:23 -0800133
134void AutonomousActor::Reset() {
135 InitializeEncoders();
136 ResetDrivetrain();
milind upadhyayb2e840a2021-03-27 13:54:49 -0700137 RetractIntake();
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800138
James Kuszmaulddd2ba62020-03-08 22:17:13 -0700139 joystick_state_fetcher_.Fetch();
140 CHECK(joystick_state_fetcher_.get() != nullptr)
141 << "Expect at least one JoystickState message before running auto...";
142 alliance_ = joystick_state_fetcher_->alliance();
Stephan Massaltd021f972020-01-05 20:41:23 -0800143}
144
145bool AutonomousActor::RunAction(
Austin Schuh6fb0a6d2021-01-23 15:43:17 -0800146 const ::frc971::autonomous::AutonomousActionParams *params) {
Stephan Massaltd021f972020-01-05 20:41:23 -0800147 Reset();
James Kuszmaul4303a5d2021-10-23 19:58:48 -0700148 if (!user_indicated_safe_to_reset_) {
149 AOS_LOG(WARNING, "Didn't send starting position prior to starting auto.");
milind-u0e203782021-10-30 21:57:20 -0700150 CHECK(starting_position_);
James Kuszmaul4303a5d2021-10-23 19:58:48 -0700151 SendStartingPosition(starting_position_.value());
152 }
James Kuszmaulead4da62021-10-24 17:36:54 -0700153 // Clear this so that we don't accidentally resend things as soon as we replan
154 // later.
155 user_indicated_safe_to_reset_ = false;
156 is_planned_ = false;
157 starting_position_.reset();
James Kuszmaul99af8b52021-03-28 10:50:15 -0700158
milind upadhyay47a0ab32020-11-25 19:34:41 -0800159 AOS_LOG(INFO, "Params are %d\n", params->mode());
James Kuszmaulddd2ba62020-03-08 22:17:13 -0700160 if (alliance_ == aos::Alliance::kInvalid) {
161 AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection.");
162 return false;
163 }
Austin Schuhd0e9e062021-10-24 17:40:58 -0700164 if (FLAGS_spline_auto) {
165 SplineAuto();
milind upadhyay47a0ab32020-11-25 19:34:41 -0800166 } else {
Austin Schuh3653cf22021-11-12 11:54:51 -0800167 if (practice_robot_) {
168 Fender();
169 } else {
170 TargetAligned();
171 }
milind upadhyay47a0ab32020-11-25 19:34:41 -0800172 }
Austin Schuh3653cf22021-11-12 11:54:51 -0800173
milind upadhyay47a0ab32020-11-25 19:34:41 -0800174 return true;
175}
Stephan Massaltd021f972020-01-05 20:41:23 -0800176
James Kuszmaul99af8b52021-03-28 10:50:15 -0700177void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) {
kyle96c406e2021-02-27 14:07:22 -0800178 // Set up the starting position for the blue alliance.
kyle96c406e2021-02-27 14:07:22 -0800179
180 // TODO(james): Resetting the localizer breaks the left/right statespace
181 // controller. That is a bug, but we can fix that later by not resetting.
182 auto builder = localizer_control_sender_.MakeBuilder();
183
184 LocalizerControl::Builder localizer_control_builder =
185 builder.MakeBuilder<LocalizerControl>();
James Kuszmaul99af8b52021-03-28 10:50:15 -0700186 localizer_control_builder.add_x(start(0));
187 localizer_control_builder.add_y(start(1));
188 localizer_control_builder.add_theta(start(2));
kyle96c406e2021-02-27 14:07:22 -0800189 localizer_control_builder.add_theta_uncertainty(0.00001);
milind-u0e203782021-10-30 21:57:20 -0700190 LOG(INFO) << "User button pressed, x: " << start(0) << " y: " << start(1)
191 << " theta: " << start(2);
milind1f1dca32021-07-03 13:50:07 -0700192 if (builder.Send(localizer_control_builder.Finish()) !=
193 aos::RawSender::Error::kOk) {
kyle96c406e2021-02-27 14:07:22 -0800194 AOS_LOG(ERROR, "Failed to reset localizer.\n");
195 }
196}
197
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700198void AutonomousActor::TargetAligned() {
Austin Schuhd0e9e062021-10-24 17:40:58 -0700199 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700200 CHECK(target_aligned_splines_);
201 auto &splines = *target_aligned_splines_;
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700202
Austin Schuhd0e9e062021-10-24 17:40:58 -0700203 // Spin up.
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700204 set_shooting(true);
milind-u0e203782021-10-30 21:57:20 -0700205 set_preloading(true);
206 set_shooter_tracking(true);
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700207 SendSuperstructureGoal();
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700208 if (!WaitForBallsShot(3)) return;
milind-u0e203782021-10-30 21:57:20 -0700209 LOG(INFO) << "Shot balls";
210 set_shooter_tracking(false);
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700211
milind-u0e203782021-10-30 21:57:20 -0700212 // Drive and intake 3 balls in front of the trench run
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700213 set_shooting(false);
Austin Schuhd0e9e062021-10-24 17:40:58 -0700214 ExtendIntake();
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700215 SendSuperstructureGoal();
216
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700217 if (!splines[0].WaitForPlan()) return;
218 splines[0].Start();
219
220 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
milind-u0e203782021-10-30 21:57:20 -0700221
Austin Schuhd0e9e062021-10-24 17:40:58 -0700222 std::this_thread::sleep_for(chrono::milliseconds(200));
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700223 RetractIntake();
224
milind-u0e203782021-10-30 21:57:20 -0700225 // Drive back to shooting position
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700226 if (!splines[1].WaitForPlan()) return;
227 splines[1].Start();
228
Austin Schuhd0e9e062021-10-24 17:40:58 -0700229 if (!splines[1].WaitForSplineDistanceRemaining(2.0)) return;
230 // Reverse the rollers for a moment to try to unjam any jammed balls. Since
231 // we are moving here, this is free to try.
232 set_roller_voltage(-12.0);
233 std::this_thread::sleep_for(chrono::milliseconds(300));
234 set_roller_voltage(0.0);
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700235
Austin Schuhd0e9e062021-10-24 17:40:58 -0700236 // Once we come to a stop, give the robot a moment to settle down. This makes
237 // the shot more accurate.
238 if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return;
milind-u0e203782021-10-30 21:57:20 -0700239 set_shooter_tracking(true);
Austin Schuhd0e9e062021-10-24 17:40:58 -0700240 std::this_thread::sleep_for(chrono::milliseconds(1500));
241 set_shooting(true);
242 const int balls = Balls();
243
244 SendSuperstructureGoal();
245
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700246 SendSuperstructureGoal();
247
Austin Schuhd0e9e062021-10-24 17:40:58 -0700248 if (!WaitUntilAbsoluteBallsShot(3 + balls)) return;
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700249
milind-u0e203782021-10-30 21:57:20 -0700250 set_shooting(false);
251 set_roller_voltage(0.0);
252 set_shooter_tracking(false);
253 set_preloading(false);
254 SendSuperstructureGoal();
255
256 // Drive close to the rendezvous point in the center of the field so that the
257 // driver can intake balls there right after auto ends.
258 if (!splines[2].WaitForPlan()) return;
259 splines[2].Start();
260
261 if (!splines[2].WaitForSplineDistanceRemaining(0.02)) return;
262
Austin Schuhd0e9e062021-10-24 17:40:58 -0700263 LOG(INFO) << "Took "
264 << chrono::duration<double>(aos::monotonic_clock::now() -
265 start_time)
266 .count();
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700267}
268
Austin Schuh3653cf22021-11-12 11:54:51 -0800269void AutonomousActor::Fender() {
270 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
271 CHECK(fender_splines_);
272 auto &splines = *fender_splines_;
273
274 // Spin up.
275 set_shooting(false);
276 set_preloading(true);
277 set_shooter_tracking(false);
278 SendSuperstructureGoal();
279
280 if (!splines[0].WaitForPlan()) return;
281 splines[0].Start();
282
283 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
284
285 SendSuperstructureGoal();
286 std::this_thread::sleep_for(chrono::milliseconds(500));
287 ApplyThrottle(0.2);
288 set_shooting(true);
289 SendSuperstructureGoal();
290 LOG(INFO) << "Took "
291 << chrono::duration<double>(aos::monotonic_clock::now() -
292 start_time)
293 .count();
294}
295
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700296void AutonomousActor::JustShoot() {
297 // shoot pre-loaded balls
298 set_shooter_tracking(true);
299 set_shooting(true);
300 SendSuperstructureGoal();
301
302 if (!WaitForBallsShot(3)) return;
303
304 set_shooting(false);
305 set_shooter_tracking(true);
306 SendSuperstructureGoal();
307}
308
309void AutonomousActor::TargetOffset() {
310 CHECK(target_offset_splines_);
311 auto &splines = *target_offset_splines_;
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700312
313 // spin up shooter
314 set_shooter_tracking(true);
315 SendSuperstructureGoal();
316 ExtendIntake();
317
318 // pickup 2 more balls in front of the trench run
319 if (!splines[0].WaitForPlan()) return;
320 splines[0].Start();
321
322 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
323 RetractIntake();
324
325 if (!splines[1].WaitForPlan()) return;
326 splines[1].Start();
327
328 if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return;
329
330 // shoot the balls from in front of the goal.
331 set_shooting(true);
332 SendSuperstructureGoal();
333
334 if (!WaitForBallsShot(5)) return;
335
336 set_shooting(false);
337 set_shooter_tracking(false);
338 SendSuperstructureGoal();
339}
340
milind upadhyay47a0ab32020-11-25 19:34:41 -0800341void AutonomousActor::SplineAuto() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700342 CHECK(test_spline_);
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800343
James Kuszmaul99af8b52021-03-28 10:50:15 -0700344 if (!test_spline_->WaitForPlan()) return;
345 test_spline_->Start();
Stephan Massaltd021f972020-01-05 20:41:23 -0800346
James Kuszmaul99af8b52021-03-28 10:50:15 -0700347 if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return;
kyle96c406e2021-02-27 14:07:22 -0800348}
349
milind upadhyay47a0ab32020-11-25 19:34:41 -0800350ProfileParametersT MakeProfileParametersT(const float max_velocity,
351 const float max_acceleration) {
352 ProfileParametersT params;
353 params.max_velocity = max_velocity;
354 params.max_acceleration = max_acceleration;
355 return params;
356}
357
358bool AutonomousActor::DriveFwd() {
Austin Schuhfd1715f2021-01-30 16:58:24 -0800359 const ProfileParametersT kDrive = MakeProfileParametersT(0.3f, 1.0f);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800360 const ProfileParametersT kTurn = MakeProfileParametersT(5.0f, 15.0f);
Austin Schuhfd1715f2021-01-30 16:58:24 -0800361 StartDrive(1.0, 0.0, kDrive, kTurn);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800362 return WaitForDriveDone();
363}
Sabina Leavera0b43b42021-03-03 20:30:04 -0800364
365void AutonomousActor::SendSuperstructureGoal() {
Sabina Leavera0b43b42021-03-03 20:30:04 -0800366 auto builder = superstructure_goal_sender_.MakeBuilder();
367
368 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
369 intake_offset;
milind upadhyayb9dec712021-03-20 15:47:51 -0700370
Sabina Leavera0b43b42021-03-03 20:30:04 -0800371 {
372 StaticZeroingSingleDOFProfiledSubsystemGoal::Builder intake_builder =
373 builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>();
374
milind upadhyayb9dec712021-03-20 15:47:51 -0700375 frc971::ProfileParameters::Builder profile_params_builder =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800376 builder.MakeBuilder<frc971::ProfileParameters>();
Austin Schuhb39a21c2021-03-31 20:12:18 -0700377 profile_params_builder.add_max_velocity(20.0);
378 profile_params_builder.add_max_acceleration(60.0);
milind upadhyayb9dec712021-03-20 15:47:51 -0700379 flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800380 profile_params_builder.Finish();
381 intake_builder.add_unsafe_goal(intake_goal_);
382 intake_builder.add_profile_params(profile_params_offset);
383 intake_offset = intake_builder.Finish();
384 }
385
Austin Schuh3653cf22021-11-12 11:54:51 -0800386 flatbuffers::Offset<superstructure::ShooterGoal> shooter_offset =
387 superstructure::CreateShooterGoal(*builder.fbb(), 400.0, 200.0);
388
Sabina Leavera0b43b42021-03-03 20:30:04 -0800389 superstructure::Goal::Builder superstructure_builder =
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700390 builder.MakeBuilder<superstructure::Goal>();
milind upadhyayb9dec712021-03-20 15:47:51 -0700391
Sabina Leavera0b43b42021-03-03 20:30:04 -0800392 superstructure_builder.add_intake(intake_offset);
milind-u0e203782021-10-30 21:57:20 -0700393 superstructure_builder.add_intake_preloading(preloading_);
Austin Schuh3653cf22021-11-12 11:54:51 -0800394 if (!shooter_tracking_ && shooting_) {
395 superstructure_builder.add_shooter(shooter_offset);
396 }
Sabina Leavera0b43b42021-03-03 20:30:04 -0800397 superstructure_builder.add_roller_voltage(roller_voltage_);
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700398 superstructure_builder.add_roller_speed_compensation(
399 kRollerSpeedCompensation);
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700400 superstructure_builder.add_hood_tracking(shooter_tracking_);
401 superstructure_builder.add_turret_tracking(shooter_tracking_);
402 superstructure_builder.add_shooter_tracking(shooter_tracking_);
403 superstructure_builder.add_shooting(shooting_);
Sabina Leavera0b43b42021-03-03 20:30:04 -0800404
milind1f1dca32021-07-03 13:50:07 -0700405 if (builder.Send(superstructure_builder.Finish()) !=
406 aos::RawSender::Error::kOk) {
Sabina Leavera0b43b42021-03-03 20:30:04 -0800407 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
408 }
Sabina Leavera0b43b42021-03-03 20:30:04 -0800409}
milind upadhyay5e589d72021-03-27 13:47:18 -0700410
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700411void AutonomousActor::ExtendIntake() {
Austin Schuhd0e9e062021-10-24 17:40:58 -0700412 set_intake_goal(1.30);
413 set_roller_voltage(6.0);
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700414 SendSuperstructureGoal();
415}
416
milind upadhyay5e589d72021-03-27 13:47:18 -0700417void AutonomousActor::RetractIntake() {
418 set_intake_goal(-0.89);
Austin Schuhd0e9e062021-10-24 17:40:58 -0700419 set_roller_voltage(6.0);
milind upadhyay5e589d72021-03-27 13:47:18 -0700420 SendSuperstructureGoal();
421}
422
Austin Schuhd0e9e062021-10-24 17:40:58 -0700423int AutonomousActor::Balls() {
Ravago Jones1f32d622021-08-26 12:20:36 -0700424 superstructure_status_fetcher_.Fetch();
425 CHECK(superstructure_status_fetcher_.get() != nullptr);
Austin Schuhd0e9e062021-10-24 17:40:58 -0700426 return superstructure_status_fetcher_->shooter()->balls_shot();
427}
Ravago Jones1f32d622021-08-26 12:20:36 -0700428
Austin Schuhd0e9e062021-10-24 17:40:58 -0700429bool AutonomousActor::WaitUntilAbsoluteBallsShot(int absolute_balls) {
Ravago Jones1f32d622021-08-26 12:20:36 -0700430 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
431 event_loop()->monotonic_now(),
Stephan Pleines743f83a2024-02-02 18:32:09 -0800432 aos::common::actions::kLoopOffset);
milind-u0e203782021-10-30 21:57:20 -0700433 superstructure_status_fetcher_.Fetch();
434 CHECK(superstructure_status_fetcher_.get() != nullptr);
435 int last_balls = superstructure_status_fetcher_->shooter()->balls_shot();
436 LOG(INFO) << "Waiting for balls, started with " << absolute_balls;
Ravago Jones1f32d622021-08-26 12:20:36 -0700437 while (true) {
438 if (ShouldCancel()) {
439 return false;
440 }
441 phased_loop.SleepUntilNext();
442 superstructure_status_fetcher_.Fetch();
Austin Schuhd0e9e062021-10-24 17:40:58 -0700443 CHECK(superstructure_status_fetcher_.get() != nullptr);
milind-u0e203782021-10-30 21:57:20 -0700444 if (superstructure_status_fetcher_->shooter()->balls_shot() != last_balls) {
445 LOG(INFO) << "Shot "
446 << superstructure_status_fetcher_->shooter()->balls_shot() -
447 last_balls
448 << " balls, now at "
449 << superstructure_status_fetcher_->shooter()->balls_shot();
450 }
Austin Schuhd0e9e062021-10-24 17:40:58 -0700451 if (superstructure_status_fetcher_->shooter()->balls_shot() >=
452 absolute_balls) {
Ravago Jones1f32d622021-08-26 12:20:36 -0700453 return true;
454 }
milind-u0e203782021-10-30 21:57:20 -0700455
456 last_balls = superstructure_status_fetcher_->shooter()->balls_shot();
Ravago Jones1f32d622021-08-26 12:20:36 -0700457 }
458}
459
Austin Schuhd0e9e062021-10-24 17:40:58 -0700460bool AutonomousActor::WaitForBallsShot(int num_wanted) {
461 return WaitUntilAbsoluteBallsShot(Balls() + num_wanted);
462}
463
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800464} // namespace y2020::actors