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