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