blob: 46a7542b19a3df7fbdf85b07621d719b3605f141 [file] [log] [blame]
Stephan Massaltd021f972020-01-05 20:41:23 -08001#include "y2020/actors/autonomous_actor.h"
2
Stephan Massaltd021f972020-01-05 20:41:23 -08003#include <chrono>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07004#include <cinttypes>
Stephan Massaltd021f972020-01-05 20:41:23 -08005#include <cmath>
6
7#include "aos/logging/logging.h"
James Kuszmaulddd2ba62020-03-08 22:17:13 -07008#include "aos/util/math.h"
Stephan Massaltd021f972020-01-05 20:41:23 -08009#include "frc971/control_loops/drivetrain/localizer_generated.h"
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080010#include "y2020/actors/auto_splines.h"
Ravago Jonesc2a08022021-02-06 17:40:54 -080011#include "y2020/control_loops/drivetrain/drivetrain_base.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080012
Austin Schuh3fb9e422021-03-31 20:11:32 -070013DEFINE_bool(spline_auto, false, "If true, define a spline autonomous mode");
Austin Schuhd0e9e062021-10-24 17:40:58 -070014DEFINE_bool(target_aligned, true,
Ravago Jonesa7b3c822021-08-26 12:36:03 -070015 "If true, run the Infinite Recharge autonomous that starts aligned "
16 "with the target");
Ravago Jones8c5737e2021-10-16 15:36:12 -070017DEFINE_bool(target_offset, false,
Ravago Jonesa7b3c822021-08-26 12:36:03 -070018 "If true, run the Infinite Recharge autonomous that starts offset "
19 "from the target");
20DEFINE_bool(just_shoot, false,
21 "If true, run the autonomous that just shoots balls.");
milind upadhyay47a0ab32020-11-25 19:34:41 -080022
Stephan Massaltd021f972020-01-05 20:41:23 -080023namespace y2020 {
24namespace actors {
25
26using ::aos::monotonic_clock;
27using ::frc971::ProfileParametersT;
28using frc971::control_loops::drivetrain::LocalizerControl;
29namespace chrono = ::std::chrono;
30
31AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
32 : frc971::autonomous::BaseAutonomousActor(
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080033 event_loop, control_loops::drivetrain::GetDrivetrainConfig()),
Ravago Jonesc2a08022021-02-06 17:40:54 -080034 localizer_control_sender_(
35 event_loop->MakeSender<
36 ::frc971::control_loops::drivetrain::LocalizerControl>(
37 "/drivetrain")),
Austin Schuh67e127e2021-03-27 13:25:23 -070038 superstructure_goal_sender_(
39 event_loop->MakeSender<control_loops::superstructure::Goal>(
40 "/superstructure")),
Ravago Jones1f32d622021-08-26 12:20:36 -070041 superstructure_status_fetcher_(
42 event_loop->MakeFetcher<y2020::control_loops::superstructure::Status>(
43 "/superstructure")),
Austin Schuhba77b7e2021-10-25 21:58:54 -070044 joystick_state_fetcher_(
45 event_loop->MakeFetcher<aos::JoystickState>("/aos")),
46 robot_state_fetcher_(event_loop->MakeFetcher<aos::RobotState>("/aos")),
Ravago Jonesc2a08022021-02-06 17:40:54 -080047 auto_splines_() {
Austin Schuhd0e9e062021-10-24 17:40:58 -070048 set_max_drivetrain_voltage(12.0);
James Kuszmaul99af8b52021-03-28 10:50:15 -070049 replan_timer_ = event_loop->AddTimer([this]() { Replan(); });
50 event_loop->OnRun([this, event_loop]() {
51 replan_timer_->Setup(event_loop->monotonic_now());
Austin Schuhba77b7e2021-10-25 21:58:54 -070052 button_poll_->Setup(event_loop->monotonic_now(), chrono::milliseconds(50));
James Kuszmaul99af8b52021-03-28 10:50:15 -070053 });
Austin Schuhba77b7e2021-10-25 21:58:54 -070054
55 button_poll_ = event_loop->AddTimer([this]() {
56 if (robot_state_fetcher_.Fetch()) {
57 if (robot_state_fetcher_->user_button()) {
58 user_indicated_safe_to_reset_ = true;
59 MaybeSendStartingPosition();
60 }
James Kuszmaul4303a5d2021-10-23 19:58:48 -070061 }
Austin Schuhba77b7e2021-10-25 21:58:54 -070062 if (joystick_state_fetcher_.Fetch()) {
63 if (joystick_state_fetcher_->has_alliance() &&
64 (joystick_state_fetcher_->alliance() != alliance_)) {
65 alliance_ = joystick_state_fetcher_->alliance();
66 Replan();
67 }
James Kuszmaul4303a5d2021-10-23 19:58:48 -070068 }
69 });
70}
71
72void AutonomousActor::MaybeSendStartingPosition() {
73 if (user_indicated_safe_to_reset_ && !sent_starting_position_) {
74 CHECK(starting_position_);
75 SendStartingPosition(starting_position_.value());
76 }
James Kuszmaul99af8b52021-03-28 10:50:15 -070077}
78
79void AutonomousActor::Replan() {
James Kuszmaul4303a5d2021-10-23 19:58:48 -070080 sent_starting_position_ = false;
Austin Schuhd0e9e062021-10-24 17:40:58 -070081 if (alliance_ == aos::Alliance::kInvalid) {
82 return;
83 }
Ravago Jones8c5737e2021-10-16 15:36:12 -070084 if (FLAGS_spline_auto) {
Tyler Chatowbf0609c2021-07-31 16:13:27 -070085 test_spline_ = PlanSpline(std::bind(&AutonomousSplines::TestSpline,
86 &auto_splines_, std::placeholders::_1),
87 SplineDirection::kForward);
James Kuszmaul4303a5d2021-10-23 19:58:48 -070088 starting_position_ = test_spline_->starting_position();
Austin Schuhd0e9e062021-10-24 17:40:58 -070089 } else if (FLAGS_target_aligned) {
90 target_aligned_splines_ = {
91 PlanSpline(std::bind(&AutonomousSplines::TargetAligned1, &auto_splines_,
92 std::placeholders::_1, alliance_),
93 SplineDirection::kForward),
94 PlanSpline(std::bind(&AutonomousSplines::TargetAligned2, &auto_splines_,
95 std::placeholders::_1, alliance_),
96 SplineDirection::kBackward)};
97 starting_position_ = target_aligned_splines_.value()[0].starting_position();
Ravago Jones8c5737e2021-10-16 15:36:12 -070098 } else if (FLAGS_target_offset) {
Ravago Jonesa7b3c822021-08-26 12:36:03 -070099 target_offset_splines_ = {
100 PlanSpline(std::bind(&AutonomousSplines::TargetOffset1, &auto_splines_,
101 std::placeholders::_1),
102 SplineDirection::kForward),
103 PlanSpline(std::bind(&AutonomousSplines::TargetOffset2, &auto_splines_,
104 std::placeholders::_1),
105 SplineDirection::kBackward)};
James Kuszmaul4303a5d2021-10-23 19:58:48 -0700106 starting_position_ = target_offset_splines_.value()[0].starting_position();
James Kuszmaul4303a5d2021-10-23 19:58:48 -0700107 } else {
108 starting_position_ = Eigen::Vector3d::Zero();
James Kuszmaul99af8b52021-03-28 10:50:15 -0700109 }
James Kuszmaul4303a5d2021-10-23 19:58:48 -0700110 MaybeSendStartingPosition();
milind upadhyay47a0ab32020-11-25 19:34:41 -0800111}
Stephan Massaltd021f972020-01-05 20:41:23 -0800112
113void AutonomousActor::Reset() {
114 InitializeEncoders();
115 ResetDrivetrain();
milind upadhyayb2e840a2021-03-27 13:54:49 -0700116 RetractIntake();
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800117
James Kuszmaulddd2ba62020-03-08 22:17:13 -0700118 joystick_state_fetcher_.Fetch();
119 CHECK(joystick_state_fetcher_.get() != nullptr)
120 << "Expect at least one JoystickState message before running auto...";
121 alliance_ = joystick_state_fetcher_->alliance();
Stephan Massaltd021f972020-01-05 20:41:23 -0800122}
123
124bool AutonomousActor::RunAction(
Austin Schuh6fb0a6d2021-01-23 15:43:17 -0800125 const ::frc971::autonomous::AutonomousActionParams *params) {
Stephan Massaltd021f972020-01-05 20:41:23 -0800126 Reset();
James Kuszmaul4303a5d2021-10-23 19:58:48 -0700127 if (!user_indicated_safe_to_reset_) {
128 AOS_LOG(WARNING, "Didn't send starting position prior to starting auto.");
129 SendStartingPosition(starting_position_.value());
130 }
James Kuszmaul99af8b52021-03-28 10:50:15 -0700131
132 // Queue up a replan to occur as soon as this action completes.
133 // TODO(james): Modify this so we don't replan during teleop.
134 replan_timer_->Setup(monotonic_now());
135
milind upadhyay47a0ab32020-11-25 19:34:41 -0800136 AOS_LOG(INFO, "Params are %d\n", params->mode());
James Kuszmaulddd2ba62020-03-08 22:17:13 -0700137 if (alliance_ == aos::Alliance::kInvalid) {
138 AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection.");
139 return false;
140 }
Austin Schuhd0e9e062021-10-24 17:40:58 -0700141 if (FLAGS_spline_auto) {
142 SplineAuto();
143 } else if (FLAGS_target_aligned) {
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700144 TargetAligned();
Ravago Jones8c5737e2021-10-16 15:36:12 -0700145 } else if (FLAGS_target_offset) {
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700146 TargetOffset();
147 } else if (FLAGS_just_shoot) {
148 JustShoot();
milind upadhyay47a0ab32020-11-25 19:34:41 -0800149 } else {
150 return DriveFwd();
151 }
152 return true;
153}
Stephan Massaltd021f972020-01-05 20:41:23 -0800154
James Kuszmaul99af8b52021-03-28 10:50:15 -0700155void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) {
kyle96c406e2021-02-27 14:07:22 -0800156 // Set up the starting position for the blue alliance.
kyle96c406e2021-02-27 14:07:22 -0800157
158 // TODO(james): Resetting the localizer breaks the left/right statespace
159 // controller. That is a bug, but we can fix that later by not resetting.
160 auto builder = localizer_control_sender_.MakeBuilder();
161
162 LocalizerControl::Builder localizer_control_builder =
163 builder.MakeBuilder<LocalizerControl>();
James Kuszmaul99af8b52021-03-28 10:50:15 -0700164 localizer_control_builder.add_x(start(0));
165 localizer_control_builder.add_y(start(1));
166 localizer_control_builder.add_theta(start(2));
kyle96c406e2021-02-27 14:07:22 -0800167 localizer_control_builder.add_theta_uncertainty(0.00001);
168 if (!builder.Send(localizer_control_builder.Finish())) {
169 AOS_LOG(ERROR, "Failed to reset localizer.\n");
170 }
171}
172
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700173void AutonomousActor::TargetAligned() {
Austin Schuhd0e9e062021-10-24 17:40:58 -0700174 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700175 CHECK(target_aligned_splines_);
176 auto &splines = *target_aligned_splines_;
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700177
Austin Schuhd0e9e062021-10-24 17:40:58 -0700178 // Spin up.
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700179 set_shooting(true);
180 SendSuperstructureGoal();
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700181 if (!WaitForBallsShot(3)) return;
182
183 set_shooting(false);
Austin Schuhd0e9e062021-10-24 17:40:58 -0700184 ExtendIntake();
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700185 SendSuperstructureGoal();
186
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700187 if (!splines[0].WaitForPlan()) return;
188 splines[0].Start();
189
190 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
Austin Schuhd0e9e062021-10-24 17:40:58 -0700191 std::this_thread::sleep_for(chrono::milliseconds(200));
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700192 RetractIntake();
193
194 if (!splines[1].WaitForPlan()) return;
195 splines[1].Start();
196
Austin Schuhd0e9e062021-10-24 17:40:58 -0700197 if (!splines[1].WaitForSplineDistanceRemaining(2.0)) return;
198 // Reverse the rollers for a moment to try to unjam any jammed balls. Since
199 // we are moving here, this is free to try.
200 set_roller_voltage(-12.0);
201 std::this_thread::sleep_for(chrono::milliseconds(300));
202 set_roller_voltage(0.0);
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700203
Austin Schuhd0e9e062021-10-24 17:40:58 -0700204 // Once we come to a stop, give the robot a moment to settle down. This makes
205 // the shot more accurate.
206 if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return;
207 std::this_thread::sleep_for(chrono::milliseconds(1500));
208 set_shooting(true);
209 const int balls = Balls();
210
211 SendSuperstructureGoal();
212
213 std::this_thread::sleep_for(chrono::milliseconds(1500));
214
215 // We have been seeing balls get stuck on the intake roller. Reverse the
216 // roller again for a moment to unjam it.
217 set_shooting(false);
218 set_roller_voltage(-12.0);
219 SendSuperstructureGoal();
220 std::this_thread::sleep_for(chrono::milliseconds(250));
221
222 set_roller_voltage(0.0);
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700223 set_shooting(true);
224 SendSuperstructureGoal();
225
Austin Schuhd0e9e062021-10-24 17:40:58 -0700226 if (!WaitUntilAbsoluteBallsShot(3 + balls)) return;
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700227
Austin Schuhd0e9e062021-10-24 17:40:58 -0700228 LOG(INFO) << "Took "
229 << chrono::duration<double>(aos::monotonic_clock::now() -
230 start_time)
231 .count();
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700232}
233
234void AutonomousActor::JustShoot() {
235 // shoot pre-loaded balls
236 set_shooter_tracking(true);
237 set_shooting(true);
238 SendSuperstructureGoal();
239
240 if (!WaitForBallsShot(3)) return;
241
242 set_shooting(false);
243 set_shooter_tracking(true);
244 SendSuperstructureGoal();
245}
246
247void AutonomousActor::TargetOffset() {
248 CHECK(target_offset_splines_);
249 auto &splines = *target_offset_splines_;
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700250
251 // spin up shooter
252 set_shooter_tracking(true);
253 SendSuperstructureGoal();
254 ExtendIntake();
255
256 // pickup 2 more balls in front of the trench run
257 if (!splines[0].WaitForPlan()) return;
258 splines[0].Start();
259
260 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
261 RetractIntake();
262
263 if (!splines[1].WaitForPlan()) return;
264 splines[1].Start();
265
266 if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return;
267
268 // shoot the balls from in front of the goal.
269 set_shooting(true);
270 SendSuperstructureGoal();
271
272 if (!WaitForBallsShot(5)) return;
273
274 set_shooting(false);
275 set_shooter_tracking(false);
276 SendSuperstructureGoal();
277}
278
milind upadhyay47a0ab32020-11-25 19:34:41 -0800279void AutonomousActor::SplineAuto() {
James Kuszmaul99af8b52021-03-28 10:50:15 -0700280 CHECK(test_spline_);
James Kuszmaul5f6d1d42020-03-01 18:10:07 -0800281
James Kuszmaul99af8b52021-03-28 10:50:15 -0700282 if (!test_spline_->WaitForPlan()) return;
283 test_spline_->Start();
Stephan Massaltd021f972020-01-05 20:41:23 -0800284
James Kuszmaul99af8b52021-03-28 10:50:15 -0700285 if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return;
kyle96c406e2021-02-27 14:07:22 -0800286}
287
milind upadhyay47a0ab32020-11-25 19:34:41 -0800288ProfileParametersT MakeProfileParametersT(const float max_velocity,
289 const float max_acceleration) {
290 ProfileParametersT params;
291 params.max_velocity = max_velocity;
292 params.max_acceleration = max_acceleration;
293 return params;
294}
295
296bool AutonomousActor::DriveFwd() {
Austin Schuhfd1715f2021-01-30 16:58:24 -0800297 const ProfileParametersT kDrive = MakeProfileParametersT(0.3f, 1.0f);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800298 const ProfileParametersT kTurn = MakeProfileParametersT(5.0f, 15.0f);
Austin Schuhfd1715f2021-01-30 16:58:24 -0800299 StartDrive(1.0, 0.0, kDrive, kTurn);
milind upadhyay47a0ab32020-11-25 19:34:41 -0800300 return WaitForDriveDone();
301}
Sabina Leavera0b43b42021-03-03 20:30:04 -0800302
303void AutonomousActor::SendSuperstructureGoal() {
Sabina Leavera0b43b42021-03-03 20:30:04 -0800304 auto builder = superstructure_goal_sender_.MakeBuilder();
305
306 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
307 intake_offset;
milind upadhyayb9dec712021-03-20 15:47:51 -0700308
Sabina Leavera0b43b42021-03-03 20:30:04 -0800309 {
310 StaticZeroingSingleDOFProfiledSubsystemGoal::Builder intake_builder =
311 builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>();
312
milind upadhyayb9dec712021-03-20 15:47:51 -0700313 frc971::ProfileParameters::Builder profile_params_builder =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800314 builder.MakeBuilder<frc971::ProfileParameters>();
Austin Schuhb39a21c2021-03-31 20:12:18 -0700315 profile_params_builder.add_max_velocity(20.0);
316 profile_params_builder.add_max_acceleration(60.0);
milind upadhyayb9dec712021-03-20 15:47:51 -0700317 flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset =
Sabina Leavera0b43b42021-03-03 20:30:04 -0800318 profile_params_builder.Finish();
319 intake_builder.add_unsafe_goal(intake_goal_);
320 intake_builder.add_profile_params(profile_params_offset);
321 intake_offset = intake_builder.Finish();
322 }
323
324 superstructure::Goal::Builder superstructure_builder =
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700325 builder.MakeBuilder<superstructure::Goal>();
milind upadhyayb9dec712021-03-20 15:47:51 -0700326
Sabina Leavera0b43b42021-03-03 20:30:04 -0800327 superstructure_builder.add_intake(intake_offset);
Austin Schuhd0e9e062021-10-24 17:40:58 -0700328 superstructure_builder.add_intake_preloading(true);
Sabina Leavera0b43b42021-03-03 20:30:04 -0800329 superstructure_builder.add_roller_voltage(roller_voltage_);
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700330 superstructure_builder.add_roller_speed_compensation(
331 kRollerSpeedCompensation);
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700332 superstructure_builder.add_hood_tracking(shooter_tracking_);
333 superstructure_builder.add_turret_tracking(shooter_tracking_);
334 superstructure_builder.add_shooter_tracking(shooter_tracking_);
335 superstructure_builder.add_shooting(shooting_);
Sabina Leavera0b43b42021-03-03 20:30:04 -0800336
337 if (!builder.Send(superstructure_builder.Finish())) {
338 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
339 }
Sabina Leavera0b43b42021-03-03 20:30:04 -0800340}
milind upadhyay5e589d72021-03-27 13:47:18 -0700341
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700342void AutonomousActor::ExtendIntake() {
Austin Schuhd0e9e062021-10-24 17:40:58 -0700343 set_intake_goal(1.30);
344 set_roller_voltage(6.0);
Ravago Jonesa7b3c822021-08-26 12:36:03 -0700345 SendSuperstructureGoal();
346}
347
milind upadhyay5e589d72021-03-27 13:47:18 -0700348void AutonomousActor::RetractIntake() {
349 set_intake_goal(-0.89);
Austin Schuhd0e9e062021-10-24 17:40:58 -0700350 set_roller_voltage(6.0);
milind upadhyay5e589d72021-03-27 13:47:18 -0700351 SendSuperstructureGoal();
352}
353
Austin Schuhd0e9e062021-10-24 17:40:58 -0700354int AutonomousActor::Balls() {
Ravago Jones1f32d622021-08-26 12:20:36 -0700355 superstructure_status_fetcher_.Fetch();
356 CHECK(superstructure_status_fetcher_.get() != nullptr);
Austin Schuhd0e9e062021-10-24 17:40:58 -0700357 return superstructure_status_fetcher_->shooter()->balls_shot();
358}
Ravago Jones1f32d622021-08-26 12:20:36 -0700359
Austin Schuhd0e9e062021-10-24 17:40:58 -0700360bool AutonomousActor::WaitUntilAbsoluteBallsShot(int absolute_balls) {
Ravago Jones1f32d622021-08-26 12:20:36 -0700361 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
362 event_loop()->monotonic_now(),
363 frc971::controls::kLoopFrequency / 2);
364 while (true) {
365 if (ShouldCancel()) {
366 return false;
367 }
368 phased_loop.SleepUntilNext();
369 superstructure_status_fetcher_.Fetch();
Austin Schuhd0e9e062021-10-24 17:40:58 -0700370 CHECK(superstructure_status_fetcher_.get() != nullptr);
371 if (superstructure_status_fetcher_->shooter()->balls_shot() >=
372 absolute_balls) {
Ravago Jones1f32d622021-08-26 12:20:36 -0700373 return true;
374 }
375 }
376}
377
Austin Schuhd0e9e062021-10-24 17:40:58 -0700378bool AutonomousActor::WaitForBallsShot(int num_wanted) {
379 return WaitUntilAbsoluteBallsShot(Balls() + num_wanted);
380}
381
Stephan Massaltd021f972020-01-05 20:41:23 -0800382} // namespace actors
383} // namespace y2020