blob: 8f42cb7f26ff2535415011847723a574dbedd016 [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
James Kuszmaul0281e152024-02-26 22:26:16 -0800171 std::optional<bool> turret_ready_for_extend_move;
Filip Kujawa7a799602024-02-23 12:27:47 -0800172 switch (state_) {
173 case SuperstructureState::IDLE:
174 if (unsafe_goal != nullptr &&
175 unsafe_goal->intake_goal() == IntakeGoal::INTAKE &&
Niko Sohmersdd971dc2024-02-25 11:40:47 -0800176 extend_at_retracted) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800177 state_ = SuperstructureState::INTAKING;
178 }
179 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:
201 if (catapult_requested_ == true) {
202 state_ = SuperstructureState::MOVING;
203 break;
204 }
205
206 if (unsafe_goal != nullptr &&
207 unsafe_goal->note_goal() != NoteGoal::NONE) {
208 // If the goal is AMP or TRAP, check if the turret is in a position to
209 // avoid collision when the extend moves.
210 if (unsafe_goal->note_goal() == NoteGoal::AMP ||
211 unsafe_goal->note_goal() == NoteGoal::TRAP) {
James Kuszmaul0281e152024-02-26 22:26:16 -0800212 turret_ready_for_extend_move =
Filip Kujawa7a799602024-02-23 12:27:47 -0800213 PositionNear(shooter_.turret().estimated_position(),
214 robot_constants_->common()
215 ->turret_avoid_extend_collision_position(),
216 kTurretLoadingThreshold);
James Kuszmaul0281e152024-02-26 22:26:16 -0800217 transfer_roller_status = TransferRollerStatus::TRANSFERING_IN;
Filip Kujawa7a799602024-02-23 12:27:47 -0800218
James Kuszmaul0281e152024-02-26 22:26:16 -0800219 if (turret_ready_for_extend_move.value()) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800220 state_ = SuperstructureState::MOVING;
221 } else {
222 move_turret_to_standby = true;
223 }
224 } else if (unsafe_goal->note_goal() == NoteGoal::CATAPULT) {
225 // If catapult is requested, switch to MOVING state
226 state_ = SuperstructureState::MOVING;
227 }
228 }
229 extend_goal = ExtendStatus::RETRACTED;
230 if (!catapult_requested_ && unsafe_goal != nullptr &&
231 unsafe_goal->note_goal() == NoteGoal::CATAPULT) {
232 catapult_requested_ = true;
233 }
234 break;
235 case SuperstructureState::MOVING:
James Kuszmaul0281e152024-02-26 22:26:16 -0800236 transfer_roller_status = TransferRollerStatus::TRANSFERING_IN;
Filip Kujawa7a799602024-02-23 12:27:47 -0800237
238 if (catapult_requested_) {
239 extend_goal = ExtendStatus::CATAPULT;
240
241 // Check if the extend is at the position to load the catapult
242 bool extend_ready_for_catapult_transfer =
243 PositionNear(extend_.position(), extend_set_points->catapult(),
244 kExtendThreshold);
245
246 // Check if the turret is at the position to accept the note from extend
247 bool turret_ready_for_load =
248 PositionNear(shooter_.turret().estimated_position(),
249 robot_constants_->common()->turret_loading_position(),
250 kTurretLoadingThreshold);
251
252 // Check if the altitude is at the position to accept the note from
253 // extend
254 bool altitude_ready_for_load = PositionNear(
255 shooter_.altitude().estimated_position(),
256 robot_constants_->common()->altitude_loading_position(),
257 kAltitudeLoadingThreshold);
258
259 if (extend_ready_for_catapult_transfer && turret_ready_for_load &&
260 altitude_ready_for_load) {
261 state_ = SuperstructureState::LOADING_CATAPULT;
262 }
263 } else {
264 if (unsafe_goal != nullptr) {
265 switch (unsafe_goal->note_goal()) {
266 case NoteGoal::AMP:
267 extend_goal = ExtendStatus::AMP;
268 move_turret_to_standby = true;
269 // Check if the extend is at the AMP position and if it is
270 // switch to READY state
271 if (PositionNear(extend_.position(), extend_set_points->amp(),
272 kExtendThreshold)) {
273 state_ = SuperstructureState::READY;
274 }
275 break;
276 case NoteGoal::TRAP:
277 extend_goal = ExtendStatus::TRAP;
278 move_turret_to_standby = true;
279 // Check if the extend is at the TRAP position and if it is
280 // switch to READY state
281 if (PositionNear(extend_.position(), extend_set_points->trap(),
282 kExtendThreshold)) {
283 state_ = SuperstructureState::READY;
284 }
285 break;
286 case NoteGoal::NONE:
287 extend_goal = ExtendStatus::RETRACTED;
288 move_turret_to_standby = true;
Niko Sohmersdd971dc2024-02-25 11:40:47 -0800289 if (extend_at_retracted) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800290 state_ = SuperstructureState::LOADED;
291 }
292 break;
293 case NoteGoal::CATAPULT:
294 catapult_requested_ = true;
295 extend_goal = ExtendStatus::CATAPULT;
296 break;
297 }
298 }
299 }
300
301 extend_moving = true;
302 break;
303 case SuperstructureState::LOADING_CATAPULT:
304 extend_moving = false;
305 extend_goal = ExtendStatus::CATAPULT;
306 extend_roller_status = ExtendRollerStatus::TRANSFERING_TO_CATAPULT;
307
308 // Switch to READY state when the catapult beambreak is triggered
309 if (position->catapult_beambreak()) {
310 state_ = SuperstructureState::READY;
311 }
312 break;
313 case SuperstructureState::READY:
314 extend_moving = false;
315
316 // Switch to FIRING state when the fire button is pressed
317 if (unsafe_goal != nullptr && unsafe_goal->fire()) {
318 state_ = SuperstructureState::FIRING;
319 }
320
321 if (catapult_requested_) {
322 extend_goal = ExtendStatus::CATAPULT;
323 } else {
324 if (unsafe_goal != nullptr) {
325 if (unsafe_goal->note_goal() == NoteGoal::AMP) {
326 extend_goal = ExtendStatus::AMP;
327 move_turret_to_standby = true;
328 } else if (unsafe_goal->note_goal() == NoteGoal::TRAP) {
329 extend_goal = ExtendStatus::TRAP;
330 move_turret_to_standby = true;
331 } else {
332 extend_goal = ExtendStatus::RETRACTED;
333 extend_moving = true;
334 state_ = SuperstructureState::MOVING;
335 }
336 }
337 }
338
339 break;
340 case SuperstructureState::FIRING:
341 if (catapult_requested_) {
342 extend_goal = ExtendStatus::CATAPULT;
343
344 // Reset the state to IDLE when the game piece is fired from the
345 // catapult. We consider the game piece to be fired from the catapult
346 // when the catapultbeambreak is no longer triggered.
347 if (!position->catapult_beambreak()) {
348 state_ = SuperstructureState::IDLE;
349 }
350 } else {
James Kuszmaul0281e152024-02-26 22:26:16 -0800351 move_turret_to_standby = true;
Filip Kujawa7a799602024-02-23 12:27:47 -0800352 if (unsafe_goal != nullptr &&
353 unsafe_goal->note_goal() == NoteGoal::AMP) {
354 extend_roller_status = ExtendRollerStatus::SCORING_IN_AMP;
355 extend_goal = ExtendStatus::AMP;
356 } else if (unsafe_goal != nullptr &&
357 unsafe_goal->note_goal() == NoteGoal::TRAP) {
358 extend_roller_status = ExtendRollerStatus::SCORING_IN_TRAP;
359 extend_goal = ExtendStatus::TRAP;
360 }
361
362 // Reset the state to IDLE when the game piece is fired from the extend.
363 // We consider the game piece to be fired from the extend when
364 // the extend beambreak is no longer triggered and the fire button is
365 // released.
366 if (!position->extend_beambreak() && unsafe_goal != nullptr &&
367 !unsafe_goal->fire()) {
368 state_ = SuperstructureState::IDLE;
369 }
370 }
371 break;
372 }
373
374 if (unsafe_goal != nullptr &&
375 unsafe_goal->intake_goal() == IntakeGoal::SPIT) {
376 intake_roller_state = IntakeRollerStatus::SPITTING;
377 transfer_roller_status = TransferRollerStatus::TRANSFERING_OUT;
378 }
379
380 // Update Intake Roller voltage based on status from state machine.
381 switch (intake_roller_state) {
382 case IntakeRollerStatus::NONE:
383 output_struct.intake_roller_voltage = 0.0;
384 break;
385 case IntakeRollerStatus::SPITTING:
386 output_struct.intake_roller_voltage =
387 robot_constants_->common()->intake_roller_voltages()->spitting();
388 break;
389 case IntakeRollerStatus::INTAKING:
390 output_struct.intake_roller_voltage =
391 robot_constants_->common()->intake_roller_voltages()->intaking();
392 break;
393 }
394
395 // Update Transfer Roller voltage based on status from state machine.
396 switch (transfer_roller_status) {
397 case TransferRollerStatus::NONE:
398 output_struct.transfer_roller_voltage = 0.0;
399 break;
400 case TransferRollerStatus::TRANSFERING_IN:
401 output_struct.transfer_roller_voltage =
402 robot_constants_->common()->transfer_roller_voltages()->transfer_in();
403 break;
404 case TransferRollerStatus::TRANSFERING_OUT:
405 output_struct.transfer_roller_voltage = robot_constants_->common()
406 ->transfer_roller_voltages()
407 ->transfer_out();
408 break;
409 }
410
411 // Update Extend Roller voltage based on status from state machine.
412 const ExtendRollerVoltages *extend_roller_voltages =
413 robot_constants_->common()->extend_roller_voltages();
414 switch (extend_roller_status) {
415 case ExtendRollerStatus::IDLE:
416 // No voltage applied when idle
417 output_struct.extend_roller_voltage = 0.0;
418 break;
419 case ExtendRollerStatus::TRANSFERING_TO_EXTEND:
420 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
421 break;
422 case ExtendRollerStatus::SCORING_IN_AMP:
423 [[fallthrough]];
424 case ExtendRollerStatus::SCORING_IN_TRAP:
425 // Apply scoring voltage during scoring in amp or trap
426 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
427 break;
428 case ExtendRollerStatus::TRANSFERING_TO_CATAPULT:
429 // Apply scoring voltage during transferring to catapult
430 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
431 break;
432 }
433
434 double extend_position = 0.0;
435
James Kuszmaul0281e152024-02-26 22:26:16 -0800436 if (unsafe_goal != nullptr && unsafe_goal->note_goal() == NoteGoal::TRAP) {
437 extend_goal = ExtendStatus::TRAP;
438 move_turret_to_standby = true;
439 }
440
441 // In lieu of having full collision avoidance ready, move the turret out of
442 // the way whenever the extend is raised too much.
443 if (extend_.position() > 0.05) {
444 move_turret_to_standby = true;
445 }
446
Filip Kujawa7a799602024-02-23 12:27:47 -0800447 // Set the extend position based on the state machine output
448 switch (extend_goal) {
449 case ExtendStatus::RETRACTED:
450 extend_position = extend_set_points->retracted();
451 break;
452 case ExtendStatus::AMP:
453 extend_position = extend_set_points->amp();
454 break;
455 case ExtendStatus::TRAP:
456 extend_position = extend_set_points->trap();
457 break;
458 case ExtendStatus::CATAPULT:
459 extend_position = extend_set_points->catapult();
460 break;
461 case ExtendStatus::MOVING:
462 // Should never happen
463 break;
464 }
465
466 // Set the extend status based on the state machine output
467 // If the extend is moving, the status is MOVING, otherwise it is the same
468 // as extend_status
469 ExtendStatus extend_status =
470 (extend_moving ? ExtendStatus::MOVING : extend_goal);
471
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800472 if (joystick_state_fetcher_.Fetch() &&
473 joystick_state_fetcher_->has_alliance()) {
474 alliance_ = joystick_state_fetcher_->alliance();
475 }
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800476
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800477 drivetrain_status_fetcher_.Fetch();
478
Niko Sohmersac4d8872024-02-23 13:55:47 -0800479 const bool collided = collision_avoidance_.IsCollided(
480 {.intake_pivot_position = intake_pivot_.estimated_position(),
481 .turret_position = shooter_.turret().estimated_position()});
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800482
Filip Kujawa6d717632024-02-01 11:40:55 -0800483 aos::FlatbufferFixedAllocatorArray<
484 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
485 climber_goal_buffer;
486
487 climber_goal_buffer.Finish(
488 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
489 *climber_goal_buffer.fbb(), climber_position));
490
491 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
492 *climber_goal = &climber_goal_buffer.message();
493
494 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
495 climber_status_offset = climber_.Iterate(
496 climber_goal, position->climber(),
497 output != nullptr ? &output_struct.climber_voltage : nullptr,
498 status->fbb());
499
Niko Sohmersac4d8872024-02-23 13:55:47 -0800500 double max_intake_pivot_position = 0;
501 double min_intake_pivot_position = 0;
502
503 aos::FlatbufferFixedAllocatorArray<
504 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
505 intake_pivot_goal_buffer;
506
507 intake_pivot_goal_buffer.Finish(
508 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
509 *intake_pivot_goal_buffer.fbb(), intake_pivot_position));
510
511 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
512 *intake_pivot_goal = &intake_pivot_goal_buffer.message();
513
514 double *intake_output =
515 (output != nullptr ? &output_struct.intake_pivot_voltage : nullptr);
516
517 const bool disabled = intake_pivot_.Correct(
518 intake_pivot_goal, position->intake_pivot(), intake_output == nullptr);
519
Filip Kujawa7a799602024-02-23 12:27:47 -0800520 // TODO(max): Change how we handle the collision with the turret and
521 // intake to be clearer
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800522 const flatbuffers::Offset<ShooterStatus> shooter_status_offset =
523 shooter_.Iterate(
524 position,
525 unsafe_goal != nullptr ? unsafe_goal->shooter_goal() : nullptr,
Filip Kujawa7a799602024-02-23 12:27:47 -0800526 unsafe_goal != nullptr ? unsafe_goal->fire() : false,
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800527 output != nullptr ? &output_struct.catapult_voltage : nullptr,
528 output != nullptr ? &output_struct.altitude_voltage : nullptr,
529 output != nullptr ? &output_struct.turret_voltage : nullptr,
530 output != nullptr ? &output_struct.retention_roller_voltage : nullptr,
Maxwell Hendersond5bf47a2024-02-23 17:16:48 -0800531 output != nullptr
532 ? &output_struct.retention_roller_stator_current_limit
533 : nullptr,
Maxwell Henderson461a8a42024-02-23 17:07:39 -0800534 robot_state().voltage_battery(), &collision_avoidance_,
Niko Sohmersac4d8872024-02-23 13:55:47 -0800535 intake_pivot_.estimated_position(), &max_intake_pivot_position,
Maxwell Henderson6b1be312024-02-28 20:15:06 -0800536 &min_intake_pivot_position, move_turret_to_standby, status->fbb(),
537 timestamp);
Niko Sohmersac4d8872024-02-23 13:55:47 -0800538
539 intake_pivot_.set_min_position(min_intake_pivot_position);
540 intake_pivot_.set_max_position(max_intake_pivot_position);
541
542 // Calculate the loops for a cycle.
543 const double voltage = intake_pivot_.UpdateController(disabled);
544
545 intake_pivot_.UpdateObserver(voltage);
546
547 // Write out all the voltages.
548 if (intake_output) {
549 *intake_output = voltage;
550 }
551
552 const flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus>
553 intake_pivot_status_offset = intake_pivot_.MakeStatus(status->fbb());
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800554
Filip Kujawa7a799602024-02-23 12:27:47 -0800555 aos::FlatbufferFixedAllocatorArray<
556 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
557 note_goal_buffer;
558
559 note_goal_buffer.Finish(
560 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
561 *note_goal_buffer.fbb(), extend_position));
562
563 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
564 *note_goal = &note_goal_buffer.message();
565
566 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
567 extend_status_offset = extend_.Iterate(
568 note_goal, position->extend(),
569 output != nullptr ? &output_struct.extend_voltage : nullptr,
570 status->fbb());
571
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800572 if (output) {
573 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
574 }
575
576 Status::Builder status_builder = status->MakeBuilder<Status>();
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800577
Filip Kujawa7a799602024-02-23 12:27:47 -0800578 const bool zeroed = intake_pivot_.zeroed() && climber_.zeroed() &&
579 shooter_.zeroed() && extend_.zeroed();
580 const bool estopped = intake_pivot_.estopped() || climber_.estopped() ||
581 shooter_.estopped() || extend_.estopped();
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800582
583 status_builder.add_zeroed(zeroed);
584 status_builder.add_estopped(estopped);
Filip Kujawa102a9b22024-02-18 09:40:23 -0800585 status_builder.add_intake_roller(intake_roller_state);
586 status_builder.add_intake_pivot(intake_pivot_status_offset);
Filip Kujawa7a799602024-02-23 12:27:47 -0800587 status_builder.add_transfer_roller(transfer_roller_status);
Filip Kujawa102a9b22024-02-18 09:40:23 -0800588 status_builder.add_climber(climber_status_offset);
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800589 status_builder.add_shooter(shooter_status_offset);
Niko Sohmersac4d8872024-02-23 13:55:47 -0800590 status_builder.add_collided(collided);
Filip Kujawa7a799602024-02-23 12:27:47 -0800591 status_builder.add_extend_roller(extend_roller_status);
592 status_builder.add_extend_status(extend_status);
593 status_builder.add_extend(extend_status_offset);
594 status_builder.add_state(state_);
James Kuszmaul0281e152024-02-26 22:26:16 -0800595 status_builder.add_extend_ready_for_transfer(extend_at_retracted);
596 if (turret_ready_for_extend_move) {
597 status_builder.add_turret_ready_for_extend_move(
598 turret_ready_for_extend_move.value());
599 }
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800600
601 (void)status->Send(status_builder.Finish());
602}
603
604double Superstructure::robot_velocity() const {
605 return (drivetrain_status_fetcher_.get() != nullptr
606 ? drivetrain_status_fetcher_->robot_speed()
607 : 0.0);
608}
609
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800610} // namespace y2024::control_loops::superstructure