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