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