Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 1 | #include "y2024/autonomous/autonomous_actor.h" |
| 2 | |
| 3 | #include <chrono> |
| 4 | #include <cinttypes> |
| 5 | #include <cmath> |
| 6 | |
| 7 | #include "aos/logging/logging.h" |
| 8 | #include "aos/util/math.h" |
| 9 | #include "frc971/control_loops/drivetrain/localizer_generated.h" |
| 10 | #include "y2024/autonomous/auto_splines.h" |
| 11 | #include "y2024/constants.h" |
| 12 | #include "y2024/control_loops/drivetrain/drivetrain_base.h" |
| 13 | |
| 14 | DEFINE_bool(spline_auto, false, "Run simple test S-spline auto mode."); |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 15 | DEFINE_bool(do_fifth_piece, true, ""); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 16 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 17 | namespace y2024::autonomous { |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 18 | |
| 19 | using ::frc971::ProfileParametersT; |
| 20 | |
| 21 | ProfileParametersT MakeProfileParameters(float max_velocity, |
| 22 | float max_acceleration) { |
| 23 | ProfileParametersT result; |
| 24 | result.max_velocity = max_velocity; |
| 25 | result.max_acceleration = max_acceleration; |
| 26 | return result; |
| 27 | } |
| 28 | |
| 29 | using ::aos::monotonic_clock; |
| 30 | using frc971::CreateProfileParameters; |
| 31 | using ::frc971::ProfileParametersT; |
| 32 | using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal; |
| 33 | using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal; |
| 34 | using frc971::control_loops::drivetrain::LocalizerControl; |
| 35 | namespace chrono = ::std::chrono; |
| 36 | |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 37 | AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop, |
| 38 | const y2024::Constants *robot_constants) |
Filip Kujawa | 58ffbf3 | 2024-02-24 18:28:34 -0800 | [diff] [blame] | 39 | : frc971::autonomous::UserButtonLocalizedAutonomousActor( |
James Kuszmaul | 2549e75 | 2024-01-20 17:42:51 -0800 | [diff] [blame] | 40 | event_loop, |
| 41 | control_loops::drivetrain::GetDrivetrainConfig(event_loop)), |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 42 | localizer_control_sender_( |
| 43 | event_loop->MakeSender< |
| 44 | ::frc971::control_loops::drivetrain::LocalizerControl>( |
| 45 | "/drivetrain")), |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 46 | superstructure_goal_sender_( |
Filip Kujawa | 58ffbf3 | 2024-02-24 18:28:34 -0800 | [diff] [blame] | 47 | event_loop |
| 48 | ->MakeSender<::y2024::control_loops::superstructure::GoalStatic>( |
| 49 | "/superstructure")), |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 50 | superstructure_status_fetcher_( |
| 51 | event_loop |
| 52 | ->MakeFetcher<::y2024::control_loops::superstructure::Status>( |
Filip Kujawa | 58ffbf3 | 2024-02-24 18:28:34 -0800 | [diff] [blame] | 53 | "/superstructure")), |
Austin Schuh | 6bdcc37 | 2024-06-27 14:49:11 -0700 | [diff] [blame^] | 54 | robot_constants_(robot_constants), |
| 55 | auto_splines_() { |
| 56 | CHECK(robot_constants_ != nullptr); |
| 57 | } |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 58 | |
| 59 | void AutonomousActor::Replan() { |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 60 | AutonomousMode mode = robot_constants_->common()->autonomous_mode(); |
| 61 | switch (mode) { |
| 62 | case AutonomousMode::NONE: |
| 63 | break; |
| 64 | case AutonomousMode::SPLINE_AUTO: |
| 65 | test_spline_ = |
| 66 | PlanSpline(std::bind(&AutonomousSplines::TestSpline, &auto_splines_, |
| 67 | std::placeholders::_1, alliance_), |
| 68 | SplineDirection::kForward); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 69 | |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 70 | starting_position_ = test_spline_->starting_position(); |
| 71 | break; |
| 72 | case AutonomousMode::MOBILITY_AND_SHOOT: |
| 73 | AOS_LOG(INFO, "Mobility & shoot replanning!"); |
| 74 | mobility_and_shoot_splines_ = {PlanSpline( |
| 75 | std::bind(&AutonomousSplines::MobilityAndShootSpline, &auto_splines_, |
| 76 | std::placeholders::_1, alliance_), |
| 77 | SplineDirection::kForward)}; |
| 78 | |
| 79 | starting_position_ = |
| 80 | mobility_and_shoot_splines_.value()[0].starting_position(); |
| 81 | CHECK(starting_position_); |
| 82 | break; |
James Kuszmaul | 58f5143 | 2024-03-24 15:00:06 -0700 | [diff] [blame] | 83 | case AutonomousMode::FIVE_PIECE: |
| 84 | AOS_LOG(INFO, "FIVE_PIECE replanning!"); |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 85 | four_piece_splines_ = { |
| 86 | PlanSpline( |
| 87 | std::bind(&AutonomousSplines::FourPieceSpline1, &auto_splines_, |
| 88 | std::placeholders::_1, alliance_), |
| 89 | SplineDirection::kForward), |
| 90 | PlanSpline( |
| 91 | std::bind(&AutonomousSplines::FourPieceSpline2, &auto_splines_, |
| 92 | std::placeholders::_1, alliance_), |
| 93 | SplineDirection::kBackward), |
| 94 | PlanSpline( |
| 95 | std::bind(&AutonomousSplines::FourPieceSpline3, &auto_splines_, |
| 96 | std::placeholders::_1, alliance_), |
| 97 | SplineDirection::kForward), |
| 98 | PlanSpline( |
| 99 | std::bind(&AutonomousSplines::FourPieceSpline4, &auto_splines_, |
| 100 | std::placeholders::_1, alliance_), |
| 101 | SplineDirection::kForward), |
| 102 | PlanSpline( |
| 103 | std::bind(&AutonomousSplines::FourPieceSpline5, &auto_splines_, |
| 104 | std::placeholders::_1, alliance_), |
| 105 | SplineDirection::kBackward)}; |
| 106 | |
| 107 | starting_position_ = four_piece_splines_.value()[0].starting_position(); |
| 108 | CHECK(starting_position_); |
| 109 | break; |
Maxwell Henderson | a2dadd0 | 2024-03-24 13:57:09 -0700 | [diff] [blame] | 110 | case AutonomousMode::TWO_PIECE_STEAL: |
| 111 | AOS_LOG(INFO, "TWO_PIECE_STEAL replanning!"); |
| 112 | two_piece_steal_splines_ = { |
| 113 | PlanSpline( |
| 114 | std::bind(&AutonomousSplines::TwoPieceStealSpline1, |
| 115 | &auto_splines_, std::placeholders::_1, alliance_), |
| 116 | SplineDirection::kForward), |
| 117 | PlanSpline( |
| 118 | std::bind(&AutonomousSplines::TwoPieceStealSpline2, |
| 119 | &auto_splines_, std::placeholders::_1, alliance_), |
| 120 | SplineDirection::kBackward), |
| 121 | PlanSpline( |
| 122 | std::bind(&AutonomousSplines::TwoPieceStealSpline3, |
| 123 | &auto_splines_, std::placeholders::_1, alliance_), |
| 124 | SplineDirection::kForward), |
| 125 | PlanSpline( |
| 126 | std::bind(&AutonomousSplines::TwoPieceStealSpline4, |
| 127 | &auto_splines_, std::placeholders::_1, alliance_), |
James Kuszmaul | 58f5143 | 2024-03-24 15:00:06 -0700 | [diff] [blame] | 128 | SplineDirection::kBackward)}; |
| 129 | |
| 130 | starting_position_ = |
| 131 | two_piece_steal_splines_.value()[0].starting_position(); |
| 132 | CHECK(starting_position_); |
| 133 | break; |
James Kuszmaul | 337e3a5 | 2024-05-04 14:06:52 -0700 | [diff] [blame] | 134 | case AutonomousMode::TWO_PIECE_VIA_STAGE: |
| 135 | AOS_LOG(INFO, "TWO_PIECE_VIA_STAGE replanning!"); |
| 136 | two_piece_via_stage_splines_ = { |
| 137 | PlanSpline( |
| 138 | std::bind(&AutonomousSplines::TwoPieceViaStageSpline1, |
| 139 | &auto_splines_, std::placeholders::_1, alliance_), |
| 140 | SplineDirection::kForward), |
| 141 | PlanSpline( |
| 142 | std::bind(&AutonomousSplines::TwoPieceViaStageSpline2, |
| 143 | &auto_splines_, std::placeholders::_1, alliance_), |
| 144 | SplineDirection::kBackward), |
| 145 | PlanSpline( |
| 146 | std::bind(&AutonomousSplines::TwoPieceViaStageSpline3, |
| 147 | &auto_splines_, std::placeholders::_1, alliance_), |
| 148 | SplineDirection::kForward), |
| 149 | PlanSpline( |
| 150 | std::bind(&AutonomousSplines::TwoPieceViaStageSpline4, |
| 151 | &auto_splines_, std::placeholders::_1, alliance_), |
| 152 | SplineDirection::kBackward)}; |
| 153 | |
| 154 | starting_position_ = |
| 155 | two_piece_via_stage_splines_.value()[0].starting_position(); |
| 156 | CHECK(starting_position_); |
| 157 | break; |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | is_planned_ = true; |
| 161 | |
| 162 | MaybeSendStartingPosition(); |
| 163 | } |
| 164 | |
Filip Kujawa | 58ffbf3 | 2024-02-24 18:28:34 -0800 | [diff] [blame] | 165 | bool AutonomousActor::Run( |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 166 | const ::frc971::autonomous::AutonomousActionParams *params) { |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 167 | AOS_LOG(INFO, "Params are %d\n", params->mode()); |
| 168 | if (alliance_ == aos::Alliance::kInvalid) { |
| 169 | AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection."); |
| 170 | return false; |
| 171 | } |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 172 | |
| 173 | AutonomousMode mode = robot_constants_->common()->autonomous_mode(); |
| 174 | switch (mode) { |
| 175 | case AutonomousMode::NONE: |
| 176 | AOS_LOG(WARNING, "No auto mode selected."); |
| 177 | break; |
| 178 | case AutonomousMode::SPLINE_AUTO: |
| 179 | SplineAuto(); |
| 180 | break; |
| 181 | case AutonomousMode::MOBILITY_AND_SHOOT: |
| 182 | MobilityAndShoot(); |
| 183 | break; |
James Kuszmaul | 58f5143 | 2024-03-24 15:00:06 -0700 | [diff] [blame] | 184 | case AutonomousMode::FIVE_PIECE: |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 185 | FourPieceAuto(); |
| 186 | break; |
Maxwell Henderson | a2dadd0 | 2024-03-24 13:57:09 -0700 | [diff] [blame] | 187 | case AutonomousMode::TWO_PIECE_STEAL: |
| 188 | TwoPieceStealAuto(); |
| 189 | break; |
James Kuszmaul | 337e3a5 | 2024-05-04 14:06:52 -0700 | [diff] [blame] | 190 | case AutonomousMode::TWO_PIECE_VIA_STAGE: |
| 191 | TwoPieceViaStageAuto(); |
| 192 | break; |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 193 | } |
| 194 | return true; |
| 195 | } |
| 196 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 197 | void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) { |
| 198 | // Set up the starting position for the blue alliance. |
| 199 | |
| 200 | auto builder = localizer_control_sender_.MakeBuilder(); |
| 201 | |
| 202 | LocalizerControl::Builder localizer_control_builder = |
| 203 | builder.MakeBuilder<LocalizerControl>(); |
| 204 | localizer_control_builder.add_x(start(0)); |
| 205 | localizer_control_builder.add_y(start(1)); |
| 206 | localizer_control_builder.add_theta(start(2)); |
| 207 | localizer_control_builder.add_theta_uncertainty(0.00001); |
| 208 | AOS_LOG(INFO, "User button pressed, x: %f y: %f theta: %f", start(0), |
| 209 | start(1), start(2)); |
| 210 | if (builder.Send(localizer_control_builder.Finish()) != |
| 211 | aos::RawSender::Error::kOk) { |
| 212 | AOS_LOG(ERROR, "Failed to reset localizer.\n"); |
| 213 | } |
| 214 | } |
Filip Kujawa | 58ffbf3 | 2024-02-24 18:28:34 -0800 | [diff] [blame] | 215 | |
| 216 | void AutonomousActor::Reset() { |
| 217 | set_intake_goal(control_loops::superstructure::IntakeGoal::NONE); |
| 218 | set_note_goal(control_loops::superstructure::NoteGoal::NONE); |
Filip Kujawa | 1286c01 | 2024-03-31 22:53:27 -0700 | [diff] [blame] | 219 | set_auto_aim(control_loops::superstructure::AutoAimMode::NONE); |
Filip Kujawa | 58ffbf3 | 2024-02-24 18:28:34 -0800 | [diff] [blame] | 220 | set_fire(false); |
| 221 | set_preloaded(false); |
| 222 | SendSuperstructureGoal(); |
| 223 | } |
| 224 | |
| 225 | void AutonomousActor::SplineAuto() { |
| 226 | CHECK(test_spline_); |
| 227 | |
| 228 | if (!test_spline_->WaitForPlan()) return; |
| 229 | test_spline_->Start(); |
| 230 | |
| 231 | if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return; |
| 232 | } |
| 233 | |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 234 | void AutonomousActor::MobilityAndShoot() { |
| 235 | aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now(); |
| 236 | |
| 237 | uint32_t initial_shot_count = shot_count(); |
| 238 | |
| 239 | CHECK(mobility_and_shoot_splines_); |
| 240 | |
| 241 | auto &splines = *mobility_and_shoot_splines_; |
| 242 | |
| 243 | AOS_LOG(INFO, "Going to preload"); |
| 244 | |
| 245 | // Always be auto-aiming. |
| 246 | Aim(); |
| 247 | |
| 248 | if (!WaitForPreloaded()) return; |
| 249 | |
| 250 | AOS_LOG(INFO, "Starting to Move"); |
| 251 | |
| 252 | if (!splines[0].WaitForPlan()) return; |
| 253 | |
| 254 | splines[0].Start(); |
| 255 | |
| 256 | if (!splines[0].WaitForSplineDistanceRemaining(0.05)) return; |
| 257 | |
| 258 | AOS_LOG( |
| 259 | INFO, "Got there %lf s\nNow Shooting\n", |
| 260 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 261 | |
| 262 | Shoot(); |
| 263 | |
| 264 | if (!WaitForNoteFired(initial_shot_count, std::chrono::seconds(5))) return; |
| 265 | |
| 266 | StopFiring(); |
| 267 | |
| 268 | AOS_LOG( |
| 269 | INFO, "Note fired at %lf seconds\n", |
| 270 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 271 | } |
| 272 | |
| 273 | void AutonomousActor::FourPieceAuto() { |
| 274 | aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now(); |
| 275 | |
| 276 | CHECK(four_piece_splines_); |
| 277 | auto &splines = *four_piece_splines_; |
| 278 | |
| 279 | uint32_t initial_shot_count = shot_count(); |
| 280 | |
| 281 | // Always be aiming & firing. |
| 282 | Aim(); |
| 283 | if (!WaitForPreloaded()) return; |
| 284 | |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 285 | Shoot(); |
| 286 | |
| 287 | AOS_LOG( |
| 288 | INFO, "Shooting Preloaded Note %lfs\n", |
| 289 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 290 | |
| 291 | if (!WaitForNoteFired(initial_shot_count, std::chrono::seconds(2))) return; |
| 292 | |
| 293 | AOS_LOG( |
| 294 | INFO, "Shot first note %lfs\n", |
| 295 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 296 | |
| 297 | Intake(); |
| 298 | StopFiring(); |
| 299 | |
| 300 | AOS_LOG( |
| 301 | INFO, "Starting Spline 1 %lfs\n", |
| 302 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 303 | |
| 304 | if (!splines[0].WaitForPlan()) return; |
| 305 | |
| 306 | splines[0].Start(); |
| 307 | |
| 308 | if (!splines[0].WaitForSplineDistanceRemaining(0.01)) return; |
| 309 | |
| 310 | if (!splines[1].WaitForPlan()) return; |
| 311 | |
| 312 | AOS_LOG( |
| 313 | INFO, "Starting second spline %lfs\n", |
| 314 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 315 | |
| 316 | splines[1].Start(); |
| 317 | |
| 318 | if (!splines[1].WaitForSplineDistanceRemaining(0.01)) return; |
| 319 | |
| 320 | AOS_LOG( |
| 321 | INFO, "Finished second spline %lfs\n", |
| 322 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 323 | |
James Kuszmaul | 58f5143 | 2024-03-24 15:00:06 -0700 | [diff] [blame] | 324 | std::this_thread::sleep_for(chrono::milliseconds(200)); |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 325 | |
| 326 | Shoot(); |
| 327 | |
| 328 | if (!WaitForNoteFired(initial_shot_count + 1, std::chrono::seconds(2))) |
| 329 | return; |
| 330 | |
| 331 | AOS_LOG( |
| 332 | INFO, "Shot second note, starting drive %lfs\n", |
| 333 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 334 | |
| 335 | if (!splines[2].WaitForPlan()) return; |
| 336 | splines[2].Start(); |
| 337 | |
| 338 | if (!splines[2].WaitForSplineDistanceRemaining(0.01)) return; |
| 339 | |
| 340 | if (!WaitForNoteFired(initial_shot_count + 3, std::chrono::seconds(5))) |
| 341 | return; |
| 342 | |
| 343 | AOS_LOG( |
| 344 | INFO, "Finished 4 notes at %lfs\n", |
| 345 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 346 | |
| 347 | if (!FLAGS_do_fifth_piece) { |
| 348 | AOS_LOG(INFO, "Exitting early due to --nodo_fifth_piece"); |
| 349 | return; |
| 350 | } |
| 351 | |
| 352 | if (!splines[3].WaitForPlan()) return; |
| 353 | splines[3].Start(); |
| 354 | |
| 355 | if (!splines[3].WaitForSplineDistanceRemaining(0.1)) return; |
| 356 | |
| 357 | AOS_LOG( |
| 358 | INFO, "Got to 5th note at %lfs\n", |
| 359 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 360 | |
| 361 | StopFiring(); |
| 362 | |
| 363 | if (!splines[4].WaitForPlan()) return; |
| 364 | splines[4].Start(); |
| 365 | |
James Kuszmaul | f2f95b3 | 2024-03-23 20:12:05 -0700 | [diff] [blame] | 366 | if (!splines[4].WaitForSplineDistanceRemaining(0.01)) return; |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 367 | |
| 368 | AOS_LOG( |
| 369 | INFO, "Done with spline %lfs\n", |
| 370 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 371 | |
James Kuszmaul | 58f5143 | 2024-03-24 15:00:06 -0700 | [diff] [blame] | 372 | std::this_thread::sleep_for(chrono::milliseconds(250)); |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 373 | |
| 374 | AOS_LOG( |
| 375 | INFO, "Shooting last note! %lfs\n", |
| 376 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 377 | |
| 378 | Shoot(); |
| 379 | |
| 380 | if (!WaitForNoteFired(initial_shot_count + 4, std::chrono::seconds(5))) |
| 381 | return; |
| 382 | |
| 383 | AOS_LOG( |
| 384 | INFO, "Done %lfs\n", |
| 385 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 386 | } |
| 387 | |
Maxwell Henderson | a2dadd0 | 2024-03-24 13:57:09 -0700 | [diff] [blame] | 388 | void AutonomousActor::TwoPieceStealAuto() { |
| 389 | aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now(); |
| 390 | |
| 391 | CHECK(two_piece_steal_splines_); |
| 392 | auto &splines = *two_piece_steal_splines_; |
| 393 | |
| 394 | uint32_t initial_shot_count = shot_count(); |
| 395 | |
| 396 | // Always be aiming & firing. |
| 397 | Aim(); |
| 398 | if (!WaitForPreloaded()) return; |
| 399 | |
Maxwell Henderson | a2dadd0 | 2024-03-24 13:57:09 -0700 | [diff] [blame] | 400 | Shoot(); |
| 401 | |
| 402 | AOS_LOG( |
| 403 | INFO, "Shooting Preloaded Note %lfs\n", |
| 404 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 405 | |
| 406 | if (!WaitForNoteFired(initial_shot_count, std::chrono::seconds(2))) return; |
| 407 | |
| 408 | AOS_LOG( |
| 409 | INFO, "Shot first note %lfs\n", |
| 410 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 411 | |
Maxwell Henderson | a2dadd0 | 2024-03-24 13:57:09 -0700 | [diff] [blame] | 412 | StopFiring(); |
| 413 | |
| 414 | AOS_LOG( |
| 415 | INFO, "Starting Spline 1 %lfs\n", |
| 416 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 417 | |
| 418 | if (!splines[0].WaitForPlan()) return; |
| 419 | |
| 420 | splines[0].Start(); |
| 421 | |
James Kuszmaul | 58f5143 | 2024-03-24 15:00:06 -0700 | [diff] [blame] | 422 | if (!splines[0].WaitForSplineDistanceRemaining(2.0)) return; |
| 423 | Intake(); |
| 424 | |
Maxwell Henderson | a2dadd0 | 2024-03-24 13:57:09 -0700 | [diff] [blame] | 425 | if (!splines[0].WaitForSplineDistanceRemaining(0.01)) return; |
| 426 | |
| 427 | if (!splines[1].WaitForPlan()) return; |
| 428 | |
| 429 | AOS_LOG( |
| 430 | INFO, "Starting second spline %lfs\n", |
| 431 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 432 | |
| 433 | splines[1].Start(); |
| 434 | |
| 435 | if (!splines[1].WaitForSplineDistanceRemaining(0.01)) return; |
| 436 | |
| 437 | AOS_LOG( |
| 438 | INFO, "Finished second spline %lfs\n", |
| 439 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 440 | |
James Kuszmaul | 58f5143 | 2024-03-24 15:00:06 -0700 | [diff] [blame] | 441 | std::this_thread::sleep_for(chrono::milliseconds(200)); |
Maxwell Henderson | a2dadd0 | 2024-03-24 13:57:09 -0700 | [diff] [blame] | 442 | |
| 443 | Shoot(); |
James Kuszmaul | 58f5143 | 2024-03-24 15:00:06 -0700 | [diff] [blame] | 444 | StopIntake(); |
Maxwell Henderson | a2dadd0 | 2024-03-24 13:57:09 -0700 | [diff] [blame] | 445 | |
| 446 | if (!WaitForNoteFired(initial_shot_count + 1, std::chrono::seconds(2))) |
| 447 | return; |
| 448 | |
| 449 | StopFiring(); |
| 450 | |
| 451 | AOS_LOG( |
| 452 | INFO, "Shot second note, starting drive %lfs\n", |
| 453 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 454 | |
| 455 | if (!splines[2].WaitForPlan()) return; |
| 456 | |
| 457 | AOS_LOG( |
| 458 | INFO, "Starting third spline %lfs\n", |
| 459 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 460 | splines[2].Start(); |
| 461 | |
James Kuszmaul | 58f5143 | 2024-03-24 15:00:06 -0700 | [diff] [blame] | 462 | if (!splines[2].WaitForSplineDistanceRemaining(2.0)) return; |
| 463 | |
| 464 | Intake(); |
| 465 | |
Maxwell Henderson | a2dadd0 | 2024-03-24 13:57:09 -0700 | [diff] [blame] | 466 | if (!splines[2].WaitForSplineDistanceRemaining(0.01)) return; |
| 467 | |
| 468 | if (!splines[3].WaitForPlan()) return; |
| 469 | |
| 470 | AOS_LOG( |
| 471 | INFO, "Starting fourth spline %lfs\n", |
| 472 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 473 | splines[3].Start(); |
| 474 | |
| 475 | if (!splines[3].WaitForSplineDistanceRemaining(0.01)) return; |
| 476 | |
| 477 | Shoot(); |
| 478 | |
James Kuszmaul | 58f5143 | 2024-03-24 15:00:06 -0700 | [diff] [blame] | 479 | std::this_thread::sleep_for(chrono::milliseconds(400)); |
| 480 | |
Maxwell Henderson | a2dadd0 | 2024-03-24 13:57:09 -0700 | [diff] [blame] | 481 | if (!WaitForNoteFired(initial_shot_count + 2, std::chrono::seconds(2))) |
| 482 | return; |
| 483 | |
| 484 | AOS_LOG( |
| 485 | INFO, "Done %lfs\n", |
| 486 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 487 | } |
| 488 | |
James Kuszmaul | 337e3a5 | 2024-05-04 14:06:52 -0700 | [diff] [blame] | 489 | void AutonomousActor::TwoPieceViaStageAuto() { |
| 490 | aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now(); |
| 491 | |
| 492 | CHECK(two_piece_via_stage_splines_); |
| 493 | auto &splines = *two_piece_via_stage_splines_; |
| 494 | |
| 495 | uint32_t initial_shot_count = shot_count(); |
| 496 | |
| 497 | // Always be aiming & firing. |
| 498 | Aim(); |
| 499 | if (!WaitForPreloaded()) return; |
| 500 | |
| 501 | Shoot(); |
| 502 | |
| 503 | AOS_LOG( |
| 504 | INFO, "Shooting Preloaded Note %lfs\n", |
| 505 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 506 | |
| 507 | if (!WaitForNoteFired(initial_shot_count, std::chrono::seconds(2))) return; |
| 508 | StopAiming(); |
| 509 | |
| 510 | AOS_LOG( |
| 511 | INFO, "Shot first note %lfs\n", |
| 512 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 513 | |
| 514 | StopFiring(); |
| 515 | |
| 516 | AOS_LOG( |
| 517 | INFO, "Starting Spline 1 %lfs\n", |
| 518 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 519 | |
| 520 | if (!splines[0].WaitForPlan()) return; |
| 521 | |
| 522 | splines[0].Start(); |
| 523 | |
| 524 | if (!splines[0].WaitForSplineDistanceRemaining(2.0)) return; |
| 525 | Intake(); |
| 526 | |
| 527 | if (!splines[0].WaitForSplineDistanceRemaining(0.01)) return; |
| 528 | |
| 529 | if (!splines[1].WaitForPlan()) return; |
| 530 | |
| 531 | AOS_LOG( |
| 532 | INFO, "Starting second spline %lfs\n", |
| 533 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 534 | |
| 535 | splines[1].Start(); |
| 536 | |
| 537 | if (!splines[1].WaitForSplineDistanceRemaining(0.01)) return; |
| 538 | |
| 539 | Aim(); |
| 540 | |
| 541 | AOS_LOG( |
| 542 | INFO, "Finished second spline %lfs\n", |
| 543 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 544 | |
| 545 | std::this_thread::sleep_for(chrono::milliseconds(1000)); |
| 546 | |
| 547 | Shoot(); |
| 548 | StopIntake(); |
| 549 | |
| 550 | if (!WaitForNoteFired(initial_shot_count + 1, std::chrono::seconds(2))) |
| 551 | return; |
| 552 | |
| 553 | StopFiring(); |
| 554 | StopAiming(); |
| 555 | |
| 556 | AOS_LOG( |
| 557 | INFO, "Shot second note, starting drive %lfs\n", |
| 558 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 559 | |
| 560 | if (!splines[2].WaitForPlan()) return; |
| 561 | |
| 562 | AOS_LOG( |
| 563 | INFO, "Starting third spline %lfs\n", |
| 564 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 565 | splines[2].Start(); |
| 566 | |
| 567 | if (!splines[2].WaitForSplineDistanceRemaining(2.0)) return; |
| 568 | |
| 569 | Intake(); |
| 570 | |
| 571 | if (!splines[2].WaitForSplineDistanceRemaining(0.01)) return; |
| 572 | |
| 573 | if (!splines[3].WaitForPlan()) return; |
| 574 | |
| 575 | AOS_LOG( |
| 576 | INFO, "Starting fourth spline %lfs\n", |
| 577 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 578 | splines[3].Start(); |
| 579 | |
| 580 | if (!splines[3].WaitForSplineDistanceRemaining(0.01)) return; |
| 581 | |
| 582 | Aim(); |
| 583 | |
| 584 | Shoot(); |
| 585 | |
| 586 | std::this_thread::sleep_for(chrono::milliseconds(400)); |
| 587 | |
| 588 | if (!WaitForNoteFired(initial_shot_count + 2, std::chrono::seconds(2))) |
| 589 | return; |
| 590 | |
| 591 | AOS_LOG( |
| 592 | INFO, "Done %lfs\n", |
| 593 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 594 | } |
| 595 | |
Filip Kujawa | 58ffbf3 | 2024-02-24 18:28:34 -0800 | [diff] [blame] | 596 | void AutonomousActor::SendSuperstructureGoal() { |
| 597 | aos::Sender<control_loops::superstructure::GoalStatic>::StaticBuilder |
| 598 | goal_builder = superstructure_goal_sender_.MakeStaticBuilder(); |
| 599 | |
| 600 | goal_builder->set_intake_goal(intake_goal_); |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 601 | if (intake_goal_ == control_loops::superstructure::IntakeGoal::INTAKE) { |
| 602 | goal_builder->set_intake_pivot( |
| 603 | control_loops::superstructure::IntakePivotGoal::DOWN); |
| 604 | } else { |
| 605 | goal_builder->set_intake_pivot( |
| 606 | control_loops::superstructure::IntakePivotGoal::UP); |
| 607 | } |
Filip Kujawa | 58ffbf3 | 2024-02-24 18:28:34 -0800 | [diff] [blame] | 608 | goal_builder->set_note_goal(note_goal_); |
| 609 | goal_builder->set_fire(fire_); |
| 610 | |
| 611 | control_loops::superstructure::ShooterGoalStatic *shooter_goal = |
| 612 | goal_builder->add_shooter_goal(); |
| 613 | |
| 614 | shooter_goal->set_auto_aim(auto_aim_); |
| 615 | shooter_goal->set_preloaded(preloaded_); |
| 616 | |
| 617 | goal_builder.CheckOk(goal_builder.Send()); |
| 618 | } |
| 619 | |
James Kuszmaul | 58f5143 | 2024-03-24 15:00:06 -0700 | [diff] [blame] | 620 | void AutonomousActor::StopIntake() { |
| 621 | set_intake_goal(control_loops::superstructure::IntakeGoal::NONE); |
| 622 | set_note_goal(control_loops::superstructure::NoteGoal::CATAPULT); |
| 623 | SendSuperstructureGoal(); |
| 624 | } |
| 625 | |
Filip Kujawa | 58ffbf3 | 2024-02-24 18:28:34 -0800 | [diff] [blame] | 626 | void AutonomousActor::Intake() { |
| 627 | set_intake_goal(control_loops::superstructure::IntakeGoal::INTAKE); |
| 628 | set_note_goal(control_loops::superstructure::NoteGoal::CATAPULT); |
| 629 | SendSuperstructureGoal(); |
| 630 | } |
| 631 | |
James Kuszmaul | 337e3a5 | 2024-05-04 14:06:52 -0700 | [diff] [blame] | 632 | void AutonomousActor::StopAiming() { |
| 633 | set_auto_aim(control_loops::superstructure::AutoAimMode::NONE); |
| 634 | SendSuperstructureGoal(); |
| 635 | } |
| 636 | |
Filip Kujawa | 58ffbf3 | 2024-02-24 18:28:34 -0800 | [diff] [blame] | 637 | void AutonomousActor::Aim() { |
Filip Kujawa | 1286c01 | 2024-03-31 22:53:27 -0700 | [diff] [blame] | 638 | set_auto_aim(control_loops::superstructure::AutoAimMode::SPEAKER); |
Filip Kujawa | 58ffbf3 | 2024-02-24 18:28:34 -0800 | [diff] [blame] | 639 | SendSuperstructureGoal(); |
| 640 | } |
| 641 | |
| 642 | void AutonomousActor::Shoot() { |
| 643 | set_fire(true); |
| 644 | SendSuperstructureGoal(); |
| 645 | } |
| 646 | |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 647 | void AutonomousActor::StopFiring() { |
| 648 | set_fire(false); |
| 649 | SendSuperstructureGoal(); |
| 650 | } |
| 651 | |
Filip Kujawa | 58ffbf3 | 2024-02-24 18:28:34 -0800 | [diff] [blame] | 652 | [[nodiscard]] bool AutonomousActor::WaitForPreloaded() { |
| 653 | set_preloaded(true); |
| 654 | SendSuperstructureGoal(); |
| 655 | |
| 656 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
| 657 | event_loop()->monotonic_now(), |
| 658 | aos::common::actions::kLoopOffset); |
| 659 | |
| 660 | bool loaded = false; |
| 661 | while (!loaded) { |
| 662 | if (ShouldCancel()) { |
| 663 | return false; |
| 664 | } |
| 665 | |
| 666 | phased_loop.SleepUntilNext(); |
| 667 | superstructure_status_fetcher_.Fetch(); |
| 668 | CHECK(superstructure_status_fetcher_.get() != nullptr); |
| 669 | |
| 670 | loaded = (superstructure_status_fetcher_->shooter()->catapult_state() == |
| 671 | control_loops::superstructure::CatapultState::LOADED); |
| 672 | } |
| 673 | |
| 674 | set_preloaded(false); |
| 675 | SendSuperstructureGoal(); |
| 676 | |
| 677 | return true; |
| 678 | } |
| 679 | |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 680 | uint32_t AutonomousActor::shot_count() { |
| 681 | superstructure_status_fetcher_.Fetch(); |
| 682 | return superstructure_status_fetcher_->shot_count(); |
| 683 | } |
| 684 | |
| 685 | [[nodiscard]] bool AutonomousActor::WaitForNoteFired( |
| 686 | uint32_t penultimate_target_shot_count, std::chrono::nanoseconds timeout) { |
| 687 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
| 688 | event_loop()->monotonic_now(), |
| 689 | aos::common::actions::kLoopOffset); |
| 690 | aos::monotonic_clock::time_point end_time = |
| 691 | event_loop()->monotonic_now() + timeout; |
| 692 | while (true) { |
| 693 | if (ShouldCancel()) { |
| 694 | return false; |
| 695 | } |
| 696 | |
| 697 | phased_loop.SleepUntilNext(); |
| 698 | |
| 699 | if (shot_count() > penultimate_target_shot_count || |
| 700 | event_loop()->monotonic_now() > end_time) { |
| 701 | return true; |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | [[nodiscard]] bool AutonomousActor::WaitForCatapultReady() { |
| 707 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
| 708 | event_loop()->monotonic_now(), |
| 709 | aos::common::actions::kLoopOffset); |
| 710 | |
| 711 | bool loaded = false; |
| 712 | while (!loaded) { |
| 713 | if (ShouldCancel()) { |
| 714 | return false; |
| 715 | } |
| 716 | |
| 717 | phased_loop.SleepUntilNext(); |
| 718 | superstructure_status_fetcher_.Fetch(); |
| 719 | CHECK(superstructure_status_fetcher_.get() != nullptr); |
| 720 | |
| 721 | loaded = (superstructure_status_fetcher_->state() == |
| 722 | control_loops::superstructure::SuperstructureState::READY); |
| 723 | } |
| 724 | |
| 725 | SendSuperstructureGoal(); |
| 726 | |
| 727 | return true; |
| 728 | } |
| 729 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 730 | } // namespace y2024::autonomous |