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