blob: 5f95072792a3488e54a60c6e5eed0fba00ceaad6 [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(
Austin Schuheb99d072019-05-12 21:03:38 -070053 ::aos::EventLoop *event_loop,
Philipp Schrader4bd29b12017-02-22 04:42:27 +000054 ::frc971::autonomous::AutonomousActionQueueGroup *s)
55 : frc971::autonomous::BaseAutonomousActor(
Austin Schuheb99d072019-05-12 21:03:38 -070056 event_loop, s, control_loops::drivetrain::GetDrivetrainConfig()) {}
Comran Morshed435f1112016-03-12 14:20:45 +000057
Austin Schuhe4ec49c2016-04-24 19:07:15 -070058constexpr double kDoNotTurnCare = 2.0;
59
Comran Morshedb134e772016-03-16 21:05:05 +000060void AutonomousActor::MoveSuperstructure(
61 double intake, double shoulder, double wrist,
62 const ProfileParameters intake_params,
63 const ProfileParameters shoulder_params,
Austin Schuh23b21802016-04-03 21:18:56 -070064 const ProfileParameters wrist_params, bool traverse_up,
65 double roller_power) {
Comran Morshedb134e772016-03-16 21:05:05 +000066 superstructure_goal_ = {intake, shoulder, wrist};
67
68 auto new_superstructure_goal =
69 ::y2016::control_loops::superstructure_queue.goal.MakeMessage();
70
71 new_superstructure_goal->angle_intake = intake;
72 new_superstructure_goal->angle_shoulder = shoulder;
73 new_superstructure_goal->angle_wrist = wrist;
74
75 new_superstructure_goal->max_angular_velocity_intake =
76 intake_params.max_velocity;
77 new_superstructure_goal->max_angular_velocity_shoulder =
78 shoulder_params.max_velocity;
79 new_superstructure_goal->max_angular_velocity_wrist =
80 wrist_params.max_velocity;
81
82 new_superstructure_goal->max_angular_acceleration_intake =
83 intake_params.max_acceleration;
84 new_superstructure_goal->max_angular_acceleration_shoulder =
85 shoulder_params.max_acceleration;
86 new_superstructure_goal->max_angular_acceleration_wrist =
87 wrist_params.max_acceleration;
88
Austin Schuh23b21802016-04-03 21:18:56 -070089 new_superstructure_goal->voltage_top_rollers = roller_power;
90 new_superstructure_goal->voltage_bottom_rollers = roller_power;
Austin Schuhf59b8ee2016-03-19 21:31:36 -070091
92 new_superstructure_goal->traverse_unlatched = true;
93 new_superstructure_goal->traverse_down = !traverse_up;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070094 new_superstructure_goal->voltage_climber = 0.0;
95 new_superstructure_goal->unfold_climber = false;
Comran Morshedb134e772016-03-16 21:05:05 +000096
97 if (!new_superstructure_goal.Send()) {
98 LOG(ERROR, "Sending superstructure goal failed.\n");
99 }
100}
101
Austin Schuh23b21802016-04-03 21:18:56 -0700102void AutonomousActor::OpenShooter() {
103 shooter_speed_ = 0.0;
104
105 if (!control_loops::shooter::shooter_queue.goal.MakeWithBuilder()
106 .angular_velocity(shooter_speed_)
107 .clamp_open(true)
108 .push_to_shooter(false)
109 .force_lights_on(false)
110 .Send()) {
111 LOG(ERROR, "Sending shooter goal failed.\n");
112 }
113}
114
115void AutonomousActor::CloseShooter() {
116 shooter_speed_ = 0.0;
117
118 if (!control_loops::shooter::shooter_queue.goal.MakeWithBuilder()
119 .angular_velocity(shooter_speed_)
120 .clamp_open(false)
121 .push_to_shooter(false)
122 .force_lights_on(false)
123 .Send()) {
124 LOG(ERROR, "Sending shooter goal failed.\n");
125 }
126}
127
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700128void AutonomousActor::SetShooterSpeed(double speed) {
129 shooter_speed_ = speed;
130
131 // In auto, we want to have the lights on whenever possible since we have no
132 // hope of a human aligning the robot.
133 bool force_lights_on = shooter_speed_ > 1.0;
134
135 if (!control_loops::shooter::shooter_queue.goal.MakeWithBuilder()
136 .angular_velocity(shooter_speed_)
137 .clamp_open(false)
138 .push_to_shooter(false)
139 .force_lights_on(force_lights_on)
140 .Send()) {
141 LOG(ERROR, "Sending shooter goal failed.\n");
142 }
143}
144
145void AutonomousActor::Shoot() {
146 uint32_t initial_shots = 0;
147
148 control_loops::shooter::shooter_queue.status.FetchLatest();
149 if (control_loops::shooter::shooter_queue.status.get()) {
150 initial_shots = control_loops::shooter::shooter_queue.status->shots;
151 }
152
153 // In auto, we want to have the lights on whenever possible since we have no
154 // hope of a human aligning the robot.
155 bool force_lights_on = shooter_speed_ > 1.0;
156
157 if (!control_loops::shooter::shooter_queue.goal.MakeWithBuilder()
158 .angular_velocity(shooter_speed_)
159 .clamp_open(false)
160 .push_to_shooter(true)
161 .force_lights_on(force_lights_on)
162 .Send()) {
163 LOG(ERROR, "Sending shooter goal failed.\n");
164 }
165
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700166 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
167 ::std::chrono::milliseconds(5) / 2);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700168 while (true) {
169 if (ShouldCancel()) return;
170
171 // Wait for the shot count to change so we know when the shot is complete.
172 control_loops::shooter::shooter_queue.status.FetchLatest();
173 if (control_loops::shooter::shooter_queue.status.get()) {
174 if (initial_shots < control_loops::shooter::shooter_queue.status->shots) {
175 return;
176 }
177 }
178 phased_loop.SleepUntilNext();
179 }
180}
181
182void AutonomousActor::WaitForShooterSpeed() {
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700183 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
184 ::std::chrono::milliseconds(5) / 2);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700185 while (true) {
186 if (ShouldCancel()) return;
187
188 control_loops::shooter::shooter_queue.status.FetchLatest();
189 if (control_loops::shooter::shooter_queue.status.get()) {
190 if (control_loops::shooter::shooter_queue.status->left.ready &&
191 control_loops::shooter::shooter_queue.status->right.ready) {
192 return;
193 }
194 }
195 phased_loop.SleepUntilNext();
196 }
197}
198
199void AutonomousActor::AlignWithVisionGoal() {
200 actors::VisionAlignActionParams params;
sabinaf5584322017-09-23 18:37:19 -0700201 vision_action_ = actors::MakeVisionAlignAction(params);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700202 vision_action_->Start();
203}
204
Austin Schuh23b21802016-04-03 21:18:56 -0700205void AutonomousActor::WaitForAlignedWithVision(
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800206 chrono::nanoseconds align_duration) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700207 bool vision_valid = false;
208 double last_angle = 0.0;
209 int ready_to_fire = 0;
210
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700211 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
212 ::std::chrono::milliseconds(5) / 2);
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800213 monotonic_clock::time_point end_time =
214 monotonic_clock::now() + align_duration;
215 while (end_time > monotonic_clock::now()) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700216 if (ShouldCancel()) break;
217
218 ::y2016::vision::vision_status.FetchLatest();
219 if (::y2016::vision::vision_status.get()) {
220 vision_valid = (::y2016::vision::vision_status->left_image_valid &&
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000221 ::y2016::vision::vision_status->right_image_valid);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700222 last_angle = ::y2016::vision::vision_status->horizontal_angle;
223 }
224
225 drivetrain_queue.status.FetchLatest();
226 drivetrain_queue.goal.FetchLatest();
227
228 if (drivetrain_queue.status.get() && drivetrain_queue.goal.get()) {
229 const double left_goal = drivetrain_queue.goal->left_goal;
230 const double right_goal = drivetrain_queue.goal->right_goal;
231 const double left_current =
232 drivetrain_queue.status->estimated_left_position;
233 const double right_current =
234 drivetrain_queue.status->estimated_right_position;
235 const double left_velocity =
236 drivetrain_queue.status->estimated_left_velocity;
237 const double right_velocity =
238 drivetrain_queue.status->estimated_right_velocity;
239
240 if (vision_valid && ::std::abs(last_angle) < 0.02 &&
241 ::std::abs((left_goal - right_goal) -
242 (left_current - right_current)) /
243 dt_config_.robot_radius / 2.0 <
244 0.02 &&
245 ::std::abs(left_velocity - right_velocity) < 0.01) {
246 ++ready_to_fire;
247 } else {
248 ready_to_fire = 0;
249 }
Austin Schuh3e4a5272016-04-20 20:11:00 -0700250 if (ready_to_fire > 15) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700251 break;
Austin Schuh23b21802016-04-03 21:18:56 -0700252 LOG(INFO, "Vision align success!\n");
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700253 }
254 }
255 phased_loop.SleepUntilNext();
256 }
257
258 vision_action_->Cancel();
259 WaitUntilDoneOrCanceled(::std::move(vision_action_));
Austin Schuh23b21802016-04-03 21:18:56 -0700260 LOG(INFO, "Done waiting for vision\n");
261}
262
263bool AutonomousActor::IntakeDone() {
264 control_loops::superstructure_queue.status.FetchAnother();
265
266 constexpr double kProfileError = 1e-5;
267 constexpr double kEpsilon = 0.15;
268
269 if (control_loops::superstructure_queue.status->state < 12 ||
270 control_loops::superstructure_queue.status->state == 16) {
271 LOG(ERROR, "Superstructure no longer running, aborting action\n");
272 return true;
273 }
274
275 if (::std::abs(control_loops::superstructure_queue.status->intake.goal_angle -
276 superstructure_goal_.intake) < kProfileError &&
277 ::std::abs(control_loops::superstructure_queue.status->intake
278 .goal_angular_velocity) < kProfileError) {
279 LOG(DEBUG, "Profile done.\n");
280 if (::std::abs(control_loops::superstructure_queue.status->intake.angle -
281 superstructure_goal_.intake) < kEpsilon &&
282 ::std::abs(control_loops::superstructure_queue.status->intake
283 .angular_velocity) < kEpsilon) {
284 LOG(INFO, "Near goal, done.\n");
285 return true;
286 }
287 }
288 return false;
289}
290
291bool AutonomousActor::SuperstructureProfileDone() {
292 constexpr double kProfileError = 1e-5;
293 return ::std::abs(
294 control_loops::superstructure_queue.status->intake.goal_angle -
295 superstructure_goal_.intake) < kProfileError &&
296 ::std::abs(
297 control_loops::superstructure_queue.status->shoulder.goal_angle -
298 superstructure_goal_.shoulder) < kProfileError &&
299 ::std::abs(
300 control_loops::superstructure_queue.status->wrist.goal_angle -
301 superstructure_goal_.wrist) < kProfileError &&
302 ::std::abs(control_loops::superstructure_queue.status->intake
303 .goal_angular_velocity) < kProfileError &&
304 ::std::abs(control_loops::superstructure_queue.status->shoulder
305 .goal_angular_velocity) < kProfileError &&
306 ::std::abs(control_loops::superstructure_queue.status->wrist
307 .goal_angular_velocity) < kProfileError;
308}
309
310bool AutonomousActor::SuperstructureDone() {
311 control_loops::superstructure_queue.status.FetchAnother();
312
313 constexpr double kEpsilon = 0.03;
314
315 if (control_loops::superstructure_queue.status->state < 12 ||
316 control_loops::superstructure_queue.status->state == 16) {
317 LOG(ERROR, "Superstructure no longer running, aborting action\n");
318 return true;
319 }
320
321 if (SuperstructureProfileDone()) {
322 LOG(DEBUG, "Profile done.\n");
323 if (::std::abs(control_loops::superstructure_queue.status->intake.angle -
324 superstructure_goal_.intake) < (kEpsilon + 0.1) &&
325 ::std::abs(control_loops::superstructure_queue.status->shoulder.angle -
326 superstructure_goal_.shoulder) < (kEpsilon + 0.05) &&
327 ::std::abs(control_loops::superstructure_queue.status->wrist.angle -
328 superstructure_goal_.wrist) < (kEpsilon + 0.01) &&
329 ::std::abs(control_loops::superstructure_queue.status->intake
330 .angular_velocity) < (kEpsilon + 0.1) &&
331 ::std::abs(control_loops::superstructure_queue.status->shoulder
332 .angular_velocity) < (kEpsilon + 0.10) &&
333 ::std::abs(control_loops::superstructure_queue.status->wrist
334 .angular_velocity) < (kEpsilon + 0.05)) {
335 LOG(INFO, "Near goal, done.\n");
336 return true;
337 }
338 }
339 return false;
340}
341
342void AutonomousActor::WaitForIntake() {
343 while (true) {
344 if (ShouldCancel()) return;
345 if (IntakeDone()) return;
346 }
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700347}
348
Comran Morshedb134e772016-03-16 21:05:05 +0000349void AutonomousActor::WaitForSuperstructure() {
350 while (true) {
351 if (ShouldCancel()) return;
Austin Schuh23b21802016-04-03 21:18:56 -0700352 if (SuperstructureDone()) return;
353 }
354}
Comran Morshedb134e772016-03-16 21:05:05 +0000355
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700356void AutonomousActor::WaitForSuperstructureProfile() {
357 while (true) {
358 if (ShouldCancel()) return;
359 control_loops::superstructure_queue.status.FetchAnother();
360
361 if (control_loops::superstructure_queue.status->state < 12 ||
362 control_loops::superstructure_queue.status->state == 16) {
363 LOG(ERROR, "Superstructure no longer running, aborting action\n");
364 return;
365 }
366
367 if (SuperstructureProfileDone()) return;
368 }
369}
370
Austin Schuh23b21802016-04-03 21:18:56 -0700371void AutonomousActor::WaitForSuperstructureLow() {
372 while (true) {
373 if (ShouldCancel()) return;
374 control_loops::superstructure_queue.status.FetchAnother();
Comran Morshedb134e772016-03-16 21:05:05 +0000375
376 if (control_loops::superstructure_queue.status->state < 12 ||
377 control_loops::superstructure_queue.status->state == 16) {
378 LOG(ERROR, "Superstructure no longer running, aborting action\n");
379 return;
380 }
Austin Schuh23b21802016-04-03 21:18:56 -0700381 if (SuperstructureProfileDone()) return;
382 if (control_loops::superstructure_queue.status->shoulder.angle < 0.1) {
383 return;
Comran Morshedb134e772016-03-16 21:05:05 +0000384 }
385 }
386}
Austin Schuh23b21802016-04-03 21:18:56 -0700387void AutonomousActor::BackLongShotLowBarTwoBall() {
388 LOG(INFO, "Expanding for back long shot\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700389 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 -0700390 {10.0, 25.0}, false, 0.0);
391}
392
393void AutonomousActor::BackLongShotTwoBall() {
394 LOG(INFO, "Expanding for back long shot\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700395 MoveSuperstructure(0.00, M_PI / 2.0 - 0.2, -0.55, {7.0, 40.0}, {4.0, 6.0},
396 {10.0, 25.0}, false, 0.0);
397}
398
399void AutonomousActor::BackLongShotTwoBallFinish() {
400 LOG(INFO, "Expanding for back long shot\n");
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000401 MoveSuperstructure(0.00, M_PI / 2.0 - 0.2, -0.625 + 0.03, {7.0, 40.0},
402 {4.0, 6.0}, {10.0, 25.0}, false, 0.0);
Austin Schuh23b21802016-04-03 21:18:56 -0700403}
404
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700405void AutonomousActor::BackLongShot() {
406 LOG(INFO, "Expanding for back long shot\n");
Austin Schuh23b21802016-04-03 21:18:56 -0700407 MoveSuperstructure(0.80, M_PI / 2.0 - 0.2, -0.62, {7.0, 40.0}, {4.0, 6.0},
408 {10.0, 25.0}, false, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700409}
410
411void AutonomousActor::BackMiddleShot() {
412 LOG(INFO, "Expanding for back middle shot\n");
413 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 -0700414 {10.0, 25.0}, false, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700415}
416
Austin Schuh3e4a5272016-04-20 20:11:00 -0700417void AutonomousActor::FrontLongShot() {
418 LOG(INFO, "Expanding for front long shot\n");
419 MoveSuperstructure(0.80, M_PI / 2.0 + 0.1, M_PI + 0.41 + 0.02, {7.0, 40.0},
420 {4.0, 6.0}, {10.0, 25.0}, false, 0.0);
421}
422
423void AutonomousActor::FrontMiddleShot() {
424 LOG(INFO, "Expanding for front middle shot\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700425 MoveSuperstructure(-0.05, M_PI / 2.0 + 0.1, M_PI + 0.44, {7.0, 40.0},
Austin Schuh3e4a5272016-04-20 20:11:00 -0700426 {4.0, 10.0}, {10.0, 25.0}, true, 0.0);
427}
428
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700429void AutonomousActor::TuckArm(bool low_bar, bool traverse_down) {
430 MoveSuperstructure(low_bar ? -0.05 : 2.0, -0.010, 0.0, {7.0, 40.0},
Austin Schuh23b21802016-04-03 21:18:56 -0700431 {4.0, 10.0}, {10.0, 25.0}, !traverse_down, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700432}
433
Austin Schuh3e4a5272016-04-20 20:11:00 -0700434void AutonomousActor::DoFullShot() {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700435 if (ShouldCancel()) return;
436 // Make sure that the base is aligned with the base.
437 LOG(INFO, "Waiting for the superstructure\n");
438 WaitForSuperstructure();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700439
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800440 this_thread::sleep_for(chrono::milliseconds(500));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700441
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700442 if (ShouldCancel()) return;
443 LOG(INFO, "Triggering the vision actor\n");
444 AlignWithVisionGoal();
445
446 // Wait for the drive base to be aligned with the target and make sure that
447 // the shooter is up to speed.
448 LOG(INFO, "Waiting for vision to be aligned\n");
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800449 WaitForAlignedWithVision(chrono::milliseconds(2000));
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700450 if (ShouldCancel()) return;
451 LOG(INFO, "Waiting for shooter to be up to speed\n");
452 WaitForShooterSpeed();
453 if (ShouldCancel()) return;
454
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800455 this_thread::sleep_for(chrono::milliseconds(300));
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700456 LOG(INFO, "Shoot!\n");
457 Shoot();
458
459 // Turn off the shooter and fold up the superstructure.
460 if (ShouldCancel()) return;
461 LOG(INFO, "Stopping shooter\n");
462 SetShooterSpeed(0.0);
463 LOG(INFO, "Folding superstructure back down\n");
464 TuckArm(false, false);
465
466 // Wait for everything to be folded up.
467 LOG(INFO, "Waiting for superstructure to be folded back down\n");
Austin Schuh3e4a5272016-04-20 20:11:00 -0700468 WaitForSuperstructureLow();
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700469}
470
471void AutonomousActor::LowBarDrive() {
472 TuckArm(false, true);
473 StartDrive(-5.5, 0.0, kLowBarDrive, kSlowTurn);
474
475 if (!WaitForDriveNear(5.3, 0.0)) return;
476 TuckArm(true, true);
477
478 if (!WaitForDriveNear(5.0, 0.0)) return;
479
480 StartDrive(0.0, 0.0, kLowBarDrive, kSlowTurn);
481
482 if (!WaitForDriveNear(3.0, 0.0)) return;
483
484 StartDrive(0.0, 0.0, kLowBarDrive, kSlowTurn);
485
486 if (!WaitForDriveNear(1.0, 0.0)) return;
487
Austin Schuh15b5f6a2016-03-26 19:43:56 -0700488 StartDrive(0, -M_PI / 4.0 - 0.2, kLowBarDrive, kSlowTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700489}
490
Austin Schuh3e4a5272016-04-20 20:11:00 -0700491void AutonomousActor::TippyDrive(double goal_distance, double tip_distance,
492 double below, double above) {
493 StartDrive(goal_distance, 0.0, kMoatDrive, kSlowTurn);
494 if (!WaitForBelowAngle(below)) return;
495 if (!WaitForAboveAngle(above)) return;
496 // Ok, we are good now. Compensate by moving the goal by the error.
497 // Should be here at 2.7
498 drivetrain_queue.status.FetchLatest();
499 if (drivetrain_queue.status.get()) {
500 const double left_error =
501 (initial_drivetrain_.left -
502 drivetrain_queue.status->estimated_left_position);
503 const double right_error =
504 (initial_drivetrain_.right -
505 drivetrain_queue.status->estimated_right_position);
506 const double distance_to_go = (left_error + right_error) / 2.0;
507 const double distance_compensation =
508 goal_distance - tip_distance - distance_to_go;
509 LOG(INFO, "Going %f further at the bump\n", distance_compensation);
510 StartDrive(distance_compensation, 0.0, kMoatDrive, kSlowTurn);
511 }
512}
513
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700514void AutonomousActor::MiddleDrive() {
515 TuckArm(false, false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700516 TippyDrive(3.65, 2.7, -0.2, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700517 if (!WaitForDriveDone()) return;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700518}
519
520void AutonomousActor::OneFromMiddleDrive(bool left) {
Austin Schuh3e4a5272016-04-20 20:11:00 -0700521 const double kTurnAngle = left ? -0.41 : 0.41;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700522 TuckArm(false, false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700523 TippyDrive(4.05, 2.7, -0.2, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700524
525 if (!WaitForDriveDone()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700526 StartDrive(0.0, kTurnAngle, kRealignDrive, kFastTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700527}
528
529void AutonomousActor::TwoFromMiddleDrive() {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700530 TuckArm(false, false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700531 constexpr double kDriveDistance = 5.10;
532 TippyDrive(kDriveDistance, 2.7, -0.2, 0.0);
533
534 if (!WaitForDriveNear(kDriveDistance - 3.0, 2.0)) return;
535 StartDrive(0, -M_PI / 2 - 0.10, kMoatDrive, kFastTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700536
537 if (!WaitForDriveDone()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700538 StartDrive(0, M_PI / 3 + 0.35, kMoatDrive, kFastTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700539}
Comran Morshedb134e772016-03-16 21:05:05 +0000540
Austin Schuh23b21802016-04-03 21:18:56 -0700541void AutonomousActor::CloseIfBall() {
542 ::y2016::sensors::ball_detector.FetchLatest();
543 if (::y2016::sensors::ball_detector.get()) {
544 const bool ball_detected = ::y2016::sensors::ball_detector->voltage > 2.5;
545 if (ball_detected) {
546 CloseShooter();
547 }
548 }
549}
550
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700551void AutonomousActor::WaitForBallOrDriveDone() {
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700552 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
553 ::std::chrono::milliseconds(5) / 2);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700554 while (true) {
555 if (ShouldCancel()) {
556 return;
557 }
558 phased_loop.SleepUntilNext();
559 drivetrain_queue.status.FetchLatest();
560 if (IsDriveDone()) {
561 return;
562 }
563
564 ::y2016::sensors::ball_detector.FetchLatest();
565 if (::y2016::sensors::ball_detector.get()) {
566 const bool ball_detected = ::y2016::sensors::ball_detector->voltage > 2.5;
567 if (ball_detected) {
568 return;
569 }
570 }
571 }
572}
573
Austin Schuh23b21802016-04-03 21:18:56 -0700574void AutonomousActor::WaitForBall() {
575 while (true) {
576 ::y2016::sensors::ball_detector.FetchAnother();
577 if (::y2016::sensors::ball_detector.get()) {
578 const bool ball_detected = ::y2016::sensors::ball_detector->voltage > 2.5;
579 if (ball_detected) {
580 return;
581 }
582 if (ShouldCancel()) return;
583 }
584 }
585}
586
Austin Schuh3e4a5272016-04-20 20:11:00 -0700587void AutonomousActor::TwoBallAuto() {
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800588 monotonic_clock::time_point start_time = monotonic_clock::now();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700589 OpenShooter();
590 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
591 false, 12.0);
592 if (ShouldCancel()) return;
593 LOG(INFO, "Waiting for the intake to come down.\n");
594
595 WaitForIntake();
596 LOG(INFO, "Intake done at %f seconds, starting to drive\n",
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800597 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700598 if (ShouldCancel()) return;
599 const double kDriveDistance = 5.05;
600 StartDrive(-kDriveDistance, 0.0, kTwoBallLowDrive, kSlowTurn);
601
602 StartDrive(0.0, 0.4, kTwoBallLowDrive, kSwerveTurn);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700603 if (!WaitForDriveNear(kDriveDistance - 0.5, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700604
Austin Schuh295c2d92016-05-01 12:28:04 -0700605 // Check if the ball is there.
606 bool first_ball_there = true;
607 ::y2016::sensors::ball_detector.FetchLatest();
608 if (::y2016::sensors::ball_detector.get()) {
609 const bool ball_detected = ::y2016::sensors::ball_detector->voltage > 2.5;
610 first_ball_there = ball_detected;
611 LOG(INFO, "Saw the ball: %d at %f\n", first_ball_there,
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800612 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh295c2d92016-05-01 12:28:04 -0700613 }
Austin Schuh3e4a5272016-04-20 20:11:00 -0700614 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 40.0}, {4.0, 10.0}, {10.0, 25.0},
615 false, 0.0);
616 LOG(INFO, "Shutting off rollers at %f seconds, starting to straighten out\n",
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800617 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700618 StartDrive(0.0, -0.4, kTwoBallLowDrive, kSwerveTurn);
619 MoveSuperstructure(-0.05, -0.010, 0.0, {8.0, 40.0}, {4.0, 10.0}, {10.0, 25.0},
620 false, 0.0);
621 CloseShooter();
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700622 if (!WaitForDriveNear(kDriveDistance - 2.4, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700623
624 // We are now under the low bar. Start lifting.
625 BackLongShotLowBarTwoBall();
626 LOG(INFO, "Spinning up the shooter wheels\n");
627 SetShooterSpeed(640.0);
628 StartDrive(0.0, 0.0, kTwoBallFastDrive, kSwerveTurn);
629
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700630 if (!WaitForDriveNear(1.50, kDoNotTurnCare)) return;
631 constexpr double kShootTurnAngle = -M_PI / 4.0 - 0.05;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700632 StartDrive(0, kShootTurnAngle, kTwoBallFastDrive, kFinishTurn);
633 BackLongShotTwoBall();
634
635 if (!WaitForDriveDone()) return;
636 LOG(INFO, "First shot done driving at %f seconds\n",
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800637 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700638
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700639 WaitForSuperstructureProfile();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700640
641 if (ShouldCancel()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700642 AlignWithVisionGoal();
643
644 WaitForShooterSpeed();
645 if (ShouldCancel()) return;
646
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800647 constexpr chrono::milliseconds kVisionExtra{0};
648 WaitForAlignedWithVision(chrono::milliseconds(500) + kVisionExtra);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700649 BackLongShotTwoBallFinish();
650 WaitForSuperstructureProfile();
651 if (ShouldCancel()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700652 LOG(INFO, "Shoot!\n");
Austin Schuh295c2d92016-05-01 12:28:04 -0700653 if (first_ball_there) {
654 Shoot();
655 } else {
656 LOG(INFO, "Nah, not shooting\n");
657 }
Austin Schuh3e4a5272016-04-20 20:11:00 -0700658
659 LOG(INFO, "First shot at %f seconds\n",
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800660 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700661 if (ShouldCancel()) return;
662
663 SetShooterSpeed(0.0);
664 LOG(INFO, "Folding superstructure back down\n");
665 TuckArm(true, true);
666
667 // Undo vision move.
668 StartDrive(0.0, 0.0, kTwoBallFastDrive, kFinishTurn);
669 if (!WaitForDriveDone()) return;
670
671 constexpr double kBackDrive = 3.09 - 0.4;
672 StartDrive(kBackDrive, 0.0, kTwoBallReturnDrive, kSlowTurn);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700673 if (!WaitForDriveNear(kBackDrive - 0.19, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700674 StartDrive(0, -kShootTurnAngle, kTwoBallReturnDrive, kSwerveTurn);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700675 if (!WaitForDriveNear(1.0, kDoNotTurnCare)) return;
676 StartDrive(0, 0, kTwoBallReturnSlow, kSwerveTurn);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700677
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700678 if (!WaitForDriveNear(0.06, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700679 LOG(INFO, "At Low Bar %f\n",
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800680 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700681
682 OpenShooter();
683 constexpr double kSecondBallAfterBarDrive = 2.10;
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700684 StartDrive(kSecondBallAfterBarDrive, 0.0, kTwoBallBallPickupAccel, kSlowTurn);
685 if (!WaitForDriveNear(kSecondBallAfterBarDrive - 0.5, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700686 constexpr double kBallSmallWallTurn = -0.11;
687 StartDrive(0, kBallSmallWallTurn, kTwoBallBallPickup, kFinishTurn);
688
689 MoveSuperstructure(0.03, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
690 false, 12.0);
691
692 if (!WaitForDriveProfileDone()) return;
693
694 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
695 false, 12.0);
696
697 LOG(INFO, "Done backing up %f\n",
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800698 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700699
700 constexpr double kDriveBackDistance = 5.15 - 0.4;
701 StartDrive(-kDriveBackDistance, 0.0, kTwoBallLowDrive, kFinishTurn);
702 CloseIfBall();
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700703 if (!WaitForDriveNear(kDriveBackDistance - 0.75, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700704
705 StartDrive(0.0, -kBallSmallWallTurn, kTwoBallLowDrive, kFinishTurn);
706 LOG(INFO, "Straightening up at %f\n",
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800707 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700708
709 CloseIfBall();
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700710 if (!WaitForDriveNear(kDriveBackDistance - 2.3, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700711
712 ::y2016::sensors::ball_detector.FetchLatest();
713 if (::y2016::sensors::ball_detector.get()) {
714 const bool ball_detected = ::y2016::sensors::ball_detector->voltage > 2.5;
715 if (!ball_detected) {
716 if (!WaitForDriveDone()) return;
717 LOG(INFO, "Aborting, no ball %f\n",
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800718 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700719 return;
720 }
721 }
722 CloseShooter();
723
724 BackLongShotLowBarTwoBall();
725 LOG(INFO, "Spinning up the shooter wheels\n");
726 SetShooterSpeed(640.0);
727 StartDrive(0.0, 0.0, kTwoBallFastDrive, kSwerveTurn);
728
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700729 if (!WaitForDriveNear(1.80, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700730 StartDrive(0, kShootTurnAngle, kTwoBallFastDrive, kFinishTurn);
731 BackLongShotTwoBall();
732
733 if (!WaitForDriveDone()) return;
734 LOG(INFO, "Second shot done driving at %f seconds\n",
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800735 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700736 WaitForSuperstructure();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700737 AlignWithVisionGoal();
738 if (ShouldCancel()) return;
739
740 WaitForShooterSpeed();
741 if (ShouldCancel()) return;
742
743 // 2.2 with 0.4 of vision.
744 // 1.8 without any vision.
745 LOG(INFO, "Going to vision align at %f\n",
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800746 DoubleSeconds(monotonic_clock::now() - start_time));
747 WaitForAlignedWithVision(
748 (start_time + chrono::milliseconds(13500) + kVisionExtra * 2) -
749 monotonic_clock::now());
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700750 BackLongShotTwoBallFinish();
751 WaitForSuperstructureProfile();
752 if (ShouldCancel()) return;
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800753 LOG(INFO, "Shoot at %f\n",
754 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700755 Shoot();
756
757 LOG(INFO, "Second shot at %f seconds\n",
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800758 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700759 if (ShouldCancel()) return;
760
761 SetShooterSpeed(0.0);
762 LOG(INFO, "Folding superstructure back down\n");
763 TuckArm(true, false);
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800764 LOG(INFO, "Shot %f\n", DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700765
766 WaitForSuperstructureLow();
767
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800768 LOG(INFO, "Done %f\n", DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700769}
770
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700771void AutonomousActor::StealAndMoveOverBy(double distance) {
772 OpenShooter();
773 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
774 true, 12.0);
775 if (ShouldCancel()) return;
776 LOG(INFO, "Waiting for the intake to come down.\n");
777
778 WaitForIntake();
779 if (ShouldCancel()) return;
780 StartDrive(-distance, M_PI / 2.0, kFastDrive, kStealTurn);
781 WaitForBallOrDriveDone();
782 if (ShouldCancel()) return;
783 MoveSuperstructure(1.0, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
784 true, 12.0);
785
786 if (!WaitForDriveDone()) return;
787 StartDrive(0.0, M_PI / 2.0, kFastDrive, kStealTurn);
788 if (!WaitForDriveDone()) return;
789}
790
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000791bool AutonomousActor::RunAction(
792 const ::frc971::autonomous::AutonomousActionParams &params) {
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800793 monotonic_clock::time_point start_time = monotonic_clock::now();
Comran Morshede68e3732016-03-12 14:12:11 +0000794 LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode);
795
Comran Morshed435f1112016-03-12 14:20:45 +0000796 InitializeEncoders();
797 ResetDrivetrain();
798
Austin Schuh295c2d92016-05-01 12:28:04 -0700799 switch (params.mode) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700800 case 0:
801 LowBarDrive();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700802 if (!WaitForDriveDone()) return true;
803 // Get the superstructure to unfold and get ready for shooting.
804 LOG(INFO, "Unfolding superstructure\n");
805 FrontLongShot();
806
807 // Spin up the shooter wheels.
808 LOG(INFO, "Spinning up the shooter wheels\n");
809 SetShooterSpeed(640.0);
810
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700811 break;
812 case 1:
813 TwoFromMiddleDrive();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700814 if (!WaitForDriveDone()) return true;
815 // Get the superstructure to unfold and get ready for shooting.
816 LOG(INFO, "Unfolding superstructure\n");
817 FrontMiddleShot();
818
819 // Spin up the shooter wheels.
820 LOG(INFO, "Spinning up the shooter wheels\n");
821 SetShooterSpeed(600.0);
822
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700823 break;
824 case 2:
825 OneFromMiddleDrive(true);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700826 if (!WaitForDriveDone()) return true;
827 // Get the superstructure to unfold and get ready for shooting.
828 LOG(INFO, "Unfolding superstructure\n");
829 FrontMiddleShot();
830
831 // Spin up the shooter wheels.
832 LOG(INFO, "Spinning up the shooter wheels\n");
833 SetShooterSpeed(600.0);
834
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700835 break;
836 case 3:
837 MiddleDrive();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700838 if (!WaitForDriveDone()) return true;
839 // Get the superstructure to unfold and get ready for shooting.
840 LOG(INFO, "Unfolding superstructure\n");
841 FrontMiddleShot();
842
843 // Spin up the shooter wheels.
844 LOG(INFO, "Spinning up the shooter wheels\n");
845 SetShooterSpeed(600.0);
846
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700847 break;
848 case 4:
849 OneFromMiddleDrive(false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700850 if (!WaitForDriveDone()) return true;
851 // Get the superstructure to unfold and get ready for shooting.
852 LOG(INFO, "Unfolding superstructure\n");
853 FrontMiddleShot();
854
855 // Spin up the shooter wheels.
856 LOG(INFO, "Spinning up the shooter wheels\n");
857 SetShooterSpeed(600.0);
858
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700859 break;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700860 case 5:
Campbell Crowley9ed61a52016-11-05 17:13:07 -0700861 case 15:
Austin Schuh3e4a5272016-04-20 20:11:00 -0700862 TwoBallAuto();
Austin Schuh23b21802016-04-03 21:18:56 -0700863 return true;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700864 break;
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700865 case 6:
866 StealAndMoveOverBy(3.10 + 2 * 52 * 2.54 / 100.0);
867 if (ShouldCancel()) return true;
868
869 TwoFromMiddleDrive();
870 if (!WaitForDriveDone()) return true;
871 // Get the superstructure to unfold and get ready for shooting.
872 LOG(INFO, "Unfolding superstructure\n");
873 FrontMiddleShot();
874
875 // Spin up the shooter wheels.
876 LOG(INFO, "Spinning up the shooter wheels\n");
877 SetShooterSpeed(600.0);
878
879 break;
880 case 7:
881 StealAndMoveOverBy(2.95 + 52 * 2.54 / 100.0);
882 if (ShouldCancel()) return true;
883
884 OneFromMiddleDrive(true);
885 if (!WaitForDriveDone()) return true;
886 // Get the superstructure to unfold and get ready for shooting.
887 LOG(INFO, "Unfolding superstructure\n");
888 FrontMiddleShot();
889
890 // Spin up the shooter wheels.
891 LOG(INFO, "Spinning up the shooter wheels\n");
892 SetShooterSpeed(600.0);
893
894 break;
895 case 8: {
896 StealAndMoveOverBy(2.95);
897 if (ShouldCancel()) return true;
898
899 MiddleDrive();
900 if (!WaitForDriveDone()) return true;
901 // Get the superstructure to unfold and get ready for shooting.
902 LOG(INFO, "Unfolding superstructure\n");
903 FrontMiddleShot();
904
905 // Spin up the shooter wheels.
906 LOG(INFO, "Spinning up the shooter wheels\n");
907 SetShooterSpeed(600.0);
908
909 } break;
910 case 9: {
911 StealAndMoveOverBy(1.70);
912 if (ShouldCancel()) return true;
913
914 OneFromMiddleDrive(false);
915 if (!WaitForDriveDone()) return true;
916 // Get the superstructure to unfold and get ready for shooting.
917 LOG(INFO, "Unfolding superstructure\n");
918 FrontMiddleShot();
919
920 // Spin up the shooter wheels.
921 LOG(INFO, "Spinning up the shooter wheels\n");
922 SetShooterSpeed(600.0);
923
924 } break;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700925 default:
Austin Schuh6c9bc622016-03-26 19:44:12 -0700926 LOG(ERROR, "Invalid auto mode %d\n", params.mode);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700927 return true;
928 }
Comran Morshed435f1112016-03-12 14:20:45 +0000929
Austin Schuh3e4a5272016-04-20 20:11:00 -0700930 DoFullShot();
931
932 StartDrive(0.5, 0.0, kMoatDrive, kFastTurn);
Comran Morshed435f1112016-03-12 14:20:45 +0000933 if (!WaitForDriveDone()) return true;
934
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800935 LOG(INFO, "Done %f\n", DoubleSeconds(monotonic_clock::now() - start_time));
Comran Morshed435f1112016-03-12 14:20:45 +0000936
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700937 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
938 ::std::chrono::milliseconds(5) / 2);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700939
Comran Morshed435f1112016-03-12 14:20:45 +0000940 while (!ShouldCancel()) {
941 phased_loop.SleepUntilNext();
Comran Morshede68e3732016-03-12 14:12:11 +0000942 }
Comran Morshed435f1112016-03-12 14:20:45 +0000943 LOG(DEBUG, "Done running\n");
Comran Morshede68e3732016-03-12 14:12:11 +0000944
945 return true;
946}
947
Comran Morshede68e3732016-03-12 14:12:11 +0000948} // namespace actors
949} // namespace y2016