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")), |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 54 | robot_constants_(CHECK_NOTNULL(robot_constants)), |
Filip Kujawa | 58ffbf3 | 2024-02-24 18:28:34 -0800 | [diff] [blame] | 55 | auto_splines_() {} |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 56 | |
| 57 | void AutonomousActor::Replan() { |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 58 | AutonomousMode mode = robot_constants_->common()->autonomous_mode(); |
| 59 | switch (mode) { |
| 60 | case AutonomousMode::NONE: |
| 61 | break; |
| 62 | case AutonomousMode::SPLINE_AUTO: |
| 63 | test_spline_ = |
| 64 | PlanSpline(std::bind(&AutonomousSplines::TestSpline, &auto_splines_, |
| 65 | std::placeholders::_1, alliance_), |
| 66 | SplineDirection::kForward); |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 67 | |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 68 | starting_position_ = test_spline_->starting_position(); |
| 69 | break; |
| 70 | case AutonomousMode::MOBILITY_AND_SHOOT: |
| 71 | AOS_LOG(INFO, "Mobility & shoot replanning!"); |
| 72 | mobility_and_shoot_splines_ = {PlanSpline( |
| 73 | std::bind(&AutonomousSplines::MobilityAndShootSpline, &auto_splines_, |
| 74 | std::placeholders::_1, alliance_), |
| 75 | SplineDirection::kForward)}; |
| 76 | |
| 77 | starting_position_ = |
| 78 | mobility_and_shoot_splines_.value()[0].starting_position(); |
| 79 | CHECK(starting_position_); |
| 80 | break; |
James Kuszmaul | 58f5143 | 2024-03-24 15:00:06 -0700 | [diff] [blame^] | 81 | case AutonomousMode::FIVE_PIECE: |
| 82 | AOS_LOG(INFO, "FIVE_PIECE replanning!"); |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 83 | four_piece_splines_ = { |
| 84 | PlanSpline( |
| 85 | std::bind(&AutonomousSplines::FourPieceSpline1, &auto_splines_, |
| 86 | std::placeholders::_1, alliance_), |
| 87 | SplineDirection::kForward), |
| 88 | PlanSpline( |
| 89 | std::bind(&AutonomousSplines::FourPieceSpline2, &auto_splines_, |
| 90 | std::placeholders::_1, alliance_), |
| 91 | SplineDirection::kBackward), |
| 92 | PlanSpline( |
| 93 | std::bind(&AutonomousSplines::FourPieceSpline3, &auto_splines_, |
| 94 | std::placeholders::_1, alliance_), |
| 95 | SplineDirection::kForward), |
| 96 | PlanSpline( |
| 97 | std::bind(&AutonomousSplines::FourPieceSpline4, &auto_splines_, |
| 98 | std::placeholders::_1, alliance_), |
| 99 | SplineDirection::kForward), |
| 100 | PlanSpline( |
| 101 | std::bind(&AutonomousSplines::FourPieceSpline5, &auto_splines_, |
| 102 | std::placeholders::_1, alliance_), |
| 103 | SplineDirection::kBackward)}; |
| 104 | |
| 105 | starting_position_ = four_piece_splines_.value()[0].starting_position(); |
| 106 | CHECK(starting_position_); |
| 107 | break; |
Maxwell Henderson | a2dadd0 | 2024-03-24 13:57:09 -0700 | [diff] [blame] | 108 | case AutonomousMode::TWO_PIECE_STEAL: |
| 109 | AOS_LOG(INFO, "TWO_PIECE_STEAL replanning!"); |
| 110 | two_piece_steal_splines_ = { |
| 111 | PlanSpline( |
| 112 | std::bind(&AutonomousSplines::TwoPieceStealSpline1, |
| 113 | &auto_splines_, std::placeholders::_1, alliance_), |
| 114 | SplineDirection::kForward), |
| 115 | PlanSpline( |
| 116 | std::bind(&AutonomousSplines::TwoPieceStealSpline2, |
| 117 | &auto_splines_, std::placeholders::_1, alliance_), |
| 118 | SplineDirection::kBackward), |
| 119 | PlanSpline( |
| 120 | std::bind(&AutonomousSplines::TwoPieceStealSpline3, |
| 121 | &auto_splines_, std::placeholders::_1, alliance_), |
| 122 | SplineDirection::kForward), |
| 123 | PlanSpline( |
| 124 | std::bind(&AutonomousSplines::TwoPieceStealSpline4, |
| 125 | &auto_splines_, std::placeholders::_1, alliance_), |
James Kuszmaul | 58f5143 | 2024-03-24 15:00:06 -0700 | [diff] [blame^] | 126 | SplineDirection::kBackward)}; |
| 127 | |
| 128 | starting_position_ = |
| 129 | two_piece_steal_splines_.value()[0].starting_position(); |
| 130 | CHECK(starting_position_); |
| 131 | break; |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | is_planned_ = true; |
| 135 | |
| 136 | MaybeSendStartingPosition(); |
| 137 | } |
| 138 | |
Filip Kujawa | 58ffbf3 | 2024-02-24 18:28:34 -0800 | [diff] [blame] | 139 | bool AutonomousActor::Run( |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 140 | const ::frc971::autonomous::AutonomousActionParams *params) { |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 141 | AOS_LOG(INFO, "Params are %d\n", params->mode()); |
| 142 | if (alliance_ == aos::Alliance::kInvalid) { |
| 143 | AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection."); |
| 144 | return false; |
| 145 | } |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 146 | |
| 147 | AutonomousMode mode = robot_constants_->common()->autonomous_mode(); |
| 148 | switch (mode) { |
| 149 | case AutonomousMode::NONE: |
| 150 | AOS_LOG(WARNING, "No auto mode selected."); |
| 151 | break; |
| 152 | case AutonomousMode::SPLINE_AUTO: |
| 153 | SplineAuto(); |
| 154 | break; |
| 155 | case AutonomousMode::MOBILITY_AND_SHOOT: |
| 156 | MobilityAndShoot(); |
| 157 | break; |
James Kuszmaul | 58f5143 | 2024-03-24 15:00:06 -0700 | [diff] [blame^] | 158 | case AutonomousMode::FIVE_PIECE: |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 159 | FourPieceAuto(); |
| 160 | break; |
Maxwell Henderson | a2dadd0 | 2024-03-24 13:57:09 -0700 | [diff] [blame] | 161 | case AutonomousMode::TWO_PIECE_STEAL: |
| 162 | TwoPieceStealAuto(); |
| 163 | break; |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 164 | } |
| 165 | return true; |
| 166 | } |
| 167 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 168 | void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) { |
| 169 | // Set up the starting position for the blue alliance. |
| 170 | |
| 171 | auto builder = localizer_control_sender_.MakeBuilder(); |
| 172 | |
| 173 | LocalizerControl::Builder localizer_control_builder = |
| 174 | builder.MakeBuilder<LocalizerControl>(); |
| 175 | localizer_control_builder.add_x(start(0)); |
| 176 | localizer_control_builder.add_y(start(1)); |
| 177 | localizer_control_builder.add_theta(start(2)); |
| 178 | localizer_control_builder.add_theta_uncertainty(0.00001); |
| 179 | AOS_LOG(INFO, "User button pressed, x: %f y: %f theta: %f", start(0), |
| 180 | start(1), start(2)); |
| 181 | if (builder.Send(localizer_control_builder.Finish()) != |
| 182 | aos::RawSender::Error::kOk) { |
| 183 | AOS_LOG(ERROR, "Failed to reset localizer.\n"); |
| 184 | } |
| 185 | } |
Filip Kujawa | 58ffbf3 | 2024-02-24 18:28:34 -0800 | [diff] [blame] | 186 | |
| 187 | void AutonomousActor::Reset() { |
| 188 | set_intake_goal(control_loops::superstructure::IntakeGoal::NONE); |
| 189 | set_note_goal(control_loops::superstructure::NoteGoal::NONE); |
| 190 | set_auto_aim(false); |
| 191 | set_fire(false); |
| 192 | set_preloaded(false); |
| 193 | SendSuperstructureGoal(); |
| 194 | } |
| 195 | |
| 196 | void AutonomousActor::SplineAuto() { |
| 197 | CHECK(test_spline_); |
| 198 | |
| 199 | if (!test_spline_->WaitForPlan()) return; |
| 200 | test_spline_->Start(); |
| 201 | |
| 202 | if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return; |
| 203 | } |
| 204 | |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 205 | void AutonomousActor::MobilityAndShoot() { |
| 206 | aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now(); |
| 207 | |
| 208 | uint32_t initial_shot_count = shot_count(); |
| 209 | |
| 210 | CHECK(mobility_and_shoot_splines_); |
| 211 | |
| 212 | auto &splines = *mobility_and_shoot_splines_; |
| 213 | |
| 214 | AOS_LOG(INFO, "Going to preload"); |
| 215 | |
| 216 | // Always be auto-aiming. |
| 217 | Aim(); |
| 218 | |
| 219 | if (!WaitForPreloaded()) return; |
| 220 | |
| 221 | AOS_LOG(INFO, "Starting to Move"); |
| 222 | |
| 223 | if (!splines[0].WaitForPlan()) return; |
| 224 | |
| 225 | splines[0].Start(); |
| 226 | |
| 227 | if (!splines[0].WaitForSplineDistanceRemaining(0.05)) return; |
| 228 | |
| 229 | AOS_LOG( |
| 230 | INFO, "Got there %lf s\nNow Shooting\n", |
| 231 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 232 | |
| 233 | Shoot(); |
| 234 | |
| 235 | if (!WaitForNoteFired(initial_shot_count, std::chrono::seconds(5))) return; |
| 236 | |
| 237 | StopFiring(); |
| 238 | |
| 239 | AOS_LOG( |
| 240 | INFO, "Note fired at %lf seconds\n", |
| 241 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 242 | } |
| 243 | |
| 244 | void AutonomousActor::FourPieceAuto() { |
| 245 | aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now(); |
| 246 | |
| 247 | CHECK(four_piece_splines_); |
| 248 | auto &splines = *four_piece_splines_; |
| 249 | |
| 250 | uint32_t initial_shot_count = shot_count(); |
| 251 | |
| 252 | // Always be aiming & firing. |
| 253 | Aim(); |
| 254 | if (!WaitForPreloaded()) return; |
| 255 | |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 256 | Shoot(); |
| 257 | |
| 258 | AOS_LOG( |
| 259 | INFO, "Shooting Preloaded Note %lfs\n", |
| 260 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 261 | |
| 262 | if (!WaitForNoteFired(initial_shot_count, std::chrono::seconds(2))) return; |
| 263 | |
| 264 | AOS_LOG( |
| 265 | INFO, "Shot first note %lfs\n", |
| 266 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 267 | |
| 268 | Intake(); |
| 269 | StopFiring(); |
| 270 | |
| 271 | AOS_LOG( |
| 272 | INFO, "Starting Spline 1 %lfs\n", |
| 273 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 274 | |
| 275 | if (!splines[0].WaitForPlan()) return; |
| 276 | |
| 277 | splines[0].Start(); |
| 278 | |
| 279 | if (!splines[0].WaitForSplineDistanceRemaining(0.01)) return; |
| 280 | |
| 281 | if (!splines[1].WaitForPlan()) return; |
| 282 | |
| 283 | AOS_LOG( |
| 284 | INFO, "Starting second spline %lfs\n", |
| 285 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 286 | |
| 287 | splines[1].Start(); |
| 288 | |
| 289 | if (!splines[1].WaitForSplineDistanceRemaining(0.01)) return; |
| 290 | |
| 291 | AOS_LOG( |
| 292 | INFO, "Finished second spline %lfs\n", |
| 293 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 294 | |
James Kuszmaul | 58f5143 | 2024-03-24 15:00:06 -0700 | [diff] [blame^] | 295 | std::this_thread::sleep_for(chrono::milliseconds(200)); |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 296 | |
| 297 | Shoot(); |
| 298 | |
| 299 | if (!WaitForNoteFired(initial_shot_count + 1, std::chrono::seconds(2))) |
| 300 | return; |
| 301 | |
| 302 | AOS_LOG( |
| 303 | INFO, "Shot second note, starting drive %lfs\n", |
| 304 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 305 | |
| 306 | if (!splines[2].WaitForPlan()) return; |
| 307 | splines[2].Start(); |
| 308 | |
| 309 | if (!splines[2].WaitForSplineDistanceRemaining(0.01)) return; |
| 310 | |
| 311 | if (!WaitForNoteFired(initial_shot_count + 3, std::chrono::seconds(5))) |
| 312 | return; |
| 313 | |
| 314 | AOS_LOG( |
| 315 | INFO, "Finished 4 notes at %lfs\n", |
| 316 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 317 | |
| 318 | if (!FLAGS_do_fifth_piece) { |
| 319 | AOS_LOG(INFO, "Exitting early due to --nodo_fifth_piece"); |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | if (!splines[3].WaitForPlan()) return; |
| 324 | splines[3].Start(); |
| 325 | |
| 326 | if (!splines[3].WaitForSplineDistanceRemaining(0.1)) return; |
| 327 | |
| 328 | AOS_LOG( |
| 329 | INFO, "Got to 5th note at %lfs\n", |
| 330 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 331 | |
| 332 | StopFiring(); |
| 333 | |
| 334 | if (!splines[4].WaitForPlan()) return; |
| 335 | splines[4].Start(); |
| 336 | |
James Kuszmaul | f2f95b3 | 2024-03-23 20:12:05 -0700 | [diff] [blame] | 337 | if (!splines[4].WaitForSplineDistanceRemaining(0.01)) return; |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 338 | |
| 339 | AOS_LOG( |
| 340 | INFO, "Done with spline %lfs\n", |
| 341 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 342 | |
James Kuszmaul | 58f5143 | 2024-03-24 15:00:06 -0700 | [diff] [blame^] | 343 | std::this_thread::sleep_for(chrono::milliseconds(250)); |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 344 | |
| 345 | AOS_LOG( |
| 346 | INFO, "Shooting last note! %lfs\n", |
| 347 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 348 | |
| 349 | Shoot(); |
| 350 | |
| 351 | if (!WaitForNoteFired(initial_shot_count + 4, std::chrono::seconds(5))) |
| 352 | return; |
| 353 | |
| 354 | AOS_LOG( |
| 355 | INFO, "Done %lfs\n", |
| 356 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 357 | } |
| 358 | |
Maxwell Henderson | a2dadd0 | 2024-03-24 13:57:09 -0700 | [diff] [blame] | 359 | void AutonomousActor::TwoPieceStealAuto() { |
| 360 | aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now(); |
| 361 | |
| 362 | CHECK(two_piece_steal_splines_); |
| 363 | auto &splines = *two_piece_steal_splines_; |
| 364 | |
| 365 | uint32_t initial_shot_count = shot_count(); |
| 366 | |
| 367 | // Always be aiming & firing. |
| 368 | Aim(); |
| 369 | if (!WaitForPreloaded()) return; |
| 370 | |
Maxwell Henderson | a2dadd0 | 2024-03-24 13:57:09 -0700 | [diff] [blame] | 371 | Shoot(); |
| 372 | |
| 373 | AOS_LOG( |
| 374 | INFO, "Shooting Preloaded Note %lfs\n", |
| 375 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 376 | |
| 377 | if (!WaitForNoteFired(initial_shot_count, std::chrono::seconds(2))) return; |
| 378 | |
| 379 | AOS_LOG( |
| 380 | INFO, "Shot first note %lfs\n", |
| 381 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 382 | |
Maxwell Henderson | a2dadd0 | 2024-03-24 13:57:09 -0700 | [diff] [blame] | 383 | StopFiring(); |
| 384 | |
| 385 | AOS_LOG( |
| 386 | INFO, "Starting Spline 1 %lfs\n", |
| 387 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 388 | |
| 389 | if (!splines[0].WaitForPlan()) return; |
| 390 | |
| 391 | splines[0].Start(); |
| 392 | |
James Kuszmaul | 58f5143 | 2024-03-24 15:00:06 -0700 | [diff] [blame^] | 393 | if (!splines[0].WaitForSplineDistanceRemaining(2.0)) return; |
| 394 | Intake(); |
| 395 | |
Maxwell Henderson | a2dadd0 | 2024-03-24 13:57:09 -0700 | [diff] [blame] | 396 | if (!splines[0].WaitForSplineDistanceRemaining(0.01)) return; |
| 397 | |
| 398 | if (!splines[1].WaitForPlan()) return; |
| 399 | |
| 400 | AOS_LOG( |
| 401 | INFO, "Starting second spline %lfs\n", |
| 402 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 403 | |
| 404 | splines[1].Start(); |
| 405 | |
| 406 | if (!splines[1].WaitForSplineDistanceRemaining(0.01)) return; |
| 407 | |
| 408 | AOS_LOG( |
| 409 | INFO, "Finished second spline %lfs\n", |
| 410 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 411 | |
James Kuszmaul | 58f5143 | 2024-03-24 15:00:06 -0700 | [diff] [blame^] | 412 | std::this_thread::sleep_for(chrono::milliseconds(200)); |
Maxwell Henderson | a2dadd0 | 2024-03-24 13:57:09 -0700 | [diff] [blame] | 413 | |
| 414 | Shoot(); |
James Kuszmaul | 58f5143 | 2024-03-24 15:00:06 -0700 | [diff] [blame^] | 415 | StopIntake(); |
Maxwell Henderson | a2dadd0 | 2024-03-24 13:57:09 -0700 | [diff] [blame] | 416 | |
| 417 | if (!WaitForNoteFired(initial_shot_count + 1, std::chrono::seconds(2))) |
| 418 | return; |
| 419 | |
| 420 | StopFiring(); |
| 421 | |
| 422 | AOS_LOG( |
| 423 | INFO, "Shot second note, starting drive %lfs\n", |
| 424 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 425 | |
| 426 | if (!splines[2].WaitForPlan()) return; |
| 427 | |
| 428 | AOS_LOG( |
| 429 | INFO, "Starting third spline %lfs\n", |
| 430 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 431 | splines[2].Start(); |
| 432 | |
James Kuszmaul | 58f5143 | 2024-03-24 15:00:06 -0700 | [diff] [blame^] | 433 | if (!splines[2].WaitForSplineDistanceRemaining(2.0)) return; |
| 434 | |
| 435 | Intake(); |
| 436 | |
Maxwell Henderson | a2dadd0 | 2024-03-24 13:57:09 -0700 | [diff] [blame] | 437 | if (!splines[2].WaitForSplineDistanceRemaining(0.01)) return; |
| 438 | |
| 439 | if (!splines[3].WaitForPlan()) return; |
| 440 | |
| 441 | AOS_LOG( |
| 442 | INFO, "Starting fourth spline %lfs\n", |
| 443 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 444 | splines[3].Start(); |
| 445 | |
| 446 | if (!splines[3].WaitForSplineDistanceRemaining(0.01)) return; |
| 447 | |
| 448 | Shoot(); |
| 449 | |
James Kuszmaul | 58f5143 | 2024-03-24 15:00:06 -0700 | [diff] [blame^] | 450 | std::this_thread::sleep_for(chrono::milliseconds(400)); |
| 451 | |
Maxwell Henderson | a2dadd0 | 2024-03-24 13:57:09 -0700 | [diff] [blame] | 452 | if (!WaitForNoteFired(initial_shot_count + 2, std::chrono::seconds(2))) |
| 453 | return; |
| 454 | |
| 455 | AOS_LOG( |
| 456 | INFO, "Done %lfs\n", |
| 457 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 458 | } |
| 459 | |
Filip Kujawa | 58ffbf3 | 2024-02-24 18:28:34 -0800 | [diff] [blame] | 460 | void AutonomousActor::SendSuperstructureGoal() { |
| 461 | aos::Sender<control_loops::superstructure::GoalStatic>::StaticBuilder |
| 462 | goal_builder = superstructure_goal_sender_.MakeStaticBuilder(); |
| 463 | |
| 464 | goal_builder->set_intake_goal(intake_goal_); |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 465 | if (intake_goal_ == control_loops::superstructure::IntakeGoal::INTAKE) { |
| 466 | goal_builder->set_intake_pivot( |
| 467 | control_loops::superstructure::IntakePivotGoal::DOWN); |
| 468 | } else { |
| 469 | goal_builder->set_intake_pivot( |
| 470 | control_loops::superstructure::IntakePivotGoal::UP); |
| 471 | } |
Filip Kujawa | 58ffbf3 | 2024-02-24 18:28:34 -0800 | [diff] [blame] | 472 | goal_builder->set_note_goal(note_goal_); |
| 473 | goal_builder->set_fire(fire_); |
| 474 | |
| 475 | control_loops::superstructure::ShooterGoalStatic *shooter_goal = |
| 476 | goal_builder->add_shooter_goal(); |
| 477 | |
| 478 | shooter_goal->set_auto_aim(auto_aim_); |
| 479 | shooter_goal->set_preloaded(preloaded_); |
| 480 | |
| 481 | goal_builder.CheckOk(goal_builder.Send()); |
| 482 | } |
| 483 | |
James Kuszmaul | 58f5143 | 2024-03-24 15:00:06 -0700 | [diff] [blame^] | 484 | void AutonomousActor::StopIntake() { |
| 485 | set_intake_goal(control_loops::superstructure::IntakeGoal::NONE); |
| 486 | set_note_goal(control_loops::superstructure::NoteGoal::CATAPULT); |
| 487 | SendSuperstructureGoal(); |
| 488 | } |
| 489 | |
Filip Kujawa | 58ffbf3 | 2024-02-24 18:28:34 -0800 | [diff] [blame] | 490 | void AutonomousActor::Intake() { |
| 491 | set_intake_goal(control_loops::superstructure::IntakeGoal::INTAKE); |
| 492 | set_note_goal(control_loops::superstructure::NoteGoal::CATAPULT); |
| 493 | SendSuperstructureGoal(); |
| 494 | } |
| 495 | |
| 496 | void AutonomousActor::Aim() { |
| 497 | set_auto_aim(true); |
| 498 | SendSuperstructureGoal(); |
| 499 | } |
| 500 | |
| 501 | void AutonomousActor::Shoot() { |
| 502 | set_fire(true); |
| 503 | SendSuperstructureGoal(); |
| 504 | } |
| 505 | |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 506 | void AutonomousActor::StopFiring() { |
| 507 | set_fire(false); |
| 508 | SendSuperstructureGoal(); |
| 509 | } |
| 510 | |
Filip Kujawa | 58ffbf3 | 2024-02-24 18:28:34 -0800 | [diff] [blame] | 511 | [[nodiscard]] bool AutonomousActor::WaitForPreloaded() { |
| 512 | set_preloaded(true); |
| 513 | SendSuperstructureGoal(); |
| 514 | |
| 515 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
| 516 | event_loop()->monotonic_now(), |
| 517 | aos::common::actions::kLoopOffset); |
| 518 | |
| 519 | bool loaded = false; |
| 520 | while (!loaded) { |
| 521 | if (ShouldCancel()) { |
| 522 | return false; |
| 523 | } |
| 524 | |
| 525 | phased_loop.SleepUntilNext(); |
| 526 | superstructure_status_fetcher_.Fetch(); |
| 527 | CHECK(superstructure_status_fetcher_.get() != nullptr); |
| 528 | |
| 529 | loaded = (superstructure_status_fetcher_->shooter()->catapult_state() == |
| 530 | control_loops::superstructure::CatapultState::LOADED); |
| 531 | } |
| 532 | |
| 533 | set_preloaded(false); |
| 534 | SendSuperstructureGoal(); |
| 535 | |
| 536 | return true; |
| 537 | } |
| 538 | |
James Kuszmaul | b5f1183 | 2024-03-15 22:30:59 -0700 | [diff] [blame] | 539 | uint32_t AutonomousActor::shot_count() { |
| 540 | superstructure_status_fetcher_.Fetch(); |
| 541 | return superstructure_status_fetcher_->shot_count(); |
| 542 | } |
| 543 | |
| 544 | [[nodiscard]] bool AutonomousActor::WaitForNoteFired( |
| 545 | uint32_t penultimate_target_shot_count, std::chrono::nanoseconds timeout) { |
| 546 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
| 547 | event_loop()->monotonic_now(), |
| 548 | aos::common::actions::kLoopOffset); |
| 549 | aos::monotonic_clock::time_point end_time = |
| 550 | event_loop()->monotonic_now() + timeout; |
| 551 | while (true) { |
| 552 | if (ShouldCancel()) { |
| 553 | return false; |
| 554 | } |
| 555 | |
| 556 | phased_loop.SleepUntilNext(); |
| 557 | |
| 558 | if (shot_count() > penultimate_target_shot_count || |
| 559 | event_loop()->monotonic_now() > end_time) { |
| 560 | return true; |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | [[nodiscard]] bool AutonomousActor::WaitForCatapultReady() { |
| 566 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
| 567 | event_loop()->monotonic_now(), |
| 568 | aos::common::actions::kLoopOffset); |
| 569 | |
| 570 | bool loaded = false; |
| 571 | while (!loaded) { |
| 572 | if (ShouldCancel()) { |
| 573 | return false; |
| 574 | } |
| 575 | |
| 576 | phased_loop.SleepUntilNext(); |
| 577 | superstructure_status_fetcher_.Fetch(); |
| 578 | CHECK(superstructure_status_fetcher_.get() != nullptr); |
| 579 | |
| 580 | loaded = (superstructure_status_fetcher_->state() == |
| 581 | control_loops::superstructure::SuperstructureState::READY); |
| 582 | } |
| 583 | |
| 584 | SendSuperstructureGoal(); |
| 585 | |
| 586 | return true; |
| 587 | } |
| 588 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 589 | } // namespace y2024::autonomous |