blob: 814988033f03d88cf1a3c949f4b6f19958c457bc [file] [log] [blame]
Tyler Chatowf31da682017-01-22 01:39:40 +00001#include "y2017/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"
Philipp Schrader996a2a22017-02-22 05:02:48 +000012#include "y2017/control_loops/drivetrain/drivetrain_base.h"
Tyler Chatowf31da682017-01-22 01:39:40 +000013
14namespace y2017 {
15namespace actors {
16using ::frc971::control_loops::drivetrain_queue;
17using ::aos::monotonic_clock;
18namespace chrono = ::std::chrono;
19namespace this_thread = ::std::this_thread;
20
21namespace {
Philipp Schrader85fca552017-03-05 00:30:50 +000022
Tyler Chatowf31da682017-01-22 01:39:40 +000023double DoubleSeconds(monotonic_clock::duration duration) {
24 return ::std::chrono::duration_cast<::std::chrono::duration<double>>(duration)
25 .count();
26}
Philipp Schrader85fca552017-03-05 00:30:50 +000027
Austin Schuh10475d72017-04-16 19:17:48 -070028const ProfileParameters kGearBallBackDrive = {3.0, 3.5};
29const ProfileParameters kGearDrive = {1.5, 2.0};
30const ProfileParameters kGearFastDrive = {2.0, 2.5};
31const ProfileParameters kGearSlowDrive = {1.0, 2.0};
32const ProfileParameters kGearPlaceDrive = {0.40, 2.0};
Austin Schuh624088a2017-03-22 22:36:16 -070033const ProfileParameters kSlowDrive = {3.0, 2.0};
Austin Schuh366f7ed2017-03-11 21:57:14 -080034const ProfileParameters kSlowTurn = {3.0, 3.0};
Austin Schuh10475d72017-04-16 19:17:48 -070035const ProfileParameters kFirstTurn = {1.0, 1.5};
36const ProfileParameters kFirstGearStartTurn = {2.0, 3.0};
37const ProfileParameters kFirstGearTurn = {2.0, 5.0};
38const ProfileParameters kSecondGearTurn = {3.0, 5.0};
Austin Schuh624088a2017-03-22 22:36:16 -070039const ProfileParameters kSmashTurn = {3.0, 5.0};
Philipp Schrader85fca552017-03-05 00:30:50 +000040
Tyler Chatowf31da682017-01-22 01:39:40 +000041} // namespace
42
Philipp Schrader996a2a22017-02-22 05:02:48 +000043AutonomousActor::AutonomousActor(
44 ::frc971::autonomous::AutonomousActionQueueGroup *s)
45 : frc971::autonomous::BaseAutonomousActor(
46 s, control_loops::drivetrain::GetDrivetrainConfig()) {}
Tyler Chatowf31da682017-01-22 01:39:40 +000047
Philipp Schrader996a2a22017-02-22 05:02:48 +000048bool AutonomousActor::RunAction(
49 const ::frc971::autonomous::AutonomousActionParams &params) {
Tyler Chatowf31da682017-01-22 01:39:40 +000050 monotonic_clock::time_point start_time = monotonic_clock::now();
51 LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode);
Austin Schuh624088a2017-03-22 22:36:16 -070052 Reset();
Tyler Chatowf31da682017-01-22 01:39:40 +000053
54 switch (params.mode) {
Austin Schuh624088a2017-03-22 22:36:16 -070055 default: {
Austin Schuh10475d72017-04-16 19:17:48 -070056 // Red is positive.
57 constexpr double kDriveDirection = -1.0;
58 // Side peg + hopper auto.
59
60 set_intake_goal(0.23);
61 set_turret_goal(-M_PI * kDriveDirection);
62 SendSuperstructureGoal();
63
64 constexpr double kLongPegDrive = 3.025;
65
66 StartDrive(kLongPegDrive, -kDriveDirection * M_PI / 4, kGearDrive,
67 kFirstGearStartTurn);
68 if (!WaitForDriveNear(100.0, M_PI / 8.0)) return true;
69 LOG(INFO, "Turn Middle: %f left to go\n", DriveDistanceLeft());
70
71 StartDrive(0.0, 0.0, kGearDrive, kFirstGearTurn);
72
73 if (!WaitForTurnProfileDone()) return true;
74 LOG(INFO, "Turn profile ended: %f left to go\n", DriveDistanceLeft());
75
76 set_hood_goal(0.43);
77 set_shooter_velocity(364.0);
78 SendSuperstructureGoal();
79
80 constexpr double kTurnDistanceFromStart = 1.08;
81 if (!WaitForDriveNear(kLongPegDrive - kTurnDistanceFromStart, 10.0)) {
82 return true;
83 }
84
85 StartDrive(0.0, kDriveDirection * (M_PI / 4 + 0.3), kGearSlowDrive,
86 kSecondGearTurn);
87 if (!WaitForTurnProfileDone()) return true;
88
89 StartDrive(0.0, 0.0, kGearFastDrive, kSecondGearTurn);
90
91 if (!WaitForDriveNear(0.3, 0.0)) return true;
92
93 set_vision_track(true);
94 SendSuperstructureGoal();
95
96 StartDrive(0.19, 0.0, kGearPlaceDrive, kSecondGearTurn);
97
98 if (!WaitForDriveNear(0.07, 0.0)) return true;
99 set_gear_servo(0.3);
100 SendSuperstructureGoal();
101
102 // Shoot from the peg.
103 //set_indexer_angular_velocity(-2.1 * M_PI);
104 //SendSuperstructureGoal();
105 //this_thread::sleep_for(chrono::milliseconds(1750));
106
107 //this_thread::sleep_for(chrono::milliseconds(500));
108 this_thread::sleep_for(chrono::milliseconds(750));
109 set_indexer_angular_velocity(0.0);
110 set_vision_track(false);
111 set_turret_goal(0.0);
112
113 set_hood_goal(0.37);
114 set_shooter_velocity(351.0);
115 set_intake_goal(0.18);
116 set_gear_servo(0.4);
117
118 SendSuperstructureGoal();
119 LOG(INFO, "Starting drive back %f\n",
120 DoubleSeconds(monotonic_clock::now() - start_time));
121
122 StartDrive(-2.69, kDriveDirection * 1.30, kSlowDrive,
123 kFirstGearStartTurn);
124 if (!WaitForDriveNear(2.4, 0.0)) return true;
125
126 if (!WaitForTurnProfileDone()) return true;
127 StartDrive(0.0, 0.0, kGearBallBackDrive, kFirstGearStartTurn);
128
129 if (!WaitForDriveNear(0.2, 0.0)) return true;
130 this_thread::sleep_for(chrono::milliseconds(200));
131 StartDrive(0.0, kDriveDirection * 1.10, kSlowDrive,
132 kSmashTurn);
133
134 if (!WaitForDriveNear(0.2, 0.35)) return true;
135 set_vision_track(true);
136 set_use_vision_for_shots(true);
137 SendSuperstructureGoal();
138
139 if (!WaitForDriveNear(0.2, 0.2)) return true;
140 StartDrive(0.0, -kDriveDirection * 0.15, kSlowDrive,
141 kSmashTurn);
142
143 LOG(INFO, "Starting second shot %f\n",
144 DoubleSeconds(monotonic_clock::now() - start_time));
145 set_indexer_angular_velocity(-2.15 * M_PI);
146 SendSuperstructureGoal();
147 if (!WaitForDriveNear(0.2, 0.1)) return true;
148
149 this_thread::sleep_for(start_time + chrono::seconds(11) -
150 monotonic_clock::now());
151 if (ShouldCancel()) return true;
152 set_intake_max_velocity(0.05);
153 set_intake_goal(0.08);
154
155 this_thread::sleep_for(start_time + chrono::seconds(15) -
156 monotonic_clock::now());
157 if (ShouldCancel()) return true;
158
159 set_intake_max_velocity(0.50);
160 set_intake_goal(0.18);
161 set_shooter_velocity(0.0);
162 set_indexer_angular_velocity(0.0);
163 SendSuperstructureGoal();
164
165 } break;
166
167 case 500: {
168 // Red is positive.
Austin Schuh624088a2017-03-22 22:36:16 -0700169 constexpr double kDriveDirection = 1.0;
Austin Schuh10475d72017-04-16 19:17:48 -0700170 // Side hopper auto.
Austin Schuh624088a2017-03-22 22:36:16 -0700171 set_intake_goal(0.07);
172 SendSuperstructureGoal();
Austin Schuh10475d72017-04-16 19:17:48 -0700173
174 StartDrive(-3.3, kDriveDirection * M_PI / 10, kSlowDrive, kFirstTurn);
175 if (!WaitForDriveNear(3.30, 0.0)) return true;
176 LOG(INFO, "Turn ended: %f left to go\n", DriveDistanceLeft());
177 // We can go to 2.50 before we hit the previous profile.
178
179 if (!WaitForDriveNear(2.42, 0.0)) return true;
180 LOG(INFO, "%f left to go\n", DriveDistanceLeft());
Austin Schuh366f7ed2017-03-11 21:57:14 -0800181
Austin Schuh624088a2017-03-22 22:36:16 -0700182 set_intake_goal(0.23);
Austin Schuh76af51e2017-04-09 18:32:38 -0700183 set_turret_goal(0.0);
184 // Values good for blue:
185 // TODO(austin): Drive these off the auto switch.
Austin Schuh10475d72017-04-16 19:17:48 -0700186
187 set_hood_goal(0.37);
188 set_shooter_velocity(353.0);
Austin Schuh624088a2017-03-22 22:36:16 -0700189 SendSuperstructureGoal();
Austin Schuh366f7ed2017-03-11 21:57:14 -0800190
Austin Schuh10475d72017-04-16 19:17:48 -0700191 StartDrive(0.0, -M_PI / 8.0 * kDriveDirection, kSlowDrive, kSlowTurn);
192 if (!WaitForDriveNear(0.20, 0.0)) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800193
Austin Schuh10475d72017-04-16 19:17:48 -0700194 this_thread::sleep_for(chrono::milliseconds(300));
195
196 StartDrive(0.0, (M_PI / 8.0 + 0.20) * kDriveDirection, kSlowDrive,
Austin Schuh624088a2017-03-22 22:36:16 -0700197 kSmashTurn);
198 if (!WaitForDriveNear(0.05, 0.2)) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800199
Austin Schuh624088a2017-03-22 22:36:16 -0700200 set_vision_track(true);
Austin Schuh10475d72017-04-16 19:17:48 -0700201 set_use_vision_for_shots(true);
Austin Schuh76af51e2017-04-09 18:32:38 -0700202
Austin Schuh10475d72017-04-16 19:17:48 -0700203 set_indexer_angular_velocity(-2.1 * M_PI);
Austin Schuh624088a2017-03-22 22:36:16 -0700204 SendSuperstructureGoal();
Austin Schuh366f7ed2017-03-11 21:57:14 -0800205
Austin Schuh624088a2017-03-22 22:36:16 -0700206 this_thread::sleep_for(chrono::milliseconds(200));
Austin Schuh366f7ed2017-03-11 21:57:14 -0800207
Austin Schuh624088a2017-03-22 22:36:16 -0700208 StartDrive(0.0, (-0.15) * kDriveDirection, kSlowDrive, kSlowTurn);
209 if (!WaitForDriveNear(0.05, 0.02)) return true;
Austin Schuh76af51e2017-04-09 18:32:38 -0700210
Austin Schuh624088a2017-03-22 22:36:16 -0700211 LOG(INFO, "Started shooting at %f\n",
212 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh366f7ed2017-03-11 21:57:14 -0800213
Austin Schuh624088a2017-03-22 22:36:16 -0700214 this_thread::sleep_for(start_time + chrono::seconds(9) -
215 monotonic_clock::now());
Austin Schuh624088a2017-03-22 22:36:16 -0700216 if (ShouldCancel()) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800217
Austin Schuh624088a2017-03-22 22:36:16 -0700218 set_intake_max_velocity(0.05);
219 set_intake_goal(0.08);
220 SendSuperstructureGoal();
Tyler Chatowf31da682017-01-22 01:39:40 +0000221
Austin Schuh624088a2017-03-22 22:36:16 -0700222 this_thread::sleep_for(start_time + chrono::seconds(15) -
223 monotonic_clock::now());
224 if (ShouldCancel()) return true;
225
226 set_shooter_velocity(0.0);
227 set_indexer_angular_velocity(0.0);
228 SendSuperstructureGoal();
229
230 } break;
Tyler Chatowf31da682017-01-22 01:39:40 +0000231 }
232
233 LOG(INFO, "Done %f\n", DoubleSeconds(monotonic_clock::now() - start_time));
234
235 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
236 ::std::chrono::milliseconds(5) / 2);
237
238 while (!ShouldCancel()) {
239 phased_loop.SleepUntilNext();
240 }
241 LOG(DEBUG, "Done running\n");
242
243 return true;
244}
245
Tyler Chatowf31da682017-01-22 01:39:40 +0000246} // namespace actors
247} // namespace y2017