blob: ad91614d8afe08aa67517cdf3c193e773c4f7da8 [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
Stephan Pleinesf63bde82024-01-13 15:59:33 -080025namespace y2024::control_loops::superstructure {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080026
27using ::aos::monotonic_clock;
28
29using frc971::control_loops::AbsoluteEncoderProfiledJointStatus;
30using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus;
31using frc971::control_loops::RelativeEncoderProfiledJointStatus;
32
33Superstructure::Superstructure(::aos::EventLoop *event_loop,
Niko Sohmers3860f8a2024-01-12 21:05:19 -080034 const ::std::string &name)
35 : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop,
36 name),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080037 constants_fetcher_(event_loop),
Niko Sohmersafc51fe2024-01-29 17:48:35 -080038 robot_constants_(CHECK_NOTNULL(&constants_fetcher_.constants())),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080039 drivetrain_status_fetcher_(
40 event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>(
41 "/drivetrain")),
42 joystick_state_fetcher_(
Niko Sohmersafc51fe2024-01-29 17:48:35 -080043 event_loop->MakeFetcher<aos::JoystickState>("/aos")),
Niko Sohmers74b0ad52024-02-03 18:00:31 -080044 intake_pivot_(robot_constants_->common()->intake_pivot(),
45 robot_constants_->robot()->intake_constants()),
Filip Kujawa6d717632024-02-01 11:40:55 -080046 climber_(
47 robot_constants_->common()->climber(),
Niko Sohmersc4d2c502024-02-19 19:35:35 -080048 robot_constants_->robot()->climber_constants()->zeroing_constants()),
Filip Kujawa7a799602024-02-23 12:27:47 -080049 shooter_(event_loop, robot_constants_),
50 extend_(
51 robot_constants_->common()->extend(),
52 robot_constants_->robot()->extend_constants()->zeroing_constants()) {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080053 event_loop->SetRuntimeRealtimePriority(30);
54}
55
Filip Kujawa7a799602024-02-23 12:27:47 -080056bool PositionNear(double position, double goal, double threshold) {
57 return std::abs(position - goal) < threshold;
58}
59
Niko Sohmers3860f8a2024-01-12 21:05:19 -080060void Superstructure::RunIteration(const Goal *unsafe_goal,
61 const Position *position,
62 aos::Sender<Output>::Builder *output,
63 aos::Sender<Status>::Builder *status) {
64 const monotonic_clock::time_point timestamp =
65 event_loop()->context().monotonic_event_time;
66
Niko Sohmers3860f8a2024-01-12 21:05:19 -080067 if (WasReset()) {
68 AOS_LOG(ERROR, "WPILib reset, restarting\n");
Niko Sohmersafc51fe2024-01-29 17:48:35 -080069 intake_pivot_.Reset();
Filip Kujawa6d717632024-02-01 11:40:55 -080070 climber_.Reset();
Niko Sohmersc4d2c502024-02-19 19:35:35 -080071 shooter_.Reset();
Filip Kujawa7a799602024-02-23 12:27:47 -080072 extend_.Reset();
Niko Sohmers3860f8a2024-01-12 21:05:19 -080073 }
74
75 OutputT output_struct;
Niko Sohmersafc51fe2024-01-29 17:48:35 -080076
Filip Kujawa7a799602024-02-23 12:27:47 -080077 // Handle Climber Goal separately from main superstructure state machine
Filip Kujawa6d717632024-02-01 11:40:55 -080078 double climber_position =
79 robot_constants_->common()->climber_set_points()->retract();
80
81 if (unsafe_goal != nullptr) {
82 switch (unsafe_goal->climber_goal()) {
83 case ClimberGoal::FULL_EXTEND:
84 climber_position =
85 robot_constants_->common()->climber_set_points()->full_extend();
86 break;
Filip Kujawa6d717632024-02-01 11:40:55 -080087 case ClimberGoal::RETRACT:
88 climber_position =
89 robot_constants_->common()->climber_set_points()->retract();
90 break;
Maxwell Henderson7db29782024-02-24 20:10:26 -080091 case ClimberGoal::STOWED:
92 climber_position =
93 robot_constants_->common()->climber_set_points()->stowed();
Filip Kujawa6d717632024-02-01 11:40:55 -080094 }
95 }
96
Filip Kujawa7a799602024-02-23 12:27:47 -080097 // If we started off preloaded, skip to the ready state.
98 if (unsafe_goal != nullptr && unsafe_goal->shooter_goal() &&
99 unsafe_goal->shooter_goal()->preloaded()) {
100 if (state_ != SuperstructureState::READY &&
101 state_ != SuperstructureState::FIRING) {
102 state_ = SuperstructureState::READY;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800103 requested_note_goal_ = NoteGoal::CATAPULT;
Filip Kujawa7a799602024-02-23 12:27:47 -0800104 }
105 }
106
107 // Handle the intake pivot goal separately from the main superstructure state
108 IntakeRollerStatus intake_roller_state = IntakeRollerStatus::NONE;
109 double intake_pivot_position =
110 robot_constants_->common()->intake_pivot_set_points()->retracted();
111
112 if (unsafe_goal != nullptr) {
113 switch (unsafe_goal->intake_goal()) {
114 case IntakeGoal::INTAKE:
115 intake_pivot_position =
116 robot_constants_->common()->intake_pivot_set_points()->extended();
Maxwell Hendersonf0d22622024-02-26 21:02:11 -0800117 intake_end_time_ = timestamp;
Filip Kujawa7a799602024-02-23 12:27:47 -0800118 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;
Austin Schuh027fd622024-03-01 21:26:07 -0800131 ExtendStatus extend_goal_location = ExtendStatus::RETRACTED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800132
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.
Niko Sohmersdd971dc2024-02-25 11:40:47 -0800143 const bool extend_at_retracted = PositionNear(
Filip Kujawa7a799602024-02-23 12:27:47 -0800144 extend_.position(), extend_set_points->retracted(), kExtendThreshold);
145
Niko Sohmers5006fc42024-03-01 17:14:22 -0800146 // Check if the turret is at the position to accept the note from extend
147 const bool turret_ready_for_load =
148 PositionNear(shooter_.turret().estimated_position(),
149 robot_constants_->common()->turret_loading_position(),
150 kTurretLoadingThreshold);
151
Niko Sohmers5006fc42024-03-01 17:14:22 -0800152 // Check if the altitude is at the position to accept the note from
153 // extend
154 const bool altitude_ready_for_load =
155 PositionNear(shooter_.altitude().estimated_position(),
156 robot_constants_->common()->altitude_loading_position(),
157 kAltitudeLoadingThreshold);
158
159 // Check if the extend is at the position to load the catapult
160 const bool extend_ready_for_catapult_transfer = PositionNear(
161 extend_.position(), extend_set_points->catapult(), kExtendThreshold);
162
Niko Sohmers5006fc42024-03-01 17:14:22 -0800163 // Only update the reuested note goal to the first goal that is requested by
164 // the manipulator
165 if (unsafe_goal != nullptr && unsafe_goal->note_goal() != NoteGoal::NONE &&
166 requested_note_goal_ == NoteGoal::NONE) {
167 requested_note_goal_ = unsafe_goal->note_goal();
168 }
169
Filip Kujawa7a799602024-02-23 12:27:47 -0800170 // Superstructure state machine:
171 // 1. IDLE. The intake is retracted and there is no note in the robot.
172 // Wait for a intake goal to switch state to INTAKING if the extend is ready
173 // 2. INTAKING. Intake the note and transfer it towards the extend.
174 // Give intake, transfer, and extend rollers positive voltage to intake and
175 // transfer. Switch to LOADED when the extend beambreak is triggered.
176 // 3. LOADED. The note is in the extend and the extend is retracted.
177 // Wait for a note goal to switch state to MOVING.
178 // For AMP/TRAP goals, check that the turret is in a position to avoid
179 // collision.
180 // 4. MOVING. The extend is moving towards a goal (AMP, TRAP, or CATAPULT).
181 // For CATAPULT goals, wait for the turret and altitude to be in a position to
182 // accept the note from the extend.
183 // Wait for the extend to reach the goal and switch state to READY if
184 // AMP or TRAP, or to LOADING_CATAPULT if CATAPULT.
185 // 5. LOADING_CATAPULT. The extend is at the position to load the catapult.
186 // Activate the extend roller to transfer the note to the catapult.
187 // Switch state to READY when the catapult beambreak is triggered.
188 // 6. READY. Ready for fire command. The note is either loaded in the catapult
189 // or in the extend and at the AMP or TRAP position. Wait for a fire command.
190 // 7. FIRING. The note is being fired, either from the extend or the catapult.
191 // Switch state back to IDLE when the note is fired.
192
193 switch (state_) {
194 case SuperstructureState::IDLE:
Niko Sohmers5006fc42024-03-01 17:14:22 -0800195 requested_note_goal_ = NoteGoal::NONE;
196
Filip Kujawa7a799602024-02-23 12:27:47 -0800197 if (unsafe_goal != nullptr &&
198 unsafe_goal->intake_goal() == IntakeGoal::INTAKE &&
Niko Sohmersdd971dc2024-02-25 11:40:47 -0800199 extend_at_retracted) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800200 state_ = SuperstructureState::INTAKING;
201 }
Maxwell Henderson4567e282024-02-25 15:38:15 -0800202
Austin Schuh027fd622024-03-01 21:26:07 -0800203 extend_goal_location = ExtendStatus::RETRACTED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800204 catapult_requested_ = false;
205 break;
206 case SuperstructureState::INTAKING:
Filip Kujawa7a799602024-02-23 12:27:47 -0800207 // Switch to LOADED state when the extend beambreak is triggered
208 // meaning the note is loaded in the extend
209 if (position->extend_beambreak()) {
210 state_ = SuperstructureState::LOADED;
211 }
212 intake_roller_state = IntakeRollerStatus::INTAKING;
213 transfer_roller_status = TransferRollerStatus::TRANSFERING_IN;
214 extend_roller_status = ExtendRollerStatus::TRANSFERING_TO_EXTEND;
Austin Schuh027fd622024-03-01 21:26:07 -0800215 extend_goal_location = ExtendStatus::RETRACTED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800216
217 if (!catapult_requested_ && unsafe_goal != nullptr &&
218 unsafe_goal->note_goal() == NoteGoal::CATAPULT) {
219 catapult_requested_ = true;
220 }
221
Maxwell Hendersonf0d22622024-02-26 21:02:11 -0800222 // If we are no longer requesting INTAKE or we are no longer requesting
223 // an INTAKE goal, wait 0.5 seconds then go back to IDLE.
224 if (!(unsafe_goal != nullptr &&
225 unsafe_goal->intake_goal() == IntakeGoal::INTAKE) &&
226 timestamp > intake_end_time_ + kExtraIntakingTime) {
227 state_ = SuperstructureState::IDLE;
228 }
229
Filip Kujawa7a799602024-02-23 12:27:47 -0800230 break;
231 case SuperstructureState::LOADED:
Maxwell Henderson4567e282024-02-25 15:38:15 -0800232 if (!position->extend_beambreak() && !position->catapult_beambreak()) {
233 state_ = SuperstructureState::IDLE;
234 }
235
Niko Sohmers5006fc42024-03-01 17:14:22 -0800236 switch (requested_note_goal_) {
237 case NoteGoal::NONE:
238 break;
239 case NoteGoal::CATAPULT:
240 state_ = SuperstructureState::MOVING;
241 transfer_roller_status = TransferRollerStatus::TRANSFERING_IN;
242 break;
243 case NoteGoal::TRAP:
244 [[fallthrough]];
245 case NoteGoal::AMP:
James Kuszmaul6cae7d72024-03-01 21:29:56 -0800246 transfer_roller_status = TransferRollerStatus::EXTEND_MOVING;
Austin Schuh027fd622024-03-01 21:26:07 -0800247 state_ = SuperstructureState::MOVING;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800248 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800249 }
Austin Schuh027fd622024-03-01 21:26:07 -0800250 extend_goal_location = ExtendStatus::RETRACTED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800251 break;
252 case SuperstructureState::MOVING:
James Kuszmaul6cae7d72024-03-01 21:29:56 -0800253 transfer_roller_status = TransferRollerStatus::EXTEND_MOVING;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800254 switch (requested_note_goal_) {
255 case NoteGoal::NONE:
Austin Schuh027fd622024-03-01 21:26:07 -0800256 extend_goal_location = ExtendStatus::RETRACTED;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800257 if (extend_at_retracted) {
258 state_ = SuperstructureState::LOADED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800259 }
Niko Sohmers5006fc42024-03-01 17:14:22 -0800260 break;
261 case NoteGoal::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800262 extend_goal_location = ExtendStatus::CATAPULT;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800263 if (extend_ready_for_catapult_transfer && turret_ready_for_load &&
264 altitude_ready_for_load) {
265 state_ = SuperstructureState::LOADING_CATAPULT;
266 }
267 break;
268 case NoteGoal::TRAP:
Austin Schuh027fd622024-03-01 21:26:07 -0800269 extend_goal_location = ExtendStatus::TRAP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800270 // Check if the extend is at the TRAP position and if it is
271 // switch to READY state
272 if (PositionNear(extend_.position(), extend_set_points->trap(),
273 kExtendThreshold)) {
274 state_ = SuperstructureState::READY;
275 }
276 break;
277 case NoteGoal::AMP:
Austin Schuh027fd622024-03-01 21:26:07 -0800278 extend_goal_location = ExtendStatus::AMP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800279 // Check if the extend is at the AMP position and if it is
280 // switch to READY state
281 if (PositionNear(extend_.position(), extend_set_points->amp(),
282 kExtendThreshold)) {
283 state_ = SuperstructureState::READY;
284 }
285 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800286 }
287
288 extend_moving = true;
289 break;
290 case SuperstructureState::LOADING_CATAPULT:
291 extend_moving = false;
Austin Schuh027fd622024-03-01 21:26:07 -0800292 extend_goal_location = ExtendStatus::CATAPULT;
Filip Kujawa7a799602024-02-23 12:27:47 -0800293 extend_roller_status = ExtendRollerStatus::TRANSFERING_TO_CATAPULT;
294
295 // Switch to READY state when the catapult beambreak is triggered
296 if (position->catapult_beambreak()) {
297 state_ = SuperstructureState::READY;
298 }
299 break;
300 case SuperstructureState::READY:
301 extend_moving = false;
302
303 // Switch to FIRING state when the fire button is pressed
304 if (unsafe_goal != nullptr && unsafe_goal->fire()) {
305 state_ = SuperstructureState::FIRING;
306 }
307
Niko Sohmers5006fc42024-03-01 17:14:22 -0800308 switch (requested_note_goal_) {
309 case NoteGoal::NONE:
Austin Schuh027fd622024-03-01 21:26:07 -0800310 extend_goal_location = ExtendStatus::RETRACTED;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800311 extend_moving = true;
312 state_ = SuperstructureState::MOVING;
313 break;
314 case NoteGoal::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800315 extend_goal_location = ExtendStatus::CATAPULT;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800316 break;
317 case NoteGoal::TRAP:
Austin Schuh027fd622024-03-01 21:26:07 -0800318 extend_goal_location = ExtendStatus::TRAP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800319 break;
320 case NoteGoal::AMP:
Austin Schuh027fd622024-03-01 21:26:07 -0800321 extend_goal_location = ExtendStatus::AMP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800322 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800323 }
Filip Kujawa7a799602024-02-23 12:27:47 -0800324 break;
325 case SuperstructureState::FIRING:
Niko Sohmers5006fc42024-03-01 17:14:22 -0800326 switch (requested_note_goal_) {
327 case NoteGoal::NONE:
Filip Kujawa7a799602024-02-23 12:27:47 -0800328
Niko Sohmers5006fc42024-03-01 17:14:22 -0800329 break;
330 case NoteGoal::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800331 extend_goal_location = ExtendStatus::CATAPULT;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800332 // Reset the state to IDLE when the game piece is fired from the
333 // catapult. We consider the game piece to be fired from the catapult
334 // when the catapultbeambreak is no longer triggered.
335 if (!position->catapult_beambreak()) {
336 state_ = SuperstructureState::IDLE;
337 }
338 break;
339 case NoteGoal::TRAP:
Filip Kujawa7a799602024-02-23 12:27:47 -0800340 extend_roller_status = ExtendRollerStatus::SCORING_IN_TRAP;
Austin Schuh027fd622024-03-01 21:26:07 -0800341 extend_goal_location = ExtendStatus::TRAP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800342 if (!position->extend_beambreak() && unsafe_goal != nullptr &&
343 !unsafe_goal->fire()) {
344 state_ = SuperstructureState::IDLE;
345 }
346 break;
347 case NoteGoal::AMP:
348 extend_roller_status = ExtendRollerStatus::SCORING_IN_AMP;
Austin Schuh027fd622024-03-01 21:26:07 -0800349 extend_goal_location = ExtendStatus::AMP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800350 if (!position->extend_beambreak() && unsafe_goal != nullptr &&
351 !unsafe_goal->fire()) {
352 state_ = SuperstructureState::IDLE;
353 }
354 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800355 }
356 break;
357 }
358
359 if (unsafe_goal != nullptr &&
360 unsafe_goal->intake_goal() == IntakeGoal::SPIT) {
361 intake_roller_state = IntakeRollerStatus::SPITTING;
362 transfer_roller_status = TransferRollerStatus::TRANSFERING_OUT;
363 }
364
365 // Update Intake Roller voltage based on status from state machine.
366 switch (intake_roller_state) {
367 case IntakeRollerStatus::NONE:
368 output_struct.intake_roller_voltage = 0.0;
369 break;
370 case IntakeRollerStatus::SPITTING:
371 output_struct.intake_roller_voltage =
372 robot_constants_->common()->intake_roller_voltages()->spitting();
373 break;
374 case IntakeRollerStatus::INTAKING:
375 output_struct.intake_roller_voltage =
376 robot_constants_->common()->intake_roller_voltages()->intaking();
377 break;
378 }
379
380 // Update Transfer Roller voltage based on status from state machine.
381 switch (transfer_roller_status) {
382 case TransferRollerStatus::NONE:
383 output_struct.transfer_roller_voltage = 0.0;
384 break;
385 case TransferRollerStatus::TRANSFERING_IN:
386 output_struct.transfer_roller_voltage =
387 robot_constants_->common()->transfer_roller_voltages()->transfer_in();
388 break;
389 case TransferRollerStatus::TRANSFERING_OUT:
390 output_struct.transfer_roller_voltage = robot_constants_->common()
391 ->transfer_roller_voltages()
392 ->transfer_out();
393 break;
James Kuszmaul6cae7d72024-03-01 21:29:56 -0800394 case TransferRollerStatus::EXTEND_MOVING:
395 output_struct.transfer_roller_voltage = robot_constants_->common()
396 ->transfer_roller_voltages()
397 ->extend_moving();
398 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800399 }
400
401 // Update Extend Roller voltage based on status from state machine.
402 const ExtendRollerVoltages *extend_roller_voltages =
403 robot_constants_->common()->extend_roller_voltages();
404 switch (extend_roller_status) {
405 case ExtendRollerStatus::IDLE:
406 // No voltage applied when idle
407 output_struct.extend_roller_voltage = 0.0;
408 break;
409 case ExtendRollerStatus::TRANSFERING_TO_EXTEND:
410 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
411 break;
412 case ExtendRollerStatus::SCORING_IN_AMP:
413 [[fallthrough]];
414 case ExtendRollerStatus::SCORING_IN_TRAP:
415 // Apply scoring voltage during scoring in amp or trap
416 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
417 break;
418 case ExtendRollerStatus::TRANSFERING_TO_CATAPULT:
419 // Apply scoring voltage during transferring to catapult
420 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
421 break;
422 }
423
Austin Schuh027fd622024-03-01 21:26:07 -0800424 double extend_goal_position = 0.0;
Filip Kujawa7a799602024-02-23 12:27:47 -0800425
James Kuszmaul0281e152024-02-26 22:26:16 -0800426 if (unsafe_goal != nullptr && unsafe_goal->note_goal() == NoteGoal::TRAP) {
Austin Schuh027fd622024-03-01 21:26:07 -0800427 extend_goal_location = ExtendStatus::TRAP;
James Kuszmaul0281e152024-02-26 22:26:16 -0800428 }
429
Filip Kujawa7a799602024-02-23 12:27:47 -0800430 // Set the extend position based on the state machine output
Austin Schuh027fd622024-03-01 21:26:07 -0800431 switch (extend_goal_location) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800432 case ExtendStatus::RETRACTED:
Austin Schuh027fd622024-03-01 21:26:07 -0800433 extend_goal_position = extend_set_points->retracted();
Filip Kujawa7a799602024-02-23 12:27:47 -0800434 break;
435 case ExtendStatus::AMP:
Austin Schuh027fd622024-03-01 21:26:07 -0800436 extend_goal_position = extend_set_points->amp();
Filip Kujawa7a799602024-02-23 12:27:47 -0800437 break;
438 case ExtendStatus::TRAP:
Austin Schuh027fd622024-03-01 21:26:07 -0800439 extend_goal_position = extend_set_points->trap();
Filip Kujawa7a799602024-02-23 12:27:47 -0800440 break;
441 case ExtendStatus::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800442 extend_goal_position = extend_set_points->catapult();
Filip Kujawa7a799602024-02-23 12:27:47 -0800443 break;
444 case ExtendStatus::MOVING:
445 // Should never happen
446 break;
447 }
448
Niko Sohmers5006fc42024-03-01 17:14:22 -0800449 NoteStatus uncompleted_note_goal_status = NoteStatus::NONE;
450
451 switch (requested_note_goal_) {
452 case NoteGoal::NONE:
453 uncompleted_note_goal_status = NoteStatus::NONE;
454 break;
455 case NoteGoal::CATAPULT:
456 uncompleted_note_goal_status = NoteStatus::CATAPULT;
457 break;
458 case NoteGoal::AMP:
459 uncompleted_note_goal_status = NoteStatus::AMP;
460 break;
461 case NoteGoal::TRAP:
462 uncompleted_note_goal_status = NoteStatus::TRAP;
463 break;
464 }
465
Filip Kujawa7a799602024-02-23 12:27:47 -0800466 // Set the extend status based on the state machine output
467 // If the extend is moving, the status is MOVING, otherwise it is the same
468 // as extend_status
469 ExtendStatus extend_status =
Austin Schuh027fd622024-03-01 21:26:07 -0800470 (extend_moving ? ExtendStatus::MOVING : extend_goal_location);
Filip Kujawa7a799602024-02-23 12:27:47 -0800471
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800472 if (joystick_state_fetcher_.Fetch() &&
473 joystick_state_fetcher_->has_alliance()) {
474 alliance_ = joystick_state_fetcher_->alliance();
475 }
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800476
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800477 drivetrain_status_fetcher_.Fetch();
478
Austin Schuh027fd622024-03-01 21:26:07 -0800479 const bool collided = collision_avoidance_.IsCollided({
480 .intake_pivot_position = intake_pivot_.estimated_position(),
481 .turret_position = shooter_.turret().estimated_position(),
482 .extend_position = extend_.estimated_position(),
483 });
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800484
Filip Kujawa6d717632024-02-01 11:40:55 -0800485 aos::FlatbufferFixedAllocatorArray<
486 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
487 climber_goal_buffer;
488
489 climber_goal_buffer.Finish(
490 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
491 *climber_goal_buffer.fbb(), climber_position));
492
493 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
494 *climber_goal = &climber_goal_buffer.message();
495
496 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
497 climber_status_offset = climber_.Iterate(
498 climber_goal, position->climber(),
499 output != nullptr ? &output_struct.climber_voltage : nullptr,
500 status->fbb());
501
Niko Sohmersac4d8872024-02-23 13:55:47 -0800502 double max_intake_pivot_position = 0;
503 double min_intake_pivot_position = 0;
Austin Schuh027fd622024-03-01 21:26:07 -0800504 double max_extend_position = 0;
505 double min_extend_position = 0;
Niko Sohmersac4d8872024-02-23 13:55:47 -0800506
507 aos::FlatbufferFixedAllocatorArray<
508 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
509 intake_pivot_goal_buffer;
510
511 intake_pivot_goal_buffer.Finish(
512 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
513 *intake_pivot_goal_buffer.fbb(), intake_pivot_position));
514
515 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
516 *intake_pivot_goal = &intake_pivot_goal_buffer.message();
517
518 double *intake_output =
519 (output != nullptr ? &output_struct.intake_pivot_voltage : nullptr);
520
521 const bool disabled = intake_pivot_.Correct(
522 intake_pivot_goal, position->intake_pivot(), intake_output == nullptr);
523
Austin Schuh027fd622024-03-01 21:26:07 -0800524 aos::FlatbufferFixedAllocatorArray<
525 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
526 extend_goal_buffer;
527
528 extend_goal_buffer.Finish(
529 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
530 *extend_goal_buffer.fbb(), extend_goal_position));
531
532 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
533 *extend_goal = &extend_goal_buffer.message();
534
Filip Kujawa7a799602024-02-23 12:27:47 -0800535 // TODO(max): Change how we handle the collision with the turret and
536 // intake to be clearer
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800537 const flatbuffers::Offset<ShooterStatus> shooter_status_offset =
538 shooter_.Iterate(
539 position,
540 unsafe_goal != nullptr ? unsafe_goal->shooter_goal() : nullptr,
Filip Kujawa7a799602024-02-23 12:27:47 -0800541 unsafe_goal != nullptr ? unsafe_goal->fire() : false,
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800542 output != nullptr ? &output_struct.catapult_voltage : nullptr,
543 output != nullptr ? &output_struct.altitude_voltage : nullptr,
544 output != nullptr ? &output_struct.turret_voltage : nullptr,
545 output != nullptr ? &output_struct.retention_roller_voltage : nullptr,
Maxwell Hendersond5bf47a2024-02-23 17:16:48 -0800546 output != nullptr
547 ? &output_struct.retention_roller_stator_current_limit
548 : nullptr,
Maxwell Henderson461a8a42024-02-23 17:07:39 -0800549 robot_state().voltage_battery(), &collision_avoidance_,
Austin Schuh027fd622024-03-01 21:26:07 -0800550 extend_goal_position, extend_.estimated_position(),
551 &max_extend_position, &min_extend_position,
Niko Sohmersac4d8872024-02-23 13:55:47 -0800552 intake_pivot_.estimated_position(), &max_intake_pivot_position,
Austin Schuh027fd622024-03-01 21:26:07 -0800553 &min_intake_pivot_position, status->fbb(), timestamp);
Niko Sohmersac4d8872024-02-23 13:55:47 -0800554
555 intake_pivot_.set_min_position(min_intake_pivot_position);
556 intake_pivot_.set_max_position(max_intake_pivot_position);
557
Austin Schuh027fd622024-03-01 21:26:07 -0800558 extend_.set_min_position(min_extend_position);
559 extend_.set_max_position(max_extend_position);
560
Niko Sohmersac4d8872024-02-23 13:55:47 -0800561 // Calculate the loops for a cycle.
562 const double voltage = intake_pivot_.UpdateController(disabled);
563
564 intake_pivot_.UpdateObserver(voltage);
565
566 // Write out all the voltages.
567 if (intake_output) {
568 *intake_output = voltage;
569 }
570
571 const flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus>
572 intake_pivot_status_offset = intake_pivot_.MakeStatus(status->fbb());
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800573
Filip Kujawa7a799602024-02-23 12:27:47 -0800574 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
575 extend_status_offset = extend_.Iterate(
Austin Schuh027fd622024-03-01 21:26:07 -0800576 extend_goal, position->extend(),
Filip Kujawa7a799602024-02-23 12:27:47 -0800577 output != nullptr ? &output_struct.extend_voltage : nullptr,
578 status->fbb());
579
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800580 if (output) {
581 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
582 }
583
584 Status::Builder status_builder = status->MakeBuilder<Status>();
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800585
Filip Kujawa7a799602024-02-23 12:27:47 -0800586 const bool zeroed = intake_pivot_.zeroed() && climber_.zeroed() &&
587 shooter_.zeroed() && extend_.zeroed();
588 const bool estopped = intake_pivot_.estopped() || climber_.estopped() ||
589 shooter_.estopped() || extend_.estopped();
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800590
591 status_builder.add_zeroed(zeroed);
592 status_builder.add_estopped(estopped);
Filip Kujawa102a9b22024-02-18 09:40:23 -0800593 status_builder.add_intake_roller(intake_roller_state);
594 status_builder.add_intake_pivot(intake_pivot_status_offset);
Filip Kujawa7a799602024-02-23 12:27:47 -0800595 status_builder.add_transfer_roller(transfer_roller_status);
Filip Kujawa102a9b22024-02-18 09:40:23 -0800596 status_builder.add_climber(climber_status_offset);
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800597 status_builder.add_shooter(shooter_status_offset);
Niko Sohmersac4d8872024-02-23 13:55:47 -0800598 status_builder.add_collided(collided);
Filip Kujawa7a799602024-02-23 12:27:47 -0800599 status_builder.add_extend_roller(extend_roller_status);
600 status_builder.add_extend_status(extend_status);
601 status_builder.add_extend(extend_status_offset);
602 status_builder.add_state(state_);
Niko Sohmers5006fc42024-03-01 17:14:22 -0800603 status_builder.add_uncompleted_note_goal(uncompleted_note_goal_status);
James Kuszmaul0281e152024-02-26 22:26:16 -0800604 status_builder.add_extend_ready_for_transfer(extend_at_retracted);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800605
606 (void)status->Send(status_builder.Finish());
607}
608
609double Superstructure::robot_velocity() const {
610 return (drivetrain_status_fetcher_.get() != nullptr
611 ? drivetrain_status_fetcher_->robot_speed()
612 : 0.0);
613}
614
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800615} // namespace y2024::control_loops::superstructure