blob: 2f5087be84e525e4e52f1cc61974561bf39471ba [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);
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700201 set_intake_preloading(true);
Austin Schuh3fb9e422021-03-31 20:11:32 -0700202 SendSuperstructureGoal();
203
204 if (!spline->WaitForPlan()) return;
205 spline->Start();
206
207 if (!spline->WaitForSplineDistanceRemaining(0.02)) return;
208 RetractIntake();
kyle96c406e2021-02-27 14:07:22 -0800209}
210
Ravago Jones9c326f52021-03-20 15:00:16 -0700211void AutonomousActor::AutoNavBounce() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700212 CHECK(bounce_splines_);
Ravago Jones9c326f52021-03-20 15:00:16 -0700213
James Kuszmaul99af8b52021-03-28 10:50:15 -0700214 auto &splines = *bounce_splines_;
Ravago Jones9c326f52021-03-20 15:00:16 -0700215
James Kuszmaul99af8b52021-03-28 10:50:15 -0700216 SendStartingPosition(splines[0].starting_position());
Ravago Jones9c326f52021-03-20 15:00:16 -0700217
James Kuszmaul99af8b52021-03-28 10:50:15 -0700218 if (!splines[0].WaitForPlan()) return;
219 splines[0].Start();
Ravago Jones9c326f52021-03-20 15:00:16 -0700220
James Kuszmaul99af8b52021-03-28 10:50:15 -0700221 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700222
James Kuszmaul99af8b52021-03-28 10:50:15 -0700223 if (!splines[1].WaitForPlan()) return;
224 splines[1].Start();
Ravago Jones9c326f52021-03-20 15:00:16 -0700225
James Kuszmaul99af8b52021-03-28 10:50:15 -0700226 if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700227
James Kuszmaul99af8b52021-03-28 10:50:15 -0700228 if (!splines[2].WaitForPlan()) return;
229 splines[2].Start();
Ravago Jones9c326f52021-03-20 15:00:16 -0700230
James Kuszmaul99af8b52021-03-28 10:50:15 -0700231 if (!splines[2].WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700232
James Kuszmaul99af8b52021-03-28 10:50:15 -0700233 if (!splines[3].WaitForPlan()) return;
234 splines[3].Start();
Ravago Jones9c326f52021-03-20 15:00:16 -0700235
James Kuszmaul99af8b52021-03-28 10:50:15 -0700236 if (!splines[3].WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700237}
238
239void AutonomousActor::AutoNavBarrel() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700240 CHECK(barrel_spline_);
Ravago Jones9c326f52021-03-20 15:00:16 -0700241
James Kuszmaul99af8b52021-03-28 10:50:15 -0700242 SendStartingPosition(barrel_spline_->starting_position());
Ravago Jones9c326f52021-03-20 15:00:16 -0700243
James Kuszmaul99af8b52021-03-28 10:50:15 -0700244 if (!barrel_spline_->WaitForPlan()) return;
245 barrel_spline_->Start();
246
247 if (!barrel_spline_->WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700248}
249
250void AutonomousActor::AutoNavSlalom() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700251 CHECK(slalom_spline_);
Ravago Jones9c326f52021-03-20 15:00:16 -0700252
James Kuszmaul99af8b52021-03-28 10:50:15 -0700253 SendStartingPosition(slalom_spline_->starting_position());
Ravago Jones9c326f52021-03-20 15:00:16 -0700254
James Kuszmaul99af8b52021-03-28 10:50:15 -0700255 if (!slalom_spline_->WaitForPlan()) return;
256 slalom_spline_->Start();
257
258 if (!slalom_spline_->WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700259}
260
milind upadhyay47a0ab32020-11-25 19:34:41 -0800261void AutonomousActor::SplineAuto() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700262 CHECK(test_spline_);
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800263
James Kuszmaul99af8b52021-03-28 10:50:15 -0700264 SendStartingPosition(test_spline_->starting_position());
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800265
James Kuszmaul99af8b52021-03-28 10:50:15 -0700266 if (!test_spline_->WaitForPlan()) return;
267 test_spline_->Start();
Stephan Massaltd021f972020-01-05 20:41:23 -0800268
James Kuszmaul99af8b52021-03-28 10:50:15 -0700269 if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return;
kyle96c406e2021-02-27 14:07:22 -0800270}
271
milind upadhyay47a0ab32020-11-25 19:34:41 -0800272ProfileParametersT MakeProfileParametersT(const float max_velocity,
273 const float max_acceleration) {
274 ProfileParametersT params;
275 params.max_velocity = max_velocity;
276 params.max_acceleration = max_acceleration;
277 return params;
278}
279
280bool AutonomousActor::DriveFwd() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700281 SendStartingPosition({0, 0, 0});
Austin Schuhfd1715f2021-01-30 16:58:24 -0800282 const ProfileParametersT kDrive = MakeProfileParametersT(0.3f, 1.0f);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800283 const ProfileParametersT kTurn = MakeProfileParametersT(5.0f, 15.0f);
Austin Schuhfd1715f2021-01-30 16:58:24 -0800284 StartDrive(1.0, 0.0, kDrive, kTurn);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800285 return WaitForDriveDone();
286}
Sabina Leavera0b43b42021-03-03 20:30:04 -0800287
288void AutonomousActor::SendSuperstructureGoal() {
Sabina Leavera0b43b42021-03-03 20:30:04 -0800289 auto builder = superstructure_goal_sender_.MakeBuilder();
290
291 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
292 intake_offset;
milind upadhyayb9dec712021-03-20 15:47:51 -0700293
Sabina Leavera0b43b42021-03-03 20:30:04 -0800294 {
295 StaticZeroingSingleDOFProfiledSubsystemGoal::Builder intake_builder =
296 builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>();
297
milind upadhyayb9dec712021-03-20 15:47:51 -0700298 frc971::ProfileParameters::Builder profile_params_builder =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800299 builder.MakeBuilder<frc971::ProfileParameters>();
Austin Schuhb39a21c2021-03-31 20:12:18 -0700300 profile_params_builder.add_max_velocity(20.0);
301 profile_params_builder.add_max_acceleration(60.0);
milind upadhyayb9dec712021-03-20 15:47:51 -0700302 flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800303 profile_params_builder.Finish();
304 intake_builder.add_unsafe_goal(intake_goal_);
305 intake_builder.add_profile_params(profile_params_offset);
306 intake_offset = intake_builder.Finish();
307 }
308
309 superstructure::Goal::Builder superstructure_builder =
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700310 builder.MakeBuilder<superstructure::Goal>();
milind upadhyayb9dec712021-03-20 15:47:51 -0700311
Sabina Leavera0b43b42021-03-03 20:30:04 -0800312 superstructure_builder.add_intake(intake_offset);
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700313 superstructure_builder.add_intake_preloading(intake_preloading_);
Sabina Leavera0b43b42021-03-03 20:30:04 -0800314 superstructure_builder.add_roller_voltage(roller_voltage_);
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700315 superstructure_builder.add_roller_speed_compensation(
316 kRollerSpeedCompensation);
Sabina Leavera0b43b42021-03-03 20:30:04 -0800317
318 if (!builder.Send(superstructure_builder.Finish())) {
319 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
320 }
Sabina Leavera0b43b42021-03-03 20:30:04 -0800321}
milind upadhyay5e589d72021-03-27 13:47:18 -0700322
323void AutonomousActor::RetractIntake() {
324 set_intake_goal(-0.89);
325 set_roller_voltage(0.0);
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700326 set_intake_preloading(false);
milind upadhyay5e589d72021-03-27 13:47:18 -0700327 SendSuperstructureGoal();
328}
329
Ravago Jones1f32d622021-08-26 12:20:36 -0700330bool AutonomousActor::WaitForBallsShot(int num_wanted) {
331 superstructure_status_fetcher_.Fetch();
332 CHECK(superstructure_status_fetcher_.get() != nullptr);
333 const int initial_balls_shot =
334 superstructure_status_fetcher_->shooter()->balls_shot();
335 int balls_shot = initial_balls_shot;
336
337 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
338 event_loop()->monotonic_now(),
339 frc971::controls::kLoopFrequency / 2);
340 while (true) {
341 if (ShouldCancel()) {
342 return false;
343 }
344 phased_loop.SleepUntilNext();
345 superstructure_status_fetcher_.Fetch();
346 balls_shot = superstructure_status_fetcher_->shooter()->balls_shot();
347 if ((balls_shot - initial_balls_shot) >= num_wanted) {
348 return true;
349 }
350 }
351}
352
Stephan Massaltd021f972020-01-05 20:41:23 -0800353} // namespace actors
354} // namespace y2020