blob: af3f9de9bf0e1b253a874bd44cd8db1f816e321f [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),
Filip Kujawa0a1282b2024-03-23 12:36:51 -070054 std::chrono::milliseconds(8)),
55 transfer_debouncer_(std::chrono::milliseconds(30),
56 std::chrono::milliseconds(8)) {
57 event_loop->SetRuntimeRealtimePriority(30);
Niko Sohmers3860f8a2024-01-12 21:05:19 -080058}
59
Filip Kujawa7a799602024-02-23 12:27:47 -080060bool PositionNear(double position, double goal, double threshold) {
61 return std::abs(position - goal) < threshold;
62}
63
Niko Sohmers3860f8a2024-01-12 21:05:19 -080064void Superstructure::RunIteration(const Goal *unsafe_goal,
65 const Position *position,
66 aos::Sender<Output>::Builder *output,
67 aos::Sender<Status>::Builder *status) {
68 const monotonic_clock::time_point timestamp =
69 event_loop()->context().monotonic_event_time;
70
Niko Sohmers3860f8a2024-01-12 21:05:19 -080071 if (WasReset()) {
72 AOS_LOG(ERROR, "WPILib reset, restarting\n");
Niko Sohmersafc51fe2024-01-29 17:48:35 -080073 intake_pivot_.Reset();
Filip Kujawa6d717632024-02-01 11:40:55 -080074 climber_.Reset();
Niko Sohmersc4d2c502024-02-19 19:35:35 -080075 shooter_.Reset();
Filip Kujawa7a799602024-02-23 12:27:47 -080076 extend_.Reset();
Niko Sohmers3860f8a2024-01-12 21:05:19 -080077 }
78
79 OutputT output_struct;
Niko Sohmersafc51fe2024-01-29 17:48:35 -080080
Niko Sohmers6adb5b92024-03-16 17:47:54 -070081 extend_debouncer_.Update(position->extend_beambreak(), timestamp);
82 const bool extend_beambreak = extend_debouncer_.state();
83
Filip Kujawa0a1282b2024-03-23 12:36:51 -070084 transfer_debouncer_.Update(position->transfer_beambreak(), timestamp);
85 const bool transfer_beambreak = transfer_debouncer_.state();
86
Filip Kujawa7a799602024-02-23 12:27:47 -080087 // Handle Climber Goal separately from main superstructure state machine
Filip Kujawa6d717632024-02-01 11:40:55 -080088 double climber_position =
89 robot_constants_->common()->climber_set_points()->retract();
90
Niko Sohmers6adb5b92024-03-16 17:47:54 -070091 double climber_velocity = robot_constants_->common()
92 ->climber()
93 ->default_profile_params()
94 ->max_velocity();
95 const double climber_accel = robot_constants_->common()
96 ->climber()
97 ->default_profile_params()
98 ->max_acceleration();
99
Filip Kujawa6d717632024-02-01 11:40:55 -0800100 if (unsafe_goal != nullptr) {
101 switch (unsafe_goal->climber_goal()) {
102 case ClimberGoal::FULL_EXTEND:
103 climber_position =
104 robot_constants_->common()->climber_set_points()->full_extend();
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700105 // The climber can go reasonably fast when extending out.
106 climber_velocity = 0.5;
107
108 if (unsafe_goal->slow_climber()) {
109 climber_velocity = 0.01;
110 }
Filip Kujawa6d717632024-02-01 11:40:55 -0800111 break;
Filip Kujawa6d717632024-02-01 11:40:55 -0800112 case ClimberGoal::RETRACT:
113 climber_position =
114 robot_constants_->common()->climber_set_points()->retract();
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700115 // Keep the climber slower while retracting.
116 climber_velocity = 0.1;
Filip Kujawa6d717632024-02-01 11:40:55 -0800117 break;
Maxwell Henderson7db29782024-02-24 20:10:26 -0800118 case ClimberGoal::STOWED:
119 climber_position =
120 robot_constants_->common()->climber_set_points()->stowed();
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700121 // Keep the climber slower while retracting.
122 climber_velocity = 0.1;
Filip Kujawa6d717632024-02-01 11:40:55 -0800123 }
124 }
125
Filip Kujawa7a799602024-02-23 12:27:47 -0800126 // If we started off preloaded, skip to the ready state.
127 if (unsafe_goal != nullptr && unsafe_goal->shooter_goal() &&
128 unsafe_goal->shooter_goal()->preloaded()) {
129 if (state_ != SuperstructureState::READY &&
130 state_ != SuperstructureState::FIRING) {
131 state_ = SuperstructureState::READY;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800132 requested_note_goal_ = NoteGoal::CATAPULT;
Filip Kujawa7a799602024-02-23 12:27:47 -0800133 }
134 }
135
136 // Handle the intake pivot goal separately from the main superstructure state
137 IntakeRollerStatus intake_roller_state = IntakeRollerStatus::NONE;
138 double intake_pivot_position =
139 robot_constants_->common()->intake_pivot_set_points()->retracted();
140
141 if (unsafe_goal != nullptr) {
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700142 switch (unsafe_goal->intake_pivot()) {
143 case IntakePivotGoal::DOWN:
Filip Kujawa7a799602024-02-23 12:27:47 -0800144 intake_pivot_position =
145 robot_constants_->common()->intake_pivot_set_points()->extended();
Maxwell Hendersonf0d22622024-02-26 21:02:11 -0800146 intake_end_time_ = timestamp;
Filip Kujawa7a799602024-02-23 12:27:47 -0800147 break;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700148 case IntakePivotGoal::UP:
Filip Kujawa7a799602024-02-23 12:27:47 -0800149 intake_pivot_position =
150 robot_constants_->common()->intake_pivot_set_points()->retracted();
151 break;
152 }
153 }
154
155 ExtendRollerStatus extend_roller_status = ExtendRollerStatus::IDLE;
Austin Schuh027fd622024-03-01 21:26:07 -0800156 ExtendStatus extend_goal_location = ExtendStatus::RETRACTED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800157
158 // True if the extend is moving towards a goal
159 bool extend_moving = false;
160
161 TransferRollerStatus transfer_roller_status = TransferRollerStatus::NONE;
162
163 const ExtendSetPoints *extend_set_points =
164 robot_constants_->common()->extend_set_points();
165
166 // Checks if the extend is close enough to the retracted position to be
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700167 // considered ready to accept note from the transfer rollers. If disable
168 // extend is triggered, this will autoatically be false.
169 const bool extend_at_retracted =
170 (!robot_constants_->common()->disable_extend() &&
171 PositionNear(extend_.position(), extend_set_points->retracted(),
172 kExtendThreshold));
Filip Kujawa7a799602024-02-23 12:27:47 -0800173
Niko Sohmers5006fc42024-03-01 17:14:22 -0800174 // Check if the turret is at the position to accept the note from extend
175 const bool turret_ready_for_load =
176 PositionNear(shooter_.turret().estimated_position(),
177 robot_constants_->common()->turret_loading_position(),
178 kTurretLoadingThreshold);
179
Niko Sohmers5006fc42024-03-01 17:14:22 -0800180 // Check if the altitude is at the position to accept the note from
181 // extend
182 const bool altitude_ready_for_load =
183 PositionNear(shooter_.altitude().estimated_position(),
184 robot_constants_->common()->altitude_loading_position(),
185 kAltitudeLoadingThreshold);
186
187 // Check if the extend is at the position to load the catapult
188 const bool extend_ready_for_catapult_transfer = PositionNear(
189 extend_.position(), extend_set_points->catapult(), kExtendThreshold);
190
Niko Sohmers5006fc42024-03-01 17:14:22 -0800191 // Only update the reuested note goal to the first goal that is requested by
192 // the manipulator
193 if (unsafe_goal != nullptr && unsafe_goal->note_goal() != NoteGoal::NONE &&
194 requested_note_goal_ == NoteGoal::NONE) {
195 requested_note_goal_ = unsafe_goal->note_goal();
196 }
197
Filip Kujawa7a799602024-02-23 12:27:47 -0800198 // Superstructure state machine:
199 // 1. IDLE. The intake is retracted and there is no note in the robot.
200 // Wait for a intake goal to switch state to INTAKING if the extend is ready
201 // 2. INTAKING. Intake the note and transfer it towards the extend.
202 // Give intake, transfer, and extend rollers positive voltage to intake and
203 // transfer. Switch to LOADED when the extend beambreak is triggered.
204 // 3. LOADED. The note is in the extend and the extend is retracted.
205 // Wait for a note goal to switch state to MOVING.
206 // For AMP/TRAP goals, check that the turret is in a position to avoid
207 // collision.
208 // 4. MOVING. The extend is moving towards a goal (AMP, TRAP, or CATAPULT).
209 // For CATAPULT goals, wait for the turret and altitude to be in a position to
210 // accept the note from the extend.
211 // Wait for the extend to reach the goal and switch state to READY if
212 // AMP or TRAP, or to LOADING_CATAPULT if CATAPULT.
213 // 5. LOADING_CATAPULT. The extend is at the position to load the catapult.
214 // Activate the extend roller to transfer the note to the catapult.
215 // Switch state to READY when the catapult beambreak is triggered.
216 // 6. READY. Ready for fire command. The note is either loaded in the catapult
217 // or in the extend and at the AMP or TRAP position. Wait for a fire command.
218 // 7. FIRING. The note is being fired, either from the extend or the catapult.
219 // Switch state back to IDLE when the note is fired.
220
221 switch (state_) {
222 case SuperstructureState::IDLE:
Niko Sohmers5006fc42024-03-01 17:14:22 -0800223 requested_note_goal_ = NoteGoal::NONE;
224
Filip Kujawa7a799602024-02-23 12:27:47 -0800225 if (unsafe_goal != nullptr &&
226 unsafe_goal->intake_goal() == IntakeGoal::INTAKE &&
Niko Sohmersdd971dc2024-02-25 11:40:47 -0800227 extend_at_retracted) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800228 state_ = SuperstructureState::INTAKING;
Filip Kujawa0a1282b2024-03-23 12:36:51 -0700229 note_in_transfer_ = false;
Filip Kujawa7a799602024-02-23 12:27:47 -0800230 }
Maxwell Henderson4567e282024-02-25 15:38:15 -0800231
Austin Schuh027fd622024-03-01 21:26:07 -0800232 extend_goal_location = ExtendStatus::RETRACTED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800233 break;
234 case SuperstructureState::INTAKING:
Filip Kujawa7a799602024-02-23 12:27:47 -0800235 // Switch to LOADED state when the extend beambreak is triggered
236 // meaning the note is loaded in the extend
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700237 if (extend_beambreak) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800238 state_ = SuperstructureState::LOADED;
239 }
Filip Kujawa0a1282b2024-03-23 12:36:51 -0700240
241 if (transfer_beambreak) {
242 note_in_transfer_ = true;
243 }
244
245 // Once the note is in the transfer, stop the intake rollers
246 if (note_in_transfer_) {
247 intake_roller_state = IntakeRollerStatus::NONE;
248 } else {
249 intake_roller_state = IntakeRollerStatus::INTAKING;
250 }
251
Filip Kujawa7a799602024-02-23 12:27:47 -0800252 transfer_roller_status = TransferRollerStatus::TRANSFERING_IN;
253 extend_roller_status = ExtendRollerStatus::TRANSFERING_TO_EXTEND;
Austin Schuh027fd622024-03-01 21:26:07 -0800254 extend_goal_location = ExtendStatus::RETRACTED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800255
Maxwell Hendersonf0d22622024-02-26 21:02:11 -0800256 // If we are no longer requesting INTAKE or we are no longer requesting
257 // an INTAKE goal, wait 0.5 seconds then go back to IDLE.
258 if (!(unsafe_goal != nullptr &&
259 unsafe_goal->intake_goal() == IntakeGoal::INTAKE) &&
260 timestamp > intake_end_time_ + kExtraIntakingTime) {
261 state_ = SuperstructureState::IDLE;
262 }
263
Filip Kujawa7a799602024-02-23 12:27:47 -0800264 break;
265 case SuperstructureState::LOADED:
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700266 if (!extend_beambreak && !position->catapult_beambreak()) {
Maxwell Henderson4567e282024-02-25 15:38:15 -0800267 state_ = SuperstructureState::IDLE;
268 }
269
Niko Sohmers5006fc42024-03-01 17:14:22 -0800270 switch (requested_note_goal_) {
271 case NoteGoal::NONE:
272 break;
273 case NoteGoal::CATAPULT:
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700274 state_ = SuperstructureState::LOADING_CATAPULT;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800275 transfer_roller_status = TransferRollerStatus::TRANSFERING_IN;
276 break;
277 case NoteGoal::TRAP:
278 [[fallthrough]];
279 case NoteGoal::AMP:
James Kuszmaul6cae7d72024-03-01 21:29:56 -0800280 transfer_roller_status = TransferRollerStatus::EXTEND_MOVING;
Austin Schuh027fd622024-03-01 21:26:07 -0800281 state_ = SuperstructureState::MOVING;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800282 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800283 }
Austin Schuh027fd622024-03-01 21:26:07 -0800284 extend_goal_location = ExtendStatus::RETRACTED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800285 break;
286 case SuperstructureState::MOVING:
James Kuszmaul6cae7d72024-03-01 21:29:56 -0800287 transfer_roller_status = TransferRollerStatus::EXTEND_MOVING;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800288 switch (requested_note_goal_) {
289 case NoteGoal::NONE:
Austin Schuh027fd622024-03-01 21:26:07 -0800290 extend_goal_location = ExtendStatus::RETRACTED;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800291 if (extend_at_retracted) {
292 state_ = SuperstructureState::LOADED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800293 }
Niko Sohmers5006fc42024-03-01 17:14:22 -0800294 break;
295 case NoteGoal::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800296 extend_goal_location = ExtendStatus::CATAPULT;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800297 if (extend_ready_for_catapult_transfer && turret_ready_for_load &&
298 altitude_ready_for_load) {
299 state_ = SuperstructureState::LOADING_CATAPULT;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700300 loading_catapult_start_time_ = timestamp;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800301 }
302 break;
303 case NoteGoal::TRAP:
Austin Schuh027fd622024-03-01 21:26:07 -0800304 extend_goal_location = ExtendStatus::TRAP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800305 // Check if the extend is at the TRAP position and if it is
306 // switch to READY state
307 if (PositionNear(extend_.position(), extend_set_points->trap(),
308 kExtendThreshold)) {
309 state_ = SuperstructureState::READY;
310 }
311 break;
312 case NoteGoal::AMP:
Austin Schuh027fd622024-03-01 21:26:07 -0800313 extend_goal_location = ExtendStatus::AMP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800314 // Check if the extend is at the AMP position and if it is
315 // switch to READY state
316 if (PositionNear(extend_.position(), extend_set_points->amp(),
317 kExtendThreshold)) {
318 state_ = SuperstructureState::READY;
319 }
320 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800321 }
322
323 extend_moving = true;
324 break;
325 case SuperstructureState::LOADING_CATAPULT:
326 extend_moving = false;
Austin Schuh027fd622024-03-01 21:26:07 -0800327 extend_goal_location = ExtendStatus::CATAPULT;
Filip Kujawa7a799602024-02-23 12:27:47 -0800328
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700329 if (extend_beambreak) {
330 loading_catapult_start_time_ = timestamp;
331 }
332
333 if (loading_catapult_start_time_ + std::chrono::seconds(10) < timestamp) {
Filip Kujawa77e3d8a2024-03-02 11:34:56 -0800334 state_ = SuperstructureState::IDLE;
335 }
336
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700337 if (turret_ready_for_load && altitude_ready_for_load) {
338 extend_roller_status = ExtendRollerStatus::TRANSFERING_TO_CATAPULT;
339 transfer_roller_status = TransferRollerStatus::EXTEND_MOVING;
340 }
341
Filip Kujawa7a799602024-02-23 12:27:47 -0800342 // Switch to READY state when the catapult beambreak is triggered
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700343 if (shooter_.loaded()) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800344 state_ = SuperstructureState::READY;
345 }
346 break;
347 case SuperstructureState::READY:
348 extend_moving = false;
349
350 // Switch to FIRING state when the fire button is pressed
351 if (unsafe_goal != nullptr && unsafe_goal->fire()) {
352 state_ = SuperstructureState::FIRING;
353 }
354
Niko Sohmers5006fc42024-03-01 17:14:22 -0800355 switch (requested_note_goal_) {
356 case NoteGoal::NONE:
Austin Schuh027fd622024-03-01 21:26:07 -0800357 extend_goal_location = ExtendStatus::RETRACTED;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800358 extend_moving = true;
359 state_ = SuperstructureState::MOVING;
360 break;
361 case NoteGoal::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800362 extend_goal_location = ExtendStatus::CATAPULT;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700363
364 if (!shooter_.loaded()) {
365 state_ = SuperstructureState::LOADING_CATAPULT;
366 }
Niko Sohmers5006fc42024-03-01 17:14:22 -0800367 break;
368 case NoteGoal::TRAP:
Austin Schuh027fd622024-03-01 21:26:07 -0800369 extend_goal_location = ExtendStatus::TRAP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800370 break;
371 case NoteGoal::AMP:
Austin Schuh027fd622024-03-01 21:26:07 -0800372 extend_goal_location = ExtendStatus::AMP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800373 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800374 }
Filip Kujawa7a799602024-02-23 12:27:47 -0800375 break;
376 case SuperstructureState::FIRING:
Niko Sohmers5006fc42024-03-01 17:14:22 -0800377 switch (requested_note_goal_) {
378 case NoteGoal::NONE:
Filip Kujawa7a799602024-02-23 12:27:47 -0800379
Niko Sohmers5006fc42024-03-01 17:14:22 -0800380 break;
381 case NoteGoal::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800382 extend_goal_location = ExtendStatus::CATAPULT;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800383 // Reset the state to IDLE when the game piece is fired from the
384 // catapult. We consider the game piece to be fired from the catapult
385 // when the catapultbeambreak is no longer triggered.
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700386 if (!shooter_.loaded() && !shooter_.Firing()) {
Niko Sohmers5006fc42024-03-01 17:14:22 -0800387 state_ = SuperstructureState::IDLE;
388 }
389 break;
390 case NoteGoal::TRAP:
Filip Kujawa7a799602024-02-23 12:27:47 -0800391 extend_roller_status = ExtendRollerStatus::SCORING_IN_TRAP;
Austin Schuh027fd622024-03-01 21:26:07 -0800392 extend_goal_location = ExtendStatus::TRAP;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700393 if (!extend_beambreak && unsafe_goal != nullptr &&
Niko Sohmers5006fc42024-03-01 17:14:22 -0800394 !unsafe_goal->fire()) {
395 state_ = SuperstructureState::IDLE;
396 }
397 break;
398 case NoteGoal::AMP:
399 extend_roller_status = ExtendRollerStatus::SCORING_IN_AMP;
Austin Schuh027fd622024-03-01 21:26:07 -0800400 extend_goal_location = ExtendStatus::AMP;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700401 if (!extend_beambreak && unsafe_goal != nullptr &&
Niko Sohmers5006fc42024-03-01 17:14:22 -0800402 !unsafe_goal->fire()) {
403 state_ = SuperstructureState::IDLE;
404 }
405 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800406 }
407 break;
408 }
409
410 if (unsafe_goal != nullptr &&
411 unsafe_goal->intake_goal() == IntakeGoal::SPIT) {
412 intake_roller_state = IntakeRollerStatus::SPITTING;
413 transfer_roller_status = TransferRollerStatus::TRANSFERING_OUT;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700414 extend_roller_status = ExtendRollerStatus::SCORING_IN_AMP;
Filip Kujawa7a799602024-02-23 12:27:47 -0800415 }
416
417 // Update Intake Roller voltage based on status from state machine.
418 switch (intake_roller_state) {
419 case IntakeRollerStatus::NONE:
420 output_struct.intake_roller_voltage = 0.0;
421 break;
422 case IntakeRollerStatus::SPITTING:
423 output_struct.intake_roller_voltage =
424 robot_constants_->common()->intake_roller_voltages()->spitting();
425 break;
426 case IntakeRollerStatus::INTAKING:
427 output_struct.intake_roller_voltage =
428 robot_constants_->common()->intake_roller_voltages()->intaking();
429 break;
430 }
431
432 // Update Transfer Roller voltage based on status from state machine.
433 switch (transfer_roller_status) {
434 case TransferRollerStatus::NONE:
435 output_struct.transfer_roller_voltage = 0.0;
436 break;
437 case TransferRollerStatus::TRANSFERING_IN:
438 output_struct.transfer_roller_voltage =
439 robot_constants_->common()->transfer_roller_voltages()->transfer_in();
440 break;
441 case TransferRollerStatus::TRANSFERING_OUT:
442 output_struct.transfer_roller_voltage = robot_constants_->common()
443 ->transfer_roller_voltages()
444 ->transfer_out();
445 break;
James Kuszmaul6cae7d72024-03-01 21:29:56 -0800446 case TransferRollerStatus::EXTEND_MOVING:
447 output_struct.transfer_roller_voltage = robot_constants_->common()
448 ->transfer_roller_voltages()
449 ->extend_moving();
450 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800451 }
452
453 // Update Extend Roller voltage based on status from state machine.
454 const ExtendRollerVoltages *extend_roller_voltages =
455 robot_constants_->common()->extend_roller_voltages();
456 switch (extend_roller_status) {
457 case ExtendRollerStatus::IDLE:
458 // No voltage applied when idle
459 output_struct.extend_roller_voltage = 0.0;
460 break;
461 case ExtendRollerStatus::TRANSFERING_TO_EXTEND:
462 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
463 break;
464 case ExtendRollerStatus::SCORING_IN_AMP:
465 [[fallthrough]];
466 case ExtendRollerStatus::SCORING_IN_TRAP:
467 // Apply scoring voltage during scoring in amp or trap
468 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
469 break;
470 case ExtendRollerStatus::TRANSFERING_TO_CATAPULT:
471 // Apply scoring voltage during transferring to catapult
472 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
473 break;
474 }
475
Maxwell Henderson45878502024-03-15 19:35:25 -0700476 if (unsafe_goal != nullptr && unsafe_goal->spit_extend()) {
477 output_struct.extend_roller_voltage = -extend_roller_voltages->scoring();
478 }
479
Austin Schuh027fd622024-03-01 21:26:07 -0800480 double extend_goal_position = 0.0;
Filip Kujawa7a799602024-02-23 12:27:47 -0800481
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700482 // If we request trap, override the extend goal to be trap unless we request
483 // amp.
James Kuszmaul0281e152024-02-26 22:26:16 -0800484 if (unsafe_goal != nullptr && unsafe_goal->note_goal() == NoteGoal::TRAP) {
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700485 trap_override_ = true;
486 }
487
488 if (unsafe_goal != nullptr && unsafe_goal->note_goal() == NoteGoal::AMP &&
489 trap_override_) {
490 trap_override_ = false;
491 requested_note_goal_ = NoteGoal::AMP;
492 state_ = SuperstructureState::READY;
493 }
494
495 if (trap_override_) {
Austin Schuh027fd622024-03-01 21:26:07 -0800496 extend_goal_location = ExtendStatus::TRAP;
James Kuszmaul0281e152024-02-26 22:26:16 -0800497 }
498
Filip Kujawa7a799602024-02-23 12:27:47 -0800499 // Set the extend position based on the state machine output
Austin Schuh027fd622024-03-01 21:26:07 -0800500 switch (extend_goal_location) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800501 case ExtendStatus::RETRACTED:
Austin Schuh027fd622024-03-01 21:26:07 -0800502 extend_goal_position = extend_set_points->retracted();
Filip Kujawa7a799602024-02-23 12:27:47 -0800503 break;
504 case ExtendStatus::AMP:
Austin Schuh027fd622024-03-01 21:26:07 -0800505 extend_goal_position = extend_set_points->amp();
Filip Kujawa7a799602024-02-23 12:27:47 -0800506 break;
507 case ExtendStatus::TRAP:
Austin Schuh027fd622024-03-01 21:26:07 -0800508 extend_goal_position = extend_set_points->trap();
Filip Kujawa7a799602024-02-23 12:27:47 -0800509 break;
510 case ExtendStatus::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800511 extend_goal_position = extend_set_points->catapult();
Filip Kujawa7a799602024-02-23 12:27:47 -0800512 break;
513 case ExtendStatus::MOVING:
514 // Should never happen
515 break;
516 }
517
Niko Sohmers5006fc42024-03-01 17:14:22 -0800518 NoteStatus uncompleted_note_goal_status = NoteStatus::NONE;
519
520 switch (requested_note_goal_) {
521 case NoteGoal::NONE:
522 uncompleted_note_goal_status = NoteStatus::NONE;
523 break;
524 case NoteGoal::CATAPULT:
525 uncompleted_note_goal_status = NoteStatus::CATAPULT;
526 break;
527 case NoteGoal::AMP:
528 uncompleted_note_goal_status = NoteStatus::AMP;
529 break;
530 case NoteGoal::TRAP:
531 uncompleted_note_goal_status = NoteStatus::TRAP;
532 break;
533 }
534
Filip Kujawa7a799602024-02-23 12:27:47 -0800535 // Set the extend status based on the state machine output
536 // If the extend is moving, the status is MOVING, otherwise it is the same
537 // as extend_status
538 ExtendStatus extend_status =
Austin Schuh027fd622024-03-01 21:26:07 -0800539 (extend_moving ? ExtendStatus::MOVING : extend_goal_location);
Filip Kujawa7a799602024-02-23 12:27:47 -0800540
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800541 if (joystick_state_fetcher_.Fetch() &&
542 joystick_state_fetcher_->has_alliance()) {
543 alliance_ = joystick_state_fetcher_->alliance();
544 }
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800545
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800546 drivetrain_status_fetcher_.Fetch();
547
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700548 // Zero out extend position if "disable_extend" is true
Austin Schuh027fd622024-03-01 21:26:07 -0800549 const bool collided = collision_avoidance_.IsCollided({
550 .intake_pivot_position = intake_pivot_.estimated_position(),
551 .turret_position = shooter_.turret().estimated_position(),
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700552 .extend_position = ((!robot_constants_->common()->disable_extend())
553 ? extend_.estimated_position()
554 : 0.0),
Austin Schuh027fd622024-03-01 21:26:07 -0800555 });
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800556
Filip Kujawa6d717632024-02-01 11:40:55 -0800557 aos::FlatbufferFixedAllocatorArray<
558 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
559 climber_goal_buffer;
560
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700561 {
562 flatbuffers::FlatBufferBuilder *climber_fbb = climber_goal_buffer.fbb();
563 flatbuffers::Offset<frc971::ProfileParameters> climber_profile =
564 frc971::CreateProfileParameters(*climber_fbb, climber_velocity,
565 climber_accel);
566
567 climber_goal_buffer.Finish(
568 frc971::control_loops::
569 CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
570 *climber_fbb, climber_position, climber_profile));
571 }
Filip Kujawa6d717632024-02-01 11:40:55 -0800572
573 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
574 *climber_goal = &climber_goal_buffer.message();
575
576 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
577 climber_status_offset = climber_.Iterate(
578 climber_goal, position->climber(),
579 output != nullptr ? &output_struct.climber_voltage : nullptr,
580 status->fbb());
581
Niko Sohmersac4d8872024-02-23 13:55:47 -0800582 double max_intake_pivot_position = 0;
583 double min_intake_pivot_position = 0;
Austin Schuh027fd622024-03-01 21:26:07 -0800584 double max_extend_position = 0;
585 double min_extend_position = 0;
Niko Sohmersac4d8872024-02-23 13:55:47 -0800586
587 aos::FlatbufferFixedAllocatorArray<
588 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
589 intake_pivot_goal_buffer;
590
591 intake_pivot_goal_buffer.Finish(
592 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
593 *intake_pivot_goal_buffer.fbb(), intake_pivot_position));
594
595 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
596 *intake_pivot_goal = &intake_pivot_goal_buffer.message();
597
598 double *intake_output =
599 (output != nullptr ? &output_struct.intake_pivot_voltage : nullptr);
600
601 const bool disabled = intake_pivot_.Correct(
602 intake_pivot_goal, position->intake_pivot(), intake_output == nullptr);
603
Austin Schuh027fd622024-03-01 21:26:07 -0800604 aos::FlatbufferFixedAllocatorArray<
605 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
606 extend_goal_buffer;
607
608 extend_goal_buffer.Finish(
609 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
610 *extend_goal_buffer.fbb(), extend_goal_position));
611
612 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
613 *extend_goal = &extend_goal_buffer.message();
614
Filip Kujawa7a799602024-02-23 12:27:47 -0800615 // TODO(max): Change how we handle the collision with the turret and
616 // intake to be clearer
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800617 const flatbuffers::Offset<ShooterStatus> shooter_status_offset =
618 shooter_.Iterate(
619 position,
620 unsafe_goal != nullptr ? unsafe_goal->shooter_goal() : nullptr,
Filip Kujawa7a799602024-02-23 12:27:47 -0800621 unsafe_goal != nullptr ? unsafe_goal->fire() : false,
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800622 output != nullptr ? &output_struct.catapult_voltage : nullptr,
623 output != nullptr ? &output_struct.altitude_voltage : nullptr,
624 output != nullptr ? &output_struct.turret_voltage : nullptr,
625 output != nullptr ? &output_struct.retention_roller_voltage : nullptr,
Maxwell Hendersond5bf47a2024-02-23 17:16:48 -0800626 output != nullptr
627 ? &output_struct.retention_roller_stator_current_limit
628 : nullptr,
Maxwell Henderson461a8a42024-02-23 17:07:39 -0800629 robot_state().voltage_battery(), &collision_avoidance_,
Austin Schuh027fd622024-03-01 21:26:07 -0800630 extend_goal_position, extend_.estimated_position(),
631 &max_extend_position, &min_extend_position,
Niko Sohmersac4d8872024-02-23 13:55:47 -0800632 intake_pivot_.estimated_position(), &max_intake_pivot_position,
Stephan Pleines9f3983a2024-03-13 20:22:38 -0700633 &min_intake_pivot_position, requested_note_goal_, status->fbb(),
634 timestamp);
Niko Sohmersac4d8872024-02-23 13:55:47 -0800635
636 intake_pivot_.set_min_position(min_intake_pivot_position);
637 intake_pivot_.set_max_position(max_intake_pivot_position);
638
Austin Schuh027fd622024-03-01 21:26:07 -0800639 extend_.set_min_position(min_extend_position);
640 extend_.set_max_position(max_extend_position);
641
Niko Sohmersac4d8872024-02-23 13:55:47 -0800642 // Calculate the loops for a cycle.
643 const double voltage = intake_pivot_.UpdateController(disabled);
644
645 intake_pivot_.UpdateObserver(voltage);
646
647 // Write out all the voltages.
648 if (intake_output) {
649 *intake_output = voltage;
650 }
651
652 const flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus>
653 intake_pivot_status_offset = intake_pivot_.MakeStatus(status->fbb());
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800654
Filip Kujawa7a799602024-02-23 12:27:47 -0800655 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
656 extend_status_offset = extend_.Iterate(
Austin Schuh027fd622024-03-01 21:26:07 -0800657 extend_goal, position->extend(),
Filip Kujawa7a799602024-02-23 12:27:47 -0800658 output != nullptr ? &output_struct.extend_voltage : nullptr,
659 status->fbb());
660
Niko Sohmers77bf1fd2024-03-16 23:22:57 -0700661 // Zero out extend voltage if "disable_extend" is true
662 if (robot_constants_->common()->disable_extend()) {
663 output_struct.extend_voltage = 0.0;
664 }
665
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800666 if (output) {
667 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
668 }
669
670 Status::Builder status_builder = status->MakeBuilder<Status>();
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800671
Filip Kujawa7a799602024-02-23 12:27:47 -0800672 const bool zeroed = intake_pivot_.zeroed() && climber_.zeroed() &&
673 shooter_.zeroed() && extend_.zeroed();
674 const bool estopped = intake_pivot_.estopped() || climber_.estopped() ||
675 shooter_.estopped() || extend_.estopped();
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800676
677 status_builder.add_zeroed(zeroed);
678 status_builder.add_estopped(estopped);
Filip Kujawa102a9b22024-02-18 09:40:23 -0800679 status_builder.add_intake_roller(intake_roller_state);
680 status_builder.add_intake_pivot(intake_pivot_status_offset);
Filip Kujawa7a799602024-02-23 12:27:47 -0800681 status_builder.add_transfer_roller(transfer_roller_status);
Filip Kujawa102a9b22024-02-18 09:40:23 -0800682 status_builder.add_climber(climber_status_offset);
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800683 status_builder.add_shooter(shooter_status_offset);
Niko Sohmersac4d8872024-02-23 13:55:47 -0800684 status_builder.add_collided(collided);
Filip Kujawa7a799602024-02-23 12:27:47 -0800685 status_builder.add_extend_roller(extend_roller_status);
686 status_builder.add_extend_status(extend_status);
687 status_builder.add_extend(extend_status_offset);
688 status_builder.add_state(state_);
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700689 status_builder.add_shot_count(shooter_.shot_count());
Niko Sohmers5006fc42024-03-01 17:14:22 -0800690 status_builder.add_uncompleted_note_goal(uncompleted_note_goal_status);
James Kuszmaul0281e152024-02-26 22:26:16 -0800691 status_builder.add_extend_ready_for_transfer(extend_at_retracted);
Niko Sohmers2a251cd2024-03-02 19:16:15 -0800692 status_builder.add_extend_at_retracted(extend_at_retracted);
693 status_builder.add_turret_ready_for_load(turret_ready_for_load);
694 status_builder.add_altitude_ready_for_load(altitude_ready_for_load);
695 status_builder.add_extend_ready_for_catapult_transfer(
696 extend_ready_for_catapult_transfer);
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700697 status_builder.add_extend_beambreak(extend_beambreak);
Niko Sohmers2a251cd2024-03-02 19:16:15 -0800698 status_builder.add_catapult_beambreak(position->catapult_beambreak());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800699
700 (void)status->Send(status_builder.Finish());
701}
702
703double Superstructure::robot_velocity() const {
704 return (drivetrain_status_fetcher_.get() != nullptr
705 ? drivetrain_status_fetcher_->robot_speed()
706 : 0.0);
707}
708
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800709} // namespace y2024::control_loops::superstructure