blob: 9ed3e6fab668413d76f9b88e83982538a0b6d1c3 [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"
13#include "y2019/control_loops/drivetrain/drivetrain_base.h"
14
15namespace y2019 {
16namespace actors {
17using ::frc971::control_loops::drivetrain_queue;
18using ::frc971::control_loops::drivetrain::localizer_control;
19using ::aos::monotonic_clock;
20namespace chrono = ::std::chrono;
21
22namespace {
23
24double DoubleSeconds(monotonic_clock::duration duration) {
25 return ::std::chrono::duration_cast<::std::chrono::duration<double>>(duration)
26 .count();
27}
28
29} // namespace
30
31AutonomousActor::AutonomousActor(
32 ::frc971::autonomous::AutonomousActionQueueGroup *s)
33 : frc971::autonomous::BaseAutonomousActor(
34 s, control_loops::drivetrain::GetDrivetrainConfig()) {}
35
36void AutonomousActor::Reset() {
37 elevator_goal_ = 0.01;
38 wrist_goal_ = -M_PI / 2.0;
39 intake_goal_ = -1.2;
40
41 suction_on_ = false;
42 suction_gamepiece_ = 1;
43
44 elevator_max_velocity_ = 0.0;
45 elevator_max_acceleration_ = 0.0;
46 wrist_max_velocity_ = 0.0;
47 wrist_max_acceleration_ = 0.0;
48
49 InitializeEncoders();
50 SendSuperstructureGoal();
51
52 {
53 auto localizer_resetter = localizer_control.MakeMessage();
54 // Start on the left l2.
55 localizer_resetter->x = 1.0;
56 localizer_resetter->y = 1.5;
57 localizer_resetter->theta = M_PI;
58 if (!localizer_resetter.Send()) {
59 LOG(ERROR, "Failed to reset localizer.\n");
60 }
61 }
62
63 // Wait for the drivetrain to run so it has time to reset the heading.
64 // Otherwise our drivetrain reset will do a 180 right at the start.
65 drivetrain_queue.status.FetchAnother();
66 LOG(INFO, "Heading is %f\n", drivetrain_queue.status->estimated_heading);
67 InitializeEncoders();
68 ResetDrivetrain();
69 drivetrain_queue.status.FetchAnother();
70 LOG(INFO, "Heading is %f\n", drivetrain_queue.status->estimated_heading);
71
72 ResetDrivetrain();
73 InitializeEncoders();
74}
75
76const ProfileParameters kJumpDrive = {2.0, 3.0};
77const ProfileParameters kDrive = {4.0, 3.0};
78const ProfileParameters kTurn = {5.0, 15.0};
79
80bool AutonomousActor::RunAction(
81 const ::frc971::autonomous::AutonomousActionParams &params) {
82 monotonic_clock::time_point start_time = monotonic_clock::now();
83 LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode);
84 Reset();
85
86 // Grab the disk, wait until we have vacuum, then jump
87 set_elevator_goal(0.01);
88 set_wrist_goal(-M_PI / 2.0);
89 set_intake_goal(-1.2);
90 set_suction_goal(true, 1);
91 SendSuperstructureGoal();
92
93 if (!WaitForGamePiece()) return true;
94 LOG(INFO, "Has game piece\n");
95
96 StartDrive(-4.5, 0.0, kJumpDrive, kTurn);
97 if (!WaitForDriveNear(3.8, 10.0)) return true;
98 LOG(INFO, "Lifting\n");
99 set_elevator_goal(0.30);
100 SendSuperstructureGoal();
101
102 if (!WaitForDriveNear(3.3, 10.0)) return true;
103 LOG(INFO, "Off the platform\n");
104
105 StartDrive(0.0, 1.00, kDrive, kTurn);
106 LOG(INFO, "Turn started\n");
107 if (!WaitForSuperstructureDone()) return true;
108 LOG(INFO, "Superstructure done\n");
109
110 if (!WaitForDriveNear(1.2, 10.0)) return true;
111 StartDrive(0.0, -0.35, kDrive, kTurn);
112
113 LOG(INFO, "Elevator up\n");
114 set_elevator_goal(0.01);
115 SendSuperstructureGoal();
116
117 if (!WaitForDriveDone()) return true;
118 LOG(INFO, "Done driving\n");
119
120 if (!WaitForSuperstructureDone()) return true;
121
122 LOG(INFO, "Done %f\n", DoubleSeconds(monotonic_clock::now() - start_time));
123
124 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
125 ::std::chrono::milliseconds(5) / 2);
126
127 while (!ShouldCancel()) {
128 phased_loop.SleepUntilNext();
129 }
130 LOG(DEBUG, "Done running\n");
131
132 return true;
133}
134
135} // namespace actors
136} // namespace y2019