blob: 1647f04705bcdd2eb8593f1aa6b33c203b2dc144 [file] [log] [blame]
Austin Schuh13379ba2019-03-12 21:06:46 -07001#include "y2019/actors/autonomous_actor.h"
2
3#include <inttypes.h>
4
5#include <chrono>
6#include <cmath>
7
8#include "aos/logging/logging.h"
9#include "aos/util/phased_loop.h"
10
11#include "frc971/control_loops/drivetrain/drivetrain.q.h"
12#include "frc971/control_loops/drivetrain/localizer.q.h"
Austin Schuh6bcc2302019-03-23 22:28:06 -070013#include "y2019/actors/auto_splines.h"
Austin Schuh13379ba2019-03-12 21:06:46 -070014#include "y2019/control_loops/drivetrain/drivetrain_base.h"
15
16namespace y2019 {
17namespace actors {
18using ::frc971::control_loops::drivetrain_queue;
19using ::frc971::control_loops::drivetrain::localizer_control;
20using ::aos::monotonic_clock;
21namespace chrono = ::std::chrono;
22
23namespace {
24
25double DoubleSeconds(monotonic_clock::duration duration) {
26 return ::std::chrono::duration_cast<::std::chrono::duration<double>>(duration)
27 .count();
28}
29
Austin Schuhda0ac232019-03-17 18:13:18 -070030constexpr bool is_left = false;
31
32constexpr double turn_scalar = is_left ? 1.0 : -1.0;
33
Austin Schuh13379ba2019-03-12 21:06:46 -070034} // namespace
35
36AutonomousActor::AutonomousActor(
37 ::frc971::autonomous::AutonomousActionQueueGroup *s)
38 : frc971::autonomous::BaseAutonomousActor(
39 s, control_loops::drivetrain::GetDrivetrainConfig()) {}
40
41void AutonomousActor::Reset() {
42 elevator_goal_ = 0.01;
43 wrist_goal_ = -M_PI / 2.0;
44 intake_goal_ = -1.2;
45
46 suction_on_ = false;
47 suction_gamepiece_ = 1;
48
49 elevator_max_velocity_ = 0.0;
50 elevator_max_acceleration_ = 0.0;
51 wrist_max_velocity_ = 0.0;
52 wrist_max_acceleration_ = 0.0;
53
54 InitializeEncoders();
55 SendSuperstructureGoal();
56
57 {
58 auto localizer_resetter = localizer_control.MakeMessage();
59 // Start on the left l2.
60 localizer_resetter->x = 1.0;
Austin Schuhda0ac232019-03-17 18:13:18 -070061 localizer_resetter->y = 1.5 * turn_scalar;
Austin Schuh13379ba2019-03-12 21:06:46 -070062 localizer_resetter->theta = M_PI;
Austin Schuh4b3a0052019-03-24 19:19:10 -070063 localizer_resetter->theta_uncertainty = 0.0000001;
Austin Schuh13379ba2019-03-12 21:06:46 -070064 if (!localizer_resetter.Send()) {
65 LOG(ERROR, "Failed to reset localizer.\n");
66 }
67 }
68
69 // Wait for the drivetrain to run so it has time to reset the heading.
70 // Otherwise our drivetrain reset will do a 180 right at the start.
71 drivetrain_queue.status.FetchAnother();
72 LOG(INFO, "Heading is %f\n", drivetrain_queue.status->estimated_heading);
73 InitializeEncoders();
74 ResetDrivetrain();
75 drivetrain_queue.status.FetchAnother();
76 LOG(INFO, "Heading is %f\n", drivetrain_queue.status->estimated_heading);
77
78 ResetDrivetrain();
79 InitializeEncoders();
80}
81
82const ProfileParameters kJumpDrive = {2.0, 3.0};
83const ProfileParameters kDrive = {4.0, 3.0};
84const ProfileParameters kTurn = {5.0, 15.0};
85
86bool AutonomousActor::RunAction(
87 const ::frc971::autonomous::AutonomousActionParams &params) {
88 monotonic_clock::time_point start_time = monotonic_clock::now();
89 LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode);
90 Reset();
91
92 // Grab the disk, wait until we have vacuum, then jump
93 set_elevator_goal(0.01);
94 set_wrist_goal(-M_PI / 2.0);
95 set_intake_goal(-1.2);
96 set_suction_goal(true, 1);
97 SendSuperstructureGoal();
98
99 if (!WaitForGamePiece()) return true;
100 LOG(INFO, "Has game piece\n");
101
Austin Schuha78857f2019-03-13 22:43:41 -0700102 StartDrive(-4.0, 0.0, kJumpDrive, kTurn);
103 if (!WaitForDriveNear(3.3, 10.0)) return true;
Austin Schuh13379ba2019-03-12 21:06:46 -0700104 LOG(INFO, "Lifting\n");
105 set_elevator_goal(0.30);
106 SendSuperstructureGoal();
107
Austin Schuha78857f2019-03-13 22:43:41 -0700108 if (!WaitForDriveNear(2.8, 10.0)) return true;
Austin Schuh13379ba2019-03-12 21:06:46 -0700109 LOG(INFO, "Off the platform\n");
110
Austin Schuhda0ac232019-03-17 18:13:18 -0700111 StartDrive(0.0, 1.00 * turn_scalar, kDrive, kTurn);
Austin Schuh13379ba2019-03-12 21:06:46 -0700112 LOG(INFO, "Turn started\n");
113 if (!WaitForSuperstructureDone()) return true;
114 LOG(INFO, "Superstructure done\n");
115
Austin Schuha78857f2019-03-13 22:43:41 -0700116 if (!WaitForDriveNear(0.7, 10.0)) return true;
Austin Schuhda0ac232019-03-17 18:13:18 -0700117 StartDrive(0.0, -0.35 * turn_scalar, kDrive, kTurn);
Austin Schuh13379ba2019-03-12 21:06:46 -0700118
119 LOG(INFO, "Elevator up\n");
Austin Schuh291ffe92019-03-24 16:03:59 -0700120 set_elevator_goal(0.78);
Austin Schuh13379ba2019-03-12 21:06:46 -0700121 SendSuperstructureGoal();
122
123 if (!WaitForDriveDone()) return true;
124 LOG(INFO, "Done driving\n");
125
126 if (!WaitForSuperstructureDone()) return true;
127
128 LOG(INFO, "Done %f\n", DoubleSeconds(monotonic_clock::now() - start_time));
129
Austin Schuh13379ba2019-03-12 21:06:46 -0700130 return true;
131}
132
133} // namespace actors
134} // namespace y2019