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