blob: dc9369e52bf19dea08196607c66c4a4590c39ec5 [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;
300
Filip Kujawa77e3d8a2024-03-02 11:34:56 -0800301 // If we lost the game piece, reset state to idle.
302 if (((timestamp - loading_catapult_start_time_) >
303 kMaxCatapultLoadingTime) &&
304 !position->catapult_beambreak() && !position->extend_beambreak()) {
305 state_ = SuperstructureState::IDLE;
306 }
307
Filip Kujawa7a799602024-02-23 12:27:47 -0800308 // 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
Niko Sohmers5006fc42024-03-01 17:14:22 -0800321 switch (requested_note_goal_) {
322 case NoteGoal::NONE:
Austin Schuh027fd622024-03-01 21:26:07 -0800323 extend_goal_location = ExtendStatus::RETRACTED;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800324 extend_moving = true;
325 state_ = SuperstructureState::MOVING;
326 break;
327 case NoteGoal::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800328 extend_goal_location = ExtendStatus::CATAPULT;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800329 break;
330 case NoteGoal::TRAP:
Austin Schuh027fd622024-03-01 21:26:07 -0800331 extend_goal_location = ExtendStatus::TRAP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800332 break;
333 case NoteGoal::AMP:
Austin Schuh027fd622024-03-01 21:26:07 -0800334 extend_goal_location = ExtendStatus::AMP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800335 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800336 }
Filip Kujawa7a799602024-02-23 12:27:47 -0800337 break;
338 case SuperstructureState::FIRING:
Niko Sohmers5006fc42024-03-01 17:14:22 -0800339 switch (requested_note_goal_) {
340 case NoteGoal::NONE:
Filip Kujawa7a799602024-02-23 12:27:47 -0800341
Niko Sohmers5006fc42024-03-01 17:14:22 -0800342 break;
343 case NoteGoal::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800344 extend_goal_location = ExtendStatus::CATAPULT;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800345 // Reset the state to IDLE when the game piece is fired from the
346 // catapult. We consider the game piece to be fired from the catapult
347 // when the catapultbeambreak is no longer triggered.
348 if (!position->catapult_beambreak()) {
349 state_ = SuperstructureState::IDLE;
350 }
351 break;
352 case NoteGoal::TRAP:
Filip Kujawa7a799602024-02-23 12:27:47 -0800353 extend_roller_status = ExtendRollerStatus::SCORING_IN_TRAP;
Austin Schuh027fd622024-03-01 21:26:07 -0800354 extend_goal_location = ExtendStatus::TRAP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800355 if (!position->extend_beambreak() && unsafe_goal != nullptr &&
356 !unsafe_goal->fire()) {
357 state_ = SuperstructureState::IDLE;
358 }
359 break;
360 case NoteGoal::AMP:
361 extend_roller_status = ExtendRollerStatus::SCORING_IN_AMP;
Austin Schuh027fd622024-03-01 21:26:07 -0800362 extend_goal_location = ExtendStatus::AMP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800363 if (!position->extend_beambreak() && unsafe_goal != nullptr &&
364 !unsafe_goal->fire()) {
365 state_ = SuperstructureState::IDLE;
366 }
367 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800368 }
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;
James Kuszmaul6cae7d72024-03-01 21:29:56 -0800407 case TransferRollerStatus::EXTEND_MOVING:
408 output_struct.transfer_roller_voltage = robot_constants_->common()
409 ->transfer_roller_voltages()
410 ->extend_moving();
411 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800412 }
413
414 // Update Extend Roller voltage based on status from state machine.
415 const ExtendRollerVoltages *extend_roller_voltages =
416 robot_constants_->common()->extend_roller_voltages();
417 switch (extend_roller_status) {
418 case ExtendRollerStatus::IDLE:
419 // No voltage applied when idle
420 output_struct.extend_roller_voltage = 0.0;
421 break;
422 case ExtendRollerStatus::TRANSFERING_TO_EXTEND:
423 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
424 break;
425 case ExtendRollerStatus::SCORING_IN_AMP:
426 [[fallthrough]];
427 case ExtendRollerStatus::SCORING_IN_TRAP:
428 // Apply scoring voltage during scoring in amp or trap
429 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
430 break;
431 case ExtendRollerStatus::TRANSFERING_TO_CATAPULT:
432 // Apply scoring voltage during transferring to catapult
433 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
434 break;
435 }
436
Austin Schuh027fd622024-03-01 21:26:07 -0800437 double extend_goal_position = 0.0;
Filip Kujawa7a799602024-02-23 12:27:47 -0800438
James Kuszmaul0281e152024-02-26 22:26:16 -0800439 if (unsafe_goal != nullptr && unsafe_goal->note_goal() == NoteGoal::TRAP) {
Austin Schuh027fd622024-03-01 21:26:07 -0800440 extend_goal_location = ExtendStatus::TRAP;
James Kuszmaul0281e152024-02-26 22:26:16 -0800441 }
442
Filip Kujawa7a799602024-02-23 12:27:47 -0800443 // Set the extend position based on the state machine output
Austin Schuh027fd622024-03-01 21:26:07 -0800444 switch (extend_goal_location) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800445 case ExtendStatus::RETRACTED:
Austin Schuh027fd622024-03-01 21:26:07 -0800446 extend_goal_position = extend_set_points->retracted();
Filip Kujawa7a799602024-02-23 12:27:47 -0800447 break;
448 case ExtendStatus::AMP:
Austin Schuh027fd622024-03-01 21:26:07 -0800449 extend_goal_position = extend_set_points->amp();
Filip Kujawa7a799602024-02-23 12:27:47 -0800450 break;
451 case ExtendStatus::TRAP:
Austin Schuh027fd622024-03-01 21:26:07 -0800452 extend_goal_position = extend_set_points->trap();
Filip Kujawa7a799602024-02-23 12:27:47 -0800453 break;
454 case ExtendStatus::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800455 extend_goal_position = extend_set_points->catapult();
Filip Kujawa7a799602024-02-23 12:27:47 -0800456 break;
457 case ExtendStatus::MOVING:
458 // Should never happen
459 break;
460 }
461
Niko Sohmers5006fc42024-03-01 17:14:22 -0800462 NoteStatus uncompleted_note_goal_status = NoteStatus::NONE;
463
464 switch (requested_note_goal_) {
465 case NoteGoal::NONE:
466 uncompleted_note_goal_status = NoteStatus::NONE;
467 break;
468 case NoteGoal::CATAPULT:
469 uncompleted_note_goal_status = NoteStatus::CATAPULT;
470 break;
471 case NoteGoal::AMP:
472 uncompleted_note_goal_status = NoteStatus::AMP;
473 break;
474 case NoteGoal::TRAP:
475 uncompleted_note_goal_status = NoteStatus::TRAP;
476 break;
477 }
478
Filip Kujawa7a799602024-02-23 12:27:47 -0800479 // Set the extend status based on the state machine output
480 // If the extend is moving, the status is MOVING, otherwise it is the same
481 // as extend_status
482 ExtendStatus extend_status =
Austin Schuh027fd622024-03-01 21:26:07 -0800483 (extend_moving ? ExtendStatus::MOVING : extend_goal_location);
Filip Kujawa7a799602024-02-23 12:27:47 -0800484
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800485 if (joystick_state_fetcher_.Fetch() &&
486 joystick_state_fetcher_->has_alliance()) {
487 alliance_ = joystick_state_fetcher_->alliance();
488 }
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800489
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800490 drivetrain_status_fetcher_.Fetch();
491
Austin Schuh027fd622024-03-01 21:26:07 -0800492 const bool collided = collision_avoidance_.IsCollided({
493 .intake_pivot_position = intake_pivot_.estimated_position(),
494 .turret_position = shooter_.turret().estimated_position(),
495 .extend_position = extend_.estimated_position(),
496 });
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800497
Filip Kujawa6d717632024-02-01 11:40:55 -0800498 aos::FlatbufferFixedAllocatorArray<
499 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
500 climber_goal_buffer;
501
502 climber_goal_buffer.Finish(
503 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
504 *climber_goal_buffer.fbb(), climber_position));
505
506 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
507 *climber_goal = &climber_goal_buffer.message();
508
509 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
510 climber_status_offset = climber_.Iterate(
511 climber_goal, position->climber(),
512 output != nullptr ? &output_struct.climber_voltage : nullptr,
513 status->fbb());
514
Niko Sohmersac4d8872024-02-23 13:55:47 -0800515 double max_intake_pivot_position = 0;
516 double min_intake_pivot_position = 0;
Austin Schuh027fd622024-03-01 21:26:07 -0800517 double max_extend_position = 0;
518 double min_extend_position = 0;
Niko Sohmersac4d8872024-02-23 13:55:47 -0800519
520 aos::FlatbufferFixedAllocatorArray<
521 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
522 intake_pivot_goal_buffer;
523
524 intake_pivot_goal_buffer.Finish(
525 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
526 *intake_pivot_goal_buffer.fbb(), intake_pivot_position));
527
528 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
529 *intake_pivot_goal = &intake_pivot_goal_buffer.message();
530
531 double *intake_output =
532 (output != nullptr ? &output_struct.intake_pivot_voltage : nullptr);
533
534 const bool disabled = intake_pivot_.Correct(
535 intake_pivot_goal, position->intake_pivot(), intake_output == nullptr);
536
Austin Schuh027fd622024-03-01 21:26:07 -0800537 aos::FlatbufferFixedAllocatorArray<
538 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
539 extend_goal_buffer;
540
541 extend_goal_buffer.Finish(
542 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
543 *extend_goal_buffer.fbb(), extend_goal_position));
544
545 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
546 *extend_goal = &extend_goal_buffer.message();
547
Filip Kujawa7a799602024-02-23 12:27:47 -0800548 // TODO(max): Change how we handle the collision with the turret and
549 // intake to be clearer
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800550 const flatbuffers::Offset<ShooterStatus> shooter_status_offset =
551 shooter_.Iterate(
552 position,
553 unsafe_goal != nullptr ? unsafe_goal->shooter_goal() : nullptr,
Filip Kujawa7a799602024-02-23 12:27:47 -0800554 unsafe_goal != nullptr ? unsafe_goal->fire() : false,
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800555 output != nullptr ? &output_struct.catapult_voltage : nullptr,
556 output != nullptr ? &output_struct.altitude_voltage : nullptr,
557 output != nullptr ? &output_struct.turret_voltage : nullptr,
558 output != nullptr ? &output_struct.retention_roller_voltage : nullptr,
Maxwell Hendersond5bf47a2024-02-23 17:16:48 -0800559 output != nullptr
560 ? &output_struct.retention_roller_stator_current_limit
561 : nullptr,
Maxwell Henderson461a8a42024-02-23 17:07:39 -0800562 robot_state().voltage_battery(), &collision_avoidance_,
Austin Schuh027fd622024-03-01 21:26:07 -0800563 extend_goal_position, extend_.estimated_position(),
564 &max_extend_position, &min_extend_position,
Niko Sohmersac4d8872024-02-23 13:55:47 -0800565 intake_pivot_.estimated_position(), &max_intake_pivot_position,
Austin Schuh027fd622024-03-01 21:26:07 -0800566 &min_intake_pivot_position, status->fbb(), timestamp);
Niko Sohmersac4d8872024-02-23 13:55:47 -0800567
568 intake_pivot_.set_min_position(min_intake_pivot_position);
569 intake_pivot_.set_max_position(max_intake_pivot_position);
570
Austin Schuh027fd622024-03-01 21:26:07 -0800571 extend_.set_min_position(min_extend_position);
572 extend_.set_max_position(max_extend_position);
573
Niko Sohmersac4d8872024-02-23 13:55:47 -0800574 // Calculate the loops for a cycle.
575 const double voltage = intake_pivot_.UpdateController(disabled);
576
577 intake_pivot_.UpdateObserver(voltage);
578
579 // Write out all the voltages.
580 if (intake_output) {
581 *intake_output = voltage;
582 }
583
584 const flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus>
585 intake_pivot_status_offset = intake_pivot_.MakeStatus(status->fbb());
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800586
Filip Kujawa7a799602024-02-23 12:27:47 -0800587 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
588 extend_status_offset = extend_.Iterate(
Austin Schuh027fd622024-03-01 21:26:07 -0800589 extend_goal, position->extend(),
Filip Kujawa7a799602024-02-23 12:27:47 -0800590 output != nullptr ? &output_struct.extend_voltage : nullptr,
591 status->fbb());
592
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800593 if (output) {
594 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
595 }
596
597 Status::Builder status_builder = status->MakeBuilder<Status>();
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800598
Filip Kujawa7a799602024-02-23 12:27:47 -0800599 const bool zeroed = intake_pivot_.zeroed() && climber_.zeroed() &&
600 shooter_.zeroed() && extend_.zeroed();
601 const bool estopped = intake_pivot_.estopped() || climber_.estopped() ||
602 shooter_.estopped() || extend_.estopped();
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800603
604 status_builder.add_zeroed(zeroed);
605 status_builder.add_estopped(estopped);
Filip Kujawa102a9b22024-02-18 09:40:23 -0800606 status_builder.add_intake_roller(intake_roller_state);
607 status_builder.add_intake_pivot(intake_pivot_status_offset);
Filip Kujawa7a799602024-02-23 12:27:47 -0800608 status_builder.add_transfer_roller(transfer_roller_status);
Filip Kujawa102a9b22024-02-18 09:40:23 -0800609 status_builder.add_climber(climber_status_offset);
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800610 status_builder.add_shooter(shooter_status_offset);
Niko Sohmersac4d8872024-02-23 13:55:47 -0800611 status_builder.add_collided(collided);
Filip Kujawa7a799602024-02-23 12:27:47 -0800612 status_builder.add_extend_roller(extend_roller_status);
613 status_builder.add_extend_status(extend_status);
614 status_builder.add_extend(extend_status_offset);
615 status_builder.add_state(state_);
Niko Sohmers5006fc42024-03-01 17:14:22 -0800616 status_builder.add_uncompleted_note_goal(uncompleted_note_goal_status);
James Kuszmaul0281e152024-02-26 22:26:16 -0800617 status_builder.add_extend_ready_for_transfer(extend_at_retracted);
Niko Sohmers2a251cd2024-03-02 19:16:15 -0800618 status_builder.add_extend_at_retracted(extend_at_retracted);
619 status_builder.add_turret_ready_for_load(turret_ready_for_load);
620 status_builder.add_altitude_ready_for_load(altitude_ready_for_load);
621 status_builder.add_extend_ready_for_catapult_transfer(
622 extend_ready_for_catapult_transfer);
623 status_builder.add_extend_beambreak(position->extend_beambreak());
624 status_builder.add_catapult_beambreak(position->catapult_beambreak());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800625
626 (void)status->Send(status_builder.Finish());
627}
628
629double Superstructure::robot_velocity() const {
630 return (drivetrain_status_fetcher_.get() != nullptr
631 ? drivetrain_status_fetcher_->robot_speed()
632 : 0.0);
633}
634
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800635} // namespace y2024::control_loops::superstructure