blob: c504fb4560aa25b3615eb23898f0243220f9bd99 [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 Jonesc2a08022021-02-06 17:40:54 -080043 auto_splines_() {
milind upadhyay47a0ab32020-11-25 19:34:41 -080044 set_max_drivetrain_voltage(2.0);
James Kuszmaul99af8b52021-03-28 10:50:15 -070045 replan_timer_ = event_loop->AddTimer([this]() { Replan(); });
46 event_loop->OnRun([this, event_loop]() {
47 replan_timer_->Setup(event_loop->monotonic_now());
48 });
49}
50
51void AutonomousActor::Replan() {
52 if (FLAGS_galactic_search) {
53 galactic_search_splines_ = {
54 .red_a = PlanSpline(std::bind(&AutonomousSplines::SplineRedA,
55 &auto_splines_, std::placeholders::_1),
56 SplineDirection::kForward),
57 .red_b = PlanSpline(std::bind(&AutonomousSplines::SplineRedB,
58 &auto_splines_, std::placeholders::_1),
59 SplineDirection::kForward),
60 .blue_a = PlanSpline(std::bind(&AutonomousSplines::SplineBlueA,
61 &auto_splines_, std::placeholders::_1),
62 SplineDirection::kForward),
63 .blue_b = PlanSpline(std::bind(&AutonomousSplines::SplineBlueB,
64 &auto_splines_, std::placeholders::_1),
65 SplineDirection::kForward)};
66 } else if (FLAGS_bounce) {
67 bounce_splines_ = {
68 PlanSpline(std::bind(&AutonomousSplines::AutoNavBounce1, &auto_splines_,
69 std::placeholders::_1),
70 SplineDirection::kForward),
71 PlanSpline(std::bind(&AutonomousSplines::AutoNavBounce2, &auto_splines_,
72 std::placeholders::_1),
73 SplineDirection::kBackward),
74 PlanSpline(std::bind(&AutonomousSplines::AutoNavBounce3, &auto_splines_,
75 std::placeholders::_1),
76 SplineDirection::kForward),
77 PlanSpline(std::bind(&AutonomousSplines::AutoNavBounce4, &auto_splines_,
78 std::placeholders::_1),
79 SplineDirection::kBackward)};
80 } else if (FLAGS_barrel) {
81 barrel_spline_ =
82 PlanSpline(std::bind(&AutonomousSplines::AutoNavBarrel, &auto_splines_,
83 std::placeholders::_1),
84 SplineDirection::kForward);
85 } else if (FLAGS_slalom) {
86 slalom_spline_ =
87 PlanSpline(std::bind(&AutonomousSplines::AutoNavSlalom, &auto_splines_,
88 std::placeholders::_1),
89 SplineDirection::kForward);
90 } else if (FLAGS_spline_auto) {
Tyler Chatowbf0609c2021-07-31 16:13:27 -070091 test_spline_ = PlanSpline(std::bind(&AutonomousSplines::TestSpline,
92 &auto_splines_, std::placeholders::_1),
93 SplineDirection::kForward);
James Kuszmaul99af8b52021-03-28 10:50:15 -070094 }
milind upadhyay47a0ab32020-11-25 19:34:41 -080095}
Stephan Massaltd021f972020-01-05 20:41:23 -080096
97void AutonomousActor::Reset() {
98 InitializeEncoders();
99 ResetDrivetrain();
milind upadhyayb2e840a2021-03-27 13:54:49 -0700100 RetractIntake();
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800101
James Kuszmaulddd2ba62020-03-08 22:17:13 -0700102 joystick_state_fetcher_.Fetch();
103 CHECK(joystick_state_fetcher_.get() != nullptr)
104 << "Expect at least one JoystickState message before running auto...";
105 alliance_ = joystick_state_fetcher_->alliance();
Stephan Massaltd021f972020-01-05 20:41:23 -0800106}
107
108bool AutonomousActor::RunAction(
Austin Schuh6fb0a6d2021-01-23 15:43:17 -0800109 const ::frc971::autonomous::AutonomousActionParams *params) {
Stephan Massaltd021f972020-01-05 20:41:23 -0800110 Reset();
James Kuszmaul99af8b52021-03-28 10:50:15 -0700111
112 // Queue up a replan to occur as soon as this action completes.
113 // TODO(james): Modify this so we don't replan during teleop.
114 replan_timer_->Setup(monotonic_now());
115
milind upadhyay47a0ab32020-11-25 19:34:41 -0800116 AOS_LOG(INFO, "Params are %d\n", params->mode());
James Kuszmaulddd2ba62020-03-08 22:17:13 -0700117 if (alliance_ == aos::Alliance::kInvalid) {
118 AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection.");
119 return false;
120 }
kyle96c406e2021-02-27 14:07:22 -0800121 if (FLAGS_galactic_search) {
122 GalacticSearch();
Ravago Jones9c326f52021-03-20 15:00:16 -0700123 } else if (FLAGS_bounce) {
124 AutoNavBounce();
125 } else if (FLAGS_barrel) {
126 AutoNavBarrel();
127 } else if (FLAGS_slalom) {
128 AutoNavSlalom();
kyle96c406e2021-02-27 14:07:22 -0800129 } else if (FLAGS_spline_auto) {
milind upadhyay47a0ab32020-11-25 19:34:41 -0800130 SplineAuto();
131 } else {
132 return DriveFwd();
133 }
134 return true;
135}
Stephan Massaltd021f972020-01-05 20:41:23 -0800136
James Kuszmaul99af8b52021-03-28 10:50:15 -0700137void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) {
kyle96c406e2021-02-27 14:07:22 -0800138 // Set up the starting position for the blue alliance.
kyle96c406e2021-02-27 14:07:22 -0800139
140 // TODO(james): Resetting the localizer breaks the left/right statespace
141 // controller. That is a bug, but we can fix that later by not resetting.
142 auto builder = localizer_control_sender_.MakeBuilder();
143
144 LocalizerControl::Builder localizer_control_builder =
145 builder.MakeBuilder<LocalizerControl>();
James Kuszmaul99af8b52021-03-28 10:50:15 -0700146 localizer_control_builder.add_x(start(0));
147 localizer_control_builder.add_y(start(1));
148 localizer_control_builder.add_theta(start(2));
kyle96c406e2021-02-27 14:07:22 -0800149 localizer_control_builder.add_theta_uncertainty(0.00001);
150 if (!builder.Send(localizer_control_builder.Finish())) {
151 AOS_LOG(ERROR, "Failed to reset localizer.\n");
152 }
153}
154
155void AutonomousActor::GalacticSearch() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700156 CHECK(galactic_search_splines_);
157
kyle96c406e2021-02-27 14:07:22 -0800158 path_fetcher_.Fetch();
Austin Schuh3fb9e422021-03-31 20:11:32 -0700159 SplineHandle *spline = nullptr;
160 if (path_fetcher_.get()) {
161 if (path_fetcher_->alliance() == y2020::vision::Alliance::kUnknown) {
162 AOS_LOG(ERROR, "The galactic search path is unknown, doing nothing.");
163 return;
164 }
James Kuszmaul99af8b52021-03-28 10:50:15 -0700165 if (path_fetcher_->alliance() == y2020::vision::Alliance::kRed) {
166 if (path_fetcher_->letter() == y2020::vision::Letter::kA) {
167 LOG(INFO) << "Red A";
168 spline = &galactic_search_splines_->red_a;
169 } else {
170 LOG(INFO) << "Red B";
171 CHECK(path_fetcher_->letter() == y2020::vision::Letter::kB);
172 spline = &galactic_search_splines_->red_b;
173 }
174 } else {
175 if (path_fetcher_->letter() == y2020::vision::Letter::kA) {
176 LOG(INFO) << "Blue A";
177 spline = &galactic_search_splines_->blue_a;
178 } else {
179 LOG(INFO) << "Blue B";
180 CHECK(path_fetcher_->letter() == y2020::vision::Letter::kB);
Austin Schuh8806bed2021-03-31 20:12:00 -0700181 spline = &galactic_search_splines_->blue_b;
James Kuszmaul99af8b52021-03-28 10:50:15 -0700182 }
183 }
kyle96c406e2021-02-27 14:07:22 -0800184 }
Austin Schuh3fb9e422021-03-31 20:11:32 -0700185 if (FLAGS_ignore_vision) {
186 LOG(INFO) << "Forcing Red B";
187 spline = &galactic_search_splines_->red_b;
188 }
189
190 CHECK(spline != nullptr)
191 << "Expect at least one GalacticSearchPath message before running "
192 "auto...";
193
194 SendStartingPosition(spline->starting_position());
195
196 set_intake_goal(1.25);
197 set_roller_voltage(12.0);
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700198 set_intake_preloading(true);
Austin Schuh3fb9e422021-03-31 20:11:32 -0700199 SendSuperstructureGoal();
200
201 if (!spline->WaitForPlan()) return;
202 spline->Start();
203
204 if (!spline->WaitForSplineDistanceRemaining(0.02)) return;
205 RetractIntake();
kyle96c406e2021-02-27 14:07:22 -0800206}
207
Ravago Jones9c326f52021-03-20 15:00:16 -0700208void AutonomousActor::AutoNavBounce() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700209 CHECK(bounce_splines_);
Ravago Jones9c326f52021-03-20 15:00:16 -0700210
James Kuszmaul99af8b52021-03-28 10:50:15 -0700211 auto &splines = *bounce_splines_;
Ravago Jones9c326f52021-03-20 15:00:16 -0700212
James Kuszmaul99af8b52021-03-28 10:50:15 -0700213 SendStartingPosition(splines[0].starting_position());
Ravago Jones9c326f52021-03-20 15:00:16 -0700214
James Kuszmaul99af8b52021-03-28 10:50:15 -0700215 if (!splines[0].WaitForPlan()) return;
216 splines[0].Start();
Ravago Jones9c326f52021-03-20 15:00:16 -0700217
James Kuszmaul99af8b52021-03-28 10:50:15 -0700218 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700219
James Kuszmaul99af8b52021-03-28 10:50:15 -0700220 if (!splines[1].WaitForPlan()) return;
221 splines[1].Start();
Ravago Jones9c326f52021-03-20 15:00:16 -0700222
James Kuszmaul99af8b52021-03-28 10:50:15 -0700223 if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700224
James Kuszmaul99af8b52021-03-28 10:50:15 -0700225 if (!splines[2].WaitForPlan()) return;
226 splines[2].Start();
Ravago Jones9c326f52021-03-20 15:00:16 -0700227
James Kuszmaul99af8b52021-03-28 10:50:15 -0700228 if (!splines[2].WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700229
James Kuszmaul99af8b52021-03-28 10:50:15 -0700230 if (!splines[3].WaitForPlan()) return;
231 splines[3].Start();
Ravago Jones9c326f52021-03-20 15:00:16 -0700232
James Kuszmaul99af8b52021-03-28 10:50:15 -0700233 if (!splines[3].WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700234}
235
236void AutonomousActor::AutoNavBarrel() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700237 CHECK(barrel_spline_);
Ravago Jones9c326f52021-03-20 15:00:16 -0700238
James Kuszmaul99af8b52021-03-28 10:50:15 -0700239 SendStartingPosition(barrel_spline_->starting_position());
Ravago Jones9c326f52021-03-20 15:00:16 -0700240
James Kuszmaul99af8b52021-03-28 10:50:15 -0700241 if (!barrel_spline_->WaitForPlan()) return;
242 barrel_spline_->Start();
243
244 if (!barrel_spline_->WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700245}
246
247void AutonomousActor::AutoNavSlalom() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700248 CHECK(slalom_spline_);
Ravago Jones9c326f52021-03-20 15:00:16 -0700249
James Kuszmaul99af8b52021-03-28 10:50:15 -0700250 SendStartingPosition(slalom_spline_->starting_position());
Ravago Jones9c326f52021-03-20 15:00:16 -0700251
James Kuszmaul99af8b52021-03-28 10:50:15 -0700252 if (!slalom_spline_->WaitForPlan()) return;
253 slalom_spline_->Start();
254
255 if (!slalom_spline_->WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700256}
257
milind upadhyay47a0ab32020-11-25 19:34:41 -0800258void AutonomousActor::SplineAuto() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700259 CHECK(test_spline_);
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800260
James Kuszmaul99af8b52021-03-28 10:50:15 -0700261 SendStartingPosition(test_spline_->starting_position());
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800262
James Kuszmaul99af8b52021-03-28 10:50:15 -0700263 if (!test_spline_->WaitForPlan()) return;
264 test_spline_->Start();
Stephan Massaltd021f972020-01-05 20:41:23 -0800265
James Kuszmaul99af8b52021-03-28 10:50:15 -0700266 if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return;
kyle96c406e2021-02-27 14:07:22 -0800267}
268
milind upadhyay47a0ab32020-11-25 19:34:41 -0800269ProfileParametersT MakeProfileParametersT(const float max_velocity,
270 const float max_acceleration) {
271 ProfileParametersT params;
272 params.max_velocity = max_velocity;
273 params.max_acceleration = max_acceleration;
274 return params;
275}
276
277bool AutonomousActor::DriveFwd() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700278 SendStartingPosition({0, 0, 0});
Austin Schuhfd1715f2021-01-30 16:58:24 -0800279 const ProfileParametersT kDrive = MakeProfileParametersT(0.3f, 1.0f);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800280 const ProfileParametersT kTurn = MakeProfileParametersT(5.0f, 15.0f);
Austin Schuhfd1715f2021-01-30 16:58:24 -0800281 StartDrive(1.0, 0.0, kDrive, kTurn);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800282 return WaitForDriveDone();
283}
Sabina Leavera0b43b42021-03-03 20:30:04 -0800284
285void AutonomousActor::SendSuperstructureGoal() {
Sabina Leavera0b43b42021-03-03 20:30:04 -0800286 auto builder = superstructure_goal_sender_.MakeBuilder();
287
288 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
289 intake_offset;
milind upadhyayb9dec712021-03-20 15:47:51 -0700290
Sabina Leavera0b43b42021-03-03 20:30:04 -0800291 {
292 StaticZeroingSingleDOFProfiledSubsystemGoal::Builder intake_builder =
293 builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>();
294
milind upadhyayb9dec712021-03-20 15:47:51 -0700295 frc971::ProfileParameters::Builder profile_params_builder =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800296 builder.MakeBuilder<frc971::ProfileParameters>();
Austin Schuhb39a21c2021-03-31 20:12:18 -0700297 profile_params_builder.add_max_velocity(20.0);
298 profile_params_builder.add_max_acceleration(60.0);
milind upadhyayb9dec712021-03-20 15:47:51 -0700299 flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800300 profile_params_builder.Finish();
301 intake_builder.add_unsafe_goal(intake_goal_);
302 intake_builder.add_profile_params(profile_params_offset);
303 intake_offset = intake_builder.Finish();
304 }
305
306 superstructure::Goal::Builder superstructure_builder =
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700307 builder.MakeBuilder<superstructure::Goal>();
milind upadhyayb9dec712021-03-20 15:47:51 -0700308
Sabina Leavera0b43b42021-03-03 20:30:04 -0800309 superstructure_builder.add_intake(intake_offset);
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700310 superstructure_builder.add_intake_preloading(intake_preloading_);
Sabina Leavera0b43b42021-03-03 20:30:04 -0800311 superstructure_builder.add_roller_voltage(roller_voltage_);
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700312 superstructure_builder.add_roller_speed_compensation(
313 kRollerSpeedCompensation);
Sabina Leavera0b43b42021-03-03 20:30:04 -0800314
315 if (!builder.Send(superstructure_builder.Finish())) {
316 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
317 }
Sabina Leavera0b43b42021-03-03 20:30:04 -0800318}
milind upadhyay5e589d72021-03-27 13:47:18 -0700319
320void AutonomousActor::RetractIntake() {
321 set_intake_goal(-0.89);
322 set_roller_voltage(0.0);
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700323 set_intake_preloading(false);
milind upadhyay5e589d72021-03-27 13:47:18 -0700324 SendSuperstructureGoal();
325}
326
Stephan Massaltd021f972020-01-05 20:41:23 -0800327} // namespace actors
328} // namespace y2020