blob: 3c7f2fa4e14592f99a774e921c10c199b3aff6ce [file] [log] [blame]
Niko Sohmers3860f8a2024-01-12 21:05:19 -08001#include "y2024/autonomous/autonomous_actor.h"
2
3#include <chrono>
4#include <cinttypes>
5#include <cmath>
6
7#include "aos/logging/logging.h"
8#include "aos/util/math.h"
9#include "frc971/control_loops/drivetrain/localizer_generated.h"
10#include "y2024/autonomous/auto_splines.h"
11#include "y2024/constants.h"
12#include "y2024/control_loops/drivetrain/drivetrain_base.h"
13
14DEFINE_bool(spline_auto, false, "Run simple test S-spline auto mode.");
15
Stephan Pleinesf63bde82024-01-13 15:59:33 -080016namespace y2024::autonomous {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080017
18using ::frc971::ProfileParametersT;
19
20ProfileParametersT MakeProfileParameters(float max_velocity,
21 float max_acceleration) {
22 ProfileParametersT result;
23 result.max_velocity = max_velocity;
24 result.max_acceleration = max_acceleration;
25 return result;
26}
27
28using ::aos::monotonic_clock;
29using frc971::CreateProfileParameters;
30using ::frc971::ProfileParametersT;
31using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal;
32using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal;
33using frc971::control_loops::drivetrain::LocalizerControl;
34namespace chrono = ::std::chrono;
35
36AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
Filip Kujawa58ffbf32024-02-24 18:28:34 -080037 : frc971::autonomous::UserButtonLocalizedAutonomousActor(
James Kuszmaul2549e752024-01-20 17:42:51 -080038 event_loop,
39 control_loops::drivetrain::GetDrivetrainConfig(event_loop)),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080040 localizer_control_sender_(
41 event_loop->MakeSender<
42 ::frc971::control_loops::drivetrain::LocalizerControl>(
43 "/drivetrain")),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080044 superstructure_goal_sender_(
Filip Kujawa58ffbf32024-02-24 18:28:34 -080045 event_loop
46 ->MakeSender<::y2024::control_loops::superstructure::GoalStatic>(
47 "/superstructure")),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080048 superstructure_status_fetcher_(
49 event_loop
50 ->MakeFetcher<::y2024::control_loops::superstructure::Status>(
Filip Kujawa58ffbf32024-02-24 18:28:34 -080051 "/superstructure")),
52 auto_splines_() {}
Niko Sohmers3860f8a2024-01-12 21:05:19 -080053
54void AutonomousActor::Replan() {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080055 if (FLAGS_spline_auto) {
56 test_spline_ =
57 PlanSpline(std::bind(&AutonomousSplines::TestSpline, &auto_splines_,
58 std::placeholders::_1, alliance_),
59 SplineDirection::kForward);
60
61 starting_position_ = test_spline_->starting_position();
62 }
63
64 is_planned_ = true;
65
66 MaybeSendStartingPosition();
67}
68
Filip Kujawa58ffbf32024-02-24 18:28:34 -080069bool AutonomousActor::Run(
Niko Sohmers3860f8a2024-01-12 21:05:19 -080070 const ::frc971::autonomous::AutonomousActionParams *params) {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080071 AOS_LOG(INFO, "Params are %d\n", params->mode());
72 if (alliance_ == aos::Alliance::kInvalid) {
73 AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection.");
74 return false;
75 }
76 if (FLAGS_spline_auto) {
77 SplineAuto();
78 } else {
79 AOS_LOG(WARNING, "No auto mode selected.");
80 }
81 return true;
82}
83
Niko Sohmers3860f8a2024-01-12 21:05:19 -080084void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) {
85 // Set up the starting position for the blue alliance.
86
87 auto builder = localizer_control_sender_.MakeBuilder();
88
89 LocalizerControl::Builder localizer_control_builder =
90 builder.MakeBuilder<LocalizerControl>();
91 localizer_control_builder.add_x(start(0));
92 localizer_control_builder.add_y(start(1));
93 localizer_control_builder.add_theta(start(2));
94 localizer_control_builder.add_theta_uncertainty(0.00001);
95 AOS_LOG(INFO, "User button pressed, x: %f y: %f theta: %f", start(0),
96 start(1), start(2));
97 if (builder.Send(localizer_control_builder.Finish()) !=
98 aos::RawSender::Error::kOk) {
99 AOS_LOG(ERROR, "Failed to reset localizer.\n");
100 }
101}
Filip Kujawa58ffbf32024-02-24 18:28:34 -0800102
103void AutonomousActor::Reset() {
104 set_intake_goal(control_loops::superstructure::IntakeGoal::NONE);
105 set_note_goal(control_loops::superstructure::NoteGoal::NONE);
106 set_auto_aim(false);
107 set_fire(false);
108 set_preloaded(false);
109 SendSuperstructureGoal();
110}
111
112void AutonomousActor::SplineAuto() {
113 CHECK(test_spline_);
114
115 if (!test_spline_->WaitForPlan()) return;
116 test_spline_->Start();
117
118 if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return;
119}
120
121void AutonomousActor::SendSuperstructureGoal() {
122 aos::Sender<control_loops::superstructure::GoalStatic>::StaticBuilder
123 goal_builder = superstructure_goal_sender_.MakeStaticBuilder();
124
125 goal_builder->set_intake_goal(intake_goal_);
126 goal_builder->set_note_goal(note_goal_);
127 goal_builder->set_fire(fire_);
128
129 control_loops::superstructure::ShooterGoalStatic *shooter_goal =
130 goal_builder->add_shooter_goal();
131
132 shooter_goal->set_auto_aim(auto_aim_);
133 shooter_goal->set_preloaded(preloaded_);
134
135 goal_builder.CheckOk(goal_builder.Send());
136}
137
138void AutonomousActor::Intake() {
139 set_intake_goal(control_loops::superstructure::IntakeGoal::INTAKE);
140 set_note_goal(control_loops::superstructure::NoteGoal::CATAPULT);
141 SendSuperstructureGoal();
142}
143
144void AutonomousActor::Aim() {
145 set_auto_aim(true);
146 SendSuperstructureGoal();
147}
148
149void AutonomousActor::Shoot() {
150 set_fire(true);
151 SendSuperstructureGoal();
152}
153
154[[nodiscard]] bool AutonomousActor::WaitForPreloaded() {
155 set_preloaded(true);
156 SendSuperstructureGoal();
157
158 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
159 event_loop()->monotonic_now(),
160 aos::common::actions::kLoopOffset);
161
162 bool loaded = false;
163 while (!loaded) {
164 if (ShouldCancel()) {
165 return false;
166 }
167
168 phased_loop.SleepUntilNext();
169 superstructure_status_fetcher_.Fetch();
170 CHECK(superstructure_status_fetcher_.get() != nullptr);
171
172 loaded = (superstructure_status_fetcher_->shooter()->catapult_state() ==
173 control_loops::superstructure::CatapultState::LOADED);
174 }
175
176 set_preloaded(false);
177 SendSuperstructureGoal();
178
179 return true;
180}
181
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800182} // namespace y2024::autonomous