blob: f486d389f992409a9ecc073d06a99131ad244f5a [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),
Austin Schuhd32b3622019-06-23 18:49:06 -0700169 event_loop()->monotonic_now(),
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700170 ::std::chrono::milliseconds(5) / 2);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700171 while (true) {
172 if (ShouldCancel()) return;
173
174 // Wait for the shot count to change so we know when the shot is complete.
175 control_loops::shooter::shooter_queue.status.FetchLatest();
176 if (control_loops::shooter::shooter_queue.status.get()) {
177 if (initial_shots < control_loops::shooter::shooter_queue.status->shots) {
178 return;
179 }
180 }
181 phased_loop.SleepUntilNext();
182 }
183}
184
185void AutonomousActor::WaitForShooterSpeed() {
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700186 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700187 event_loop()->monotonic_now(),
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700188 ::std::chrono::milliseconds(5) / 2);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700189 while (true) {
190 if (ShouldCancel()) return;
191
192 control_loops::shooter::shooter_queue.status.FetchLatest();
193 if (control_loops::shooter::shooter_queue.status.get()) {
194 if (control_loops::shooter::shooter_queue.status->left.ready &&
195 control_loops::shooter::shooter_queue.status->right.ready) {
196 return;
197 }
198 }
199 phased_loop.SleepUntilNext();
200 }
201}
202
203void AutonomousActor::AlignWithVisionGoal() {
204 actors::VisionAlignActionParams params;
Austin Schuh1bf8a212019-05-26 22:13:14 -0700205 vision_action_ = vision_align_actor_factory_.Make(params);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700206 vision_action_->Start();
207}
208
Austin Schuh23b21802016-04-03 21:18:56 -0700209void AutonomousActor::WaitForAlignedWithVision(
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800210 chrono::nanoseconds align_duration) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700211 bool vision_valid = false;
212 double last_angle = 0.0;
213 int ready_to_fire = 0;
214
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700215 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700216 event_loop()->monotonic_now(),
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700217 ::std::chrono::milliseconds(5) / 2);
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800218 monotonic_clock::time_point end_time =
219 monotonic_clock::now() + align_duration;
220 while (end_time > monotonic_clock::now()) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700221 if (ShouldCancel()) break;
222
Austin Schuh28bde302019-05-26 22:24:33 -0700223 vision_status_fetcher_.Fetch();
224 if (vision_status_fetcher_.get()) {
225 vision_valid = (vision_status_fetcher_->left_image_valid &&
226 vision_status_fetcher_->right_image_valid);
227 last_angle = vision_status_fetcher_->horizontal_angle;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700228 }
229
230 drivetrain_queue.status.FetchLatest();
231 drivetrain_queue.goal.FetchLatest();
232
233 if (drivetrain_queue.status.get() && drivetrain_queue.goal.get()) {
234 const double left_goal = drivetrain_queue.goal->left_goal;
235 const double right_goal = drivetrain_queue.goal->right_goal;
236 const double left_current =
237 drivetrain_queue.status->estimated_left_position;
238 const double right_current =
239 drivetrain_queue.status->estimated_right_position;
240 const double left_velocity =
241 drivetrain_queue.status->estimated_left_velocity;
242 const double right_velocity =
243 drivetrain_queue.status->estimated_right_velocity;
244
245 if (vision_valid && ::std::abs(last_angle) < 0.02 &&
246 ::std::abs((left_goal - right_goal) -
247 (left_current - right_current)) /
248 dt_config_.robot_radius / 2.0 <
249 0.02 &&
250 ::std::abs(left_velocity - right_velocity) < 0.01) {
251 ++ready_to_fire;
252 } else {
253 ready_to_fire = 0;
254 }
Austin Schuh3e4a5272016-04-20 20:11:00 -0700255 if (ready_to_fire > 15) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700256 break;
Austin Schuh23b21802016-04-03 21:18:56 -0700257 LOG(INFO, "Vision align success!\n");
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700258 }
259 }
260 phased_loop.SleepUntilNext();
261 }
262
263 vision_action_->Cancel();
264 WaitUntilDoneOrCanceled(::std::move(vision_action_));
Austin Schuh23b21802016-04-03 21:18:56 -0700265 LOG(INFO, "Done waiting for vision\n");
266}
267
268bool AutonomousActor::IntakeDone() {
269 control_loops::superstructure_queue.status.FetchAnother();
270
271 constexpr double kProfileError = 1e-5;
272 constexpr double kEpsilon = 0.15;
273
274 if (control_loops::superstructure_queue.status->state < 12 ||
275 control_loops::superstructure_queue.status->state == 16) {
276 LOG(ERROR, "Superstructure no longer running, aborting action\n");
277 return true;
278 }
279
280 if (::std::abs(control_loops::superstructure_queue.status->intake.goal_angle -
281 superstructure_goal_.intake) < kProfileError &&
282 ::std::abs(control_loops::superstructure_queue.status->intake
283 .goal_angular_velocity) < kProfileError) {
284 LOG(DEBUG, "Profile done.\n");
285 if (::std::abs(control_loops::superstructure_queue.status->intake.angle -
286 superstructure_goal_.intake) < kEpsilon &&
287 ::std::abs(control_loops::superstructure_queue.status->intake
288 .angular_velocity) < kEpsilon) {
289 LOG(INFO, "Near goal, done.\n");
290 return true;
291 }
292 }
293 return false;
294}
295
296bool AutonomousActor::SuperstructureProfileDone() {
297 constexpr double kProfileError = 1e-5;
298 return ::std::abs(
299 control_loops::superstructure_queue.status->intake.goal_angle -
300 superstructure_goal_.intake) < kProfileError &&
301 ::std::abs(
302 control_loops::superstructure_queue.status->shoulder.goal_angle -
303 superstructure_goal_.shoulder) < kProfileError &&
304 ::std::abs(
305 control_loops::superstructure_queue.status->wrist.goal_angle -
306 superstructure_goal_.wrist) < kProfileError &&
307 ::std::abs(control_loops::superstructure_queue.status->intake
308 .goal_angular_velocity) < kProfileError &&
309 ::std::abs(control_loops::superstructure_queue.status->shoulder
310 .goal_angular_velocity) < kProfileError &&
311 ::std::abs(control_loops::superstructure_queue.status->wrist
312 .goal_angular_velocity) < kProfileError;
313}
314
315bool AutonomousActor::SuperstructureDone() {
316 control_loops::superstructure_queue.status.FetchAnother();
317
318 constexpr double kEpsilon = 0.03;
319
320 if (control_loops::superstructure_queue.status->state < 12 ||
321 control_loops::superstructure_queue.status->state == 16) {
322 LOG(ERROR, "Superstructure no longer running, aborting action\n");
323 return true;
324 }
325
326 if (SuperstructureProfileDone()) {
327 LOG(DEBUG, "Profile done.\n");
328 if (::std::abs(control_loops::superstructure_queue.status->intake.angle -
329 superstructure_goal_.intake) < (kEpsilon + 0.1) &&
330 ::std::abs(control_loops::superstructure_queue.status->shoulder.angle -
331 superstructure_goal_.shoulder) < (kEpsilon + 0.05) &&
332 ::std::abs(control_loops::superstructure_queue.status->wrist.angle -
333 superstructure_goal_.wrist) < (kEpsilon + 0.01) &&
334 ::std::abs(control_loops::superstructure_queue.status->intake
335 .angular_velocity) < (kEpsilon + 0.1) &&
336 ::std::abs(control_loops::superstructure_queue.status->shoulder
337 .angular_velocity) < (kEpsilon + 0.10) &&
338 ::std::abs(control_loops::superstructure_queue.status->wrist
339 .angular_velocity) < (kEpsilon + 0.05)) {
340 LOG(INFO, "Near goal, done.\n");
341 return true;
342 }
343 }
344 return false;
345}
346
347void AutonomousActor::WaitForIntake() {
348 while (true) {
349 if (ShouldCancel()) return;
350 if (IntakeDone()) return;
351 }
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700352}
353
Comran Morshedb134e772016-03-16 21:05:05 +0000354void AutonomousActor::WaitForSuperstructure() {
355 while (true) {
356 if (ShouldCancel()) return;
Austin Schuh23b21802016-04-03 21:18:56 -0700357 if (SuperstructureDone()) return;
358 }
359}
Comran Morshedb134e772016-03-16 21:05:05 +0000360
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700361void AutonomousActor::WaitForSuperstructureProfile() {
362 while (true) {
363 if (ShouldCancel()) return;
364 control_loops::superstructure_queue.status.FetchAnother();
365
366 if (control_loops::superstructure_queue.status->state < 12 ||
367 control_loops::superstructure_queue.status->state == 16) {
368 LOG(ERROR, "Superstructure no longer running, aborting action\n");
369 return;
370 }
371
372 if (SuperstructureProfileDone()) return;
373 }
374}
375
Austin Schuh23b21802016-04-03 21:18:56 -0700376void AutonomousActor::WaitForSuperstructureLow() {
377 while (true) {
378 if (ShouldCancel()) return;
379 control_loops::superstructure_queue.status.FetchAnother();
Comran Morshedb134e772016-03-16 21:05:05 +0000380
381 if (control_loops::superstructure_queue.status->state < 12 ||
382 control_loops::superstructure_queue.status->state == 16) {
383 LOG(ERROR, "Superstructure no longer running, aborting action\n");
384 return;
385 }
Austin Schuh23b21802016-04-03 21:18:56 -0700386 if (SuperstructureProfileDone()) return;
387 if (control_loops::superstructure_queue.status->shoulder.angle < 0.1) {
388 return;
Comran Morshedb134e772016-03-16 21:05:05 +0000389 }
390 }
391}
Austin Schuh23b21802016-04-03 21:18:56 -0700392void AutonomousActor::BackLongShotLowBarTwoBall() {
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},
Austin Schuh23b21802016-04-03 21:18:56 -0700395 {10.0, 25.0}, false, 0.0);
396}
397
398void AutonomousActor::BackLongShotTwoBall() {
399 LOG(INFO, "Expanding for back long shot\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700400 MoveSuperstructure(0.00, M_PI / 2.0 - 0.2, -0.55, {7.0, 40.0}, {4.0, 6.0},
401 {10.0, 25.0}, false, 0.0);
402}
403
404void AutonomousActor::BackLongShotTwoBallFinish() {
405 LOG(INFO, "Expanding for back long shot\n");
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000406 MoveSuperstructure(0.00, M_PI / 2.0 - 0.2, -0.625 + 0.03, {7.0, 40.0},
407 {4.0, 6.0}, {10.0, 25.0}, false, 0.0);
Austin Schuh23b21802016-04-03 21:18:56 -0700408}
409
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700410void AutonomousActor::BackLongShot() {
411 LOG(INFO, "Expanding for back long shot\n");
Austin Schuh23b21802016-04-03 21:18:56 -0700412 MoveSuperstructure(0.80, M_PI / 2.0 - 0.2, -0.62, {7.0, 40.0}, {4.0, 6.0},
413 {10.0, 25.0}, false, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700414}
415
416void AutonomousActor::BackMiddleShot() {
417 LOG(INFO, "Expanding for back middle shot\n");
418 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 -0700419 {10.0, 25.0}, false, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700420}
421
Austin Schuh3e4a5272016-04-20 20:11:00 -0700422void AutonomousActor::FrontLongShot() {
423 LOG(INFO, "Expanding for front long shot\n");
424 MoveSuperstructure(0.80, M_PI / 2.0 + 0.1, M_PI + 0.41 + 0.02, {7.0, 40.0},
425 {4.0, 6.0}, {10.0, 25.0}, false, 0.0);
426}
427
428void AutonomousActor::FrontMiddleShot() {
429 LOG(INFO, "Expanding for front middle shot\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700430 MoveSuperstructure(-0.05, M_PI / 2.0 + 0.1, M_PI + 0.44, {7.0, 40.0},
Austin Schuh3e4a5272016-04-20 20:11:00 -0700431 {4.0, 10.0}, {10.0, 25.0}, true, 0.0);
432}
433
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700434void AutonomousActor::TuckArm(bool low_bar, bool traverse_down) {
435 MoveSuperstructure(low_bar ? -0.05 : 2.0, -0.010, 0.0, {7.0, 40.0},
Austin Schuh23b21802016-04-03 21:18:56 -0700436 {4.0, 10.0}, {10.0, 25.0}, !traverse_down, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700437}
438
Austin Schuh3e4a5272016-04-20 20:11:00 -0700439void AutonomousActor::DoFullShot() {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700440 if (ShouldCancel()) return;
441 // Make sure that the base is aligned with the base.
442 LOG(INFO, "Waiting for the superstructure\n");
443 WaitForSuperstructure();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700444
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800445 this_thread::sleep_for(chrono::milliseconds(500));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700446
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700447 if (ShouldCancel()) return;
448 LOG(INFO, "Triggering the vision actor\n");
449 AlignWithVisionGoal();
450
451 // Wait for the drive base to be aligned with the target and make sure that
452 // the shooter is up to speed.
453 LOG(INFO, "Waiting for vision to be aligned\n");
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800454 WaitForAlignedWithVision(chrono::milliseconds(2000));
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700455 if (ShouldCancel()) return;
456 LOG(INFO, "Waiting for shooter to be up to speed\n");
457 WaitForShooterSpeed();
458 if (ShouldCancel()) return;
459
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800460 this_thread::sleep_for(chrono::milliseconds(300));
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700461 LOG(INFO, "Shoot!\n");
462 Shoot();
463
464 // Turn off the shooter and fold up the superstructure.
465 if (ShouldCancel()) return;
466 LOG(INFO, "Stopping shooter\n");
467 SetShooterSpeed(0.0);
468 LOG(INFO, "Folding superstructure back down\n");
469 TuckArm(false, false);
470
471 // Wait for everything to be folded up.
472 LOG(INFO, "Waiting for superstructure to be folded back down\n");
Austin Schuh3e4a5272016-04-20 20:11:00 -0700473 WaitForSuperstructureLow();
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700474}
475
476void AutonomousActor::LowBarDrive() {
477 TuckArm(false, true);
478 StartDrive(-5.5, 0.0, kLowBarDrive, kSlowTurn);
479
480 if (!WaitForDriveNear(5.3, 0.0)) return;
481 TuckArm(true, true);
482
483 if (!WaitForDriveNear(5.0, 0.0)) return;
484
485 StartDrive(0.0, 0.0, kLowBarDrive, kSlowTurn);
486
487 if (!WaitForDriveNear(3.0, 0.0)) return;
488
489 StartDrive(0.0, 0.0, kLowBarDrive, kSlowTurn);
490
491 if (!WaitForDriveNear(1.0, 0.0)) return;
492
Austin Schuh15b5f6a2016-03-26 19:43:56 -0700493 StartDrive(0, -M_PI / 4.0 - 0.2, kLowBarDrive, kSlowTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700494}
495
Austin Schuh3e4a5272016-04-20 20:11:00 -0700496void AutonomousActor::TippyDrive(double goal_distance, double tip_distance,
497 double below, double above) {
498 StartDrive(goal_distance, 0.0, kMoatDrive, kSlowTurn);
499 if (!WaitForBelowAngle(below)) return;
500 if (!WaitForAboveAngle(above)) return;
501 // Ok, we are good now. Compensate by moving the goal by the error.
502 // Should be here at 2.7
503 drivetrain_queue.status.FetchLatest();
504 if (drivetrain_queue.status.get()) {
505 const double left_error =
506 (initial_drivetrain_.left -
507 drivetrain_queue.status->estimated_left_position);
508 const double right_error =
509 (initial_drivetrain_.right -
510 drivetrain_queue.status->estimated_right_position);
511 const double distance_to_go = (left_error + right_error) / 2.0;
512 const double distance_compensation =
513 goal_distance - tip_distance - distance_to_go;
514 LOG(INFO, "Going %f further at the bump\n", distance_compensation);
515 StartDrive(distance_compensation, 0.0, kMoatDrive, kSlowTurn);
516 }
517}
518
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700519void AutonomousActor::MiddleDrive() {
520 TuckArm(false, false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700521 TippyDrive(3.65, 2.7, -0.2, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700522 if (!WaitForDriveDone()) return;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700523}
524
525void AutonomousActor::OneFromMiddleDrive(bool left) {
Austin Schuh3e4a5272016-04-20 20:11:00 -0700526 const double kTurnAngle = left ? -0.41 : 0.41;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700527 TuckArm(false, false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700528 TippyDrive(4.05, 2.7, -0.2, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700529
530 if (!WaitForDriveDone()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700531 StartDrive(0.0, kTurnAngle, kRealignDrive, kFastTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700532}
533
534void AutonomousActor::TwoFromMiddleDrive() {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700535 TuckArm(false, false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700536 constexpr double kDriveDistance = 5.10;
537 TippyDrive(kDriveDistance, 2.7, -0.2, 0.0);
538
539 if (!WaitForDriveNear(kDriveDistance - 3.0, 2.0)) return;
540 StartDrive(0, -M_PI / 2 - 0.10, kMoatDrive, kFastTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700541
542 if (!WaitForDriveDone()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700543 StartDrive(0, M_PI / 3 + 0.35, kMoatDrive, kFastTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700544}
Comran Morshedb134e772016-03-16 21:05:05 +0000545
Austin Schuh23b21802016-04-03 21:18:56 -0700546void AutonomousActor::CloseIfBall() {
Austin Schuh4b652c92019-05-27 13:22:27 -0700547 ball_detector_fetcher_.Fetch();
548 if (ball_detector_fetcher_.get()) {
549 const bool ball_detected = ball_detector_fetcher_->voltage > 2.5;
Austin Schuh23b21802016-04-03 21:18:56 -0700550 if (ball_detected) {
551 CloseShooter();
552 }
553 }
554}
555
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700556void AutonomousActor::WaitForBallOrDriveDone() {
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700557 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700558 event_loop()->monotonic_now(),
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700559 ::std::chrono::milliseconds(5) / 2);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700560 while (true) {
561 if (ShouldCancel()) {
562 return;
563 }
564 phased_loop.SleepUntilNext();
565 drivetrain_queue.status.FetchLatest();
566 if (IsDriveDone()) {
567 return;
568 }
569
Austin Schuh4b652c92019-05-27 13:22:27 -0700570 ball_detector_fetcher_.Fetch();
571 if (ball_detector_fetcher_.get()) {
572 const bool ball_detected = ball_detector_fetcher_->voltage > 2.5;
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700573 if (ball_detected) {
574 return;
575 }
576 }
577 }
578}
579
Austin Schuh3e4a5272016-04-20 20:11:00 -0700580void AutonomousActor::TwoBallAuto() {
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800581 monotonic_clock::time_point start_time = monotonic_clock::now();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700582 OpenShooter();
583 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
584 false, 12.0);
585 if (ShouldCancel()) return;
586 LOG(INFO, "Waiting for the intake to come down.\n");
587
588 WaitForIntake();
589 LOG(INFO, "Intake done at %f seconds, starting to drive\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700590 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700591 if (ShouldCancel()) return;
592 const double kDriveDistance = 5.05;
593 StartDrive(-kDriveDistance, 0.0, kTwoBallLowDrive, kSlowTurn);
594
595 StartDrive(0.0, 0.4, kTwoBallLowDrive, kSwerveTurn);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700596 if (!WaitForDriveNear(kDriveDistance - 0.5, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700597
Austin Schuh295c2d92016-05-01 12:28:04 -0700598 // Check if the ball is there.
599 bool first_ball_there = true;
Austin Schuh4b652c92019-05-27 13:22:27 -0700600 ball_detector_fetcher_.Fetch();
601 if (ball_detector_fetcher_.get()) {
602 const bool ball_detected = ball_detector_fetcher_->voltage > 2.5;
Austin Schuh295c2d92016-05-01 12:28:04 -0700603 first_ball_there = ball_detected;
604 LOG(INFO, "Saw the ball: %d at %f\n", first_ball_there,
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700605 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh295c2d92016-05-01 12:28:04 -0700606 }
Austin Schuh3e4a5272016-04-20 20:11:00 -0700607 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 40.0}, {4.0, 10.0}, {10.0, 25.0},
608 false, 0.0);
609 LOG(INFO, "Shutting off rollers at %f seconds, starting to straighten out\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700610 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700611 StartDrive(0.0, -0.4, kTwoBallLowDrive, kSwerveTurn);
612 MoveSuperstructure(-0.05, -0.010, 0.0, {8.0, 40.0}, {4.0, 10.0}, {10.0, 25.0},
613 false, 0.0);
614 CloseShooter();
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700615 if (!WaitForDriveNear(kDriveDistance - 2.4, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700616
617 // We are now under the low bar. Start lifting.
618 BackLongShotLowBarTwoBall();
619 LOG(INFO, "Spinning up the shooter wheels\n");
620 SetShooterSpeed(640.0);
621 StartDrive(0.0, 0.0, kTwoBallFastDrive, kSwerveTurn);
622
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700623 if (!WaitForDriveNear(1.50, kDoNotTurnCare)) return;
624 constexpr double kShootTurnAngle = -M_PI / 4.0 - 0.05;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700625 StartDrive(0, kShootTurnAngle, kTwoBallFastDrive, kFinishTurn);
626 BackLongShotTwoBall();
627
628 if (!WaitForDriveDone()) return;
629 LOG(INFO, "First shot done driving at %f seconds\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700630 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700631
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700632 WaitForSuperstructureProfile();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700633
634 if (ShouldCancel()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700635 AlignWithVisionGoal();
636
637 WaitForShooterSpeed();
638 if (ShouldCancel()) return;
639
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800640 constexpr chrono::milliseconds kVisionExtra{0};
641 WaitForAlignedWithVision(chrono::milliseconds(500) + kVisionExtra);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700642 BackLongShotTwoBallFinish();
643 WaitForSuperstructureProfile();
644 if (ShouldCancel()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700645 LOG(INFO, "Shoot!\n");
Austin Schuh295c2d92016-05-01 12:28:04 -0700646 if (first_ball_there) {
647 Shoot();
648 } else {
649 LOG(INFO, "Nah, not shooting\n");
650 }
Austin Schuh3e4a5272016-04-20 20:11:00 -0700651
652 LOG(INFO, "First shot at %f seconds\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700653 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700654 if (ShouldCancel()) return;
655
656 SetShooterSpeed(0.0);
657 LOG(INFO, "Folding superstructure back down\n");
658 TuckArm(true, true);
659
660 // Undo vision move.
661 StartDrive(0.0, 0.0, kTwoBallFastDrive, kFinishTurn);
662 if (!WaitForDriveDone()) return;
663
664 constexpr double kBackDrive = 3.09 - 0.4;
665 StartDrive(kBackDrive, 0.0, kTwoBallReturnDrive, kSlowTurn);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700666 if (!WaitForDriveNear(kBackDrive - 0.19, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700667 StartDrive(0, -kShootTurnAngle, kTwoBallReturnDrive, kSwerveTurn);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700668 if (!WaitForDriveNear(1.0, kDoNotTurnCare)) return;
669 StartDrive(0, 0, kTwoBallReturnSlow, kSwerveTurn);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700670
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700671 if (!WaitForDriveNear(0.06, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700672 LOG(INFO, "At Low Bar %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700673 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700674
675 OpenShooter();
676 constexpr double kSecondBallAfterBarDrive = 2.10;
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700677 StartDrive(kSecondBallAfterBarDrive, 0.0, kTwoBallBallPickupAccel, kSlowTurn);
678 if (!WaitForDriveNear(kSecondBallAfterBarDrive - 0.5, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700679 constexpr double kBallSmallWallTurn = -0.11;
680 StartDrive(0, kBallSmallWallTurn, kTwoBallBallPickup, kFinishTurn);
681
682 MoveSuperstructure(0.03, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
683 false, 12.0);
684
685 if (!WaitForDriveProfileDone()) return;
686
687 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
688 false, 12.0);
689
690 LOG(INFO, "Done backing up %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700691 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700692
693 constexpr double kDriveBackDistance = 5.15 - 0.4;
694 StartDrive(-kDriveBackDistance, 0.0, kTwoBallLowDrive, kFinishTurn);
695 CloseIfBall();
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700696 if (!WaitForDriveNear(kDriveBackDistance - 0.75, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700697
698 StartDrive(0.0, -kBallSmallWallTurn, kTwoBallLowDrive, kFinishTurn);
699 LOG(INFO, "Straightening up at %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700700 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700701
702 CloseIfBall();
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700703 if (!WaitForDriveNear(kDriveBackDistance - 2.3, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700704
Austin Schuh4b652c92019-05-27 13:22:27 -0700705 ball_detector_fetcher_.Fetch();
706 if (ball_detector_fetcher_.get()) {
707 const bool ball_detected = ball_detector_fetcher_->voltage > 2.5;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700708 if (!ball_detected) {
709 if (!WaitForDriveDone()) return;
710 LOG(INFO, "Aborting, no ball %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700711 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700712 return;
713 }
714 }
715 CloseShooter();
716
717 BackLongShotLowBarTwoBall();
718 LOG(INFO, "Spinning up the shooter wheels\n");
719 SetShooterSpeed(640.0);
720 StartDrive(0.0, 0.0, kTwoBallFastDrive, kSwerveTurn);
721
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700722 if (!WaitForDriveNear(1.80, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700723 StartDrive(0, kShootTurnAngle, kTwoBallFastDrive, kFinishTurn);
724 BackLongShotTwoBall();
725
726 if (!WaitForDriveDone()) return;
727 LOG(INFO, "Second shot done driving at %f seconds\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700728 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700729 WaitForSuperstructure();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700730 AlignWithVisionGoal();
731 if (ShouldCancel()) return;
732
733 WaitForShooterSpeed();
734 if (ShouldCancel()) return;
735
736 // 2.2 with 0.4 of vision.
737 // 1.8 without any vision.
738 LOG(INFO, "Going to vision align at %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700739 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800740 WaitForAlignedWithVision(
741 (start_time + chrono::milliseconds(13500) + kVisionExtra * 2) -
742 monotonic_clock::now());
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700743 BackLongShotTwoBallFinish();
744 WaitForSuperstructureProfile();
745 if (ShouldCancel()) return;
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800746 LOG(INFO, "Shoot at %f\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 Shoot();
749
750 LOG(INFO, "Second shot at %f seconds\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700751 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700752 if (ShouldCancel()) return;
753
754 SetShooterSpeed(0.0);
755 LOG(INFO, "Folding superstructure back down\n");
756 TuckArm(true, false);
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700757 LOG(INFO, "Shot %f\n",
758 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700759
760 WaitForSuperstructureLow();
761
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700762 LOG(INFO, "Done %f\n",
763 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700764}
765
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700766void AutonomousActor::StealAndMoveOverBy(double distance) {
767 OpenShooter();
768 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
769 true, 12.0);
770 if (ShouldCancel()) return;
771 LOG(INFO, "Waiting for the intake to come down.\n");
772
773 WaitForIntake();
774 if (ShouldCancel()) return;
775 StartDrive(-distance, M_PI / 2.0, kFastDrive, kStealTurn);
776 WaitForBallOrDriveDone();
777 if (ShouldCancel()) return;
778 MoveSuperstructure(1.0, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
779 true, 12.0);
780
781 if (!WaitForDriveDone()) return;
782 StartDrive(0.0, M_PI / 2.0, kFastDrive, kStealTurn);
783 if (!WaitForDriveDone()) return;
784}
785
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000786bool AutonomousActor::RunAction(
787 const ::frc971::autonomous::AutonomousActionParams &params) {
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800788 monotonic_clock::time_point start_time = monotonic_clock::now();
Comran Morshede68e3732016-03-12 14:12:11 +0000789 LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode);
790
Comran Morshed435f1112016-03-12 14:20:45 +0000791 InitializeEncoders();
792 ResetDrivetrain();
793
Austin Schuh295c2d92016-05-01 12:28:04 -0700794 switch (params.mode) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700795 case 0:
796 LowBarDrive();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700797 if (!WaitForDriveDone()) return true;
798 // Get the superstructure to unfold and get ready for shooting.
799 LOG(INFO, "Unfolding superstructure\n");
800 FrontLongShot();
801
802 // Spin up the shooter wheels.
803 LOG(INFO, "Spinning up the shooter wheels\n");
804 SetShooterSpeed(640.0);
805
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700806 break;
807 case 1:
808 TwoFromMiddleDrive();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700809 if (!WaitForDriveDone()) return true;
810 // Get the superstructure to unfold and get ready for shooting.
811 LOG(INFO, "Unfolding superstructure\n");
812 FrontMiddleShot();
813
814 // Spin up the shooter wheels.
815 LOG(INFO, "Spinning up the shooter wheels\n");
816 SetShooterSpeed(600.0);
817
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700818 break;
819 case 2:
820 OneFromMiddleDrive(true);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700821 if (!WaitForDriveDone()) return true;
822 // Get the superstructure to unfold and get ready for shooting.
823 LOG(INFO, "Unfolding superstructure\n");
824 FrontMiddleShot();
825
826 // Spin up the shooter wheels.
827 LOG(INFO, "Spinning up the shooter wheels\n");
828 SetShooterSpeed(600.0);
829
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700830 break;
831 case 3:
832 MiddleDrive();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700833 if (!WaitForDriveDone()) return true;
834 // Get the superstructure to unfold and get ready for shooting.
835 LOG(INFO, "Unfolding superstructure\n");
836 FrontMiddleShot();
837
838 // Spin up the shooter wheels.
839 LOG(INFO, "Spinning up the shooter wheels\n");
840 SetShooterSpeed(600.0);
841
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700842 break;
843 case 4:
844 OneFromMiddleDrive(false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700845 if (!WaitForDriveDone()) return true;
846 // Get the superstructure to unfold and get ready for shooting.
847 LOG(INFO, "Unfolding superstructure\n");
848 FrontMiddleShot();
849
850 // Spin up the shooter wheels.
851 LOG(INFO, "Spinning up the shooter wheels\n");
852 SetShooterSpeed(600.0);
853
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700854 break;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700855 case 5:
Campbell Crowley9ed61a52016-11-05 17:13:07 -0700856 case 15:
Austin Schuh3e4a5272016-04-20 20:11:00 -0700857 TwoBallAuto();
Austin Schuh23b21802016-04-03 21:18:56 -0700858 return true;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700859 break;
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700860 case 6:
861 StealAndMoveOverBy(3.10 + 2 * 52 * 2.54 / 100.0);
862 if (ShouldCancel()) return true;
863
864 TwoFromMiddleDrive();
865 if (!WaitForDriveDone()) return true;
866 // Get the superstructure to unfold and get ready for shooting.
867 LOG(INFO, "Unfolding superstructure\n");
868 FrontMiddleShot();
869
870 // Spin up the shooter wheels.
871 LOG(INFO, "Spinning up the shooter wheels\n");
872 SetShooterSpeed(600.0);
873
874 break;
875 case 7:
876 StealAndMoveOverBy(2.95 + 52 * 2.54 / 100.0);
877 if (ShouldCancel()) return true;
878
879 OneFromMiddleDrive(true);
880 if (!WaitForDriveDone()) return true;
881 // Get the superstructure to unfold and get ready for shooting.
882 LOG(INFO, "Unfolding superstructure\n");
883 FrontMiddleShot();
884
885 // Spin up the shooter wheels.
886 LOG(INFO, "Spinning up the shooter wheels\n");
887 SetShooterSpeed(600.0);
888
889 break;
890 case 8: {
891 StealAndMoveOverBy(2.95);
892 if (ShouldCancel()) return true;
893
894 MiddleDrive();
895 if (!WaitForDriveDone()) return true;
896 // Get the superstructure to unfold and get ready for shooting.
897 LOG(INFO, "Unfolding superstructure\n");
898 FrontMiddleShot();
899
900 // Spin up the shooter wheels.
901 LOG(INFO, "Spinning up the shooter wheels\n");
902 SetShooterSpeed(600.0);
903
904 } break;
905 case 9: {
906 StealAndMoveOverBy(1.70);
907 if (ShouldCancel()) return true;
908
909 OneFromMiddleDrive(false);
910 if (!WaitForDriveDone()) return true;
911 // Get the superstructure to unfold and get ready for shooting.
912 LOG(INFO, "Unfolding superstructure\n");
913 FrontMiddleShot();
914
915 // Spin up the shooter wheels.
916 LOG(INFO, "Spinning up the shooter wheels\n");
917 SetShooterSpeed(600.0);
918
919 } break;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700920 default:
Austin Schuh6c9bc622016-03-26 19:44:12 -0700921 LOG(ERROR, "Invalid auto mode %d\n", params.mode);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700922 return true;
923 }
Comran Morshed435f1112016-03-12 14:20:45 +0000924
Austin Schuh3e4a5272016-04-20 20:11:00 -0700925 DoFullShot();
926
927 StartDrive(0.5, 0.0, kMoatDrive, kFastTurn);
Comran Morshed435f1112016-03-12 14:20:45 +0000928 if (!WaitForDriveDone()) return true;
929
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700930 LOG(INFO, "Done %f\n",
931 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Comran Morshed435f1112016-03-12 14:20:45 +0000932
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700933 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700934 event_loop()->monotonic_now(),
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700935 ::std::chrono::milliseconds(5) / 2);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700936
Comran Morshed435f1112016-03-12 14:20:45 +0000937 while (!ShouldCancel()) {
938 phased_loop.SleepUntilNext();
Comran Morshede68e3732016-03-12 14:12:11 +0000939 }
Comran Morshed435f1112016-03-12 14:20:45 +0000940 LOG(DEBUG, "Done running\n");
Comran Morshede68e3732016-03-12 14:12:11 +0000941
942 return true;
943}
944
Comran Morshede68e3732016-03-12 14:12:11 +0000945} // namespace actors
946} // namespace y2016