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