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