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