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