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