blob: d3d85705550b69720d29d0590c82d34d72ff4467 [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>(
43 "/pi1/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) {
116 target_spline = auto_splines_.SplineRedA(builder);
117 } else {
118 CHECK(path_fetcher_->letter() == y2020::vision::Letter::kB);
119 target_spline = auto_splines_.SplineRedB(builder);
120 }
121 } else {
122 if (path_fetcher_->letter() == y2020::vision::Letter::kA) {
123 target_spline = auto_splines_.SplineBlueA(builder);
124 } else {
125 CHECK(path_fetcher_->letter() == y2020::vision::Letter::kB);
126 target_spline = auto_splines_.SplineBlueB(builder);
127 }
128 }
129 const frc971::MultiSpline *const spline =
130 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
131
132 SendStartingPosition(CHECK_NOTNULL(spline));
133
134 return target_spline;
135 },
136 SplineDirection::kForward);
137
milind upadhyayb9dec712021-03-20 15:47:51 -0700138 set_intake_goal(1.2);
139 set_roller_voltage(7.0);
140 SendSuperstructureGoal();
141
kyle96c406e2021-02-27 14:07:22 -0800142 if (!spline1.WaitForPlan()) return;
143 spline1.Start();
144
145 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
milind upadhyayb9dec712021-03-20 15:47:51 -0700146 set_intake_goal(-0.89);
147 set_roller_voltage(0.0);
148 SendSuperstructureGoal();
kyle96c406e2021-02-27 14:07:22 -0800149 }
150}
151
Ravago Jones9c326f52021-03-20 15:00:16 -0700152void AutonomousActor::AutoNavBounce() {
153 SplineHandle spline1 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800154 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
Ravago Jones9c326f52021-03-20 15:00:16 -0700155 *builder) {
156 flatbuffers::Offset<frc971::MultiSpline> target_spline =
157 auto_splines_.AutoNavBounce1(builder);
158 const frc971::MultiSpline *const spline =
159 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
160 SendStartingPosition(CHECK_NOTNULL(spline));
161 return target_spline;
162 },
163 SplineDirection::kForward);
164
165 if (!spline1.WaitForPlan()) return;
166 spline1.Start();
167
168 SplineHandle spline2 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800169 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
Ravago Jones9c326f52021-03-20 15:00:16 -0700170 *builder) { return auto_splines_.AutoNavBounce2(builder); },
171 SplineDirection::kBackward);
172
173 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
174
175 if (!spline2.WaitForPlan()) return;
176 spline2.Start();
177
178 SplineHandle spline3 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800179 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
Ravago Jones9c326f52021-03-20 15:00:16 -0700180 *builder) { return auto_splines_.AutoNavBounce3(builder); },
181 SplineDirection::kForward);
182
183 if (!spline2.WaitForSplineDistanceRemaining(0.02)) return;
184
185 if (!spline3.WaitForPlan()) return;
186 spline3.Start();
187
188 SplineHandle spline4 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800189 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
Ravago Jones9c326f52021-03-20 15:00:16 -0700190 *builder) { return auto_splines_.AutoNavBounce4(builder); },
191 SplineDirection::kBackward);
192
193 if (!spline3.WaitForSplineDistanceRemaining(0.02)) return;
194
195 if (!spline4.WaitForPlan()) return;
196 spline4.Start();
197
198 if (!spline4.WaitForSplineDistanceRemaining(0.02)) return;
199}
200
201void AutonomousActor::AutoNavBarrel() {
202 SplineHandle spline1 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800203 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
Ravago Jones9c326f52021-03-20 15:00:16 -0700204 *builder) {
205 flatbuffers::Offset<frc971::MultiSpline> target_spline;
206 target_spline = auto_splines_.AutoNavBarrel(builder);
207 const frc971::MultiSpline *const spline =
208 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
209 SendStartingPosition(CHECK_NOTNULL(spline));
210 return target_spline;
211 },
212 SplineDirection::kForward);
213
214 if (!spline1.WaitForPlan()) return;
215 spline1.Start();
216
217 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
218}
219
220void AutonomousActor::AutoNavSlalom() {
221 SplineHandle spline1 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800222 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
Ravago Jones9c326f52021-03-20 15:00:16 -0700223 *builder) {
224 flatbuffers::Offset<frc971::MultiSpline> target_spline;
225 target_spline = auto_splines_.AutoNavSlalom(builder);
226 const frc971::MultiSpline *const spline =
227 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
228 SendStartingPosition(CHECK_NOTNULL(spline));
229 return target_spline;
230 },
231 SplineDirection::kForward);
232
233 if (!spline1.WaitForPlan()) return;
234 spline1.Start();
235
236 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
237}
238
milind upadhyay47a0ab32020-11-25 19:34:41 -0800239void AutonomousActor::SplineAuto() {
Ravago Jonesc2a08022021-02-06 17:40:54 -0800240 SplineHandle spline1 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800241 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
kyle96c406e2021-02-27 14:07:22 -0800242 *builder) {
243 flatbuffers::Offset<frc971::MultiSpline> target_spline;
244 target_spline = auto_splines_.TestSpline(builder);
245 const frc971::MultiSpline *const spline =
246 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
247 SendStartingPosition(CHECK_NOTNULL(spline));
248 return target_spline;
249 },
Ravago Jonesc2a08022021-02-06 17:40:54 -0800250 SplineDirection::kForward);
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800251
milind upadhyay47a0ab32020-11-25 19:34:41 -0800252 if (!spline1.WaitForPlan()) return;
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800253 spline1.Start();
254
milind upadhyay47a0ab32020-11-25 19:34:41 -0800255 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
Stephan Massaltd021f972020-01-05 20:41:23 -0800256}
257
kyle96c406e2021-02-27 14:07:22 -0800258void AutonomousActor::SendStartingPosition(
259 const frc971::MultiSpline *const spline) {
260 float x = spline->spline_x()->Get(0);
261 float y = spline->spline_y()->Get(0);
262
263 Eigen::Matrix<double, 2, 6> control_points;
264 for (size_t ii = 0; ii < 6; ++ii) {
265 control_points(0, ii) = spline->spline_x()->Get(ii);
266 control_points(1, ii) = spline->spline_y()->Get(ii);
267 }
268
269 frc971::control_loops::drivetrain::Spline spline_object(control_points);
270
271 SendStartingPosition(x, y, spline_object.Theta(0));
272}
273
milind upadhyay47a0ab32020-11-25 19:34:41 -0800274ProfileParametersT MakeProfileParametersT(const float max_velocity,
275 const float max_acceleration) {
276 ProfileParametersT params;
277 params.max_velocity = max_velocity;
278 params.max_acceleration = max_acceleration;
279 return params;
280}
281
282bool AutonomousActor::DriveFwd() {
kyle96c406e2021-02-27 14:07:22 -0800283 SendStartingPosition(0, 0, 0);
Austin Schuhfd1715f2021-01-30 16:58:24 -0800284 const ProfileParametersT kDrive = MakeProfileParametersT(0.3f, 1.0f);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800285 const ProfileParametersT kTurn = MakeProfileParametersT(5.0f, 15.0f);
Austin Schuhfd1715f2021-01-30 16:58:24 -0800286 StartDrive(1.0, 0.0, kDrive, kTurn);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800287 return WaitForDriveDone();
288}
Sabina Leavera0b43b42021-03-03 20:30:04 -0800289
290void AutonomousActor::SendSuperstructureGoal() {
291
292 auto builder = superstructure_goal_sender_.MakeBuilder();
293
294 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
295 intake_offset;
milind upadhyayb9dec712021-03-20 15:47:51 -0700296
Sabina Leavera0b43b42021-03-03 20:30:04 -0800297 {
298 StaticZeroingSingleDOFProfiledSubsystemGoal::Builder intake_builder =
299 builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>();
300
milind upadhyayb9dec712021-03-20 15:47:51 -0700301 frc971::ProfileParameters::Builder profile_params_builder =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800302 builder.MakeBuilder<frc971::ProfileParameters>();
Sabina Leaver8302e3c2021-03-20 16:36:05 -0700303 profile_params_builder.add_max_velocity(10.0);
304 profile_params_builder.add_max_acceleration(30.0);
milind upadhyayb9dec712021-03-20 15:47:51 -0700305 flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800306 profile_params_builder.Finish();
307 intake_builder.add_unsafe_goal(intake_goal_);
308 intake_builder.add_profile_params(profile_params_offset);
309 intake_offset = intake_builder.Finish();
310 }
311
312 superstructure::Goal::Builder superstructure_builder =
313 builder.MakeBuilder<superstructure::Goal>();
milind upadhyayb9dec712021-03-20 15:47:51 -0700314
Sabina Leavera0b43b42021-03-03 20:30:04 -0800315 superstructure_builder.add_intake(intake_offset);
316 superstructure_builder.add_roller_voltage(roller_voltage_);
317 superstructure_builder.add_roller_speed_compensation(kRollerSpeedCompensation);
318
319 if (!builder.Send(superstructure_builder.Finish())) {
320 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
321 }
322
323}
Stephan Massaltd021f972020-01-05 20:41:23 -0800324} // namespace actors
325} // namespace y2020