blob: 2e0bebc1ac2e598a7600cc13088435851be00dde [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 Schuhfd1715f2021-01-30 16:58:24 -080014DEFINE_bool(spline_auto, true, "If true, define a spline autonomous mode");
kyle96c406e2021-02-27 14:07:22 -080015DEFINE_bool(galactic_search, false,
16 "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");
19DEFINE_bool(slalom, false, "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) {
91 test_spline_ =
92 PlanSpline(std::bind(&AutonomousSplines::TestSpline, &auto_splines_,
93 std::placeholders::_1),
94 SplineDirection::kForward);
95 }
milind upadhyay47a0ab32020-11-25 19:34:41 -080096}
Stephan Massaltd021f972020-01-05 20:41:23 -080097
98void AutonomousActor::Reset() {
99 InitializeEncoders();
100 ResetDrivetrain();
milind upadhyayb2e840a2021-03-27 13:54:49 -0700101 RetractIntake();
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800102
James Kuszmaulddd2ba62020-03-08 22:17:13 -0700103 joystick_state_fetcher_.Fetch();
104 CHECK(joystick_state_fetcher_.get() != nullptr)
105 << "Expect at least one JoystickState message before running auto...";
106 alliance_ = joystick_state_fetcher_->alliance();
Stephan Massaltd021f972020-01-05 20:41:23 -0800107}
108
109bool AutonomousActor::RunAction(
Austin Schuh6fb0a6d2021-01-23 15:43:17 -0800110 const ::frc971::autonomous::AutonomousActionParams *params) {
Stephan Massaltd021f972020-01-05 20:41:23 -0800111 Reset();
James Kuszmaul99af8b52021-03-28 10:50:15 -0700112
113 // Queue up a replan to occur as soon as this action completes.
114 // TODO(james): Modify this so we don't replan during teleop.
115 replan_timer_->Setup(monotonic_now());
116
milind upadhyay47a0ab32020-11-25 19:34:41 -0800117 AOS_LOG(INFO, "Params are %d\n", params->mode());
James Kuszmaulddd2ba62020-03-08 22:17:13 -0700118 if (alliance_ == aos::Alliance::kInvalid) {
119 AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection.");
120 return false;
121 }
kyle96c406e2021-02-27 14:07:22 -0800122 if (FLAGS_galactic_search) {
123 GalacticSearch();
Ravago Jones9c326f52021-03-20 15:00:16 -0700124 } else if (FLAGS_bounce) {
125 AutoNavBounce();
126 } else if (FLAGS_barrel) {
127 AutoNavBarrel();
128 } else if (FLAGS_slalom) {
129 AutoNavSlalom();
kyle96c406e2021-02-27 14:07:22 -0800130 } else if (FLAGS_spline_auto) {
milind upadhyay47a0ab32020-11-25 19:34:41 -0800131 SplineAuto();
132 } else {
133 return DriveFwd();
134 }
135 return true;
136}
Stephan Massaltd021f972020-01-05 20:41:23 -0800137
James Kuszmaul99af8b52021-03-28 10:50:15 -0700138void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) {
kyle96c406e2021-02-27 14:07:22 -0800139 // Set up the starting position for the blue alliance.
kyle96c406e2021-02-27 14:07:22 -0800140
141 // TODO(james): Resetting the localizer breaks the left/right statespace
142 // controller. That is a bug, but we can fix that later by not resetting.
143 auto builder = localizer_control_sender_.MakeBuilder();
144
145 LocalizerControl::Builder localizer_control_builder =
146 builder.MakeBuilder<LocalizerControl>();
James Kuszmaul99af8b52021-03-28 10:50:15 -0700147 localizer_control_builder.add_x(start(0));
148 localizer_control_builder.add_y(start(1));
149 localizer_control_builder.add_theta(start(2));
kyle96c406e2021-02-27 14:07:22 -0800150 localizer_control_builder.add_theta_uncertainty(0.00001);
151 if (!builder.Send(localizer_control_builder.Finish())) {
152 AOS_LOG(ERROR, "Failed to reset localizer.\n");
153 }
154}
155
156void AutonomousActor::GalacticSearch() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700157 CHECK(galactic_search_splines_);
158
kyle96c406e2021-02-27 14:07:22 -0800159 path_fetcher_.Fetch();
160 CHECK(path_fetcher_.get() != nullptr)
161 << "Expect at least one GalacticSearchPath message before running "
162 "auto...";
163 if (path_fetcher_->alliance() == y2020::vision::Alliance::kUnknown) {
164 AOS_LOG(ERROR, "The galactic search path is unknown, doing nothing.");
165 } else {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700166 SplineHandle *spline = nullptr;
167 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);
183 spline = &galactic_search_splines_->red_b;
184 }
185 }
186 CHECK_NOTNULL(spline);
kyle96c406e2021-02-27 14:07:22 -0800187
James Kuszmaul99af8b52021-03-28 10:50:15 -0700188 SendStartingPosition(spline->starting_position());
kyle96c406e2021-02-27 14:07:22 -0800189
milind upadhyayb9dec712021-03-20 15:47:51 -0700190 set_intake_goal(1.2);
191 set_roller_voltage(7.0);
192 SendSuperstructureGoal();
193
James Kuszmaul99af8b52021-03-28 10:50:15 -0700194 if (!spline->WaitForPlan()) return;
195 spline->Start();
kyle96c406e2021-02-27 14:07:22 -0800196
James Kuszmaul99af8b52021-03-28 10:50:15 -0700197 if (!spline->WaitForSplineDistanceRemaining(0.02)) return;
milind upadhyay5e589d72021-03-27 13:47:18 -0700198 RetractIntake();
kyle96c406e2021-02-27 14:07:22 -0800199 }
200}
201
Ravago Jones9c326f52021-03-20 15:00:16 -0700202void AutonomousActor::AutoNavBounce() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700203 CHECK(bounce_splines_);
Ravago Jones9c326f52021-03-20 15:00:16 -0700204
James Kuszmaul99af8b52021-03-28 10:50:15 -0700205 auto &splines = *bounce_splines_;
Ravago Jones9c326f52021-03-20 15:00:16 -0700206
James Kuszmaul99af8b52021-03-28 10:50:15 -0700207 SendStartingPosition(splines[0].starting_position());
Ravago Jones9c326f52021-03-20 15:00:16 -0700208
James Kuszmaul99af8b52021-03-28 10:50:15 -0700209 if (!splines[0].WaitForPlan()) return;
210 splines[0].Start();
Ravago Jones9c326f52021-03-20 15:00:16 -0700211
James Kuszmaul99af8b52021-03-28 10:50:15 -0700212 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700213
James Kuszmaul99af8b52021-03-28 10:50:15 -0700214 if (!splines[1].WaitForPlan()) return;
215 splines[1].Start();
Ravago Jones9c326f52021-03-20 15:00:16 -0700216
James Kuszmaul99af8b52021-03-28 10:50:15 -0700217 if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700218
James Kuszmaul99af8b52021-03-28 10:50:15 -0700219 if (!splines[2].WaitForPlan()) return;
220 splines[2].Start();
Ravago Jones9c326f52021-03-20 15:00:16 -0700221
James Kuszmaul99af8b52021-03-28 10:50:15 -0700222 if (!splines[2].WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700223
James Kuszmaul99af8b52021-03-28 10:50:15 -0700224 if (!splines[3].WaitForPlan()) return;
225 splines[3].Start();
Ravago Jones9c326f52021-03-20 15:00:16 -0700226
James Kuszmaul99af8b52021-03-28 10:50:15 -0700227 if (!splines[3].WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700228}
229
230void AutonomousActor::AutoNavBarrel() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700231 CHECK(barrel_spline_);
Ravago Jones9c326f52021-03-20 15:00:16 -0700232
James Kuszmaul99af8b52021-03-28 10:50:15 -0700233 SendStartingPosition(barrel_spline_->starting_position());
Ravago Jones9c326f52021-03-20 15:00:16 -0700234
James Kuszmaul99af8b52021-03-28 10:50:15 -0700235 if (!barrel_spline_->WaitForPlan()) return;
236 barrel_spline_->Start();
237
238 if (!barrel_spline_->WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700239}
240
241void AutonomousActor::AutoNavSlalom() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700242 CHECK(slalom_spline_);
Ravago Jones9c326f52021-03-20 15:00:16 -0700243
James Kuszmaul99af8b52021-03-28 10:50:15 -0700244 SendStartingPosition(slalom_spline_->starting_position());
Ravago Jones9c326f52021-03-20 15:00:16 -0700245
James Kuszmaul99af8b52021-03-28 10:50:15 -0700246 if (!slalom_spline_->WaitForPlan()) return;
247 slalom_spline_->Start();
248
249 if (!slalom_spline_->WaitForSplineDistanceRemaining(0.02)) return;
Ravago Jones9c326f52021-03-20 15:00:16 -0700250}
251
milind upadhyay47a0ab32020-11-25 19:34:41 -0800252void AutonomousActor::SplineAuto() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700253 CHECK(test_spline_);
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800254
James Kuszmaul99af8b52021-03-28 10:50:15 -0700255 SendStartingPosition(test_spline_->starting_position());
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800256
James Kuszmaul99af8b52021-03-28 10:50:15 -0700257 if (!test_spline_->WaitForPlan()) return;
258 test_spline_->Start();
Stephan Massaltd021f972020-01-05 20:41:23 -0800259
James Kuszmaul99af8b52021-03-28 10:50:15 -0700260 if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return;
kyle96c406e2021-02-27 14:07:22 -0800261}
262
milind upadhyay47a0ab32020-11-25 19:34:41 -0800263ProfileParametersT MakeProfileParametersT(const float max_velocity,
264 const float max_acceleration) {
265 ProfileParametersT params;
266 params.max_velocity = max_velocity;
267 params.max_acceleration = max_acceleration;
268 return params;
269}
270
271bool AutonomousActor::DriveFwd() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700272 SendStartingPosition({0, 0, 0});
Austin Schuhfd1715f2021-01-30 16:58:24 -0800273 const ProfileParametersT kDrive = MakeProfileParametersT(0.3f, 1.0f);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800274 const ProfileParametersT kTurn = MakeProfileParametersT(5.0f, 15.0f);
Austin Schuhfd1715f2021-01-30 16:58:24 -0800275 StartDrive(1.0, 0.0, kDrive, kTurn);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800276 return WaitForDriveDone();
277}
Sabina Leavera0b43b42021-03-03 20:30:04 -0800278
279void AutonomousActor::SendSuperstructureGoal() {
Sabina Leavera0b43b42021-03-03 20:30:04 -0800280 auto builder = superstructure_goal_sender_.MakeBuilder();
281
282 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
283 intake_offset;
milind upadhyayb9dec712021-03-20 15:47:51 -0700284
Sabina Leavera0b43b42021-03-03 20:30:04 -0800285 {
286 StaticZeroingSingleDOFProfiledSubsystemGoal::Builder intake_builder =
287 builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>();
288
milind upadhyayb9dec712021-03-20 15:47:51 -0700289 frc971::ProfileParameters::Builder profile_params_builder =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800290 builder.MakeBuilder<frc971::ProfileParameters>();
Sabina Leaver8302e3c2021-03-20 16:36:05 -0700291 profile_params_builder.add_max_velocity(10.0);
292 profile_params_builder.add_max_acceleration(30.0);
milind upadhyayb9dec712021-03-20 15:47:51 -0700293 flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800294 profile_params_builder.Finish();
295 intake_builder.add_unsafe_goal(intake_goal_);
296 intake_builder.add_profile_params(profile_params_offset);
297 intake_offset = intake_builder.Finish();
298 }
299
300 superstructure::Goal::Builder superstructure_builder =
301 builder.MakeBuilder<superstructure::Goal>();
milind upadhyayb9dec712021-03-20 15:47:51 -0700302
Sabina Leavera0b43b42021-03-03 20:30:04 -0800303 superstructure_builder.add_intake(intake_offset);
304 superstructure_builder.add_roller_voltage(roller_voltage_);
305 superstructure_builder.add_roller_speed_compensation(kRollerSpeedCompensation);
306
307 if (!builder.Send(superstructure_builder.Finish())) {
308 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
309 }
310
311}
milind upadhyay5e589d72021-03-27 13:47:18 -0700312
313void AutonomousActor::RetractIntake() {
314 set_intake_goal(-0.89);
315 set_roller_voltage(0.0);
316 SendSuperstructureGoal();
317}
318
Stephan Massaltd021f972020-01-05 20:41:23 -0800319} // namespace actors
320} // namespace y2020