blob: e8d40c1e25b1c2dcba0420ef4c94aa056523a69d [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
Maxwell Hendersonf0d22622024-02-26 21:02:11 -080022constexpr std::chrono::milliseconds kExtraIntakingTime =
23 std::chrono::milliseconds(500);
24
Filip Kujawa77e3d8a2024-03-02 11:34:56 -080025// Exit catapult loading state after this much time if we never
26// trigger any beambreaks.
27constexpr std::chrono::milliseconds kMaxCatapultLoadingTime =
28 std::chrono::milliseconds(3000);
29
Stephan Pleinesf63bde82024-01-13 15:59:33 -080030namespace y2024::control_loops::superstructure {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080031
32using ::aos::monotonic_clock;
33
34using frc971::control_loops::AbsoluteEncoderProfiledJointStatus;
35using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus;
36using frc971::control_loops::RelativeEncoderProfiledJointStatus;
37
38Superstructure::Superstructure(::aos::EventLoop *event_loop,
Niko Sohmers3860f8a2024-01-12 21:05:19 -080039 const ::std::string &name)
40 : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop,
41 name),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080042 constants_fetcher_(event_loop),
Niko Sohmersafc51fe2024-01-29 17:48:35 -080043 robot_constants_(CHECK_NOTNULL(&constants_fetcher_.constants())),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080044 drivetrain_status_fetcher_(
45 event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>(
46 "/drivetrain")),
47 joystick_state_fetcher_(
Niko Sohmersafc51fe2024-01-29 17:48:35 -080048 event_loop->MakeFetcher<aos::JoystickState>("/aos")),
Niko Sohmers74b0ad52024-02-03 18:00:31 -080049 intake_pivot_(robot_constants_->common()->intake_pivot(),
50 robot_constants_->robot()->intake_constants()),
Filip Kujawa6d717632024-02-01 11:40:55 -080051 climber_(
52 robot_constants_->common()->climber(),
Niko Sohmersc4d2c502024-02-19 19:35:35 -080053 robot_constants_->robot()->climber_constants()->zeroing_constants()),
Filip Kujawa7a799602024-02-23 12:27:47 -080054 shooter_(event_loop, robot_constants_),
55 extend_(
56 robot_constants_->common()->extend(),
57 robot_constants_->robot()->extend_constants()->zeroing_constants()) {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080058 event_loop->SetRuntimeRealtimePriority(30);
59}
60
Filip Kujawa7a799602024-02-23 12:27:47 -080061bool PositionNear(double position, double goal, double threshold) {
62 return std::abs(position - goal) < threshold;
63}
64
Niko Sohmers3860f8a2024-01-12 21:05:19 -080065void Superstructure::RunIteration(const Goal *unsafe_goal,
66 const Position *position,
67 aos::Sender<Output>::Builder *output,
68 aos::Sender<Status>::Builder *status) {
69 const monotonic_clock::time_point timestamp =
70 event_loop()->context().monotonic_event_time;
71
Niko Sohmers3860f8a2024-01-12 21:05:19 -080072 if (WasReset()) {
73 AOS_LOG(ERROR, "WPILib reset, restarting\n");
Niko Sohmersafc51fe2024-01-29 17:48:35 -080074 intake_pivot_.Reset();
Filip Kujawa6d717632024-02-01 11:40:55 -080075 climber_.Reset();
Niko Sohmersc4d2c502024-02-19 19:35:35 -080076 shooter_.Reset();
Filip Kujawa7a799602024-02-23 12:27:47 -080077 extend_.Reset();
Niko Sohmers3860f8a2024-01-12 21:05:19 -080078 }
79
80 OutputT output_struct;
Niko Sohmersafc51fe2024-01-29 17:48:35 -080081
Filip Kujawa7a799602024-02-23 12:27:47 -080082 // Handle Climber Goal separately from main superstructure state machine
Filip Kujawa6d717632024-02-01 11:40:55 -080083 double climber_position =
84 robot_constants_->common()->climber_set_points()->retract();
85
86 if (unsafe_goal != nullptr) {
87 switch (unsafe_goal->climber_goal()) {
88 case ClimberGoal::FULL_EXTEND:
89 climber_position =
90 robot_constants_->common()->climber_set_points()->full_extend();
91 break;
Filip Kujawa6d717632024-02-01 11:40:55 -080092 case ClimberGoal::RETRACT:
93 climber_position =
94 robot_constants_->common()->climber_set_points()->retract();
95 break;
Maxwell Henderson7db29782024-02-24 20:10:26 -080096 case ClimberGoal::STOWED:
97 climber_position =
98 robot_constants_->common()->climber_set_points()->stowed();
Filip Kujawa6d717632024-02-01 11:40:55 -080099 }
100 }
101
Filip Kujawa7a799602024-02-23 12:27:47 -0800102 // If we started off preloaded, skip to the ready state.
103 if (unsafe_goal != nullptr && unsafe_goal->shooter_goal() &&
104 unsafe_goal->shooter_goal()->preloaded()) {
105 if (state_ != SuperstructureState::READY &&
106 state_ != SuperstructureState::FIRING) {
107 state_ = SuperstructureState::READY;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800108 requested_note_goal_ = NoteGoal::CATAPULT;
Filip Kujawa7a799602024-02-23 12:27:47 -0800109 }
110 }
111
112 // Handle the intake pivot goal separately from the main superstructure state
113 IntakeRollerStatus intake_roller_state = IntakeRollerStatus::NONE;
114 double intake_pivot_position =
115 robot_constants_->common()->intake_pivot_set_points()->retracted();
116
117 if (unsafe_goal != nullptr) {
118 switch (unsafe_goal->intake_goal()) {
119 case IntakeGoal::INTAKE:
120 intake_pivot_position =
121 robot_constants_->common()->intake_pivot_set_points()->extended();
Maxwell Hendersonf0d22622024-02-26 21:02:11 -0800122 intake_end_time_ = timestamp;
Filip Kujawa7a799602024-02-23 12:27:47 -0800123 break;
124 case IntakeGoal::SPIT:
125 intake_pivot_position =
126 robot_constants_->common()->intake_pivot_set_points()->retracted();
127 break;
128 case IntakeGoal::NONE:
129 intake_pivot_position =
130 robot_constants_->common()->intake_pivot_set_points()->retracted();
131 break;
132 }
133 }
134
135 ExtendRollerStatus extend_roller_status = ExtendRollerStatus::IDLE;
Austin Schuh027fd622024-03-01 21:26:07 -0800136 ExtendStatus extend_goal_location = ExtendStatus::RETRACTED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800137
138 // True if the extend is moving towards a goal
139 bool extend_moving = false;
140
141 TransferRollerStatus transfer_roller_status = TransferRollerStatus::NONE;
142
143 const ExtendSetPoints *extend_set_points =
144 robot_constants_->common()->extend_set_points();
145
146 // Checks if the extend is close enough to the retracted position to be
147 // considered ready to accept note from the transfer rollers.
Niko Sohmersdd971dc2024-02-25 11:40:47 -0800148 const bool extend_at_retracted = PositionNear(
Filip Kujawa7a799602024-02-23 12:27:47 -0800149 extend_.position(), extend_set_points->retracted(), kExtendThreshold);
150
Niko Sohmers5006fc42024-03-01 17:14:22 -0800151 // Check if the turret is at the position to accept the note from extend
152 const bool turret_ready_for_load =
153 PositionNear(shooter_.turret().estimated_position(),
154 robot_constants_->common()->turret_loading_position(),
155 kTurretLoadingThreshold);
156
Niko Sohmers5006fc42024-03-01 17:14:22 -0800157 // Check if the altitude is at the position to accept the note from
158 // extend
159 const bool altitude_ready_for_load =
160 PositionNear(shooter_.altitude().estimated_position(),
161 robot_constants_->common()->altitude_loading_position(),
162 kAltitudeLoadingThreshold);
163
164 // Check if the extend is at the position to load the catapult
165 const bool extend_ready_for_catapult_transfer = PositionNear(
166 extend_.position(), extend_set_points->catapult(), kExtendThreshold);
167
Niko Sohmers5006fc42024-03-01 17:14:22 -0800168 // Only update the reuested note goal to the first goal that is requested by
169 // the manipulator
170 if (unsafe_goal != nullptr && unsafe_goal->note_goal() != NoteGoal::NONE &&
171 requested_note_goal_ == NoteGoal::NONE) {
172 requested_note_goal_ = unsafe_goal->note_goal();
173 }
174
Filip Kujawa7a799602024-02-23 12:27:47 -0800175 // Superstructure state machine:
176 // 1. IDLE. The intake is retracted and there is no note in the robot.
177 // Wait for a intake goal to switch state to INTAKING if the extend is ready
178 // 2. INTAKING. Intake the note and transfer it towards the extend.
179 // Give intake, transfer, and extend rollers positive voltage to intake and
180 // transfer. Switch to LOADED when the extend beambreak is triggered.
181 // 3. LOADED. The note is in the extend and the extend is retracted.
182 // Wait for a note goal to switch state to MOVING.
183 // For AMP/TRAP goals, check that the turret is in a position to avoid
184 // collision.
185 // 4. MOVING. The extend is moving towards a goal (AMP, TRAP, or CATAPULT).
186 // For CATAPULT goals, wait for the turret and altitude to be in a position to
187 // accept the note from the extend.
188 // Wait for the extend to reach the goal and switch state to READY if
189 // AMP or TRAP, or to LOADING_CATAPULT if CATAPULT.
190 // 5. LOADING_CATAPULT. The extend is at the position to load the catapult.
191 // Activate the extend roller to transfer the note to the catapult.
192 // Switch state to READY when the catapult beambreak is triggered.
193 // 6. READY. Ready for fire command. The note is either loaded in the catapult
194 // or in the extend and at the AMP or TRAP position. Wait for a fire command.
195 // 7. FIRING. The note is being fired, either from the extend or the catapult.
196 // Switch state back to IDLE when the note is fired.
197
198 switch (state_) {
199 case SuperstructureState::IDLE:
Niko Sohmers5006fc42024-03-01 17:14:22 -0800200 requested_note_goal_ = NoteGoal::NONE;
201
Filip Kujawa7a799602024-02-23 12:27:47 -0800202 if (unsafe_goal != nullptr &&
203 unsafe_goal->intake_goal() == IntakeGoal::INTAKE &&
Niko Sohmersdd971dc2024-02-25 11:40:47 -0800204 extend_at_retracted) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800205 state_ = SuperstructureState::INTAKING;
206 }
Maxwell Henderson4567e282024-02-25 15:38:15 -0800207
Austin Schuh027fd622024-03-01 21:26:07 -0800208 extend_goal_location = ExtendStatus::RETRACTED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800209 catapult_requested_ = false;
210 break;
211 case SuperstructureState::INTAKING:
Filip Kujawa7a799602024-02-23 12:27:47 -0800212 // Switch to LOADED state when the extend beambreak is triggered
213 // meaning the note is loaded in the extend
214 if (position->extend_beambreak()) {
215 state_ = SuperstructureState::LOADED;
216 }
217 intake_roller_state = IntakeRollerStatus::INTAKING;
218 transfer_roller_status = TransferRollerStatus::TRANSFERING_IN;
219 extend_roller_status = ExtendRollerStatus::TRANSFERING_TO_EXTEND;
Austin Schuh027fd622024-03-01 21:26:07 -0800220 extend_goal_location = ExtendStatus::RETRACTED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800221
222 if (!catapult_requested_ && unsafe_goal != nullptr &&
223 unsafe_goal->note_goal() == NoteGoal::CATAPULT) {
224 catapult_requested_ = true;
225 }
226
Maxwell Hendersonf0d22622024-02-26 21:02:11 -0800227 // If we are no longer requesting INTAKE or we are no longer requesting
228 // an INTAKE goal, wait 0.5 seconds then go back to IDLE.
229 if (!(unsafe_goal != nullptr &&
230 unsafe_goal->intake_goal() == IntakeGoal::INTAKE) &&
231 timestamp > intake_end_time_ + kExtraIntakingTime) {
232 state_ = SuperstructureState::IDLE;
233 }
234
Filip Kujawa7a799602024-02-23 12:27:47 -0800235 break;
236 case SuperstructureState::LOADED:
Maxwell Henderson4567e282024-02-25 15:38:15 -0800237 if (!position->extend_beambreak() && !position->catapult_beambreak()) {
238 state_ = SuperstructureState::IDLE;
239 }
240
Niko Sohmers5006fc42024-03-01 17:14:22 -0800241 switch (requested_note_goal_) {
242 case NoteGoal::NONE:
243 break;
244 case NoteGoal::CATAPULT:
245 state_ = SuperstructureState::MOVING;
246 transfer_roller_status = TransferRollerStatus::TRANSFERING_IN;
247 break;
248 case NoteGoal::TRAP:
249 [[fallthrough]];
250 case NoteGoal::AMP:
James Kuszmaul6cae7d72024-03-01 21:29:56 -0800251 transfer_roller_status = TransferRollerStatus::EXTEND_MOVING;
Austin Schuh027fd622024-03-01 21:26:07 -0800252 state_ = SuperstructureState::MOVING;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800253 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800254 }
Austin Schuh027fd622024-03-01 21:26:07 -0800255 extend_goal_location = ExtendStatus::RETRACTED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800256 break;
257 case SuperstructureState::MOVING:
James Kuszmaul6cae7d72024-03-01 21:29:56 -0800258 transfer_roller_status = TransferRollerStatus::EXTEND_MOVING;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800259 switch (requested_note_goal_) {
260 case NoteGoal::NONE:
Austin Schuh027fd622024-03-01 21:26:07 -0800261 extend_goal_location = ExtendStatus::RETRACTED;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800262 if (extend_at_retracted) {
263 state_ = SuperstructureState::LOADED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800264 }
Niko Sohmers5006fc42024-03-01 17:14:22 -0800265 break;
266 case NoteGoal::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800267 extend_goal_location = ExtendStatus::CATAPULT;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800268 if (extend_ready_for_catapult_transfer && turret_ready_for_load &&
269 altitude_ready_for_load) {
Filip Kujawa77e3d8a2024-03-02 11:34:56 -0800270 loading_catapult_start_time_ = timestamp;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800271 state_ = SuperstructureState::LOADING_CATAPULT;
272 }
273 break;
274 case NoteGoal::TRAP:
Austin Schuh027fd622024-03-01 21:26:07 -0800275 extend_goal_location = ExtendStatus::TRAP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800276 // Check if the extend is at the TRAP position and if it is
277 // switch to READY state
278 if (PositionNear(extend_.position(), extend_set_points->trap(),
279 kExtendThreshold)) {
280 state_ = SuperstructureState::READY;
281 }
282 break;
283 case NoteGoal::AMP:
Austin Schuh027fd622024-03-01 21:26:07 -0800284 extend_goal_location = ExtendStatus::AMP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800285 // Check if the extend is at the AMP position and if it is
286 // switch to READY state
287 if (PositionNear(extend_.position(), extend_set_points->amp(),
288 kExtendThreshold)) {
289 state_ = SuperstructureState::READY;
290 }
291 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800292 }
293
294 extend_moving = true;
295 break;
296 case SuperstructureState::LOADING_CATAPULT:
297 extend_moving = false;
Austin Schuh027fd622024-03-01 21:26:07 -0800298 extend_goal_location = ExtendStatus::CATAPULT;
Filip Kujawa7a799602024-02-23 12:27:47 -0800299 extend_roller_status = ExtendRollerStatus::TRANSFERING_TO_CATAPULT;
Austin Schuh6c9f4d82024-03-01 22:42:00 -0800300 transfer_roller_status = TransferRollerStatus::TRANSFERING_IN;
Filip Kujawa7a799602024-02-23 12:27:47 -0800301
Filip Kujawa77e3d8a2024-03-02 11:34:56 -0800302 // If we lost the game piece, reset state to idle.
303 if (((timestamp - loading_catapult_start_time_) >
304 kMaxCatapultLoadingTime) &&
305 !position->catapult_beambreak() && !position->extend_beambreak()) {
306 state_ = SuperstructureState::IDLE;
307 }
308
Filip Kujawa7a799602024-02-23 12:27:47 -0800309 // Switch to READY state when the catapult beambreak is triggered
310 if (position->catapult_beambreak()) {
311 state_ = SuperstructureState::READY;
312 }
313 break;
314 case SuperstructureState::READY:
315 extend_moving = false;
316
317 // Switch to FIRING state when the fire button is pressed
318 if (unsafe_goal != nullptr && unsafe_goal->fire()) {
319 state_ = SuperstructureState::FIRING;
320 }
321
Niko Sohmers5006fc42024-03-01 17:14:22 -0800322 switch (requested_note_goal_) {
323 case NoteGoal::NONE:
Austin Schuh027fd622024-03-01 21:26:07 -0800324 extend_goal_location = ExtendStatus::RETRACTED;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800325 extend_moving = true;
326 state_ = SuperstructureState::MOVING;
327 break;
328 case NoteGoal::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800329 extend_goal_location = ExtendStatus::CATAPULT;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800330 break;
331 case NoteGoal::TRAP:
Austin Schuh027fd622024-03-01 21:26:07 -0800332 extend_goal_location = ExtendStatus::TRAP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800333 break;
334 case NoteGoal::AMP:
Austin Schuh027fd622024-03-01 21:26:07 -0800335 extend_goal_location = ExtendStatus::AMP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800336 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800337 }
Filip Kujawa7a799602024-02-23 12:27:47 -0800338 break;
339 case SuperstructureState::FIRING:
Niko Sohmers5006fc42024-03-01 17:14:22 -0800340 switch (requested_note_goal_) {
341 case NoteGoal::NONE:
Filip Kujawa7a799602024-02-23 12:27:47 -0800342
Niko Sohmers5006fc42024-03-01 17:14:22 -0800343 break;
344 case NoteGoal::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800345 extend_goal_location = ExtendStatus::CATAPULT;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800346 // 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 break;
353 case NoteGoal::TRAP:
Filip Kujawa7a799602024-02-23 12:27:47 -0800354 extend_roller_status = ExtendRollerStatus::SCORING_IN_TRAP;
Austin Schuh027fd622024-03-01 21:26:07 -0800355 extend_goal_location = ExtendStatus::TRAP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800356 if (!position->extend_beambreak() && unsafe_goal != nullptr &&
357 !unsafe_goal->fire()) {
358 state_ = SuperstructureState::IDLE;
359 }
360 break;
361 case NoteGoal::AMP:
362 extend_roller_status = ExtendRollerStatus::SCORING_IN_AMP;
Austin Schuh027fd622024-03-01 21:26:07 -0800363 extend_goal_location = ExtendStatus::AMP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800364 if (!position->extend_beambreak() && unsafe_goal != nullptr &&
365 !unsafe_goal->fire()) {
366 state_ = SuperstructureState::IDLE;
367 }
368 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800369 }
370 break;
371 }
372
373 if (unsafe_goal != nullptr &&
374 unsafe_goal->intake_goal() == IntakeGoal::SPIT) {
375 intake_roller_state = IntakeRollerStatus::SPITTING;
376 transfer_roller_status = TransferRollerStatus::TRANSFERING_OUT;
377 }
378
379 // Update Intake Roller voltage based on status from state machine.
380 switch (intake_roller_state) {
381 case IntakeRollerStatus::NONE:
382 output_struct.intake_roller_voltage = 0.0;
383 break;
384 case IntakeRollerStatus::SPITTING:
385 output_struct.intake_roller_voltage =
386 robot_constants_->common()->intake_roller_voltages()->spitting();
387 break;
388 case IntakeRollerStatus::INTAKING:
389 output_struct.intake_roller_voltage =
390 robot_constants_->common()->intake_roller_voltages()->intaking();
391 break;
392 }
393
394 // Update Transfer Roller voltage based on status from state machine.
395 switch (transfer_roller_status) {
396 case TransferRollerStatus::NONE:
397 output_struct.transfer_roller_voltage = 0.0;
398 break;
399 case TransferRollerStatus::TRANSFERING_IN:
400 output_struct.transfer_roller_voltage =
401 robot_constants_->common()->transfer_roller_voltages()->transfer_in();
402 break;
403 case TransferRollerStatus::TRANSFERING_OUT:
404 output_struct.transfer_roller_voltage = robot_constants_->common()
405 ->transfer_roller_voltages()
406 ->transfer_out();
407 break;
James Kuszmaul6cae7d72024-03-01 21:29:56 -0800408 case TransferRollerStatus::EXTEND_MOVING:
409 output_struct.transfer_roller_voltage = robot_constants_->common()
410 ->transfer_roller_voltages()
411 ->extend_moving();
412 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800413 }
414
415 // Update Extend Roller voltage based on status from state machine.
416 const ExtendRollerVoltages *extend_roller_voltages =
417 robot_constants_->common()->extend_roller_voltages();
418 switch (extend_roller_status) {
419 case ExtendRollerStatus::IDLE:
420 // No voltage applied when idle
421 output_struct.extend_roller_voltage = 0.0;
422 break;
423 case ExtendRollerStatus::TRANSFERING_TO_EXTEND:
424 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
425 break;
426 case ExtendRollerStatus::SCORING_IN_AMP:
427 [[fallthrough]];
428 case ExtendRollerStatus::SCORING_IN_TRAP:
429 // Apply scoring voltage during scoring in amp or trap
430 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
431 break;
432 case ExtendRollerStatus::TRANSFERING_TO_CATAPULT:
433 // Apply scoring voltage during transferring to catapult
434 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
435 break;
436 }
437
Austin Schuh027fd622024-03-01 21:26:07 -0800438 double extend_goal_position = 0.0;
Filip Kujawa7a799602024-02-23 12:27:47 -0800439
James Kuszmaul0281e152024-02-26 22:26:16 -0800440 if (unsafe_goal != nullptr && unsafe_goal->note_goal() == NoteGoal::TRAP) {
Austin Schuh027fd622024-03-01 21:26:07 -0800441 extend_goal_location = ExtendStatus::TRAP;
James Kuszmaul0281e152024-02-26 22:26:16 -0800442 }
443
Filip Kujawa7a799602024-02-23 12:27:47 -0800444 // Set the extend position based on the state machine output
Austin Schuh027fd622024-03-01 21:26:07 -0800445 switch (extend_goal_location) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800446 case ExtendStatus::RETRACTED:
Austin Schuh027fd622024-03-01 21:26:07 -0800447 extend_goal_position = extend_set_points->retracted();
Filip Kujawa7a799602024-02-23 12:27:47 -0800448 break;
449 case ExtendStatus::AMP:
Austin Schuh027fd622024-03-01 21:26:07 -0800450 extend_goal_position = extend_set_points->amp();
Filip Kujawa7a799602024-02-23 12:27:47 -0800451 break;
452 case ExtendStatus::TRAP:
Austin Schuh027fd622024-03-01 21:26:07 -0800453 extend_goal_position = extend_set_points->trap();
Filip Kujawa7a799602024-02-23 12:27:47 -0800454 break;
455 case ExtendStatus::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800456 extend_goal_position = extend_set_points->catapult();
Filip Kujawa7a799602024-02-23 12:27:47 -0800457 break;
458 case ExtendStatus::MOVING:
459 // Should never happen
460 break;
461 }
462
Niko Sohmers5006fc42024-03-01 17:14:22 -0800463 NoteStatus uncompleted_note_goal_status = NoteStatus::NONE;
464
465 switch (requested_note_goal_) {
466 case NoteGoal::NONE:
467 uncompleted_note_goal_status = NoteStatus::NONE;
468 break;
469 case NoteGoal::CATAPULT:
470 uncompleted_note_goal_status = NoteStatus::CATAPULT;
471 break;
472 case NoteGoal::AMP:
473 uncompleted_note_goal_status = NoteStatus::AMP;
474 break;
475 case NoteGoal::TRAP:
476 uncompleted_note_goal_status = NoteStatus::TRAP;
477 break;
478 }
479
Filip Kujawa7a799602024-02-23 12:27:47 -0800480 // Set the extend status based on the state machine output
481 // If the extend is moving, the status is MOVING, otherwise it is the same
482 // as extend_status
483 ExtendStatus extend_status =
Austin Schuh027fd622024-03-01 21:26:07 -0800484 (extend_moving ? ExtendStatus::MOVING : extend_goal_location);
Filip Kujawa7a799602024-02-23 12:27:47 -0800485
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800486 if (joystick_state_fetcher_.Fetch() &&
487 joystick_state_fetcher_->has_alliance()) {
488 alliance_ = joystick_state_fetcher_->alliance();
489 }
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800490
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800491 drivetrain_status_fetcher_.Fetch();
492
Austin Schuh027fd622024-03-01 21:26:07 -0800493 const bool collided = collision_avoidance_.IsCollided({
494 .intake_pivot_position = intake_pivot_.estimated_position(),
495 .turret_position = shooter_.turret().estimated_position(),
496 .extend_position = extend_.estimated_position(),
497 });
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800498
Filip Kujawa6d717632024-02-01 11:40:55 -0800499 aos::FlatbufferFixedAllocatorArray<
500 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
501 climber_goal_buffer;
502
503 climber_goal_buffer.Finish(
504 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
505 *climber_goal_buffer.fbb(), climber_position));
506
507 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
508 *climber_goal = &climber_goal_buffer.message();
509
510 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
511 climber_status_offset = climber_.Iterate(
512 climber_goal, position->climber(),
513 output != nullptr ? &output_struct.climber_voltage : nullptr,
514 status->fbb());
515
Niko Sohmersac4d8872024-02-23 13:55:47 -0800516 double max_intake_pivot_position = 0;
517 double min_intake_pivot_position = 0;
Austin Schuh027fd622024-03-01 21:26:07 -0800518 double max_extend_position = 0;
519 double min_extend_position = 0;
Niko Sohmersac4d8872024-02-23 13:55:47 -0800520
521 aos::FlatbufferFixedAllocatorArray<
522 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
523 intake_pivot_goal_buffer;
524
525 intake_pivot_goal_buffer.Finish(
526 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
527 *intake_pivot_goal_buffer.fbb(), intake_pivot_position));
528
529 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
530 *intake_pivot_goal = &intake_pivot_goal_buffer.message();
531
532 double *intake_output =
533 (output != nullptr ? &output_struct.intake_pivot_voltage : nullptr);
534
535 const bool disabled = intake_pivot_.Correct(
536 intake_pivot_goal, position->intake_pivot(), intake_output == nullptr);
537
Austin Schuh027fd622024-03-01 21:26:07 -0800538 aos::FlatbufferFixedAllocatorArray<
539 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
540 extend_goal_buffer;
541
542 extend_goal_buffer.Finish(
543 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
544 *extend_goal_buffer.fbb(), extend_goal_position));
545
546 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
547 *extend_goal = &extend_goal_buffer.message();
548
Filip Kujawa7a799602024-02-23 12:27:47 -0800549 // TODO(max): Change how we handle the collision with the turret and
550 // intake to be clearer
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800551 const flatbuffers::Offset<ShooterStatus> shooter_status_offset =
552 shooter_.Iterate(
553 position,
554 unsafe_goal != nullptr ? unsafe_goal->shooter_goal() : nullptr,
Filip Kujawa7a799602024-02-23 12:27:47 -0800555 unsafe_goal != nullptr ? unsafe_goal->fire() : false,
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800556 output != nullptr ? &output_struct.catapult_voltage : nullptr,
557 output != nullptr ? &output_struct.altitude_voltage : nullptr,
558 output != nullptr ? &output_struct.turret_voltage : nullptr,
559 output != nullptr ? &output_struct.retention_roller_voltage : nullptr,
Maxwell Hendersond5bf47a2024-02-23 17:16:48 -0800560 output != nullptr
561 ? &output_struct.retention_roller_stator_current_limit
562 : nullptr,
Maxwell Henderson461a8a42024-02-23 17:07:39 -0800563 robot_state().voltage_battery(), &collision_avoidance_,
Austin Schuh027fd622024-03-01 21:26:07 -0800564 extend_goal_position, extend_.estimated_position(),
565 &max_extend_position, &min_extend_position,
Niko Sohmersac4d8872024-02-23 13:55:47 -0800566 intake_pivot_.estimated_position(), &max_intake_pivot_position,
Stephan Pleines9f3983a2024-03-13 20:22:38 -0700567 &min_intake_pivot_position, requested_note_goal_, status->fbb(),
568 timestamp);
Niko Sohmersac4d8872024-02-23 13:55:47 -0800569
570 intake_pivot_.set_min_position(min_intake_pivot_position);
571 intake_pivot_.set_max_position(max_intake_pivot_position);
572
Austin Schuh027fd622024-03-01 21:26:07 -0800573 extend_.set_min_position(min_extend_position);
574 extend_.set_max_position(max_extend_position);
575
Niko Sohmersac4d8872024-02-23 13:55:47 -0800576 // Calculate the loops for a cycle.
577 const double voltage = intake_pivot_.UpdateController(disabled);
578
579 intake_pivot_.UpdateObserver(voltage);
580
581 // Write out all the voltages.
582 if (intake_output) {
583 *intake_output = voltage;
584 }
585
586 const flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus>
587 intake_pivot_status_offset = intake_pivot_.MakeStatus(status->fbb());
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800588
Filip Kujawa7a799602024-02-23 12:27:47 -0800589 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
590 extend_status_offset = extend_.Iterate(
Austin Schuh027fd622024-03-01 21:26:07 -0800591 extend_goal, position->extend(),
Filip Kujawa7a799602024-02-23 12:27:47 -0800592 output != nullptr ? &output_struct.extend_voltage : nullptr,
593 status->fbb());
594
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800595 if (output) {
596 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
597 }
598
599 Status::Builder status_builder = status->MakeBuilder<Status>();
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800600
Filip Kujawa7a799602024-02-23 12:27:47 -0800601 const bool zeroed = intake_pivot_.zeroed() && climber_.zeroed() &&
602 shooter_.zeroed() && extend_.zeroed();
603 const bool estopped = intake_pivot_.estopped() || climber_.estopped() ||
604 shooter_.estopped() || extend_.estopped();
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800605
606 status_builder.add_zeroed(zeroed);
607 status_builder.add_estopped(estopped);
Filip Kujawa102a9b22024-02-18 09:40:23 -0800608 status_builder.add_intake_roller(intake_roller_state);
609 status_builder.add_intake_pivot(intake_pivot_status_offset);
Filip Kujawa7a799602024-02-23 12:27:47 -0800610 status_builder.add_transfer_roller(transfer_roller_status);
Filip Kujawa102a9b22024-02-18 09:40:23 -0800611 status_builder.add_climber(climber_status_offset);
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800612 status_builder.add_shooter(shooter_status_offset);
Niko Sohmersac4d8872024-02-23 13:55:47 -0800613 status_builder.add_collided(collided);
Filip Kujawa7a799602024-02-23 12:27:47 -0800614 status_builder.add_extend_roller(extend_roller_status);
615 status_builder.add_extend_status(extend_status);
616 status_builder.add_extend(extend_status_offset);
617 status_builder.add_state(state_);
Niko Sohmers5006fc42024-03-01 17:14:22 -0800618 status_builder.add_uncompleted_note_goal(uncompleted_note_goal_status);
James Kuszmaul0281e152024-02-26 22:26:16 -0800619 status_builder.add_extend_ready_for_transfer(extend_at_retracted);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800620
621 (void)status->Send(status_builder.Finish());
622}
623
624double Superstructure::robot_velocity() const {
625 return (drivetrain_status_fetcher_.get() != nullptr
626 ? drivetrain_status_fetcher_->robot_speed()
627 : 0.0);
628}
629
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800630} // namespace y2024::control_loops::superstructure