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