blob: fece6e337352d858f4dbf9109c5235fe09067db5 [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
Stephan Pleinesf63bde82024-01-13 15:59:33 -080022namespace y2024::control_loops::superstructure {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080023
24using ::aos::monotonic_clock;
25
26using frc971::control_loops::AbsoluteEncoderProfiledJointStatus;
27using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus;
28using frc971::control_loops::RelativeEncoderProfiledJointStatus;
29
30Superstructure::Superstructure(::aos::EventLoop *event_loop,
Niko Sohmers3860f8a2024-01-12 21:05:19 -080031 const ::std::string &name)
32 : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop,
33 name),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080034 constants_fetcher_(event_loop),
Niko Sohmersafc51fe2024-01-29 17:48:35 -080035 robot_constants_(CHECK_NOTNULL(&constants_fetcher_.constants())),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080036 drivetrain_status_fetcher_(
37 event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>(
38 "/drivetrain")),
39 joystick_state_fetcher_(
Niko Sohmersafc51fe2024-01-29 17:48:35 -080040 event_loop->MakeFetcher<aos::JoystickState>("/aos")),
Niko Sohmers74b0ad52024-02-03 18:00:31 -080041 intake_pivot_(robot_constants_->common()->intake_pivot(),
42 robot_constants_->robot()->intake_constants()),
Filip Kujawa6d717632024-02-01 11:40:55 -080043 climber_(
44 robot_constants_->common()->climber(),
Niko Sohmersc4d2c502024-02-19 19:35:35 -080045 robot_constants_->robot()->climber_constants()->zeroing_constants()),
Filip Kujawa7a799602024-02-23 12:27:47 -080046 shooter_(event_loop, robot_constants_),
47 extend_(
48 robot_constants_->common()->extend(),
49 robot_constants_->robot()->extend_constants()->zeroing_constants()) {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080050 event_loop->SetRuntimeRealtimePriority(30);
51}
52
Filip Kujawa7a799602024-02-23 12:27:47 -080053bool PositionNear(double position, double goal, double threshold) {
54 return std::abs(position - goal) < threshold;
55}
56
Niko Sohmers3860f8a2024-01-12 21:05:19 -080057void Superstructure::RunIteration(const Goal *unsafe_goal,
58 const Position *position,
59 aos::Sender<Output>::Builder *output,
60 aos::Sender<Status>::Builder *status) {
61 const monotonic_clock::time_point timestamp =
62 event_loop()->context().monotonic_event_time;
63
Maxwell Henderson461a8a42024-02-23 17:07:39 -080064 (void)timestamp;
65
Niko Sohmers3860f8a2024-01-12 21:05:19 -080066 if (WasReset()) {
67 AOS_LOG(ERROR, "WPILib reset, restarting\n");
Niko Sohmersafc51fe2024-01-29 17:48:35 -080068 intake_pivot_.Reset();
Filip Kujawa6d717632024-02-01 11:40:55 -080069 climber_.Reset();
Niko Sohmersc4d2c502024-02-19 19:35:35 -080070 shooter_.Reset();
Filip Kujawa7a799602024-02-23 12:27:47 -080071 extend_.Reset();
Niko Sohmers3860f8a2024-01-12 21:05:19 -080072 }
73
74 OutputT output_struct;
Niko Sohmersafc51fe2024-01-29 17:48:35 -080075
Filip Kujawa7a799602024-02-23 12:27:47 -080076 // Handle Climber Goal separately from main superstructure state machine
Filip Kujawa6d717632024-02-01 11:40:55 -080077 double climber_position =
78 robot_constants_->common()->climber_set_points()->retract();
79
80 if (unsafe_goal != nullptr) {
81 switch (unsafe_goal->climber_goal()) {
82 case ClimberGoal::FULL_EXTEND:
83 climber_position =
84 robot_constants_->common()->climber_set_points()->full_extend();
85 break;
Filip Kujawa6d717632024-02-01 11:40:55 -080086 case ClimberGoal::RETRACT:
87 climber_position =
88 robot_constants_->common()->climber_set_points()->retract();
89 break;
90 default:
91 break;
92 }
93 }
94
Filip Kujawa7a799602024-02-23 12:27:47 -080095 // If we started off preloaded, skip to the ready state.
96 if (unsafe_goal != nullptr && unsafe_goal->shooter_goal() &&
97 unsafe_goal->shooter_goal()->preloaded()) {
98 if (state_ != SuperstructureState::READY &&
99 state_ != SuperstructureState::FIRING) {
100 state_ = SuperstructureState::READY;
101 }
102 }
103
104 // Handle the intake pivot goal separately from the main superstructure state
105 IntakeRollerStatus intake_roller_state = IntakeRollerStatus::NONE;
106 double intake_pivot_position =
107 robot_constants_->common()->intake_pivot_set_points()->retracted();
108
109 if (unsafe_goal != nullptr) {
110 switch (unsafe_goal->intake_goal()) {
111 case IntakeGoal::INTAKE:
112 intake_pivot_position =
113 robot_constants_->common()->intake_pivot_set_points()->extended();
114 break;
115 case IntakeGoal::SPIT:
116 intake_pivot_position =
117 robot_constants_->common()->intake_pivot_set_points()->retracted();
118 break;
119 case IntakeGoal::NONE:
120 intake_pivot_position =
121 robot_constants_->common()->intake_pivot_set_points()->retracted();
122 break;
123 }
124 }
125
126 ExtendRollerStatus extend_roller_status = ExtendRollerStatus::IDLE;
127 ExtendStatus extend_goal = ExtendStatus::RETRACTED;
128
129 // True if the extend is moving towards a goal
130 bool extend_moving = false;
131
132 TransferRollerStatus transfer_roller_status = TransferRollerStatus::NONE;
133
134 const ExtendSetPoints *extend_set_points =
135 robot_constants_->common()->extend_set_points();
136
137 // Checks if the extend is close enough to the retracted position to be
138 // considered ready to accept note from the transfer rollers.
139 const bool extend_ready_for_transfer = PositionNear(
140 extend_.position(), extend_set_points->retracted(), kExtendThreshold);
141
142 // If true, the turret should be moved to the position to avoid collision with
143 // the extend.
144 bool move_turret_to_standby = false;
145
146 // Superstructure state machine:
147 // 1. IDLE. The intake is retracted and there is no note in the robot.
148 // Wait for a intake goal to switch state to INTAKING if the extend is ready
149 // 2. INTAKING. Intake the note and transfer it towards the extend.
150 // Give intake, transfer, and extend rollers positive voltage to intake and
151 // transfer. Switch to LOADED when the extend beambreak is triggered.
152 // 3. LOADED. The note is in the extend and the extend is retracted.
153 // Wait for a note goal to switch state to MOVING.
154 // For AMP/TRAP goals, check that the turret is in a position to avoid
155 // collision.
156 // 4. MOVING. The extend is moving towards a goal (AMP, TRAP, or CATAPULT).
157 // For CATAPULT goals, wait for the turret and altitude to be in a position to
158 // accept the note from the extend.
159 // Wait for the extend to reach the goal and switch state to READY if
160 // AMP or TRAP, or to LOADING_CATAPULT if CATAPULT.
161 // 5. LOADING_CATAPULT. The extend is at the position to load the catapult.
162 // Activate the extend roller to transfer the note to the catapult.
163 // Switch state to READY when the catapult beambreak is triggered.
164 // 6. READY. Ready for fire command. The note is either loaded in the catapult
165 // or in the extend and at the AMP or TRAP position. Wait for a fire command.
166 // 7. FIRING. The note is being fired, either from the extend or the catapult.
167 // Switch state back to IDLE when the note is fired.
168
169 switch (state_) {
170 case SuperstructureState::IDLE:
171 if (unsafe_goal != nullptr &&
172 unsafe_goal->intake_goal() == IntakeGoal::INTAKE &&
173 extend_ready_for_transfer) {
174 state_ = SuperstructureState::INTAKING;
175 }
176 extend_goal = ExtendStatus::RETRACTED;
177 catapult_requested_ = false;
178 break;
179 case SuperstructureState::INTAKING:
180
181 // Switch to LOADED state when the extend beambreak is triggered
182 // meaning the note is loaded in the extend
183 if (position->extend_beambreak()) {
184 state_ = SuperstructureState::LOADED;
185 }
186 intake_roller_state = IntakeRollerStatus::INTAKING;
187 transfer_roller_status = TransferRollerStatus::TRANSFERING_IN;
188 extend_roller_status = ExtendRollerStatus::TRANSFERING_TO_EXTEND;
189 extend_goal = ExtendStatus::RETRACTED;
190
191 if (!catapult_requested_ && unsafe_goal != nullptr &&
192 unsafe_goal->note_goal() == NoteGoal::CATAPULT) {
193 catapult_requested_ = true;
194 }
195
196 break;
197 case SuperstructureState::LOADED:
198 if (catapult_requested_ == true) {
199 state_ = SuperstructureState::MOVING;
200 break;
201 }
202
203 if (unsafe_goal != nullptr &&
204 unsafe_goal->note_goal() != NoteGoal::NONE) {
205 // If the goal is AMP or TRAP, check if the turret is in a position to
206 // avoid collision when the extend moves.
207 if (unsafe_goal->note_goal() == NoteGoal::AMP ||
208 unsafe_goal->note_goal() == NoteGoal::TRAP) {
209 bool turret_ready_for_extend_move =
210 PositionNear(shooter_.turret().estimated_position(),
211 robot_constants_->common()
212 ->turret_avoid_extend_collision_position(),
213 kTurretLoadingThreshold);
214
215 if (turret_ready_for_extend_move) {
216 state_ = SuperstructureState::MOVING;
217 } else {
218 move_turret_to_standby = true;
219 }
220 } else if (unsafe_goal->note_goal() == NoteGoal::CATAPULT) {
221 // If catapult is requested, switch to MOVING state
222 state_ = SuperstructureState::MOVING;
223 }
224 }
225 extend_goal = ExtendStatus::RETRACTED;
226 if (!catapult_requested_ && unsafe_goal != nullptr &&
227 unsafe_goal->note_goal() == NoteGoal::CATAPULT) {
228 catapult_requested_ = true;
229 }
230 break;
231 case SuperstructureState::MOVING:
232
233 if (catapult_requested_) {
234 extend_goal = ExtendStatus::CATAPULT;
235
236 // Check if the extend is at the position to load the catapult
237 bool extend_ready_for_catapult_transfer =
238 PositionNear(extend_.position(), extend_set_points->catapult(),
239 kExtendThreshold);
240
241 // Check if the turret is at the position to accept the note from extend
242 bool turret_ready_for_load =
243 PositionNear(shooter_.turret().estimated_position(),
244 robot_constants_->common()->turret_loading_position(),
245 kTurretLoadingThreshold);
246
247 // Check if the altitude is at the position to accept the note from
248 // extend
249 bool altitude_ready_for_load = PositionNear(
250 shooter_.altitude().estimated_position(),
251 robot_constants_->common()->altitude_loading_position(),
252 kAltitudeLoadingThreshold);
253
254 if (extend_ready_for_catapult_transfer && turret_ready_for_load &&
255 altitude_ready_for_load) {
256 state_ = SuperstructureState::LOADING_CATAPULT;
257 }
258 } else {
259 if (unsafe_goal != nullptr) {
260 switch (unsafe_goal->note_goal()) {
261 case NoteGoal::AMP:
262 extend_goal = ExtendStatus::AMP;
263 move_turret_to_standby = true;
264 // Check if the extend is at the AMP position and if it is
265 // switch to READY state
266 if (PositionNear(extend_.position(), extend_set_points->amp(),
267 kExtendThreshold)) {
268 state_ = SuperstructureState::READY;
269 }
270 break;
271 case NoteGoal::TRAP:
272 extend_goal = ExtendStatus::TRAP;
273 move_turret_to_standby = true;
274 // Check if the extend is at the TRAP position and if it is
275 // switch to READY state
276 if (PositionNear(extend_.position(), extend_set_points->trap(),
277 kExtendThreshold)) {
278 state_ = SuperstructureState::READY;
279 }
280 break;
281 case NoteGoal::NONE:
282 extend_goal = ExtendStatus::RETRACTED;
283 move_turret_to_standby = true;
284 if (extend_ready_for_transfer) {
285 state_ = SuperstructureState::LOADED;
286 }
287 break;
288 case NoteGoal::CATAPULT:
289 catapult_requested_ = true;
290 extend_goal = ExtendStatus::CATAPULT;
291 break;
292 }
293 }
294 }
295
296 extend_moving = true;
297 break;
298 case SuperstructureState::LOADING_CATAPULT:
299 extend_moving = false;
300 extend_goal = ExtendStatus::CATAPULT;
301 extend_roller_status = ExtendRollerStatus::TRANSFERING_TO_CATAPULT;
302
303 // Switch to READY state when the catapult beambreak is triggered
304 if (position->catapult_beambreak()) {
305 state_ = SuperstructureState::READY;
306 }
307 break;
308 case SuperstructureState::READY:
309 extend_moving = false;
310
311 // Switch to FIRING state when the fire button is pressed
312 if (unsafe_goal != nullptr && unsafe_goal->fire()) {
313 state_ = SuperstructureState::FIRING;
314 }
315
316 if (catapult_requested_) {
317 extend_goal = ExtendStatus::CATAPULT;
318 } else {
319 if (unsafe_goal != nullptr) {
320 if (unsafe_goal->note_goal() == NoteGoal::AMP) {
321 extend_goal = ExtendStatus::AMP;
322 move_turret_to_standby = true;
323 } else if (unsafe_goal->note_goal() == NoteGoal::TRAP) {
324 extend_goal = ExtendStatus::TRAP;
325 move_turret_to_standby = true;
326 } else {
327 extend_goal = ExtendStatus::RETRACTED;
328 extend_moving = true;
329 state_ = SuperstructureState::MOVING;
330 }
331 }
332 }
333
334 break;
335 case SuperstructureState::FIRING:
336 if (catapult_requested_) {
337 extend_goal = ExtendStatus::CATAPULT;
338
339 // Reset the state to IDLE when the game piece is fired from the
340 // catapult. We consider the game piece to be fired from the catapult
341 // when the catapultbeambreak is no longer triggered.
342 if (!position->catapult_beambreak()) {
343 state_ = SuperstructureState::IDLE;
344 }
345 } else {
346 if (unsafe_goal != nullptr &&
347 unsafe_goal->note_goal() == NoteGoal::AMP) {
348 extend_roller_status = ExtendRollerStatus::SCORING_IN_AMP;
349 extend_goal = ExtendStatus::AMP;
350 } else if (unsafe_goal != nullptr &&
351 unsafe_goal->note_goal() == NoteGoal::TRAP) {
352 extend_roller_status = ExtendRollerStatus::SCORING_IN_TRAP;
353 extend_goal = ExtendStatus::TRAP;
354 }
355
356 // Reset the state to IDLE when the game piece is fired from the extend.
357 // We consider the game piece to be fired from the extend when
358 // the extend beambreak is no longer triggered and the fire button is
359 // released.
360 if (!position->extend_beambreak() && unsafe_goal != nullptr &&
361 !unsafe_goal->fire()) {
362 state_ = SuperstructureState::IDLE;
363 }
364 }
365 break;
366 }
367
368 if (unsafe_goal != nullptr &&
369 unsafe_goal->intake_goal() == IntakeGoal::SPIT) {
370 intake_roller_state = IntakeRollerStatus::SPITTING;
371 transfer_roller_status = TransferRollerStatus::TRANSFERING_OUT;
372 }
373
374 // Update Intake Roller voltage based on status from state machine.
375 switch (intake_roller_state) {
376 case IntakeRollerStatus::NONE:
377 output_struct.intake_roller_voltage = 0.0;
378 break;
379 case IntakeRollerStatus::SPITTING:
380 output_struct.intake_roller_voltage =
381 robot_constants_->common()->intake_roller_voltages()->spitting();
382 break;
383 case IntakeRollerStatus::INTAKING:
384 output_struct.intake_roller_voltage =
385 robot_constants_->common()->intake_roller_voltages()->intaking();
386 break;
387 }
388
389 // Update Transfer Roller voltage based on status from state machine.
390 switch (transfer_roller_status) {
391 case TransferRollerStatus::NONE:
392 output_struct.transfer_roller_voltage = 0.0;
393 break;
394 case TransferRollerStatus::TRANSFERING_IN:
395 output_struct.transfer_roller_voltage =
396 robot_constants_->common()->transfer_roller_voltages()->transfer_in();
397 break;
398 case TransferRollerStatus::TRANSFERING_OUT:
399 output_struct.transfer_roller_voltage = robot_constants_->common()
400 ->transfer_roller_voltages()
401 ->transfer_out();
402 break;
403 }
404
405 // Update Extend Roller voltage based on status from state machine.
406 const ExtendRollerVoltages *extend_roller_voltages =
407 robot_constants_->common()->extend_roller_voltages();
408 switch (extend_roller_status) {
409 case ExtendRollerStatus::IDLE:
410 // No voltage applied when idle
411 output_struct.extend_roller_voltage = 0.0;
412 break;
413 case ExtendRollerStatus::TRANSFERING_TO_EXTEND:
414 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
415 break;
416 case ExtendRollerStatus::SCORING_IN_AMP:
417 [[fallthrough]];
418 case ExtendRollerStatus::SCORING_IN_TRAP:
419 // Apply scoring voltage during scoring in amp or trap
420 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
421 break;
422 case ExtendRollerStatus::TRANSFERING_TO_CATAPULT:
423 // Apply scoring voltage during transferring to catapult
424 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
425 break;
426 }
427
428 double extend_position = 0.0;
429
430 // Set the extend position based on the state machine output
431 switch (extend_goal) {
432 case ExtendStatus::RETRACTED:
433 extend_position = extend_set_points->retracted();
434 break;
435 case ExtendStatus::AMP:
436 extend_position = extend_set_points->amp();
437 break;
438 case ExtendStatus::TRAP:
439 extend_position = extend_set_points->trap();
440 break;
441 case ExtendStatus::CATAPULT:
442 extend_position = extend_set_points->catapult();
443 break;
444 case ExtendStatus::MOVING:
445 // Should never happen
446 break;
447 }
448
449 // Set the extend status based on the state machine output
450 // If the extend is moving, the status is MOVING, otherwise it is the same
451 // as extend_status
452 ExtendStatus extend_status =
453 (extend_moving ? ExtendStatus::MOVING : extend_goal);
454
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800455 if (joystick_state_fetcher_.Fetch() &&
456 joystick_state_fetcher_->has_alliance()) {
457 alliance_ = joystick_state_fetcher_->alliance();
458 }
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800459
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800460 drivetrain_status_fetcher_.Fetch();
461
Niko Sohmersac4d8872024-02-23 13:55:47 -0800462 const bool collided = collision_avoidance_.IsCollided(
463 {.intake_pivot_position = intake_pivot_.estimated_position(),
464 .turret_position = shooter_.turret().estimated_position()});
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800465
Filip Kujawa6d717632024-02-01 11:40:55 -0800466 aos::FlatbufferFixedAllocatorArray<
467 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
468 climber_goal_buffer;
469
470 climber_goal_buffer.Finish(
471 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
472 *climber_goal_buffer.fbb(), climber_position));
473
474 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
475 *climber_goal = &climber_goal_buffer.message();
476
477 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
478 climber_status_offset = climber_.Iterate(
479 climber_goal, position->climber(),
480 output != nullptr ? &output_struct.climber_voltage : nullptr,
481 status->fbb());
482
Niko Sohmersac4d8872024-02-23 13:55:47 -0800483 double max_intake_pivot_position = 0;
484 double min_intake_pivot_position = 0;
485
486 aos::FlatbufferFixedAllocatorArray<
487 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
488 intake_pivot_goal_buffer;
489
490 intake_pivot_goal_buffer.Finish(
491 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
492 *intake_pivot_goal_buffer.fbb(), intake_pivot_position));
493
494 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
495 *intake_pivot_goal = &intake_pivot_goal_buffer.message();
496
497 double *intake_output =
498 (output != nullptr ? &output_struct.intake_pivot_voltage : nullptr);
499
500 const bool disabled = intake_pivot_.Correct(
501 intake_pivot_goal, position->intake_pivot(), intake_output == nullptr);
502
Filip Kujawa7a799602024-02-23 12:27:47 -0800503 // TODO(max): Change how we handle the collision with the turret and
504 // intake to be clearer
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800505 const flatbuffers::Offset<ShooterStatus> shooter_status_offset =
506 shooter_.Iterate(
507 position,
508 unsafe_goal != nullptr ? unsafe_goal->shooter_goal() : nullptr,
Filip Kujawa7a799602024-02-23 12:27:47 -0800509 unsafe_goal != nullptr ? unsafe_goal->fire() : false,
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800510 output != nullptr ? &output_struct.catapult_voltage : nullptr,
511 output != nullptr ? &output_struct.altitude_voltage : nullptr,
512 output != nullptr ? &output_struct.turret_voltage : nullptr,
513 output != nullptr ? &output_struct.retention_roller_voltage : nullptr,
Maxwell Hendersond5bf47a2024-02-23 17:16:48 -0800514 output != nullptr
515 ? &output_struct.retention_roller_stator_current_limit
516 : nullptr,
Maxwell Henderson461a8a42024-02-23 17:07:39 -0800517 robot_state().voltage_battery(), &collision_avoidance_,
Niko Sohmersac4d8872024-02-23 13:55:47 -0800518 intake_pivot_.estimated_position(), &max_intake_pivot_position,
Filip Kujawa7a799602024-02-23 12:27:47 -0800519 &min_intake_pivot_position, move_turret_to_standby, status->fbb());
Niko Sohmersac4d8872024-02-23 13:55:47 -0800520
521 intake_pivot_.set_min_position(min_intake_pivot_position);
522 intake_pivot_.set_max_position(max_intake_pivot_position);
523
524 // Calculate the loops for a cycle.
525 const double voltage = intake_pivot_.UpdateController(disabled);
526
527 intake_pivot_.UpdateObserver(voltage);
528
529 // Write out all the voltages.
530 if (intake_output) {
531 *intake_output = voltage;
532 }
533
534 const flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus>
535 intake_pivot_status_offset = intake_pivot_.MakeStatus(status->fbb());
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800536
Filip Kujawa7a799602024-02-23 12:27:47 -0800537 aos::FlatbufferFixedAllocatorArray<
538 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
539 note_goal_buffer;
540
541 note_goal_buffer.Finish(
542 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
543 *note_goal_buffer.fbb(), extend_position));
544
545 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
546 *note_goal = &note_goal_buffer.message();
547
548 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
549 extend_status_offset = extend_.Iterate(
550 note_goal, position->extend(),
551 output != nullptr ? &output_struct.extend_voltage : nullptr,
552 status->fbb());
553
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800554 if (output) {
555 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
556 }
557
558 Status::Builder status_builder = status->MakeBuilder<Status>();
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800559
Filip Kujawa7a799602024-02-23 12:27:47 -0800560 const bool zeroed = intake_pivot_.zeroed() && climber_.zeroed() &&
561 shooter_.zeroed() && extend_.zeroed();
562 const bool estopped = intake_pivot_.estopped() || climber_.estopped() ||
563 shooter_.estopped() || extend_.estopped();
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800564
565 status_builder.add_zeroed(zeroed);
566 status_builder.add_estopped(estopped);
Filip Kujawa102a9b22024-02-18 09:40:23 -0800567 status_builder.add_intake_roller(intake_roller_state);
568 status_builder.add_intake_pivot(intake_pivot_status_offset);
Filip Kujawa7a799602024-02-23 12:27:47 -0800569 status_builder.add_transfer_roller(transfer_roller_status);
Filip Kujawa102a9b22024-02-18 09:40:23 -0800570 status_builder.add_climber(climber_status_offset);
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800571 status_builder.add_shooter(shooter_status_offset);
Niko Sohmersac4d8872024-02-23 13:55:47 -0800572 status_builder.add_collided(collided);
Filip Kujawa7a799602024-02-23 12:27:47 -0800573 status_builder.add_extend_roller(extend_roller_status);
574 status_builder.add_extend_status(extend_status);
575 status_builder.add_extend(extend_status_offset);
576 status_builder.add_state(state_);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800577
578 (void)status->Send(status_builder.Finish());
579}
580
581double Superstructure::robot_velocity() const {
582 return (drivetrain_status_fetcher_.get() != nullptr
583 ? drivetrain_status_fetcher_->robot_speed()
584 : 0.0);
585}
586
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800587} // namespace y2024::control_loops::superstructure