blob: ea909443cdadd00ae3a67ec2687aed746ef45125 [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/util/phased_loop.h"
9#include "aos/logging/logging.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 {
Comran Morshed435f1112016-03-12 14:20:45 +000020using ::frc971::control_loops::drivetrain_queue;
Austin Schuhf2a50ba2016-12-24 16:16:26 -080021using ::aos::monotonic_clock;
22namespace 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
46double DoubleSeconds(monotonic_clock::duration duration) {
47 return ::std::chrono::duration_cast<::std::chrono::duration<double>>(duration)
48 .count();
49}
Comran Morshed435f1112016-03-12 14:20:45 +000050} // namespace
Comran Morshede68e3732016-03-12 14:12:11 +000051
Philipp Schrader4bd29b12017-02-22 04:42:27 +000052AutonomousActor::AutonomousActor(
53 ::frc971::autonomous::AutonomousActionQueueGroup *s)
54 : frc971::autonomous::BaseAutonomousActor(
55 s, control_loops::drivetrain::GetDrivetrainConfig()) {}
Comran Morshed435f1112016-03-12 14:20:45 +000056
Austin Schuhe4ec49c2016-04-24 19:07:15 -070057constexpr double kDoNotTurnCare = 2.0;
58
Comran Morshedb134e772016-03-16 21:05:05 +000059void AutonomousActor::MoveSuperstructure(
60 double intake, double shoulder, double wrist,
61 const ProfileParameters intake_params,
62 const ProfileParameters shoulder_params,
Austin Schuh23b21802016-04-03 21:18:56 -070063 const ProfileParameters wrist_params, bool traverse_up,
64 double roller_power) {
Comran Morshedb134e772016-03-16 21:05:05 +000065 superstructure_goal_ = {intake, shoulder, wrist};
66
67 auto new_superstructure_goal =
68 ::y2016::control_loops::superstructure_queue.goal.MakeMessage();
69
70 new_superstructure_goal->angle_intake = intake;
71 new_superstructure_goal->angle_shoulder = shoulder;
72 new_superstructure_goal->angle_wrist = wrist;
73
74 new_superstructure_goal->max_angular_velocity_intake =
75 intake_params.max_velocity;
76 new_superstructure_goal->max_angular_velocity_shoulder =
77 shoulder_params.max_velocity;
78 new_superstructure_goal->max_angular_velocity_wrist =
79 wrist_params.max_velocity;
80
81 new_superstructure_goal->max_angular_acceleration_intake =
82 intake_params.max_acceleration;
83 new_superstructure_goal->max_angular_acceleration_shoulder =
84 shoulder_params.max_acceleration;
85 new_superstructure_goal->max_angular_acceleration_wrist =
86 wrist_params.max_acceleration;
87
Austin Schuh23b21802016-04-03 21:18:56 -070088 new_superstructure_goal->voltage_top_rollers = roller_power;
89 new_superstructure_goal->voltage_bottom_rollers = roller_power;
Austin Schuhf59b8ee2016-03-19 21:31:36 -070090
91 new_superstructure_goal->traverse_unlatched = true;
92 new_superstructure_goal->traverse_down = !traverse_up;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070093 new_superstructure_goal->voltage_climber = 0.0;
94 new_superstructure_goal->unfold_climber = false;
Comran Morshedb134e772016-03-16 21:05:05 +000095
96 if (!new_superstructure_goal.Send()) {
97 LOG(ERROR, "Sending superstructure goal failed.\n");
98 }
99}
100
Austin Schuh23b21802016-04-03 21:18:56 -0700101void AutonomousActor::OpenShooter() {
102 shooter_speed_ = 0.0;
103
104 if (!control_loops::shooter::shooter_queue.goal.MakeWithBuilder()
105 .angular_velocity(shooter_speed_)
106 .clamp_open(true)
107 .push_to_shooter(false)
108 .force_lights_on(false)
109 .Send()) {
110 LOG(ERROR, "Sending shooter goal failed.\n");
111 }
112}
113
114void AutonomousActor::CloseShooter() {
115 shooter_speed_ = 0.0;
116
117 if (!control_loops::shooter::shooter_queue.goal.MakeWithBuilder()
118 .angular_velocity(shooter_speed_)
119 .clamp_open(false)
120 .push_to_shooter(false)
121 .force_lights_on(false)
122 .Send()) {
123 LOG(ERROR, "Sending shooter goal failed.\n");
124 }
125}
126
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700127void AutonomousActor::SetShooterSpeed(double speed) {
128 shooter_speed_ = speed;
129
130 // In auto, we want to have the lights on whenever possible since we have no
131 // hope of a human aligning the robot.
132 bool force_lights_on = shooter_speed_ > 1.0;
133
134 if (!control_loops::shooter::shooter_queue.goal.MakeWithBuilder()
135 .angular_velocity(shooter_speed_)
136 .clamp_open(false)
137 .push_to_shooter(false)
138 .force_lights_on(force_lights_on)
139 .Send()) {
140 LOG(ERROR, "Sending shooter goal failed.\n");
141 }
142}
143
144void AutonomousActor::Shoot() {
145 uint32_t initial_shots = 0;
146
147 control_loops::shooter::shooter_queue.status.FetchLatest();
148 if (control_loops::shooter::shooter_queue.status.get()) {
149 initial_shots = control_loops::shooter::shooter_queue.status->shots;
150 }
151
152 // In auto, we want to have the lights on whenever possible since we have no
153 // hope of a human aligning the robot.
154 bool force_lights_on = shooter_speed_ > 1.0;
155
156 if (!control_loops::shooter::shooter_queue.goal.MakeWithBuilder()
157 .angular_velocity(shooter_speed_)
158 .clamp_open(false)
159 .push_to_shooter(true)
160 .force_lights_on(force_lights_on)
161 .Send()) {
162 LOG(ERROR, "Sending shooter goal failed.\n");
163 }
164
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700165 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
166 ::std::chrono::milliseconds(5) / 2);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700167 while (true) {
168 if (ShouldCancel()) return;
169
170 // Wait for the shot count to change so we know when the shot is complete.
171 control_loops::shooter::shooter_queue.status.FetchLatest();
172 if (control_loops::shooter::shooter_queue.status.get()) {
173 if (initial_shots < control_loops::shooter::shooter_queue.status->shots) {
174 return;
175 }
176 }
177 phased_loop.SleepUntilNext();
178 }
179}
180
181void AutonomousActor::WaitForShooterSpeed() {
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700182 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
183 ::std::chrono::milliseconds(5) / 2);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700184 while (true) {
185 if (ShouldCancel()) return;
186
187 control_loops::shooter::shooter_queue.status.FetchLatest();
188 if (control_loops::shooter::shooter_queue.status.get()) {
189 if (control_loops::shooter::shooter_queue.status->left.ready &&
190 control_loops::shooter::shooter_queue.status->right.ready) {
191 return;
192 }
193 }
194 phased_loop.SleepUntilNext();
195 }
196}
197
198void AutonomousActor::AlignWithVisionGoal() {
199 actors::VisionAlignActionParams params;
sabinaf5584322017-09-23 18:37:19 -0700200 vision_action_ = actors::MakeVisionAlignAction(params);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700201 vision_action_->Start();
202}
203
Austin Schuh23b21802016-04-03 21:18:56 -0700204void AutonomousActor::WaitForAlignedWithVision(
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800205 chrono::nanoseconds align_duration) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700206 bool vision_valid = false;
207 double last_angle = 0.0;
208 int ready_to_fire = 0;
209
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700210 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
211 ::std::chrono::milliseconds(5) / 2);
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800212 monotonic_clock::time_point end_time =
213 monotonic_clock::now() + align_duration;
214 while (end_time > monotonic_clock::now()) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700215 if (ShouldCancel()) break;
216
217 ::y2016::vision::vision_status.FetchLatest();
218 if (::y2016::vision::vision_status.get()) {
219 vision_valid = (::y2016::vision::vision_status->left_image_valid &&
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000220 ::y2016::vision::vision_status->right_image_valid);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700221 last_angle = ::y2016::vision::vision_status->horizontal_angle;
222 }
223
224 drivetrain_queue.status.FetchLatest();
225 drivetrain_queue.goal.FetchLatest();
226
227 if (drivetrain_queue.status.get() && drivetrain_queue.goal.get()) {
228 const double left_goal = drivetrain_queue.goal->left_goal;
229 const double right_goal = drivetrain_queue.goal->right_goal;
230 const double left_current =
231 drivetrain_queue.status->estimated_left_position;
232 const double right_current =
233 drivetrain_queue.status->estimated_right_position;
234 const double left_velocity =
235 drivetrain_queue.status->estimated_left_velocity;
236 const double right_velocity =
237 drivetrain_queue.status->estimated_right_velocity;
238
239 if (vision_valid && ::std::abs(last_angle) < 0.02 &&
240 ::std::abs((left_goal - right_goal) -
241 (left_current - right_current)) /
242 dt_config_.robot_radius / 2.0 <
243 0.02 &&
244 ::std::abs(left_velocity - right_velocity) < 0.01) {
245 ++ready_to_fire;
246 } else {
247 ready_to_fire = 0;
248 }
Austin Schuh3e4a5272016-04-20 20:11:00 -0700249 if (ready_to_fire > 15) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700250 break;
Austin Schuh23b21802016-04-03 21:18:56 -0700251 LOG(INFO, "Vision align success!\n");
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700252 }
253 }
254 phased_loop.SleepUntilNext();
255 }
256
257 vision_action_->Cancel();
258 WaitUntilDoneOrCanceled(::std::move(vision_action_));
Austin Schuh23b21802016-04-03 21:18:56 -0700259 LOG(INFO, "Done waiting for vision\n");
260}
261
262bool AutonomousActor::IntakeDone() {
263 control_loops::superstructure_queue.status.FetchAnother();
264
265 constexpr double kProfileError = 1e-5;
266 constexpr double kEpsilon = 0.15;
267
268 if (control_loops::superstructure_queue.status->state < 12 ||
269 control_loops::superstructure_queue.status->state == 16) {
270 LOG(ERROR, "Superstructure no longer running, aborting action\n");
271 return true;
272 }
273
274 if (::std::abs(control_loops::superstructure_queue.status->intake.goal_angle -
275 superstructure_goal_.intake) < kProfileError &&
276 ::std::abs(control_loops::superstructure_queue.status->intake
277 .goal_angular_velocity) < kProfileError) {
278 LOG(DEBUG, "Profile done.\n");
279 if (::std::abs(control_loops::superstructure_queue.status->intake.angle -
280 superstructure_goal_.intake) < kEpsilon &&
281 ::std::abs(control_loops::superstructure_queue.status->intake
282 .angular_velocity) < kEpsilon) {
283 LOG(INFO, "Near goal, done.\n");
284 return true;
285 }
286 }
287 return false;
288}
289
290bool AutonomousActor::SuperstructureProfileDone() {
291 constexpr double kProfileError = 1e-5;
292 return ::std::abs(
293 control_loops::superstructure_queue.status->intake.goal_angle -
294 superstructure_goal_.intake) < kProfileError &&
295 ::std::abs(
296 control_loops::superstructure_queue.status->shoulder.goal_angle -
297 superstructure_goal_.shoulder) < kProfileError &&
298 ::std::abs(
299 control_loops::superstructure_queue.status->wrist.goal_angle -
300 superstructure_goal_.wrist) < kProfileError &&
301 ::std::abs(control_loops::superstructure_queue.status->intake
302 .goal_angular_velocity) < kProfileError &&
303 ::std::abs(control_loops::superstructure_queue.status->shoulder
304 .goal_angular_velocity) < kProfileError &&
305 ::std::abs(control_loops::superstructure_queue.status->wrist
306 .goal_angular_velocity) < kProfileError;
307}
308
309bool AutonomousActor::SuperstructureDone() {
310 control_loops::superstructure_queue.status.FetchAnother();
311
312 constexpr double kEpsilon = 0.03;
313
314 if (control_loops::superstructure_queue.status->state < 12 ||
315 control_loops::superstructure_queue.status->state == 16) {
316 LOG(ERROR, "Superstructure no longer running, aborting action\n");
317 return true;
318 }
319
320 if (SuperstructureProfileDone()) {
321 LOG(DEBUG, "Profile done.\n");
322 if (::std::abs(control_loops::superstructure_queue.status->intake.angle -
323 superstructure_goal_.intake) < (kEpsilon + 0.1) &&
324 ::std::abs(control_loops::superstructure_queue.status->shoulder.angle -
325 superstructure_goal_.shoulder) < (kEpsilon + 0.05) &&
326 ::std::abs(control_loops::superstructure_queue.status->wrist.angle -
327 superstructure_goal_.wrist) < (kEpsilon + 0.01) &&
328 ::std::abs(control_loops::superstructure_queue.status->intake
329 .angular_velocity) < (kEpsilon + 0.1) &&
330 ::std::abs(control_loops::superstructure_queue.status->shoulder
331 .angular_velocity) < (kEpsilon + 0.10) &&
332 ::std::abs(control_loops::superstructure_queue.status->wrist
333 .angular_velocity) < (kEpsilon + 0.05)) {
334 LOG(INFO, "Near goal, done.\n");
335 return true;
336 }
337 }
338 return false;
339}
340
341void AutonomousActor::WaitForIntake() {
342 while (true) {
343 if (ShouldCancel()) return;
344 if (IntakeDone()) return;
345 }
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700346}
347
Comran Morshedb134e772016-03-16 21:05:05 +0000348void AutonomousActor::WaitForSuperstructure() {
349 while (true) {
350 if (ShouldCancel()) return;
Austin Schuh23b21802016-04-03 21:18:56 -0700351 if (SuperstructureDone()) return;
352 }
353}
Comran Morshedb134e772016-03-16 21:05:05 +0000354
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700355void AutonomousActor::WaitForSuperstructureProfile() {
356 while (true) {
357 if (ShouldCancel()) return;
358 control_loops::superstructure_queue.status.FetchAnother();
359
360 if (control_loops::superstructure_queue.status->state < 12 ||
361 control_loops::superstructure_queue.status->state == 16) {
362 LOG(ERROR, "Superstructure no longer running, aborting action\n");
363 return;
364 }
365
366 if (SuperstructureProfileDone()) return;
367 }
368}
369
Austin Schuh23b21802016-04-03 21:18:56 -0700370void AutonomousActor::WaitForSuperstructureLow() {
371 while (true) {
372 if (ShouldCancel()) return;
373 control_loops::superstructure_queue.status.FetchAnother();
Comran Morshedb134e772016-03-16 21:05:05 +0000374
375 if (control_loops::superstructure_queue.status->state < 12 ||
376 control_loops::superstructure_queue.status->state == 16) {
377 LOG(ERROR, "Superstructure no longer running, aborting action\n");
378 return;
379 }
Austin Schuh23b21802016-04-03 21:18:56 -0700380 if (SuperstructureProfileDone()) return;
381 if (control_loops::superstructure_queue.status->shoulder.angle < 0.1) {
382 return;
Comran Morshedb134e772016-03-16 21:05:05 +0000383 }
384 }
385}
Austin Schuh23b21802016-04-03 21:18:56 -0700386void AutonomousActor::BackLongShotLowBarTwoBall() {
387 LOG(INFO, "Expanding for back long shot\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700388 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 -0700389 {10.0, 25.0}, false, 0.0);
390}
391
392void AutonomousActor::BackLongShotTwoBall() {
393 LOG(INFO, "Expanding for back long shot\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700394 MoveSuperstructure(0.00, M_PI / 2.0 - 0.2, -0.55, {7.0, 40.0}, {4.0, 6.0},
395 {10.0, 25.0}, false, 0.0);
396}
397
398void AutonomousActor::BackLongShotTwoBallFinish() {
399 LOG(INFO, "Expanding for back long shot\n");
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000400 MoveSuperstructure(0.00, M_PI / 2.0 - 0.2, -0.625 + 0.03, {7.0, 40.0},
401 {4.0, 6.0}, {10.0, 25.0}, false, 0.0);
Austin Schuh23b21802016-04-03 21:18:56 -0700402}
403
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700404void AutonomousActor::BackLongShot() {
405 LOG(INFO, "Expanding for back long shot\n");
Austin Schuh23b21802016-04-03 21:18:56 -0700406 MoveSuperstructure(0.80, M_PI / 2.0 - 0.2, -0.62, {7.0, 40.0}, {4.0, 6.0},
407 {10.0, 25.0}, false, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700408}
409
410void AutonomousActor::BackMiddleShot() {
411 LOG(INFO, "Expanding for back middle shot\n");
412 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 -0700413 {10.0, 25.0}, false, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700414}
415
Austin Schuh3e4a5272016-04-20 20:11:00 -0700416void AutonomousActor::FrontLongShot() {
417 LOG(INFO, "Expanding for front long shot\n");
418 MoveSuperstructure(0.80, M_PI / 2.0 + 0.1, M_PI + 0.41 + 0.02, {7.0, 40.0},
419 {4.0, 6.0}, {10.0, 25.0}, false, 0.0);
420}
421
422void AutonomousActor::FrontMiddleShot() {
423 LOG(INFO, "Expanding for front middle shot\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700424 MoveSuperstructure(-0.05, M_PI / 2.0 + 0.1, M_PI + 0.44, {7.0, 40.0},
Austin Schuh3e4a5272016-04-20 20:11:00 -0700425 {4.0, 10.0}, {10.0, 25.0}, true, 0.0);
426}
427
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700428void AutonomousActor::TuckArm(bool low_bar, bool traverse_down) {
429 MoveSuperstructure(low_bar ? -0.05 : 2.0, -0.010, 0.0, {7.0, 40.0},
Austin Schuh23b21802016-04-03 21:18:56 -0700430 {4.0, 10.0}, {10.0, 25.0}, !traverse_down, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700431}
432
Austin Schuh3e4a5272016-04-20 20:11:00 -0700433void AutonomousActor::DoFullShot() {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700434 if (ShouldCancel()) return;
435 // Make sure that the base is aligned with the base.
436 LOG(INFO, "Waiting for the superstructure\n");
437 WaitForSuperstructure();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700438
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800439 this_thread::sleep_for(chrono::milliseconds(500));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700440
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700441 if (ShouldCancel()) return;
442 LOG(INFO, "Triggering the vision actor\n");
443 AlignWithVisionGoal();
444
445 // Wait for the drive base to be aligned with the target and make sure that
446 // the shooter is up to speed.
447 LOG(INFO, "Waiting for vision to be aligned\n");
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800448 WaitForAlignedWithVision(chrono::milliseconds(2000));
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700449 if (ShouldCancel()) return;
450 LOG(INFO, "Waiting for shooter to be up to speed\n");
451 WaitForShooterSpeed();
452 if (ShouldCancel()) return;
453
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800454 this_thread::sleep_for(chrono::milliseconds(300));
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700455 LOG(INFO, "Shoot!\n");
456 Shoot();
457
458 // Turn off the shooter and fold up the superstructure.
459 if (ShouldCancel()) return;
460 LOG(INFO, "Stopping shooter\n");
461 SetShooterSpeed(0.0);
462 LOG(INFO, "Folding superstructure back down\n");
463 TuckArm(false, false);
464
465 // Wait for everything to be folded up.
466 LOG(INFO, "Waiting for superstructure to be folded back down\n");
Austin Schuh3e4a5272016-04-20 20:11:00 -0700467 WaitForSuperstructureLow();
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700468}
469
470void AutonomousActor::LowBarDrive() {
471 TuckArm(false, true);
472 StartDrive(-5.5, 0.0, kLowBarDrive, kSlowTurn);
473
474 if (!WaitForDriveNear(5.3, 0.0)) return;
475 TuckArm(true, true);
476
477 if (!WaitForDriveNear(5.0, 0.0)) return;
478
479 StartDrive(0.0, 0.0, kLowBarDrive, kSlowTurn);
480
481 if (!WaitForDriveNear(3.0, 0.0)) return;
482
483 StartDrive(0.0, 0.0, kLowBarDrive, kSlowTurn);
484
485 if (!WaitForDriveNear(1.0, 0.0)) return;
486
Austin Schuh15b5f6a2016-03-26 19:43:56 -0700487 StartDrive(0, -M_PI / 4.0 - 0.2, kLowBarDrive, kSlowTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700488}
489
Austin Schuh3e4a5272016-04-20 20:11:00 -0700490void AutonomousActor::TippyDrive(double goal_distance, double tip_distance,
491 double below, double above) {
492 StartDrive(goal_distance, 0.0, kMoatDrive, kSlowTurn);
493 if (!WaitForBelowAngle(below)) return;
494 if (!WaitForAboveAngle(above)) return;
495 // Ok, we are good now. Compensate by moving the goal by the error.
496 // Should be here at 2.7
497 drivetrain_queue.status.FetchLatest();
498 if (drivetrain_queue.status.get()) {
499 const double left_error =
500 (initial_drivetrain_.left -
501 drivetrain_queue.status->estimated_left_position);
502 const double right_error =
503 (initial_drivetrain_.right -
504 drivetrain_queue.status->estimated_right_position);
505 const double distance_to_go = (left_error + right_error) / 2.0;
506 const double distance_compensation =
507 goal_distance - tip_distance - distance_to_go;
508 LOG(INFO, "Going %f further at the bump\n", distance_compensation);
509 StartDrive(distance_compensation, 0.0, kMoatDrive, kSlowTurn);
510 }
511}
512
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700513void AutonomousActor::MiddleDrive() {
514 TuckArm(false, false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700515 TippyDrive(3.65, 2.7, -0.2, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700516 if (!WaitForDriveDone()) return;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700517}
518
519void AutonomousActor::OneFromMiddleDrive(bool left) {
Austin Schuh3e4a5272016-04-20 20:11:00 -0700520 const double kTurnAngle = left ? -0.41 : 0.41;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700521 TuckArm(false, false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700522 TippyDrive(4.05, 2.7, -0.2, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700523
524 if (!WaitForDriveDone()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700525 StartDrive(0.0, kTurnAngle, kRealignDrive, kFastTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700526}
527
528void AutonomousActor::TwoFromMiddleDrive() {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700529 TuckArm(false, false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700530 constexpr double kDriveDistance = 5.10;
531 TippyDrive(kDriveDistance, 2.7, -0.2, 0.0);
532
533 if (!WaitForDriveNear(kDriveDistance - 3.0, 2.0)) return;
534 StartDrive(0, -M_PI / 2 - 0.10, kMoatDrive, kFastTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700535
536 if (!WaitForDriveDone()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700537 StartDrive(0, M_PI / 3 + 0.35, kMoatDrive, kFastTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700538}
Comran Morshedb134e772016-03-16 21:05:05 +0000539
Austin Schuh23b21802016-04-03 21:18:56 -0700540void AutonomousActor::CloseIfBall() {
541 ::y2016::sensors::ball_detector.FetchLatest();
542 if (::y2016::sensors::ball_detector.get()) {
543 const bool ball_detected = ::y2016::sensors::ball_detector->voltage > 2.5;
544 if (ball_detected) {
545 CloseShooter();
546 }
547 }
548}
549
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700550void AutonomousActor::WaitForBallOrDriveDone() {
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700551 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
552 ::std::chrono::milliseconds(5) / 2);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700553 while (true) {
554 if (ShouldCancel()) {
555 return;
556 }
557 phased_loop.SleepUntilNext();
558 drivetrain_queue.status.FetchLatest();
559 if (IsDriveDone()) {
560 return;
561 }
562
563 ::y2016::sensors::ball_detector.FetchLatest();
564 if (::y2016::sensors::ball_detector.get()) {
565 const bool ball_detected = ::y2016::sensors::ball_detector->voltage > 2.5;
566 if (ball_detected) {
567 return;
568 }
569 }
570 }
571}
572
Austin Schuh23b21802016-04-03 21:18:56 -0700573void AutonomousActor::WaitForBall() {
574 while (true) {
575 ::y2016::sensors::ball_detector.FetchAnother();
576 if (::y2016::sensors::ball_detector.get()) {
577 const bool ball_detected = ::y2016::sensors::ball_detector->voltage > 2.5;
578 if (ball_detected) {
579 return;
580 }
581 if (ShouldCancel()) return;
582 }
583 }
584}
585
Austin Schuh3e4a5272016-04-20 20:11:00 -0700586void AutonomousActor::TwoBallAuto() {
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800587 monotonic_clock::time_point start_time = monotonic_clock::now();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700588 OpenShooter();
589 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
590 false, 12.0);
591 if (ShouldCancel()) return;
592 LOG(INFO, "Waiting for the intake to come down.\n");
593
594 WaitForIntake();
595 LOG(INFO, "Intake done at %f seconds, starting to drive\n",
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800596 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700597 if (ShouldCancel()) return;
598 const double kDriveDistance = 5.05;
599 StartDrive(-kDriveDistance, 0.0, kTwoBallLowDrive, kSlowTurn);
600
601 StartDrive(0.0, 0.4, kTwoBallLowDrive, kSwerveTurn);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700602 if (!WaitForDriveNear(kDriveDistance - 0.5, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700603
Austin Schuh295c2d92016-05-01 12:28:04 -0700604 // Check if the ball is there.
605 bool first_ball_there = true;
606 ::y2016::sensors::ball_detector.FetchLatest();
607 if (::y2016::sensors::ball_detector.get()) {
608 const bool ball_detected = ::y2016::sensors::ball_detector->voltage > 2.5;
609 first_ball_there = ball_detected;
610 LOG(INFO, "Saw the ball: %d at %f\n", first_ball_there,
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800611 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh295c2d92016-05-01 12:28:04 -0700612 }
Austin Schuh3e4a5272016-04-20 20:11:00 -0700613 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 40.0}, {4.0, 10.0}, {10.0, 25.0},
614 false, 0.0);
615 LOG(INFO, "Shutting off rollers at %f seconds, starting to straighten out\n",
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800616 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700617 StartDrive(0.0, -0.4, kTwoBallLowDrive, kSwerveTurn);
618 MoveSuperstructure(-0.05, -0.010, 0.0, {8.0, 40.0}, {4.0, 10.0}, {10.0, 25.0},
619 false, 0.0);
620 CloseShooter();
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700621 if (!WaitForDriveNear(kDriveDistance - 2.4, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700622
623 // We are now under the low bar. Start lifting.
624 BackLongShotLowBarTwoBall();
625 LOG(INFO, "Spinning up the shooter wheels\n");
626 SetShooterSpeed(640.0);
627 StartDrive(0.0, 0.0, kTwoBallFastDrive, kSwerveTurn);
628
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700629 if (!WaitForDriveNear(1.50, kDoNotTurnCare)) return;
630 constexpr double kShootTurnAngle = -M_PI / 4.0 - 0.05;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700631 StartDrive(0, kShootTurnAngle, kTwoBallFastDrive, kFinishTurn);
632 BackLongShotTwoBall();
633
634 if (!WaitForDriveDone()) return;
635 LOG(INFO, "First shot done driving at %f seconds\n",
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800636 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700637
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700638 WaitForSuperstructureProfile();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700639
640 if (ShouldCancel()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700641 AlignWithVisionGoal();
642
643 WaitForShooterSpeed();
644 if (ShouldCancel()) return;
645
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800646 constexpr chrono::milliseconds kVisionExtra{0};
647 WaitForAlignedWithVision(chrono::milliseconds(500) + kVisionExtra);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700648 BackLongShotTwoBallFinish();
649 WaitForSuperstructureProfile();
650 if (ShouldCancel()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700651 LOG(INFO, "Shoot!\n");
Austin Schuh295c2d92016-05-01 12:28:04 -0700652 if (first_ball_there) {
653 Shoot();
654 } else {
655 LOG(INFO, "Nah, not shooting\n");
656 }
Austin Schuh3e4a5272016-04-20 20:11:00 -0700657
658 LOG(INFO, "First shot at %f seconds\n",
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800659 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700660 if (ShouldCancel()) return;
661
662 SetShooterSpeed(0.0);
663 LOG(INFO, "Folding superstructure back down\n");
664 TuckArm(true, true);
665
666 // Undo vision move.
667 StartDrive(0.0, 0.0, kTwoBallFastDrive, kFinishTurn);
668 if (!WaitForDriveDone()) return;
669
670 constexpr double kBackDrive = 3.09 - 0.4;
671 StartDrive(kBackDrive, 0.0, kTwoBallReturnDrive, kSlowTurn);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700672 if (!WaitForDriveNear(kBackDrive - 0.19, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700673 StartDrive(0, -kShootTurnAngle, kTwoBallReturnDrive, kSwerveTurn);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700674 if (!WaitForDriveNear(1.0, kDoNotTurnCare)) return;
675 StartDrive(0, 0, kTwoBallReturnSlow, kSwerveTurn);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700676
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700677 if (!WaitForDriveNear(0.06, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700678 LOG(INFO, "At Low Bar %f\n",
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800679 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700680
681 OpenShooter();
682 constexpr double kSecondBallAfterBarDrive = 2.10;
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700683 StartDrive(kSecondBallAfterBarDrive, 0.0, kTwoBallBallPickupAccel, kSlowTurn);
684 if (!WaitForDriveNear(kSecondBallAfterBarDrive - 0.5, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700685 constexpr double kBallSmallWallTurn = -0.11;
686 StartDrive(0, kBallSmallWallTurn, kTwoBallBallPickup, kFinishTurn);
687
688 MoveSuperstructure(0.03, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
689 false, 12.0);
690
691 if (!WaitForDriveProfileDone()) return;
692
693 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
694 false, 12.0);
695
696 LOG(INFO, "Done backing up %f\n",
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800697 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700698
699 constexpr double kDriveBackDistance = 5.15 - 0.4;
700 StartDrive(-kDriveBackDistance, 0.0, kTwoBallLowDrive, kFinishTurn);
701 CloseIfBall();
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700702 if (!WaitForDriveNear(kDriveBackDistance - 0.75, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700703
704 StartDrive(0.0, -kBallSmallWallTurn, kTwoBallLowDrive, kFinishTurn);
705 LOG(INFO, "Straightening up at %f\n",
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800706 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700707
708 CloseIfBall();
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700709 if (!WaitForDriveNear(kDriveBackDistance - 2.3, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700710
711 ::y2016::sensors::ball_detector.FetchLatest();
712 if (::y2016::sensors::ball_detector.get()) {
713 const bool ball_detected = ::y2016::sensors::ball_detector->voltage > 2.5;
714 if (!ball_detected) {
715 if (!WaitForDriveDone()) return;
716 LOG(INFO, "Aborting, no ball %f\n",
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800717 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700718 return;
719 }
720 }
721 CloseShooter();
722
723 BackLongShotLowBarTwoBall();
724 LOG(INFO, "Spinning up the shooter wheels\n");
725 SetShooterSpeed(640.0);
726 StartDrive(0.0, 0.0, kTwoBallFastDrive, kSwerveTurn);
727
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700728 if (!WaitForDriveNear(1.80, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700729 StartDrive(0, kShootTurnAngle, kTwoBallFastDrive, kFinishTurn);
730 BackLongShotTwoBall();
731
732 if (!WaitForDriveDone()) return;
733 LOG(INFO, "Second shot done driving at %f seconds\n",
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800734 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700735 WaitForSuperstructure();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700736 AlignWithVisionGoal();
737 if (ShouldCancel()) return;
738
739 WaitForShooterSpeed();
740 if (ShouldCancel()) return;
741
742 // 2.2 with 0.4 of vision.
743 // 1.8 without any vision.
744 LOG(INFO, "Going to vision align at %f\n",
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800745 DoubleSeconds(monotonic_clock::now() - start_time));
746 WaitForAlignedWithVision(
747 (start_time + chrono::milliseconds(13500) + kVisionExtra * 2) -
748 monotonic_clock::now());
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700749 BackLongShotTwoBallFinish();
750 WaitForSuperstructureProfile();
751 if (ShouldCancel()) return;
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800752 LOG(INFO, "Shoot at %f\n",
753 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700754 Shoot();
755
756 LOG(INFO, "Second shot at %f seconds\n",
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800757 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700758 if (ShouldCancel()) return;
759
760 SetShooterSpeed(0.0);
761 LOG(INFO, "Folding superstructure back down\n");
762 TuckArm(true, false);
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800763 LOG(INFO, "Shot %f\n", DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700764
765 WaitForSuperstructureLow();
766
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800767 LOG(INFO, "Done %f\n", DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700768}
769
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700770void AutonomousActor::StealAndMoveOverBy(double distance) {
771 OpenShooter();
772 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
773 true, 12.0);
774 if (ShouldCancel()) return;
775 LOG(INFO, "Waiting for the intake to come down.\n");
776
777 WaitForIntake();
778 if (ShouldCancel()) return;
779 StartDrive(-distance, M_PI / 2.0, kFastDrive, kStealTurn);
780 WaitForBallOrDriveDone();
781 if (ShouldCancel()) return;
782 MoveSuperstructure(1.0, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
783 true, 12.0);
784
785 if (!WaitForDriveDone()) return;
786 StartDrive(0.0, M_PI / 2.0, kFastDrive, kStealTurn);
787 if (!WaitForDriveDone()) return;
788}
789
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000790bool AutonomousActor::RunAction(
791 const ::frc971::autonomous::AutonomousActionParams &params) {
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800792 monotonic_clock::time_point start_time = monotonic_clock::now();
Comran Morshede68e3732016-03-12 14:12:11 +0000793 LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode);
794
Comran Morshed435f1112016-03-12 14:20:45 +0000795 InitializeEncoders();
796 ResetDrivetrain();
797
Austin Schuh295c2d92016-05-01 12:28:04 -0700798 switch (params.mode) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700799 case 0:
800 LowBarDrive();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700801 if (!WaitForDriveDone()) return true;
802 // Get the superstructure to unfold and get ready for shooting.
803 LOG(INFO, "Unfolding superstructure\n");
804 FrontLongShot();
805
806 // Spin up the shooter wheels.
807 LOG(INFO, "Spinning up the shooter wheels\n");
808 SetShooterSpeed(640.0);
809
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700810 break;
811 case 1:
812 TwoFromMiddleDrive();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700813 if (!WaitForDriveDone()) return true;
814 // Get the superstructure to unfold and get ready for shooting.
815 LOG(INFO, "Unfolding superstructure\n");
816 FrontMiddleShot();
817
818 // Spin up the shooter wheels.
819 LOG(INFO, "Spinning up the shooter wheels\n");
820 SetShooterSpeed(600.0);
821
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700822 break;
823 case 2:
824 OneFromMiddleDrive(true);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700825 if (!WaitForDriveDone()) return true;
826 // Get the superstructure to unfold and get ready for shooting.
827 LOG(INFO, "Unfolding superstructure\n");
828 FrontMiddleShot();
829
830 // Spin up the shooter wheels.
831 LOG(INFO, "Spinning up the shooter wheels\n");
832 SetShooterSpeed(600.0);
833
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700834 break;
835 case 3:
836 MiddleDrive();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700837 if (!WaitForDriveDone()) return true;
838 // Get the superstructure to unfold and get ready for shooting.
839 LOG(INFO, "Unfolding superstructure\n");
840 FrontMiddleShot();
841
842 // Spin up the shooter wheels.
843 LOG(INFO, "Spinning up the shooter wheels\n");
844 SetShooterSpeed(600.0);
845
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700846 break;
847 case 4:
848 OneFromMiddleDrive(false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700849 if (!WaitForDriveDone()) return true;
850 // Get the superstructure to unfold and get ready for shooting.
851 LOG(INFO, "Unfolding superstructure\n");
852 FrontMiddleShot();
853
854 // Spin up the shooter wheels.
855 LOG(INFO, "Spinning up the shooter wheels\n");
856 SetShooterSpeed(600.0);
857
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700858 break;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700859 case 5:
Campbell Crowley9ed61a52016-11-05 17:13:07 -0700860 case 15:
Austin Schuh3e4a5272016-04-20 20:11:00 -0700861 TwoBallAuto();
Austin Schuh23b21802016-04-03 21:18:56 -0700862 return true;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700863 break;
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700864 case 6:
865 StealAndMoveOverBy(3.10 + 2 * 52 * 2.54 / 100.0);
866 if (ShouldCancel()) return true;
867
868 TwoFromMiddleDrive();
869 if (!WaitForDriveDone()) return true;
870 // Get the superstructure to unfold and get ready for shooting.
871 LOG(INFO, "Unfolding superstructure\n");
872 FrontMiddleShot();
873
874 // Spin up the shooter wheels.
875 LOG(INFO, "Spinning up the shooter wheels\n");
876 SetShooterSpeed(600.0);
877
878 break;
879 case 7:
880 StealAndMoveOverBy(2.95 + 52 * 2.54 / 100.0);
881 if (ShouldCancel()) return true;
882
883 OneFromMiddleDrive(true);
884 if (!WaitForDriveDone()) return true;
885 // Get the superstructure to unfold and get ready for shooting.
886 LOG(INFO, "Unfolding superstructure\n");
887 FrontMiddleShot();
888
889 // Spin up the shooter wheels.
890 LOG(INFO, "Spinning up the shooter wheels\n");
891 SetShooterSpeed(600.0);
892
893 break;
894 case 8: {
895 StealAndMoveOverBy(2.95);
896 if (ShouldCancel()) return true;
897
898 MiddleDrive();
899 if (!WaitForDriveDone()) return true;
900 // Get the superstructure to unfold and get ready for shooting.
901 LOG(INFO, "Unfolding superstructure\n");
902 FrontMiddleShot();
903
904 // Spin up the shooter wheels.
905 LOG(INFO, "Spinning up the shooter wheels\n");
906 SetShooterSpeed(600.0);
907
908 } break;
909 case 9: {
910 StealAndMoveOverBy(1.70);
911 if (ShouldCancel()) return true;
912
913 OneFromMiddleDrive(false);
914 if (!WaitForDriveDone()) return true;
915 // Get the superstructure to unfold and get ready for shooting.
916 LOG(INFO, "Unfolding superstructure\n");
917 FrontMiddleShot();
918
919 // Spin up the shooter wheels.
920 LOG(INFO, "Spinning up the shooter wheels\n");
921 SetShooterSpeed(600.0);
922
923 } break;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700924 default:
Austin Schuh6c9bc622016-03-26 19:44:12 -0700925 LOG(ERROR, "Invalid auto mode %d\n", params.mode);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700926 return true;
927 }
Comran Morshed435f1112016-03-12 14:20:45 +0000928
Austin Schuh3e4a5272016-04-20 20:11:00 -0700929 DoFullShot();
930
931 StartDrive(0.5, 0.0, kMoatDrive, kFastTurn);
Comran Morshed435f1112016-03-12 14:20:45 +0000932 if (!WaitForDriveDone()) return true;
933
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800934 LOG(INFO, "Done %f\n", DoubleSeconds(monotonic_clock::now() - start_time));
Comran Morshed435f1112016-03-12 14:20:45 +0000935
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700936 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
937 ::std::chrono::milliseconds(5) / 2);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700938
Comran Morshed435f1112016-03-12 14:20:45 +0000939 while (!ShouldCancel()) {
940 phased_loop.SleepUntilNext();
Comran Morshede68e3732016-03-12 14:12:11 +0000941 }
Comran Morshed435f1112016-03-12 14:20:45 +0000942 LOG(DEBUG, "Done running\n");
Comran Morshede68e3732016-03-12 14:12:11 +0000943
944 return true;
945}
946
Comran Morshede68e3732016-03-12 14:12:11 +0000947} // namespace actors
948} // namespace y2016