blob: ae18d52baf80fce93adfb75d0a338905f5c17cee [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 Schuh3840ada2021-04-04 16:58:00 -070014DEFINE_bool(ignore_vision, false, "If true, ignore vision");
15DEFINE_bool(galactic_search, false,
kyle96c406e2021-02-27 14:07:22 -080016 "If true, do the galactic search autonomous");
Ravago Jones9c326f52021-03-20 15:00:16 -070017DEFINE_bool(bounce, false, "If true, run the AutoNav Bounce autonomous");
18DEFINE_bool(barrel, false, "If true, run the AutoNav Barrel autonomous");
Ravago Jonesa7b3c822021-08-26 12:36:03 -070019DEFINE_bool(slalom, false, "If true, run the AutoNav Slalom autonomous");
20DEFINE_bool(infinite_recharge_target_aligned, false,
21 "If true, run the Infinite Recharge autonomous that starts aligned "
22 "with the target");
23DEFINE_bool(infinite_recharge_target_offset, false,
24 "If true, run the Infinite Recharge autonomous that starts offset "
25 "from the target");
26DEFINE_bool(just_shoot, false,
27 "If true, run the autonomous that just shoots balls.");
milind upadhyay47a0ab32020-11-25 19:34:41 -080028
Stephan Massaltd021f972020-01-05 20:41:23 -080029namespace y2020 {
30namespace actors {
31
32using ::aos::monotonic_clock;
33using ::frc971::ProfileParametersT;
34using frc971::control_loops::drivetrain::LocalizerControl;
35namespace chrono = ::std::chrono;
36
37AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
38 : frc971::autonomous::BaseAutonomousActor(
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080039 event_loop, control_loops::drivetrain::GetDrivetrainConfig()),
Ravago Jonesc2a08022021-02-06 17:40:54 -080040 localizer_control_sender_(
41 event_loop->MakeSender<
42 ::frc971::control_loops::drivetrain::LocalizerControl>(
43 "/drivetrain")),
Austin Schuh67e127e2021-03-27 13:25:23 -070044 superstructure_goal_sender_(
45 event_loop->MakeSender<control_loops::superstructure::Goal>(
46 "/superstructure")),
James Kuszmaulddd2ba62020-03-08 22:17:13 -070047 joystick_state_fetcher_(
Ravago Jonesc2a08022021-02-06 17:40:54 -080048 event_loop->MakeFetcher<aos::JoystickState>("/aos")),
kyle96c406e2021-02-27 14:07:22 -080049 path_fetcher_(event_loop->MakeFetcher<y2020::vision::GalacticSearchPath>(
Austin Schuhc6442fc2021-03-27 13:25:42 -070050 "/pi2/camera")),
Ravago Jones1f32d622021-08-26 12:20:36 -070051 superstructure_status_fetcher_(
52 event_loop->MakeFetcher<y2020::control_loops::superstructure::Status>(
53 "/superstructure")),
Ravago Jonesc2a08022021-02-06 17:40:54 -080054 auto_splines_() {
milind upadhyay47a0ab32020-11-25 19:34:41 -080055 set_max_drivetrain_voltage(2.0);
James Kuszmaul99af8b52021-03-28 10:50:15 -070056 replan_timer_ = event_loop->AddTimer([this]() { Replan(); });
57 event_loop->OnRun([this, event_loop]() {
58 replan_timer_->Setup(event_loop->monotonic_now());
59 });
60}
61
62void AutonomousActor::Replan() {
63 if (FLAGS_galactic_search) {
64 galactic_search_splines_ = {
65 .red_a = PlanSpline(std::bind(&AutonomousSplines::SplineRedA,
66 &auto_splines_, std::placeholders::_1),
67 SplineDirection::kForward),
68 .red_b = PlanSpline(std::bind(&AutonomousSplines::SplineRedB,
69 &auto_splines_, std::placeholders::_1),
70 SplineDirection::kForward),
71 .blue_a = PlanSpline(std::bind(&AutonomousSplines::SplineBlueA,
72 &auto_splines_, std::placeholders::_1),
73 SplineDirection::kForward),
74 .blue_b = PlanSpline(std::bind(&AutonomousSplines::SplineBlueB,
75 &auto_splines_, std::placeholders::_1),
76 SplineDirection::kForward)};
77 } else if (FLAGS_bounce) {
78 bounce_splines_ = {
79 PlanSpline(std::bind(&AutonomousSplines::AutoNavBounce1, &auto_splines_,
80 std::placeholders::_1),
81 SplineDirection::kForward),
82 PlanSpline(std::bind(&AutonomousSplines::AutoNavBounce2, &auto_splines_,
83 std::placeholders::_1),
84 SplineDirection::kBackward),
85 PlanSpline(std::bind(&AutonomousSplines::AutoNavBounce3, &auto_splines_,
86 std::placeholders::_1),
87 SplineDirection::kForward),
88 PlanSpline(std::bind(&AutonomousSplines::AutoNavBounce4, &auto_splines_,
89 std::placeholders::_1),
90 SplineDirection::kBackward)};
91 } else if (FLAGS_barrel) {
92 barrel_spline_ =
93 PlanSpline(std::bind(&AutonomousSplines::AutoNavBarrel, &auto_splines_,
94 std::placeholders::_1),
95 SplineDirection::kForward);
96 } else if (FLAGS_slalom) {
97 slalom_spline_ =
98 PlanSpline(std::bind(&AutonomousSplines::AutoNavSlalom, &auto_splines_,
99 std::placeholders::_1),
100 SplineDirection::kForward);
101 } else if (FLAGS_spline_auto) {
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700102 test_spline_ = PlanSpline(std::bind(&AutonomousSplines::TestSpline,
103 &auto_splines_, std::placeholders::_1),
104 SplineDirection::kForward);
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700105 } else if (FLAGS_infinite_recharge_target_offset) {
106 target_offset_splines_ = {
107 PlanSpline(std::bind(&AutonomousSplines::TargetOffset1, &auto_splines_,
108 std::placeholders::_1),
109 SplineDirection::kForward),
110 PlanSpline(std::bind(&AutonomousSplines::TargetOffset2, &auto_splines_,
111 std::placeholders::_1),
112 SplineDirection::kBackward)};
113 } else if (FLAGS_infinite_recharge_target_aligned) {
114 target_aligned_splines_ = {
115 PlanSpline(std::bind(&AutonomousSplines::TargetAligned1, &auto_splines_,
116 std::placeholders::_1),
117 SplineDirection::kForward),
118 PlanSpline(std::bind(&AutonomousSplines::TargetAligned2, &auto_splines_,
119 std::placeholders::_1),
120 SplineDirection::kBackward)};
James Kuszmaul99af8b52021-03-28 10:50:15 -0700121 }
milind upadhyay47a0ab32020-11-25 19:34:41 -0800122}
Stephan Massaltd021f972020-01-05 20:41:23 -0800123
124void AutonomousActor::Reset() {
125 InitializeEncoders();
126 ResetDrivetrain();
milind upadhyayb2e840a2021-03-27 13:54:49 -0700127 RetractIntake();
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800128
James Kuszmaulddd2ba62020-03-08 22:17:13 -0700129 joystick_state_fetcher_.Fetch();
130 CHECK(joystick_state_fetcher_.get() != nullptr)
131 << "Expect at least one JoystickState message before running auto...";
132 alliance_ = joystick_state_fetcher_->alliance();
Stephan Massaltd021f972020-01-05 20:41:23 -0800133}
134
135bool AutonomousActor::RunAction(
Austin Schuh6fb0a6d2021-01-23 15:43:17 -0800136 const ::frc971::autonomous::AutonomousActionParams *params) {
Stephan Massaltd021f972020-01-05 20:41:23 -0800137 Reset();
James Kuszmaul99af8b52021-03-28 10:50:15 -0700138
139 // Queue up a replan to occur as soon as this action completes.
140 // TODO(james): Modify this so we don't replan during teleop.
141 replan_timer_->Setup(monotonic_now());
142
milind upadhyay47a0ab32020-11-25 19:34:41 -0800143 AOS_LOG(INFO, "Params are %d\n", params->mode());
James Kuszmaulddd2ba62020-03-08 22:17:13 -0700144 if (alliance_ == aos::Alliance::kInvalid) {
145 AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection.");
146 return false;
147 }
kyle96c406e2021-02-27 14:07:22 -0800148 if (FLAGS_galactic_search) {
149 GalacticSearch();
Ravago Jones9c326f52021-03-20 15:00:16 -0700150 } else if (FLAGS_bounce) {
151 AutoNavBounce();
152 } else if (FLAGS_barrel) {
153 AutoNavBarrel();
154 } else if (FLAGS_slalom) {
155 AutoNavSlalom();
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700156 } else if (FLAGS_infinite_recharge_target_aligned) {
157 TargetAligned();
158 } else if (FLAGS_infinite_recharge_target_offset) {
159 TargetOffset();
160 } else if (FLAGS_just_shoot) {
161 JustShoot();
kyle96c406e2021-02-27 14:07:22 -0800162 } else if (FLAGS_spline_auto) {
milind upadhyay47a0ab32020-11-25 19:34:41 -0800163 SplineAuto();
164 } else {
165 return DriveFwd();
166 }
167 return true;
168}
Stephan Massaltd021f972020-01-05 20:41:23 -0800169
James Kuszmaul99af8b52021-03-28 10:50:15 -0700170void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) {
kyle96c406e2021-02-27 14:07:22 -0800171 // Set up the starting position for the blue alliance.
kyle96c406e2021-02-27 14:07:22 -0800172
173 // TODO(james): Resetting the localizer breaks the left/right statespace
174 // controller. That is a bug, but we can fix that later by not resetting.
175 auto builder = localizer_control_sender_.MakeBuilder();
176
177 LocalizerControl::Builder localizer_control_builder =
178 builder.MakeBuilder<LocalizerControl>();
James Kuszmaul99af8b52021-03-28 10:50:15 -0700179 localizer_control_builder.add_x(start(0));
180 localizer_control_builder.add_y(start(1));
181 localizer_control_builder.add_theta(start(2));
kyle96c406e2021-02-27 14:07:22 -0800182 localizer_control_builder.add_theta_uncertainty(0.00001);
183 if (!builder.Send(localizer_control_builder.Finish())) {
184 AOS_LOG(ERROR, "Failed to reset localizer.\n");
185 }
186}
187
188void AutonomousActor::GalacticSearch() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700189 CHECK(galactic_search_splines_);
190
kyle96c406e2021-02-27 14:07:22 -0800191 path_fetcher_.Fetch();
Austin Schuh3fb9e422021-03-31 20:11:32 -0700192 SplineHandle *spline = nullptr;
193 if (path_fetcher_.get()) {
194 if (path_fetcher_->alliance() == y2020::vision::Alliance::kUnknown) {
195 AOS_LOG(ERROR, "The galactic search path is unknown, doing nothing.");
196 return;
197 }
James Kuszmaul99af8b52021-03-28 10:50:15 -0700198 if (path_fetcher_->alliance() == y2020::vision::Alliance::kRed) {
199 if (path_fetcher_->letter() == y2020::vision::Letter::kA) {
200 LOG(INFO) << "Red A";
201 spline = &galactic_search_splines_->red_a;
202 } else {
203 LOG(INFO) << "Red B";
204 CHECK(path_fetcher_->letter() == y2020::vision::Letter::kB);
205 spline = &galactic_search_splines_->red_b;
206 }
207 } else {
208 if (path_fetcher_->letter() == y2020::vision::Letter::kA) {
209 LOG(INFO) << "Blue A";
210 spline = &galactic_search_splines_->blue_a;
211 } else {
212 LOG(INFO) << "Blue B";
213 CHECK(path_fetcher_->letter() == y2020::vision::Letter::kB);
Austin Schuh8806bed2021-03-31 20:12:00 -0700214 spline = &galactic_search_splines_->blue_b;
James Kuszmaul99af8b52021-03-28 10:50:15 -0700215 }
216 }
kyle96c406e2021-02-27 14:07:22 -0800217 }
Austin Schuh3fb9e422021-03-31 20:11:32 -0700218 if (FLAGS_ignore_vision) {
219 LOG(INFO) << "Forcing Red B";
220 spline = &galactic_search_splines_->red_b;
221 }
222
223 CHECK(spline != nullptr)
224 << "Expect at least one GalacticSearchPath message before running "
225 "auto...";
226
227 SendStartingPosition(spline->starting_position());
228
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700229 ExtendIntake();
Austin Schuh3fb9e422021-03-31 20:11:32 -0700230
231 if (!spline->WaitForPlan()) return;
232 spline->Start();
233
234 if (!spline->WaitForSplineDistanceRemaining(0.02)) return;
235 RetractIntake();
kyle96c406e2021-02-27 14:07:22 -0800236}
237
Ravago Jones9c326f52021-03-20 15:00:16 -0700238void AutonomousActor::AutoNavBounce() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700239 CHECK(bounce_splines_);
Ravago Jones9c326f52021-03-20 15:00:16 -0700240
James Kuszmaul99af8b52021-03-28 10:50:15 -0700241 auto &splines = *bounce_splines_;
Ravago Jones9c326f52021-03-20 15:00:16 -0700242
James Kuszmaul99af8b52021-03-28 10:50:15 -0700243 SendStartingPosition(splines[0].starting_position());
Ravago Jones9c326f52021-03-20 15:00:16 -0700244
James Kuszmaul99af8b52021-03-28 10:50:15 -0700245 if (!splines[0].WaitForPlan()) return;
246 splines[0].Start();
Ravago Jones9c326f52021-03-20 15:00:16 -0700247
James Kuszmaul99af8b52021-03-28 10:50:15 -0700248 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700249
James Kuszmaul99af8b52021-03-28 10:50:15 -0700250 if (!splines[1].WaitForPlan()) return;
251 splines[1].Start();
Ravago Jones9c326f52021-03-20 15:00:16 -0700252
James Kuszmaul99af8b52021-03-28 10:50:15 -0700253 if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700254
James Kuszmaul99af8b52021-03-28 10:50:15 -0700255 if (!splines[2].WaitForPlan()) return;
256 splines[2].Start();
Ravago Jones9c326f52021-03-20 15:00:16 -0700257
James Kuszmaul99af8b52021-03-28 10:50:15 -0700258 if (!splines[2].WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700259
James Kuszmaul99af8b52021-03-28 10:50:15 -0700260 if (!splines[3].WaitForPlan()) return;
261 splines[3].Start();
Ravago Jones9c326f52021-03-20 15:00:16 -0700262
James Kuszmaul99af8b52021-03-28 10:50:15 -0700263 if (!splines[3].WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700264}
265
266void AutonomousActor::AutoNavBarrel() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700267 CHECK(barrel_spline_);
Ravago Jones9c326f52021-03-20 15:00:16 -0700268
James Kuszmaul99af8b52021-03-28 10:50:15 -0700269 SendStartingPosition(barrel_spline_->starting_position());
Ravago Jones9c326f52021-03-20 15:00:16 -0700270
James Kuszmaul99af8b52021-03-28 10:50:15 -0700271 if (!barrel_spline_->WaitForPlan()) return;
272 barrel_spline_->Start();
273
274 if (!barrel_spline_->WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700275}
276
277void AutonomousActor::AutoNavSlalom() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700278 CHECK(slalom_spline_);
Ravago Jones9c326f52021-03-20 15:00:16 -0700279
James Kuszmaul99af8b52021-03-28 10:50:15 -0700280 SendStartingPosition(slalom_spline_->starting_position());
Ravago Jones9c326f52021-03-20 15:00:16 -0700281
James Kuszmaul99af8b52021-03-28 10:50:15 -0700282 if (!slalom_spline_->WaitForPlan()) return;
283 slalom_spline_->Start();
284
285 if (!slalom_spline_->WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700286}
287
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700288void AutonomousActor::TargetAligned() {
289 CHECK(target_aligned_splines_);
290 auto &splines = *target_aligned_splines_;
291 SendStartingPosition(splines[0].starting_position());
292
293 // shoot pre-loaded balls
294 set_shooter_tracking(true);
295 set_shooting(true);
296 SendSuperstructureGoal();
297
298 if (!WaitForBallsShot(3)) return;
299
300 set_shooting(false);
301 SendSuperstructureGoal();
302
303 ExtendIntake();
304
305 // pickup 3 more balls
306 if (!splines[0].WaitForPlan()) return;
307 splines[0].Start();
308
309 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
310 RetractIntake();
311
312 if (!splines[1].WaitForPlan()) return;
313 splines[1].Start();
314
315 if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return;
316
317 // shoot the new balls in front of the goal.
318 set_shooting(true);
319 SendSuperstructureGoal();
320
321 if (!WaitForBallsShot(3)) return;
322
323 set_shooting(false);
324 set_shooter_tracking(false);
325 SendSuperstructureGoal();
326}
327
328void AutonomousActor::JustShoot() {
329 // shoot pre-loaded balls
330 set_shooter_tracking(true);
331 set_shooting(true);
332 SendSuperstructureGoal();
333
334 if (!WaitForBallsShot(3)) return;
335
336 set_shooting(false);
337 set_shooter_tracking(true);
338 SendSuperstructureGoal();
339}
340
341void AutonomousActor::TargetOffset() {
342 CHECK(target_offset_splines_);
343 auto &splines = *target_offset_splines_;
344 SendStartingPosition(splines[0].starting_position());
345
346 // spin up shooter
347 set_shooter_tracking(true);
348 SendSuperstructureGoal();
349 ExtendIntake();
350
351 // pickup 2 more balls in front of the trench run
352 if (!splines[0].WaitForPlan()) return;
353 splines[0].Start();
354
355 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
356 RetractIntake();
357
358 if (!splines[1].WaitForPlan()) return;
359 splines[1].Start();
360
361 if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return;
362
363 // shoot the balls from in front of the goal.
364 set_shooting(true);
365 SendSuperstructureGoal();
366
367 if (!WaitForBallsShot(5)) return;
368
369 set_shooting(false);
370 set_shooter_tracking(false);
371 SendSuperstructureGoal();
372}
373
milind upadhyay47a0ab32020-11-25 19:34:41 -0800374void AutonomousActor::SplineAuto() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700375 CHECK(test_spline_);
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800376
James Kuszmaul99af8b52021-03-28 10:50:15 -0700377 SendStartingPosition(test_spline_->starting_position());
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800378
James Kuszmaul99af8b52021-03-28 10:50:15 -0700379 if (!test_spline_->WaitForPlan()) return;
380 test_spline_->Start();
Stephan Massaltd021f972020-01-05 20:41:23 -0800381
James Kuszmaul99af8b52021-03-28 10:50:15 -0700382 if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return;
kyle96c406e2021-02-27 14:07:22 -0800383}
384
milind upadhyay47a0ab32020-11-25 19:34:41 -0800385ProfileParametersT MakeProfileParametersT(const float max_velocity,
386 const float max_acceleration) {
387 ProfileParametersT params;
388 params.max_velocity = max_velocity;
389 params.max_acceleration = max_acceleration;
390 return params;
391}
392
393bool AutonomousActor::DriveFwd() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700394 SendStartingPosition({0, 0, 0});
Austin Schuhfd1715f2021-01-30 16:58:24 -0800395 const ProfileParametersT kDrive = MakeProfileParametersT(0.3f, 1.0f);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800396 const ProfileParametersT kTurn = MakeProfileParametersT(5.0f, 15.0f);
Austin Schuhfd1715f2021-01-30 16:58:24 -0800397 StartDrive(1.0, 0.0, kDrive, kTurn);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800398 return WaitForDriveDone();
399}
Sabina Leavera0b43b42021-03-03 20:30:04 -0800400
401void AutonomousActor::SendSuperstructureGoal() {
Sabina Leavera0b43b42021-03-03 20:30:04 -0800402 auto builder = superstructure_goal_sender_.MakeBuilder();
403
404 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
405 intake_offset;
milind upadhyayb9dec712021-03-20 15:47:51 -0700406
Sabina Leavera0b43b42021-03-03 20:30:04 -0800407 {
408 StaticZeroingSingleDOFProfiledSubsystemGoal::Builder intake_builder =
409 builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>();
410
milind upadhyayb9dec712021-03-20 15:47:51 -0700411 frc971::ProfileParameters::Builder profile_params_builder =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800412 builder.MakeBuilder<frc971::ProfileParameters>();
Austin Schuhb39a21c2021-03-31 20:12:18 -0700413 profile_params_builder.add_max_velocity(20.0);
414 profile_params_builder.add_max_acceleration(60.0);
milind upadhyayb9dec712021-03-20 15:47:51 -0700415 flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800416 profile_params_builder.Finish();
417 intake_builder.add_unsafe_goal(intake_goal_);
418 intake_builder.add_profile_params(profile_params_offset);
419 intake_offset = intake_builder.Finish();
420 }
421
422 superstructure::Goal::Builder superstructure_builder =
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700423 builder.MakeBuilder<superstructure::Goal>();
milind upadhyayb9dec712021-03-20 15:47:51 -0700424
Sabina Leavera0b43b42021-03-03 20:30:04 -0800425 superstructure_builder.add_intake(intake_offset);
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700426 superstructure_builder.add_intake_preloading(intake_preloading_);
Sabina Leavera0b43b42021-03-03 20:30:04 -0800427 superstructure_builder.add_roller_voltage(roller_voltage_);
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700428 superstructure_builder.add_roller_speed_compensation(
429 kRollerSpeedCompensation);
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700430 superstructure_builder.add_hood_tracking(shooter_tracking_);
431 superstructure_builder.add_turret_tracking(shooter_tracking_);
432 superstructure_builder.add_shooter_tracking(shooter_tracking_);
433 superstructure_builder.add_shooting(shooting_);
Sabina Leavera0b43b42021-03-03 20:30:04 -0800434
435 if (!builder.Send(superstructure_builder.Finish())) {
436 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
437 }
Sabina Leavera0b43b42021-03-03 20:30:04 -0800438}
milind upadhyay5e589d72021-03-27 13:47:18 -0700439
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700440void AutonomousActor::ExtendIntake() {
441 set_intake_goal(1.25);
442 set_roller_voltage(12.0);
443 set_intake_preloading(true);
444 SendSuperstructureGoal();
445}
446
milind upadhyay5e589d72021-03-27 13:47:18 -0700447void AutonomousActor::RetractIntake() {
448 set_intake_goal(-0.89);
449 set_roller_voltage(0.0);
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700450 set_intake_preloading(false);
milind upadhyay5e589d72021-03-27 13:47:18 -0700451 SendSuperstructureGoal();
452}
453
Ravago Jones1f32d622021-08-26 12:20:36 -0700454bool AutonomousActor::WaitForBallsShot(int num_wanted) {
455 superstructure_status_fetcher_.Fetch();
456 CHECK(superstructure_status_fetcher_.get() != nullptr);
457 const int initial_balls_shot =
458 superstructure_status_fetcher_->shooter()->balls_shot();
459 int balls_shot = initial_balls_shot;
460
461 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
462 event_loop()->monotonic_now(),
463 frc971::controls::kLoopFrequency / 2);
464 while (true) {
465 if (ShouldCancel()) {
466 return false;
467 }
468 phased_loop.SleepUntilNext();
469 superstructure_status_fetcher_.Fetch();
470 balls_shot = superstructure_status_fetcher_->shooter()->balls_shot();
471 if ((balls_shot - initial_balls_shot) >= num_wanted) {
472 return true;
473 }
474 }
475}
476
Stephan Massaltd021f972020-01-05 20:41:23 -0800477} // namespace actors
478} // namespace y2020