blob: f2a779a26f39311eaef0f7d005b7b691db3c1f81 [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>(
Austin Schuhae023fb2019-06-29 17:11:45 -070058 ".y2016.sensors.ball_detector")),
59 shooter_goal_sender_(
60 event_loop
61 ->MakeSender<::y2016::control_loops::shooter::ShooterQueue::Goal>(
62 ".y2016.control_loops.shooter.shooter_queue.goal")),
63 shooter_status_fetcher_(
64 event_loop->MakeFetcher<
65 ::y2016::control_loops::shooter::ShooterQueue::Status>(
66 ".y2016.control_loops.shooter.shooter_queue.status")) {}
Comran Morshed435f1112016-03-12 14:20:45 +000067
Austin Schuhe4ec49c2016-04-24 19:07:15 -070068constexpr double kDoNotTurnCare = 2.0;
69
Comran Morshedb134e772016-03-16 21:05:05 +000070void AutonomousActor::MoveSuperstructure(
71 double intake, double shoulder, double wrist,
72 const ProfileParameters intake_params,
73 const ProfileParameters shoulder_params,
Austin Schuh23b21802016-04-03 21:18:56 -070074 const ProfileParameters wrist_params, bool traverse_up,
75 double roller_power) {
Comran Morshedb134e772016-03-16 21:05:05 +000076 superstructure_goal_ = {intake, shoulder, wrist};
77
78 auto new_superstructure_goal =
79 ::y2016::control_loops::superstructure_queue.goal.MakeMessage();
80
81 new_superstructure_goal->angle_intake = intake;
82 new_superstructure_goal->angle_shoulder = shoulder;
83 new_superstructure_goal->angle_wrist = wrist;
84
85 new_superstructure_goal->max_angular_velocity_intake =
86 intake_params.max_velocity;
87 new_superstructure_goal->max_angular_velocity_shoulder =
88 shoulder_params.max_velocity;
89 new_superstructure_goal->max_angular_velocity_wrist =
90 wrist_params.max_velocity;
91
92 new_superstructure_goal->max_angular_acceleration_intake =
93 intake_params.max_acceleration;
94 new_superstructure_goal->max_angular_acceleration_shoulder =
95 shoulder_params.max_acceleration;
96 new_superstructure_goal->max_angular_acceleration_wrist =
97 wrist_params.max_acceleration;
98
Austin Schuh23b21802016-04-03 21:18:56 -070099 new_superstructure_goal->voltage_top_rollers = roller_power;
100 new_superstructure_goal->voltage_bottom_rollers = roller_power;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700101
102 new_superstructure_goal->traverse_unlatched = true;
103 new_superstructure_goal->traverse_down = !traverse_up;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700104 new_superstructure_goal->voltage_climber = 0.0;
105 new_superstructure_goal->unfold_climber = false;
Comran Morshedb134e772016-03-16 21:05:05 +0000106
107 if (!new_superstructure_goal.Send()) {
108 LOG(ERROR, "Sending superstructure goal failed.\n");
109 }
110}
111
Austin Schuh23b21802016-04-03 21:18:56 -0700112void AutonomousActor::OpenShooter() {
113 shooter_speed_ = 0.0;
114
Austin Schuhae023fb2019-06-29 17:11:45 -0700115 auto shooter_goal = shooter_goal_sender_.MakeMessage();
116 shooter_goal->angular_velocity = shooter_speed_;
117 shooter_goal->clamp_open = true;
118 shooter_goal->push_to_shooter = false;
119 shooter_goal->force_lights_on = false;
120 if (!shooter_goal.Send()) {
Austin Schuh23b21802016-04-03 21:18:56 -0700121 LOG(ERROR, "Sending shooter goal failed.\n");
122 }
123}
124
125void AutonomousActor::CloseShooter() {
126 shooter_speed_ = 0.0;
127
Austin Schuhae023fb2019-06-29 17:11:45 -0700128 auto shooter_goal = shooter_goal_sender_.MakeMessage();
129 shooter_goal->angular_velocity = shooter_speed_;
130 shooter_goal->clamp_open = false;
131 shooter_goal->push_to_shooter = false;
132 shooter_goal->force_lights_on = false;
133
134 if (!shooter_goal.Send()) {
Austin Schuh23b21802016-04-03 21:18:56 -0700135 LOG(ERROR, "Sending shooter goal failed.\n");
136 }
137}
138
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700139void AutonomousActor::SetShooterSpeed(double speed) {
140 shooter_speed_ = speed;
141
142 // In auto, we want to have the lights on whenever possible since we have no
143 // hope of a human aligning the robot.
144 bool force_lights_on = shooter_speed_ > 1.0;
145
Austin Schuhae023fb2019-06-29 17:11:45 -0700146 auto shooter_goal = shooter_goal_sender_.MakeMessage();
147 shooter_goal->angular_velocity = shooter_speed_;
148 shooter_goal->clamp_open = false;
149 shooter_goal->push_to_shooter = false;
150 shooter_goal->force_lights_on = force_lights_on;
151
152 if (!shooter_goal.Send()) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700153 LOG(ERROR, "Sending shooter goal failed.\n");
154 }
155}
156
157void AutonomousActor::Shoot() {
158 uint32_t initial_shots = 0;
159
Austin Schuhae023fb2019-06-29 17:11:45 -0700160 shooter_status_fetcher_.Fetch();
161 if (shooter_status_fetcher_.get()) {
162 initial_shots = shooter_status_fetcher_->shots;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700163 }
164
165 // In auto, we want to have the lights on whenever possible since we have no
166 // hope of a human aligning the robot.
167 bool force_lights_on = shooter_speed_ > 1.0;
168
Austin Schuhae023fb2019-06-29 17:11:45 -0700169 auto shooter_goal = shooter_goal_sender_.MakeMessage();
170 shooter_goal->angular_velocity = shooter_speed_;
171 shooter_goal->clamp_open = false;
172 shooter_goal->push_to_shooter = true;
173 shooter_goal->force_lights_on = force_lights_on;
174
175 if (!shooter_goal.Send()) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700176 LOG(ERROR, "Sending shooter goal failed.\n");
177 }
178
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700179 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700180 event_loop()->monotonic_now(),
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700181 ::std::chrono::milliseconds(5) / 2);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700182 while (true) {
183 if (ShouldCancel()) return;
184
185 // Wait for the shot count to change so we know when the shot is complete.
Austin Schuhae023fb2019-06-29 17:11:45 -0700186 shooter_status_fetcher_.Fetch();
187 if (shooter_status_fetcher_.get()) {
188 if (initial_shots < shooter_status_fetcher_->shots) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700189 return;
190 }
191 }
192 phased_loop.SleepUntilNext();
193 }
194}
195
196void AutonomousActor::WaitForShooterSpeed() {
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700197 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700198 event_loop()->monotonic_now(),
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700199 ::std::chrono::milliseconds(5) / 2);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700200 while (true) {
201 if (ShouldCancel()) return;
202
Austin Schuhae023fb2019-06-29 17:11:45 -0700203 shooter_status_fetcher_.Fetch();
204 if (shooter_status_fetcher_.get()) {
205 if (shooter_status_fetcher_->left.ready &&
206 shooter_status_fetcher_->right.ready) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700207 return;
208 }
209 }
210 phased_loop.SleepUntilNext();
211 }
212}
213
214void AutonomousActor::AlignWithVisionGoal() {
215 actors::VisionAlignActionParams params;
Austin Schuh1bf8a212019-05-26 22:13:14 -0700216 vision_action_ = vision_align_actor_factory_.Make(params);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700217 vision_action_->Start();
218}
219
Austin Schuh23b21802016-04-03 21:18:56 -0700220void AutonomousActor::WaitForAlignedWithVision(
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800221 chrono::nanoseconds align_duration) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700222 bool vision_valid = false;
223 double last_angle = 0.0;
224 int ready_to_fire = 0;
225
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700226 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700227 event_loop()->monotonic_now(),
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700228 ::std::chrono::milliseconds(5) / 2);
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800229 monotonic_clock::time_point end_time =
230 monotonic_clock::now() + align_duration;
231 while (end_time > monotonic_clock::now()) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700232 if (ShouldCancel()) break;
233
Austin Schuh28bde302019-05-26 22:24:33 -0700234 vision_status_fetcher_.Fetch();
235 if (vision_status_fetcher_.get()) {
236 vision_valid = (vision_status_fetcher_->left_image_valid &&
237 vision_status_fetcher_->right_image_valid);
238 last_angle = vision_status_fetcher_->horizontal_angle;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700239 }
240
241 drivetrain_queue.status.FetchLatest();
242 drivetrain_queue.goal.FetchLatest();
243
244 if (drivetrain_queue.status.get() && drivetrain_queue.goal.get()) {
245 const double left_goal = drivetrain_queue.goal->left_goal;
246 const double right_goal = drivetrain_queue.goal->right_goal;
247 const double left_current =
248 drivetrain_queue.status->estimated_left_position;
249 const double right_current =
250 drivetrain_queue.status->estimated_right_position;
251 const double left_velocity =
252 drivetrain_queue.status->estimated_left_velocity;
253 const double right_velocity =
254 drivetrain_queue.status->estimated_right_velocity;
255
256 if (vision_valid && ::std::abs(last_angle) < 0.02 &&
257 ::std::abs((left_goal - right_goal) -
258 (left_current - right_current)) /
259 dt_config_.robot_radius / 2.0 <
260 0.02 &&
261 ::std::abs(left_velocity - right_velocity) < 0.01) {
262 ++ready_to_fire;
263 } else {
264 ready_to_fire = 0;
265 }
Austin Schuh3e4a5272016-04-20 20:11:00 -0700266 if (ready_to_fire > 15) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700267 break;
Austin Schuh23b21802016-04-03 21:18:56 -0700268 LOG(INFO, "Vision align success!\n");
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700269 }
270 }
271 phased_loop.SleepUntilNext();
272 }
273
274 vision_action_->Cancel();
275 WaitUntilDoneOrCanceled(::std::move(vision_action_));
Austin Schuh23b21802016-04-03 21:18:56 -0700276 LOG(INFO, "Done waiting for vision\n");
277}
278
279bool AutonomousActor::IntakeDone() {
280 control_loops::superstructure_queue.status.FetchAnother();
281
282 constexpr double kProfileError = 1e-5;
283 constexpr double kEpsilon = 0.15;
284
285 if (control_loops::superstructure_queue.status->state < 12 ||
286 control_loops::superstructure_queue.status->state == 16) {
287 LOG(ERROR, "Superstructure no longer running, aborting action\n");
288 return true;
289 }
290
291 if (::std::abs(control_loops::superstructure_queue.status->intake.goal_angle -
292 superstructure_goal_.intake) < kProfileError &&
293 ::std::abs(control_loops::superstructure_queue.status->intake
294 .goal_angular_velocity) < kProfileError) {
295 LOG(DEBUG, "Profile done.\n");
296 if (::std::abs(control_loops::superstructure_queue.status->intake.angle -
297 superstructure_goal_.intake) < kEpsilon &&
298 ::std::abs(control_loops::superstructure_queue.status->intake
299 .angular_velocity) < kEpsilon) {
300 LOG(INFO, "Near goal, done.\n");
301 return true;
302 }
303 }
304 return false;
305}
306
307bool AutonomousActor::SuperstructureProfileDone() {
308 constexpr double kProfileError = 1e-5;
309 return ::std::abs(
310 control_loops::superstructure_queue.status->intake.goal_angle -
311 superstructure_goal_.intake) < kProfileError &&
312 ::std::abs(
313 control_loops::superstructure_queue.status->shoulder.goal_angle -
314 superstructure_goal_.shoulder) < kProfileError &&
315 ::std::abs(
316 control_loops::superstructure_queue.status->wrist.goal_angle -
317 superstructure_goal_.wrist) < kProfileError &&
318 ::std::abs(control_loops::superstructure_queue.status->intake
319 .goal_angular_velocity) < kProfileError &&
320 ::std::abs(control_loops::superstructure_queue.status->shoulder
321 .goal_angular_velocity) < kProfileError &&
322 ::std::abs(control_loops::superstructure_queue.status->wrist
323 .goal_angular_velocity) < kProfileError;
324}
325
326bool AutonomousActor::SuperstructureDone() {
327 control_loops::superstructure_queue.status.FetchAnother();
328
329 constexpr double kEpsilon = 0.03;
330
331 if (control_loops::superstructure_queue.status->state < 12 ||
332 control_loops::superstructure_queue.status->state == 16) {
333 LOG(ERROR, "Superstructure no longer running, aborting action\n");
334 return true;
335 }
336
337 if (SuperstructureProfileDone()) {
338 LOG(DEBUG, "Profile done.\n");
339 if (::std::abs(control_loops::superstructure_queue.status->intake.angle -
340 superstructure_goal_.intake) < (kEpsilon + 0.1) &&
341 ::std::abs(control_loops::superstructure_queue.status->shoulder.angle -
342 superstructure_goal_.shoulder) < (kEpsilon + 0.05) &&
343 ::std::abs(control_loops::superstructure_queue.status->wrist.angle -
344 superstructure_goal_.wrist) < (kEpsilon + 0.01) &&
345 ::std::abs(control_loops::superstructure_queue.status->intake
346 .angular_velocity) < (kEpsilon + 0.1) &&
347 ::std::abs(control_loops::superstructure_queue.status->shoulder
348 .angular_velocity) < (kEpsilon + 0.10) &&
349 ::std::abs(control_loops::superstructure_queue.status->wrist
350 .angular_velocity) < (kEpsilon + 0.05)) {
351 LOG(INFO, "Near goal, done.\n");
352 return true;
353 }
354 }
355 return false;
356}
357
358void AutonomousActor::WaitForIntake() {
359 while (true) {
360 if (ShouldCancel()) return;
361 if (IntakeDone()) return;
362 }
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700363}
364
Comran Morshedb134e772016-03-16 21:05:05 +0000365void AutonomousActor::WaitForSuperstructure() {
366 while (true) {
367 if (ShouldCancel()) return;
Austin Schuh23b21802016-04-03 21:18:56 -0700368 if (SuperstructureDone()) return;
369 }
370}
Comran Morshedb134e772016-03-16 21:05:05 +0000371
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700372void AutonomousActor::WaitForSuperstructureProfile() {
373 while (true) {
374 if (ShouldCancel()) return;
375 control_loops::superstructure_queue.status.FetchAnother();
376
377 if (control_loops::superstructure_queue.status->state < 12 ||
378 control_loops::superstructure_queue.status->state == 16) {
379 LOG(ERROR, "Superstructure no longer running, aborting action\n");
380 return;
381 }
382
383 if (SuperstructureProfileDone()) return;
384 }
385}
386
Austin Schuh23b21802016-04-03 21:18:56 -0700387void AutonomousActor::WaitForSuperstructureLow() {
388 while (true) {
389 if (ShouldCancel()) return;
390 control_loops::superstructure_queue.status.FetchAnother();
Comran Morshedb134e772016-03-16 21:05:05 +0000391
392 if (control_loops::superstructure_queue.status->state < 12 ||
393 control_loops::superstructure_queue.status->state == 16) {
394 LOG(ERROR, "Superstructure no longer running, aborting action\n");
395 return;
396 }
Austin Schuh23b21802016-04-03 21:18:56 -0700397 if (SuperstructureProfileDone()) return;
398 if (control_loops::superstructure_queue.status->shoulder.angle < 0.1) {
399 return;
Comran Morshedb134e772016-03-16 21:05:05 +0000400 }
401 }
402}
Austin Schuh23b21802016-04-03 21:18:56 -0700403void AutonomousActor::BackLongShotLowBarTwoBall() {
404 LOG(INFO, "Expanding for back long shot\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700405 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 -0700406 {10.0, 25.0}, false, 0.0);
407}
408
409void AutonomousActor::BackLongShotTwoBall() {
410 LOG(INFO, "Expanding for back long shot\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700411 MoveSuperstructure(0.00, M_PI / 2.0 - 0.2, -0.55, {7.0, 40.0}, {4.0, 6.0},
412 {10.0, 25.0}, false, 0.0);
413}
414
415void AutonomousActor::BackLongShotTwoBallFinish() {
416 LOG(INFO, "Expanding for back long shot\n");
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000417 MoveSuperstructure(0.00, M_PI / 2.0 - 0.2, -0.625 + 0.03, {7.0, 40.0},
418 {4.0, 6.0}, {10.0, 25.0}, false, 0.0);
Austin Schuh23b21802016-04-03 21:18:56 -0700419}
420
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700421void AutonomousActor::BackLongShot() {
422 LOG(INFO, "Expanding for back long shot\n");
Austin Schuh23b21802016-04-03 21:18:56 -0700423 MoveSuperstructure(0.80, M_PI / 2.0 - 0.2, -0.62, {7.0, 40.0}, {4.0, 6.0},
424 {10.0, 25.0}, false, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700425}
426
427void AutonomousActor::BackMiddleShot() {
428 LOG(INFO, "Expanding for back middle shot\n");
429 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 -0700430 {10.0, 25.0}, false, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700431}
432
Austin Schuh3e4a5272016-04-20 20:11:00 -0700433void AutonomousActor::FrontLongShot() {
434 LOG(INFO, "Expanding for front long shot\n");
435 MoveSuperstructure(0.80, M_PI / 2.0 + 0.1, M_PI + 0.41 + 0.02, {7.0, 40.0},
436 {4.0, 6.0}, {10.0, 25.0}, false, 0.0);
437}
438
439void AutonomousActor::FrontMiddleShot() {
440 LOG(INFO, "Expanding for front middle shot\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700441 MoveSuperstructure(-0.05, M_PI / 2.0 + 0.1, M_PI + 0.44, {7.0, 40.0},
Austin Schuh3e4a5272016-04-20 20:11:00 -0700442 {4.0, 10.0}, {10.0, 25.0}, true, 0.0);
443}
444
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700445void AutonomousActor::TuckArm(bool low_bar, bool traverse_down) {
446 MoveSuperstructure(low_bar ? -0.05 : 2.0, -0.010, 0.0, {7.0, 40.0},
Austin Schuh23b21802016-04-03 21:18:56 -0700447 {4.0, 10.0}, {10.0, 25.0}, !traverse_down, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700448}
449
Austin Schuh3e4a5272016-04-20 20:11:00 -0700450void AutonomousActor::DoFullShot() {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700451 if (ShouldCancel()) return;
452 // Make sure that the base is aligned with the base.
453 LOG(INFO, "Waiting for the superstructure\n");
454 WaitForSuperstructure();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700455
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800456 this_thread::sleep_for(chrono::milliseconds(500));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700457
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700458 if (ShouldCancel()) return;
459 LOG(INFO, "Triggering the vision actor\n");
460 AlignWithVisionGoal();
461
462 // Wait for the drive base to be aligned with the target and make sure that
463 // the shooter is up to speed.
464 LOG(INFO, "Waiting for vision to be aligned\n");
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800465 WaitForAlignedWithVision(chrono::milliseconds(2000));
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700466 if (ShouldCancel()) return;
467 LOG(INFO, "Waiting for shooter to be up to speed\n");
468 WaitForShooterSpeed();
469 if (ShouldCancel()) return;
470
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800471 this_thread::sleep_for(chrono::milliseconds(300));
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700472 LOG(INFO, "Shoot!\n");
473 Shoot();
474
475 // Turn off the shooter and fold up the superstructure.
476 if (ShouldCancel()) return;
477 LOG(INFO, "Stopping shooter\n");
478 SetShooterSpeed(0.0);
479 LOG(INFO, "Folding superstructure back down\n");
480 TuckArm(false, false);
481
482 // Wait for everything to be folded up.
483 LOG(INFO, "Waiting for superstructure to be folded back down\n");
Austin Schuh3e4a5272016-04-20 20:11:00 -0700484 WaitForSuperstructureLow();
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700485}
486
487void AutonomousActor::LowBarDrive() {
488 TuckArm(false, true);
489 StartDrive(-5.5, 0.0, kLowBarDrive, kSlowTurn);
490
491 if (!WaitForDriveNear(5.3, 0.0)) return;
492 TuckArm(true, true);
493
494 if (!WaitForDriveNear(5.0, 0.0)) return;
495
496 StartDrive(0.0, 0.0, kLowBarDrive, kSlowTurn);
497
498 if (!WaitForDriveNear(3.0, 0.0)) return;
499
500 StartDrive(0.0, 0.0, kLowBarDrive, kSlowTurn);
501
502 if (!WaitForDriveNear(1.0, 0.0)) return;
503
Austin Schuh15b5f6a2016-03-26 19:43:56 -0700504 StartDrive(0, -M_PI / 4.0 - 0.2, kLowBarDrive, kSlowTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700505}
506
Austin Schuh3e4a5272016-04-20 20:11:00 -0700507void AutonomousActor::TippyDrive(double goal_distance, double tip_distance,
508 double below, double above) {
509 StartDrive(goal_distance, 0.0, kMoatDrive, kSlowTurn);
510 if (!WaitForBelowAngle(below)) return;
511 if (!WaitForAboveAngle(above)) return;
512 // Ok, we are good now. Compensate by moving the goal by the error.
513 // Should be here at 2.7
514 drivetrain_queue.status.FetchLatest();
515 if (drivetrain_queue.status.get()) {
516 const double left_error =
517 (initial_drivetrain_.left -
518 drivetrain_queue.status->estimated_left_position);
519 const double right_error =
520 (initial_drivetrain_.right -
521 drivetrain_queue.status->estimated_right_position);
522 const double distance_to_go = (left_error + right_error) / 2.0;
523 const double distance_compensation =
524 goal_distance - tip_distance - distance_to_go;
525 LOG(INFO, "Going %f further at the bump\n", distance_compensation);
526 StartDrive(distance_compensation, 0.0, kMoatDrive, kSlowTurn);
527 }
528}
529
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700530void AutonomousActor::MiddleDrive() {
531 TuckArm(false, false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700532 TippyDrive(3.65, 2.7, -0.2, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700533 if (!WaitForDriveDone()) return;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700534}
535
536void AutonomousActor::OneFromMiddleDrive(bool left) {
Austin Schuh3e4a5272016-04-20 20:11:00 -0700537 const double kTurnAngle = left ? -0.41 : 0.41;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700538 TuckArm(false, false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700539 TippyDrive(4.05, 2.7, -0.2, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700540
541 if (!WaitForDriveDone()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700542 StartDrive(0.0, kTurnAngle, kRealignDrive, kFastTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700543}
544
545void AutonomousActor::TwoFromMiddleDrive() {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700546 TuckArm(false, false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700547 constexpr double kDriveDistance = 5.10;
548 TippyDrive(kDriveDistance, 2.7, -0.2, 0.0);
549
550 if (!WaitForDriveNear(kDriveDistance - 3.0, 2.0)) return;
551 StartDrive(0, -M_PI / 2 - 0.10, kMoatDrive, kFastTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700552
553 if (!WaitForDriveDone()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700554 StartDrive(0, M_PI / 3 + 0.35, kMoatDrive, kFastTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700555}
Comran Morshedb134e772016-03-16 21:05:05 +0000556
Austin Schuh23b21802016-04-03 21:18:56 -0700557void AutonomousActor::CloseIfBall() {
Austin Schuh4b652c92019-05-27 13:22:27 -0700558 ball_detector_fetcher_.Fetch();
559 if (ball_detector_fetcher_.get()) {
560 const bool ball_detected = ball_detector_fetcher_->voltage > 2.5;
Austin Schuh23b21802016-04-03 21:18:56 -0700561 if (ball_detected) {
562 CloseShooter();
563 }
564 }
565}
566
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700567void AutonomousActor::WaitForBallOrDriveDone() {
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700568 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700569 event_loop()->monotonic_now(),
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700570 ::std::chrono::milliseconds(5) / 2);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700571 while (true) {
572 if (ShouldCancel()) {
573 return;
574 }
575 phased_loop.SleepUntilNext();
576 drivetrain_queue.status.FetchLatest();
577 if (IsDriveDone()) {
578 return;
579 }
580
Austin Schuh4b652c92019-05-27 13:22:27 -0700581 ball_detector_fetcher_.Fetch();
582 if (ball_detector_fetcher_.get()) {
583 const bool ball_detected = ball_detector_fetcher_->voltage > 2.5;
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700584 if (ball_detected) {
585 return;
586 }
587 }
588 }
589}
590
Austin Schuh3e4a5272016-04-20 20:11:00 -0700591void AutonomousActor::TwoBallAuto() {
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800592 monotonic_clock::time_point start_time = monotonic_clock::now();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700593 OpenShooter();
594 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
595 false, 12.0);
596 if (ShouldCancel()) return;
597 LOG(INFO, "Waiting for the intake to come down.\n");
598
599 WaitForIntake();
600 LOG(INFO, "Intake done at %f seconds, starting to drive\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700601 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700602 if (ShouldCancel()) return;
603 const double kDriveDistance = 5.05;
604 StartDrive(-kDriveDistance, 0.0, kTwoBallLowDrive, kSlowTurn);
605
606 StartDrive(0.0, 0.4, kTwoBallLowDrive, kSwerveTurn);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700607 if (!WaitForDriveNear(kDriveDistance - 0.5, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700608
Austin Schuh295c2d92016-05-01 12:28:04 -0700609 // Check if the ball is there.
610 bool first_ball_there = true;
Austin Schuh4b652c92019-05-27 13:22:27 -0700611 ball_detector_fetcher_.Fetch();
612 if (ball_detector_fetcher_.get()) {
613 const bool ball_detected = ball_detector_fetcher_->voltage > 2.5;
Austin Schuh295c2d92016-05-01 12:28:04 -0700614 first_ball_there = ball_detected;
615 LOG(INFO, "Saw the ball: %d at %f\n", first_ball_there,
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700616 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh295c2d92016-05-01 12:28:04 -0700617 }
Austin Schuh3e4a5272016-04-20 20:11:00 -0700618 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 40.0}, {4.0, 10.0}, {10.0, 25.0},
619 false, 0.0);
620 LOG(INFO, "Shutting off rollers at %f seconds, starting to straighten out\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700621 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700622 StartDrive(0.0, -0.4, kTwoBallLowDrive, kSwerveTurn);
623 MoveSuperstructure(-0.05, -0.010, 0.0, {8.0, 40.0}, {4.0, 10.0}, {10.0, 25.0},
624 false, 0.0);
625 CloseShooter();
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700626 if (!WaitForDriveNear(kDriveDistance - 2.4, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700627
628 // We are now under the low bar. Start lifting.
629 BackLongShotLowBarTwoBall();
630 LOG(INFO, "Spinning up the shooter wheels\n");
631 SetShooterSpeed(640.0);
632 StartDrive(0.0, 0.0, kTwoBallFastDrive, kSwerveTurn);
633
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700634 if (!WaitForDriveNear(1.50, kDoNotTurnCare)) return;
635 constexpr double kShootTurnAngle = -M_PI / 4.0 - 0.05;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700636 StartDrive(0, kShootTurnAngle, kTwoBallFastDrive, kFinishTurn);
637 BackLongShotTwoBall();
638
639 if (!WaitForDriveDone()) return;
640 LOG(INFO, "First shot done driving at %f seconds\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700641 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700642
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700643 WaitForSuperstructureProfile();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700644
645 if (ShouldCancel()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700646 AlignWithVisionGoal();
647
648 WaitForShooterSpeed();
649 if (ShouldCancel()) return;
650
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800651 constexpr chrono::milliseconds kVisionExtra{0};
652 WaitForAlignedWithVision(chrono::milliseconds(500) + kVisionExtra);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700653 BackLongShotTwoBallFinish();
654 WaitForSuperstructureProfile();
655 if (ShouldCancel()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700656 LOG(INFO, "Shoot!\n");
Austin Schuh295c2d92016-05-01 12:28:04 -0700657 if (first_ball_there) {
658 Shoot();
659 } else {
660 LOG(INFO, "Nah, not shooting\n");
661 }
Austin Schuh3e4a5272016-04-20 20:11:00 -0700662
663 LOG(INFO, "First shot at %f seconds\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700664 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700665 if (ShouldCancel()) return;
666
667 SetShooterSpeed(0.0);
668 LOG(INFO, "Folding superstructure back down\n");
669 TuckArm(true, true);
670
671 // Undo vision move.
672 StartDrive(0.0, 0.0, kTwoBallFastDrive, kFinishTurn);
673 if (!WaitForDriveDone()) return;
674
675 constexpr double kBackDrive = 3.09 - 0.4;
676 StartDrive(kBackDrive, 0.0, kTwoBallReturnDrive, kSlowTurn);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700677 if (!WaitForDriveNear(kBackDrive - 0.19, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700678 StartDrive(0, -kShootTurnAngle, kTwoBallReturnDrive, kSwerveTurn);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700679 if (!WaitForDriveNear(1.0, kDoNotTurnCare)) return;
680 StartDrive(0, 0, kTwoBallReturnSlow, kSwerveTurn);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700681
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700682 if (!WaitForDriveNear(0.06, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700683 LOG(INFO, "At Low Bar %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700684 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700685
686 OpenShooter();
687 constexpr double kSecondBallAfterBarDrive = 2.10;
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700688 StartDrive(kSecondBallAfterBarDrive, 0.0, kTwoBallBallPickupAccel, kSlowTurn);
689 if (!WaitForDriveNear(kSecondBallAfterBarDrive - 0.5, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700690 constexpr double kBallSmallWallTurn = -0.11;
691 StartDrive(0, kBallSmallWallTurn, kTwoBallBallPickup, kFinishTurn);
692
693 MoveSuperstructure(0.03, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
694 false, 12.0);
695
696 if (!WaitForDriveProfileDone()) return;
697
698 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
699 false, 12.0);
700
701 LOG(INFO, "Done backing up %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700702 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700703
704 constexpr double kDriveBackDistance = 5.15 - 0.4;
705 StartDrive(-kDriveBackDistance, 0.0, kTwoBallLowDrive, kFinishTurn);
706 CloseIfBall();
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700707 if (!WaitForDriveNear(kDriveBackDistance - 0.75, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700708
709 StartDrive(0.0, -kBallSmallWallTurn, kTwoBallLowDrive, kFinishTurn);
710 LOG(INFO, "Straightening up at %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
713 CloseIfBall();
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700714 if (!WaitForDriveNear(kDriveBackDistance - 2.3, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700715
Austin Schuh4b652c92019-05-27 13:22:27 -0700716 ball_detector_fetcher_.Fetch();
717 if (ball_detector_fetcher_.get()) {
718 const bool ball_detected = ball_detector_fetcher_->voltage > 2.5;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700719 if (!ball_detected) {
720 if (!WaitForDriveDone()) return;
721 LOG(INFO, "Aborting, no ball %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700722 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700723 return;
724 }
725 }
726 CloseShooter();
727
728 BackLongShotLowBarTwoBall();
729 LOG(INFO, "Spinning up the shooter wheels\n");
730 SetShooterSpeed(640.0);
731 StartDrive(0.0, 0.0, kTwoBallFastDrive, kSwerveTurn);
732
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700733 if (!WaitForDriveNear(1.80, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700734 StartDrive(0, kShootTurnAngle, kTwoBallFastDrive, kFinishTurn);
735 BackLongShotTwoBall();
736
737 if (!WaitForDriveDone()) return;
738 LOG(INFO, "Second shot done driving at %f seconds\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700739 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700740 WaitForSuperstructure();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700741 AlignWithVisionGoal();
742 if (ShouldCancel()) return;
743
744 WaitForShooterSpeed();
745 if (ShouldCancel()) return;
746
747 // 2.2 with 0.4 of vision.
748 // 1.8 without any vision.
749 LOG(INFO, "Going to vision align at %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700750 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800751 WaitForAlignedWithVision(
752 (start_time + chrono::milliseconds(13500) + kVisionExtra * 2) -
753 monotonic_clock::now());
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700754 BackLongShotTwoBallFinish();
755 WaitForSuperstructureProfile();
756 if (ShouldCancel()) return;
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800757 LOG(INFO, "Shoot at %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700758 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700759 Shoot();
760
761 LOG(INFO, "Second shot at %f seconds\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700762 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700763 if (ShouldCancel()) return;
764
765 SetShooterSpeed(0.0);
766 LOG(INFO, "Folding superstructure back down\n");
767 TuckArm(true, false);
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700768 LOG(INFO, "Shot %f\n",
769 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700770
771 WaitForSuperstructureLow();
772
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700773 LOG(INFO, "Done %f\n",
774 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700775}
776
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700777void AutonomousActor::StealAndMoveOverBy(double distance) {
778 OpenShooter();
779 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
780 true, 12.0);
781 if (ShouldCancel()) return;
782 LOG(INFO, "Waiting for the intake to come down.\n");
783
784 WaitForIntake();
785 if (ShouldCancel()) return;
786 StartDrive(-distance, M_PI / 2.0, kFastDrive, kStealTurn);
787 WaitForBallOrDriveDone();
788 if (ShouldCancel()) return;
789 MoveSuperstructure(1.0, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
790 true, 12.0);
791
792 if (!WaitForDriveDone()) return;
793 StartDrive(0.0, M_PI / 2.0, kFastDrive, kStealTurn);
794 if (!WaitForDriveDone()) return;
795}
796
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000797bool AutonomousActor::RunAction(
798 const ::frc971::autonomous::AutonomousActionParams &params) {
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800799 monotonic_clock::time_point start_time = monotonic_clock::now();
Comran Morshede68e3732016-03-12 14:12:11 +0000800 LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode);
801
Comran Morshed435f1112016-03-12 14:20:45 +0000802 InitializeEncoders();
803 ResetDrivetrain();
804
Austin Schuh295c2d92016-05-01 12:28:04 -0700805 switch (params.mode) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700806 case 0:
807 LowBarDrive();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700808 if (!WaitForDriveDone()) return true;
809 // Get the superstructure to unfold and get ready for shooting.
810 LOG(INFO, "Unfolding superstructure\n");
811 FrontLongShot();
812
813 // Spin up the shooter wheels.
814 LOG(INFO, "Spinning up the shooter wheels\n");
815 SetShooterSpeed(640.0);
816
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700817 break;
818 case 1:
819 TwoFromMiddleDrive();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700820 if (!WaitForDriveDone()) return true;
821 // Get the superstructure to unfold and get ready for shooting.
822 LOG(INFO, "Unfolding superstructure\n");
823 FrontMiddleShot();
824
825 // Spin up the shooter wheels.
826 LOG(INFO, "Spinning up the shooter wheels\n");
827 SetShooterSpeed(600.0);
828
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700829 break;
830 case 2:
831 OneFromMiddleDrive(true);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700832 if (!WaitForDriveDone()) return true;
833 // Get the superstructure to unfold and get ready for shooting.
834 LOG(INFO, "Unfolding superstructure\n");
835 FrontMiddleShot();
836
837 // Spin up the shooter wheels.
838 LOG(INFO, "Spinning up the shooter wheels\n");
839 SetShooterSpeed(600.0);
840
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700841 break;
842 case 3:
843 MiddleDrive();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700844 if (!WaitForDriveDone()) return true;
845 // Get the superstructure to unfold and get ready for shooting.
846 LOG(INFO, "Unfolding superstructure\n");
847 FrontMiddleShot();
848
849 // Spin up the shooter wheels.
850 LOG(INFO, "Spinning up the shooter wheels\n");
851 SetShooterSpeed(600.0);
852
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700853 break;
854 case 4:
855 OneFromMiddleDrive(false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700856 if (!WaitForDriveDone()) return true;
857 // Get the superstructure to unfold and get ready for shooting.
858 LOG(INFO, "Unfolding superstructure\n");
859 FrontMiddleShot();
860
861 // Spin up the shooter wheels.
862 LOG(INFO, "Spinning up the shooter wheels\n");
863 SetShooterSpeed(600.0);
864
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700865 break;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700866 case 5:
Campbell Crowley9ed61a52016-11-05 17:13:07 -0700867 case 15:
Austin Schuh3e4a5272016-04-20 20:11:00 -0700868 TwoBallAuto();
Austin Schuh23b21802016-04-03 21:18:56 -0700869 return true;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700870 break;
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700871 case 6:
872 StealAndMoveOverBy(3.10 + 2 * 52 * 2.54 / 100.0);
873 if (ShouldCancel()) return true;
874
875 TwoFromMiddleDrive();
876 if (!WaitForDriveDone()) return true;
877 // Get the superstructure to unfold and get ready for shooting.
878 LOG(INFO, "Unfolding superstructure\n");
879 FrontMiddleShot();
880
881 // Spin up the shooter wheels.
882 LOG(INFO, "Spinning up the shooter wheels\n");
883 SetShooterSpeed(600.0);
884
885 break;
886 case 7:
887 StealAndMoveOverBy(2.95 + 52 * 2.54 / 100.0);
888 if (ShouldCancel()) return true;
889
890 OneFromMiddleDrive(true);
891 if (!WaitForDriveDone()) return true;
892 // Get the superstructure to unfold and get ready for shooting.
893 LOG(INFO, "Unfolding superstructure\n");
894 FrontMiddleShot();
895
896 // Spin up the shooter wheels.
897 LOG(INFO, "Spinning up the shooter wheels\n");
898 SetShooterSpeed(600.0);
899
900 break;
901 case 8: {
902 StealAndMoveOverBy(2.95);
903 if (ShouldCancel()) return true;
904
905 MiddleDrive();
906 if (!WaitForDriveDone()) return true;
907 // Get the superstructure to unfold and get ready for shooting.
908 LOG(INFO, "Unfolding superstructure\n");
909 FrontMiddleShot();
910
911 // Spin up the shooter wheels.
912 LOG(INFO, "Spinning up the shooter wheels\n");
913 SetShooterSpeed(600.0);
914
915 } break;
916 case 9: {
917 StealAndMoveOverBy(1.70);
918 if (ShouldCancel()) return true;
919
920 OneFromMiddleDrive(false);
921 if (!WaitForDriveDone()) return true;
922 // Get the superstructure to unfold and get ready for shooting.
923 LOG(INFO, "Unfolding superstructure\n");
924 FrontMiddleShot();
925
926 // Spin up the shooter wheels.
927 LOG(INFO, "Spinning up the shooter wheels\n");
928 SetShooterSpeed(600.0);
929
930 } break;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700931 default:
Austin Schuh6c9bc622016-03-26 19:44:12 -0700932 LOG(ERROR, "Invalid auto mode %d\n", params.mode);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700933 return true;
934 }
Comran Morshed435f1112016-03-12 14:20:45 +0000935
Austin Schuh3e4a5272016-04-20 20:11:00 -0700936 DoFullShot();
937
938 StartDrive(0.5, 0.0, kMoatDrive, kFastTurn);
Comran Morshed435f1112016-03-12 14:20:45 +0000939 if (!WaitForDriveDone()) return true;
940
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700941 LOG(INFO, "Done %f\n",
942 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Comran Morshed435f1112016-03-12 14:20:45 +0000943
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700944 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700945 event_loop()->monotonic_now(),
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700946 ::std::chrono::milliseconds(5) / 2);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700947
Comran Morshed435f1112016-03-12 14:20:45 +0000948 while (!ShouldCancel()) {
949 phased_loop.SleepUntilNext();
Comran Morshede68e3732016-03-12 14:12:11 +0000950 }
Comran Morshed435f1112016-03-12 14:20:45 +0000951 LOG(DEBUG, "Done running\n");
Comran Morshede68e3732016-03-12 14:12:11 +0000952
953 return true;
954}
955
Comran Morshede68e3732016-03-12 14:12:11 +0000956} // namespace actors
957} // namespace y2016