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")), |
James Kuszmaul | ddd2ba6 | 2020-03-08 22:17:13 -0700 | [diff] [blame] | 41 | joystick_state_fetcher_( |
Ravago Jones | c2a0802 | 2021-02-06 17:40:54 -0800 | [diff] [blame] | 42 | event_loop->MakeFetcher<aos::JoystickState>("/aos")), |
Ravago Jones | 1f32d62 | 2021-08-26 12:20:36 -0700 | [diff] [blame] | 43 | superstructure_status_fetcher_( |
| 44 | event_loop->MakeFetcher<y2020::control_loops::superstructure::Status>( |
| 45 | "/superstructure")), |
Ravago Jones | c2a0802 | 2021-02-06 17:40:54 -0800 | [diff] [blame] | 46 | auto_splines_() { |
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]() { |
| 50 | replan_timer_->Setup(event_loop->monotonic_now()); |
| 51 | }); |
James Kuszmaul | 4303a5d | 2021-10-23 19:58:48 -0700 | [diff] [blame] | 52 | event_loop->MakeWatcher("/aos", [this](const aos::RobotState &msg) { |
| 53 | if (msg.user_button()) { |
| 54 | user_indicated_safe_to_reset_ = true; |
| 55 | MaybeSendStartingPosition(); |
| 56 | } |
| 57 | }); |
| 58 | event_loop->MakeWatcher("/aos", [this](const aos::JoystickState &msg) { |
| 59 | if (msg.has_alliance() && (msg.alliance() != alliance_)) { |
| 60 | alliance_ = msg.alliance(); |
| 61 | Replan(); |
| 62 | } |
| 63 | }); |
| 64 | } |
| 65 | |
| 66 | void AutonomousActor::MaybeSendStartingPosition() { |
| 67 | if (user_indicated_safe_to_reset_ && !sent_starting_position_) { |
| 68 | CHECK(starting_position_); |
| 69 | SendStartingPosition(starting_position_.value()); |
| 70 | } |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void AutonomousActor::Replan() { |
James Kuszmaul | 4303a5d | 2021-10-23 19:58:48 -0700 | [diff] [blame] | 74 | sent_starting_position_ = false; |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame^] | 75 | if (alliance_ == aos::Alliance::kInvalid) { |
| 76 | return; |
| 77 | } |
Ravago Jones | 8c5737e | 2021-10-16 15:36:12 -0700 | [diff] [blame] | 78 | if (FLAGS_spline_auto) { |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 79 | test_spline_ = PlanSpline(std::bind(&AutonomousSplines::TestSpline, |
| 80 | &auto_splines_, std::placeholders::_1), |
| 81 | SplineDirection::kForward); |
James Kuszmaul | 4303a5d | 2021-10-23 19:58:48 -0700 | [diff] [blame] | 82 | starting_position_ = test_spline_->starting_position(); |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame^] | 83 | } else if (FLAGS_target_aligned) { |
| 84 | target_aligned_splines_ = { |
| 85 | PlanSpline(std::bind(&AutonomousSplines::TargetAligned1, &auto_splines_, |
| 86 | std::placeholders::_1, alliance_), |
| 87 | SplineDirection::kForward), |
| 88 | PlanSpline(std::bind(&AutonomousSplines::TargetAligned2, &auto_splines_, |
| 89 | std::placeholders::_1, alliance_), |
| 90 | SplineDirection::kBackward)}; |
| 91 | starting_position_ = target_aligned_splines_.value()[0].starting_position(); |
Ravago Jones | 8c5737e | 2021-10-16 15:36:12 -0700 | [diff] [blame] | 92 | } else if (FLAGS_target_offset) { |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 93 | target_offset_splines_ = { |
| 94 | PlanSpline(std::bind(&AutonomousSplines::TargetOffset1, &auto_splines_, |
| 95 | std::placeholders::_1), |
| 96 | SplineDirection::kForward), |
| 97 | PlanSpline(std::bind(&AutonomousSplines::TargetOffset2, &auto_splines_, |
| 98 | std::placeholders::_1), |
| 99 | SplineDirection::kBackward)}; |
James Kuszmaul | 4303a5d | 2021-10-23 19:58:48 -0700 | [diff] [blame] | 100 | starting_position_ = target_offset_splines_.value()[0].starting_position(); |
James Kuszmaul | 4303a5d | 2021-10-23 19:58:48 -0700 | [diff] [blame] | 101 | } else { |
| 102 | starting_position_ = Eigen::Vector3d::Zero(); |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 103 | } |
James Kuszmaul | 4303a5d | 2021-10-23 19:58:48 -0700 | [diff] [blame] | 104 | MaybeSendStartingPosition(); |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 105 | } |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 106 | |
| 107 | void AutonomousActor::Reset() { |
| 108 | InitializeEncoders(); |
| 109 | ResetDrivetrain(); |
milind upadhyay | b2e840a | 2021-03-27 13:54:49 -0700 | [diff] [blame] | 110 | RetractIntake(); |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 111 | |
James Kuszmaul | ddd2ba6 | 2020-03-08 22:17:13 -0700 | [diff] [blame] | 112 | joystick_state_fetcher_.Fetch(); |
| 113 | CHECK(joystick_state_fetcher_.get() != nullptr) |
| 114 | << "Expect at least one JoystickState message before running auto..."; |
| 115 | alliance_ = joystick_state_fetcher_->alliance(); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | bool AutonomousActor::RunAction( |
Austin Schuh | 6fb0a6d | 2021-01-23 15:43:17 -0800 | [diff] [blame] | 119 | const ::frc971::autonomous::AutonomousActionParams *params) { |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 120 | Reset(); |
James Kuszmaul | 4303a5d | 2021-10-23 19:58:48 -0700 | [diff] [blame] | 121 | if (!user_indicated_safe_to_reset_) { |
| 122 | AOS_LOG(WARNING, "Didn't send starting position prior to starting auto."); |
| 123 | SendStartingPosition(starting_position_.value()); |
| 124 | } |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 125 | |
| 126 | // Queue up a replan to occur as soon as this action completes. |
| 127 | // TODO(james): Modify this so we don't replan during teleop. |
| 128 | replan_timer_->Setup(monotonic_now()); |
| 129 | |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 130 | AOS_LOG(INFO, "Params are %d\n", params->mode()); |
James Kuszmaul | ddd2ba6 | 2020-03-08 22:17:13 -0700 | [diff] [blame] | 131 | if (alliance_ == aos::Alliance::kInvalid) { |
| 132 | AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection."); |
| 133 | return false; |
| 134 | } |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame^] | 135 | if (FLAGS_spline_auto) { |
| 136 | SplineAuto(); |
| 137 | } else if (FLAGS_target_aligned) { |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 138 | TargetAligned(); |
Ravago Jones | 8c5737e | 2021-10-16 15:36:12 -0700 | [diff] [blame] | 139 | } else if (FLAGS_target_offset) { |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 140 | TargetOffset(); |
| 141 | } else if (FLAGS_just_shoot) { |
| 142 | JustShoot(); |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 143 | } else { |
| 144 | return DriveFwd(); |
| 145 | } |
| 146 | return true; |
| 147 | } |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 148 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 149 | void AutonomousActor::SendStartingPosition(const Eigen::Vector3d &start) { |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 150 | // Set up the starting position for the blue alliance. |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 151 | |
| 152 | // TODO(james): Resetting the localizer breaks the left/right statespace |
| 153 | // controller. That is a bug, but we can fix that later by not resetting. |
| 154 | auto builder = localizer_control_sender_.MakeBuilder(); |
| 155 | |
| 156 | LocalizerControl::Builder localizer_control_builder = |
| 157 | builder.MakeBuilder<LocalizerControl>(); |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 158 | localizer_control_builder.add_x(start(0)); |
| 159 | localizer_control_builder.add_y(start(1)); |
| 160 | localizer_control_builder.add_theta(start(2)); |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 161 | localizer_control_builder.add_theta_uncertainty(0.00001); |
| 162 | if (!builder.Send(localizer_control_builder.Finish())) { |
| 163 | AOS_LOG(ERROR, "Failed to reset localizer.\n"); |
| 164 | } |
| 165 | } |
| 166 | |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 167 | void AutonomousActor::TargetAligned() { |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame^] | 168 | aos::monotonic_clock::time_point start_time = aos::monotonic_clock::now(); |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 169 | CHECK(target_aligned_splines_); |
| 170 | auto &splines = *target_aligned_splines_; |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 171 | |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame^] | 172 | // Spin up. |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 173 | set_shooting(true); |
| 174 | SendSuperstructureGoal(); |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 175 | if (!WaitForBallsShot(3)) return; |
| 176 | |
| 177 | set_shooting(false); |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame^] | 178 | ExtendIntake(); |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 179 | SendSuperstructureGoal(); |
| 180 | |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 181 | if (!splines[0].WaitForPlan()) return; |
| 182 | splines[0].Start(); |
| 183 | |
| 184 | if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return; |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame^] | 185 | std::this_thread::sleep_for(chrono::milliseconds(200)); |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 186 | RetractIntake(); |
| 187 | |
| 188 | if (!splines[1].WaitForPlan()) return; |
| 189 | splines[1].Start(); |
| 190 | |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame^] | 191 | if (!splines[1].WaitForSplineDistanceRemaining(2.0)) return; |
| 192 | // Reverse the rollers for a moment to try to unjam any jammed balls. Since |
| 193 | // we are moving here, this is free to try. |
| 194 | set_roller_voltage(-12.0); |
| 195 | std::this_thread::sleep_for(chrono::milliseconds(300)); |
| 196 | set_roller_voltage(0.0); |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 197 | |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame^] | 198 | // Once we come to a stop, give the robot a moment to settle down. This makes |
| 199 | // the shot more accurate. |
| 200 | if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return; |
| 201 | std::this_thread::sleep_for(chrono::milliseconds(1500)); |
| 202 | set_shooting(true); |
| 203 | const int balls = Balls(); |
| 204 | |
| 205 | SendSuperstructureGoal(); |
| 206 | |
| 207 | std::this_thread::sleep_for(chrono::milliseconds(1500)); |
| 208 | |
| 209 | // We have been seeing balls get stuck on the intake roller. Reverse the |
| 210 | // roller again for a moment to unjam it. |
| 211 | set_shooting(false); |
| 212 | set_roller_voltage(-12.0); |
| 213 | SendSuperstructureGoal(); |
| 214 | std::this_thread::sleep_for(chrono::milliseconds(250)); |
| 215 | |
| 216 | set_roller_voltage(0.0); |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 217 | set_shooting(true); |
| 218 | SendSuperstructureGoal(); |
| 219 | |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame^] | 220 | if (!WaitUntilAbsoluteBallsShot(3 + balls)) return; |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 221 | |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame^] | 222 | LOG(INFO) << "Took " |
| 223 | << chrono::duration<double>(aos::monotonic_clock::now() - |
| 224 | start_time) |
| 225 | .count(); |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void AutonomousActor::JustShoot() { |
| 229 | // shoot pre-loaded balls |
| 230 | set_shooter_tracking(true); |
| 231 | set_shooting(true); |
| 232 | SendSuperstructureGoal(); |
| 233 | |
| 234 | if (!WaitForBallsShot(3)) return; |
| 235 | |
| 236 | set_shooting(false); |
| 237 | set_shooter_tracking(true); |
| 238 | SendSuperstructureGoal(); |
| 239 | } |
| 240 | |
| 241 | void AutonomousActor::TargetOffset() { |
| 242 | CHECK(target_offset_splines_); |
| 243 | auto &splines = *target_offset_splines_; |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 244 | |
| 245 | // spin up shooter |
| 246 | set_shooter_tracking(true); |
| 247 | SendSuperstructureGoal(); |
| 248 | ExtendIntake(); |
| 249 | |
| 250 | // pickup 2 more balls in front of the trench run |
| 251 | if (!splines[0].WaitForPlan()) return; |
| 252 | splines[0].Start(); |
| 253 | |
| 254 | if (!splines[0].WaitForSplineDistanceRemaining(0.02)) return; |
| 255 | RetractIntake(); |
| 256 | |
| 257 | if (!splines[1].WaitForPlan()) return; |
| 258 | splines[1].Start(); |
| 259 | |
| 260 | if (!splines[1].WaitForSplineDistanceRemaining(0.02)) return; |
| 261 | |
| 262 | // shoot the balls from in front of the goal. |
| 263 | set_shooting(true); |
| 264 | SendSuperstructureGoal(); |
| 265 | |
| 266 | if (!WaitForBallsShot(5)) return; |
| 267 | |
| 268 | set_shooting(false); |
| 269 | set_shooter_tracking(false); |
| 270 | SendSuperstructureGoal(); |
| 271 | } |
| 272 | |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 273 | void AutonomousActor::SplineAuto() { |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 274 | CHECK(test_spline_); |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 275 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 276 | if (!test_spline_->WaitForPlan()) return; |
| 277 | test_spline_->Start(); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 278 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 279 | if (!test_spline_->WaitForSplineDistanceRemaining(0.02)) return; |
kyle | 96c406e | 2021-02-27 14:07:22 -0800 | [diff] [blame] | 280 | } |
| 281 | |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 282 | ProfileParametersT MakeProfileParametersT(const float max_velocity, |
| 283 | const float max_acceleration) { |
| 284 | ProfileParametersT params; |
| 285 | params.max_velocity = max_velocity; |
| 286 | params.max_acceleration = max_acceleration; |
| 287 | return params; |
| 288 | } |
| 289 | |
| 290 | bool AutonomousActor::DriveFwd() { |
Austin Schuh | fd1715f | 2021-01-30 16:58:24 -0800 | [diff] [blame] | 291 | const ProfileParametersT kDrive = MakeProfileParametersT(0.3f, 1.0f); |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 292 | const ProfileParametersT kTurn = MakeProfileParametersT(5.0f, 15.0f); |
Austin Schuh | fd1715f | 2021-01-30 16:58:24 -0800 | [diff] [blame] | 293 | StartDrive(1.0, 0.0, kDrive, kTurn); |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 294 | return WaitForDriveDone(); |
| 295 | } |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 296 | |
| 297 | void AutonomousActor::SendSuperstructureGoal() { |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 298 | auto builder = superstructure_goal_sender_.MakeBuilder(); |
| 299 | |
| 300 | flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal> |
| 301 | intake_offset; |
milind upadhyay | b9dec71 | 2021-03-20 15:47:51 -0700 | [diff] [blame] | 302 | |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 303 | { |
| 304 | StaticZeroingSingleDOFProfiledSubsystemGoal::Builder intake_builder = |
| 305 | builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>(); |
| 306 | |
milind upadhyay | b9dec71 | 2021-03-20 15:47:51 -0700 | [diff] [blame] | 307 | frc971::ProfileParameters::Builder profile_params_builder = |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 308 | builder.MakeBuilder<frc971::ProfileParameters>(); |
Austin Schuh | b39a21c | 2021-03-31 20:12:18 -0700 | [diff] [blame] | 309 | profile_params_builder.add_max_velocity(20.0); |
| 310 | profile_params_builder.add_max_acceleration(60.0); |
milind upadhyay | b9dec71 | 2021-03-20 15:47:51 -0700 | [diff] [blame] | 311 | flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset = |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 312 | profile_params_builder.Finish(); |
| 313 | intake_builder.add_unsafe_goal(intake_goal_); |
| 314 | intake_builder.add_profile_params(profile_params_offset); |
| 315 | intake_offset = intake_builder.Finish(); |
| 316 | } |
| 317 | |
| 318 | superstructure::Goal::Builder superstructure_builder = |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 319 | builder.MakeBuilder<superstructure::Goal>(); |
milind upadhyay | b9dec71 | 2021-03-20 15:47:51 -0700 | [diff] [blame] | 320 | |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 321 | superstructure_builder.add_intake(intake_offset); |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame^] | 322 | superstructure_builder.add_intake_preloading(true); |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 323 | superstructure_builder.add_roller_voltage(roller_voltage_); |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 324 | superstructure_builder.add_roller_speed_compensation( |
| 325 | kRollerSpeedCompensation); |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 326 | superstructure_builder.add_hood_tracking(shooter_tracking_); |
| 327 | superstructure_builder.add_turret_tracking(shooter_tracking_); |
| 328 | superstructure_builder.add_shooter_tracking(shooter_tracking_); |
| 329 | superstructure_builder.add_shooting(shooting_); |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 330 | |
| 331 | if (!builder.Send(superstructure_builder.Finish())) { |
| 332 | AOS_LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 333 | } |
Sabina Leaver | a0b43b4 | 2021-03-03 20:30:04 -0800 | [diff] [blame] | 334 | } |
milind upadhyay | 5e589d7 | 2021-03-27 13:47:18 -0700 | [diff] [blame] | 335 | |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 336 | void AutonomousActor::ExtendIntake() { |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame^] | 337 | set_intake_goal(1.30); |
| 338 | set_roller_voltage(6.0); |
Ravago Jones | a7b3c82 | 2021-08-26 12:36:03 -0700 | [diff] [blame] | 339 | SendSuperstructureGoal(); |
| 340 | } |
| 341 | |
milind upadhyay | 5e589d7 | 2021-03-27 13:47:18 -0700 | [diff] [blame] | 342 | void AutonomousActor::RetractIntake() { |
| 343 | set_intake_goal(-0.89); |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame^] | 344 | set_roller_voltage(6.0); |
milind upadhyay | 5e589d7 | 2021-03-27 13:47:18 -0700 | [diff] [blame] | 345 | SendSuperstructureGoal(); |
| 346 | } |
| 347 | |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame^] | 348 | int AutonomousActor::Balls() { |
Ravago Jones | 1f32d62 | 2021-08-26 12:20:36 -0700 | [diff] [blame] | 349 | superstructure_status_fetcher_.Fetch(); |
| 350 | CHECK(superstructure_status_fetcher_.get() != nullptr); |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame^] | 351 | return superstructure_status_fetcher_->shooter()->balls_shot(); |
| 352 | } |
Ravago Jones | 1f32d62 | 2021-08-26 12:20:36 -0700 | [diff] [blame] | 353 | |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame^] | 354 | bool AutonomousActor::WaitUntilAbsoluteBallsShot(int absolute_balls) { |
Ravago Jones | 1f32d62 | 2021-08-26 12:20:36 -0700 | [diff] [blame] | 355 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
| 356 | event_loop()->monotonic_now(), |
| 357 | frc971::controls::kLoopFrequency / 2); |
| 358 | while (true) { |
| 359 | if (ShouldCancel()) { |
| 360 | return false; |
| 361 | } |
| 362 | phased_loop.SleepUntilNext(); |
| 363 | superstructure_status_fetcher_.Fetch(); |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame^] | 364 | CHECK(superstructure_status_fetcher_.get() != nullptr); |
| 365 | if (superstructure_status_fetcher_->shooter()->balls_shot() >= |
| 366 | absolute_balls) { |
Ravago Jones | 1f32d62 | 2021-08-26 12:20:36 -0700 | [diff] [blame] | 367 | return true; |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
Austin Schuh | d0e9e06 | 2021-10-24 17:40:58 -0700 | [diff] [blame^] | 372 | bool AutonomousActor::WaitForBallsShot(int num_wanted) { |
| 373 | return WaitUntilAbsoluteBallsShot(Balls() + num_wanted); |
| 374 | } |
| 375 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 376 | } // namespace actors |
| 377 | } // namespace y2020 |