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