blob: 2ddaf646131d96a4110ae409152a5ddf1d8a9e3c [file] [log] [blame]
Niko Sohmersc4d2c502024-02-19 19:35:35 -08001#include "y2024/control_loops/superstructure/shooter.h"
2
3#include "aos/flatbuffers.h"
4#include "aos/flatbuffers/base.h"
5#include "frc971/control_loops/aiming/aiming.h"
6#include "y2024/control_loops/superstructure/catapult/catapult_plant.h"
Niko Sohmersac4d8872024-02-23 13:55:47 -08007#include "y2024/control_loops/superstructure/collision_avoidance.h"
Niko Sohmersc4d2c502024-02-19 19:35:35 -08008
9namespace y2024::control_loops::superstructure {
10
11using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus;
12
Niko Sohmersc4d2c502024-02-19 19:35:35 -080013constexpr double kCatapultActivationThreshold = 0.01;
14
15Shooter::Shooter(aos::EventLoop *event_loop, const Constants *robot_constants)
16 : drivetrain_status_fetcher_(
17 event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>(
18 "/drivetrain")),
Niko Sohmersc4d2c502024-02-19 19:35:35 -080019 robot_constants_(robot_constants),
20 catapult_(
21 robot_constants->common()->catapult(),
22 robot_constants->robot()->catapult_constants()->zeroing_constants()),
23 turret_(
24 robot_constants_->common()->turret(),
25 robot_constants_->robot()->turret_constants()->zeroing_constants()),
26 altitude_(
27 robot_constants_->common()->altitude(),
28 robot_constants_->robot()->altitude_constants()->zeroing_constants()),
29 aimer_(event_loop, robot_constants_),
30 interpolation_table_(
31 y2024::constants::Values::InterpolationTableFromFlatbuffer(
32 robot_constants_->common()->shooter_interpolation_table())) {}
33
34flatbuffers::Offset<y2024::control_loops::superstructure::ShooterStatus>
35Shooter::Iterate(
36 const y2024::control_loops::superstructure::Position *position,
37 const y2024::control_loops::superstructure::ShooterGoal *shooter_goal,
38 double *catapult_output, double *altitude_output, double *turret_output,
Maxwell Hendersond5bf47a2024-02-23 17:16:48 -080039 double *retention_roller_output,
40 double *retention_roller_stator_current_limit, double /*battery_voltage*/,
Niko Sohmersac4d8872024-02-23 13:55:47 -080041 CollisionAvoidance *collision_avoidance, const double intake_pivot_position,
42 double *max_intake_pivot_position, double *min_intake_pivot_position,
Niko Sohmersc4d2c502024-02-19 19:35:35 -080043 flatbuffers::FlatBufferBuilder *fbb) {
Niko Sohmersc4d2c502024-02-19 19:35:35 -080044 drivetrain_status_fetcher_.Fetch();
Niko Sohmersc4d2c502024-02-19 19:35:35 -080045
46 // If our current is over the minimum current and our velocity is under our
47 // maximum velocity, then set loaded to true. If we are preloaded set it to
48 // true as well.
49 //
50 // TODO(austin): Debounce piece_loaded?
Maxwell Henderson461a8a42024-02-23 17:07:39 -080051 bool piece_loaded = position->catapult_beambreak() ||
52 (shooter_goal != nullptr && shooter_goal->preloaded());
Niko Sohmersc4d2c502024-02-19 19:35:35 -080053
54 aos::fbs::FixedStackAllocator<aos::fbs::Builder<
55 frc971::control_loops::
56 StaticZeroingSingleDOFProfiledSubsystemGoalStatic>::kBufferSize>
57 turret_allocator;
58
59 aos::fbs::Builder<
60 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoalStatic>
61 turret_goal_builder(&turret_allocator);
62
63 aos::fbs::FixedStackAllocator<aos::fbs::Builder<
64 frc971::control_loops::
65 StaticZeroingSingleDOFProfiledSubsystemGoalStatic>::kBufferSize>
66 altitude_allocator;
67
68 aos::fbs::Builder<
69 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoalStatic>
70 altitude_goal_builder(&altitude_allocator);
71
72 const double distance_to_goal = aimer_.DistanceToGoal();
73
74 // Always retain the game piece if we are enabled.
75 if (retention_roller_output != nullptr) {
76 *retention_roller_output =
77 robot_constants_->common()->retention_roller_voltages()->retaining();
Maxwell Hendersond5bf47a2024-02-23 17:16:48 -080078
79 if (piece_loaded) {
80 *retention_roller_stator_current_limit =
81 robot_constants_->common()
82 ->current_limits()
83 ->slower_retention_roller_stator_current_limit();
84 } else {
85 *retention_roller_stator_current_limit =
86 robot_constants_->common()
87 ->current_limits()
88 ->retention_roller_stator_current_limit();
89 }
Niko Sohmersc4d2c502024-02-19 19:35:35 -080090 }
91
92 bool aiming = false;
93
94 // We don't have the note so we should be ready to intake it.
95 if (shooter_goal == nullptr || !shooter_goal->auto_aim() ||
96 (!piece_loaded && state_ == CatapultState::READY)) {
97 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
98 turret_goal_builder.get(),
99 robot_constants_->common()->turret_loading_position());
100
101 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
102 altitude_goal_builder.get(),
103 robot_constants_->common()->altitude_loading_position());
104 } else {
105 // We have a game piece, lets start aiming.
106 if (drivetrain_status_fetcher_.get() != nullptr) {
107 aiming = true;
108 aimer_.Update(drivetrain_status_fetcher_.get(),
109 frc971::control_loops::aiming::ShotMode::kShootOnTheFly,
110 turret_goal_builder.get());
111 }
112 }
113
114 // We have a game piece and are being asked to aim.
115 constants::Values::ShotParams shot_params;
116 if (piece_loaded && shooter_goal != nullptr && shooter_goal->auto_aim() &&
117 interpolation_table_.GetInRange(distance_to_goal, &shot_params)) {
118 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
119 altitude_goal_builder.get(), shot_params.shot_altitude_angle);
120 }
121
122 // The builder will contain either the auto-aim goal, or the loading goal. Use
123 // it if we have no goal, or no subsystem goal, or if we are auto-aiming.
124 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
125 *turret_goal = (shooter_goal != nullptr && !shooter_goal->auto_aim() &&
126 shooter_goal->has_turret_position())
127 ? shooter_goal->turret_position()
128 : &turret_goal_builder->AsFlatbuffer();
129 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
130 *altitude_goal = (shooter_goal != nullptr && !shooter_goal->auto_aim() &&
131 shooter_goal->has_altitude_position())
132 ? shooter_goal->altitude_position()
133 : &altitude_goal_builder->AsFlatbuffer();
134
135 bool subsystems_in_range =
136 (std::abs(turret_.estimated_position() - turret_goal->unsafe_goal()) <
137 kCatapultActivationThreshold &&
138 std::abs(altitude_.estimated_position() - altitude_goal->unsafe_goal()) <
139 kCatapultActivationThreshold &&
140 altitude_.estimated_position() >
141 robot_constants_->common()->min_altitude_shooting_angle());
142
Niko Sohmersac4d8872024-02-23 13:55:47 -0800143 const bool disabled = turret_.Correct(turret_goal, position->turret(),
144 turret_output == nullptr);
145
146 collision_avoidance->UpdateGoal(
147 {.intake_pivot_position = intake_pivot_position,
148 .turret_position = turret_.estimated_position()},
149 turret_goal->unsafe_goal());
150
151 turret_.set_min_position(collision_avoidance->min_turret_goal());
152 turret_.set_max_position(collision_avoidance->max_turret_goal());
153
154 *max_intake_pivot_position = collision_avoidance->max_intake_pivot_goal();
155 *min_intake_pivot_position = collision_avoidance->min_intake_pivot_goal();
156
157 // Calculate the loops for a cycle.
158 const double voltage = turret_.UpdateController(disabled);
159
160 turret_.UpdateObserver(voltage);
161
162 // Write out all the voltages.
163 if (turret_output) {
164 *turret_output = voltage;
165 }
166
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800167 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
Niko Sohmersac4d8872024-02-23 13:55:47 -0800168 turret_status_offset = turret_.MakeStatus(fbb);
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800169
170 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
171 altitude_status_offset = altitude_.Iterate(
172 altitude_goal, position->altitude(), altitude_output, fbb);
173
174 flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
175 catapult_status_offset;
176 {
177 // The catapult will never use a provided goal. We'll always fabricate one
178 // for it.
179 //
180 // Correct handles resetting our state when disabled.
181 const bool disabled = catapult_.Correct(nullptr, position->catapult(),
182 shooter_goal == nullptr);
183
184 catapult_.set_enable_profile(true);
185 // We want a trajectory which accelerates up over the first portion of the
186 // range of motion, holds top speed for a little bit, then decelerates
187 // before it swings too far.
188 //
189 // We can solve for these 3 parameters through the range of motion. Top
190 // speed is goverened by the voltage headroom we want to have for the
191 // controller.
192 //
193 // Accel can be tuned given the distance to accelerate over, and decel can
194 // be solved similarly.
195 //
196 // accel = v^2 / (2 * x)
197 catapult_.mutable_profile()->set_maximum_velocity(
198 catapult::kFreeSpeed * catapult::kOutputRatio * 4.0 / 12.0);
199
200 if (disabled) {
201 state_ = CatapultState::RETRACTING;
202 }
203
204 switch (state_) {
205 case CatapultState::READY:
206 case CatapultState::LOADED: {
207 if (piece_loaded) {
208 state_ = CatapultState::LOADED;
209 } else {
210 state_ = CatapultState::READY;
211 }
212
213 const bool catapult_close = CatapultClose();
214
215 if (subsystems_in_range && shooter_goal != nullptr &&
216 shooter_goal->fire() && catapult_close && piece_loaded) {
217 state_ = CatapultState::FIRING;
218 } else {
219 catapult_.set_controller_index(0);
220 catapult_.mutable_profile()->set_maximum_acceleration(100.0);
221 catapult_.mutable_profile()->set_maximum_deceleration(50.0);
222 catapult_.set_unprofiled_goal(0.0, 0.0);
223
224 if (!catapult_close) {
225 state_ = CatapultState::RETRACTING;
226 }
227 break;
228 }
229 [[fallthrough]];
230 }
231 case CatapultState::FIRING:
232 *retention_roller_output =
233 robot_constants_->common()->retention_roller_voltages()->spitting();
234 catapult_.set_controller_index(0);
235 catapult_.mutable_profile()->set_maximum_acceleration(400.0);
236 catapult_.mutable_profile()->set_maximum_deceleration(600.0);
237 catapult_.set_unprofiled_goal(2.0, 0.0);
238 if (CatapultClose()) {
239 state_ = CatapultState::RETRACTING;
240 } else {
241 break;
242 }
243 [[fallthrough]];
244 case CatapultState::RETRACTING:
245 catapult_.set_controller_index(0);
246 catapult_.mutable_profile()->set_maximum_acceleration(100.0);
247 catapult_.mutable_profile()->set_maximum_deceleration(50.0);
248 catapult_.set_unprofiled_goal(0.0, 0.0);
249
250 if (CatapultClose()) {
251 if (piece_loaded) {
252 state_ = CatapultState::LOADED;
253 } else {
254 state_ = CatapultState::READY;
255 }
256 }
257 break;
258 }
259
260 const double voltage = catapult_.UpdateController(disabled);
261 catapult_.UpdateObserver(voltage);
262 if (catapult_output != nullptr) {
263 *catapult_output = voltage;
264 }
265 catapult_status_offset = catapult_.MakeStatus(fbb);
266 }
267
268 flatbuffers::Offset<AimerStatus> aimer_offset;
269 if (aiming) {
270 aimer_offset = aimer_.PopulateStatus(fbb);
271 }
272
273 y2024::control_loops::superstructure::ShooterStatus::Builder status_builder(
274 *fbb);
275 status_builder.add_turret(turret_status_offset);
276 status_builder.add_altitude(altitude_status_offset);
277 status_builder.add_catapult(catapult_status_offset);
278 status_builder.add_catapult_state(state_);
279 if (aiming) {
280 status_builder.add_aimer(aimer_offset);
281 }
282
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800283 return status_builder.Finish();
284}
285
286} // namespace y2024::control_loops::superstructure