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/logging/logging.h" |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 9 | #include "aos/util/phased_loop.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 | |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 18 | using ::aos::monotonic_clock; |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 19 | using ::frc971::control_loops::drivetrain_queue; |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 20 | namespace chrono = ::std::chrono; |
| 21 | namespace this_thread = ::std::this_thread; |
| 22 | |
| 23 | namespace { |
| 24 | |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 25 | const ProfileParameters kFinalSwitchDrive = {0.5, 1.5}; |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 26 | const ProfileParameters kDrive = {5.0, 2.5}; |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 27 | const ProfileParameters kThirdBoxDrive = {3.0, 2.5}; |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 28 | const ProfileParameters kSlowDrive = {1.5, 2.5}; |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 29 | const ProfileParameters kScaleTurnDrive = {3.0, 2.5}; |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 30 | const ProfileParameters kFarSwitchTurnDrive = {2.0, 2.5}; |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 31 | const ProfileParameters kFarScaleFinalTurnDrive = kFarSwitchTurnDrive; |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 32 | const ProfileParameters kTurn = {4.0, 2.0}; |
| 33 | const ProfileParameters kSweepingTurn = {5.0, 7.0}; |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 34 | const ProfileParameters kFarScaleSweepingTurn = kSweepingTurn; |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 35 | const ProfileParameters kFastTurn = {5.0, 7.0}; |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 36 | const ProfileParameters kReallyFastTurn = {1.5, 9.0}; |
| 37 | |
| 38 | const ProfileParameters kThirdBoxSlowBackup = {0.35, 1.5}; |
| 39 | const ProfileParameters kThirdBoxSlowTurn = {1.5, 4.0}; |
| 40 | |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 41 | const ProfileParameters kThirdBoxPlaceDrive = {4.0, 2.0}; |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 42 | |
| 43 | const ProfileParameters kFinalFrontFarSwitchDrive = {2.0, 2.0}; |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 44 | |
| 45 | } // namespace |
| 46 | |
| 47 | AutonomousActor::AutonomousActor( |
Austin Schuh | eb99d07 | 2019-05-12 21:03:38 -0700 | [diff] [blame] | 48 | ::aos::EventLoop *event_loop, |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 49 | ::frc971::autonomous::AutonomousActionQueueGroup *s) |
| 50 | : frc971::autonomous::BaseAutonomousActor( |
Austin Schuh | eb99d07 | 2019-05-12 21:03:38 -0700 | [diff] [blame] | 51 | event_loop, s, control_loops::drivetrain::GetDrivetrainConfig()) {} |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 52 | |
| 53 | bool AutonomousActor::RunAction( |
| 54 | const ::frc971::autonomous::AutonomousActionParams ¶ms) { |
| 55 | monotonic_clock::time_point start_time = monotonic_clock::now(); |
| 56 | LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode); |
| 57 | Reset(); |
| 58 | |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 59 | // Switch |
| 60 | /* |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 61 | switch (params.mode) { |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 62 | case 0: |
| 63 | case 2: { |
| 64 | if (FarSwitch(start_time, true)) return true; |
| 65 | } break; |
| 66 | |
| 67 | case 3: |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 68 | case 1: { |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 69 | if (CloseSwitch(start_time)) return true; |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 70 | } break; |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 71 | } |
| 72 | */ |
| 73 | // Scale |
| 74 | switch (params.mode) { |
| 75 | case 0: |
| 76 | case 1: { |
| 77 | if (FarScale(start_time)) return true; |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 78 | } break; |
| 79 | |
| 80 | case 3: |
| 81 | case 2: { |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 82 | if (ThreeCubeAuto(start_time)) return true; |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 83 | } break; |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 84 | } |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 85 | /* |
| 86 | switch (params.mode) { |
| 87 | case 1: { |
| 88 | if (FarScale(start_time)) return true; |
| 89 | //if (CloseSwitch(start_time)) return true; |
| 90 | } break; |
| 91 | case 0: { |
| 92 | if (DriveStraight()) return true; |
| 93 | } break; |
| 94 | case 200: { |
| 95 | if (FarSwitch(start_time)) return true; |
| 96 | } break; |
| 97 | |
| 98 | case 3: |
| 99 | case 2: { |
| 100 | if (ThreeCubeAuto(start_time)) { |
| 101 | return true; |
| 102 | } |
| 103 | } break; |
| 104 | } |
| 105 | */ |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 106 | |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 107 | LOG(INFO, "Done %f\n", |
| 108 | ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time)); |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 109 | |
| 110 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
| 111 | ::std::chrono::milliseconds(5) / 2); |
| 112 | |
| 113 | while (!ShouldCancel()) { |
| 114 | phased_loop.SleepUntilNext(); |
| 115 | } |
| 116 | LOG(DEBUG, "Done running\n"); |
| 117 | |
| 118 | return true; |
| 119 | } |
| 120 | |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 121 | bool AutonomousActor::FarSwitch(monotonic_clock::time_point start_time, |
| 122 | bool drive_behind, bool left) { |
| 123 | const double turn_scalar = left ? 1.0 : -1.0; |
| 124 | if (drive_behind) { |
| 125 | // Start on the left. Hit the switch. |
| 126 | constexpr double kFullDriveLength = 9.93; |
| 127 | constexpr double kTurnDistance = 4.40; |
| 128 | StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn); |
| 129 | set_arm_goal_position(arm::NeutralIndex()); |
| 130 | set_grab_box(false); |
| 131 | SendSuperstructureGoal(); |
| 132 | if (!WaitForDriveProfileNear(kFullDriveLength - (kTurnDistance - 1.4))) |
| 133 | return true; |
| 134 | StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn); |
| 135 | |
| 136 | if (!WaitForDriveProfileNear(kFullDriveLength - kTurnDistance)) return true; |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 137 | StartDrive(0.0, turn_scalar * (-M_PI / 2.0), kFarSwitchTurnDrive, |
| 138 | kSweepingTurn); |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 139 | if (!WaitForTurnProfileDone()) return true; |
| 140 | |
| 141 | // Now, close so let's move the arm up. |
| 142 | set_arm_goal_position(arm::FrontSwitchAutoIndex()); |
| 143 | SendSuperstructureGoal(); |
| 144 | |
| 145 | StartDrive(0.0, 0.0, kDrive, kTurn); |
| 146 | if (!WaitForDriveProfileNear(2.0)) return true; |
| 147 | StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kFastTurn); |
| 148 | |
| 149 | if (!WaitForDriveProfileNear(1.3)) return true; |
| 150 | |
| 151 | StartDrive(0.0, turn_scalar * M_PI / 2.0, kFarSwitchTurnDrive, kFastTurn); |
| 152 | if (!WaitForTurnProfileDone()) return true; |
| 153 | |
| 154 | constexpr double kGentlePushDrive = 0.70; |
| 155 | StartDrive(kGentlePushDrive, 0.0, kSlowDrive, kTurn); |
| 156 | |
| 157 | if (!WaitForDriveProfileNear(kGentlePushDrive - 0.25)) return true; |
| 158 | // Turn down the peak voltage when we push against the wall so we don't blow |
| 159 | // breakers or pull the battery voltage down too far. |
| 160 | set_max_drivetrain_voltage(6.0); |
| 161 | StartDrive(0.00, 0.0, kFinalSwitchDrive, kTurn); |
| 162 | |
| 163 | if (!WaitForArmTrajectoryClose(0.001)) return true; |
| 164 | LOG(INFO, "Arm close at %f\n", |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 165 | ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time)); |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 166 | |
| 167 | ::std::this_thread::sleep_for(chrono::milliseconds(1000)); |
| 168 | |
| 169 | set_open_claw(true); |
| 170 | SendSuperstructureGoal(); |
| 171 | |
| 172 | ::std::this_thread::sleep_for(chrono::milliseconds(1500)); |
| 173 | set_arm_goal_position(arm::NeutralIndex()); |
| 174 | SendSuperstructureGoal(); |
| 175 | if (ShouldCancel()) return true; |
| 176 | set_max_drivetrain_voltage(12.0); |
| 177 | StartDrive(-0.5, 0.0, kDrive, kTurn); |
| 178 | if (!WaitForDriveProfileDone()) return true; |
| 179 | } else { |
| 180 | // Start on the left. Hit the switch. |
| 181 | constexpr double kFullDriveLength = 5.55; |
| 182 | constexpr double kTurnDistance = 0.35; |
| 183 | StartDrive(-kFullDriveLength, 0.0, kFarSwitchTurnDrive, kTurn); |
| 184 | |
| 185 | if (!WaitForDriveProfileNear(kFullDriveLength - kTurnDistance)) return true; |
| 186 | StartDrive(0.0, turn_scalar * (-M_PI / 2.0), kFarSwitchTurnDrive, |
| 187 | kSweepingTurn); |
| 188 | if (!WaitForTurnProfileDone()) return true; |
| 189 | StartDrive(0.0, 0.0, kDrive, kTurn); |
| 190 | if (!WaitForDriveProfileDone()) return true; |
| 191 | |
| 192 | // Now, close so let's move the arm up. |
| 193 | set_arm_goal_position(arm::FrontSwitchIndex()); |
| 194 | SendSuperstructureGoal(); |
| 195 | |
| 196 | StartDrive(0.0, turn_scalar * (-M_PI / 2.0), kDrive, kFastTurn); |
| 197 | if (!WaitForTurnProfileDone()) return true; |
| 198 | |
| 199 | set_max_drivetrain_voltage(10.0); |
| 200 | StartDrive(1.1, 0.0, kDrive, kTurn); |
| 201 | if (!WaitForArmTrajectoryClose(0.001)) return true; |
| 202 | if (!WaitForDriveNear(0.6, M_PI / 2.0)) return true; |
| 203 | StartDrive(0.0, 0.0, kFinalFrontFarSwitchDrive, kTurn); |
| 204 | |
| 205 | if (!WaitForDriveNear(0.3, M_PI / 2.0)) return true; |
| 206 | set_max_drivetrain_voltage(6.0); |
| 207 | StartDrive(0.0, 0.0, kFinalFrontFarSwitchDrive, kTurn); |
| 208 | |
| 209 | // if (!WaitForDriveNear(0.2, M_PI / 2.0)) return true; |
| 210 | ::std::this_thread::sleep_for(chrono::milliseconds(300)); |
| 211 | |
| 212 | set_open_claw(true); |
| 213 | SendSuperstructureGoal(); |
| 214 | |
| 215 | ::std::this_thread::sleep_for(chrono::milliseconds(1000)); |
| 216 | set_arm_goal_position(arm::NeutralIndex()); |
| 217 | SendSuperstructureGoal(); |
| 218 | if (ShouldCancel()) return true; |
| 219 | set_max_drivetrain_voltage(12.0); |
| 220 | StartDrive(-0.5, 0.0, kDrive, kTurn); |
| 221 | if (!WaitForDriveProfileDone()) return true; |
| 222 | } |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | bool AutonomousActor::FarScale(monotonic_clock::time_point start_time) { |
| 227 | // Start on the left. Hit the switch. |
| 228 | constexpr double kFullDriveLength = 11.40; |
| 229 | constexpr double kFirstTurnDistance = 4.40; |
| 230 | constexpr double kSecondTurnDistance = 9.30; |
| 231 | StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn); |
| 232 | if (!WaitForDriveProfileNear(kFullDriveLength - (kFirstTurnDistance - 1.4))) |
| 233 | return true; |
| 234 | StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn); |
| 235 | |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 236 | if (!WaitForDriveProfileNear(kFullDriveLength - kFirstTurnDistance)) |
| 237 | return true; |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 238 | StartDrive(0.0, -M_PI / 2.0, kFarSwitchTurnDrive, kSweepingTurn); |
| 239 | set_arm_goal_position(arm::BackHighBoxIndex()); |
| 240 | SendSuperstructureGoal(); |
| 241 | if (!WaitForTurnProfileDone()) return true; |
| 242 | |
| 243 | StartDrive(0.0, 0.0, kDrive, kTurn); |
| 244 | |
| 245 | if (!WaitForDriveProfileNear(kFullDriveLength - (kSecondTurnDistance - 1.4))) |
| 246 | return true; |
| 247 | |
| 248 | StartDrive(0.0, 0.0, kFarScaleFinalTurnDrive, kTurn); |
| 249 | if (!WaitForDriveProfileNear(kFullDriveLength - (kSecondTurnDistance))) |
| 250 | return true; |
| 251 | LOG(INFO, "Final turn at %f\n", |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 252 | ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time)); |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 253 | |
| 254 | StartDrive(0.0, M_PI / 2.0, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn); |
| 255 | if (!WaitForDriveProfileNear(0.15)) return true; |
| 256 | |
| 257 | StartDrive(0.0, 0.3, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn); |
| 258 | |
| 259 | LOG(INFO, "Dropping at %f\n", |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 260 | ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time)); |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 261 | set_open_claw(true); |
| 262 | SendSuperstructureGoal(); |
| 263 | |
| 264 | ::std::this_thread::sleep_for(chrono::milliseconds(1000)); |
| 265 | LOG(INFO, "Backing up at %f\n", |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 266 | ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time)); |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 267 | |
| 268 | StartDrive(1.5, -0.55, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn); |
| 269 | |
| 270 | if (!WaitForDriveProfileNear(1.4)) return true; |
| 271 | set_arm_goal_position(arm::NeutralIndex()); |
| 272 | set_open_claw(false); |
| 273 | set_grab_box(true); |
| 274 | SendSuperstructureGoal(); |
| 275 | |
| 276 | if (!WaitForDriveProfileNear(0.3)) return true; |
| 277 | |
| 278 | set_intake_angle(0.15); |
| 279 | set_roller_voltage(10.0); |
| 280 | SendSuperstructureGoal(); |
| 281 | |
| 282 | set_max_drivetrain_voltage(6.0); |
| 283 | StartDrive(0.0, 0.0, kFarScaleFinalTurnDrive, kFarScaleSweepingTurn); |
| 284 | |
| 285 | if (!WaitForDriveProfileDone()) return true; |
| 286 | if (!WaitForTurnProfileDone()) return true; |
| 287 | |
| 288 | StartDrive(0.2, 0.0, kThirdBoxSlowBackup, kThirdBoxSlowTurn); |
| 289 | |
| 290 | if (!WaitForBoxGrabed()) return true; |
| 291 | set_arm_goal_position(arm::BackHighBoxIndex()); |
| 292 | set_intake_angle(-3.0); |
| 293 | set_roller_voltage(0.0); |
| 294 | set_grab_box(false); |
| 295 | SendSuperstructureGoal(); |
| 296 | |
| 297 | StartDrive(-1.40, 0.15, kThirdBoxPlaceDrive, kFarScaleSweepingTurn); |
| 298 | |
| 299 | if (!WaitForDriveProfileNear(0.1)) return true; |
| 300 | |
| 301 | StartDrive(0.0, 0.5, kThirdBoxPlaceDrive, kFarScaleSweepingTurn); |
| 302 | |
| 303 | if (!WaitForDriveProfileDone()) return true; |
| 304 | if (!WaitForTurnProfileDone()) return true; |
| 305 | ::std::this_thread::sleep_for(chrono::milliseconds(500)); |
| 306 | |
| 307 | LOG(INFO, "Dropping second box at %f\n", |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 308 | ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time)); |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 309 | set_open_claw(true); |
| 310 | SendSuperstructureGoal(); |
| 311 | |
| 312 | ::std::this_thread::sleep_for(chrono::milliseconds(1000)); |
| 313 | |
| 314 | StartDrive(1.4, -0.7, kThirdBoxPlaceDrive, kFarScaleSweepingTurn); |
| 315 | ::std::this_thread::sleep_for(chrono::milliseconds(200)); |
| 316 | set_arm_goal_position(arm::NeutralIndex()); |
| 317 | set_open_claw(false); |
| 318 | SendSuperstructureGoal(); |
| 319 | |
| 320 | if (!WaitForDriveProfileNear(1.0)) return true; |
| 321 | |
| 322 | StartDrive(0.0, -0.6, kThirdBoxPlaceDrive, kFarScaleSweepingTurn); |
| 323 | return false; |
| 324 | } |
| 325 | |
| 326 | bool AutonomousActor::FarReadyScale(monotonic_clock::time_point start_time) { |
| 327 | // Start on the left. Hit the switch. |
| 328 | constexpr double kFullDriveLength = 7.5; |
| 329 | constexpr double kFirstTurnDistance = 4.40; |
| 330 | StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn); |
| 331 | if (!WaitForDriveProfileNear(kFullDriveLength - (kFirstTurnDistance - 1.4))) |
| 332 | return true; |
| 333 | StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn); |
| 334 | |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 335 | if (!WaitForDriveProfileNear(kFullDriveLength - kFirstTurnDistance)) |
| 336 | return true; |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 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", |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 341 | ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time)); |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 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", |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 374 | ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time)); |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 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", |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 426 | ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time)); |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 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", |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 434 | ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time)); |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 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", |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 461 | ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time)); |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 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", |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 469 | ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time)); |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 470 | ::std::this_thread::sleep_for(chrono::milliseconds(500)); |
| 471 | |
| 472 | set_grab_box(false); |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 473 | // set_arm_goal_position(arm::UpIndex()); |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 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", |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 479 | ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time)); |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 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", |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 489 | ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time)); |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 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. |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 495 | // if (!WaitForArmTrajectoryClose(0.20)) return true; |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 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", |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 501 | ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time)); |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 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", |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 509 | ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time)); |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 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", |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 530 | ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time)); |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 531 | if (!WaitForBoxGrabed()) return true; |
| 532 | LOG(INFO, "Third box grabbed %f\n", |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 533 | ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time)); |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 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", |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 538 | ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time)); |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 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", |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 568 | ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time)); |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 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 |