Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 1 | #include "y2017/actors/autonomous_actor.h" |
| 2 | |
| 3 | #include <inttypes.h> |
| 4 | |
| 5 | #include <chrono> |
| 6 | #include <cmath> |
| 7 | |
| 8 | #include "aos/common/util/phased_loop.h" |
| 9 | #include "aos/common/logging/logging.h" |
| 10 | |
| 11 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Philipp Schrader | 996a2a2 | 2017-02-22 05:02:48 +0000 | [diff] [blame] | 12 | #include "y2017/control_loops/drivetrain/drivetrain_base.h" |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 13 | |
| 14 | namespace y2017 { |
| 15 | namespace actors { |
| 16 | using ::frc971::control_loops::drivetrain_queue; |
| 17 | using ::aos::monotonic_clock; |
| 18 | namespace chrono = ::std::chrono; |
| 19 | namespace this_thread = ::std::this_thread; |
| 20 | |
| 21 | namespace { |
Philipp Schrader | 85fca55 | 2017-03-05 00:30:50 +0000 | [diff] [blame] | 22 | |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 23 | double DoubleSeconds(monotonic_clock::duration duration) { |
| 24 | return ::std::chrono::duration_cast<::std::chrono::duration<double>>(duration) |
| 25 | .count(); |
| 26 | } |
Philipp Schrader | 85fca55 | 2017-03-05 00:30:50 +0000 | [diff] [blame] | 27 | |
Austin Schuh | 10475d7 | 2017-04-16 19:17:48 -0700 | [diff] [blame^] | 28 | const ProfileParameters kGearBallBackDrive = {3.0, 3.5}; |
| 29 | const ProfileParameters kGearDrive = {1.5, 2.0}; |
| 30 | const ProfileParameters kGearFastDrive = {2.0, 2.5}; |
| 31 | const ProfileParameters kGearSlowDrive = {1.0, 2.0}; |
| 32 | const ProfileParameters kGearPlaceDrive = {0.40, 2.0}; |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 33 | const ProfileParameters kSlowDrive = {3.0, 2.0}; |
Austin Schuh | 366f7ed | 2017-03-11 21:57:14 -0800 | [diff] [blame] | 34 | const ProfileParameters kSlowTurn = {3.0, 3.0}; |
Austin Schuh | 10475d7 | 2017-04-16 19:17:48 -0700 | [diff] [blame^] | 35 | const ProfileParameters kFirstTurn = {1.0, 1.5}; |
| 36 | const ProfileParameters kFirstGearStartTurn = {2.0, 3.0}; |
| 37 | const ProfileParameters kFirstGearTurn = {2.0, 5.0}; |
| 38 | const ProfileParameters kSecondGearTurn = {3.0, 5.0}; |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 39 | const ProfileParameters kSmashTurn = {3.0, 5.0}; |
Philipp Schrader | 85fca55 | 2017-03-05 00:30:50 +0000 | [diff] [blame] | 40 | |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 41 | } // namespace |
| 42 | |
Philipp Schrader | 996a2a2 | 2017-02-22 05:02:48 +0000 | [diff] [blame] | 43 | AutonomousActor::AutonomousActor( |
| 44 | ::frc971::autonomous::AutonomousActionQueueGroup *s) |
| 45 | : frc971::autonomous::BaseAutonomousActor( |
| 46 | s, control_loops::drivetrain::GetDrivetrainConfig()) {} |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 47 | |
Philipp Schrader | 996a2a2 | 2017-02-22 05:02:48 +0000 | [diff] [blame] | 48 | bool AutonomousActor::RunAction( |
| 49 | const ::frc971::autonomous::AutonomousActionParams ¶ms) { |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 50 | monotonic_clock::time_point start_time = monotonic_clock::now(); |
| 51 | LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode); |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 52 | Reset(); |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 53 | |
| 54 | switch (params.mode) { |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 55 | default: { |
Austin Schuh | 10475d7 | 2017-04-16 19:17:48 -0700 | [diff] [blame^] | 56 | // Red is positive. |
| 57 | constexpr double kDriveDirection = -1.0; |
| 58 | // Side peg + hopper auto. |
| 59 | |
| 60 | set_intake_goal(0.23); |
| 61 | set_turret_goal(-M_PI * kDriveDirection); |
| 62 | SendSuperstructureGoal(); |
| 63 | |
| 64 | constexpr double kLongPegDrive = 3.025; |
| 65 | |
| 66 | StartDrive(kLongPegDrive, -kDriveDirection * M_PI / 4, kGearDrive, |
| 67 | kFirstGearStartTurn); |
| 68 | if (!WaitForDriveNear(100.0, M_PI / 8.0)) return true; |
| 69 | LOG(INFO, "Turn Middle: %f left to go\n", DriveDistanceLeft()); |
| 70 | |
| 71 | StartDrive(0.0, 0.0, kGearDrive, kFirstGearTurn); |
| 72 | |
| 73 | if (!WaitForTurnProfileDone()) return true; |
| 74 | LOG(INFO, "Turn profile ended: %f left to go\n", DriveDistanceLeft()); |
| 75 | |
| 76 | set_hood_goal(0.43); |
| 77 | set_shooter_velocity(364.0); |
| 78 | SendSuperstructureGoal(); |
| 79 | |
| 80 | constexpr double kTurnDistanceFromStart = 1.08; |
| 81 | if (!WaitForDriveNear(kLongPegDrive - kTurnDistanceFromStart, 10.0)) { |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | StartDrive(0.0, kDriveDirection * (M_PI / 4 + 0.3), kGearSlowDrive, |
| 86 | kSecondGearTurn); |
| 87 | if (!WaitForTurnProfileDone()) return true; |
| 88 | |
| 89 | StartDrive(0.0, 0.0, kGearFastDrive, kSecondGearTurn); |
| 90 | |
| 91 | if (!WaitForDriveNear(0.3, 0.0)) return true; |
| 92 | |
| 93 | set_vision_track(true); |
| 94 | SendSuperstructureGoal(); |
| 95 | |
| 96 | StartDrive(0.19, 0.0, kGearPlaceDrive, kSecondGearTurn); |
| 97 | |
| 98 | if (!WaitForDriveNear(0.07, 0.0)) return true; |
| 99 | set_gear_servo(0.3); |
| 100 | SendSuperstructureGoal(); |
| 101 | |
| 102 | // Shoot from the peg. |
| 103 | //set_indexer_angular_velocity(-2.1 * M_PI); |
| 104 | //SendSuperstructureGoal(); |
| 105 | //this_thread::sleep_for(chrono::milliseconds(1750)); |
| 106 | |
| 107 | //this_thread::sleep_for(chrono::milliseconds(500)); |
| 108 | this_thread::sleep_for(chrono::milliseconds(750)); |
| 109 | set_indexer_angular_velocity(0.0); |
| 110 | set_vision_track(false); |
| 111 | set_turret_goal(0.0); |
| 112 | |
| 113 | set_hood_goal(0.37); |
| 114 | set_shooter_velocity(351.0); |
| 115 | set_intake_goal(0.18); |
| 116 | set_gear_servo(0.4); |
| 117 | |
| 118 | SendSuperstructureGoal(); |
| 119 | LOG(INFO, "Starting drive back %f\n", |
| 120 | DoubleSeconds(monotonic_clock::now() - start_time)); |
| 121 | |
| 122 | StartDrive(-2.69, kDriveDirection * 1.30, kSlowDrive, |
| 123 | kFirstGearStartTurn); |
| 124 | if (!WaitForDriveNear(2.4, 0.0)) return true; |
| 125 | |
| 126 | if (!WaitForTurnProfileDone()) return true; |
| 127 | StartDrive(0.0, 0.0, kGearBallBackDrive, kFirstGearStartTurn); |
| 128 | |
| 129 | if (!WaitForDriveNear(0.2, 0.0)) return true; |
| 130 | this_thread::sleep_for(chrono::milliseconds(200)); |
| 131 | StartDrive(0.0, kDriveDirection * 1.10, kSlowDrive, |
| 132 | kSmashTurn); |
| 133 | |
| 134 | if (!WaitForDriveNear(0.2, 0.35)) return true; |
| 135 | set_vision_track(true); |
| 136 | set_use_vision_for_shots(true); |
| 137 | SendSuperstructureGoal(); |
| 138 | |
| 139 | if (!WaitForDriveNear(0.2, 0.2)) return true; |
| 140 | StartDrive(0.0, -kDriveDirection * 0.15, kSlowDrive, |
| 141 | kSmashTurn); |
| 142 | |
| 143 | LOG(INFO, "Starting second shot %f\n", |
| 144 | DoubleSeconds(monotonic_clock::now() - start_time)); |
| 145 | set_indexer_angular_velocity(-2.15 * M_PI); |
| 146 | SendSuperstructureGoal(); |
| 147 | if (!WaitForDriveNear(0.2, 0.1)) return true; |
| 148 | |
| 149 | this_thread::sleep_for(start_time + chrono::seconds(11) - |
| 150 | monotonic_clock::now()); |
| 151 | if (ShouldCancel()) return true; |
| 152 | set_intake_max_velocity(0.05); |
| 153 | set_intake_goal(0.08); |
| 154 | |
| 155 | this_thread::sleep_for(start_time + chrono::seconds(15) - |
| 156 | monotonic_clock::now()); |
| 157 | if (ShouldCancel()) return true; |
| 158 | |
| 159 | set_intake_max_velocity(0.50); |
| 160 | set_intake_goal(0.18); |
| 161 | set_shooter_velocity(0.0); |
| 162 | set_indexer_angular_velocity(0.0); |
| 163 | SendSuperstructureGoal(); |
| 164 | |
| 165 | } break; |
| 166 | |
| 167 | case 500: { |
| 168 | // Red is positive. |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 169 | constexpr double kDriveDirection = 1.0; |
Austin Schuh | 10475d7 | 2017-04-16 19:17:48 -0700 | [diff] [blame^] | 170 | // Side hopper auto. |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 171 | set_intake_goal(0.07); |
| 172 | SendSuperstructureGoal(); |
Austin Schuh | 10475d7 | 2017-04-16 19:17:48 -0700 | [diff] [blame^] | 173 | |
| 174 | StartDrive(-3.3, kDriveDirection * M_PI / 10, kSlowDrive, kFirstTurn); |
| 175 | if (!WaitForDriveNear(3.30, 0.0)) return true; |
| 176 | LOG(INFO, "Turn ended: %f left to go\n", DriveDistanceLeft()); |
| 177 | // We can go to 2.50 before we hit the previous profile. |
| 178 | |
| 179 | if (!WaitForDriveNear(2.42, 0.0)) return true; |
| 180 | LOG(INFO, "%f left to go\n", DriveDistanceLeft()); |
Austin Schuh | 366f7ed | 2017-03-11 21:57:14 -0800 | [diff] [blame] | 181 | |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 182 | set_intake_goal(0.23); |
Austin Schuh | 76af51e | 2017-04-09 18:32:38 -0700 | [diff] [blame] | 183 | set_turret_goal(0.0); |
| 184 | // Values good for blue: |
| 185 | // TODO(austin): Drive these off the auto switch. |
Austin Schuh | 10475d7 | 2017-04-16 19:17:48 -0700 | [diff] [blame^] | 186 | |
| 187 | set_hood_goal(0.37); |
| 188 | set_shooter_velocity(353.0); |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 189 | SendSuperstructureGoal(); |
Austin Schuh | 366f7ed | 2017-03-11 21:57:14 -0800 | [diff] [blame] | 190 | |
Austin Schuh | 10475d7 | 2017-04-16 19:17:48 -0700 | [diff] [blame^] | 191 | StartDrive(0.0, -M_PI / 8.0 * kDriveDirection, kSlowDrive, kSlowTurn); |
| 192 | if (!WaitForDriveNear(0.20, 0.0)) return true; |
Austin Schuh | 366f7ed | 2017-03-11 21:57:14 -0800 | [diff] [blame] | 193 | |
Austin Schuh | 10475d7 | 2017-04-16 19:17:48 -0700 | [diff] [blame^] | 194 | this_thread::sleep_for(chrono::milliseconds(300)); |
| 195 | |
| 196 | StartDrive(0.0, (M_PI / 8.0 + 0.20) * kDriveDirection, kSlowDrive, |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 197 | kSmashTurn); |
| 198 | if (!WaitForDriveNear(0.05, 0.2)) return true; |
Austin Schuh | 366f7ed | 2017-03-11 21:57:14 -0800 | [diff] [blame] | 199 | |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 200 | set_vision_track(true); |
Austin Schuh | 10475d7 | 2017-04-16 19:17:48 -0700 | [diff] [blame^] | 201 | set_use_vision_for_shots(true); |
Austin Schuh | 76af51e | 2017-04-09 18:32:38 -0700 | [diff] [blame] | 202 | |
Austin Schuh | 10475d7 | 2017-04-16 19:17:48 -0700 | [diff] [blame^] | 203 | set_indexer_angular_velocity(-2.1 * M_PI); |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 204 | SendSuperstructureGoal(); |
Austin Schuh | 366f7ed | 2017-03-11 21:57:14 -0800 | [diff] [blame] | 205 | |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 206 | this_thread::sleep_for(chrono::milliseconds(200)); |
Austin Schuh | 366f7ed | 2017-03-11 21:57:14 -0800 | [diff] [blame] | 207 | |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 208 | StartDrive(0.0, (-0.15) * kDriveDirection, kSlowDrive, kSlowTurn); |
| 209 | if (!WaitForDriveNear(0.05, 0.02)) return true; |
Austin Schuh | 76af51e | 2017-04-09 18:32:38 -0700 | [diff] [blame] | 210 | |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 211 | LOG(INFO, "Started shooting at %f\n", |
| 212 | DoubleSeconds(monotonic_clock::now() - start_time)); |
Austin Schuh | 366f7ed | 2017-03-11 21:57:14 -0800 | [diff] [blame] | 213 | |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 214 | this_thread::sleep_for(start_time + chrono::seconds(9) - |
| 215 | monotonic_clock::now()); |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 216 | if (ShouldCancel()) return true; |
Austin Schuh | 366f7ed | 2017-03-11 21:57:14 -0800 | [diff] [blame] | 217 | |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 218 | set_intake_max_velocity(0.05); |
| 219 | set_intake_goal(0.08); |
| 220 | SendSuperstructureGoal(); |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 221 | |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 222 | this_thread::sleep_for(start_time + chrono::seconds(15) - |
| 223 | monotonic_clock::now()); |
| 224 | if (ShouldCancel()) return true; |
| 225 | |
| 226 | set_shooter_velocity(0.0); |
| 227 | set_indexer_angular_velocity(0.0); |
| 228 | SendSuperstructureGoal(); |
| 229 | |
| 230 | } break; |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | LOG(INFO, "Done %f\n", DoubleSeconds(monotonic_clock::now() - start_time)); |
| 234 | |
| 235 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
| 236 | ::std::chrono::milliseconds(5) / 2); |
| 237 | |
| 238 | while (!ShouldCancel()) { |
| 239 | phased_loop.SleepUntilNext(); |
| 240 | } |
| 241 | LOG(DEBUG, "Done running\n"); |
| 242 | |
| 243 | return true; |
| 244 | } |
| 245 | |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 246 | } // namespace actors |
| 247 | } // namespace y2017 |