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 | |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 5 | #include <cmath> |
| 6 | |
Comran Morshed | e68e373 | 2016-03-12 14:12:11 +0000 | [diff] [blame] | 7 | #include "aos/common/util/phased_loop.h" |
| 8 | #include "aos/common/logging/logging.h" |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 9 | |
| 10 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 11 | #include "y2016/control_loops/drivetrain/drivetrain_base.h" |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 12 | #include "y2016/control_loops/shooter/shooter.q.h" |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 13 | #include "y2016/control_loops/superstructure/superstructure.q.h" |
| 14 | #include "y2016/actors/autonomous_action.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; |
| 21 | |
| 22 | namespace { |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 23 | const ProfileParameters kSlowDrive = {0.8, 2.5}; |
| 24 | const ProfileParameters kLowBarDrive = {1.3, 2.5}; |
| 25 | const ProfileParameters kMoatDrive = {1.2, 3.5}; |
| 26 | const ProfileParameters kRealignDrive = {2.0, 2.5}; |
| 27 | const ProfileParameters kRockWallDrive = {0.8, 2.5}; |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 28 | const ProfileParameters kFastDrive = {3.0, 2.5}; |
| 29 | |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 30 | const ProfileParameters kSlowTurn = {0.8, 3.0}; |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 31 | const ProfileParameters kFastTurn = {3.0, 10.0}; |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 32 | const ProfileParameters kStealTurn = {4.0, 15.0}; |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 33 | const ProfileParameters kSwerveTurn = {2.0, 7.0}; |
| 34 | const ProfileParameters kFinishTurn = {2.0, 5.0}; |
| 35 | |
| 36 | const ProfileParameters kTwoBallLowDrive = {1.7, 3.5}; |
| 37 | const ProfileParameters kTwoBallFastDrive = {3.0, 1.5}; |
| 38 | const ProfileParameters kTwoBallReturnDrive = {3.0, 1.9}; |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 39 | const ProfileParameters kTwoBallReturnSlow = {3.0, 2.5}; |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 40 | const ProfileParameters kTwoBallBallPickup = {2.0, 1.75}; |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 41 | const ProfileParameters kTwoBallBallPickupAccel = {2.0, 2.5}; |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 42 | } // namespace |
Comran Morshed | e68e373 | 2016-03-12 14:12:11 +0000 | [diff] [blame] | 43 | |
| 44 | AutonomousActor::AutonomousActor(actors::AutonomousActionQueueGroup *s) |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 45 | : aos::common::actions::ActorBase<actors::AutonomousActionQueueGroup>(s), |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 46 | dt_config_(control_loops::drivetrain::GetDrivetrainConfig()), |
| 47 | initial_drivetrain_({0.0, 0.0}) {} |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 48 | |
| 49 | void AutonomousActor::ResetDrivetrain() { |
| 50 | LOG(INFO, "resetting the drivetrain\n"); |
| 51 | drivetrain_queue.goal.MakeWithBuilder() |
| 52 | .control_loop_driving(false) |
| 53 | .highgear(true) |
| 54 | .steering(0.0) |
| 55 | .throttle(0.0) |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 56 | .left_goal(initial_drivetrain_.left) |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 57 | .left_velocity_goal(0) |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 58 | .right_goal(initial_drivetrain_.right) |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 59 | .right_velocity_goal(0) |
| 60 | .Send(); |
| 61 | } |
| 62 | |
| 63 | void AutonomousActor::StartDrive(double distance, double angle, |
| 64 | ProfileParameters linear, |
| 65 | ProfileParameters angular) { |
| 66 | { |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 67 | LOG(INFO, "Driving distance %f, angle %f\n", distance, angle); |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 68 | { |
| 69 | const double dangle = angle * dt_config_.robot_radius; |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 70 | initial_drivetrain_.left += distance - dangle; |
| 71 | initial_drivetrain_.right += distance + dangle; |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 74 | auto drivetrain_message = drivetrain_queue.goal.MakeMessage(); |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 75 | drivetrain_message->control_loop_driving = true; |
| 76 | drivetrain_message->highgear = true; |
| 77 | drivetrain_message->steering = 0.0; |
| 78 | drivetrain_message->throttle = 0.0; |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 79 | drivetrain_message->left_goal = initial_drivetrain_.left; |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 80 | drivetrain_message->left_velocity_goal = 0; |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 81 | drivetrain_message->right_goal = initial_drivetrain_.right; |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 82 | drivetrain_message->right_velocity_goal = 0; |
| 83 | drivetrain_message->linear = linear; |
| 84 | drivetrain_message->angular = angular; |
| 85 | |
| 86 | LOG_STRUCT(DEBUG, "drivetrain_goal", *drivetrain_message); |
| 87 | |
| 88 | drivetrain_message.Send(); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void AutonomousActor::InitializeEncoders() { |
| 93 | drivetrain_queue.status.FetchAnother(); |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 94 | initial_drivetrain_.left = drivetrain_queue.status->estimated_left_position; |
| 95 | initial_drivetrain_.right = drivetrain_queue.status->estimated_right_position; |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void AutonomousActor::WaitUntilDoneOrCanceled( |
| 99 | ::std::unique_ptr<aos::common::actions::Action> action) { |
| 100 | if (!action) { |
| 101 | LOG(ERROR, "No action, not waiting\n"); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5), |
| 106 | ::aos::time::Time::InMS(5) / 2); |
| 107 | while (true) { |
| 108 | // Poll the running bit and see if we should cancel. |
| 109 | phased_loop.SleepUntilNext(); |
| 110 | if (!action->Running() || ShouldCancel()) { |
| 111 | return; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 116 | constexpr double kDoNotTurnCare = 2.0; |
| 117 | |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 118 | bool AutonomousActor::WaitForDriveNear(double distance, double angle) { |
| 119 | ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5), |
| 120 | ::aos::time::Time::InMS(5) / 2); |
| 121 | constexpr double kPositionTolerance = 0.02; |
| 122 | constexpr double kProfileTolerance = 0.001; |
| 123 | |
| 124 | while (true) { |
| 125 | if (ShouldCancel()) { |
| 126 | return false; |
| 127 | } |
| 128 | phased_loop.SleepUntilNext(); |
| 129 | drivetrain_queue.status.FetchLatest(); |
| 130 | if (drivetrain_queue.status.get()) { |
| 131 | const double left_profile_error = |
| 132 | (initial_drivetrain_.left - |
| 133 | drivetrain_queue.status->profiled_left_position_goal); |
| 134 | const double right_profile_error = |
| 135 | (initial_drivetrain_.right - |
| 136 | drivetrain_queue.status->profiled_right_position_goal); |
| 137 | |
| 138 | const double left_error = |
| 139 | (initial_drivetrain_.left - |
| 140 | drivetrain_queue.status->estimated_left_position); |
| 141 | const double right_error = |
| 142 | (initial_drivetrain_.right - |
| 143 | drivetrain_queue.status->estimated_right_position); |
| 144 | |
| 145 | const double profile_distance_to_go = |
| 146 | (left_profile_error + right_profile_error) / 2.0; |
| 147 | const double profile_angle_to_go = |
| 148 | (right_profile_error - left_profile_error) / |
| 149 | (dt_config_.robot_radius * 2.0); |
| 150 | |
| 151 | const double distance_to_go = (left_error + right_error) / 2.0; |
| 152 | const double angle_to_go = |
| 153 | (right_error - left_error) / (dt_config_.robot_radius * 2.0); |
| 154 | |
| 155 | if (::std::abs(profile_distance_to_go) < distance + kProfileTolerance && |
| 156 | ::std::abs(profile_angle_to_go) < angle + kProfileTolerance && |
| 157 | ::std::abs(distance_to_go) < distance + kPositionTolerance && |
| 158 | ::std::abs(angle_to_go) < angle + kPositionTolerance) { |
| 159 | LOG(INFO, "Closer than %f distance, %f angle\n", distance, angle); |
| 160 | return true; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 166 | bool AutonomousActor::WaitForDriveProfileDone() { |
| 167 | ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5), |
| 168 | ::aos::time::Time::InMS(5) / 2); |
| 169 | constexpr double kProfileTolerance = 0.001; |
| 170 | |
| 171 | while (true) { |
| 172 | if (ShouldCancel()) { |
| 173 | return false; |
| 174 | } |
| 175 | phased_loop.SleepUntilNext(); |
| 176 | drivetrain_queue.status.FetchLatest(); |
| 177 | if (drivetrain_queue.status.get()) { |
| 178 | if (::std::abs(drivetrain_queue.status->profiled_left_position_goal - |
| 179 | initial_drivetrain_.left) < kProfileTolerance && |
| 180 | ::std::abs(drivetrain_queue.status->profiled_right_position_goal - |
| 181 | initial_drivetrain_.right) < kProfileTolerance) { |
| 182 | LOG(INFO, "Finished drive\n"); |
| 183 | return true; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 189 | bool AutonomousActor::WaitForMaxBy(double angle) { |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 190 | ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5), |
| 191 | ::aos::time::Time::InMS(5) / 2); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 192 | double max_angle = -M_PI; |
| 193 | while (true) { |
| 194 | if (ShouldCancel()) { |
| 195 | return false; |
| 196 | } |
| 197 | phased_loop.SleepUntilNext(); |
| 198 | drivetrain_queue.status.FetchLatest(); |
| 199 | if (IsDriveDone()) { |
| 200 | return true; |
| 201 | } |
| 202 | if (drivetrain_queue.status.get()) { |
| 203 | if (drivetrain_queue.status->ground_angle > max_angle) { |
| 204 | max_angle = drivetrain_queue.status->ground_angle; |
| 205 | } |
| 206 | if (drivetrain_queue.status->ground_angle < max_angle - angle) { |
| 207 | return true; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | bool AutonomousActor::WaitForAboveAngle(double angle) { |
| 214 | ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5), |
| 215 | ::aos::time::Time::InMS(5) / 2); |
| 216 | while (true) { |
| 217 | if (ShouldCancel()) { |
| 218 | return false; |
| 219 | } |
| 220 | phased_loop.SleepUntilNext(); |
| 221 | drivetrain_queue.status.FetchLatest(); |
| 222 | if (IsDriveDone()) { |
| 223 | return true; |
| 224 | } |
| 225 | if (drivetrain_queue.status.get()) { |
| 226 | if (drivetrain_queue.status->ground_angle > angle) { |
| 227 | return true; |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | bool AutonomousActor::WaitForBelowAngle(double angle) { |
| 234 | ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5), |
| 235 | ::aos::time::Time::InMS(5) / 2); |
| 236 | while (true) { |
| 237 | if (ShouldCancel()) { |
| 238 | return false; |
| 239 | } |
| 240 | phased_loop.SleepUntilNext(); |
| 241 | drivetrain_queue.status.FetchLatest(); |
| 242 | if (IsDriveDone()) { |
| 243 | return true; |
| 244 | } |
| 245 | if (drivetrain_queue.status.get()) { |
| 246 | if (drivetrain_queue.status->ground_angle < angle) { |
| 247 | return true; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | bool AutonomousActor::IsDriveDone() { |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 254 | constexpr double kPositionTolerance = 0.02; |
| 255 | constexpr double kVelocityTolerance = 0.10; |
| 256 | constexpr double kProfileTolerance = 0.001; |
| 257 | |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 258 | if (drivetrain_queue.status.get()) { |
| 259 | if (::std::abs(drivetrain_queue.status->profiled_left_position_goal - |
| 260 | initial_drivetrain_.left) < kProfileTolerance && |
| 261 | ::std::abs(drivetrain_queue.status->profiled_right_position_goal - |
| 262 | initial_drivetrain_.right) < kProfileTolerance && |
| 263 | ::std::abs(drivetrain_queue.status->estimated_left_position - |
| 264 | initial_drivetrain_.left) < kPositionTolerance && |
| 265 | ::std::abs(drivetrain_queue.status->estimated_right_position - |
| 266 | initial_drivetrain_.right) < kPositionTolerance && |
| 267 | ::std::abs(drivetrain_queue.status->estimated_left_velocity) < |
| 268 | kVelocityTolerance && |
| 269 | ::std::abs(drivetrain_queue.status->estimated_right_velocity) < |
| 270 | kVelocityTolerance) { |
| 271 | LOG(INFO, "Finished drive\n"); |
| 272 | return true; |
| 273 | } |
| 274 | } |
| 275 | return false; |
| 276 | } |
| 277 | |
| 278 | bool AutonomousActor::WaitForDriveDone() { |
| 279 | ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5), |
| 280 | ::aos::time::Time::InMS(5) / 2); |
| 281 | |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 282 | while (true) { |
| 283 | if (ShouldCancel()) { |
| 284 | return false; |
| 285 | } |
| 286 | phased_loop.SleepUntilNext(); |
| 287 | drivetrain_queue.status.FetchLatest(); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 288 | if (IsDriveDone()) { |
| 289 | return true; |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | } |
Comran Morshed | e68e373 | 2016-03-12 14:12:11 +0000 | [diff] [blame] | 293 | |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 294 | void AutonomousActor::MoveSuperstructure( |
| 295 | double intake, double shoulder, double wrist, |
| 296 | const ProfileParameters intake_params, |
| 297 | const ProfileParameters shoulder_params, |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 298 | const ProfileParameters wrist_params, bool traverse_up, |
| 299 | double roller_power) { |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 300 | superstructure_goal_ = {intake, shoulder, wrist}; |
| 301 | |
| 302 | auto new_superstructure_goal = |
| 303 | ::y2016::control_loops::superstructure_queue.goal.MakeMessage(); |
| 304 | |
| 305 | new_superstructure_goal->angle_intake = intake; |
| 306 | new_superstructure_goal->angle_shoulder = shoulder; |
| 307 | new_superstructure_goal->angle_wrist = wrist; |
| 308 | |
| 309 | new_superstructure_goal->max_angular_velocity_intake = |
| 310 | intake_params.max_velocity; |
| 311 | new_superstructure_goal->max_angular_velocity_shoulder = |
| 312 | shoulder_params.max_velocity; |
| 313 | new_superstructure_goal->max_angular_velocity_wrist = |
| 314 | wrist_params.max_velocity; |
| 315 | |
| 316 | new_superstructure_goal->max_angular_acceleration_intake = |
| 317 | intake_params.max_acceleration; |
| 318 | new_superstructure_goal->max_angular_acceleration_shoulder = |
| 319 | shoulder_params.max_acceleration; |
| 320 | new_superstructure_goal->max_angular_acceleration_wrist = |
| 321 | wrist_params.max_acceleration; |
| 322 | |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 323 | new_superstructure_goal->voltage_top_rollers = roller_power; |
| 324 | new_superstructure_goal->voltage_bottom_rollers = roller_power; |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 325 | |
| 326 | new_superstructure_goal->traverse_unlatched = true; |
| 327 | new_superstructure_goal->traverse_down = !traverse_up; |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 328 | new_superstructure_goal->voltage_climber = 0.0; |
| 329 | new_superstructure_goal->unfold_climber = false; |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 330 | |
| 331 | if (!new_superstructure_goal.Send()) { |
| 332 | LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 333 | } |
| 334 | } |
| 335 | |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 336 | void AutonomousActor::OpenShooter() { |
| 337 | shooter_speed_ = 0.0; |
| 338 | |
| 339 | if (!control_loops::shooter::shooter_queue.goal.MakeWithBuilder() |
| 340 | .angular_velocity(shooter_speed_) |
| 341 | .clamp_open(true) |
| 342 | .push_to_shooter(false) |
| 343 | .force_lights_on(false) |
| 344 | .Send()) { |
| 345 | LOG(ERROR, "Sending shooter goal failed.\n"); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | void AutonomousActor::CloseShooter() { |
| 350 | shooter_speed_ = 0.0; |
| 351 | |
| 352 | if (!control_loops::shooter::shooter_queue.goal.MakeWithBuilder() |
| 353 | .angular_velocity(shooter_speed_) |
| 354 | .clamp_open(false) |
| 355 | .push_to_shooter(false) |
| 356 | .force_lights_on(false) |
| 357 | .Send()) { |
| 358 | LOG(ERROR, "Sending shooter goal failed.\n"); |
| 359 | } |
| 360 | } |
| 361 | |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 362 | void AutonomousActor::SetShooterSpeed(double speed) { |
| 363 | shooter_speed_ = speed; |
| 364 | |
| 365 | // In auto, we want to have the lights on whenever possible since we have no |
| 366 | // hope of a human aligning the robot. |
| 367 | bool force_lights_on = shooter_speed_ > 1.0; |
| 368 | |
| 369 | if (!control_loops::shooter::shooter_queue.goal.MakeWithBuilder() |
| 370 | .angular_velocity(shooter_speed_) |
| 371 | .clamp_open(false) |
| 372 | .push_to_shooter(false) |
| 373 | .force_lights_on(force_lights_on) |
| 374 | .Send()) { |
| 375 | LOG(ERROR, "Sending shooter goal failed.\n"); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | void AutonomousActor::Shoot() { |
| 380 | uint32_t initial_shots = 0; |
| 381 | |
| 382 | control_loops::shooter::shooter_queue.status.FetchLatest(); |
| 383 | if (control_loops::shooter::shooter_queue.status.get()) { |
| 384 | initial_shots = control_loops::shooter::shooter_queue.status->shots; |
| 385 | } |
| 386 | |
| 387 | // In auto, we want to have the lights on whenever possible since we have no |
| 388 | // hope of a human aligning the robot. |
| 389 | bool force_lights_on = shooter_speed_ > 1.0; |
| 390 | |
| 391 | if (!control_loops::shooter::shooter_queue.goal.MakeWithBuilder() |
| 392 | .angular_velocity(shooter_speed_) |
| 393 | .clamp_open(false) |
| 394 | .push_to_shooter(true) |
| 395 | .force_lights_on(force_lights_on) |
| 396 | .Send()) { |
| 397 | LOG(ERROR, "Sending shooter goal failed.\n"); |
| 398 | } |
| 399 | |
| 400 | ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5), |
| 401 | ::aos::time::Time::InMS(5) / 2); |
| 402 | while (true) { |
| 403 | if (ShouldCancel()) return; |
| 404 | |
| 405 | // Wait for the shot count to change so we know when the shot is complete. |
| 406 | control_loops::shooter::shooter_queue.status.FetchLatest(); |
| 407 | if (control_loops::shooter::shooter_queue.status.get()) { |
| 408 | if (initial_shots < control_loops::shooter::shooter_queue.status->shots) { |
| 409 | return; |
| 410 | } |
| 411 | } |
| 412 | phased_loop.SleepUntilNext(); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | void AutonomousActor::WaitForShooterSpeed() { |
| 417 | ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5), |
| 418 | ::aos::time::Time::InMS(5) / 2); |
| 419 | while (true) { |
| 420 | if (ShouldCancel()) return; |
| 421 | |
| 422 | control_loops::shooter::shooter_queue.status.FetchLatest(); |
| 423 | if (control_loops::shooter::shooter_queue.status.get()) { |
| 424 | if (control_loops::shooter::shooter_queue.status->left.ready && |
| 425 | control_loops::shooter::shooter_queue.status->right.ready) { |
| 426 | return; |
| 427 | } |
| 428 | } |
| 429 | phased_loop.SleepUntilNext(); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | void AutonomousActor::AlignWithVisionGoal() { |
| 434 | actors::VisionAlignActionParams params; |
| 435 | vision_action_ = ::std::move(actors::MakeVisionAlignAction(params)); |
| 436 | vision_action_->Start(); |
| 437 | } |
| 438 | |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 439 | void AutonomousActor::WaitForAlignedWithVision( |
| 440 | ::aos::time::Time align_duration) { |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 441 | bool vision_valid = false; |
| 442 | double last_angle = 0.0; |
| 443 | int ready_to_fire = 0; |
| 444 | |
| 445 | ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5), |
| 446 | ::aos::time::Time::InMS(5) / 2); |
| 447 | ::aos::time::Time end_time = |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 448 | ::aos::time::Time::Now() + align_duration; |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 449 | while (end_time > ::aos::time::Time::Now()) { |
| 450 | if (ShouldCancel()) break; |
| 451 | |
| 452 | ::y2016::vision::vision_status.FetchLatest(); |
| 453 | if (::y2016::vision::vision_status.get()) { |
| 454 | vision_valid = (::y2016::vision::vision_status->left_image_valid && |
| 455 | ::y2016::vision::vision_status->right_image_valid); |
| 456 | last_angle = ::y2016::vision::vision_status->horizontal_angle; |
| 457 | } |
| 458 | |
| 459 | drivetrain_queue.status.FetchLatest(); |
| 460 | drivetrain_queue.goal.FetchLatest(); |
| 461 | |
| 462 | if (drivetrain_queue.status.get() && drivetrain_queue.goal.get()) { |
| 463 | const double left_goal = drivetrain_queue.goal->left_goal; |
| 464 | const double right_goal = drivetrain_queue.goal->right_goal; |
| 465 | const double left_current = |
| 466 | drivetrain_queue.status->estimated_left_position; |
| 467 | const double right_current = |
| 468 | drivetrain_queue.status->estimated_right_position; |
| 469 | const double left_velocity = |
| 470 | drivetrain_queue.status->estimated_left_velocity; |
| 471 | const double right_velocity = |
| 472 | drivetrain_queue.status->estimated_right_velocity; |
| 473 | |
| 474 | if (vision_valid && ::std::abs(last_angle) < 0.02 && |
| 475 | ::std::abs((left_goal - right_goal) - |
| 476 | (left_current - right_current)) / |
| 477 | dt_config_.robot_radius / 2.0 < |
| 478 | 0.02 && |
| 479 | ::std::abs(left_velocity - right_velocity) < 0.01) { |
| 480 | ++ready_to_fire; |
| 481 | } else { |
| 482 | ready_to_fire = 0; |
| 483 | } |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 484 | if (ready_to_fire > 15) { |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 485 | break; |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 486 | LOG(INFO, "Vision align success!\n"); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | phased_loop.SleepUntilNext(); |
| 490 | } |
| 491 | |
| 492 | vision_action_->Cancel(); |
| 493 | WaitUntilDoneOrCanceled(::std::move(vision_action_)); |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 494 | LOG(INFO, "Done waiting for vision\n"); |
| 495 | } |
| 496 | |
| 497 | bool AutonomousActor::IntakeDone() { |
| 498 | control_loops::superstructure_queue.status.FetchAnother(); |
| 499 | |
| 500 | constexpr double kProfileError = 1e-5; |
| 501 | constexpr double kEpsilon = 0.15; |
| 502 | |
| 503 | if (control_loops::superstructure_queue.status->state < 12 || |
| 504 | control_loops::superstructure_queue.status->state == 16) { |
| 505 | LOG(ERROR, "Superstructure no longer running, aborting action\n"); |
| 506 | return true; |
| 507 | } |
| 508 | |
| 509 | if (::std::abs(control_loops::superstructure_queue.status->intake.goal_angle - |
| 510 | superstructure_goal_.intake) < kProfileError && |
| 511 | ::std::abs(control_loops::superstructure_queue.status->intake |
| 512 | .goal_angular_velocity) < kProfileError) { |
| 513 | LOG(DEBUG, "Profile done.\n"); |
| 514 | if (::std::abs(control_loops::superstructure_queue.status->intake.angle - |
| 515 | superstructure_goal_.intake) < kEpsilon && |
| 516 | ::std::abs(control_loops::superstructure_queue.status->intake |
| 517 | .angular_velocity) < kEpsilon) { |
| 518 | LOG(INFO, "Near goal, done.\n"); |
| 519 | return true; |
| 520 | } |
| 521 | } |
| 522 | return false; |
| 523 | } |
| 524 | |
| 525 | bool AutonomousActor::SuperstructureProfileDone() { |
| 526 | constexpr double kProfileError = 1e-5; |
| 527 | return ::std::abs( |
| 528 | control_loops::superstructure_queue.status->intake.goal_angle - |
| 529 | superstructure_goal_.intake) < kProfileError && |
| 530 | ::std::abs( |
| 531 | control_loops::superstructure_queue.status->shoulder.goal_angle - |
| 532 | superstructure_goal_.shoulder) < kProfileError && |
| 533 | ::std::abs( |
| 534 | control_loops::superstructure_queue.status->wrist.goal_angle - |
| 535 | superstructure_goal_.wrist) < kProfileError && |
| 536 | ::std::abs(control_loops::superstructure_queue.status->intake |
| 537 | .goal_angular_velocity) < kProfileError && |
| 538 | ::std::abs(control_loops::superstructure_queue.status->shoulder |
| 539 | .goal_angular_velocity) < kProfileError && |
| 540 | ::std::abs(control_loops::superstructure_queue.status->wrist |
| 541 | .goal_angular_velocity) < kProfileError; |
| 542 | } |
| 543 | |
| 544 | bool AutonomousActor::SuperstructureDone() { |
| 545 | control_loops::superstructure_queue.status.FetchAnother(); |
| 546 | |
| 547 | constexpr double kEpsilon = 0.03; |
| 548 | |
| 549 | if (control_loops::superstructure_queue.status->state < 12 || |
| 550 | control_loops::superstructure_queue.status->state == 16) { |
| 551 | LOG(ERROR, "Superstructure no longer running, aborting action\n"); |
| 552 | return true; |
| 553 | } |
| 554 | |
| 555 | if (SuperstructureProfileDone()) { |
| 556 | LOG(DEBUG, "Profile done.\n"); |
| 557 | if (::std::abs(control_loops::superstructure_queue.status->intake.angle - |
| 558 | superstructure_goal_.intake) < (kEpsilon + 0.1) && |
| 559 | ::std::abs(control_loops::superstructure_queue.status->shoulder.angle - |
| 560 | superstructure_goal_.shoulder) < (kEpsilon + 0.05) && |
| 561 | ::std::abs(control_loops::superstructure_queue.status->wrist.angle - |
| 562 | superstructure_goal_.wrist) < (kEpsilon + 0.01) && |
| 563 | ::std::abs(control_loops::superstructure_queue.status->intake |
| 564 | .angular_velocity) < (kEpsilon + 0.1) && |
| 565 | ::std::abs(control_loops::superstructure_queue.status->shoulder |
| 566 | .angular_velocity) < (kEpsilon + 0.10) && |
| 567 | ::std::abs(control_loops::superstructure_queue.status->wrist |
| 568 | .angular_velocity) < (kEpsilon + 0.05)) { |
| 569 | LOG(INFO, "Near goal, done.\n"); |
| 570 | return true; |
| 571 | } |
| 572 | } |
| 573 | return false; |
| 574 | } |
| 575 | |
| 576 | void AutonomousActor::WaitForIntake() { |
| 577 | while (true) { |
| 578 | if (ShouldCancel()) return; |
| 579 | if (IntakeDone()) return; |
| 580 | } |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 581 | } |
| 582 | |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 583 | void AutonomousActor::WaitForSuperstructure() { |
| 584 | while (true) { |
| 585 | if (ShouldCancel()) return; |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 586 | if (SuperstructureDone()) return; |
| 587 | } |
| 588 | } |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 589 | |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 590 | void AutonomousActor::WaitForSuperstructureProfile() { |
| 591 | while (true) { |
| 592 | if (ShouldCancel()) return; |
| 593 | control_loops::superstructure_queue.status.FetchAnother(); |
| 594 | |
| 595 | if (control_loops::superstructure_queue.status->state < 12 || |
| 596 | control_loops::superstructure_queue.status->state == 16) { |
| 597 | LOG(ERROR, "Superstructure no longer running, aborting action\n"); |
| 598 | return; |
| 599 | } |
| 600 | |
| 601 | if (SuperstructureProfileDone()) return; |
| 602 | } |
| 603 | } |
| 604 | |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 605 | void AutonomousActor::WaitForSuperstructureLow() { |
| 606 | while (true) { |
| 607 | if (ShouldCancel()) return; |
| 608 | control_loops::superstructure_queue.status.FetchAnother(); |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 609 | |
| 610 | if (control_loops::superstructure_queue.status->state < 12 || |
| 611 | control_loops::superstructure_queue.status->state == 16) { |
| 612 | LOG(ERROR, "Superstructure no longer running, aborting action\n"); |
| 613 | return; |
| 614 | } |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 615 | if (SuperstructureProfileDone()) return; |
| 616 | if (control_loops::superstructure_queue.status->shoulder.angle < 0.1) { |
| 617 | return; |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | } |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 621 | void AutonomousActor::BackLongShotLowBarTwoBall() { |
| 622 | LOG(INFO, "Expanding for back long shot\n"); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 623 | 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] | 624 | {10.0, 25.0}, false, 0.0); |
| 625 | } |
| 626 | |
| 627 | void AutonomousActor::BackLongShotTwoBall() { |
| 628 | LOG(INFO, "Expanding for back long shot\n"); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 629 | MoveSuperstructure(0.00, M_PI / 2.0 - 0.2, -0.55, {7.0, 40.0}, {4.0, 6.0}, |
| 630 | {10.0, 25.0}, false, 0.0); |
| 631 | } |
| 632 | |
| 633 | void AutonomousActor::BackLongShotTwoBallFinish() { |
| 634 | LOG(INFO, "Expanding for back long shot\n"); |
Austin Schuh | 9096336 | 2016-05-01 12:27:31 -0700 | [diff] [blame] | 635 | MoveSuperstructure(0.00, M_PI / 2.0 - 0.2, -0.625, {7.0, 40.0}, {4.0, 6.0}, |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 636 | {10.0, 25.0}, false, 0.0); |
| 637 | } |
| 638 | |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 639 | void AutonomousActor::BackLongShot() { |
| 640 | LOG(INFO, "Expanding for back long shot\n"); |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 641 | MoveSuperstructure(0.80, M_PI / 2.0 - 0.2, -0.62, {7.0, 40.0}, {4.0, 6.0}, |
| 642 | {10.0, 25.0}, false, 0.0); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | void AutonomousActor::BackMiddleShot() { |
| 646 | LOG(INFO, "Expanding for back middle shot\n"); |
| 647 | 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] | 648 | {10.0, 25.0}, false, 0.0); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 651 | void AutonomousActor::FrontLongShot() { |
| 652 | LOG(INFO, "Expanding for front long shot\n"); |
| 653 | MoveSuperstructure(0.80, M_PI / 2.0 + 0.1, M_PI + 0.41 + 0.02, {7.0, 40.0}, |
| 654 | {4.0, 6.0}, {10.0, 25.0}, false, 0.0); |
| 655 | } |
| 656 | |
| 657 | void AutonomousActor::FrontMiddleShot() { |
| 658 | LOG(INFO, "Expanding for front middle shot\n"); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 659 | 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] | 660 | {4.0, 10.0}, {10.0, 25.0}, true, 0.0); |
| 661 | } |
| 662 | |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 663 | void AutonomousActor::TuckArm(bool low_bar, bool traverse_down) { |
| 664 | 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] | 665 | {4.0, 10.0}, {10.0, 25.0}, !traverse_down, 0.0); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 668 | void AutonomousActor::DoFullShot() { |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 669 | if (ShouldCancel()) return; |
| 670 | // Make sure that the base is aligned with the base. |
| 671 | LOG(INFO, "Waiting for the superstructure\n"); |
| 672 | WaitForSuperstructure(); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 673 | |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 674 | ::aos::time::SleepFor(::aos::time::Time::InSeconds(0.5)); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 675 | |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 676 | if (ShouldCancel()) return; |
| 677 | LOG(INFO, "Triggering the vision actor\n"); |
| 678 | AlignWithVisionGoal(); |
| 679 | |
| 680 | // Wait for the drive base to be aligned with the target and make sure that |
| 681 | // the shooter is up to speed. |
| 682 | LOG(INFO, "Waiting for vision to be aligned\n"); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 683 | WaitForAlignedWithVision(aos::time::Time::InSeconds(2)); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 684 | if (ShouldCancel()) return; |
| 685 | LOG(INFO, "Waiting for shooter to be up to speed\n"); |
| 686 | WaitForShooterSpeed(); |
| 687 | if (ShouldCancel()) return; |
| 688 | |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 689 | ::aos::time::SleepFor(::aos::time::Time::InSeconds(0.3)); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 690 | LOG(INFO, "Shoot!\n"); |
| 691 | Shoot(); |
| 692 | |
| 693 | // Turn off the shooter and fold up the superstructure. |
| 694 | if (ShouldCancel()) return; |
| 695 | LOG(INFO, "Stopping shooter\n"); |
| 696 | SetShooterSpeed(0.0); |
| 697 | LOG(INFO, "Folding superstructure back down\n"); |
| 698 | TuckArm(false, false); |
| 699 | |
| 700 | // Wait for everything to be folded up. |
| 701 | LOG(INFO, "Waiting for superstructure to be folded back down\n"); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 702 | WaitForSuperstructureLow(); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | void AutonomousActor::LowBarDrive() { |
| 706 | TuckArm(false, true); |
| 707 | StartDrive(-5.5, 0.0, kLowBarDrive, kSlowTurn); |
| 708 | |
| 709 | if (!WaitForDriveNear(5.3, 0.0)) return; |
| 710 | TuckArm(true, true); |
| 711 | |
| 712 | if (!WaitForDriveNear(5.0, 0.0)) return; |
| 713 | |
| 714 | StartDrive(0.0, 0.0, kLowBarDrive, kSlowTurn); |
| 715 | |
| 716 | if (!WaitForDriveNear(3.0, 0.0)) return; |
| 717 | |
| 718 | StartDrive(0.0, 0.0, kLowBarDrive, kSlowTurn); |
| 719 | |
| 720 | if (!WaitForDriveNear(1.0, 0.0)) return; |
| 721 | |
Austin Schuh | 15b5f6a | 2016-03-26 19:43:56 -0700 | [diff] [blame] | 722 | StartDrive(0, -M_PI / 4.0 - 0.2, kLowBarDrive, kSlowTurn); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 723 | } |
| 724 | |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 725 | void AutonomousActor::TippyDrive(double goal_distance, double tip_distance, |
| 726 | double below, double above) { |
| 727 | StartDrive(goal_distance, 0.0, kMoatDrive, kSlowTurn); |
| 728 | if (!WaitForBelowAngle(below)) return; |
| 729 | if (!WaitForAboveAngle(above)) return; |
| 730 | // Ok, we are good now. Compensate by moving the goal by the error. |
| 731 | // Should be here at 2.7 |
| 732 | drivetrain_queue.status.FetchLatest(); |
| 733 | if (drivetrain_queue.status.get()) { |
| 734 | const double left_error = |
| 735 | (initial_drivetrain_.left - |
| 736 | drivetrain_queue.status->estimated_left_position); |
| 737 | const double right_error = |
| 738 | (initial_drivetrain_.right - |
| 739 | drivetrain_queue.status->estimated_right_position); |
| 740 | const double distance_to_go = (left_error + right_error) / 2.0; |
| 741 | const double distance_compensation = |
| 742 | goal_distance - tip_distance - distance_to_go; |
| 743 | LOG(INFO, "Going %f further at the bump\n", distance_compensation); |
| 744 | StartDrive(distance_compensation, 0.0, kMoatDrive, kSlowTurn); |
| 745 | } |
| 746 | } |
| 747 | |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 748 | void AutonomousActor::MiddleDrive() { |
| 749 | TuckArm(false, false); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 750 | TippyDrive(3.65, 2.7, -0.2, 0.0); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 751 | if (!WaitForDriveDone()) return; |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | void AutonomousActor::OneFromMiddleDrive(bool left) { |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 755 | const double kTurnAngle = left ? -0.41 : 0.41; |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 756 | TuckArm(false, false); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 757 | TippyDrive(4.05, 2.7, -0.2, 0.0); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 758 | |
| 759 | if (!WaitForDriveDone()) return; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 760 | StartDrive(0.0, kTurnAngle, kRealignDrive, kFastTurn); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | void AutonomousActor::TwoFromMiddleDrive() { |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 764 | TuckArm(false, false); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 765 | constexpr double kDriveDistance = 5.10; |
| 766 | TippyDrive(kDriveDistance, 2.7, -0.2, 0.0); |
| 767 | |
| 768 | if (!WaitForDriveNear(kDriveDistance - 3.0, 2.0)) return; |
| 769 | StartDrive(0, -M_PI / 2 - 0.10, kMoatDrive, kFastTurn); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 770 | |
| 771 | if (!WaitForDriveDone()) return; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 772 | StartDrive(0, M_PI / 3 + 0.35, kMoatDrive, kFastTurn); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 773 | } |
Comran Morshed | b134e77 | 2016-03-16 21:05:05 +0000 | [diff] [blame] | 774 | |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 775 | void AutonomousActor::CloseIfBall() { |
| 776 | ::y2016::sensors::ball_detector.FetchLatest(); |
| 777 | if (::y2016::sensors::ball_detector.get()) { |
| 778 | const bool ball_detected = ::y2016::sensors::ball_detector->voltage > 2.5; |
| 779 | if (ball_detected) { |
| 780 | CloseShooter(); |
| 781 | } |
| 782 | } |
| 783 | } |
| 784 | |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 785 | void AutonomousActor::WaitForBallOrDriveDone() { |
| 786 | ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5), |
| 787 | ::aos::time::Time::InMS(5) / 2); |
| 788 | while (true) { |
| 789 | if (ShouldCancel()) { |
| 790 | return; |
| 791 | } |
| 792 | phased_loop.SleepUntilNext(); |
| 793 | drivetrain_queue.status.FetchLatest(); |
| 794 | if (IsDriveDone()) { |
| 795 | return; |
| 796 | } |
| 797 | |
| 798 | ::y2016::sensors::ball_detector.FetchLatest(); |
| 799 | if (::y2016::sensors::ball_detector.get()) { |
| 800 | const bool ball_detected = ::y2016::sensors::ball_detector->voltage > 2.5; |
| 801 | if (ball_detected) { |
| 802 | return; |
| 803 | } |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 808 | void AutonomousActor::WaitForBall() { |
| 809 | while (true) { |
| 810 | ::y2016::sensors::ball_detector.FetchAnother(); |
| 811 | if (::y2016::sensors::ball_detector.get()) { |
| 812 | const bool ball_detected = ::y2016::sensors::ball_detector->voltage > 2.5; |
| 813 | if (ball_detected) { |
| 814 | return; |
| 815 | } |
| 816 | if (ShouldCancel()) return; |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 821 | void AutonomousActor::TwoBallAuto() { |
| 822 | aos::time::Time start_time = aos::time::Time::Now(); |
| 823 | OpenShooter(); |
| 824 | MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0}, |
| 825 | false, 12.0); |
| 826 | if (ShouldCancel()) return; |
| 827 | LOG(INFO, "Waiting for the intake to come down.\n"); |
| 828 | |
| 829 | WaitForIntake(); |
| 830 | LOG(INFO, "Intake done at %f seconds, starting to drive\n", |
| 831 | (aos::time::Time::Now() - start_time).ToSeconds()); |
| 832 | if (ShouldCancel()) return; |
| 833 | const double kDriveDistance = 5.05; |
| 834 | StartDrive(-kDriveDistance, 0.0, kTwoBallLowDrive, kSlowTurn); |
| 835 | |
| 836 | StartDrive(0.0, 0.4, kTwoBallLowDrive, kSwerveTurn); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 837 | if (!WaitForDriveNear(kDriveDistance - 0.5, kDoNotTurnCare)) return; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 838 | |
Austin Schuh | 295c2d9 | 2016-05-01 12:28:04 -0700 | [diff] [blame] | 839 | // Check if the ball is there. |
| 840 | bool first_ball_there = true; |
| 841 | ::y2016::sensors::ball_detector.FetchLatest(); |
| 842 | if (::y2016::sensors::ball_detector.get()) { |
| 843 | const bool ball_detected = ::y2016::sensors::ball_detector->voltage > 2.5; |
| 844 | first_ball_there = ball_detected; |
| 845 | LOG(INFO, "Saw the ball: %d at %f\n", first_ball_there, |
| 846 | (aos::time::Time::Now() - start_time).ToSeconds()); |
| 847 | } |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 848 | MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 40.0}, {4.0, 10.0}, {10.0, 25.0}, |
| 849 | false, 0.0); |
| 850 | LOG(INFO, "Shutting off rollers at %f seconds, starting to straighten out\n", |
| 851 | (aos::time::Time::Now() - start_time).ToSeconds()); |
| 852 | StartDrive(0.0, -0.4, kTwoBallLowDrive, kSwerveTurn); |
| 853 | MoveSuperstructure(-0.05, -0.010, 0.0, {8.0, 40.0}, {4.0, 10.0}, {10.0, 25.0}, |
| 854 | false, 0.0); |
| 855 | CloseShooter(); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 856 | if (!WaitForDriveNear(kDriveDistance - 2.4, kDoNotTurnCare)) return; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 857 | |
| 858 | // We are now under the low bar. Start lifting. |
| 859 | BackLongShotLowBarTwoBall(); |
| 860 | LOG(INFO, "Spinning up the shooter wheels\n"); |
| 861 | SetShooterSpeed(640.0); |
| 862 | StartDrive(0.0, 0.0, kTwoBallFastDrive, kSwerveTurn); |
| 863 | |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 864 | if (!WaitForDriveNear(1.50, kDoNotTurnCare)) return; |
| 865 | constexpr double kShootTurnAngle = -M_PI / 4.0 - 0.05; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 866 | StartDrive(0, kShootTurnAngle, kTwoBallFastDrive, kFinishTurn); |
| 867 | BackLongShotTwoBall(); |
| 868 | |
| 869 | if (!WaitForDriveDone()) return; |
| 870 | LOG(INFO, "First shot done driving at %f seconds\n", |
| 871 | (aos::time::Time::Now() - start_time).ToSeconds()); |
| 872 | |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 873 | WaitForSuperstructureProfile(); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 874 | |
| 875 | if (ShouldCancel()) return; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 876 | AlignWithVisionGoal(); |
| 877 | |
| 878 | WaitForShooterSpeed(); |
| 879 | if (ShouldCancel()) return; |
| 880 | |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 881 | constexpr double kVisionExtra = 0.0; |
| 882 | WaitForAlignedWithVision(aos::time::Time::InSeconds(0.5 + kVisionExtra)); |
| 883 | BackLongShotTwoBallFinish(); |
| 884 | WaitForSuperstructureProfile(); |
| 885 | if (ShouldCancel()) return; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 886 | LOG(INFO, "Shoot!\n"); |
Austin Schuh | 295c2d9 | 2016-05-01 12:28:04 -0700 | [diff] [blame] | 887 | if (first_ball_there) { |
| 888 | Shoot(); |
| 889 | } else { |
| 890 | LOG(INFO, "Nah, not shooting\n"); |
| 891 | } |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 892 | |
| 893 | LOG(INFO, "First shot at %f seconds\n", |
| 894 | (aos::time::Time::Now() - start_time).ToSeconds()); |
| 895 | if (ShouldCancel()) return; |
| 896 | |
| 897 | SetShooterSpeed(0.0); |
| 898 | LOG(INFO, "Folding superstructure back down\n"); |
| 899 | TuckArm(true, true); |
| 900 | |
| 901 | // Undo vision move. |
| 902 | StartDrive(0.0, 0.0, kTwoBallFastDrive, kFinishTurn); |
| 903 | if (!WaitForDriveDone()) return; |
| 904 | |
| 905 | constexpr double kBackDrive = 3.09 - 0.4; |
| 906 | StartDrive(kBackDrive, 0.0, kTwoBallReturnDrive, kSlowTurn); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 907 | if (!WaitForDriveNear(kBackDrive - 0.19, kDoNotTurnCare)) return; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 908 | StartDrive(0, -kShootTurnAngle, kTwoBallReturnDrive, kSwerveTurn); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 909 | if (!WaitForDriveNear(1.0, kDoNotTurnCare)) return; |
| 910 | StartDrive(0, 0, kTwoBallReturnSlow, kSwerveTurn); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 911 | |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 912 | if (!WaitForDriveNear(0.06, kDoNotTurnCare)) return; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 913 | LOG(INFO, "At Low Bar %f\n", |
| 914 | (aos::time::Time::Now() - start_time).ToSeconds()); |
| 915 | |
| 916 | OpenShooter(); |
| 917 | constexpr double kSecondBallAfterBarDrive = 2.10; |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 918 | StartDrive(kSecondBallAfterBarDrive, 0.0, kTwoBallBallPickupAccel, kSlowTurn); |
| 919 | if (!WaitForDriveNear(kSecondBallAfterBarDrive - 0.5, kDoNotTurnCare)) return; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 920 | constexpr double kBallSmallWallTurn = -0.11; |
| 921 | StartDrive(0, kBallSmallWallTurn, kTwoBallBallPickup, kFinishTurn); |
| 922 | |
| 923 | MoveSuperstructure(0.03, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0}, |
| 924 | false, 12.0); |
| 925 | |
| 926 | if (!WaitForDriveProfileDone()) return; |
| 927 | |
| 928 | MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0}, |
| 929 | false, 12.0); |
| 930 | |
| 931 | LOG(INFO, "Done backing up %f\n", |
| 932 | (aos::time::Time::Now() - start_time).ToSeconds()); |
| 933 | |
| 934 | constexpr double kDriveBackDistance = 5.15 - 0.4; |
| 935 | StartDrive(-kDriveBackDistance, 0.0, kTwoBallLowDrive, kFinishTurn); |
| 936 | CloseIfBall(); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 937 | if (!WaitForDriveNear(kDriveBackDistance - 0.75, kDoNotTurnCare)) return; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 938 | |
| 939 | StartDrive(0.0, -kBallSmallWallTurn, kTwoBallLowDrive, kFinishTurn); |
| 940 | LOG(INFO, "Straightening up at %f\n", |
| 941 | (aos::time::Time::Now() - start_time).ToSeconds()); |
| 942 | |
| 943 | CloseIfBall(); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 944 | if (!WaitForDriveNear(kDriveBackDistance - 2.3, kDoNotTurnCare)) return; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 945 | |
| 946 | ::y2016::sensors::ball_detector.FetchLatest(); |
| 947 | if (::y2016::sensors::ball_detector.get()) { |
| 948 | const bool ball_detected = ::y2016::sensors::ball_detector->voltage > 2.5; |
| 949 | if (!ball_detected) { |
| 950 | if (!WaitForDriveDone()) return; |
| 951 | LOG(INFO, "Aborting, no ball %f\n", |
| 952 | (aos::time::Time::Now() - start_time).ToSeconds()); |
| 953 | return; |
| 954 | } |
| 955 | } |
| 956 | CloseShooter(); |
| 957 | |
| 958 | BackLongShotLowBarTwoBall(); |
| 959 | LOG(INFO, "Spinning up the shooter wheels\n"); |
| 960 | SetShooterSpeed(640.0); |
| 961 | StartDrive(0.0, 0.0, kTwoBallFastDrive, kSwerveTurn); |
| 962 | |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 963 | if (!WaitForDriveNear(1.80, kDoNotTurnCare)) return; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 964 | StartDrive(0, kShootTurnAngle, kTwoBallFastDrive, kFinishTurn); |
| 965 | BackLongShotTwoBall(); |
| 966 | |
| 967 | if (!WaitForDriveDone()) return; |
| 968 | LOG(INFO, "Second shot done driving at %f seconds\n", |
| 969 | (aos::time::Time::Now() - start_time).ToSeconds()); |
| 970 | WaitForSuperstructure(); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 971 | AlignWithVisionGoal(); |
| 972 | if (ShouldCancel()) return; |
| 973 | |
| 974 | WaitForShooterSpeed(); |
| 975 | if (ShouldCancel()) return; |
| 976 | |
| 977 | // 2.2 with 0.4 of vision. |
| 978 | // 1.8 without any vision. |
| 979 | LOG(INFO, "Going to vision align at %f\n", |
| 980 | (aos::time::Time::Now() - start_time).ToSeconds()); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 981 | WaitForAlignedWithVision(start_time + aos::time::Time::InSeconds(13.5 + kVisionExtra * 2) - |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 982 | aos::time::Time::Now()); |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 983 | BackLongShotTwoBallFinish(); |
| 984 | WaitForSuperstructureProfile(); |
| 985 | if (ShouldCancel()) return; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 986 | LOG(INFO, "Shoot at %f\n", (aos::time::Time::Now() - start_time).ToSeconds()); |
| 987 | Shoot(); |
| 988 | |
| 989 | LOG(INFO, "Second shot at %f seconds\n", |
| 990 | (aos::time::Time::Now() - start_time).ToSeconds()); |
| 991 | if (ShouldCancel()) return; |
| 992 | |
| 993 | SetShooterSpeed(0.0); |
| 994 | LOG(INFO, "Folding superstructure back down\n"); |
| 995 | TuckArm(true, false); |
| 996 | LOG(INFO, "Shot %f\n", (aos::time::Time::Now() - start_time).ToSeconds()); |
| 997 | |
| 998 | WaitForSuperstructureLow(); |
| 999 | |
| 1000 | LOG(INFO, "Done %f\n", (aos::time::Time::Now() - start_time).ToSeconds()); |
| 1001 | } |
| 1002 | |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 1003 | void AutonomousActor::StealAndMoveOverBy(double distance) { |
| 1004 | OpenShooter(); |
| 1005 | MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0}, |
| 1006 | true, 12.0); |
| 1007 | if (ShouldCancel()) return; |
| 1008 | LOG(INFO, "Waiting for the intake to come down.\n"); |
| 1009 | |
| 1010 | WaitForIntake(); |
| 1011 | if (ShouldCancel()) return; |
| 1012 | StartDrive(-distance, M_PI / 2.0, kFastDrive, kStealTurn); |
| 1013 | WaitForBallOrDriveDone(); |
| 1014 | if (ShouldCancel()) return; |
| 1015 | MoveSuperstructure(1.0, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0}, |
| 1016 | true, 12.0); |
| 1017 | |
| 1018 | if (!WaitForDriveDone()) return; |
| 1019 | StartDrive(0.0, M_PI / 2.0, kFastDrive, kStealTurn); |
| 1020 | if (!WaitForDriveDone()) return; |
| 1021 | } |
| 1022 | |
Comran Morshed | e68e373 | 2016-03-12 14:12:11 +0000 | [diff] [blame] | 1023 | bool AutonomousActor::RunAction(const actors::AutonomousActionParams ¶ms) { |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 1024 | aos::time::Time start_time = aos::time::Time::Now(); |
Comran Morshed | e68e373 | 2016-03-12 14:12:11 +0000 | [diff] [blame] | 1025 | LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode); |
| 1026 | |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 1027 | InitializeEncoders(); |
| 1028 | ResetDrivetrain(); |
| 1029 | |
Austin Schuh | 295c2d9 | 2016-05-01 12:28:04 -0700 | [diff] [blame] | 1030 | switch (params.mode) { |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 1031 | case 0: |
| 1032 | LowBarDrive(); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 1033 | if (!WaitForDriveDone()) return true; |
| 1034 | // Get the superstructure to unfold and get ready for shooting. |
| 1035 | LOG(INFO, "Unfolding superstructure\n"); |
| 1036 | FrontLongShot(); |
| 1037 | |
| 1038 | // Spin up the shooter wheels. |
| 1039 | LOG(INFO, "Spinning up the shooter wheels\n"); |
| 1040 | SetShooterSpeed(640.0); |
| 1041 | |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 1042 | break; |
| 1043 | case 1: |
| 1044 | TwoFromMiddleDrive(); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 1045 | if (!WaitForDriveDone()) return true; |
| 1046 | // Get the superstructure to unfold and get ready for shooting. |
| 1047 | LOG(INFO, "Unfolding superstructure\n"); |
| 1048 | FrontMiddleShot(); |
| 1049 | |
| 1050 | // Spin up the shooter wheels. |
| 1051 | LOG(INFO, "Spinning up the shooter wheels\n"); |
| 1052 | SetShooterSpeed(600.0); |
| 1053 | |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 1054 | break; |
| 1055 | case 2: |
| 1056 | OneFromMiddleDrive(true); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 1057 | if (!WaitForDriveDone()) return true; |
| 1058 | // Get the superstructure to unfold and get ready for shooting. |
| 1059 | LOG(INFO, "Unfolding superstructure\n"); |
| 1060 | FrontMiddleShot(); |
| 1061 | |
| 1062 | // Spin up the shooter wheels. |
| 1063 | LOG(INFO, "Spinning up the shooter wheels\n"); |
| 1064 | SetShooterSpeed(600.0); |
| 1065 | |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 1066 | break; |
| 1067 | case 3: |
| 1068 | MiddleDrive(); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 1069 | if (!WaitForDriveDone()) return true; |
| 1070 | // Get the superstructure to unfold and get ready for shooting. |
| 1071 | LOG(INFO, "Unfolding superstructure\n"); |
| 1072 | FrontMiddleShot(); |
| 1073 | |
| 1074 | // Spin up the shooter wheels. |
| 1075 | LOG(INFO, "Spinning up the shooter wheels\n"); |
| 1076 | SetShooterSpeed(600.0); |
| 1077 | |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 1078 | break; |
| 1079 | case 4: |
| 1080 | OneFromMiddleDrive(false); |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 1081 | if (!WaitForDriveDone()) return true; |
| 1082 | // Get the superstructure to unfold and get ready for shooting. |
| 1083 | LOG(INFO, "Unfolding superstructure\n"); |
| 1084 | FrontMiddleShot(); |
| 1085 | |
| 1086 | // Spin up the shooter wheels. |
| 1087 | LOG(INFO, "Spinning up the shooter wheels\n"); |
| 1088 | SetShooterSpeed(600.0); |
| 1089 | |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 1090 | break; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 1091 | case 5: |
| 1092 | TwoBallAuto(); |
Austin Schuh | 23b2180 | 2016-04-03 21:18:56 -0700 | [diff] [blame] | 1093 | return true; |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 1094 | break; |
Austin Schuh | e4ec49c | 2016-04-24 19:07:15 -0700 | [diff] [blame] | 1095 | case 6: |
| 1096 | StealAndMoveOverBy(3.10 + 2 * 52 * 2.54 / 100.0); |
| 1097 | if (ShouldCancel()) return true; |
| 1098 | |
| 1099 | TwoFromMiddleDrive(); |
| 1100 | if (!WaitForDriveDone()) return true; |
| 1101 | // Get the superstructure to unfold and get ready for shooting. |
| 1102 | LOG(INFO, "Unfolding superstructure\n"); |
| 1103 | FrontMiddleShot(); |
| 1104 | |
| 1105 | // Spin up the shooter wheels. |
| 1106 | LOG(INFO, "Spinning up the shooter wheels\n"); |
| 1107 | SetShooterSpeed(600.0); |
| 1108 | |
| 1109 | break; |
| 1110 | case 7: |
| 1111 | StealAndMoveOverBy(2.95 + 52 * 2.54 / 100.0); |
| 1112 | if (ShouldCancel()) return true; |
| 1113 | |
| 1114 | OneFromMiddleDrive(true); |
| 1115 | if (!WaitForDriveDone()) return true; |
| 1116 | // Get the superstructure to unfold and get ready for shooting. |
| 1117 | LOG(INFO, "Unfolding superstructure\n"); |
| 1118 | FrontMiddleShot(); |
| 1119 | |
| 1120 | // Spin up the shooter wheels. |
| 1121 | LOG(INFO, "Spinning up the shooter wheels\n"); |
| 1122 | SetShooterSpeed(600.0); |
| 1123 | |
| 1124 | break; |
| 1125 | case 8: { |
| 1126 | StealAndMoveOverBy(2.95); |
| 1127 | if (ShouldCancel()) return true; |
| 1128 | |
| 1129 | MiddleDrive(); |
| 1130 | if (!WaitForDriveDone()) return true; |
| 1131 | // Get the superstructure to unfold and get ready for shooting. |
| 1132 | LOG(INFO, "Unfolding superstructure\n"); |
| 1133 | FrontMiddleShot(); |
| 1134 | |
| 1135 | // Spin up the shooter wheels. |
| 1136 | LOG(INFO, "Spinning up the shooter wheels\n"); |
| 1137 | SetShooterSpeed(600.0); |
| 1138 | |
| 1139 | } break; |
| 1140 | case 9: { |
| 1141 | StealAndMoveOverBy(1.70); |
| 1142 | if (ShouldCancel()) return true; |
| 1143 | |
| 1144 | OneFromMiddleDrive(false); |
| 1145 | if (!WaitForDriveDone()) return true; |
| 1146 | // Get the superstructure to unfold and get ready for shooting. |
| 1147 | LOG(INFO, "Unfolding superstructure\n"); |
| 1148 | FrontMiddleShot(); |
| 1149 | |
| 1150 | // Spin up the shooter wheels. |
| 1151 | LOG(INFO, "Spinning up the shooter wheels\n"); |
| 1152 | SetShooterSpeed(600.0); |
| 1153 | |
| 1154 | } break; |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 1155 | default: |
Austin Schuh | 6c9bc62 | 2016-03-26 19:44:12 -0700 | [diff] [blame] | 1156 | LOG(ERROR, "Invalid auto mode %d\n", params.mode); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 1157 | return true; |
| 1158 | } |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 1159 | |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 1160 | DoFullShot(); |
| 1161 | |
| 1162 | StartDrive(0.5, 0.0, kMoatDrive, kFastTurn); |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 1163 | if (!WaitForDriveDone()) return true; |
| 1164 | |
Austin Schuh | 3e4a527 | 2016-04-20 20:11:00 -0700 | [diff] [blame] | 1165 | LOG(INFO, "Done %f\n", (aos::time::Time::Now() - start_time).ToSeconds()); |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 1166 | |
| 1167 | ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5), |
| 1168 | ::aos::time::Time::InMS(5) / 2); |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 1169 | |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 1170 | while (!ShouldCancel()) { |
| 1171 | phased_loop.SleepUntilNext(); |
Comran Morshed | e68e373 | 2016-03-12 14:12:11 +0000 | [diff] [blame] | 1172 | } |
Comran Morshed | 435f111 | 2016-03-12 14:20:45 +0000 | [diff] [blame] | 1173 | LOG(DEBUG, "Done running\n"); |
Comran Morshed | e68e373 | 2016-03-12 14:12:11 +0000 | [diff] [blame] | 1174 | |
| 1175 | return true; |
| 1176 | } |
| 1177 | |
| 1178 | ::std::unique_ptr<AutonomousAction> MakeAutonomousAction( |
| 1179 | const ::y2016::actors::AutonomousActionParams ¶ms) { |
| 1180 | return ::std::unique_ptr<AutonomousAction>( |
| 1181 | new AutonomousAction(&::y2016::actors::autonomous_action, params)); |
| 1182 | } |
| 1183 | |
| 1184 | } // namespace actors |
| 1185 | } // namespace y2016 |