milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 1 | #include "y2022/actors/autonomous_actor.h" |
| 2 | |
| 3 | #include <chrono> |
| 4 | #include <cinttypes> |
| 5 | #include <cmath> |
| 6 | |
| 7 | #include "aos/logging/logging.h" |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 8 | #include "aos/network/team_number.h" |
| 9 | #include "aos/util/math.h" |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 10 | #include "frc971/control_loops/drivetrain/localizer_generated.h" |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 11 | #include "y2022/actors/auto_splines.h" |
| 12 | #include "y2022/constants.h" |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 13 | #include "y2022/control_loops/drivetrain/drivetrain_base.h" |
| 14 | |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 15 | DEFINE_bool(spline_auto, false, "If true, define a spline autonomous mode"); |
Austin Schuh | 9651c5a | 2022-04-17 19:12:42 -0700 | [diff] [blame] | 16 | DEFINE_bool(rapid_react, true, |
Milind Upadhyay | a779396 | 2022-03-11 19:39:36 -0800 | [diff] [blame] | 17 | "If true, run the main rapid react autonomous mode"); |
Austin Schuh | 9651c5a | 2022-04-17 19:12:42 -0700 | [diff] [blame] | 18 | DEFINE_bool(rapid_react_two, false, |
Henry Speiser | 5eed1de | 2022-04-07 21:52:10 -0700 | [diff] [blame] | 19 | "If true, run the two ball rapid react autonomous mode"); |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 20 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 21 | namespace y2022::actors { |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 22 | namespace { |
Henry Speiser | e23d4de | 2022-04-05 16:47:47 -0700 | [diff] [blame] | 23 | constexpr double kExtendIntakeGoal = -0.10; |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 24 | constexpr double kRetractIntakeGoal = 1.47; |
Austin Schuh | 42d7e5f | 2022-03-16 23:35:09 -0700 | [diff] [blame] | 25 | constexpr double kIntakeRollerVoltage = 12.0; |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 26 | constexpr double kRollerVoltage = 12.0; |
| 27 | constexpr double kCatapultReturnPosition = -0.908; |
| 28 | } // namespace |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 29 | |
| 30 | using ::aos::monotonic_clock; |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 31 | using frc971::CreateProfileParameters; |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 32 | using ::frc971::ProfileParametersT; |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 33 | using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal; |
| 34 | using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal; |
Nathan Leong | dd72800 | 2024-02-03 15:26:53 -0800 | [diff] [blame^] | 35 | using frc971::control_loops::catapult::CatapultGoal; |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 36 | using frc971::control_loops::drivetrain::LocalizerControl; |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 37 | |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 38 | namespace chrono = ::std::chrono; |
| 39 | |
| 40 | AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop) |
| 41 | : frc971::autonomous::BaseAutonomousActor( |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -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 | superstructure_goal_sender_( |
| 48 | event_loop->MakeSender<control_loops::superstructure::Goal>( |
| 49 | "/superstructure")), |
| 50 | superstructure_status_fetcher_( |
| 51 | event_loop->MakeFetcher<control_loops::superstructure::Status>( |
| 52 | "/superstructure")), |
| 53 | joystick_state_fetcher_( |
| 54 | event_loop->MakeFetcher<aos::JoystickState>("/aos")), |
| 55 | robot_state_fetcher_(event_loop->MakeFetcher<aos::RobotState>("/aos")), |
| 56 | auto_splines_() { |
| 57 | set_max_drivetrain_voltage(12.0); |
| 58 | replan_timer_ = event_loop->AddTimer([this]() { Replan(); }); |
| 59 | event_loop->OnRun([this, event_loop]() { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 60 | replan_timer_->Schedule(event_loop->monotonic_now()); |
| 61 | button_poll_->Schedule(event_loop->monotonic_now(), |
| 62 | chrono::milliseconds(50)); |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 63 | }); |
| 64 | |
| 65 | button_poll_ = event_loop->AddTimer([this]() { |
| 66 | const aos::monotonic_clock::time_point now = |
| 67 | this->event_loop()->context().monotonic_event_time; |
| 68 | if (robot_state_fetcher_.Fetch()) { |
| 69 | if (robot_state_fetcher_->user_button()) { |
| 70 | user_indicated_safe_to_reset_ = true; |
| 71 | MaybeSendStartingPosition(); |
| 72 | } |
| 73 | } |
| 74 | if (joystick_state_fetcher_.Fetch()) { |
| 75 | if (joystick_state_fetcher_->has_alliance() && |
| 76 | (joystick_state_fetcher_->alliance() != alliance_)) { |
| 77 | alliance_ = joystick_state_fetcher_->alliance(); |
| 78 | is_planned_ = false; |
| 79 | // Only kick the planning out by 2 seconds. If we end up enabled in that |
| 80 | // second, then we will kick it out further based on the code below. |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 81 | replan_timer_->Schedule(now + std::chrono::seconds(2)); |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 82 | } |
| 83 | if (joystick_state_fetcher_->enabled()) { |
| 84 | if (!is_planned_) { |
| 85 | // Only replan once we've been disabled for 5 seconds. |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 86 | replan_timer_->Schedule(now + std::chrono::seconds(5)); |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | } |
| 90 | }); |
| 91 | } |
| 92 | |
| 93 | void AutonomousActor::Replan() { |
| 94 | LOG(INFO) << "Alliance " << static_cast<int>(alliance_); |
| 95 | if (alliance_ == aos::Alliance::kInvalid) { |
| 96 | return; |
| 97 | } |
| 98 | sent_starting_position_ = false; |
| 99 | if (FLAGS_spline_auto) { |
| 100 | test_spline_ = |
| 101 | PlanSpline(std::bind(&AutonomousSplines::TestSpline, &auto_splines_, |
| 102 | std::placeholders::_1, alliance_), |
| 103 | SplineDirection::kForward); |
| 104 | |
| 105 | starting_position_ = test_spline_->starting_position(); |
Milind Upadhyay | a779396 | 2022-03-11 19:39:36 -0800 | [diff] [blame] | 106 | } else if (FLAGS_rapid_react) { |
| 107 | rapid_react_splines_ = { |
| 108 | PlanSpline(std::bind(&AutonomousSplines::Spline1, &auto_splines_, |
| 109 | std::placeholders::_1, alliance_), |
Henry Speiser | 09e8da9 | 2022-03-14 20:58:45 -0700 | [diff] [blame] | 110 | SplineDirection::kBackward), |
Milind Upadhyay | a779396 | 2022-03-11 19:39:36 -0800 | [diff] [blame] | 111 | PlanSpline(std::bind(&AutonomousSplines::Spline2, &auto_splines_, |
| 112 | std::placeholders::_1, alliance_), |
Henry Speiser | e23d4de | 2022-04-05 16:47:47 -0700 | [diff] [blame] | 113 | SplineDirection::kBackward), |
Milind Upadhyay | a779396 | 2022-03-11 19:39:36 -0800 | [diff] [blame] | 114 | PlanSpline(std::bind(&AutonomousSplines::Spline3, &auto_splines_, |
| 115 | std::placeholders::_1, alliance_), |
Henry Speiser | e23d4de | 2022-04-05 16:47:47 -0700 | [diff] [blame] | 116 | SplineDirection::kForward)}; |
Milind Upadhyay | a779396 | 2022-03-11 19:39:36 -0800 | [diff] [blame] | 117 | starting_position_ = rapid_react_splines_.value()[0].starting_position(); |
| 118 | CHECK(starting_position_); |
Henry Speiser | 5eed1de | 2022-04-07 21:52:10 -0700 | [diff] [blame] | 119 | } else if (FLAGS_rapid_react_two) { |
| 120 | rapid_react_two_spline_ = { |
Austin Schuh | f3413b7 | 2022-04-16 19:09:33 -0700 | [diff] [blame] | 121 | PlanSpline(std::bind(&AutonomousSplines::SplineTwoBall1, &auto_splines_, |
Henry Speiser | 5eed1de | 2022-04-07 21:52:10 -0700 | [diff] [blame] | 122 | std::placeholders::_1, alliance_), |
Austin Schuh | f3413b7 | 2022-04-16 19:09:33 -0700 | [diff] [blame] | 123 | SplineDirection::kBackward), |
| 124 | PlanSpline(std::bind(&AutonomousSplines::SplineTwoBall2, &auto_splines_, |
| 125 | std::placeholders::_1, alliance_), |
| 126 | SplineDirection::kForward)}; |
Henry Speiser | 5eed1de | 2022-04-07 21:52:10 -0700 | [diff] [blame] | 127 | starting_position_ = rapid_react_two_spline_.value()[0].starting_position(); |
| 128 | CHECK(starting_position_); |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | is_planned_ = true; |
| 132 | |
| 133 | MaybeSendStartingPosition(); |
| 134 | } |
| 135 | |
| 136 | void AutonomousActor::MaybeSendStartingPosition() { |
| 137 | if (is_planned_ && user_indicated_safe_to_reset_ && |
| 138 | !sent_starting_position_) { |
| 139 | CHECK(starting_position_); |
| 140 | SendStartingPosition(starting_position_.value()); |
| 141 | } |
| 142 | } |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 143 | |
| 144 | void AutonomousActor::Reset() { |
| 145 | InitializeEncoders(); |
| 146 | ResetDrivetrain(); |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 147 | RetractFrontIntake(); |
| 148 | RetractBackIntake(); |
| 149 | |
| 150 | joystick_state_fetcher_.Fetch(); |
| 151 | CHECK(joystick_state_fetcher_.get() != nullptr) |
| 152 | << "Expect at least one JoystickState message before running auto..."; |
| 153 | alliance_ = joystick_state_fetcher_->alliance(); |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | bool AutonomousActor::RunAction( |
| 157 | const ::frc971::autonomous::AutonomousActionParams *params) { |
| 158 | Reset(); |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 159 | if (!user_indicated_safe_to_reset_) { |
| 160 | AOS_LOG(WARNING, "Didn't send starting position prior to starting auto."); |
| 161 | CHECK(starting_position_); |
| 162 | SendStartingPosition(starting_position_.value()); |
| 163 | } |
| 164 | // Clear this so that we don't accidentally resend things as soon as we replan |
| 165 | // later. |
| 166 | user_indicated_safe_to_reset_ = false; |
| 167 | is_planned_ = false; |
| 168 | starting_position_.reset(); |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 169 | |
| 170 | AOS_LOG(INFO, "Params are %d\n", params->mode()); |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 171 | if (alliance_ == aos::Alliance::kInvalid) { |
| 172 | AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection."); |
| 173 | return false; |
| 174 | } |
| 175 | if (FLAGS_spline_auto) { |
| 176 | SplineAuto(); |
Milind Upadhyay | a779396 | 2022-03-11 19:39:36 -0800 | [diff] [blame] | 177 | } else if (FLAGS_rapid_react) { |
| 178 | RapidReact(); |
Henry Speiser | 5eed1de | 2022-04-07 21:52:10 -0700 | [diff] [blame] | 179 | } else if (FLAGS_rapid_react_two) { |
| 180 | RapidReactTwo(); |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 181 | } |
| 182 | |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 183 | return true; |
| 184 | } |
| 185 | |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 186 | void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) { |
| 187 | // Set up the starting position for the blue alliance. |
| 188 | |
| 189 | // TODO(james): Resetting the localizer breaks the left/right statespace |
| 190 | // controller. That is a bug, but we can fix that later by not resetting. |
| 191 | auto builder = localizer_control_sender_.MakeBuilder(); |
| 192 | |
| 193 | LocalizerControl::Builder localizer_control_builder = |
| 194 | builder.MakeBuilder<LocalizerControl>(); |
| 195 | localizer_control_builder.add_x(start(0)); |
| 196 | localizer_control_builder.add_y(start(1)); |
| 197 | localizer_control_builder.add_theta(start(2)); |
| 198 | localizer_control_builder.add_theta_uncertainty(0.00001); |
| 199 | LOG(INFO) << "User button pressed, x: " << start(0) << " y: " << start(1) |
| 200 | << " theta: " << start(2); |
| 201 | if (builder.Send(localizer_control_builder.Finish()) != |
| 202 | aos::RawSender::Error::kOk) { |
| 203 | AOS_LOG(ERROR, "Failed to reset localizer.\n"); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | void AutonomousActor::SplineAuto() { |
| 208 | CHECK(test_spline_); |
| 209 | |
| 210 | if (!test_spline_->WaitForPlan()) return; |
| 211 | test_spline_->Start(); |
| 212 | |
| 213 | if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return; |
| 214 | } |
| 215 | |
Milind Upadhyay | a779396 | 2022-03-11 19:39:36 -0800 | [diff] [blame] | 216 | void AutonomousActor::RapidReact() { |
| 217 | aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now(); |
| 218 | |
| 219 | CHECK(rapid_react_splines_); |
| 220 | |
| 221 | auto &splines = *rapid_react_splines_; |
| 222 | |
| 223 | // Tell the superstructure a ball was preloaded |
Milind Upadhyay | a779396 | 2022-03-11 19:39:36 -0800 | [diff] [blame] | 224 | if (!WaitForPreloaded()) return; |
Henry Speiser | 09e8da9 | 2022-03-14 20:58:45 -0700 | [diff] [blame] | 225 | |
Henry Speiser | e23d4de | 2022-04-05 16:47:47 -0700 | [diff] [blame] | 226 | // Fire preloaded ball while driving |
Henry Speiser | 09e8da9 | 2022-03-14 20:58:45 -0700 | [diff] [blame] | 227 | set_fire_at_will(true); |
| 228 | SendSuperstructureGoal(); |
Milind Upadhyay | f35bb11 | 2022-03-16 23:08:04 -0700 | [diff] [blame] | 229 | if (!WaitForBallsShot()) return; |
Austin Schuh | 42d7e5f | 2022-03-16 23:35:09 -0700 | [diff] [blame] | 230 | LOG(INFO) << "Shot first ball " |
| 231 | << chrono::duration<double>(aos::monotonic_clock::now() - |
| 232 | start_time) |
| 233 | .count() |
| 234 | << 's'; |
Henry Speiser | 09e8da9 | 2022-03-14 20:58:45 -0700 | [diff] [blame] | 235 | set_fire_at_will(false); |
| 236 | SendSuperstructureGoal(); |
| 237 | |
Henry Speiser | e23d4de | 2022-04-05 16:47:47 -0700 | [diff] [blame] | 238 | // Drive and intake the ball nearest to the starting zone. |
| 239 | // Fire while moving. |
Henry Speiser | 09e8da9 | 2022-03-14 20:58:45 -0700 | [diff] [blame] | 240 | ExtendBackIntake(); |
Milind Upadhyay | a779396 | 2022-03-11 19:39:36 -0800 | [diff] [blame] | 241 | if (!splines[0].WaitForPlan()) return; |
| 242 | splines[0].Start(); |
Henry Speiser | e23d4de | 2022-04-05 16:47:47 -0700 | [diff] [blame] | 243 | // Distance before we don't shoot while moving. |
Austin Schuh | 9651c5a | 2022-04-17 19:12:42 -0700 | [diff] [blame] | 244 | if (!splines[0].WaitForSplineDistanceRemaining(2.1)) return; |
| 245 | LOG(INFO) << "Tring to take the shot"; |
Milind Upadhyay | a779396 | 2022-03-11 19:39:36 -0800 | [diff] [blame] | 246 | |
Milind Upadhyay | a779396 | 2022-03-11 19:39:36 -0800 | [diff] [blame] | 247 | set_fire_at_will(true); |
| 248 | SendSuperstructureGoal(); |
Henry Speiser | e23d4de | 2022-04-05 16:47:47 -0700 | [diff] [blame] | 249 | |
| 250 | if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return; |
| 251 | |
Austin Schuh | 9651c5a | 2022-04-17 19:12:42 -0700 | [diff] [blame] | 252 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
Henry Speiser | e23d4de | 2022-04-05 16:47:47 -0700 | [diff] [blame] | 253 | |
| 254 | // Fire the last ball we picked up when stopped. |
| 255 | SendSuperstructureGoal(); |
| 256 | LOG(INFO) << "Close"; |
Milind Upadhyay | f35bb11 | 2022-03-16 23:08:04 -0700 | [diff] [blame] | 257 | if (!WaitForBallsShot()) return; |
Austin Schuh | 42d7e5f | 2022-03-16 23:35:09 -0700 | [diff] [blame] | 258 | LOG(INFO) << "Shot first 3 balls " |
| 259 | << chrono::duration<double>(aos::monotonic_clock::now() - |
| 260 | start_time) |
| 261 | .count() |
| 262 | << 's'; |
Milind Upadhyay | a779396 | 2022-03-11 19:39:36 -0800 | [diff] [blame] | 263 | |
| 264 | // Drive to the human player station while intaking two balls. |
| 265 | // Once is already placed down, |
| 266 | // and one will be rolled to the robot by the human player |
Henry Speiser | 09e8da9 | 2022-03-14 20:58:45 -0700 | [diff] [blame] | 267 | if (!splines[1].WaitForPlan()) return; |
| 268 | splines[1].Start(); |
Henry Speiser | e23d4de | 2022-04-05 16:47:47 -0700 | [diff] [blame] | 269 | |
| 270 | std::this_thread::sleep_for(std::chrono::milliseconds(1500)); |
| 271 | |
| 272 | set_fire_at_will(false); |
| 273 | SendSuperstructureGoal(); |
| 274 | |
Henry Speiser | 09e8da9 | 2022-03-14 20:58:45 -0700 | [diff] [blame] | 275 | if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return; |
Henry Speiser | e23d4de | 2022-04-05 16:47:47 -0700 | [diff] [blame] | 276 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
Austin Schuh | 42d7e5f | 2022-03-16 23:35:09 -0700 | [diff] [blame] | 277 | LOG(INFO) << "At balls 4/5 " |
| 278 | << chrono::duration<double>(aos::monotonic_clock::now() - |
| 279 | start_time) |
| 280 | .count() |
| 281 | << 's'; |
Milind Upadhyay | a779396 | 2022-03-11 19:39:36 -0800 | [diff] [blame] | 282 | |
| 283 | // Drive to the shooting position |
Henry Speiser | 09e8da9 | 2022-03-14 20:58:45 -0700 | [diff] [blame] | 284 | if (!splines[2].WaitForPlan()) return; |
| 285 | splines[2].Start(); |
| 286 | if (!splines[2].WaitForSplineDistanceRemaining(2.00)) return; |
| 287 | RetractFrontIntake(); |
| 288 | |
| 289 | if (!splines[2].WaitForSplineDistanceRemaining(0.02)) return; |
Austin Schuh | 42d7e5f | 2022-03-16 23:35:09 -0700 | [diff] [blame] | 290 | LOG(INFO) << "Shooting last balls " |
| 291 | << chrono::duration<double>(aos::monotonic_clock::now() - |
| 292 | start_time) |
| 293 | .count() |
| 294 | << 's'; |
Milind Upadhyay | a779396 | 2022-03-11 19:39:36 -0800 | [diff] [blame] | 295 | |
| 296 | // Fire the two balls once we stopped |
| 297 | set_fire_at_will(true); |
| 298 | SendSuperstructureGoal(); |
Milind Upadhyay | f35bb11 | 2022-03-16 23:08:04 -0700 | [diff] [blame] | 299 | if (!WaitForBallsShot()) return; |
Milind Upadhyay | a779396 | 2022-03-11 19:39:36 -0800 | [diff] [blame] | 300 | |
| 301 | LOG(INFO) << "Took " |
| 302 | << chrono::duration<double>(aos::monotonic_clock::now() - |
| 303 | start_time) |
| 304 | .count() |
| 305 | << 's'; |
Austin Schuh | 9651c5a | 2022-04-17 19:12:42 -0700 | [diff] [blame] | 306 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 307 | set_fire_at_will(false); |
| 308 | SendSuperstructureGoal(); |
Milind Upadhyay | a779396 | 2022-03-11 19:39:36 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Henry Speiser | 5eed1de | 2022-04-07 21:52:10 -0700 | [diff] [blame] | 311 | // Rapid React Two Ball Autonomous. |
| 312 | void AutonomousActor::RapidReactTwo() { |
| 313 | aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now(); |
| 314 | |
| 315 | CHECK(rapid_react_two_spline_); |
| 316 | |
| 317 | auto &splines = *rapid_react_two_spline_; |
| 318 | |
| 319 | // Tell the superstructure a ball was preloaded |
| 320 | if (!WaitForPreloaded()) return; |
| 321 | set_fire_at_will(true); |
| 322 | SendSuperstructureGoal(); |
| 323 | if (!WaitForBallsShot()) return; |
| 324 | LOG(INFO) << "Shot first ball " |
| 325 | << chrono::duration<double>(aos::monotonic_clock::now() - |
| 326 | start_time) |
| 327 | .count() |
| 328 | << 's'; |
| 329 | set_fire_at_will(false); |
| 330 | SendSuperstructureGoal(); |
| 331 | |
| 332 | ExtendBackIntake(); |
| 333 | if (!splines[0].WaitForPlan()) return; |
| 334 | splines[0].Start(); |
| 335 | if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return; |
| 336 | |
Austin Schuh | f3413b7 | 2022-04-16 19:09:33 -0700 | [diff] [blame] | 337 | std::this_thread::sleep_for(std::chrono::milliseconds(300)); |
| 338 | |
| 339 | if (!splines[1].WaitForPlan()) return; |
| 340 | splines[1].Start(); |
| 341 | if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return; |
| 342 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 343 | |
Henry Speiser | 5eed1de | 2022-04-07 21:52:10 -0700 | [diff] [blame] | 344 | // Fire the ball once we stopped |
Henry Speiser | 5eed1de | 2022-04-07 21:52:10 -0700 | [diff] [blame] | 345 | set_fire_at_will(true); |
| 346 | SendSuperstructureGoal(); |
| 347 | if (!WaitForBallsShot()) return; |
| 348 | LOG(INFO) << "Shot last ball " |
| 349 | << chrono::duration<double>(aos::monotonic_clock::now() - |
| 350 | start_time) |
| 351 | .count() |
| 352 | << 's'; |
| 353 | set_fire_at_will(false); |
Austin Schuh | f3413b7 | 2022-04-16 19:09:33 -0700 | [diff] [blame] | 354 | RetractBackIntake(); |
Henry Speiser | 5eed1de | 2022-04-07 21:52:10 -0700 | [diff] [blame] | 355 | SendSuperstructureGoal(); |
| 356 | } |
| 357 | |
Milind Upadhyay | a779396 | 2022-03-11 19:39:36 -0800 | [diff] [blame] | 358 | [[nodiscard]] bool AutonomousActor::WaitForPreloaded() { |
Milind Upadhyay | 803bbf0 | 2022-03-11 17:56:26 -0800 | [diff] [blame] | 359 | set_preloaded(true); |
| 360 | SendSuperstructureGoal(); |
| 361 | |
| 362 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
| 363 | event_loop()->monotonic_now(), |
| 364 | ActorBase::kLoopOffset); |
| 365 | |
| 366 | bool loaded = false; |
| 367 | while (!loaded) { |
| 368 | if (ShouldCancel()) { |
| 369 | return false; |
| 370 | } |
| 371 | |
| 372 | phased_loop.SleepUntilNext(); |
| 373 | superstructure_status_fetcher_.Fetch(); |
| 374 | CHECK(superstructure_status_fetcher_.get() != nullptr); |
| 375 | |
| 376 | loaded = (superstructure_status_fetcher_->state() == |
| 377 | control_loops::superstructure::SuperstructureState::LOADED); |
| 378 | } |
| 379 | |
| 380 | set_preloaded(false); |
| 381 | SendSuperstructureGoal(); |
| 382 | |
| 383 | return true; |
| 384 | } |
| 385 | |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 386 | void AutonomousActor::SendSuperstructureGoal() { |
| 387 | auto builder = superstructure_goal_sender_.MakeBuilder(); |
| 388 | |
| 389 | flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal> |
| 390 | intake_front_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 391 | *builder.fbb(), intake_front_goal_, |
| 392 | CreateProfileParameters(*builder.fbb(), 20.0, 60.0)); |
| 393 | |
| 394 | flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal> |
| 395 | intake_back_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 396 | *builder.fbb(), intake_back_goal_, |
| 397 | CreateProfileParameters(*builder.fbb(), 20.0, 60.0)); |
| 398 | |
| 399 | flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal> |
| 400 | catapult_return_position_offset = |
| 401 | CreateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 402 | *builder.fbb(), kCatapultReturnPosition, |
| 403 | CreateProfileParameters(*builder.fbb(), 9.0, 50.0)); |
| 404 | |
Nathan Leong | dd72800 | 2024-02-03 15:26:53 -0800 | [diff] [blame^] | 405 | CatapultGoal::Builder catapult_goal_builder(*builder.fbb()); |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 406 | catapult_goal_builder.add_shot_position(0.03); |
| 407 | catapult_goal_builder.add_shot_velocity(18.0); |
| 408 | catapult_goal_builder.add_return_position(catapult_return_position_offset); |
Nathan Leong | dd72800 | 2024-02-03 15:26:53 -0800 | [diff] [blame^] | 409 | flatbuffers::Offset<CatapultGoal> catapult_goal_offset = |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 410 | catapult_goal_builder.Finish(); |
| 411 | |
| 412 | superstructure::Goal::Builder superstructure_builder = |
| 413 | builder.MakeBuilder<superstructure::Goal>(); |
| 414 | |
| 415 | superstructure_builder.add_intake_front(intake_front_offset); |
| 416 | superstructure_builder.add_intake_back(intake_back_offset); |
Austin Schuh | 42d7e5f | 2022-03-16 23:35:09 -0700 | [diff] [blame] | 417 | superstructure_builder.add_roller_speed_compensation(0.0); |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 418 | superstructure_builder.add_roller_speed_front(roller_front_voltage_); |
| 419 | superstructure_builder.add_roller_speed_back(roller_back_voltage_); |
Milind Upadhyay | d86b02c | 2022-03-13 20:57:46 -0700 | [diff] [blame] | 420 | if (requested_intake_.has_value()) { |
| 421 | superstructure_builder.add_turret_intake(*requested_intake_); |
| 422 | } |
Milind Upadhyay | 29dcc17 | 2022-04-02 19:21:30 -0700 | [diff] [blame] | 423 | superstructure_builder.add_transfer_roller_speed(transfer_roller_voltage_); |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 424 | superstructure_builder.add_catapult(catapult_goal_offset); |
| 425 | superstructure_builder.add_fire(fire_); |
Milind Upadhyay | 803bbf0 | 2022-03-11 17:56:26 -0800 | [diff] [blame] | 426 | superstructure_builder.add_preloaded(preloaded_); |
Austin Schuh | 42d7e5f | 2022-03-16 23:35:09 -0700 | [diff] [blame] | 427 | superstructure_builder.add_auto_aim(true); |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 428 | |
| 429 | if (builder.Send(superstructure_builder.Finish()) != |
| 430 | aos::RawSender::Error::kOk) { |
| 431 | AOS_LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | void AutonomousActor::ExtendFrontIntake() { |
Milind Upadhyay | d86b02c | 2022-03-13 20:57:46 -0700 | [diff] [blame] | 436 | set_requested_intake(RequestedIntake::kFront); |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 437 | set_intake_front_goal(kExtendIntakeGoal); |
Austin Schuh | e126437 | 2022-03-13 20:22:36 -0700 | [diff] [blame] | 438 | set_roller_front_voltage(kIntakeRollerVoltage); |
Milind Upadhyay | 29dcc17 | 2022-04-02 19:21:30 -0700 | [diff] [blame] | 439 | set_transfer_roller_voltage(kRollerVoltage); |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 440 | SendSuperstructureGoal(); |
| 441 | } |
| 442 | |
| 443 | void AutonomousActor::RetractFrontIntake() { |
Milind Upadhyay | d86b02c | 2022-03-13 20:57:46 -0700 | [diff] [blame] | 444 | set_requested_intake(std::nullopt); |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 445 | set_intake_front_goal(kRetractIntakeGoal); |
Austin Schuh | e126437 | 2022-03-13 20:22:36 -0700 | [diff] [blame] | 446 | set_roller_front_voltage(0.0); |
Milind Upadhyay | 29dcc17 | 2022-04-02 19:21:30 -0700 | [diff] [blame] | 447 | set_transfer_roller_voltage(0.0); |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 448 | SendSuperstructureGoal(); |
| 449 | } |
| 450 | |
| 451 | void AutonomousActor::ExtendBackIntake() { |
Milind Upadhyay | d86b02c | 2022-03-13 20:57:46 -0700 | [diff] [blame] | 452 | set_requested_intake(RequestedIntake::kBack); |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 453 | set_intake_back_goal(kExtendIntakeGoal); |
Austin Schuh | e126437 | 2022-03-13 20:22:36 -0700 | [diff] [blame] | 454 | set_roller_back_voltage(kIntakeRollerVoltage); |
Milind Upadhyay | 29dcc17 | 2022-04-02 19:21:30 -0700 | [diff] [blame] | 455 | set_transfer_roller_voltage(-kRollerVoltage); |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 456 | SendSuperstructureGoal(); |
| 457 | } |
| 458 | |
| 459 | void AutonomousActor::RetractBackIntake() { |
Milind Upadhyay | d86b02c | 2022-03-13 20:57:46 -0700 | [diff] [blame] | 460 | set_requested_intake(std::nullopt); |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 461 | set_intake_back_goal(kRetractIntakeGoal); |
Austin Schuh | e126437 | 2022-03-13 20:22:36 -0700 | [diff] [blame] | 462 | set_roller_back_voltage(0.0); |
Milind Upadhyay | 29dcc17 | 2022-04-02 19:21:30 -0700 | [diff] [blame] | 463 | set_transfer_roller_voltage(0.0); |
Ravago Jones | 81e5063 | 2022-03-11 16:23:51 -0800 | [diff] [blame] | 464 | SendSuperstructureGoal(); |
| 465 | } |
| 466 | |
Milind Upadhyay | f35bb11 | 2022-03-16 23:08:04 -0700 | [diff] [blame] | 467 | [[nodiscard]] bool AutonomousActor::WaitForBallsShot() { |
Austin Schuh | 42d7e5f | 2022-03-16 23:35:09 -0700 | [diff] [blame] | 468 | superstructure_status_fetcher_.Fetch(); |
| 469 | CHECK(superstructure_status_fetcher_.get()); |
Milind Upadhyay | f35bb11 | 2022-03-16 23:08:04 -0700 | [diff] [blame] | 470 | |
Ravago Jones | f699d1c | 2022-03-11 18:39:56 -0800 | [diff] [blame] | 471 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
| 472 | event_loop()->monotonic_now(), |
| 473 | ActorBase::kLoopOffset); |
| 474 | superstructure_status_fetcher_.Fetch(); |
| 475 | CHECK(superstructure_status_fetcher_.get() != nullptr); |
Henry Speiser | e23d4de | 2022-04-05 16:47:47 -0700 | [diff] [blame] | 476 | |
Ravago Jones | f699d1c | 2022-03-11 18:39:56 -0800 | [diff] [blame] | 477 | while (true) { |
| 478 | if (ShouldCancel()) { |
| 479 | return false; |
| 480 | } |
| 481 | phased_loop.SleepUntilNext(); |
| 482 | superstructure_status_fetcher_.Fetch(); |
| 483 | CHECK(superstructure_status_fetcher_.get() != nullptr); |
Henry Speiser | e23d4de | 2022-04-05 16:47:47 -0700 | [diff] [blame] | 484 | |
| 485 | if (!superstructure_status_fetcher_->front_intake_has_ball() && |
| 486 | !superstructure_status_fetcher_->back_intake_has_ball() && |
| 487 | superstructure_status_fetcher_->state() == |
| 488 | control_loops::superstructure::SuperstructureState::IDLE) { |
Ravago Jones | f699d1c | 2022-03-11 18:39:56 -0800 | [diff] [blame] | 489 | return true; |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 494 | } // namespace y2022::actors |