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