blob: eb8ea42052507147c85908e05d07edf68713dfb0 [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"
James Kuszmaulddd2ba62020-03-08 22:17:13 -07008#include "aos/util/math.h"
Stephan Massaltd021f972020-01-05 20:41:23 -08009#include "frc971/control_loops/drivetrain/localizer_generated.h"
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080010#include "y2020/actors/auto_splines.h"
Ravago Jonesc2a08022021-02-06 17:40:54 -080011#include "y2020/control_loops/drivetrain/drivetrain_base.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080012
Austin Schuh3fb9e422021-03-31 20:11:32 -070013DEFINE_bool(spline_auto, false, "If true, define a spline autonomous mode");
Austin Schuhd0e9e062021-10-24 17:40:58 -070014DEFINE_bool(target_aligned, true,
Ravago Jonesa7b3c822021-08-26 12:36:03 -070015 "If true, run the Infinite Recharge autonomous that starts aligned "
16 "with the target");
Ravago Jones8c5737e2021-10-16 15:36:12 -070017DEFINE_bool(target_offset, false,
Ravago Jonesa7b3c822021-08-26 12:36:03 -070018 "If true, run the Infinite Recharge autonomous that starts offset "
19 "from the target");
20DEFINE_bool(just_shoot, false,
21 "If true, run the autonomous that just shoots balls.");
milind upadhyay47a0ab32020-11-25 19:34:41 -080022
Stephan Massaltd021f972020-01-05 20:41:23 -080023namespace y2020 {
24namespace actors {
25
26using ::aos::monotonic_clock;
27using ::frc971::ProfileParametersT;
28using frc971::control_loops::drivetrain::LocalizerControl;
29namespace chrono = ::std::chrono;
30
31AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
32 : frc971::autonomous::BaseAutonomousActor(
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080033 event_loop, control_loops::drivetrain::GetDrivetrainConfig()),
Ravago Jonesc2a08022021-02-06 17:40:54 -080034 localizer_control_sender_(
35 event_loop->MakeSender<
36 ::frc971::control_loops::drivetrain::LocalizerControl>(
37 "/drivetrain")),
Austin Schuh67e127e2021-03-27 13:25:23 -070038 superstructure_goal_sender_(
39 event_loop->MakeSender<control_loops::superstructure::Goal>(
40 "/superstructure")),
Ravago Jones1f32d622021-08-26 12:20:36 -070041 superstructure_status_fetcher_(
42 event_loop->MakeFetcher<y2020::control_loops::superstructure::Status>(
43 "/superstructure")),
Austin Schuhba77b7e2021-10-25 21:58:54 -070044 joystick_state_fetcher_(
45 event_loop->MakeFetcher<aos::JoystickState>("/aos")),
46 robot_state_fetcher_(event_loop->MakeFetcher<aos::RobotState>("/aos")),
Ravago Jonesc2a08022021-02-06 17:40:54 -080047 auto_splines_() {
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]() {
51 replan_timer_->Setup(event_loop->monotonic_now());
Austin Schuhba77b7e2021-10-25 21:58:54 -070052 button_poll_->Setup(event_loop->monotonic_now(), chrono::milliseconds(50));
James Kuszmaul99af8b52021-03-28 10:50:15 -070053 });
Austin Schuhba77b7e2021-10-25 21:58:54 -070054
55 button_poll_ = event_loop->AddTimer([this]() {
James Kuszmaulead4da62021-10-24 17:36:54 -070056 const aos::monotonic_clock::time_point now =
57 this->event_loop()->context().monotonic_event_time;
Austin Schuhba77b7e2021-10-25 21:58:54 -070058 if (robot_state_fetcher_.Fetch()) {
59 if (robot_state_fetcher_->user_button()) {
60 user_indicated_safe_to_reset_ = true;
61 MaybeSendStartingPosition();
62 }
James Kuszmaul4303a5d2021-10-23 19:58:48 -070063 }
Austin Schuhba77b7e2021-10-25 21:58:54 -070064 if (joystick_state_fetcher_.Fetch()) {
65 if (joystick_state_fetcher_->has_alliance() &&
66 (joystick_state_fetcher_->alliance() != alliance_)) {
67 alliance_ = joystick_state_fetcher_->alliance();
James Kuszmaulead4da62021-10-24 17:36:54 -070068 is_planned_ = false;
69 // Only kick the planning out by 2 seconds. If we end up enabled in that
70 // second, then we will kick it out further based on the code below.
71 replan_timer_->Setup(now + std::chrono::seconds(2));
72 }
73 if (joystick_state_fetcher_->enabled()) {
74 if (!is_planned_) {
75 // Only replan once we've been disabled for 5 seconds.
76 replan_timer_->Setup(now + std::chrono::seconds(5));
77 }
Austin Schuhba77b7e2021-10-25 21:58:54 -070078 }
James Kuszmaul4303a5d2021-10-23 19:58:48 -070079 }
80 });
81}
82
83void AutonomousActor::MaybeSendStartingPosition() {
James Kuszmaulead4da62021-10-24 17:36:54 -070084 if (is_planned_ && user_indicated_safe_to_reset_ &&
85 !sent_starting_position_) {
James Kuszmaul4303a5d2021-10-23 19:58:48 -070086 CHECK(starting_position_);
87 SendStartingPosition(starting_position_.value());
88 }
James Kuszmaul99af8b52021-03-28 10:50:15 -070089}
90
91void AutonomousActor::Replan() {
milind-u0e203782021-10-30 21:57:20 -070092 LOG(INFO) << "Alliance " << static_cast<int>(alliance_);
Austin Schuhd0e9e062021-10-24 17:40:58 -070093 if (alliance_ == aos::Alliance::kInvalid) {
94 return;
95 }
milind-u0e203782021-10-30 21:57:20 -070096 sent_starting_position_ = false;
Ravago Jones8c5737e2021-10-16 15:36:12 -070097 if (FLAGS_spline_auto) {
Tyler Chatowbf0609c2021-07-31 16:13:27 -070098 test_spline_ = PlanSpline(std::bind(&AutonomousSplines::TestSpline,
99 &auto_splines_, std::placeholders::_1),
100 SplineDirection::kForward);
James Kuszmaul4303a5d2021-10-23 19:58:48 -0700101 starting_position_ = test_spline_->starting_position();
Austin Schuhd0e9e062021-10-24 17:40:58 -0700102 } else if (FLAGS_target_aligned) {
103 target_aligned_splines_ = {
104 PlanSpline(std::bind(&AutonomousSplines::TargetAligned1, &auto_splines_,
105 std::placeholders::_1, alliance_),
106 SplineDirection::kForward),
107 PlanSpline(std::bind(&AutonomousSplines::TargetAligned2, &auto_splines_,
108 std::placeholders::_1, alliance_),
milind-u0e203782021-10-30 21:57:20 -0700109 SplineDirection::kBackward),
110 PlanSpline(std::bind(&AutonomousSplines::TargetAligned3, &auto_splines_,
111 std::placeholders::_1, alliance_),
112 SplineDirection::kForward)};
Austin Schuhd0e9e062021-10-24 17:40:58 -0700113 starting_position_ = target_aligned_splines_.value()[0].starting_position();
milind-u0e203782021-10-30 21:57:20 -0700114 CHECK(starting_position_);
Ravago Jones8c5737e2021-10-16 15:36:12 -0700115 } else if (FLAGS_target_offset) {
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700116 target_offset_splines_ = {
117 PlanSpline(std::bind(&AutonomousSplines::TargetOffset1, &auto_splines_,
118 std::placeholders::_1),
119 SplineDirection::kForward),
120 PlanSpline(std::bind(&AutonomousSplines::TargetOffset2, &auto_splines_,
121 std::placeholders::_1),
122 SplineDirection::kBackward)};
James Kuszmaul4303a5d2021-10-23 19:58:48 -0700123 starting_position_ = target_offset_splines_.value()[0].starting_position();
James Kuszmaul4303a5d2021-10-23 19:58:48 -0700124 } else {
125 starting_position_ = Eigen::Vector3d::Zero();
James Kuszmaul99af8b52021-03-28 10:50:15 -0700126 }
James Kuszmaulead4da62021-10-24 17:36:54 -0700127
128 is_planned_ = true;
129
James Kuszmaul4303a5d2021-10-23 19:58:48 -0700130 MaybeSendStartingPosition();
milind upadhyay47a0ab32020-11-25 19:34:41 -0800131}
Stephan Massaltd021f972020-01-05 20:41:23 -0800132
133void AutonomousActor::Reset() {
134 InitializeEncoders();
135 ResetDrivetrain();
milind upadhyayb2e840a2021-03-27 13:54:49 -0700136 RetractIntake();
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800137
James Kuszmaulddd2ba62020-03-08 22:17:13 -0700138 joystick_state_fetcher_.Fetch();
139 CHECK(joystick_state_fetcher_.get() != nullptr)
140 << "Expect at least one JoystickState message before running auto...";
141 alliance_ = joystick_state_fetcher_->alliance();
Stephan Massaltd021f972020-01-05 20:41:23 -0800142}
143
144bool AutonomousActor::RunAction(
Austin Schuh6fb0a6d2021-01-23 15:43:17 -0800145 const ::frc971::autonomous::AutonomousActionParams *params) {
Stephan Massaltd021f972020-01-05 20:41:23 -0800146 Reset();
James Kuszmaul4303a5d2021-10-23 19:58:48 -0700147 if (!user_indicated_safe_to_reset_) {
148 AOS_LOG(WARNING, "Didn't send starting position prior to starting auto.");
milind-u0e203782021-10-30 21:57:20 -0700149 CHECK(starting_position_);
James Kuszmaul4303a5d2021-10-23 19:58:48 -0700150 SendStartingPosition(starting_position_.value());
151 }
James Kuszmaulead4da62021-10-24 17:36:54 -0700152 // Clear this so that we don't accidentally resend things as soon as we replan
153 // later.
154 user_indicated_safe_to_reset_ = false;
155 is_planned_ = false;
156 starting_position_.reset();
James Kuszmaul99af8b52021-03-28 10:50:15 -0700157
milind upadhyay47a0ab32020-11-25 19:34:41 -0800158 AOS_LOG(INFO, "Params are %d\n", params->mode());
James Kuszmaulddd2ba62020-03-08 22:17:13 -0700159 if (alliance_ == aos::Alliance::kInvalid) {
160 AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection.");
161 return false;
162 }
Austin Schuhd0e9e062021-10-24 17:40:58 -0700163 if (FLAGS_spline_auto) {
164 SplineAuto();
165 } else if (FLAGS_target_aligned) {
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700166 TargetAligned();
Ravago Jones8c5737e2021-10-16 15:36:12 -0700167 } else if (FLAGS_target_offset) {
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700168 TargetOffset();
169 } else if (FLAGS_just_shoot) {
170 JustShoot();
milind upadhyay47a0ab32020-11-25 19:34:41 -0800171 } else {
172 return DriveFwd();
173 }
174 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
269void AutonomousActor::JustShoot() {
270 // shoot pre-loaded balls
271 set_shooter_tracking(true);
272 set_shooting(true);
273 SendSuperstructureGoal();
274
275 if (!WaitForBallsShot(3)) return;
276
277 set_shooting(false);
278 set_shooter_tracking(true);
279 SendSuperstructureGoal();
280}
281
282void AutonomousActor::TargetOffset() {
283 CHECK(target_offset_splines_);
284 auto &splines = *target_offset_splines_;
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700285
286 // spin up shooter
287 set_shooter_tracking(true);
288 SendSuperstructureGoal();
289 ExtendIntake();
290
291 // pickup 2 more balls in front of the trench run
292 if (!splines[0].WaitForPlan()) return;
293 splines[0].Start();
294
295 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
296 RetractIntake();
297
298 if (!splines[1].WaitForPlan()) return;
299 splines[1].Start();
300
301 if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return;
302
303 // shoot the balls from in front of the goal.
304 set_shooting(true);
305 SendSuperstructureGoal();
306
307 if (!WaitForBallsShot(5)) return;
308
309 set_shooting(false);
310 set_shooter_tracking(false);
311 SendSuperstructureGoal();
312}
313
milind upadhyay47a0ab32020-11-25 19:34:41 -0800314void AutonomousActor::SplineAuto() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700315 CHECK(test_spline_);
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800316
James Kuszmaul99af8b52021-03-28 10:50:15 -0700317 if (!test_spline_->WaitForPlan()) return;
318 test_spline_->Start();
Stephan Massaltd021f972020-01-05 20:41:23 -0800319
James Kuszmaul99af8b52021-03-28 10:50:15 -0700320 if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return;
kyle96c406e2021-02-27 14:07:22 -0800321}
322
milind upadhyay47a0ab32020-11-25 19:34:41 -0800323ProfileParametersT MakeProfileParametersT(const float max_velocity,
324 const float max_acceleration) {
325 ProfileParametersT params;
326 params.max_velocity = max_velocity;
327 params.max_acceleration = max_acceleration;
328 return params;
329}
330
331bool AutonomousActor::DriveFwd() {
Austin Schuhfd1715f2021-01-30 16:58:24 -0800332 const ProfileParametersT kDrive = MakeProfileParametersT(0.3f, 1.0f);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800333 const ProfileParametersT kTurn = MakeProfileParametersT(5.0f, 15.0f);
Austin Schuhfd1715f2021-01-30 16:58:24 -0800334 StartDrive(1.0, 0.0, kDrive, kTurn);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800335 return WaitForDriveDone();
336}
Sabina Leavera0b43b42021-03-03 20:30:04 -0800337
338void AutonomousActor::SendSuperstructureGoal() {
Sabina Leavera0b43b42021-03-03 20:30:04 -0800339 auto builder = superstructure_goal_sender_.MakeBuilder();
340
341 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
342 intake_offset;
milind upadhyayb9dec712021-03-20 15:47:51 -0700343
Sabina Leavera0b43b42021-03-03 20:30:04 -0800344 {
345 StaticZeroingSingleDOFProfiledSubsystemGoal::Builder intake_builder =
346 builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>();
347
milind upadhyayb9dec712021-03-20 15:47:51 -0700348 frc971::ProfileParameters::Builder profile_params_builder =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800349 builder.MakeBuilder<frc971::ProfileParameters>();
Austin Schuhb39a21c2021-03-31 20:12:18 -0700350 profile_params_builder.add_max_velocity(20.0);
351 profile_params_builder.add_max_acceleration(60.0);
milind upadhyayb9dec712021-03-20 15:47:51 -0700352 flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800353 profile_params_builder.Finish();
354 intake_builder.add_unsafe_goal(intake_goal_);
355 intake_builder.add_profile_params(profile_params_offset);
356 intake_offset = intake_builder.Finish();
357 }
358
359 superstructure::Goal::Builder superstructure_builder =
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700360 builder.MakeBuilder<superstructure::Goal>();
milind upadhyayb9dec712021-03-20 15:47:51 -0700361
Sabina Leavera0b43b42021-03-03 20:30:04 -0800362 superstructure_builder.add_intake(intake_offset);
milind-u0e203782021-10-30 21:57:20 -0700363 superstructure_builder.add_intake_preloading(preloading_);
Sabina Leavera0b43b42021-03-03 20:30:04 -0800364 superstructure_builder.add_roller_voltage(roller_voltage_);
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700365 superstructure_builder.add_roller_speed_compensation(
366 kRollerSpeedCompensation);
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700367 superstructure_builder.add_hood_tracking(shooter_tracking_);
368 superstructure_builder.add_turret_tracking(shooter_tracking_);
369 superstructure_builder.add_shooter_tracking(shooter_tracking_);
370 superstructure_builder.add_shooting(shooting_);
Sabina Leavera0b43b42021-03-03 20:30:04 -0800371
milind1f1dca32021-07-03 13:50:07 -0700372 if (builder.Send(superstructure_builder.Finish()) !=
373 aos::RawSender::Error::kOk) {
Sabina Leavera0b43b42021-03-03 20:30:04 -0800374 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
375 }
Sabina Leavera0b43b42021-03-03 20:30:04 -0800376}
milind upadhyay5e589d72021-03-27 13:47:18 -0700377
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700378void AutonomousActor::ExtendIntake() {
Austin Schuhd0e9e062021-10-24 17:40:58 -0700379 set_intake_goal(1.30);
380 set_roller_voltage(6.0);
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700381 SendSuperstructureGoal();
382}
383
milind upadhyay5e589d72021-03-27 13:47:18 -0700384void AutonomousActor::RetractIntake() {
385 set_intake_goal(-0.89);
Austin Schuhd0e9e062021-10-24 17:40:58 -0700386 set_roller_voltage(6.0);
milind upadhyay5e589d72021-03-27 13:47:18 -0700387 SendSuperstructureGoal();
388}
389
Austin Schuhd0e9e062021-10-24 17:40:58 -0700390int AutonomousActor::Balls() {
Ravago Jones1f32d622021-08-26 12:20:36 -0700391 superstructure_status_fetcher_.Fetch();
392 CHECK(superstructure_status_fetcher_.get() != nullptr);
Austin Schuhd0e9e062021-10-24 17:40:58 -0700393 return superstructure_status_fetcher_->shooter()->balls_shot();
394}
Ravago Jones1f32d622021-08-26 12:20:36 -0700395
Austin Schuhd0e9e062021-10-24 17:40:58 -0700396bool AutonomousActor::WaitUntilAbsoluteBallsShot(int absolute_balls) {
Ravago Jones1f32d622021-08-26 12:20:36 -0700397 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
398 event_loop()->monotonic_now(),
399 frc971::controls::kLoopFrequency / 2);
milind-u0e203782021-10-30 21:57:20 -0700400 superstructure_status_fetcher_.Fetch();
401 CHECK(superstructure_status_fetcher_.get() != nullptr);
402 int last_balls = superstructure_status_fetcher_->shooter()->balls_shot();
403 LOG(INFO) << "Waiting for balls, started with " << absolute_balls;
Ravago Jones1f32d622021-08-26 12:20:36 -0700404 while (true) {
405 if (ShouldCancel()) {
406 return false;
407 }
408 phased_loop.SleepUntilNext();
409 superstructure_status_fetcher_.Fetch();
Austin Schuhd0e9e062021-10-24 17:40:58 -0700410 CHECK(superstructure_status_fetcher_.get() != nullptr);
milind-u0e203782021-10-30 21:57:20 -0700411 if (superstructure_status_fetcher_->shooter()->balls_shot() != last_balls) {
412 LOG(INFO) << "Shot "
413 << superstructure_status_fetcher_->shooter()->balls_shot() -
414 last_balls
415 << " balls, now at "
416 << superstructure_status_fetcher_->shooter()->balls_shot();
417 }
Austin Schuhd0e9e062021-10-24 17:40:58 -0700418 if (superstructure_status_fetcher_->shooter()->balls_shot() >=
419 absolute_balls) {
Ravago Jones1f32d622021-08-26 12:20:36 -0700420 return true;
421 }
milind-u0e203782021-10-30 21:57:20 -0700422
423 last_balls = superstructure_status_fetcher_->shooter()->balls_shot();
Ravago Jones1f32d622021-08-26 12:20:36 -0700424 }
425}
426
Austin Schuhd0e9e062021-10-24 17:40:58 -0700427bool AutonomousActor::WaitForBallsShot(int num_wanted) {
428 return WaitUntilAbsoluteBallsShot(Balls() + num_wanted);
429}
430
Stephan Massaltd021f972020-01-05 20:41:23 -0800431} // namespace actors
432} // namespace y2020