blob: c12361297d4dbf269ea2df38e2934728410dd54e [file] [log] [blame]
Tyler Chatowf31da682017-01-22 01:39:40 +00001#include "y2017/actors/autonomous_actor.h"
2
3#include <inttypes.h>
4
5#include <chrono>
6#include <cmath>
7
John Park33858a32018-09-28 23:05:48 -07008#include "aos/util/phased_loop.h"
9#include "aos/logging/logging.h"
Tyler Chatowf31da682017-01-22 01:39:40 +000010
11#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Philipp Schrader996a2a22017-02-22 05:02:48 +000012#include "y2017/control_loops/drivetrain/drivetrain_base.h"
Tyler Chatowf31da682017-01-22 01:39:40 +000013
14namespace y2017 {
15namespace actors {
16using ::frc971::control_loops::drivetrain_queue;
17using ::aos::monotonic_clock;
18namespace chrono = ::std::chrono;
19namespace this_thread = ::std::this_thread;
20
21namespace {
Philipp Schrader85fca552017-03-05 00:30:50 +000022
Tyler Chatowf31da682017-01-22 01:39:40 +000023double DoubleSeconds(monotonic_clock::duration duration) {
24 return ::std::chrono::duration_cast<::std::chrono::duration<double>>(duration)
25 .count();
26}
Philipp Schrader85fca552017-03-05 00:30:50 +000027
Austin Schuh10475d72017-04-16 19:17:48 -070028const ProfileParameters kGearBallBackDrive = {3.0, 3.5};
29const ProfileParameters kGearDrive = {1.5, 2.0};
30const ProfileParameters kGearFastDrive = {2.0, 2.5};
31const ProfileParameters kGearSlowDrive = {1.0, 2.0};
32const ProfileParameters kGearPlaceDrive = {0.40, 2.0};
Austin Schuh624088a2017-03-22 22:36:16 -070033const ProfileParameters kSlowDrive = {3.0, 2.0};
Austin Schuh366f7ed2017-03-11 21:57:14 -080034const ProfileParameters kSlowTurn = {3.0, 3.0};
Austin Schuh10475d72017-04-16 19:17:48 -070035const ProfileParameters kFirstTurn = {1.0, 1.5};
36const ProfileParameters kFirstGearStartTurn = {2.0, 3.0};
37const ProfileParameters kFirstGearTurn = {2.0, 5.0};
38const ProfileParameters kSecondGearTurn = {3.0, 5.0};
Austin Schuh19e15d72017-06-24 13:34:47 -070039const ProfileParameters kSmashTurn = {1.5, 5.0};
Philipp Schrader85fca552017-03-05 00:30:50 +000040
Tyler Chatowf31da682017-01-22 01:39:40 +000041} // namespace
42
Philipp Schrader996a2a22017-02-22 05:02:48 +000043AutonomousActor::AutonomousActor(
44 ::frc971::autonomous::AutonomousActionQueueGroup *s)
45 : frc971::autonomous::BaseAutonomousActor(
46 s, control_loops::drivetrain::GetDrivetrainConfig()) {}
Tyler Chatowf31da682017-01-22 01:39:40 +000047
Philipp Schrader996a2a22017-02-22 05:02:48 +000048bool AutonomousActor::RunAction(
49 const ::frc971::autonomous::AutonomousActionParams &params) {
Tyler Chatowf31da682017-01-22 01:39:40 +000050 monotonic_clock::time_point start_time = monotonic_clock::now();
51 LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode);
Austin Schuh624088a2017-03-22 22:36:16 -070052 Reset();
Tyler Chatowf31da682017-01-22 01:39:40 +000053
54 switch (params.mode) {
Austin Schuh19e15d72017-06-24 13:34:47 -070055 case 503: {
56 // Middle gear auto.
57 // Red is positive.
58 // Line up on boiler side edge.
59 constexpr double kDriveDirection = 1.0;
60
61 set_intake_goal(0.18);
62 set_turret_goal(0.0);
63 SendSuperstructureGoal();
64
65 set_turret_goal(-M_PI / 4.0 * kDriveDirection);
66 SendSuperstructureGoal();
67
68 // Turn towards the peg.
69 StartDrive(0.0, kDriveDirection * 0.162, kGearDrive, kSlowTurn);
70 if (!WaitForDriveNear(100.0, 0.02)) return true;
71 if (!WaitForTurnProfileDone()) return true;
72
73 // Drive, but get within 0.3 meters
74 StartDrive(1.73, 0.0, kGearFastDrive, kSlowTurn);
75 if (!WaitForDriveNear(0.3, 0.0)) return true;
76
77 // Now, add a slow, short move to actually place the gear.
78 StartDrive(0.18, 0.0, kGearPlaceDrive, kSecondGearTurn);
79 if (!WaitForDriveNear(0.07, 0.0)) return true;
80
81 set_gear_servo(0.3);
82 SendSuperstructureGoal();
83
84 // Slow down and then pause to let Chris pull the gear off.
85 this_thread::sleep_for(chrono::milliseconds(1000));
86
87 // Back up
88 StartDrive(-1.00, 0.0, kGearFastDrive, kSlowTurn);
89 if (!WaitForDriveNear(0.1, 0.1)) return true;
90
91 // Turn towards the boiler.
92 StartDrive(0.0, -kDriveDirection * M_PI / 2.0, kGearFastDrive, kSlowTurn);
93 if (!WaitForDriveNear(0.1, 0.1)) return true;
94
95 // Drive up near it.
96 StartDrive(1.8, 0.0, kGearFastDrive, kSlowTurn);
97 if (!WaitForDriveNear(0.1, 0.1)) return true;
98
99 set_hood_goal(0.37);
100 set_intake_goal(0.23);
101 set_shooter_velocity(353.0);
102 set_vision_track(true);
103 set_use_vision_for_shots(true);
104 set_indexer_angular_velocity(-1.1 * M_PI);
105 SendSuperstructureGoal();
106
107 this_thread::sleep_for(start_time + chrono::seconds(15) -
108 monotonic_clock::now());
109 if (ShouldCancel()) return true;
110
111 set_shooter_velocity(0.0);
112 set_indexer_angular_velocity(0.0);
113 SendSuperstructureGoal();
114
115 } break;
116
117 case 500: {
Austin Schuh10475d72017-04-16 19:17:48 -0700118 // Red is positive.
119 constexpr double kDriveDirection = -1.0;
120 // Side peg + hopper auto.
121
122 set_intake_goal(0.23);
123 set_turret_goal(-M_PI * kDriveDirection);
124 SendSuperstructureGoal();
125
126 constexpr double kLongPegDrive = 3.025;
127
128 StartDrive(kLongPegDrive, -kDriveDirection * M_PI / 4, kGearDrive,
129 kFirstGearStartTurn);
130 if (!WaitForDriveNear(100.0, M_PI / 8.0)) return true;
131 LOG(INFO, "Turn Middle: %f left to go\n", DriveDistanceLeft());
132
133 StartDrive(0.0, 0.0, kGearDrive, kFirstGearTurn);
134
135 if (!WaitForTurnProfileDone()) return true;
136 LOG(INFO, "Turn profile ended: %f left to go\n", DriveDistanceLeft());
137
138 set_hood_goal(0.43);
139 set_shooter_velocity(364.0);
140 SendSuperstructureGoal();
141
142 constexpr double kTurnDistanceFromStart = 1.08;
143 if (!WaitForDriveNear(kLongPegDrive - kTurnDistanceFromStart, 10.0)) {
144 return true;
145 }
146
147 StartDrive(0.0, kDriveDirection * (M_PI / 4 + 0.3), kGearSlowDrive,
148 kSecondGearTurn);
149 if (!WaitForTurnProfileDone()) return true;
150
151 StartDrive(0.0, 0.0, kGearFastDrive, kSecondGearTurn);
152
153 if (!WaitForDriveNear(0.3, 0.0)) return true;
154
155 set_vision_track(true);
156 SendSuperstructureGoal();
157
158 StartDrive(0.19, 0.0, kGearPlaceDrive, kSecondGearTurn);
159
160 if (!WaitForDriveNear(0.07, 0.0)) return true;
161 set_gear_servo(0.3);
162 SendSuperstructureGoal();
163
164 // Shoot from the peg.
165 //set_indexer_angular_velocity(-2.1 * M_PI);
166 //SendSuperstructureGoal();
167 //this_thread::sleep_for(chrono::milliseconds(1750));
168
169 //this_thread::sleep_for(chrono::milliseconds(500));
170 this_thread::sleep_for(chrono::milliseconds(750));
171 set_indexer_angular_velocity(0.0);
172 set_vision_track(false);
173 set_turret_goal(0.0);
174
175 set_hood_goal(0.37);
176 set_shooter_velocity(351.0);
177 set_intake_goal(0.18);
178 set_gear_servo(0.4);
179
180 SendSuperstructureGoal();
181 LOG(INFO, "Starting drive back %f\n",
182 DoubleSeconds(monotonic_clock::now() - start_time));
183
Austin Schuh19e15d72017-06-24 13:34:47 -0700184 StartDrive(-2.75, kDriveDirection * 1.24, kSlowDrive,
Austin Schuh10475d72017-04-16 19:17:48 -0700185 kFirstGearStartTurn);
186 if (!WaitForDriveNear(2.4, 0.0)) return true;
187
188 if (!WaitForTurnProfileDone()) return true;
189 StartDrive(0.0, 0.0, kGearBallBackDrive, kFirstGearStartTurn);
190
191 if (!WaitForDriveNear(0.2, 0.0)) return true;
192 this_thread::sleep_for(chrono::milliseconds(200));
Austin Schuh19e15d72017-06-24 13:34:47 -0700193 // Trip the hopper
Austin Schuh10475d72017-04-16 19:17:48 -0700194 StartDrive(0.0, kDriveDirection * 1.10, kSlowDrive,
195 kSmashTurn);
196
197 if (!WaitForDriveNear(0.2, 0.35)) return true;
198 set_vision_track(true);
199 set_use_vision_for_shots(true);
200 SendSuperstructureGoal();
201
202 if (!WaitForDriveNear(0.2, 0.2)) return true;
203 StartDrive(0.0, -kDriveDirection * 0.15, kSlowDrive,
204 kSmashTurn);
205
206 LOG(INFO, "Starting second shot %f\n",
207 DoubleSeconds(monotonic_clock::now() - start_time));
208 set_indexer_angular_velocity(-2.15 * M_PI);
209 SendSuperstructureGoal();
210 if (!WaitForDriveNear(0.2, 0.1)) return true;
211
212 this_thread::sleep_for(start_time + chrono::seconds(11) -
213 monotonic_clock::now());
214 if (ShouldCancel()) return true;
215 set_intake_max_velocity(0.05);
216 set_intake_goal(0.08);
217
218 this_thread::sleep_for(start_time + chrono::seconds(15) -
219 monotonic_clock::now());
220 if (ShouldCancel()) return true;
221
222 set_intake_max_velocity(0.50);
223 set_intake_goal(0.18);
224 set_shooter_velocity(0.0);
225 set_indexer_angular_velocity(0.0);
226 SendSuperstructureGoal();
227
228 } break;
229
Austin Schuh19e15d72017-06-24 13:34:47 -0700230 default: {
231 // hopper auto
Austin Schuh10475d72017-04-16 19:17:48 -0700232 // Red is positive.
Austin Schuh624088a2017-03-22 22:36:16 -0700233 constexpr double kDriveDirection = 1.0;
Austin Schuh624088a2017-03-22 22:36:16 -0700234 set_intake_goal(0.07);
235 SendSuperstructureGoal();
Austin Schuh10475d72017-04-16 19:17:48 -0700236
Austin Schuh19e15d72017-06-24 13:34:47 -0700237 StartDrive(-3.42, kDriveDirection * (M_PI / 10 - 0.057) , kSlowDrive, kFirstTurn);
Austin Schuh10475d72017-04-16 19:17:48 -0700238 if (!WaitForDriveNear(3.30, 0.0)) return true;
239 LOG(INFO, "Turn ended: %f left to go\n", DriveDistanceLeft());
240 // We can go to 2.50 before we hit the previous profile.
241
Austin Schuh19e15d72017-06-24 13:34:47 -0700242 if (!WaitForDriveNear(2.48, 0.0)) return true;
Austin Schuh10475d72017-04-16 19:17:48 -0700243 LOG(INFO, "%f left to go\n", DriveDistanceLeft());
Austin Schuh366f7ed2017-03-11 21:57:14 -0800244
Austin Schuh624088a2017-03-22 22:36:16 -0700245 set_intake_goal(0.23);
Austin Schuh76af51e2017-04-09 18:32:38 -0700246 set_turret_goal(0.0);
247 // Values good for blue:
248 // TODO(austin): Drive these off the auto switch.
Austin Schuh10475d72017-04-16 19:17:48 -0700249
250 set_hood_goal(0.37);
251 set_shooter_velocity(353.0);
Austin Schuh624088a2017-03-22 22:36:16 -0700252 SendSuperstructureGoal();
Austin Schuh366f7ed2017-03-11 21:57:14 -0800253
Austin Schuh10475d72017-04-16 19:17:48 -0700254 StartDrive(0.0, -M_PI / 8.0 * kDriveDirection, kSlowDrive, kSlowTurn);
255 if (!WaitForDriveNear(0.20, 0.0)) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800256
Austin Schuh10475d72017-04-16 19:17:48 -0700257 this_thread::sleep_for(chrono::milliseconds(300));
258
Austin Schuh19e15d72017-06-24 13:34:47 -0700259 // Turn to trigger the hopper.
Austin Schuh10475d72017-04-16 19:17:48 -0700260 StartDrive(0.0, (M_PI / 8.0 + 0.20) * kDriveDirection, kSlowDrive,
Austin Schuh624088a2017-03-22 22:36:16 -0700261 kSmashTurn);
262 if (!WaitForDriveNear(0.05, 0.2)) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800263
Austin Schuh624088a2017-03-22 22:36:16 -0700264 set_vision_track(true);
Austin Schuh10475d72017-04-16 19:17:48 -0700265 set_use_vision_for_shots(true);
Austin Schuh624088a2017-03-22 22:36:16 -0700266 SendSuperstructureGoal();
Austin Schuh366f7ed2017-03-11 21:57:14 -0800267
Austin Schuh19e15d72017-06-24 13:34:47 -0700268 // Now that everything is tracking, wait for the hood to zero before
269 // trying to shoot.
270 WaitForHoodZeroed();
271 if (ShouldCancel()) return true;
272
Austin Schuh624088a2017-03-22 22:36:16 -0700273 this_thread::sleep_for(chrono::milliseconds(200));
Austin Schuh366f7ed2017-03-11 21:57:14 -0800274
Austin Schuh19e15d72017-06-24 13:34:47 -0700275 // Turn back.
Austin Schuh624088a2017-03-22 22:36:16 -0700276 StartDrive(0.0, (-0.15) * kDriveDirection, kSlowDrive, kSlowTurn);
277 if (!WaitForDriveNear(0.05, 0.02)) return true;
Austin Schuh76af51e2017-04-09 18:32:38 -0700278
Austin Schuh19e15d72017-06-24 13:34:47 -0700279 set_indexer_angular_velocity(-2.1 * M_PI);
280 SendSuperstructureGoal();
281
Austin Schuh624088a2017-03-22 22:36:16 -0700282 LOG(INFO, "Started shooting at %f\n",
283 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh366f7ed2017-03-11 21:57:14 -0800284
Austin Schuh624088a2017-03-22 22:36:16 -0700285 this_thread::sleep_for(start_time + chrono::seconds(9) -
286 monotonic_clock::now());
Austin Schuh624088a2017-03-22 22:36:16 -0700287 if (ShouldCancel()) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800288
Austin Schuh624088a2017-03-22 22:36:16 -0700289 set_intake_max_velocity(0.05);
290 set_intake_goal(0.08);
291 SendSuperstructureGoal();
Tyler Chatowf31da682017-01-22 01:39:40 +0000292
Austin Schuh624088a2017-03-22 22:36:16 -0700293 this_thread::sleep_for(start_time + chrono::seconds(15) -
294 monotonic_clock::now());
295 if (ShouldCancel()) return true;
296
297 set_shooter_velocity(0.0);
298 set_indexer_angular_velocity(0.0);
299 SendSuperstructureGoal();
300
301 } break;
Tyler Chatowf31da682017-01-22 01:39:40 +0000302 }
303
304 LOG(INFO, "Done %f\n", DoubleSeconds(monotonic_clock::now() - start_time));
305
306 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
307 ::std::chrono::milliseconds(5) / 2);
308
309 while (!ShouldCancel()) {
310 phased_loop.SleepUntilNext();
311 }
312 LOG(DEBUG, "Done running\n");
313
314 return true;
315}
316
Tyler Chatowf31da682017-01-22 01:39:40 +0000317} // namespace actors
318} // namespace y2017