blob: 03476d94af595129a69ffcef0b22d2ceb9583f65 [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")),
James Kuszmaulddd2ba62020-03-08 22:17:13 -070037 joystick_state_fetcher_(
Ravago Jonesc2a08022021-02-06 17:40:54 -080038 event_loop->MakeFetcher<aos::JoystickState>("/aos")),
kyle96c406e2021-02-27 14:07:22 -080039 path_fetcher_(event_loop->MakeFetcher<y2020::vision::GalacticSearchPath>(
40 "/pi1/camera")),
Ravago Jonesc2a08022021-02-06 17:40:54 -080041 auto_splines_() {
milind upadhyay47a0ab32020-11-25 19:34:41 -080042 set_max_drivetrain_voltage(2.0);
43}
Stephan Massaltd021f972020-01-05 20:41:23 -080044
45void AutonomousActor::Reset() {
46 InitializeEncoders();
47 ResetDrivetrain();
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080048
James Kuszmaulddd2ba62020-03-08 22:17:13 -070049 joystick_state_fetcher_.Fetch();
50 CHECK(joystick_state_fetcher_.get() != nullptr)
51 << "Expect at least one JoystickState message before running auto...";
52 alliance_ = joystick_state_fetcher_->alliance();
Stephan Massaltd021f972020-01-05 20:41:23 -080053}
54
55bool AutonomousActor::RunAction(
Austin Schuh6fb0a6d2021-01-23 15:43:17 -080056 const ::frc971::autonomous::AutonomousActionParams *params) {
Stephan Massaltd021f972020-01-05 20:41:23 -080057 Reset();
milind upadhyay47a0ab32020-11-25 19:34:41 -080058 AOS_LOG(INFO, "Params are %d\n", params->mode());
James Kuszmaulddd2ba62020-03-08 22:17:13 -070059 if (alliance_ == aos::Alliance::kInvalid) {
60 AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection.");
61 return false;
62 }
kyle96c406e2021-02-27 14:07:22 -080063 if (FLAGS_galactic_search) {
64 GalacticSearch();
Ravago Jones9c326f52021-03-20 15:00:16 -070065 } else if (FLAGS_bounce) {
66 AutoNavBounce();
67 } else if (FLAGS_barrel) {
68 AutoNavBarrel();
69 } else if (FLAGS_slalom) {
70 AutoNavSlalom();
kyle96c406e2021-02-27 14:07:22 -080071 } else if (FLAGS_spline_auto) {
milind upadhyay47a0ab32020-11-25 19:34:41 -080072 SplineAuto();
73 } else {
74 return DriveFwd();
75 }
76 return true;
77}
Stephan Massaltd021f972020-01-05 20:41:23 -080078
kyle96c406e2021-02-27 14:07:22 -080079void AutonomousActor::SendStartingPosition(double x, double y, double theta) {
80 // Set up the starting position for the blue alliance.
81 double starting_heading = theta;
82
83 // TODO(james): Resetting the localizer breaks the left/right statespace
84 // controller. That is a bug, but we can fix that later by not resetting.
85 auto builder = localizer_control_sender_.MakeBuilder();
86
87 LocalizerControl::Builder localizer_control_builder =
88 builder.MakeBuilder<LocalizerControl>();
89 localizer_control_builder.add_x(x);
90 localizer_control_builder.add_y(y);
91 localizer_control_builder.add_theta(starting_heading);
92 localizer_control_builder.add_theta_uncertainty(0.00001);
93 if (!builder.Send(localizer_control_builder.Finish())) {
94 AOS_LOG(ERROR, "Failed to reset localizer.\n");
95 }
96}
97
98void AutonomousActor::GalacticSearch() {
99 path_fetcher_.Fetch();
100 CHECK(path_fetcher_.get() != nullptr)
101 << "Expect at least one GalacticSearchPath message before running "
102 "auto...";
103 if (path_fetcher_->alliance() == y2020::vision::Alliance::kUnknown) {
104 AOS_LOG(ERROR, "The galactic search path is unknown, doing nothing.");
105 } else {
106 SplineHandle spline1 = PlanSpline(
107 [this](aos::Sender<frc971::control_loops::drivetrain::Goal>::Builder
108 *builder) {
109 flatbuffers::Offset<frc971::MultiSpline> target_spline;
110 if (path_fetcher_->alliance() == y2020::vision::Alliance::kRed) {
111 if (path_fetcher_->letter() == y2020::vision::Letter::kA) {
112 target_spline = auto_splines_.SplineRedA(builder);
113 } else {
114 CHECK(path_fetcher_->letter() == y2020::vision::Letter::kB);
115 target_spline = auto_splines_.SplineRedB(builder);
116 }
117 } else {
118 if (path_fetcher_->letter() == y2020::vision::Letter::kA) {
119 target_spline = auto_splines_.SplineBlueA(builder);
120 } else {
121 CHECK(path_fetcher_->letter() == y2020::vision::Letter::kB);
122 target_spline = auto_splines_.SplineBlueB(builder);
123 }
124 }
125 const frc971::MultiSpline *const spline =
126 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
127
128 SendStartingPosition(CHECK_NOTNULL(spline));
129
130 return target_spline;
131 },
132 SplineDirection::kForward);
133
milind upadhyayb9dec712021-03-20 15:47:51 -0700134 set_intake_goal(1.2);
135 set_roller_voltage(7.0);
136 SendSuperstructureGoal();
137
kyle96c406e2021-02-27 14:07:22 -0800138 if (!spline1.WaitForPlan()) return;
139 spline1.Start();
140
141 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
milind upadhyayb9dec712021-03-20 15:47:51 -0700142 set_intake_goal(-0.89);
143 set_roller_voltage(0.0);
144 SendSuperstructureGoal();
kyle96c406e2021-02-27 14:07:22 -0800145 }
146}
147
Ravago Jones9c326f52021-03-20 15:00:16 -0700148void AutonomousActor::AutoNavBounce() {
149 SplineHandle spline1 = PlanSpline(
150 [this](aos::Sender<frc971::control_loops::drivetrain::Goal>::Builder
151 *builder) {
152 flatbuffers::Offset<frc971::MultiSpline> target_spline =
153 auto_splines_.AutoNavBounce1(builder);
154 const frc971::MultiSpline *const spline =
155 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
156 SendStartingPosition(CHECK_NOTNULL(spline));
157 return target_spline;
158 },
159 SplineDirection::kForward);
160
161 if (!spline1.WaitForPlan()) return;
162 spline1.Start();
163
164 SplineHandle spline2 = PlanSpline(
165 [this](aos::Sender<frc971::control_loops::drivetrain::Goal>::Builder
166 *builder) { return auto_splines_.AutoNavBounce2(builder); },
167 SplineDirection::kBackward);
168
169 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
170
171 if (!spline2.WaitForPlan()) return;
172 spline2.Start();
173
174 SplineHandle spline3 = PlanSpline(
175 [this](aos::Sender<frc971::control_loops::drivetrain::Goal>::Builder
176 *builder) { return auto_splines_.AutoNavBounce3(builder); },
177 SplineDirection::kForward);
178
179 if (!spline2.WaitForSplineDistanceRemaining(0.02)) return;
180
181 if (!spline3.WaitForPlan()) return;
182 spline3.Start();
183
184 SplineHandle spline4 = PlanSpline(
185 [this](aos::Sender<frc971::control_loops::drivetrain::Goal>::Builder
186 *builder) { return auto_splines_.AutoNavBounce4(builder); },
187 SplineDirection::kBackward);
188
189 if (!spline3.WaitForSplineDistanceRemaining(0.02)) return;
190
191 if (!spline4.WaitForPlan()) return;
192 spline4.Start();
193
194 if (!spline4.WaitForSplineDistanceRemaining(0.02)) return;
195}
196
197void AutonomousActor::AutoNavBarrel() {
198 SplineHandle spline1 = PlanSpline(
199 [this](aos::Sender<frc971::control_loops::drivetrain::Goal>::Builder
200 *builder) {
201 flatbuffers::Offset<frc971::MultiSpline> target_spline;
202 target_spline = auto_splines_.AutoNavBarrel(builder);
203 const frc971::MultiSpline *const spline =
204 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
205 SendStartingPosition(CHECK_NOTNULL(spline));
206 return target_spline;
207 },
208 SplineDirection::kForward);
209
210 if (!spline1.WaitForPlan()) return;
211 spline1.Start();
212
213 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
214}
215
216void AutonomousActor::AutoNavSlalom() {
217 SplineHandle spline1 = PlanSpline(
218 [this](aos::Sender<frc971::control_loops::drivetrain::Goal>::Builder
219 *builder) {
220 flatbuffers::Offset<frc971::MultiSpline> target_spline;
221 target_spline = auto_splines_.AutoNavSlalom(builder);
222 const frc971::MultiSpline *const spline =
223 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
224 SendStartingPosition(CHECK_NOTNULL(spline));
225 return target_spline;
226 },
227 SplineDirection::kForward);
228
229 if (!spline1.WaitForPlan()) return;
230 spline1.Start();
231
232 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
233}
234
milind upadhyay47a0ab32020-11-25 19:34:41 -0800235void AutonomousActor::SplineAuto() {
Ravago Jonesc2a08022021-02-06 17:40:54 -0800236 SplineHandle spline1 = PlanSpline(
237 [this](aos::Sender<frc971::control_loops::drivetrain::Goal>::Builder
kyle96c406e2021-02-27 14:07:22 -0800238 *builder) {
239 flatbuffers::Offset<frc971::MultiSpline> target_spline;
240 target_spline = auto_splines_.TestSpline(builder);
241 const frc971::MultiSpline *const spline =
242 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
243 SendStartingPosition(CHECK_NOTNULL(spline));
244 return target_spline;
245 },
Ravago Jonesc2a08022021-02-06 17:40:54 -0800246 SplineDirection::kForward);
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800247
milind upadhyay47a0ab32020-11-25 19:34:41 -0800248 if (!spline1.WaitForPlan()) return;
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800249 spline1.Start();
250
milind upadhyay47a0ab32020-11-25 19:34:41 -0800251 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
Stephan Massaltd021f972020-01-05 20:41:23 -0800252}
253
kyle96c406e2021-02-27 14:07:22 -0800254void AutonomousActor::SendStartingPosition(
255 const frc971::MultiSpline *const spline) {
256 float x = spline->spline_x()->Get(0);
257 float y = spline->spline_y()->Get(0);
258
259 Eigen::Matrix<double, 2, 6> control_points;
260 for (size_t ii = 0; ii < 6; ++ii) {
261 control_points(0, ii) = spline->spline_x()->Get(ii);
262 control_points(1, ii) = spline->spline_y()->Get(ii);
263 }
264
265 frc971::control_loops::drivetrain::Spline spline_object(control_points);
266
267 SendStartingPosition(x, y, spline_object.Theta(0));
268}
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() {
kyle96c406e2021-02-27 14:07:22 -0800279 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() {
287
288 auto builder = superstructure_goal_sender_.MakeBuilder();
289
290 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
291 intake_offset;
milind upadhyayb9dec712021-03-20 15:47:51 -0700292
Sabina Leavera0b43b42021-03-03 20:30:04 -0800293 {
294 StaticZeroingSingleDOFProfiledSubsystemGoal::Builder intake_builder =
295 builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>();
296
milind upadhyayb9dec712021-03-20 15:47:51 -0700297 frc971::ProfileParameters::Builder profile_params_builder =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800298 builder.MakeBuilder<frc971::ProfileParameters>();
299 profile_params_builder.add_max_velocity(0.0);
300 profile_params_builder.add_max_acceleration(0.0);
milind upadhyayb9dec712021-03-20 15:47:51 -0700301 flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800302 profile_params_builder.Finish();
303 intake_builder.add_unsafe_goal(intake_goal_);
304 intake_builder.add_profile_params(profile_params_offset);
305 intake_offset = intake_builder.Finish();
306 }
307
308 superstructure::Goal::Builder superstructure_builder =
309 builder.MakeBuilder<superstructure::Goal>();
milind upadhyayb9dec712021-03-20 15:47:51 -0700310
Sabina Leavera0b43b42021-03-03 20:30:04 -0800311 superstructure_builder.add_intake(intake_offset);
312 superstructure_builder.add_roller_voltage(roller_voltage_);
313 superstructure_builder.add_roller_speed_compensation(kRollerSpeedCompensation);
314
315 if (!builder.Send(superstructure_builder.Finish())) {
316 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
317 }
318
319}
Stephan Massaltd021f972020-01-05 20:41:23 -0800320} // namespace actors
321} // namespace y2020