milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 1 | #include "y2022/control_loops/superstructure/superstructure.h" |
| 2 | |
| 3 | #include "aos/events/event_loop.h" |
Milind Upadhyay | 225156b | 2022-02-25 22:42:12 -0800 | [diff] [blame] | 4 | #include "y2022/control_loops/superstructure/collision_avoidance.h" |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 5 | |
| 6 | namespace y2022 { |
| 7 | namespace control_loops { |
| 8 | namespace superstructure { |
| 9 | |
| 10 | using frc971::control_loops::AbsoluteEncoderProfiledJointStatus; |
| 11 | using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus; |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 12 | using frc971::control_loops::RelativeEncoderProfiledJointStatus; |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 13 | |
| 14 | Superstructure::Superstructure(::aos::EventLoop *event_loop, |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 15 | std::shared_ptr<const constants::Values> values, |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 16 | const ::std::string &name) |
| 17 | : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop, |
Siddhant Kanwar | 0e37f59 | 2022-02-21 19:26:50 -0800 | [diff] [blame] | 18 | name), |
Austin Schuh | 39f26f6 | 2022-02-24 21:34:46 -0800 | [diff] [blame] | 19 | values_(values), |
| 20 | climber_(values_->climber.subsystem_params), |
| 21 | intake_front_(values_->intake_front.subsystem_params), |
| 22 | intake_back_(values_->intake_back.subsystem_params), |
| 23 | turret_(values_->turret.subsystem_params), |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 24 | catapult_(*values_), |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 25 | drivetrain_status_fetcher_( |
| 26 | event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>( |
Austin Schuh | 39f26f6 | 2022-02-24 21:34:46 -0800 | [diff] [blame] | 27 | "/drivetrain")), |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 28 | can_position_fetcher_( |
| 29 | event_loop->MakeFetcher<CANPosition>("/superstructure")) { |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 30 | event_loop->SetRuntimeRealtimePriority(30); |
| 31 | } |
| 32 | |
| 33 | void Superstructure::RunIteration(const Goal *unsafe_goal, |
Siddhant Kanwar | 0e37f59 | 2022-02-21 19:26:50 -0800 | [diff] [blame] | 34 | const Position *position, |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 35 | aos::Sender<Output>::Builder *output, |
| 36 | aos::Sender<Status>::Builder *status) { |
| 37 | if (WasReset()) { |
| 38 | AOS_LOG(ERROR, "WPILib reset, restarting\n"); |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 39 | intake_front_.Reset(); |
| 40 | intake_back_.Reset(); |
| 41 | turret_.Reset(); |
| 42 | climber_.Reset(); |
Austin Schuh | 39f26f6 | 2022-02-24 21:34:46 -0800 | [diff] [blame] | 43 | catapult_.Reset(); |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 46 | OutputT output_struct; |
Milind Upadhyay | 225156b | 2022-02-25 22:42:12 -0800 | [diff] [blame] | 47 | |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 48 | aos::FlatbufferFixedAllocatorArray< |
| 49 | frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal, 64> |
| 50 | turret_goal_buffer; |
| 51 | |
| 52 | const aos::monotonic_clock::time_point timestamp = |
| 53 | event_loop()->context().monotonic_event_time; |
Milind Upadhyay | 225156b | 2022-02-25 22:42:12 -0800 | [diff] [blame] | 54 | |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 55 | drivetrain_status_fetcher_.Fetch(); |
| 56 | const float velocity = robot_velocity(); |
| 57 | |
James Kuszmaul | 84083f4 | 2022-02-27 17:24:38 -0800 | [diff] [blame] | 58 | const turret::Aimer::Goal *auto_aim_goal = nullptr; |
| 59 | if (drivetrain_status_fetcher_.get() != nullptr) { |
| 60 | aimer_.Update(drivetrain_status_fetcher_.get(), |
| 61 | turret::Aimer::ShotMode::kShootOnTheFly); |
| 62 | auto_aim_goal = aimer_.TurretGoal(); |
| 63 | } |
| 64 | |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 65 | const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal |
| 66 | *turret_goal = nullptr; |
| 67 | double roller_speed_compensated_front = 0.0; |
| 68 | double roller_speed_compensated_back = 0.0; |
| 69 | double transfer_roller_speed = 0.0; |
| 70 | double flipper_arms_voltage = 0.0; |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 71 | |
| 72 | if (unsafe_goal != nullptr) { |
| 73 | roller_speed_compensated_front = |
| 74 | unsafe_goal->roller_speed_front() + |
| 75 | std::max(velocity * unsafe_goal->roller_speed_compensation(), 0.0); |
| 76 | |
| 77 | roller_speed_compensated_back = |
| 78 | unsafe_goal->roller_speed_back() - |
| 79 | std::min(velocity * unsafe_goal->roller_speed_compensation(), 0.0); |
| 80 | |
| 81 | transfer_roller_speed = unsafe_goal->transfer_roller_speed(); |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 82 | |
James Kuszmaul | 84083f4 | 2022-02-27 17:24:38 -0800 | [diff] [blame] | 83 | turret_goal = |
| 84 | unsafe_goal->auto_aim() ? auto_aim_goal : unsafe_goal->turret(); |
| 85 | } |
Austin Schuh | 39f26f6 | 2022-02-24 21:34:46 -0800 | [diff] [blame] | 86 | |
Milind Upadhyay | 803bbf0 | 2022-03-11 17:56:26 -0800 | [diff] [blame^] | 87 | // Superstructure state machine: |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 88 | // 1. IDLE: Wait until an intake beambreak is triggerred, meaning that a ball |
| 89 | // is being intaked. This means that the transfer rollers have a ball. If |
| 90 | // we've been waiting here for too long without any beambreak triggered, the |
| 91 | // ball got lost, so reset. |
| 92 | // 2. TRANSFERRING: Until the turret reaches the loading position where the |
| 93 | // ball can be transferred into the catapult, wiggle the ball in place. |
| 94 | // Once the turret reaches the loading position, send the ball forward with |
| 95 | // the transfer rollers until the turret beambreak is triggered. |
| 96 | // If we have been in this state for too long, the ball probably got lost so |
| 97 | // reset back to IDLE. |
| 98 | // 3. LOADING: To load the ball into the catapult, put the flippers at the |
| 99 | // feeding speed. Wait for a timeout, and then wait until the ball has gone |
| 100 | // past the turret beambreak and the flippers have stopped moving, meaning |
| 101 | // that the ball is fully loaded in the catapult. |
| 102 | // 4. LOADED: Wait until the user asks us to fire to transition to the |
| 103 | // shooting stage. If asked to cancel the shot, reset back to the IDLE state. |
| 104 | // 5. SHOOTING: Open the flippers to get ready for the shot. If they don't |
| 105 | // open quickly enough, try reseating the ball and going back to the LOADING |
| 106 | // stage, which moves the flippers in the opposite direction first. |
| 107 | // Now, hold the flippers open and wait until the turret has reached its |
| 108 | // aiming goal. Once the turret is ready, tell the catapult to fire. |
| 109 | // If the flippers move back for some reason now, it could damage the |
| 110 | // catapult, so estop it. Otherwise, wait until the catapult shoots a ball and |
| 111 | // goes back to its return position. We have now finished the shot, so return |
| 112 | // to IDLE. |
| 113 | |
Milind Upadhyay | 803bbf0 | 2022-03-11 17:56:26 -0800 | [diff] [blame^] | 114 | // If we started off preloaded, skip to the loaded state. |
| 115 | // Make sure we weren't already there just in case. |
| 116 | if (unsafe_goal != nullptr && unsafe_goal->preloaded()) { |
| 117 | switch (state_) { |
| 118 | case SuperstructureState::IDLE: |
| 119 | case SuperstructureState::TRANSFERRING: |
| 120 | case SuperstructureState::LOADING: |
| 121 | state_ = SuperstructureState::LOADED; |
| 122 | loading_timer_ = timestamp; |
| 123 | break; |
| 124 | case SuperstructureState::LOADED: |
| 125 | case SuperstructureState::SHOOTING: |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 130 | const bool is_spitting = ((intake_state_ == IntakeState::INTAKE_FRONT_BALL && |
| 131 | transfer_roller_speed < 0) || |
| 132 | (intake_state_ == IntakeState::INTAKE_BACK_BALL && |
| 133 | transfer_roller_speed > 0)); |
| 134 | |
| 135 | // Intake handling should happen regardless of the turret state |
| 136 | if (position->intake_beambreak_front() || position->intake_beambreak_back()) { |
| 137 | if (intake_state_ == IntakeState::NO_BALL) { |
| 138 | if (position->intake_beambreak_front()) { |
| 139 | intake_state_ = IntakeState::INTAKE_FRONT_BALL; |
| 140 | } else if (position->intake_beambreak_back()) { |
| 141 | intake_state_ = IntakeState::INTAKE_BACK_BALL; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | intake_beambreak_timer_ = timestamp; |
| 146 | } |
| 147 | |
Milind Upadhyay | 5dfabef | 2022-03-06 14:08:15 -0800 | [diff] [blame] | 148 | if (timestamp > |
| 149 | intake_beambreak_timer_ + constants::Values::kBallLostTime()) { |
| 150 | intake_state_ = IntakeState::NO_BALL; |
| 151 | } |
| 152 | |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 153 | if (intake_state_ != IntakeState::NO_BALL) { |
| 154 | // Block intaking in |
| 155 | roller_speed_compensated_front = 0.0; |
| 156 | roller_speed_compensated_back = 0.0; |
| 157 | |
| 158 | const double wiggle_voltage = |
| 159 | (intake_state_ == IntakeState::INTAKE_FRONT_BALL |
| 160 | ? constants::Values::kTransferRollerFrontWiggleVoltage() |
| 161 | : constants::Values::kTransferRollerBackWiggleVoltage()); |
| 162 | // Wiggle transfer rollers: send the ball back and forth while waiting |
| 163 | // for the turret or waiting for another shot to be completed |
| 164 | if ((intake_state_ == IntakeState::INTAKE_FRONT_BALL && |
| 165 | position->intake_beambreak_front()) || |
| 166 | (intake_state_ == IntakeState::INTAKE_BACK_BALL && |
| 167 | position->intake_beambreak_back())) { |
| 168 | transfer_roller_speed = -wiggle_voltage; |
| 169 | } else { |
| 170 | transfer_roller_speed = wiggle_voltage; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | switch (state_) { |
| 175 | case SuperstructureState::IDLE: { |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 176 | if (is_spitting) { |
| 177 | intake_state_ = IntakeState::NO_BALL; |
| 178 | } |
| 179 | |
| 180 | if (intake_state_ == IntakeState::NO_BALL || |
| 181 | !(position->intake_beambreak_front() || |
| 182 | position->intake_beambreak_back())) { |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | state_ = SuperstructureState::TRANSFERRING; |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 187 | // Save the side the ball is on for later |
| 188 | |
| 189 | break; |
| 190 | } |
| 191 | case SuperstructureState::TRANSFERRING: { |
| 192 | // If we've been transferring for too long, the ball probably got lost |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 193 | if (intake_state_ == IntakeState::NO_BALL) { |
| 194 | state_ = SuperstructureState::IDLE; |
| 195 | break; |
| 196 | } |
| 197 | |
| 198 | double turret_loading_position = |
| 199 | (intake_state_ == IntakeState::INTAKE_FRONT_BALL |
| 200 | ? constants::Values::kTurretFrontIntakePos() |
| 201 | : constants::Values::kTurretBackIntakePos()); |
| 202 | |
Ravago Jones | 2c39086 | 2022-03-11 17:23:21 -0800 | [diff] [blame] | 203 | // Turn to the loading position as close to the current position as |
| 204 | // possible |
| 205 | // Strategy is copied from frc971/control_loops/aiming/aiming.cc |
| 206 | turret_loading_position = |
| 207 | turret_.estimated_position() + |
| 208 | aos::math::NormalizeAngle(turret_loading_position - |
| 209 | turret_.estimated_position()); |
| 210 | // if out of range, reset back to within +/- pi of zero. |
| 211 | if (turret_loading_position > constants::Values::kTurretRange().upper || |
| 212 | turret_loading_position < constants::Values::kTurretRange().lower) { |
| 213 | turret_loading_position = |
| 214 | aos::math::NormalizeAngle(turret_loading_position); |
| 215 | } |
| 216 | |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 217 | turret_goal_buffer.Finish( |
| 218 | frc971::control_loops:: |
| 219 | CreateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 220 | *turret_goal_buffer.fbb(), turret_loading_position)); |
| 221 | turret_goal = &turret_goal_buffer.message(); |
| 222 | |
| 223 | const bool turret_near_goal = |
| 224 | std::abs(turret_.estimated_position() - turret_loading_position) < |
| 225 | kTurretGoalThreshold; |
| 226 | if (!turret_near_goal) { |
| 227 | break; // Wait for turret to reach the chosen intake |
| 228 | } |
| 229 | |
| 230 | // Transfer rollers and flipper arm belt on |
| 231 | transfer_roller_speed = |
| 232 | (intake_state_ == IntakeState::INTAKE_FRONT_BALL |
| 233 | ? constants::Values::kTransferRollerFrontVoltage() |
| 234 | : constants::Values::kTransferRollerBackVoltage()); |
| 235 | flipper_arms_voltage = constants::Values::kFlipperFeedVoltage(); |
| 236 | |
| 237 | // Ball is in catapult |
| 238 | if (position->turret_beambreak()) { |
| 239 | intake_state_ = IntakeState::NO_BALL; |
| 240 | state_ = SuperstructureState::LOADING; |
| 241 | loading_timer_ = timestamp; |
| 242 | } |
| 243 | break; |
| 244 | } |
| 245 | case SuperstructureState::LOADING: { |
| 246 | flipper_arms_voltage = constants::Values::kFlipperFeedVoltage(); |
| 247 | |
| 248 | // Keep feeding for kExtraLoadingTime |
| 249 | |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 250 | // The ball should go past the turret beambreak to be loaded. |
Milind Upadhyay | 5dfabef | 2022-03-06 14:08:15 -0800 | [diff] [blame] | 251 | // If we got a CAN reading not too long ago, the flippers should have |
| 252 | // also stopped. |
Milind Upadhyay | 47e0d03 | 2022-03-05 23:06:25 -0800 | [diff] [blame] | 253 | if (position->turret_beambreak()) { |
| 254 | loading_timer_ = timestamp; |
| 255 | } else if (timestamp > |
| 256 | loading_timer_ + constants::Values::kExtraLoadingTime()) { |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 257 | state_ = SuperstructureState::LOADED; |
| 258 | reseating_in_catapult_ = false; |
| 259 | } |
| 260 | break; |
| 261 | } |
| 262 | case SuperstructureState::LOADED: { |
| 263 | if (unsafe_goal != nullptr) { |
| 264 | if (unsafe_goal->cancel_shot()) { |
| 265 | // Cancel the shot process |
| 266 | state_ = SuperstructureState::IDLE; |
| 267 | } else if (unsafe_goal->fire()) { |
| 268 | // Start if we were asked to and the turret is at goal |
| 269 | state_ = SuperstructureState::SHOOTING; |
| 270 | prev_shot_count_ = catapult_.shot_count(); |
| 271 | |
| 272 | // Reset opening timeout |
| 273 | flipper_opening_start_time_ = timestamp; |
| 274 | } |
| 275 | } |
| 276 | break; |
| 277 | } |
| 278 | case SuperstructureState::SHOOTING: { |
| 279 | // Opening flipper arms could fail, wait until they are open using their |
| 280 | // potentiometers (the member below is just named encoder). |
| 281 | // Be a little more lenient if the flippers were already open in case of |
| 282 | // noise or collisions. |
| 283 | const double flipper_open_position = |
| 284 | (flippers_open_ ? constants::Values::kReseatFlipperPosition() |
| 285 | : constants::Values::kFlipperOpenPosition()); |
milind-u | 37dc40b | 2022-03-08 19:51:27 -0800 | [diff] [blame] | 286 | |
| 287 | // TODO(milind): add left arm back once it's fixed |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 288 | flippers_open_ = |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 289 | position->flipper_arm_right()->encoder() >= flipper_open_position; |
| 290 | |
| 291 | if (flippers_open_) { |
| 292 | // Hold at kFlipperHoldVoltage |
| 293 | flipper_arms_voltage = constants::Values::kFlipperHoldVoltage(); |
| 294 | } else { |
| 295 | // Open at kFlipperOpenVoltage |
| 296 | flipper_arms_voltage = constants::Values::kFlipperOpenVoltage(); |
| 297 | } |
| 298 | |
| 299 | if (!flippers_open_ && |
| 300 | timestamp > |
| 301 | loading_timer_ + constants::Values::kFlipperOpeningTimeout()) { |
| 302 | // Reseat the ball and try again |
| 303 | state_ = SuperstructureState::LOADING; |
| 304 | loading_timer_ = timestamp; |
| 305 | reseating_in_catapult_ = true; |
| 306 | break; |
| 307 | } |
| 308 | |
| 309 | const bool turret_near_goal = |
| 310 | turret_goal != nullptr && |
| 311 | std::abs(turret_goal->unsafe_goal() - turret_.position()) < |
| 312 | kTurretGoalThreshold; |
Henry Speiser | 28288cc | 2022-03-09 22:59:24 -0800 | [diff] [blame] | 313 | const bool collided = collision_avoidance_.IsCollided( |
| 314 | {.intake_front_position = intake_front_.estimated_position(), |
| 315 | .intake_back_position = intake_back_.estimated_position(), |
| 316 | .turret_position = turret_.estimated_position(), |
| 317 | .shooting = true}); |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 318 | |
Henry Speiser | 28288cc | 2022-03-09 22:59:24 -0800 | [diff] [blame] | 319 | // If the turret reached the aiming goal and the catapult is safe to move |
| 320 | // up, fire! |
| 321 | if (flippers_open_ && turret_near_goal && !collided) { |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 322 | fire_ = true; |
| 323 | } |
| 324 | |
| 325 | // If we started firing and the flippers closed a bit, estop to prevent |
| 326 | // damage |
| 327 | if (fire_ && !flippers_open_) { |
| 328 | catapult_.Estop(); |
| 329 | } |
| 330 | |
| 331 | const bool near_return_position = |
| 332 | (unsafe_goal != nullptr && unsafe_goal->has_catapult() && |
| 333 | unsafe_goal->catapult()->has_return_position() && |
| 334 | std::abs(unsafe_goal->catapult()->return_position()->unsafe_goal() - |
| 335 | catapult_.estimated_position()) < kCatapultGoalThreshold); |
| 336 | |
| 337 | // Once the shot is complete and the catapult is back to its return |
| 338 | // position, go back to IDLE |
| 339 | if (catapult_.shot_count() > prev_shot_count_ && near_return_position) { |
| 340 | prev_shot_count_ = catapult_.shot_count(); |
| 341 | fire_ = false; |
| 342 | state_ = SuperstructureState::IDLE; |
| 343 | } |
| 344 | |
| 345 | break; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | collision_avoidance_.UpdateGoal( |
| 350 | {.intake_front_position = intake_front_.estimated_position(), |
| 351 | .intake_back_position = intake_back_.estimated_position(), |
Henry Speiser | 28288cc | 2022-03-09 22:59:24 -0800 | [diff] [blame] | 352 | .turret_position = turret_.estimated_position(), |
| 353 | .shooting = state_ == SuperstructureState::SHOOTING}, |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 354 | turret_goal); |
| 355 | |
| 356 | turret_.set_min_position(collision_avoidance_.min_turret_goal()); |
| 357 | turret_.set_max_position(collision_avoidance_.max_turret_goal()); |
| 358 | intake_front_.set_min_position(collision_avoidance_.min_intake_front_goal()); |
| 359 | intake_front_.set_max_position(collision_avoidance_.max_intake_front_goal()); |
| 360 | intake_back_.set_min_position(collision_avoidance_.min_intake_back_goal()); |
| 361 | intake_back_.set_max_position(collision_avoidance_.max_intake_back_goal()); |
| 362 | |
James Kuszmaul | 84083f4 | 2022-02-27 17:24:38 -0800 | [diff] [blame] | 363 | const flatbuffers::Offset<AimerStatus> aimer_offset = |
| 364 | aimer_.PopulateStatus(status->fbb()); |
| 365 | |
Milind Upadhyay | 5dfabef | 2022-03-06 14:08:15 -0800 | [diff] [blame] | 366 | // Disable the catapult if we want to restart to prevent damage with |
| 367 | // flippers |
Austin Schuh | 39f26f6 | 2022-02-24 21:34:46 -0800 | [diff] [blame] | 368 | const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus> |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 369 | catapult_status_offset = |
| 370 | catapult_.Iterate(unsafe_goal, position, |
| 371 | output != nullptr && !catapult_.estopped() |
| 372 | ? &(output_struct.catapult_voltage) |
| 373 | : nullptr, |
| 374 | fire_, status->fbb()); |
Siddhant Kanwar | 0e37f59 | 2022-02-21 19:26:50 -0800 | [diff] [blame] | 375 | |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 376 | const flatbuffers::Offset<RelativeEncoderProfiledJointStatus> |
Siddhant Kanwar | 0e37f59 | 2022-02-21 19:26:50 -0800 | [diff] [blame] | 377 | climber_status_offset = climber_.Iterate( |
| 378 | unsafe_goal != nullptr ? unsafe_goal->climber() : nullptr, |
Austin Schuh | 7f12036 | 2022-03-05 17:10:11 -0800 | [diff] [blame] | 379 | position->climber(), |
| 380 | output != nullptr ? &output_struct.climber_voltage : nullptr, |
| 381 | status->fbb()); |
Siddhant Kanwar | 0e37f59 | 2022-02-21 19:26:50 -0800 | [diff] [blame] | 382 | |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 383 | const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus> |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 384 | intake_status_offset_front = intake_front_.Iterate( |
| 385 | unsafe_goal != nullptr ? unsafe_goal->intake_front() : nullptr, |
Austin Schuh | 7f12036 | 2022-03-05 17:10:11 -0800 | [diff] [blame] | 386 | position->intake_front(), |
| 387 | output != nullptr ? &output_struct.intake_voltage_front : nullptr, |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 388 | status->fbb()); |
| 389 | |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 390 | const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus> |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 391 | intake_status_offset_back = intake_back_.Iterate( |
| 392 | unsafe_goal != nullptr ? unsafe_goal->intake_back() : nullptr, |
Austin Schuh | 7f12036 | 2022-03-05 17:10:11 -0800 | [diff] [blame] | 393 | position->intake_back(), |
| 394 | output != nullptr ? &output_struct.intake_voltage_back : nullptr, |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 395 | status->fbb()); |
| 396 | |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 397 | const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus> |
Austin Schuh | 7f12036 | 2022-03-05 17:10:11 -0800 | [diff] [blame] | 398 | turret_status_offset = turret_.Iterate( |
| 399 | turret_goal, position->turret(), |
| 400 | output != nullptr ? &output_struct.turret_voltage : nullptr, |
| 401 | status->fbb()); |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 402 | |
Siddhant Kanwar | 0e37f59 | 2022-02-21 19:26:50 -0800 | [diff] [blame] | 403 | if (output != nullptr) { |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 404 | output_struct.roller_voltage_front = roller_speed_compensated_front; |
| 405 | output_struct.roller_voltage_back = roller_speed_compensated_back; |
| 406 | output_struct.transfer_roller_voltage = transfer_roller_speed; |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 407 | output_struct.flipper_arms_voltage = flipper_arms_voltage; |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 408 | |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 409 | output->CheckOk(output->Send(Output::Pack(*output->fbb(), &output_struct))); |
| 410 | } |
| 411 | |
| 412 | Status::Builder status_builder = status->MakeBuilder<Status>(); |
| 413 | |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 414 | const bool zeroed = intake_front_.zeroed() && intake_back_.zeroed() && |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 415 | turret_.zeroed() && climber_.zeroed() && |
| 416 | catapult_.zeroed(); |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 417 | const bool estopped = intake_front_.estopped() || intake_back_.estopped() || |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 418 | turret_.estopped() || climber_.estopped() || |
| 419 | catapult_.estopped(); |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 420 | |
| 421 | status_builder.add_zeroed(zeroed); |
| 422 | status_builder.add_estopped(estopped); |
| 423 | |
| 424 | status_builder.add_intake_front(intake_status_offset_front); |
| 425 | status_builder.add_intake_back(intake_status_offset_back); |
| 426 | status_builder.add_turret(turret_status_offset); |
Siddhant Kanwar | 0e37f59 | 2022-02-21 19:26:50 -0800 | [diff] [blame] | 427 | status_builder.add_climber(climber_status_offset); |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 428 | |
Austin Schuh | 39f26f6 | 2022-02-24 21:34:46 -0800 | [diff] [blame] | 429 | status_builder.add_catapult(catapult_status_offset); |
| 430 | status_builder.add_solve_time(catapult_.solve_time()); |
Austin Schuh | 80fc275 | 2022-02-25 13:33:56 -0800 | [diff] [blame] | 431 | status_builder.add_shot_count(catapult_.shot_count()); |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 432 | status_builder.add_mpc_active(catapult_.mpc_active()); |
| 433 | |
| 434 | status_builder.add_flippers_open(flippers_open_); |
| 435 | status_builder.add_reseating_in_catapult(reseating_in_catapult_); |
| 436 | status_builder.add_fire(fire_); |
| 437 | status_builder.add_state(state_); |
| 438 | status_builder.add_intake_state(intake_state_); |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 439 | |
James Kuszmaul | 84083f4 | 2022-02-27 17:24:38 -0800 | [diff] [blame] | 440 | status_builder.add_aimer(aimer_offset); |
| 441 | |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 442 | (void)status->Send(status_builder.Finish()); |
| 443 | } |
| 444 | |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 445 | double Superstructure::robot_velocity() const { |
| 446 | return (drivetrain_status_fetcher_.get() != nullptr |
| 447 | ? drivetrain_status_fetcher_->robot_speed() |
| 448 | : 0.0); |
| 449 | } |
| 450 | |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 451 | } // namespace superstructure |
| 452 | } // namespace control_loops |
| 453 | } // namespace y2022 |