blob: 74a1282fe160d06cb410222cbaf7a70ea61a444c [file] [log] [blame]
Comran Morshede68e3732016-03-12 14:12:11 +00001#include "y2016/actors/autonomous_actor.h"
2
3#include <inttypes.h>
4
Austin Schuh8aec1ed2016-05-01 13:29:20 -07005#include <chrono>
Comran Morshedb134e772016-03-16 21:05:05 +00006#include <cmath>
7
John Park33858a32018-09-28 23:05:48 -07008#include "aos/logging/logging.h"
James Kuszmaul651fc3f2019-05-15 21:14:25 -07009#include "aos/util/phased_loop.h"
Comran Morshed435f1112016-03-12 14:20:45 +000010
11#include "frc971/control_loops/drivetrain/drivetrain.q.h"
12#include "y2016/control_loops/drivetrain/drivetrain_base.h"
Austin Schuhf59b8ee2016-03-19 21:31:36 -070013#include "y2016/control_loops/shooter/shooter.q.h"
Comran Morshedb134e772016-03-16 21:05:05 +000014#include "y2016/control_loops/superstructure/superstructure.q.h"
Austin Schuh23b21802016-04-03 21:18:56 -070015#include "y2016/queues/ball_detector.q.h"
Austin Schuhf59b8ee2016-03-19 21:31:36 -070016#include "y2016/vision/vision.q.h"
Comran Morshede68e3732016-03-12 14:12:11 +000017
18namespace y2016 {
19namespace actors {
Austin Schuhf2a50ba2016-12-24 16:16:26 -080020using ::aos::monotonic_clock;
James Kuszmaul651fc3f2019-05-15 21:14:25 -070021using ::frc971::control_loops::drivetrain_queue;
Austin Schuhf2a50ba2016-12-24 16:16:26 -080022namespace chrono = ::std::chrono;
23namespace this_thread = ::std::this_thread;
Comran Morshed435f1112016-03-12 14:20:45 +000024
25namespace {
Austin Schuhf59b8ee2016-03-19 21:31:36 -070026const ProfileParameters kSlowDrive = {0.8, 2.5};
27const ProfileParameters kLowBarDrive = {1.3, 2.5};
28const ProfileParameters kMoatDrive = {1.2, 3.5};
29const ProfileParameters kRealignDrive = {2.0, 2.5};
30const ProfileParameters kRockWallDrive = {0.8, 2.5};
Comran Morshed435f1112016-03-12 14:20:45 +000031const ProfileParameters kFastDrive = {3.0, 2.5};
32
Austin Schuhf59b8ee2016-03-19 21:31:36 -070033const ProfileParameters kSlowTurn = {0.8, 3.0};
Comran Morshed435f1112016-03-12 14:20:45 +000034const ProfileParameters kFastTurn = {3.0, 10.0};
Austin Schuhe4ec49c2016-04-24 19:07:15 -070035const ProfileParameters kStealTurn = {4.0, 15.0};
Austin Schuh23b21802016-04-03 21:18:56 -070036const ProfileParameters kSwerveTurn = {2.0, 7.0};
37const ProfileParameters kFinishTurn = {2.0, 5.0};
38
39const ProfileParameters kTwoBallLowDrive = {1.7, 3.5};
40const ProfileParameters kTwoBallFastDrive = {3.0, 1.5};
41const ProfileParameters kTwoBallReturnDrive = {3.0, 1.9};
Austin Schuhe4ec49c2016-04-24 19:07:15 -070042const ProfileParameters kTwoBallReturnSlow = {3.0, 2.5};
Austin Schuh23b21802016-04-03 21:18:56 -070043const ProfileParameters kTwoBallBallPickup = {2.0, 1.75};
Austin Schuhe4ec49c2016-04-24 19:07:15 -070044const ProfileParameters kTwoBallBallPickupAccel = {2.0, 2.5};
Austin Schuhf2a50ba2016-12-24 16:16:26 -080045
Comran Morshed435f1112016-03-12 14:20:45 +000046} // namespace
Comran Morshede68e3732016-03-12 14:12:11 +000047
Austin Schuh1bf8a212019-05-26 22:13:14 -070048AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
Philipp Schrader4bd29b12017-02-22 04:42:27 +000049 : frc971::autonomous::BaseAutonomousActor(
Austin Schuh1bf8a212019-05-26 22:13:14 -070050 event_loop, control_loops::drivetrain::GetDrivetrainConfig()),
51 vision_align_actor_factory_(
Austin Schuh28bde302019-05-26 22:24:33 -070052 actors::VisionAlignActor::MakeFactory(event_loop)),
53 vision_status_fetcher_(
54 event_loop->MakeFetcher<::y2016::vision::VisionStatus>(
Austin Schuh4b652c92019-05-27 13:22:27 -070055 ".y2016.vision.vision_status")),
56 ball_detector_fetcher_(
57 event_loop->MakeFetcher<::y2016::sensors::BallDetector>(
58 ".y2016.sensors.ball_detector")) {}
Comran Morshed435f1112016-03-12 14:20:45 +000059
Austin Schuhe4ec49c2016-04-24 19:07:15 -070060constexpr double kDoNotTurnCare = 2.0;
61
Comran Morshedb134e772016-03-16 21:05:05 +000062void AutonomousActor::MoveSuperstructure(
63 double intake, double shoulder, double wrist,
64 const ProfileParameters intake_params,
65 const ProfileParameters shoulder_params,
Austin Schuh23b21802016-04-03 21:18:56 -070066 const ProfileParameters wrist_params, bool traverse_up,
67 double roller_power) {
Comran Morshedb134e772016-03-16 21:05:05 +000068 superstructure_goal_ = {intake, shoulder, wrist};
69
70 auto new_superstructure_goal =
71 ::y2016::control_loops::superstructure_queue.goal.MakeMessage();
72
73 new_superstructure_goal->angle_intake = intake;
74 new_superstructure_goal->angle_shoulder = shoulder;
75 new_superstructure_goal->angle_wrist = wrist;
76
77 new_superstructure_goal->max_angular_velocity_intake =
78 intake_params.max_velocity;
79 new_superstructure_goal->max_angular_velocity_shoulder =
80 shoulder_params.max_velocity;
81 new_superstructure_goal->max_angular_velocity_wrist =
82 wrist_params.max_velocity;
83
84 new_superstructure_goal->max_angular_acceleration_intake =
85 intake_params.max_acceleration;
86 new_superstructure_goal->max_angular_acceleration_shoulder =
87 shoulder_params.max_acceleration;
88 new_superstructure_goal->max_angular_acceleration_wrist =
89 wrist_params.max_acceleration;
90
Austin Schuh23b21802016-04-03 21:18:56 -070091 new_superstructure_goal->voltage_top_rollers = roller_power;
92 new_superstructure_goal->voltage_bottom_rollers = roller_power;
Austin Schuhf59b8ee2016-03-19 21:31:36 -070093
94 new_superstructure_goal->traverse_unlatched = true;
95 new_superstructure_goal->traverse_down = !traverse_up;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070096 new_superstructure_goal->voltage_climber = 0.0;
97 new_superstructure_goal->unfold_climber = false;
Comran Morshedb134e772016-03-16 21:05:05 +000098
99 if (!new_superstructure_goal.Send()) {
100 LOG(ERROR, "Sending superstructure goal failed.\n");
101 }
102}
103
Austin Schuh23b21802016-04-03 21:18:56 -0700104void AutonomousActor::OpenShooter() {
105 shooter_speed_ = 0.0;
106
107 if (!control_loops::shooter::shooter_queue.goal.MakeWithBuilder()
108 .angular_velocity(shooter_speed_)
109 .clamp_open(true)
110 .push_to_shooter(false)
111 .force_lights_on(false)
112 .Send()) {
113 LOG(ERROR, "Sending shooter goal failed.\n");
114 }
115}
116
117void AutonomousActor::CloseShooter() {
118 shooter_speed_ = 0.0;
119
120 if (!control_loops::shooter::shooter_queue.goal.MakeWithBuilder()
121 .angular_velocity(shooter_speed_)
122 .clamp_open(false)
123 .push_to_shooter(false)
124 .force_lights_on(false)
125 .Send()) {
126 LOG(ERROR, "Sending shooter goal failed.\n");
127 }
128}
129
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700130void AutonomousActor::SetShooterSpeed(double speed) {
131 shooter_speed_ = speed;
132
133 // In auto, we want to have the lights on whenever possible since we have no
134 // hope of a human aligning the robot.
135 bool force_lights_on = shooter_speed_ > 1.0;
136
137 if (!control_loops::shooter::shooter_queue.goal.MakeWithBuilder()
138 .angular_velocity(shooter_speed_)
139 .clamp_open(false)
140 .push_to_shooter(false)
141 .force_lights_on(force_lights_on)
142 .Send()) {
143 LOG(ERROR, "Sending shooter goal failed.\n");
144 }
145}
146
147void AutonomousActor::Shoot() {
148 uint32_t initial_shots = 0;
149
150 control_loops::shooter::shooter_queue.status.FetchLatest();
151 if (control_loops::shooter::shooter_queue.status.get()) {
152 initial_shots = control_loops::shooter::shooter_queue.status->shots;
153 }
154
155 // In auto, we want to have the lights on whenever possible since we have no
156 // hope of a human aligning the robot.
157 bool force_lights_on = shooter_speed_ > 1.0;
158
159 if (!control_loops::shooter::shooter_queue.goal.MakeWithBuilder()
160 .angular_velocity(shooter_speed_)
161 .clamp_open(false)
162 .push_to_shooter(true)
163 .force_lights_on(force_lights_on)
164 .Send()) {
165 LOG(ERROR, "Sending shooter goal failed.\n");
166 }
167
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700168 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
169 ::std::chrono::milliseconds(5) / 2);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700170 while (true) {
171 if (ShouldCancel()) return;
172
173 // Wait for the shot count to change so we know when the shot is complete.
174 control_loops::shooter::shooter_queue.status.FetchLatest();
175 if (control_loops::shooter::shooter_queue.status.get()) {
176 if (initial_shots < control_loops::shooter::shooter_queue.status->shots) {
177 return;
178 }
179 }
180 phased_loop.SleepUntilNext();
181 }
182}
183
184void AutonomousActor::WaitForShooterSpeed() {
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700185 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
186 ::std::chrono::milliseconds(5) / 2);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700187 while (true) {
188 if (ShouldCancel()) return;
189
190 control_loops::shooter::shooter_queue.status.FetchLatest();
191 if (control_loops::shooter::shooter_queue.status.get()) {
192 if (control_loops::shooter::shooter_queue.status->left.ready &&
193 control_loops::shooter::shooter_queue.status->right.ready) {
194 return;
195 }
196 }
197 phased_loop.SleepUntilNext();
198 }
199}
200
201void AutonomousActor::AlignWithVisionGoal() {
202 actors::VisionAlignActionParams params;
Austin Schuh1bf8a212019-05-26 22:13:14 -0700203 vision_action_ = vision_align_actor_factory_.Make(params);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700204 vision_action_->Start();
205}
206
Austin Schuh23b21802016-04-03 21:18:56 -0700207void AutonomousActor::WaitForAlignedWithVision(
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800208 chrono::nanoseconds align_duration) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700209 bool vision_valid = false;
210 double last_angle = 0.0;
211 int ready_to_fire = 0;
212
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700213 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
214 ::std::chrono::milliseconds(5) / 2);
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800215 monotonic_clock::time_point end_time =
216 monotonic_clock::now() + align_duration;
217 while (end_time > monotonic_clock::now()) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700218 if (ShouldCancel()) break;
219
Austin Schuh28bde302019-05-26 22:24:33 -0700220 vision_status_fetcher_.Fetch();
221 if (vision_status_fetcher_.get()) {
222 vision_valid = (vision_status_fetcher_->left_image_valid &&
223 vision_status_fetcher_->right_image_valid);
224 last_angle = vision_status_fetcher_->horizontal_angle;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700225 }
226
227 drivetrain_queue.status.FetchLatest();
228 drivetrain_queue.goal.FetchLatest();
229
230 if (drivetrain_queue.status.get() && drivetrain_queue.goal.get()) {
231 const double left_goal = drivetrain_queue.goal->left_goal;
232 const double right_goal = drivetrain_queue.goal->right_goal;
233 const double left_current =
234 drivetrain_queue.status->estimated_left_position;
235 const double right_current =
236 drivetrain_queue.status->estimated_right_position;
237 const double left_velocity =
238 drivetrain_queue.status->estimated_left_velocity;
239 const double right_velocity =
240 drivetrain_queue.status->estimated_right_velocity;
241
242 if (vision_valid && ::std::abs(last_angle) < 0.02 &&
243 ::std::abs((left_goal - right_goal) -
244 (left_current - right_current)) /
245 dt_config_.robot_radius / 2.0 <
246 0.02 &&
247 ::std::abs(left_velocity - right_velocity) < 0.01) {
248 ++ready_to_fire;
249 } else {
250 ready_to_fire = 0;
251 }
Austin Schuh3e4a5272016-04-20 20:11:00 -0700252 if (ready_to_fire > 15) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700253 break;
Austin Schuh23b21802016-04-03 21:18:56 -0700254 LOG(INFO, "Vision align success!\n");
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700255 }
256 }
257 phased_loop.SleepUntilNext();
258 }
259
260 vision_action_->Cancel();
261 WaitUntilDoneOrCanceled(::std::move(vision_action_));
Austin Schuh23b21802016-04-03 21:18:56 -0700262 LOG(INFO, "Done waiting for vision\n");
263}
264
265bool AutonomousActor::IntakeDone() {
266 control_loops::superstructure_queue.status.FetchAnother();
267
268 constexpr double kProfileError = 1e-5;
269 constexpr double kEpsilon = 0.15;
270
271 if (control_loops::superstructure_queue.status->state < 12 ||
272 control_loops::superstructure_queue.status->state == 16) {
273 LOG(ERROR, "Superstructure no longer running, aborting action\n");
274 return true;
275 }
276
277 if (::std::abs(control_loops::superstructure_queue.status->intake.goal_angle -
278 superstructure_goal_.intake) < kProfileError &&
279 ::std::abs(control_loops::superstructure_queue.status->intake
280 .goal_angular_velocity) < kProfileError) {
281 LOG(DEBUG, "Profile done.\n");
282 if (::std::abs(control_loops::superstructure_queue.status->intake.angle -
283 superstructure_goal_.intake) < kEpsilon &&
284 ::std::abs(control_loops::superstructure_queue.status->intake
285 .angular_velocity) < kEpsilon) {
286 LOG(INFO, "Near goal, done.\n");
287 return true;
288 }
289 }
290 return false;
291}
292
293bool AutonomousActor::SuperstructureProfileDone() {
294 constexpr double kProfileError = 1e-5;
295 return ::std::abs(
296 control_loops::superstructure_queue.status->intake.goal_angle -
297 superstructure_goal_.intake) < kProfileError &&
298 ::std::abs(
299 control_loops::superstructure_queue.status->shoulder.goal_angle -
300 superstructure_goal_.shoulder) < kProfileError &&
301 ::std::abs(
302 control_loops::superstructure_queue.status->wrist.goal_angle -
303 superstructure_goal_.wrist) < kProfileError &&
304 ::std::abs(control_loops::superstructure_queue.status->intake
305 .goal_angular_velocity) < kProfileError &&
306 ::std::abs(control_loops::superstructure_queue.status->shoulder
307 .goal_angular_velocity) < kProfileError &&
308 ::std::abs(control_loops::superstructure_queue.status->wrist
309 .goal_angular_velocity) < kProfileError;
310}
311
312bool AutonomousActor::SuperstructureDone() {
313 control_loops::superstructure_queue.status.FetchAnother();
314
315 constexpr double kEpsilon = 0.03;
316
317 if (control_loops::superstructure_queue.status->state < 12 ||
318 control_loops::superstructure_queue.status->state == 16) {
319 LOG(ERROR, "Superstructure no longer running, aborting action\n");
320 return true;
321 }
322
323 if (SuperstructureProfileDone()) {
324 LOG(DEBUG, "Profile done.\n");
325 if (::std::abs(control_loops::superstructure_queue.status->intake.angle -
326 superstructure_goal_.intake) < (kEpsilon + 0.1) &&
327 ::std::abs(control_loops::superstructure_queue.status->shoulder.angle -
328 superstructure_goal_.shoulder) < (kEpsilon + 0.05) &&
329 ::std::abs(control_loops::superstructure_queue.status->wrist.angle -
330 superstructure_goal_.wrist) < (kEpsilon + 0.01) &&
331 ::std::abs(control_loops::superstructure_queue.status->intake
332 .angular_velocity) < (kEpsilon + 0.1) &&
333 ::std::abs(control_loops::superstructure_queue.status->shoulder
334 .angular_velocity) < (kEpsilon + 0.10) &&
335 ::std::abs(control_loops::superstructure_queue.status->wrist
336 .angular_velocity) < (kEpsilon + 0.05)) {
337 LOG(INFO, "Near goal, done.\n");
338 return true;
339 }
340 }
341 return false;
342}
343
344void AutonomousActor::WaitForIntake() {
345 while (true) {
346 if (ShouldCancel()) return;
347 if (IntakeDone()) return;
348 }
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700349}
350
Comran Morshedb134e772016-03-16 21:05:05 +0000351void AutonomousActor::WaitForSuperstructure() {
352 while (true) {
353 if (ShouldCancel()) return;
Austin Schuh23b21802016-04-03 21:18:56 -0700354 if (SuperstructureDone()) return;
355 }
356}
Comran Morshedb134e772016-03-16 21:05:05 +0000357
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700358void AutonomousActor::WaitForSuperstructureProfile() {
359 while (true) {
360 if (ShouldCancel()) return;
361 control_loops::superstructure_queue.status.FetchAnother();
362
363 if (control_loops::superstructure_queue.status->state < 12 ||
364 control_loops::superstructure_queue.status->state == 16) {
365 LOG(ERROR, "Superstructure no longer running, aborting action\n");
366 return;
367 }
368
369 if (SuperstructureProfileDone()) return;
370 }
371}
372
Austin Schuh23b21802016-04-03 21:18:56 -0700373void AutonomousActor::WaitForSuperstructureLow() {
374 while (true) {
375 if (ShouldCancel()) return;
376 control_loops::superstructure_queue.status.FetchAnother();
Comran Morshedb134e772016-03-16 21:05:05 +0000377
378 if (control_loops::superstructure_queue.status->state < 12 ||
379 control_loops::superstructure_queue.status->state == 16) {
380 LOG(ERROR, "Superstructure no longer running, aborting action\n");
381 return;
382 }
Austin Schuh23b21802016-04-03 21:18:56 -0700383 if (SuperstructureProfileDone()) return;
384 if (control_loops::superstructure_queue.status->shoulder.angle < 0.1) {
385 return;
Comran Morshedb134e772016-03-16 21:05:05 +0000386 }
387 }
388}
Austin Schuh23b21802016-04-03 21:18:56 -0700389void AutonomousActor::BackLongShotLowBarTwoBall() {
390 LOG(INFO, "Expanding for back long shot\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700391 MoveSuperstructure(0.00, M_PI / 2.0 - 0.2, -0.55, {7.0, 40.0}, {4.0, 6.0},
Austin Schuh23b21802016-04-03 21:18:56 -0700392 {10.0, 25.0}, false, 0.0);
393}
394
395void AutonomousActor::BackLongShotTwoBall() {
396 LOG(INFO, "Expanding for back long shot\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700397 MoveSuperstructure(0.00, M_PI / 2.0 - 0.2, -0.55, {7.0, 40.0}, {4.0, 6.0},
398 {10.0, 25.0}, false, 0.0);
399}
400
401void AutonomousActor::BackLongShotTwoBallFinish() {
402 LOG(INFO, "Expanding for back long shot\n");
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000403 MoveSuperstructure(0.00, M_PI / 2.0 - 0.2, -0.625 + 0.03, {7.0, 40.0},
404 {4.0, 6.0}, {10.0, 25.0}, false, 0.0);
Austin Schuh23b21802016-04-03 21:18:56 -0700405}
406
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700407void AutonomousActor::BackLongShot() {
408 LOG(INFO, "Expanding for back long shot\n");
Austin Schuh23b21802016-04-03 21:18:56 -0700409 MoveSuperstructure(0.80, M_PI / 2.0 - 0.2, -0.62, {7.0, 40.0}, {4.0, 6.0},
410 {10.0, 25.0}, false, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700411}
412
413void AutonomousActor::BackMiddleShot() {
414 LOG(INFO, "Expanding for back middle shot\n");
415 MoveSuperstructure(-0.05, M_PI / 2.0 - 0.2, -0.665, {7.0, 40.0}, {4.0, 10.0},
Austin Schuh23b21802016-04-03 21:18:56 -0700416 {10.0, 25.0}, false, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700417}
418
Austin Schuh3e4a5272016-04-20 20:11:00 -0700419void AutonomousActor::FrontLongShot() {
420 LOG(INFO, "Expanding for front long shot\n");
421 MoveSuperstructure(0.80, M_PI / 2.0 + 0.1, M_PI + 0.41 + 0.02, {7.0, 40.0},
422 {4.0, 6.0}, {10.0, 25.0}, false, 0.0);
423}
424
425void AutonomousActor::FrontMiddleShot() {
426 LOG(INFO, "Expanding for front middle shot\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700427 MoveSuperstructure(-0.05, M_PI / 2.0 + 0.1, M_PI + 0.44, {7.0, 40.0},
Austin Schuh3e4a5272016-04-20 20:11:00 -0700428 {4.0, 10.0}, {10.0, 25.0}, true, 0.0);
429}
430
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700431void AutonomousActor::TuckArm(bool low_bar, bool traverse_down) {
432 MoveSuperstructure(low_bar ? -0.05 : 2.0, -0.010, 0.0, {7.0, 40.0},
Austin Schuh23b21802016-04-03 21:18:56 -0700433 {4.0, 10.0}, {10.0, 25.0}, !traverse_down, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700434}
435
Austin Schuh3e4a5272016-04-20 20:11:00 -0700436void AutonomousActor::DoFullShot() {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700437 if (ShouldCancel()) return;
438 // Make sure that the base is aligned with the base.
439 LOG(INFO, "Waiting for the superstructure\n");
440 WaitForSuperstructure();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700441
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800442 this_thread::sleep_for(chrono::milliseconds(500));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700443
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700444 if (ShouldCancel()) return;
445 LOG(INFO, "Triggering the vision actor\n");
446 AlignWithVisionGoal();
447
448 // Wait for the drive base to be aligned with the target and make sure that
449 // the shooter is up to speed.
450 LOG(INFO, "Waiting for vision to be aligned\n");
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800451 WaitForAlignedWithVision(chrono::milliseconds(2000));
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700452 if (ShouldCancel()) return;
453 LOG(INFO, "Waiting for shooter to be up to speed\n");
454 WaitForShooterSpeed();
455 if (ShouldCancel()) return;
456
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800457 this_thread::sleep_for(chrono::milliseconds(300));
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700458 LOG(INFO, "Shoot!\n");
459 Shoot();
460
461 // Turn off the shooter and fold up the superstructure.
462 if (ShouldCancel()) return;
463 LOG(INFO, "Stopping shooter\n");
464 SetShooterSpeed(0.0);
465 LOG(INFO, "Folding superstructure back down\n");
466 TuckArm(false, false);
467
468 // Wait for everything to be folded up.
469 LOG(INFO, "Waiting for superstructure to be folded back down\n");
Austin Schuh3e4a5272016-04-20 20:11:00 -0700470 WaitForSuperstructureLow();
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700471}
472
473void AutonomousActor::LowBarDrive() {
474 TuckArm(false, true);
475 StartDrive(-5.5, 0.0, kLowBarDrive, kSlowTurn);
476
477 if (!WaitForDriveNear(5.3, 0.0)) return;
478 TuckArm(true, true);
479
480 if (!WaitForDriveNear(5.0, 0.0)) return;
481
482 StartDrive(0.0, 0.0, kLowBarDrive, kSlowTurn);
483
484 if (!WaitForDriveNear(3.0, 0.0)) return;
485
486 StartDrive(0.0, 0.0, kLowBarDrive, kSlowTurn);
487
488 if (!WaitForDriveNear(1.0, 0.0)) return;
489
Austin Schuh15b5f6a2016-03-26 19:43:56 -0700490 StartDrive(0, -M_PI / 4.0 - 0.2, kLowBarDrive, kSlowTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700491}
492
Austin Schuh3e4a5272016-04-20 20:11:00 -0700493void AutonomousActor::TippyDrive(double goal_distance, double tip_distance,
494 double below, double above) {
495 StartDrive(goal_distance, 0.0, kMoatDrive, kSlowTurn);
496 if (!WaitForBelowAngle(below)) return;
497 if (!WaitForAboveAngle(above)) return;
498 // Ok, we are good now. Compensate by moving the goal by the error.
499 // Should be here at 2.7
500 drivetrain_queue.status.FetchLatest();
501 if (drivetrain_queue.status.get()) {
502 const double left_error =
503 (initial_drivetrain_.left -
504 drivetrain_queue.status->estimated_left_position);
505 const double right_error =
506 (initial_drivetrain_.right -
507 drivetrain_queue.status->estimated_right_position);
508 const double distance_to_go = (left_error + right_error) / 2.0;
509 const double distance_compensation =
510 goal_distance - tip_distance - distance_to_go;
511 LOG(INFO, "Going %f further at the bump\n", distance_compensation);
512 StartDrive(distance_compensation, 0.0, kMoatDrive, kSlowTurn);
513 }
514}
515
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700516void AutonomousActor::MiddleDrive() {
517 TuckArm(false, false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700518 TippyDrive(3.65, 2.7, -0.2, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700519 if (!WaitForDriveDone()) return;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700520}
521
522void AutonomousActor::OneFromMiddleDrive(bool left) {
Austin Schuh3e4a5272016-04-20 20:11:00 -0700523 const double kTurnAngle = left ? -0.41 : 0.41;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700524 TuckArm(false, false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700525 TippyDrive(4.05, 2.7, -0.2, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700526
527 if (!WaitForDriveDone()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700528 StartDrive(0.0, kTurnAngle, kRealignDrive, kFastTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700529}
530
531void AutonomousActor::TwoFromMiddleDrive() {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700532 TuckArm(false, false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700533 constexpr double kDriveDistance = 5.10;
534 TippyDrive(kDriveDistance, 2.7, -0.2, 0.0);
535
536 if (!WaitForDriveNear(kDriveDistance - 3.0, 2.0)) return;
537 StartDrive(0, -M_PI / 2 - 0.10, kMoatDrive, kFastTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700538
539 if (!WaitForDriveDone()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700540 StartDrive(0, M_PI / 3 + 0.35, kMoatDrive, kFastTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700541}
Comran Morshedb134e772016-03-16 21:05:05 +0000542
Austin Schuh23b21802016-04-03 21:18:56 -0700543void AutonomousActor::CloseIfBall() {
Austin Schuh4b652c92019-05-27 13:22:27 -0700544 ball_detector_fetcher_.Fetch();
545 if (ball_detector_fetcher_.get()) {
546 const bool ball_detected = ball_detector_fetcher_->voltage > 2.5;
Austin Schuh23b21802016-04-03 21:18:56 -0700547 if (ball_detected) {
548 CloseShooter();
549 }
550 }
551}
552
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700553void AutonomousActor::WaitForBallOrDriveDone() {
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700554 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
555 ::std::chrono::milliseconds(5) / 2);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700556 while (true) {
557 if (ShouldCancel()) {
558 return;
559 }
560 phased_loop.SleepUntilNext();
561 drivetrain_queue.status.FetchLatest();
562 if (IsDriveDone()) {
563 return;
564 }
565
Austin Schuh4b652c92019-05-27 13:22:27 -0700566 ball_detector_fetcher_.Fetch();
567 if (ball_detector_fetcher_.get()) {
568 const bool ball_detected = ball_detector_fetcher_->voltage > 2.5;
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700569 if (ball_detected) {
570 return;
571 }
572 }
573 }
574}
575
Austin Schuh3e4a5272016-04-20 20:11:00 -0700576void AutonomousActor::TwoBallAuto() {
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800577 monotonic_clock::time_point start_time = monotonic_clock::now();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700578 OpenShooter();
579 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
580 false, 12.0);
581 if (ShouldCancel()) return;
582 LOG(INFO, "Waiting for the intake to come down.\n");
583
584 WaitForIntake();
585 LOG(INFO, "Intake done at %f seconds, starting to drive\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700586 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700587 if (ShouldCancel()) return;
588 const double kDriveDistance = 5.05;
589 StartDrive(-kDriveDistance, 0.0, kTwoBallLowDrive, kSlowTurn);
590
591 StartDrive(0.0, 0.4, kTwoBallLowDrive, kSwerveTurn);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700592 if (!WaitForDriveNear(kDriveDistance - 0.5, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700593
Austin Schuh295c2d92016-05-01 12:28:04 -0700594 // Check if the ball is there.
595 bool first_ball_there = true;
Austin Schuh4b652c92019-05-27 13:22:27 -0700596 ball_detector_fetcher_.Fetch();
597 if (ball_detector_fetcher_.get()) {
598 const bool ball_detected = ball_detector_fetcher_->voltage > 2.5;
Austin Schuh295c2d92016-05-01 12:28:04 -0700599 first_ball_there = ball_detected;
600 LOG(INFO, "Saw the ball: %d at %f\n", first_ball_there,
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700601 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh295c2d92016-05-01 12:28:04 -0700602 }
Austin Schuh3e4a5272016-04-20 20:11:00 -0700603 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 40.0}, {4.0, 10.0}, {10.0, 25.0},
604 false, 0.0);
605 LOG(INFO, "Shutting off rollers at %f seconds, starting to straighten out\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700606 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700607 StartDrive(0.0, -0.4, kTwoBallLowDrive, kSwerveTurn);
608 MoveSuperstructure(-0.05, -0.010, 0.0, {8.0, 40.0}, {4.0, 10.0}, {10.0, 25.0},
609 false, 0.0);
610 CloseShooter();
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700611 if (!WaitForDriveNear(kDriveDistance - 2.4, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700612
613 // We are now under the low bar. Start lifting.
614 BackLongShotLowBarTwoBall();
615 LOG(INFO, "Spinning up the shooter wheels\n");
616 SetShooterSpeed(640.0);
617 StartDrive(0.0, 0.0, kTwoBallFastDrive, kSwerveTurn);
618
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700619 if (!WaitForDriveNear(1.50, kDoNotTurnCare)) return;
620 constexpr double kShootTurnAngle = -M_PI / 4.0 - 0.05;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700621 StartDrive(0, kShootTurnAngle, kTwoBallFastDrive, kFinishTurn);
622 BackLongShotTwoBall();
623
624 if (!WaitForDriveDone()) return;
625 LOG(INFO, "First shot done driving at %f seconds\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700626 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700627
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700628 WaitForSuperstructureProfile();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700629
630 if (ShouldCancel()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700631 AlignWithVisionGoal();
632
633 WaitForShooterSpeed();
634 if (ShouldCancel()) return;
635
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800636 constexpr chrono::milliseconds kVisionExtra{0};
637 WaitForAlignedWithVision(chrono::milliseconds(500) + kVisionExtra);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700638 BackLongShotTwoBallFinish();
639 WaitForSuperstructureProfile();
640 if (ShouldCancel()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700641 LOG(INFO, "Shoot!\n");
Austin Schuh295c2d92016-05-01 12:28:04 -0700642 if (first_ball_there) {
643 Shoot();
644 } else {
645 LOG(INFO, "Nah, not shooting\n");
646 }
Austin Schuh3e4a5272016-04-20 20:11:00 -0700647
648 LOG(INFO, "First shot at %f seconds\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700649 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700650 if (ShouldCancel()) return;
651
652 SetShooterSpeed(0.0);
653 LOG(INFO, "Folding superstructure back down\n");
654 TuckArm(true, true);
655
656 // Undo vision move.
657 StartDrive(0.0, 0.0, kTwoBallFastDrive, kFinishTurn);
658 if (!WaitForDriveDone()) return;
659
660 constexpr double kBackDrive = 3.09 - 0.4;
661 StartDrive(kBackDrive, 0.0, kTwoBallReturnDrive, kSlowTurn);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700662 if (!WaitForDriveNear(kBackDrive - 0.19, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700663 StartDrive(0, -kShootTurnAngle, kTwoBallReturnDrive, kSwerveTurn);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700664 if (!WaitForDriveNear(1.0, kDoNotTurnCare)) return;
665 StartDrive(0, 0, kTwoBallReturnSlow, kSwerveTurn);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700666
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700667 if (!WaitForDriveNear(0.06, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700668 LOG(INFO, "At Low Bar %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700669 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700670
671 OpenShooter();
672 constexpr double kSecondBallAfterBarDrive = 2.10;
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700673 StartDrive(kSecondBallAfterBarDrive, 0.0, kTwoBallBallPickupAccel, kSlowTurn);
674 if (!WaitForDriveNear(kSecondBallAfterBarDrive - 0.5, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700675 constexpr double kBallSmallWallTurn = -0.11;
676 StartDrive(0, kBallSmallWallTurn, kTwoBallBallPickup, kFinishTurn);
677
678 MoveSuperstructure(0.03, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
679 false, 12.0);
680
681 if (!WaitForDriveProfileDone()) return;
682
683 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
684 false, 12.0);
685
686 LOG(INFO, "Done backing up %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700687 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700688
689 constexpr double kDriveBackDistance = 5.15 - 0.4;
690 StartDrive(-kDriveBackDistance, 0.0, kTwoBallLowDrive, kFinishTurn);
691 CloseIfBall();
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700692 if (!WaitForDriveNear(kDriveBackDistance - 0.75, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700693
694 StartDrive(0.0, -kBallSmallWallTurn, kTwoBallLowDrive, kFinishTurn);
695 LOG(INFO, "Straightening up at %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700696 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700697
698 CloseIfBall();
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700699 if (!WaitForDriveNear(kDriveBackDistance - 2.3, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700700
Austin Schuh4b652c92019-05-27 13:22:27 -0700701 ball_detector_fetcher_.Fetch();
702 if (ball_detector_fetcher_.get()) {
703 const bool ball_detected = ball_detector_fetcher_->voltage > 2.5;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700704 if (!ball_detected) {
705 if (!WaitForDriveDone()) return;
706 LOG(INFO, "Aborting, no ball %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700707 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700708 return;
709 }
710 }
711 CloseShooter();
712
713 BackLongShotLowBarTwoBall();
714 LOG(INFO, "Spinning up the shooter wheels\n");
715 SetShooterSpeed(640.0);
716 StartDrive(0.0, 0.0, kTwoBallFastDrive, kSwerveTurn);
717
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700718 if (!WaitForDriveNear(1.80, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700719 StartDrive(0, kShootTurnAngle, kTwoBallFastDrive, kFinishTurn);
720 BackLongShotTwoBall();
721
722 if (!WaitForDriveDone()) return;
723 LOG(INFO, "Second shot done driving at %f seconds\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700724 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700725 WaitForSuperstructure();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700726 AlignWithVisionGoal();
727 if (ShouldCancel()) return;
728
729 WaitForShooterSpeed();
730 if (ShouldCancel()) return;
731
732 // 2.2 with 0.4 of vision.
733 // 1.8 without any vision.
734 LOG(INFO, "Going to vision align at %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700735 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800736 WaitForAlignedWithVision(
737 (start_time + chrono::milliseconds(13500) + kVisionExtra * 2) -
738 monotonic_clock::now());
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700739 BackLongShotTwoBallFinish();
740 WaitForSuperstructureProfile();
741 if (ShouldCancel()) return;
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800742 LOG(INFO, "Shoot at %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700743 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700744 Shoot();
745
746 LOG(INFO, "Second shot at %f seconds\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700747 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700748 if (ShouldCancel()) return;
749
750 SetShooterSpeed(0.0);
751 LOG(INFO, "Folding superstructure back down\n");
752 TuckArm(true, false);
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700753 LOG(INFO, "Shot %f\n",
754 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700755
756 WaitForSuperstructureLow();
757
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700758 LOG(INFO, "Done %f\n",
759 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700760}
761
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700762void AutonomousActor::StealAndMoveOverBy(double distance) {
763 OpenShooter();
764 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
765 true, 12.0);
766 if (ShouldCancel()) return;
767 LOG(INFO, "Waiting for the intake to come down.\n");
768
769 WaitForIntake();
770 if (ShouldCancel()) return;
771 StartDrive(-distance, M_PI / 2.0, kFastDrive, kStealTurn);
772 WaitForBallOrDriveDone();
773 if (ShouldCancel()) return;
774 MoveSuperstructure(1.0, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
775 true, 12.0);
776
777 if (!WaitForDriveDone()) return;
778 StartDrive(0.0, M_PI / 2.0, kFastDrive, kStealTurn);
779 if (!WaitForDriveDone()) return;
780}
781
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000782bool AutonomousActor::RunAction(
783 const ::frc971::autonomous::AutonomousActionParams &params) {
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800784 monotonic_clock::time_point start_time = monotonic_clock::now();
Comran Morshede68e3732016-03-12 14:12:11 +0000785 LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode);
786
Comran Morshed435f1112016-03-12 14:20:45 +0000787 InitializeEncoders();
788 ResetDrivetrain();
789
Austin Schuh295c2d92016-05-01 12:28:04 -0700790 switch (params.mode) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700791 case 0:
792 LowBarDrive();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700793 if (!WaitForDriveDone()) return true;
794 // Get the superstructure to unfold and get ready for shooting.
795 LOG(INFO, "Unfolding superstructure\n");
796 FrontLongShot();
797
798 // Spin up the shooter wheels.
799 LOG(INFO, "Spinning up the shooter wheels\n");
800 SetShooterSpeed(640.0);
801
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700802 break;
803 case 1:
804 TwoFromMiddleDrive();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700805 if (!WaitForDriveDone()) return true;
806 // Get the superstructure to unfold and get ready for shooting.
807 LOG(INFO, "Unfolding superstructure\n");
808 FrontMiddleShot();
809
810 // Spin up the shooter wheels.
811 LOG(INFO, "Spinning up the shooter wheels\n");
812 SetShooterSpeed(600.0);
813
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700814 break;
815 case 2:
816 OneFromMiddleDrive(true);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700817 if (!WaitForDriveDone()) return true;
818 // Get the superstructure to unfold and get ready for shooting.
819 LOG(INFO, "Unfolding superstructure\n");
820 FrontMiddleShot();
821
822 // Spin up the shooter wheels.
823 LOG(INFO, "Spinning up the shooter wheels\n");
824 SetShooterSpeed(600.0);
825
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700826 break;
827 case 3:
828 MiddleDrive();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700829 if (!WaitForDriveDone()) return true;
830 // Get the superstructure to unfold and get ready for shooting.
831 LOG(INFO, "Unfolding superstructure\n");
832 FrontMiddleShot();
833
834 // Spin up the shooter wheels.
835 LOG(INFO, "Spinning up the shooter wheels\n");
836 SetShooterSpeed(600.0);
837
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700838 break;
839 case 4:
840 OneFromMiddleDrive(false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700841 if (!WaitForDriveDone()) return true;
842 // Get the superstructure to unfold and get ready for shooting.
843 LOG(INFO, "Unfolding superstructure\n");
844 FrontMiddleShot();
845
846 // Spin up the shooter wheels.
847 LOG(INFO, "Spinning up the shooter wheels\n");
848 SetShooterSpeed(600.0);
849
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700850 break;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700851 case 5:
Campbell Crowley9ed61a52016-11-05 17:13:07 -0700852 case 15:
Austin Schuh3e4a5272016-04-20 20:11:00 -0700853 TwoBallAuto();
Austin Schuh23b21802016-04-03 21:18:56 -0700854 return true;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700855 break;
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700856 case 6:
857 StealAndMoveOverBy(3.10 + 2 * 52 * 2.54 / 100.0);
858 if (ShouldCancel()) return true;
859
860 TwoFromMiddleDrive();
861 if (!WaitForDriveDone()) return true;
862 // Get the superstructure to unfold and get ready for shooting.
863 LOG(INFO, "Unfolding superstructure\n");
864 FrontMiddleShot();
865
866 // Spin up the shooter wheels.
867 LOG(INFO, "Spinning up the shooter wheels\n");
868 SetShooterSpeed(600.0);
869
870 break;
871 case 7:
872 StealAndMoveOverBy(2.95 + 52 * 2.54 / 100.0);
873 if (ShouldCancel()) return true;
874
875 OneFromMiddleDrive(true);
876 if (!WaitForDriveDone()) return true;
877 // Get the superstructure to unfold and get ready for shooting.
878 LOG(INFO, "Unfolding superstructure\n");
879 FrontMiddleShot();
880
881 // Spin up the shooter wheels.
882 LOG(INFO, "Spinning up the shooter wheels\n");
883 SetShooterSpeed(600.0);
884
885 break;
886 case 8: {
887 StealAndMoveOverBy(2.95);
888 if (ShouldCancel()) return true;
889
890 MiddleDrive();
891 if (!WaitForDriveDone()) return true;
892 // Get the superstructure to unfold and get ready for shooting.
893 LOG(INFO, "Unfolding superstructure\n");
894 FrontMiddleShot();
895
896 // Spin up the shooter wheels.
897 LOG(INFO, "Spinning up the shooter wheels\n");
898 SetShooterSpeed(600.0);
899
900 } break;
901 case 9: {
902 StealAndMoveOverBy(1.70);
903 if (ShouldCancel()) return true;
904
905 OneFromMiddleDrive(false);
906 if (!WaitForDriveDone()) return true;
907 // Get the superstructure to unfold and get ready for shooting.
908 LOG(INFO, "Unfolding superstructure\n");
909 FrontMiddleShot();
910
911 // Spin up the shooter wheels.
912 LOG(INFO, "Spinning up the shooter wheels\n");
913 SetShooterSpeed(600.0);
914
915 } break;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700916 default:
Austin Schuh6c9bc622016-03-26 19:44:12 -0700917 LOG(ERROR, "Invalid auto mode %d\n", params.mode);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700918 return true;
919 }
Comran Morshed435f1112016-03-12 14:20:45 +0000920
Austin Schuh3e4a5272016-04-20 20:11:00 -0700921 DoFullShot();
922
923 StartDrive(0.5, 0.0, kMoatDrive, kFastTurn);
Comran Morshed435f1112016-03-12 14:20:45 +0000924 if (!WaitForDriveDone()) return true;
925
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700926 LOG(INFO, "Done %f\n",
927 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Comran Morshed435f1112016-03-12 14:20:45 +0000928
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700929 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
930 ::std::chrono::milliseconds(5) / 2);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700931
Comran Morshed435f1112016-03-12 14:20:45 +0000932 while (!ShouldCancel()) {
933 phased_loop.SleepUntilNext();
Comran Morshede68e3732016-03-12 14:12:11 +0000934 }
Comran Morshed435f1112016-03-12 14:20:45 +0000935 LOG(DEBUG, "Done running\n");
Comran Morshede68e3732016-03-12 14:12:11 +0000936
937 return true;
938}
939
Comran Morshede68e3732016-03-12 14:12:11 +0000940} // namespace actors
941} // namespace y2016