blob: e22dc809991da8d8d8343febc4bc50ec690e4207 [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
Niko Sohmers6adb5b92024-03-16 17:47:54 -070019constexpr double kTurretLoadingThreshold = 0.05;
20constexpr double kAltitudeLoadingThreshold = 0.02;
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 Kujawa7a799602024-02-23 12:27:47 -080046 shooter_(event_loop, robot_constants_),
47 extend_(
48 robot_constants_->common()->extend(),
Niko Sohmers6adb5b92024-03-16 17:47:54 -070049 robot_constants_->robot()->extend_constants()->zeroing_constants()),
James Kuszmaul9effe0f2024-03-23 15:58:54 -070050 extend_debouncer_(std::chrono::milliseconds(12),
Filip Kujawa0a1282b2024-03-23 12:36:51 -070051 std::chrono::milliseconds(8)),
52 transfer_debouncer_(std::chrono::milliseconds(30),
53 std::chrono::milliseconds(8)) {
54 event_loop->SetRuntimeRealtimePriority(30);
Niko Sohmers3860f8a2024-01-12 21:05:19 -080055}
56
Filip Kujawa7a799602024-02-23 12:27:47 -080057bool PositionNear(double position, double goal, double threshold) {
58 return std::abs(position - goal) < threshold;
59}
60
Niko Sohmers3860f8a2024-01-12 21:05:19 -080061void Superstructure::RunIteration(const Goal *unsafe_goal,
62 const Position *position,
63 aos::Sender<Output>::Builder *output,
64 aos::Sender<Status>::Builder *status) {
65 const monotonic_clock::time_point timestamp =
66 event_loop()->context().monotonic_event_time;
67
Niko Sohmers3860f8a2024-01-12 21:05:19 -080068 if (WasReset()) {
69 AOS_LOG(ERROR, "WPILib reset, restarting\n");
Niko Sohmersafc51fe2024-01-29 17:48:35 -080070 intake_pivot_.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
Niko Sohmers6adb5b92024-03-16 17:47:54 -070077 extend_debouncer_.Update(position->extend_beambreak(), timestamp);
78 const bool extend_beambreak = extend_debouncer_.state();
79
Filip Kujawa0a1282b2024-03-23 12:36:51 -070080 transfer_debouncer_.Update(position->transfer_beambreak(), timestamp);
81 const bool transfer_beambreak = transfer_debouncer_.state();
82
Filip Kujawa7a799602024-02-23 12:27:47 -080083 // If we started off preloaded, skip to the ready state.
84 if (unsafe_goal != nullptr && unsafe_goal->shooter_goal() &&
85 unsafe_goal->shooter_goal()->preloaded()) {
86 if (state_ != SuperstructureState::READY &&
87 state_ != SuperstructureState::FIRING) {
88 state_ = SuperstructureState::READY;
Niko Sohmers5006fc42024-03-01 17:14:22 -080089 requested_note_goal_ = NoteGoal::CATAPULT;
Filip Kujawa7a799602024-02-23 12:27:47 -080090 }
91 }
92
93 // Handle the intake pivot goal separately from the main superstructure state
94 IntakeRollerStatus intake_roller_state = IntakeRollerStatus::NONE;
95 double intake_pivot_position =
96 robot_constants_->common()->intake_pivot_set_points()->retracted();
97
98 if (unsafe_goal != nullptr) {
Niko Sohmers6adb5b92024-03-16 17:47:54 -070099 switch (unsafe_goal->intake_pivot()) {
100 case IntakePivotGoal::DOWN:
Filip Kujawa7a799602024-02-23 12:27:47 -0800101 intake_pivot_position =
102 robot_constants_->common()->intake_pivot_set_points()->extended();
Maxwell Hendersonf0d22622024-02-26 21:02:11 -0800103 intake_end_time_ = timestamp;
Filip Kujawa7a799602024-02-23 12:27:47 -0800104 break;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700105 case IntakePivotGoal::UP:
Filip Kujawa7a799602024-02-23 12:27:47 -0800106 intake_pivot_position =
107 robot_constants_->common()->intake_pivot_set_points()->retracted();
108 break;
109 }
110 }
111
112 ExtendRollerStatus extend_roller_status = ExtendRollerStatus::IDLE;
Austin Schuh027fd622024-03-01 21:26:07 -0800113 ExtendStatus extend_goal_location = ExtendStatus::RETRACTED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800114
115 // True if the extend is moving towards a goal
116 bool extend_moving = false;
117
118 TransferRollerStatus transfer_roller_status = TransferRollerStatus::NONE;
119
120 const ExtendSetPoints *extend_set_points =
121 robot_constants_->common()->extend_set_points();
122
123 // Checks if the extend is close enough to the retracted position to be
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700124 // considered ready to accept note from the transfer rollers. If disable
125 // extend is triggered, this will autoatically be false.
126 const bool extend_at_retracted =
Niko Sohmers58461f52024-03-20 20:12:10 -0700127 (!robot_constants_->robot()->disable_extend() &&
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700128 PositionNear(extend_.position(), extend_set_points->retracted(),
129 kExtendThreshold));
Filip Kujawa7a799602024-02-23 12:27:47 -0800130
Niko Sohmers5006fc42024-03-01 17:14:22 -0800131 // Check if the turret is at the position to accept the note from extend
132 const bool turret_ready_for_load =
133 PositionNear(shooter_.turret().estimated_position(),
134 robot_constants_->common()->turret_loading_position(),
135 kTurretLoadingThreshold);
136
Niko Sohmers5006fc42024-03-01 17:14:22 -0800137 // Check if the altitude is at the position to accept the note from
138 // extend
139 const bool altitude_ready_for_load =
140 PositionNear(shooter_.altitude().estimated_position(),
141 robot_constants_->common()->altitude_loading_position(),
142 kAltitudeLoadingThreshold);
143
144 // Check if the extend is at the position to load the catapult
145 const bool extend_ready_for_catapult_transfer = PositionNear(
146 extend_.position(), extend_set_points->catapult(), kExtendThreshold);
147
Niko Sohmers5006fc42024-03-01 17:14:22 -0800148 // Only update the reuested note goal to the first goal that is requested by
149 // the manipulator
150 if (unsafe_goal != nullptr && unsafe_goal->note_goal() != NoteGoal::NONE &&
151 requested_note_goal_ == NoteGoal::NONE) {
152 requested_note_goal_ = unsafe_goal->note_goal();
153 }
154
Filip Kujawa7a799602024-02-23 12:27:47 -0800155 // Superstructure state machine:
156 // 1. IDLE. The intake is retracted and there is no note in the robot.
157 // Wait for a intake goal to switch state to INTAKING if the extend is ready
158 // 2. INTAKING. Intake the note and transfer it towards the extend.
159 // Give intake, transfer, and extend rollers positive voltage to intake and
160 // transfer. Switch to LOADED when the extend beambreak is triggered.
161 // 3. LOADED. The note is in the extend and the extend is retracted.
162 // Wait for a note goal to switch state to MOVING.
163 // For AMP/TRAP goals, check that the turret is in a position to avoid
164 // collision.
165 // 4. MOVING. The extend is moving towards a goal (AMP, TRAP, or CATAPULT).
166 // For CATAPULT goals, wait for the turret and altitude to be in a position to
167 // accept the note from the extend.
168 // Wait for the extend to reach the goal and switch state to READY if
169 // AMP or TRAP, or to LOADING_CATAPULT if CATAPULT.
170 // 5. LOADING_CATAPULT. The extend is at the position to load the catapult.
171 // Activate the extend roller to transfer the note to the catapult.
172 // Switch state to READY when the catapult beambreak is triggered.
173 // 6. READY. Ready for fire command. The note is either loaded in the catapult
174 // or in the extend and at the AMP or TRAP position. Wait for a fire command.
175 // 7. FIRING. The note is being fired, either from the extend or the catapult.
176 // Switch state back to IDLE when the note is fired.
177
178 switch (state_) {
179 case SuperstructureState::IDLE:
Niko Sohmers5006fc42024-03-01 17:14:22 -0800180 requested_note_goal_ = NoteGoal::NONE;
181
Filip Kujawa7a799602024-02-23 12:27:47 -0800182 if (unsafe_goal != nullptr &&
183 unsafe_goal->intake_goal() == IntakeGoal::INTAKE &&
Niko Sohmersdd971dc2024-02-25 11:40:47 -0800184 extend_at_retracted) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800185 state_ = SuperstructureState::INTAKING;
Filip Kujawa0a1282b2024-03-23 12:36:51 -0700186 note_in_transfer_ = false;
Filip Kujawa7a799602024-02-23 12:27:47 -0800187 }
Maxwell Henderson4567e282024-02-25 15:38:15 -0800188
Austin Schuh027fd622024-03-01 21:26:07 -0800189 extend_goal_location = ExtendStatus::RETRACTED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800190 break;
191 case SuperstructureState::INTAKING:
Filip Kujawa7a799602024-02-23 12:27:47 -0800192 // Switch to LOADED state when the extend beambreak is triggered
193 // meaning the note is loaded in the extend
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700194 if (extend_beambreak) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800195 state_ = SuperstructureState::LOADED;
196 }
Filip Kujawa0a1282b2024-03-23 12:36:51 -0700197
198 if (transfer_beambreak) {
199 note_in_transfer_ = true;
200 }
201
202 // Once the note is in the transfer, stop the intake rollers
203 if (note_in_transfer_) {
204 intake_roller_state = IntakeRollerStatus::NONE;
205 } else {
206 intake_roller_state = IntakeRollerStatus::INTAKING;
207 }
208
Filip Kujawa7a799602024-02-23 12:27:47 -0800209 transfer_roller_status = TransferRollerStatus::TRANSFERING_IN;
210 extend_roller_status = ExtendRollerStatus::TRANSFERING_TO_EXTEND;
Austin Schuh027fd622024-03-01 21:26:07 -0800211 extend_goal_location = ExtendStatus::RETRACTED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800212
Maxwell Hendersonf0d22622024-02-26 21:02:11 -0800213 // If we are no longer requesting INTAKE or we are no longer requesting
214 // an INTAKE goal, wait 0.5 seconds then go back to IDLE.
215 if (!(unsafe_goal != nullptr &&
216 unsafe_goal->intake_goal() == IntakeGoal::INTAKE) &&
217 timestamp > intake_end_time_ + kExtraIntakingTime) {
218 state_ = SuperstructureState::IDLE;
219 }
220
Filip Kujawa7a799602024-02-23 12:27:47 -0800221 break;
222 case SuperstructureState::LOADED:
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700223 if (!extend_beambreak && !position->catapult_beambreak()) {
Maxwell Henderson4567e282024-02-25 15:38:15 -0800224 state_ = SuperstructureState::IDLE;
225 }
226
Niko Sohmers5006fc42024-03-01 17:14:22 -0800227 switch (requested_note_goal_) {
228 case NoteGoal::NONE:
229 break;
230 case NoteGoal::CATAPULT:
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700231 state_ = SuperstructureState::LOADING_CATAPULT;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800232 transfer_roller_status = TransferRollerStatus::TRANSFERING_IN;
233 break;
234 case NoteGoal::TRAP:
235 [[fallthrough]];
236 case NoteGoal::AMP:
James Kuszmaul6cae7d72024-03-01 21:29:56 -0800237 transfer_roller_status = TransferRollerStatus::EXTEND_MOVING;
Austin Schuh027fd622024-03-01 21:26:07 -0800238 state_ = SuperstructureState::MOVING;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800239 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800240 }
Austin Schuh027fd622024-03-01 21:26:07 -0800241 extend_goal_location = ExtendStatus::RETRACTED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800242 break;
243 case SuperstructureState::MOVING:
James Kuszmaul6cae7d72024-03-01 21:29:56 -0800244 transfer_roller_status = TransferRollerStatus::EXTEND_MOVING;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800245 switch (requested_note_goal_) {
246 case NoteGoal::NONE:
Austin Schuh027fd622024-03-01 21:26:07 -0800247 extend_goal_location = ExtendStatus::RETRACTED;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800248 if (extend_at_retracted) {
249 state_ = SuperstructureState::LOADED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800250 }
Niko Sohmers5006fc42024-03-01 17:14:22 -0800251 break;
252 case NoteGoal::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800253 extend_goal_location = ExtendStatus::CATAPULT;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800254 if (extend_ready_for_catapult_transfer && turret_ready_for_load &&
255 altitude_ready_for_load) {
256 state_ = SuperstructureState::LOADING_CATAPULT;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700257 loading_catapult_start_time_ = timestamp;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800258 }
259 break;
260 case NoteGoal::TRAP:
Austin Schuh027fd622024-03-01 21:26:07 -0800261 extend_goal_location = ExtendStatus::TRAP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800262 // Check if the extend is at the TRAP position and if it is
263 // switch to READY state
264 if (PositionNear(extend_.position(), extend_set_points->trap(),
265 kExtendThreshold)) {
266 state_ = SuperstructureState::READY;
267 }
268 break;
269 case NoteGoal::AMP:
Austin Schuh027fd622024-03-01 21:26:07 -0800270 extend_goal_location = ExtendStatus::AMP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800271 // Check if the extend is at the AMP position and if it is
272 // switch to READY state
273 if (PositionNear(extend_.position(), extend_set_points->amp(),
274 kExtendThreshold)) {
275 state_ = SuperstructureState::READY;
276 }
277 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800278 }
279
280 extend_moving = true;
281 break;
282 case SuperstructureState::LOADING_CATAPULT:
283 extend_moving = false;
Austin Schuh027fd622024-03-01 21:26:07 -0800284 extend_goal_location = ExtendStatus::CATAPULT;
Filip Kujawa7a799602024-02-23 12:27:47 -0800285
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700286 if (extend_beambreak) {
287 loading_catapult_start_time_ = timestamp;
288 }
289
290 if (loading_catapult_start_time_ + std::chrono::seconds(10) < timestamp) {
Filip Kujawa77e3d8a2024-03-02 11:34:56 -0800291 state_ = SuperstructureState::IDLE;
292 }
293
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700294 if (turret_ready_for_load && altitude_ready_for_load) {
295 extend_roller_status = ExtendRollerStatus::TRANSFERING_TO_CATAPULT;
296 transfer_roller_status = TransferRollerStatus::EXTEND_MOVING;
297 }
298
Filip Kujawa7a799602024-02-23 12:27:47 -0800299 // Switch to READY state when the catapult beambreak is triggered
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700300 if (shooter_.loaded()) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800301 state_ = SuperstructureState::READY;
302 }
303 break;
304 case SuperstructureState::READY:
305 extend_moving = false;
306
307 // Switch to FIRING state when the fire button is pressed
308 if (unsafe_goal != nullptr && unsafe_goal->fire()) {
309 state_ = SuperstructureState::FIRING;
310 }
311
Niko Sohmers5006fc42024-03-01 17:14:22 -0800312 switch (requested_note_goal_) {
313 case NoteGoal::NONE:
Austin Schuh027fd622024-03-01 21:26:07 -0800314 extend_goal_location = ExtendStatus::RETRACTED;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800315 extend_moving = true;
316 state_ = SuperstructureState::MOVING;
317 break;
318 case NoteGoal::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800319 extend_goal_location = ExtendStatus::CATAPULT;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700320
321 if (!shooter_.loaded()) {
322 state_ = SuperstructureState::LOADING_CATAPULT;
323 }
Niko Sohmers5006fc42024-03-01 17:14:22 -0800324 break;
325 case NoteGoal::TRAP:
Austin Schuh027fd622024-03-01 21:26:07 -0800326 extend_goal_location = ExtendStatus::TRAP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800327 break;
328 case NoteGoal::AMP:
Austin Schuh027fd622024-03-01 21:26:07 -0800329 extend_goal_location = ExtendStatus::AMP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800330 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800331 }
Filip Kujawa7a799602024-02-23 12:27:47 -0800332 break;
333 case SuperstructureState::FIRING:
Niko Sohmers5006fc42024-03-01 17:14:22 -0800334 switch (requested_note_goal_) {
335 case NoteGoal::NONE:
Filip Kujawa7a799602024-02-23 12:27:47 -0800336
Niko Sohmers5006fc42024-03-01 17:14:22 -0800337 break;
338 case NoteGoal::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800339 extend_goal_location = ExtendStatus::CATAPULT;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800340 // Reset the state to IDLE when the game piece is fired from the
341 // catapult. We consider the game piece to be fired from the catapult
342 // when the catapultbeambreak is no longer triggered.
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700343 if (!shooter_.loaded() && !shooter_.Firing()) {
Niko Sohmers5006fc42024-03-01 17:14:22 -0800344 state_ = SuperstructureState::IDLE;
345 }
346 break;
347 case NoteGoal::TRAP:
Filip Kujawa7a799602024-02-23 12:27:47 -0800348 extend_roller_status = ExtendRollerStatus::SCORING_IN_TRAP;
Austin Schuh027fd622024-03-01 21:26:07 -0800349 extend_goal_location = ExtendStatus::TRAP;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700350 if (!extend_beambreak && unsafe_goal != nullptr &&
Niko Sohmers5006fc42024-03-01 17:14:22 -0800351 !unsafe_goal->fire()) {
352 state_ = SuperstructureState::IDLE;
353 }
354 break;
355 case NoteGoal::AMP:
356 extend_roller_status = ExtendRollerStatus::SCORING_IN_AMP;
Austin Schuh027fd622024-03-01 21:26:07 -0800357 extend_goal_location = ExtendStatus::AMP;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700358 if (!extend_beambreak && unsafe_goal != nullptr &&
Niko Sohmers5006fc42024-03-01 17:14:22 -0800359 !unsafe_goal->fire()) {
360 state_ = SuperstructureState::IDLE;
361 }
362 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800363 }
364 break;
365 }
366
367 if (unsafe_goal != nullptr &&
368 unsafe_goal->intake_goal() == IntakeGoal::SPIT) {
369 intake_roller_state = IntakeRollerStatus::SPITTING;
370 transfer_roller_status = TransferRollerStatus::TRANSFERING_OUT;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700371 extend_roller_status = ExtendRollerStatus::SCORING_IN_AMP;
Filip Kujawa7a799602024-02-23 12:27:47 -0800372 }
373
374 // Update Intake Roller voltage based on status from state machine.
375 switch (intake_roller_state) {
376 case IntakeRollerStatus::NONE:
377 output_struct.intake_roller_voltage = 0.0;
378 break;
379 case IntakeRollerStatus::SPITTING:
380 output_struct.intake_roller_voltage =
381 robot_constants_->common()->intake_roller_voltages()->spitting();
382 break;
383 case IntakeRollerStatus::INTAKING:
384 output_struct.intake_roller_voltage =
385 robot_constants_->common()->intake_roller_voltages()->intaking();
386 break;
387 }
388
389 // Update Transfer Roller voltage based on status from state machine.
390 switch (transfer_roller_status) {
391 case TransferRollerStatus::NONE:
392 output_struct.transfer_roller_voltage = 0.0;
393 break;
394 case TransferRollerStatus::TRANSFERING_IN:
395 output_struct.transfer_roller_voltage =
396 robot_constants_->common()->transfer_roller_voltages()->transfer_in();
397 break;
398 case TransferRollerStatus::TRANSFERING_OUT:
399 output_struct.transfer_roller_voltage = robot_constants_->common()
400 ->transfer_roller_voltages()
401 ->transfer_out();
402 break;
James Kuszmaul6cae7d72024-03-01 21:29:56 -0800403 case TransferRollerStatus::EXTEND_MOVING:
404 output_struct.transfer_roller_voltage = robot_constants_->common()
405 ->transfer_roller_voltages()
406 ->extend_moving();
407 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800408 }
409
410 // Update Extend Roller voltage based on status from state machine.
411 const ExtendRollerVoltages *extend_roller_voltages =
412 robot_constants_->common()->extend_roller_voltages();
413 switch (extend_roller_status) {
414 case ExtendRollerStatus::IDLE:
415 // No voltage applied when idle
416 output_struct.extend_roller_voltage = 0.0;
417 break;
418 case ExtendRollerStatus::TRANSFERING_TO_EXTEND:
419 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
420 break;
421 case ExtendRollerStatus::SCORING_IN_AMP:
422 [[fallthrough]];
423 case ExtendRollerStatus::SCORING_IN_TRAP:
424 // Apply scoring voltage during scoring in amp or trap
425 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
426 break;
427 case ExtendRollerStatus::TRANSFERING_TO_CATAPULT:
428 // Apply scoring voltage during transferring to catapult
429 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
430 break;
431 }
432
Maxwell Henderson45878502024-03-15 19:35:25 -0700433 if (unsafe_goal != nullptr && unsafe_goal->spit_extend()) {
434 output_struct.extend_roller_voltage = -extend_roller_voltages->scoring();
435 }
436
Austin Schuh027fd622024-03-01 21:26:07 -0800437 double extend_goal_position = 0.0;
Filip Kujawa7a799602024-02-23 12:27:47 -0800438
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700439 // If we request trap, override the extend goal to be trap unless we request
440 // amp.
James Kuszmaul0281e152024-02-26 22:26:16 -0800441 if (unsafe_goal != nullptr && unsafe_goal->note_goal() == NoteGoal::TRAP) {
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700442 trap_override_ = true;
443 }
444
445 if (unsafe_goal != nullptr && unsafe_goal->note_goal() == NoteGoal::AMP &&
446 trap_override_) {
447 trap_override_ = false;
448 requested_note_goal_ = NoteGoal::AMP;
449 state_ = SuperstructureState::READY;
450 }
451
452 if (trap_override_) {
Austin Schuh027fd622024-03-01 21:26:07 -0800453 extend_goal_location = ExtendStatus::TRAP;
James Kuszmaul0281e152024-02-26 22:26:16 -0800454 }
455
Filip Kujawa7a799602024-02-23 12:27:47 -0800456 // Set the extend position based on the state machine output
Austin Schuh027fd622024-03-01 21:26:07 -0800457 switch (extend_goal_location) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800458 case ExtendStatus::RETRACTED:
Austin Schuh027fd622024-03-01 21:26:07 -0800459 extend_goal_position = extend_set_points->retracted();
Filip Kujawa7a799602024-02-23 12:27:47 -0800460 break;
461 case ExtendStatus::AMP:
Austin Schuh027fd622024-03-01 21:26:07 -0800462 extend_goal_position = extend_set_points->amp();
Filip Kujawa7a799602024-02-23 12:27:47 -0800463 break;
464 case ExtendStatus::TRAP:
Austin Schuh027fd622024-03-01 21:26:07 -0800465 extend_goal_position = extend_set_points->trap();
Filip Kujawa7a799602024-02-23 12:27:47 -0800466 break;
467 case ExtendStatus::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800468 extend_goal_position = extend_set_points->catapult();
Filip Kujawa7a799602024-02-23 12:27:47 -0800469 break;
470 case ExtendStatus::MOVING:
471 // Should never happen
472 break;
473 }
474
Niko Sohmers5006fc42024-03-01 17:14:22 -0800475 NoteStatus uncompleted_note_goal_status = NoteStatus::NONE;
476
477 switch (requested_note_goal_) {
478 case NoteGoal::NONE:
479 uncompleted_note_goal_status = NoteStatus::NONE;
480 break;
481 case NoteGoal::CATAPULT:
482 uncompleted_note_goal_status = NoteStatus::CATAPULT;
483 break;
484 case NoteGoal::AMP:
485 uncompleted_note_goal_status = NoteStatus::AMP;
486 break;
487 case NoteGoal::TRAP:
488 uncompleted_note_goal_status = NoteStatus::TRAP;
489 break;
490 }
491
Filip Kujawa7a799602024-02-23 12:27:47 -0800492 // Set the extend status based on the state machine output
493 // If the extend is moving, the status is MOVING, otherwise it is the same
494 // as extend_status
495 ExtendStatus extend_status =
Austin Schuh027fd622024-03-01 21:26:07 -0800496 (extend_moving ? ExtendStatus::MOVING : extend_goal_location);
Filip Kujawa7a799602024-02-23 12:27:47 -0800497
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800498 if (joystick_state_fetcher_.Fetch() &&
499 joystick_state_fetcher_->has_alliance()) {
500 alliance_ = joystick_state_fetcher_->alliance();
501 }
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800502
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800503 drivetrain_status_fetcher_.Fetch();
504
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700505 // Zero out extend position if "disable_extend" is true
Austin Schuh027fd622024-03-01 21:26:07 -0800506 const bool collided = collision_avoidance_.IsCollided({
507 .intake_pivot_position = intake_pivot_.estimated_position(),
508 .turret_position = shooter_.turret().estimated_position(),
Niko Sohmers58461f52024-03-20 20:12:10 -0700509 .extend_position = ((!robot_constants_->robot()->disable_extend())
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700510 ? extend_.estimated_position()
511 : 0.0),
Austin Schuh027fd622024-03-01 21:26:07 -0800512 });
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800513
Niko Sohmersac4d8872024-02-23 13:55:47 -0800514 double max_intake_pivot_position = 0;
515 double min_intake_pivot_position = 0;
Austin Schuh027fd622024-03-01 21:26:07 -0800516 double max_extend_position = 0;
517 double min_extend_position = 0;
Niko Sohmersac4d8872024-02-23 13:55:47 -0800518
519 aos::FlatbufferFixedAllocatorArray<
520 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
521 intake_pivot_goal_buffer;
522
523 intake_pivot_goal_buffer.Finish(
524 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
525 *intake_pivot_goal_buffer.fbb(), intake_pivot_position));
526
527 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
528 *intake_pivot_goal = &intake_pivot_goal_buffer.message();
529
530 double *intake_output =
531 (output != nullptr ? &output_struct.intake_pivot_voltage : nullptr);
532
533 const bool disabled = intake_pivot_.Correct(
534 intake_pivot_goal, position->intake_pivot(), intake_output == nullptr);
535
Austin Schuh027fd622024-03-01 21:26:07 -0800536 aos::FlatbufferFixedAllocatorArray<
537 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
538 extend_goal_buffer;
539
540 extend_goal_buffer.Finish(
541 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
542 *extend_goal_buffer.fbb(), extend_goal_position));
543
544 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
545 *extend_goal = &extend_goal_buffer.message();
546
James Kuszmaul42a87452024-04-05 17:30:47 -0700547 // Ignore climber voltage goal if "disable_climber" is true
548 output_struct.climber_voltage =
549 (!robot_constants_->robot()->disable_climber() && unsafe_goal != nullptr)
550 ? unsafe_goal->climber_goal_voltage()
551 : 0.0;
552
553 if (output) {
554 if (output_struct.climber_voltage != 0.0) {
555 ++climbing_;
556 }
557 }
558
Filip Kujawa7a799602024-02-23 12:27:47 -0800559 // TODO(max): Change how we handle the collision with the turret and
560 // intake to be clearer
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800561 const flatbuffers::Offset<ShooterStatus> shooter_status_offset =
562 shooter_.Iterate(
563 position,
564 unsafe_goal != nullptr ? unsafe_goal->shooter_goal() : nullptr,
Filip Kujawa7a799602024-02-23 12:27:47 -0800565 unsafe_goal != nullptr ? unsafe_goal->fire() : false,
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800566 output != nullptr ? &output_struct.catapult_voltage : nullptr,
567 output != nullptr ? &output_struct.altitude_voltage : nullptr,
568 output != nullptr ? &output_struct.turret_voltage : nullptr,
569 output != nullptr ? &output_struct.retention_roller_voltage : nullptr,
Maxwell Hendersond5bf47a2024-02-23 17:16:48 -0800570 output != nullptr
571 ? &output_struct.retention_roller_stator_current_limit
572 : nullptr,
Maxwell Henderson461a8a42024-02-23 17:07:39 -0800573 robot_state().voltage_battery(), &collision_avoidance_,
Austin Schuh027fd622024-03-01 21:26:07 -0800574 extend_goal_position, extend_.estimated_position(),
575 &max_extend_position, &min_extend_position,
Niko Sohmersac4d8872024-02-23 13:55:47 -0800576 intake_pivot_.estimated_position(), &max_intake_pivot_position,
Stephan Pleines9f3983a2024-03-13 20:22:38 -0700577 &min_intake_pivot_position, requested_note_goal_, status->fbb(),
James Kuszmaul42a87452024-04-05 17:30:47 -0700578 timestamp, climbing_ > 50);
Niko Sohmersac4d8872024-02-23 13:55:47 -0800579
580 intake_pivot_.set_min_position(min_intake_pivot_position);
581 intake_pivot_.set_max_position(max_intake_pivot_position);
582
Austin Schuh027fd622024-03-01 21:26:07 -0800583 extend_.set_min_position(min_extend_position);
584 extend_.set_max_position(max_extend_position);
585
Niko Sohmersac4d8872024-02-23 13:55:47 -0800586 // Calculate the loops for a cycle.
587 const double voltage = intake_pivot_.UpdateController(disabled);
588
589 intake_pivot_.UpdateObserver(voltage);
590
591 // Write out all the voltages.
592 if (intake_output) {
593 *intake_output = voltage;
594 }
595
596 const flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus>
597 intake_pivot_status_offset = intake_pivot_.MakeStatus(status->fbb());
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800598
Filip Kujawa7a799602024-02-23 12:27:47 -0800599 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
600 extend_status_offset = extend_.Iterate(
Austin Schuh027fd622024-03-01 21:26:07 -0800601 extend_goal, position->extend(),
Filip Kujawa7a799602024-02-23 12:27:47 -0800602 output != nullptr ? &output_struct.extend_voltage : nullptr,
603 status->fbb());
604
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700605 // Zero out extend voltage if "disable_extend" is true
Niko Sohmers58461f52024-03-20 20:12:10 -0700606 if (robot_constants_->robot()->disable_extend()) {
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700607 output_struct.extend_voltage = 0.0;
608 }
609
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800610 if (output) {
611 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
612 }
613
614 Status::Builder status_builder = status->MakeBuilder<Status>();
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800615
Niko Sohmers4d93d4c2024-03-24 14:48:26 -0700616 const bool zeroed =
617 intake_pivot_.zeroed() && shooter_.zeroed() && extend_.zeroed();
618 const bool estopped =
619 intake_pivot_.estopped() || shooter_.estopped() || extend_.estopped();
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800620
621 status_builder.add_zeroed(zeroed);
622 status_builder.add_estopped(estopped);
Filip Kujawa102a9b22024-02-18 09:40:23 -0800623 status_builder.add_intake_roller(intake_roller_state);
624 status_builder.add_intake_pivot(intake_pivot_status_offset);
Filip Kujawa7a799602024-02-23 12:27:47 -0800625 status_builder.add_transfer_roller(transfer_roller_status);
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800626 status_builder.add_shooter(shooter_status_offset);
Niko Sohmersac4d8872024-02-23 13:55:47 -0800627 status_builder.add_collided(collided);
Filip Kujawa7a799602024-02-23 12:27:47 -0800628 status_builder.add_extend_roller(extend_roller_status);
629 status_builder.add_extend_status(extend_status);
630 status_builder.add_extend(extend_status_offset);
631 status_builder.add_state(state_);
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700632 status_builder.add_shot_count(shooter_.shot_count());
Niko Sohmers5006fc42024-03-01 17:14:22 -0800633 status_builder.add_uncompleted_note_goal(uncompleted_note_goal_status);
James Kuszmaul0281e152024-02-26 22:26:16 -0800634 status_builder.add_extend_ready_for_transfer(extend_at_retracted);
Niko Sohmers2a251cd2024-03-02 19:16:15 -0800635 status_builder.add_extend_at_retracted(extend_at_retracted);
636 status_builder.add_turret_ready_for_load(turret_ready_for_load);
637 status_builder.add_altitude_ready_for_load(altitude_ready_for_load);
638 status_builder.add_extend_ready_for_catapult_transfer(
639 extend_ready_for_catapult_transfer);
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700640 status_builder.add_extend_beambreak(extend_beambreak);
Niko Sohmers2a251cd2024-03-02 19:16:15 -0800641 status_builder.add_catapult_beambreak(position->catapult_beambreak());
Niko Sohmers2ac60c52024-03-16 18:44:54 -0700642 status_builder.add_transfer_beambreak(transfer_beambreak);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800643
644 (void)status->Send(status_builder.Finish());
645}
646
647double Superstructure::robot_velocity() const {
648 return (drivetrain_status_fetcher_.get() != nullptr
649 ? drivetrain_status_fetcher_->robot_speed()
650 : 0.0);
651}
652
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800653} // namespace y2024::control_loops::superstructure