blob: a33fc8767a1be1843af4c320b1ffbe7ce44af46e [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
Austin Schuh99f7c6a2024-06-25 22:07:44 -070012ABSL_FLAG(bool, ignore_distance, false,
13 "If true, ignore distance when shooting and obey joystick_reader");
Filip Kujawa7a799602024-02-23 12:27:47 -080014
15// The threshold used when decided if the extend is close enough to a goal to
16// continue.
17constexpr double kExtendThreshold = 0.01;
18
Niko Sohmers6adb5b92024-03-16 17:47:54 -070019constexpr double kTurretLoadingThreshold = 0.05;
Austin Schuh79903e82024-04-07 23:12:00 -070020// Extra large so that we can survive loss of zeroes.
21constexpr double kAltitudeLoadingThreshold = 0.04;
Niko Sohmers3860f8a2024-01-12 21:05:19 -080022
Maxwell Hendersonf0d22622024-02-26 21:02:11 -080023constexpr std::chrono::milliseconds kExtraIntakingTime =
24 std::chrono::milliseconds(500);
25
Stephan Pleinesf63bde82024-01-13 15:59:33 -080026namespace y2024::control_loops::superstructure {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080027
28using ::aos::monotonic_clock;
29
30using frc971::control_loops::AbsoluteEncoderProfiledJointStatus;
31using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus;
32using frc971::control_loops::RelativeEncoderProfiledJointStatus;
33
34Superstructure::Superstructure(::aos::EventLoop *event_loop,
Niko Sohmers3860f8a2024-01-12 21:05:19 -080035 const ::std::string &name)
36 : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop,
37 name),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080038 constants_fetcher_(event_loop),
Austin Schuh6bdcc372024-06-27 14:49:11 -070039 robot_constants_(&constants_fetcher_.constants()),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080040 drivetrain_status_fetcher_(
41 event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>(
42 "/drivetrain")),
43 joystick_state_fetcher_(
Niko Sohmersafc51fe2024-01-29 17:48:35 -080044 event_loop->MakeFetcher<aos::JoystickState>("/aos")),
Niko Sohmers74b0ad52024-02-03 18:00:31 -080045 intake_pivot_(robot_constants_->common()->intake_pivot(),
46 robot_constants_->robot()->intake_constants()),
Filip Kujawa7a799602024-02-23 12:27:47 -080047 shooter_(event_loop, robot_constants_),
48 extend_(
49 robot_constants_->common()->extend(),
Niko Sohmers6adb5b92024-03-16 17:47:54 -070050 robot_constants_->robot()->extend_constants()->zeroing_constants()),
James Kuszmaul9effe0f2024-03-23 15:58:54 -070051 extend_debouncer_(std::chrono::milliseconds(12),
Filip Kujawa0a1282b2024-03-23 12:36:51 -070052 std::chrono::milliseconds(8)),
53 transfer_debouncer_(std::chrono::milliseconds(30),
54 std::chrono::milliseconds(8)) {
55 event_loop->SetRuntimeRealtimePriority(30);
Niko Sohmers3860f8a2024-01-12 21:05:19 -080056}
57
Filip Kujawa7a799602024-02-23 12:27:47 -080058bool PositionNear(double position, double goal, double threshold) {
59 return std::abs(position - goal) < threshold;
60}
61
Niko Sohmers3860f8a2024-01-12 21:05:19 -080062void Superstructure::RunIteration(const Goal *unsafe_goal,
63 const Position *position,
64 aos::Sender<Output>::Builder *output,
65 aos::Sender<Status>::Builder *status) {
66 const monotonic_clock::time_point timestamp =
67 event_loop()->context().monotonic_event_time;
68
Niko Sohmers3860f8a2024-01-12 21:05:19 -080069 if (WasReset()) {
70 AOS_LOG(ERROR, "WPILib reset, restarting\n");
Niko Sohmersafc51fe2024-01-29 17:48:35 -080071 intake_pivot_.Reset();
Niko Sohmersc4d2c502024-02-19 19:35:35 -080072 shooter_.Reset();
Filip Kujawa7a799602024-02-23 12:27:47 -080073 extend_.Reset();
Niko Sohmers3860f8a2024-01-12 21:05:19 -080074 }
75
76 OutputT output_struct;
Niko Sohmersafc51fe2024-01-29 17:48:35 -080077
Niko Sohmers6adb5b92024-03-16 17:47:54 -070078 extend_debouncer_.Update(position->extend_beambreak(), timestamp);
79 const bool extend_beambreak = extend_debouncer_.state();
80
Filip Kujawa0a1282b2024-03-23 12:36:51 -070081 transfer_debouncer_.Update(position->transfer_beambreak(), timestamp);
82 const bool transfer_beambreak = transfer_debouncer_.state();
83
Filip Kujawa7a799602024-02-23 12:27:47 -080084 // If we started off preloaded, skip to the ready state.
85 if (unsafe_goal != nullptr && unsafe_goal->shooter_goal() &&
86 unsafe_goal->shooter_goal()->preloaded()) {
87 if (state_ != SuperstructureState::READY &&
88 state_ != SuperstructureState::FIRING) {
89 state_ = SuperstructureState::READY;
Niko Sohmers5006fc42024-03-01 17:14:22 -080090 requested_note_goal_ = NoteGoal::CATAPULT;
Filip Kujawa7a799602024-02-23 12:27:47 -080091 }
92 }
93
94 // Handle the intake pivot goal separately from the main superstructure state
95 IntakeRollerStatus intake_roller_state = IntakeRollerStatus::NONE;
96 double intake_pivot_position =
97 robot_constants_->common()->intake_pivot_set_points()->retracted();
98
99 if (unsafe_goal != nullptr) {
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700100 switch (unsafe_goal->intake_pivot()) {
101 case IntakePivotGoal::DOWN:
Filip Kujawa7a799602024-02-23 12:27:47 -0800102 intake_pivot_position =
103 robot_constants_->common()->intake_pivot_set_points()->extended();
Maxwell Hendersonf0d22622024-02-26 21:02:11 -0800104 intake_end_time_ = timestamp;
Filip Kujawa7a799602024-02-23 12:27:47 -0800105 break;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700106 case IntakePivotGoal::UP:
Filip Kujawa7a799602024-02-23 12:27:47 -0800107 intake_pivot_position =
108 robot_constants_->common()->intake_pivot_set_points()->retracted();
109 break;
110 }
111 }
112
113 ExtendRollerStatus extend_roller_status = ExtendRollerStatus::IDLE;
Austin Schuh027fd622024-03-01 21:26:07 -0800114 ExtendStatus extend_goal_location = ExtendStatus::RETRACTED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800115
116 // True if the extend is moving towards a goal
117 bool extend_moving = false;
118
119 TransferRollerStatus transfer_roller_status = TransferRollerStatus::NONE;
120
121 const ExtendSetPoints *extend_set_points =
122 robot_constants_->common()->extend_set_points();
123
124 // Checks if the extend is close enough to the retracted position to be
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700125 // considered ready to accept note from the transfer rollers. If disable
126 // extend is triggered, this will autoatically be false.
127 const bool extend_at_retracted =
Niko Sohmers58461f52024-03-20 20:12:10 -0700128 (!robot_constants_->robot()->disable_extend() &&
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700129 PositionNear(extend_.position(), extend_set_points->retracted(),
130 kExtendThreshold));
Filip Kujawa7a799602024-02-23 12:27:47 -0800131
Niko Sohmers5006fc42024-03-01 17:14:22 -0800132 // Check if the turret is at the position to accept the note from extend
133 const bool turret_ready_for_load =
134 PositionNear(shooter_.turret().estimated_position(),
135 robot_constants_->common()->turret_loading_position(),
136 kTurretLoadingThreshold);
137
Niko Sohmers5006fc42024-03-01 17:14:22 -0800138 // Check if the altitude is at the position to accept the note from
139 // extend
140 const bool altitude_ready_for_load =
141 PositionNear(shooter_.altitude().estimated_position(),
142 robot_constants_->common()->altitude_loading_position(),
143 kAltitudeLoadingThreshold);
144
145 // Check if the extend is at the position to load the catapult
146 const bool extend_ready_for_catapult_transfer = PositionNear(
147 extend_.position(), extend_set_points->catapult(), kExtendThreshold);
148
Niko Sohmers5006fc42024-03-01 17:14:22 -0800149 // Only update the reuested note goal to the first goal that is requested by
150 // the manipulator
151 if (unsafe_goal != nullptr && unsafe_goal->note_goal() != NoteGoal::NONE &&
152 requested_note_goal_ == NoteGoal::NONE) {
153 requested_note_goal_ = unsafe_goal->note_goal();
154 }
155
Filip Kujawa7a799602024-02-23 12:27:47 -0800156 // Superstructure state machine:
157 // 1. IDLE. The intake is retracted and there is no note in the robot.
158 // Wait for a intake goal to switch state to INTAKING if the extend is ready
159 // 2. INTAKING. Intake the note and transfer it towards the extend.
160 // Give intake, transfer, and extend rollers positive voltage to intake and
161 // transfer. Switch to LOADED when the extend beambreak is triggered.
162 // 3. LOADED. The note is in the extend and the extend is retracted.
163 // Wait for a note goal to switch state to MOVING.
164 // For AMP/TRAP goals, check that the turret is in a position to avoid
165 // collision.
166 // 4. MOVING. The extend is moving towards a goal (AMP, TRAP, or CATAPULT).
167 // For CATAPULT goals, wait for the turret and altitude to be in a position to
168 // accept the note from the extend.
169 // Wait for the extend to reach the goal and switch state to READY if
170 // AMP or TRAP, or to LOADING_CATAPULT if CATAPULT.
171 // 5. LOADING_CATAPULT. The extend is at the position to load the catapult.
172 // Activate the extend roller to transfer the note to the catapult.
173 // Switch state to READY when the catapult beambreak is triggered.
174 // 6. READY. Ready for fire command. The note is either loaded in the catapult
175 // or in the extend and at the AMP or TRAP position. Wait for a fire command.
176 // 7. FIRING. The note is being fired, either from the extend or the catapult.
177 // Switch state back to IDLE when the note is fired.
178
179 switch (state_) {
180 case SuperstructureState::IDLE:
Niko Sohmers5006fc42024-03-01 17:14:22 -0800181 requested_note_goal_ = NoteGoal::NONE;
182
Filip Kujawa7a799602024-02-23 12:27:47 -0800183 if (unsafe_goal != nullptr &&
184 unsafe_goal->intake_goal() == IntakeGoal::INTAKE &&
Niko Sohmersdd971dc2024-02-25 11:40:47 -0800185 extend_at_retracted) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800186 state_ = SuperstructureState::INTAKING;
Filip Kujawa0a1282b2024-03-23 12:36:51 -0700187 note_in_transfer_ = false;
Filip Kujawa7a799602024-02-23 12:27:47 -0800188 }
Maxwell Henderson4567e282024-02-25 15:38:15 -0800189
Austin Schuh027fd622024-03-01 21:26:07 -0800190 extend_goal_location = ExtendStatus::RETRACTED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800191 break;
192 case SuperstructureState::INTAKING:
Filip Kujawa7a799602024-02-23 12:27:47 -0800193 // Switch to LOADED state when the extend beambreak is triggered
194 // meaning the note is loaded in the extend
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700195 if (extend_beambreak) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800196 state_ = SuperstructureState::LOADED;
197 }
Filip Kujawa0a1282b2024-03-23 12:36:51 -0700198
199 if (transfer_beambreak) {
200 note_in_transfer_ = true;
201 }
202
203 // Once the note is in the transfer, stop the intake rollers
204 if (note_in_transfer_) {
205 intake_roller_state = IntakeRollerStatus::NONE;
206 } else {
207 intake_roller_state = IntakeRollerStatus::INTAKING;
208 }
209
Filip Kujawa7a799602024-02-23 12:27:47 -0800210 transfer_roller_status = TransferRollerStatus::TRANSFERING_IN;
211 extend_roller_status = ExtendRollerStatus::TRANSFERING_TO_EXTEND;
Austin Schuh027fd622024-03-01 21:26:07 -0800212 extend_goal_location = ExtendStatus::RETRACTED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800213
Maxwell Hendersonf0d22622024-02-26 21:02:11 -0800214 // If we are no longer requesting INTAKE or we are no longer requesting
215 // an INTAKE goal, wait 0.5 seconds then go back to IDLE.
216 if (!(unsafe_goal != nullptr &&
217 unsafe_goal->intake_goal() == IntakeGoal::INTAKE) &&
218 timestamp > intake_end_time_ + kExtraIntakingTime) {
219 state_ = SuperstructureState::IDLE;
220 }
221
Filip Kujawa7a799602024-02-23 12:27:47 -0800222 break;
223 case SuperstructureState::LOADED:
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700224 if (!extend_beambreak && !position->catapult_beambreak()) {
Maxwell Henderson4567e282024-02-25 15:38:15 -0800225 state_ = SuperstructureState::IDLE;
226 }
227
Niko Sohmers5006fc42024-03-01 17:14:22 -0800228 switch (requested_note_goal_) {
229 case NoteGoal::NONE:
230 break;
231 case NoteGoal::CATAPULT:
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700232 state_ = SuperstructureState::LOADING_CATAPULT;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800233 transfer_roller_status = TransferRollerStatus::TRANSFERING_IN;
234 break;
235 case NoteGoal::TRAP:
236 [[fallthrough]];
237 case NoteGoal::AMP:
James Kuszmaul6cae7d72024-03-01 21:29:56 -0800238 transfer_roller_status = TransferRollerStatus::EXTEND_MOVING;
Austin Schuh027fd622024-03-01 21:26:07 -0800239 state_ = SuperstructureState::MOVING;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800240 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800241 }
Austin Schuh027fd622024-03-01 21:26:07 -0800242 extend_goal_location = ExtendStatus::RETRACTED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800243 break;
244 case SuperstructureState::MOVING:
James Kuszmaul6cae7d72024-03-01 21:29:56 -0800245 transfer_roller_status = TransferRollerStatus::EXTEND_MOVING;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800246 switch (requested_note_goal_) {
247 case NoteGoal::NONE:
Austin Schuh027fd622024-03-01 21:26:07 -0800248 extend_goal_location = ExtendStatus::RETRACTED;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800249 if (extend_at_retracted) {
250 state_ = SuperstructureState::LOADED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800251 }
Niko Sohmers5006fc42024-03-01 17:14:22 -0800252 break;
253 case NoteGoal::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800254 extend_goal_location = ExtendStatus::CATAPULT;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800255 if (extend_ready_for_catapult_transfer && turret_ready_for_load &&
256 altitude_ready_for_load) {
257 state_ = SuperstructureState::LOADING_CATAPULT;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700258 loading_catapult_start_time_ = timestamp;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800259 }
260 break;
261 case NoteGoal::TRAP:
Austin Schuh027fd622024-03-01 21:26:07 -0800262 extend_goal_location = ExtendStatus::TRAP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800263 // Check if the extend is at the TRAP position and if it is
264 // switch to READY state
265 if (PositionNear(extend_.position(), extend_set_points->trap(),
266 kExtendThreshold)) {
267 state_ = SuperstructureState::READY;
268 }
269 break;
270 case NoteGoal::AMP:
Austin Schuh027fd622024-03-01 21:26:07 -0800271 extend_goal_location = ExtendStatus::AMP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800272 // Check if the extend is at the AMP position and if it is
273 // switch to READY state
274 if (PositionNear(extend_.position(), extend_set_points->amp(),
275 kExtendThreshold)) {
276 state_ = SuperstructureState::READY;
277 }
278 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800279 }
280
281 extend_moving = true;
282 break;
283 case SuperstructureState::LOADING_CATAPULT:
284 extend_moving = false;
Austin Schuh027fd622024-03-01 21:26:07 -0800285 extend_goal_location = ExtendStatus::CATAPULT;
Filip Kujawa7a799602024-02-23 12:27:47 -0800286
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700287 if (extend_beambreak) {
288 loading_catapult_start_time_ = timestamp;
289 }
290
291 if (loading_catapult_start_time_ + std::chrono::seconds(10) < timestamp) {
Filip Kujawa77e3d8a2024-03-02 11:34:56 -0800292 state_ = SuperstructureState::IDLE;
293 }
294
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700295 if (turret_ready_for_load && altitude_ready_for_load) {
296 extend_roller_status = ExtendRollerStatus::TRANSFERING_TO_CATAPULT;
297 transfer_roller_status = TransferRollerStatus::EXTEND_MOVING;
298 }
299
Filip Kujawa7a799602024-02-23 12:27:47 -0800300 // Switch to READY state when the catapult beambreak is triggered
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700301 if (shooter_.loaded()) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800302 state_ = SuperstructureState::READY;
303 }
304 break;
305 case SuperstructureState::READY:
306 extend_moving = false;
307
308 // Switch to FIRING state when the fire button is pressed
309 if (unsafe_goal != nullptr && unsafe_goal->fire()) {
310 state_ = SuperstructureState::FIRING;
311 }
312
Niko Sohmers5006fc42024-03-01 17:14:22 -0800313 switch (requested_note_goal_) {
314 case NoteGoal::NONE:
Austin Schuh027fd622024-03-01 21:26:07 -0800315 extend_goal_location = ExtendStatus::RETRACTED;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800316 extend_moving = true;
317 state_ = SuperstructureState::MOVING;
318 break;
319 case NoteGoal::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800320 extend_goal_location = ExtendStatus::CATAPULT;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700321
322 if (!shooter_.loaded()) {
323 state_ = SuperstructureState::LOADING_CATAPULT;
324 }
Niko Sohmers5006fc42024-03-01 17:14:22 -0800325 break;
326 case NoteGoal::TRAP:
Austin Schuh027fd622024-03-01 21:26:07 -0800327 extend_goal_location = ExtendStatus::TRAP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800328 break;
329 case NoteGoal::AMP:
Austin Schuh027fd622024-03-01 21:26:07 -0800330 extend_goal_location = ExtendStatus::AMP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800331 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800332 }
Filip Kujawa7a799602024-02-23 12:27:47 -0800333 break;
334 case SuperstructureState::FIRING:
Niko Sohmers5006fc42024-03-01 17:14:22 -0800335 switch (requested_note_goal_) {
336 case NoteGoal::NONE:
Filip Kujawa7a799602024-02-23 12:27:47 -0800337
Niko Sohmers5006fc42024-03-01 17:14:22 -0800338 break;
339 case NoteGoal::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800340 extend_goal_location = ExtendStatus::CATAPULT;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800341 // Reset the state to IDLE when the game piece is fired from the
342 // catapult. We consider the game piece to be fired from the catapult
343 // when the catapultbeambreak is no longer triggered.
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700344 if (!shooter_.loaded() && !shooter_.Firing()) {
Niko Sohmers5006fc42024-03-01 17:14:22 -0800345 state_ = SuperstructureState::IDLE;
346 }
347 break;
348 case NoteGoal::TRAP:
Filip Kujawa7a799602024-02-23 12:27:47 -0800349 extend_roller_status = ExtendRollerStatus::SCORING_IN_TRAP;
Austin Schuh027fd622024-03-01 21:26:07 -0800350 extend_goal_location = ExtendStatus::TRAP;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700351 if (!extend_beambreak && unsafe_goal != nullptr &&
Niko Sohmers5006fc42024-03-01 17:14:22 -0800352 !unsafe_goal->fire()) {
353 state_ = SuperstructureState::IDLE;
354 }
355 break;
356 case NoteGoal::AMP:
357 extend_roller_status = ExtendRollerStatus::SCORING_IN_AMP;
Austin Schuh027fd622024-03-01 21:26:07 -0800358 extend_goal_location = ExtendStatus::AMP;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700359 if (!extend_beambreak && unsafe_goal != nullptr &&
Niko Sohmers5006fc42024-03-01 17:14:22 -0800360 !unsafe_goal->fire()) {
361 state_ = SuperstructureState::IDLE;
362 }
363 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800364 }
365 break;
366 }
367
368 if (unsafe_goal != nullptr &&
369 unsafe_goal->intake_goal() == IntakeGoal::SPIT) {
370 intake_roller_state = IntakeRollerStatus::SPITTING;
371 transfer_roller_status = TransferRollerStatus::TRANSFERING_OUT;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700372 extend_roller_status = ExtendRollerStatus::SCORING_IN_AMP;
Filip Kujawa7a799602024-02-23 12:27:47 -0800373 }
374
375 // Update Intake Roller voltage based on status from state machine.
376 switch (intake_roller_state) {
377 case IntakeRollerStatus::NONE:
378 output_struct.intake_roller_voltage = 0.0;
379 break;
380 case IntakeRollerStatus::SPITTING:
381 output_struct.intake_roller_voltage =
382 robot_constants_->common()->intake_roller_voltages()->spitting();
383 break;
384 case IntakeRollerStatus::INTAKING:
385 output_struct.intake_roller_voltage =
386 robot_constants_->common()->intake_roller_voltages()->intaking();
387 break;
388 }
389
390 // Update Transfer Roller voltage based on status from state machine.
391 switch (transfer_roller_status) {
392 case TransferRollerStatus::NONE:
393 output_struct.transfer_roller_voltage = 0.0;
394 break;
395 case TransferRollerStatus::TRANSFERING_IN:
396 output_struct.transfer_roller_voltage =
397 robot_constants_->common()->transfer_roller_voltages()->transfer_in();
398 break;
399 case TransferRollerStatus::TRANSFERING_OUT:
400 output_struct.transfer_roller_voltage = robot_constants_->common()
401 ->transfer_roller_voltages()
402 ->transfer_out();
403 break;
James Kuszmaul6cae7d72024-03-01 21:29:56 -0800404 case TransferRollerStatus::EXTEND_MOVING:
405 output_struct.transfer_roller_voltage = robot_constants_->common()
406 ->transfer_roller_voltages()
407 ->extend_moving();
408 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800409 }
410
411 // Update Extend Roller voltage based on status from state machine.
412 const ExtendRollerVoltages *extend_roller_voltages =
413 robot_constants_->common()->extend_roller_voltages();
414 switch (extend_roller_status) {
415 case ExtendRollerStatus::IDLE:
416 // No voltage applied when idle
417 output_struct.extend_roller_voltage = 0.0;
418 break;
419 case ExtendRollerStatus::TRANSFERING_TO_EXTEND:
420 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
421 break;
422 case ExtendRollerStatus::SCORING_IN_AMP:
423 [[fallthrough]];
424 case ExtendRollerStatus::SCORING_IN_TRAP:
425 // Apply scoring voltage during scoring in amp or trap
426 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
427 break;
428 case ExtendRollerStatus::TRANSFERING_TO_CATAPULT:
429 // Apply scoring voltage during transferring to catapult
430 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
431 break;
432 }
433
Maxwell Henderson45878502024-03-15 19:35:25 -0700434 if (unsafe_goal != nullptr && unsafe_goal->spit_extend()) {
435 output_struct.extend_roller_voltage = -extend_roller_voltages->scoring();
436 }
437
Austin Schuh027fd622024-03-01 21:26:07 -0800438 double extend_goal_position = 0.0;
Filip Kujawa7a799602024-02-23 12:27:47 -0800439
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700440 // If we request trap, override the extend goal to be trap unless we request
441 // amp.
James Kuszmaul0281e152024-02-26 22:26:16 -0800442 if (unsafe_goal != nullptr && unsafe_goal->note_goal() == NoteGoal::TRAP) {
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700443 trap_override_ = true;
444 }
445
446 if (unsafe_goal != nullptr && unsafe_goal->note_goal() == NoteGoal::AMP &&
447 trap_override_) {
448 trap_override_ = false;
449 requested_note_goal_ = NoteGoal::AMP;
450 state_ = SuperstructureState::READY;
451 }
452
453 if (trap_override_) {
Austin Schuh027fd622024-03-01 21:26:07 -0800454 extend_goal_location = ExtendStatus::TRAP;
James Kuszmaul0281e152024-02-26 22:26:16 -0800455 }
456
Filip Kujawa7a799602024-02-23 12:27:47 -0800457 // Set the extend position based on the state machine output
Austin Schuh027fd622024-03-01 21:26:07 -0800458 switch (extend_goal_location) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800459 case ExtendStatus::RETRACTED:
Austin Schuh027fd622024-03-01 21:26:07 -0800460 extend_goal_position = extend_set_points->retracted();
Filip Kujawa7a799602024-02-23 12:27:47 -0800461 break;
462 case ExtendStatus::AMP:
Austin Schuh027fd622024-03-01 21:26:07 -0800463 extend_goal_position = extend_set_points->amp();
Filip Kujawa7a799602024-02-23 12:27:47 -0800464 break;
465 case ExtendStatus::TRAP:
Austin Schuh027fd622024-03-01 21:26:07 -0800466 extend_goal_position = extend_set_points->trap();
Filip Kujawa7a799602024-02-23 12:27:47 -0800467 break;
468 case ExtendStatus::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800469 extend_goal_position = extend_set_points->catapult();
Filip Kujawa7a799602024-02-23 12:27:47 -0800470 break;
471 case ExtendStatus::MOVING:
472 // Should never happen
473 break;
474 }
475
Niko Sohmers5006fc42024-03-01 17:14:22 -0800476 NoteStatus uncompleted_note_goal_status = NoteStatus::NONE;
477
478 switch (requested_note_goal_) {
479 case NoteGoal::NONE:
480 uncompleted_note_goal_status = NoteStatus::NONE;
481 break;
482 case NoteGoal::CATAPULT:
483 uncompleted_note_goal_status = NoteStatus::CATAPULT;
484 break;
485 case NoteGoal::AMP:
486 uncompleted_note_goal_status = NoteStatus::AMP;
487 break;
488 case NoteGoal::TRAP:
489 uncompleted_note_goal_status = NoteStatus::TRAP;
490 break;
491 }
492
Filip Kujawa7a799602024-02-23 12:27:47 -0800493 // Set the extend status based on the state machine output
494 // If the extend is moving, the status is MOVING, otherwise it is the same
495 // as extend_status
496 ExtendStatus extend_status =
Austin Schuh027fd622024-03-01 21:26:07 -0800497 (extend_moving ? ExtendStatus::MOVING : extend_goal_location);
Filip Kujawa7a799602024-02-23 12:27:47 -0800498
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800499 if (joystick_state_fetcher_.Fetch() &&
500 joystick_state_fetcher_->has_alliance()) {
501 alliance_ = joystick_state_fetcher_->alliance();
502 }
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800503
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800504 drivetrain_status_fetcher_.Fetch();
505
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700506 // Zero out extend position if "disable_extend" is true
Austin Schuh027fd622024-03-01 21:26:07 -0800507 const bool collided = collision_avoidance_.IsCollided({
508 .intake_pivot_position = intake_pivot_.estimated_position(),
509 .turret_position = shooter_.turret().estimated_position(),
Niko Sohmers58461f52024-03-20 20:12:10 -0700510 .extend_position = ((!robot_constants_->robot()->disable_extend())
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700511 ? extend_.estimated_position()
512 : 0.0),
Austin Schuh027fd622024-03-01 21:26:07 -0800513 });
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800514
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
James Kuszmaul42a87452024-04-05 17:30:47 -0700548 // Ignore climber voltage goal if "disable_climber" is true
549 output_struct.climber_voltage =
550 (!robot_constants_->robot()->disable_climber() && unsafe_goal != nullptr)
551 ? unsafe_goal->climber_goal_voltage()
552 : 0.0;
553
554 if (output) {
555 if (output_struct.climber_voltage != 0.0) {
556 ++climbing_;
557 }
558 }
559
Filip Kujawa7a799602024-02-23 12:27:47 -0800560 // TODO(max): Change how we handle the collision with the turret and
561 // intake to be clearer
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800562 const flatbuffers::Offset<ShooterStatus> shooter_status_offset =
563 shooter_.Iterate(
564 position,
565 unsafe_goal != nullptr ? unsafe_goal->shooter_goal() : nullptr,
Filip Kujawa7a799602024-02-23 12:27:47 -0800566 unsafe_goal != nullptr ? unsafe_goal->fire() : false,
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800567 output != nullptr ? &output_struct.catapult_voltage : nullptr,
568 output != nullptr ? &output_struct.altitude_voltage : nullptr,
569 output != nullptr ? &output_struct.turret_voltage : nullptr,
570 output != nullptr ? &output_struct.retention_roller_voltage : nullptr,
Maxwell Hendersond5bf47a2024-02-23 17:16:48 -0800571 output != nullptr
572 ? &output_struct.retention_roller_stator_current_limit
573 : nullptr,
Maxwell Henderson461a8a42024-02-23 17:07:39 -0800574 robot_state().voltage_battery(), &collision_avoidance_,
Austin Schuh027fd622024-03-01 21:26:07 -0800575 extend_goal_position, extend_.estimated_position(),
576 &max_extend_position, &min_extend_position,
Niko Sohmersac4d8872024-02-23 13:55:47 -0800577 intake_pivot_.estimated_position(), &max_intake_pivot_position,
Stephan Pleines9f3983a2024-03-13 20:22:38 -0700578 &min_intake_pivot_position, requested_note_goal_, status->fbb(),
James Kuszmaul42a87452024-04-05 17:30:47 -0700579 timestamp, climbing_ > 50);
Niko Sohmersac4d8872024-02-23 13:55:47 -0800580
581 intake_pivot_.set_min_position(min_intake_pivot_position);
582 intake_pivot_.set_max_position(max_intake_pivot_position);
583
Austin Schuh027fd622024-03-01 21:26:07 -0800584 extend_.set_min_position(min_extend_position);
585 extend_.set_max_position(max_extend_position);
586
Niko Sohmersac4d8872024-02-23 13:55:47 -0800587 // Calculate the loops for a cycle.
588 const double voltage = intake_pivot_.UpdateController(disabled);
589
590 intake_pivot_.UpdateObserver(voltage);
591
592 // Write out all the voltages.
593 if (intake_output) {
594 *intake_output = voltage;
595 }
596
597 const flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus>
598 intake_pivot_status_offset = intake_pivot_.MakeStatus(status->fbb());
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800599
Filip Kujawa7a799602024-02-23 12:27:47 -0800600 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
601 extend_status_offset = extend_.Iterate(
Austin Schuh027fd622024-03-01 21:26:07 -0800602 extend_goal, position->extend(),
Filip Kujawa7a799602024-02-23 12:27:47 -0800603 output != nullptr ? &output_struct.extend_voltage : nullptr,
604 status->fbb());
605
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700606 // Zero out extend voltage if "disable_extend" is true
Niko Sohmers58461f52024-03-20 20:12:10 -0700607 if (robot_constants_->robot()->disable_extend()) {
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700608 output_struct.extend_voltage = 0.0;
609 }
610
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800611 if (output) {
612 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
613 }
614
615 Status::Builder status_builder = status->MakeBuilder<Status>();
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800616
Niko Sohmers4d93d4c2024-03-24 14:48:26 -0700617 const bool zeroed =
618 intake_pivot_.zeroed() && shooter_.zeroed() && extend_.zeroed();
619 const bool estopped =
620 intake_pivot_.estopped() || shooter_.estopped() || extend_.estopped();
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800621
622 status_builder.add_zeroed(zeroed);
623 status_builder.add_estopped(estopped);
Filip Kujawa102a9b22024-02-18 09:40:23 -0800624 status_builder.add_intake_roller(intake_roller_state);
625 status_builder.add_intake_pivot(intake_pivot_status_offset);
Filip Kujawa7a799602024-02-23 12:27:47 -0800626 status_builder.add_transfer_roller(transfer_roller_status);
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800627 status_builder.add_shooter(shooter_status_offset);
Niko Sohmersac4d8872024-02-23 13:55:47 -0800628 status_builder.add_collided(collided);
Filip Kujawa7a799602024-02-23 12:27:47 -0800629 status_builder.add_extend_roller(extend_roller_status);
630 status_builder.add_extend_status(extend_status);
631 status_builder.add_extend(extend_status_offset);
632 status_builder.add_state(state_);
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700633 status_builder.add_shot_count(shooter_.shot_count());
Niko Sohmers5006fc42024-03-01 17:14:22 -0800634 status_builder.add_uncompleted_note_goal(uncompleted_note_goal_status);
James Kuszmaul0281e152024-02-26 22:26:16 -0800635 status_builder.add_extend_ready_for_transfer(extend_at_retracted);
Niko Sohmers2a251cd2024-03-02 19:16:15 -0800636 status_builder.add_extend_at_retracted(extend_at_retracted);
637 status_builder.add_turret_ready_for_load(turret_ready_for_load);
638 status_builder.add_altitude_ready_for_load(altitude_ready_for_load);
639 status_builder.add_extend_ready_for_catapult_transfer(
640 extend_ready_for_catapult_transfer);
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700641 status_builder.add_extend_beambreak(extend_beambreak);
Niko Sohmers2a251cd2024-03-02 19:16:15 -0800642 status_builder.add_catapult_beambreak(position->catapult_beambreak());
Niko Sohmers2ac60c52024-03-16 18:44:54 -0700643 status_builder.add_transfer_beambreak(transfer_beambreak);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800644
645 (void)status->Send(status_builder.Finish());
646}
647
648double Superstructure::robot_velocity() const {
649 return (drivetrain_status_fetcher_.get() != nullptr
650 ? drivetrain_status_fetcher_->robot_speed()
651 : 0.0);
652}
653
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800654} // namespace y2024::control_loops::superstructure