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