Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 1 | #include "y2024/control_loops/superstructure/shooter.h" |
| 2 | |
| 3 | #include "aos/flatbuffers.h" |
| 4 | #include "aos/flatbuffers/base.h" |
| 5 | #include "frc971/control_loops/aiming/aiming.h" |
| 6 | #include "y2024/control_loops/superstructure/catapult/catapult_plant.h" |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 7 | #include "y2024/control_loops/superstructure/collision_avoidance.h" |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 8 | |
| 9 | namespace y2024::control_loops::superstructure { |
| 10 | |
| 11 | using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus; |
| 12 | |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 13 | constexpr double kCatapultActivationThreshold = 0.01; |
| 14 | |
| 15 | Shooter::Shooter(aos::EventLoop *event_loop, const Constants *robot_constants) |
| 16 | : drivetrain_status_fetcher_( |
| 17 | event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>( |
| 18 | "/drivetrain")), |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 19 | robot_constants_(robot_constants), |
| 20 | catapult_( |
| 21 | robot_constants->common()->catapult(), |
| 22 | robot_constants->robot()->catapult_constants()->zeroing_constants()), |
| 23 | turret_( |
| 24 | robot_constants_->common()->turret(), |
| 25 | robot_constants_->robot()->turret_constants()->zeroing_constants()), |
| 26 | altitude_( |
| 27 | robot_constants_->common()->altitude(), |
| 28 | robot_constants_->robot()->altitude_constants()->zeroing_constants()), |
| 29 | aimer_(event_loop, robot_constants_), |
| 30 | interpolation_table_( |
| 31 | y2024::constants::Values::InterpolationTableFromFlatbuffer( |
Maxwell Henderson | 6b1be31 | 2024-02-28 20:15:06 -0800 | [diff] [blame] | 32 | robot_constants_->common()->shooter_interpolation_table())), |
| 33 | debouncer_(std::chrono::milliseconds(100), std::chrono::milliseconds(8)) { |
| 34 | } |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 35 | |
| 36 | flatbuffers::Offset<y2024::control_loops::superstructure::ShooterStatus> |
| 37 | Shooter::Iterate( |
| 38 | const y2024::control_loops::superstructure::Position *position, |
| 39 | const y2024::control_loops::superstructure::ShooterGoal *shooter_goal, |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 40 | bool fire, double *catapult_output, double *altitude_output, |
| 41 | double *turret_output, double *retention_roller_output, |
Maxwell Henderson | d5bf47a | 2024-02-23 17:16:48 -0800 | [diff] [blame] | 42 | double *retention_roller_stator_current_limit, double /*battery_voltage*/, |
Austin Schuh | 027fd62 | 2024-03-01 21:26:07 -0800 | [diff] [blame] | 43 | CollisionAvoidance *collision_avoidance, const double extend_position, |
| 44 | const double extend_goal, double *max_extend_position, |
| 45 | double *min_extend_position, const double intake_pivot_position, |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 46 | double *max_intake_pivot_position, double *min_intake_pivot_position, |
Austin Schuh | 027fd62 | 2024-03-01 21:26:07 -0800 | [diff] [blame] | 47 | flatbuffers::FlatBufferBuilder *fbb, |
Maxwell Henderson | 6b1be31 | 2024-02-28 20:15:06 -0800 | [diff] [blame] | 48 | aos::monotonic_clock::time_point monotonic_now) { |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 49 | drivetrain_status_fetcher_.Fetch(); |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 50 | |
| 51 | // If our current is over the minimum current and our velocity is under our |
| 52 | // maximum velocity, then set loaded to true. If we are preloaded set it to |
| 53 | // true as well. |
Maxwell Henderson | 6b1be31 | 2024-02-28 20:15:06 -0800 | [diff] [blame] | 54 | debouncer_.Update(position->catapult_beambreak() || |
| 55 | (shooter_goal != nullptr && shooter_goal->preloaded()), |
| 56 | monotonic_now); |
| 57 | const bool piece_loaded = debouncer_.state(); |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 58 | |
| 59 | aos::fbs::FixedStackAllocator<aos::fbs::Builder< |
| 60 | frc971::control_loops:: |
| 61 | StaticZeroingSingleDOFProfiledSubsystemGoalStatic>::kBufferSize> |
| 62 | turret_allocator; |
| 63 | |
| 64 | aos::fbs::Builder< |
| 65 | frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoalStatic> |
| 66 | turret_goal_builder(&turret_allocator); |
| 67 | |
| 68 | aos::fbs::FixedStackAllocator<aos::fbs::Builder< |
| 69 | frc971::control_loops:: |
| 70 | StaticZeroingSingleDOFProfiledSubsystemGoalStatic>::kBufferSize> |
| 71 | altitude_allocator; |
| 72 | |
| 73 | aos::fbs::Builder< |
| 74 | frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoalStatic> |
| 75 | altitude_goal_builder(&altitude_allocator); |
| 76 | |
| 77 | const double distance_to_goal = aimer_.DistanceToGoal(); |
| 78 | |
| 79 | // Always retain the game piece if we are enabled. |
| 80 | if (retention_roller_output != nullptr) { |
| 81 | *retention_roller_output = |
| 82 | robot_constants_->common()->retention_roller_voltages()->retaining(); |
Maxwell Henderson | d5bf47a | 2024-02-23 17:16:48 -0800 | [diff] [blame] | 83 | |
| 84 | if (piece_loaded) { |
| 85 | *retention_roller_stator_current_limit = |
| 86 | robot_constants_->common() |
| 87 | ->current_limits() |
| 88 | ->slower_retention_roller_stator_current_limit(); |
| 89 | } else { |
| 90 | *retention_roller_stator_current_limit = |
| 91 | robot_constants_->common() |
| 92 | ->current_limits() |
| 93 | ->retention_roller_stator_current_limit(); |
| 94 | } |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | bool aiming = false; |
| 98 | |
Austin Schuh | 027fd62 | 2024-03-01 21:26:07 -0800 | [diff] [blame] | 99 | if (shooter_goal == nullptr || !shooter_goal->auto_aim() || |
| 100 | (!piece_loaded && state_ == CatapultState::READY)) { |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 101 | // We don't have the note so we should be ready to intake it. |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 102 | PopulateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 103 | turret_goal_builder.get(), |
| 104 | robot_constants_->common()->turret_loading_position()); |
| 105 | |
| 106 | PopulateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 107 | altitude_goal_builder.get(), |
| 108 | robot_constants_->common()->altitude_loading_position()); |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 109 | |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 110 | } else { |
| 111 | // We have a game piece, lets start aiming. |
| 112 | if (drivetrain_status_fetcher_.get() != nullptr) { |
| 113 | aiming = true; |
| 114 | aimer_.Update(drivetrain_status_fetcher_.get(), |
| 115 | frc971::control_loops::aiming::ShotMode::kShootOnTheFly, |
| 116 | turret_goal_builder.get()); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // We have a game piece and are being asked to aim. |
| 121 | constants::Values::ShotParams shot_params; |
| 122 | if (piece_loaded && shooter_goal != nullptr && shooter_goal->auto_aim() && |
| 123 | interpolation_table_.GetInRange(distance_to_goal, &shot_params)) { |
| 124 | PopulateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 125 | altitude_goal_builder.get(), shot_params.shot_altitude_angle); |
| 126 | } |
| 127 | |
| 128 | // The builder will contain either the auto-aim goal, or the loading goal. Use |
| 129 | // it if we have no goal, or no subsystem goal, or if we are auto-aiming. |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 130 | |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 131 | const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal |
| 132 | *turret_goal = (shooter_goal != nullptr && !shooter_goal->auto_aim() && |
Austin Schuh | 027fd62 | 2024-03-01 21:26:07 -0800 | [diff] [blame] | 133 | shooter_goal->has_turret_position()) |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 134 | ? shooter_goal->turret_position() |
| 135 | : &turret_goal_builder->AsFlatbuffer(); |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 136 | |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 137 | const frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal |
| 138 | *altitude_goal = (shooter_goal != nullptr && !shooter_goal->auto_aim() && |
| 139 | shooter_goal->has_altitude_position()) |
| 140 | ? shooter_goal->altitude_position() |
| 141 | : &altitude_goal_builder->AsFlatbuffer(); |
| 142 | |
Niko Sohmers | cc3aa45 | 2024-03-03 17:20:04 -0800 | [diff] [blame^] | 143 | const bool turret_in_range = |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 144 | (std::abs(turret_.estimated_position() - turret_goal->unsafe_goal()) < |
Niko Sohmers | cc3aa45 | 2024-03-03 17:20:04 -0800 | [diff] [blame^] | 145 | kCatapultActivationThreshold); |
| 146 | const bool altitude_in_range = |
| 147 | (std::abs(altitude_.estimated_position() - altitude_goal->unsafe_goal()) < |
| 148 | kCatapultActivationThreshold); |
| 149 | const bool altitude_above_min_angle = |
| 150 | (altitude_.estimated_position() > |
| 151 | robot_constants_->common()->min_altitude_shooting_angle()); |
| 152 | |
| 153 | bool subsystems_in_range = |
| 154 | (turret_in_range && altitude_in_range && altitude_above_min_angle); |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 155 | |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 156 | const bool disabled = turret_.Correct(turret_goal, position->turret(), |
| 157 | turret_output == nullptr); |
| 158 | |
| 159 | collision_avoidance->UpdateGoal( |
| 160 | {.intake_pivot_position = intake_pivot_position, |
Austin Schuh | 027fd62 | 2024-03-01 21:26:07 -0800 | [diff] [blame] | 161 | .turret_position = turret_.estimated_position(), |
| 162 | .extend_position = extend_position}, |
| 163 | turret_goal->unsafe_goal(), extend_goal); |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 164 | |
| 165 | turret_.set_min_position(collision_avoidance->min_turret_goal()); |
| 166 | turret_.set_max_position(collision_avoidance->max_turret_goal()); |
| 167 | |
| 168 | *max_intake_pivot_position = collision_avoidance->max_intake_pivot_goal(); |
| 169 | *min_intake_pivot_position = collision_avoidance->min_intake_pivot_goal(); |
| 170 | |
Austin Schuh | 027fd62 | 2024-03-01 21:26:07 -0800 | [diff] [blame] | 171 | *max_extend_position = collision_avoidance->max_extend_goal(); |
| 172 | *min_extend_position = collision_avoidance->min_extend_goal(); |
| 173 | |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 174 | // Calculate the loops for a cycle. |
| 175 | const double voltage = turret_.UpdateController(disabled); |
| 176 | |
| 177 | turret_.UpdateObserver(voltage); |
| 178 | |
| 179 | // Write out all the voltages. |
| 180 | if (turret_output) { |
| 181 | *turret_output = voltage; |
| 182 | } |
| 183 | |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 184 | const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus> |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 185 | turret_status_offset = turret_.MakeStatus(fbb); |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 186 | |
| 187 | const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus> |
| 188 | altitude_status_offset = altitude_.Iterate( |
| 189 | altitude_goal, position->altitude(), altitude_output, fbb); |
| 190 | |
| 191 | flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus> |
| 192 | catapult_status_offset; |
| 193 | { |
| 194 | // The catapult will never use a provided goal. We'll always fabricate one |
| 195 | // for it. |
| 196 | // |
| 197 | // Correct handles resetting our state when disabled. |
| 198 | const bool disabled = catapult_.Correct(nullptr, position->catapult(), |
Maxwell Henderson | 3d68e14 | 2024-02-25 09:58:11 -0800 | [diff] [blame] | 199 | catapult_output == nullptr); |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 200 | |
| 201 | catapult_.set_enable_profile(true); |
| 202 | // We want a trajectory which accelerates up over the first portion of the |
| 203 | // range of motion, holds top speed for a little bit, then decelerates |
| 204 | // before it swings too far. |
| 205 | // |
| 206 | // We can solve for these 3 parameters through the range of motion. Top |
| 207 | // speed is goverened by the voltage headroom we want to have for the |
| 208 | // controller. |
| 209 | // |
| 210 | // Accel can be tuned given the distance to accelerate over, and decel can |
| 211 | // be solved similarly. |
| 212 | // |
| 213 | // accel = v^2 / (2 * x) |
| 214 | catapult_.mutable_profile()->set_maximum_velocity( |
| 215 | catapult::kFreeSpeed * catapult::kOutputRatio * 4.0 / 12.0); |
| 216 | |
| 217 | if (disabled) { |
| 218 | state_ = CatapultState::RETRACTING; |
| 219 | } |
| 220 | |
Maxwell Henderson | 6b1be31 | 2024-02-28 20:15:06 -0800 | [diff] [blame] | 221 | constexpr double kLoadingAcceleration = 20.0; |
| 222 | constexpr double kLoadingDeceleration = 10.0; |
| 223 | |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 224 | switch (state_) { |
| 225 | case CatapultState::READY: |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 226 | [[fallthrough]]; |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 227 | case CatapultState::LOADED: { |
| 228 | if (piece_loaded) { |
| 229 | state_ = CatapultState::LOADED; |
| 230 | } else { |
| 231 | state_ = CatapultState::READY; |
| 232 | } |
| 233 | |
| 234 | const bool catapult_close = CatapultClose(); |
| 235 | |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 236 | if (subsystems_in_range && shooter_goal != nullptr && fire && |
| 237 | catapult_close && piece_loaded) { |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 238 | state_ = CatapultState::FIRING; |
| 239 | } else { |
| 240 | catapult_.set_controller_index(0); |
Maxwell Henderson | 6b1be31 | 2024-02-28 20:15:06 -0800 | [diff] [blame] | 241 | catapult_.mutable_profile()->set_maximum_acceleration( |
| 242 | kLoadingAcceleration); |
| 243 | catapult_.mutable_profile()->set_maximum_deceleration( |
| 244 | kLoadingDeceleration); |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 245 | catapult_.set_unprofiled_goal(0.0, 0.0); |
| 246 | |
| 247 | if (!catapult_close) { |
| 248 | state_ = CatapultState::RETRACTING; |
| 249 | } |
| 250 | break; |
| 251 | } |
| 252 | [[fallthrough]]; |
| 253 | } |
| 254 | case CatapultState::FIRING: |
| 255 | *retention_roller_output = |
| 256 | robot_constants_->common()->retention_roller_voltages()->spitting(); |
Maxwell Henderson | 6b1be31 | 2024-02-28 20:15:06 -0800 | [diff] [blame] | 257 | *retention_roller_stator_current_limit = |
| 258 | robot_constants_->common() |
| 259 | ->current_limits() |
| 260 | ->shooting_retention_roller_stator_current_limit(); |
| 261 | catapult_.set_controller_index(1); |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 262 | catapult_.mutable_profile()->set_maximum_acceleration(400.0); |
Maxwell Henderson | 6b1be31 | 2024-02-28 20:15:06 -0800 | [diff] [blame] | 263 | catapult_.mutable_profile()->set_maximum_deceleration(500.0); |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 264 | catapult_.set_unprofiled_goal(2.0, 0.0); |
| 265 | if (CatapultClose()) { |
| 266 | state_ = CatapultState::RETRACTING; |
| 267 | } else { |
| 268 | break; |
| 269 | } |
| 270 | [[fallthrough]]; |
| 271 | case CatapultState::RETRACTING: |
| 272 | catapult_.set_controller_index(0); |
Maxwell Henderson | 6b1be31 | 2024-02-28 20:15:06 -0800 | [diff] [blame] | 273 | catapult_.mutable_profile()->set_maximum_acceleration( |
| 274 | kLoadingAcceleration); |
| 275 | catapult_.mutable_profile()->set_maximum_deceleration( |
| 276 | kLoadingDeceleration); |
| 277 | // TODO: catapult_return_position |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 278 | catapult_.set_unprofiled_goal(0.0, 0.0); |
| 279 | |
| 280 | if (CatapultClose()) { |
| 281 | if (piece_loaded) { |
| 282 | state_ = CatapultState::LOADED; |
| 283 | } else { |
| 284 | state_ = CatapultState::READY; |
| 285 | } |
| 286 | } |
| 287 | break; |
| 288 | } |
| 289 | |
| 290 | const double voltage = catapult_.UpdateController(disabled); |
| 291 | catapult_.UpdateObserver(voltage); |
| 292 | if (catapult_output != nullptr) { |
| 293 | *catapult_output = voltage; |
| 294 | } |
| 295 | catapult_status_offset = catapult_.MakeStatus(fbb); |
| 296 | } |
| 297 | |
| 298 | flatbuffers::Offset<AimerStatus> aimer_offset; |
| 299 | if (aiming) { |
| 300 | aimer_offset = aimer_.PopulateStatus(fbb); |
| 301 | } |
| 302 | |
| 303 | y2024::control_loops::superstructure::ShooterStatus::Builder status_builder( |
| 304 | *fbb); |
| 305 | status_builder.add_turret(turret_status_offset); |
| 306 | status_builder.add_altitude(altitude_status_offset); |
| 307 | status_builder.add_catapult(catapult_status_offset); |
| 308 | status_builder.add_catapult_state(state_); |
Niko Sohmers | cc3aa45 | 2024-03-03 17:20:04 -0800 | [diff] [blame^] | 309 | status_builder.add_turret_in_range(turret_in_range); |
| 310 | status_builder.add_altitude_in_range(altitude_in_range); |
| 311 | status_builder.add_altitude_above_min_angle(altitude_above_min_angle); |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 312 | if (aiming) { |
| 313 | status_builder.add_aimer(aimer_offset); |
| 314 | } |
| 315 | |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 316 | return status_builder.Finish(); |
| 317 | } |
| 318 | |
| 319 | } // namespace y2024::control_loops::superstructure |