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