blob: ff25bbe2df3e714aee4c204cc84285e3bee426d3 [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;
Maxwell Henderson7db29782024-02-24 20:10:26 -080090 case ClimberGoal::STOWED:
91 climber_position =
92 robot_constants_->common()->climber_set_points()->stowed();
Filip Kujawa6d717632024-02-01 11:40:55 -080093 }
94 }
95
Filip Kujawa7a799602024-02-23 12:27:47 -080096 // If we started off preloaded, skip to the ready state.
97 if (unsafe_goal != nullptr && unsafe_goal->shooter_goal() &&
98 unsafe_goal->shooter_goal()->preloaded()) {
99 if (state_ != SuperstructureState::READY &&
100 state_ != SuperstructureState::FIRING) {
101 state_ = SuperstructureState::READY;
Niko Sohmersdd971dc2024-02-25 11:40:47 -0800102 catapult_requested_ = true;
Filip Kujawa7a799602024-02-23 12:27:47 -0800103 }
104 }
105
106 // Handle the intake pivot goal separately from the main superstructure state
107 IntakeRollerStatus intake_roller_state = IntakeRollerStatus::NONE;
108 double intake_pivot_position =
109 robot_constants_->common()->intake_pivot_set_points()->retracted();
110
111 if (unsafe_goal != nullptr) {
112 switch (unsafe_goal->intake_goal()) {
113 case IntakeGoal::INTAKE:
114 intake_pivot_position =
115 robot_constants_->common()->intake_pivot_set_points()->extended();
116 break;
117 case IntakeGoal::SPIT:
118 intake_pivot_position =
119 robot_constants_->common()->intake_pivot_set_points()->retracted();
120 break;
121 case IntakeGoal::NONE:
122 intake_pivot_position =
123 robot_constants_->common()->intake_pivot_set_points()->retracted();
124 break;
125 }
126 }
127
128 ExtendRollerStatus extend_roller_status = ExtendRollerStatus::IDLE;
129 ExtendStatus extend_goal = ExtendStatus::RETRACTED;
130
131 // True if the extend is moving towards a goal
132 bool extend_moving = false;
133
134 TransferRollerStatus transfer_roller_status = TransferRollerStatus::NONE;
135
136 const ExtendSetPoints *extend_set_points =
137 robot_constants_->common()->extend_set_points();
138
139 // Checks if the extend is close enough to the retracted position to be
140 // considered ready to accept note from the transfer rollers.
Niko Sohmersdd971dc2024-02-25 11:40:47 -0800141 const bool extend_at_retracted = PositionNear(
Filip Kujawa7a799602024-02-23 12:27:47 -0800142 extend_.position(), extend_set_points->retracted(), kExtendThreshold);
143
144 // If true, the turret should be moved to the position to avoid collision with
145 // the extend.
146 bool move_turret_to_standby = false;
147
148 // Superstructure state machine:
149 // 1. IDLE. The intake is retracted and there is no note in the robot.
150 // Wait for a intake goal to switch state to INTAKING if the extend is ready
151 // 2. INTAKING. Intake the note and transfer it towards the extend.
152 // Give intake, transfer, and extend rollers positive voltage to intake and
153 // transfer. Switch to LOADED when the extend beambreak is triggered.
154 // 3. LOADED. The note is in the extend and the extend is retracted.
155 // Wait for a note goal to switch state to MOVING.
156 // For AMP/TRAP goals, check that the turret is in a position to avoid
157 // collision.
158 // 4. MOVING. The extend is moving towards a goal (AMP, TRAP, or CATAPULT).
159 // For CATAPULT goals, wait for the turret and altitude to be in a position to
160 // accept the note from the extend.
161 // Wait for the extend to reach the goal and switch state to READY if
162 // AMP or TRAP, or to LOADING_CATAPULT if CATAPULT.
163 // 5. LOADING_CATAPULT. The extend is at the position to load the catapult.
164 // Activate the extend roller to transfer the note to the catapult.
165 // Switch state to READY when the catapult beambreak is triggered.
166 // 6. READY. Ready for fire command. The note is either loaded in the catapult
167 // or in the extend and at the AMP or TRAP position. Wait for a fire command.
168 // 7. FIRING. The note is being fired, either from the extend or the catapult.
169 // Switch state back to IDLE when the note is fired.
170
171 switch (state_) {
172 case SuperstructureState::IDLE:
173 if (unsafe_goal != nullptr &&
174 unsafe_goal->intake_goal() == IntakeGoal::INTAKE &&
Niko Sohmersdd971dc2024-02-25 11:40:47 -0800175 extend_at_retracted) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800176 state_ = SuperstructureState::INTAKING;
177 }
Maxwell Henderson4567e282024-02-25 15:38:15 -0800178
Filip Kujawa7a799602024-02-23 12:27:47 -0800179 extend_goal = ExtendStatus::RETRACTED;
180 catapult_requested_ = false;
181 break;
182 case SuperstructureState::INTAKING:
183
184 // 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
199 break;
200 case SuperstructureState::LOADED:
Maxwell Henderson4567e282024-02-25 15:38:15 -0800201 if (!position->extend_beambreak() && !position->catapult_beambreak()) {
202 state_ = SuperstructureState::IDLE;
203 }
204
Filip Kujawa7a799602024-02-23 12:27:47 -0800205 if (catapult_requested_ == true) {
206 state_ = SuperstructureState::MOVING;
207 break;
208 }
209
210 if (unsafe_goal != nullptr &&
211 unsafe_goal->note_goal() != NoteGoal::NONE) {
212 // If the goal is AMP or TRAP, check if the turret is in a position to
213 // avoid collision when the extend moves.
214 if (unsafe_goal->note_goal() == NoteGoal::AMP ||
215 unsafe_goal->note_goal() == NoteGoal::TRAP) {
216 bool turret_ready_for_extend_move =
217 PositionNear(shooter_.turret().estimated_position(),
218 robot_constants_->common()
219 ->turret_avoid_extend_collision_position(),
220 kTurretLoadingThreshold);
221
222 if (turret_ready_for_extend_move) {
223 state_ = SuperstructureState::MOVING;
224 } else {
225 move_turret_to_standby = true;
226 }
227 } else if (unsafe_goal->note_goal() == NoteGoal::CATAPULT) {
228 // If catapult is requested, switch to MOVING state
229 state_ = SuperstructureState::MOVING;
230 }
231 }
232 extend_goal = ExtendStatus::RETRACTED;
233 if (!catapult_requested_ && unsafe_goal != nullptr &&
234 unsafe_goal->note_goal() == NoteGoal::CATAPULT) {
235 catapult_requested_ = true;
236 }
237 break;
238 case SuperstructureState::MOVING:
239
240 if (catapult_requested_) {
241 extend_goal = ExtendStatus::CATAPULT;
242
243 // Check if the extend is at the position to load the catapult
244 bool extend_ready_for_catapult_transfer =
245 PositionNear(extend_.position(), extend_set_points->catapult(),
246 kExtendThreshold);
247
248 // Check if the turret is at the position to accept the note from extend
249 bool turret_ready_for_load =
250 PositionNear(shooter_.turret().estimated_position(),
251 robot_constants_->common()->turret_loading_position(),
252 kTurretLoadingThreshold);
253
254 // Check if the altitude is at the position to accept the note from
255 // extend
256 bool altitude_ready_for_load = PositionNear(
257 shooter_.altitude().estimated_position(),
258 robot_constants_->common()->altitude_loading_position(),
259 kAltitudeLoadingThreshold);
260
261 if (extend_ready_for_catapult_transfer && turret_ready_for_load &&
262 altitude_ready_for_load) {
263 state_ = SuperstructureState::LOADING_CATAPULT;
264 }
265 } else {
266 if (unsafe_goal != nullptr) {
267 switch (unsafe_goal->note_goal()) {
268 case NoteGoal::AMP:
269 extend_goal = ExtendStatus::AMP;
270 move_turret_to_standby = true;
271 // Check if the extend is at the AMP position and if it is
272 // switch to READY state
273 if (PositionNear(extend_.position(), extend_set_points->amp(),
274 kExtendThreshold)) {
275 state_ = SuperstructureState::READY;
276 }
277 break;
278 case NoteGoal::TRAP:
279 extend_goal = ExtendStatus::TRAP;
280 move_turret_to_standby = true;
281 // Check if the extend is at the TRAP position and if it is
282 // switch to READY state
283 if (PositionNear(extend_.position(), extend_set_points->trap(),
284 kExtendThreshold)) {
285 state_ = SuperstructureState::READY;
286 }
287 break;
288 case NoteGoal::NONE:
289 extend_goal = ExtendStatus::RETRACTED;
290 move_turret_to_standby = true;
Niko Sohmersdd971dc2024-02-25 11:40:47 -0800291 if (extend_at_retracted) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800292 state_ = SuperstructureState::LOADED;
293 }
294 break;
295 case NoteGoal::CATAPULT:
296 catapult_requested_ = true;
297 extend_goal = ExtendStatus::CATAPULT;
298 break;
299 }
300 }
301 }
302
303 extend_moving = true;
304 break;
305 case SuperstructureState::LOADING_CATAPULT:
306 extend_moving = false;
307 extend_goal = ExtendStatus::CATAPULT;
308 extend_roller_status = ExtendRollerStatus::TRANSFERING_TO_CATAPULT;
309
310 // Switch to READY state when the catapult beambreak is triggered
311 if (position->catapult_beambreak()) {
312 state_ = SuperstructureState::READY;
313 }
314 break;
315 case SuperstructureState::READY:
316 extend_moving = false;
317
318 // Switch to FIRING state when the fire button is pressed
319 if (unsafe_goal != nullptr && unsafe_goal->fire()) {
320 state_ = SuperstructureState::FIRING;
321 }
322
323 if (catapult_requested_) {
324 extend_goal = ExtendStatus::CATAPULT;
325 } else {
326 if (unsafe_goal != nullptr) {
327 if (unsafe_goal->note_goal() == NoteGoal::AMP) {
328 extend_goal = ExtendStatus::AMP;
329 move_turret_to_standby = true;
330 } else if (unsafe_goal->note_goal() == NoteGoal::TRAP) {
331 extend_goal = ExtendStatus::TRAP;
332 move_turret_to_standby = true;
333 } else {
334 extend_goal = ExtendStatus::RETRACTED;
335 extend_moving = true;
336 state_ = SuperstructureState::MOVING;
337 }
338 }
339 }
340
341 break;
342 case SuperstructureState::FIRING:
343 if (catapult_requested_) {
344 extend_goal = ExtendStatus::CATAPULT;
345
346 // Reset the state to IDLE when the game piece is fired from the
347 // catapult. We consider the game piece to be fired from the catapult
348 // when the catapultbeambreak is no longer triggered.
349 if (!position->catapult_beambreak()) {
350 state_ = SuperstructureState::IDLE;
351 }
352 } else {
353 if (unsafe_goal != nullptr &&
354 unsafe_goal->note_goal() == NoteGoal::AMP) {
355 extend_roller_status = ExtendRollerStatus::SCORING_IN_AMP;
356 extend_goal = ExtendStatus::AMP;
357 } else if (unsafe_goal != nullptr &&
358 unsafe_goal->note_goal() == NoteGoal::TRAP) {
359 extend_roller_status = ExtendRollerStatus::SCORING_IN_TRAP;
360 extend_goal = ExtendStatus::TRAP;
361 }
362
363 // Reset the state to IDLE when the game piece is fired from the extend.
364 // We consider the game piece to be fired from the extend when
365 // the extend beambreak is no longer triggered and the fire button is
366 // released.
367 if (!position->extend_beambreak() && unsafe_goal != nullptr &&
368 !unsafe_goal->fire()) {
369 state_ = SuperstructureState::IDLE;
370 }
371 }
372 break;
373 }
374
375 if (unsafe_goal != nullptr &&
376 unsafe_goal->intake_goal() == IntakeGoal::SPIT) {
377 intake_roller_state = IntakeRollerStatus::SPITTING;
378 transfer_roller_status = TransferRollerStatus::TRANSFERING_OUT;
379 }
380
381 // Update Intake Roller voltage based on status from state machine.
382 switch (intake_roller_state) {
383 case IntakeRollerStatus::NONE:
384 output_struct.intake_roller_voltage = 0.0;
385 break;
386 case IntakeRollerStatus::SPITTING:
387 output_struct.intake_roller_voltage =
388 robot_constants_->common()->intake_roller_voltages()->spitting();
389 break;
390 case IntakeRollerStatus::INTAKING:
391 output_struct.intake_roller_voltage =
392 robot_constants_->common()->intake_roller_voltages()->intaking();
393 break;
394 }
395
396 // Update Transfer Roller voltage based on status from state machine.
397 switch (transfer_roller_status) {
398 case TransferRollerStatus::NONE:
399 output_struct.transfer_roller_voltage = 0.0;
400 break;
401 case TransferRollerStatus::TRANSFERING_IN:
402 output_struct.transfer_roller_voltage =
403 robot_constants_->common()->transfer_roller_voltages()->transfer_in();
404 break;
405 case TransferRollerStatus::TRANSFERING_OUT:
406 output_struct.transfer_roller_voltage = robot_constants_->common()
407 ->transfer_roller_voltages()
408 ->transfer_out();
409 break;
410 }
411
412 // Update Extend Roller voltage based on status from state machine.
413 const ExtendRollerVoltages *extend_roller_voltages =
414 robot_constants_->common()->extend_roller_voltages();
415 switch (extend_roller_status) {
416 case ExtendRollerStatus::IDLE:
417 // No voltage applied when idle
418 output_struct.extend_roller_voltage = 0.0;
419 break;
420 case ExtendRollerStatus::TRANSFERING_TO_EXTEND:
421 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
422 break;
423 case ExtendRollerStatus::SCORING_IN_AMP:
424 [[fallthrough]];
425 case ExtendRollerStatus::SCORING_IN_TRAP:
426 // Apply scoring voltage during scoring in amp or trap
427 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
428 break;
429 case ExtendRollerStatus::TRANSFERING_TO_CATAPULT:
430 // Apply scoring voltage during transferring to catapult
431 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
432 break;
433 }
434
435 double extend_position = 0.0;
436
437 // Set the extend position based on the state machine output
438 switch (extend_goal) {
439 case ExtendStatus::RETRACTED:
440 extend_position = extend_set_points->retracted();
441 break;
442 case ExtendStatus::AMP:
443 extend_position = extend_set_points->amp();
444 break;
445 case ExtendStatus::TRAP:
446 extend_position = extend_set_points->trap();
447 break;
448 case ExtendStatus::CATAPULT:
449 extend_position = extend_set_points->catapult();
450 break;
451 case ExtendStatus::MOVING:
452 // Should never happen
453 break;
454 }
455
456 // Set the extend status based on the state machine output
457 // If the extend is moving, the status is MOVING, otherwise it is the same
458 // as extend_status
459 ExtendStatus extend_status =
460 (extend_moving ? ExtendStatus::MOVING : extend_goal);
461
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800462 if (joystick_state_fetcher_.Fetch() &&
463 joystick_state_fetcher_->has_alliance()) {
464 alliance_ = joystick_state_fetcher_->alliance();
465 }
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800466
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800467 drivetrain_status_fetcher_.Fetch();
468
Niko Sohmersac4d8872024-02-23 13:55:47 -0800469 const bool collided = collision_avoidance_.IsCollided(
470 {.intake_pivot_position = intake_pivot_.estimated_position(),
471 .turret_position = shooter_.turret().estimated_position()});
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800472
Filip Kujawa6d717632024-02-01 11:40:55 -0800473 aos::FlatbufferFixedAllocatorArray<
474 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
475 climber_goal_buffer;
476
477 climber_goal_buffer.Finish(
478 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
479 *climber_goal_buffer.fbb(), climber_position));
480
481 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
482 *climber_goal = &climber_goal_buffer.message();
483
484 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
485 climber_status_offset = climber_.Iterate(
486 climber_goal, position->climber(),
487 output != nullptr ? &output_struct.climber_voltage : nullptr,
488 status->fbb());
489
Niko Sohmersac4d8872024-02-23 13:55:47 -0800490 double max_intake_pivot_position = 0;
491 double min_intake_pivot_position = 0;
492
493 aos::FlatbufferFixedAllocatorArray<
494 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
495 intake_pivot_goal_buffer;
496
497 intake_pivot_goal_buffer.Finish(
498 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
499 *intake_pivot_goal_buffer.fbb(), intake_pivot_position));
500
501 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
502 *intake_pivot_goal = &intake_pivot_goal_buffer.message();
503
504 double *intake_output =
505 (output != nullptr ? &output_struct.intake_pivot_voltage : nullptr);
506
507 const bool disabled = intake_pivot_.Correct(
508 intake_pivot_goal, position->intake_pivot(), intake_output == nullptr);
509
Filip Kujawa7a799602024-02-23 12:27:47 -0800510 // TODO(max): Change how we handle the collision with the turret and
511 // intake to be clearer
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800512 const flatbuffers::Offset<ShooterStatus> shooter_status_offset =
513 shooter_.Iterate(
514 position,
515 unsafe_goal != nullptr ? unsafe_goal->shooter_goal() : nullptr,
Filip Kujawa7a799602024-02-23 12:27:47 -0800516 unsafe_goal != nullptr ? unsafe_goal->fire() : false,
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800517 output != nullptr ? &output_struct.catapult_voltage : nullptr,
518 output != nullptr ? &output_struct.altitude_voltage : nullptr,
519 output != nullptr ? &output_struct.turret_voltage : nullptr,
520 output != nullptr ? &output_struct.retention_roller_voltage : nullptr,
Maxwell Hendersond5bf47a2024-02-23 17:16:48 -0800521 output != nullptr
522 ? &output_struct.retention_roller_stator_current_limit
523 : nullptr,
Maxwell Henderson461a8a42024-02-23 17:07:39 -0800524 robot_state().voltage_battery(), &collision_avoidance_,
Niko Sohmersac4d8872024-02-23 13:55:47 -0800525 intake_pivot_.estimated_position(), &max_intake_pivot_position,
Filip Kujawa7a799602024-02-23 12:27:47 -0800526 &min_intake_pivot_position, move_turret_to_standby, status->fbb());
Niko Sohmersac4d8872024-02-23 13:55:47 -0800527
528 intake_pivot_.set_min_position(min_intake_pivot_position);
529 intake_pivot_.set_max_position(max_intake_pivot_position);
530
531 // Calculate the loops for a cycle.
532 const double voltage = intake_pivot_.UpdateController(disabled);
533
534 intake_pivot_.UpdateObserver(voltage);
535
536 // Write out all the voltages.
537 if (intake_output) {
538 *intake_output = voltage;
539 }
540
541 const flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus>
542 intake_pivot_status_offset = intake_pivot_.MakeStatus(status->fbb());
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800543
Filip Kujawa7a799602024-02-23 12:27:47 -0800544 aos::FlatbufferFixedAllocatorArray<
545 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
546 note_goal_buffer;
547
548 note_goal_buffer.Finish(
549 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
550 *note_goal_buffer.fbb(), extend_position));
551
552 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
553 *note_goal = &note_goal_buffer.message();
554
555 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
556 extend_status_offset = extend_.Iterate(
557 note_goal, position->extend(),
558 output != nullptr ? &output_struct.extend_voltage : nullptr,
559 status->fbb());
560
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800561 if (output) {
562 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
563 }
564
565 Status::Builder status_builder = status->MakeBuilder<Status>();
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800566
Filip Kujawa7a799602024-02-23 12:27:47 -0800567 const bool zeroed = intake_pivot_.zeroed() && climber_.zeroed() &&
568 shooter_.zeroed() && extend_.zeroed();
569 const bool estopped = intake_pivot_.estopped() || climber_.estopped() ||
570 shooter_.estopped() || extend_.estopped();
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800571
572 status_builder.add_zeroed(zeroed);
573 status_builder.add_estopped(estopped);
Filip Kujawa102a9b22024-02-18 09:40:23 -0800574 status_builder.add_intake_roller(intake_roller_state);
575 status_builder.add_intake_pivot(intake_pivot_status_offset);
Filip Kujawa7a799602024-02-23 12:27:47 -0800576 status_builder.add_transfer_roller(transfer_roller_status);
Filip Kujawa102a9b22024-02-18 09:40:23 -0800577 status_builder.add_climber(climber_status_offset);
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800578 status_builder.add_shooter(shooter_status_offset);
Niko Sohmersac4d8872024-02-23 13:55:47 -0800579 status_builder.add_collided(collided);
Filip Kujawa7a799602024-02-23 12:27:47 -0800580 status_builder.add_extend_roller(extend_roller_status);
581 status_builder.add_extend_status(extend_status);
582 status_builder.add_extend(extend_status_offset);
583 status_builder.add_state(state_);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800584
585 (void)status->Send(status_builder.Finish());
586}
587
588double Superstructure::robot_velocity() const {
589 return (drivetrain_status_fetcher_.get() != nullptr
590 ? drivetrain_status_fetcher_->robot_speed()
591 : 0.0);
592}
593
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800594} // namespace y2024::control_loops::superstructure