blob: b30d530c3788d8ba2a44319e06b475da9e7b4eba [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)) {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080055 event_loop->SetRuntimeRealtimePriority(30);
56}
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
162 // considered ready to accept note from the transfer rollers.
Niko Sohmersdd971dc2024-02-25 11:40:47 -0800163 const bool extend_at_retracted = PositionNear(
Filip Kujawa7a799602024-02-23 12:27:47 -0800164 extend_.position(), extend_set_points->retracted(), kExtendThreshold);
165
Niko Sohmers5006fc42024-03-01 17:14:22 -0800166 // Check if the turret is at the position to accept the note from extend
167 const bool turret_ready_for_load =
168 PositionNear(shooter_.turret().estimated_position(),
169 robot_constants_->common()->turret_loading_position(),
170 kTurretLoadingThreshold);
171
Niko Sohmers5006fc42024-03-01 17:14:22 -0800172 // Check if the altitude is at the position to accept the note from
173 // extend
174 const bool altitude_ready_for_load =
175 PositionNear(shooter_.altitude().estimated_position(),
176 robot_constants_->common()->altitude_loading_position(),
177 kAltitudeLoadingThreshold);
178
179 // Check if the extend is at the position to load the catapult
180 const bool extend_ready_for_catapult_transfer = PositionNear(
181 extend_.position(), extend_set_points->catapult(), kExtendThreshold);
182
Niko Sohmers5006fc42024-03-01 17:14:22 -0800183 // Only update the reuested note goal to the first goal that is requested by
184 // the manipulator
185 if (unsafe_goal != nullptr && unsafe_goal->note_goal() != NoteGoal::NONE &&
186 requested_note_goal_ == NoteGoal::NONE) {
187 requested_note_goal_ = unsafe_goal->note_goal();
188 }
189
Filip Kujawa7a799602024-02-23 12:27:47 -0800190 // Superstructure state machine:
191 // 1. IDLE. The intake is retracted and there is no note in the robot.
192 // Wait for a intake goal to switch state to INTAKING if the extend is ready
193 // 2. INTAKING. Intake the note and transfer it towards the extend.
194 // Give intake, transfer, and extend rollers positive voltage to intake and
195 // transfer. Switch to LOADED when the extend beambreak is triggered.
196 // 3. LOADED. The note is in the extend and the extend is retracted.
197 // Wait for a note goal to switch state to MOVING.
198 // For AMP/TRAP goals, check that the turret is in a position to avoid
199 // collision.
200 // 4. MOVING. The extend is moving towards a goal (AMP, TRAP, or CATAPULT).
201 // For CATAPULT goals, wait for the turret and altitude to be in a position to
202 // accept the note from the extend.
203 // Wait for the extend to reach the goal and switch state to READY if
204 // AMP or TRAP, or to LOADING_CATAPULT if CATAPULT.
205 // 5. LOADING_CATAPULT. The extend is at the position to load the catapult.
206 // Activate the extend roller to transfer the note to the catapult.
207 // Switch state to READY when the catapult beambreak is triggered.
208 // 6. READY. Ready for fire command. The note is either loaded in the catapult
209 // or in the extend and at the AMP or TRAP position. Wait for a fire command.
210 // 7. FIRING. The note is being fired, either from the extend or the catapult.
211 // Switch state back to IDLE when the note is fired.
212
213 switch (state_) {
214 case SuperstructureState::IDLE:
Niko Sohmers5006fc42024-03-01 17:14:22 -0800215 requested_note_goal_ = NoteGoal::NONE;
216
Filip Kujawa7a799602024-02-23 12:27:47 -0800217 if (unsafe_goal != nullptr &&
218 unsafe_goal->intake_goal() == IntakeGoal::INTAKE &&
Niko Sohmersdd971dc2024-02-25 11:40:47 -0800219 extend_at_retracted) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800220 state_ = SuperstructureState::INTAKING;
221 }
Maxwell Henderson4567e282024-02-25 15:38:15 -0800222
Austin Schuh027fd622024-03-01 21:26:07 -0800223 extend_goal_location = ExtendStatus::RETRACTED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800224 break;
225 case SuperstructureState::INTAKING:
Filip Kujawa7a799602024-02-23 12:27:47 -0800226 // Switch to LOADED state when the extend beambreak is triggered
227 // meaning the note is loaded in the extend
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700228 if (extend_beambreak) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800229 state_ = SuperstructureState::LOADED;
230 }
231 intake_roller_state = IntakeRollerStatus::INTAKING;
232 transfer_roller_status = TransferRollerStatus::TRANSFERING_IN;
233 extend_roller_status = ExtendRollerStatus::TRANSFERING_TO_EXTEND;
Austin Schuh027fd622024-03-01 21:26:07 -0800234 extend_goal_location = ExtendStatus::RETRACTED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800235
Maxwell Hendersonf0d22622024-02-26 21:02:11 -0800236 // If we are no longer requesting INTAKE or we are no longer requesting
237 // an INTAKE goal, wait 0.5 seconds then go back to IDLE.
238 if (!(unsafe_goal != nullptr &&
239 unsafe_goal->intake_goal() == IntakeGoal::INTAKE) &&
240 timestamp > intake_end_time_ + kExtraIntakingTime) {
241 state_ = SuperstructureState::IDLE;
242 }
243
Filip Kujawa7a799602024-02-23 12:27:47 -0800244 break;
245 case SuperstructureState::LOADED:
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700246 if (!extend_beambreak && !position->catapult_beambreak()) {
Maxwell Henderson4567e282024-02-25 15:38:15 -0800247 state_ = SuperstructureState::IDLE;
248 }
249
Niko Sohmers5006fc42024-03-01 17:14:22 -0800250 switch (requested_note_goal_) {
251 case NoteGoal::NONE:
252 break;
253 case NoteGoal::CATAPULT:
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700254 state_ = SuperstructureState::LOADING_CATAPULT;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800255 transfer_roller_status = TransferRollerStatus::TRANSFERING_IN;
256 break;
257 case NoteGoal::TRAP:
258 [[fallthrough]];
259 case NoteGoal::AMP:
James Kuszmaul6cae7d72024-03-01 21:29:56 -0800260 transfer_roller_status = TransferRollerStatus::EXTEND_MOVING;
Austin Schuh027fd622024-03-01 21:26:07 -0800261 state_ = SuperstructureState::MOVING;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800262 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800263 }
Austin Schuh027fd622024-03-01 21:26:07 -0800264 extend_goal_location = ExtendStatus::RETRACTED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800265 break;
266 case SuperstructureState::MOVING:
James Kuszmaul6cae7d72024-03-01 21:29:56 -0800267 transfer_roller_status = TransferRollerStatus::EXTEND_MOVING;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800268 switch (requested_note_goal_) {
269 case NoteGoal::NONE:
Austin Schuh027fd622024-03-01 21:26:07 -0800270 extend_goal_location = ExtendStatus::RETRACTED;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800271 if (extend_at_retracted) {
272 state_ = SuperstructureState::LOADED;
Filip Kujawa7a799602024-02-23 12:27:47 -0800273 }
Niko Sohmers5006fc42024-03-01 17:14:22 -0800274 break;
275 case NoteGoal::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800276 extend_goal_location = ExtendStatus::CATAPULT;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800277 if (extend_ready_for_catapult_transfer && turret_ready_for_load &&
278 altitude_ready_for_load) {
279 state_ = SuperstructureState::LOADING_CATAPULT;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700280 loading_catapult_start_time_ = timestamp;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800281 }
282 break;
283 case NoteGoal::TRAP:
Austin Schuh027fd622024-03-01 21:26:07 -0800284 extend_goal_location = ExtendStatus::TRAP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800285 // Check if the extend is at the TRAP position and if it is
286 // switch to READY state
287 if (PositionNear(extend_.position(), extend_set_points->trap(),
288 kExtendThreshold)) {
289 state_ = SuperstructureState::READY;
290 }
291 break;
292 case NoteGoal::AMP:
Austin Schuh027fd622024-03-01 21:26:07 -0800293 extend_goal_location = ExtendStatus::AMP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800294 // Check if the extend is at the AMP position and if it is
295 // switch to READY state
296 if (PositionNear(extend_.position(), extend_set_points->amp(),
297 kExtendThreshold)) {
298 state_ = SuperstructureState::READY;
299 }
300 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800301 }
302
303 extend_moving = true;
304 break;
305 case SuperstructureState::LOADING_CATAPULT:
306 extend_moving = false;
Austin Schuh027fd622024-03-01 21:26:07 -0800307 extend_goal_location = ExtendStatus::CATAPULT;
Filip Kujawa7a799602024-02-23 12:27:47 -0800308
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700309 if (extend_beambreak) {
310 loading_catapult_start_time_ = timestamp;
311 }
312
313 if (loading_catapult_start_time_ + std::chrono::seconds(10) < timestamp) {
Filip Kujawa77e3d8a2024-03-02 11:34:56 -0800314 state_ = SuperstructureState::IDLE;
315 }
316
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700317 if (turret_ready_for_load && altitude_ready_for_load) {
318 extend_roller_status = ExtendRollerStatus::TRANSFERING_TO_CATAPULT;
319 transfer_roller_status = TransferRollerStatus::EXTEND_MOVING;
320 }
321
Filip Kujawa7a799602024-02-23 12:27:47 -0800322 // Switch to READY state when the catapult beambreak is triggered
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700323 if (shooter_.loaded()) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800324 state_ = SuperstructureState::READY;
325 }
326 break;
327 case SuperstructureState::READY:
328 extend_moving = false;
329
330 // Switch to FIRING state when the fire button is pressed
331 if (unsafe_goal != nullptr && unsafe_goal->fire()) {
332 state_ = SuperstructureState::FIRING;
333 }
334
Niko Sohmers5006fc42024-03-01 17:14:22 -0800335 switch (requested_note_goal_) {
336 case NoteGoal::NONE:
Austin Schuh027fd622024-03-01 21:26:07 -0800337 extend_goal_location = ExtendStatus::RETRACTED;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800338 extend_moving = true;
339 state_ = SuperstructureState::MOVING;
340 break;
341 case NoteGoal::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800342 extend_goal_location = ExtendStatus::CATAPULT;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700343
344 if (!shooter_.loaded()) {
345 state_ = SuperstructureState::LOADING_CATAPULT;
346 }
Niko Sohmers5006fc42024-03-01 17:14:22 -0800347 break;
348 case NoteGoal::TRAP:
Austin Schuh027fd622024-03-01 21:26:07 -0800349 extend_goal_location = ExtendStatus::TRAP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800350 break;
351 case NoteGoal::AMP:
Austin Schuh027fd622024-03-01 21:26:07 -0800352 extend_goal_location = ExtendStatus::AMP;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800353 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800354 }
Filip Kujawa7a799602024-02-23 12:27:47 -0800355 break;
356 case SuperstructureState::FIRING:
Niko Sohmers5006fc42024-03-01 17:14:22 -0800357 switch (requested_note_goal_) {
358 case NoteGoal::NONE:
Filip Kujawa7a799602024-02-23 12:27:47 -0800359
Niko Sohmers5006fc42024-03-01 17:14:22 -0800360 break;
361 case NoteGoal::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800362 extend_goal_location = ExtendStatus::CATAPULT;
Niko Sohmers5006fc42024-03-01 17:14:22 -0800363 // Reset the state to IDLE when the game piece is fired from the
364 // catapult. We consider the game piece to be fired from the catapult
365 // when the catapultbeambreak is no longer triggered.
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700366 if (!shooter_.loaded() && !shooter_.Firing()) {
Niko Sohmers5006fc42024-03-01 17:14:22 -0800367 state_ = SuperstructureState::IDLE;
368 }
369 break;
370 case NoteGoal::TRAP:
Filip Kujawa7a799602024-02-23 12:27:47 -0800371 extend_roller_status = ExtendRollerStatus::SCORING_IN_TRAP;
Austin Schuh027fd622024-03-01 21:26:07 -0800372 extend_goal_location = ExtendStatus::TRAP;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700373 if (!extend_beambreak && unsafe_goal != nullptr &&
Niko Sohmers5006fc42024-03-01 17:14:22 -0800374 !unsafe_goal->fire()) {
375 state_ = SuperstructureState::IDLE;
376 }
377 break;
378 case NoteGoal::AMP:
379 extend_roller_status = ExtendRollerStatus::SCORING_IN_AMP;
Austin Schuh027fd622024-03-01 21:26:07 -0800380 extend_goal_location = ExtendStatus::AMP;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700381 if (!extend_beambreak && unsafe_goal != nullptr &&
Niko Sohmers5006fc42024-03-01 17:14:22 -0800382 !unsafe_goal->fire()) {
383 state_ = SuperstructureState::IDLE;
384 }
385 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800386 }
387 break;
388 }
389
390 if (unsafe_goal != nullptr &&
391 unsafe_goal->intake_goal() == IntakeGoal::SPIT) {
392 intake_roller_state = IntakeRollerStatus::SPITTING;
393 transfer_roller_status = TransferRollerStatus::TRANSFERING_OUT;
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700394 extend_roller_status = ExtendRollerStatus::SCORING_IN_AMP;
Filip Kujawa7a799602024-02-23 12:27:47 -0800395 }
396
397 // Update Intake Roller voltage based on status from state machine.
398 switch (intake_roller_state) {
399 case IntakeRollerStatus::NONE:
400 output_struct.intake_roller_voltage = 0.0;
401 break;
402 case IntakeRollerStatus::SPITTING:
403 output_struct.intake_roller_voltage =
404 robot_constants_->common()->intake_roller_voltages()->spitting();
405 break;
406 case IntakeRollerStatus::INTAKING:
407 output_struct.intake_roller_voltage =
408 robot_constants_->common()->intake_roller_voltages()->intaking();
409 break;
410 }
411
412 // Update Transfer Roller voltage based on status from state machine.
413 switch (transfer_roller_status) {
414 case TransferRollerStatus::NONE:
415 output_struct.transfer_roller_voltage = 0.0;
416 break;
417 case TransferRollerStatus::TRANSFERING_IN:
418 output_struct.transfer_roller_voltage =
419 robot_constants_->common()->transfer_roller_voltages()->transfer_in();
420 break;
421 case TransferRollerStatus::TRANSFERING_OUT:
422 output_struct.transfer_roller_voltage = robot_constants_->common()
423 ->transfer_roller_voltages()
424 ->transfer_out();
425 break;
James Kuszmaul6cae7d72024-03-01 21:29:56 -0800426 case TransferRollerStatus::EXTEND_MOVING:
427 output_struct.transfer_roller_voltage = robot_constants_->common()
428 ->transfer_roller_voltages()
429 ->extend_moving();
430 break;
Filip Kujawa7a799602024-02-23 12:27:47 -0800431 }
432
433 // Update Extend Roller voltage based on status from state machine.
434 const ExtendRollerVoltages *extend_roller_voltages =
435 robot_constants_->common()->extend_roller_voltages();
436 switch (extend_roller_status) {
437 case ExtendRollerStatus::IDLE:
438 // No voltage applied when idle
439 output_struct.extend_roller_voltage = 0.0;
440 break;
441 case ExtendRollerStatus::TRANSFERING_TO_EXTEND:
442 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
443 break;
444 case ExtendRollerStatus::SCORING_IN_AMP:
445 [[fallthrough]];
446 case ExtendRollerStatus::SCORING_IN_TRAP:
447 // Apply scoring voltage during scoring in amp or trap
448 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
449 break;
450 case ExtendRollerStatus::TRANSFERING_TO_CATAPULT:
451 // Apply scoring voltage during transferring to catapult
452 output_struct.extend_roller_voltage = extend_roller_voltages->scoring();
453 break;
454 }
455
Maxwell Henderson45878502024-03-15 19:35:25 -0700456 if (unsafe_goal != nullptr && unsafe_goal->spit_extend()) {
457 output_struct.extend_roller_voltage = -extend_roller_voltages->scoring();
458 }
459
Austin Schuh027fd622024-03-01 21:26:07 -0800460 double extend_goal_position = 0.0;
Filip Kujawa7a799602024-02-23 12:27:47 -0800461
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700462 // If we request trap, override the extend goal to be trap unless we request
463 // amp.
James Kuszmaul0281e152024-02-26 22:26:16 -0800464 if (unsafe_goal != nullptr && unsafe_goal->note_goal() == NoteGoal::TRAP) {
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700465 trap_override_ = true;
466 }
467
468 if (unsafe_goal != nullptr && unsafe_goal->note_goal() == NoteGoal::AMP &&
469 trap_override_) {
470 trap_override_ = false;
471 requested_note_goal_ = NoteGoal::AMP;
472 state_ = SuperstructureState::READY;
473 }
474
475 if (trap_override_) {
Austin Schuh027fd622024-03-01 21:26:07 -0800476 extend_goal_location = ExtendStatus::TRAP;
James Kuszmaul0281e152024-02-26 22:26:16 -0800477 }
478
Filip Kujawa7a799602024-02-23 12:27:47 -0800479 // Set the extend position based on the state machine output
Austin Schuh027fd622024-03-01 21:26:07 -0800480 switch (extend_goal_location) {
Filip Kujawa7a799602024-02-23 12:27:47 -0800481 case ExtendStatus::RETRACTED:
Austin Schuh027fd622024-03-01 21:26:07 -0800482 extend_goal_position = extend_set_points->retracted();
Filip Kujawa7a799602024-02-23 12:27:47 -0800483 break;
484 case ExtendStatus::AMP:
Austin Schuh027fd622024-03-01 21:26:07 -0800485 extend_goal_position = extend_set_points->amp();
Filip Kujawa7a799602024-02-23 12:27:47 -0800486 break;
487 case ExtendStatus::TRAP:
Austin Schuh027fd622024-03-01 21:26:07 -0800488 extend_goal_position = extend_set_points->trap();
Filip Kujawa7a799602024-02-23 12:27:47 -0800489 break;
490 case ExtendStatus::CATAPULT:
Austin Schuh027fd622024-03-01 21:26:07 -0800491 extend_goal_position = extend_set_points->catapult();
Filip Kujawa7a799602024-02-23 12:27:47 -0800492 break;
493 case ExtendStatus::MOVING:
494 // Should never happen
495 break;
496 }
497
Niko Sohmers5006fc42024-03-01 17:14:22 -0800498 NoteStatus uncompleted_note_goal_status = NoteStatus::NONE;
499
500 switch (requested_note_goal_) {
501 case NoteGoal::NONE:
502 uncompleted_note_goal_status = NoteStatus::NONE;
503 break;
504 case NoteGoal::CATAPULT:
505 uncompleted_note_goal_status = NoteStatus::CATAPULT;
506 break;
507 case NoteGoal::AMP:
508 uncompleted_note_goal_status = NoteStatus::AMP;
509 break;
510 case NoteGoal::TRAP:
511 uncompleted_note_goal_status = NoteStatus::TRAP;
512 break;
513 }
514
Filip Kujawa7a799602024-02-23 12:27:47 -0800515 // Set the extend status based on the state machine output
516 // If the extend is moving, the status is MOVING, otherwise it is the same
517 // as extend_status
518 ExtendStatus extend_status =
Austin Schuh027fd622024-03-01 21:26:07 -0800519 (extend_moving ? ExtendStatus::MOVING : extend_goal_location);
Filip Kujawa7a799602024-02-23 12:27:47 -0800520
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800521 if (joystick_state_fetcher_.Fetch() &&
522 joystick_state_fetcher_->has_alliance()) {
523 alliance_ = joystick_state_fetcher_->alliance();
524 }
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800525
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800526 drivetrain_status_fetcher_.Fetch();
527
Austin Schuh027fd622024-03-01 21:26:07 -0800528 const bool collided = collision_avoidance_.IsCollided({
529 .intake_pivot_position = intake_pivot_.estimated_position(),
530 .turret_position = shooter_.turret().estimated_position(),
531 .extend_position = extend_.estimated_position(),
532 });
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800533
Filip Kujawa6d717632024-02-01 11:40:55 -0800534 aos::FlatbufferFixedAllocatorArray<
535 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
536 climber_goal_buffer;
537
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700538 {
539 flatbuffers::FlatBufferBuilder *climber_fbb = climber_goal_buffer.fbb();
540 flatbuffers::Offset<frc971::ProfileParameters> climber_profile =
541 frc971::CreateProfileParameters(*climber_fbb, climber_velocity,
542 climber_accel);
543
544 climber_goal_buffer.Finish(
545 frc971::control_loops::
546 CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
547 *climber_fbb, climber_position, climber_profile));
548 }
Filip Kujawa6d717632024-02-01 11:40:55 -0800549
550 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
551 *climber_goal = &climber_goal_buffer.message();
552
553 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
554 climber_status_offset = climber_.Iterate(
555 climber_goal, position->climber(),
556 output != nullptr ? &output_struct.climber_voltage : nullptr,
557 status->fbb());
558
Niko Sohmersac4d8872024-02-23 13:55:47 -0800559 double max_intake_pivot_position = 0;
560 double min_intake_pivot_position = 0;
Austin Schuh027fd622024-03-01 21:26:07 -0800561 double max_extend_position = 0;
562 double min_extend_position = 0;
Niko Sohmersac4d8872024-02-23 13:55:47 -0800563
564 aos::FlatbufferFixedAllocatorArray<
565 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
566 intake_pivot_goal_buffer;
567
568 intake_pivot_goal_buffer.Finish(
569 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
570 *intake_pivot_goal_buffer.fbb(), intake_pivot_position));
571
572 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
573 *intake_pivot_goal = &intake_pivot_goal_buffer.message();
574
575 double *intake_output =
576 (output != nullptr ? &output_struct.intake_pivot_voltage : nullptr);
577
578 const bool disabled = intake_pivot_.Correct(
579 intake_pivot_goal, position->intake_pivot(), intake_output == nullptr);
580
Austin Schuh027fd622024-03-01 21:26:07 -0800581 aos::FlatbufferFixedAllocatorArray<
582 frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512>
583 extend_goal_buffer;
584
585 extend_goal_buffer.Finish(
586 frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal(
587 *extend_goal_buffer.fbb(), extend_goal_position));
588
589 const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal
590 *extend_goal = &extend_goal_buffer.message();
591
Filip Kujawa7a799602024-02-23 12:27:47 -0800592 // TODO(max): Change how we handle the collision with the turret and
593 // intake to be clearer
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800594 const flatbuffers::Offset<ShooterStatus> shooter_status_offset =
595 shooter_.Iterate(
596 position,
597 unsafe_goal != nullptr ? unsafe_goal->shooter_goal() : nullptr,
Filip Kujawa7a799602024-02-23 12:27:47 -0800598 unsafe_goal != nullptr ? unsafe_goal->fire() : false,
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800599 output != nullptr ? &output_struct.catapult_voltage : nullptr,
600 output != nullptr ? &output_struct.altitude_voltage : nullptr,
601 output != nullptr ? &output_struct.turret_voltage : nullptr,
602 output != nullptr ? &output_struct.retention_roller_voltage : nullptr,
Maxwell Hendersond5bf47a2024-02-23 17:16:48 -0800603 output != nullptr
604 ? &output_struct.retention_roller_stator_current_limit
605 : nullptr,
Maxwell Henderson461a8a42024-02-23 17:07:39 -0800606 robot_state().voltage_battery(), &collision_avoidance_,
Austin Schuh027fd622024-03-01 21:26:07 -0800607 extend_goal_position, extend_.estimated_position(),
608 &max_extend_position, &min_extend_position,
Niko Sohmersac4d8872024-02-23 13:55:47 -0800609 intake_pivot_.estimated_position(), &max_intake_pivot_position,
Stephan Pleines9f3983a2024-03-13 20:22:38 -0700610 &min_intake_pivot_position, requested_note_goal_, status->fbb(),
611 timestamp);
Niko Sohmersac4d8872024-02-23 13:55:47 -0800612
613 intake_pivot_.set_min_position(min_intake_pivot_position);
614 intake_pivot_.set_max_position(max_intake_pivot_position);
615
Austin Schuh027fd622024-03-01 21:26:07 -0800616 extend_.set_min_position(min_extend_position);
617 extend_.set_max_position(max_extend_position);
618
Niko Sohmersac4d8872024-02-23 13:55:47 -0800619 // Calculate the loops for a cycle.
620 const double voltage = intake_pivot_.UpdateController(disabled);
621
622 intake_pivot_.UpdateObserver(voltage);
623
624 // Write out all the voltages.
625 if (intake_output) {
626 *intake_output = voltage;
627 }
628
629 const flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus>
630 intake_pivot_status_offset = intake_pivot_.MakeStatus(status->fbb());
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800631
Filip Kujawa7a799602024-02-23 12:27:47 -0800632 const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus>
633 extend_status_offset = extend_.Iterate(
Austin Schuh027fd622024-03-01 21:26:07 -0800634 extend_goal, position->extend(),
Filip Kujawa7a799602024-02-23 12:27:47 -0800635 output != nullptr ? &output_struct.extend_voltage : nullptr,
636 status->fbb());
637
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800638 if (output) {
639 output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct)));
640 }
641
642 Status::Builder status_builder = status->MakeBuilder<Status>();
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800643
Filip Kujawa7a799602024-02-23 12:27:47 -0800644 const bool zeroed = intake_pivot_.zeroed() && climber_.zeroed() &&
645 shooter_.zeroed() && extend_.zeroed();
646 const bool estopped = intake_pivot_.estopped() || climber_.estopped() ||
647 shooter_.estopped() || extend_.estopped();
Niko Sohmersafc51fe2024-01-29 17:48:35 -0800648
649 status_builder.add_zeroed(zeroed);
650 status_builder.add_estopped(estopped);
Filip Kujawa102a9b22024-02-18 09:40:23 -0800651 status_builder.add_intake_roller(intake_roller_state);
652 status_builder.add_intake_pivot(intake_pivot_status_offset);
Filip Kujawa7a799602024-02-23 12:27:47 -0800653 status_builder.add_transfer_roller(transfer_roller_status);
Filip Kujawa102a9b22024-02-18 09:40:23 -0800654 status_builder.add_climber(climber_status_offset);
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800655 status_builder.add_shooter(shooter_status_offset);
Niko Sohmersac4d8872024-02-23 13:55:47 -0800656 status_builder.add_collided(collided);
Filip Kujawa7a799602024-02-23 12:27:47 -0800657 status_builder.add_extend_roller(extend_roller_status);
658 status_builder.add_extend_status(extend_status);
659 status_builder.add_extend(extend_status_offset);
660 status_builder.add_state(state_);
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700661 status_builder.add_shot_count(shooter_.shot_count());
Niko Sohmers5006fc42024-03-01 17:14:22 -0800662 status_builder.add_uncompleted_note_goal(uncompleted_note_goal_status);
James Kuszmaul0281e152024-02-26 22:26:16 -0800663 status_builder.add_extend_ready_for_transfer(extend_at_retracted);
Niko Sohmers2a251cd2024-03-02 19:16:15 -0800664 status_builder.add_extend_at_retracted(extend_at_retracted);
665 status_builder.add_turret_ready_for_load(turret_ready_for_load);
666 status_builder.add_altitude_ready_for_load(altitude_ready_for_load);
667 status_builder.add_extend_ready_for_catapult_transfer(
668 extend_ready_for_catapult_transfer);
Niko Sohmers6adb5b92024-03-16 17:47:54 -0700669 status_builder.add_extend_beambreak(extend_beambreak);
Niko Sohmers2a251cd2024-03-02 19:16:15 -0800670 status_builder.add_catapult_beambreak(position->catapult_beambreak());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800671
672 (void)status->Send(status_builder.Finish());
673}
674
675double Superstructure::robot_velocity() const {
676 return (drivetrain_status_fetcher_.get() != nullptr
677 ? drivetrain_status_fetcher_->robot_speed()
678 : 0.0);
679}
680
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800681} // namespace y2024::control_loops::superstructure