blob: 6abdd3b952e76334d9749be9ada30f51bebc4bed [file] [log] [blame]
Niko Sohmers3860f8a2024-01-12 21:05:19 -08001#include "y2024/control_loops/superstructure/superstructure.h"
2
Filip Kujawa7a799602024-02-23 12:27:47 -08003#include <chrono>
4
Niko Sohmers3860f8a2024-01-12 21:05:19 -08005#include "aos/events/event_loop.h"
6#include "aos/flatbuffer_merge.h"
7#include "aos/network/team_number.h"
Filip Kujawa7a799602024-02-23 12:27:47 -08008#include "aos/time/time.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -08009#include "frc971/shooter_interpolation/interpolation.h"
10#include "frc971/zeroing/wrap.h"
11
12DEFINE_bool(ignore_distance, false,
Filip Kujawa7a799602024-02-23 12:27:47 -080013 "If true, ignore distance when shooting and obey joystick_reader");
14
15// The threshold used when decided if the extend is close enough to a goal to
16// continue.
17constexpr double kExtendThreshold = 0.01;
18
19constexpr double kTurretLoadingThreshold = 0.01;
20constexpr double kAltitudeLoadingThreshold = 0.01;
Niko Sohmers3860f8a2024-01-12 21:05:19 -080021
Maxwell Hendersonf0d22622024-02-26 21:02:11 -080022constexpr std::chrono::milliseconds kExtraIntakingTime =
23 std::chrono::milliseconds(500);
24
Stephan Pleinesf63bde82024-01-13 15:59:33 -080025namespace y2024::control_loops::superstructure {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080026
27using ::aos::monotonic_clock;
28
29using frc971::control_loops::AbsoluteEncoderProfiledJointStatus;
30using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus;
31using frc971::control_loops::RelativeEncoderProfiledJointStatus;
32
33Superstructure::Superstructure(::aos::EventLoop *event_loop,
Niko Sohmers3860f8a2024-01-12 21:05:19 -080034 const ::std::string &name)
35 : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop,
36 name),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080037 constants_fetcher_(event_loop),
Niko Sohmersafc51fe2024-01-29 17:48:35 -080038 robot_constants_(CHECK_NOTNULL(&constants_fetcher_.constants())),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080039 drivetrain_status_fetcher_(
40 event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>(
41 "/drivetrain")),
42 joystick_state_fetcher_(
Niko Sohmersafc51fe2024-01-29 17:48:35 -080043 event_loop->MakeFetcher<aos::JoystickState>("/aos")),
Niko Sohmers74b0ad52024-02-03 18:00:31 -080044 intake_pivot_(robot_constants_->common()->intake_pivot(),
45 robot_constants_->robot()->intake_constants()),
Filip Kujawa6d717632024-02-01 11:40:55 -080046 climber_(
47 robot_constants_->common()->climber(),
Niko Sohmersc4d2c502024-02-19 19:35:35 -080048 robot_constants_->robot()->climber_constants()->zeroing_constants()),
Filip Kujawa7a799602024-02-23 12:27:47 -080049 shooter_(event_loop, robot_constants_),
50 extend_(
51 robot_constants_->common()->extend(),
52 robot_constants_->robot()->extend_constants()->zeroing_constants()) {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080053 event_loop->SetRuntimeRealtimePriority(30);
54}
55
Filip Kujawa7a799602024-02-23 12:27:47 -080056bool PositionNear(double position, double goal, double threshold) {
57 return std::abs(position - goal) < threshold;
58}
59
Niko Sohmers3860f8a2024-01-12 21:05:19 -080060void Superstructure::RunIteration(const Goal *unsafe_goal,
61 const Position *position,
62 aos::Sender<Output>::Builder *output,
63 aos::Sender<Status>::Builder *status) {
64 const monotonic_clock::time_point timestamp =
65 event_loop()->context().monotonic_event_time;
66
Niko Sohmers3860f8a2024-01-12 21:05:19 -080067 if (WasReset()) {
68 AOS_LOG(ERROR, "WPILib reset, restarting\n");
Niko Sohmersafc51fe2024-01-29 17:48:35 -080069 intake_pivot_.Reset();
Filip Kujawa6d717632024-02-01 11:40:55 -080070 climber_.Reset();
Niko Sohmersc4d2c502024-02-19 19:35:35 -080071 shooter_.Reset();
Filip Kujawa7a799602024-02-23 12:27:47 -080072 extend_.Reset();
Niko Sohmers3860f8a2024-01-12 21:05:19 -080073 }
74
75 OutputT output_struct;
Niko Sohmersafc51fe2024-01-29 17:48:35 -080076
Filip Kujawa7a799602024-02-23 12:27:47 -080077 // Handle Climber Goal separately from main superstructure state machine
Filip Kujawa6d717632024-02-01 11:40:55 -080078 double climber_position =
79 robot_constants_->common()->climber_set_points()->retract();
80
81 if (unsafe_goal != nullptr) {
82 switch (unsafe_goal->climber_goal()) {
83 case ClimberGoal::FULL_EXTEND:
84 climber_position =
85 robot_constants_->common()->climber_set_points()->full_extend();
86 break;
Filip Kujawa6d717632024-02-01 11:40:55 -080087 case ClimberGoal::RETRACT:
88 climber_position =
89 robot_constants_->common()->climber_set_points()->retract();
90 break;
Maxwell Henderson7db29782024-02-24 20:10:26 -080091 case ClimberGoal::STOWED:
92 climber_position =
93 robot_constants_->common()->climber_set_points()->stowed();
Filip Kujawa6d717632024-02-01 11:40:55 -080094 }
95 }
96
Filip Kujawa7a799602024-02-23 12:27:47 -080097 // If we started off preloaded, skip to the ready state.
98 if (unsafe_goal != nullptr && unsafe_goal->shooter_goal() &&
99 unsafe_goal->shooter_goal()->preloaded()) {
100 if (state_ != SuperstructureState::READY &&
101 state_ != SuperstructureState::FIRING) {
102 state_ = SuperstructureState::READY;
Niko Sohmersdd971dc2024-02-25 11:40:47 -0800103 catapult_requested_ = true;
Filip Kujawa7a799602024-02-23 12:27:47 -0800104 }
105 }
106
107 // Handle the intake pivot goal separately from the main superstructure state
108 IntakeRollerStatus intake_roller_state = IntakeRollerStatus::NONE;
109 double intake_pivot_position =
110 robot_constants_->common()->intake_pivot_set_points()->retracted();
111
112 if (unsafe_goal != nullptr) {
113 switch (unsafe_goal->intake_goal()) {
114 case IntakeGoal::INTAKE:
115 intake_pivot_position =
116 robot_constants_->common()->intake_pivot_set_points()->extended();
Maxwell Hendersonf0d22622024-02-26 21:02:11 -0800117 intake_end_time_ = timestamp;
Filip Kujawa7a799602024-02-23 12:27:47 -0800118 break;
119 case IntakeGoal::SPIT:
120 intake_pivot_position =
121 robot_constants_->common()->intake_pivot_set_points()->retracted();
122 break;
123 case IntakeGoal::NONE:
124 intake_pivot_position =
125 robot_constants_->common()->intake_pivot_set_points()->retracted();
126 break;
127 }
128 }
129
130 ExtendRollerStatus extend_roller_status = ExtendRollerStatus::IDLE;
131 ExtendStatus extend_goal = ExtendStatus::RETRACTED;
132
133 // True if the extend is moving towards a goal
134 bool extend_moving = false;
135
136 TransferRollerStatus transfer_roller_status = TransferRollerStatus::NONE;
137
138 const ExtendSetPoints *extend_set_points =
139 robot_constants_->common()->extend_set_points();
140
141 // Checks if the extend is close enough to the retracted position to be
142 // considered ready to accept note from the transfer rollers.
Niko Sohmersdd971dc2024-02-25 11:40:47 -0800143 const bool extend_at_retracted = PositionNear(
Filip Kujawa7a799602024-02-23 12:27:47 -0800144 extend_.position(), extend_set_points->retracted(), kExtendThreshold);
145
146 // If true, the turret should be moved to the position to avoid collision with
147 // the extend.
148 bool move_turret_to_standby = false;
149
150 // Superstructure state machine:
151 // 1. IDLE. The intake is retracted and there is no note in the robot.
152 // Wait for a intake goal to switch state to INTAKING if the extend is ready
153 // 2. INTAKING. Intake the note and transfer it towards the extend.
154 // Give intake, transfer, and extend rollers positive voltage to intake and
155 // transfer. Switch to LOADED when the extend beambreak is triggered.
156 // 3. LOADED. The note is in the extend and the extend is retracted.
157 // Wait for a note goal to switch state to MOVING.
158 // For AMP/TRAP goals, check that the turret is in a position to avoid
159 // collision.
160 // 4. MOVING. The extend is moving towards a goal (AMP, TRAP, or CATAPULT).
161 // For CATAPULT goals, wait for the turret and altitude to be in a position to
162 // accept the note from the extend.
163 // Wait for the extend to reach the goal and switch state to READY if
164 // AMP or TRAP, or to LOADING_CATAPULT if CATAPULT.
165 // 5. LOADING_CATAPULT. The extend is at the position to load the catapult.
166 // Activate the extend roller to transfer the note to the catapult.
167 // Switch state to READY when the catapult beambreak is triggered.
168 // 6. READY. Ready for fire command. The note is either loaded in the catapult
169 // or in the extend and at the AMP or TRAP position. Wait for a fire command.
170 // 7. FIRING. The note is being fired, either from the extend or the catapult.
171 // Switch state back to IDLE when the note is fired.
172
James Kuszmaul0281e152024-02-26 22:26:16 -0800173 std::optional<bool> turret_ready_for_extend_move;
Filip Kujawa7a799602024-02-23 12:27:47 -0800174 switch (state_) {
175 case SuperstructureState::IDLE:
176 if (unsafe_goal != nullptr &&
177 unsafe_goal->intake_goal() == IntakeGoal::INTAKE &&
Niko Sohmersdd971dc2024-02-25 11:40:47 -0800178 extend_at_retracted) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800179 state_ = SuperstructureState::INTAKING;
180 }
Maxwell Henderson4567e282024-02-25 15:38:15 -0800181
Filip Kujawa7a799602024-02-23 12:27:47 -0800182 extend_goal = ExtendStatus::RETRACTED;
183 catapult_requested_ = false;
184 break;
185 case SuperstructureState::INTAKING:
Filip Kujawa7a799602024-02-23 12:27:47 -0800186 // Switch to LOADED state when the extend beambreak is triggered
187 // meaning the note is loaded in the extend
188 if (position->extend_beambreak()) {
189 state_ = SuperstructureState::LOADED;
190 }
191 intake_roller_state = IntakeRollerStatus::INTAKING;
192 transfer_roller_status = TransferRollerStatus::TRANSFERING_IN;
193 extend_roller_status = ExtendRollerStatus::TRANSFERING_TO_EXTEND;
194 extend_goal = ExtendStatus::RETRACTED;
195
196 if (!catapult_requested_ && unsafe_goal != nullptr &&
197 unsafe_goal->note_goal() == NoteGoal::CATAPULT) {
198 catapult_requested_ = true;
199 }
200
Maxwell Hendersonf0d22622024-02-26 21:02:11 -0800201 // If we are no longer requesting INTAKE or we are no longer requesting
202 // an INTAKE goal, wait 0.5 seconds then go back to IDLE.
203 if (!(unsafe_goal != nullptr &&
204 unsafe_goal->intake_goal() == IntakeGoal::INTAKE) &&
205 timestamp > intake_end_time_ + kExtraIntakingTime) {
206 state_ = SuperstructureState::IDLE;
207 }
208
Filip Kujawa7a799602024-02-23 12:27:47 -0800209 break;
210 case SuperstructureState::LOADED:
Maxwell Henderson4567e282024-02-25 15:38:15 -0800211 if (!position->extend_beambreak() && !position->catapult_beambreak()) {
212 state_ = SuperstructureState::IDLE;
213 }
214
Filip Kujawa7a799602024-02-23 12:27:47 -0800215 if (catapult_requested_ == true) {
216 state_ = SuperstructureState::MOVING;
217 break;
218 }
219
220 if (unsafe_goal != nullptr &&
221 unsafe_goal->note_goal() != NoteGoal::NONE) {
222 // If the goal is AMP or TRAP, check if the turret is in a position to
223 // avoid collision when the extend moves.
224 if (unsafe_goal->note_goal() == NoteGoal::AMP ||
225 unsafe_goal->note_goal() == NoteGoal::TRAP) {
James Kuszmaul0281e152024-02-26 22:26:16 -0800226 turret_ready_for_extend_move =
Filip Kujawa7a799602024-02-23 12:27:47 -0800227 PositionNear(shooter_.turret().estimated_position(),
228 robot_constants_->common()
229 ->turret_avoid_extend_collision_position(),
230 kTurretLoadingThreshold);
James Kuszmaul0281e152024-02-26 22:26:16 -0800231 transfer_roller_status = TransferRollerStatus::TRANSFERING_IN;
Filip Kujawa7a799602024-02-23 12:27:47 -0800232
James Kuszmaul0281e152024-02-26 22:26:16 -0800233 if (turret_ready_for_extend_move.value()) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800234 state_ = SuperstructureState::MOVING;
235 } else {
236 move_turret_to_standby = true;
237 }
238 } else if (unsafe_goal->note_goal() == NoteGoal::CATAPULT) {
239 // If catapult is requested, switch to MOVING state
240 state_ = SuperstructureState::MOVING;
241 }
242 }
243 extend_goal = ExtendStatus::RETRACTED;
244 if (!catapult_requested_ && unsafe_goal != nullptr &&
245 unsafe_goal->note_goal() == NoteGoal::CATAPULT) {
246 catapult_requested_ = true;
247 }
248 break;
249 case SuperstructureState::MOVING:
James Kuszmaul0281e152024-02-26 22:26:16 -0800250 transfer_roller_status = TransferRollerStatus::TRANSFERING_IN;
Filip Kujawa7a799602024-02-23 12:27:47 -0800251
252 if (catapult_requested_) {
253 extend_goal = ExtendStatus::CATAPULT;
254
255 // Check if the extend is at the position to load the catapult
256 bool extend_ready_for_catapult_transfer =
257 PositionNear(extend_.position(), extend_set_points->catapult(),
258 kExtendThreshold);
259
260 // Check if the turret is at the position to accept the note from extend
261 bool turret_ready_for_load =
262 PositionNear(shooter_.turret().estimated_position(),
263 robot_constants_->common()->turret_loading_position(),
264 kTurretLoadingThreshold);
265
266 // Check if the altitude is at the position to accept the note from
267 // extend
268 bool altitude_ready_for_load = PositionNear(
269 shooter_.altitude().estimated_position(),
270 robot_constants_->common()->altitude_loading_position(),
271 kAltitudeLoadingThreshold);
272
273 if (extend_ready_for_catapult_transfer && turret_ready_for_load &&
274 altitude_ready_for_load) {
275 state_ = SuperstructureState::LOADING_CATAPULT;
276 }
277 } else {
278 if (unsafe_goal != nullptr) {
279 switch (unsafe_goal->note_goal()) {
280 case NoteGoal::AMP:
281 extend_goal = ExtendStatus::AMP;
282 move_turret_to_standby = true;
283 // Check if the extend is at the AMP position and if it is
284 // switch to READY state
285 if (PositionNear(extend_.position(), extend_set_points->amp(),
286 kExtendThreshold)) {
287 state_ = SuperstructureState::READY;
288 }
289 break;
290 case NoteGoal::TRAP:
291 extend_goal = ExtendStatus::TRAP;
292 move_turret_to_standby = true;
293 // Check if the extend is at the TRAP position and if it is
294 // switch to READY state
295 if (PositionNear(extend_.position(), extend_set_points->trap(),
296 kExtendThreshold)) {
297 state_ = SuperstructureState::READY;
298 }
299 break;
300 case NoteGoal::NONE:
301 extend_goal = ExtendStatus::RETRACTED;
302 move_turret_to_standby = true;
Niko Sohmersdd971dc2024-02-25 11:40:47 -0800303 if (extend_at_retracted) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800304 state_ = SuperstructureState::LOADED;
305 }
306 break;
307 case NoteGoal::CATAPULT:
308 catapult_requested_ = true;
309 extend_goal = ExtendStatus::CATAPULT;
310 break;
311 }
312 }
313 }
314
315 extend_moving = true;
316 break;
317 case SuperstructureState::LOADING_CATAPULT:
318 extend_moving = false;
319 extend_goal = ExtendStatus::CATAPULT;
320 extend_roller_status = ExtendRollerStatus::TRANSFERING_TO_CATAPULT;
321
322 // Switch to READY state when the catapult beambreak is triggered
323 if (position->catapult_beambreak()) {
324 state_ = SuperstructureState::READY;
325 }
326 break;
327 case SuperstructureState::READY:
328 extend_moving = false;
329
330 // Switch to FIRING state when the fire button is pressed
331 if (unsafe_goal != nullptr && unsafe_goal->fire()) {
332 state_ = SuperstructureState::FIRING;
333 }
334
335 if (catapult_requested_) {
336 extend_goal = ExtendStatus::CATAPULT;
337 } else {
338 if (unsafe_goal != nullptr) {
339 if (unsafe_goal->note_goal() == NoteGoal::AMP) {
340 extend_goal = ExtendStatus::AMP;
341 move_turret_to_standby = true;
342 } else if (unsafe_goal->note_goal() == NoteGoal::TRAP) {
343 extend_goal = ExtendStatus::TRAP;
344 move_turret_to_standby = true;
345 } else {
346 extend_goal = ExtendStatus::RETRACTED;
347 extend_moving = true;
348 state_ = SuperstructureState::MOVING;
349 }
350 }
351 }
352
353 break;
354 case SuperstructureState::FIRING:
355 if (catapult_requested_) {
356 extend_goal = ExtendStatus::CATAPULT;
357
358 // Reset the state to IDLE when the game piece is fired from the
359 // catapult. We consider the game piece to be fired from the catapult
360 // when the catapultbeambreak is no longer triggered.
361 if (!position->catapult_beambreak()) {
362 state_ = SuperstructureState::IDLE;
363 }
364 } else {
James Kuszmaul0281e152024-02-26 22:26:16 -0800365 move_turret_to_standby = true;
Filip Kujawa7a799602024-02-23 12:27:47 -0800366 if (unsafe_goal != nullptr &&
367 unsafe_goal->note_goal() == NoteGoal::AMP) {
368 extend_roller_status = ExtendRollerStatus::SCORING_IN_AMP;
369 extend_goal = ExtendStatus::AMP;
370 } else if (unsafe_goal != nullptr &&
371 unsafe_goal->note_goal() == NoteGoal::TRAP) {
372 extend_roller_status = ExtendRollerStatus::SCORING_IN_TRAP;
373 extend_goal = ExtendStatus::TRAP;
374 }
375
376 // Reset the state to IDLE when the game piece is fired from the extend.
377 // We consider the game piece to be fired from the extend when
378 // the extend beambreak is no longer triggered and the fire button is
379 // released.
380 if (!position->extend_beambreak() && unsafe_goal != nullptr &&
381 !unsafe_goal->fire()) {
382 state_ = SuperstructureState::IDLE;
383 }
384 }
385 break;
386 }
387
388 if (unsafe_goal != nullptr &&
389 unsafe_goal->intake_goal() == IntakeGoal::SPIT) {
390 intake_roller_state = IntakeRollerStatus::SPITTING;
391 transfer_roller_status = TransferRollerStatus::TRANSFERING_OUT;
392 }
393
394 // Update Intake Roller voltage based on status from state machine.
395 switch (intake_roller_state) {
396 case IntakeRollerStatus::NONE:
397 output_struct.intake_roller_voltage = 0.0;
398 break;
399 case IntakeRollerStatus::SPITTING:
400 output_struct.intake_roller_voltage =
401 robot_constants_->common()->intake_roller_voltages()->spitting();
402 break;
403 case IntakeRollerStatus::INTAKING:
404 output_struct.intake_roller_voltage =
405 robot_constants_->common()->intake_roller_voltages()->intaking();
406 break;
407 }
408
409 // Update Transfer Roller voltage based on status from state machine.
410 switch (transfer_roller_status) {
411 case TransferRollerStatus::NONE:
412 output_struct.transfer_roller_voltage = 0.0;
413 break;
414 case TransferRollerStatus::TRANSFERING_IN:
415 output_struct.transfer_roller_voltage =
416 robot_constants_->common()->transfer_roller_voltages()->transfer_in();
417 break;
418 case TransferRollerStatus::TRANSFERING_OUT:
419 output_struct.transfer_roller_voltage = robot_constants_->common()
420 ->transfer_roller_voltages()
421 ->transfer_out();
422 break;
423 }
424
425 // Update Extend Roller voltage based on status from state machine.
426 const ExtendRollerVoltages *extend_roller_voltages =
427 robot_constants_->common()->extend_roller_voltages();
428 switch (extend_roller_status) {
429 case ExtendRollerStatus::IDLE:
430 // No voltage applied when idle
431 output_struct.extend_roller_voltage = 0.0;
432 break;
433 case ExtendRollerStatus::TRANSFERING_TO_EXTEND:
434 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
435 break;
436 case ExtendRollerStatus::SCORING_IN_AMP:
437 [[fallthrough]];
438 case ExtendRollerStatus::SCORING_IN_TRAP:
439 // Apply scoring voltage during scoring in amp or trap
440 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
441 break;
442 case ExtendRollerStatus::TRANSFERING_TO_CATAPULT:
443 // Apply scoring voltage during transferring to catapult
444 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
445 break;
446 }
447
448 double extend_position = 0.0;
449
James Kuszmaul0281e152024-02-26 22:26:16 -0800450 if (unsafe_goal != nullptr && unsafe_goal->note_goal() == NoteGoal::TRAP) {
451 extend_goal = ExtendStatus::TRAP;
452 move_turret_to_standby = true;
453 }
454
455 // In lieu of having full collision avoidance ready, move the turret out of
456 // the way whenever the extend is raised too much.
457 if (extend_.position() > 0.05) {
458 move_turret_to_standby = true;
459 }
460
Filip Kujawa7a799602024-02-23 12:27:47 -0800461 // Set the extend position based on the state machine output
462 switch (extend_goal) {
463 case ExtendStatus::RETRACTED:
464 extend_position = extend_set_points->retracted();
465 break;
466 case ExtendStatus::AMP:
467 extend_position = extend_set_points->amp();
468 break;
469 case ExtendStatus::TRAP:
470 extend_position = extend_set_points->trap();
471 break;
472 case ExtendStatus::CATAPULT:
473 extend_position = extend_set_points->catapult();
474 break;
475 case ExtendStatus::MOVING:
476 // Should never happen
477 break;
478 }
479
480 // Set the extend status based on the state machine output
481 // If the extend is moving, the status is MOVING, otherwise it is the same
482 // as extend_status
483 ExtendStatus extend_status =
484 (extend_moving ? ExtendStatus::MOVING : extend_goal);
485
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800486 if (joystick_state_fetcher_.Fetch() &&
487 joystick_state_fetcher_->has_alliance()) {
488 alliance_ = joystick_state_fetcher_->alliance();
489 }
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800490
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800491 drivetrain_status_fetcher_.Fetch();
492
Niko Sohmersac4d8872024-02-23 13:55:47 -0800493 const bool collided = collision_avoidance_.IsCollided(
494 {.intake_pivot_position = intake_pivot_.estimated_position(),
495 .turret_position = shooter_.turret().estimated_position()});
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800496
Filip Kujawa6d717632024-02-01 11:40:55 -0800497 aos::FlatbufferFixedAllocatorArray<
498 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
499 climber_goal_buffer;
500
501 climber_goal_buffer.Finish(
502 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
503 *climber_goal_buffer.fbb(), climber_position));
504
505 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
506 *climber_goal = &climber_goal_buffer.message();
507
508 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
509 climber_status_offset = climber_.Iterate(
510 climber_goal, position->climber(),
511 output != nullptr ? &output_struct.climber_voltage : nullptr,
512 status->fbb());
513
Niko Sohmersac4d8872024-02-23 13:55:47 -0800514 double max_intake_pivot_position = 0;
515 double min_intake_pivot_position = 0;
516
517 aos::FlatbufferFixedAllocatorArray<
518 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
519 intake_pivot_goal_buffer;
520
521 intake_pivot_goal_buffer.Finish(
522 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
523 *intake_pivot_goal_buffer.fbb(), intake_pivot_position));
524
525 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
526 *intake_pivot_goal = &intake_pivot_goal_buffer.message();
527
528 double *intake_output =
529 (output != nullptr ? &output_struct.intake_pivot_voltage : nullptr);
530
531 const bool disabled = intake_pivot_.Correct(
532 intake_pivot_goal, position->intake_pivot(), intake_output == nullptr);
533
Filip Kujawa7a799602024-02-23 12:27:47 -0800534 // TODO(max): Change how we handle the collision with the turret and
535 // intake to be clearer
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800536 const flatbuffers::Offset<ShooterStatus> shooter_status_offset =
537 shooter_.Iterate(
538 position,
539 unsafe_goal != nullptr ? unsafe_goal->shooter_goal() : nullptr,
Filip Kujawa7a799602024-02-23 12:27:47 -0800540 unsafe_goal != nullptr ? unsafe_goal->fire() : false,
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800541 output != nullptr ? &output_struct.catapult_voltage : nullptr,
542 output != nullptr ? &output_struct.altitude_voltage : nullptr,
543 output != nullptr ? &output_struct.turret_voltage : nullptr,
544 output != nullptr ? &output_struct.retention_roller_voltage : nullptr,
Maxwell Hendersond5bf47a2024-02-23 17:16:48 -0800545 output != nullptr
546 ? &output_struct.retention_roller_stator_current_limit
547 : nullptr,
Maxwell Henderson461a8a42024-02-23 17:07:39 -0800548 robot_state().voltage_battery(), &collision_avoidance_,
Niko Sohmersac4d8872024-02-23 13:55:47 -0800549 intake_pivot_.estimated_position(), &max_intake_pivot_position,
Maxwell Henderson6b1be312024-02-28 20:15:06 -0800550 &min_intake_pivot_position, move_turret_to_standby, status->fbb(),
551 timestamp);
Niko Sohmersac4d8872024-02-23 13:55:47 -0800552
553 intake_pivot_.set_min_position(min_intake_pivot_position);
554 intake_pivot_.set_max_position(max_intake_pivot_position);
555
556 // Calculate the loops for a cycle.
557 const double voltage = intake_pivot_.UpdateController(disabled);
558
559 intake_pivot_.UpdateObserver(voltage);
560
561 // Write out all the voltages.
562 if (intake_output) {
563 *intake_output = voltage;
564 }
565
566 const flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus>
567 intake_pivot_status_offset = intake_pivot_.MakeStatus(status->fbb());
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800568
Filip Kujawa7a799602024-02-23 12:27:47 -0800569 aos::FlatbufferFixedAllocatorArray<
570 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
571 note_goal_buffer;
572
573 note_goal_buffer.Finish(
574 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
575 *note_goal_buffer.fbb(), extend_position));
576
577 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
578 *note_goal = &note_goal_buffer.message();
579
580 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
581 extend_status_offset = extend_.Iterate(
582 note_goal, position->extend(),
583 output != nullptr ? &output_struct.extend_voltage : nullptr,
584 status->fbb());
585
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800586 if (output) {
587 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
588 }
589
590 Status::Builder status_builder = status->MakeBuilder<Status>();
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800591
Filip Kujawa7a799602024-02-23 12:27:47 -0800592 const bool zeroed = intake_pivot_.zeroed() && climber_.zeroed() &&
593 shooter_.zeroed() && extend_.zeroed();
594 const bool estopped = intake_pivot_.estopped() || climber_.estopped() ||
595 shooter_.estopped() || extend_.estopped();
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800596
597 status_builder.add_zeroed(zeroed);
598 status_builder.add_estopped(estopped);
Filip Kujawa102a9b22024-02-18 09:40:23 -0800599 status_builder.add_intake_roller(intake_roller_state);
600 status_builder.add_intake_pivot(intake_pivot_status_offset);
Filip Kujawa7a799602024-02-23 12:27:47 -0800601 status_builder.add_transfer_roller(transfer_roller_status);
Filip Kujawa102a9b22024-02-18 09:40:23 -0800602 status_builder.add_climber(climber_status_offset);
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800603 status_builder.add_shooter(shooter_status_offset);
Niko Sohmersac4d8872024-02-23 13:55:47 -0800604 status_builder.add_collided(collided);
Filip Kujawa7a799602024-02-23 12:27:47 -0800605 status_builder.add_extend_roller(extend_roller_status);
606 status_builder.add_extend_status(extend_status);
607 status_builder.add_extend(extend_status_offset);
608 status_builder.add_state(state_);
James Kuszmaul0281e152024-02-26 22:26:16 -0800609 status_builder.add_extend_ready_for_transfer(extend_at_retracted);
610 if (turret_ready_for_extend_move) {
611 status_builder.add_turret_ready_for_extend_move(
612 turret_ready_for_extend_move.value());
613 }
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800614
615 (void)status->Send(status_builder.Finish());
616}
617
618double Superstructure::robot_velocity() const {
619 return (drivetrain_status_fetcher_.get() != nullptr
620 ? drivetrain_status_fetcher_->robot_speed()
621 : 0.0);
622}
623
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800624} // namespace y2024::control_loops::superstructure