blob: 837b353177699804a7e25a2c306ebdf21c809910 [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(
Maxwell Henderson6b1be312024-02-28 20:15:06 -080032 robot_constants_->common()->shooter_interpolation_table())),
33 debouncer_(std::chrono::milliseconds(100), std::chrono::milliseconds(8)) {
34}
Niko Sohmersc4d2c502024-02-19 19:35:35 -080035
36flatbuffers::Offset<y2024::control_loops::superstructure::ShooterStatus>
37Shooter::Iterate(
38 const y2024::control_loops::superstructure::Position *position,
39 const y2024::control_loops::superstructure::ShooterGoal *shooter_goal,
Filip Kujawa7a799602024-02-23 12:27:47 -080040 bool fire, double *catapult_output, double *altitude_output,
41 double *turret_output, double *retention_roller_output,
Maxwell Hendersond5bf47a2024-02-23 17:16:48 -080042 double *retention_roller_stator_current_limit, double /*battery_voltage*/,
Austin Schuh027fd622024-03-01 21:26:07 -080043 CollisionAvoidance *collision_avoidance, const double extend_position,
44 const double extend_goal, double *max_extend_position,
45 double *min_extend_position, const double intake_pivot_position,
Niko Sohmersac4d8872024-02-23 13:55:47 -080046 double *max_intake_pivot_position, double *min_intake_pivot_position,
Austin Schuh027fd622024-03-01 21:26:07 -080047 flatbuffers::FlatBufferBuilder *fbb,
Maxwell Henderson6b1be312024-02-28 20:15:06 -080048 aos::monotonic_clock::time_point monotonic_now) {
Niko Sohmersc4d2c502024-02-19 19:35:35 -080049 drivetrain_status_fetcher_.Fetch();
Niko Sohmersc4d2c502024-02-19 19:35:35 -080050
51 // If our current is over the minimum current and our velocity is under our
52 // maximum velocity, then set loaded to true. If we are preloaded set it to
53 // true as well.
Maxwell Henderson6b1be312024-02-28 20:15:06 -080054 debouncer_.Update(position->catapult_beambreak() ||
55 (shooter_goal != nullptr && shooter_goal->preloaded()),
56 monotonic_now);
57 const bool piece_loaded = debouncer_.state();
Niko Sohmersc4d2c502024-02-19 19:35:35 -080058
59 aos::fbs::FixedStackAllocator<aos::fbs::Builder<
60 frc971::control_loops::
61 StaticZeroingSingleDOFProfiledSubsystemGoalStatic>::kBufferSize>
62 turret_allocator;
63
64 aos::fbs::Builder<
65 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoalStatic>
66 turret_goal_builder(&turret_allocator);
67
68 aos::fbs::FixedStackAllocator<aos::fbs::Builder<
69 frc971::control_loops::
70 StaticZeroingSingleDOFProfiledSubsystemGoalStatic>::kBufferSize>
71 altitude_allocator;
72
73 aos::fbs::Builder<
74 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoalStatic>
75 altitude_goal_builder(&altitude_allocator);
76
77 const double distance_to_goal = aimer_.DistanceToGoal();
78
79 // Always retain the game piece if we are enabled.
80 if (retention_roller_output != nullptr) {
81 *retention_roller_output =
82 robot_constants_->common()->retention_roller_voltages()->retaining();
Maxwell Hendersond5bf47a2024-02-23 17:16:48 -080083
84 if (piece_loaded) {
85 *retention_roller_stator_current_limit =
86 robot_constants_->common()
87 ->current_limits()
88 ->slower_retention_roller_stator_current_limit();
89 } else {
90 *retention_roller_stator_current_limit =
91 robot_constants_->common()
92 ->current_limits()
93 ->retention_roller_stator_current_limit();
94 }
Niko Sohmersc4d2c502024-02-19 19:35:35 -080095 }
96
97 bool aiming = false;
98
Austin Schuh027fd622024-03-01 21:26:07 -080099 if (shooter_goal == nullptr || !shooter_goal->auto_aim() ||
100 (!piece_loaded && state_ == CatapultState::READY)) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800101 // We don't have the note so we should be ready to intake it.
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800102 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
103 turret_goal_builder.get(),
104 robot_constants_->common()->turret_loading_position());
105
106 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
107 altitude_goal_builder.get(),
108 robot_constants_->common()->altitude_loading_position());
Filip Kujawa7a799602024-02-23 12:27:47 -0800109
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800110 } else {
111 // We have a game piece, lets start aiming.
112 if (drivetrain_status_fetcher_.get() != nullptr) {
113 aiming = true;
114 aimer_.Update(drivetrain_status_fetcher_.get(),
115 frc971::control_loops::aiming::ShotMode::kShootOnTheFly,
116 turret_goal_builder.get());
117 }
118 }
119
120 // We have a game piece and are being asked to aim.
121 constants::Values::ShotParams shot_params;
122 if (piece_loaded && shooter_goal != nullptr && shooter_goal->auto_aim() &&
123 interpolation_table_.GetInRange(distance_to_goal, &shot_params)) {
124 PopulateStaticZeroingSingleDOFProfiledSubsystemGoal(
125 altitude_goal_builder.get(), shot_params.shot_altitude_angle);
126 }
127
128 // The builder will contain either the auto-aim goal, or the loading goal. Use
129 // it if we have no goal, or no subsystem goal, or if we are auto-aiming.
Filip Kujawa7a799602024-02-23 12:27:47 -0800130
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800131 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
132 *turret_goal = (shooter_goal != nullptr && !shooter_goal->auto_aim() &&
Austin Schuh027fd622024-03-01 21:26:07 -0800133 shooter_goal->has_turret_position())
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800134 ? shooter_goal->turret_position()
135 : &turret_goal_builder->AsFlatbuffer();
Filip Kujawa7a799602024-02-23 12:27:47 -0800136
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800137 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
138 *altitude_goal = (shooter_goal != nullptr && !shooter_goal->auto_aim() &&
139 shooter_goal->has_altitude_position())
140 ? shooter_goal->altitude_position()
141 : &altitude_goal_builder->AsFlatbuffer();
142
Niko Sohmerscc3aa452024-03-03 17:20:04 -0800143 const bool turret_in_range =
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800144 (std::abs(turret_.estimated_position() - turret_goal->unsafe_goal()) <
Niko Sohmerscc3aa452024-03-03 17:20:04 -0800145 kCatapultActivationThreshold);
146 const bool altitude_in_range =
147 (std::abs(altitude_.estimated_position() - altitude_goal->unsafe_goal()) <
148 kCatapultActivationThreshold);
149 const bool altitude_above_min_angle =
150 (altitude_.estimated_position() >
151 robot_constants_->common()->min_altitude_shooting_angle());
152
153 bool subsystems_in_range =
154 (turret_in_range && altitude_in_range && altitude_above_min_angle);
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800155
Niko Sohmersac4d8872024-02-23 13:55:47 -0800156 const bool disabled = turret_.Correct(turret_goal, position->turret(),
157 turret_output == nullptr);
158
159 collision_avoidance->UpdateGoal(
160 {.intake_pivot_position = intake_pivot_position,
Austin Schuh027fd622024-03-01 21:26:07 -0800161 .turret_position = turret_.estimated_position(),
162 .extend_position = extend_position},
163 turret_goal->unsafe_goal(), extend_goal);
Niko Sohmersac4d8872024-02-23 13:55:47 -0800164
165 turret_.set_min_position(collision_avoidance->min_turret_goal());
166 turret_.set_max_position(collision_avoidance->max_turret_goal());
167
168 *max_intake_pivot_position = collision_avoidance->max_intake_pivot_goal();
169 *min_intake_pivot_position = collision_avoidance->min_intake_pivot_goal();
170
Austin Schuh027fd622024-03-01 21:26:07 -0800171 *max_extend_position = collision_avoidance->max_extend_goal();
172 *min_extend_position = collision_avoidance->min_extend_goal();
173
Niko Sohmersac4d8872024-02-23 13:55:47 -0800174 // Calculate the loops for a cycle.
175 const double voltage = turret_.UpdateController(disabled);
176
177 turret_.UpdateObserver(voltage);
178
179 // Write out all the voltages.
180 if (turret_output) {
181 *turret_output = voltage;
182 }
183
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800184 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
Niko Sohmersac4d8872024-02-23 13:55:47 -0800185 turret_status_offset = turret_.MakeStatus(fbb);
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800186
187 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
188 altitude_status_offset = altitude_.Iterate(
189 altitude_goal, position->altitude(), altitude_output, fbb);
190
191 flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
192 catapult_status_offset;
193 {
194 // The catapult will never use a provided goal. We'll always fabricate one
195 // for it.
196 //
197 // Correct handles resetting our state when disabled.
198 const bool disabled = catapult_.Correct(nullptr, position->catapult(),
Maxwell Henderson3d68e142024-02-25 09:58:11 -0800199 catapult_output == nullptr);
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800200
201 catapult_.set_enable_profile(true);
202 // We want a trajectory which accelerates up over the first portion of the
203 // range of motion, holds top speed for a little bit, then decelerates
204 // before it swings too far.
205 //
206 // We can solve for these 3 parameters through the range of motion. Top
207 // speed is goverened by the voltage headroom we want to have for the
208 // controller.
209 //
210 // Accel can be tuned given the distance to accelerate over, and decel can
211 // be solved similarly.
212 //
213 // accel = v^2 / (2 * x)
214 catapult_.mutable_profile()->set_maximum_velocity(
215 catapult::kFreeSpeed * catapult::kOutputRatio * 4.0 / 12.0);
216
217 if (disabled) {
218 state_ = CatapultState::RETRACTING;
219 }
220
Maxwell Henderson6b1be312024-02-28 20:15:06 -0800221 constexpr double kLoadingAcceleration = 20.0;
222 constexpr double kLoadingDeceleration = 10.0;
223
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800224 switch (state_) {
225 case CatapultState::READY:
Filip Kujawa7a799602024-02-23 12:27:47 -0800226 [[fallthrough]];
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800227 case CatapultState::LOADED: {
228 if (piece_loaded) {
229 state_ = CatapultState::LOADED;
230 } else {
231 state_ = CatapultState::READY;
232 }
233
234 const bool catapult_close = CatapultClose();
235
Filip Kujawa7a799602024-02-23 12:27:47 -0800236 if (subsystems_in_range && shooter_goal != nullptr && fire &&
237 catapult_close && piece_loaded) {
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800238 state_ = CatapultState::FIRING;
239 } else {
240 catapult_.set_controller_index(0);
Maxwell Henderson6b1be312024-02-28 20:15:06 -0800241 catapult_.mutable_profile()->set_maximum_acceleration(
242 kLoadingAcceleration);
243 catapult_.mutable_profile()->set_maximum_deceleration(
244 kLoadingDeceleration);
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800245 catapult_.set_unprofiled_goal(0.0, 0.0);
246
247 if (!catapult_close) {
248 state_ = CatapultState::RETRACTING;
249 }
250 break;
251 }
252 [[fallthrough]];
253 }
254 case CatapultState::FIRING:
255 *retention_roller_output =
256 robot_constants_->common()->retention_roller_voltages()->spitting();
Maxwell Henderson6b1be312024-02-28 20:15:06 -0800257 *retention_roller_stator_current_limit =
258 robot_constants_->common()
259 ->current_limits()
260 ->shooting_retention_roller_stator_current_limit();
261 catapult_.set_controller_index(1);
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800262 catapult_.mutable_profile()->set_maximum_acceleration(400.0);
Maxwell Henderson6b1be312024-02-28 20:15:06 -0800263 catapult_.mutable_profile()->set_maximum_deceleration(500.0);
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800264 catapult_.set_unprofiled_goal(2.0, 0.0);
265 if (CatapultClose()) {
266 state_ = CatapultState::RETRACTING;
267 } else {
268 break;
269 }
270 [[fallthrough]];
271 case CatapultState::RETRACTING:
272 catapult_.set_controller_index(0);
Maxwell Henderson6b1be312024-02-28 20:15:06 -0800273 catapult_.mutable_profile()->set_maximum_acceleration(
274 kLoadingAcceleration);
275 catapult_.mutable_profile()->set_maximum_deceleration(
276 kLoadingDeceleration);
277 // TODO: catapult_return_position
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800278 catapult_.set_unprofiled_goal(0.0, 0.0);
279
280 if (CatapultClose()) {
281 if (piece_loaded) {
282 state_ = CatapultState::LOADED;
283 } else {
284 state_ = CatapultState::READY;
285 }
286 }
287 break;
288 }
289
290 const double voltage = catapult_.UpdateController(disabled);
291 catapult_.UpdateObserver(voltage);
292 if (catapult_output != nullptr) {
293 *catapult_output = voltage;
294 }
295 catapult_status_offset = catapult_.MakeStatus(fbb);
296 }
297
298 flatbuffers::Offset<AimerStatus> aimer_offset;
299 if (aiming) {
300 aimer_offset = aimer_.PopulateStatus(fbb);
301 }
302
303 y2024::control_loops::superstructure::ShooterStatus::Builder status_builder(
304 *fbb);
305 status_builder.add_turret(turret_status_offset);
306 status_builder.add_altitude(altitude_status_offset);
307 status_builder.add_catapult(catapult_status_offset);
308 status_builder.add_catapult_state(state_);
Niko Sohmerscc3aa452024-03-03 17:20:04 -0800309 status_builder.add_turret_in_range(turret_in_range);
310 status_builder.add_altitude_in_range(altitude_in_range);
311 status_builder.add_altitude_above_min_angle(altitude_above_min_angle);
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800312 if (aiming) {
313 status_builder.add_aimer(aimer_offset);
314 }
315
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800316 return status_builder.Finish();
317}
318
319} // namespace y2024::control_loops::superstructure