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