blob: c2e299913f8200e07ca2cbdbd98226b981766553 [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();
milind upadhyayb2e840a2021-03-27 13:54:49 -070051 RetractIntake();
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080052
James Kuszmaulddd2ba62020-03-08 22:17:13 -070053 joystick_state_fetcher_.Fetch();
54 CHECK(joystick_state_fetcher_.get() != nullptr)
55 << "Expect at least one JoystickState message before running auto...";
56 alliance_ = joystick_state_fetcher_->alliance();
Stephan Massaltd021f972020-01-05 20:41:23 -080057}
58
59bool AutonomousActor::RunAction(
Austin Schuh6fb0a6d2021-01-23 15:43:17 -080060 const ::frc971::autonomous::AutonomousActionParams *params) {
Stephan Massaltd021f972020-01-05 20:41:23 -080061 Reset();
milind upadhyay47a0ab32020-11-25 19:34:41 -080062 AOS_LOG(INFO, "Params are %d\n", params->mode());
James Kuszmaulddd2ba62020-03-08 22:17:13 -070063 if (alliance_ == aos::Alliance::kInvalid) {
64 AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection.");
65 return false;
66 }
kyle96c406e2021-02-27 14:07:22 -080067 if (FLAGS_galactic_search) {
68 GalacticSearch();
Ravago Jones9c326f52021-03-20 15:00:16 -070069 } else if (FLAGS_bounce) {
70 AutoNavBounce();
71 } else if (FLAGS_barrel) {
72 AutoNavBarrel();
73 } else if (FLAGS_slalom) {
74 AutoNavSlalom();
kyle96c406e2021-02-27 14:07:22 -080075 } else if (FLAGS_spline_auto) {
milind upadhyay47a0ab32020-11-25 19:34:41 -080076 SplineAuto();
77 } else {
78 return DriveFwd();
79 }
80 return true;
81}
Stephan Massaltd021f972020-01-05 20:41:23 -080082
kyle96c406e2021-02-27 14:07:22 -080083void AutonomousActor::SendStartingPosition(double x, double y, double theta) {
84 // Set up the starting position for the blue alliance.
85 double starting_heading = theta;
86
87 // TODO(james): Resetting the localizer breaks the left/right statespace
88 // controller. That is a bug, but we can fix that later by not resetting.
89 auto builder = localizer_control_sender_.MakeBuilder();
90
91 LocalizerControl::Builder localizer_control_builder =
92 builder.MakeBuilder<LocalizerControl>();
93 localizer_control_builder.add_x(x);
94 localizer_control_builder.add_y(y);
95 localizer_control_builder.add_theta(starting_heading);
96 localizer_control_builder.add_theta_uncertainty(0.00001);
97 if (!builder.Send(localizer_control_builder.Finish())) {
98 AOS_LOG(ERROR, "Failed to reset localizer.\n");
99 }
100}
101
102void AutonomousActor::GalacticSearch() {
103 path_fetcher_.Fetch();
104 CHECK(path_fetcher_.get() != nullptr)
105 << "Expect at least one GalacticSearchPath message before running "
106 "auto...";
107 if (path_fetcher_->alliance() == y2020::vision::Alliance::kUnknown) {
108 AOS_LOG(ERROR, "The galactic search path is unknown, doing nothing.");
109 } else {
110 SplineHandle spline1 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800111 [this](
112 aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
113 *builder) {
kyle96c406e2021-02-27 14:07:22 -0800114 flatbuffers::Offset<frc971::MultiSpline> target_spline;
115 if (path_fetcher_->alliance() == y2020::vision::Alliance::kRed) {
116 if (path_fetcher_->letter() == y2020::vision::Letter::kA) {
Austin Schuhc8ba53a2021-03-27 13:26:04 -0700117 LOG(INFO) << "Red A";
kyle96c406e2021-02-27 14:07:22 -0800118 target_spline = auto_splines_.SplineRedA(builder);
119 } else {
Austin Schuhc8ba53a2021-03-27 13:26:04 -0700120 LOG(INFO) << "Red B";
kyle96c406e2021-02-27 14:07:22 -0800121 CHECK(path_fetcher_->letter() == y2020::vision::Letter::kB);
122 target_spline = auto_splines_.SplineRedB(builder);
123 }
124 } else {
125 if (path_fetcher_->letter() == y2020::vision::Letter::kA) {
Austin Schuhc8ba53a2021-03-27 13:26:04 -0700126 LOG(INFO) << "Blue A";
kyle96c406e2021-02-27 14:07:22 -0800127 target_spline = auto_splines_.SplineBlueA(builder);
128 } else {
Austin Schuhc8ba53a2021-03-27 13:26:04 -0700129 LOG(INFO) << "Blue B";
kyle96c406e2021-02-27 14:07:22 -0800130 CHECK(path_fetcher_->letter() == y2020::vision::Letter::kB);
131 target_spline = auto_splines_.SplineBlueB(builder);
132 }
133 }
134 const frc971::MultiSpline *const spline =
135 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
136
137 SendStartingPosition(CHECK_NOTNULL(spline));
138
139 return target_spline;
140 },
141 SplineDirection::kForward);
142
milind upadhyayb9dec712021-03-20 15:47:51 -0700143 set_intake_goal(1.2);
144 set_roller_voltage(7.0);
145 SendSuperstructureGoal();
146
kyle96c406e2021-02-27 14:07:22 -0800147 if (!spline1.WaitForPlan()) return;
148 spline1.Start();
149
150 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
milind upadhyay5e589d72021-03-27 13:47:18 -0700151 RetractIntake();
kyle96c406e2021-02-27 14:07:22 -0800152 }
153}
154
Ravago Jones9c326f52021-03-20 15:00:16 -0700155void AutonomousActor::AutoNavBounce() {
156 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() {
205 SplineHandle spline1 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800206 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
Ravago Jones9c326f52021-03-20 15:00:16 -0700207 *builder) {
208 flatbuffers::Offset<frc971::MultiSpline> target_spline;
209 target_spline = auto_splines_.AutoNavBarrel(builder);
210 const frc971::MultiSpline *const spline =
211 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
212 SendStartingPosition(CHECK_NOTNULL(spline));
213 return target_spline;
214 },
215 SplineDirection::kForward);
216
217 if (!spline1.WaitForPlan()) return;
218 spline1.Start();
219
220 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
221}
222
223void AutonomousActor::AutoNavSlalom() {
224 SplineHandle spline1 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800225 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
Ravago Jones9c326f52021-03-20 15:00:16 -0700226 *builder) {
227 flatbuffers::Offset<frc971::MultiSpline> target_spline;
228 target_spline = auto_splines_.AutoNavSlalom(builder);
229 const frc971::MultiSpline *const spline =
230 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
231 SendStartingPosition(CHECK_NOTNULL(spline));
232 return target_spline;
233 },
234 SplineDirection::kForward);
235
236 if (!spline1.WaitForPlan()) return;
237 spline1.Start();
238
239 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
240}
241
milind upadhyay47a0ab32020-11-25 19:34:41 -0800242void AutonomousActor::SplineAuto() {
Ravago Jonesc2a08022021-02-06 17:40:54 -0800243 SplineHandle spline1 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800244 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
kyle96c406e2021-02-27 14:07:22 -0800245 *builder) {
246 flatbuffers::Offset<frc971::MultiSpline> target_spline;
247 target_spline = auto_splines_.TestSpline(builder);
248 const frc971::MultiSpline *const spline =
249 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
250 SendStartingPosition(CHECK_NOTNULL(spline));
251 return target_spline;
252 },
Ravago Jonesc2a08022021-02-06 17:40:54 -0800253 SplineDirection::kForward);
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800254
milind upadhyay47a0ab32020-11-25 19:34:41 -0800255 if (!spline1.WaitForPlan()) return;
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800256 spline1.Start();
257
milind upadhyay47a0ab32020-11-25 19:34:41 -0800258 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
Stephan Massaltd021f972020-01-05 20:41:23 -0800259}
260
kyle96c406e2021-02-27 14:07:22 -0800261void AutonomousActor::SendStartingPosition(
262 const frc971::MultiSpline *const spline) {
263 float x = spline->spline_x()->Get(0);
264 float y = spline->spline_y()->Get(0);
265
266 Eigen::Matrix<double, 2, 6> control_points;
267 for (size_t ii = 0; ii < 6; ++ii) {
268 control_points(0, ii) = spline->spline_x()->Get(ii);
269 control_points(1, ii) = spline->spline_y()->Get(ii);
270 }
271
272 frc971::control_loops::drivetrain::Spline spline_object(control_points);
273
274 SendStartingPosition(x, y, spline_object.Theta(0));
275}
276
milind upadhyay47a0ab32020-11-25 19:34:41 -0800277ProfileParametersT MakeProfileParametersT(const float max_velocity,
278 const float max_acceleration) {
279 ProfileParametersT params;
280 params.max_velocity = max_velocity;
281 params.max_acceleration = max_acceleration;
282 return params;
283}
284
285bool AutonomousActor::DriveFwd() {
kyle96c406e2021-02-27 14:07:22 -0800286 SendStartingPosition(0, 0, 0);
Austin Schuhfd1715f2021-01-30 16:58:24 -0800287 const ProfileParametersT kDrive = MakeProfileParametersT(0.3f, 1.0f);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800288 const ProfileParametersT kTurn = MakeProfileParametersT(5.0f, 15.0f);
Austin Schuhfd1715f2021-01-30 16:58:24 -0800289 StartDrive(1.0, 0.0, kDrive, kTurn);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800290 return WaitForDriveDone();
291}
Sabina Leavera0b43b42021-03-03 20:30:04 -0800292
293void AutonomousActor::SendSuperstructureGoal() {
294
295 auto builder = superstructure_goal_sender_.MakeBuilder();
296
297 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
298 intake_offset;
milind upadhyayb9dec712021-03-20 15:47:51 -0700299
Sabina Leavera0b43b42021-03-03 20:30:04 -0800300 {
301 StaticZeroingSingleDOFProfiledSubsystemGoal::Builder intake_builder =
302 builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>();
303
milind upadhyayb9dec712021-03-20 15:47:51 -0700304 frc971::ProfileParameters::Builder profile_params_builder =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800305 builder.MakeBuilder<frc971::ProfileParameters>();
Sabina Leaver8302e3c2021-03-20 16:36:05 -0700306 profile_params_builder.add_max_velocity(10.0);
307 profile_params_builder.add_max_acceleration(30.0);
milind upadhyayb9dec712021-03-20 15:47:51 -0700308 flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800309 profile_params_builder.Finish();
310 intake_builder.add_unsafe_goal(intake_goal_);
311 intake_builder.add_profile_params(profile_params_offset);
312 intake_offset = intake_builder.Finish();
313 }
314
315 superstructure::Goal::Builder superstructure_builder =
316 builder.MakeBuilder<superstructure::Goal>();
milind upadhyayb9dec712021-03-20 15:47:51 -0700317
Sabina Leavera0b43b42021-03-03 20:30:04 -0800318 superstructure_builder.add_intake(intake_offset);
319 superstructure_builder.add_roller_voltage(roller_voltage_);
320 superstructure_builder.add_roller_speed_compensation(kRollerSpeedCompensation);
321
322 if (!builder.Send(superstructure_builder.Finish())) {
323 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
324 }
325
326}
milind upadhyay5e589d72021-03-27 13:47:18 -0700327
328void AutonomousActor::RetractIntake() {
329 set_intake_goal(-0.89);
330 set_roller_voltage(0.0);
331 SendSuperstructureGoal();
332}
333
Stephan Massaltd021f972020-01-05 20:41:23 -0800334} // namespace actors
335} // namespace y2020