Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 1 | #include "y2023/autonomous/autonomous_actor.h" |
| 2 | |
| 3 | #include <chrono> |
| 4 | #include <cinttypes> |
| 5 | #include <cmath> |
| 6 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 7 | #include "absl/flags/flag.h" |
| 8 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 9 | #include "aos/logging/logging.h" |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 10 | #include "aos/util/math.h" |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 11 | #include "frc971/control_loops/drivetrain/localizer_generated.h" |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 12 | #include "y2023/autonomous/auto_splines.h" |
| 13 | #include "y2023/constants.h" |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 14 | #include "y2023/control_loops/drivetrain/drivetrain_base.h" |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 15 | #include "y2023/control_loops/superstructure/arm/generated_graph.h" |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 16 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 17 | ABSL_FLAG(bool, spline_auto, false, "Run simple test S-spline auto mode."); |
| 18 | ABSL_FLAG(bool, charged_up, true, "If true run charged up autonomous mode"); |
| 19 | ABSL_FLAG(bool, charged_up_cable, false, |
| 20 | "If true run cable side autonomous mode"); |
| 21 | ABSL_FLAG(bool, do_balance, true, "If true run the balance."); |
James Kuszmaul | 713c5ce | 2023-03-04 18:23:24 -0800 | [diff] [blame] | 22 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 23 | namespace y2023::autonomous { |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 24 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 25 | using ::frc971::ProfileParametersT; |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 26 | |
| 27 | ProfileParametersT MakeProfileParameters(float max_velocity, |
| 28 | float max_acceleration) { |
| 29 | ProfileParametersT result; |
| 30 | result.max_velocity = max_velocity; |
| 31 | result.max_acceleration = max_acceleration; |
| 32 | return result; |
| 33 | } |
| 34 | |
| 35 | using ::aos::monotonic_clock; |
| 36 | using frc971::CreateProfileParameters; |
| 37 | using ::frc971::ProfileParametersT; |
| 38 | using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal; |
| 39 | using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 40 | using frc971::control_loops::drivetrain::LocalizerControl; |
| 41 | namespace chrono = ::std::chrono; |
| 42 | |
| 43 | AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop) |
Maxwell Henderson | 12b07b9 | 2024-01-08 20:17:25 -0800 | [diff] [blame] | 44 | : frc971::autonomous::UserButtonLocalizedAutonomousActor( |
James Kuszmaul | 713c5ce | 2023-03-04 18:23:24 -0800 | [diff] [blame] | 45 | event_loop, control_loops::drivetrain::GetDrivetrainConfig()), |
| 46 | localizer_control_sender_( |
| 47 | event_loop->MakeSender< |
| 48 | ::frc971::control_loops::drivetrain::LocalizerControl>( |
| 49 | "/drivetrain")), |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 50 | auto_splines_(), |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 51 | arm_goal_position_(control_loops::superstructure::arm::StartingIndex()), |
| 52 | superstructure_goal_sender_( |
| 53 | event_loop->MakeSender<::y2023::control_loops::superstructure::Goal>( |
| 54 | "/superstructure")), |
| 55 | superstructure_status_fetcher_( |
| 56 | event_loop |
| 57 | ->MakeFetcher<::y2023::control_loops::superstructure::Status>( |
| 58 | "/superstructure")), |
Maxwell Henderson | 12b07b9 | 2024-01-08 20:17:25 -0800 | [diff] [blame] | 59 | points_(control_loops::superstructure::arm::PointList()) {} |
James Kuszmaul | 713c5ce | 2023-03-04 18:23:24 -0800 | [diff] [blame] | 60 | |
| 61 | void AutonomousActor::Replan() { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 62 | if (absl::GetFlag(FLAGS_spline_auto)) { |
James Kuszmaul | 713c5ce | 2023-03-04 18:23:24 -0800 | [diff] [blame] | 63 | test_spline_ = |
| 64 | PlanSpline(std::bind(&AutonomousSplines::TestSpline, &auto_splines_, |
| 65 | std::placeholders::_1, alliance_), |
| 66 | SplineDirection::kForward); |
| 67 | |
| 68 | starting_position_ = test_spline_->starting_position(); |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 69 | } else if (absl::GetFlag(FLAGS_charged_up)) { |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 70 | AOS_LOG(INFO, "Charged up replanning!"); |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 71 | charged_up_splines_ = { |
| 72 | PlanSpline(std::bind(&AutonomousSplines::Spline1, &auto_splines_, |
| 73 | std::placeholders::_1, alliance_), |
| 74 | SplineDirection::kBackward), |
| 75 | PlanSpline(std::bind(&AutonomousSplines::Spline2, &auto_splines_, |
| 76 | std::placeholders::_1, alliance_), |
| 77 | SplineDirection::kForward), |
| 78 | PlanSpline(std::bind(&AutonomousSplines::Spline3, &auto_splines_, |
| 79 | std::placeholders::_1, alliance_), |
| 80 | SplineDirection::kBackward), |
| 81 | PlanSpline(std::bind(&AutonomousSplines::Spline4, &auto_splines_, |
| 82 | std::placeholders::_1, alliance_), |
Maxwell Henderson | 2537883 | 2023-04-07 14:37:41 -0700 | [diff] [blame] | 83 | SplineDirection::kForward)}; |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 84 | |
| 85 | starting_position_ = charged_up_splines_.value()[0].starting_position(); |
| 86 | CHECK(starting_position_); |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 87 | } else if (absl::GetFlag(FLAGS_charged_up_cable)) { |
Maxwell Henderson | 2537883 | 2023-04-07 14:37:41 -0700 | [diff] [blame] | 88 | charged_up_cable_splines_ = { |
| 89 | PlanSpline(std::bind(&AutonomousSplines::SplineCable1, &auto_splines_, |
| 90 | std::placeholders::_1, alliance_), |
| 91 | SplineDirection::kBackward), |
| 92 | PlanSpline(std::bind(&AutonomousSplines::SplineCable2, &auto_splines_, |
| 93 | std::placeholders::_1, alliance_), |
| 94 | SplineDirection::kForward), |
| 95 | PlanSpline(std::bind(&AutonomousSplines::SplineCable3, &auto_splines_, |
| 96 | std::placeholders::_1, alliance_), |
| 97 | SplineDirection::kBackward), |
| 98 | PlanSpline(std::bind(&AutonomousSplines::SplineCable4, &auto_splines_, |
| 99 | std::placeholders::_1, alliance_), |
| 100 | SplineDirection::kForward)}; |
| 101 | |
| 102 | starting_position_ = |
| 103 | charged_up_cable_splines_.value()[0].starting_position(); |
| 104 | CHECK(starting_position_); |
James Kuszmaul | 713c5ce | 2023-03-04 18:23:24 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | is_planned_ = true; |
| 108 | |
| 109 | MaybeSendStartingPosition(); |
| 110 | } |
| 111 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 112 | void AutonomousActor::Reset() { |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 113 | wrist_goal_ = 0.0; |
| 114 | roller_goal_ = control_loops::superstructure::RollerGoal::IDLE; |
| 115 | arm_goal_position_ = control_loops::superstructure::arm::StartingIndex(); |
| 116 | preloaded_ = false; |
| 117 | SendSuperstructureGoal(); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Maxwell Henderson | 12b07b9 | 2024-01-08 20:17:25 -0800 | [diff] [blame] | 120 | bool AutonomousActor::Run( |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 121 | const ::frc971::autonomous::AutonomousActionParams *params) { |
James Kuszmaul | 713c5ce | 2023-03-04 18:23:24 -0800 | [diff] [blame] | 122 | AOS_LOG(INFO, "Params are %d\n", params->mode()); |
| 123 | if (alliance_ == aos::Alliance::kInvalid) { |
| 124 | AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection."); |
| 125 | return false; |
| 126 | } |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 127 | if (absl::GetFlag(FLAGS_spline_auto)) { |
James Kuszmaul | 713c5ce | 2023-03-04 18:23:24 -0800 | [diff] [blame] | 128 | SplineAuto(); |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 129 | } else if (absl::GetFlag(FLAGS_charged_up)) { |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 130 | ChargedUp(); |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 131 | } else if (absl::GetFlag(FLAGS_charged_up_cable)) { |
Austin Schuh | 8c9d2d0 | 2023-04-09 20:05:42 -0700 | [diff] [blame] | 132 | ChargedUpCableSide(); |
James Kuszmaul | 713c5ce | 2023-03-04 18:23:24 -0800 | [diff] [blame] | 133 | } else { |
| 134 | AOS_LOG(WARNING, "No auto mode selected."); |
| 135 | } |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 136 | return true; |
| 137 | } |
| 138 | |
James Kuszmaul | 713c5ce | 2023-03-04 18:23:24 -0800 | [diff] [blame] | 139 | void AutonomousActor::SplineAuto() { |
| 140 | CHECK(test_spline_); |
| 141 | |
| 142 | if (!test_spline_->WaitForPlan()) return; |
| 143 | test_spline_->Start(); |
| 144 | |
| 145 | if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return; |
| 146 | } |
| 147 | |
| 148 | void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) { |
| 149 | // Set up the starting position for the blue alliance. |
| 150 | |
| 151 | auto builder = localizer_control_sender_.MakeBuilder(); |
| 152 | |
| 153 | LocalizerControl::Builder localizer_control_builder = |
| 154 | builder.MakeBuilder<LocalizerControl>(); |
| 155 | localizer_control_builder.add_x(start(0)); |
| 156 | localizer_control_builder.add_y(start(1)); |
| 157 | localizer_control_builder.add_theta(start(2)); |
| 158 | localizer_control_builder.add_theta_uncertainty(0.00001); |
| 159 | AOS_LOG(INFO, "User button pressed, x: %f y: %f theta: %f", start(0), |
| 160 | start(1), start(2)); |
| 161 | if (builder.Send(localizer_control_builder.Finish()) != |
| 162 | aos::RawSender::Error::kOk) { |
| 163 | AOS_LOG(ERROR, "Failed to reset localizer.\n"); |
| 164 | } |
| 165 | } |
| 166 | |
Maxwell Henderson | 2537883 | 2023-04-07 14:37:41 -0700 | [diff] [blame] | 167 | // Charged Up 3 Game Object Autonomous (non-cable side) |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 168 | void AutonomousActor::ChargedUp() { |
| 169 | aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now(); |
| 170 | |
| 171 | CHECK(charged_up_splines_); |
| 172 | |
| 173 | auto &splines = *charged_up_splines_; |
| 174 | |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 175 | AOS_LOG(INFO, "Going to preload"); |
| 176 | |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 177 | // Tell the superstructure a cone was preloaded |
| 178 | if (!WaitForPreloaded()) return; |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 179 | AOS_LOG(INFO, "Moving arm"); |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 180 | |
| 181 | // Place first cone on mid level |
| 182 | MidConeScore(); |
| 183 | |
| 184 | // Wait until the arm is at the goal to spit |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 185 | if (!WaitForArmGoal(0.10)) return; |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 186 | Spit(); |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 187 | if (!WaitForArmGoal(0.01)) return; |
| 188 | |
| 189 | std::this_thread::sleep_for(chrono::milliseconds(100)); |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 190 | |
| 191 | AOS_LOG( |
| 192 | INFO, "Placed first cone %lf s\n", |
| 193 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 194 | |
| 195 | // Drive and intake the cube nearest to the starting zone |
| 196 | if (!splines[0].WaitForPlan()) return; |
| 197 | splines[0].Start(); |
| 198 | |
| 199 | // Move arm into position to pickup a cube and start cube intake |
| 200 | PickupCube(); |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 201 | |
| 202 | std::this_thread::sleep_for(chrono::milliseconds(500)); |
| 203 | |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 204 | IntakeCube(); |
| 205 | |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 206 | AOS_LOG( |
| 207 | INFO, "Turning on rollers %lf s\n", |
| 208 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 209 | |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 210 | if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return; |
| 211 | |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 212 | AOS_LOG( |
| 213 | INFO, "Got there %lf s\n", |
| 214 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 215 | |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 216 | // Drive back to grid and place cube on high level |
| 217 | if (!splines[1].WaitForPlan()) return; |
| 218 | splines[1].Start(); |
| 219 | |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 220 | std::this_thread::sleep_for(chrono::milliseconds(300)); |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 221 | HighCubeScore(); |
| 222 | |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 223 | if (!splines[1].WaitForSplineDistanceRemaining(0.08)) return; |
| 224 | AOS_LOG( |
| 225 | INFO, "Back for first cube %lf s\n", |
| 226 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 227 | |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 228 | if (!WaitForArmGoal(0.10)) return; |
| 229 | |
| 230 | AOS_LOG( |
| 231 | INFO, "Arm in place for first cube %lf s\n", |
| 232 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 233 | |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 234 | Spit(); |
| 235 | |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 236 | if (!splines[1].WaitForSplineDistanceRemaining(0.08)) return; |
| 237 | |
| 238 | AOS_LOG( |
| 239 | INFO, "Finished spline back %lf s\n", |
| 240 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 241 | |
| 242 | if (!WaitForArmGoal(0.05)) return; |
| 243 | |
Austin Schuh | a7c3bc6 | 2023-04-16 19:46:23 -0700 | [diff] [blame] | 244 | std::this_thread::sleep_for(chrono::milliseconds(100)); |
| 245 | |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 246 | AOS_LOG( |
| 247 | INFO, "Placed first cube %lf s\n", |
| 248 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 249 | |
| 250 | // Drive and intake the cube second nearest to the starting zone |
| 251 | if (!splines[2].WaitForPlan()) return; |
| 252 | splines[2].Start(); |
| 253 | |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 254 | std::this_thread::sleep_for(chrono::milliseconds(200)); |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 255 | PickupCube(); |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 256 | |
| 257 | std::this_thread::sleep_for(chrono::milliseconds(500)); |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 258 | IntakeCube(); |
| 259 | |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 260 | if (!splines[2].WaitForSplineDistanceRemaining(0.05)) return; |
| 261 | AOS_LOG( |
| 262 | INFO, "Picked up second cube %lf s\n", |
| 263 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 264 | |
| 265 | // Drive back to grid and place object on mid level |
| 266 | if (!splines[3].WaitForPlan()) return; |
| 267 | splines[3].Start(); |
| 268 | |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 269 | AOS_LOG( |
| 270 | INFO, "Driving back %lf s\n", |
| 271 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 272 | |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 273 | MidCubeScore(); |
| 274 | |
Austin Schuh | 700bfff | 2023-04-05 19:45:55 -0700 | [diff] [blame] | 275 | if (!splines[3].WaitForSplineDistanceRemaining(0.07)) return; |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 276 | AOS_LOG( |
| 277 | INFO, "Got back from second cube at %lf s\n", |
| 278 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 279 | |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 280 | if (!WaitForArmGoal(0.05)) return; |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 281 | Spit(); |
| 282 | |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 283 | if (!splines[3].WaitForSplineDistanceRemaining(0.02)) return; |
| 284 | |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 285 | AOS_LOG( |
| 286 | INFO, "Placed second cube %lf s\n", |
| 287 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
James Kuszmaul | 6fc7ee2 | 2023-09-30 10:23:31 -0700 | [diff] [blame] | 288 | |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 289 | InitializeEncoders(); |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 290 | |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 291 | const ProfileParametersT kDrive = MakeProfileParameters(2.0, 4.0); |
| 292 | const ProfileParametersT kTurn = MakeProfileParameters(3.0, 4.5); |
| 293 | StartDrive(0.0, 0.0, kDrive, kTurn); |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 294 | |
Austin Schuh | 700bfff | 2023-04-05 19:45:55 -0700 | [diff] [blame] | 295 | std::this_thread::sleep_for(chrono::milliseconds(100)); |
| 296 | |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 297 | { |
| 298 | double side_scalar = (alliance_ == aos::Alliance::kRed) ? 1.0 : -1.0; |
| 299 | StartDrive(6.33 - std::abs(X()), 0.0, kDrive, kTurn); |
| 300 | if (!WaitForDriveProfileNear(0.01)) return; |
| 301 | |
| 302 | AOS_LOG( |
| 303 | INFO, "Done backing up %lf s\n", |
| 304 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 305 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 306 | if (!absl::GetFlag(FLAGS_do_balance)) { |
James Kuszmaul | 6fc7ee2 | 2023-09-30 10:23:31 -0700 | [diff] [blame] | 307 | StopSpitting(); |
| 308 | return; |
| 309 | } |
| 310 | |
Austin Schuh | a7c3bc6 | 2023-04-16 19:46:23 -0700 | [diff] [blame] | 311 | const ProfileParametersT kInPlaceTurn = MakeProfileParameters(2.7, 8.0); |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 312 | StartDrive(0.0, aos::math::NormalizeAngle(M_PI / 2.0 - Theta()), kDrive, |
| 313 | kInPlaceTurn); |
| 314 | |
| 315 | std::this_thread::sleep_for(chrono::milliseconds(400)); |
| 316 | StopSpitting(); |
| 317 | |
| 318 | AOS_LOG( |
| 319 | INFO, "Roller off %lf s\n", |
| 320 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 321 | |
Austin Schuh | 700bfff | 2023-04-05 19:45:55 -0700 | [diff] [blame] | 322 | if (!WaitForTurnProfileNear(0.6)) return; |
| 323 | AOS_LOG( |
| 324 | INFO, "Balance arm %lf s\n", |
| 325 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 326 | |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 327 | Balance(); |
Austin Schuh | 700bfff | 2023-04-05 19:45:55 -0700 | [diff] [blame] | 328 | if (!WaitForTurnProfileNear(0.001)) return; |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 329 | |
| 330 | AOS_LOG( |
| 331 | INFO, "Done turning %lf s\n", |
| 332 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 333 | |
| 334 | const ProfileParametersT kDrive = MakeProfileParameters(1.4, 3.0); |
| 335 | const ProfileParametersT kFinalTurn = MakeProfileParameters(3.0, 4.5); |
Austin Schuh | 700bfff | 2023-04-05 19:45:55 -0700 | [diff] [blame] | 336 | const double kDriveDistance = 3.11; |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 337 | StartDrive(-kDriveDistance, 0.0, kDrive, kFinalTurn); |
| 338 | |
| 339 | const ProfileParametersT kFastTurn = MakeProfileParameters(5.0, 8.0); |
| 340 | if (!WaitForDriveProfileNear(kDriveDistance - 0.4)) return; |
| 341 | |
| 342 | AOS_LOG( |
| 343 | INFO, "Turning %lf s\n", |
| 344 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 345 | StartDrive(0.0, -side_scalar * M_PI / 2.0, kDrive, kFastTurn); |
| 346 | if (!WaitForDriveProfileDone()) return; |
| 347 | if (!WaitForTurnProfileDone()) return; |
| 348 | AOS_LOG( |
| 349 | INFO, "Done %lf s\n", |
| 350 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 351 | } |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Maxwell Henderson | 2537883 | 2023-04-07 14:37:41 -0700 | [diff] [blame] | 354 | // Charged Up 3 Game Object Autonomous (cable side) |
| 355 | void AutonomousActor::ChargedUpCableSide() { |
| 356 | aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now(); |
| 357 | |
| 358 | CHECK(charged_up_cable_splines_); |
| 359 | |
| 360 | auto &splines = *charged_up_cable_splines_; |
| 361 | |
| 362 | AOS_LOG(INFO, "Going to preload"); |
| 363 | |
| 364 | // Tell the superstructure a cone was preloaded |
| 365 | if (!WaitForPreloaded()) return; |
| 366 | AOS_LOG(INFO, "Moving arm"); |
| 367 | |
| 368 | // Place first cone on mid level |
| 369 | MidConeScore(); |
| 370 | |
| 371 | // Wait until the arm is at the goal to spit |
| 372 | if (!WaitForArmGoal(0.10)) return; |
| 373 | Spit(); |
| 374 | if (!WaitForArmGoal(0.01)) return; |
| 375 | |
| 376 | std::this_thread::sleep_for(chrono::milliseconds(100)); |
| 377 | |
| 378 | AOS_LOG( |
| 379 | INFO, "Placed first cone %lf s\n", |
| 380 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 381 | |
| 382 | // Drive and intake the cube nearest to the starting zone |
| 383 | if (!splines[0].WaitForPlan()) return; |
| 384 | splines[0].Start(); |
| 385 | |
| 386 | // Move arm into position to pickup a cube and start cube intake |
| 387 | PickupCube(); |
| 388 | |
| 389 | std::this_thread::sleep_for(chrono::milliseconds(500)); |
| 390 | |
| 391 | IntakeCube(); |
| 392 | |
| 393 | AOS_LOG( |
| 394 | INFO, "Turning on rollers %lf s\n", |
| 395 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 396 | |
| 397 | if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return; |
| 398 | |
| 399 | AOS_LOG( |
| 400 | INFO, "Got there %lf s\n", |
| 401 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 402 | |
| 403 | // Drive back to grid and place cube on high level |
| 404 | if (!splines[1].WaitForPlan()) return; |
| 405 | splines[1].Start(); |
| 406 | |
| 407 | std::this_thread::sleep_for(chrono::milliseconds(300)); |
Austin Schuh | dd12882 | 2023-04-09 22:19:06 -0700 | [diff] [blame] | 408 | Neutral(); |
| 409 | |
| 410 | if (!splines[1].WaitForSplineDistanceTraveled(3.2)) return; |
Maxwell Henderson | 2537883 | 2023-04-07 14:37:41 -0700 | [diff] [blame] | 411 | HighCubeScore(); |
Austin Schuh | dd12882 | 2023-04-09 22:19:06 -0700 | [diff] [blame] | 412 | AOS_LOG( |
| 413 | INFO, "Extending arm %lf s\n", |
| 414 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
Maxwell Henderson | 2537883 | 2023-04-07 14:37:41 -0700 | [diff] [blame] | 415 | |
| 416 | if (!splines[1].WaitForSplineDistanceRemaining(0.08)) return; |
| 417 | AOS_LOG( |
| 418 | INFO, "Back for first cube %lf s\n", |
| 419 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 420 | |
| 421 | if (!WaitForArmGoal(0.10)) return; |
| 422 | |
| 423 | AOS_LOG( |
| 424 | INFO, "Arm in place for first cube %lf s\n", |
| 425 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 426 | |
| 427 | Spit(); |
| 428 | |
| 429 | if (!splines[1].WaitForSplineDistanceRemaining(0.08)) return; |
| 430 | |
| 431 | AOS_LOG( |
| 432 | INFO, "Finished spline back %lf s\n", |
| 433 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 434 | |
| 435 | if (!WaitForArmGoal(0.05)) return; |
| 436 | |
| 437 | AOS_LOG( |
| 438 | INFO, "Placed first cube %lf s\n", |
| 439 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 440 | |
| 441 | // Drive and intake the cube second nearest to the starting zone |
| 442 | if (!splines[2].WaitForPlan()) return; |
| 443 | splines[2].Start(); |
| 444 | |
| 445 | std::this_thread::sleep_for(chrono::milliseconds(200)); |
| 446 | PickupCube(); |
| 447 | |
| 448 | std::this_thread::sleep_for(chrono::milliseconds(500)); |
| 449 | IntakeCube(); |
| 450 | |
| 451 | if (!splines[2].WaitForSplineDistanceRemaining(0.05)) return; |
| 452 | AOS_LOG( |
| 453 | INFO, "Picked up second cube %lf s\n", |
| 454 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 455 | |
| 456 | // Drive back to grid and place object on mid level |
| 457 | if (!splines[3].WaitForPlan()) return; |
| 458 | splines[3].Start(); |
| 459 | |
Austin Schuh | dd12882 | 2023-04-09 22:19:06 -0700 | [diff] [blame] | 460 | std::this_thread::sleep_for(chrono::milliseconds(400)); |
| 461 | Neutral(); |
| 462 | |
Maxwell Henderson | 2537883 | 2023-04-07 14:37:41 -0700 | [diff] [blame] | 463 | AOS_LOG( |
| 464 | INFO, "Driving back %lf s\n", |
| 465 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 466 | |
Austin Schuh | dd12882 | 2023-04-09 22:19:06 -0700 | [diff] [blame] | 467 | if (!splines[3].WaitForSplineDistanceTraveled(3.5)) return; |
| 468 | AOS_LOG( |
| 469 | INFO, "Extending arm %lf s\n", |
| 470 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
Maxwell Henderson | 2537883 | 2023-04-07 14:37:41 -0700 | [diff] [blame] | 471 | MidCubeScore(); |
| 472 | |
| 473 | if (!splines[3].WaitForSplineDistanceRemaining(0.07)) return; |
| 474 | AOS_LOG( |
| 475 | INFO, "Got back from second cube at %lf s\n", |
| 476 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 477 | |
| 478 | if (!WaitForArmGoal(0.05)) return; |
| 479 | Spit(); |
| 480 | |
| 481 | if (!splines[3].WaitForSplineDistanceRemaining(0.02)) return; |
| 482 | |
| 483 | AOS_LOG( |
| 484 | INFO, "Placed second cube %lf s\n", |
| 485 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
Austin Schuh | 8c9d2d0 | 2023-04-09 20:05:42 -0700 | [diff] [blame] | 486 | |
| 487 | std::this_thread::sleep_for(chrono::milliseconds(200)); |
| 488 | Neutral(); |
Austin Schuh | a7c3bc6 | 2023-04-16 19:46:23 -0700 | [diff] [blame] | 489 | AOS_LOG( |
| 490 | INFO, "Going to neutral %lf s\n", |
| 491 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
| 492 | |
| 493 | if (!WaitForArmGoal(0.05)) return; |
| 494 | AOS_LOG( |
| 495 | INFO, "Done at neutral %lf s\n", |
| 496 | aos::time::DurationInSeconds(aos::monotonic_clock::now() - start_time)); |
Maxwell Henderson | 2537883 | 2023-04-07 14:37:41 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 499 | void AutonomousActor::SendSuperstructureGoal() { |
| 500 | auto builder = superstructure_goal_sender_.MakeBuilder(); |
| 501 | |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 502 | flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal> |
| 503 | wrist_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 504 | *builder.fbb(), wrist_goal_, |
| 505 | CreateProfileParameters(*builder.fbb(), 12.0, 90.0)); |
| 506 | |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 507 | control_loops::superstructure::Goal::Builder superstructure_builder = |
| 508 | builder.MakeBuilder<control_loops::superstructure::Goal>(); |
| 509 | |
| 510 | superstructure_builder.add_arm_goal_position(arm_goal_position_); |
| 511 | superstructure_builder.add_preloaded_with_cone(preloaded_); |
| 512 | superstructure_builder.add_roller_goal(roller_goal_); |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 513 | superstructure_builder.add_wrist(wrist_offset); |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 514 | |
| 515 | if (builder.Send(superstructure_builder.Finish()) != |
| 516 | aos::RawSender::Error::kOk) { |
| 517 | AOS_LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | [[nodiscard]] bool AutonomousActor::WaitForPreloaded() { |
| 522 | set_preloaded(true); |
| 523 | SendSuperstructureGoal(); |
| 524 | |
| 525 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
| 526 | event_loop()->monotonic_now(), |
Stephan Pleines | 743f83a | 2024-02-02 18:32:09 -0800 | [diff] [blame] | 527 | aos::common::actions::kLoopOffset); |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 528 | |
| 529 | bool loaded = false; |
| 530 | while (!loaded) { |
| 531 | if (ShouldCancel()) { |
| 532 | return false; |
| 533 | } |
| 534 | |
| 535 | phased_loop.SleepUntilNext(); |
| 536 | superstructure_status_fetcher_.Fetch(); |
| 537 | CHECK(superstructure_status_fetcher_.get() != nullptr); |
| 538 | |
| 539 | loaded = (superstructure_status_fetcher_->end_effector_state() == |
| 540 | control_loops::superstructure::EndEffectorState::LOADED); |
| 541 | } |
| 542 | |
| 543 | set_preloaded(false); |
| 544 | SendSuperstructureGoal(); |
| 545 | |
| 546 | return true; |
| 547 | } |
| 548 | |
| 549 | void AutonomousActor::MidConeScore() { |
| 550 | set_arm_goal_position( |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 551 | control_loops::superstructure::arm::ScoreFrontMidConeUpAutoIndex()); |
| 552 | set_wrist_goal(0.0); |
| 553 | SendSuperstructureGoal(); |
| 554 | } |
| 555 | |
| 556 | void AutonomousActor::Neutral() { |
| 557 | set_arm_goal_position(control_loops::superstructure::arm::NeutralIndex()); |
Austin Schuh | a7c3bc6 | 2023-04-16 19:46:23 -0700 | [diff] [blame] | 558 | set_wrist_goal(1.0); |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 559 | SendSuperstructureGoal(); |
| 560 | } |
| 561 | |
| 562 | void AutonomousActor::Balance() { |
| 563 | set_arm_goal_position( |
Austin Schuh | 700bfff | 2023-04-05 19:45:55 -0700 | [diff] [blame] | 564 | control_loops::superstructure::arm::ScoreFrontLowCubeIndex()); |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 565 | set_wrist_goal(0.0); |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 566 | SendSuperstructureGoal(); |
| 567 | } |
| 568 | |
| 569 | void AutonomousActor::HighCubeScore() { |
| 570 | set_arm_goal_position( |
| 571 | control_loops::superstructure::arm::ScoreFrontHighCubeIndex()); |
Austin Schuh | a7c3bc6 | 2023-04-16 19:46:23 -0700 | [diff] [blame] | 572 | set_wrist_goal(1.0); |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 573 | SendSuperstructureGoal(); |
| 574 | } |
| 575 | |
| 576 | void AutonomousActor::MidCubeScore() { |
| 577 | set_arm_goal_position( |
| 578 | control_loops::superstructure::arm::ScoreFrontMidCubeIndex()); |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 579 | set_wrist_goal(1.0); |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 580 | SendSuperstructureGoal(); |
| 581 | } |
| 582 | |
| 583 | void AutonomousActor::PickupCube() { |
| 584 | set_arm_goal_position( |
| 585 | control_loops::superstructure::arm::GroundPickupBackCubeIndex()); |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 586 | set_wrist_goal(1.0); |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 587 | SendSuperstructureGoal(); |
| 588 | } |
| 589 | |
| 590 | void AutonomousActor::Spit() { |
| 591 | set_roller_goal(control_loops::superstructure::RollerGoal::SPIT); |
| 592 | SendSuperstructureGoal(); |
| 593 | } |
| 594 | |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 595 | void AutonomousActor::StopSpitting() { |
| 596 | set_roller_goal(control_loops::superstructure::RollerGoal::IDLE); |
| 597 | SendSuperstructureGoal(); |
| 598 | } |
| 599 | |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 600 | void AutonomousActor::IntakeCube() { |
| 601 | set_roller_goal(control_loops::superstructure::RollerGoal::INTAKE_CUBE); |
| 602 | SendSuperstructureGoal(); |
| 603 | } |
| 604 | |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 605 | [[nodiscard]] bool AutonomousActor::WaitForArmGoal(double distance_to_go) { |
| 606 | constexpr double kEpsTheta = 0.10; |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 607 | |
| 608 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
| 609 | event_loop()->monotonic_now(), |
Stephan Pleines | 743f83a | 2024-02-02 18:32:09 -0800 | [diff] [blame] | 610 | aos::common::actions::kLoopOffset); |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 611 | |
| 612 | bool at_goal = false; |
| 613 | while (!at_goal) { |
| 614 | if (ShouldCancel()) { |
| 615 | return false; |
| 616 | } |
| 617 | |
| 618 | phased_loop.SleepUntilNext(); |
| 619 | superstructure_status_fetcher_.Fetch(); |
| 620 | CHECK(superstructure_status_fetcher_.get() != nullptr); |
| 621 | |
Maxwell Henderson | 3d0beaf | 2023-03-23 11:32:44 -0700 | [diff] [blame] | 622 | at_goal = (arm_goal_position_ == |
| 623 | superstructure_status_fetcher_->arm()->current_node() && |
| 624 | superstructure_status_fetcher_->arm()->path_distance_to_go() < |
| 625 | distance_to_go) && |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 626 | (std::abs(wrist_goal_ - |
| 627 | superstructure_status_fetcher_->wrist()->position()) < |
| 628 | kEpsTheta); |
| 629 | } |
| 630 | |
Maxwell Henderson | 64f3745 | 2023-03-11 13:39:21 -0800 | [diff] [blame] | 631 | return true; |
| 632 | } |
| 633 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 634 | } // namespace y2023::autonomous |