Comran Morshed | e68e373 | 2016-03-12 14:12:11 +0000 | [diff] [blame] | 1 | #include "y2016/actors/autonomous_actor.h" |
| 2 | |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 3 | #include <chrono> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 4 | #include <cinttypes> |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 5 | #include <cmath> |
| 6 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 7 | #include "aos/logging/logging.h" |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 8 | #include "aos/util/phased_loop.h" |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 9 | #include "y2016/control_loops/drivetrain/drivetrain_base.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 10 | #include "y2016/control_loops/shooter/shooter_goal_generated.h" |
| 11 | #include "y2016/control_loops/shooter/shooter_status_generated.h" |
| 12 | #include "y2016/control_loops/superstructure/superstructure_goal_generated.h" |
| 13 | #include "y2016/control_loops/superstructure/superstructure_status_generated.h" |
| 14 | #include "y2016/queues/ball_detector_generated.h" |
| 15 | #include "y2016/vision/vision_generated.h" |
Comran Morshed | e68e373 | 2016-03-12 14:12:11 +0000 | [diff] [blame] | 16 | |
| 17 | namespace y2016 { |
| 18 | namespace actors { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 19 | using ::aos::monotonic_clock; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 20 | using ::frc971::ProfileParametersT; |
| 21 | namespace superstructure = y2016::control_loops::superstructure; |
| 22 | namespace shooter = y2016::control_loops::shooter; |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 23 | namespace chrono = ::std::chrono; |
| 24 | namespace this_thread = ::std::this_thread; |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 25 | |
| 26 | namespace { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 27 | ProfileParametersT MakeProfileParameters(float max_velocity, |
| 28 | float max_acceleration) { |
| 29 | ProfileParametersT result; |
| 30 | result.max_velocity = max_velocity; |
| 31 | result.max_acceleration = max_acceleration; |
| 32 | return result; |
| 33 | } |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 34 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 35 | const ProfileParametersT kSlowDrive = MakeProfileParameters(0.8, 2.5); |
| 36 | const ProfileParametersT kLowBarDrive = MakeProfileParameters(1.3, 2.5); |
| 37 | const ProfileParametersT kMoatDrive = MakeProfileParameters(1.2, 3.5); |
| 38 | const ProfileParametersT kRealignDrive = MakeProfileParameters(2.0, 2.5); |
| 39 | const ProfileParametersT kRockWallDrive = MakeProfileParameters(0.8, 2.5); |
| 40 | const ProfileParametersT kFastDrive = MakeProfileParameters(3.0, 2.5); |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 41 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 42 | const ProfileParametersT kSlowTurn = MakeProfileParameters(0.8, 3.0); |
| 43 | const ProfileParametersT kFastTurn = MakeProfileParameters(3.0, 10.0); |
| 44 | const ProfileParametersT kStealTurn = MakeProfileParameters(4.0, 15.0); |
| 45 | const ProfileParametersT kSwerveTurn = MakeProfileParameters(2.0, 7.0); |
| 46 | const ProfileParametersT kFinishTurn = MakeProfileParameters(2.0, 5.0); |
| 47 | |
| 48 | const ProfileParametersT kTwoBallLowDrive = MakeProfileParameters(1.7, 3.5); |
| 49 | const ProfileParametersT kTwoBallFastDrive = MakeProfileParameters(3.0, 1.5); |
| 50 | const ProfileParametersT kTwoBallReturnDrive = MakeProfileParameters(3.0, 1.9); |
| 51 | const ProfileParametersT kTwoBallReturnSlow = MakeProfileParameters(3.0, 2.5); |
| 52 | const ProfileParametersT kTwoBallBallPickup = MakeProfileParameters(2.0, 1.75); |
| 53 | const ProfileParametersT kTwoBallBallPickupAccel = |
| 54 | MakeProfileParameters(2.0, 2.5); |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 55 | |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 56 | } // namespace |
Comran Morshed | e68e373 | 2016-03-12 14:12:11 +0000 | [diff] [blame] | 57 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 58 | AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop) |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 59 | : frc971::autonomous::BaseAutonomousActor( |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 60 | event_loop, control_loops::drivetrain::GetDrivetrainConfig()), |
| 61 | vision_align_actor_factory_( |
Austin Schuh | 28bde30 | 2019-05-26 22:24:33 -0700 | [diff] [blame] | 62 | actors::VisionAlignActor::MakeFactory(event_loop)), |
| 63 | vision_status_fetcher_( |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 64 | event_loop->MakeFetcher<::y2016::vision::VisionStatus>("/vision")), |
Austin Schuh | 4b652c9 | 2019-05-27 13:22:27 -0700 | [diff] [blame] | 65 | ball_detector_fetcher_( |
| 66 | event_loop->MakeFetcher<::y2016::sensors::BallDetector>( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 67 | "/superstructure")), |
Austin Schuh | ae023fb | 2019-06-29 17:11:45 -0700 | [diff] [blame] | 68 | shooter_goal_sender_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 69 | event_loop->MakeSender<::y2016::control_loops::shooter::Goal>( |
| 70 | "/shooter")), |
Austin Schuh | ae023fb | 2019-06-29 17:11:45 -0700 | [diff] [blame] | 71 | shooter_status_fetcher_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 72 | event_loop->MakeFetcher<::y2016::control_loops::shooter::Status>( |
| 73 | "/shooter")), |
Austin Schuh | 9481d0d | 2019-06-29 21:56:17 -0700 | [diff] [blame] | 74 | superstructure_status_fetcher_( |
Austin Schuh | 9481d0d | 2019-06-29 21:56:17 -0700 | [diff] [blame] | 75 | event_loop |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 76 | ->MakeFetcher<::y2016::control_loops::superstructure::Status>( |
| 77 | "/superstructure")), |
| 78 | superstructure_goal_sender_( |
| 79 | event_loop->MakeSender<::y2016::control_loops::superstructure::Goal>( |
| 80 | "/superstructure")) {} |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 81 | |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 82 | constexpr double kDoNotTurnCare = 2.0; |
| 83 | |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 84 | void AutonomousActor::MoveSuperstructure( |
| 85 | double intake, double shoulder, double wrist, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 86 | const ProfileParametersT intake_params, |
| 87 | const ProfileParametersT shoulder_params, |
| 88 | const ProfileParametersT wrist_params, bool traverse_up, |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 89 | double roller_power) { |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 90 | superstructure_goal_ = {intake, shoulder, wrist}; |
| 91 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 92 | auto builder = superstructure_goal_sender_.MakeBuilder(); |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 93 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 94 | superstructure::Goal::Builder superstructure_goal_builder = |
| 95 | builder.MakeBuilder<superstructure::Goal>(); |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 96 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 97 | superstructure_goal_builder.add_angle_intake(intake); |
| 98 | superstructure_goal_builder.add_angle_shoulder(shoulder); |
| 99 | superstructure_goal_builder.add_angle_wrist(wrist); |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 100 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 101 | superstructure_goal_builder.add_max_angular_velocity_intake( |
| 102 | intake_params.max_velocity); |
| 103 | superstructure_goal_builder.add_max_angular_velocity_shoulder( |
| 104 | shoulder_params.max_velocity); |
| 105 | superstructure_goal_builder.add_max_angular_velocity_wrist( |
| 106 | wrist_params.max_velocity); |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 107 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 108 | superstructure_goal_builder.add_max_angular_acceleration_intake( |
| 109 | intake_params.max_acceleration); |
| 110 | superstructure_goal_builder.add_max_angular_acceleration_shoulder( |
| 111 | shoulder_params.max_acceleration); |
| 112 | superstructure_goal_builder.add_max_angular_acceleration_wrist( |
| 113 | wrist_params.max_acceleration); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 114 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 115 | superstructure_goal_builder.add_voltage_top_rollers(roller_power); |
| 116 | superstructure_goal_builder.add_voltage_bottom_rollers(roller_power); |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 117 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 118 | superstructure_goal_builder.add_traverse_unlatched(true); |
| 119 | superstructure_goal_builder.add_traverse_down(!traverse_up); |
| 120 | superstructure_goal_builder.add_voltage_climber(0.0); |
| 121 | superstructure_goal_builder.add_unfold_climber(false); |
| 122 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 123 | if (builder.Send(superstructure_goal_builder.Finish()) != |
| 124 | aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 125 | AOS_LOG(ERROR, "Sending superstructure goal failed.\n"); |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 129 | void AutonomousActor::OpenShooter() { |
| 130 | shooter_speed_ = 0.0; |
| 131 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 132 | auto builder = shooter_goal_sender_.MakeBuilder(); |
| 133 | shooter::Goal::Builder shooter_goal_builder = |
| 134 | builder.MakeBuilder<shooter::Goal>(); |
| 135 | shooter_goal_builder.add_angular_velocity(shooter_speed_); |
| 136 | shooter_goal_builder.add_clamp_open(true); |
| 137 | shooter_goal_builder.add_push_to_shooter(false); |
| 138 | shooter_goal_builder.add_force_lights_on(false); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 139 | if (builder.Send(shooter_goal_builder.Finish()) != |
| 140 | aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 141 | AOS_LOG(ERROR, "Sending shooter goal failed.\n"); |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | void AutonomousActor::CloseShooter() { |
| 146 | shooter_speed_ = 0.0; |
| 147 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 148 | auto builder = shooter_goal_sender_.MakeBuilder(); |
| 149 | shooter::Goal::Builder shooter_goal_builder = |
| 150 | builder.MakeBuilder<shooter::Goal>(); |
| 151 | shooter_goal_builder.add_angular_velocity(shooter_speed_); |
| 152 | shooter_goal_builder.add_clamp_open(false); |
| 153 | shooter_goal_builder.add_push_to_shooter(false); |
| 154 | shooter_goal_builder.add_force_lights_on(false); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 155 | if (builder.Send(shooter_goal_builder.Finish()) != |
| 156 | aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 157 | AOS_LOG(ERROR, "Sending shooter goal failed.\n"); |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 161 | void AutonomousActor::SetShooterSpeed(double speed) { |
| 162 | shooter_speed_ = speed; |
| 163 | |
| 164 | // In auto, we want to have the lights on whenever possible since we have no |
| 165 | // hope of a human aligning the robot. |
| 166 | bool force_lights_on = shooter_speed_ > 1.0; |
| 167 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 168 | auto builder = shooter_goal_sender_.MakeBuilder(); |
| 169 | shooter::Goal::Builder shooter_goal_builder = |
| 170 | builder.MakeBuilder<shooter::Goal>(); |
| 171 | shooter_goal_builder.add_angular_velocity(shooter_speed_); |
| 172 | shooter_goal_builder.add_clamp_open(false); |
| 173 | shooter_goal_builder.add_push_to_shooter(false); |
| 174 | shooter_goal_builder.add_force_lights_on(force_lights_on); |
Austin Schuh | ae023fb | 2019-06-29 17:11:45 -0700 | [diff] [blame] | 175 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 176 | if (builder.Send(shooter_goal_builder.Finish()) != |
| 177 | aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 178 | AOS_LOG(ERROR, "Sending shooter goal failed.\n"); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | void AutonomousActor::Shoot() { |
| 183 | uint32_t initial_shots = 0; |
| 184 | |
Austin Schuh | ae023fb | 2019-06-29 17:11:45 -0700 | [diff] [blame] | 185 | shooter_status_fetcher_.Fetch(); |
| 186 | if (shooter_status_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 187 | initial_shots = shooter_status_fetcher_->shots(); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | // In auto, we want to have the lights on whenever possible since we have no |
| 191 | // hope of a human aligning the robot. |
| 192 | bool force_lights_on = shooter_speed_ > 1.0; |
| 193 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 194 | auto builder = shooter_goal_sender_.MakeBuilder(); |
| 195 | shooter::Goal::Builder shooter_goal_builder = |
| 196 | builder.MakeBuilder<shooter::Goal>(); |
| 197 | shooter_goal_builder.add_angular_velocity(shooter_speed_); |
| 198 | shooter_goal_builder.add_clamp_open(false); |
| 199 | shooter_goal_builder.add_push_to_shooter(true); |
| 200 | shooter_goal_builder.add_force_lights_on(force_lights_on); |
Austin Schuh | ae023fb | 2019-06-29 17:11:45 -0700 | [diff] [blame] | 201 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 202 | if (builder.Send(shooter_goal_builder.Finish()) != |
| 203 | aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 204 | AOS_LOG(ERROR, "Sending shooter goal failed.\n"); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 207 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 208 | event_loop()->monotonic_now(), |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 209 | ActorBase::kLoopOffset); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 210 | while (true) { |
| 211 | if (ShouldCancel()) return; |
| 212 | |
| 213 | // Wait for the shot count to change so we know when the shot is complete. |
Austin Schuh | ae023fb | 2019-06-29 17:11:45 -0700 | [diff] [blame] | 214 | shooter_status_fetcher_.Fetch(); |
| 215 | if (shooter_status_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 216 | if (initial_shots < shooter_status_fetcher_->shots()) { |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 217 | return; |
| 218 | } |
| 219 | } |
| 220 | phased_loop.SleepUntilNext(); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | void AutonomousActor::WaitForShooterSpeed() { |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 225 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 226 | event_loop()->monotonic_now(), |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 227 | ActorBase::kLoopOffset); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 228 | while (true) { |
| 229 | if (ShouldCancel()) return; |
| 230 | |
Austin Schuh | ae023fb | 2019-06-29 17:11:45 -0700 | [diff] [blame] | 231 | shooter_status_fetcher_.Fetch(); |
| 232 | if (shooter_status_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 233 | if (shooter_status_fetcher_->left()->ready() && |
| 234 | shooter_status_fetcher_->right()->ready()) { |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 235 | return; |
| 236 | } |
| 237 | } |
| 238 | phased_loop.SleepUntilNext(); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | void AutonomousActor::AlignWithVisionGoal() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 243 | vision_align_action::VisionAlignActionParamsT params; |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 244 | vision_action_ = vision_align_actor_factory_.Make(params); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 245 | vision_action_->Start(); |
| 246 | } |
| 247 | |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 248 | void AutonomousActor::WaitForAlignedWithVision( |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 249 | chrono::nanoseconds align_duration) { |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 250 | bool vision_valid = false; |
| 251 | double last_angle = 0.0; |
| 252 | int ready_to_fire = 0; |
| 253 | |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 254 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 255 | event_loop()->monotonic_now(), |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 256 | ActorBase::kLoopOffset); |
Austin Schuh | 77ed543 | 2019-07-07 20:41:36 -0700 | [diff] [blame] | 257 | const monotonic_clock::time_point end_time = monotonic_now() + align_duration; |
| 258 | while (end_time > monotonic_now()) { |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 259 | if (ShouldCancel()) break; |
| 260 | |
Austin Schuh | 28bde30 | 2019-05-26 22:24:33 -0700 | [diff] [blame] | 261 | vision_status_fetcher_.Fetch(); |
| 262 | if (vision_status_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 263 | vision_valid = (vision_status_fetcher_->left_image_valid() && |
| 264 | vision_status_fetcher_->right_image_valid()); |
| 265 | last_angle = vision_status_fetcher_->horizontal_angle(); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 268 | drivetrain_status_fetcher_.Fetch(); |
| 269 | drivetrain_goal_fetcher_.Fetch(); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 270 | |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 271 | if (drivetrain_status_fetcher_.get() && drivetrain_goal_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 272 | const double left_goal = drivetrain_goal_fetcher_->left_goal(); |
| 273 | const double right_goal = drivetrain_goal_fetcher_->right_goal(); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 274 | const double left_current = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 275 | drivetrain_status_fetcher_->estimated_left_position(); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 276 | const double right_current = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 277 | drivetrain_status_fetcher_->estimated_right_position(); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 278 | const double left_velocity = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 279 | drivetrain_status_fetcher_->estimated_left_velocity(); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 280 | const double right_velocity = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 281 | drivetrain_status_fetcher_->estimated_right_velocity(); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 282 | |
| 283 | if (vision_valid && ::std::abs(last_angle) < 0.02 && |
| 284 | ::std::abs((left_goal - right_goal) - |
| 285 | (left_current - right_current)) / |
| 286 | dt_config_.robot_radius / 2.0 < |
| 287 | 0.02 && |
| 288 | ::std::abs(left_velocity - right_velocity) < 0.01) { |
| 289 | ++ready_to_fire; |
| 290 | } else { |
| 291 | ready_to_fire = 0; |
| 292 | } |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 293 | if (ready_to_fire > 15) { |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 294 | break; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 295 | AOS_LOG(INFO, "Vision align success!\n"); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | phased_loop.SleepUntilNext(); |
| 299 | } |
| 300 | |
| 301 | vision_action_->Cancel(); |
| 302 | WaitUntilDoneOrCanceled(::std::move(vision_action_)); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 303 | AOS_LOG(INFO, "Done waiting for vision\n"); |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | bool AutonomousActor::IntakeDone() { |
Austin Schuh | 9481d0d | 2019-06-29 21:56:17 -0700 | [diff] [blame] | 307 | superstructure_status_fetcher_.Fetch(); |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 308 | |
| 309 | constexpr double kProfileError = 1e-5; |
| 310 | constexpr double kEpsilon = 0.15; |
| 311 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 312 | if (superstructure_status_fetcher_->state() < 12 || |
| 313 | superstructure_status_fetcher_->state() == 16) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 314 | AOS_LOG(ERROR, "Superstructure no longer running, aborting action\n"); |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 315 | return true; |
| 316 | } |
| 317 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 318 | if (::std::abs(superstructure_status_fetcher_->intake()->goal_angle() - |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 319 | superstructure_goal_.intake) < kProfileError && |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 320 | ::std::abs( |
| 321 | superstructure_status_fetcher_->intake()->goal_angular_velocity()) < |
| 322 | kProfileError) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 323 | AOS_LOG(DEBUG, "Profile done.\n"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 324 | if (::std::abs(superstructure_status_fetcher_->intake()->angle() - |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 325 | superstructure_goal_.intake) < kEpsilon && |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 326 | ::std::abs( |
| 327 | superstructure_status_fetcher_->intake()->angular_velocity()) < |
| 328 | kEpsilon) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 329 | AOS_LOG(INFO, "Near goal, done.\n"); |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 330 | return true; |
| 331 | } |
| 332 | } |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | bool AutonomousActor::SuperstructureProfileDone() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 337 | if (superstructure_status_fetcher_->state() < 12 || |
| 338 | superstructure_status_fetcher_->state() == 16) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 339 | AOS_LOG(ERROR, "Superstructure no longer running, aborting action\n"); |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 340 | return true; |
| 341 | } |
| 342 | |
Austin Schuh | 9481d0d | 2019-06-29 21:56:17 -0700 | [diff] [blame] | 343 | constexpr double kProfileError = 1e-5; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 344 | return ::std::abs(superstructure_status_fetcher_->intake()->goal_angle() - |
| 345 | superstructure_goal_.intake) < kProfileError && |
| 346 | ::std::abs(superstructure_status_fetcher_->shoulder()->goal_angle() - |
| 347 | superstructure_goal_.shoulder) < kProfileError && |
| 348 | ::std::abs(superstructure_status_fetcher_->wrist()->goal_angle() - |
| 349 | superstructure_goal_.wrist) < kProfileError && |
| 350 | ::std::abs(superstructure_status_fetcher_->intake() |
| 351 | ->goal_angular_velocity()) < kProfileError && |
| 352 | ::std::abs(superstructure_status_fetcher_->shoulder() |
| 353 | ->goal_angular_velocity()) < kProfileError && |
Austin Schuh | 9481d0d | 2019-06-29 21:56:17 -0700 | [diff] [blame] | 354 | ::std::abs( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 355 | superstructure_status_fetcher_->wrist()->goal_angular_velocity()) < |
| 356 | kProfileError; |
Austin Schuh | 9481d0d | 2019-06-29 21:56:17 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | bool AutonomousActor::SuperstructureDone() { |
| 360 | superstructure_status_fetcher_.Fetch(); |
| 361 | |
| 362 | constexpr double kEpsilon = 0.03; |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 363 | if (SuperstructureProfileDone()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 364 | AOS_LOG(DEBUG, "Profile done.\n"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 365 | if (::std::abs(superstructure_status_fetcher_->intake()->angle() - |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 366 | superstructure_goal_.intake) < (kEpsilon + 0.1) && |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 367 | ::std::abs(superstructure_status_fetcher_->shoulder()->angle() - |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 368 | superstructure_goal_.shoulder) < (kEpsilon + 0.05) && |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 369 | ::std::abs(superstructure_status_fetcher_->wrist()->angle() - |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 370 | superstructure_goal_.wrist) < (kEpsilon + 0.01) && |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 371 | ::std::abs( |
| 372 | superstructure_status_fetcher_->intake()->angular_velocity()) < |
| 373 | (kEpsilon + 0.1) && |
| 374 | ::std::abs( |
| 375 | superstructure_status_fetcher_->shoulder()->angular_velocity()) < |
| 376 | (kEpsilon + 0.10) && |
| 377 | ::std::abs( |
| 378 | superstructure_status_fetcher_->wrist()->angular_velocity()) < |
| 379 | (kEpsilon + 0.05)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 380 | AOS_LOG(INFO, "Near goal, done.\n"); |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 381 | return true; |
| 382 | } |
| 383 | } |
| 384 | return false; |
| 385 | } |
| 386 | |
| 387 | void AutonomousActor::WaitForIntake() { |
Austin Schuh | 9481d0d | 2019-06-29 21:56:17 -0700 | [diff] [blame] | 388 | WaitUntil(::std::bind(&AutonomousActor::IntakeDone, this)); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 391 | void AutonomousActor::WaitForSuperstructure() { |
Austin Schuh | 9481d0d | 2019-06-29 21:56:17 -0700 | [diff] [blame] | 392 | WaitUntil(::std::bind(&AutonomousActor::SuperstructureDone, this)); |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 393 | } |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 394 | |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 395 | void AutonomousActor::WaitForSuperstructureProfile() { |
Austin Schuh | 9481d0d | 2019-06-29 21:56:17 -0700 | [diff] [blame] | 396 | WaitUntil([this]() { |
| 397 | superstructure_status_fetcher_.Fetch(); |
| 398 | return SuperstructureProfileDone(); |
| 399 | }); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 402 | void AutonomousActor::WaitForSuperstructureLow() { |
Austin Schuh | 9481d0d | 2019-06-29 21:56:17 -0700 | [diff] [blame] | 403 | WaitUntil([this]() { |
| 404 | superstructure_status_fetcher_.Fetch(); |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 405 | |
Austin Schuh | 9481d0d | 2019-06-29 21:56:17 -0700 | [diff] [blame] | 406 | return SuperstructureProfileDone() || |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 407 | superstructure_status_fetcher_->shoulder()->angle() < 0.1; |
Austin Schuh | 9481d0d | 2019-06-29 21:56:17 -0700 | [diff] [blame] | 408 | }); |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 409 | } |
Austin Schuh | 9481d0d | 2019-06-29 21:56:17 -0700 | [diff] [blame] | 410 | |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 411 | void AutonomousActor::BackLongShotLowBarTwoBall() { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 412 | AOS_LOG(INFO, "Expanding for back long shot\n"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 413 | MoveSuperstructure(0.00, M_PI / 2.0 - 0.2, -0.55, |
| 414 | MakeProfileParameters(7.0, 40.0), |
| 415 | MakeProfileParameters(4.0, 6.0), |
| 416 | MakeProfileParameters(10.0, 25.0), false, 0.0); |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | void AutonomousActor::BackLongShotTwoBall() { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 420 | AOS_LOG(INFO, "Expanding for back long shot\n"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 421 | MoveSuperstructure(0.00, M_PI / 2.0 - 0.2, -0.55, |
| 422 | MakeProfileParameters(7.0, 40.0), |
| 423 | MakeProfileParameters(4.0, 6.0), |
| 424 | MakeProfileParameters(10.0, 25.0), false, 0.0); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | void AutonomousActor::BackLongShotTwoBallFinish() { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 428 | AOS_LOG(INFO, "Expanding for back long shot\n"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 429 | MoveSuperstructure(0.00, M_PI / 2.0 - 0.2, -0.625 + 0.03, |
| 430 | MakeProfileParameters(7.0, 40.0), |
| 431 | MakeProfileParameters(4.0, 6.0), |
| 432 | MakeProfileParameters(10.0, 25.0), false, 0.0); |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 435 | void AutonomousActor::BackLongShot() { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 436 | AOS_LOG(INFO, "Expanding for back long shot\n"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 437 | MoveSuperstructure(0.80, M_PI / 2.0 - 0.2, -0.62, |
| 438 | MakeProfileParameters(7.0, 40.0), |
| 439 | MakeProfileParameters(4.0, 6.0), |
| 440 | MakeProfileParameters(10.0, 25.0), false, 0.0); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | void AutonomousActor::BackMiddleShot() { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 444 | AOS_LOG(INFO, "Expanding for back middle shot\n"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 445 | MoveSuperstructure(-0.05, M_PI / 2.0 - 0.2, -0.665, |
| 446 | MakeProfileParameters(7.0, 40.0), |
| 447 | MakeProfileParameters(4.0, 10.0), |
| 448 | MakeProfileParameters(10.0, 25.0), false, 0.0); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 451 | void AutonomousActor::FrontLongShot() { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 452 | AOS_LOG(INFO, "Expanding for front long shot\n"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 453 | MoveSuperstructure(0.80, M_PI / 2.0 + 0.1, M_PI + 0.41 + 0.02, |
| 454 | MakeProfileParameters(7.0, 40.0), |
| 455 | MakeProfileParameters(4.0, 6.0), |
| 456 | MakeProfileParameters(10.0, 25.0), false, 0.0); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | void AutonomousActor::FrontMiddleShot() { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 460 | AOS_LOG(INFO, "Expanding for front middle shot\n"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 461 | MoveSuperstructure(-0.05, M_PI / 2.0 + 0.1, M_PI + 0.44, |
| 462 | MakeProfileParameters(7.0, 40.0), |
| 463 | MakeProfileParameters(4.0, 10.0), |
| 464 | MakeProfileParameters(10.0, 25.0), true, 0.0); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 467 | void AutonomousActor::TuckArm(bool low_bar, bool traverse_down) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 468 | MoveSuperstructure(low_bar ? -0.05 : 2.0, -0.010, 0.0, |
| 469 | MakeProfileParameters(7.0, 40.0), |
| 470 | MakeProfileParameters(4.0, 10.0), |
| 471 | MakeProfileParameters(10.0, 25.0), !traverse_down, 0.0); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 474 | void AutonomousActor::DoFullShot() { |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 475 | if (ShouldCancel()) return; |
| 476 | // Make sure that the base is aligned with the base. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 477 | AOS_LOG(INFO, "Waiting for the superstructure\n"); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 478 | WaitForSuperstructure(); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 479 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 480 | this_thread::sleep_for(chrono::milliseconds(500)); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 481 | |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 482 | if (ShouldCancel()) return; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 483 | AOS_LOG(INFO, "Triggering the vision actor\n"); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 484 | AlignWithVisionGoal(); |
| 485 | |
| 486 | // Wait for the drive base to be aligned with the target and make sure that |
| 487 | // the shooter is up to speed. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 488 | AOS_LOG(INFO, "Waiting for vision to be aligned\n"); |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 489 | WaitForAlignedWithVision(chrono::milliseconds(2000)); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 490 | if (ShouldCancel()) return; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 491 | AOS_LOG(INFO, "Waiting for shooter to be up to speed\n"); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 492 | WaitForShooterSpeed(); |
| 493 | if (ShouldCancel()) return; |
| 494 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 495 | this_thread::sleep_for(chrono::milliseconds(300)); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 496 | AOS_LOG(INFO, "Shoot!\n"); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 497 | Shoot(); |
| 498 | |
| 499 | // Turn off the shooter and fold up the superstructure. |
| 500 | if (ShouldCancel()) return; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 501 | AOS_LOG(INFO, "Stopping shooter\n"); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 502 | SetShooterSpeed(0.0); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 503 | AOS_LOG(INFO, "Folding superstructure back down\n"); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 504 | TuckArm(false, false); |
| 505 | |
| 506 | // Wait for everything to be folded up. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 507 | AOS_LOG(INFO, "Waiting for superstructure to be folded back down\n"); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 508 | WaitForSuperstructureLow(); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | void AutonomousActor::LowBarDrive() { |
| 512 | TuckArm(false, true); |
| 513 | StartDrive(-5.5, 0.0, kLowBarDrive, kSlowTurn); |
| 514 | |
| 515 | if (!WaitForDriveNear(5.3, 0.0)) return; |
| 516 | TuckArm(true, true); |
| 517 | |
| 518 | if (!WaitForDriveNear(5.0, 0.0)) return; |
| 519 | |
| 520 | StartDrive(0.0, 0.0, kLowBarDrive, kSlowTurn); |
| 521 | |
| 522 | if (!WaitForDriveNear(3.0, 0.0)) return; |
| 523 | |
| 524 | StartDrive(0.0, 0.0, kLowBarDrive, kSlowTurn); |
| 525 | |
| 526 | if (!WaitForDriveNear(1.0, 0.0)) return; |
| 527 | |
Austin Schuh | 15b5f6a | 2016-03-26 19:43:56 -0700 | [diff] [blame] | 528 | StartDrive(0, -M_PI / 4.0 - 0.2, kLowBarDrive, kSlowTurn); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 531 | void AutonomousActor::TippyDrive(double goal_distance, double tip_distance, |
| 532 | double below, double above) { |
| 533 | StartDrive(goal_distance, 0.0, kMoatDrive, kSlowTurn); |
| 534 | if (!WaitForBelowAngle(below)) return; |
| 535 | if (!WaitForAboveAngle(above)) return; |
| 536 | // Ok, we are good now. Compensate by moving the goal by the error. |
| 537 | // Should be here at 2.7 |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 538 | drivetrain_status_fetcher_.Fetch(); |
| 539 | if (drivetrain_status_fetcher_.get()) { |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 540 | const double left_error = |
| 541 | (initial_drivetrain_.left - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 542 | drivetrain_status_fetcher_->estimated_left_position()); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 543 | const double right_error = |
| 544 | (initial_drivetrain_.right - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 545 | drivetrain_status_fetcher_->estimated_right_position()); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 546 | const double distance_to_go = (left_error + right_error) / 2.0; |
| 547 | const double distance_compensation = |
| 548 | goal_distance - tip_distance - distance_to_go; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 549 | AOS_LOG(INFO, "Going %f further at the bump\n", distance_compensation); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 550 | StartDrive(distance_compensation, 0.0, kMoatDrive, kSlowTurn); |
| 551 | } |
| 552 | } |
| 553 | |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 554 | void AutonomousActor::MiddleDrive() { |
| 555 | TuckArm(false, false); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 556 | TippyDrive(3.65, 2.7, -0.2, 0.0); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 557 | if (!WaitForDriveDone()) return; |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | void AutonomousActor::OneFromMiddleDrive(bool left) { |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 561 | const double kTurnAngle = left ? -0.41 : 0.41; |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 562 | TuckArm(false, false); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 563 | TippyDrive(4.05, 2.7, -0.2, 0.0); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 564 | |
| 565 | if (!WaitForDriveDone()) return; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 566 | StartDrive(0.0, kTurnAngle, kRealignDrive, kFastTurn); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | void AutonomousActor::TwoFromMiddleDrive() { |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 570 | TuckArm(false, false); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 571 | constexpr double kDriveDistance = 5.10; |
| 572 | TippyDrive(kDriveDistance, 2.7, -0.2, 0.0); |
| 573 | |
| 574 | if (!WaitForDriveNear(kDriveDistance - 3.0, 2.0)) return; |
| 575 | StartDrive(0, -M_PI / 2 - 0.10, kMoatDrive, kFastTurn); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 576 | |
| 577 | if (!WaitForDriveDone()) return; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 578 | StartDrive(0, M_PI / 3 + 0.35, kMoatDrive, kFastTurn); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 579 | } |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 580 | |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 581 | void AutonomousActor::CloseIfBall() { |
Austin Schuh | 4b652c9 | 2019-05-27 13:22:27 -0700 | [diff] [blame] | 582 | ball_detector_fetcher_.Fetch(); |
| 583 | if (ball_detector_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 584 | const bool ball_detected = ball_detector_fetcher_->voltage() > 2.5; |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 585 | if (ball_detected) { |
| 586 | CloseShooter(); |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 591 | void AutonomousActor::WaitForBallOrDriveDone() { |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 592 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 593 | event_loop()->monotonic_now(), |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 594 | ActorBase::kLoopOffset); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 595 | while (true) { |
| 596 | if (ShouldCancel()) { |
| 597 | return; |
| 598 | } |
| 599 | phased_loop.SleepUntilNext(); |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 600 | drivetrain_status_fetcher_.Fetch(); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 601 | if (IsDriveDone()) { |
| 602 | return; |
| 603 | } |
| 604 | |
Austin Schuh | 4b652c9 | 2019-05-27 13:22:27 -0700 | [diff] [blame] | 605 | ball_detector_fetcher_.Fetch(); |
| 606 | if (ball_detector_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 607 | const bool ball_detected = ball_detector_fetcher_->voltage() > 2.5; |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 608 | if (ball_detected) { |
| 609 | return; |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 615 | void AutonomousActor::TwoBallAuto() { |
Austin Schuh | 77ed543 | 2019-07-07 20:41:36 -0700 | [diff] [blame] | 616 | const monotonic_clock::time_point start_time = monotonic_now(); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 617 | OpenShooter(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 618 | MoveSuperstructure(0.10, -0.010, 0.0, MakeProfileParameters(8.0, 60.0), |
| 619 | MakeProfileParameters(4.0, 10.0), |
| 620 | MakeProfileParameters(10.0, 25.0), false, 12.0); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 621 | if (ShouldCancel()) return; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 622 | AOS_LOG(INFO, "Waiting for the intake to come down.\n"); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 623 | |
| 624 | WaitForIntake(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 625 | AOS_LOG(INFO, "Intake done at %f seconds, starting to drive\n", |
| 626 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 627 | if (ShouldCancel()) return; |
| 628 | const double kDriveDistance = 5.05; |
| 629 | StartDrive(-kDriveDistance, 0.0, kTwoBallLowDrive, kSlowTurn); |
| 630 | |
| 631 | StartDrive(0.0, 0.4, kTwoBallLowDrive, kSwerveTurn); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 632 | if (!WaitForDriveNear(kDriveDistance - 0.5, kDoNotTurnCare)) return; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 633 | |
Austin Schuh | 295c2d9 | 2016-05-01 12:28:04 -0700 | [diff] [blame] | 634 | // Check if the ball is there. |
| 635 | bool first_ball_there = true; |
Austin Schuh | 4b652c9 | 2019-05-27 13:22:27 -0700 | [diff] [blame] | 636 | ball_detector_fetcher_.Fetch(); |
| 637 | if (ball_detector_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 638 | const bool ball_detected = ball_detector_fetcher_->voltage() > 2.5; |
Austin Schuh | 295c2d9 | 2016-05-01 12:28:04 -0700 | [diff] [blame] | 639 | first_ball_there = ball_detected; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 640 | AOS_LOG(INFO, "Saw the ball: %d at %f\n", first_ball_there, |
| 641 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | 295c2d9 | 2016-05-01 12:28:04 -0700 | [diff] [blame] | 642 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 643 | MoveSuperstructure(0.10, -0.010, 0.0, MakeProfileParameters(8.0, 40.0), |
| 644 | MakeProfileParameters(4.0, 10.0), |
| 645 | MakeProfileParameters(10.0, 25.0), false, 0.0); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 646 | AOS_LOG(INFO, |
| 647 | "Shutting off rollers at %f seconds, starting to straighten out\n", |
| 648 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 649 | StartDrive(0.0, -0.4, kTwoBallLowDrive, kSwerveTurn); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 650 | MoveSuperstructure(-0.05, -0.010, 0.0, MakeProfileParameters(8.0, 40.0), |
| 651 | MakeProfileParameters(4.0, 10.0), |
| 652 | MakeProfileParameters(10.0, 25.0), false, 0.0); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 653 | CloseShooter(); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 654 | if (!WaitForDriveNear(kDriveDistance - 2.4, kDoNotTurnCare)) return; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 655 | |
| 656 | // We are now under the low bar. Start lifting. |
| 657 | BackLongShotLowBarTwoBall(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 658 | AOS_LOG(INFO, "Spinning up the shooter wheels\n"); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 659 | SetShooterSpeed(640.0); |
| 660 | StartDrive(0.0, 0.0, kTwoBallFastDrive, kSwerveTurn); |
| 661 | |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 662 | if (!WaitForDriveNear(1.50, kDoNotTurnCare)) return; |
| 663 | constexpr double kShootTurnAngle = -M_PI / 4.0 - 0.05; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 664 | StartDrive(0, kShootTurnAngle, kTwoBallFastDrive, kFinishTurn); |
| 665 | BackLongShotTwoBall(); |
| 666 | |
| 667 | if (!WaitForDriveDone()) return; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 668 | AOS_LOG(INFO, "First shot done driving at %f seconds\n", |
| 669 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 670 | |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 671 | WaitForSuperstructureProfile(); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 672 | |
| 673 | if (ShouldCancel()) return; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 674 | AlignWithVisionGoal(); |
| 675 | |
| 676 | WaitForShooterSpeed(); |
| 677 | if (ShouldCancel()) return; |
| 678 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 679 | constexpr chrono::milliseconds kVisionExtra{0}; |
| 680 | WaitForAlignedWithVision(chrono::milliseconds(500) + kVisionExtra); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 681 | BackLongShotTwoBallFinish(); |
| 682 | WaitForSuperstructureProfile(); |
| 683 | if (ShouldCancel()) return; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 684 | AOS_LOG(INFO, "Shoot!\n"); |
Austin Schuh | 295c2d9 | 2016-05-01 12:28:04 -0700 | [diff] [blame] | 685 | if (first_ball_there) { |
| 686 | Shoot(); |
| 687 | } else { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 688 | AOS_LOG(INFO, "Nah, not shooting\n"); |
Austin Schuh | 295c2d9 | 2016-05-01 12:28:04 -0700 | [diff] [blame] | 689 | } |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 690 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 691 | AOS_LOG(INFO, "First shot at %f seconds\n", |
| 692 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 693 | if (ShouldCancel()) return; |
| 694 | |
| 695 | SetShooterSpeed(0.0); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 696 | AOS_LOG(INFO, "Folding superstructure back down\n"); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 697 | TuckArm(true, true); |
| 698 | |
| 699 | // Undo vision move. |
| 700 | StartDrive(0.0, 0.0, kTwoBallFastDrive, kFinishTurn); |
| 701 | if (!WaitForDriveDone()) return; |
| 702 | |
| 703 | constexpr double kBackDrive = 3.09 - 0.4; |
| 704 | StartDrive(kBackDrive, 0.0, kTwoBallReturnDrive, kSlowTurn); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 705 | if (!WaitForDriveNear(kBackDrive - 0.19, kDoNotTurnCare)) return; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 706 | StartDrive(0, -kShootTurnAngle, kTwoBallReturnDrive, kSwerveTurn); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 707 | if (!WaitForDriveNear(1.0, kDoNotTurnCare)) return; |
| 708 | StartDrive(0, 0, kTwoBallReturnSlow, kSwerveTurn); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 709 | |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 710 | if (!WaitForDriveNear(0.06, kDoNotTurnCare)) return; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 711 | AOS_LOG(INFO, "At Low Bar %f\n", |
| 712 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 713 | |
| 714 | OpenShooter(); |
| 715 | constexpr double kSecondBallAfterBarDrive = 2.10; |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 716 | StartDrive(kSecondBallAfterBarDrive, 0.0, kTwoBallBallPickupAccel, kSlowTurn); |
| 717 | if (!WaitForDriveNear(kSecondBallAfterBarDrive - 0.5, kDoNotTurnCare)) return; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 718 | constexpr double kBallSmallWallTurn = -0.11; |
| 719 | StartDrive(0, kBallSmallWallTurn, kTwoBallBallPickup, kFinishTurn); |
| 720 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 721 | MoveSuperstructure(0.03, -0.010, 0.0, MakeProfileParameters(8.0, 60.0), |
| 722 | MakeProfileParameters(4.0, 10.0), |
| 723 | MakeProfileParameters(10.0, 25.0), false, 12.0); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 724 | |
| 725 | if (!WaitForDriveProfileDone()) return; |
| 726 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 727 | MoveSuperstructure(0.10, -0.010, 0.0, MakeProfileParameters(8.0, 60.0), |
| 728 | MakeProfileParameters(4.0, 10.0), |
| 729 | MakeProfileParameters(10.0, 25.0), false, 12.0); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 730 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 731 | AOS_LOG(INFO, "Done backing up %f\n", |
| 732 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 733 | |
| 734 | constexpr double kDriveBackDistance = 5.15 - 0.4; |
| 735 | StartDrive(-kDriveBackDistance, 0.0, kTwoBallLowDrive, kFinishTurn); |
| 736 | CloseIfBall(); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 737 | if (!WaitForDriveNear(kDriveBackDistance - 0.75, kDoNotTurnCare)) return; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 738 | |
| 739 | StartDrive(0.0, -kBallSmallWallTurn, kTwoBallLowDrive, kFinishTurn); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 740 | AOS_LOG(INFO, "Straightening up at %f\n", |
| 741 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 742 | |
| 743 | CloseIfBall(); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 744 | if (!WaitForDriveNear(kDriveBackDistance - 2.3, kDoNotTurnCare)) return; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 745 | |
Austin Schuh | 4b652c9 | 2019-05-27 13:22:27 -0700 | [diff] [blame] | 746 | ball_detector_fetcher_.Fetch(); |
| 747 | if (ball_detector_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 748 | const bool ball_detected = ball_detector_fetcher_->voltage() > 2.5; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 749 | if (!ball_detected) { |
| 750 | if (!WaitForDriveDone()) return; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 751 | AOS_LOG(INFO, "Aborting, no ball %f\n", |
| 752 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 753 | return; |
| 754 | } |
| 755 | } |
| 756 | CloseShooter(); |
| 757 | |
| 758 | BackLongShotLowBarTwoBall(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 759 | AOS_LOG(INFO, "Spinning up the shooter wheels\n"); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 760 | SetShooterSpeed(640.0); |
| 761 | StartDrive(0.0, 0.0, kTwoBallFastDrive, kSwerveTurn); |
| 762 | |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 763 | if (!WaitForDriveNear(1.80, kDoNotTurnCare)) return; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 764 | StartDrive(0, kShootTurnAngle, kTwoBallFastDrive, kFinishTurn); |
| 765 | BackLongShotTwoBall(); |
| 766 | |
| 767 | if (!WaitForDriveDone()) return; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 768 | AOS_LOG(INFO, "Second shot done driving at %f seconds\n", |
| 769 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 770 | WaitForSuperstructure(); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 771 | AlignWithVisionGoal(); |
| 772 | if (ShouldCancel()) return; |
| 773 | |
| 774 | WaitForShooterSpeed(); |
| 775 | if (ShouldCancel()) return; |
| 776 | |
| 777 | // 2.2 with 0.4 of vision. |
| 778 | // 1.8 without any vision. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 779 | AOS_LOG(INFO, "Going to vision align at %f\n", |
| 780 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 781 | WaitForAlignedWithVision( |
| 782 | (start_time + chrono::milliseconds(13500) + kVisionExtra * 2) - |
Austin Schuh | 77ed543 | 2019-07-07 20:41:36 -0700 | [diff] [blame] | 783 | monotonic_now()); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 784 | BackLongShotTwoBallFinish(); |
| 785 | WaitForSuperstructureProfile(); |
| 786 | if (ShouldCancel()) return; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 787 | AOS_LOG(INFO, "Shoot at %f\n", |
| 788 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 789 | Shoot(); |
| 790 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 791 | AOS_LOG(INFO, "Second shot at %f seconds\n", |
| 792 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 793 | if (ShouldCancel()) return; |
| 794 | |
| 795 | SetShooterSpeed(0.0); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 796 | AOS_LOG(INFO, "Folding superstructure back down\n"); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 797 | TuckArm(true, false); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 798 | AOS_LOG(INFO, "Shot %f\n", |
| 799 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 800 | |
| 801 | WaitForSuperstructureLow(); |
| 802 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 803 | AOS_LOG(INFO, "Done %f\n", |
| 804 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 805 | } |
| 806 | |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 807 | void AutonomousActor::StealAndMoveOverBy(double distance) { |
| 808 | OpenShooter(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 809 | MoveSuperstructure(0.10, -0.010, 0.0, MakeProfileParameters(8.0, 60.0), |
| 810 | MakeProfileParameters(4.0, 10.0), |
| 811 | MakeProfileParameters(10.0, 25.0), true, 12.0); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 812 | if (ShouldCancel()) return; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 813 | AOS_LOG(INFO, "Waiting for the intake to come down.\n"); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 814 | |
| 815 | WaitForIntake(); |
| 816 | if (ShouldCancel()) return; |
| 817 | StartDrive(-distance, M_PI / 2.0, kFastDrive, kStealTurn); |
| 818 | WaitForBallOrDriveDone(); |
| 819 | if (ShouldCancel()) return; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 820 | MoveSuperstructure(1.0, -0.010, 0.0, MakeProfileParameters(8.0, 60.0), |
| 821 | MakeProfileParameters(4.0, 10.0), |
| 822 | MakeProfileParameters(10.0, 25.0), true, 12.0); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 823 | |
| 824 | if (!WaitForDriveDone()) return; |
| 825 | StartDrive(0.0, M_PI / 2.0, kFastDrive, kStealTurn); |
| 826 | if (!WaitForDriveDone()) return; |
| 827 | } |
| 828 | |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 829 | bool AutonomousActor::RunAction( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 830 | const ::frc971::autonomous::AutonomousActionParams *params) { |
Austin Schuh | 77ed543 | 2019-07-07 20:41:36 -0700 | [diff] [blame] | 831 | monotonic_clock::time_point start_time = monotonic_now(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 832 | AOS_LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 833 | params->mode()); |
Comran Morshed | e68e373 | 2016-03-12 14:12:11 +0000 | [diff] [blame] | 834 | |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 835 | InitializeEncoders(); |
| 836 | ResetDrivetrain(); |
| 837 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 838 | switch (params->mode()) { |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 839 | case 0: |
| 840 | LowBarDrive(); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 841 | if (!WaitForDriveDone()) return true; |
| 842 | // Get the superstructure to unfold and get ready for shooting. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 843 | AOS_LOG(INFO, "Unfolding superstructure\n"); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 844 | FrontLongShot(); |
| 845 | |
| 846 | // Spin up the shooter wheels. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 847 | AOS_LOG(INFO, "Spinning up the shooter wheels\n"); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 848 | SetShooterSpeed(640.0); |
| 849 | |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 850 | break; |
| 851 | case 1: |
| 852 | TwoFromMiddleDrive(); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 853 | if (!WaitForDriveDone()) return true; |
| 854 | // Get the superstructure to unfold and get ready for shooting. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 855 | AOS_LOG(INFO, "Unfolding superstructure\n"); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 856 | FrontMiddleShot(); |
| 857 | |
| 858 | // Spin up the shooter wheels. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 859 | AOS_LOG(INFO, "Spinning up the shooter wheels\n"); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 860 | SetShooterSpeed(600.0); |
| 861 | |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 862 | break; |
| 863 | case 2: |
| 864 | OneFromMiddleDrive(true); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 865 | if (!WaitForDriveDone()) return true; |
| 866 | // Get the superstructure to unfold and get ready for shooting. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 867 | AOS_LOG(INFO, "Unfolding superstructure\n"); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 868 | FrontMiddleShot(); |
| 869 | |
| 870 | // Spin up the shooter wheels. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 871 | AOS_LOG(INFO, "Spinning up the shooter wheels\n"); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 872 | SetShooterSpeed(600.0); |
| 873 | |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 874 | break; |
| 875 | case 3: |
| 876 | MiddleDrive(); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 877 | if (!WaitForDriveDone()) return true; |
| 878 | // Get the superstructure to unfold and get ready for shooting. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 879 | AOS_LOG(INFO, "Unfolding superstructure\n"); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 880 | FrontMiddleShot(); |
| 881 | |
| 882 | // Spin up the shooter wheels. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 883 | AOS_LOG(INFO, "Spinning up the shooter wheels\n"); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 884 | SetShooterSpeed(600.0); |
| 885 | |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 886 | break; |
| 887 | case 4: |
| 888 | OneFromMiddleDrive(false); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 889 | if (!WaitForDriveDone()) return true; |
| 890 | // Get the superstructure to unfold and get ready for shooting. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 891 | AOS_LOG(INFO, "Unfolding superstructure\n"); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 892 | FrontMiddleShot(); |
| 893 | |
| 894 | // Spin up the shooter wheels. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 895 | AOS_LOG(INFO, "Spinning up the shooter wheels\n"); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 896 | SetShooterSpeed(600.0); |
| 897 | |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 898 | break; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 899 | case 5: |
Campbell Crowley | 9ed61a5 | 2016-11-05 17:13:07 -0700 | [diff] [blame] | 900 | case 15: |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 901 | TwoBallAuto(); |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 902 | return true; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 903 | break; |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 904 | case 6: |
| 905 | StealAndMoveOverBy(3.10 + 2 * 52 * 2.54 / 100.0); |
| 906 | if (ShouldCancel()) return true; |
| 907 | |
| 908 | TwoFromMiddleDrive(); |
| 909 | if (!WaitForDriveDone()) return true; |
| 910 | // Get the superstructure to unfold and get ready for shooting. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 911 | AOS_LOG(INFO, "Unfolding superstructure\n"); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 912 | FrontMiddleShot(); |
| 913 | |
| 914 | // Spin up the shooter wheels. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 915 | AOS_LOG(INFO, "Spinning up the shooter wheels\n"); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 916 | SetShooterSpeed(600.0); |
| 917 | |
| 918 | break; |
| 919 | case 7: |
| 920 | StealAndMoveOverBy(2.95 + 52 * 2.54 / 100.0); |
| 921 | if (ShouldCancel()) return true; |
| 922 | |
| 923 | OneFromMiddleDrive(true); |
| 924 | if (!WaitForDriveDone()) return true; |
| 925 | // Get the superstructure to unfold and get ready for shooting. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 926 | AOS_LOG(INFO, "Unfolding superstructure\n"); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 927 | FrontMiddleShot(); |
| 928 | |
| 929 | // Spin up the shooter wheels. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 930 | AOS_LOG(INFO, "Spinning up the shooter wheels\n"); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 931 | SetShooterSpeed(600.0); |
| 932 | |
| 933 | break; |
| 934 | case 8: { |
| 935 | StealAndMoveOverBy(2.95); |
| 936 | if (ShouldCancel()) return true; |
| 937 | |
| 938 | MiddleDrive(); |
| 939 | if (!WaitForDriveDone()) return true; |
| 940 | // Get the superstructure to unfold and get ready for shooting. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 941 | AOS_LOG(INFO, "Unfolding superstructure\n"); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 942 | FrontMiddleShot(); |
| 943 | |
| 944 | // Spin up the shooter wheels. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 945 | AOS_LOG(INFO, "Spinning up the shooter wheels\n"); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 946 | SetShooterSpeed(600.0); |
| 947 | |
| 948 | } break; |
| 949 | case 9: { |
| 950 | StealAndMoveOverBy(1.70); |
| 951 | if (ShouldCancel()) return true; |
| 952 | |
| 953 | OneFromMiddleDrive(false); |
| 954 | if (!WaitForDriveDone()) return true; |
| 955 | // Get the superstructure to unfold and get ready for shooting. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 956 | AOS_LOG(INFO, "Unfolding superstructure\n"); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 957 | FrontMiddleShot(); |
| 958 | |
| 959 | // Spin up the shooter wheels. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 960 | AOS_LOG(INFO, "Spinning up the shooter wheels\n"); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 961 | SetShooterSpeed(600.0); |
| 962 | |
| 963 | } break; |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 964 | default: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 965 | AOS_LOG(ERROR, "Invalid auto mode %d\n", params->mode()); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 966 | return true; |
| 967 | } |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 968 | |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 969 | DoFullShot(); |
| 970 | |
| 971 | StartDrive(0.5, 0.0, kMoatDrive, kFastTurn); |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 972 | if (!WaitForDriveDone()) return true; |
| 973 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 974 | AOS_LOG(INFO, "Done %f\n", |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 975 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 976 | |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 977 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
| 978 | monotonic_now(), ActorBase::kLoopOffset); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 979 | |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 980 | while (!ShouldCancel()) { |
| 981 | phased_loop.SleepUntilNext(); |
Comran Morshed | e68e373 | 2016-03-12 14:12:11 +0000 | [diff] [blame] | 982 | } |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 983 | AOS_LOG(DEBUG, "Done running\n"); |
Comran Morshed | e68e373 | 2016-03-12 14:12:11 +0000 | [diff] [blame] | 984 | |
| 985 | return true; |
| 986 | } |
| 987 | |
Comran Morshed | e68e373 | 2016-03-12 14:12:11 +0000 | [diff] [blame] | 988 | } // namespace actors |
| 989 | } // namespace y2016 |