blob: 498a6f17bc4909fde722bb6029db6e80b192492e [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;
21namespace chrono = ::std::chrono;
22namespace this_thread = ::std::this_thread;
Comran Morshed435f1112016-03-12 14:20:45 +000023
24namespace {
Austin Schuhf59b8ee2016-03-19 21:31:36 -070025const ProfileParameters kSlowDrive = {0.8, 2.5};
26const ProfileParameters kLowBarDrive = {1.3, 2.5};
27const ProfileParameters kMoatDrive = {1.2, 3.5};
28const ProfileParameters kRealignDrive = {2.0, 2.5};
29const ProfileParameters kRockWallDrive = {0.8, 2.5};
Comran Morshed435f1112016-03-12 14:20:45 +000030const ProfileParameters kFastDrive = {3.0, 2.5};
31
Austin Schuhf59b8ee2016-03-19 21:31:36 -070032const ProfileParameters kSlowTurn = {0.8, 3.0};
Comran Morshed435f1112016-03-12 14:20:45 +000033const ProfileParameters kFastTurn = {3.0, 10.0};
Austin Schuhe4ec49c2016-04-24 19:07:15 -070034const ProfileParameters kStealTurn = {4.0, 15.0};
Austin Schuh23b21802016-04-03 21:18:56 -070035const ProfileParameters kSwerveTurn = {2.0, 7.0};
36const ProfileParameters kFinishTurn = {2.0, 5.0};
37
38const ProfileParameters kTwoBallLowDrive = {1.7, 3.5};
39const ProfileParameters kTwoBallFastDrive = {3.0, 1.5};
40const ProfileParameters kTwoBallReturnDrive = {3.0, 1.9};
Austin Schuhe4ec49c2016-04-24 19:07:15 -070041const ProfileParameters kTwoBallReturnSlow = {3.0, 2.5};
Austin Schuh23b21802016-04-03 21:18:56 -070042const ProfileParameters kTwoBallBallPickup = {2.0, 1.75};
Austin Schuhe4ec49c2016-04-24 19:07:15 -070043const ProfileParameters kTwoBallBallPickupAccel = {2.0, 2.5};
Austin Schuhf2a50ba2016-12-24 16:16:26 -080044
Comran Morshed435f1112016-03-12 14:20:45 +000045} // namespace
Comran Morshede68e3732016-03-12 14:12:11 +000046
Austin Schuh1bf8a212019-05-26 22:13:14 -070047AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
Philipp Schrader4bd29b12017-02-22 04:42:27 +000048 : frc971::autonomous::BaseAutonomousActor(
Austin Schuh1bf8a212019-05-26 22:13:14 -070049 event_loop, control_loops::drivetrain::GetDrivetrainConfig()),
50 vision_align_actor_factory_(
Austin Schuh28bde302019-05-26 22:24:33 -070051 actors::VisionAlignActor::MakeFactory(event_loop)),
52 vision_status_fetcher_(
53 event_loop->MakeFetcher<::y2016::vision::VisionStatus>(
Austin Schuh4b652c92019-05-27 13:22:27 -070054 ".y2016.vision.vision_status")),
55 ball_detector_fetcher_(
56 event_loop->MakeFetcher<::y2016::sensors::BallDetector>(
Austin Schuhae023fb2019-06-29 17:11:45 -070057 ".y2016.sensors.ball_detector")),
58 shooter_goal_sender_(
59 event_loop
60 ->MakeSender<::y2016::control_loops::shooter::ShooterQueue::Goal>(
61 ".y2016.control_loops.shooter.shooter_queue.goal")),
62 shooter_status_fetcher_(
63 event_loop->MakeFetcher<
64 ::y2016::control_loops::shooter::ShooterQueue::Status>(
Austin Schuh9481d0d2019-06-29 21:56:17 -070065 ".y2016.control_loops.shooter.shooter_queue.status")),
66 superstructure_status_fetcher_(
67 event_loop->MakeFetcher<
68 ::y2016::control_loops::SuperstructureQueue::Status>(
69 ".y2016.control_loops.superstructure_queue.status")),
70 superstructure_goal_sender_(
71 event_loop
72 ->MakeSender<::y2016::control_loops::SuperstructureQueue::Goal>(
73 ".y2016.control_loops.superstructure_queue.goal")) {}
Comran Morshed435f1112016-03-12 14:20:45 +000074
Austin Schuhe4ec49c2016-04-24 19:07:15 -070075constexpr double kDoNotTurnCare = 2.0;
76
Comran Morshedb134e772016-03-16 21:05:05 +000077void AutonomousActor::MoveSuperstructure(
78 double intake, double shoulder, double wrist,
79 const ProfileParameters intake_params,
80 const ProfileParameters shoulder_params,
Austin Schuh23b21802016-04-03 21:18:56 -070081 const ProfileParameters wrist_params, bool traverse_up,
82 double roller_power) {
Comran Morshedb134e772016-03-16 21:05:05 +000083 superstructure_goal_ = {intake, shoulder, wrist};
84
Austin Schuh9481d0d2019-06-29 21:56:17 -070085 auto new_superstructure_goal = superstructure_goal_sender_.MakeMessage();
Comran Morshedb134e772016-03-16 21:05:05 +000086
87 new_superstructure_goal->angle_intake = intake;
88 new_superstructure_goal->angle_shoulder = shoulder;
89 new_superstructure_goal->angle_wrist = wrist;
90
91 new_superstructure_goal->max_angular_velocity_intake =
92 intake_params.max_velocity;
93 new_superstructure_goal->max_angular_velocity_shoulder =
94 shoulder_params.max_velocity;
95 new_superstructure_goal->max_angular_velocity_wrist =
96 wrist_params.max_velocity;
97
98 new_superstructure_goal->max_angular_acceleration_intake =
99 intake_params.max_acceleration;
100 new_superstructure_goal->max_angular_acceleration_shoulder =
101 shoulder_params.max_acceleration;
102 new_superstructure_goal->max_angular_acceleration_wrist =
103 wrist_params.max_acceleration;
104
Austin Schuh23b21802016-04-03 21:18:56 -0700105 new_superstructure_goal->voltage_top_rollers = roller_power;
106 new_superstructure_goal->voltage_bottom_rollers = roller_power;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700107
108 new_superstructure_goal->traverse_unlatched = true;
109 new_superstructure_goal->traverse_down = !traverse_up;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700110 new_superstructure_goal->voltage_climber = 0.0;
111 new_superstructure_goal->unfold_climber = false;
Comran Morshedb134e772016-03-16 21:05:05 +0000112
113 if (!new_superstructure_goal.Send()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700114 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
Comran Morshedb134e772016-03-16 21:05:05 +0000115 }
116}
117
Austin Schuh23b21802016-04-03 21:18:56 -0700118void AutonomousActor::OpenShooter() {
119 shooter_speed_ = 0.0;
120
Austin Schuhae023fb2019-06-29 17:11:45 -0700121 auto shooter_goal = shooter_goal_sender_.MakeMessage();
122 shooter_goal->angular_velocity = shooter_speed_;
123 shooter_goal->clamp_open = true;
124 shooter_goal->push_to_shooter = false;
125 shooter_goal->force_lights_on = false;
126 if (!shooter_goal.Send()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700127 AOS_LOG(ERROR, "Sending shooter goal failed.\n");
Austin Schuh23b21802016-04-03 21:18:56 -0700128 }
129}
130
131void AutonomousActor::CloseShooter() {
132 shooter_speed_ = 0.0;
133
Austin Schuhae023fb2019-06-29 17:11:45 -0700134 auto shooter_goal = shooter_goal_sender_.MakeMessage();
135 shooter_goal->angular_velocity = shooter_speed_;
136 shooter_goal->clamp_open = false;
137 shooter_goal->push_to_shooter = false;
138 shooter_goal->force_lights_on = false;
139
140 if (!shooter_goal.Send()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700141 AOS_LOG(ERROR, "Sending shooter goal failed.\n");
Austin Schuh23b21802016-04-03 21:18:56 -0700142 }
143}
144
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700145void AutonomousActor::SetShooterSpeed(double speed) {
146 shooter_speed_ = speed;
147
148 // In auto, we want to have the lights on whenever possible since we have no
149 // hope of a human aligning the robot.
150 bool force_lights_on = shooter_speed_ > 1.0;
151
Austin Schuhae023fb2019-06-29 17:11:45 -0700152 auto shooter_goal = shooter_goal_sender_.MakeMessage();
153 shooter_goal->angular_velocity = shooter_speed_;
154 shooter_goal->clamp_open = false;
155 shooter_goal->push_to_shooter = false;
156 shooter_goal->force_lights_on = force_lights_on;
157
158 if (!shooter_goal.Send()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700159 AOS_LOG(ERROR, "Sending shooter goal failed.\n");
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700160 }
161}
162
163void AutonomousActor::Shoot() {
164 uint32_t initial_shots = 0;
165
Austin Schuhae023fb2019-06-29 17:11:45 -0700166 shooter_status_fetcher_.Fetch();
167 if (shooter_status_fetcher_.get()) {
168 initial_shots = shooter_status_fetcher_->shots;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700169 }
170
171 // In auto, we want to have the lights on whenever possible since we have no
172 // hope of a human aligning the robot.
173 bool force_lights_on = shooter_speed_ > 1.0;
174
Austin Schuhae023fb2019-06-29 17:11:45 -0700175 auto shooter_goal = shooter_goal_sender_.MakeMessage();
176 shooter_goal->angular_velocity = shooter_speed_;
177 shooter_goal->clamp_open = false;
178 shooter_goal->push_to_shooter = true;
179 shooter_goal->force_lights_on = force_lights_on;
180
181 if (!shooter_goal.Send()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700182 AOS_LOG(ERROR, "Sending shooter goal failed.\n");
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700183 }
184
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700185 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700186 event_loop()->monotonic_now(),
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700187 ::std::chrono::milliseconds(5) / 2);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700188 while (true) {
189 if (ShouldCancel()) return;
190
191 // Wait for the shot count to change so we know when the shot is complete.
Austin Schuhae023fb2019-06-29 17:11:45 -0700192 shooter_status_fetcher_.Fetch();
193 if (shooter_status_fetcher_.get()) {
194 if (initial_shots < shooter_status_fetcher_->shots) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700195 return;
196 }
197 }
198 phased_loop.SleepUntilNext();
199 }
200}
201
202void AutonomousActor::WaitForShooterSpeed() {
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700203 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700204 event_loop()->monotonic_now(),
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700205 ::std::chrono::milliseconds(5) / 2);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700206 while (true) {
207 if (ShouldCancel()) return;
208
Austin Schuhae023fb2019-06-29 17:11:45 -0700209 shooter_status_fetcher_.Fetch();
210 if (shooter_status_fetcher_.get()) {
211 if (shooter_status_fetcher_->left.ready &&
212 shooter_status_fetcher_->right.ready) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700213 return;
214 }
215 }
216 phased_loop.SleepUntilNext();
217 }
218}
219
220void AutonomousActor::AlignWithVisionGoal() {
221 actors::VisionAlignActionParams params;
Austin Schuh1bf8a212019-05-26 22:13:14 -0700222 vision_action_ = vision_align_actor_factory_.Make(params);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700223 vision_action_->Start();
224}
225
Austin Schuh23b21802016-04-03 21:18:56 -0700226void AutonomousActor::WaitForAlignedWithVision(
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800227 chrono::nanoseconds align_duration) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700228 bool vision_valid = false;
229 double last_angle = 0.0;
230 int ready_to_fire = 0;
231
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700232 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700233 event_loop()->monotonic_now(),
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700234 ::std::chrono::milliseconds(5) / 2);
Austin Schuh77ed5432019-07-07 20:41:36 -0700235 const monotonic_clock::time_point end_time = monotonic_now() + align_duration;
236 while (end_time > monotonic_now()) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700237 if (ShouldCancel()) break;
238
Austin Schuh28bde302019-05-26 22:24:33 -0700239 vision_status_fetcher_.Fetch();
240 if (vision_status_fetcher_.get()) {
241 vision_valid = (vision_status_fetcher_->left_image_valid &&
242 vision_status_fetcher_->right_image_valid);
243 last_angle = vision_status_fetcher_->horizontal_angle;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700244 }
245
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700246 drivetrain_status_fetcher_.Fetch();
247 drivetrain_goal_fetcher_.Fetch();
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700248
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700249 if (drivetrain_status_fetcher_.get() && drivetrain_goal_fetcher_.get()) {
250 const double left_goal = drivetrain_goal_fetcher_->left_goal;
251 const double right_goal = drivetrain_goal_fetcher_->right_goal;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700252 const double left_current =
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700253 drivetrain_status_fetcher_->estimated_left_position;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700254 const double right_current =
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700255 drivetrain_status_fetcher_->estimated_right_position;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700256 const double left_velocity =
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700257 drivetrain_status_fetcher_->estimated_left_velocity;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700258 const double right_velocity =
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700259 drivetrain_status_fetcher_->estimated_right_velocity;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700260
261 if (vision_valid && ::std::abs(last_angle) < 0.02 &&
262 ::std::abs((left_goal - right_goal) -
263 (left_current - right_current)) /
264 dt_config_.robot_radius / 2.0 <
265 0.02 &&
266 ::std::abs(left_velocity - right_velocity) < 0.01) {
267 ++ready_to_fire;
268 } else {
269 ready_to_fire = 0;
270 }
Austin Schuh3e4a5272016-04-20 20:11:00 -0700271 if (ready_to_fire > 15) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700272 break;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700273 AOS_LOG(INFO, "Vision align success!\n");
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700274 }
275 }
276 phased_loop.SleepUntilNext();
277 }
278
279 vision_action_->Cancel();
280 WaitUntilDoneOrCanceled(::std::move(vision_action_));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700281 AOS_LOG(INFO, "Done waiting for vision\n");
Austin Schuh23b21802016-04-03 21:18:56 -0700282}
283
284bool AutonomousActor::IntakeDone() {
Austin Schuh9481d0d2019-06-29 21:56:17 -0700285 superstructure_status_fetcher_.Fetch();
Austin Schuh23b21802016-04-03 21:18:56 -0700286
287 constexpr double kProfileError = 1e-5;
288 constexpr double kEpsilon = 0.15;
289
Austin Schuh9481d0d2019-06-29 21:56:17 -0700290 if (superstructure_status_fetcher_->state < 12 ||
291 superstructure_status_fetcher_->state == 16) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700292 AOS_LOG(ERROR, "Superstructure no longer running, aborting action\n");
Austin Schuh23b21802016-04-03 21:18:56 -0700293 return true;
294 }
295
Austin Schuh9481d0d2019-06-29 21:56:17 -0700296 if (::std::abs(superstructure_status_fetcher_->intake.goal_angle -
Austin Schuh23b21802016-04-03 21:18:56 -0700297 superstructure_goal_.intake) < kProfileError &&
Austin Schuh9481d0d2019-06-29 21:56:17 -0700298 ::std::abs(superstructure_status_fetcher_->intake
Austin Schuh23b21802016-04-03 21:18:56 -0700299 .goal_angular_velocity) < kProfileError) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700300 AOS_LOG(DEBUG, "Profile done.\n");
Austin Schuh9481d0d2019-06-29 21:56:17 -0700301 if (::std::abs(superstructure_status_fetcher_->intake.angle -
Austin Schuh23b21802016-04-03 21:18:56 -0700302 superstructure_goal_.intake) < kEpsilon &&
Austin Schuh9481d0d2019-06-29 21:56:17 -0700303 ::std::abs(superstructure_status_fetcher_->intake
Austin Schuh23b21802016-04-03 21:18:56 -0700304 .angular_velocity) < kEpsilon) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700305 AOS_LOG(INFO, "Near goal, done.\n");
Austin Schuh23b21802016-04-03 21:18:56 -0700306 return true;
307 }
308 }
309 return false;
310}
311
312bool AutonomousActor::SuperstructureProfileDone() {
Austin Schuh9481d0d2019-06-29 21:56:17 -0700313 if (superstructure_status_fetcher_->state < 12 ||
314 superstructure_status_fetcher_->state == 16) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700315 AOS_LOG(ERROR, "Superstructure no longer running, aborting action\n");
Austin Schuh23b21802016-04-03 21:18:56 -0700316 return true;
317 }
318
Austin Schuh9481d0d2019-06-29 21:56:17 -0700319 constexpr double kProfileError = 1e-5;
320 return ::std::abs(
321 superstructure_status_fetcher_->intake.goal_angle -
322 superstructure_goal_.intake) < kProfileError &&
323 ::std::abs(
324 superstructure_status_fetcher_->shoulder.goal_angle -
325 superstructure_goal_.shoulder) < kProfileError &&
326 ::std::abs(
327 superstructure_status_fetcher_->wrist.goal_angle -
328 superstructure_goal_.wrist) < kProfileError &&
329 ::std::abs(superstructure_status_fetcher_->intake
330 .goal_angular_velocity) < kProfileError &&
331 ::std::abs(superstructure_status_fetcher_->shoulder
332 .goal_angular_velocity) < kProfileError &&
333 ::std::abs(superstructure_status_fetcher_->wrist
334 .goal_angular_velocity) < kProfileError;
335}
336
337bool AutonomousActor::SuperstructureDone() {
338 superstructure_status_fetcher_.Fetch();
339
340 constexpr double kEpsilon = 0.03;
Austin Schuh23b21802016-04-03 21:18:56 -0700341 if (SuperstructureProfileDone()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700342 AOS_LOG(DEBUG, "Profile done.\n");
Austin Schuh9481d0d2019-06-29 21:56:17 -0700343 if (::std::abs(superstructure_status_fetcher_->intake.angle -
Austin Schuh23b21802016-04-03 21:18:56 -0700344 superstructure_goal_.intake) < (kEpsilon + 0.1) &&
Austin Schuh9481d0d2019-06-29 21:56:17 -0700345 ::std::abs(superstructure_status_fetcher_->shoulder.angle -
Austin Schuh23b21802016-04-03 21:18:56 -0700346 superstructure_goal_.shoulder) < (kEpsilon + 0.05) &&
Austin Schuh9481d0d2019-06-29 21:56:17 -0700347 ::std::abs(superstructure_status_fetcher_->wrist.angle -
Austin Schuh23b21802016-04-03 21:18:56 -0700348 superstructure_goal_.wrist) < (kEpsilon + 0.01) &&
Austin Schuh9481d0d2019-06-29 21:56:17 -0700349 ::std::abs(superstructure_status_fetcher_->intake
Austin Schuh23b21802016-04-03 21:18:56 -0700350 .angular_velocity) < (kEpsilon + 0.1) &&
Austin Schuh9481d0d2019-06-29 21:56:17 -0700351 ::std::abs(superstructure_status_fetcher_->shoulder
Austin Schuh23b21802016-04-03 21:18:56 -0700352 .angular_velocity) < (kEpsilon + 0.10) &&
Austin Schuh9481d0d2019-06-29 21:56:17 -0700353 ::std::abs(superstructure_status_fetcher_->wrist
Austin Schuh23b21802016-04-03 21:18:56 -0700354 .angular_velocity) < (kEpsilon + 0.05)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700355 AOS_LOG(INFO, "Near goal, done.\n");
Austin Schuh23b21802016-04-03 21:18:56 -0700356 return true;
357 }
358 }
359 return false;
360}
361
362void AutonomousActor::WaitForIntake() {
Austin Schuh9481d0d2019-06-29 21:56:17 -0700363 WaitUntil(::std::bind(&AutonomousActor::IntakeDone, this));
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700364}
365
Comran Morshedb134e772016-03-16 21:05:05 +0000366void AutonomousActor::WaitForSuperstructure() {
Austin Schuh9481d0d2019-06-29 21:56:17 -0700367 WaitUntil(::std::bind(&AutonomousActor::SuperstructureDone, this));
Austin Schuh23b21802016-04-03 21:18:56 -0700368}
Comran Morshedb134e772016-03-16 21:05:05 +0000369
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700370void AutonomousActor::WaitForSuperstructureProfile() {
Austin Schuh9481d0d2019-06-29 21:56:17 -0700371 WaitUntil([this]() {
372 superstructure_status_fetcher_.Fetch();
373 return SuperstructureProfileDone();
374 });
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700375}
376
Austin Schuh23b21802016-04-03 21:18:56 -0700377void AutonomousActor::WaitForSuperstructureLow() {
Austin Schuh9481d0d2019-06-29 21:56:17 -0700378 WaitUntil([this]() {
379 superstructure_status_fetcher_.Fetch();
Comran Morshedb134e772016-03-16 21:05:05 +0000380
Austin Schuh9481d0d2019-06-29 21:56:17 -0700381 return SuperstructureProfileDone() ||
382 superstructure_status_fetcher_->shoulder.angle < 0.1;
383 });
Comran Morshedb134e772016-03-16 21:05:05 +0000384}
Austin Schuh9481d0d2019-06-29 21:56:17 -0700385
Austin Schuh23b21802016-04-03 21:18:56 -0700386void AutonomousActor::BackLongShotLowBarTwoBall() {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700387 AOS_LOG(INFO, "Expanding for back long shot\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700388 MoveSuperstructure(0.00, M_PI / 2.0 - 0.2, -0.55, {7.0, 40.0}, {4.0, 6.0},
Austin Schuh23b21802016-04-03 21:18:56 -0700389 {10.0, 25.0}, false, 0.0);
390}
391
392void AutonomousActor::BackLongShotTwoBall() {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700393 AOS_LOG(INFO, "Expanding for back long shot\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700394 MoveSuperstructure(0.00, M_PI / 2.0 - 0.2, -0.55, {7.0, 40.0}, {4.0, 6.0},
395 {10.0, 25.0}, false, 0.0);
396}
397
398void AutonomousActor::BackLongShotTwoBallFinish() {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700399 AOS_LOG(INFO, "Expanding for back long shot\n");
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000400 MoveSuperstructure(0.00, M_PI / 2.0 - 0.2, -0.625 + 0.03, {7.0, 40.0},
401 {4.0, 6.0}, {10.0, 25.0}, false, 0.0);
Austin Schuh23b21802016-04-03 21:18:56 -0700402}
403
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700404void AutonomousActor::BackLongShot() {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700405 AOS_LOG(INFO, "Expanding for back long shot\n");
Austin Schuh23b21802016-04-03 21:18:56 -0700406 MoveSuperstructure(0.80, M_PI / 2.0 - 0.2, -0.62, {7.0, 40.0}, {4.0, 6.0},
407 {10.0, 25.0}, false, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700408}
409
410void AutonomousActor::BackMiddleShot() {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700411 AOS_LOG(INFO, "Expanding for back middle shot\n");
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700412 MoveSuperstructure(-0.05, M_PI / 2.0 - 0.2, -0.665, {7.0, 40.0}, {4.0, 10.0},
Austin Schuh23b21802016-04-03 21:18:56 -0700413 {10.0, 25.0}, false, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700414}
415
Austin Schuh3e4a5272016-04-20 20:11:00 -0700416void AutonomousActor::FrontLongShot() {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700417 AOS_LOG(INFO, "Expanding for front long shot\n");
Austin Schuh3e4a5272016-04-20 20:11:00 -0700418 MoveSuperstructure(0.80, M_PI / 2.0 + 0.1, M_PI + 0.41 + 0.02, {7.0, 40.0},
419 {4.0, 6.0}, {10.0, 25.0}, false, 0.0);
420}
421
422void AutonomousActor::FrontMiddleShot() {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700423 AOS_LOG(INFO, "Expanding for front middle shot\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700424 MoveSuperstructure(-0.05, M_PI / 2.0 + 0.1, M_PI + 0.44, {7.0, 40.0},
Austin Schuh3e4a5272016-04-20 20:11:00 -0700425 {4.0, 10.0}, {10.0, 25.0}, true, 0.0);
426}
427
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700428void AutonomousActor::TuckArm(bool low_bar, bool traverse_down) {
429 MoveSuperstructure(low_bar ? -0.05 : 2.0, -0.010, 0.0, {7.0, 40.0},
Austin Schuh23b21802016-04-03 21:18:56 -0700430 {4.0, 10.0}, {10.0, 25.0}, !traverse_down, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700431}
432
Austin Schuh3e4a5272016-04-20 20:11:00 -0700433void AutonomousActor::DoFullShot() {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700434 if (ShouldCancel()) return;
435 // Make sure that the base is aligned with the base.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700436 AOS_LOG(INFO, "Waiting for the superstructure\n");
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700437 WaitForSuperstructure();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700438
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800439 this_thread::sleep_for(chrono::milliseconds(500));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700440
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700441 if (ShouldCancel()) return;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700442 AOS_LOG(INFO, "Triggering the vision actor\n");
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700443 AlignWithVisionGoal();
444
445 // Wait for the drive base to be aligned with the target and make sure that
446 // the shooter is up to speed.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700447 AOS_LOG(INFO, "Waiting for vision to be aligned\n");
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800448 WaitForAlignedWithVision(chrono::milliseconds(2000));
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700449 if (ShouldCancel()) return;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700450 AOS_LOG(INFO, "Waiting for shooter to be up to speed\n");
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700451 WaitForShooterSpeed();
452 if (ShouldCancel()) return;
453
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800454 this_thread::sleep_for(chrono::milliseconds(300));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700455 AOS_LOG(INFO, "Shoot!\n");
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700456 Shoot();
457
458 // Turn off the shooter and fold up the superstructure.
459 if (ShouldCancel()) return;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700460 AOS_LOG(INFO, "Stopping shooter\n");
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700461 SetShooterSpeed(0.0);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700462 AOS_LOG(INFO, "Folding superstructure back down\n");
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700463 TuckArm(false, false);
464
465 // Wait for everything to be folded up.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700466 AOS_LOG(INFO, "Waiting for superstructure to be folded back down\n");
Austin Schuh3e4a5272016-04-20 20:11:00 -0700467 WaitForSuperstructureLow();
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700468}
469
470void AutonomousActor::LowBarDrive() {
471 TuckArm(false, true);
472 StartDrive(-5.5, 0.0, kLowBarDrive, kSlowTurn);
473
474 if (!WaitForDriveNear(5.3, 0.0)) return;
475 TuckArm(true, true);
476
477 if (!WaitForDriveNear(5.0, 0.0)) return;
478
479 StartDrive(0.0, 0.0, kLowBarDrive, kSlowTurn);
480
481 if (!WaitForDriveNear(3.0, 0.0)) return;
482
483 StartDrive(0.0, 0.0, kLowBarDrive, kSlowTurn);
484
485 if (!WaitForDriveNear(1.0, 0.0)) return;
486
Austin Schuh15b5f6a2016-03-26 19:43:56 -0700487 StartDrive(0, -M_PI / 4.0 - 0.2, kLowBarDrive, kSlowTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700488}
489
Austin Schuh3e4a5272016-04-20 20:11:00 -0700490void AutonomousActor::TippyDrive(double goal_distance, double tip_distance,
491 double below, double above) {
492 StartDrive(goal_distance, 0.0, kMoatDrive, kSlowTurn);
493 if (!WaitForBelowAngle(below)) return;
494 if (!WaitForAboveAngle(above)) return;
495 // Ok, we are good now. Compensate by moving the goal by the error.
496 // Should be here at 2.7
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700497 drivetrain_status_fetcher_.Fetch();
498 if (drivetrain_status_fetcher_.get()) {
Austin Schuh3e4a5272016-04-20 20:11:00 -0700499 const double left_error =
500 (initial_drivetrain_.left -
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700501 drivetrain_status_fetcher_->estimated_left_position);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700502 const double right_error =
503 (initial_drivetrain_.right -
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700504 drivetrain_status_fetcher_->estimated_right_position);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700505 const double distance_to_go = (left_error + right_error) / 2.0;
506 const double distance_compensation =
507 goal_distance - tip_distance - distance_to_go;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700508 AOS_LOG(INFO, "Going %f further at the bump\n", distance_compensation);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700509 StartDrive(distance_compensation, 0.0, kMoatDrive, kSlowTurn);
510 }
511}
512
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700513void AutonomousActor::MiddleDrive() {
514 TuckArm(false, false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700515 TippyDrive(3.65, 2.7, -0.2, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700516 if (!WaitForDriveDone()) return;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700517}
518
519void AutonomousActor::OneFromMiddleDrive(bool left) {
Austin Schuh3e4a5272016-04-20 20:11:00 -0700520 const double kTurnAngle = left ? -0.41 : 0.41;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700521 TuckArm(false, false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700522 TippyDrive(4.05, 2.7, -0.2, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700523
524 if (!WaitForDriveDone()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700525 StartDrive(0.0, kTurnAngle, kRealignDrive, kFastTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700526}
527
528void AutonomousActor::TwoFromMiddleDrive() {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700529 TuckArm(false, false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700530 constexpr double kDriveDistance = 5.10;
531 TippyDrive(kDriveDistance, 2.7, -0.2, 0.0);
532
533 if (!WaitForDriveNear(kDriveDistance - 3.0, 2.0)) return;
534 StartDrive(0, -M_PI / 2 - 0.10, kMoatDrive, kFastTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700535
536 if (!WaitForDriveDone()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700537 StartDrive(0, M_PI / 3 + 0.35, kMoatDrive, kFastTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700538}
Comran Morshedb134e772016-03-16 21:05:05 +0000539
Austin Schuh23b21802016-04-03 21:18:56 -0700540void AutonomousActor::CloseIfBall() {
Austin Schuh4b652c92019-05-27 13:22:27 -0700541 ball_detector_fetcher_.Fetch();
542 if (ball_detector_fetcher_.get()) {
543 const bool ball_detected = ball_detector_fetcher_->voltage > 2.5;
Austin Schuh23b21802016-04-03 21:18:56 -0700544 if (ball_detected) {
545 CloseShooter();
546 }
547 }
548}
549
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700550void AutonomousActor::WaitForBallOrDriveDone() {
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700551 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700552 event_loop()->monotonic_now(),
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700553 ::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();
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700559 drivetrain_status_fetcher_.Fetch();
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700560 if (IsDriveDone()) {
561 return;
562 }
563
Austin Schuh4b652c92019-05-27 13:22:27 -0700564 ball_detector_fetcher_.Fetch();
565 if (ball_detector_fetcher_.get()) {
566 const bool ball_detected = ball_detector_fetcher_->voltage > 2.5;
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700567 if (ball_detected) {
568 return;
569 }
570 }
571 }
572}
573
Austin Schuh3e4a5272016-04-20 20:11:00 -0700574void AutonomousActor::TwoBallAuto() {
Austin Schuh77ed5432019-07-07 20:41:36 -0700575 const monotonic_clock::time_point start_time = monotonic_now();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700576 OpenShooter();
577 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
578 false, 12.0);
579 if (ShouldCancel()) return;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700580 AOS_LOG(INFO, "Waiting for the intake to come down.\n");
Austin Schuh3e4a5272016-04-20 20:11:00 -0700581
582 WaitForIntake();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700583 AOS_LOG(INFO, "Intake done at %f seconds, starting to drive\n",
584 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700585 if (ShouldCancel()) return;
586 const double kDriveDistance = 5.05;
587 StartDrive(-kDriveDistance, 0.0, kTwoBallLowDrive, kSlowTurn);
588
589 StartDrive(0.0, 0.4, kTwoBallLowDrive, kSwerveTurn);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700590 if (!WaitForDriveNear(kDriveDistance - 0.5, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700591
Austin Schuh295c2d92016-05-01 12:28:04 -0700592 // Check if the ball is there.
593 bool first_ball_there = true;
Austin Schuh4b652c92019-05-27 13:22:27 -0700594 ball_detector_fetcher_.Fetch();
595 if (ball_detector_fetcher_.get()) {
596 const bool ball_detected = ball_detector_fetcher_->voltage > 2.5;
Austin Schuh295c2d92016-05-01 12:28:04 -0700597 first_ball_there = ball_detected;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700598 AOS_LOG(INFO, "Saw the ball: %d at %f\n", first_ball_there,
599 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh295c2d92016-05-01 12:28:04 -0700600 }
Austin Schuh3e4a5272016-04-20 20:11:00 -0700601 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 40.0}, {4.0, 10.0}, {10.0, 25.0},
602 false, 0.0);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700603 AOS_LOG(INFO,
604 "Shutting off rollers at %f seconds, starting to straighten out\n",
605 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700606 StartDrive(0.0, -0.4, kTwoBallLowDrive, kSwerveTurn);
607 MoveSuperstructure(-0.05, -0.010, 0.0, {8.0, 40.0}, {4.0, 10.0}, {10.0, 25.0},
608 false, 0.0);
609 CloseShooter();
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700610 if (!WaitForDriveNear(kDriveDistance - 2.4, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700611
612 // We are now under the low bar. Start lifting.
613 BackLongShotLowBarTwoBall();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700614 AOS_LOG(INFO, "Spinning up the shooter wheels\n");
Austin Schuh3e4a5272016-04-20 20:11:00 -0700615 SetShooterSpeed(640.0);
616 StartDrive(0.0, 0.0, kTwoBallFastDrive, kSwerveTurn);
617
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700618 if (!WaitForDriveNear(1.50, kDoNotTurnCare)) return;
619 constexpr double kShootTurnAngle = -M_PI / 4.0 - 0.05;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700620 StartDrive(0, kShootTurnAngle, kTwoBallFastDrive, kFinishTurn);
621 BackLongShotTwoBall();
622
623 if (!WaitForDriveDone()) return;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700624 AOS_LOG(INFO, "First shot done driving at %f seconds\n",
625 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700626
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700627 WaitForSuperstructureProfile();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700628
629 if (ShouldCancel()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700630 AlignWithVisionGoal();
631
632 WaitForShooterSpeed();
633 if (ShouldCancel()) return;
634
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800635 constexpr chrono::milliseconds kVisionExtra{0};
636 WaitForAlignedWithVision(chrono::milliseconds(500) + kVisionExtra);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700637 BackLongShotTwoBallFinish();
638 WaitForSuperstructureProfile();
639 if (ShouldCancel()) return;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700640 AOS_LOG(INFO, "Shoot!\n");
Austin Schuh295c2d92016-05-01 12:28:04 -0700641 if (first_ball_there) {
642 Shoot();
643 } else {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700644 AOS_LOG(INFO, "Nah, not shooting\n");
Austin Schuh295c2d92016-05-01 12:28:04 -0700645 }
Austin Schuh3e4a5272016-04-20 20:11:00 -0700646
Austin Schuhf257f3c2019-10-27 21:00:43 -0700647 AOS_LOG(INFO, "First shot at %f seconds\n",
648 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700649 if (ShouldCancel()) return;
650
651 SetShooterSpeed(0.0);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700652 AOS_LOG(INFO, "Folding superstructure back down\n");
Austin Schuh3e4a5272016-04-20 20:11:00 -0700653 TuckArm(true, true);
654
655 // Undo vision move.
656 StartDrive(0.0, 0.0, kTwoBallFastDrive, kFinishTurn);
657 if (!WaitForDriveDone()) return;
658
659 constexpr double kBackDrive = 3.09 - 0.4;
660 StartDrive(kBackDrive, 0.0, kTwoBallReturnDrive, kSlowTurn);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700661 if (!WaitForDriveNear(kBackDrive - 0.19, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700662 StartDrive(0, -kShootTurnAngle, kTwoBallReturnDrive, kSwerveTurn);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700663 if (!WaitForDriveNear(1.0, kDoNotTurnCare)) return;
664 StartDrive(0, 0, kTwoBallReturnSlow, kSwerveTurn);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700665
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700666 if (!WaitForDriveNear(0.06, kDoNotTurnCare)) return;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700667 AOS_LOG(INFO, "At Low Bar %f\n",
668 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700669
670 OpenShooter();
671 constexpr double kSecondBallAfterBarDrive = 2.10;
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700672 StartDrive(kSecondBallAfterBarDrive, 0.0, kTwoBallBallPickupAccel, kSlowTurn);
673 if (!WaitForDriveNear(kSecondBallAfterBarDrive - 0.5, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700674 constexpr double kBallSmallWallTurn = -0.11;
675 StartDrive(0, kBallSmallWallTurn, kTwoBallBallPickup, kFinishTurn);
676
677 MoveSuperstructure(0.03, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
678 false, 12.0);
679
680 if (!WaitForDriveProfileDone()) return;
681
682 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
683 false, 12.0);
684
Austin Schuhf257f3c2019-10-27 21:00:43 -0700685 AOS_LOG(INFO, "Done backing up %f\n",
686 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700687
688 constexpr double kDriveBackDistance = 5.15 - 0.4;
689 StartDrive(-kDriveBackDistance, 0.0, kTwoBallLowDrive, kFinishTurn);
690 CloseIfBall();
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700691 if (!WaitForDriveNear(kDriveBackDistance - 0.75, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700692
693 StartDrive(0.0, -kBallSmallWallTurn, kTwoBallLowDrive, kFinishTurn);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700694 AOS_LOG(INFO, "Straightening up at %f\n",
695 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700696
697 CloseIfBall();
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700698 if (!WaitForDriveNear(kDriveBackDistance - 2.3, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700699
Austin Schuh4b652c92019-05-27 13:22:27 -0700700 ball_detector_fetcher_.Fetch();
701 if (ball_detector_fetcher_.get()) {
702 const bool ball_detected = ball_detector_fetcher_->voltage > 2.5;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700703 if (!ball_detected) {
704 if (!WaitForDriveDone()) return;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700705 AOS_LOG(INFO, "Aborting, no ball %f\n",
706 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700707 return;
708 }
709 }
710 CloseShooter();
711
712 BackLongShotLowBarTwoBall();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700713 AOS_LOG(INFO, "Spinning up the shooter wheels\n");
Austin Schuh3e4a5272016-04-20 20:11:00 -0700714 SetShooterSpeed(640.0);
715 StartDrive(0.0, 0.0, kTwoBallFastDrive, kSwerveTurn);
716
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700717 if (!WaitForDriveNear(1.80, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700718 StartDrive(0, kShootTurnAngle, kTwoBallFastDrive, kFinishTurn);
719 BackLongShotTwoBall();
720
721 if (!WaitForDriveDone()) return;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700722 AOS_LOG(INFO, "Second shot done driving at %f seconds\n",
723 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700724 WaitForSuperstructure();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700725 AlignWithVisionGoal();
726 if (ShouldCancel()) return;
727
728 WaitForShooterSpeed();
729 if (ShouldCancel()) return;
730
731 // 2.2 with 0.4 of vision.
732 // 1.8 without any vision.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700733 AOS_LOG(INFO, "Going to vision align at %f\n",
734 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800735 WaitForAlignedWithVision(
736 (start_time + chrono::milliseconds(13500) + kVisionExtra * 2) -
Austin Schuh77ed5432019-07-07 20:41:36 -0700737 monotonic_now());
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700738 BackLongShotTwoBallFinish();
739 WaitForSuperstructureProfile();
740 if (ShouldCancel()) return;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700741 AOS_LOG(INFO, "Shoot at %f\n",
742 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700743 Shoot();
744
Austin Schuhf257f3c2019-10-27 21:00:43 -0700745 AOS_LOG(INFO, "Second shot at %f seconds\n",
746 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700747 if (ShouldCancel()) return;
748
749 SetShooterSpeed(0.0);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700750 AOS_LOG(INFO, "Folding superstructure back down\n");
Austin Schuh3e4a5272016-04-20 20:11:00 -0700751 TuckArm(true, false);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700752 AOS_LOG(INFO, "Shot %f\n",
753 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700754
755 WaitForSuperstructureLow();
756
Austin Schuhf257f3c2019-10-27 21:00:43 -0700757 AOS_LOG(INFO, "Done %f\n",
758 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700759}
760
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700761void AutonomousActor::StealAndMoveOverBy(double distance) {
762 OpenShooter();
763 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
764 true, 12.0);
765 if (ShouldCancel()) return;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700766 AOS_LOG(INFO, "Waiting for the intake to come down.\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700767
768 WaitForIntake();
769 if (ShouldCancel()) return;
770 StartDrive(-distance, M_PI / 2.0, kFastDrive, kStealTurn);
771 WaitForBallOrDriveDone();
772 if (ShouldCancel()) return;
773 MoveSuperstructure(1.0, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
774 true, 12.0);
775
776 if (!WaitForDriveDone()) return;
777 StartDrive(0.0, M_PI / 2.0, kFastDrive, kStealTurn);
778 if (!WaitForDriveDone()) return;
779}
780
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000781bool AutonomousActor::RunAction(
782 const ::frc971::autonomous::AutonomousActionParams &params) {
Austin Schuh77ed5432019-07-07 20:41:36 -0700783 monotonic_clock::time_point start_time = monotonic_now();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700784 AOS_LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n",
785 params.mode);
Comran Morshede68e3732016-03-12 14:12:11 +0000786
Comran Morshed435f1112016-03-12 14:20:45 +0000787 InitializeEncoders();
788 ResetDrivetrain();
789
Austin Schuh295c2d92016-05-01 12:28:04 -0700790 switch (params.mode) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700791 case 0:
792 LowBarDrive();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700793 if (!WaitForDriveDone()) return true;
794 // Get the superstructure to unfold and get ready for shooting.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700795 AOS_LOG(INFO, "Unfolding superstructure\n");
Austin Schuh3e4a5272016-04-20 20:11:00 -0700796 FrontLongShot();
797
798 // Spin up the shooter wheels.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700799 AOS_LOG(INFO, "Spinning up the shooter wheels\n");
Austin Schuh3e4a5272016-04-20 20:11:00 -0700800 SetShooterSpeed(640.0);
801
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700802 break;
803 case 1:
804 TwoFromMiddleDrive();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700805 if (!WaitForDriveDone()) return true;
806 // Get the superstructure to unfold and get ready for shooting.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700807 AOS_LOG(INFO, "Unfolding superstructure\n");
Austin Schuh3e4a5272016-04-20 20:11:00 -0700808 FrontMiddleShot();
809
810 // Spin up the shooter wheels.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700811 AOS_LOG(INFO, "Spinning up the shooter wheels\n");
Austin Schuh3e4a5272016-04-20 20:11:00 -0700812 SetShooterSpeed(600.0);
813
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700814 break;
815 case 2:
816 OneFromMiddleDrive(true);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700817 if (!WaitForDriveDone()) return true;
818 // Get the superstructure to unfold and get ready for shooting.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700819 AOS_LOG(INFO, "Unfolding superstructure\n");
Austin Schuh3e4a5272016-04-20 20:11:00 -0700820 FrontMiddleShot();
821
822 // Spin up the shooter wheels.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700823 AOS_LOG(INFO, "Spinning up the shooter wheels\n");
Austin Schuh3e4a5272016-04-20 20:11:00 -0700824 SetShooterSpeed(600.0);
825
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700826 break;
827 case 3:
828 MiddleDrive();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700829 if (!WaitForDriveDone()) return true;
830 // Get the superstructure to unfold and get ready for shooting.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700831 AOS_LOG(INFO, "Unfolding superstructure\n");
Austin Schuh3e4a5272016-04-20 20:11:00 -0700832 FrontMiddleShot();
833
834 // Spin up the shooter wheels.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700835 AOS_LOG(INFO, "Spinning up the shooter wheels\n");
Austin Schuh3e4a5272016-04-20 20:11:00 -0700836 SetShooterSpeed(600.0);
837
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700838 break;
839 case 4:
840 OneFromMiddleDrive(false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700841 if (!WaitForDriveDone()) return true;
842 // Get the superstructure to unfold and get ready for shooting.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700843 AOS_LOG(INFO, "Unfolding superstructure\n");
Austin Schuh3e4a5272016-04-20 20:11:00 -0700844 FrontMiddleShot();
845
846 // Spin up the shooter wheels.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700847 AOS_LOG(INFO, "Spinning up the shooter wheels\n");
Austin Schuh3e4a5272016-04-20 20:11:00 -0700848 SetShooterSpeed(600.0);
849
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700850 break;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700851 case 5:
Campbell Crowley9ed61a52016-11-05 17:13:07 -0700852 case 15:
Austin Schuh3e4a5272016-04-20 20:11:00 -0700853 TwoBallAuto();
Austin Schuh23b21802016-04-03 21:18:56 -0700854 return true;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700855 break;
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700856 case 6:
857 StealAndMoveOverBy(3.10 + 2 * 52 * 2.54 / 100.0);
858 if (ShouldCancel()) return true;
859
860 TwoFromMiddleDrive();
861 if (!WaitForDriveDone()) return true;
862 // Get the superstructure to unfold and get ready for shooting.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700863 AOS_LOG(INFO, "Unfolding superstructure\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700864 FrontMiddleShot();
865
866 // Spin up the shooter wheels.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700867 AOS_LOG(INFO, "Spinning up the shooter wheels\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700868 SetShooterSpeed(600.0);
869
870 break;
871 case 7:
872 StealAndMoveOverBy(2.95 + 52 * 2.54 / 100.0);
873 if (ShouldCancel()) return true;
874
875 OneFromMiddleDrive(true);
876 if (!WaitForDriveDone()) return true;
877 // Get the superstructure to unfold and get ready for shooting.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700878 AOS_LOG(INFO, "Unfolding superstructure\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700879 FrontMiddleShot();
880
881 // Spin up the shooter wheels.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700882 AOS_LOG(INFO, "Spinning up the shooter wheels\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700883 SetShooterSpeed(600.0);
884
885 break;
886 case 8: {
887 StealAndMoveOverBy(2.95);
888 if (ShouldCancel()) return true;
889
890 MiddleDrive();
891 if (!WaitForDriveDone()) return true;
892 // Get the superstructure to unfold and get ready for shooting.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700893 AOS_LOG(INFO, "Unfolding superstructure\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700894 FrontMiddleShot();
895
896 // Spin up the shooter wheels.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700897 AOS_LOG(INFO, "Spinning up the shooter wheels\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700898 SetShooterSpeed(600.0);
899
900 } break;
901 case 9: {
902 StealAndMoveOverBy(1.70);
903 if (ShouldCancel()) return true;
904
905 OneFromMiddleDrive(false);
906 if (!WaitForDriveDone()) return true;
907 // Get the superstructure to unfold and get ready for shooting.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700908 AOS_LOG(INFO, "Unfolding superstructure\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700909 FrontMiddleShot();
910
911 // Spin up the shooter wheels.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700912 AOS_LOG(INFO, "Spinning up the shooter wheels\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700913 SetShooterSpeed(600.0);
914
915 } break;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700916 default:
Austin Schuhf257f3c2019-10-27 21:00:43 -0700917 AOS_LOG(ERROR, "Invalid auto mode %d\n", params.mode);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700918 return true;
919 }
Comran Morshed435f1112016-03-12 14:20:45 +0000920
Austin Schuh3e4a5272016-04-20 20:11:00 -0700921 DoFullShot();
922
923 StartDrive(0.5, 0.0, kMoatDrive, kFastTurn);
Comran Morshed435f1112016-03-12 14:20:45 +0000924 if (!WaitForDriveDone()) return true;
925
Austin Schuhf257f3c2019-10-27 21:00:43 -0700926 AOS_LOG(INFO, "Done %f\n",
Austin Schuh77ed5432019-07-07 20:41:36 -0700927 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Comran Morshed435f1112016-03-12 14:20:45 +0000928
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700929 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuh77ed5432019-07-07 20:41:36 -0700930 monotonic_now(),
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700931 ::std::chrono::milliseconds(5) / 2);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700932
Comran Morshed435f1112016-03-12 14:20:45 +0000933 while (!ShouldCancel()) {
934 phased_loop.SleepUntilNext();
Comran Morshede68e3732016-03-12 14:12:11 +0000935 }
Austin Schuhf257f3c2019-10-27 21:00:43 -0700936 AOS_LOG(DEBUG, "Done running\n");
Comran Morshede68e3732016-03-12 14:12:11 +0000937
938 return true;
939}
940
Comran Morshede68e3732016-03-12 14:12:11 +0000941} // namespace actors
942} // namespace y2016