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