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