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