blob: 41a70fd42a6ca13695dff671b75facad937cb993 [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
30} // namespace
31
32AutonomousActor::AutonomousActor(
33 ::frc971::autonomous::AutonomousActionQueueGroup *s)
34 : frc971::autonomous::BaseAutonomousActor(
35 s, control_loops::drivetrain::GetDrivetrainConfig()) {}
36
Austin Schuha9644062019-03-28 14:31:52 -070037void AutonomousActor::Reset(bool is_left) {
38 const double turn_scalar = is_left ? 1.0 : -1.0;
Austin Schuh13379ba2019-03-12 21:06:46 -070039 elevator_goal_ = 0.01;
40 wrist_goal_ = -M_PI / 2.0;
41 intake_goal_ = -1.2;
42
43 suction_on_ = false;
44 suction_gamepiece_ = 1;
45
46 elevator_max_velocity_ = 0.0;
47 elevator_max_acceleration_ = 0.0;
48 wrist_max_velocity_ = 0.0;
49 wrist_max_acceleration_ = 0.0;
50
51 InitializeEncoders();
52 SendSuperstructureGoal();
53
54 {
55 auto localizer_resetter = localizer_control.MakeMessage();
56 // Start on the left l2.
57 localizer_resetter->x = 1.0;
Austin Schuhda0ac232019-03-17 18:13:18 -070058 localizer_resetter->y = 1.5 * turn_scalar;
Austin Schuh13379ba2019-03-12 21:06:46 -070059 localizer_resetter->theta = M_PI;
Austin Schuh4b3a0052019-03-24 19:19:10 -070060 localizer_resetter->theta_uncertainty = 0.0000001;
Austin Schuh13379ba2019-03-12 21:06:46 -070061 if (!localizer_resetter.Send()) {
62 LOG(ERROR, "Failed to reset localizer.\n");
63 }
64 }
65
66 // Wait for the drivetrain to run so it has time to reset the heading.
67 // Otherwise our drivetrain reset will do a 180 right at the start.
68 drivetrain_queue.status.FetchAnother();
69 LOG(INFO, "Heading is %f\n", drivetrain_queue.status->estimated_heading);
70 InitializeEncoders();
71 ResetDrivetrain();
72 drivetrain_queue.status.FetchAnother();
73 LOG(INFO, "Heading is %f\n", drivetrain_queue.status->estimated_heading);
74
75 ResetDrivetrain();
76 InitializeEncoders();
77}
78
79const ProfileParameters kJumpDrive = {2.0, 3.0};
80const ProfileParameters kDrive = {4.0, 3.0};
81const ProfileParameters kTurn = {5.0, 15.0};
82
83bool AutonomousActor::RunAction(
84 const ::frc971::autonomous::AutonomousActionParams &params) {
85 monotonic_clock::time_point start_time = monotonic_clock::now();
Austin Schuha9644062019-03-28 14:31:52 -070086 const bool is_left = params.mode == 0;
87
88 {
89 LOG(INFO, "Starting autonomous action with mode %" PRId32 " %s\n",
90 params.mode, is_left ? "left" : "right");
91 }
92 const double turn_scalar = is_left ? 1.0 : -1.0;
93
94 Reset(is_left);
Austin Schuh13379ba2019-03-12 21:06:46 -070095
96 // Grab the disk, wait until we have vacuum, then jump
97 set_elevator_goal(0.01);
98 set_wrist_goal(-M_PI / 2.0);
99 set_intake_goal(-1.2);
100 set_suction_goal(true, 1);
101 SendSuperstructureGoal();
102
103 if (!WaitForGamePiece()) return true;
104 LOG(INFO, "Has game piece\n");
105
Austin Schuha78857f2019-03-13 22:43:41 -0700106 StartDrive(-4.0, 0.0, kJumpDrive, kTurn);
107 if (!WaitForDriveNear(3.3, 10.0)) return true;
Austin Schuh13379ba2019-03-12 21:06:46 -0700108 LOG(INFO, "Lifting\n");
109 set_elevator_goal(0.30);
110 SendSuperstructureGoal();
111
Austin Schuha78857f2019-03-13 22:43:41 -0700112 if (!WaitForDriveNear(2.8, 10.0)) return true;
Austin Schuh13379ba2019-03-12 21:06:46 -0700113 LOG(INFO, "Off the platform\n");
114
Austin Schuhda0ac232019-03-17 18:13:18 -0700115 StartDrive(0.0, 1.00 * turn_scalar, kDrive, kTurn);
Austin Schuh13379ba2019-03-12 21:06:46 -0700116 LOG(INFO, "Turn started\n");
117 if (!WaitForSuperstructureDone()) return true;
118 LOG(INFO, "Superstructure done\n");
119
Austin Schuha78857f2019-03-13 22:43:41 -0700120 if (!WaitForDriveNear(0.7, 10.0)) return true;
Austin Schuhda0ac232019-03-17 18:13:18 -0700121 StartDrive(0.0, -0.35 * turn_scalar, kDrive, kTurn);
Austin Schuh13379ba2019-03-12 21:06:46 -0700122
123 LOG(INFO, "Elevator up\n");
Austin Schuh291ffe92019-03-24 16:03:59 -0700124 set_elevator_goal(0.78);
Austin Schuh13379ba2019-03-12 21:06:46 -0700125 SendSuperstructureGoal();
126
127 if (!WaitForDriveDone()) return true;
128 LOG(INFO, "Done driving\n");
129
130 if (!WaitForSuperstructureDone()) return true;
131
132 LOG(INFO, "Done %f\n", DoubleSeconds(monotonic_clock::now() - start_time));
133
Austin Schuh13379ba2019-03-12 21:06:46 -0700134 return true;
135}
136
137} // namespace actors
138} // namespace y2019