blob: 199dda5ba9ec89e5337ea4f3405d567eee8dc95d [file] [log] [blame]
Comran Morshede68e3732016-03-12 14:12:11 +00001#include "y2016/actors/autonomous_actor.h"
2
3#include <inttypes.h>
4
Comran Morshedb134e772016-03-16 21:05:05 +00005#include <cmath>
6
Comran Morshede68e3732016-03-12 14:12:11 +00007#include "aos/common/util/phased_loop.h"
8#include "aos/common/logging/logging.h"
Comran Morshed435f1112016-03-12 14:20:45 +00009
10#include "frc971/control_loops/drivetrain/drivetrain.q.h"
11#include "y2016/control_loops/drivetrain/drivetrain_base.h"
Austin Schuhf59b8ee2016-03-19 21:31:36 -070012#include "y2016/control_loops/shooter/shooter.q.h"
Comran Morshedb134e772016-03-16 21:05:05 +000013#include "y2016/control_loops/superstructure/superstructure.q.h"
14#include "y2016/actors/autonomous_action.q.h"
Austin Schuh23b21802016-04-03 21:18:56 -070015#include "y2016/queues/ball_detector.q.h"
Austin Schuhf59b8ee2016-03-19 21:31:36 -070016#include "y2016/vision/vision.q.h"
Comran Morshede68e3732016-03-12 14:12:11 +000017
18namespace y2016 {
19namespace actors {
Comran Morshed435f1112016-03-12 14:20:45 +000020using ::frc971::control_loops::drivetrain_queue;
21
22namespace {
Austin Schuhf59b8ee2016-03-19 21:31:36 -070023const ProfileParameters kSlowDrive = {0.8, 2.5};
24const ProfileParameters kLowBarDrive = {1.3, 2.5};
25const ProfileParameters kMoatDrive = {1.2, 3.5};
26const ProfileParameters kRealignDrive = {2.0, 2.5};
27const ProfileParameters kRockWallDrive = {0.8, 2.5};
Comran Morshed435f1112016-03-12 14:20:45 +000028const ProfileParameters kFastDrive = {3.0, 2.5};
29
Austin Schuhf59b8ee2016-03-19 21:31:36 -070030const ProfileParameters kSlowTurn = {0.8, 3.0};
Comran Morshed435f1112016-03-12 14:20:45 +000031const ProfileParameters kFastTurn = {3.0, 10.0};
Austin Schuhe4ec49c2016-04-24 19:07:15 -070032const ProfileParameters kStealTurn = {4.0, 15.0};
Austin Schuh23b21802016-04-03 21:18:56 -070033const ProfileParameters kSwerveTurn = {2.0, 7.0};
34const ProfileParameters kFinishTurn = {2.0, 5.0};
35
36const ProfileParameters kTwoBallLowDrive = {1.7, 3.5};
37const ProfileParameters kTwoBallFastDrive = {3.0, 1.5};
38const ProfileParameters kTwoBallReturnDrive = {3.0, 1.9};
Austin Schuhe4ec49c2016-04-24 19:07:15 -070039const ProfileParameters kTwoBallReturnSlow = {3.0, 2.5};
Austin Schuh23b21802016-04-03 21:18:56 -070040const ProfileParameters kTwoBallBallPickup = {2.0, 1.75};
Austin Schuhe4ec49c2016-04-24 19:07:15 -070041const ProfileParameters kTwoBallBallPickupAccel = {2.0, 2.5};
Comran Morshed435f1112016-03-12 14:20:45 +000042} // namespace
Comran Morshede68e3732016-03-12 14:12:11 +000043
44AutonomousActor::AutonomousActor(actors::AutonomousActionQueueGroup *s)
Comran Morshed435f1112016-03-12 14:20:45 +000045 : aos::common::actions::ActorBase<actors::AutonomousActionQueueGroup>(s),
Comran Morshedb134e772016-03-16 21:05:05 +000046 dt_config_(control_loops::drivetrain::GetDrivetrainConfig()),
47 initial_drivetrain_({0.0, 0.0}) {}
Comran Morshed435f1112016-03-12 14:20:45 +000048
49void AutonomousActor::ResetDrivetrain() {
50 LOG(INFO, "resetting the drivetrain\n");
51 drivetrain_queue.goal.MakeWithBuilder()
52 .control_loop_driving(false)
53 .highgear(true)
54 .steering(0.0)
55 .throttle(0.0)
Comran Morshedb134e772016-03-16 21:05:05 +000056 .left_goal(initial_drivetrain_.left)
Comran Morshed435f1112016-03-12 14:20:45 +000057 .left_velocity_goal(0)
Comran Morshedb134e772016-03-16 21:05:05 +000058 .right_goal(initial_drivetrain_.right)
Comran Morshed435f1112016-03-12 14:20:45 +000059 .right_velocity_goal(0)
60 .Send();
61}
62
63void AutonomousActor::StartDrive(double distance, double angle,
64 ProfileParameters linear,
65 ProfileParameters angular) {
66 {
Austin Schuh23b21802016-04-03 21:18:56 -070067 LOG(INFO, "Driving distance %f, angle %f\n", distance, angle);
Comran Morshed435f1112016-03-12 14:20:45 +000068 {
69 const double dangle = angle * dt_config_.robot_radius;
Comran Morshedb134e772016-03-16 21:05:05 +000070 initial_drivetrain_.left += distance - dangle;
71 initial_drivetrain_.right += distance + dangle;
Comran Morshed435f1112016-03-12 14:20:45 +000072 }
73
Comran Morshedb134e772016-03-16 21:05:05 +000074 auto drivetrain_message = drivetrain_queue.goal.MakeMessage();
Comran Morshed435f1112016-03-12 14:20:45 +000075 drivetrain_message->control_loop_driving = true;
76 drivetrain_message->highgear = true;
77 drivetrain_message->steering = 0.0;
78 drivetrain_message->throttle = 0.0;
Comran Morshedb134e772016-03-16 21:05:05 +000079 drivetrain_message->left_goal = initial_drivetrain_.left;
Comran Morshed435f1112016-03-12 14:20:45 +000080 drivetrain_message->left_velocity_goal = 0;
Comran Morshedb134e772016-03-16 21:05:05 +000081 drivetrain_message->right_goal = initial_drivetrain_.right;
Comran Morshed435f1112016-03-12 14:20:45 +000082 drivetrain_message->right_velocity_goal = 0;
83 drivetrain_message->linear = linear;
84 drivetrain_message->angular = angular;
85
86 LOG_STRUCT(DEBUG, "drivetrain_goal", *drivetrain_message);
87
88 drivetrain_message.Send();
89 }
90}
91
92void AutonomousActor::InitializeEncoders() {
93 drivetrain_queue.status.FetchAnother();
Comran Morshedb134e772016-03-16 21:05:05 +000094 initial_drivetrain_.left = drivetrain_queue.status->estimated_left_position;
95 initial_drivetrain_.right = drivetrain_queue.status->estimated_right_position;
Comran Morshed435f1112016-03-12 14:20:45 +000096}
97
98void AutonomousActor::WaitUntilDoneOrCanceled(
99 ::std::unique_ptr<aos::common::actions::Action> action) {
100 if (!action) {
101 LOG(ERROR, "No action, not waiting\n");
102 return;
103 }
104
105 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5),
106 ::aos::time::Time::InMS(5) / 2);
107 while (true) {
108 // Poll the running bit and see if we should cancel.
109 phased_loop.SleepUntilNext();
110 if (!action->Running() || ShouldCancel()) {
111 return;
112 }
113 }
114}
115
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700116constexpr double kDoNotTurnCare = 2.0;
117
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700118bool AutonomousActor::WaitForDriveNear(double distance, double angle) {
119 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5),
120 ::aos::time::Time::InMS(5) / 2);
121 constexpr double kPositionTolerance = 0.02;
122 constexpr double kProfileTolerance = 0.001;
123
124 while (true) {
125 if (ShouldCancel()) {
126 return false;
127 }
128 phased_loop.SleepUntilNext();
129 drivetrain_queue.status.FetchLatest();
130 if (drivetrain_queue.status.get()) {
131 const double left_profile_error =
132 (initial_drivetrain_.left -
133 drivetrain_queue.status->profiled_left_position_goal);
134 const double right_profile_error =
135 (initial_drivetrain_.right -
136 drivetrain_queue.status->profiled_right_position_goal);
137
138 const double left_error =
139 (initial_drivetrain_.left -
140 drivetrain_queue.status->estimated_left_position);
141 const double right_error =
142 (initial_drivetrain_.right -
143 drivetrain_queue.status->estimated_right_position);
144
145 const double profile_distance_to_go =
146 (left_profile_error + right_profile_error) / 2.0;
147 const double profile_angle_to_go =
148 (right_profile_error - left_profile_error) /
149 (dt_config_.robot_radius * 2.0);
150
151 const double distance_to_go = (left_error + right_error) / 2.0;
152 const double angle_to_go =
153 (right_error - left_error) / (dt_config_.robot_radius * 2.0);
154
155 if (::std::abs(profile_distance_to_go) < distance + kProfileTolerance &&
156 ::std::abs(profile_angle_to_go) < angle + kProfileTolerance &&
157 ::std::abs(distance_to_go) < distance + kPositionTolerance &&
158 ::std::abs(angle_to_go) < angle + kPositionTolerance) {
159 LOG(INFO, "Closer than %f distance, %f angle\n", distance, angle);
160 return true;
161 }
162 }
163 }
164}
165
Austin Schuh23b21802016-04-03 21:18:56 -0700166bool AutonomousActor::WaitForDriveProfileDone() {
167 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5),
168 ::aos::time::Time::InMS(5) / 2);
169 constexpr double kProfileTolerance = 0.001;
170
171 while (true) {
172 if (ShouldCancel()) {
173 return false;
174 }
175 phased_loop.SleepUntilNext();
176 drivetrain_queue.status.FetchLatest();
177 if (drivetrain_queue.status.get()) {
178 if (::std::abs(drivetrain_queue.status->profiled_left_position_goal -
179 initial_drivetrain_.left) < kProfileTolerance &&
180 ::std::abs(drivetrain_queue.status->profiled_right_position_goal -
181 initial_drivetrain_.right) < kProfileTolerance) {
182 LOG(INFO, "Finished drive\n");
183 return true;
184 }
185 }
186 }
187}
188
Austin Schuh3e4a5272016-04-20 20:11:00 -0700189bool AutonomousActor::WaitForMaxBy(double angle) {
Comran Morshed435f1112016-03-12 14:20:45 +0000190 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5),
191 ::aos::time::Time::InMS(5) / 2);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700192 double max_angle = -M_PI;
193 while (true) {
194 if (ShouldCancel()) {
195 return false;
196 }
197 phased_loop.SleepUntilNext();
198 drivetrain_queue.status.FetchLatest();
199 if (IsDriveDone()) {
200 return true;
201 }
202 if (drivetrain_queue.status.get()) {
203 if (drivetrain_queue.status->ground_angle > max_angle) {
204 max_angle = drivetrain_queue.status->ground_angle;
205 }
206 if (drivetrain_queue.status->ground_angle < max_angle - angle) {
207 return true;
208 }
209 }
210 }
211}
212
213bool AutonomousActor::WaitForAboveAngle(double angle) {
214 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5),
215 ::aos::time::Time::InMS(5) / 2);
216 while (true) {
217 if (ShouldCancel()) {
218 return false;
219 }
220 phased_loop.SleepUntilNext();
221 drivetrain_queue.status.FetchLatest();
222 if (IsDriveDone()) {
223 return true;
224 }
225 if (drivetrain_queue.status.get()) {
226 if (drivetrain_queue.status->ground_angle > angle) {
227 return true;
228 }
229 }
230 }
231}
232
233bool AutonomousActor::WaitForBelowAngle(double angle) {
234 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5),
235 ::aos::time::Time::InMS(5) / 2);
236 while (true) {
237 if (ShouldCancel()) {
238 return false;
239 }
240 phased_loop.SleepUntilNext();
241 drivetrain_queue.status.FetchLatest();
242 if (IsDriveDone()) {
243 return true;
244 }
245 if (drivetrain_queue.status.get()) {
246 if (drivetrain_queue.status->ground_angle < angle) {
247 return true;
248 }
249 }
250 }
251}
252
253bool AutonomousActor::IsDriveDone() {
Comran Morshed435f1112016-03-12 14:20:45 +0000254 constexpr double kPositionTolerance = 0.02;
255 constexpr double kVelocityTolerance = 0.10;
256 constexpr double kProfileTolerance = 0.001;
257
Austin Schuh3e4a5272016-04-20 20:11:00 -0700258 if (drivetrain_queue.status.get()) {
259 if (::std::abs(drivetrain_queue.status->profiled_left_position_goal -
260 initial_drivetrain_.left) < kProfileTolerance &&
261 ::std::abs(drivetrain_queue.status->profiled_right_position_goal -
262 initial_drivetrain_.right) < kProfileTolerance &&
263 ::std::abs(drivetrain_queue.status->estimated_left_position -
264 initial_drivetrain_.left) < kPositionTolerance &&
265 ::std::abs(drivetrain_queue.status->estimated_right_position -
266 initial_drivetrain_.right) < kPositionTolerance &&
267 ::std::abs(drivetrain_queue.status->estimated_left_velocity) <
268 kVelocityTolerance &&
269 ::std::abs(drivetrain_queue.status->estimated_right_velocity) <
270 kVelocityTolerance) {
271 LOG(INFO, "Finished drive\n");
272 return true;
273 }
274 }
275 return false;
276}
277
278bool AutonomousActor::WaitForDriveDone() {
279 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5),
280 ::aos::time::Time::InMS(5) / 2);
281
Comran Morshed435f1112016-03-12 14:20:45 +0000282 while (true) {
283 if (ShouldCancel()) {
284 return false;
285 }
286 phased_loop.SleepUntilNext();
287 drivetrain_queue.status.FetchLatest();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700288 if (IsDriveDone()) {
289 return true;
Comran Morshed435f1112016-03-12 14:20:45 +0000290 }
291 }
292}
Comran Morshede68e3732016-03-12 14:12:11 +0000293
Comran Morshedb134e772016-03-16 21:05:05 +0000294void AutonomousActor::MoveSuperstructure(
295 double intake, double shoulder, double wrist,
296 const ProfileParameters intake_params,
297 const ProfileParameters shoulder_params,
Austin Schuh23b21802016-04-03 21:18:56 -0700298 const ProfileParameters wrist_params, bool traverse_up,
299 double roller_power) {
Comran Morshedb134e772016-03-16 21:05:05 +0000300 superstructure_goal_ = {intake, shoulder, wrist};
301
302 auto new_superstructure_goal =
303 ::y2016::control_loops::superstructure_queue.goal.MakeMessage();
304
305 new_superstructure_goal->angle_intake = intake;
306 new_superstructure_goal->angle_shoulder = shoulder;
307 new_superstructure_goal->angle_wrist = wrist;
308
309 new_superstructure_goal->max_angular_velocity_intake =
310 intake_params.max_velocity;
311 new_superstructure_goal->max_angular_velocity_shoulder =
312 shoulder_params.max_velocity;
313 new_superstructure_goal->max_angular_velocity_wrist =
314 wrist_params.max_velocity;
315
316 new_superstructure_goal->max_angular_acceleration_intake =
317 intake_params.max_acceleration;
318 new_superstructure_goal->max_angular_acceleration_shoulder =
319 shoulder_params.max_acceleration;
320 new_superstructure_goal->max_angular_acceleration_wrist =
321 wrist_params.max_acceleration;
322
Austin Schuh23b21802016-04-03 21:18:56 -0700323 new_superstructure_goal->voltage_top_rollers = roller_power;
324 new_superstructure_goal->voltage_bottom_rollers = roller_power;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700325
326 new_superstructure_goal->traverse_unlatched = true;
327 new_superstructure_goal->traverse_down = !traverse_up;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700328 new_superstructure_goal->voltage_climber = 0.0;
329 new_superstructure_goal->unfold_climber = false;
Comran Morshedb134e772016-03-16 21:05:05 +0000330
331 if (!new_superstructure_goal.Send()) {
332 LOG(ERROR, "Sending superstructure goal failed.\n");
333 }
334}
335
Austin Schuh23b21802016-04-03 21:18:56 -0700336void AutonomousActor::OpenShooter() {
337 shooter_speed_ = 0.0;
338
339 if (!control_loops::shooter::shooter_queue.goal.MakeWithBuilder()
340 .angular_velocity(shooter_speed_)
341 .clamp_open(true)
342 .push_to_shooter(false)
343 .force_lights_on(false)
344 .Send()) {
345 LOG(ERROR, "Sending shooter goal failed.\n");
346 }
347}
348
349void AutonomousActor::CloseShooter() {
350 shooter_speed_ = 0.0;
351
352 if (!control_loops::shooter::shooter_queue.goal.MakeWithBuilder()
353 .angular_velocity(shooter_speed_)
354 .clamp_open(false)
355 .push_to_shooter(false)
356 .force_lights_on(false)
357 .Send()) {
358 LOG(ERROR, "Sending shooter goal failed.\n");
359 }
360}
361
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700362void AutonomousActor::SetShooterSpeed(double speed) {
363 shooter_speed_ = speed;
364
365 // In auto, we want to have the lights on whenever possible since we have no
366 // hope of a human aligning the robot.
367 bool force_lights_on = shooter_speed_ > 1.0;
368
369 if (!control_loops::shooter::shooter_queue.goal.MakeWithBuilder()
370 .angular_velocity(shooter_speed_)
371 .clamp_open(false)
372 .push_to_shooter(false)
373 .force_lights_on(force_lights_on)
374 .Send()) {
375 LOG(ERROR, "Sending shooter goal failed.\n");
376 }
377}
378
379void AutonomousActor::Shoot() {
380 uint32_t initial_shots = 0;
381
382 control_loops::shooter::shooter_queue.status.FetchLatest();
383 if (control_loops::shooter::shooter_queue.status.get()) {
384 initial_shots = control_loops::shooter::shooter_queue.status->shots;
385 }
386
387 // In auto, we want to have the lights on whenever possible since we have no
388 // hope of a human aligning the robot.
389 bool force_lights_on = shooter_speed_ > 1.0;
390
391 if (!control_loops::shooter::shooter_queue.goal.MakeWithBuilder()
392 .angular_velocity(shooter_speed_)
393 .clamp_open(false)
394 .push_to_shooter(true)
395 .force_lights_on(force_lights_on)
396 .Send()) {
397 LOG(ERROR, "Sending shooter goal failed.\n");
398 }
399
400 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5),
401 ::aos::time::Time::InMS(5) / 2);
402 while (true) {
403 if (ShouldCancel()) return;
404
405 // Wait for the shot count to change so we know when the shot is complete.
406 control_loops::shooter::shooter_queue.status.FetchLatest();
407 if (control_loops::shooter::shooter_queue.status.get()) {
408 if (initial_shots < control_loops::shooter::shooter_queue.status->shots) {
409 return;
410 }
411 }
412 phased_loop.SleepUntilNext();
413 }
414}
415
416void AutonomousActor::WaitForShooterSpeed() {
417 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5),
418 ::aos::time::Time::InMS(5) / 2);
419 while (true) {
420 if (ShouldCancel()) return;
421
422 control_loops::shooter::shooter_queue.status.FetchLatest();
423 if (control_loops::shooter::shooter_queue.status.get()) {
424 if (control_loops::shooter::shooter_queue.status->left.ready &&
425 control_loops::shooter::shooter_queue.status->right.ready) {
426 return;
427 }
428 }
429 phased_loop.SleepUntilNext();
430 }
431}
432
433void AutonomousActor::AlignWithVisionGoal() {
434 actors::VisionAlignActionParams params;
435 vision_action_ = ::std::move(actors::MakeVisionAlignAction(params));
436 vision_action_->Start();
437}
438
Austin Schuh23b21802016-04-03 21:18:56 -0700439void AutonomousActor::WaitForAlignedWithVision(
440 ::aos::time::Time align_duration) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700441 bool vision_valid = false;
442 double last_angle = 0.0;
443 int ready_to_fire = 0;
444
445 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5),
446 ::aos::time::Time::InMS(5) / 2);
447 ::aos::time::Time end_time =
Austin Schuh23b21802016-04-03 21:18:56 -0700448 ::aos::time::Time::Now() + align_duration;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700449 while (end_time > ::aos::time::Time::Now()) {
450 if (ShouldCancel()) break;
451
452 ::y2016::vision::vision_status.FetchLatest();
453 if (::y2016::vision::vision_status.get()) {
454 vision_valid = (::y2016::vision::vision_status->left_image_valid &&
455 ::y2016::vision::vision_status->right_image_valid);
456 last_angle = ::y2016::vision::vision_status->horizontal_angle;
457 }
458
459 drivetrain_queue.status.FetchLatest();
460 drivetrain_queue.goal.FetchLatest();
461
462 if (drivetrain_queue.status.get() && drivetrain_queue.goal.get()) {
463 const double left_goal = drivetrain_queue.goal->left_goal;
464 const double right_goal = drivetrain_queue.goal->right_goal;
465 const double left_current =
466 drivetrain_queue.status->estimated_left_position;
467 const double right_current =
468 drivetrain_queue.status->estimated_right_position;
469 const double left_velocity =
470 drivetrain_queue.status->estimated_left_velocity;
471 const double right_velocity =
472 drivetrain_queue.status->estimated_right_velocity;
473
474 if (vision_valid && ::std::abs(last_angle) < 0.02 &&
475 ::std::abs((left_goal - right_goal) -
476 (left_current - right_current)) /
477 dt_config_.robot_radius / 2.0 <
478 0.02 &&
479 ::std::abs(left_velocity - right_velocity) < 0.01) {
480 ++ready_to_fire;
481 } else {
482 ready_to_fire = 0;
483 }
Austin Schuh3e4a5272016-04-20 20:11:00 -0700484 if (ready_to_fire > 15) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700485 break;
Austin Schuh23b21802016-04-03 21:18:56 -0700486 LOG(INFO, "Vision align success!\n");
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700487 }
488 }
489 phased_loop.SleepUntilNext();
490 }
491
492 vision_action_->Cancel();
493 WaitUntilDoneOrCanceled(::std::move(vision_action_));
Austin Schuh23b21802016-04-03 21:18:56 -0700494 LOG(INFO, "Done waiting for vision\n");
495}
496
497bool AutonomousActor::IntakeDone() {
498 control_loops::superstructure_queue.status.FetchAnother();
499
500 constexpr double kProfileError = 1e-5;
501 constexpr double kEpsilon = 0.15;
502
503 if (control_loops::superstructure_queue.status->state < 12 ||
504 control_loops::superstructure_queue.status->state == 16) {
505 LOG(ERROR, "Superstructure no longer running, aborting action\n");
506 return true;
507 }
508
509 if (::std::abs(control_loops::superstructure_queue.status->intake.goal_angle -
510 superstructure_goal_.intake) < kProfileError &&
511 ::std::abs(control_loops::superstructure_queue.status->intake
512 .goal_angular_velocity) < kProfileError) {
513 LOG(DEBUG, "Profile done.\n");
514 if (::std::abs(control_loops::superstructure_queue.status->intake.angle -
515 superstructure_goal_.intake) < kEpsilon &&
516 ::std::abs(control_loops::superstructure_queue.status->intake
517 .angular_velocity) < kEpsilon) {
518 LOG(INFO, "Near goal, done.\n");
519 return true;
520 }
521 }
522 return false;
523}
524
525bool AutonomousActor::SuperstructureProfileDone() {
526 constexpr double kProfileError = 1e-5;
527 return ::std::abs(
528 control_loops::superstructure_queue.status->intake.goal_angle -
529 superstructure_goal_.intake) < kProfileError &&
530 ::std::abs(
531 control_loops::superstructure_queue.status->shoulder.goal_angle -
532 superstructure_goal_.shoulder) < kProfileError &&
533 ::std::abs(
534 control_loops::superstructure_queue.status->wrist.goal_angle -
535 superstructure_goal_.wrist) < kProfileError &&
536 ::std::abs(control_loops::superstructure_queue.status->intake
537 .goal_angular_velocity) < kProfileError &&
538 ::std::abs(control_loops::superstructure_queue.status->shoulder
539 .goal_angular_velocity) < kProfileError &&
540 ::std::abs(control_loops::superstructure_queue.status->wrist
541 .goal_angular_velocity) < kProfileError;
542}
543
544bool AutonomousActor::SuperstructureDone() {
545 control_loops::superstructure_queue.status.FetchAnother();
546
547 constexpr double kEpsilon = 0.03;
548
549 if (control_loops::superstructure_queue.status->state < 12 ||
550 control_loops::superstructure_queue.status->state == 16) {
551 LOG(ERROR, "Superstructure no longer running, aborting action\n");
552 return true;
553 }
554
555 if (SuperstructureProfileDone()) {
556 LOG(DEBUG, "Profile done.\n");
557 if (::std::abs(control_loops::superstructure_queue.status->intake.angle -
558 superstructure_goal_.intake) < (kEpsilon + 0.1) &&
559 ::std::abs(control_loops::superstructure_queue.status->shoulder.angle -
560 superstructure_goal_.shoulder) < (kEpsilon + 0.05) &&
561 ::std::abs(control_loops::superstructure_queue.status->wrist.angle -
562 superstructure_goal_.wrist) < (kEpsilon + 0.01) &&
563 ::std::abs(control_loops::superstructure_queue.status->intake
564 .angular_velocity) < (kEpsilon + 0.1) &&
565 ::std::abs(control_loops::superstructure_queue.status->shoulder
566 .angular_velocity) < (kEpsilon + 0.10) &&
567 ::std::abs(control_loops::superstructure_queue.status->wrist
568 .angular_velocity) < (kEpsilon + 0.05)) {
569 LOG(INFO, "Near goal, done.\n");
570 return true;
571 }
572 }
573 return false;
574}
575
576void AutonomousActor::WaitForIntake() {
577 while (true) {
578 if (ShouldCancel()) return;
579 if (IntakeDone()) return;
580 }
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700581}
582
Comran Morshedb134e772016-03-16 21:05:05 +0000583void AutonomousActor::WaitForSuperstructure() {
584 while (true) {
585 if (ShouldCancel()) return;
Austin Schuh23b21802016-04-03 21:18:56 -0700586 if (SuperstructureDone()) return;
587 }
588}
Comran Morshedb134e772016-03-16 21:05:05 +0000589
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700590void AutonomousActor::WaitForSuperstructureProfile() {
591 while (true) {
592 if (ShouldCancel()) return;
593 control_loops::superstructure_queue.status.FetchAnother();
594
595 if (control_loops::superstructure_queue.status->state < 12 ||
596 control_loops::superstructure_queue.status->state == 16) {
597 LOG(ERROR, "Superstructure no longer running, aborting action\n");
598 return;
599 }
600
601 if (SuperstructureProfileDone()) return;
602 }
603}
604
Austin Schuh23b21802016-04-03 21:18:56 -0700605void AutonomousActor::WaitForSuperstructureLow() {
606 while (true) {
607 if (ShouldCancel()) return;
608 control_loops::superstructure_queue.status.FetchAnother();
Comran Morshedb134e772016-03-16 21:05:05 +0000609
610 if (control_loops::superstructure_queue.status->state < 12 ||
611 control_loops::superstructure_queue.status->state == 16) {
612 LOG(ERROR, "Superstructure no longer running, aborting action\n");
613 return;
614 }
Austin Schuh23b21802016-04-03 21:18:56 -0700615 if (SuperstructureProfileDone()) return;
616 if (control_loops::superstructure_queue.status->shoulder.angle < 0.1) {
617 return;
Comran Morshedb134e772016-03-16 21:05:05 +0000618 }
619 }
620}
Austin Schuh23b21802016-04-03 21:18:56 -0700621void AutonomousActor::BackLongShotLowBarTwoBall() {
622 LOG(INFO, "Expanding for back long shot\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700623 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 -0700624 {10.0, 25.0}, false, 0.0);
625}
626
627void AutonomousActor::BackLongShotTwoBall() {
628 LOG(INFO, "Expanding for back long shot\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700629 MoveSuperstructure(0.00, M_PI / 2.0 - 0.2, -0.55, {7.0, 40.0}, {4.0, 6.0},
630 {10.0, 25.0}, false, 0.0);
631}
632
633void AutonomousActor::BackLongShotTwoBallFinish() {
634 LOG(INFO, "Expanding for back long shot\n");
Austin Schuh90963362016-05-01 12:27:31 -0700635 MoveSuperstructure(0.00, M_PI / 2.0 - 0.2, -0.625, {7.0, 40.0}, {4.0, 6.0},
Austin Schuh23b21802016-04-03 21:18:56 -0700636 {10.0, 25.0}, false, 0.0);
637}
638
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700639void AutonomousActor::BackLongShot() {
640 LOG(INFO, "Expanding for back long shot\n");
Austin Schuh23b21802016-04-03 21:18:56 -0700641 MoveSuperstructure(0.80, M_PI / 2.0 - 0.2, -0.62, {7.0, 40.0}, {4.0, 6.0},
642 {10.0, 25.0}, false, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700643}
644
645void AutonomousActor::BackMiddleShot() {
646 LOG(INFO, "Expanding for back middle shot\n");
647 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 -0700648 {10.0, 25.0}, false, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700649}
650
Austin Schuh3e4a5272016-04-20 20:11:00 -0700651void AutonomousActor::FrontLongShot() {
652 LOG(INFO, "Expanding for front long shot\n");
653 MoveSuperstructure(0.80, M_PI / 2.0 + 0.1, M_PI + 0.41 + 0.02, {7.0, 40.0},
654 {4.0, 6.0}, {10.0, 25.0}, false, 0.0);
655}
656
657void AutonomousActor::FrontMiddleShot() {
658 LOG(INFO, "Expanding for front middle shot\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700659 MoveSuperstructure(-0.05, M_PI / 2.0 + 0.1, M_PI + 0.44, {7.0, 40.0},
Austin Schuh3e4a5272016-04-20 20:11:00 -0700660 {4.0, 10.0}, {10.0, 25.0}, true, 0.0);
661}
662
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700663void AutonomousActor::TuckArm(bool low_bar, bool traverse_down) {
664 MoveSuperstructure(low_bar ? -0.05 : 2.0, -0.010, 0.0, {7.0, 40.0},
Austin Schuh23b21802016-04-03 21:18:56 -0700665 {4.0, 10.0}, {10.0, 25.0}, !traverse_down, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700666}
667
Austin Schuh3e4a5272016-04-20 20:11:00 -0700668void AutonomousActor::DoFullShot() {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700669 if (ShouldCancel()) return;
670 // Make sure that the base is aligned with the base.
671 LOG(INFO, "Waiting for the superstructure\n");
672 WaitForSuperstructure();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700673
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700674 ::aos::time::SleepFor(::aos::time::Time::InSeconds(0.5));
Austin Schuh3e4a5272016-04-20 20:11:00 -0700675
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700676 if (ShouldCancel()) return;
677 LOG(INFO, "Triggering the vision actor\n");
678 AlignWithVisionGoal();
679
680 // Wait for the drive base to be aligned with the target and make sure that
681 // the shooter is up to speed.
682 LOG(INFO, "Waiting for vision to be aligned\n");
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700683 WaitForAlignedWithVision(aos::time::Time::InSeconds(2));
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700684 if (ShouldCancel()) return;
685 LOG(INFO, "Waiting for shooter to be up to speed\n");
686 WaitForShooterSpeed();
687 if (ShouldCancel()) return;
688
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700689 ::aos::time::SleepFor(::aos::time::Time::InSeconds(0.3));
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700690 LOG(INFO, "Shoot!\n");
691 Shoot();
692
693 // Turn off the shooter and fold up the superstructure.
694 if (ShouldCancel()) return;
695 LOG(INFO, "Stopping shooter\n");
696 SetShooterSpeed(0.0);
697 LOG(INFO, "Folding superstructure back down\n");
698 TuckArm(false, false);
699
700 // Wait for everything to be folded up.
701 LOG(INFO, "Waiting for superstructure to be folded back down\n");
Austin Schuh3e4a5272016-04-20 20:11:00 -0700702 WaitForSuperstructureLow();
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700703}
704
705void AutonomousActor::LowBarDrive() {
706 TuckArm(false, true);
707 StartDrive(-5.5, 0.0, kLowBarDrive, kSlowTurn);
708
709 if (!WaitForDriveNear(5.3, 0.0)) return;
710 TuckArm(true, true);
711
712 if (!WaitForDriveNear(5.0, 0.0)) return;
713
714 StartDrive(0.0, 0.0, kLowBarDrive, kSlowTurn);
715
716 if (!WaitForDriveNear(3.0, 0.0)) return;
717
718 StartDrive(0.0, 0.0, kLowBarDrive, kSlowTurn);
719
720 if (!WaitForDriveNear(1.0, 0.0)) return;
721
Austin Schuh15b5f6a2016-03-26 19:43:56 -0700722 StartDrive(0, -M_PI / 4.0 - 0.2, kLowBarDrive, kSlowTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700723}
724
Austin Schuh3e4a5272016-04-20 20:11:00 -0700725void AutonomousActor::TippyDrive(double goal_distance, double tip_distance,
726 double below, double above) {
727 StartDrive(goal_distance, 0.0, kMoatDrive, kSlowTurn);
728 if (!WaitForBelowAngle(below)) return;
729 if (!WaitForAboveAngle(above)) return;
730 // Ok, we are good now. Compensate by moving the goal by the error.
731 // Should be here at 2.7
732 drivetrain_queue.status.FetchLatest();
733 if (drivetrain_queue.status.get()) {
734 const double left_error =
735 (initial_drivetrain_.left -
736 drivetrain_queue.status->estimated_left_position);
737 const double right_error =
738 (initial_drivetrain_.right -
739 drivetrain_queue.status->estimated_right_position);
740 const double distance_to_go = (left_error + right_error) / 2.0;
741 const double distance_compensation =
742 goal_distance - tip_distance - distance_to_go;
743 LOG(INFO, "Going %f further at the bump\n", distance_compensation);
744 StartDrive(distance_compensation, 0.0, kMoatDrive, kSlowTurn);
745 }
746}
747
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700748void AutonomousActor::MiddleDrive() {
749 TuckArm(false, false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700750 TippyDrive(3.65, 2.7, -0.2, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700751 if (!WaitForDriveDone()) return;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700752}
753
754void AutonomousActor::OneFromMiddleDrive(bool left) {
Austin Schuh3e4a5272016-04-20 20:11:00 -0700755 const double kTurnAngle = left ? -0.41 : 0.41;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700756 TuckArm(false, false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700757 TippyDrive(4.05, 2.7, -0.2, 0.0);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700758
759 if (!WaitForDriveDone()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700760 StartDrive(0.0, kTurnAngle, kRealignDrive, kFastTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700761}
762
763void AutonomousActor::TwoFromMiddleDrive() {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700764 TuckArm(false, false);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700765 constexpr double kDriveDistance = 5.10;
766 TippyDrive(kDriveDistance, 2.7, -0.2, 0.0);
767
768 if (!WaitForDriveNear(kDriveDistance - 3.0, 2.0)) return;
769 StartDrive(0, -M_PI / 2 - 0.10, kMoatDrive, kFastTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700770
771 if (!WaitForDriveDone()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700772 StartDrive(0, M_PI / 3 + 0.35, kMoatDrive, kFastTurn);
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700773}
Comran Morshedb134e772016-03-16 21:05:05 +0000774
Austin Schuh23b21802016-04-03 21:18:56 -0700775void AutonomousActor::CloseIfBall() {
776 ::y2016::sensors::ball_detector.FetchLatest();
777 if (::y2016::sensors::ball_detector.get()) {
778 const bool ball_detected = ::y2016::sensors::ball_detector->voltage > 2.5;
779 if (ball_detected) {
780 CloseShooter();
781 }
782 }
783}
784
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700785void AutonomousActor::WaitForBallOrDriveDone() {
786 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5),
787 ::aos::time::Time::InMS(5) / 2);
788 while (true) {
789 if (ShouldCancel()) {
790 return;
791 }
792 phased_loop.SleepUntilNext();
793 drivetrain_queue.status.FetchLatest();
794 if (IsDriveDone()) {
795 return;
796 }
797
798 ::y2016::sensors::ball_detector.FetchLatest();
799 if (::y2016::sensors::ball_detector.get()) {
800 const bool ball_detected = ::y2016::sensors::ball_detector->voltage > 2.5;
801 if (ball_detected) {
802 return;
803 }
804 }
805 }
806}
807
Austin Schuh23b21802016-04-03 21:18:56 -0700808void AutonomousActor::WaitForBall() {
809 while (true) {
810 ::y2016::sensors::ball_detector.FetchAnother();
811 if (::y2016::sensors::ball_detector.get()) {
812 const bool ball_detected = ::y2016::sensors::ball_detector->voltage > 2.5;
813 if (ball_detected) {
814 return;
815 }
816 if (ShouldCancel()) return;
817 }
818 }
819}
820
Austin Schuh3e4a5272016-04-20 20:11:00 -0700821void AutonomousActor::TwoBallAuto() {
822 aos::time::Time start_time = aos::time::Time::Now();
823 OpenShooter();
824 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
825 false, 12.0);
826 if (ShouldCancel()) return;
827 LOG(INFO, "Waiting for the intake to come down.\n");
828
829 WaitForIntake();
830 LOG(INFO, "Intake done at %f seconds, starting to drive\n",
831 (aos::time::Time::Now() - start_time).ToSeconds());
832 if (ShouldCancel()) return;
833 const double kDriveDistance = 5.05;
834 StartDrive(-kDriveDistance, 0.0, kTwoBallLowDrive, kSlowTurn);
835
836 StartDrive(0.0, 0.4, kTwoBallLowDrive, kSwerveTurn);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700837 if (!WaitForDriveNear(kDriveDistance - 0.5, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700838
Austin Schuh295c2d92016-05-01 12:28:04 -0700839 // Check if the ball is there.
840 bool first_ball_there = true;
841 ::y2016::sensors::ball_detector.FetchLatest();
842 if (::y2016::sensors::ball_detector.get()) {
843 const bool ball_detected = ::y2016::sensors::ball_detector->voltage > 2.5;
844 first_ball_there = ball_detected;
845 LOG(INFO, "Saw the ball: %d at %f\n", first_ball_there,
846 (aos::time::Time::Now() - start_time).ToSeconds());
847 }
Austin Schuh3e4a5272016-04-20 20:11:00 -0700848 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 40.0}, {4.0, 10.0}, {10.0, 25.0},
849 false, 0.0);
850 LOG(INFO, "Shutting off rollers at %f seconds, starting to straighten out\n",
851 (aos::time::Time::Now() - start_time).ToSeconds());
852 StartDrive(0.0, -0.4, kTwoBallLowDrive, kSwerveTurn);
853 MoveSuperstructure(-0.05, -0.010, 0.0, {8.0, 40.0}, {4.0, 10.0}, {10.0, 25.0},
854 false, 0.0);
855 CloseShooter();
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700856 if (!WaitForDriveNear(kDriveDistance - 2.4, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700857
858 // We are now under the low bar. Start lifting.
859 BackLongShotLowBarTwoBall();
860 LOG(INFO, "Spinning up the shooter wheels\n");
861 SetShooterSpeed(640.0);
862 StartDrive(0.0, 0.0, kTwoBallFastDrive, kSwerveTurn);
863
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700864 if (!WaitForDriveNear(1.50, kDoNotTurnCare)) return;
865 constexpr double kShootTurnAngle = -M_PI / 4.0 - 0.05;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700866 StartDrive(0, kShootTurnAngle, kTwoBallFastDrive, kFinishTurn);
867 BackLongShotTwoBall();
868
869 if (!WaitForDriveDone()) return;
870 LOG(INFO, "First shot done driving at %f seconds\n",
871 (aos::time::Time::Now() - start_time).ToSeconds());
872
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700873 WaitForSuperstructureProfile();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700874
875 if (ShouldCancel()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700876 AlignWithVisionGoal();
877
878 WaitForShooterSpeed();
879 if (ShouldCancel()) return;
880
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700881 constexpr double kVisionExtra = 0.0;
882 WaitForAlignedWithVision(aos::time::Time::InSeconds(0.5 + kVisionExtra));
883 BackLongShotTwoBallFinish();
884 WaitForSuperstructureProfile();
885 if (ShouldCancel()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700886 LOG(INFO, "Shoot!\n");
Austin Schuh295c2d92016-05-01 12:28:04 -0700887 if (first_ball_there) {
888 Shoot();
889 } else {
890 LOG(INFO, "Nah, not shooting\n");
891 }
Austin Schuh3e4a5272016-04-20 20:11:00 -0700892
893 LOG(INFO, "First shot at %f seconds\n",
894 (aos::time::Time::Now() - start_time).ToSeconds());
895 if (ShouldCancel()) return;
896
897 SetShooterSpeed(0.0);
898 LOG(INFO, "Folding superstructure back down\n");
899 TuckArm(true, true);
900
901 // Undo vision move.
902 StartDrive(0.0, 0.0, kTwoBallFastDrive, kFinishTurn);
903 if (!WaitForDriveDone()) return;
904
905 constexpr double kBackDrive = 3.09 - 0.4;
906 StartDrive(kBackDrive, 0.0, kTwoBallReturnDrive, kSlowTurn);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700907 if (!WaitForDriveNear(kBackDrive - 0.19, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700908 StartDrive(0, -kShootTurnAngle, kTwoBallReturnDrive, kSwerveTurn);
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700909 if (!WaitForDriveNear(1.0, kDoNotTurnCare)) return;
910 StartDrive(0, 0, kTwoBallReturnSlow, kSwerveTurn);
Austin Schuh3e4a5272016-04-20 20:11:00 -0700911
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700912 if (!WaitForDriveNear(0.06, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700913 LOG(INFO, "At Low Bar %f\n",
914 (aos::time::Time::Now() - start_time).ToSeconds());
915
916 OpenShooter();
917 constexpr double kSecondBallAfterBarDrive = 2.10;
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700918 StartDrive(kSecondBallAfterBarDrive, 0.0, kTwoBallBallPickupAccel, kSlowTurn);
919 if (!WaitForDriveNear(kSecondBallAfterBarDrive - 0.5, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700920 constexpr double kBallSmallWallTurn = -0.11;
921 StartDrive(0, kBallSmallWallTurn, kTwoBallBallPickup, kFinishTurn);
922
923 MoveSuperstructure(0.03, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
924 false, 12.0);
925
926 if (!WaitForDriveProfileDone()) return;
927
928 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
929 false, 12.0);
930
931 LOG(INFO, "Done backing up %f\n",
932 (aos::time::Time::Now() - start_time).ToSeconds());
933
934 constexpr double kDriveBackDistance = 5.15 - 0.4;
935 StartDrive(-kDriveBackDistance, 0.0, kTwoBallLowDrive, kFinishTurn);
936 CloseIfBall();
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700937 if (!WaitForDriveNear(kDriveBackDistance - 0.75, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700938
939 StartDrive(0.0, -kBallSmallWallTurn, kTwoBallLowDrive, kFinishTurn);
940 LOG(INFO, "Straightening up at %f\n",
941 (aos::time::Time::Now() - start_time).ToSeconds());
942
943 CloseIfBall();
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700944 if (!WaitForDriveNear(kDriveBackDistance - 2.3, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700945
946 ::y2016::sensors::ball_detector.FetchLatest();
947 if (::y2016::sensors::ball_detector.get()) {
948 const bool ball_detected = ::y2016::sensors::ball_detector->voltage > 2.5;
949 if (!ball_detected) {
950 if (!WaitForDriveDone()) return;
951 LOG(INFO, "Aborting, no ball %f\n",
952 (aos::time::Time::Now() - start_time).ToSeconds());
953 return;
954 }
955 }
956 CloseShooter();
957
958 BackLongShotLowBarTwoBall();
959 LOG(INFO, "Spinning up the shooter wheels\n");
960 SetShooterSpeed(640.0);
961 StartDrive(0.0, 0.0, kTwoBallFastDrive, kSwerveTurn);
962
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700963 if (!WaitForDriveNear(1.80, kDoNotTurnCare)) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700964 StartDrive(0, kShootTurnAngle, kTwoBallFastDrive, kFinishTurn);
965 BackLongShotTwoBall();
966
967 if (!WaitForDriveDone()) return;
968 LOG(INFO, "Second shot done driving at %f seconds\n",
969 (aos::time::Time::Now() - start_time).ToSeconds());
970 WaitForSuperstructure();
Austin Schuh3e4a5272016-04-20 20:11:00 -0700971 AlignWithVisionGoal();
972 if (ShouldCancel()) return;
973
974 WaitForShooterSpeed();
975 if (ShouldCancel()) return;
976
977 // 2.2 with 0.4 of vision.
978 // 1.8 without any vision.
979 LOG(INFO, "Going to vision align at %f\n",
980 (aos::time::Time::Now() - start_time).ToSeconds());
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700981 WaitForAlignedWithVision(start_time + aos::time::Time::InSeconds(13.5 + kVisionExtra * 2) -
Austin Schuh3e4a5272016-04-20 20:11:00 -0700982 aos::time::Time::Now());
Austin Schuhe4ec49c2016-04-24 19:07:15 -0700983 BackLongShotTwoBallFinish();
984 WaitForSuperstructureProfile();
985 if (ShouldCancel()) return;
Austin Schuh3e4a5272016-04-20 20:11:00 -0700986 LOG(INFO, "Shoot at %f\n", (aos::time::Time::Now() - start_time).ToSeconds());
987 Shoot();
988
989 LOG(INFO, "Second shot at %f seconds\n",
990 (aos::time::Time::Now() - start_time).ToSeconds());
991 if (ShouldCancel()) return;
992
993 SetShooterSpeed(0.0);
994 LOG(INFO, "Folding superstructure back down\n");
995 TuckArm(true, false);
996 LOG(INFO, "Shot %f\n", (aos::time::Time::Now() - start_time).ToSeconds());
997
998 WaitForSuperstructureLow();
999
1000 LOG(INFO, "Done %f\n", (aos::time::Time::Now() - start_time).ToSeconds());
1001}
1002
Austin Schuhe4ec49c2016-04-24 19:07:15 -07001003void AutonomousActor::StealAndMoveOverBy(double distance) {
1004 OpenShooter();
1005 MoveSuperstructure(0.10, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
1006 true, 12.0);
1007 if (ShouldCancel()) return;
1008 LOG(INFO, "Waiting for the intake to come down.\n");
1009
1010 WaitForIntake();
1011 if (ShouldCancel()) return;
1012 StartDrive(-distance, M_PI / 2.0, kFastDrive, kStealTurn);
1013 WaitForBallOrDriveDone();
1014 if (ShouldCancel()) return;
1015 MoveSuperstructure(1.0, -0.010, 0.0, {8.0, 60.0}, {4.0, 10.0}, {10.0, 25.0},
1016 true, 12.0);
1017
1018 if (!WaitForDriveDone()) return;
1019 StartDrive(0.0, M_PI / 2.0, kFastDrive, kStealTurn);
1020 if (!WaitForDriveDone()) return;
1021}
1022
Comran Morshede68e3732016-03-12 14:12:11 +00001023bool AutonomousActor::RunAction(const actors::AutonomousActionParams &params) {
Austin Schuh3e4a5272016-04-20 20:11:00 -07001024 aos::time::Time start_time = aos::time::Time::Now();
Comran Morshede68e3732016-03-12 14:12:11 +00001025 LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode);
1026
Comran Morshed435f1112016-03-12 14:20:45 +00001027 InitializeEncoders();
1028 ResetDrivetrain();
1029
Austin Schuh295c2d92016-05-01 12:28:04 -07001030 switch (params.mode) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -07001031 case 0:
1032 LowBarDrive();
Austin Schuh3e4a5272016-04-20 20:11:00 -07001033 if (!WaitForDriveDone()) return true;
1034 // Get the superstructure to unfold and get ready for shooting.
1035 LOG(INFO, "Unfolding superstructure\n");
1036 FrontLongShot();
1037
1038 // Spin up the shooter wheels.
1039 LOG(INFO, "Spinning up the shooter wheels\n");
1040 SetShooterSpeed(640.0);
1041
Austin Schuhf59b8ee2016-03-19 21:31:36 -07001042 break;
1043 case 1:
1044 TwoFromMiddleDrive();
Austin Schuh3e4a5272016-04-20 20:11:00 -07001045 if (!WaitForDriveDone()) return true;
1046 // Get the superstructure to unfold and get ready for shooting.
1047 LOG(INFO, "Unfolding superstructure\n");
1048 FrontMiddleShot();
1049
1050 // Spin up the shooter wheels.
1051 LOG(INFO, "Spinning up the shooter wheels\n");
1052 SetShooterSpeed(600.0);
1053
Austin Schuhf59b8ee2016-03-19 21:31:36 -07001054 break;
1055 case 2:
1056 OneFromMiddleDrive(true);
Austin Schuh3e4a5272016-04-20 20:11:00 -07001057 if (!WaitForDriveDone()) return true;
1058 // Get the superstructure to unfold and get ready for shooting.
1059 LOG(INFO, "Unfolding superstructure\n");
1060 FrontMiddleShot();
1061
1062 // Spin up the shooter wheels.
1063 LOG(INFO, "Spinning up the shooter wheels\n");
1064 SetShooterSpeed(600.0);
1065
Austin Schuhf59b8ee2016-03-19 21:31:36 -07001066 break;
1067 case 3:
1068 MiddleDrive();
Austin Schuh3e4a5272016-04-20 20:11:00 -07001069 if (!WaitForDriveDone()) return true;
1070 // Get the superstructure to unfold and get ready for shooting.
1071 LOG(INFO, "Unfolding superstructure\n");
1072 FrontMiddleShot();
1073
1074 // Spin up the shooter wheels.
1075 LOG(INFO, "Spinning up the shooter wheels\n");
1076 SetShooterSpeed(600.0);
1077
Austin Schuhf59b8ee2016-03-19 21:31:36 -07001078 break;
1079 case 4:
1080 OneFromMiddleDrive(false);
Austin Schuh3e4a5272016-04-20 20:11:00 -07001081 if (!WaitForDriveDone()) return true;
1082 // Get the superstructure to unfold and get ready for shooting.
1083 LOG(INFO, "Unfolding superstructure\n");
1084 FrontMiddleShot();
1085
1086 // Spin up the shooter wheels.
1087 LOG(INFO, "Spinning up the shooter wheels\n");
1088 SetShooterSpeed(600.0);
1089
Austin Schuhf59b8ee2016-03-19 21:31:36 -07001090 break;
Austin Schuh3e4a5272016-04-20 20:11:00 -07001091 case 5:
1092 TwoBallAuto();
Austin Schuh23b21802016-04-03 21:18:56 -07001093 return true;
Austin Schuh3e4a5272016-04-20 20:11:00 -07001094 break;
Austin Schuhe4ec49c2016-04-24 19:07:15 -07001095 case 6:
1096 StealAndMoveOverBy(3.10 + 2 * 52 * 2.54 / 100.0);
1097 if (ShouldCancel()) return true;
1098
1099 TwoFromMiddleDrive();
1100 if (!WaitForDriveDone()) return true;
1101 // Get the superstructure to unfold and get ready for shooting.
1102 LOG(INFO, "Unfolding superstructure\n");
1103 FrontMiddleShot();
1104
1105 // Spin up the shooter wheels.
1106 LOG(INFO, "Spinning up the shooter wheels\n");
1107 SetShooterSpeed(600.0);
1108
1109 break;
1110 case 7:
1111 StealAndMoveOverBy(2.95 + 52 * 2.54 / 100.0);
1112 if (ShouldCancel()) return true;
1113
1114 OneFromMiddleDrive(true);
1115 if (!WaitForDriveDone()) return true;
1116 // Get the superstructure to unfold and get ready for shooting.
1117 LOG(INFO, "Unfolding superstructure\n");
1118 FrontMiddleShot();
1119
1120 // Spin up the shooter wheels.
1121 LOG(INFO, "Spinning up the shooter wheels\n");
1122 SetShooterSpeed(600.0);
1123
1124 break;
1125 case 8: {
1126 StealAndMoveOverBy(2.95);
1127 if (ShouldCancel()) return true;
1128
1129 MiddleDrive();
1130 if (!WaitForDriveDone()) return true;
1131 // Get the superstructure to unfold and get ready for shooting.
1132 LOG(INFO, "Unfolding superstructure\n");
1133 FrontMiddleShot();
1134
1135 // Spin up the shooter wheels.
1136 LOG(INFO, "Spinning up the shooter wheels\n");
1137 SetShooterSpeed(600.0);
1138
1139 } break;
1140 case 9: {
1141 StealAndMoveOverBy(1.70);
1142 if (ShouldCancel()) return true;
1143
1144 OneFromMiddleDrive(false);
1145 if (!WaitForDriveDone()) return true;
1146 // Get the superstructure to unfold and get ready for shooting.
1147 LOG(INFO, "Unfolding superstructure\n");
1148 FrontMiddleShot();
1149
1150 // Spin up the shooter wheels.
1151 LOG(INFO, "Spinning up the shooter wheels\n");
1152 SetShooterSpeed(600.0);
1153
1154 } break;
Austin Schuhf59b8ee2016-03-19 21:31:36 -07001155 default:
Austin Schuh6c9bc622016-03-26 19:44:12 -07001156 LOG(ERROR, "Invalid auto mode %d\n", params.mode);
Austin Schuhf59b8ee2016-03-19 21:31:36 -07001157 return true;
1158 }
Comran Morshed435f1112016-03-12 14:20:45 +00001159
Austin Schuh3e4a5272016-04-20 20:11:00 -07001160 DoFullShot();
1161
1162 StartDrive(0.5, 0.0, kMoatDrive, kFastTurn);
Comran Morshed435f1112016-03-12 14:20:45 +00001163 if (!WaitForDriveDone()) return true;
1164
Austin Schuh3e4a5272016-04-20 20:11:00 -07001165 LOG(INFO, "Done %f\n", (aos::time::Time::Now() - start_time).ToSeconds());
Comran Morshed435f1112016-03-12 14:20:45 +00001166
1167 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5),
1168 ::aos::time::Time::InMS(5) / 2);
Austin Schuhf59b8ee2016-03-19 21:31:36 -07001169
Comran Morshed435f1112016-03-12 14:20:45 +00001170 while (!ShouldCancel()) {
1171 phased_loop.SleepUntilNext();
Comran Morshede68e3732016-03-12 14:12:11 +00001172 }
Comran Morshed435f1112016-03-12 14:20:45 +00001173 LOG(DEBUG, "Done running\n");
Comran Morshede68e3732016-03-12 14:12:11 +00001174
1175 return true;
1176}
1177
1178::std::unique_ptr<AutonomousAction> MakeAutonomousAction(
1179 const ::y2016::actors::AutonomousActionParams &params) {
1180 return ::std::unique_ptr<AutonomousAction>(
1181 new AutonomousAction(&::y2016::actors::autonomous_action, params));
1182}
1183
1184} // namespace actors
1185} // namespace y2016