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 | 3840ada | 2021-04-04 16:58:00 -0700 | [diff] [blame] | 14 | DEFINE_bool(ignore_vision, false, "If true, ignore vision"); |
| 15 | DEFINE_bool(galactic_search, false, |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 16 | "If true, do the galactic search autonomous"); |
Ravago Jones | 9c326f5 | 2021-03-20 15:00:16 -0700 | [diff] [blame] | 17 | DEFINE_bool(bounce, false, "If true, run the AutoNav Bounce autonomous"); |
| 18 | DEFINE_bool(barrel, false, "If true, run the AutoNav Barrel autonomous"); |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame^] | 19 | DEFINE_bool(slalom, false, "If true, run the AutoNav Slalom autonomous"); |
| 20 | DEFINE_bool(infinite_recharge_target_aligned, false, |
| 21 | "If true, run the Infinite Recharge autonomous that starts aligned " |
| 22 | "with the target"); |
| 23 | DEFINE_bool(infinite_recharge_target_offset, false, |
| 24 | "If true, run the Infinite Recharge autonomous that starts offset " |
| 25 | "from the target"); |
| 26 | DEFINE_bool(just_shoot, false, |
| 27 | "If true, run the autonomous that just shoots balls."); |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 28 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 29 | namespace y2020 { |
| 30 | namespace actors { |
| 31 | |
| 32 | using ::aos::monotonic_clock; |
| 33 | using ::frc971::ProfileParametersT; |
| 34 | using frc971::control_loops::drivetrain::LocalizerControl; |
| 35 | namespace chrono = ::std::chrono; |
| 36 | |
| 37 | AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop) |
| 38 | : frc971::autonomous::BaseAutonomousActor( |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 39 | event_loop, control_loops::drivetrain::GetDrivetrainConfig()), |
Ravago Jones | c2a0802 | 2021-02-06 17:40:54 -0800 | [diff] [blame] | 40 | localizer_control_sender_( |
| 41 | event_loop->MakeSender< |
| 42 | ::frc971::control_loops::drivetrain::LocalizerControl>( |
| 43 | "/drivetrain")), |
Austin Schuh | 67e127e | 2021-03-27 13:25:23 -0700 | [diff] [blame] | 44 | superstructure_goal_sender_( |
| 45 | event_loop->MakeSender<control_loops::superstructure::Goal>( |
| 46 | "/superstructure")), |
James Kuszmaul | ddd2ba6 | 2020-03-08 22:17:13 -0700 | [diff] [blame] | 47 | joystick_state_fetcher_( |
Ravago Jones | c2a0802 | 2021-02-06 17:40:54 -0800 | [diff] [blame] | 48 | event_loop->MakeFetcher<aos::JoystickState>("/aos")), |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 49 | path_fetcher_(event_loop->MakeFetcher<y2020::vision::GalacticSearchPath>( |
Austin Schuh | c6442fc | 2021-03-27 13:25:42 -0700 | [diff] [blame] | 50 | "/pi2/camera")), |
Ravago Jones | 1f32d62 | 2021-08-26 12:20:36 -0700 | [diff] [blame] | 51 | superstructure_status_fetcher_( |
| 52 | event_loop->MakeFetcher<y2020::control_loops::superstructure::Status>( |
| 53 | "/superstructure")), |
Ravago Jones | c2a0802 | 2021-02-06 17:40:54 -0800 | [diff] [blame] | 54 | auto_splines_() { |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 55 | set_max_drivetrain_voltage(2.0); |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 56 | replan_timer_ = event_loop->AddTimer([this]() { Replan(); }); |
| 57 | event_loop->OnRun([this, event_loop]() { |
| 58 | replan_timer_->Setup(event_loop->monotonic_now()); |
| 59 | }); |
| 60 | } |
| 61 | |
| 62 | void AutonomousActor::Replan() { |
| 63 | if (FLAGS_galactic_search) { |
| 64 | galactic_search_splines_ = { |
| 65 | .red_a = PlanSpline(std::bind(&AutonomousSplines::SplineRedA, |
| 66 | &auto_splines_, std::placeholders::_1), |
| 67 | SplineDirection::kForward), |
| 68 | .red_b = PlanSpline(std::bind(&AutonomousSplines::SplineRedB, |
| 69 | &auto_splines_, std::placeholders::_1), |
| 70 | SplineDirection::kForward), |
| 71 | .blue_a = PlanSpline(std::bind(&AutonomousSplines::SplineBlueA, |
| 72 | &auto_splines_, std::placeholders::_1), |
| 73 | SplineDirection::kForward), |
| 74 | .blue_b = PlanSpline(std::bind(&AutonomousSplines::SplineBlueB, |
| 75 | &auto_splines_, std::placeholders::_1), |
| 76 | SplineDirection::kForward)}; |
| 77 | } else if (FLAGS_bounce) { |
| 78 | bounce_splines_ = { |
| 79 | PlanSpline(std::bind(&AutonomousSplines::AutoNavBounce1, &auto_splines_, |
| 80 | std::placeholders::_1), |
| 81 | SplineDirection::kForward), |
| 82 | PlanSpline(std::bind(&AutonomousSplines::AutoNavBounce2, &auto_splines_, |
| 83 | std::placeholders::_1), |
| 84 | SplineDirection::kBackward), |
| 85 | PlanSpline(std::bind(&AutonomousSplines::AutoNavBounce3, &auto_splines_, |
| 86 | std::placeholders::_1), |
| 87 | SplineDirection::kForward), |
| 88 | PlanSpline(std::bind(&AutonomousSplines::AutoNavBounce4, &auto_splines_, |
| 89 | std::placeholders::_1), |
| 90 | SplineDirection::kBackward)}; |
| 91 | } else if (FLAGS_barrel) { |
| 92 | barrel_spline_ = |
| 93 | PlanSpline(std::bind(&AutonomousSplines::AutoNavBarrel, &auto_splines_, |
| 94 | std::placeholders::_1), |
| 95 | SplineDirection::kForward); |
| 96 | } else if (FLAGS_slalom) { |
| 97 | slalom_spline_ = |
| 98 | PlanSpline(std::bind(&AutonomousSplines::AutoNavSlalom, &auto_splines_, |
| 99 | std::placeholders::_1), |
| 100 | SplineDirection::kForward); |
| 101 | } else if (FLAGS_spline_auto) { |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 102 | test_spline_ = PlanSpline(std::bind(&AutonomousSplines::TestSpline, |
| 103 | &auto_splines_, std::placeholders::_1), |
| 104 | SplineDirection::kForward); |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame^] | 105 | } else if (FLAGS_infinite_recharge_target_offset) { |
| 106 | target_offset_splines_ = { |
| 107 | PlanSpline(std::bind(&AutonomousSplines::TargetOffset1, &auto_splines_, |
| 108 | std::placeholders::_1), |
| 109 | SplineDirection::kForward), |
| 110 | PlanSpline(std::bind(&AutonomousSplines::TargetOffset2, &auto_splines_, |
| 111 | std::placeholders::_1), |
| 112 | SplineDirection::kBackward)}; |
| 113 | } else if (FLAGS_infinite_recharge_target_aligned) { |
| 114 | target_aligned_splines_ = { |
| 115 | PlanSpline(std::bind(&AutonomousSplines::TargetAligned1, &auto_splines_, |
| 116 | std::placeholders::_1), |
| 117 | SplineDirection::kForward), |
| 118 | PlanSpline(std::bind(&AutonomousSplines::TargetAligned2, &auto_splines_, |
| 119 | std::placeholders::_1), |
| 120 | SplineDirection::kBackward)}; |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 121 | } |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 122 | } |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 123 | |
| 124 | void AutonomousActor::Reset() { |
| 125 | InitializeEncoders(); |
| 126 | ResetDrivetrain(); |
milind upadhyay | b2e840a | 2021-03-27 13:54:49 -0700 | [diff] [blame] | 127 | RetractIntake(); |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 128 | |
James Kuszmaul | ddd2ba6 | 2020-03-08 22:17:13 -0700 | [diff] [blame] | 129 | joystick_state_fetcher_.Fetch(); |
| 130 | CHECK(joystick_state_fetcher_.get() != nullptr) |
| 131 | << "Expect at least one JoystickState message before running auto..."; |
| 132 | alliance_ = joystick_state_fetcher_->alliance(); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | bool AutonomousActor::RunAction( |
Austin Schuh | 6fb0a6d | 2021-01-23 15:43:17 -0800 | [diff] [blame] | 136 | const ::frc971::autonomous::AutonomousActionParams *params) { |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 137 | Reset(); |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 138 | |
| 139 | // Queue up a replan to occur as soon as this action completes. |
| 140 | // TODO(james): Modify this so we don't replan during teleop. |
| 141 | replan_timer_->Setup(monotonic_now()); |
| 142 | |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 143 | AOS_LOG(INFO, "Params are %d\n", params->mode()); |
James Kuszmaul | ddd2ba6 | 2020-03-08 22:17:13 -0700 | [diff] [blame] | 144 | if (alliance_ == aos::Alliance::kInvalid) { |
| 145 | AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection."); |
| 146 | return false; |
| 147 | } |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 148 | if (FLAGS_galactic_search) { |
| 149 | GalacticSearch(); |
Ravago Jones | 9c326f5 | 2021-03-20 15:00:16 -0700 | [diff] [blame] | 150 | } else if (FLAGS_bounce) { |
| 151 | AutoNavBounce(); |
| 152 | } else if (FLAGS_barrel) { |
| 153 | AutoNavBarrel(); |
| 154 | } else if (FLAGS_slalom) { |
| 155 | AutoNavSlalom(); |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame^] | 156 | } else if (FLAGS_infinite_recharge_target_aligned) { |
| 157 | TargetAligned(); |
| 158 | } else if (FLAGS_infinite_recharge_target_offset) { |
| 159 | TargetOffset(); |
| 160 | } else if (FLAGS_just_shoot) { |
| 161 | JustShoot(); |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 162 | } else if (FLAGS_spline_auto) { |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 163 | SplineAuto(); |
| 164 | } else { |
| 165 | return DriveFwd(); |
| 166 | } |
| 167 | return true; |
| 168 | } |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 169 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 170 | void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) { |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 171 | // Set up the starting position for the blue alliance. |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 172 | |
| 173 | // TODO(james): Resetting the localizer breaks the left/right statespace |
| 174 | // controller. That is a bug, but we can fix that later by not resetting. |
| 175 | auto builder = localizer_control_sender_.MakeBuilder(); |
| 176 | |
| 177 | LocalizerControl::Builder localizer_control_builder = |
| 178 | builder.MakeBuilder<LocalizerControl>(); |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 179 | localizer_control_builder.add_x(start(0)); |
| 180 | localizer_control_builder.add_y(start(1)); |
| 181 | localizer_control_builder.add_theta(start(2)); |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 182 | localizer_control_builder.add_theta_uncertainty(0.00001); |
| 183 | if (!builder.Send(localizer_control_builder.Finish())) { |
| 184 | AOS_LOG(ERROR, "Failed to reset localizer.\n"); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | void AutonomousActor::GalacticSearch() { |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 189 | CHECK(galactic_search_splines_); |
| 190 | |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 191 | path_fetcher_.Fetch(); |
Austin Schuh | 3fb9e42 | 2021-03-31 20:11:32 -0700 | [diff] [blame] | 192 | SplineHandle *spline = nullptr; |
| 193 | if (path_fetcher_.get()) { |
| 194 | if (path_fetcher_->alliance() == y2020::vision::Alliance::kUnknown) { |
| 195 | AOS_LOG(ERROR, "The galactic search path is unknown, doing nothing."); |
| 196 | return; |
| 197 | } |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 198 | if (path_fetcher_->alliance() == y2020::vision::Alliance::kRed) { |
| 199 | if (path_fetcher_->letter() == y2020::vision::Letter::kA) { |
| 200 | LOG(INFO) << "Red A"; |
| 201 | spline = &galactic_search_splines_->red_a; |
| 202 | } else { |
| 203 | LOG(INFO) << "Red B"; |
| 204 | CHECK(path_fetcher_->letter() == y2020::vision::Letter::kB); |
| 205 | spline = &galactic_search_splines_->red_b; |
| 206 | } |
| 207 | } else { |
| 208 | if (path_fetcher_->letter() == y2020::vision::Letter::kA) { |
| 209 | LOG(INFO) << "Blue A"; |
| 210 | spline = &galactic_search_splines_->blue_a; |
| 211 | } else { |
| 212 | LOG(INFO) << "Blue B"; |
| 213 | CHECK(path_fetcher_->letter() == y2020::vision::Letter::kB); |
Austin Schuh | 8806bed | 2021-03-31 20:12:00 -0700 | [diff] [blame] | 214 | spline = &galactic_search_splines_->blue_b; |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 217 | } |
Austin Schuh | 3fb9e42 | 2021-03-31 20:11:32 -0700 | [diff] [blame] | 218 | if (FLAGS_ignore_vision) { |
| 219 | LOG(INFO) << "Forcing Red B"; |
| 220 | spline = &galactic_search_splines_->red_b; |
| 221 | } |
| 222 | |
| 223 | CHECK(spline != nullptr) |
| 224 | << "Expect at least one GalacticSearchPath message before running " |
| 225 | "auto..."; |
| 226 | |
| 227 | SendStartingPosition(spline->starting_position()); |
| 228 | |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame^] | 229 | ExtendIntake(); |
Austin Schuh | 3fb9e42 | 2021-03-31 20:11:32 -0700 | [diff] [blame] | 230 | |
| 231 | if (!spline->WaitForPlan()) return; |
| 232 | spline->Start(); |
| 233 | |
| 234 | if (!spline->WaitForSplineDistanceRemaining(0.02)) return; |
| 235 | RetractIntake(); |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Ravago Jones | 9c326f5 | 2021-03-20 15:00:16 -0700 | [diff] [blame] | 238 | void AutonomousActor::AutoNavBounce() { |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 239 | CHECK(bounce_splines_); |
Ravago Jones | 9c326f5 | 2021-03-20 15:00:16 -0700 | [diff] [blame] | 240 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 241 | auto &splines = *bounce_splines_; |
Ravago Jones | 9c326f5 | 2021-03-20 15:00:16 -0700 | [diff] [blame] | 242 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 243 | SendStartingPosition(splines[0].starting_position()); |
Ravago Jones | 9c326f5 | 2021-03-20 15:00:16 -0700 | [diff] [blame] | 244 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 245 | if (!splines[0].WaitForPlan()) return; |
| 246 | splines[0].Start(); |
Ravago Jones | 9c326f5 | 2021-03-20 15:00:16 -0700 | [diff] [blame] | 247 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 248 | if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return; |
Ravago Jones | 9c326f5 | 2021-03-20 15:00:16 -0700 | [diff] [blame] | 249 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 250 | if (!splines[1].WaitForPlan()) return; |
| 251 | splines[1].Start(); |
Ravago Jones | 9c326f5 | 2021-03-20 15:00:16 -0700 | [diff] [blame] | 252 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 253 | if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return; |
Ravago Jones | 9c326f5 | 2021-03-20 15:00:16 -0700 | [diff] [blame] | 254 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 255 | if (!splines[2].WaitForPlan()) return; |
| 256 | splines[2].Start(); |
Ravago Jones | 9c326f5 | 2021-03-20 15:00:16 -0700 | [diff] [blame] | 257 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 258 | if (!splines[2].WaitForSplineDistanceRemaining(0.02)) return; |
Ravago Jones | 9c326f5 | 2021-03-20 15:00:16 -0700 | [diff] [blame] | 259 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 260 | if (!splines[3].WaitForPlan()) return; |
| 261 | splines[3].Start(); |
Ravago Jones | 9c326f5 | 2021-03-20 15:00:16 -0700 | [diff] [blame] | 262 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 263 | if (!splines[3].WaitForSplineDistanceRemaining(0.02)) return; |
Ravago Jones | 9c326f5 | 2021-03-20 15:00:16 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | void AutonomousActor::AutoNavBarrel() { |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 267 | CHECK(barrel_spline_); |
Ravago Jones | 9c326f5 | 2021-03-20 15:00:16 -0700 | [diff] [blame] | 268 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 269 | SendStartingPosition(barrel_spline_->starting_position()); |
Ravago Jones | 9c326f5 | 2021-03-20 15:00:16 -0700 | [diff] [blame] | 270 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 271 | if (!barrel_spline_->WaitForPlan()) return; |
| 272 | barrel_spline_->Start(); |
| 273 | |
| 274 | if (!barrel_spline_->WaitForSplineDistanceRemaining(0.02)) return; |
Ravago Jones | 9c326f5 | 2021-03-20 15:00:16 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | void AutonomousActor::AutoNavSlalom() { |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 278 | CHECK(slalom_spline_); |
Ravago Jones | 9c326f5 | 2021-03-20 15:00:16 -0700 | [diff] [blame] | 279 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 280 | SendStartingPosition(slalom_spline_->starting_position()); |
Ravago Jones | 9c326f5 | 2021-03-20 15:00:16 -0700 | [diff] [blame] | 281 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 282 | if (!slalom_spline_->WaitForPlan()) return; |
| 283 | slalom_spline_->Start(); |
| 284 | |
| 285 | if (!slalom_spline_->WaitForSplineDistanceRemaining(0.02)) return; |
Ravago Jones | 9c326f5 | 2021-03-20 15:00:16 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame^] | 288 | void AutonomousActor::TargetAligned() { |
| 289 | CHECK(target_aligned_splines_); |
| 290 | auto &splines = *target_aligned_splines_; |
| 291 | SendStartingPosition(splines[0].starting_position()); |
| 292 | |
| 293 | // shoot pre-loaded balls |
| 294 | set_shooter_tracking(true); |
| 295 | set_shooting(true); |
| 296 | SendSuperstructureGoal(); |
| 297 | |
| 298 | if (!WaitForBallsShot(3)) return; |
| 299 | |
| 300 | set_shooting(false); |
| 301 | SendSuperstructureGoal(); |
| 302 | |
| 303 | ExtendIntake(); |
| 304 | |
| 305 | // pickup 3 more balls |
| 306 | if (!splines[0].WaitForPlan()) return; |
| 307 | splines[0].Start(); |
| 308 | |
| 309 | if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return; |
| 310 | RetractIntake(); |
| 311 | |
| 312 | if (!splines[1].WaitForPlan()) return; |
| 313 | splines[1].Start(); |
| 314 | |
| 315 | if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return; |
| 316 | |
| 317 | // shoot the new balls in front of the goal. |
| 318 | set_shooting(true); |
| 319 | SendSuperstructureGoal(); |
| 320 | |
| 321 | if (!WaitForBallsShot(3)) return; |
| 322 | |
| 323 | set_shooting(false); |
| 324 | set_shooter_tracking(false); |
| 325 | SendSuperstructureGoal(); |
| 326 | } |
| 327 | |
| 328 | void AutonomousActor::JustShoot() { |
| 329 | // shoot pre-loaded balls |
| 330 | set_shooter_tracking(true); |
| 331 | set_shooting(true); |
| 332 | SendSuperstructureGoal(); |
| 333 | |
| 334 | if (!WaitForBallsShot(3)) return; |
| 335 | |
| 336 | set_shooting(false); |
| 337 | set_shooter_tracking(true); |
| 338 | SendSuperstructureGoal(); |
| 339 | } |
| 340 | |
| 341 | void AutonomousActor::TargetOffset() { |
| 342 | CHECK(target_offset_splines_); |
| 343 | auto &splines = *target_offset_splines_; |
| 344 | SendStartingPosition(splines[0].starting_position()); |
| 345 | |
| 346 | // spin up shooter |
| 347 | set_shooter_tracking(true); |
| 348 | SendSuperstructureGoal(); |
| 349 | ExtendIntake(); |
| 350 | |
| 351 | // pickup 2 more balls in front of the trench run |
| 352 | if (!splines[0].WaitForPlan()) return; |
| 353 | splines[0].Start(); |
| 354 | |
| 355 | if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return; |
| 356 | RetractIntake(); |
| 357 | |
| 358 | if (!splines[1].WaitForPlan()) return; |
| 359 | splines[1].Start(); |
| 360 | |
| 361 | if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return; |
| 362 | |
| 363 | // shoot the balls from in front of the goal. |
| 364 | set_shooting(true); |
| 365 | SendSuperstructureGoal(); |
| 366 | |
| 367 | if (!WaitForBallsShot(5)) return; |
| 368 | |
| 369 | set_shooting(false); |
| 370 | set_shooter_tracking(false); |
| 371 | SendSuperstructureGoal(); |
| 372 | } |
| 373 | |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 374 | void AutonomousActor::SplineAuto() { |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 375 | CHECK(test_spline_); |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 376 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 377 | SendStartingPosition(test_spline_->starting_position()); |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 378 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 379 | if (!test_spline_->WaitForPlan()) return; |
| 380 | test_spline_->Start(); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 381 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 382 | if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return; |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 383 | } |
| 384 | |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 385 | ProfileParametersT MakeProfileParametersT(const float max_velocity, |
| 386 | const float max_acceleration) { |
| 387 | ProfileParametersT params; |
| 388 | params.max_velocity = max_velocity; |
| 389 | params.max_acceleration = max_acceleration; |
| 390 | return params; |
| 391 | } |
| 392 | |
| 393 | bool AutonomousActor::DriveFwd() { |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 394 | SendStartingPosition({0, 0, 0}); |
Austin Schuh | fd1715f | 2021-01-30 16:58:24 -0800 | [diff] [blame] | 395 | const ProfileParametersT kDrive = MakeProfileParametersT(0.3f, 1.0f); |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 396 | const ProfileParametersT kTurn = MakeProfileParametersT(5.0f, 15.0f); |
Austin Schuh | fd1715f | 2021-01-30 16:58:24 -0800 | [diff] [blame] | 397 | StartDrive(1.0, 0.0, kDrive, kTurn); |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 398 | return WaitForDriveDone(); |
| 399 | } |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 400 | |
| 401 | void AutonomousActor::SendSuperstructureGoal() { |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 402 | auto builder = superstructure_goal_sender_.MakeBuilder(); |
| 403 | |
| 404 | flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal> |
| 405 | intake_offset; |
milind upadhyay | b9dec71 | 2021-03-20 15:47:51 -0700 | [diff] [blame] | 406 | |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 407 | { |
| 408 | StaticZeroingSingleDOFProfiledSubsystemGoal::Builder intake_builder = |
| 409 | builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>(); |
| 410 | |
milind upadhyay | b9dec71 | 2021-03-20 15:47:51 -0700 | [diff] [blame] | 411 | frc971::ProfileParameters::Builder profile_params_builder = |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 412 | builder.MakeBuilder<frc971::ProfileParameters>(); |
Austin Schuh | b39a21c | 2021-03-31 20:12:18 -0700 | [diff] [blame] | 413 | profile_params_builder.add_max_velocity(20.0); |
| 414 | profile_params_builder.add_max_acceleration(60.0); |
milind upadhyay | b9dec71 | 2021-03-20 15:47:51 -0700 | [diff] [blame] | 415 | flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset = |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 416 | profile_params_builder.Finish(); |
| 417 | intake_builder.add_unsafe_goal(intake_goal_); |
| 418 | intake_builder.add_profile_params(profile_params_offset); |
| 419 | intake_offset = intake_builder.Finish(); |
| 420 | } |
| 421 | |
| 422 | superstructure::Goal::Builder superstructure_builder = |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 423 | builder.MakeBuilder<superstructure::Goal>(); |
milind upadhyay | b9dec71 | 2021-03-20 15:47:51 -0700 | [diff] [blame] | 424 | |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 425 | superstructure_builder.add_intake(intake_offset); |
Ravago Jones | f8b7bfe | 2021-10-09 16:25:29 -0700 | [diff] [blame] | 426 | superstructure_builder.add_intake_preloading(intake_preloading_); |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 427 | superstructure_builder.add_roller_voltage(roller_voltage_); |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 428 | superstructure_builder.add_roller_speed_compensation( |
| 429 | kRollerSpeedCompensation); |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame^] | 430 | superstructure_builder.add_hood_tracking(shooter_tracking_); |
| 431 | superstructure_builder.add_turret_tracking(shooter_tracking_); |
| 432 | superstructure_builder.add_shooter_tracking(shooter_tracking_); |
| 433 | superstructure_builder.add_shooting(shooting_); |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 434 | |
| 435 | if (!builder.Send(superstructure_builder.Finish())) { |
| 436 | AOS_LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 437 | } |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 438 | } |
milind upadhyay | 5e589d7 | 2021-03-27 13:47:18 -0700 | [diff] [blame] | 439 | |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame^] | 440 | void AutonomousActor::ExtendIntake() { |
| 441 | set_intake_goal(1.25); |
| 442 | set_roller_voltage(12.0); |
| 443 | set_intake_preloading(true); |
| 444 | SendSuperstructureGoal(); |
| 445 | } |
| 446 | |
milind upadhyay | 5e589d7 | 2021-03-27 13:47:18 -0700 | [diff] [blame] | 447 | void AutonomousActor::RetractIntake() { |
| 448 | set_intake_goal(-0.89); |
| 449 | set_roller_voltage(0.0); |
Ravago Jones | f8b7bfe | 2021-10-09 16:25:29 -0700 | [diff] [blame] | 450 | set_intake_preloading(false); |
milind upadhyay | 5e589d7 | 2021-03-27 13:47:18 -0700 | [diff] [blame] | 451 | SendSuperstructureGoal(); |
| 452 | } |
| 453 | |
Ravago Jones | 1f32d62 | 2021-08-26 12:20:36 -0700 | [diff] [blame] | 454 | bool AutonomousActor::WaitForBallsShot(int num_wanted) { |
| 455 | superstructure_status_fetcher_.Fetch(); |
| 456 | CHECK(superstructure_status_fetcher_.get() != nullptr); |
| 457 | const int initial_balls_shot = |
| 458 | superstructure_status_fetcher_->shooter()->balls_shot(); |
| 459 | int balls_shot = initial_balls_shot; |
| 460 | |
| 461 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
| 462 | event_loop()->monotonic_now(), |
| 463 | frc971::controls::kLoopFrequency / 2); |
| 464 | while (true) { |
| 465 | if (ShouldCancel()) { |
| 466 | return false; |
| 467 | } |
| 468 | phased_loop.SleepUntilNext(); |
| 469 | superstructure_status_fetcher_.Fetch(); |
| 470 | balls_shot = superstructure_status_fetcher_->shooter()->balls_shot(); |
| 471 | if ((balls_shot - initial_balls_shot) >= num_wanted) { |
| 472 | return true; |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 477 | } // namespace actors |
| 478 | } // namespace y2020 |