blob: 57275460bcf0f15ffda911de271dd92083996bd9 [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 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(),
Niko Sohmers6adb5b92024-03-16 17:47:54 -070052 robot_constants_->robot()->extend_constants()->zeroing_constants()),
53 extend_debouncer_(std::chrono::milliseconds(30),
54 std::chrono::milliseconds(8)) {
Austin Schuhed5ce8b2024-03-16 21:16:41 -070055 event_loop->SetRuntimeRealtimePriority(37);
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();
Filip Kujawa6d717632024-02-01 11:40:55 -080072 climber_.Reset();
Niko Sohmersc4d2c502024-02-19 19:35:35 -080073 shooter_.Reset();
Filip Kujawa7a799602024-02-23 12:27:47 -080074 extend_.Reset();
Niko Sohmers3860f8a2024-01-12 21:05:19 -080075 }
76
77 OutputT output_struct;
Niko Sohmersafc51fe2024-01-29 17:48:35 -080078
Niko Sohmers6adb5b92024-03-16 17:47:54 -070079 extend_debouncer_.Update(position->extend_beambreak(), timestamp);
80 const bool extend_beambreak = extend_debouncer_.state();
81
Filip Kujawa7a799602024-02-23 12:27:47 -080082 // Handle Climber Goal separately from main superstructure state machine
Filip Kujawa6d717632024-02-01 11:40:55 -080083 double climber_position =
84 robot_constants_->common()->climber_set_points()->retract();
85
Niko Sohmers6adb5b92024-03-16 17:47:54 -070086 double climber_velocity = robot_constants_->common()
87 ->climber()
88 ->default_profile_params()
89 ->max_velocity();
90 const double climber_accel = robot_constants_->common()
91 ->climber()
92 ->default_profile_params()
93 ->max_acceleration();
94
Filip Kujawa6d717632024-02-01 11:40:55 -080095 if (unsafe_goal != nullptr) {
96 switch (unsafe_goal->climber_goal()) {
97 case ClimberGoal::FULL_EXTEND:
98 climber_position =
99 robot_constants_->common()->climber_set_points()->full_extend();
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700100 // The climber can go reasonably fast when extending out.
101 climber_velocity = 0.5;
102
103 if (unsafe_goal->slow_climber()) {
104 climber_velocity = 0.01;
105 }
Filip Kujawa6d717632024-02-01 11:40:55 -0800106 break;
Filip Kujawa6d717632024-02-01 11:40:55 -0800107 case ClimberGoal::RETRACT:
108 climber_position =
109 robot_constants_->common()->climber_set_points()->retract();
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700110 // Keep the climber slower while retracting.
111 climber_velocity = 0.1;
Filip Kujawa6d717632024-02-01 11:40:55 -0800112 break;
Maxwell Henderson7db29782024-02-24 20:10:26 -0800113 case ClimberGoal::STOWED:
114 climber_position =
115 robot_constants_->common()->climber_set_points()->stowed();
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700116 // Keep the climber slower while retracting.
117 climber_velocity = 0.1;
Filip Kujawa6d717632024-02-01 11:40:55 -0800118 }
119 }
120
Filip Kujawa7a799602024-02-23 12:27:47 -0800121 // If we started off preloaded, skip to the ready state.
122 if (unsafe_goal != nullptr && unsafe_goal->shooter_goal() &&
123 unsafe_goal->shooter_goal()->preloaded()) {
124 if (state_ != SuperstructureState::READY &&
125 state_ != SuperstructureState::FIRING) {
126 state_ = SuperstructureState::READY;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800127 requested_note_goal_ = NoteGoal::CATAPULT;
Filip Kujawa7a799602024-02-23 12:27:47 -0800128 }
129 }
130
131 // Handle the intake pivot goal separately from the main superstructure state
132 IntakeRollerStatus intake_roller_state = IntakeRollerStatus::NONE;
133 double intake_pivot_position =
134 robot_constants_->common()->intake_pivot_set_points()->retracted();
135
136 if (unsafe_goal != nullptr) {
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700137 switch (unsafe_goal->intake_pivot()) {
138 case IntakePivotGoal::DOWN:
Filip Kujawa7a799602024-02-23 12:27:47 -0800139 intake_pivot_position =
140 robot_constants_->common()->intake_pivot_set_points()->extended();
Maxwell Hendersonf0d22622024-02-26 21:02:11 -0800141 intake_end_time_ = timestamp;
Filip Kujawa7a799602024-02-23 12:27:47 -0800142 break;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700143 case IntakePivotGoal::UP:
Filip Kujawa7a799602024-02-23 12:27:47 -0800144 intake_pivot_position =
145 robot_constants_->common()->intake_pivot_set_points()->retracted();
146 break;
147 }
148 }
149
150 ExtendRollerStatus extend_roller_status = ExtendRollerStatus::IDLE;
Austin Schuh027fd622024-03-01 21:26:07 -0800151 ExtendStatus extend_goal_location = ExtendStatus::RETRACTED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800152
153 // True if the extend is moving towards a goal
154 bool extend_moving = false;
155
156 TransferRollerStatus transfer_roller_status = TransferRollerStatus::NONE;
157
158 const ExtendSetPoints *extend_set_points =
159 robot_constants_->common()->extend_set_points();
160
161 // Checks if the extend is close enough to the retracted position to be
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700162 // considered ready to accept note from the transfer rollers. If disable
163 // extend is triggered, this will autoatically be false.
164 const bool extend_at_retracted =
165 (!robot_constants_->common()->disable_extend() &&
166 PositionNear(extend_.position(), extend_set_points->retracted(),
167 kExtendThreshold));
Filip Kujawa7a799602024-02-23 12:27:47 -0800168
Niko Sohmers5006fc42024-03-01 17:14:22 -0800169 // Check if the turret is at the position to accept the note from extend
170 const bool turret_ready_for_load =
171 PositionNear(shooter_.turret().estimated_position(),
172 robot_constants_->common()->turret_loading_position(),
173 kTurretLoadingThreshold);
174
Niko Sohmers5006fc42024-03-01 17:14:22 -0800175 // Check if the altitude is at the position to accept the note from
176 // extend
177 const bool altitude_ready_for_load =
178 PositionNear(shooter_.altitude().estimated_position(),
179 robot_constants_->common()->altitude_loading_position(),
180 kAltitudeLoadingThreshold);
181
182 // Check if the extend is at the position to load the catapult
183 const bool extend_ready_for_catapult_transfer = PositionNear(
184 extend_.position(), extend_set_points->catapult(), kExtendThreshold);
185
Niko Sohmers5006fc42024-03-01 17:14:22 -0800186 // Only update the reuested note goal to the first goal that is requested by
187 // the manipulator
188 if (unsafe_goal != nullptr && unsafe_goal->note_goal() != NoteGoal::NONE &&
189 requested_note_goal_ == NoteGoal::NONE) {
190 requested_note_goal_ = unsafe_goal->note_goal();
191 }
192
Filip Kujawa7a799602024-02-23 12:27:47 -0800193 // Superstructure state machine:
194 // 1. IDLE. The intake is retracted and there is no note in the robot.
195 // Wait for a intake goal to switch state to INTAKING if the extend is ready
196 // 2. INTAKING. Intake the note and transfer it towards the extend.
197 // Give intake, transfer, and extend rollers positive voltage to intake and
198 // transfer. Switch to LOADED when the extend beambreak is triggered.
199 // 3. LOADED. The note is in the extend and the extend is retracted.
200 // Wait for a note goal to switch state to MOVING.
201 // For AMP/TRAP goals, check that the turret is in a position to avoid
202 // collision.
203 // 4. MOVING. The extend is moving towards a goal (AMP, TRAP, or CATAPULT).
204 // For CATAPULT goals, wait for the turret and altitude to be in a position to
205 // accept the note from the extend.
206 // Wait for the extend to reach the goal and switch state to READY if
207 // AMP or TRAP, or to LOADING_CATAPULT if CATAPULT.
208 // 5. LOADING_CATAPULT. The extend is at the position to load the catapult.
209 // Activate the extend roller to transfer the note to the catapult.
210 // Switch state to READY when the catapult beambreak is triggered.
211 // 6. READY. Ready for fire command. The note is either loaded in the catapult
212 // or in the extend and at the AMP or TRAP position. Wait for a fire command.
213 // 7. FIRING. The note is being fired, either from the extend or the catapult.
214 // Switch state back to IDLE when the note is fired.
215
216 switch (state_) {
217 case SuperstructureState::IDLE:
Niko Sohmers5006fc42024-03-01 17:14:22 -0800218 requested_note_goal_ = NoteGoal::NONE;
219
Filip Kujawa7a799602024-02-23 12:27:47 -0800220 if (unsafe_goal != nullptr &&
221 unsafe_goal->intake_goal() == IntakeGoal::INTAKE &&
Niko Sohmersdd971dc2024-02-25 11:40:47 -0800222 extend_at_retracted) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800223 state_ = SuperstructureState::INTAKING;
224 }
Maxwell Henderson4567e282024-02-25 15:38:15 -0800225
Austin Schuh027fd622024-03-01 21:26:07 -0800226 extend_goal_location = ExtendStatus::RETRACTED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800227 break;
228 case SuperstructureState::INTAKING:
Filip Kujawa7a799602024-02-23 12:27:47 -0800229 // Switch to LOADED state when the extend beambreak is triggered
230 // meaning the note is loaded in the extend
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700231 if (extend_beambreak) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800232 state_ = SuperstructureState::LOADED;
233 }
234 intake_roller_state = IntakeRollerStatus::INTAKING;
235 transfer_roller_status = TransferRollerStatus::TRANSFERING_IN;
236 extend_roller_status = ExtendRollerStatus::TRANSFERING_TO_EXTEND;
Austin Schuh027fd622024-03-01 21:26:07 -0800237 extend_goal_location = ExtendStatus::RETRACTED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800238
Maxwell Hendersonf0d22622024-02-26 21:02:11 -0800239 // If we are no longer requesting INTAKE or we are no longer requesting
240 // an INTAKE goal, wait 0.5 seconds then go back to IDLE.
241 if (!(unsafe_goal != nullptr &&
242 unsafe_goal->intake_goal() == IntakeGoal::INTAKE) &&
243 timestamp > intake_end_time_ + kExtraIntakingTime) {
244 state_ = SuperstructureState::IDLE;
245 }
246
Filip Kujawa7a799602024-02-23 12:27:47 -0800247 break;
248 case SuperstructureState::LOADED:
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700249 if (!extend_beambreak && !position->catapult_beambreak()) {
Maxwell Henderson4567e282024-02-25 15:38:15 -0800250 state_ = SuperstructureState::IDLE;
251 }
252
Niko Sohmers5006fc42024-03-01 17:14:22 -0800253 switch (requested_note_goal_) {
254 case NoteGoal::NONE:
255 break;
256 case NoteGoal::CATAPULT:
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700257 state_ = SuperstructureState::LOADING_CATAPULT;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800258 transfer_roller_status = TransferRollerStatus::TRANSFERING_IN;
259 break;
260 case NoteGoal::TRAP:
261 [[fallthrough]];
262 case NoteGoal::AMP:
James Kuszmaul6cae7d72024-03-01 21:29:56 -0800263 transfer_roller_status = TransferRollerStatus::EXTEND_MOVING;
Austin Schuh027fd622024-03-01 21:26:07 -0800264 state_ = SuperstructureState::MOVING;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800265 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800266 }
Austin Schuh027fd622024-03-01 21:26:07 -0800267 extend_goal_location = ExtendStatus::RETRACTED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800268 break;
269 case SuperstructureState::MOVING:
James Kuszmaul6cae7d72024-03-01 21:29:56 -0800270 transfer_roller_status = TransferRollerStatus::EXTEND_MOVING;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800271 switch (requested_note_goal_) {
272 case NoteGoal::NONE:
Austin Schuh027fd622024-03-01 21:26:07 -0800273 extend_goal_location = ExtendStatus::RETRACTED;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800274 if (extend_at_retracted) {
275 state_ = SuperstructureState::LOADED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800276 }
Niko Sohmers5006fc42024-03-01 17:14:22 -0800277 break;
278 case NoteGoal::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800279 extend_goal_location = ExtendStatus::CATAPULT;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800280 if (extend_ready_for_catapult_transfer && turret_ready_for_load &&
281 altitude_ready_for_load) {
282 state_ = SuperstructureState::LOADING_CATAPULT;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700283 loading_catapult_start_time_ = timestamp;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800284 }
285 break;
286 case NoteGoal::TRAP:
Austin Schuh027fd622024-03-01 21:26:07 -0800287 extend_goal_location = ExtendStatus::TRAP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800288 // Check if the extend is at the TRAP position and if it is
289 // switch to READY state
290 if (PositionNear(extend_.position(), extend_set_points->trap(),
291 kExtendThreshold)) {
292 state_ = SuperstructureState::READY;
293 }
294 break;
295 case NoteGoal::AMP:
Austin Schuh027fd622024-03-01 21:26:07 -0800296 extend_goal_location = ExtendStatus::AMP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800297 // Check if the extend is at the AMP position and if it is
298 // switch to READY state
299 if (PositionNear(extend_.position(), extend_set_points->amp(),
300 kExtendThreshold)) {
301 state_ = SuperstructureState::READY;
302 }
303 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800304 }
305
306 extend_moving = true;
307 break;
308 case SuperstructureState::LOADING_CATAPULT:
309 extend_moving = false;
Austin Schuh027fd622024-03-01 21:26:07 -0800310 extend_goal_location = ExtendStatus::CATAPULT;
Filip Kujawa7a799602024-02-23 12:27:47 -0800311
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700312 if (extend_beambreak) {
313 loading_catapult_start_time_ = timestamp;
314 }
315
316 if (loading_catapult_start_time_ + std::chrono::seconds(10) < timestamp) {
Filip Kujawa77e3d8a2024-03-02 11:34:56 -0800317 state_ = SuperstructureState::IDLE;
318 }
319
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700320 if (turret_ready_for_load && altitude_ready_for_load) {
321 extend_roller_status = ExtendRollerStatus::TRANSFERING_TO_CATAPULT;
322 transfer_roller_status = TransferRollerStatus::EXTEND_MOVING;
323 }
324
Filip Kujawa7a799602024-02-23 12:27:47 -0800325 // Switch to READY state when the catapult beambreak is triggered
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700326 if (shooter_.loaded()) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800327 state_ = SuperstructureState::READY;
328 }
329 break;
330 case SuperstructureState::READY:
331 extend_moving = false;
332
333 // Switch to FIRING state when the fire button is pressed
334 if (unsafe_goal != nullptr && unsafe_goal->fire()) {
335 state_ = SuperstructureState::FIRING;
336 }
337
Niko Sohmers5006fc42024-03-01 17:14:22 -0800338 switch (requested_note_goal_) {
339 case NoteGoal::NONE:
Austin Schuh027fd622024-03-01 21:26:07 -0800340 extend_goal_location = ExtendStatus::RETRACTED;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800341 extend_moving = true;
342 state_ = SuperstructureState::MOVING;
343 break;
344 case NoteGoal::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800345 extend_goal_location = ExtendStatus::CATAPULT;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700346
347 if (!shooter_.loaded()) {
348 state_ = SuperstructureState::LOADING_CATAPULT;
349 }
Niko Sohmers5006fc42024-03-01 17:14:22 -0800350 break;
351 case NoteGoal::TRAP:
Austin Schuh027fd622024-03-01 21:26:07 -0800352 extend_goal_location = ExtendStatus::TRAP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800353 break;
354 case NoteGoal::AMP:
Austin Schuh027fd622024-03-01 21:26:07 -0800355 extend_goal_location = ExtendStatus::AMP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800356 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800357 }
Filip Kujawa7a799602024-02-23 12:27:47 -0800358 break;
359 case SuperstructureState::FIRING:
Niko Sohmers5006fc42024-03-01 17:14:22 -0800360 switch (requested_note_goal_) {
361 case NoteGoal::NONE:
Filip Kujawa7a799602024-02-23 12:27:47 -0800362
Niko Sohmers5006fc42024-03-01 17:14:22 -0800363 break;
364 case NoteGoal::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800365 extend_goal_location = ExtendStatus::CATAPULT;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800366 // Reset the state to IDLE when the game piece is fired from the
367 // catapult. We consider the game piece to be fired from the catapult
368 // when the catapultbeambreak is no longer triggered.
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700369 if (!shooter_.loaded() && !shooter_.Firing()) {
Niko Sohmers5006fc42024-03-01 17:14:22 -0800370 state_ = SuperstructureState::IDLE;
371 }
372 break;
373 case NoteGoal::TRAP:
Filip Kujawa7a799602024-02-23 12:27:47 -0800374 extend_roller_status = ExtendRollerStatus::SCORING_IN_TRAP;
Austin Schuh027fd622024-03-01 21:26:07 -0800375 extend_goal_location = ExtendStatus::TRAP;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700376 if (!extend_beambreak && unsafe_goal != nullptr &&
Niko Sohmers5006fc42024-03-01 17:14:22 -0800377 !unsafe_goal->fire()) {
378 state_ = SuperstructureState::IDLE;
379 }
380 break;
381 case NoteGoal::AMP:
382 extend_roller_status = ExtendRollerStatus::SCORING_IN_AMP;
Austin Schuh027fd622024-03-01 21:26:07 -0800383 extend_goal_location = ExtendStatus::AMP;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700384 if (!extend_beambreak && unsafe_goal != nullptr &&
Niko Sohmers5006fc42024-03-01 17:14:22 -0800385 !unsafe_goal->fire()) {
386 state_ = SuperstructureState::IDLE;
387 }
388 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800389 }
390 break;
391 }
392
393 if (unsafe_goal != nullptr &&
394 unsafe_goal->intake_goal() == IntakeGoal::SPIT) {
395 intake_roller_state = IntakeRollerStatus::SPITTING;
396 transfer_roller_status = TransferRollerStatus::TRANSFERING_OUT;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700397 extend_roller_status = ExtendRollerStatus::SCORING_IN_AMP;
Filip Kujawa7a799602024-02-23 12:27:47 -0800398 }
399
400 // Update Intake Roller voltage based on status from state machine.
401 switch (intake_roller_state) {
402 case IntakeRollerStatus::NONE:
403 output_struct.intake_roller_voltage = 0.0;
404 break;
405 case IntakeRollerStatus::SPITTING:
406 output_struct.intake_roller_voltage =
407 robot_constants_->common()->intake_roller_voltages()->spitting();
408 break;
409 case IntakeRollerStatus::INTAKING:
410 output_struct.intake_roller_voltage =
411 robot_constants_->common()->intake_roller_voltages()->intaking();
412 break;
413 }
414
415 // Update Transfer Roller voltage based on status from state machine.
416 switch (transfer_roller_status) {
417 case TransferRollerStatus::NONE:
418 output_struct.transfer_roller_voltage = 0.0;
419 break;
420 case TransferRollerStatus::TRANSFERING_IN:
421 output_struct.transfer_roller_voltage =
422 robot_constants_->common()->transfer_roller_voltages()->transfer_in();
423 break;
424 case TransferRollerStatus::TRANSFERING_OUT:
425 output_struct.transfer_roller_voltage = robot_constants_->common()
426 ->transfer_roller_voltages()
427 ->transfer_out();
428 break;
James Kuszmaul6cae7d72024-03-01 21:29:56 -0800429 case TransferRollerStatus::EXTEND_MOVING:
430 output_struct.transfer_roller_voltage = robot_constants_->common()
431 ->transfer_roller_voltages()
432 ->extend_moving();
433 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800434 }
435
436 // Update Extend Roller voltage based on status from state machine.
437 const ExtendRollerVoltages *extend_roller_voltages =
438 robot_constants_->common()->extend_roller_voltages();
439 switch (extend_roller_status) {
440 case ExtendRollerStatus::IDLE:
441 // No voltage applied when idle
442 output_struct.extend_roller_voltage = 0.0;
443 break;
444 case ExtendRollerStatus::TRANSFERING_TO_EXTEND:
445 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
446 break;
447 case ExtendRollerStatus::SCORING_IN_AMP:
448 [[fallthrough]];
449 case ExtendRollerStatus::SCORING_IN_TRAP:
450 // Apply scoring voltage during scoring in amp or trap
451 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
452 break;
453 case ExtendRollerStatus::TRANSFERING_TO_CATAPULT:
454 // Apply scoring voltage during transferring to catapult
455 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
456 break;
457 }
458
Maxwell Henderson45878502024-03-15 19:35:25 -0700459 if (unsafe_goal != nullptr && unsafe_goal->spit_extend()) {
460 output_struct.extend_roller_voltage = -extend_roller_voltages->scoring();
461 }
462
Austin Schuh027fd622024-03-01 21:26:07 -0800463 double extend_goal_position = 0.0;
Filip Kujawa7a799602024-02-23 12:27:47 -0800464
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700465 // If we request trap, override the extend goal to be trap unless we request
466 // amp.
James Kuszmaul0281e152024-02-26 22:26:16 -0800467 if (unsafe_goal != nullptr && unsafe_goal->note_goal() == NoteGoal::TRAP) {
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700468 trap_override_ = true;
469 }
470
471 if (unsafe_goal != nullptr && unsafe_goal->note_goal() == NoteGoal::AMP &&
472 trap_override_) {
473 trap_override_ = false;
474 requested_note_goal_ = NoteGoal::AMP;
475 state_ = SuperstructureState::READY;
476 }
477
478 if (trap_override_) {
Austin Schuh027fd622024-03-01 21:26:07 -0800479 extend_goal_location = ExtendStatus::TRAP;
James Kuszmaul0281e152024-02-26 22:26:16 -0800480 }
481
Filip Kujawa7a799602024-02-23 12:27:47 -0800482 // Set the extend position based on the state machine output
Austin Schuh027fd622024-03-01 21:26:07 -0800483 switch (extend_goal_location) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800484 case ExtendStatus::RETRACTED:
Austin Schuh027fd622024-03-01 21:26:07 -0800485 extend_goal_position = extend_set_points->retracted();
Filip Kujawa7a799602024-02-23 12:27:47 -0800486 break;
487 case ExtendStatus::AMP:
Austin Schuh027fd622024-03-01 21:26:07 -0800488 extend_goal_position = extend_set_points->amp();
Filip Kujawa7a799602024-02-23 12:27:47 -0800489 break;
490 case ExtendStatus::TRAP:
Austin Schuh027fd622024-03-01 21:26:07 -0800491 extend_goal_position = extend_set_points->trap();
Filip Kujawa7a799602024-02-23 12:27:47 -0800492 break;
493 case ExtendStatus::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800494 extend_goal_position = extend_set_points->catapult();
Filip Kujawa7a799602024-02-23 12:27:47 -0800495 break;
496 case ExtendStatus::MOVING:
497 // Should never happen
498 break;
499 }
500
Niko Sohmers5006fc42024-03-01 17:14:22 -0800501 NoteStatus uncompleted_note_goal_status = NoteStatus::NONE;
502
503 switch (requested_note_goal_) {
504 case NoteGoal::NONE:
505 uncompleted_note_goal_status = NoteStatus::NONE;
506 break;
507 case NoteGoal::CATAPULT:
508 uncompleted_note_goal_status = NoteStatus::CATAPULT;
509 break;
510 case NoteGoal::AMP:
511 uncompleted_note_goal_status = NoteStatus::AMP;
512 break;
513 case NoteGoal::TRAP:
514 uncompleted_note_goal_status = NoteStatus::TRAP;
515 break;
516 }
517
Filip Kujawa7a799602024-02-23 12:27:47 -0800518 // Set the extend status based on the state machine output
519 // If the extend is moving, the status is MOVING, otherwise it is the same
520 // as extend_status
521 ExtendStatus extend_status =
Austin Schuh027fd622024-03-01 21:26:07 -0800522 (extend_moving ? ExtendStatus::MOVING : extend_goal_location);
Filip Kujawa7a799602024-02-23 12:27:47 -0800523
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800524 if (joystick_state_fetcher_.Fetch() &&
525 joystick_state_fetcher_->has_alliance()) {
526 alliance_ = joystick_state_fetcher_->alliance();
527 }
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800528
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800529 drivetrain_status_fetcher_.Fetch();
530
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700531 // Zero out extend position if "disable_extend" is true
Austin Schuh027fd622024-03-01 21:26:07 -0800532 const bool collided = collision_avoidance_.IsCollided({
533 .intake_pivot_position = intake_pivot_.estimated_position(),
534 .turret_position = shooter_.turret().estimated_position(),
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700535 .extend_position = ((!robot_constants_->common()->disable_extend())
536 ? extend_.estimated_position()
537 : 0.0),
Austin Schuh027fd622024-03-01 21:26:07 -0800538 });
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800539
Filip Kujawa6d717632024-02-01 11:40:55 -0800540 aos::FlatbufferFixedAllocatorArray<
541 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
542 climber_goal_buffer;
543
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700544 {
545 flatbuffers::FlatBufferBuilder *climber_fbb = climber_goal_buffer.fbb();
546 flatbuffers::Offset<frc971::ProfileParameters> climber_profile =
547 frc971::CreateProfileParameters(*climber_fbb, climber_velocity,
548 climber_accel);
549
550 climber_goal_buffer.Finish(
551 frc971::control_loops::
552 CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
553 *climber_fbb, climber_position, climber_profile));
554 }
Filip Kujawa6d717632024-02-01 11:40:55 -0800555
556 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
557 *climber_goal = &climber_goal_buffer.message();
558
559 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
560 climber_status_offset = climber_.Iterate(
561 climber_goal, position->climber(),
562 output != nullptr ? &output_struct.climber_voltage : nullptr,
563 status->fbb());
564
Niko Sohmersac4d8872024-02-23 13:55:47 -0800565 double max_intake_pivot_position = 0;
566 double min_intake_pivot_position = 0;
Austin Schuh027fd622024-03-01 21:26:07 -0800567 double max_extend_position = 0;
568 double min_extend_position = 0;
Niko Sohmersac4d8872024-02-23 13:55:47 -0800569
570 aos::FlatbufferFixedAllocatorArray<
571 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
572 intake_pivot_goal_buffer;
573
574 intake_pivot_goal_buffer.Finish(
575 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
576 *intake_pivot_goal_buffer.fbb(), intake_pivot_position));
577
578 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
579 *intake_pivot_goal = &intake_pivot_goal_buffer.message();
580
581 double *intake_output =
582 (output != nullptr ? &output_struct.intake_pivot_voltage : nullptr);
583
584 const bool disabled = intake_pivot_.Correct(
585 intake_pivot_goal, position->intake_pivot(), intake_output == nullptr);
586
Austin Schuh027fd622024-03-01 21:26:07 -0800587 aos::FlatbufferFixedAllocatorArray<
588 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
589 extend_goal_buffer;
590
591 extend_goal_buffer.Finish(
592 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
593 *extend_goal_buffer.fbb(), extend_goal_position));
594
595 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
596 *extend_goal = &extend_goal_buffer.message();
597
Filip Kujawa7a799602024-02-23 12:27:47 -0800598 // TODO(max): Change how we handle the collision with the turret and
599 // intake to be clearer
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800600 const flatbuffers::Offset<ShooterStatus> shooter_status_offset =
601 shooter_.Iterate(
602 position,
603 unsafe_goal != nullptr ? unsafe_goal->shooter_goal() : nullptr,
Filip Kujawa7a799602024-02-23 12:27:47 -0800604 unsafe_goal != nullptr ? unsafe_goal->fire() : false,
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800605 output != nullptr ? &output_struct.catapult_voltage : nullptr,
606 output != nullptr ? &output_struct.altitude_voltage : nullptr,
607 output != nullptr ? &output_struct.turret_voltage : nullptr,
608 output != nullptr ? &output_struct.retention_roller_voltage : nullptr,
Maxwell Hendersond5bf47a2024-02-23 17:16:48 -0800609 output != nullptr
610 ? &output_struct.retention_roller_stator_current_limit
611 : nullptr,
Maxwell Henderson461a8a42024-02-23 17:07:39 -0800612 robot_state().voltage_battery(), &collision_avoidance_,
Austin Schuh027fd622024-03-01 21:26:07 -0800613 extend_goal_position, extend_.estimated_position(),
614 &max_extend_position, &min_extend_position,
Niko Sohmersac4d8872024-02-23 13:55:47 -0800615 intake_pivot_.estimated_position(), &max_intake_pivot_position,
Stephan Pleines9f3983a2024-03-13 20:22:38 -0700616 &min_intake_pivot_position, requested_note_goal_, status->fbb(),
617 timestamp);
Niko Sohmersac4d8872024-02-23 13:55:47 -0800618
619 intake_pivot_.set_min_position(min_intake_pivot_position);
620 intake_pivot_.set_max_position(max_intake_pivot_position);
621
Austin Schuh027fd622024-03-01 21:26:07 -0800622 extend_.set_min_position(min_extend_position);
623 extend_.set_max_position(max_extend_position);
624
Niko Sohmersac4d8872024-02-23 13:55:47 -0800625 // Calculate the loops for a cycle.
626 const double voltage = intake_pivot_.UpdateController(disabled);
627
628 intake_pivot_.UpdateObserver(voltage);
629
630 // Write out all the voltages.
631 if (intake_output) {
632 *intake_output = voltage;
633 }
634
635 const flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus>
636 intake_pivot_status_offset = intake_pivot_.MakeStatus(status->fbb());
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800637
Filip Kujawa7a799602024-02-23 12:27:47 -0800638 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
639 extend_status_offset = extend_.Iterate(
Austin Schuh027fd622024-03-01 21:26:07 -0800640 extend_goal, position->extend(),
Filip Kujawa7a799602024-02-23 12:27:47 -0800641 output != nullptr ? &output_struct.extend_voltage : nullptr,
642 status->fbb());
643
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700644 // Zero out extend voltage if "disable_extend" is true
645 if (robot_constants_->common()->disable_extend()) {
646 output_struct.extend_voltage = 0.0;
647 }
648
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800649 if (output) {
650 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
651 }
652
653 Status::Builder status_builder = status->MakeBuilder<Status>();
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800654
Filip Kujawa7a799602024-02-23 12:27:47 -0800655 const bool zeroed = intake_pivot_.zeroed() && climber_.zeroed() &&
656 shooter_.zeroed() && extend_.zeroed();
657 const bool estopped = intake_pivot_.estopped() || climber_.estopped() ||
658 shooter_.estopped() || extend_.estopped();
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800659
660 status_builder.add_zeroed(zeroed);
661 status_builder.add_estopped(estopped);
Filip Kujawa102a9b22024-02-18 09:40:23 -0800662 status_builder.add_intake_roller(intake_roller_state);
663 status_builder.add_intake_pivot(intake_pivot_status_offset);
Filip Kujawa7a799602024-02-23 12:27:47 -0800664 status_builder.add_transfer_roller(transfer_roller_status);
Filip Kujawa102a9b22024-02-18 09:40:23 -0800665 status_builder.add_climber(climber_status_offset);
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800666 status_builder.add_shooter(shooter_status_offset);
Niko Sohmersac4d8872024-02-23 13:55:47 -0800667 status_builder.add_collided(collided);
Filip Kujawa7a799602024-02-23 12:27:47 -0800668 status_builder.add_extend_roller(extend_roller_status);
669 status_builder.add_extend_status(extend_status);
670 status_builder.add_extend(extend_status_offset);
671 status_builder.add_state(state_);
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700672 status_builder.add_shot_count(shooter_.shot_count());
Niko Sohmers5006fc42024-03-01 17:14:22 -0800673 status_builder.add_uncompleted_note_goal(uncompleted_note_goal_status);
James Kuszmaul0281e152024-02-26 22:26:16 -0800674 status_builder.add_extend_ready_for_transfer(extend_at_retracted);
Niko Sohmers2a251cd2024-03-02 19:16:15 -0800675 status_builder.add_extend_at_retracted(extend_at_retracted);
676 status_builder.add_turret_ready_for_load(turret_ready_for_load);
677 status_builder.add_altitude_ready_for_load(altitude_ready_for_load);
678 status_builder.add_extend_ready_for_catapult_transfer(
679 extend_ready_for_catapult_transfer);
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700680 status_builder.add_extend_beambreak(extend_beambreak);
Niko Sohmers2a251cd2024-03-02 19:16:15 -0800681 status_builder.add_catapult_beambreak(position->catapult_beambreak());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800682
683 (void)status->Send(status_builder.Finish());
684}
685
686double Superstructure::robot_velocity() const {
687 return (drivetrain_status_fetcher_.get() != nullptr
688 ? drivetrain_status_fetcher_->robot_speed()
689 : 0.0);
690}
691
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800692} // namespace y2024::control_loops::superstructure