blob: 5e344ac193572ee0e46812b34dfedf1a48003137 [file] [log] [blame]
milind-u086d7262022-01-19 20:44:18 -08001#include "y2022/actors/autonomous_actor.h"
2
3#include <chrono>
4#include <cinttypes>
5#include <cmath>
6
7#include "aos/logging/logging.h"
Ravago Jones81e50632022-03-11 16:23:51 -08008#include "aos/network/team_number.h"
9#include "aos/util/math.h"
milind-u086d7262022-01-19 20:44:18 -080010#include "frc971/control_loops/drivetrain/localizer_generated.h"
Ravago Jones81e50632022-03-11 16:23:51 -080011#include "y2022/actors/auto_splines.h"
12#include "y2022/constants.h"
milind-u086d7262022-01-19 20:44:18 -080013#include "y2022/control_loops/drivetrain/drivetrain_base.h"
14
Ravago Jones81e50632022-03-11 16:23:51 -080015DEFINE_bool(spline_auto, false, "If true, define a spline autonomous mode");
Milind Upadhyaya7793962022-03-11 19:39:36 -080016DEFINE_bool(rapid_react, false,
17 "If true, run the main rapid react autonomous mode");
Ravago Jones81e50632022-03-11 16:23:51 -080018
milind-u086d7262022-01-19 20:44:18 -080019namespace y2022 {
20namespace actors {
Ravago Jones81e50632022-03-11 16:23:51 -080021namespace {
Austin Schuhe1264372022-03-13 20:22:36 -070022constexpr double kExtendIntakeGoal = -0.02;
Ravago Jones81e50632022-03-11 16:23:51 -080023constexpr double kRetractIntakeGoal = 1.47;
Austin Schuhe1264372022-03-13 20:22:36 -070024constexpr double kIntakeRollerVoltage = 8.0;
Ravago Jones81e50632022-03-11 16:23:51 -080025constexpr double kRollerVoltage = 12.0;
26constexpr double kCatapultReturnPosition = -0.908;
27} // namespace
milind-u086d7262022-01-19 20:44:18 -080028
29using ::aos::monotonic_clock;
Ravago Jones81e50632022-03-11 16:23:51 -080030using frc971::CreateProfileParameters;
milind-u086d7262022-01-19 20:44:18 -080031using ::frc971::ProfileParametersT;
Ravago Jones81e50632022-03-11 16:23:51 -080032using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal;
33using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal;
milind-u086d7262022-01-19 20:44:18 -080034using frc971::control_loops::drivetrain::LocalizerControl;
Ravago Jones81e50632022-03-11 16:23:51 -080035
milind-u086d7262022-01-19 20:44:18 -080036namespace chrono = ::std::chrono;
37
38AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
39 : frc971::autonomous::BaseAutonomousActor(
Ravago Jones81e50632022-03-11 16:23:51 -080040 event_loop, control_loops::drivetrain::GetDrivetrainConfig()),
41 localizer_control_sender_(
42 event_loop->MakeSender<
43 ::frc971::control_loops::drivetrain::LocalizerControl>(
44 "/drivetrain")),
45 superstructure_goal_sender_(
46 event_loop->MakeSender<control_loops::superstructure::Goal>(
47 "/superstructure")),
48 superstructure_status_fetcher_(
49 event_loop->MakeFetcher<control_loops::superstructure::Status>(
50 "/superstructure")),
51 joystick_state_fetcher_(
52 event_loop->MakeFetcher<aos::JoystickState>("/aos")),
53 robot_state_fetcher_(event_loop->MakeFetcher<aos::RobotState>("/aos")),
54 auto_splines_() {
55 set_max_drivetrain_voltage(12.0);
56 replan_timer_ = event_loop->AddTimer([this]() { Replan(); });
57 event_loop->OnRun([this, event_loop]() {
58 replan_timer_->Setup(event_loop->monotonic_now());
59 button_poll_->Setup(event_loop->monotonic_now(), chrono::milliseconds(50));
60 });
61
62 button_poll_ = event_loop->AddTimer([this]() {
63 const aos::monotonic_clock::time_point now =
64 this->event_loop()->context().monotonic_event_time;
65 if (robot_state_fetcher_.Fetch()) {
66 if (robot_state_fetcher_->user_button()) {
67 user_indicated_safe_to_reset_ = true;
68 MaybeSendStartingPosition();
69 }
70 }
71 if (joystick_state_fetcher_.Fetch()) {
72 if (joystick_state_fetcher_->has_alliance() &&
73 (joystick_state_fetcher_->alliance() != alliance_)) {
74 alliance_ = joystick_state_fetcher_->alliance();
75 is_planned_ = false;
76 // Only kick the planning out by 2 seconds. If we end up enabled in that
77 // second, then we will kick it out further based on the code below.
78 replan_timer_->Setup(now + std::chrono::seconds(2));
79 }
80 if (joystick_state_fetcher_->enabled()) {
81 if (!is_planned_) {
82 // Only replan once we've been disabled for 5 seconds.
83 replan_timer_->Setup(now + std::chrono::seconds(5));
84 }
85 }
86 }
87 });
88}
89
90void AutonomousActor::Replan() {
91 LOG(INFO) << "Alliance " << static_cast<int>(alliance_);
92 if (alliance_ == aos::Alliance::kInvalid) {
93 return;
94 }
95 sent_starting_position_ = false;
96 if (FLAGS_spline_auto) {
97 test_spline_ =
98 PlanSpline(std::bind(&AutonomousSplines::TestSpline, &auto_splines_,
99 std::placeholders::_1, alliance_),
100 SplineDirection::kForward);
101
102 starting_position_ = test_spline_->starting_position();
Milind Upadhyaya7793962022-03-11 19:39:36 -0800103 } else if (FLAGS_rapid_react) {
104 rapid_react_splines_ = {
105 PlanSpline(std::bind(&AutonomousSplines::Spline1, &auto_splines_,
106 std::placeholders::_1, alliance_),
Henry Speiser09e8da92022-03-14 20:58:45 -0700107 SplineDirection::kBackward),
Milind Upadhyaya7793962022-03-11 19:39:36 -0800108 PlanSpline(std::bind(&AutonomousSplines::Spline2, &auto_splines_,
109 std::placeholders::_1, alliance_),
110 SplineDirection::kForward),
111 PlanSpline(std::bind(&AutonomousSplines::Spline3, &auto_splines_,
112 std::placeholders::_1, alliance_),
Milind Upadhyaya7793962022-03-11 19:39:36 -0800113 SplineDirection::kBackward)};
114 starting_position_ = rapid_react_splines_.value()[0].starting_position();
115 CHECK(starting_position_);
Ravago Jones81e50632022-03-11 16:23:51 -0800116 }
117
118 is_planned_ = true;
119
120 MaybeSendStartingPosition();
121}
122
123void AutonomousActor::MaybeSendStartingPosition() {
124 if (is_planned_ && user_indicated_safe_to_reset_ &&
125 !sent_starting_position_) {
126 CHECK(starting_position_);
127 SendStartingPosition(starting_position_.value());
128 }
129}
milind-u086d7262022-01-19 20:44:18 -0800130
131void AutonomousActor::Reset() {
132 InitializeEncoders();
133 ResetDrivetrain();
Ravago Jones81e50632022-03-11 16:23:51 -0800134 RetractFrontIntake();
135 RetractBackIntake();
136
137 joystick_state_fetcher_.Fetch();
138 CHECK(joystick_state_fetcher_.get() != nullptr)
139 << "Expect at least one JoystickState message before running auto...";
140 alliance_ = joystick_state_fetcher_->alliance();
milind-u086d7262022-01-19 20:44:18 -0800141}
142
143bool AutonomousActor::RunAction(
144 const ::frc971::autonomous::AutonomousActionParams *params) {
145 Reset();
Ravago Jones81e50632022-03-11 16:23:51 -0800146 if (!user_indicated_safe_to_reset_) {
147 AOS_LOG(WARNING, "Didn't send starting position prior to starting auto.");
148 CHECK(starting_position_);
149 SendStartingPosition(starting_position_.value());
150 }
151 // Clear this so that we don't accidentally resend things as soon as we replan
152 // later.
153 user_indicated_safe_to_reset_ = false;
154 is_planned_ = false;
155 starting_position_.reset();
milind-u086d7262022-01-19 20:44:18 -0800156
157 AOS_LOG(INFO, "Params are %d\n", params->mode());
Ravago Jones81e50632022-03-11 16:23:51 -0800158 if (alliance_ == aos::Alliance::kInvalid) {
159 AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection.");
160 return false;
161 }
162 if (FLAGS_spline_auto) {
163 SplineAuto();
Milind Upadhyaya7793962022-03-11 19:39:36 -0800164 } else if (FLAGS_rapid_react) {
165 RapidReact();
Ravago Jones81e50632022-03-11 16:23:51 -0800166 }
167
milind-u086d7262022-01-19 20:44:18 -0800168 return true;
169}
170
Ravago Jones81e50632022-03-11 16:23:51 -0800171void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) {
172 // Set up the starting position for the blue alliance.
173
174 // TODO(james): Resetting the localizer breaks the left/right statespace
175 // controller. That is a bug, but we can fix that later by not resetting.
176 auto builder = localizer_control_sender_.MakeBuilder();
177
178 LocalizerControl::Builder localizer_control_builder =
179 builder.MakeBuilder<LocalizerControl>();
180 localizer_control_builder.add_x(start(0));
181 localizer_control_builder.add_y(start(1));
182 localizer_control_builder.add_theta(start(2));
183 localizer_control_builder.add_theta_uncertainty(0.00001);
184 LOG(INFO) << "User button pressed, x: " << start(0) << " y: " << start(1)
185 << " theta: " << start(2);
186 if (builder.Send(localizer_control_builder.Finish()) !=
187 aos::RawSender::Error::kOk) {
188 AOS_LOG(ERROR, "Failed to reset localizer.\n");
189 }
190}
191
192void AutonomousActor::SplineAuto() {
193 CHECK(test_spline_);
194
195 if (!test_spline_->WaitForPlan()) return;
196 test_spline_->Start();
197
198 if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return;
199}
200
Milind Upadhyaya7793962022-03-11 19:39:36 -0800201void AutonomousActor::RapidReact() {
202 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now();
203
204 CHECK(rapid_react_splines_);
205
206 auto &splines = *rapid_react_splines_;
207
208 // Tell the superstructure a ball was preloaded
Milind Upadhyaya7793962022-03-11 19:39:36 -0800209 if (!WaitForPreloaded()) return;
Henry Speiser09e8da92022-03-14 20:58:45 -0700210
211 // Fire preloaded ball
212 set_turret_goal(constants::Values::kTurretFrontIntakePos());
213 set_fire_at_will(true);
214 SendSuperstructureGoal();
215 if (!WaitForBallsShot(1)) return;
216 set_fire_at_will(false);
217 SendSuperstructureGoal();
218
219 // Drive and intake the 2 balls in nearest to the starting zonei
220 set_turret_goal(constants::Values::kTurretBackIntakePos());
221 ExtendBackIntake();
Milind Upadhyaya7793962022-03-11 19:39:36 -0800222 if (!splines[0].WaitForPlan()) return;
223 splines[0].Start();
224 if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return;
225
226 // Fire the two balls once we stopped
Henry Speiser09e8da92022-03-14 20:58:45 -0700227 RetractBackIntake();
Milind Upadhyaya7793962022-03-11 19:39:36 -0800228 set_fire_at_will(true);
229 SendSuperstructureGoal();
230 if (!WaitForBallsShot(2)) return;
231 set_fire_at_will(false);
Milind Upadhyaya7793962022-03-11 19:39:36 -0800232 SendSuperstructureGoal();
Milind Upadhyaya7793962022-03-11 19:39:36 -0800233
234 // Drive to the human player station while intaking two balls.
235 // Once is already placed down,
236 // and one will be rolled to the robot by the human player
Henry Speiser09e8da92022-03-14 20:58:45 -0700237 ExtendFrontIntake();
238 if (!splines[1].WaitForPlan()) return;
239 splines[1].Start();
240 if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return;
Milind Upadhyaya7793962022-03-11 19:39:36 -0800241
242 // Drive to the shooting position
Henry Speiser09e8da92022-03-14 20:58:45 -0700243 if (!splines[2].WaitForPlan()) return;
244 splines[2].Start();
245 if (!splines[2].WaitForSplineDistanceRemaining(2.00)) return;
246 RetractFrontIntake();
247
248 if (!splines[2].WaitForSplineDistanceRemaining(0.02)) return;
Milind Upadhyaya7793962022-03-11 19:39:36 -0800249
250 // Fire the two balls once we stopped
251 set_fire_at_will(true);
252 SendSuperstructureGoal();
253 if (!WaitForBallsShot(2)) return;
254 set_fire_at_will(false);
Henry Speiser09e8da92022-03-14 20:58:45 -0700255 SendSuperstructureGoal();
Milind Upadhyaya7793962022-03-11 19:39:36 -0800256
257 LOG(INFO) << "Took "
258 << chrono::duration<double>(aos::monotonic_clock::now() -
259 start_time)
260 .count()
261 << 's';
262}
263
264[[nodiscard]] bool AutonomousActor::WaitForPreloaded() {
Milind Upadhyay803bbf02022-03-11 17:56:26 -0800265 set_preloaded(true);
266 SendSuperstructureGoal();
267
268 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
269 event_loop()->monotonic_now(),
270 ActorBase::kLoopOffset);
271
272 bool loaded = false;
273 while (!loaded) {
274 if (ShouldCancel()) {
275 return false;
276 }
277
278 phased_loop.SleepUntilNext();
279 superstructure_status_fetcher_.Fetch();
280 CHECK(superstructure_status_fetcher_.get() != nullptr);
281
282 loaded = (superstructure_status_fetcher_->state() ==
283 control_loops::superstructure::SuperstructureState::LOADED);
284 }
285
286 set_preloaded(false);
287 SendSuperstructureGoal();
288
289 return true;
290}
291
Ravago Jones81e50632022-03-11 16:23:51 -0800292void AutonomousActor::SendSuperstructureGoal() {
293 auto builder = superstructure_goal_sender_.MakeBuilder();
294
295 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
296 intake_front_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
297 *builder.fbb(), intake_front_goal_,
298 CreateProfileParameters(*builder.fbb(), 20.0, 60.0));
299
300 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
301 intake_back_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
302 *builder.fbb(), intake_back_goal_,
303 CreateProfileParameters(*builder.fbb(), 20.0, 60.0));
304
305 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
Henry Speiser09e8da92022-03-14 20:58:45 -0700306 turret_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
307 *builder.fbb(), turret_goal_,
308 CreateProfileParameters(*builder.fbb(), 12.0, 20.0));
309
310 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
Ravago Jones81e50632022-03-11 16:23:51 -0800311 catapult_return_position_offset =
312 CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
313 *builder.fbb(), kCatapultReturnPosition,
314 CreateProfileParameters(*builder.fbb(), 9.0, 50.0));
315
316 superstructure::CatapultGoal::Builder catapult_goal_builder(*builder.fbb());
317 catapult_goal_builder.add_shot_position(0.03);
318 catapult_goal_builder.add_shot_velocity(18.0);
319 catapult_goal_builder.add_return_position(catapult_return_position_offset);
320 flatbuffers::Offset<superstructure::CatapultGoal> catapult_goal_offset =
321 catapult_goal_builder.Finish();
322
323 superstructure::Goal::Builder superstructure_builder =
324 builder.MakeBuilder<superstructure::Goal>();
325
326 superstructure_builder.add_intake_front(intake_front_offset);
327 superstructure_builder.add_intake_back(intake_back_offset);
328 superstructure_builder.add_roller_speed_compensation(1.5);
329 superstructure_builder.add_roller_speed_front(roller_front_voltage_);
330 superstructure_builder.add_roller_speed_back(roller_back_voltage_);
Milind Upadhyayd86b02c2022-03-13 20:57:46 -0700331 if (requested_intake_.has_value()) {
332 superstructure_builder.add_turret_intake(*requested_intake_);
333 }
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -0800334 superstructure_builder.add_transfer_roller_speed_front(
335 transfer_roller_front_voltage_);
336 superstructure_builder.add_transfer_roller_speed_back(
337 transfer_roller_back_voltage_);
Henry Speiser09e8da92022-03-14 20:58:45 -0700338 superstructure_builder.add_turret(turret_offset);
Ravago Jones81e50632022-03-11 16:23:51 -0800339 superstructure_builder.add_catapult(catapult_goal_offset);
340 superstructure_builder.add_fire(fire_);
Milind Upadhyay803bbf02022-03-11 17:56:26 -0800341 superstructure_builder.add_preloaded(preloaded_);
Henry Speiser09e8da92022-03-14 20:58:45 -0700342 superstructure_builder.add_auto_aim(false);
Ravago Jones81e50632022-03-11 16:23:51 -0800343
344 if (builder.Send(superstructure_builder.Finish()) !=
345 aos::RawSender::Error::kOk) {
346 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
347 }
348}
349
350void AutonomousActor::ExtendFrontIntake() {
Milind Upadhyayd86b02c2022-03-13 20:57:46 -0700351 set_requested_intake(RequestedIntake::kFront);
Ravago Jones81e50632022-03-11 16:23:51 -0800352 set_intake_front_goal(kExtendIntakeGoal);
Austin Schuhe1264372022-03-13 20:22:36 -0700353 set_roller_front_voltage(kIntakeRollerVoltage);
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -0800354 set_transfer_roller_front_voltage(kRollerVoltage);
355 set_transfer_roller_back_voltage(-kRollerVoltage);
Ravago Jones81e50632022-03-11 16:23:51 -0800356 SendSuperstructureGoal();
357}
358
359void AutonomousActor::RetractFrontIntake() {
Milind Upadhyayd86b02c2022-03-13 20:57:46 -0700360 set_requested_intake(std::nullopt);
Ravago Jones81e50632022-03-11 16:23:51 -0800361 set_intake_front_goal(kRetractIntakeGoal);
Austin Schuhe1264372022-03-13 20:22:36 -0700362 set_roller_front_voltage(0.0);
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -0800363 set_transfer_roller_front_voltage(0.0);
364 set_transfer_roller_back_voltage(0.0);
Ravago Jones81e50632022-03-11 16:23:51 -0800365 SendSuperstructureGoal();
366}
367
368void AutonomousActor::ExtendBackIntake() {
Milind Upadhyayd86b02c2022-03-13 20:57:46 -0700369 set_requested_intake(RequestedIntake::kBack);
Ravago Jones81e50632022-03-11 16:23:51 -0800370 set_intake_back_goal(kExtendIntakeGoal);
Austin Schuhe1264372022-03-13 20:22:36 -0700371 set_roller_back_voltage(kIntakeRollerVoltage);
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -0800372 set_transfer_roller_back_voltage(kRollerVoltage);
373 set_transfer_roller_front_voltage(-kRollerVoltage);
Ravago Jones81e50632022-03-11 16:23:51 -0800374 SendSuperstructureGoal();
375}
376
377void AutonomousActor::RetractBackIntake() {
Milind Upadhyayd86b02c2022-03-13 20:57:46 -0700378 set_requested_intake(std::nullopt);
Ravago Jones81e50632022-03-11 16:23:51 -0800379 set_intake_back_goal(kRetractIntakeGoal);
Austin Schuhe1264372022-03-13 20:22:36 -0700380 set_roller_back_voltage(0.0);
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -0800381 set_transfer_roller_front_voltage(0.0);
382 set_transfer_roller_back_voltage(0.0);
Ravago Jones81e50632022-03-11 16:23:51 -0800383 SendSuperstructureGoal();
384}
385
Milind Upadhyaya7793962022-03-11 19:39:36 -0800386[[nodiscard]] bool AutonomousActor::WaitForBallsShot(int num_wanted) {
Ravago Jonesf699d1c2022-03-11 18:39:56 -0800387 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
388 event_loop()->monotonic_now(),
389 ActorBase::kLoopOffset);
390 superstructure_status_fetcher_.Fetch();
391 CHECK(superstructure_status_fetcher_.get() != nullptr);
392 int initial_balls = superstructure_status_fetcher_->shot_count();
393 LOG(INFO) << "Waiting for balls, started with " << initial_balls;
394 while (true) {
395 if (ShouldCancel()) {
396 return false;
397 }
398 phased_loop.SleepUntilNext();
399 superstructure_status_fetcher_.Fetch();
400 CHECK(superstructure_status_fetcher_.get() != nullptr);
401 if (superstructure_status_fetcher_->shot_count() - initial_balls >=
402 num_wanted) {
403 return true;
404 }
405 }
406}
407
milind-u086d7262022-01-19 20:44:18 -0800408} // namespace actors
409} // namespace y2022