blob: 022998db3d2de49614b96be26331d0e8874b4726 [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"
kyle96c406e2021-02-27 14:07:22 -080011#include "frc971/control_loops/drivetrain/spline.h"
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080012#include "y2020/actors/auto_splines.h"
Ravago Jonesc2a08022021-02-06 17:40:54 -080013#include "y2020/control_loops/drivetrain/drivetrain_base.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080014
Austin Schuhfd1715f2021-01-30 16:58:24 -080015DEFINE_bool(spline_auto, true, "If true, define a spline autonomous mode");
kyle96c406e2021-02-27 14:07:22 -080016DEFINE_bool(galactic_search, false,
17 "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);
46}
Stephan Massaltd021f972020-01-05 20:41:23 -080047
48void AutonomousActor::Reset() {
49 InitializeEncoders();
50 ResetDrivetrain();
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080051
James Kuszmaulddd2ba62020-03-08 22:17:13 -070052 joystick_state_fetcher_.Fetch();
53 CHECK(joystick_state_fetcher_.get() != nullptr)
54 << "Expect at least one JoystickState message before running auto...";
55 alliance_ = joystick_state_fetcher_->alliance();
Stephan Massaltd021f972020-01-05 20:41:23 -080056}
57
58bool AutonomousActor::RunAction(
Austin Schuh6fb0a6d2021-01-23 15:43:17 -080059 const ::frc971::autonomous::AutonomousActionParams *params) {
Stephan Massaltd021f972020-01-05 20:41:23 -080060 Reset();
milind upadhyay47a0ab32020-11-25 19:34:41 -080061 AOS_LOG(INFO, "Params are %d\n", params->mode());
James Kuszmaulddd2ba62020-03-08 22:17:13 -070062 if (alliance_ == aos::Alliance::kInvalid) {
63 AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection.");
64 return false;
65 }
kyle96c406e2021-02-27 14:07:22 -080066 if (FLAGS_galactic_search) {
67 GalacticSearch();
Ravago Jones9c326f52021-03-20 15:00:16 -070068 } else if (FLAGS_bounce) {
69 AutoNavBounce();
70 } else if (FLAGS_barrel) {
71 AutoNavBarrel();
72 } else if (FLAGS_slalom) {
73 AutoNavSlalom();
kyle96c406e2021-02-27 14:07:22 -080074 } else if (FLAGS_spline_auto) {
milind upadhyay47a0ab32020-11-25 19:34:41 -080075 SplineAuto();
76 } else {
77 return DriveFwd();
78 }
79 return true;
80}
Stephan Massaltd021f972020-01-05 20:41:23 -080081
kyle96c406e2021-02-27 14:07:22 -080082void AutonomousActor::SendStartingPosition(double x, double y, double theta) {
83 // Set up the starting position for the blue alliance.
84 double starting_heading = theta;
85
86 // TODO(james): Resetting the localizer breaks the left/right statespace
87 // controller. That is a bug, but we can fix that later by not resetting.
88 auto builder = localizer_control_sender_.MakeBuilder();
89
90 LocalizerControl::Builder localizer_control_builder =
91 builder.MakeBuilder<LocalizerControl>();
92 localizer_control_builder.add_x(x);
93 localizer_control_builder.add_y(y);
94 localizer_control_builder.add_theta(starting_heading);
95 localizer_control_builder.add_theta_uncertainty(0.00001);
96 if (!builder.Send(localizer_control_builder.Finish())) {
97 AOS_LOG(ERROR, "Failed to reset localizer.\n");
98 }
99}
100
101void AutonomousActor::GalacticSearch() {
102 path_fetcher_.Fetch();
103 CHECK(path_fetcher_.get() != nullptr)
104 << "Expect at least one GalacticSearchPath message before running "
105 "auto...";
106 if (path_fetcher_->alliance() == y2020::vision::Alliance::kUnknown) {
107 AOS_LOG(ERROR, "The galactic search path is unknown, doing nothing.");
108 } else {
109 SplineHandle spline1 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800110 [this](
111 aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
112 *builder) {
kyle96c406e2021-02-27 14:07:22 -0800113 flatbuffers::Offset<frc971::MultiSpline> target_spline;
114 if (path_fetcher_->alliance() == y2020::vision::Alliance::kRed) {
115 if (path_fetcher_->letter() == y2020::vision::Letter::kA) {
Austin Schuhc8ba53a2021-03-27 13:26:04 -0700116 LOG(INFO) << "Red A";
kyle96c406e2021-02-27 14:07:22 -0800117 target_spline = auto_splines_.SplineRedA(builder);
118 } else {
Austin Schuhc8ba53a2021-03-27 13:26:04 -0700119 LOG(INFO) << "Red B";
kyle96c406e2021-02-27 14:07:22 -0800120 CHECK(path_fetcher_->letter() == y2020::vision::Letter::kB);
121 target_spline = auto_splines_.SplineRedB(builder);
122 }
123 } else {
124 if (path_fetcher_->letter() == y2020::vision::Letter::kA) {
Austin Schuhc8ba53a2021-03-27 13:26:04 -0700125 LOG(INFO) << "Blue A";
kyle96c406e2021-02-27 14:07:22 -0800126 target_spline = auto_splines_.SplineBlueA(builder);
127 } else {
Austin Schuhc8ba53a2021-03-27 13:26:04 -0700128 LOG(INFO) << "Blue B";
kyle96c406e2021-02-27 14:07:22 -0800129 CHECK(path_fetcher_->letter() == y2020::vision::Letter::kB);
130 target_spline = auto_splines_.SplineBlueB(builder);
131 }
132 }
133 const frc971::MultiSpline *const spline =
134 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
135
136 SendStartingPosition(CHECK_NOTNULL(spline));
137
138 return target_spline;
139 },
140 SplineDirection::kForward);
141
milind upadhyayb9dec712021-03-20 15:47:51 -0700142 set_intake_goal(1.2);
143 set_roller_voltage(7.0);
144 SendSuperstructureGoal();
145
kyle96c406e2021-02-27 14:07:22 -0800146 if (!spline1.WaitForPlan()) return;
147 spline1.Start();
148
149 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
milind upadhyayb9dec712021-03-20 15:47:51 -0700150 set_intake_goal(-0.89);
151 set_roller_voltage(0.0);
152 SendSuperstructureGoal();
kyle96c406e2021-02-27 14:07:22 -0800153 }
154}
155
Ravago Jones9c326f52021-03-20 15:00:16 -0700156void AutonomousActor::AutoNavBounce() {
157 SplineHandle spline1 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800158 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
Ravago Jones9c326f52021-03-20 15:00:16 -0700159 *builder) {
160 flatbuffers::Offset<frc971::MultiSpline> target_spline =
161 auto_splines_.AutoNavBounce1(builder);
162 const frc971::MultiSpline *const spline =
163 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
164 SendStartingPosition(CHECK_NOTNULL(spline));
165 return target_spline;
166 },
167 SplineDirection::kForward);
168
169 if (!spline1.WaitForPlan()) return;
170 spline1.Start();
171
172 SplineHandle spline2 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800173 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
Ravago Jones9c326f52021-03-20 15:00:16 -0700174 *builder) { return auto_splines_.AutoNavBounce2(builder); },
175 SplineDirection::kBackward);
176
177 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
178
179 if (!spline2.WaitForPlan()) return;
180 spline2.Start();
181
182 SplineHandle spline3 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800183 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
Ravago Jones9c326f52021-03-20 15:00:16 -0700184 *builder) { return auto_splines_.AutoNavBounce3(builder); },
185 SplineDirection::kForward);
186
187 if (!spline2.WaitForSplineDistanceRemaining(0.02)) return;
188
189 if (!spline3.WaitForPlan()) return;
190 spline3.Start();
191
192 SplineHandle spline4 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800193 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
Ravago Jones9c326f52021-03-20 15:00:16 -0700194 *builder) { return auto_splines_.AutoNavBounce4(builder); },
195 SplineDirection::kBackward);
196
197 if (!spline3.WaitForSplineDistanceRemaining(0.02)) return;
198
199 if (!spline4.WaitForPlan()) return;
200 spline4.Start();
201
202 if (!spline4.WaitForSplineDistanceRemaining(0.02)) return;
203}
204
205void AutonomousActor::AutoNavBarrel() {
206 SplineHandle spline1 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800207 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
Ravago Jones9c326f52021-03-20 15:00:16 -0700208 *builder) {
209 flatbuffers::Offset<frc971::MultiSpline> target_spline;
210 target_spline = auto_splines_.AutoNavBarrel(builder);
211 const frc971::MultiSpline *const spline =
212 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
213 SendStartingPosition(CHECK_NOTNULL(spline));
214 return target_spline;
215 },
216 SplineDirection::kForward);
217
218 if (!spline1.WaitForPlan()) return;
219 spline1.Start();
220
221 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
222}
223
224void AutonomousActor::AutoNavSlalom() {
225 SplineHandle spline1 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800226 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
Ravago Jones9c326f52021-03-20 15:00:16 -0700227 *builder) {
228 flatbuffers::Offset<frc971::MultiSpline> target_spline;
229 target_spline = auto_splines_.AutoNavSlalom(builder);
230 const frc971::MultiSpline *const spline =
231 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
232 SendStartingPosition(CHECK_NOTNULL(spline));
233 return target_spline;
234 },
235 SplineDirection::kForward);
236
237 if (!spline1.WaitForPlan()) return;
238 spline1.Start();
239
240 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
241}
242
milind upadhyay47a0ab32020-11-25 19:34:41 -0800243void AutonomousActor::SplineAuto() {
Ravago Jonesc2a08022021-02-06 17:40:54 -0800244 SplineHandle spline1 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800245 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
kyle96c406e2021-02-27 14:07:22 -0800246 *builder) {
247 flatbuffers::Offset<frc971::MultiSpline> target_spline;
248 target_spline = auto_splines_.TestSpline(builder);
249 const frc971::MultiSpline *const spline =
250 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
251 SendStartingPosition(CHECK_NOTNULL(spline));
252 return target_spline;
253 },
Ravago Jonesc2a08022021-02-06 17:40:54 -0800254 SplineDirection::kForward);
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800255
milind upadhyay47a0ab32020-11-25 19:34:41 -0800256 if (!spline1.WaitForPlan()) return;
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800257 spline1.Start();
258
milind upadhyay47a0ab32020-11-25 19:34:41 -0800259 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
Stephan Massaltd021f972020-01-05 20:41:23 -0800260}
261
kyle96c406e2021-02-27 14:07:22 -0800262void AutonomousActor::SendStartingPosition(
263 const frc971::MultiSpline *const spline) {
264 float x = spline->spline_x()->Get(0);
265 float y = spline->spline_y()->Get(0);
266
267 Eigen::Matrix<double, 2, 6> control_points;
268 for (size_t ii = 0; ii < 6; ++ii) {
269 control_points(0, ii) = spline->spline_x()->Get(ii);
270 control_points(1, ii) = spline->spline_y()->Get(ii);
271 }
272
273 frc971::control_loops::drivetrain::Spline spline_object(control_points);
274
275 SendStartingPosition(x, y, spline_object.Theta(0));
276}
277
milind upadhyay47a0ab32020-11-25 19:34:41 -0800278ProfileParametersT MakeProfileParametersT(const float max_velocity,
279 const float max_acceleration) {
280 ProfileParametersT params;
281 params.max_velocity = max_velocity;
282 params.max_acceleration = max_acceleration;
283 return params;
284}
285
286bool AutonomousActor::DriveFwd() {
kyle96c406e2021-02-27 14:07:22 -0800287 SendStartingPosition(0, 0, 0);
Austin Schuhfd1715f2021-01-30 16:58:24 -0800288 const ProfileParametersT kDrive = MakeProfileParametersT(0.3f, 1.0f);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800289 const ProfileParametersT kTurn = MakeProfileParametersT(5.0f, 15.0f);
Austin Schuhfd1715f2021-01-30 16:58:24 -0800290 StartDrive(1.0, 0.0, kDrive, kTurn);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800291 return WaitForDriveDone();
292}
Sabina Leavera0b43b42021-03-03 20:30:04 -0800293
294void AutonomousActor::SendSuperstructureGoal() {
295
296 auto builder = superstructure_goal_sender_.MakeBuilder();
297
298 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
299 intake_offset;
milind upadhyayb9dec712021-03-20 15:47:51 -0700300
Sabina Leavera0b43b42021-03-03 20:30:04 -0800301 {
302 StaticZeroingSingleDOFProfiledSubsystemGoal::Builder intake_builder =
303 builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>();
304
milind upadhyayb9dec712021-03-20 15:47:51 -0700305 frc971::ProfileParameters::Builder profile_params_builder =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800306 builder.MakeBuilder<frc971::ProfileParameters>();
Sabina Leaver8302e3c2021-03-20 16:36:05 -0700307 profile_params_builder.add_max_velocity(10.0);
308 profile_params_builder.add_max_acceleration(30.0);
milind upadhyayb9dec712021-03-20 15:47:51 -0700309 flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800310 profile_params_builder.Finish();
311 intake_builder.add_unsafe_goal(intake_goal_);
312 intake_builder.add_profile_params(profile_params_offset);
313 intake_offset = intake_builder.Finish();
314 }
315
316 superstructure::Goal::Builder superstructure_builder =
317 builder.MakeBuilder<superstructure::Goal>();
milind upadhyayb9dec712021-03-20 15:47:51 -0700318
Sabina Leavera0b43b42021-03-03 20:30:04 -0800319 superstructure_builder.add_intake(intake_offset);
320 superstructure_builder.add_roller_voltage(roller_voltage_);
321 superstructure_builder.add_roller_speed_compensation(kRollerSpeedCompensation);
322
323 if (!builder.Send(superstructure_builder.Finish())) {
324 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
325 }
326
327}
Stephan Massaltd021f972020-01-05 20:41:23 -0800328} // namespace actors
329} // namespace y2020