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 | |
| 143 | bool subsystems_in_range = |
| 144 | (std::abs(turret_.estimated_position() - turret_goal->unsafe_goal()) < |
| 145 | kCatapultActivationThreshold && |
| 146 | std::abs(altitude_.estimated_position() - altitude_goal->unsafe_goal()) < |
| 147 | kCatapultActivationThreshold && |
| 148 | altitude_.estimated_position() > |
| 149 | robot_constants_->common()->min_altitude_shooting_angle()); |
| 150 | |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 151 | const bool disabled = turret_.Correct(turret_goal, position->turret(), |
| 152 | turret_output == nullptr); |
| 153 | |
| 154 | collision_avoidance->UpdateGoal( |
| 155 | {.intake_pivot_position = intake_pivot_position, |
Austin Schuh | 027fd62 | 2024-03-01 21:26:07 -0800 | [diff] [blame^] | 156 | .turret_position = turret_.estimated_position(), |
| 157 | .extend_position = extend_position}, |
| 158 | turret_goal->unsafe_goal(), extend_goal); |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 159 | |
| 160 | turret_.set_min_position(collision_avoidance->min_turret_goal()); |
| 161 | turret_.set_max_position(collision_avoidance->max_turret_goal()); |
| 162 | |
| 163 | *max_intake_pivot_position = collision_avoidance->max_intake_pivot_goal(); |
| 164 | *min_intake_pivot_position = collision_avoidance->min_intake_pivot_goal(); |
| 165 | |
Austin Schuh | 027fd62 | 2024-03-01 21:26:07 -0800 | [diff] [blame^] | 166 | *max_extend_position = collision_avoidance->max_extend_goal(); |
| 167 | *min_extend_position = collision_avoidance->min_extend_goal(); |
| 168 | |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 169 | // Calculate the loops for a cycle. |
| 170 | const double voltage = turret_.UpdateController(disabled); |
| 171 | |
| 172 | turret_.UpdateObserver(voltage); |
| 173 | |
| 174 | // Write out all the voltages. |
| 175 | if (turret_output) { |
| 176 | *turret_output = voltage; |
| 177 | } |
| 178 | |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 179 | const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus> |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 180 | turret_status_offset = turret_.MakeStatus(fbb); |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 181 | |
| 182 | const flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus> |
| 183 | altitude_status_offset = altitude_.Iterate( |
| 184 | altitude_goal, position->altitude(), altitude_output, fbb); |
| 185 | |
| 186 | flatbuffers::Offset<PotAndAbsoluteEncoderProfiledJointStatus> |
| 187 | catapult_status_offset; |
| 188 | { |
| 189 | // The catapult will never use a provided goal. We'll always fabricate one |
| 190 | // for it. |
| 191 | // |
| 192 | // Correct handles resetting our state when disabled. |
| 193 | const bool disabled = catapult_.Correct(nullptr, position->catapult(), |
Maxwell Henderson | 3d68e14 | 2024-02-25 09:58:11 -0800 | [diff] [blame] | 194 | catapult_output == nullptr); |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 195 | |
| 196 | catapult_.set_enable_profile(true); |
| 197 | // We want a trajectory which accelerates up over the first portion of the |
| 198 | // range of motion, holds top speed for a little bit, then decelerates |
| 199 | // before it swings too far. |
| 200 | // |
| 201 | // We can solve for these 3 parameters through the range of motion. Top |
| 202 | // speed is goverened by the voltage headroom we want to have for the |
| 203 | // controller. |
| 204 | // |
| 205 | // Accel can be tuned given the distance to accelerate over, and decel can |
| 206 | // be solved similarly. |
| 207 | // |
| 208 | // accel = v^2 / (2 * x) |
| 209 | catapult_.mutable_profile()->set_maximum_velocity( |
| 210 | catapult::kFreeSpeed * catapult::kOutputRatio * 4.0 / 12.0); |
| 211 | |
| 212 | if (disabled) { |
| 213 | state_ = CatapultState::RETRACTING; |
| 214 | } |
| 215 | |
Maxwell Henderson | 6b1be31 | 2024-02-28 20:15:06 -0800 | [diff] [blame] | 216 | constexpr double kLoadingAcceleration = 20.0; |
| 217 | constexpr double kLoadingDeceleration = 10.0; |
| 218 | |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 219 | switch (state_) { |
| 220 | case CatapultState::READY: |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 221 | [[fallthrough]]; |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 222 | case CatapultState::LOADED: { |
| 223 | if (piece_loaded) { |
| 224 | state_ = CatapultState::LOADED; |
| 225 | } else { |
| 226 | state_ = CatapultState::READY; |
| 227 | } |
| 228 | |
| 229 | const bool catapult_close = CatapultClose(); |
| 230 | |
Filip Kujawa | 7a79960 | 2024-02-23 12:27:47 -0800 | [diff] [blame] | 231 | if (subsystems_in_range && shooter_goal != nullptr && fire && |
| 232 | catapult_close && piece_loaded) { |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 233 | state_ = CatapultState::FIRING; |
| 234 | } else { |
| 235 | catapult_.set_controller_index(0); |
Maxwell Henderson | 6b1be31 | 2024-02-28 20:15:06 -0800 | [diff] [blame] | 236 | catapult_.mutable_profile()->set_maximum_acceleration( |
| 237 | kLoadingAcceleration); |
| 238 | catapult_.mutable_profile()->set_maximum_deceleration( |
| 239 | kLoadingDeceleration); |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 240 | catapult_.set_unprofiled_goal(0.0, 0.0); |
| 241 | |
| 242 | if (!catapult_close) { |
| 243 | state_ = CatapultState::RETRACTING; |
| 244 | } |
| 245 | break; |
| 246 | } |
| 247 | [[fallthrough]]; |
| 248 | } |
| 249 | case CatapultState::FIRING: |
| 250 | *retention_roller_output = |
| 251 | robot_constants_->common()->retention_roller_voltages()->spitting(); |
Maxwell Henderson | 6b1be31 | 2024-02-28 20:15:06 -0800 | [diff] [blame] | 252 | *retention_roller_stator_current_limit = |
| 253 | robot_constants_->common() |
| 254 | ->current_limits() |
| 255 | ->shooting_retention_roller_stator_current_limit(); |
| 256 | catapult_.set_controller_index(1); |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 257 | catapult_.mutable_profile()->set_maximum_acceleration(400.0); |
Maxwell Henderson | 6b1be31 | 2024-02-28 20:15:06 -0800 | [diff] [blame] | 258 | catapult_.mutable_profile()->set_maximum_deceleration(500.0); |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 259 | catapult_.set_unprofiled_goal(2.0, 0.0); |
| 260 | if (CatapultClose()) { |
| 261 | state_ = CatapultState::RETRACTING; |
| 262 | } else { |
| 263 | break; |
| 264 | } |
| 265 | [[fallthrough]]; |
| 266 | case CatapultState::RETRACTING: |
| 267 | catapult_.set_controller_index(0); |
Maxwell Henderson | 6b1be31 | 2024-02-28 20:15:06 -0800 | [diff] [blame] | 268 | catapult_.mutable_profile()->set_maximum_acceleration( |
| 269 | kLoadingAcceleration); |
| 270 | catapult_.mutable_profile()->set_maximum_deceleration( |
| 271 | kLoadingDeceleration); |
| 272 | // TODO: catapult_return_position |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 273 | catapult_.set_unprofiled_goal(0.0, 0.0); |
| 274 | |
| 275 | if (CatapultClose()) { |
| 276 | if (piece_loaded) { |
| 277 | state_ = CatapultState::LOADED; |
| 278 | } else { |
| 279 | state_ = CatapultState::READY; |
| 280 | } |
| 281 | } |
| 282 | break; |
| 283 | } |
| 284 | |
| 285 | const double voltage = catapult_.UpdateController(disabled); |
| 286 | catapult_.UpdateObserver(voltage); |
| 287 | if (catapult_output != nullptr) { |
| 288 | *catapult_output = voltage; |
| 289 | } |
| 290 | catapult_status_offset = catapult_.MakeStatus(fbb); |
| 291 | } |
| 292 | |
| 293 | flatbuffers::Offset<AimerStatus> aimer_offset; |
| 294 | if (aiming) { |
| 295 | aimer_offset = aimer_.PopulateStatus(fbb); |
| 296 | } |
| 297 | |
| 298 | y2024::control_loops::superstructure::ShooterStatus::Builder status_builder( |
| 299 | *fbb); |
| 300 | status_builder.add_turret(turret_status_offset); |
| 301 | status_builder.add_altitude(altitude_status_offset); |
| 302 | status_builder.add_catapult(catapult_status_offset); |
| 303 | status_builder.add_catapult_state(state_); |
| 304 | if (aiming) { |
| 305 | status_builder.add_aimer(aimer_offset); |
| 306 | } |
| 307 | |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 308 | return status_builder.Finish(); |
| 309 | } |
| 310 | |
| 311 | } // namespace y2024::control_loops::superstructure |