Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 1 | #include "y2018/actors/autonomous_actor.h" |
| 2 | |
| 3 | #include <inttypes.h> |
| 4 | |
| 5 | #include <chrono> |
| 6 | #include <cmath> |
| 7 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame^] | 8 | #include "aos/util/phased_loop.h" |
| 9 | #include "aos/logging/logging.h" |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 10 | |
| 11 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 12 | #include "y2018/control_loops/drivetrain/drivetrain_base.h" |
| 13 | |
| 14 | namespace y2018 { |
| 15 | namespace actors { |
| 16 | using ::frc971::ProfileParameters; |
| 17 | |
| 18 | using ::frc971::control_loops::drivetrain_queue; |
| 19 | using ::aos::monotonic_clock; |
| 20 | namespace chrono = ::std::chrono; |
| 21 | namespace this_thread = ::std::this_thread; |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | double DoubleSeconds(monotonic_clock::duration duration) { |
| 26 | return ::std::chrono::duration_cast<::std::chrono::duration<double>>(duration) |
| 27 | .count(); |
| 28 | } |
| 29 | |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 30 | const ProfileParameters kFinalSwitchDrive = {0.5, 1.5}; |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 31 | const ProfileParameters kDrive = {5.0, 2.5}; |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 32 | const ProfileParameters kThirdBoxDrive = {3.0, 2.5}; |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 33 | const ProfileParameters kSlowDrive = {1.5, 2.5}; |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 34 | const ProfileParameters kScaleTurnDrive = {3.0, 2.5}; |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 35 | const ProfileParameters kFarSwitchTurnDrive = {2.0, 2.5}; |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 36 | const ProfileParameters kFarScaleFinalTurnDrive = kFarSwitchTurnDrive; |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 37 | const ProfileParameters kTurn = {4.0, 2.0}; |
| 38 | const ProfileParameters kSweepingTurn = {5.0, 7.0}; |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 39 | const ProfileParameters kFarScaleSweepingTurn = kSweepingTurn; |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 40 | const ProfileParameters kFastTurn = {5.0, 7.0}; |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 41 | const ProfileParameters kReallyFastTurn = {1.5, 9.0}; |
| 42 | |
| 43 | const ProfileParameters kThirdBoxSlowBackup = {0.35, 1.5}; |
| 44 | const ProfileParameters kThirdBoxSlowTurn = {1.5, 4.0}; |
| 45 | |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 46 | const ProfileParameters kThirdBoxPlaceDrive = {4.0, 2.0}; |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 47 | |
| 48 | const ProfileParameters kFinalFrontFarSwitchDrive = {2.0, 2.0}; |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 49 | |
| 50 | } // namespace |
| 51 | |
| 52 | AutonomousActor::AutonomousActor( |
| 53 | ::frc971::autonomous::AutonomousActionQueueGroup *s) |
| 54 | : frc971::autonomous::BaseAutonomousActor( |
| 55 | s, control_loops::drivetrain::GetDrivetrainConfig()) {} |
| 56 | |
| 57 | bool AutonomousActor::RunAction( |
| 58 | const ::frc971::autonomous::AutonomousActionParams ¶ms) { |
| 59 | monotonic_clock::time_point start_time = monotonic_clock::now(); |
| 60 | LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode); |
| 61 | Reset(); |
| 62 | |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 63 | // Switch |
| 64 | /* |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 65 | switch (params.mode) { |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 66 | case 0: |
| 67 | case 2: { |
| 68 | if (FarSwitch(start_time, true)) return true; |
| 69 | } break; |
| 70 | |
| 71 | case 3: |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 72 | case 1: { |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 73 | if (CloseSwitch(start_time)) return true; |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 74 | } break; |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 75 | } |
| 76 | */ |
| 77 | // Scale |
| 78 | switch (params.mode) { |
| 79 | case 0: |
| 80 | case 1: { |
| 81 | if (FarScale(start_time)) return true; |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 82 | } break; |
| 83 | |
| 84 | case 3: |
| 85 | case 2: { |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 86 | if (ThreeCubeAuto(start_time)) return true; |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 87 | } break; |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 88 | } |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 89 | /* |
| 90 | switch (params.mode) { |
| 91 | case 1: { |
| 92 | if (FarScale(start_time)) return true; |
| 93 | //if (CloseSwitch(start_time)) return true; |
| 94 | } break; |
| 95 | case 0: { |
| 96 | if (DriveStraight()) return true; |
| 97 | } break; |
| 98 | case 200: { |
| 99 | if (FarSwitch(start_time)) return true; |
| 100 | } break; |
| 101 | |
| 102 | case 3: |
| 103 | case 2: { |
| 104 | if (ThreeCubeAuto(start_time)) { |
| 105 | return true; |
| 106 | } |
| 107 | } break; |
| 108 | } |
| 109 | */ |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 110 | |
| 111 | LOG(INFO, "Done %f\n", DoubleSeconds(monotonic_clock::now() - start_time)); |
| 112 | |
| 113 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
| 114 | ::std::chrono::milliseconds(5) / 2); |
| 115 | |
| 116 | while (!ShouldCancel()) { |
| 117 | phased_loop.SleepUntilNext(); |
| 118 | } |
| 119 | LOG(DEBUG, "Done running\n"); |
| 120 | |
| 121 | return true; |
| 122 | } |
| 123 | |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 124 | bool AutonomousActor::FarSwitch(monotonic_clock::time_point start_time, |
| 125 | bool drive_behind, bool left) { |
| 126 | const double turn_scalar = left ? 1.0 : -1.0; |
| 127 | if (drive_behind) { |
| 128 | // Start on the left. Hit the switch. |
| 129 | constexpr double kFullDriveLength = 9.93; |
| 130 | constexpr double kTurnDistance = 4.40; |
| 131 | StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn); |
| 132 | set_arm_goal_position(arm::NeutralIndex()); |
| 133 | set_grab_box(false); |
| 134 | SendSuperstructureGoal(); |
| 135 | if (!WaitForDriveProfileNear(kFullDriveLength - (kTurnDistance - 1.4))) |
| 136 | return true; |
| 137 | StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn); |
| 138 | |
| 139 | if (!WaitForDriveProfileNear(kFullDriveLength - kTurnDistance)) return true; |
| 140 | StartDrive(0.0, turn_scalar * (-M_PI / 2.0), kFarSwitchTurnDrive, kSweepingTurn); |
| 141 | if (!WaitForTurnProfileDone()) return true; |
| 142 | |
| 143 | // Now, close so let's move the arm up. |
| 144 | set_arm_goal_position(arm::FrontSwitchAutoIndex()); |
| 145 | SendSuperstructureGoal(); |
| 146 | |
| 147 | StartDrive(0.0, 0.0, kDrive, kTurn); |
| 148 | if (!WaitForDriveProfileNear(2.0)) return true; |
| 149 | StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kFastTurn); |
| 150 | |
| 151 | if (!WaitForDriveProfileNear(1.3)) return true; |
| 152 | |
| 153 | StartDrive(0.0, turn_scalar * M_PI / 2.0, kFarSwitchTurnDrive, kFastTurn); |
| 154 | if (!WaitForTurnProfileDone()) return true; |
| 155 | |
| 156 | constexpr double kGentlePushDrive = 0.70; |
| 157 | StartDrive(kGentlePushDrive, 0.0, kSlowDrive, kTurn); |
| 158 | |
| 159 | if (!WaitForDriveProfileNear(kGentlePushDrive - 0.25)) return true; |
| 160 | // Turn down the peak voltage when we push against the wall so we don't blow |
| 161 | // breakers or pull the battery voltage down too far. |
| 162 | set_max_drivetrain_voltage(6.0); |
| 163 | StartDrive(0.00, 0.0, kFinalSwitchDrive, kTurn); |
| 164 | |
| 165 | if (!WaitForArmTrajectoryClose(0.001)) return true; |
| 166 | LOG(INFO, "Arm close at %f\n", |
| 167 | DoubleSeconds(monotonic_clock::now() - start_time)); |
| 168 | |
| 169 | ::std::this_thread::sleep_for(chrono::milliseconds(1000)); |
| 170 | |
| 171 | set_open_claw(true); |
| 172 | SendSuperstructureGoal(); |
| 173 | |
| 174 | ::std::this_thread::sleep_for(chrono::milliseconds(1500)); |
| 175 | set_arm_goal_position(arm::NeutralIndex()); |
| 176 | SendSuperstructureGoal(); |
| 177 | if (ShouldCancel()) return true; |
| 178 | set_max_drivetrain_voltage(12.0); |
| 179 | StartDrive(-0.5, 0.0, kDrive, kTurn); |
| 180 | if (!WaitForDriveProfileDone()) return true; |
| 181 | } else { |
| 182 | // Start on the left. Hit the switch. |
| 183 | constexpr double kFullDriveLength = 5.55; |
| 184 | constexpr double kTurnDistance = 0.35; |
| 185 | StartDrive(-kFullDriveLength, 0.0, kFarSwitchTurnDrive, kTurn); |
| 186 | |
| 187 | if (!WaitForDriveProfileNear(kFullDriveLength - kTurnDistance)) return true; |
| 188 | StartDrive(0.0, turn_scalar * (-M_PI / 2.0), kFarSwitchTurnDrive, |
| 189 | kSweepingTurn); |
| 190 | if (!WaitForTurnProfileDone()) return true; |
| 191 | StartDrive(0.0, 0.0, kDrive, kTurn); |
| 192 | if (!WaitForDriveProfileDone()) return true; |
| 193 | |
| 194 | // Now, close so let's move the arm up. |
| 195 | set_arm_goal_position(arm::FrontSwitchIndex()); |
| 196 | SendSuperstructureGoal(); |
| 197 | |
| 198 | StartDrive(0.0, turn_scalar * (-M_PI / 2.0), kDrive, kFastTurn); |
| 199 | if (!WaitForTurnProfileDone()) return true; |
| 200 | |
| 201 | set_max_drivetrain_voltage(10.0); |
| 202 | StartDrive(1.1, 0.0, kDrive, kTurn); |
| 203 | if (!WaitForArmTrajectoryClose(0.001)) return true; |
| 204 | if (!WaitForDriveNear(0.6, M_PI / 2.0)) return true; |
| 205 | StartDrive(0.0, 0.0, kFinalFrontFarSwitchDrive, kTurn); |
| 206 | |
| 207 | if (!WaitForDriveNear(0.3, M_PI / 2.0)) return true; |
| 208 | set_max_drivetrain_voltage(6.0); |
| 209 | StartDrive(0.0, 0.0, kFinalFrontFarSwitchDrive, kTurn); |
| 210 | |
| 211 | // if (!WaitForDriveNear(0.2, M_PI / 2.0)) return true; |
| 212 | ::std::this_thread::sleep_for(chrono::milliseconds(300)); |
| 213 | |
| 214 | set_open_claw(true); |
| 215 | SendSuperstructureGoal(); |
| 216 | |
| 217 | ::std::this_thread::sleep_for(chrono::milliseconds(1000)); |
| 218 | set_arm_goal_position(arm::NeutralIndex()); |
| 219 | SendSuperstructureGoal(); |
| 220 | if (ShouldCancel()) return true; |
| 221 | set_max_drivetrain_voltage(12.0); |
| 222 | StartDrive(-0.5, 0.0, kDrive, kTurn); |
| 223 | if (!WaitForDriveProfileDone()) return true; |
| 224 | } |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | bool AutonomousActor::FarScale(monotonic_clock::time_point start_time) { |
| 229 | // Start on the left. Hit the switch. |
| 230 | constexpr double kFullDriveLength = 11.40; |
| 231 | constexpr double kFirstTurnDistance = 4.40; |
| 232 | constexpr double kSecondTurnDistance = 9.30; |
| 233 | StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn); |
| 234 | if (!WaitForDriveProfileNear(kFullDriveLength - (kFirstTurnDistance - 1.4))) |
| 235 | return true; |
| 236 | StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn); |
| 237 | |
| 238 | if (!WaitForDriveProfileNear(kFullDriveLength - kFirstTurnDistance)) return true; |
| 239 | StartDrive(0.0, -M_PI / 2.0, kFarSwitchTurnDrive, kSweepingTurn); |
| 240 | set_arm_goal_position(arm::BackHighBoxIndex()); |
| 241 | SendSuperstructureGoal(); |
| 242 | if (!WaitForTurnProfileDone()) return true; |
| 243 | |
| 244 | StartDrive(0.0, 0.0, kDrive, kTurn); |
| 245 | |
| 246 | if (!WaitForDriveProfileNear(kFullDriveLength - (kSecondTurnDistance - 1.4))) |
| 247 | return true; |
| 248 | |
| 249 | StartDrive(0.0, 0.0, kFarScaleFinalTurnDrive, kTurn); |
| 250 | if (!WaitForDriveProfileNear(kFullDriveLength - (kSecondTurnDistance))) |
| 251 | return true; |
| 252 | LOG(INFO, "Final turn at %f\n", |
| 253 | DoubleSeconds(monotonic_clock::now() - start_time)); |
| 254 | |
| 255 | StartDrive(0.0, M_PI / 2.0, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn); |
| 256 | if (!WaitForDriveProfileNear(0.15)) return true; |
| 257 | |
| 258 | StartDrive(0.0, 0.3, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn); |
| 259 | |
| 260 | LOG(INFO, "Dropping at %f\n", |
| 261 | DoubleSeconds(monotonic_clock::now() - start_time)); |
| 262 | set_open_claw(true); |
| 263 | SendSuperstructureGoal(); |
| 264 | |
| 265 | ::std::this_thread::sleep_for(chrono::milliseconds(1000)); |
| 266 | LOG(INFO, "Backing up at %f\n", |
| 267 | DoubleSeconds(monotonic_clock::now() - start_time)); |
| 268 | |
| 269 | StartDrive(1.5, -0.55, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn); |
| 270 | |
| 271 | if (!WaitForDriveProfileNear(1.4)) return true; |
| 272 | set_arm_goal_position(arm::NeutralIndex()); |
| 273 | set_open_claw(false); |
| 274 | set_grab_box(true); |
| 275 | SendSuperstructureGoal(); |
| 276 | |
| 277 | if (!WaitForDriveProfileNear(0.3)) return true; |
| 278 | |
| 279 | set_intake_angle(0.15); |
| 280 | set_roller_voltage(10.0); |
| 281 | SendSuperstructureGoal(); |
| 282 | |
| 283 | set_max_drivetrain_voltage(6.0); |
| 284 | StartDrive(0.0, 0.0, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn); |
| 285 | |
| 286 | if (!WaitForDriveProfileDone()) return true; |
| 287 | if (!WaitForTurnProfileDone()) return true; |
| 288 | |
| 289 | StartDrive(0.2, 0.0, kThirdBoxSlowBackup, kThirdBoxSlowTurn); |
| 290 | |
| 291 | if (!WaitForBoxGrabed()) return true; |
| 292 | set_arm_goal_position(arm::BackHighBoxIndex()); |
| 293 | set_intake_angle(-3.0); |
| 294 | set_roller_voltage(0.0); |
| 295 | set_grab_box(false); |
| 296 | SendSuperstructureGoal(); |
| 297 | |
| 298 | StartDrive(-1.40, 0.15, kThirdBoxPlaceDrive, kFarScaleSweepingTurn); |
| 299 | |
| 300 | if (!WaitForDriveProfileNear(0.1)) return true; |
| 301 | |
| 302 | StartDrive(0.0, 0.5, kThirdBoxPlaceDrive, kFarScaleSweepingTurn); |
| 303 | |
| 304 | if (!WaitForDriveProfileDone()) return true; |
| 305 | if (!WaitForTurnProfileDone()) return true; |
| 306 | ::std::this_thread::sleep_for(chrono::milliseconds(500)); |
| 307 | |
| 308 | LOG(INFO, "Dropping second box at %f\n", |
| 309 | DoubleSeconds(monotonic_clock::now() - start_time)); |
| 310 | set_open_claw(true); |
| 311 | SendSuperstructureGoal(); |
| 312 | |
| 313 | ::std::this_thread::sleep_for(chrono::milliseconds(1000)); |
| 314 | |
| 315 | StartDrive(1.4, -0.7, kThirdBoxPlaceDrive, kFarScaleSweepingTurn); |
| 316 | ::std::this_thread::sleep_for(chrono::milliseconds(200)); |
| 317 | set_arm_goal_position(arm::NeutralIndex()); |
| 318 | set_open_claw(false); |
| 319 | SendSuperstructureGoal(); |
| 320 | |
| 321 | if (!WaitForDriveProfileNear(1.0)) return true; |
| 322 | |
| 323 | StartDrive(0.0, -0.6, kThirdBoxPlaceDrive, kFarScaleSweepingTurn); |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | bool AutonomousActor::FarReadyScale(monotonic_clock::time_point start_time) { |
| 328 | // Start on the left. Hit the switch. |
| 329 | constexpr double kFullDriveLength = 7.5; |
| 330 | constexpr double kFirstTurnDistance = 4.40; |
| 331 | StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn); |
| 332 | if (!WaitForDriveProfileNear(kFullDriveLength - (kFirstTurnDistance - 1.4))) |
| 333 | return true; |
| 334 | StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn); |
| 335 | |
| 336 | if (!WaitForDriveProfileNear(kFullDriveLength - kFirstTurnDistance)) return true; |
| 337 | StartDrive(0.0, -M_PI / 2.0, kFarSwitchTurnDrive, kSweepingTurn); |
| 338 | set_arm_goal_position(arm::UpIndex()); |
| 339 | SendSuperstructureGoal(); |
| 340 | LOG(INFO, "Lifting arm at %f\n", |
| 341 | DoubleSeconds(monotonic_clock::now() - start_time)); |
| 342 | if (!WaitForTurnProfileDone()) return true; |
| 343 | |
| 344 | StartDrive(0.0, 0.0, kDrive, kTurn); |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | bool AutonomousActor::DriveStraight() { |
| 349 | StartDrive(-3.2, 0.0, kDrive, kTurn); |
| 350 | if (!WaitForDriveProfileDone()) return true; |
| 351 | return false; |
| 352 | } |
| 353 | |
| 354 | bool AutonomousActor::CloseSwitch(monotonic_clock::time_point start_time, |
| 355 | bool left) { |
| 356 | const double turn_scalar = left ? 1.0 : -1.0; |
| 357 | |
| 358 | constexpr double kDriveDistance = 3.2; |
| 359 | // Start on the left. Drive, arc a turn, and drop in the close switch. |
| 360 | StartDrive(-kDriveDistance, 0.0, kDrive, kTurn); |
| 361 | if (!WaitForDriveNear(2.5, M_PI / 2.0)) return true; |
| 362 | |
| 363 | // Now, close so let's move the arm up. |
| 364 | set_arm_goal_position(arm::BackSwitchIndex()); |
| 365 | SendSuperstructureGoal(); |
| 366 | |
| 367 | StartDrive(0.0, 0.0, kSlowDrive, kSweepingTurn); |
| 368 | if (!WaitForDriveNear(1.6, M_PI / 2.0)) return true; |
| 369 | |
| 370 | StartDrive(0.0, turn_scalar * (-M_PI / 4.0 - 0.2), kSlowDrive, kSweepingTurn); |
| 371 | if (!WaitForDriveNear(0.2, 0.2)) return true; |
| 372 | set_max_drivetrain_voltage(6.0); |
| 373 | LOG(INFO, "Lowered drivetrain voltage %f\n", |
| 374 | DoubleSeconds(monotonic_clock::now() - start_time)); |
| 375 | ::std::this_thread::sleep_for(chrono::milliseconds(300)); |
| 376 | |
| 377 | set_open_claw(true); |
| 378 | SendSuperstructureGoal(); |
| 379 | |
| 380 | ::std::this_thread::sleep_for(chrono::milliseconds(500)); |
| 381 | set_max_drivetrain_voltage(12.0); |
| 382 | StartDrive(0.7, 0.0, kDrive, kTurn); |
| 383 | if (!WaitForTurnProfileDone()) return true; |
| 384 | |
| 385 | set_arm_goal_position(arm::NeutralIndex()); |
| 386 | SendSuperstructureGoal(); |
| 387 | return false; |
| 388 | } |
| 389 | |
| 390 | bool AutonomousActor::ThreeCubeAuto(monotonic_clock::time_point start_time) { |
| 391 | // Start on the left. Hit the scale. |
| 392 | constexpr double kDriveDistance = 6.95; |
| 393 | // Distance and angle to do the big drive to the third cube. |
| 394 | constexpr double kThirdCubeDrive = 1.57 + 0.05 + 0.05; |
| 395 | constexpr double kThirdCubeTurn = M_PI / 4.0 + 0.1; |
| 396 | // Angle to do the slow pickup turn on the third box. |
| 397 | constexpr double kThirdBoxEndTurnAngle = 0.30; |
| 398 | |
| 399 | // Distance to drive back to the scale with the third cube. |
| 400 | constexpr double kThirdCubeDropDrive = kThirdCubeDrive + 0.40; |
| 401 | |
| 402 | // Drive. |
| 403 | StartDrive(-kDriveDistance, 0.0, kDrive, kTurn); |
| 404 | if (!WaitForDriveNear(kDriveDistance - 1.0, M_PI / 2.0)) return true; |
| 405 | // Once we are away from the wall, start the arm. |
| 406 | set_arm_goal_position(arm::BackMiddle2BoxIndex()); |
| 407 | SendSuperstructureGoal(); |
| 408 | |
| 409 | // We are starting to get close. Slow down for the turn. |
| 410 | if (!WaitForDriveNear(4.0, M_PI / 2.0)) return true; |
| 411 | StartDrive(0.0, 0.0, kScaleTurnDrive, kFastTurn); |
| 412 | |
| 413 | // Once we've gotten slowed down a bit, start turning. |
| 414 | if (!WaitForDriveNear(3.25, M_PI / 2.0)) return true; |
| 415 | StartDrive(0.0, -M_PI / 6.0, kScaleTurnDrive, kFastTurn); |
| 416 | |
| 417 | if (!WaitForDriveNear(1.0, M_PI / 2.0)) return true; |
| 418 | StartDrive(0.0, M_PI / 6.0, kScaleTurnDrive, kFastTurn); |
| 419 | |
| 420 | // Get close and open the claw. |
| 421 | if (!WaitForDriveNear(0.15, 0.25)) return true; |
| 422 | set_open_claw(true); |
| 423 | SendSuperstructureGoal(); |
| 424 | set_intake_angle(-0.60); |
| 425 | LOG(INFO, "Dropped first box %f\n", |
| 426 | DoubleSeconds(monotonic_clock::now() - start_time)); |
| 427 | |
| 428 | ::std::this_thread::sleep_for(chrono::milliseconds(700)); |
| 429 | |
| 430 | set_grab_box(true); |
| 431 | SendSuperstructureGoal(); |
| 432 | |
| 433 | LOG(INFO, "Starting second box drive %f\n", |
| 434 | DoubleSeconds(monotonic_clock::now() - start_time)); |
| 435 | constexpr double kSecondBoxSwerveAngle = 0.35; |
| 436 | constexpr double kSecondBoxDrive = 1.38; |
| 437 | StartDrive(kSecondBoxDrive, 0.0, kDrive, kFastTurn); |
| 438 | if (!WaitForDriveNear(kSecondBoxDrive - 0.2, M_PI / 2.0)) return true; |
| 439 | |
| 440 | StartDrive(0.0, kSecondBoxSwerveAngle, kDrive, kFastTurn); |
| 441 | if (!WaitForDriveNear(0.5, M_PI / 2.0)) return true; |
| 442 | |
| 443 | set_open_claw(true); |
| 444 | set_disable_box_correct(false); |
| 445 | SendSuperstructureGoal(); |
| 446 | |
| 447 | StartDrive(0.0, -kSecondBoxSwerveAngle, kDrive, kFastTurn); |
| 448 | |
| 449 | if (!WaitForDriveProfileDone()) return true; |
| 450 | set_max_drivetrain_voltage(6.0); |
| 451 | StartDrive(0.0, 0.0, kDrive, kFastTurn); |
| 452 | |
| 453 | set_intake_angle(0.15); |
| 454 | set_arm_goal_position(arm::BackHighBoxIndex()); |
| 455 | set_open_claw(false); |
| 456 | |
| 457 | set_roller_voltage(10.0); |
| 458 | SendSuperstructureGoal(); |
| 459 | |
| 460 | LOG(INFO, "Grabbing second box %f\n", |
| 461 | DoubleSeconds(monotonic_clock::now() - start_time)); |
| 462 | ::std::this_thread::sleep_for(chrono::milliseconds(200)); |
| 463 | StartDrive(-0.04, 0.0, kThirdBoxSlowBackup, kThirdBoxSlowTurn); |
| 464 | |
| 465 | if (!WaitForBoxGrabed()) return true; |
| 466 | set_max_drivetrain_voltage(12.0); |
| 467 | |
| 468 | LOG(INFO, "Got second box %f\n", |
| 469 | DoubleSeconds(monotonic_clock::now() - start_time)); |
| 470 | ::std::this_thread::sleep_for(chrono::milliseconds(500)); |
| 471 | |
| 472 | set_grab_box(false); |
| 473 | //set_arm_goal_position(arm::UpIndex()); |
| 474 | set_arm_goal_position(arm::BackHighBoxIndex()); |
| 475 | set_roller_voltage(0.0); |
| 476 | set_disable_box_correct(false); |
| 477 | SendSuperstructureGoal(); |
| 478 | LOG(INFO, "Driving to place second box %f\n", |
| 479 | DoubleSeconds(monotonic_clock::now() - start_time)); |
| 480 | |
| 481 | StartDrive(-kSecondBoxDrive + 0.16, kSecondBoxSwerveAngle, kDrive, kFastTurn); |
| 482 | if (!WaitForDriveNear(0.4, M_PI / 2.0)) return true; |
| 483 | |
| 484 | constexpr double kSecondBoxEndExtraAngle = 0.3; |
| 485 | StartDrive(0.0, -kSecondBoxSwerveAngle - kSecondBoxEndExtraAngle, kDrive, |
| 486 | kFastTurn); |
| 487 | |
| 488 | LOG(INFO, "Starting throw %f\n", |
| 489 | DoubleSeconds(monotonic_clock::now() - start_time)); |
| 490 | if (!WaitForDriveNear(0.4, M_PI / 2.0)) return true; |
| 491 | if (!WaitForArmTrajectoryClose(0.25)) return true; |
| 492 | SendSuperstructureGoal(); |
| 493 | |
| 494 | // Throw the box. |
| 495 | //if (!WaitForArmTrajectoryClose(0.20)) return true; |
| 496 | if (!WaitForDriveNear(0.2, M_PI / 2.0)) return true; |
| 497 | |
| 498 | set_open_claw(true); |
| 499 | set_intake_angle(-M_PI / 4.0); |
| 500 | LOG(INFO, "Releasing second box %f\n", |
| 501 | DoubleSeconds(monotonic_clock::now() - start_time)); |
| 502 | SendSuperstructureGoal(); |
| 503 | |
| 504 | ::std::this_thread::sleep_for(chrono::milliseconds(700)); |
| 505 | set_open_claw(false); |
| 506 | SendSuperstructureGoal(); |
| 507 | |
| 508 | LOG(INFO, "Driving to third box %f\n", |
| 509 | DoubleSeconds(monotonic_clock::now() - start_time)); |
| 510 | StartDrive(kThirdCubeDrive, kSecondBoxEndExtraAngle, kThirdBoxDrive, |
| 511 | kFastTurn); |
| 512 | if (!WaitForDriveNear(kThirdCubeDrive - 0.1, M_PI / 4.0)) return true; |
| 513 | set_grab_box(true); |
| 514 | SendSuperstructureGoal(); |
| 515 | |
| 516 | StartDrive(0.0, kThirdCubeTurn, kThirdBoxDrive, kFastTurn); |
| 517 | if (!WaitForDriveNear(0.05, M_PI / 4.0)) return true; |
| 518 | |
| 519 | set_intake_angle(0.05); |
| 520 | set_roller_voltage(9.0); |
| 521 | SendSuperstructureGoal(); |
| 522 | |
| 523 | if (!WaitForDriveProfileDone()) return true; |
| 524 | if (!WaitForTurnProfileDone()) return true; |
| 525 | StartDrive(0.35, kThirdBoxEndTurnAngle, kThirdBoxSlowBackup, |
| 526 | kThirdBoxSlowTurn); |
| 527 | if (!WaitForDriveProfileDone()) return true; |
| 528 | |
| 529 | LOG(INFO, "Waiting for third box %f\n", |
| 530 | DoubleSeconds(monotonic_clock::now() - start_time)); |
| 531 | if (!WaitForBoxGrabed()) return true; |
| 532 | LOG(INFO, "Third box grabbed %f\n", |
| 533 | DoubleSeconds(monotonic_clock::now() - start_time)); |
| 534 | const bool too_late = |
| 535 | monotonic_clock::now() > start_time + chrono::milliseconds(12500); |
| 536 | if (too_late) { |
| 537 | LOG(INFO, "Third box too long, going up. %f\n", |
| 538 | DoubleSeconds(monotonic_clock::now() - start_time)); |
| 539 | set_grab_box(false); |
| 540 | set_arm_goal_position(arm::UpIndex()); |
| 541 | set_roller_voltage(0.0); |
| 542 | SendSuperstructureGoal(); |
| 543 | } |
| 544 | ::std::this_thread::sleep_for(chrono::milliseconds(400)); |
| 545 | |
| 546 | set_grab_box(false); |
| 547 | if (!too_late) { |
| 548 | set_arm_goal_position(arm::BackMiddle2BoxIndex()); |
| 549 | } |
| 550 | set_roller_voltage(0.0); |
| 551 | SendSuperstructureGoal(); |
| 552 | |
| 553 | StartDrive(-kThirdCubeDropDrive, 0.0, kThirdBoxPlaceDrive, kReallyFastTurn); |
| 554 | |
| 555 | if (!WaitForDriveNear(kThirdCubeDropDrive - 0.23, M_PI / 4.0)) return true; |
| 556 | StartDrive(0.0, -kThirdCubeTurn - 0.2 - kThirdBoxEndTurnAngle - 0.3, |
| 557 | kThirdBoxPlaceDrive, kReallyFastTurn); |
| 558 | |
| 559 | if (!WaitForDriveNear(0.30, M_PI / 4.0 + 0.2)) return true; |
| 560 | |
| 561 | if (!too_late) { |
| 562 | set_open_claw(true); |
| 563 | set_intake_angle(-M_PI / 4.0); |
| 564 | set_roller_voltage(0.0); |
| 565 | SendSuperstructureGoal(); |
| 566 | |
| 567 | LOG(INFO, "Final open %f\n", |
| 568 | DoubleSeconds(monotonic_clock::now() - start_time)); |
| 569 | } |
| 570 | |
| 571 | if (!WaitForDriveProfileDone()) return true; |
| 572 | if (!WaitForTurnProfileDone()) return true; |
| 573 | |
| 574 | ::std::this_thread::sleep_for(chrono::milliseconds(14750) - |
| 575 | (monotonic_clock::now() - start_time)); |
| 576 | |
| 577 | set_arm_goal_position(arm::UpIndex()); |
| 578 | SendSuperstructureGoal(); |
| 579 | |
| 580 | ::std::this_thread::sleep_for(chrono::milliseconds(15000) - |
| 581 | (monotonic_clock::now() - start_time)); |
| 582 | |
| 583 | set_close_claw(true); |
| 584 | SendSuperstructureGoal(); |
| 585 | return false; |
| 586 | } |
| 587 | |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 588 | } // namespace actors |
| 589 | } // namespace y2018 |