blob: 1c0f040669f73cbffdb84d5e1b8b99a804fdb716 [file] [log] [blame]
Stephan Massaltd021f972020-01-05 20:41:23 -08001#include "y2020/actors/autonomous_actor.h"
2
3#include <inttypes.h>
4
5#include <chrono>
6#include <cmath>
7
8#include "aos/logging/logging.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"
Ravago Jonesc2a08022021-02-06 17:40:54 -080012#include "y2020/control_loops/drivetrain/drivetrain_base.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080013
Austin Schuh3fb9e422021-03-31 20:11:32 -070014DEFINE_bool(spline_auto, false, "If true, define a spline autonomous mode");
15DEFINE_bool(ignore_vision, true, "If true, ignore vision");
16DEFINE_bool(galactic_search, true,
kyle96c406e2021-02-27 14:07:22 -080017 "If true, do the galactic search autonomous");
Ravago Jones9c326f52021-03-20 15:00:16 -070018DEFINE_bool(bounce, false, "If true, run the AutoNav Bounce autonomous");
19DEFINE_bool(barrel, false, "If true, run the AutoNav Barrel autonomous");
20DEFINE_bool(slalom, false, "If true, run the AutoNav Slalom autonomous");
milind upadhyay47a0ab32020-11-25 19:34:41 -080021
Stephan Massaltd021f972020-01-05 20:41:23 -080022namespace y2020 {
23namespace actors {
24
25using ::aos::monotonic_clock;
26using ::frc971::ProfileParametersT;
27using frc971::control_loops::drivetrain::LocalizerControl;
28namespace chrono = ::std::chrono;
29
30AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
31 : frc971::autonomous::BaseAutonomousActor(
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080032 event_loop, control_loops::drivetrain::GetDrivetrainConfig()),
Ravago Jonesc2a08022021-02-06 17:40:54 -080033 localizer_control_sender_(
34 event_loop->MakeSender<
35 ::frc971::control_loops::drivetrain::LocalizerControl>(
36 "/drivetrain")),
Austin Schuh67e127e2021-03-27 13:25:23 -070037 superstructure_goal_sender_(
38 event_loop->MakeSender<control_loops::superstructure::Goal>(
39 "/superstructure")),
James Kuszmaulddd2ba62020-03-08 22:17:13 -070040 joystick_state_fetcher_(
Ravago Jonesc2a08022021-02-06 17:40:54 -080041 event_loop->MakeFetcher<aos::JoystickState>("/aos")),
kyle96c406e2021-02-27 14:07:22 -080042 path_fetcher_(event_loop->MakeFetcher<y2020::vision::GalacticSearchPath>(
Austin Schuhc6442fc2021-03-27 13:25:42 -070043 "/pi2/camera")),
Ravago Jonesc2a08022021-02-06 17:40:54 -080044 auto_splines_() {
milind upadhyay47a0ab32020-11-25 19:34:41 -080045 set_max_drivetrain_voltage(2.0);
James Kuszmaul99af8b52021-03-28 10:50:15 -070046 replan_timer_ = event_loop->AddTimer([this]() { Replan(); });
47 event_loop->OnRun([this, event_loop]() {
48 replan_timer_->Setup(event_loop->monotonic_now());
49 });
50}
51
52void AutonomousActor::Replan() {
53 if (FLAGS_galactic_search) {
54 galactic_search_splines_ = {
55 .red_a = PlanSpline(std::bind(&AutonomousSplines::SplineRedA,
56 &auto_splines_, std::placeholders::_1),
57 SplineDirection::kForward),
58 .red_b = PlanSpline(std::bind(&AutonomousSplines::SplineRedB,
59 &auto_splines_, std::placeholders::_1),
60 SplineDirection::kForward),
61 .blue_a = PlanSpline(std::bind(&AutonomousSplines::SplineBlueA,
62 &auto_splines_, std::placeholders::_1),
63 SplineDirection::kForward),
64 .blue_b = PlanSpline(std::bind(&AutonomousSplines::SplineBlueB,
65 &auto_splines_, std::placeholders::_1),
66 SplineDirection::kForward)};
67 } else if (FLAGS_bounce) {
68 bounce_splines_ = {
69 PlanSpline(std::bind(&AutonomousSplines::AutoNavBounce1, &auto_splines_,
70 std::placeholders::_1),
71 SplineDirection::kForward),
72 PlanSpline(std::bind(&AutonomousSplines::AutoNavBounce2, &auto_splines_,
73 std::placeholders::_1),
74 SplineDirection::kBackward),
75 PlanSpline(std::bind(&AutonomousSplines::AutoNavBounce3, &auto_splines_,
76 std::placeholders::_1),
77 SplineDirection::kForward),
78 PlanSpline(std::bind(&AutonomousSplines::AutoNavBounce4, &auto_splines_,
79 std::placeholders::_1),
80 SplineDirection::kBackward)};
81 } else if (FLAGS_barrel) {
82 barrel_spline_ =
83 PlanSpline(std::bind(&AutonomousSplines::AutoNavBarrel, &auto_splines_,
84 std::placeholders::_1),
85 SplineDirection::kForward);
86 } else if (FLAGS_slalom) {
87 slalom_spline_ =
88 PlanSpline(std::bind(&AutonomousSplines::AutoNavSlalom, &auto_splines_,
89 std::placeholders::_1),
90 SplineDirection::kForward);
91 } else if (FLAGS_spline_auto) {
92 test_spline_ =
93 PlanSpline(std::bind(&AutonomousSplines::TestSpline, &auto_splines_,
94 std::placeholders::_1),
95 SplineDirection::kForward);
96 }
milind upadhyay47a0ab32020-11-25 19:34:41 -080097}
Stephan Massaltd021f972020-01-05 20:41:23 -080098
99void AutonomousActor::Reset() {
100 InitializeEncoders();
101 ResetDrivetrain();
milind upadhyayb2e840a2021-03-27 13:54:49 -0700102 RetractIntake();
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800103
James Kuszmaulddd2ba62020-03-08 22:17:13 -0700104 joystick_state_fetcher_.Fetch();
105 CHECK(joystick_state_fetcher_.get() != nullptr)
106 << "Expect at least one JoystickState message before running auto...";
107 alliance_ = joystick_state_fetcher_->alliance();
Stephan Massaltd021f972020-01-05 20:41:23 -0800108}
109
110bool AutonomousActor::RunAction(
Austin Schuh6fb0a6d2021-01-23 15:43:17 -0800111 const ::frc971::autonomous::AutonomousActionParams *params) {
Stephan Massaltd021f972020-01-05 20:41:23 -0800112 Reset();
James Kuszmaul99af8b52021-03-28 10:50:15 -0700113
114 // Queue up a replan to occur as soon as this action completes.
115 // TODO(james): Modify this so we don't replan during teleop.
116 replan_timer_->Setup(monotonic_now());
117
milind upadhyay47a0ab32020-11-25 19:34:41 -0800118 AOS_LOG(INFO, "Params are %d\n", params->mode());
James Kuszmaulddd2ba62020-03-08 22:17:13 -0700119 if (alliance_ == aos::Alliance::kInvalid) {
120 AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection.");
121 return false;
122 }
kyle96c406e2021-02-27 14:07:22 -0800123 if (FLAGS_galactic_search) {
124 GalacticSearch();
Ravago Jones9c326f52021-03-20 15:00:16 -0700125 } else if (FLAGS_bounce) {
126 AutoNavBounce();
127 } else if (FLAGS_barrel) {
128 AutoNavBarrel();
129 } else if (FLAGS_slalom) {
130 AutoNavSlalom();
kyle96c406e2021-02-27 14:07:22 -0800131 } else if (FLAGS_spline_auto) {
milind upadhyay47a0ab32020-11-25 19:34:41 -0800132 SplineAuto();
133 } else {
134 return DriveFwd();
135 }
136 return true;
137}
Stephan Massaltd021f972020-01-05 20:41:23 -0800138
James Kuszmaul99af8b52021-03-28 10:50:15 -0700139void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) {
kyle96c406e2021-02-27 14:07:22 -0800140 // Set up the starting position for the blue alliance.
kyle96c406e2021-02-27 14:07:22 -0800141
142 // TODO(james): Resetting the localizer breaks the left/right statespace
143 // controller. That is a bug, but we can fix that later by not resetting.
144 auto builder = localizer_control_sender_.MakeBuilder();
145
146 LocalizerControl::Builder localizer_control_builder =
147 builder.MakeBuilder<LocalizerControl>();
James Kuszmaul99af8b52021-03-28 10:50:15 -0700148 localizer_control_builder.add_x(start(0));
149 localizer_control_builder.add_y(start(1));
150 localizer_control_builder.add_theta(start(2));
kyle96c406e2021-02-27 14:07:22 -0800151 localizer_control_builder.add_theta_uncertainty(0.00001);
152 if (!builder.Send(localizer_control_builder.Finish())) {
153 AOS_LOG(ERROR, "Failed to reset localizer.\n");
154 }
155}
156
157void AutonomousActor::GalacticSearch() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700158 CHECK(galactic_search_splines_);
159
kyle96c406e2021-02-27 14:07:22 -0800160 path_fetcher_.Fetch();
Austin Schuh3fb9e422021-03-31 20:11:32 -0700161 SplineHandle *spline = nullptr;
162 if (path_fetcher_.get()) {
163 if (path_fetcher_->alliance() == y2020::vision::Alliance::kUnknown) {
164 AOS_LOG(ERROR, "The galactic search path is unknown, doing nothing.");
165 return;
166 }
James Kuszmaul99af8b52021-03-28 10:50:15 -0700167 if (path_fetcher_->alliance() == y2020::vision::Alliance::kRed) {
168 if (path_fetcher_->letter() == y2020::vision::Letter::kA) {
169 LOG(INFO) << "Red A";
170 spline = &galactic_search_splines_->red_a;
171 } else {
172 LOG(INFO) << "Red B";
173 CHECK(path_fetcher_->letter() == y2020::vision::Letter::kB);
174 spline = &galactic_search_splines_->red_b;
175 }
176 } else {
177 if (path_fetcher_->letter() == y2020::vision::Letter::kA) {
178 LOG(INFO) << "Blue A";
179 spline = &galactic_search_splines_->blue_a;
180 } else {
181 LOG(INFO) << "Blue B";
182 CHECK(path_fetcher_->letter() == y2020::vision::Letter::kB);
Austin Schuh8806bed2021-03-31 20:12:00 -0700183 spline = &galactic_search_splines_->blue_b;
James Kuszmaul99af8b52021-03-28 10:50:15 -0700184 }
185 }
kyle96c406e2021-02-27 14:07:22 -0800186 }
Austin Schuh3fb9e422021-03-31 20:11:32 -0700187 if (FLAGS_ignore_vision) {
188 LOG(INFO) << "Forcing Red B";
189 spline = &galactic_search_splines_->red_b;
190 }
191
192 CHECK(spline != nullptr)
193 << "Expect at least one GalacticSearchPath message before running "
194 "auto...";
195
196 SendStartingPosition(spline->starting_position());
197
198 set_intake_goal(1.25);
199 set_roller_voltage(12.0);
200 SendSuperstructureGoal();
201
202 if (!spline->WaitForPlan()) return;
203 spline->Start();
204
205 if (!spline->WaitForSplineDistanceRemaining(0.02)) return;
206 RetractIntake();
kyle96c406e2021-02-27 14:07:22 -0800207}
208
Ravago Jones9c326f52021-03-20 15:00:16 -0700209void AutonomousActor::AutoNavBounce() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700210 CHECK(bounce_splines_);
Ravago Jones9c326f52021-03-20 15:00:16 -0700211
James Kuszmaul99af8b52021-03-28 10:50:15 -0700212 auto &splines = *bounce_splines_;
Ravago Jones9c326f52021-03-20 15:00:16 -0700213
James Kuszmaul99af8b52021-03-28 10:50:15 -0700214 SendStartingPosition(splines[0].starting_position());
Ravago Jones9c326f52021-03-20 15:00:16 -0700215
James Kuszmaul99af8b52021-03-28 10:50:15 -0700216 if (!splines[0].WaitForPlan()) return;
217 splines[0].Start();
Ravago Jones9c326f52021-03-20 15:00:16 -0700218
James Kuszmaul99af8b52021-03-28 10:50:15 -0700219 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700220
James Kuszmaul99af8b52021-03-28 10:50:15 -0700221 if (!splines[1].WaitForPlan()) return;
222 splines[1].Start();
Ravago Jones9c326f52021-03-20 15:00:16 -0700223
James Kuszmaul99af8b52021-03-28 10:50:15 -0700224 if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700225
James Kuszmaul99af8b52021-03-28 10:50:15 -0700226 if (!splines[2].WaitForPlan()) return;
227 splines[2].Start();
Ravago Jones9c326f52021-03-20 15:00:16 -0700228
James Kuszmaul99af8b52021-03-28 10:50:15 -0700229 if (!splines[2].WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700230
James Kuszmaul99af8b52021-03-28 10:50:15 -0700231 if (!splines[3].WaitForPlan()) return;
232 splines[3].Start();
Ravago Jones9c326f52021-03-20 15:00:16 -0700233
James Kuszmaul99af8b52021-03-28 10:50:15 -0700234 if (!splines[3].WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700235}
236
237void AutonomousActor::AutoNavBarrel() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700238 CHECK(barrel_spline_);
Ravago Jones9c326f52021-03-20 15:00:16 -0700239
James Kuszmaul99af8b52021-03-28 10:50:15 -0700240 SendStartingPosition(barrel_spline_->starting_position());
Ravago Jones9c326f52021-03-20 15:00:16 -0700241
James Kuszmaul99af8b52021-03-28 10:50:15 -0700242 if (!barrel_spline_->WaitForPlan()) return;
243 barrel_spline_->Start();
244
245 if (!barrel_spline_->WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700246}
247
248void AutonomousActor::AutoNavSlalom() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700249 CHECK(slalom_spline_);
Ravago Jones9c326f52021-03-20 15:00:16 -0700250
James Kuszmaul99af8b52021-03-28 10:50:15 -0700251 SendStartingPosition(slalom_spline_->starting_position());
Ravago Jones9c326f52021-03-20 15:00:16 -0700252
James Kuszmaul99af8b52021-03-28 10:50:15 -0700253 if (!slalom_spline_->WaitForPlan()) return;
254 slalom_spline_->Start();
255
256 if (!slalom_spline_->WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700257}
258
milind upadhyay47a0ab32020-11-25 19:34:41 -0800259void AutonomousActor::SplineAuto() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700260 CHECK(test_spline_);
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800261
James Kuszmaul99af8b52021-03-28 10:50:15 -0700262 SendStartingPosition(test_spline_->starting_position());
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800263
James Kuszmaul99af8b52021-03-28 10:50:15 -0700264 if (!test_spline_->WaitForPlan()) return;
265 test_spline_->Start();
Stephan Massaltd021f972020-01-05 20:41:23 -0800266
James Kuszmaul99af8b52021-03-28 10:50:15 -0700267 if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return;
kyle96c406e2021-02-27 14:07:22 -0800268}
269
milind upadhyay47a0ab32020-11-25 19:34:41 -0800270ProfileParametersT MakeProfileParametersT(const float max_velocity,
271 const float max_acceleration) {
272 ProfileParametersT params;
273 params.max_velocity = max_velocity;
274 params.max_acceleration = max_acceleration;
275 return params;
276}
277
278bool AutonomousActor::DriveFwd() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700279 SendStartingPosition({0, 0, 0});
Austin Schuhfd1715f2021-01-30 16:58:24 -0800280 const ProfileParametersT kDrive = MakeProfileParametersT(0.3f, 1.0f);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800281 const ProfileParametersT kTurn = MakeProfileParametersT(5.0f, 15.0f);
Austin Schuhfd1715f2021-01-30 16:58:24 -0800282 StartDrive(1.0, 0.0, kDrive, kTurn);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800283 return WaitForDriveDone();
284}
Sabina Leavera0b43b42021-03-03 20:30:04 -0800285
286void AutonomousActor::SendSuperstructureGoal() {
Sabina Leavera0b43b42021-03-03 20:30:04 -0800287 auto builder = superstructure_goal_sender_.MakeBuilder();
288
289 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
290 intake_offset;
milind upadhyayb9dec712021-03-20 15:47:51 -0700291
Sabina Leavera0b43b42021-03-03 20:30:04 -0800292 {
293 StaticZeroingSingleDOFProfiledSubsystemGoal::Builder intake_builder =
294 builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>();
295
milind upadhyayb9dec712021-03-20 15:47:51 -0700296 frc971::ProfileParameters::Builder profile_params_builder =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800297 builder.MakeBuilder<frc971::ProfileParameters>();
Sabina Leaver8302e3c2021-03-20 16:36:05 -0700298 profile_params_builder.add_max_velocity(10.0);
299 profile_params_builder.add_max_acceleration(30.0);
milind upadhyayb9dec712021-03-20 15:47:51 -0700300 flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800301 profile_params_builder.Finish();
302 intake_builder.add_unsafe_goal(intake_goal_);
303 intake_builder.add_profile_params(profile_params_offset);
304 intake_offset = intake_builder.Finish();
305 }
306
307 superstructure::Goal::Builder superstructure_builder =
308 builder.MakeBuilder<superstructure::Goal>();
milind upadhyayb9dec712021-03-20 15:47:51 -0700309
Sabina Leavera0b43b42021-03-03 20:30:04 -0800310 superstructure_builder.add_intake(intake_offset);
311 superstructure_builder.add_roller_voltage(roller_voltage_);
312 superstructure_builder.add_roller_speed_compensation(kRollerSpeedCompensation);
313
314 if (!builder.Send(superstructure_builder.Finish())) {
315 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
316 }
317
318}
milind upadhyay5e589d72021-03-27 13:47:18 -0700319
320void AutonomousActor::RetractIntake() {
321 set_intake_goal(-0.89);
322 set_roller_voltage(0.0);
323 SendSuperstructureGoal();
324}
325
Stephan Massaltd021f972020-01-05 20:41:23 -0800326} // namespace actors
327} // namespace y2020