blob: d78f7fc362419696ac7da663f2dbc6ca5dafd5bb [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 upadhyay5e589d72021-03-27 13:47:18 -0700150 RetractIntake();
kyle96c406e2021-02-27 14:07:22 -0800151 }
152}
153
Ravago Jones9c326f52021-03-20 15:00:16 -0700154void AutonomousActor::AutoNavBounce() {
milind upadhyay5e589d72021-03-27 13:47:18 -0700155 RetractIntake();
Ravago Jones9c326f52021-03-20 15:00:16 -0700156 SplineHandle spline1 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800157 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
Ravago Jones9c326f52021-03-20 15:00:16 -0700158 *builder) {
159 flatbuffers::Offset<frc971::MultiSpline> target_spline =
160 auto_splines_.AutoNavBounce1(builder);
161 const frc971::MultiSpline *const spline =
162 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
163 SendStartingPosition(CHECK_NOTNULL(spline));
164 return target_spline;
165 },
166 SplineDirection::kForward);
167
168 if (!spline1.WaitForPlan()) return;
169 spline1.Start();
170
171 SplineHandle spline2 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800172 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
Ravago Jones9c326f52021-03-20 15:00:16 -0700173 *builder) { return auto_splines_.AutoNavBounce2(builder); },
174 SplineDirection::kBackward);
175
176 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
177
178 if (!spline2.WaitForPlan()) return;
179 spline2.Start();
180
181 SplineHandle spline3 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800182 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
Ravago Jones9c326f52021-03-20 15:00:16 -0700183 *builder) { return auto_splines_.AutoNavBounce3(builder); },
184 SplineDirection::kForward);
185
186 if (!spline2.WaitForSplineDistanceRemaining(0.02)) return;
187
188 if (!spline3.WaitForPlan()) return;
189 spline3.Start();
190
191 SplineHandle spline4 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800192 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
Ravago Jones9c326f52021-03-20 15:00:16 -0700193 *builder) { return auto_splines_.AutoNavBounce4(builder); },
194 SplineDirection::kBackward);
195
196 if (!spline3.WaitForSplineDistanceRemaining(0.02)) return;
197
198 if (!spline4.WaitForPlan()) return;
199 spline4.Start();
200
201 if (!spline4.WaitForSplineDistanceRemaining(0.02)) return;
202}
203
204void AutonomousActor::AutoNavBarrel() {
milind upadhyay5e589d72021-03-27 13:47:18 -0700205 RetractIntake();
Ravago Jones9c326f52021-03-20 15:00:16 -0700206 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() {
milind upadhyay5e589d72021-03-27 13:47:18 -0700225 RetractIntake();
Ravago Jones9c326f52021-03-20 15:00:16 -0700226 SplineHandle spline1 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800227 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
Ravago Jones9c326f52021-03-20 15:00:16 -0700228 *builder) {
229 flatbuffers::Offset<frc971::MultiSpline> target_spline;
230 target_spline = auto_splines_.AutoNavSlalom(builder);
231 const frc971::MultiSpline *const spline =
232 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
233 SendStartingPosition(CHECK_NOTNULL(spline));
234 return target_spline;
235 },
236 SplineDirection::kForward);
237
238 if (!spline1.WaitForPlan()) return;
239 spline1.Start();
240
241 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
242}
243
milind upadhyay47a0ab32020-11-25 19:34:41 -0800244void AutonomousActor::SplineAuto() {
milind upadhyay5e589d72021-03-27 13:47:18 -0700245 RetractIntake();
Ravago Jonesc2a08022021-02-06 17:40:54 -0800246 SplineHandle spline1 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800247 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
kyle96c406e2021-02-27 14:07:22 -0800248 *builder) {
249 flatbuffers::Offset<frc971::MultiSpline> target_spline;
250 target_spline = auto_splines_.TestSpline(builder);
251 const frc971::MultiSpline *const spline =
252 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
253 SendStartingPosition(CHECK_NOTNULL(spline));
254 return target_spline;
255 },
Ravago Jonesc2a08022021-02-06 17:40:54 -0800256 SplineDirection::kForward);
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800257
milind upadhyay47a0ab32020-11-25 19:34:41 -0800258 if (!spline1.WaitForPlan()) return;
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800259 spline1.Start();
260
milind upadhyay47a0ab32020-11-25 19:34:41 -0800261 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
Stephan Massaltd021f972020-01-05 20:41:23 -0800262}
263
kyle96c406e2021-02-27 14:07:22 -0800264void AutonomousActor::SendStartingPosition(
265 const frc971::MultiSpline *const spline) {
266 float x = spline->spline_x()->Get(0);
267 float y = spline->spline_y()->Get(0);
268
269 Eigen::Matrix<double, 2, 6> control_points;
270 for (size_t ii = 0; ii < 6; ++ii) {
271 control_points(0, ii) = spline->spline_x()->Get(ii);
272 control_points(1, ii) = spline->spline_y()->Get(ii);
273 }
274
275 frc971::control_loops::drivetrain::Spline spline_object(control_points);
276
277 SendStartingPosition(x, y, spline_object.Theta(0));
278}
279
milind upadhyay47a0ab32020-11-25 19:34:41 -0800280ProfileParametersT MakeProfileParametersT(const float max_velocity,
281 const float max_acceleration) {
282 ProfileParametersT params;
283 params.max_velocity = max_velocity;
284 params.max_acceleration = max_acceleration;
285 return params;
286}
287
288bool AutonomousActor::DriveFwd() {
kyle96c406e2021-02-27 14:07:22 -0800289 SendStartingPosition(0, 0, 0);
Austin Schuhfd1715f2021-01-30 16:58:24 -0800290 const ProfileParametersT kDrive = MakeProfileParametersT(0.3f, 1.0f);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800291 const ProfileParametersT kTurn = MakeProfileParametersT(5.0f, 15.0f);
Austin Schuhfd1715f2021-01-30 16:58:24 -0800292 StartDrive(1.0, 0.0, kDrive, kTurn);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800293 return WaitForDriveDone();
294}
Sabina Leavera0b43b42021-03-03 20:30:04 -0800295
296void AutonomousActor::SendSuperstructureGoal() {
297
298 auto builder = superstructure_goal_sender_.MakeBuilder();
299
300 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
301 intake_offset;
milind upadhyayb9dec712021-03-20 15:47:51 -0700302
Sabina Leavera0b43b42021-03-03 20:30:04 -0800303 {
304 StaticZeroingSingleDOFProfiledSubsystemGoal::Builder intake_builder =
305 builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>();
306
milind upadhyayb9dec712021-03-20 15:47:51 -0700307 frc971::ProfileParameters::Builder profile_params_builder =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800308 builder.MakeBuilder<frc971::ProfileParameters>();
Sabina Leaver8302e3c2021-03-20 16:36:05 -0700309 profile_params_builder.add_max_velocity(10.0);
310 profile_params_builder.add_max_acceleration(30.0);
milind upadhyayb9dec712021-03-20 15:47:51 -0700311 flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800312 profile_params_builder.Finish();
313 intake_builder.add_unsafe_goal(intake_goal_);
314 intake_builder.add_profile_params(profile_params_offset);
315 intake_offset = intake_builder.Finish();
316 }
317
318 superstructure::Goal::Builder superstructure_builder =
319 builder.MakeBuilder<superstructure::Goal>();
milind upadhyayb9dec712021-03-20 15:47:51 -0700320
Sabina Leavera0b43b42021-03-03 20:30:04 -0800321 superstructure_builder.add_intake(intake_offset);
322 superstructure_builder.add_roller_voltage(roller_voltage_);
323 superstructure_builder.add_roller_speed_compensation(kRollerSpeedCompensation);
324
325 if (!builder.Send(superstructure_builder.Finish())) {
326 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
327 }
328
329}
milind upadhyay5e589d72021-03-27 13:47:18 -0700330
331void AutonomousActor::RetractIntake() {
332 set_intake_goal(-0.89);
333 set_roller_voltage(0.0);
334 SendSuperstructureGoal();
335}
336
Stephan Massaltd021f972020-01-05 20:41:23 -0800337} // namespace actors
338} // namespace y2020