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