Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 1 | #include "y2024/control_loops/superstructure/superstructure.h" |
| 2 | |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 3 | #include <chrono> |
| 4 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 5 | #include "aos/events/event_loop.h" |
| 6 | #include "aos/flatbuffer_merge.h" |
| 7 | #include "aos/network/team_number.h" |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 8 | #include "aos/time/time.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 9 | #include "frc971/shooter_interpolation/interpolation.h" |
| 10 | #include "frc971/zeroing/wrap.h" |
| 11 | |
| 12 | DEFINE_bool(ignore_distance, false, |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 13 | "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. |
| 17 | constexpr double kExtendThreshold = 0.01; |
| 18 | |
| 19 | constexpr double kTurretLoadingThreshold = 0.01; |
| 20 | constexpr double kAltitudeLoadingThreshold = 0.01; |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 21 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 22 | namespace y2024::control_loops::superstructure { |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 23 | |
| 24 | using ::aos::monotonic_clock; |
| 25 | |
| 26 | using frc971::control_loops::AbsoluteEncoderProfiledJointStatus; |
| 27 | using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus; |
| 28 | using frc971::control_loops::RelativeEncoderProfiledJointStatus; |
| 29 | |
| 30 | Superstructure::Superstructure(::aos::EventLoop *event_loop, |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 31 | const ::std::string &name) |
| 32 | : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop, |
| 33 | name), |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 34 | constants_fetcher_(event_loop), |
Niko Sohmers | afc51fe | 2024-01-29 17:48:35 -0800 | [diff] [blame] | 35 | robot_constants_(CHECK_NOTNULL(&constants_fetcher_.constants())), |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 36 | drivetrain_status_fetcher_( |
| 37 | event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>( |
| 38 | "/drivetrain")), |
| 39 | joystick_state_fetcher_( |
Niko Sohmers | afc51fe | 2024-01-29 17:48:35 -0800 | [diff] [blame] | 40 | event_loop->MakeFetcher<aos::JoystickState>("/aos")), |
Niko Sohmers | 74b0ad5 | 2024-02-03 18:00:31 -0800 | [diff] [blame] | 41 | intake_pivot_(robot_constants_->common()->intake_pivot(), |
| 42 | robot_constants_->robot()->intake_constants()), |
Filip Kujawa | 6d71763 | 2024-02-01 11:40:55 -0800 | [diff] [blame] | 43 | climber_( |
| 44 | robot_constants_->common()->climber(), |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 45 | robot_constants_->robot()->climber_constants()->zeroing_constants()), |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 46 | shooter_(event_loop, robot_constants_), |
| 47 | extend_( |
| 48 | robot_constants_->common()->extend(), |
| 49 | robot_constants_->robot()->extend_constants()->zeroing_constants()) { |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 50 | event_loop->SetRuntimeRealtimePriority(30); |
| 51 | } |
| 52 | |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 53 | bool PositionNear(double position, double goal, double threshold) { |
| 54 | return std::abs(position - goal) < threshold; |
| 55 | } |
| 56 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 57 | void Superstructure::RunIteration(const Goal *unsafe_goal, |
| 58 | const Position *position, |
| 59 | aos::Sender<Output>::Builder *output, |
| 60 | aos::Sender<Status>::Builder *status) { |
| 61 | const monotonic_clock::time_point timestamp = |
| 62 | event_loop()->context().monotonic_event_time; |
| 63 | |
Maxwell Henderson | 461a8a4 | 2024-02-23 17:07:39 -0800 | [diff] [blame] | 64 | (void)timestamp; |
| 65 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 66 | if (WasReset()) { |
| 67 | AOS_LOG(ERROR, "WPILib reset, restarting\n"); |
Niko Sohmers | afc51fe | 2024-01-29 17:48:35 -0800 | [diff] [blame] | 68 | intake_pivot_.Reset(); |
Filip Kujawa | 6d71763 | 2024-02-01 11:40:55 -0800 | [diff] [blame] | 69 | climber_.Reset(); |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 70 | shooter_.Reset(); |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 71 | extend_.Reset(); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | OutputT output_struct; |
Niko Sohmers | afc51fe | 2024-01-29 17:48:35 -0800 | [diff] [blame] | 75 | |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 76 | // Handle Climber Goal separately from main superstructure state machine |
Filip Kujawa | 6d71763 | 2024-02-01 11:40:55 -0800 | [diff] [blame] | 77 | double climber_position = |
| 78 | robot_constants_->common()->climber_set_points()->retract(); |
| 79 | |
| 80 | if (unsafe_goal != nullptr) { |
| 81 | switch (unsafe_goal->climber_goal()) { |
| 82 | case ClimberGoal::FULL_EXTEND: |
| 83 | climber_position = |
| 84 | robot_constants_->common()->climber_set_points()->full_extend(); |
| 85 | break; |
Filip Kujawa | 6d71763 | 2024-02-01 11:40:55 -0800 | [diff] [blame] | 86 | case ClimberGoal::RETRACT: |
| 87 | climber_position = |
| 88 | robot_constants_->common()->climber_set_points()->retract(); |
| 89 | break; |
Maxwell Henderson | 7db2978 | 2024-02-24 20:10:26 -0800 | [diff] [blame] | 90 | case ClimberGoal::STOWED: |
| 91 | climber_position = |
| 92 | robot_constants_->common()->climber_set_points()->stowed(); |
Filip Kujawa | 6d71763 | 2024-02-01 11:40:55 -0800 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 96 | // If we started off preloaded, skip to the ready state. |
| 97 | if (unsafe_goal != nullptr && unsafe_goal->shooter_goal() && |
| 98 | unsafe_goal->shooter_goal()->preloaded()) { |
| 99 | if (state_ != SuperstructureState::READY && |
| 100 | state_ != SuperstructureState::FIRING) { |
| 101 | state_ = SuperstructureState::READY; |
Niko Sohmers | dd971dc | 2024-02-25 11:40:47 -0800 | [diff] [blame^] | 102 | catapult_requested_ = true; |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | // Handle the intake pivot goal separately from the main superstructure state |
| 107 | IntakeRollerStatus intake_roller_state = IntakeRollerStatus::NONE; |
| 108 | double intake_pivot_position = |
| 109 | robot_constants_->common()->intake_pivot_set_points()->retracted(); |
| 110 | |
| 111 | if (unsafe_goal != nullptr) { |
| 112 | switch (unsafe_goal->intake_goal()) { |
| 113 | case IntakeGoal::INTAKE: |
| 114 | intake_pivot_position = |
| 115 | robot_constants_->common()->intake_pivot_set_points()->extended(); |
| 116 | break; |
| 117 | case IntakeGoal::SPIT: |
| 118 | intake_pivot_position = |
| 119 | robot_constants_->common()->intake_pivot_set_points()->retracted(); |
| 120 | break; |
| 121 | case IntakeGoal::NONE: |
| 122 | intake_pivot_position = |
| 123 | robot_constants_->common()->intake_pivot_set_points()->retracted(); |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | ExtendRollerStatus extend_roller_status = ExtendRollerStatus::IDLE; |
| 129 | ExtendStatus extend_goal = ExtendStatus::RETRACTED; |
| 130 | |
| 131 | // True if the extend is moving towards a goal |
| 132 | bool extend_moving = false; |
| 133 | |
| 134 | TransferRollerStatus transfer_roller_status = TransferRollerStatus::NONE; |
| 135 | |
| 136 | const ExtendSetPoints *extend_set_points = |
| 137 | robot_constants_->common()->extend_set_points(); |
| 138 | |
| 139 | // Checks if the extend is close enough to the retracted position to be |
| 140 | // considered ready to accept note from the transfer rollers. |
Niko Sohmers | dd971dc | 2024-02-25 11:40:47 -0800 | [diff] [blame^] | 141 | const bool extend_at_retracted = PositionNear( |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 142 | extend_.position(), extend_set_points->retracted(), kExtendThreshold); |
| 143 | |
| 144 | // If true, the turret should be moved to the position to avoid collision with |
| 145 | // the extend. |
| 146 | bool move_turret_to_standby = false; |
| 147 | |
| 148 | // Superstructure state machine: |
| 149 | // 1. IDLE. The intake is retracted and there is no note in the robot. |
| 150 | // Wait for a intake goal to switch state to INTAKING if the extend is ready |
| 151 | // 2. INTAKING. Intake the note and transfer it towards the extend. |
| 152 | // Give intake, transfer, and extend rollers positive voltage to intake and |
| 153 | // transfer. Switch to LOADED when the extend beambreak is triggered. |
| 154 | // 3. LOADED. The note is in the extend and the extend is retracted. |
| 155 | // Wait for a note goal to switch state to MOVING. |
| 156 | // For AMP/TRAP goals, check that the turret is in a position to avoid |
| 157 | // collision. |
| 158 | // 4. MOVING. The extend is moving towards a goal (AMP, TRAP, or CATAPULT). |
| 159 | // For CATAPULT goals, wait for the turret and altitude to be in a position to |
| 160 | // accept the note from the extend. |
| 161 | // Wait for the extend to reach the goal and switch state to READY if |
| 162 | // AMP or TRAP, or to LOADING_CATAPULT if CATAPULT. |
| 163 | // 5. LOADING_CATAPULT. The extend is at the position to load the catapult. |
| 164 | // Activate the extend roller to transfer the note to the catapult. |
| 165 | // Switch state to READY when the catapult beambreak is triggered. |
| 166 | // 6. READY. Ready for fire command. The note is either loaded in the catapult |
| 167 | // or in the extend and at the AMP or TRAP position. Wait for a fire command. |
| 168 | // 7. FIRING. The note is being fired, either from the extend or the catapult. |
| 169 | // Switch state back to IDLE when the note is fired. |
| 170 | |
| 171 | switch (state_) { |
| 172 | case SuperstructureState::IDLE: |
| 173 | if (unsafe_goal != nullptr && |
| 174 | unsafe_goal->intake_goal() == IntakeGoal::INTAKE && |
Niko Sohmers | dd971dc | 2024-02-25 11:40:47 -0800 | [diff] [blame^] | 175 | extend_at_retracted) { |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 176 | state_ = SuperstructureState::INTAKING; |
| 177 | } |
| 178 | extend_goal = ExtendStatus::RETRACTED; |
| 179 | catapult_requested_ = false; |
| 180 | break; |
| 181 | case SuperstructureState::INTAKING: |
| 182 | |
| 183 | // Switch to LOADED state when the extend beambreak is triggered |
| 184 | // meaning the note is loaded in the extend |
| 185 | if (position->extend_beambreak()) { |
| 186 | state_ = SuperstructureState::LOADED; |
| 187 | } |
| 188 | intake_roller_state = IntakeRollerStatus::INTAKING; |
| 189 | transfer_roller_status = TransferRollerStatus::TRANSFERING_IN; |
| 190 | extend_roller_status = ExtendRollerStatus::TRANSFERING_TO_EXTEND; |
| 191 | extend_goal = ExtendStatus::RETRACTED; |
| 192 | |
| 193 | if (!catapult_requested_ && unsafe_goal != nullptr && |
| 194 | unsafe_goal->note_goal() == NoteGoal::CATAPULT) { |
| 195 | catapult_requested_ = true; |
| 196 | } |
| 197 | |
| 198 | break; |
| 199 | case SuperstructureState::LOADED: |
| 200 | if (catapult_requested_ == true) { |
| 201 | state_ = SuperstructureState::MOVING; |
| 202 | break; |
| 203 | } |
| 204 | |
| 205 | if (unsafe_goal != nullptr && |
| 206 | unsafe_goal->note_goal() != NoteGoal::NONE) { |
| 207 | // If the goal is AMP or TRAP, check if the turret is in a position to |
| 208 | // avoid collision when the extend moves. |
| 209 | if (unsafe_goal->note_goal() == NoteGoal::AMP || |
| 210 | unsafe_goal->note_goal() == NoteGoal::TRAP) { |
| 211 | bool turret_ready_for_extend_move = |
| 212 | PositionNear(shooter_.turret().estimated_position(), |
| 213 | robot_constants_->common() |
| 214 | ->turret_avoid_extend_collision_position(), |
| 215 | kTurretLoadingThreshold); |
| 216 | |
| 217 | if (turret_ready_for_extend_move) { |
| 218 | state_ = SuperstructureState::MOVING; |
| 219 | } else { |
| 220 | move_turret_to_standby = true; |
| 221 | } |
| 222 | } else if (unsafe_goal->note_goal() == NoteGoal::CATAPULT) { |
| 223 | // If catapult is requested, switch to MOVING state |
| 224 | state_ = SuperstructureState::MOVING; |
| 225 | } |
| 226 | } |
| 227 | extend_goal = ExtendStatus::RETRACTED; |
| 228 | if (!catapult_requested_ && unsafe_goal != nullptr && |
| 229 | unsafe_goal->note_goal() == NoteGoal::CATAPULT) { |
| 230 | catapult_requested_ = true; |
| 231 | } |
| 232 | break; |
| 233 | case SuperstructureState::MOVING: |
| 234 | |
| 235 | if (catapult_requested_) { |
| 236 | extend_goal = ExtendStatus::CATAPULT; |
| 237 | |
| 238 | // Check if the extend is at the position to load the catapult |
| 239 | bool extend_ready_for_catapult_transfer = |
| 240 | PositionNear(extend_.position(), extend_set_points->catapult(), |
| 241 | kExtendThreshold); |
| 242 | |
| 243 | // Check if the turret is at the position to accept the note from extend |
| 244 | bool turret_ready_for_load = |
| 245 | PositionNear(shooter_.turret().estimated_position(), |
| 246 | robot_constants_->common()->turret_loading_position(), |
| 247 | kTurretLoadingThreshold); |
| 248 | |
| 249 | // Check if the altitude is at the position to accept the note from |
| 250 | // extend |
| 251 | bool altitude_ready_for_load = PositionNear( |
| 252 | shooter_.altitude().estimated_position(), |
| 253 | robot_constants_->common()->altitude_loading_position(), |
| 254 | kAltitudeLoadingThreshold); |
| 255 | |
| 256 | if (extend_ready_for_catapult_transfer && turret_ready_for_load && |
| 257 | altitude_ready_for_load) { |
| 258 | state_ = SuperstructureState::LOADING_CATAPULT; |
| 259 | } |
| 260 | } else { |
| 261 | if (unsafe_goal != nullptr) { |
| 262 | switch (unsafe_goal->note_goal()) { |
| 263 | case NoteGoal::AMP: |
| 264 | extend_goal = ExtendStatus::AMP; |
| 265 | move_turret_to_standby = true; |
| 266 | // Check if the extend is at the AMP position and if it is |
| 267 | // switch to READY state |
| 268 | if (PositionNear(extend_.position(), extend_set_points->amp(), |
| 269 | kExtendThreshold)) { |
| 270 | state_ = SuperstructureState::READY; |
| 271 | } |
| 272 | break; |
| 273 | case NoteGoal::TRAP: |
| 274 | extend_goal = ExtendStatus::TRAP; |
| 275 | move_turret_to_standby = true; |
| 276 | // Check if the extend is at the TRAP position and if it is |
| 277 | // switch to READY state |
| 278 | if (PositionNear(extend_.position(), extend_set_points->trap(), |
| 279 | kExtendThreshold)) { |
| 280 | state_ = SuperstructureState::READY; |
| 281 | } |
| 282 | break; |
| 283 | case NoteGoal::NONE: |
| 284 | extend_goal = ExtendStatus::RETRACTED; |
| 285 | move_turret_to_standby = true; |
Niko Sohmers | dd971dc | 2024-02-25 11:40:47 -0800 | [diff] [blame^] | 286 | if (extend_at_retracted) { |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 287 | state_ = SuperstructureState::LOADED; |
| 288 | } |
| 289 | break; |
| 290 | case NoteGoal::CATAPULT: |
| 291 | catapult_requested_ = true; |
| 292 | extend_goal = ExtendStatus::CATAPULT; |
| 293 | break; |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | extend_moving = true; |
| 299 | break; |
| 300 | case SuperstructureState::LOADING_CATAPULT: |
| 301 | extend_moving = false; |
| 302 | extend_goal = ExtendStatus::CATAPULT; |
| 303 | extend_roller_status = ExtendRollerStatus::TRANSFERING_TO_CATAPULT; |
| 304 | |
| 305 | // Switch to READY state when the catapult beambreak is triggered |
| 306 | if (position->catapult_beambreak()) { |
| 307 | state_ = SuperstructureState::READY; |
| 308 | } |
| 309 | break; |
| 310 | case SuperstructureState::READY: |
| 311 | extend_moving = false; |
| 312 | |
| 313 | // Switch to FIRING state when the fire button is pressed |
| 314 | if (unsafe_goal != nullptr && unsafe_goal->fire()) { |
| 315 | state_ = SuperstructureState::FIRING; |
| 316 | } |
| 317 | |
| 318 | if (catapult_requested_) { |
| 319 | extend_goal = ExtendStatus::CATAPULT; |
| 320 | } else { |
| 321 | if (unsafe_goal != nullptr) { |
| 322 | if (unsafe_goal->note_goal() == NoteGoal::AMP) { |
| 323 | extend_goal = ExtendStatus::AMP; |
| 324 | move_turret_to_standby = true; |
| 325 | } else if (unsafe_goal->note_goal() == NoteGoal::TRAP) { |
| 326 | extend_goal = ExtendStatus::TRAP; |
| 327 | move_turret_to_standby = true; |
| 328 | } else { |
| 329 | extend_goal = ExtendStatus::RETRACTED; |
| 330 | extend_moving = true; |
| 331 | state_ = SuperstructureState::MOVING; |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | break; |
| 337 | case SuperstructureState::FIRING: |
| 338 | if (catapult_requested_) { |
| 339 | extend_goal = ExtendStatus::CATAPULT; |
| 340 | |
| 341 | // Reset the state to IDLE when the game piece is fired from the |
| 342 | // catapult. We consider the game piece to be fired from the catapult |
| 343 | // when the catapultbeambreak is no longer triggered. |
| 344 | if (!position->catapult_beambreak()) { |
| 345 | state_ = SuperstructureState::IDLE; |
| 346 | } |
| 347 | } else { |
| 348 | if (unsafe_goal != nullptr && |
| 349 | unsafe_goal->note_goal() == NoteGoal::AMP) { |
| 350 | extend_roller_status = ExtendRollerStatus::SCORING_IN_AMP; |
| 351 | extend_goal = ExtendStatus::AMP; |
| 352 | } else if (unsafe_goal != nullptr && |
| 353 | unsafe_goal->note_goal() == NoteGoal::TRAP) { |
| 354 | extend_roller_status = ExtendRollerStatus::SCORING_IN_TRAP; |
| 355 | extend_goal = ExtendStatus::TRAP; |
| 356 | } |
| 357 | |
| 358 | // Reset the state to IDLE when the game piece is fired from the extend. |
| 359 | // We consider the game piece to be fired from the extend when |
| 360 | // the extend beambreak is no longer triggered and the fire button is |
| 361 | // released. |
| 362 | if (!position->extend_beambreak() && unsafe_goal != nullptr && |
| 363 | !unsafe_goal->fire()) { |
| 364 | state_ = SuperstructureState::IDLE; |
| 365 | } |
| 366 | } |
| 367 | break; |
| 368 | } |
| 369 | |
| 370 | if (unsafe_goal != nullptr && |
| 371 | unsafe_goal->intake_goal() == IntakeGoal::SPIT) { |
| 372 | intake_roller_state = IntakeRollerStatus::SPITTING; |
| 373 | transfer_roller_status = TransferRollerStatus::TRANSFERING_OUT; |
| 374 | } |
| 375 | |
| 376 | // Update Intake Roller voltage based on status from state machine. |
| 377 | switch (intake_roller_state) { |
| 378 | case IntakeRollerStatus::NONE: |
| 379 | output_struct.intake_roller_voltage = 0.0; |
| 380 | break; |
| 381 | case IntakeRollerStatus::SPITTING: |
| 382 | output_struct.intake_roller_voltage = |
| 383 | robot_constants_->common()->intake_roller_voltages()->spitting(); |
| 384 | break; |
| 385 | case IntakeRollerStatus::INTAKING: |
| 386 | output_struct.intake_roller_voltage = |
| 387 | robot_constants_->common()->intake_roller_voltages()->intaking(); |
| 388 | break; |
| 389 | } |
| 390 | |
| 391 | // Update Transfer Roller voltage based on status from state machine. |
| 392 | switch (transfer_roller_status) { |
| 393 | case TransferRollerStatus::NONE: |
| 394 | output_struct.transfer_roller_voltage = 0.0; |
| 395 | break; |
| 396 | case TransferRollerStatus::TRANSFERING_IN: |
| 397 | output_struct.transfer_roller_voltage = |
| 398 | robot_constants_->common()->transfer_roller_voltages()->transfer_in(); |
| 399 | break; |
| 400 | case TransferRollerStatus::TRANSFERING_OUT: |
| 401 | output_struct.transfer_roller_voltage = robot_constants_->common() |
| 402 | ->transfer_roller_voltages() |
| 403 | ->transfer_out(); |
| 404 | break; |
| 405 | } |
| 406 | |
| 407 | // Update Extend Roller voltage based on status from state machine. |
| 408 | const ExtendRollerVoltages *extend_roller_voltages = |
| 409 | robot_constants_->common()->extend_roller_voltages(); |
| 410 | switch (extend_roller_status) { |
| 411 | case ExtendRollerStatus::IDLE: |
| 412 | // No voltage applied when idle |
| 413 | output_struct.extend_roller_voltage = 0.0; |
| 414 | break; |
| 415 | case ExtendRollerStatus::TRANSFERING_TO_EXTEND: |
| 416 | output_struct.extend_roller_voltage = extend_roller_voltages->scoring(); |
| 417 | break; |
| 418 | case ExtendRollerStatus::SCORING_IN_AMP: |
| 419 | [[fallthrough]]; |
| 420 | case ExtendRollerStatus::SCORING_IN_TRAP: |
| 421 | // Apply scoring voltage during scoring in amp or trap |
| 422 | output_struct.extend_roller_voltage = extend_roller_voltages->scoring(); |
| 423 | break; |
| 424 | case ExtendRollerStatus::TRANSFERING_TO_CATAPULT: |
| 425 | // Apply scoring voltage during transferring to catapult |
| 426 | output_struct.extend_roller_voltage = extend_roller_voltages->scoring(); |
| 427 | break; |
| 428 | } |
| 429 | |
| 430 | double extend_position = 0.0; |
| 431 | |
| 432 | // Set the extend position based on the state machine output |
| 433 | switch (extend_goal) { |
| 434 | case ExtendStatus::RETRACTED: |
| 435 | extend_position = extend_set_points->retracted(); |
| 436 | break; |
| 437 | case ExtendStatus::AMP: |
| 438 | extend_position = extend_set_points->amp(); |
| 439 | break; |
| 440 | case ExtendStatus::TRAP: |
| 441 | extend_position = extend_set_points->trap(); |
| 442 | break; |
| 443 | case ExtendStatus::CATAPULT: |
| 444 | extend_position = extend_set_points->catapult(); |
| 445 | break; |
| 446 | case ExtendStatus::MOVING: |
| 447 | // Should never happen |
| 448 | break; |
| 449 | } |
| 450 | |
| 451 | // Set the extend status based on the state machine output |
| 452 | // If the extend is moving, the status is MOVING, otherwise it is the same |
| 453 | // as extend_status |
| 454 | ExtendStatus extend_status = |
| 455 | (extend_moving ? ExtendStatus::MOVING : extend_goal); |
| 456 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 457 | if (joystick_state_fetcher_.Fetch() && |
| 458 | joystick_state_fetcher_->has_alliance()) { |
| 459 | alliance_ = joystick_state_fetcher_->alliance(); |
| 460 | } |
Niko Sohmers | afc51fe | 2024-01-29 17:48:35 -0800 | [diff] [blame] | 461 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 462 | drivetrain_status_fetcher_.Fetch(); |
| 463 | |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 464 | const bool collided = collision_avoidance_.IsCollided( |
| 465 | {.intake_pivot_position = intake_pivot_.estimated_position(), |
| 466 | .turret_position = shooter_.turret().estimated_position()}); |
Niko Sohmers | afc51fe | 2024-01-29 17:48:35 -0800 | [diff] [blame] | 467 | |
Filip Kujawa | 6d71763 | 2024-02-01 11:40:55 -0800 | [diff] [blame] | 468 | aos::FlatbufferFixedAllocatorArray< |
| 469 | frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512> |
| 470 | climber_goal_buffer; |
| 471 | |
| 472 | climber_goal_buffer.Finish( |
| 473 | frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 474 | *climber_goal_buffer.fbb(), climber_position)); |
| 475 | |
| 476 | const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal |
| 477 | *climber_goal = &climber_goal_buffer.message(); |
| 478 | |
| 479 | const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus> |
| 480 | climber_status_offset = climber_.Iterate( |
| 481 | climber_goal, position->climber(), |
| 482 | output != nullptr ? &output_struct.climber_voltage : nullptr, |
| 483 | status->fbb()); |
| 484 | |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 485 | double max_intake_pivot_position = 0; |
| 486 | double min_intake_pivot_position = 0; |
| 487 | |
| 488 | aos::FlatbufferFixedAllocatorArray< |
| 489 | frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512> |
| 490 | intake_pivot_goal_buffer; |
| 491 | |
| 492 | intake_pivot_goal_buffer.Finish( |
| 493 | frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 494 | *intake_pivot_goal_buffer.fbb(), intake_pivot_position)); |
| 495 | |
| 496 | const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal |
| 497 | *intake_pivot_goal = &intake_pivot_goal_buffer.message(); |
| 498 | |
| 499 | double *intake_output = |
| 500 | (output != nullptr ? &output_struct.intake_pivot_voltage : nullptr); |
| 501 | |
| 502 | const bool disabled = intake_pivot_.Correct( |
| 503 | intake_pivot_goal, position->intake_pivot(), intake_output == nullptr); |
| 504 | |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 505 | // TODO(max): Change how we handle the collision with the turret and |
| 506 | // intake to be clearer |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 507 | const flatbuffers::Offset<ShooterStatus> shooter_status_offset = |
| 508 | shooter_.Iterate( |
| 509 | position, |
| 510 | unsafe_goal != nullptr ? unsafe_goal->shooter_goal() : nullptr, |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 511 | unsafe_goal != nullptr ? unsafe_goal->fire() : false, |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 512 | output != nullptr ? &output_struct.catapult_voltage : nullptr, |
| 513 | output != nullptr ? &output_struct.altitude_voltage : nullptr, |
| 514 | output != nullptr ? &output_struct.turret_voltage : nullptr, |
| 515 | output != nullptr ? &output_struct.retention_roller_voltage : nullptr, |
Maxwell Henderson | d5bf47a | 2024-02-23 17:16:48 -0800 | [diff] [blame] | 516 | output != nullptr |
| 517 | ? &output_struct.retention_roller_stator_current_limit |
| 518 | : nullptr, |
Maxwell Henderson | 461a8a4 | 2024-02-23 17:07:39 -0800 | [diff] [blame] | 519 | robot_state().voltage_battery(), &collision_avoidance_, |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 520 | intake_pivot_.estimated_position(), &max_intake_pivot_position, |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 521 | &min_intake_pivot_position, move_turret_to_standby, status->fbb()); |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 522 | |
| 523 | intake_pivot_.set_min_position(min_intake_pivot_position); |
| 524 | intake_pivot_.set_max_position(max_intake_pivot_position); |
| 525 | |
| 526 | // Calculate the loops for a cycle. |
| 527 | const double voltage = intake_pivot_.UpdateController(disabled); |
| 528 | |
| 529 | intake_pivot_.UpdateObserver(voltage); |
| 530 | |
| 531 | // Write out all the voltages. |
| 532 | if (intake_output) { |
| 533 | *intake_output = voltage; |
| 534 | } |
| 535 | |
| 536 | const flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> |
| 537 | intake_pivot_status_offset = intake_pivot_.MakeStatus(status->fbb()); |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 538 | |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 539 | aos::FlatbufferFixedAllocatorArray< |
| 540 | frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 512> |
| 541 | note_goal_buffer; |
| 542 | |
| 543 | note_goal_buffer.Finish( |
| 544 | frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 545 | *note_goal_buffer.fbb(), extend_position)); |
| 546 | |
| 547 | const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal |
| 548 | *note_goal = ¬e_goal_buffer.message(); |
| 549 | |
| 550 | const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus> |
| 551 | extend_status_offset = extend_.Iterate( |
| 552 | note_goal, position->extend(), |
| 553 | output != nullptr ? &output_struct.extend_voltage : nullptr, |
| 554 | status->fbb()); |
| 555 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 556 | if (output) { |
| 557 | output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct))); |
| 558 | } |
| 559 | |
| 560 | Status::Builder status_builder = status->MakeBuilder<Status>(); |
Niko Sohmers | afc51fe | 2024-01-29 17:48:35 -0800 | [diff] [blame] | 561 | |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 562 | const bool zeroed = intake_pivot_.zeroed() && climber_.zeroed() && |
| 563 | shooter_.zeroed() && extend_.zeroed(); |
| 564 | const bool estopped = intake_pivot_.estopped() || climber_.estopped() || |
| 565 | shooter_.estopped() || extend_.estopped(); |
Niko Sohmers | afc51fe | 2024-01-29 17:48:35 -0800 | [diff] [blame] | 566 | |
| 567 | status_builder.add_zeroed(zeroed); |
| 568 | status_builder.add_estopped(estopped); |
Filip Kujawa | 102a9b2 | 2024-02-18 09:40:23 -0800 | [diff] [blame] | 569 | status_builder.add_intake_roller(intake_roller_state); |
| 570 | status_builder.add_intake_pivot(intake_pivot_status_offset); |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 571 | status_builder.add_transfer_roller(transfer_roller_status); |
Filip Kujawa | 102a9b2 | 2024-02-18 09:40:23 -0800 | [diff] [blame] | 572 | status_builder.add_climber(climber_status_offset); |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 573 | status_builder.add_shooter(shooter_status_offset); |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 574 | status_builder.add_collided(collided); |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 575 | status_builder.add_extend_roller(extend_roller_status); |
| 576 | status_builder.add_extend_status(extend_status); |
| 577 | status_builder.add_extend(extend_status_offset); |
| 578 | status_builder.add_state(state_); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 579 | |
| 580 | (void)status->Send(status_builder.Finish()); |
| 581 | } |
| 582 | |
| 583 | double Superstructure::robot_velocity() const { |
| 584 | return (drivetrain_status_fetcher_.get() != nullptr |
| 585 | ? drivetrain_status_fetcher_->robot_speed() |
| 586 | : 0.0); |
| 587 | } |
| 588 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 589 | } // namespace y2024::control_loops::superstructure |