blob: e94cccdbecaa326de29069f7e3db0bcb05619827 [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>(
Austin Schuh9481d0d2019-06-29 21:56:17 -070066 ".y2016.control_loops.shooter.shooter_queue.status")),
67 superstructure_status_fetcher_(
68 event_loop->MakeFetcher<
69 ::y2016::control_loops::SuperstructureQueue::Status>(
70 ".y2016.control_loops.superstructure_queue.status")),
71 superstructure_goal_sender_(
72 event_loop
73 ->MakeSender<::y2016::control_loops::SuperstructureQueue::Goal>(
74 ".y2016.control_loops.superstructure_queue.goal")) {}
Comran Morshed435f1112016-03-12 14:20:45 +000075
Austin Schuhe4ec49c2016-04-24 19:07:15 -070076constexpr double kDoNotTurnCare = 2.0;
77
Comran Morshedb134e772016-03-16 21:05:05 +000078void AutonomousActor::MoveSuperstructure(
79 double intake, double shoulder, double wrist,
80 const ProfileParameters intake_params,
81 const ProfileParameters shoulder_params,
Austin Schuh23b21802016-04-03 21:18:56 -070082 const ProfileParameters wrist_params, bool traverse_up,
83 double roller_power) {
Comran Morshedb134e772016-03-16 21:05:05 +000084 superstructure_goal_ = {intake, shoulder, wrist};
85
Austin Schuh9481d0d2019-06-29 21:56:17 -070086 auto new_superstructure_goal = superstructure_goal_sender_.MakeMessage();
Comran Morshedb134e772016-03-16 21:05:05 +000087
88 new_superstructure_goal->angle_intake = intake;
89 new_superstructure_goal->angle_shoulder = shoulder;
90 new_superstructure_goal->angle_wrist = wrist;
91
92 new_superstructure_goal->max_angular_velocity_intake =
93 intake_params.max_velocity;
94 new_superstructure_goal->max_angular_velocity_shoulder =
95 shoulder_params.max_velocity;
96 new_superstructure_goal->max_angular_velocity_wrist =
97 wrist_params.max_velocity;
98
99 new_superstructure_goal->max_angular_acceleration_intake =
100 intake_params.max_acceleration;
101 new_superstructure_goal->max_angular_acceleration_shoulder =
102 shoulder_params.max_acceleration;
103 new_superstructure_goal->max_angular_acceleration_wrist =
104 wrist_params.max_acceleration;
105
Austin Schuh23b21802016-04-03 21:18:56 -0700106 new_superstructure_goal->voltage_top_rollers = roller_power;
107 new_superstructure_goal->voltage_bottom_rollers = roller_power;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700108
109 new_superstructure_goal->traverse_unlatched = true;
110 new_superstructure_goal->traverse_down = !traverse_up;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700111 new_superstructure_goal->voltage_climber = 0.0;
112 new_superstructure_goal->unfold_climber = false;
Comran Morshedb134e772016-03-16 21:05:05 +0000113
114 if (!new_superstructure_goal.Send()) {
115 LOG(ERROR, "Sending superstructure goal failed.\n");
116 }
117}
118
Austin Schuh23b21802016-04-03 21:18:56 -0700119void AutonomousActor::OpenShooter() {
120 shooter_speed_ = 0.0;
121
Austin Schuhae023fb2019-06-29 17:11:45 -0700122 auto shooter_goal = shooter_goal_sender_.MakeMessage();
123 shooter_goal->angular_velocity = shooter_speed_;
124 shooter_goal->clamp_open = true;
125 shooter_goal->push_to_shooter = false;
126 shooter_goal->force_lights_on = false;
127 if (!shooter_goal.Send()) {
Austin Schuh23b21802016-04-03 21:18:56 -0700128 LOG(ERROR, "Sending shooter goal failed.\n");
129 }
130}
131
132void AutonomousActor::CloseShooter() {
133 shooter_speed_ = 0.0;
134
Austin Schuhae023fb2019-06-29 17:11:45 -0700135 auto shooter_goal = shooter_goal_sender_.MakeMessage();
136 shooter_goal->angular_velocity = shooter_speed_;
137 shooter_goal->clamp_open = false;
138 shooter_goal->push_to_shooter = false;
139 shooter_goal->force_lights_on = false;
140
141 if (!shooter_goal.Send()) {
Austin Schuh23b21802016-04-03 21:18:56 -0700142 LOG(ERROR, "Sending shooter goal failed.\n");
143 }
144}
145
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700146void AutonomousActor::SetShooterSpeed(double speed) {
147 shooter_speed_ = speed;
148
149 // In auto, we want to have the lights on whenever possible since we have no
150 // hope of a human aligning the robot.
151 bool force_lights_on = shooter_speed_ > 1.0;
152
Austin Schuhae023fb2019-06-29 17:11:45 -0700153 auto shooter_goal = shooter_goal_sender_.MakeMessage();
154 shooter_goal->angular_velocity = shooter_speed_;
155 shooter_goal->clamp_open = false;
156 shooter_goal->push_to_shooter = false;
157 shooter_goal->force_lights_on = force_lights_on;
158
159 if (!shooter_goal.Send()) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700160 LOG(ERROR, "Sending shooter goal failed.\n");
161 }
162}
163
164void AutonomousActor::Shoot() {
165 uint32_t initial_shots = 0;
166
Austin Schuhae023fb2019-06-29 17:11:45 -0700167 shooter_status_fetcher_.Fetch();
168 if (shooter_status_fetcher_.get()) {
169 initial_shots = shooter_status_fetcher_->shots;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700170 }
171
172 // In auto, we want to have the lights on whenever possible since we have no
173 // hope of a human aligning the robot.
174 bool force_lights_on = shooter_speed_ > 1.0;
175
Austin Schuhae023fb2019-06-29 17:11:45 -0700176 auto shooter_goal = shooter_goal_sender_.MakeMessage();
177 shooter_goal->angular_velocity = shooter_speed_;
178 shooter_goal->clamp_open = false;
179 shooter_goal->push_to_shooter = true;
180 shooter_goal->force_lights_on = force_lights_on;
181
182 if (!shooter_goal.Send()) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700183 LOG(ERROR, "Sending shooter goal failed.\n");
184 }
185
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700186 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700187 event_loop()->monotonic_now(),
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700188 ::std::chrono::milliseconds(5) / 2);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700189 while (true) {
190 if (ShouldCancel()) return;
191
192 // Wait for the shot count to change so we know when the shot is complete.
Austin Schuhae023fb2019-06-29 17:11:45 -0700193 shooter_status_fetcher_.Fetch();
194 if (shooter_status_fetcher_.get()) {
195 if (initial_shots < shooter_status_fetcher_->shots) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700196 return;
197 }
198 }
199 phased_loop.SleepUntilNext();
200 }
201}
202
203void AutonomousActor::WaitForShooterSpeed() {
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700204 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700205 event_loop()->monotonic_now(),
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700206 ::std::chrono::milliseconds(5) / 2);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700207 while (true) {
208 if (ShouldCancel()) return;
209
Austin Schuhae023fb2019-06-29 17:11:45 -0700210 shooter_status_fetcher_.Fetch();
211 if (shooter_status_fetcher_.get()) {
212 if (shooter_status_fetcher_->left.ready &&
213 shooter_status_fetcher_->right.ready) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700214 return;
215 }
216 }
217 phased_loop.SleepUntilNext();
218 }
219}
220
221void AutonomousActor::AlignWithVisionGoal() {
222 actors::VisionAlignActionParams params;
Austin Schuh1bf8a212019-05-26 22:13:14 -0700223 vision_action_ = vision_align_actor_factory_.Make(params);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700224 vision_action_->Start();
225}
226
Austin Schuh23b21802016-04-03 21:18:56 -0700227void AutonomousActor::WaitForAlignedWithVision(
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800228 chrono::nanoseconds align_duration) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700229 bool vision_valid = false;
230 double last_angle = 0.0;
231 int ready_to_fire = 0;
232
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700233 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700234 event_loop()->monotonic_now(),
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700235 ::std::chrono::milliseconds(5) / 2);
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800236 monotonic_clock::time_point end_time =
237 monotonic_clock::now() + align_duration;
238 while (end_time > monotonic_clock::now()) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700239 if (ShouldCancel()) break;
240
Austin Schuh28bde302019-05-26 22:24:33 -0700241 vision_status_fetcher_.Fetch();
242 if (vision_status_fetcher_.get()) {
243 vision_valid = (vision_status_fetcher_->left_image_valid &&
244 vision_status_fetcher_->right_image_valid);
245 last_angle = vision_status_fetcher_->horizontal_angle;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700246 }
247
248 drivetrain_queue.status.FetchLatest();
249 drivetrain_queue.goal.FetchLatest();
250
251 if (drivetrain_queue.status.get() && drivetrain_queue.goal.get()) {
252 const double left_goal = drivetrain_queue.goal->left_goal;
253 const double right_goal = drivetrain_queue.goal->right_goal;
254 const double left_current =
255 drivetrain_queue.status->estimated_left_position;
256 const double right_current =
257 drivetrain_queue.status->estimated_right_position;
258 const double left_velocity =
259 drivetrain_queue.status->estimated_left_velocity;
260 const double right_velocity =
261 drivetrain_queue.status->estimated_right_velocity;
262
263 if (vision_valid && ::std::abs(last_angle) < 0.02 &&
264 ::std::abs((left_goal - right_goal) -
265 (left_current - right_current)) /
266 dt_config_.robot_radius / 2.0 <
267 0.02 &&
268 ::std::abs(left_velocity - right_velocity) < 0.01) {
269 ++ready_to_fire;
270 } else {
271 ready_to_fire = 0;
272 }
Austin Schuh3e4a5272016-04-20 20:11:00 -0700273 if (ready_to_fire > 15) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700274 break;
Austin Schuh23b21802016-04-03 21:18:56 -0700275 LOG(INFO, "Vision align success!\n");
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700276 }
277 }
278 phased_loop.SleepUntilNext();
279 }
280
281 vision_action_->Cancel();
282 WaitUntilDoneOrCanceled(::std::move(vision_action_));
Austin Schuh23b21802016-04-03 21:18:56 -0700283 LOG(INFO, "Done waiting for vision\n");
284}
285
286bool AutonomousActor::IntakeDone() {
Austin Schuh9481d0d2019-06-29 21:56:17 -0700287 superstructure_status_fetcher_.Fetch();
Austin Schuh23b21802016-04-03 21:18:56 -0700288
289 constexpr double kProfileError = 1e-5;
290 constexpr double kEpsilon = 0.15;
291
Austin Schuh9481d0d2019-06-29 21:56:17 -0700292 if (superstructure_status_fetcher_->state < 12 ||
293 superstructure_status_fetcher_->state == 16) {
Austin Schuh23b21802016-04-03 21:18:56 -0700294 LOG(ERROR, "Superstructure no longer running, aborting action\n");
295 return true;
296 }
297
Austin Schuh9481d0d2019-06-29 21:56:17 -0700298 if (::std::abs(superstructure_status_fetcher_->intake.goal_angle -
Austin Schuh23b21802016-04-03 21:18:56 -0700299 superstructure_goal_.intake) < kProfileError &&
Austin Schuh9481d0d2019-06-29 21:56:17 -0700300 ::std::abs(superstructure_status_fetcher_->intake
Austin Schuh23b21802016-04-03 21:18:56 -0700301 .goal_angular_velocity) < kProfileError) {
302 LOG(DEBUG, "Profile done.\n");
Austin Schuh9481d0d2019-06-29 21:56:17 -0700303 if (::std::abs(superstructure_status_fetcher_->intake.angle -
Austin Schuh23b21802016-04-03 21:18:56 -0700304 superstructure_goal_.intake) < kEpsilon &&
Austin Schuh9481d0d2019-06-29 21:56:17 -0700305 ::std::abs(superstructure_status_fetcher_->intake
Austin Schuh23b21802016-04-03 21:18:56 -0700306 .angular_velocity) < kEpsilon) {
307 LOG(INFO, "Near goal, done.\n");
308 return true;
309 }
310 }
311 return false;
312}
313
314bool AutonomousActor::SuperstructureProfileDone() {
Austin Schuh9481d0d2019-06-29 21:56:17 -0700315 if (superstructure_status_fetcher_->state < 12 ||
316 superstructure_status_fetcher_->state == 16) {
Austin Schuh23b21802016-04-03 21:18:56 -0700317 LOG(ERROR, "Superstructure no longer running, aborting action\n");
318 return true;
319 }
320
Austin Schuh9481d0d2019-06-29 21:56:17 -0700321 constexpr double kProfileError = 1e-5;
322 return ::std::abs(
323 superstructure_status_fetcher_->intake.goal_angle -
324 superstructure_goal_.intake) < kProfileError &&
325 ::std::abs(
326 superstructure_status_fetcher_->shoulder.goal_angle -
327 superstructure_goal_.shoulder) < kProfileError &&
328 ::std::abs(
329 superstructure_status_fetcher_->wrist.goal_angle -
330 superstructure_goal_.wrist) < kProfileError &&
331 ::std::abs(superstructure_status_fetcher_->intake
332 .goal_angular_velocity) < kProfileError &&
333 ::std::abs(superstructure_status_fetcher_->shoulder
334 .goal_angular_velocity) < kProfileError &&
335 ::std::abs(superstructure_status_fetcher_->wrist
336 .goal_angular_velocity) < kProfileError;
337}
338
339bool AutonomousActor::SuperstructureDone() {
340 superstructure_status_fetcher_.Fetch();
341
342 constexpr double kEpsilon = 0.03;
Austin Schuh23b21802016-04-03 21:18:56 -0700343 if (SuperstructureProfileDone()) {
344 LOG(DEBUG, "Profile done.\n");
Austin Schuh9481d0d2019-06-29 21:56:17 -0700345 if (::std::abs(superstructure_status_fetcher_->intake.angle -
Austin Schuh23b21802016-04-03 21:18:56 -0700346 superstructure_goal_.intake) < (kEpsilon + 0.1) &&
Austin Schuh9481d0d2019-06-29 21:56:17 -0700347 ::std::abs(superstructure_status_fetcher_->shoulder.angle -
Austin Schuh23b21802016-04-03 21:18:56 -0700348 superstructure_goal_.shoulder) < (kEpsilon + 0.05) &&
Austin Schuh9481d0d2019-06-29 21:56:17 -0700349 ::std::abs(superstructure_status_fetcher_->wrist.angle -
Austin Schuh23b21802016-04-03 21:18:56 -0700350 superstructure_goal_.wrist) < (kEpsilon + 0.01) &&
Austin Schuh9481d0d2019-06-29 21:56:17 -0700351 ::std::abs(superstructure_status_fetcher_->intake
Austin Schuh23b21802016-04-03 21:18:56 -0700352 .angular_velocity) < (kEpsilon + 0.1) &&
Austin Schuh9481d0d2019-06-29 21:56:17 -0700353 ::std::abs(superstructure_status_fetcher_->shoulder
Austin Schuh23b21802016-04-03 21:18:56 -0700354 .angular_velocity) < (kEpsilon + 0.10) &&
Austin Schuh9481d0d2019-06-29 21:56:17 -0700355 ::std::abs(superstructure_status_fetcher_->wrist
Austin Schuh23b21802016-04-03 21:18:56 -0700356 .angular_velocity) < (kEpsilon + 0.05)) {
357 LOG(INFO, "Near goal, done.\n");
358 return true;
359 }
360 }
361 return false;
362}
363
364void AutonomousActor::WaitForIntake() {
Austin Schuh9481d0d2019-06-29 21:56:17 -0700365 WaitUntil(::std::bind(&AutonomousActor::IntakeDone, this));
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700366}
367
Comran Morshedb134e772016-03-16 21:05:05 +0000368void AutonomousActor::WaitForSuperstructure() {
Austin Schuh9481d0d2019-06-29 21:56:17 -0700369 WaitUntil(::std::bind(&AutonomousActor::SuperstructureDone, this));
Austin Schuh23b21802016-04-03 21:18:56 -0700370}
Comran Morshedb134e772016-03-16 21:05:05 +0000371
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700372void AutonomousActor::WaitForSuperstructureProfile() {
Austin Schuh9481d0d2019-06-29 21:56:17 -0700373 WaitUntil([this]() {
374 superstructure_status_fetcher_.Fetch();
375 return SuperstructureProfileDone();
376 });
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700377}
378
Austin Schuh23b21802016-04-03 21:18:56 -0700379void AutonomousActor::WaitForSuperstructureLow() {
Austin Schuh9481d0d2019-06-29 21:56:17 -0700380 WaitUntil([this]() {
381 superstructure_status_fetcher_.Fetch();
Comran Morshedb134e772016-03-16 21:05:05 +0000382
Austin Schuh9481d0d2019-06-29 21:56:17 -0700383 return SuperstructureProfileDone() ||
384 superstructure_status_fetcher_->shoulder.angle < 0.1;
385 });
Comran Morshedb134e772016-03-16 21:05:05 +0000386}
Austin Schuh9481d0d2019-06-29 21:56:17 -0700387
Austin Schuh23b21802016-04-03 21:18:56 -0700388void AutonomousActor::BackLongShotLowBarTwoBall() {
389 LOG(INFO, "Expanding for back long shot\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700390 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 -0700391 {10.0, 25.0}, false, 0.0);
392}
393
394void AutonomousActor::BackLongShotTwoBall() {
395 LOG(INFO, "Expanding for back long shot\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700396 MoveSuperstructure(0.00, M_PI / 2.0 - 0.2, -0.55, {7.0, 40.0}, {4.0, 6.0},
397 {10.0, 25.0}, false, 0.0);
398}
399
400void AutonomousActor::BackLongShotTwoBallFinish() {
401 LOG(INFO, "Expanding for back long shot\n");
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000402 MoveSuperstructure(0.00, M_PI / 2.0 - 0.2, -0.625 + 0.03, {7.0, 40.0},
403 {4.0, 6.0}, {10.0, 25.0}, false, 0.0);
Austin Schuh23b21802016-04-03 21:18:56 -0700404}
405
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700406void AutonomousActor::BackLongShot() {
407 LOG(INFO, "Expanding for back long shot\n");
Austin Schuh23b21802016-04-03 21:18:56 -0700408 MoveSuperstructure(0.80, M_PI / 2.0 - 0.2, -0.62, {7.0, 40.0}, {4.0, 6.0},
409 {10.0, 25.0}, false, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700410}
411
412void AutonomousActor::BackMiddleShot() {
413 LOG(INFO, "Expanding for back middle shot\n");
414 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 -0700415 {10.0, 25.0}, false, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700416}
417
Austin Schuh3e4a5272016-04-20 20:11:00 -0700418void AutonomousActor::FrontLongShot() {
419 LOG(INFO, "Expanding for front long shot\n");
420 MoveSuperstructure(0.80, M_PI / 2.0 + 0.1, M_PI + 0.41 + 0.02, {7.0, 40.0},
421 {4.0, 6.0}, {10.0, 25.0}, false, 0.0);
422}
423
424void AutonomousActor::FrontMiddleShot() {
425 LOG(INFO, "Expanding for front middle shot\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700426 MoveSuperstructure(-0.05, M_PI / 2.0 + 0.1, M_PI + 0.44, {7.0, 40.0},
Austin Schuh3e4a5272016-04-20 20:11:00 -0700427 {4.0, 10.0}, {10.0, 25.0}, true, 0.0);
428}
429
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700430void AutonomousActor::TuckArm(bool low_bar, bool traverse_down) {
431 MoveSuperstructure(low_bar ? -0.05 : 2.0, -0.010, 0.0, {7.0, 40.0},
Austin Schuh23b21802016-04-03 21:18:56 -0700432 {4.0, 10.0}, {10.0, 25.0}, !traverse_down, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700433}
434
Austin Schuh3e4a5272016-04-20 20:11:00 -0700435void AutonomousActor::DoFullShot() {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700436 if (ShouldCancel()) return;
437 // Make sure that the base is aligned with the base.
438 LOG(INFO, "Waiting for the superstructure\n");
439 WaitForSuperstructure();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700440
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800441 this_thread::sleep_for(chrono::milliseconds(500));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700442
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700443 if (ShouldCancel()) return;
444 LOG(INFO, "Triggering the vision actor\n");
445 AlignWithVisionGoal();
446
447 // Wait for the drive base to be aligned with the target and make sure that
448 // the shooter is up to speed.
449 LOG(INFO, "Waiting for vision to be aligned\n");
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800450 WaitForAlignedWithVision(chrono::milliseconds(2000));
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700451 if (ShouldCancel()) return;
452 LOG(INFO, "Waiting for shooter to be up to speed\n");
453 WaitForShooterSpeed();
454 if (ShouldCancel()) return;
455
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800456 this_thread::sleep_for(chrono::milliseconds(300));
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700457 LOG(INFO, "Shoot!\n");
458 Shoot();
459
460 // Turn off the shooter and fold up the superstructure.
461 if (ShouldCancel()) return;
462 LOG(INFO, "Stopping shooter\n");
463 SetShooterSpeed(0.0);
464 LOG(INFO, "Folding superstructure back down\n");
465 TuckArm(false, false);
466
467 // Wait for everything to be folded up.
468 LOG(INFO, "Waiting for superstructure to be folded back down\n");
Austin Schuh3e4a5272016-04-20 20:11:00 -0700469 WaitForSuperstructureLow();
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700470}
471
472void AutonomousActor::LowBarDrive() {
473 TuckArm(false, true);
474 StartDrive(-5.5, 0.0, kLowBarDrive, kSlowTurn);
475
476 if (!WaitForDriveNear(5.3, 0.0)) return;
477 TuckArm(true, true);
478
479 if (!WaitForDriveNear(5.0, 0.0)) return;
480
481 StartDrive(0.0, 0.0, kLowBarDrive, kSlowTurn);
482
483 if (!WaitForDriveNear(3.0, 0.0)) return;
484
485 StartDrive(0.0, 0.0, kLowBarDrive, kSlowTurn);
486
487 if (!WaitForDriveNear(1.0, 0.0)) return;
488
Austin Schuh15b5f6a2016-03-26 19:43:56 -0700489 StartDrive(0, -M_PI / 4.0 - 0.2, kLowBarDrive, kSlowTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700490}
491
Austin Schuh3e4a5272016-04-20 20:11:00 -0700492void AutonomousActor::TippyDrive(double goal_distance, double tip_distance,
493 double below, double above) {
494 StartDrive(goal_distance, 0.0, kMoatDrive, kSlowTurn);
495 if (!WaitForBelowAngle(below)) return;
496 if (!WaitForAboveAngle(above)) return;
497 // Ok, we are good now. Compensate by moving the goal by the error.
498 // Should be here at 2.7
499 drivetrain_queue.status.FetchLatest();
500 if (drivetrain_queue.status.get()) {
501 const double left_error =
502 (initial_drivetrain_.left -
503 drivetrain_queue.status->estimated_left_position);
504 const double right_error =
505 (initial_drivetrain_.right -
506 drivetrain_queue.status->estimated_right_position);
507 const double distance_to_go = (left_error + right_error) / 2.0;
508 const double distance_compensation =
509 goal_distance - tip_distance - distance_to_go;
510 LOG(INFO, "Going %f further at the bump\n", distance_compensation);
511 StartDrive(distance_compensation, 0.0, kMoatDrive, kSlowTurn);
512 }
513}
514
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700515void AutonomousActor::MiddleDrive() {
516 TuckArm(false, false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700517 TippyDrive(3.65, 2.7, -0.2, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700518 if (!WaitForDriveDone()) return;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700519}
520
521void AutonomousActor::OneFromMiddleDrive(bool left) {
Austin Schuh3e4a5272016-04-20 20:11:00 -0700522 const double kTurnAngle = left ? -0.41 : 0.41;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700523 TuckArm(false, false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700524 TippyDrive(4.05, 2.7, -0.2, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700525
526 if (!WaitForDriveDone()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700527 StartDrive(0.0, kTurnAngle, kRealignDrive, kFastTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700528}
529
530void AutonomousActor::TwoFromMiddleDrive() {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700531 TuckArm(false, false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700532 constexpr double kDriveDistance = 5.10;
533 TippyDrive(kDriveDistance, 2.7, -0.2, 0.0);
534
535 if (!WaitForDriveNear(kDriveDistance - 3.0, 2.0)) return;
536 StartDrive(0, -M_PI / 2 - 0.10, kMoatDrive, kFastTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700537
538 if (!WaitForDriveDone()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700539 StartDrive(0, M_PI / 3 + 0.35, kMoatDrive, kFastTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700540}
Comran Morshedb134e772016-03-16 21:05:05 +0000541
Austin Schuh23b21802016-04-03 21:18:56 -0700542void AutonomousActor::CloseIfBall() {
Austin Schuh4b652c92019-05-27 13:22:27 -0700543 ball_detector_fetcher_.Fetch();
544 if (ball_detector_fetcher_.get()) {
545 const bool ball_detected = ball_detector_fetcher_->voltage > 2.5;
Austin Schuh23b21802016-04-03 21:18:56 -0700546 if (ball_detected) {
547 CloseShooter();
548 }
549 }
550}
551
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700552void AutonomousActor::WaitForBallOrDriveDone() {
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700553 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700554 event_loop()->monotonic_now(),
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700555 ::std::chrono::milliseconds(5) / 2);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700556 while (true) {
557 if (ShouldCancel()) {
558 return;
559 }
560 phased_loop.SleepUntilNext();
561 drivetrain_queue.status.FetchLatest();
562 if (IsDriveDone()) {
563 return;
564 }
565
Austin Schuh4b652c92019-05-27 13:22:27 -0700566 ball_detector_fetcher_.Fetch();
567 if (ball_detector_fetcher_.get()) {
568 const bool ball_detected = ball_detector_fetcher_->voltage > 2.5;
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700569 if (ball_detected) {
570 return;
571 }
572 }
573 }
574}
575
Austin Schuh3e4a5272016-04-20 20:11:00 -0700576void AutonomousActor::TwoBallAuto() {
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800577 monotonic_clock::time_point start_time = monotonic_clock::now();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700578 OpenShooter();
579 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
580 false, 12.0);
581 if (ShouldCancel()) return;
582 LOG(INFO, "Waiting for the intake to come down.\n");
583
584 WaitForIntake();
585 LOG(INFO, "Intake done at %f seconds, starting to drive\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700586 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700587 if (ShouldCancel()) return;
588 const double kDriveDistance = 5.05;
589 StartDrive(-kDriveDistance, 0.0, kTwoBallLowDrive, kSlowTurn);
590
591 StartDrive(0.0, 0.4, kTwoBallLowDrive, kSwerveTurn);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700592 if (!WaitForDriveNear(kDriveDistance - 0.5, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700593
Austin Schuh295c2d92016-05-01 12:28:04 -0700594 // Check if the ball is there.
595 bool first_ball_there = true;
Austin Schuh4b652c92019-05-27 13:22:27 -0700596 ball_detector_fetcher_.Fetch();
597 if (ball_detector_fetcher_.get()) {
598 const bool ball_detected = ball_detector_fetcher_->voltage > 2.5;
Austin Schuh295c2d92016-05-01 12:28:04 -0700599 first_ball_there = ball_detected;
600 LOG(INFO, "Saw the ball: %d at %f\n", first_ball_there,
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700601 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh295c2d92016-05-01 12:28:04 -0700602 }
Austin Schuh3e4a5272016-04-20 20:11:00 -0700603 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 40.0}, {4.0, 10.0}, {10.0, 25.0},
604 false, 0.0);
605 LOG(INFO, "Shutting off rollers at %f seconds, starting to straighten out\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700606 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700607 StartDrive(0.0, -0.4, kTwoBallLowDrive, kSwerveTurn);
608 MoveSuperstructure(-0.05, -0.010, 0.0, {8.0, 40.0}, {4.0, 10.0}, {10.0, 25.0},
609 false, 0.0);
610 CloseShooter();
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700611 if (!WaitForDriveNear(kDriveDistance - 2.4, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700612
613 // We are now under the low bar. Start lifting.
614 BackLongShotLowBarTwoBall();
615 LOG(INFO, "Spinning up the shooter wheels\n");
616 SetShooterSpeed(640.0);
617 StartDrive(0.0, 0.0, kTwoBallFastDrive, kSwerveTurn);
618
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700619 if (!WaitForDriveNear(1.50, kDoNotTurnCare)) return;
620 constexpr double kShootTurnAngle = -M_PI / 4.0 - 0.05;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700621 StartDrive(0, kShootTurnAngle, kTwoBallFastDrive, kFinishTurn);
622 BackLongShotTwoBall();
623
624 if (!WaitForDriveDone()) return;
625 LOG(INFO, "First shot done driving at %f seconds\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700626 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700627
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700628 WaitForSuperstructureProfile();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700629
630 if (ShouldCancel()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700631 AlignWithVisionGoal();
632
633 WaitForShooterSpeed();
634 if (ShouldCancel()) return;
635
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800636 constexpr chrono::milliseconds kVisionExtra{0};
637 WaitForAlignedWithVision(chrono::milliseconds(500) + kVisionExtra);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700638 BackLongShotTwoBallFinish();
639 WaitForSuperstructureProfile();
640 if (ShouldCancel()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700641 LOG(INFO, "Shoot!\n");
Austin Schuh295c2d92016-05-01 12:28:04 -0700642 if (first_ball_there) {
643 Shoot();
644 } else {
645 LOG(INFO, "Nah, not shooting\n");
646 }
Austin Schuh3e4a5272016-04-20 20:11:00 -0700647
648 LOG(INFO, "First shot at %f seconds\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700649 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700650 if (ShouldCancel()) return;
651
652 SetShooterSpeed(0.0);
653 LOG(INFO, "Folding superstructure back down\n");
654 TuckArm(true, true);
655
656 // Undo vision move.
657 StartDrive(0.0, 0.0, kTwoBallFastDrive, kFinishTurn);
658 if (!WaitForDriveDone()) return;
659
660 constexpr double kBackDrive = 3.09 - 0.4;
661 StartDrive(kBackDrive, 0.0, kTwoBallReturnDrive, kSlowTurn);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700662 if (!WaitForDriveNear(kBackDrive - 0.19, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700663 StartDrive(0, -kShootTurnAngle, kTwoBallReturnDrive, kSwerveTurn);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700664 if (!WaitForDriveNear(1.0, kDoNotTurnCare)) return;
665 StartDrive(0, 0, kTwoBallReturnSlow, kSwerveTurn);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700666
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700667 if (!WaitForDriveNear(0.06, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700668 LOG(INFO, "At Low Bar %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700669 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700670
671 OpenShooter();
672 constexpr double kSecondBallAfterBarDrive = 2.10;
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700673 StartDrive(kSecondBallAfterBarDrive, 0.0, kTwoBallBallPickupAccel, kSlowTurn);
674 if (!WaitForDriveNear(kSecondBallAfterBarDrive - 0.5, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700675 constexpr double kBallSmallWallTurn = -0.11;
676 StartDrive(0, kBallSmallWallTurn, kTwoBallBallPickup, kFinishTurn);
677
678 MoveSuperstructure(0.03, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
679 false, 12.0);
680
681 if (!WaitForDriveProfileDone()) return;
682
683 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
684 false, 12.0);
685
686 LOG(INFO, "Done backing up %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700687 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700688
689 constexpr double kDriveBackDistance = 5.15 - 0.4;
690 StartDrive(-kDriveBackDistance, 0.0, kTwoBallLowDrive, kFinishTurn);
691 CloseIfBall();
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700692 if (!WaitForDriveNear(kDriveBackDistance - 0.75, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700693
694 StartDrive(0.0, -kBallSmallWallTurn, kTwoBallLowDrive, kFinishTurn);
695 LOG(INFO, "Straightening up at %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700696 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700697
698 CloseIfBall();
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700699 if (!WaitForDriveNear(kDriveBackDistance - 2.3, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700700
Austin Schuh4b652c92019-05-27 13:22:27 -0700701 ball_detector_fetcher_.Fetch();
702 if (ball_detector_fetcher_.get()) {
703 const bool ball_detected = ball_detector_fetcher_->voltage > 2.5;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700704 if (!ball_detected) {
705 if (!WaitForDriveDone()) return;
706 LOG(INFO, "Aborting, no ball %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700707 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700708 return;
709 }
710 }
711 CloseShooter();
712
713 BackLongShotLowBarTwoBall();
714 LOG(INFO, "Spinning up the shooter wheels\n");
715 SetShooterSpeed(640.0);
716 StartDrive(0.0, 0.0, kTwoBallFastDrive, kSwerveTurn);
717
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700718 if (!WaitForDriveNear(1.80, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700719 StartDrive(0, kShootTurnAngle, kTwoBallFastDrive, kFinishTurn);
720 BackLongShotTwoBall();
721
722 if (!WaitForDriveDone()) return;
723 LOG(INFO, "Second shot done driving at %f seconds\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700724 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700725 WaitForSuperstructure();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700726 AlignWithVisionGoal();
727 if (ShouldCancel()) return;
728
729 WaitForShooterSpeed();
730 if (ShouldCancel()) return;
731
732 // 2.2 with 0.4 of vision.
733 // 1.8 without any vision.
734 LOG(INFO, "Going to vision align at %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700735 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800736 WaitForAlignedWithVision(
737 (start_time + chrono::milliseconds(13500) + kVisionExtra * 2) -
738 monotonic_clock::now());
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700739 BackLongShotTwoBallFinish();
740 WaitForSuperstructureProfile();
741 if (ShouldCancel()) return;
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800742 LOG(INFO, "Shoot at %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700743 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700744 Shoot();
745
746 LOG(INFO, "Second shot at %f seconds\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700747 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700748 if (ShouldCancel()) return;
749
750 SetShooterSpeed(0.0);
751 LOG(INFO, "Folding superstructure back down\n");
752 TuckArm(true, false);
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700753 LOG(INFO, "Shot %f\n",
754 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700755
756 WaitForSuperstructureLow();
757
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700758 LOG(INFO, "Done %f\n",
759 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700760}
761
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700762void AutonomousActor::StealAndMoveOverBy(double distance) {
763 OpenShooter();
764 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
765 true, 12.0);
766 if (ShouldCancel()) return;
767 LOG(INFO, "Waiting for the intake to come down.\n");
768
769 WaitForIntake();
770 if (ShouldCancel()) return;
771 StartDrive(-distance, M_PI / 2.0, kFastDrive, kStealTurn);
772 WaitForBallOrDriveDone();
773 if (ShouldCancel()) return;
774 MoveSuperstructure(1.0, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
775 true, 12.0);
776
777 if (!WaitForDriveDone()) return;
778 StartDrive(0.0, M_PI / 2.0, kFastDrive, kStealTurn);
779 if (!WaitForDriveDone()) return;
780}
781
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000782bool AutonomousActor::RunAction(
783 const ::frc971::autonomous::AutonomousActionParams &params) {
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800784 monotonic_clock::time_point start_time = monotonic_clock::now();
Comran Morshede68e3732016-03-12 14:12:11 +0000785 LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode);
786
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.
795 LOG(INFO, "Unfolding superstructure\n");
796 FrontLongShot();
797
798 // Spin up the shooter wheels.
799 LOG(INFO, "Spinning up the shooter wheels\n");
800 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.
807 LOG(INFO, "Unfolding superstructure\n");
808 FrontMiddleShot();
809
810 // Spin up the shooter wheels.
811 LOG(INFO, "Spinning up the shooter wheels\n");
812 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.
819 LOG(INFO, "Unfolding superstructure\n");
820 FrontMiddleShot();
821
822 // Spin up the shooter wheels.
823 LOG(INFO, "Spinning up the shooter wheels\n");
824 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.
831 LOG(INFO, "Unfolding superstructure\n");
832 FrontMiddleShot();
833
834 // Spin up the shooter wheels.
835 LOG(INFO, "Spinning up the shooter wheels\n");
836 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.
843 LOG(INFO, "Unfolding superstructure\n");
844 FrontMiddleShot();
845
846 // Spin up the shooter wheels.
847 LOG(INFO, "Spinning up the shooter wheels\n");
848 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.
863 LOG(INFO, "Unfolding superstructure\n");
864 FrontMiddleShot();
865
866 // Spin up the shooter wheels.
867 LOG(INFO, "Spinning up the shooter wheels\n");
868 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.
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 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.
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 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.
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;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700916 default:
Austin Schuh6c9bc622016-03-26 19:44:12 -0700917 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
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700926 LOG(INFO, "Done %f\n",
927 ::aos::time::DurationInSeconds(monotonic_clock::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 Schuhd32b3622019-06-23 18:49:06 -0700930 event_loop()->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 }
Comran Morshed435f1112016-03-12 14:20:45 +0000936 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