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