blob: 7aa649ab807e426a7f1418f82b0decb3da027c39 [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 Massaltd021f972020-01-05 20:41:23 -080019namespace y2020 {
20namespace actors {
21
22using ::aos::monotonic_clock;
23using ::frc971::ProfileParametersT;
24using frc971::control_loops::drivetrain::LocalizerControl;
25namespace chrono = ::std::chrono;
26
27AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
28 : frc971::autonomous::BaseAutonomousActor(
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080029 event_loop, control_loops::drivetrain::GetDrivetrainConfig()),
Ravago Jonesc2a08022021-02-06 17:40:54 -080030 localizer_control_sender_(
31 event_loop->MakeSender<
32 ::frc971::control_loops::drivetrain::LocalizerControl>(
33 "/drivetrain")),
Austin Schuh67e127e2021-03-27 13:25:23 -070034 superstructure_goal_sender_(
35 event_loop->MakeSender<control_loops::superstructure::Goal>(
36 "/superstructure")),
Ravago Jones1f32d622021-08-26 12:20:36 -070037 superstructure_status_fetcher_(
38 event_loop->MakeFetcher<y2020::control_loops::superstructure::Status>(
39 "/superstructure")),
Austin Schuhba77b7e2021-10-25 21:58:54 -070040 joystick_state_fetcher_(
41 event_loop->MakeFetcher<aos::JoystickState>("/aos")),
42 robot_state_fetcher_(event_loop->MakeFetcher<aos::RobotState>("/aos")),
Ravago Jonesc2a08022021-02-06 17:40:54 -080043 auto_splines_() {
Austin Schuh3653cf22021-11-12 11:54:51 -080044 practice_robot_ =
45 ::aos::network::GetTeamNumber() == constants::Values::kPracticeTeamNumber;
46
Austin Schuhd0e9e062021-10-24 17:40:58 -070047 set_max_drivetrain_voltage(12.0);
James Kuszmaul99af8b52021-03-28 10:50:15 -070048 replan_timer_ = event_loop->AddTimer([this]() { Replan(); });
49 event_loop->OnRun([this, event_loop]() {
50 replan_timer_->Setup(event_loop->monotonic_now());
Austin Schuhba77b7e2021-10-25 21:58:54 -070051 button_poll_->Setup(event_loop->monotonic_now(), 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.
70 replan_timer_->Setup(now + std::chrono::seconds(2));
71 }
72 if (joystick_state_fetcher_->enabled()) {
73 if (!is_planned_) {
74 // Only replan once we've been disabled for 5 seconds.
75 replan_timer_->Setup(now + std::chrono::seconds(5));
76 }
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);
kyle96c406e2021-02-27 14:07:22 -0800192 if (!builder.Send(localizer_control_builder.Finish())) {
193 AOS_LOG(ERROR, "Failed to reset localizer.\n");
194 }
195}
196
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700197void AutonomousActor::TargetAligned() {
Austin Schuhd0e9e062021-10-24 17:40:58 -0700198 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700199 CHECK(target_aligned_splines_);
200 auto &splines = *target_aligned_splines_;
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700201
Austin Schuhd0e9e062021-10-24 17:40:58 -0700202 // Spin up.
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700203 set_shooting(true);
milind-u0e203782021-10-30 21:57:20 -0700204 set_preloading(true);
205 set_shooter_tracking(true);
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700206 SendSuperstructureGoal();
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700207 if (!WaitForBallsShot(3)) return;
milind-u0e203782021-10-30 21:57:20 -0700208 LOG(INFO) << "Shot balls";
209 set_shooter_tracking(false);
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700210
milind-u0e203782021-10-30 21:57:20 -0700211 // Drive and intake 3 balls in front of the trench run
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700212 set_shooting(false);
Austin Schuhd0e9e062021-10-24 17:40:58 -0700213 ExtendIntake();
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700214 SendSuperstructureGoal();
215
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700216 if (!splines[0].WaitForPlan()) return;
217 splines[0].Start();
218
219 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
milind-u0e203782021-10-30 21:57:20 -0700220
Austin Schuhd0e9e062021-10-24 17:40:58 -0700221 std::this_thread::sleep_for(chrono::milliseconds(200));
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700222 RetractIntake();
223
milind-u0e203782021-10-30 21:57:20 -0700224 // Drive back to shooting position
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700225 if (!splines[1].WaitForPlan()) return;
226 splines[1].Start();
227
Austin Schuhd0e9e062021-10-24 17:40:58 -0700228 if (!splines[1].WaitForSplineDistanceRemaining(2.0)) return;
229 // Reverse the rollers for a moment to try to unjam any jammed balls. Since
230 // we are moving here, this is free to try.
231 set_roller_voltage(-12.0);
232 std::this_thread::sleep_for(chrono::milliseconds(300));
233 set_roller_voltage(0.0);
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700234
Austin Schuhd0e9e062021-10-24 17:40:58 -0700235 // Once we come to a stop, give the robot a moment to settle down. This makes
236 // the shot more accurate.
237 if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return;
milind-u0e203782021-10-30 21:57:20 -0700238 set_shooter_tracking(true);
Austin Schuhd0e9e062021-10-24 17:40:58 -0700239 std::this_thread::sleep_for(chrono::milliseconds(1500));
240 set_shooting(true);
241 const int balls = Balls();
242
243 SendSuperstructureGoal();
244
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700245 SendSuperstructureGoal();
246
Austin Schuhd0e9e062021-10-24 17:40:58 -0700247 if (!WaitUntilAbsoluteBallsShot(3 + balls)) return;
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700248
milind-u0e203782021-10-30 21:57:20 -0700249 set_shooting(false);
250 set_roller_voltage(0.0);
251 set_shooter_tracking(false);
252 set_preloading(false);
253 SendSuperstructureGoal();
254
255 // Drive close to the rendezvous point in the center of the field so that the
256 // driver can intake balls there right after auto ends.
257 if (!splines[2].WaitForPlan()) return;
258 splines[2].Start();
259
260 if (!splines[2].WaitForSplineDistanceRemaining(0.02)) return;
261
Austin Schuhd0e9e062021-10-24 17:40:58 -0700262 LOG(INFO) << "Took "
263 << chrono::duration<double>(aos::monotonic_clock::now() -
264 start_time)
265 .count();
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700266}
267
Austin Schuh3653cf22021-11-12 11:54:51 -0800268void AutonomousActor::Fender() {
269 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
270 CHECK(fender_splines_);
271 auto &splines = *fender_splines_;
272
273 // Spin up.
274 set_shooting(false);
275 set_preloading(true);
276 set_shooter_tracking(false);
277 SendSuperstructureGoal();
278
279 if (!splines[0].WaitForPlan()) return;
280 splines[0].Start();
281
282 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
283
284 SendSuperstructureGoal();
285 std::this_thread::sleep_for(chrono::milliseconds(500));
286 ApplyThrottle(0.2);
287 set_shooting(true);
288 SendSuperstructureGoal();
289 LOG(INFO) << "Took "
290 << chrono::duration<double>(aos::monotonic_clock::now() -
291 start_time)
292 .count();
293}
294
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700295void AutonomousActor::JustShoot() {
296 // shoot pre-loaded balls
297 set_shooter_tracking(true);
298 set_shooting(true);
299 SendSuperstructureGoal();
300
301 if (!WaitForBallsShot(3)) return;
302
303 set_shooting(false);
304 set_shooter_tracking(true);
305 SendSuperstructureGoal();
306}
307
308void AutonomousActor::TargetOffset() {
309 CHECK(target_offset_splines_);
310 auto &splines = *target_offset_splines_;
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700311
312 // spin up shooter
313 set_shooter_tracking(true);
314 SendSuperstructureGoal();
315 ExtendIntake();
316
317 // pickup 2 more balls in front of the trench run
318 if (!splines[0].WaitForPlan()) return;
319 splines[0].Start();
320
321 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
322 RetractIntake();
323
324 if (!splines[1].WaitForPlan()) return;
325 splines[1].Start();
326
327 if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return;
328
329 // shoot the balls from in front of the goal.
330 set_shooting(true);
331 SendSuperstructureGoal();
332
333 if (!WaitForBallsShot(5)) return;
334
335 set_shooting(false);
336 set_shooter_tracking(false);
337 SendSuperstructureGoal();
338}
339
milind upadhyay47a0ab32020-11-25 19:34:41 -0800340void AutonomousActor::SplineAuto() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700341 CHECK(test_spline_);
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800342
James Kuszmaul99af8b52021-03-28 10:50:15 -0700343 if (!test_spline_->WaitForPlan()) return;
344 test_spline_->Start();
Stephan Massaltd021f972020-01-05 20:41:23 -0800345
James Kuszmaul99af8b52021-03-28 10:50:15 -0700346 if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return;
kyle96c406e2021-02-27 14:07:22 -0800347}
348
milind upadhyay47a0ab32020-11-25 19:34:41 -0800349ProfileParametersT MakeProfileParametersT(const float max_velocity,
350 const float max_acceleration) {
351 ProfileParametersT params;
352 params.max_velocity = max_velocity;
353 params.max_acceleration = max_acceleration;
354 return params;
355}
356
357bool AutonomousActor::DriveFwd() {
Austin Schuhfd1715f2021-01-30 16:58:24 -0800358 const ProfileParametersT kDrive = MakeProfileParametersT(0.3f, 1.0f);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800359 const ProfileParametersT kTurn = MakeProfileParametersT(5.0f, 15.0f);
Austin Schuhfd1715f2021-01-30 16:58:24 -0800360 StartDrive(1.0, 0.0, kDrive, kTurn);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800361 return WaitForDriveDone();
362}
Sabina Leavera0b43b42021-03-03 20:30:04 -0800363
364void AutonomousActor::SendSuperstructureGoal() {
Sabina Leavera0b43b42021-03-03 20:30:04 -0800365 auto builder = superstructure_goal_sender_.MakeBuilder();
366
367 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
368 intake_offset;
milind upadhyayb9dec712021-03-20 15:47:51 -0700369
Sabina Leavera0b43b42021-03-03 20:30:04 -0800370 {
371 StaticZeroingSingleDOFProfiledSubsystemGoal::Builder intake_builder =
372 builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>();
373
milind upadhyayb9dec712021-03-20 15:47:51 -0700374 frc971::ProfileParameters::Builder profile_params_builder =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800375 builder.MakeBuilder<frc971::ProfileParameters>();
Austin Schuhb39a21c2021-03-31 20:12:18 -0700376 profile_params_builder.add_max_velocity(20.0);
377 profile_params_builder.add_max_acceleration(60.0);
milind upadhyayb9dec712021-03-20 15:47:51 -0700378 flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800379 profile_params_builder.Finish();
380 intake_builder.add_unsafe_goal(intake_goal_);
381 intake_builder.add_profile_params(profile_params_offset);
382 intake_offset = intake_builder.Finish();
383 }
384
Austin Schuh3653cf22021-11-12 11:54:51 -0800385 flatbuffers::Offset<superstructure::ShooterGoal> shooter_offset =
386 superstructure::CreateShooterGoal(*builder.fbb(), 400.0, 200.0);
387
Sabina Leavera0b43b42021-03-03 20:30:04 -0800388 superstructure::Goal::Builder superstructure_builder =
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700389 builder.MakeBuilder<superstructure::Goal>();
milind upadhyayb9dec712021-03-20 15:47:51 -0700390
Sabina Leavera0b43b42021-03-03 20:30:04 -0800391 superstructure_builder.add_intake(intake_offset);
milind-u0e203782021-10-30 21:57:20 -0700392 superstructure_builder.add_intake_preloading(preloading_);
Austin Schuh3653cf22021-11-12 11:54:51 -0800393 if (!shooter_tracking_ && shooting_) {
394 superstructure_builder.add_shooter(shooter_offset);
395 }
Sabina Leavera0b43b42021-03-03 20:30:04 -0800396 superstructure_builder.add_roller_voltage(roller_voltage_);
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700397 superstructure_builder.add_roller_speed_compensation(
398 kRollerSpeedCompensation);
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700399 superstructure_builder.add_hood_tracking(shooter_tracking_);
400 superstructure_builder.add_turret_tracking(shooter_tracking_);
401 superstructure_builder.add_shooter_tracking(shooter_tracking_);
402 superstructure_builder.add_shooting(shooting_);
Sabina Leavera0b43b42021-03-03 20:30:04 -0800403
404 if (!builder.Send(superstructure_builder.Finish())) {
405 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
406 }
Sabina Leavera0b43b42021-03-03 20:30:04 -0800407}
milind upadhyay5e589d72021-03-27 13:47:18 -0700408
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700409void AutonomousActor::ExtendIntake() {
Austin Schuhd0e9e062021-10-24 17:40:58 -0700410 set_intake_goal(1.30);
411 set_roller_voltage(6.0);
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700412 SendSuperstructureGoal();
413}
414
milind upadhyay5e589d72021-03-27 13:47:18 -0700415void AutonomousActor::RetractIntake() {
416 set_intake_goal(-0.89);
Austin Schuhd0e9e062021-10-24 17:40:58 -0700417 set_roller_voltage(6.0);
milind upadhyay5e589d72021-03-27 13:47:18 -0700418 SendSuperstructureGoal();
419}
420
Austin Schuhd0e9e062021-10-24 17:40:58 -0700421int AutonomousActor::Balls() {
Ravago Jones1f32d622021-08-26 12:20:36 -0700422 superstructure_status_fetcher_.Fetch();
423 CHECK(superstructure_status_fetcher_.get() != nullptr);
Austin Schuhd0e9e062021-10-24 17:40:58 -0700424 return superstructure_status_fetcher_->shooter()->balls_shot();
425}
Ravago Jones1f32d622021-08-26 12:20:36 -0700426
Austin Schuhd0e9e062021-10-24 17:40:58 -0700427bool AutonomousActor::WaitUntilAbsoluteBallsShot(int absolute_balls) {
Ravago Jones1f32d622021-08-26 12:20:36 -0700428 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
429 event_loop()->monotonic_now(),
430 frc971::controls::kLoopFrequency / 2);
milind-u0e203782021-10-30 21:57:20 -0700431 superstructure_status_fetcher_.Fetch();
432 CHECK(superstructure_status_fetcher_.get() != nullptr);
433 int last_balls = superstructure_status_fetcher_->shooter()->balls_shot();
434 LOG(INFO) << "Waiting for balls, started with " << absolute_balls;
Ravago Jones1f32d622021-08-26 12:20:36 -0700435 while (true) {
436 if (ShouldCancel()) {
437 return false;
438 }
439 phased_loop.SleepUntilNext();
440 superstructure_status_fetcher_.Fetch();
Austin Schuhd0e9e062021-10-24 17:40:58 -0700441 CHECK(superstructure_status_fetcher_.get() != nullptr);
milind-u0e203782021-10-30 21:57:20 -0700442 if (superstructure_status_fetcher_->shooter()->balls_shot() != last_balls) {
443 LOG(INFO) << "Shot "
444 << superstructure_status_fetcher_->shooter()->balls_shot() -
445 last_balls
446 << " balls, now at "
447 << superstructure_status_fetcher_->shooter()->balls_shot();
448 }
Austin Schuhd0e9e062021-10-24 17:40:58 -0700449 if (superstructure_status_fetcher_->shooter()->balls_shot() >=
450 absolute_balls) {
Ravago Jones1f32d622021-08-26 12:20:36 -0700451 return true;
452 }
milind-u0e203782021-10-30 21:57:20 -0700453
454 last_balls = superstructure_status_fetcher_->shooter()->balls_shot();
Ravago Jones1f32d622021-08-26 12:20:36 -0700455 }
456}
457
Austin Schuhd0e9e062021-10-24 17:40:58 -0700458bool AutonomousActor::WaitForBallsShot(int num_wanted) {
459 return WaitUntilAbsoluteBallsShot(Balls() + num_wanted);
460}
461
Stephan Massaltd021f972020-01-05 20:41:23 -0800462} // namespace actors
463} // namespace y2020