blob: 30e16129195cb39aaa6bda4a8834c43ab3b2c498 [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");
Austin Schuh3840ada2021-04-04 16:58:00 -070019DEFINE_bool(slalom, true, "If true, run the AutoNav Slalom autonomous");
milind upadhyay47a0ab32020-11-25 19:34:41 -080020
Stephan Massaltd021f972020-01-05 20:41:23 -080021namespace y2020 {
22namespace actors {
23
24using ::aos::monotonic_clock;
25using ::frc971::ProfileParametersT;
26using frc971::control_loops::drivetrain::LocalizerControl;
27namespace chrono = ::std::chrono;
28
29AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
30 : frc971::autonomous::BaseAutonomousActor(
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080031 event_loop, control_loops::drivetrain::GetDrivetrainConfig()),
Ravago Jonesc2a08022021-02-06 17:40:54 -080032 localizer_control_sender_(
33 event_loop->MakeSender<
34 ::frc971::control_loops::drivetrain::LocalizerControl>(
35 "/drivetrain")),
Austin Schuh67e127e2021-03-27 13:25:23 -070036 superstructure_goal_sender_(
37 event_loop->MakeSender<control_loops::superstructure::Goal>(
38 "/superstructure")),
James Kuszmaulddd2ba62020-03-08 22:17:13 -070039 joystick_state_fetcher_(
Ravago Jonesc2a08022021-02-06 17:40:54 -080040 event_loop->MakeFetcher<aos::JoystickState>("/aos")),
kyle96c406e2021-02-27 14:07:22 -080041 path_fetcher_(event_loop->MakeFetcher<y2020::vision::GalacticSearchPath>(
Austin Schuhc6442fc2021-03-27 13:25:42 -070042 "/pi2/camera")),
Ravago Jones1f32d622021-08-26 12:20:36 -070043 superstructure_status_fetcher_(
44 event_loop->MakeFetcher<y2020::control_loops::superstructure::Status>(
45 "/superstructure")),
Ravago Jonesc2a08022021-02-06 17:40:54 -080046 auto_splines_() {
milind upadhyay47a0ab32020-11-25 19:34:41 -080047 set_max_drivetrain_voltage(2.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());
51 });
52}
53
54void AutonomousActor::Replan() {
55 if (FLAGS_galactic_search) {
56 galactic_search_splines_ = {
57 .red_a = PlanSpline(std::bind(&AutonomousSplines::SplineRedA,
58 &auto_splines_, std::placeholders::_1),
59 SplineDirection::kForward),
60 .red_b = PlanSpline(std::bind(&AutonomousSplines::SplineRedB,
61 &auto_splines_, std::placeholders::_1),
62 SplineDirection::kForward),
63 .blue_a = PlanSpline(std::bind(&AutonomousSplines::SplineBlueA,
64 &auto_splines_, std::placeholders::_1),
65 SplineDirection::kForward),
66 .blue_b = PlanSpline(std::bind(&AutonomousSplines::SplineBlueB,
67 &auto_splines_, std::placeholders::_1),
68 SplineDirection::kForward)};
69 } else if (FLAGS_bounce) {
70 bounce_splines_ = {
71 PlanSpline(std::bind(&AutonomousSplines::AutoNavBounce1, &auto_splines_,
72 std::placeholders::_1),
73 SplineDirection::kForward),
74 PlanSpline(std::bind(&AutonomousSplines::AutoNavBounce2, &auto_splines_,
75 std::placeholders::_1),
76 SplineDirection::kBackward),
77 PlanSpline(std::bind(&AutonomousSplines::AutoNavBounce3, &auto_splines_,
78 std::placeholders::_1),
79 SplineDirection::kForward),
80 PlanSpline(std::bind(&AutonomousSplines::AutoNavBounce4, &auto_splines_,
81 std::placeholders::_1),
82 SplineDirection::kBackward)};
83 } else if (FLAGS_barrel) {
84 barrel_spline_ =
85 PlanSpline(std::bind(&AutonomousSplines::AutoNavBarrel, &auto_splines_,
86 std::placeholders::_1),
87 SplineDirection::kForward);
88 } else if (FLAGS_slalom) {
89 slalom_spline_ =
90 PlanSpline(std::bind(&AutonomousSplines::AutoNavSlalom, &auto_splines_,
91 std::placeholders::_1),
92 SplineDirection::kForward);
93 } else if (FLAGS_spline_auto) {
Tyler Chatowbf0609c2021-07-31 16:13:27 -070094 test_spline_ = PlanSpline(std::bind(&AutonomousSplines::TestSpline,
95 &auto_splines_, std::placeholders::_1),
96 SplineDirection::kForward);
James Kuszmaul99af8b52021-03-28 10:50:15 -070097 }
milind upadhyay47a0ab32020-11-25 19:34:41 -080098}
Stephan Massaltd021f972020-01-05 20:41:23 -080099
100void AutonomousActor::Reset() {
101 InitializeEncoders();
102 ResetDrivetrain();
milind upadhyayb2e840a2021-03-27 13:54:49 -0700103 RetractIntake();
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800104
James Kuszmaulddd2ba62020-03-08 22:17:13 -0700105 joystick_state_fetcher_.Fetch();
106 CHECK(joystick_state_fetcher_.get() != nullptr)
107 << "Expect at least one JoystickState message before running auto...";
108 alliance_ = joystick_state_fetcher_->alliance();
Stephan Massaltd021f972020-01-05 20:41:23 -0800109}
110
111bool AutonomousActor::RunAction(
Austin Schuh6fb0a6d2021-01-23 15:43:17 -0800112 const ::frc971::autonomous::AutonomousActionParams *params) {
Stephan Massaltd021f972020-01-05 20:41:23 -0800113 Reset();
James Kuszmaul99af8b52021-03-28 10:50:15 -0700114
115 // Queue up a replan to occur as soon as this action completes.
116 // TODO(james): Modify this so we don't replan during teleop.
117 replan_timer_->Setup(monotonic_now());
118
milind upadhyay47a0ab32020-11-25 19:34:41 -0800119 AOS_LOG(INFO, "Params are %d\n", params->mode());
James Kuszmaulddd2ba62020-03-08 22:17:13 -0700120 if (alliance_ == aos::Alliance::kInvalid) {
121 AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection.");
122 return false;
123 }
kyle96c406e2021-02-27 14:07:22 -0800124 if (FLAGS_galactic_search) {
125 GalacticSearch();
Ravago Jones9c326f52021-03-20 15:00:16 -0700126 } else if (FLAGS_bounce) {
127 AutoNavBounce();
128 } else if (FLAGS_barrel) {
129 AutoNavBarrel();
130 } else if (FLAGS_slalom) {
131 AutoNavSlalom();
kyle96c406e2021-02-27 14:07:22 -0800132 } else if (FLAGS_spline_auto) {
milind upadhyay47a0ab32020-11-25 19:34:41 -0800133 SplineAuto();
134 } else {
135 return DriveFwd();
136 }
137 return true;
138}
Stephan Massaltd021f972020-01-05 20:41:23 -0800139
James Kuszmaul99af8b52021-03-28 10:50:15 -0700140void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) {
kyle96c406e2021-02-27 14:07:22 -0800141 // Set up the starting position for the blue alliance.
kyle96c406e2021-02-27 14:07:22 -0800142
143 // TODO(james): Resetting the localizer breaks the left/right statespace
144 // controller. That is a bug, but we can fix that later by not resetting.
145 auto builder = localizer_control_sender_.MakeBuilder();
146
147 LocalizerControl::Builder localizer_control_builder =
148 builder.MakeBuilder<LocalizerControl>();
James Kuszmaul99af8b52021-03-28 10:50:15 -0700149 localizer_control_builder.add_x(start(0));
150 localizer_control_builder.add_y(start(1));
151 localizer_control_builder.add_theta(start(2));
kyle96c406e2021-02-27 14:07:22 -0800152 localizer_control_builder.add_theta_uncertainty(0.00001);
153 if (!builder.Send(localizer_control_builder.Finish())) {
154 AOS_LOG(ERROR, "Failed to reset localizer.\n");
155 }
156}
157
158void AutonomousActor::GalacticSearch() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700159 CHECK(galactic_search_splines_);
160
kyle96c406e2021-02-27 14:07:22 -0800161 path_fetcher_.Fetch();
Austin Schuh3fb9e422021-03-31 20:11:32 -0700162 SplineHandle *spline = nullptr;
163 if (path_fetcher_.get()) {
164 if (path_fetcher_->alliance() == y2020::vision::Alliance::kUnknown) {
165 AOS_LOG(ERROR, "The galactic search path is unknown, doing nothing.");
166 return;
167 }
James Kuszmaul99af8b52021-03-28 10:50:15 -0700168 if (path_fetcher_->alliance() == y2020::vision::Alliance::kRed) {
169 if (path_fetcher_->letter() == y2020::vision::Letter::kA) {
170 LOG(INFO) << "Red A";
171 spline = &galactic_search_splines_->red_a;
172 } else {
173 LOG(INFO) << "Red B";
174 CHECK(path_fetcher_->letter() == y2020::vision::Letter::kB);
175 spline = &galactic_search_splines_->red_b;
176 }
177 } else {
178 if (path_fetcher_->letter() == y2020::vision::Letter::kA) {
179 LOG(INFO) << "Blue A";
180 spline = &galactic_search_splines_->blue_a;
181 } else {
182 LOG(INFO) << "Blue B";
183 CHECK(path_fetcher_->letter() == y2020::vision::Letter::kB);
Austin Schuh8806bed2021-03-31 20:12:00 -0700184 spline = &galactic_search_splines_->blue_b;
James Kuszmaul99af8b52021-03-28 10:50:15 -0700185 }
186 }
kyle96c406e2021-02-27 14:07:22 -0800187 }
Austin Schuh3fb9e422021-03-31 20:11:32 -0700188 if (FLAGS_ignore_vision) {
189 LOG(INFO) << "Forcing Red B";
190 spline = &galactic_search_splines_->red_b;
191 }
192
193 CHECK(spline != nullptr)
194 << "Expect at least one GalacticSearchPath message before running "
195 "auto...";
196
197 SendStartingPosition(spline->starting_position());
198
199 set_intake_goal(1.25);
200 set_roller_voltage(12.0);
201 SendSuperstructureGoal();
202
203 if (!spline->WaitForPlan()) return;
204 spline->Start();
205
206 if (!spline->WaitForSplineDistanceRemaining(0.02)) return;
207 RetractIntake();
kyle96c406e2021-02-27 14:07:22 -0800208}
209
Ravago Jones9c326f52021-03-20 15:00:16 -0700210void AutonomousActor::AutoNavBounce() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700211 CHECK(bounce_splines_);
Ravago Jones9c326f52021-03-20 15:00:16 -0700212
James Kuszmaul99af8b52021-03-28 10:50:15 -0700213 auto &splines = *bounce_splines_;
Ravago Jones9c326f52021-03-20 15:00:16 -0700214
James Kuszmaul99af8b52021-03-28 10:50:15 -0700215 SendStartingPosition(splines[0].starting_position());
Ravago Jones9c326f52021-03-20 15:00:16 -0700216
James Kuszmaul99af8b52021-03-28 10:50:15 -0700217 if (!splines[0].WaitForPlan()) return;
218 splines[0].Start();
Ravago Jones9c326f52021-03-20 15:00:16 -0700219
James Kuszmaul99af8b52021-03-28 10:50:15 -0700220 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700221
James Kuszmaul99af8b52021-03-28 10:50:15 -0700222 if (!splines[1].WaitForPlan()) return;
223 splines[1].Start();
Ravago Jones9c326f52021-03-20 15:00:16 -0700224
James Kuszmaul99af8b52021-03-28 10:50:15 -0700225 if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700226
James Kuszmaul99af8b52021-03-28 10:50:15 -0700227 if (!splines[2].WaitForPlan()) return;
228 splines[2].Start();
Ravago Jones9c326f52021-03-20 15:00:16 -0700229
James Kuszmaul99af8b52021-03-28 10:50:15 -0700230 if (!splines[2].WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700231
James Kuszmaul99af8b52021-03-28 10:50:15 -0700232 if (!splines[3].WaitForPlan()) return;
233 splines[3].Start();
Ravago Jones9c326f52021-03-20 15:00:16 -0700234
James Kuszmaul99af8b52021-03-28 10:50:15 -0700235 if (!splines[3].WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700236}
237
238void AutonomousActor::AutoNavBarrel() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700239 CHECK(barrel_spline_);
Ravago Jones9c326f52021-03-20 15:00:16 -0700240
James Kuszmaul99af8b52021-03-28 10:50:15 -0700241 SendStartingPosition(barrel_spline_->starting_position());
Ravago Jones9c326f52021-03-20 15:00:16 -0700242
James Kuszmaul99af8b52021-03-28 10:50:15 -0700243 if (!barrel_spline_->WaitForPlan()) return;
244 barrel_spline_->Start();
245
246 if (!barrel_spline_->WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700247}
248
249void AutonomousActor::AutoNavSlalom() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700250 CHECK(slalom_spline_);
Ravago Jones9c326f52021-03-20 15:00:16 -0700251
James Kuszmaul99af8b52021-03-28 10:50:15 -0700252 SendStartingPosition(slalom_spline_->starting_position());
Ravago Jones9c326f52021-03-20 15:00:16 -0700253
James Kuszmaul99af8b52021-03-28 10:50:15 -0700254 if (!slalom_spline_->WaitForPlan()) return;
255 slalom_spline_->Start();
256
257 if (!slalom_spline_->WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700258}
259
milind upadhyay47a0ab32020-11-25 19:34:41 -0800260void AutonomousActor::SplineAuto() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700261 CHECK(test_spline_);
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800262
James Kuszmaul99af8b52021-03-28 10:50:15 -0700263 SendStartingPosition(test_spline_->starting_position());
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800264
James Kuszmaul99af8b52021-03-28 10:50:15 -0700265 if (!test_spline_->WaitForPlan()) return;
266 test_spline_->Start();
Stephan Massaltd021f972020-01-05 20:41:23 -0800267
James Kuszmaul99af8b52021-03-28 10:50:15 -0700268 if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return;
kyle96c406e2021-02-27 14:07:22 -0800269}
270
milind upadhyay47a0ab32020-11-25 19:34:41 -0800271ProfileParametersT MakeProfileParametersT(const float max_velocity,
272 const float max_acceleration) {
273 ProfileParametersT params;
274 params.max_velocity = max_velocity;
275 params.max_acceleration = max_acceleration;
276 return params;
277}
278
279bool AutonomousActor::DriveFwd() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700280 SendStartingPosition({0, 0, 0});
Austin Schuhfd1715f2021-01-30 16:58:24 -0800281 const ProfileParametersT kDrive = MakeProfileParametersT(0.3f, 1.0f);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800282 const ProfileParametersT kTurn = MakeProfileParametersT(5.0f, 15.0f);
Austin Schuhfd1715f2021-01-30 16:58:24 -0800283 StartDrive(1.0, 0.0, kDrive, kTurn);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800284 return WaitForDriveDone();
285}
Sabina Leavera0b43b42021-03-03 20:30:04 -0800286
287void AutonomousActor::SendSuperstructureGoal() {
Sabina Leavera0b43b42021-03-03 20:30:04 -0800288 auto builder = superstructure_goal_sender_.MakeBuilder();
289
290 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
291 intake_offset;
milind upadhyayb9dec712021-03-20 15:47:51 -0700292
Sabina Leavera0b43b42021-03-03 20:30:04 -0800293 {
294 StaticZeroingSingleDOFProfiledSubsystemGoal::Builder intake_builder =
295 builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>();
296
milind upadhyayb9dec712021-03-20 15:47:51 -0700297 frc971::ProfileParameters::Builder profile_params_builder =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800298 builder.MakeBuilder<frc971::ProfileParameters>();
Austin Schuhb39a21c2021-03-31 20:12:18 -0700299 profile_params_builder.add_max_velocity(20.0);
300 profile_params_builder.add_max_acceleration(60.0);
milind upadhyayb9dec712021-03-20 15:47:51 -0700301 flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800302 profile_params_builder.Finish();
303 intake_builder.add_unsafe_goal(intake_goal_);
304 intake_builder.add_profile_params(profile_params_offset);
305 intake_offset = intake_builder.Finish();
306 }
307
308 superstructure::Goal::Builder superstructure_builder =
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700309 builder.MakeBuilder<superstructure::Goal>();
milind upadhyayb9dec712021-03-20 15:47:51 -0700310
Sabina Leavera0b43b42021-03-03 20:30:04 -0800311 superstructure_builder.add_intake(intake_offset);
312 superstructure_builder.add_roller_voltage(roller_voltage_);
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700313 superstructure_builder.add_roller_speed_compensation(
314 kRollerSpeedCompensation);
Sabina Leavera0b43b42021-03-03 20:30:04 -0800315
316 if (!builder.Send(superstructure_builder.Finish())) {
317 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
318 }
Sabina Leavera0b43b42021-03-03 20:30:04 -0800319}
milind upadhyay5e589d72021-03-27 13:47:18 -0700320
321void AutonomousActor::RetractIntake() {
322 set_intake_goal(-0.89);
323 set_roller_voltage(0.0);
324 SendSuperstructureGoal();
325}
326
Ravago Jones1f32d622021-08-26 12:20:36 -0700327bool AutonomousActor::WaitForBallsShot(int num_wanted) {
328 superstructure_status_fetcher_.Fetch();
329 CHECK(superstructure_status_fetcher_.get() != nullptr);
330 const int initial_balls_shot =
331 superstructure_status_fetcher_->shooter()->balls_shot();
332 int balls_shot = initial_balls_shot;
333
334 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
335 event_loop()->monotonic_now(),
336 frc971::controls::kLoopFrequency / 2);
337 while (true) {
338 if (ShouldCancel()) {
339 return false;
340 }
341 phased_loop.SleepUntilNext();
342 superstructure_status_fetcher_.Fetch();
343 balls_shot = superstructure_status_fetcher_->shooter()->balls_shot();
344 if ((balls_shot - initial_balls_shot) >= num_wanted) {
345 return true;
346 }
347 }
348}
349
Stephan Massaltd021f972020-01-05 20:41:23 -0800350} // namespace actors
351} // namespace y2020