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