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