Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 1 | #include "y2020/actors/autonomous_actor.h" |
| 2 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 3 | #include <chrono> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 4 | #include <cinttypes> |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 5 | #include <cmath> |
| 6 | |
| 7 | #include "aos/logging/logging.h" |
Austin Schuh | 3653cf2 | 2021-11-12 11:54:51 -0800 | [diff] [blame] | 8 | #include "aos/network/team_number.h" |
James Kuszmaul | ddd2ba6 | 2020-03-08 22:17:13 -0700 | [diff] [blame] | 9 | #include "aos/util/math.h" |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 10 | #include "frc971/control_loops/drivetrain/localizer_generated.h" |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 11 | #include "y2020/actors/auto_splines.h" |
Austin Schuh | 3653cf2 | 2021-11-12 11:54:51 -0800 | [diff] [blame] | 12 | #include "y2020/constants.h" |
Ravago Jones | c2a0802 | 2021-02-06 17:40:54 -0800 | [diff] [blame] | 13 | #include "y2020/control_loops/drivetrain/drivetrain_base.h" |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 14 | |
Austin Schuh | 3fb9e42 | 2021-03-31 20:11:32 -0700 | [diff] [blame] | 15 | DEFINE_bool(spline_auto, false, "If true, define a spline autonomous mode"); |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 16 | DEFINE_bool(just_shoot, false, |
| 17 | "If true, run the autonomous that just shoots balls."); |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 18 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 19 | namespace y2020::actors { |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 20 | |
| 21 | using ::aos::monotonic_clock; |
| 22 | using ::frc971::ProfileParametersT; |
| 23 | using frc971::control_loops::drivetrain::LocalizerControl; |
| 24 | namespace chrono = ::std::chrono; |
| 25 | |
| 26 | AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop) |
| 27 | : frc971::autonomous::BaseAutonomousActor( |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 28 | event_loop, control_loops::drivetrain::GetDrivetrainConfig()), |
Ravago Jones | c2a0802 | 2021-02-06 17:40:54 -0800 | [diff] [blame] | 29 | localizer_control_sender_( |
| 30 | event_loop->MakeSender< |
| 31 | ::frc971::control_loops::drivetrain::LocalizerControl>( |
| 32 | "/drivetrain")), |
Austin Schuh | 67e127e | 2021-03-27 13:25:23 -0700 | [diff] [blame] | 33 | superstructure_goal_sender_( |
| 34 | event_loop->MakeSender<control_loops::superstructure::Goal>( |
| 35 | "/superstructure")), |
Ravago Jones | 1f32d62 | 2021-08-26 12:20:36 -0700 | [diff] [blame] | 36 | superstructure_status_fetcher_( |
| 37 | event_loop->MakeFetcher<y2020::control_loops::superstructure::Status>( |
| 38 | "/superstructure")), |
Austin Schuh | ba77b7e | 2021-10-25 21:58:54 -0700 | [diff] [blame] | 39 | joystick_state_fetcher_( |
| 40 | event_loop->MakeFetcher<aos::JoystickState>("/aos")), |
| 41 | robot_state_fetcher_(event_loop->MakeFetcher<aos::RobotState>("/aos")), |
Ravago Jones | c2a0802 | 2021-02-06 17:40:54 -0800 | [diff] [blame] | 42 | auto_splines_() { |
Austin Schuh | 3653cf2 | 2021-11-12 11:54:51 -0800 | [diff] [blame] | 43 | practice_robot_ = |
| 44 | ::aos::network::GetTeamNumber() == constants::Values::kPracticeTeamNumber; |
| 45 | |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame] | 46 | set_max_drivetrain_voltage(12.0); |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 47 | replan_timer_ = event_loop->AddTimer([this]() { Replan(); }); |
| 48 | event_loop->OnRun([this, event_loop]() { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 49 | replan_timer_->Schedule(event_loop->monotonic_now()); |
| 50 | button_poll_->Schedule(event_loop->monotonic_now(), |
| 51 | chrono::milliseconds(50)); |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 52 | }); |
Austin Schuh | ba77b7e | 2021-10-25 21:58:54 -0700 | [diff] [blame] | 53 | |
| 54 | button_poll_ = event_loop->AddTimer([this]() { |
James Kuszmaul | ead4da6 | 2021-10-24 17:36:54 -0700 | [diff] [blame] | 55 | const aos::monotonic_clock::time_point now = |
| 56 | this->event_loop()->context().monotonic_event_time; |
Austin Schuh | ba77b7e | 2021-10-25 21:58:54 -0700 | [diff] [blame] | 57 | if (robot_state_fetcher_.Fetch()) { |
| 58 | if (robot_state_fetcher_->user_button()) { |
| 59 | user_indicated_safe_to_reset_ = true; |
| 60 | MaybeSendStartingPosition(); |
| 61 | } |
James Kuszmaul | 4303a5d | 2021-10-23 19:58:48 -0700 | [diff] [blame] | 62 | } |
Austin Schuh | ba77b7e | 2021-10-25 21:58:54 -0700 | [diff] [blame] | 63 | if (joystick_state_fetcher_.Fetch()) { |
| 64 | if (joystick_state_fetcher_->has_alliance() && |
| 65 | (joystick_state_fetcher_->alliance() != alliance_)) { |
| 66 | alliance_ = joystick_state_fetcher_->alliance(); |
James Kuszmaul | ead4da6 | 2021-10-24 17:36:54 -0700 | [diff] [blame] | 67 | is_planned_ = false; |
| 68 | // Only kick the planning out by 2 seconds. If we end up enabled in that |
| 69 | // 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] | 70 | replan_timer_->Schedule(now + std::chrono::seconds(2)); |
James Kuszmaul | ead4da6 | 2021-10-24 17:36:54 -0700 | [diff] [blame] | 71 | } |
| 72 | if (joystick_state_fetcher_->enabled()) { |
| 73 | if (!is_planned_) { |
| 74 | // Only replan once we've been disabled for 5 seconds. |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 75 | replan_timer_->Schedule(now + std::chrono::seconds(5)); |
James Kuszmaul | ead4da6 | 2021-10-24 17:36:54 -0700 | [diff] [blame] | 76 | } |
Austin Schuh | ba77b7e | 2021-10-25 21:58:54 -0700 | [diff] [blame] | 77 | } |
James Kuszmaul | 4303a5d | 2021-10-23 19:58:48 -0700 | [diff] [blame] | 78 | } |
| 79 | }); |
| 80 | } |
| 81 | |
| 82 | void AutonomousActor::MaybeSendStartingPosition() { |
James Kuszmaul | ead4da6 | 2021-10-24 17:36:54 -0700 | [diff] [blame] | 83 | if (is_planned_ && user_indicated_safe_to_reset_ && |
| 84 | !sent_starting_position_) { |
James Kuszmaul | 4303a5d | 2021-10-23 19:58:48 -0700 | [diff] [blame] | 85 | CHECK(starting_position_); |
| 86 | SendStartingPosition(starting_position_.value()); |
| 87 | } |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void AutonomousActor::Replan() { |
milind-u | 0e20378 | 2021-10-30 21:57:20 -0700 | [diff] [blame] | 91 | LOG(INFO) << "Alliance " << static_cast<int>(alliance_); |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame] | 92 | if (alliance_ == aos::Alliance::kInvalid) { |
| 93 | return; |
| 94 | } |
milind-u | 0e20378 | 2021-10-30 21:57:20 -0700 | [diff] [blame] | 95 | sent_starting_position_ = false; |
Ravago Jones | 8c5737e | 2021-10-16 15:36:12 -0700 | [diff] [blame] | 96 | if (FLAGS_spline_auto) { |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 97 | test_spline_ = PlanSpline(std::bind(&AutonomousSplines::TestSpline, |
| 98 | &auto_splines_, std::placeholders::_1), |
| 99 | SplineDirection::kForward); |
James Kuszmaul | 4303a5d | 2021-10-23 19:58:48 -0700 | [diff] [blame] | 100 | starting_position_ = test_spline_->starting_position(); |
James Kuszmaul | 4303a5d | 2021-10-23 19:58:48 -0700 | [diff] [blame] | 101 | } else { |
Austin Schuh | 3653cf2 | 2021-11-12 11:54:51 -0800 | [diff] [blame] | 102 | if (practice_robot_) { |
| 103 | fender_splines_ = {PlanSpline( |
| 104 | std::bind(&AutonomousSplines::FarSideFender, &auto_splines_, |
| 105 | std::placeholders::_1, alliance_), |
| 106 | SplineDirection::kForward)}; |
| 107 | starting_position_ = fender_splines_.value()[0].starting_position(); |
| 108 | CHECK(starting_position_); |
| 109 | } else { |
| 110 | target_aligned_splines_ = { |
| 111 | PlanSpline( |
| 112 | std::bind(&AutonomousSplines::TargetAligned1, &auto_splines_, |
| 113 | std::placeholders::_1, alliance_), |
| 114 | SplineDirection::kForward), |
| 115 | PlanSpline( |
| 116 | std::bind(&AutonomousSplines::TargetAligned2, &auto_splines_, |
| 117 | std::placeholders::_1, alliance_), |
| 118 | SplineDirection::kBackward), |
| 119 | PlanSpline( |
| 120 | std::bind(&AutonomousSplines::TargetAligned3, &auto_splines_, |
| 121 | std::placeholders::_1, alliance_), |
| 122 | SplineDirection::kForward)}; |
| 123 | starting_position_ = |
| 124 | target_aligned_splines_.value()[0].starting_position(); |
| 125 | CHECK(starting_position_); |
| 126 | } |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 127 | } |
James Kuszmaul | ead4da6 | 2021-10-24 17:36:54 -0700 | [diff] [blame] | 128 | |
| 129 | is_planned_ = true; |
| 130 | |
James Kuszmaul | 4303a5d | 2021-10-23 19:58:48 -0700 | [diff] [blame] | 131 | MaybeSendStartingPosition(); |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 132 | } |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 133 | |
| 134 | void AutonomousActor::Reset() { |
| 135 | InitializeEncoders(); |
| 136 | ResetDrivetrain(); |
milind upadhyay | b2e840a | 2021-03-27 13:54:49 -0700 | [diff] [blame] | 137 | RetractIntake(); |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 138 | |
James Kuszmaul | ddd2ba6 | 2020-03-08 22:17:13 -0700 | [diff] [blame] | 139 | joystick_state_fetcher_.Fetch(); |
| 140 | CHECK(joystick_state_fetcher_.get() != nullptr) |
| 141 | << "Expect at least one JoystickState message before running auto..."; |
| 142 | alliance_ = joystick_state_fetcher_->alliance(); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | bool AutonomousActor::RunAction( |
Austin Schuh | 6fb0a6d | 2021-01-23 15:43:17 -0800 | [diff] [blame] | 146 | const ::frc971::autonomous::AutonomousActionParams *params) { |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 147 | Reset(); |
James Kuszmaul | 4303a5d | 2021-10-23 19:58:48 -0700 | [diff] [blame] | 148 | if (!user_indicated_safe_to_reset_) { |
| 149 | AOS_LOG(WARNING, "Didn't send starting position prior to starting auto."); |
milind-u | 0e20378 | 2021-10-30 21:57:20 -0700 | [diff] [blame] | 150 | CHECK(starting_position_); |
James Kuszmaul | 4303a5d | 2021-10-23 19:58:48 -0700 | [diff] [blame] | 151 | SendStartingPosition(starting_position_.value()); |
| 152 | } |
James Kuszmaul | ead4da6 | 2021-10-24 17:36:54 -0700 | [diff] [blame] | 153 | // Clear this so that we don't accidentally resend things as soon as we replan |
| 154 | // later. |
| 155 | user_indicated_safe_to_reset_ = false; |
| 156 | is_planned_ = false; |
| 157 | starting_position_.reset(); |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 158 | |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 159 | AOS_LOG(INFO, "Params are %d\n", params->mode()); |
James Kuszmaul | ddd2ba6 | 2020-03-08 22:17:13 -0700 | [diff] [blame] | 160 | if (alliance_ == aos::Alliance::kInvalid) { |
| 161 | AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection."); |
| 162 | return false; |
| 163 | } |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame] | 164 | if (FLAGS_spline_auto) { |
| 165 | SplineAuto(); |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 166 | } else { |
Austin Schuh | 3653cf2 | 2021-11-12 11:54:51 -0800 | [diff] [blame] | 167 | if (practice_robot_) { |
| 168 | Fender(); |
| 169 | } else { |
| 170 | TargetAligned(); |
| 171 | } |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 172 | } |
Austin Schuh | 3653cf2 | 2021-11-12 11:54:51 -0800 | [diff] [blame] | 173 | |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 174 | return true; |
| 175 | } |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 176 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 177 | void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) { |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 178 | // Set up the starting position for the blue alliance. |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 179 | |
| 180 | // TODO(james): Resetting the localizer breaks the left/right statespace |
| 181 | // controller. That is a bug, but we can fix that later by not resetting. |
| 182 | auto builder = localizer_control_sender_.MakeBuilder(); |
| 183 | |
| 184 | LocalizerControl::Builder localizer_control_builder = |
| 185 | builder.MakeBuilder<LocalizerControl>(); |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 186 | localizer_control_builder.add_x(start(0)); |
| 187 | localizer_control_builder.add_y(start(1)); |
| 188 | localizer_control_builder.add_theta(start(2)); |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 189 | localizer_control_builder.add_theta_uncertainty(0.00001); |
milind-u | 0e20378 | 2021-10-30 21:57:20 -0700 | [diff] [blame] | 190 | LOG(INFO) << "User button pressed, x: " << start(0) << " y: " << start(1) |
| 191 | << " theta: " << start(2); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 192 | if (builder.Send(localizer_control_builder.Finish()) != |
| 193 | aos::RawSender::Error::kOk) { |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 194 | AOS_LOG(ERROR, "Failed to reset localizer.\n"); |
| 195 | } |
| 196 | } |
| 197 | |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 198 | void AutonomousActor::TargetAligned() { |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame] | 199 | aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now(); |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 200 | CHECK(target_aligned_splines_); |
| 201 | auto &splines = *target_aligned_splines_; |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 202 | |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame] | 203 | // Spin up. |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 204 | set_shooting(true); |
milind-u | 0e20378 | 2021-10-30 21:57:20 -0700 | [diff] [blame] | 205 | set_preloading(true); |
| 206 | set_shooter_tracking(true); |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 207 | SendSuperstructureGoal(); |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 208 | if (!WaitForBallsShot(3)) return; |
milind-u | 0e20378 | 2021-10-30 21:57:20 -0700 | [diff] [blame] | 209 | LOG(INFO) << "Shot balls"; |
| 210 | set_shooter_tracking(false); |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 211 | |
milind-u | 0e20378 | 2021-10-30 21:57:20 -0700 | [diff] [blame] | 212 | // Drive and intake 3 balls in front of the trench run |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 213 | set_shooting(false); |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame] | 214 | ExtendIntake(); |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 215 | SendSuperstructureGoal(); |
| 216 | |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 217 | if (!splines[0].WaitForPlan()) return; |
| 218 | splines[0].Start(); |
| 219 | |
| 220 | if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return; |
milind-u | 0e20378 | 2021-10-30 21:57:20 -0700 | [diff] [blame] | 221 | |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame] | 222 | std::this_thread::sleep_for(chrono::milliseconds(200)); |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 223 | RetractIntake(); |
| 224 | |
milind-u | 0e20378 | 2021-10-30 21:57:20 -0700 | [diff] [blame] | 225 | // Drive back to shooting position |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 226 | if (!splines[1].WaitForPlan()) return; |
| 227 | splines[1].Start(); |
| 228 | |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame] | 229 | if (!splines[1].WaitForSplineDistanceRemaining(2.0)) return; |
| 230 | // Reverse the rollers for a moment to try to unjam any jammed balls. Since |
| 231 | // we are moving here, this is free to try. |
| 232 | set_roller_voltage(-12.0); |
| 233 | std::this_thread::sleep_for(chrono::milliseconds(300)); |
| 234 | set_roller_voltage(0.0); |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 235 | |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame] | 236 | // Once we come to a stop, give the robot a moment to settle down. This makes |
| 237 | // the shot more accurate. |
| 238 | if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return; |
milind-u | 0e20378 | 2021-10-30 21:57:20 -0700 | [diff] [blame] | 239 | set_shooter_tracking(true); |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame] | 240 | std::this_thread::sleep_for(chrono::milliseconds(1500)); |
| 241 | set_shooting(true); |
| 242 | const int balls = Balls(); |
| 243 | |
| 244 | SendSuperstructureGoal(); |
| 245 | |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 246 | SendSuperstructureGoal(); |
| 247 | |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame] | 248 | if (!WaitUntilAbsoluteBallsShot(3 + balls)) return; |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 249 | |
milind-u | 0e20378 | 2021-10-30 21:57:20 -0700 | [diff] [blame] | 250 | set_shooting(false); |
| 251 | set_roller_voltage(0.0); |
| 252 | set_shooter_tracking(false); |
| 253 | set_preloading(false); |
| 254 | SendSuperstructureGoal(); |
| 255 | |
| 256 | // Drive close to the rendezvous point in the center of the field so that the |
| 257 | // driver can intake balls there right after auto ends. |
| 258 | if (!splines[2].WaitForPlan()) return; |
| 259 | splines[2].Start(); |
| 260 | |
| 261 | if (!splines[2].WaitForSplineDistanceRemaining(0.02)) return; |
| 262 | |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame] | 263 | LOG(INFO) << "Took " |
| 264 | << chrono::duration<double>(aos::monotonic_clock::now() - |
| 265 | start_time) |
| 266 | .count(); |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Austin Schuh | 3653cf2 | 2021-11-12 11:54:51 -0800 | [diff] [blame] | 269 | void AutonomousActor::Fender() { |
| 270 | aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now(); |
| 271 | CHECK(fender_splines_); |
| 272 | auto &splines = *fender_splines_; |
| 273 | |
| 274 | // Spin up. |
| 275 | set_shooting(false); |
| 276 | set_preloading(true); |
| 277 | set_shooter_tracking(false); |
| 278 | SendSuperstructureGoal(); |
| 279 | |
| 280 | if (!splines[0].WaitForPlan()) return; |
| 281 | splines[0].Start(); |
| 282 | |
| 283 | if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return; |
| 284 | |
| 285 | SendSuperstructureGoal(); |
| 286 | std::this_thread::sleep_for(chrono::milliseconds(500)); |
| 287 | ApplyThrottle(0.2); |
| 288 | set_shooting(true); |
| 289 | SendSuperstructureGoal(); |
| 290 | LOG(INFO) << "Took " |
| 291 | << chrono::duration<double>(aos::monotonic_clock::now() - |
| 292 | start_time) |
| 293 | .count(); |
| 294 | } |
| 295 | |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 296 | void AutonomousActor::JustShoot() { |
| 297 | // shoot pre-loaded balls |
| 298 | set_shooter_tracking(true); |
| 299 | set_shooting(true); |
| 300 | SendSuperstructureGoal(); |
| 301 | |
| 302 | if (!WaitForBallsShot(3)) return; |
| 303 | |
| 304 | set_shooting(false); |
| 305 | set_shooter_tracking(true); |
| 306 | SendSuperstructureGoal(); |
| 307 | } |
| 308 | |
| 309 | void AutonomousActor::TargetOffset() { |
| 310 | CHECK(target_offset_splines_); |
| 311 | auto &splines = *target_offset_splines_; |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 312 | |
| 313 | // spin up shooter |
| 314 | set_shooter_tracking(true); |
| 315 | SendSuperstructureGoal(); |
| 316 | ExtendIntake(); |
| 317 | |
| 318 | // pickup 2 more balls in front of the trench run |
| 319 | if (!splines[0].WaitForPlan()) return; |
| 320 | splines[0].Start(); |
| 321 | |
| 322 | if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return; |
| 323 | RetractIntake(); |
| 324 | |
| 325 | if (!splines[1].WaitForPlan()) return; |
| 326 | splines[1].Start(); |
| 327 | |
| 328 | if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return; |
| 329 | |
| 330 | // shoot the balls from in front of the goal. |
| 331 | set_shooting(true); |
| 332 | SendSuperstructureGoal(); |
| 333 | |
| 334 | if (!WaitForBallsShot(5)) return; |
| 335 | |
| 336 | set_shooting(false); |
| 337 | set_shooter_tracking(false); |
| 338 | SendSuperstructureGoal(); |
| 339 | } |
| 340 | |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 341 | void AutonomousActor::SplineAuto() { |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 342 | CHECK(test_spline_); |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 343 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 344 | if (!test_spline_->WaitForPlan()) return; |
| 345 | test_spline_->Start(); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 346 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 347 | if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return; |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 348 | } |
| 349 | |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 350 | ProfileParametersT MakeProfileParametersT(const float max_velocity, |
| 351 | const float max_acceleration) { |
| 352 | ProfileParametersT params; |
| 353 | params.max_velocity = max_velocity; |
| 354 | params.max_acceleration = max_acceleration; |
| 355 | return params; |
| 356 | } |
| 357 | |
| 358 | bool AutonomousActor::DriveFwd() { |
Austin Schuh | fd1715f | 2021-01-30 16:58:24 -0800 | [diff] [blame] | 359 | const ProfileParametersT kDrive = MakeProfileParametersT(0.3f, 1.0f); |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 360 | const ProfileParametersT kTurn = MakeProfileParametersT(5.0f, 15.0f); |
Austin Schuh | fd1715f | 2021-01-30 16:58:24 -0800 | [diff] [blame] | 361 | StartDrive(1.0, 0.0, kDrive, kTurn); |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 362 | return WaitForDriveDone(); |
| 363 | } |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 364 | |
| 365 | void AutonomousActor::SendSuperstructureGoal() { |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 366 | auto builder = superstructure_goal_sender_.MakeBuilder(); |
| 367 | |
| 368 | flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal> |
| 369 | intake_offset; |
milind upadhyay | b9dec71 | 2021-03-20 15:47:51 -0700 | [diff] [blame] | 370 | |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 371 | { |
| 372 | StaticZeroingSingleDOFProfiledSubsystemGoal::Builder intake_builder = |
| 373 | builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>(); |
| 374 | |
milind upadhyay | b9dec71 | 2021-03-20 15:47:51 -0700 | [diff] [blame] | 375 | frc971::ProfileParameters::Builder profile_params_builder = |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 376 | builder.MakeBuilder<frc971::ProfileParameters>(); |
Austin Schuh | b39a21c | 2021-03-31 20:12:18 -0700 | [diff] [blame] | 377 | profile_params_builder.add_max_velocity(20.0); |
| 378 | profile_params_builder.add_max_acceleration(60.0); |
milind upadhyay | b9dec71 | 2021-03-20 15:47:51 -0700 | [diff] [blame] | 379 | flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset = |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 380 | profile_params_builder.Finish(); |
| 381 | intake_builder.add_unsafe_goal(intake_goal_); |
| 382 | intake_builder.add_profile_params(profile_params_offset); |
| 383 | intake_offset = intake_builder.Finish(); |
| 384 | } |
| 385 | |
Austin Schuh | 3653cf2 | 2021-11-12 11:54:51 -0800 | [diff] [blame] | 386 | flatbuffers::Offset<superstructure::ShooterGoal> shooter_offset = |
| 387 | superstructure::CreateShooterGoal(*builder.fbb(), 400.0, 200.0); |
| 388 | |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 389 | superstructure::Goal::Builder superstructure_builder = |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 390 | builder.MakeBuilder<superstructure::Goal>(); |
milind upadhyay | b9dec71 | 2021-03-20 15:47:51 -0700 | [diff] [blame] | 391 | |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 392 | superstructure_builder.add_intake(intake_offset); |
milind-u | 0e20378 | 2021-10-30 21:57:20 -0700 | [diff] [blame] | 393 | superstructure_builder.add_intake_preloading(preloading_); |
Austin Schuh | 3653cf2 | 2021-11-12 11:54:51 -0800 | [diff] [blame] | 394 | if (!shooter_tracking_ && shooting_) { |
| 395 | superstructure_builder.add_shooter(shooter_offset); |
| 396 | } |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 397 | superstructure_builder.add_roller_voltage(roller_voltage_); |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 398 | superstructure_builder.add_roller_speed_compensation( |
| 399 | kRollerSpeedCompensation); |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 400 | superstructure_builder.add_hood_tracking(shooter_tracking_); |
| 401 | superstructure_builder.add_turret_tracking(shooter_tracking_); |
| 402 | superstructure_builder.add_shooter_tracking(shooter_tracking_); |
| 403 | superstructure_builder.add_shooting(shooting_); |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 404 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 405 | if (builder.Send(superstructure_builder.Finish()) != |
| 406 | aos::RawSender::Error::kOk) { |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 407 | AOS_LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 408 | } |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 409 | } |
milind upadhyay | 5e589d7 | 2021-03-27 13:47:18 -0700 | [diff] [blame] | 410 | |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 411 | void AutonomousActor::ExtendIntake() { |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame] | 412 | set_intake_goal(1.30); |
| 413 | set_roller_voltage(6.0); |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 414 | SendSuperstructureGoal(); |
| 415 | } |
| 416 | |
milind upadhyay | 5e589d7 | 2021-03-27 13:47:18 -0700 | [diff] [blame] | 417 | void AutonomousActor::RetractIntake() { |
| 418 | set_intake_goal(-0.89); |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame] | 419 | set_roller_voltage(6.0); |
milind upadhyay | 5e589d7 | 2021-03-27 13:47:18 -0700 | [diff] [blame] | 420 | SendSuperstructureGoal(); |
| 421 | } |
| 422 | |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame] | 423 | int AutonomousActor::Balls() { |
Ravago Jones | 1f32d62 | 2021-08-26 12:20:36 -0700 | [diff] [blame] | 424 | superstructure_status_fetcher_.Fetch(); |
| 425 | CHECK(superstructure_status_fetcher_.get() != nullptr); |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame] | 426 | return superstructure_status_fetcher_->shooter()->balls_shot(); |
| 427 | } |
Ravago Jones | 1f32d62 | 2021-08-26 12:20:36 -0700 | [diff] [blame] | 428 | |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame] | 429 | bool AutonomousActor::WaitUntilAbsoluteBallsShot(int absolute_balls) { |
Ravago Jones | 1f32d62 | 2021-08-26 12:20:36 -0700 | [diff] [blame] | 430 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
| 431 | event_loop()->monotonic_now(), |
Stephan Pleines | 743f83a | 2024-02-02 18:32:09 -0800 | [diff] [blame] | 432 | aos::common::actions::kLoopOffset); |
milind-u | 0e20378 | 2021-10-30 21:57:20 -0700 | [diff] [blame] | 433 | superstructure_status_fetcher_.Fetch(); |
| 434 | CHECK(superstructure_status_fetcher_.get() != nullptr); |
| 435 | int last_balls = superstructure_status_fetcher_->shooter()->balls_shot(); |
| 436 | LOG(INFO) << "Waiting for balls, started with " << absolute_balls; |
Ravago Jones | 1f32d62 | 2021-08-26 12:20:36 -0700 | [diff] [blame] | 437 | while (true) { |
| 438 | if (ShouldCancel()) { |
| 439 | return false; |
| 440 | } |
| 441 | phased_loop.SleepUntilNext(); |
| 442 | superstructure_status_fetcher_.Fetch(); |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame] | 443 | CHECK(superstructure_status_fetcher_.get() != nullptr); |
milind-u | 0e20378 | 2021-10-30 21:57:20 -0700 | [diff] [blame] | 444 | if (superstructure_status_fetcher_->shooter()->balls_shot() != last_balls) { |
| 445 | LOG(INFO) << "Shot " |
| 446 | << superstructure_status_fetcher_->shooter()->balls_shot() - |
| 447 | last_balls |
| 448 | << " balls, now at " |
| 449 | << superstructure_status_fetcher_->shooter()->balls_shot(); |
| 450 | } |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame] | 451 | if (superstructure_status_fetcher_->shooter()->balls_shot() >= |
| 452 | absolute_balls) { |
Ravago Jones | 1f32d62 | 2021-08-26 12:20:36 -0700 | [diff] [blame] | 453 | return true; |
| 454 | } |
milind-u | 0e20378 | 2021-10-30 21:57:20 -0700 | [diff] [blame] | 455 | |
| 456 | last_balls = superstructure_status_fetcher_->shooter()->balls_shot(); |
Ravago Jones | 1f32d62 | 2021-08-26 12:20:36 -0700 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame] | 460 | bool AutonomousActor::WaitForBallsShot(int num_wanted) { |
| 461 | return WaitUntilAbsoluteBallsShot(Balls() + num_wanted); |
| 462 | } |
| 463 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 464 | } // namespace y2020::actors |