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