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