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