blob: 6274da57d5c07c5ef86f5fdf6f5eccde7362b915 [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
173 switch (state_) {
174 case SuperstructureState::IDLE:
175 if (unsafe_goal != nullptr &&
176 unsafe_goal->intake_goal() == IntakeGoal::INTAKE &&
Niko Sohmersdd971dc2024-02-25 11:40:47 -0800177 extend_at_retracted) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800178 state_ = SuperstructureState::INTAKING;
179 }
180 extend_goal = ExtendStatus::RETRACTED;
181 catapult_requested_ = false;
182 break;
183 case SuperstructureState::INTAKING:
Filip Kujawa7a799602024-02-23 12:27:47 -0800184 // Switch to LOADED state when the extend beambreak is triggered
185 // meaning the note is loaded in the extend
186 if (position->extend_beambreak()) {
187 state_ = SuperstructureState::LOADED;
188 }
189 intake_roller_state = IntakeRollerStatus::INTAKING;
190 transfer_roller_status = TransferRollerStatus::TRANSFERING_IN;
191 extend_roller_status = ExtendRollerStatus::TRANSFERING_TO_EXTEND;
192 extend_goal = ExtendStatus::RETRACTED;
193
194 if (!catapult_requested_ && unsafe_goal != nullptr &&
195 unsafe_goal->note_goal() == NoteGoal::CATAPULT) {
196 catapult_requested_ = true;
197 }
198
Maxwell Hendersonf0d22622024-02-26 21:02:11 -0800199 // If we are no longer requesting INTAKE or we are no longer requesting
200 // an INTAKE goal, wait 0.5 seconds then go back to IDLE.
201 if (!(unsafe_goal != nullptr &&
202 unsafe_goal->intake_goal() == IntakeGoal::INTAKE) &&
203 timestamp > intake_end_time_ + kExtraIntakingTime) {
204 state_ = SuperstructureState::IDLE;
205 }
206
Filip Kujawa7a799602024-02-23 12:27:47 -0800207 break;
208 case SuperstructureState::LOADED:
209 if (catapult_requested_ == true) {
210 state_ = SuperstructureState::MOVING;
211 break;
212 }
213
214 if (unsafe_goal != nullptr &&
215 unsafe_goal->note_goal() != NoteGoal::NONE) {
216 // If the goal is AMP or TRAP, check if the turret is in a position to
217 // avoid collision when the extend moves.
218 if (unsafe_goal->note_goal() == NoteGoal::AMP ||
219 unsafe_goal->note_goal() == NoteGoal::TRAP) {
220 bool turret_ready_for_extend_move =
221 PositionNear(shooter_.turret().estimated_position(),
222 robot_constants_->common()
223 ->turret_avoid_extend_collision_position(),
224 kTurretLoadingThreshold);
225
226 if (turret_ready_for_extend_move) {
227 state_ = SuperstructureState::MOVING;
228 } else {
229 move_turret_to_standby = true;
230 }
231 } else if (unsafe_goal->note_goal() == NoteGoal::CATAPULT) {
232 // If catapult is requested, switch to MOVING state
233 state_ = SuperstructureState::MOVING;
234 }
235 }
236 extend_goal = ExtendStatus::RETRACTED;
237 if (!catapult_requested_ && unsafe_goal != nullptr &&
238 unsafe_goal->note_goal() == NoteGoal::CATAPULT) {
239 catapult_requested_ = true;
240 }
241 break;
242 case SuperstructureState::MOVING:
243
244 if (catapult_requested_) {
245 extend_goal = ExtendStatus::CATAPULT;
246
247 // Check if the extend is at the position to load the catapult
248 bool extend_ready_for_catapult_transfer =
249 PositionNear(extend_.position(), extend_set_points->catapult(),
250 kExtendThreshold);
251
252 // Check if the turret is at the position to accept the note from extend
253 bool turret_ready_for_load =
254 PositionNear(shooter_.turret().estimated_position(),
255 robot_constants_->common()->turret_loading_position(),
256 kTurretLoadingThreshold);
257
258 // Check if the altitude is at the position to accept the note from
259 // extend
260 bool altitude_ready_for_load = PositionNear(
261 shooter_.altitude().estimated_position(),
262 robot_constants_->common()->altitude_loading_position(),
263 kAltitudeLoadingThreshold);
264
265 if (extend_ready_for_catapult_transfer && turret_ready_for_load &&
266 altitude_ready_for_load) {
267 state_ = SuperstructureState::LOADING_CATAPULT;
268 }
269 } else {
270 if (unsafe_goal != nullptr) {
271 switch (unsafe_goal->note_goal()) {
272 case NoteGoal::AMP:
273 extend_goal = ExtendStatus::AMP;
274 move_turret_to_standby = true;
275 // Check if the extend is at the AMP position and if it is
276 // switch to READY state
277 if (PositionNear(extend_.position(), extend_set_points->amp(),
278 kExtendThreshold)) {
279 state_ = SuperstructureState::READY;
280 }
281 break;
282 case NoteGoal::TRAP:
283 extend_goal = ExtendStatus::TRAP;
284 move_turret_to_standby = true;
285 // Check if the extend is at the TRAP position and if it is
286 // switch to READY state
287 if (PositionNear(extend_.position(), extend_set_points->trap(),
288 kExtendThreshold)) {
289 state_ = SuperstructureState::READY;
290 }
291 break;
292 case NoteGoal::NONE:
293 extend_goal = ExtendStatus::RETRACTED;
294 move_turret_to_standby = true;
Niko Sohmersdd971dc2024-02-25 11:40:47 -0800295 if (extend_at_retracted) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800296 state_ = SuperstructureState::LOADED;
297 }
298 break;
299 case NoteGoal::CATAPULT:
300 catapult_requested_ = true;
301 extend_goal = ExtendStatus::CATAPULT;
302 break;
303 }
304 }
305 }
306
307 extend_moving = true;
308 break;
309 case SuperstructureState::LOADING_CATAPULT:
310 extend_moving = false;
311 extend_goal = ExtendStatus::CATAPULT;
312 extend_roller_status = ExtendRollerStatus::TRANSFERING_TO_CATAPULT;
313
314 // Switch to READY state when the catapult beambreak is triggered
315 if (position->catapult_beambreak()) {
316 state_ = SuperstructureState::READY;
317 }
318 break;
319 case SuperstructureState::READY:
320 extend_moving = false;
321
322 // Switch to FIRING state when the fire button is pressed
323 if (unsafe_goal != nullptr && unsafe_goal->fire()) {
324 state_ = SuperstructureState::FIRING;
325 }
326
327 if (catapult_requested_) {
328 extend_goal = ExtendStatus::CATAPULT;
329 } else {
330 if (unsafe_goal != nullptr) {
331 if (unsafe_goal->note_goal() == NoteGoal::AMP) {
332 extend_goal = ExtendStatus::AMP;
333 move_turret_to_standby = true;
334 } else if (unsafe_goal->note_goal() == NoteGoal::TRAP) {
335 extend_goal = ExtendStatus::TRAP;
336 move_turret_to_standby = true;
337 } else {
338 extend_goal = ExtendStatus::RETRACTED;
339 extend_moving = true;
340 state_ = SuperstructureState::MOVING;
341 }
342 }
343 }
344
345 break;
346 case SuperstructureState::FIRING:
347 if (catapult_requested_) {
348 extend_goal = ExtendStatus::CATAPULT;
349
350 // Reset the state to IDLE when the game piece is fired from the
351 // catapult. We consider the game piece to be fired from the catapult
352 // when the catapultbeambreak is no longer triggered.
353 if (!position->catapult_beambreak()) {
354 state_ = SuperstructureState::IDLE;
355 }
356 } else {
357 if (unsafe_goal != nullptr &&
358 unsafe_goal->note_goal() == NoteGoal::AMP) {
359 extend_roller_status = ExtendRollerStatus::SCORING_IN_AMP;
360 extend_goal = ExtendStatus::AMP;
361 } else if (unsafe_goal != nullptr &&
362 unsafe_goal->note_goal() == NoteGoal::TRAP) {
363 extend_roller_status = ExtendRollerStatus::SCORING_IN_TRAP;
364 extend_goal = ExtendStatus::TRAP;
365 }
366
367 // Reset the state to IDLE when the game piece is fired from the extend.
368 // We consider the game piece to be fired from the extend when
369 // the extend beambreak is no longer triggered and the fire button is
370 // released.
371 if (!position->extend_beambreak() && unsafe_goal != nullptr &&
372 !unsafe_goal->fire()) {
373 state_ = SuperstructureState::IDLE;
374 }
375 }
376 break;
377 }
378
379 if (unsafe_goal != nullptr &&
380 unsafe_goal->intake_goal() == IntakeGoal::SPIT) {
381 intake_roller_state = IntakeRollerStatus::SPITTING;
382 transfer_roller_status = TransferRollerStatus::TRANSFERING_OUT;
383 }
384
385 // Update Intake Roller voltage based on status from state machine.
386 switch (intake_roller_state) {
387 case IntakeRollerStatus::NONE:
388 output_struct.intake_roller_voltage = 0.0;
389 break;
390 case IntakeRollerStatus::SPITTING:
391 output_struct.intake_roller_voltage =
392 robot_constants_->common()->intake_roller_voltages()->spitting();
393 break;
394 case IntakeRollerStatus::INTAKING:
395 output_struct.intake_roller_voltage =
396 robot_constants_->common()->intake_roller_voltages()->intaking();
397 break;
398 }
399
400 // Update Transfer Roller voltage based on status from state machine.
401 switch (transfer_roller_status) {
402 case TransferRollerStatus::NONE:
403 output_struct.transfer_roller_voltage = 0.0;
404 break;
405 case TransferRollerStatus::TRANSFERING_IN:
406 output_struct.transfer_roller_voltage =
407 robot_constants_->common()->transfer_roller_voltages()->transfer_in();
408 break;
409 case TransferRollerStatus::TRANSFERING_OUT:
410 output_struct.transfer_roller_voltage = robot_constants_->common()
411 ->transfer_roller_voltages()
412 ->transfer_out();
413 break;
414 }
415
416 // Update Extend Roller voltage based on status from state machine.
417 const ExtendRollerVoltages *extend_roller_voltages =
418 robot_constants_->common()->extend_roller_voltages();
419 switch (extend_roller_status) {
420 case ExtendRollerStatus::IDLE:
421 // No voltage applied when idle
422 output_struct.extend_roller_voltage = 0.0;
423 break;
424 case ExtendRollerStatus::TRANSFERING_TO_EXTEND:
425 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
426 break;
427 case ExtendRollerStatus::SCORING_IN_AMP:
428 [[fallthrough]];
429 case ExtendRollerStatus::SCORING_IN_TRAP:
430 // Apply scoring voltage during scoring in amp or trap
431 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
432 break;
433 case ExtendRollerStatus::TRANSFERING_TO_CATAPULT:
434 // Apply scoring voltage during transferring to catapult
435 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
436 break;
437 }
438
439 double extend_position = 0.0;
440
441 // Set the extend position based on the state machine output
442 switch (extend_goal) {
443 case ExtendStatus::RETRACTED:
444 extend_position = extend_set_points->retracted();
445 break;
446 case ExtendStatus::AMP:
447 extend_position = extend_set_points->amp();
448 break;
449 case ExtendStatus::TRAP:
450 extend_position = extend_set_points->trap();
451 break;
452 case ExtendStatus::CATAPULT:
453 extend_position = extend_set_points->catapult();
454 break;
455 case ExtendStatus::MOVING:
456 // Should never happen
457 break;
458 }
459
460 // Set the extend status based on the state machine output
461 // If the extend is moving, the status is MOVING, otherwise it is the same
462 // as extend_status
463 ExtendStatus extend_status =
464 (extend_moving ? ExtendStatus::MOVING : extend_goal);
465
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800466 if (joystick_state_fetcher_.Fetch() &&
467 joystick_state_fetcher_->has_alliance()) {
468 alliance_ = joystick_state_fetcher_->alliance();
469 }
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800470
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800471 drivetrain_status_fetcher_.Fetch();
472
Niko Sohmersac4d8872024-02-23 13:55:47 -0800473 const bool collided = collision_avoidance_.IsCollided(
474 {.intake_pivot_position = intake_pivot_.estimated_position(),
475 .turret_position = shooter_.turret().estimated_position()});
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800476
Filip Kujawa6d717632024-02-01 11:40:55 -0800477 aos::FlatbufferFixedAllocatorArray<
478 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
479 climber_goal_buffer;
480
481 climber_goal_buffer.Finish(
482 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
483 *climber_goal_buffer.fbb(), climber_position));
484
485 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
486 *climber_goal = &climber_goal_buffer.message();
487
488 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
489 climber_status_offset = climber_.Iterate(
490 climber_goal, position->climber(),
491 output != nullptr ? &output_struct.climber_voltage : nullptr,
492 status->fbb());
493
Niko Sohmersac4d8872024-02-23 13:55:47 -0800494 double max_intake_pivot_position = 0;
495 double min_intake_pivot_position = 0;
496
497 aos::FlatbufferFixedAllocatorArray<
498 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
499 intake_pivot_goal_buffer;
500
501 intake_pivot_goal_buffer.Finish(
502 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
503 *intake_pivot_goal_buffer.fbb(), intake_pivot_position));
504
505 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
506 *intake_pivot_goal = &intake_pivot_goal_buffer.message();
507
508 double *intake_output =
509 (output != nullptr ? &output_struct.intake_pivot_voltage : nullptr);
510
511 const bool disabled = intake_pivot_.Correct(
512 intake_pivot_goal, position->intake_pivot(), intake_output == nullptr);
513
Filip Kujawa7a799602024-02-23 12:27:47 -0800514 // TODO(max): Change how we handle the collision with the turret and
515 // intake to be clearer
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800516 const flatbuffers::Offset<ShooterStatus> shooter_status_offset =
517 shooter_.Iterate(
518 position,
519 unsafe_goal != nullptr ? unsafe_goal->shooter_goal() : nullptr,
Filip Kujawa7a799602024-02-23 12:27:47 -0800520 unsafe_goal != nullptr ? unsafe_goal->fire() : false,
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800521 output != nullptr ? &output_struct.catapult_voltage : nullptr,
522 output != nullptr ? &output_struct.altitude_voltage : nullptr,
523 output != nullptr ? &output_struct.turret_voltage : nullptr,
524 output != nullptr ? &output_struct.retention_roller_voltage : nullptr,
Maxwell Hendersond5bf47a2024-02-23 17:16:48 -0800525 output != nullptr
526 ? &output_struct.retention_roller_stator_current_limit
527 : nullptr,
Maxwell Henderson461a8a42024-02-23 17:07:39 -0800528 robot_state().voltage_battery(), &collision_avoidance_,
Niko Sohmersac4d8872024-02-23 13:55:47 -0800529 intake_pivot_.estimated_position(), &max_intake_pivot_position,
Filip Kujawa7a799602024-02-23 12:27:47 -0800530 &min_intake_pivot_position, move_turret_to_standby, status->fbb());
Niko Sohmersac4d8872024-02-23 13:55:47 -0800531
532 intake_pivot_.set_min_position(min_intake_pivot_position);
533 intake_pivot_.set_max_position(max_intake_pivot_position);
534
535 // Calculate the loops for a cycle.
536 const double voltage = intake_pivot_.UpdateController(disabled);
537
538 intake_pivot_.UpdateObserver(voltage);
539
540 // Write out all the voltages.
541 if (intake_output) {
542 *intake_output = voltage;
543 }
544
545 const flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus>
546 intake_pivot_status_offset = intake_pivot_.MakeStatus(status->fbb());
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800547
Filip Kujawa7a799602024-02-23 12:27:47 -0800548 aos::FlatbufferFixedAllocatorArray<
549 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
550 note_goal_buffer;
551
552 note_goal_buffer.Finish(
553 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
554 *note_goal_buffer.fbb(), extend_position));
555
556 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
557 *note_goal = &note_goal_buffer.message();
558
559 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
560 extend_status_offset = extend_.Iterate(
561 note_goal, position->extend(),
562 output != nullptr ? &output_struct.extend_voltage : nullptr,
563 status->fbb());
564
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800565 if (output) {
566 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
567 }
568
569 Status::Builder status_builder = status->MakeBuilder<Status>();
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800570
Filip Kujawa7a799602024-02-23 12:27:47 -0800571 const bool zeroed = intake_pivot_.zeroed() && climber_.zeroed() &&
572 shooter_.zeroed() && extend_.zeroed();
573 const bool estopped = intake_pivot_.estopped() || climber_.estopped() ||
574 shooter_.estopped() || extend_.estopped();
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800575
576 status_builder.add_zeroed(zeroed);
577 status_builder.add_estopped(estopped);
Filip Kujawa102a9b22024-02-18 09:40:23 -0800578 status_builder.add_intake_roller(intake_roller_state);
579 status_builder.add_intake_pivot(intake_pivot_status_offset);
Filip Kujawa7a799602024-02-23 12:27:47 -0800580 status_builder.add_transfer_roller(transfer_roller_status);
Filip Kujawa102a9b22024-02-18 09:40:23 -0800581 status_builder.add_climber(climber_status_offset);
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800582 status_builder.add_shooter(shooter_status_offset);
Niko Sohmersac4d8872024-02-23 13:55:47 -0800583 status_builder.add_collided(collided);
Filip Kujawa7a799602024-02-23 12:27:47 -0800584 status_builder.add_extend_roller(extend_roller_status);
585 status_builder.add_extend_status(extend_status);
586 status_builder.add_extend(extend_status_offset);
587 status_builder.add_state(state_);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800588
589 (void)status->Send(status_builder.Finish());
590}
591
592double Superstructure::robot_velocity() const {
593 return (drivetrain_status_fetcher_.get() != nullptr
594 ? drivetrain_status_fetcher_->robot_speed()
595 : 0.0);
596}
597
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800598} // namespace y2024::control_loops::superstructure