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