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