blob: 85d516c0b7239b838dddd80ece9c1d91b233546e [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;
63 if (!localizer_resetter.Send()) {
64 LOG(ERROR, "Failed to reset localizer.\n");
65 }
66 }
67
68 // Wait for the drivetrain to run so it has time to reset the heading.
69 // Otherwise our drivetrain reset will do a 180 right at the start.
70 drivetrain_queue.status.FetchAnother();
71 LOG(INFO, "Heading is %f\n", drivetrain_queue.status->estimated_heading);
72 InitializeEncoders();
73 ResetDrivetrain();
74 drivetrain_queue.status.FetchAnother();
75 LOG(INFO, "Heading is %f\n", drivetrain_queue.status->estimated_heading);
76
77 ResetDrivetrain();
78 InitializeEncoders();
79}
80
81const ProfileParameters kJumpDrive = {2.0, 3.0};
82const ProfileParameters kDrive = {4.0, 3.0};
83const ProfileParameters kTurn = {5.0, 15.0};
84
85bool AutonomousActor::RunAction(
86 const ::frc971::autonomous::AutonomousActionParams &params) {
87 monotonic_clock::time_point start_time = monotonic_clock::now();
88 LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode);
89 Reset();
90
91 // Grab the disk, wait until we have vacuum, then jump
92 set_elevator_goal(0.01);
93 set_wrist_goal(-M_PI / 2.0);
94 set_intake_goal(-1.2);
95 set_suction_goal(true, 1);
96 SendSuperstructureGoal();
97
98 if (!WaitForGamePiece()) return true;
99 LOG(INFO, "Has game piece\n");
100
Austin Schuha78857f2019-03-13 22:43:41 -0700101 StartDrive(-4.0, 0.0, kJumpDrive, kTurn);
102 if (!WaitForDriveNear(3.3, 10.0)) return true;
Austin Schuh13379ba2019-03-12 21:06:46 -0700103 LOG(INFO, "Lifting\n");
104 set_elevator_goal(0.30);
105 SendSuperstructureGoal();
106
Austin Schuha78857f2019-03-13 22:43:41 -0700107 if (!WaitForDriveNear(2.8, 10.0)) return true;
Austin Schuh13379ba2019-03-12 21:06:46 -0700108 LOG(INFO, "Off the platform\n");
109
Austin Schuhda0ac232019-03-17 18:13:18 -0700110 StartDrive(0.0, 1.00 * turn_scalar, kDrive, kTurn);
Austin Schuh13379ba2019-03-12 21:06:46 -0700111 LOG(INFO, "Turn started\n");
112 if (!WaitForSuperstructureDone()) return true;
113 LOG(INFO, "Superstructure done\n");
114
Austin Schuha78857f2019-03-13 22:43:41 -0700115 if (!WaitForDriveNear(0.7, 10.0)) return true;
Austin Schuhda0ac232019-03-17 18:13:18 -0700116 StartDrive(0.0, -0.35 * turn_scalar, kDrive, kTurn);
Austin Schuh13379ba2019-03-12 21:06:46 -0700117
118 LOG(INFO, "Elevator up\n");
Austin Schuh291ffe92019-03-24 16:03:59 -0700119 set_elevator_goal(0.78);
Austin Schuh13379ba2019-03-12 21:06:46 -0700120 SendSuperstructureGoal();
121
122 if (!WaitForDriveDone()) return true;
123 LOG(INFO, "Done driving\n");
124
125 if (!WaitForSuperstructureDone()) return true;
126
127 LOG(INFO, "Done %f\n", DoubleSeconds(monotonic_clock::now() - start_time));
128
Austin Schuh13379ba2019-03-12 21:06:46 -0700129 return true;
130}
131
132} // namespace actors
133} // namespace y2019