blob: a31d2f309b9710bafeee68c0d3c0b284117c7606 [file] [log] [blame]
Austin Schuha3c148e2018-03-09 21:04:05 -08001#include "y2018/actors/autonomous_actor.h"
2
3#include <inttypes.h>
4
5#include <chrono>
6#include <cmath>
7
8#include "aos/common/util/phased_loop.h"
9#include "aos/common/logging/logging.h"
10
11#include "frc971/control_loops/drivetrain/drivetrain.q.h"
12#include "y2018/control_loops/drivetrain/drivetrain_base.h"
13
14namespace y2018 {
15namespace actors {
16using ::frc971::ProfileParameters;
17
18using ::frc971::control_loops::drivetrain_queue;
19using ::aos::monotonic_clock;
20namespace chrono = ::std::chrono;
21namespace this_thread = ::std::this_thread;
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 Schuhc231df42018-03-21 20:43:24 -070030const ProfileParameters kFinalSwitchDrive = {0.5, 0.5};
31const ProfileParameters kDrive = {5.0, 2.5};
32const ProfileParameters kSlowDrive = {1.5, 2.5};
33const ProfileParameters kFarSwitchTurnDrive = {2.0, 2.5};
34const ProfileParameters kTurn = {4.0, 2.0};
35const ProfileParameters kSweepingTurn = {5.0, 7.0};
36const ProfileParameters kFastTurn = {5.0, 7.0};
Austin Schuha3c148e2018-03-09 21:04:05 -080037
38} // namespace
39
40AutonomousActor::AutonomousActor(
41 ::frc971::autonomous::AutonomousActionQueueGroup *s)
42 : frc971::autonomous::BaseAutonomousActor(
43 s, control_loops::drivetrain::GetDrivetrainConfig()) {}
44
45bool AutonomousActor::RunAction(
46 const ::frc971::autonomous::AutonomousActionParams &params) {
47 monotonic_clock::time_point start_time = monotonic_clock::now();
48 LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode);
49 Reset();
50
Austin Schuhc231df42018-03-21 20:43:24 -070051 switch (params.mode) {
52 case 1: {
53 constexpr double kDriveDistance = 3.2;
54 // Start on the left. Drive, arc a turn, and drop in the close switch.
55 StartDrive(-kDriveDistance, 0.0, kDrive, kTurn);
56 if (!WaitForDriveNear(2.0, M_PI / 2.0)) return true;
Austin Schuha3c148e2018-03-09 21:04:05 -080057
Austin Schuhc231df42018-03-21 20:43:24 -070058 // Now, close so let's move the arm up.
59 set_arm_goal_position(arm::BackSwitchIndex());
60 SendSuperstructureGoal();
61
62 StartDrive(0.0, 0.0, kSlowDrive, kSweepingTurn);
63 if (!WaitForDriveNear(1.6, M_PI / 2.0)) return true;
64
65 StartDrive(0.0, -M_PI / 4.0 - 0.2, kSlowDrive, kSweepingTurn);
66 if (!WaitForDriveNear(0.2, 0.2)) return true;
67 set_max_drivetrain_voltage(6.0);
68 ::std::this_thread::sleep_for(chrono::milliseconds(300));
69
70 set_open_claw(true);
71 SendSuperstructureGoal();
72
73 ::std::this_thread::sleep_for(chrono::milliseconds(500));
74 set_max_drivetrain_voltage(12.0);
75 StartDrive(0.2, 0.0, kDrive, kTurn);
76 if (!WaitForTurnProfileDone()) return true;
77
78 set_arm_goal_position(arm::NeutralIndex());
79 SendSuperstructureGoal();
80
81 } break;
82 case 0: {
83 // Start on the left. Hit the scale.
84 constexpr double kFullDriveLength = 9.95;
85 constexpr double kTurnDistance = 4.40;
86 StartDrive(-kFullDriveLength, 0.0, kDrive, kTurn);
87 if (!WaitForDriveProfileNear(kFullDriveLength - (kTurnDistance - 1.4)))
88 return true;
89 StartDrive(0.0, 0.0, kFarSwitchTurnDrive, kTurn);
90
91 if (!WaitForDriveProfileNear(kFullDriveLength - kTurnDistance))
92 return true;
93 StartDrive(0.0, -M_PI / 2.0, kFarSwitchTurnDrive, kSweepingTurn);
94 if (!WaitForTurnProfileDone()) return true;
95 StartDrive(0.0, 0.0, kDrive, kTurn);
96 if (!WaitForDriveProfileDone()) return true;
97
98 // Now, close so let's move the arm up.
99 set_arm_goal_position(arm::FrontSwitchAutoIndex());
100 SendSuperstructureGoal();
101
102 StartDrive(0.0, M_PI / 2.0, kDrive, kFastTurn);
103 if (!WaitForTurnProfileDone()) return true;
104
105 set_max_drivetrain_voltage(6.0);
106 StartDrive(0.35, 0.0, kFinalSwitchDrive, kTurn);
107 if (!WaitForArmTrajectoryClose(0.001)) return true;
108 //if (!WaitForDriveNear(0.2, M_PI / 2.0)) return true;
109 ::std::this_thread::sleep_for(chrono::milliseconds(1500));
110
111 set_open_claw(true);
112 SendSuperstructureGoal();
113
114 ::std::this_thread::sleep_for(chrono::milliseconds(1500));
115 set_arm_goal_position(arm::NeutralIndex());
116 SendSuperstructureGoal();
117 if (ShouldCancel()) return true;
118 set_max_drivetrain_voltage(12.0);
119 StartDrive(-0.5, 0.0, kDrive, kTurn);
120 if (!WaitForDriveProfileDone()) return true;
121 } break;
122
123 case 3:
124 case 2: {
125 // Start on the left. Hit the scale.
126 constexpr double kDriveDistance = 7.0;
127 // Drive.
128 StartDrive(-kDriveDistance, 0.0, kDrive, kTurn);
129 if (!WaitForDriveNear(kDriveDistance - 1.0, M_PI / 2.0)) return true;
130 // Once we are away from the wall, start the arm.
131 set_arm_goal_position(arm::BackHighBoxIndex());
132 SendSuperstructureGoal();
133
134 // We are starting to get close. Slow down for the turn.
135 if (!WaitForDriveNear(2.5, M_PI / 2.0)) return true;
136 StartDrive(0.0, 0.0, kSlowDrive, kSweepingTurn);
137
138 // Once we've gotten slowed down a bit, start turning.
139 if (!WaitForDriveNear(1.6, M_PI / 2.0)) return true;
140 StartDrive(0.0, -M_PI / 4.0 - 0.1, kSlowDrive, kSweepingTurn);
141
142 // Get close and open the claw.
143 if (!WaitForDriveNear(0.25, 0.25)) return true;
144 set_open_claw(true);
145 SendSuperstructureGoal();
146
147 ::std::this_thread::sleep_for(chrono::milliseconds(500));
148
149 StartDrive(0.25, 0.0, kDrive, kTurn);
150 if (!WaitForDriveProfileDone()) return true;
151 } break;
Austin Schuha3c148e2018-03-09 21:04:05 -0800152 }
153
154 LOG(INFO, "Done %f\n", DoubleSeconds(monotonic_clock::now() - start_time));
155
156 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
157 ::std::chrono::milliseconds(5) / 2);
158
159 while (!ShouldCancel()) {
160 phased_loop.SleepUntilNext();
161 }
162 LOG(DEBUG, "Done running\n");
163
164 return true;
165}
166
167} // namespace actors
168} // namespace y2018