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