blob: c59aa7abe9a34441b83e8ef304df9a23fcea516c [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(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800107 [this](
108 aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
109 *builder) {
kyle96c406e2021-02-27 14:07:22 -0800110 flatbuffers::Offset<frc971::MultiSpline> target_spline;
111 if (path_fetcher_->alliance() == y2020::vision::Alliance::kRed) {
112 if (path_fetcher_->letter() == y2020::vision::Letter::kA) {
113 target_spline = auto_splines_.SplineRedA(builder);
114 } else {
115 CHECK(path_fetcher_->letter() == y2020::vision::Letter::kB);
116 target_spline = auto_splines_.SplineRedB(builder);
117 }
118 } else {
119 if (path_fetcher_->letter() == y2020::vision::Letter::kA) {
120 target_spline = auto_splines_.SplineBlueA(builder);
121 } else {
122 CHECK(path_fetcher_->letter() == y2020::vision::Letter::kB);
123 target_spline = auto_splines_.SplineBlueB(builder);
124 }
125 }
126 const frc971::MultiSpline *const spline =
127 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
128
129 SendStartingPosition(CHECK_NOTNULL(spline));
130
131 return target_spline;
132 },
133 SplineDirection::kForward);
134
milind upadhyayb9dec712021-03-20 15:47:51 -0700135 set_intake_goal(1.2);
136 set_roller_voltage(7.0);
137 SendSuperstructureGoal();
138
kyle96c406e2021-02-27 14:07:22 -0800139 if (!spline1.WaitForPlan()) return;
140 spline1.Start();
141
142 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
milind upadhyayb9dec712021-03-20 15:47:51 -0700143 set_intake_goal(-0.89);
144 set_roller_voltage(0.0);
145 SendSuperstructureGoal();
kyle96c406e2021-02-27 14:07:22 -0800146 }
147}
148
Ravago Jones9c326f52021-03-20 15:00:16 -0700149void AutonomousActor::AutoNavBounce() {
150 SplineHandle spline1 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800151 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
Ravago Jones9c326f52021-03-20 15:00:16 -0700152 *builder) {
153 flatbuffers::Offset<frc971::MultiSpline> target_spline =
154 auto_splines_.AutoNavBounce1(builder);
155 const frc971::MultiSpline *const spline =
156 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
157 SendStartingPosition(CHECK_NOTNULL(spline));
158 return target_spline;
159 },
160 SplineDirection::kForward);
161
162 if (!spline1.WaitForPlan()) return;
163 spline1.Start();
164
165 SplineHandle spline2 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800166 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
Ravago Jones9c326f52021-03-20 15:00:16 -0700167 *builder) { return auto_splines_.AutoNavBounce2(builder); },
168 SplineDirection::kBackward);
169
170 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
171
172 if (!spline2.WaitForPlan()) return;
173 spline2.Start();
174
175 SplineHandle spline3 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800176 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
Ravago Jones9c326f52021-03-20 15:00:16 -0700177 *builder) { return auto_splines_.AutoNavBounce3(builder); },
178 SplineDirection::kForward);
179
180 if (!spline2.WaitForSplineDistanceRemaining(0.02)) return;
181
182 if (!spline3.WaitForPlan()) return;
183 spline3.Start();
184
185 SplineHandle spline4 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800186 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
Ravago Jones9c326f52021-03-20 15:00:16 -0700187 *builder) { return auto_splines_.AutoNavBounce4(builder); },
188 SplineDirection::kBackward);
189
190 if (!spline3.WaitForSplineDistanceRemaining(0.02)) return;
191
192 if (!spline4.WaitForPlan()) return;
193 spline4.Start();
194
195 if (!spline4.WaitForSplineDistanceRemaining(0.02)) return;
196}
197
198void AutonomousActor::AutoNavBarrel() {
199 SplineHandle spline1 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800200 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
Ravago Jones9c326f52021-03-20 15:00:16 -0700201 *builder) {
202 flatbuffers::Offset<frc971::MultiSpline> target_spline;
203 target_spline = auto_splines_.AutoNavBarrel(builder);
204 const frc971::MultiSpline *const spline =
205 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
206 SendStartingPosition(CHECK_NOTNULL(spline));
207 return target_spline;
208 },
209 SplineDirection::kForward);
210
211 if (!spline1.WaitForPlan()) return;
212 spline1.Start();
213
214 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
215}
216
217void AutonomousActor::AutoNavSlalom() {
218 SplineHandle spline1 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800219 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
Ravago Jones9c326f52021-03-20 15:00:16 -0700220 *builder) {
221 flatbuffers::Offset<frc971::MultiSpline> target_spline;
222 target_spline = auto_splines_.AutoNavSlalom(builder);
223 const frc971::MultiSpline *const spline =
224 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
225 SendStartingPosition(CHECK_NOTNULL(spline));
226 return target_spline;
227 },
228 SplineDirection::kForward);
229
230 if (!spline1.WaitForPlan()) return;
231 spline1.Start();
232
233 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
234}
235
milind upadhyay47a0ab32020-11-25 19:34:41 -0800236void AutonomousActor::SplineAuto() {
Ravago Jonesc2a08022021-02-06 17:40:54 -0800237 SplineHandle spline1 = PlanSpline(
James Kuszmaul75a18c52021-03-10 22:02:07 -0800238 [this](aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
kyle96c406e2021-02-27 14:07:22 -0800239 *builder) {
240 flatbuffers::Offset<frc971::MultiSpline> target_spline;
241 target_spline = auto_splines_.TestSpline(builder);
242 const frc971::MultiSpline *const spline =
243 flatbuffers::GetTemporaryPointer(*builder->fbb(), target_spline);
244 SendStartingPosition(CHECK_NOTNULL(spline));
245 return target_spline;
246 },
Ravago Jonesc2a08022021-02-06 17:40:54 -0800247 SplineDirection::kForward);
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800248
milind upadhyay47a0ab32020-11-25 19:34:41 -0800249 if (!spline1.WaitForPlan()) return;
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800250 spline1.Start();
251
milind upadhyay47a0ab32020-11-25 19:34:41 -0800252 if (!spline1.WaitForSplineDistanceRemaining(0.02)) return;
Stephan Massaltd021f972020-01-05 20:41:23 -0800253}
254
kyle96c406e2021-02-27 14:07:22 -0800255void AutonomousActor::SendStartingPosition(
256 const frc971::MultiSpline *const spline) {
257 float x = spline->spline_x()->Get(0);
258 float y = spline->spline_y()->Get(0);
259
260 Eigen::Matrix<double, 2, 6> control_points;
261 for (size_t ii = 0; ii < 6; ++ii) {
262 control_points(0, ii) = spline->spline_x()->Get(ii);
263 control_points(1, ii) = spline->spline_y()->Get(ii);
264 }
265
266 frc971::control_loops::drivetrain::Spline spline_object(control_points);
267
268 SendStartingPosition(x, y, spline_object.Theta(0));
269}
270
milind upadhyay47a0ab32020-11-25 19:34:41 -0800271ProfileParametersT MakeProfileParametersT(const float max_velocity,
272 const float max_acceleration) {
273 ProfileParametersT params;
274 params.max_velocity = max_velocity;
275 params.max_acceleration = max_acceleration;
276 return params;
277}
278
279bool AutonomousActor::DriveFwd() {
kyle96c406e2021-02-27 14:07:22 -0800280 SendStartingPosition(0, 0, 0);
Austin Schuhfd1715f2021-01-30 16:58:24 -0800281 const ProfileParametersT kDrive = MakeProfileParametersT(0.3f, 1.0f);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800282 const ProfileParametersT kTurn = MakeProfileParametersT(5.0f, 15.0f);
Austin Schuhfd1715f2021-01-30 16:58:24 -0800283 StartDrive(1.0, 0.0, kDrive, kTurn);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800284 return WaitForDriveDone();
285}
Sabina Leavera0b43b42021-03-03 20:30:04 -0800286
287void AutonomousActor::SendSuperstructureGoal() {
288
289 auto builder = superstructure_goal_sender_.MakeBuilder();
290
291 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
292 intake_offset;
milind upadhyayb9dec712021-03-20 15:47:51 -0700293
Sabina Leavera0b43b42021-03-03 20:30:04 -0800294 {
295 StaticZeroingSingleDOFProfiledSubsystemGoal::Builder intake_builder =
296 builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>();
297
milind upadhyayb9dec712021-03-20 15:47:51 -0700298 frc971::ProfileParameters::Builder profile_params_builder =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800299 builder.MakeBuilder<frc971::ProfileParameters>();
Sabina Leaver8302e3c2021-03-20 16:36:05 -0700300 profile_params_builder.add_max_velocity(10.0);
301 profile_params_builder.add_max_acceleration(30.0);
milind upadhyayb9dec712021-03-20 15:47:51 -0700302 flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800303 profile_params_builder.Finish();
304 intake_builder.add_unsafe_goal(intake_goal_);
305 intake_builder.add_profile_params(profile_params_offset);
306 intake_offset = intake_builder.Finish();
307 }
308
309 superstructure::Goal::Builder superstructure_builder =
310 builder.MakeBuilder<superstructure::Goal>();
milind upadhyayb9dec712021-03-20 15:47:51 -0700311
Sabina Leavera0b43b42021-03-03 20:30:04 -0800312 superstructure_builder.add_intake(intake_offset);
313 superstructure_builder.add_roller_voltage(roller_voltage_);
314 superstructure_builder.add_roller_speed_compensation(kRollerSpeedCompensation);
315
316 if (!builder.Send(superstructure_builder.Finish())) {
317 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
318 }
319
320}
Stephan Massaltd021f972020-01-05 20:41:23 -0800321} // namespace actors
322} // namespace y2020