blob: 6070a71212e4541230cd506f220a784c20ad188e [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
John Park33858a32018-09-28 23:05:48 -07008#include "aos/util/phased_loop.h"
9#include "aos/logging/logging.h"
Tyler Chatowf31da682017-01-22 01:39:40 +000010
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 Schuh19e15d72017-06-24 13:34:47 -070039const ProfileParameters kSmashTurn = {1.5, 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(
Austin Schuheb99d072019-05-12 21:03:38 -070044 ::aos::EventLoop *event_loop,
Philipp Schrader996a2a22017-02-22 05:02:48 +000045 ::frc971::autonomous::AutonomousActionQueueGroup *s)
46 : frc971::autonomous::BaseAutonomousActor(
Austin Schuheb99d072019-05-12 21:03:38 -070047 event_loop, s, control_loops::drivetrain::GetDrivetrainConfig()) {}
Tyler Chatowf31da682017-01-22 01:39:40 +000048
Philipp Schrader996a2a22017-02-22 05:02:48 +000049bool AutonomousActor::RunAction(
50 const ::frc971::autonomous::AutonomousActionParams &params) {
Tyler Chatowf31da682017-01-22 01:39:40 +000051 monotonic_clock::time_point start_time = monotonic_clock::now();
52 LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode);
Austin Schuh624088a2017-03-22 22:36:16 -070053 Reset();
Tyler Chatowf31da682017-01-22 01:39:40 +000054
55 switch (params.mode) {
Austin Schuh19e15d72017-06-24 13:34:47 -070056 case 503: {
57 // Middle gear auto.
58 // Red is positive.
59 // Line up on boiler side edge.
60 constexpr double kDriveDirection = 1.0;
61
62 set_intake_goal(0.18);
63 set_turret_goal(0.0);
64 SendSuperstructureGoal();
65
66 set_turret_goal(-M_PI / 4.0 * kDriveDirection);
67 SendSuperstructureGoal();
68
69 // Turn towards the peg.
70 StartDrive(0.0, kDriveDirection * 0.162, kGearDrive, kSlowTurn);
71 if (!WaitForDriveNear(100.0, 0.02)) return true;
72 if (!WaitForTurnProfileDone()) return true;
73
74 // Drive, but get within 0.3 meters
75 StartDrive(1.73, 0.0, kGearFastDrive, kSlowTurn);
76 if (!WaitForDriveNear(0.3, 0.0)) return true;
77
78 // Now, add a slow, short move to actually place the gear.
79 StartDrive(0.18, 0.0, kGearPlaceDrive, kSecondGearTurn);
80 if (!WaitForDriveNear(0.07, 0.0)) return true;
81
82 set_gear_servo(0.3);
83 SendSuperstructureGoal();
84
85 // Slow down and then pause to let Chris pull the gear off.
86 this_thread::sleep_for(chrono::milliseconds(1000));
87
88 // Back up
89 StartDrive(-1.00, 0.0, kGearFastDrive, kSlowTurn);
90 if (!WaitForDriveNear(0.1, 0.1)) return true;
91
92 // Turn towards the boiler.
93 StartDrive(0.0, -kDriveDirection * M_PI / 2.0, kGearFastDrive, kSlowTurn);
94 if (!WaitForDriveNear(0.1, 0.1)) return true;
95
96 // Drive up near it.
97 StartDrive(1.8, 0.0, kGearFastDrive, kSlowTurn);
98 if (!WaitForDriveNear(0.1, 0.1)) return true;
99
100 set_hood_goal(0.37);
101 set_intake_goal(0.23);
102 set_shooter_velocity(353.0);
103 set_vision_track(true);
104 set_use_vision_for_shots(true);
105 set_indexer_angular_velocity(-1.1 * M_PI);
106 SendSuperstructureGoal();
107
108 this_thread::sleep_for(start_time + chrono::seconds(15) -
109 monotonic_clock::now());
110 if (ShouldCancel()) return true;
111
112 set_shooter_velocity(0.0);
113 set_indexer_angular_velocity(0.0);
114 SendSuperstructureGoal();
115
116 } break;
117
118 case 500: {
Austin Schuh10475d72017-04-16 19:17:48 -0700119 // Red is positive.
120 constexpr double kDriveDirection = -1.0;
121 // Side peg + hopper auto.
122
123 set_intake_goal(0.23);
124 set_turret_goal(-M_PI * kDriveDirection);
125 SendSuperstructureGoal();
126
127 constexpr double kLongPegDrive = 3.025;
128
129 StartDrive(kLongPegDrive, -kDriveDirection * M_PI / 4, kGearDrive,
130 kFirstGearStartTurn);
131 if (!WaitForDriveNear(100.0, M_PI / 8.0)) return true;
132 LOG(INFO, "Turn Middle: %f left to go\n", DriveDistanceLeft());
133
134 StartDrive(0.0, 0.0, kGearDrive, kFirstGearTurn);
135
136 if (!WaitForTurnProfileDone()) return true;
137 LOG(INFO, "Turn profile ended: %f left to go\n", DriveDistanceLeft());
138
139 set_hood_goal(0.43);
140 set_shooter_velocity(364.0);
141 SendSuperstructureGoal();
142
143 constexpr double kTurnDistanceFromStart = 1.08;
144 if (!WaitForDriveNear(kLongPegDrive - kTurnDistanceFromStart, 10.0)) {
145 return true;
146 }
147
148 StartDrive(0.0, kDriveDirection * (M_PI / 4 + 0.3), kGearSlowDrive,
149 kSecondGearTurn);
150 if (!WaitForTurnProfileDone()) return true;
151
152 StartDrive(0.0, 0.0, kGearFastDrive, kSecondGearTurn);
153
154 if (!WaitForDriveNear(0.3, 0.0)) return true;
155
156 set_vision_track(true);
157 SendSuperstructureGoal();
158
159 StartDrive(0.19, 0.0, kGearPlaceDrive, kSecondGearTurn);
160
161 if (!WaitForDriveNear(0.07, 0.0)) return true;
162 set_gear_servo(0.3);
163 SendSuperstructureGoal();
164
165 // Shoot from the peg.
166 //set_indexer_angular_velocity(-2.1 * M_PI);
167 //SendSuperstructureGoal();
168 //this_thread::sleep_for(chrono::milliseconds(1750));
169
170 //this_thread::sleep_for(chrono::milliseconds(500));
171 this_thread::sleep_for(chrono::milliseconds(750));
172 set_indexer_angular_velocity(0.0);
173 set_vision_track(false);
174 set_turret_goal(0.0);
175
176 set_hood_goal(0.37);
177 set_shooter_velocity(351.0);
178 set_intake_goal(0.18);
179 set_gear_servo(0.4);
180
181 SendSuperstructureGoal();
182 LOG(INFO, "Starting drive back %f\n",
183 DoubleSeconds(monotonic_clock::now() - start_time));
184
Austin Schuh19e15d72017-06-24 13:34:47 -0700185 StartDrive(-2.75, kDriveDirection * 1.24, kSlowDrive,
Austin Schuh10475d72017-04-16 19:17:48 -0700186 kFirstGearStartTurn);
187 if (!WaitForDriveNear(2.4, 0.0)) return true;
188
189 if (!WaitForTurnProfileDone()) return true;
190 StartDrive(0.0, 0.0, kGearBallBackDrive, kFirstGearStartTurn);
191
192 if (!WaitForDriveNear(0.2, 0.0)) return true;
193 this_thread::sleep_for(chrono::milliseconds(200));
Austin Schuh19e15d72017-06-24 13:34:47 -0700194 // Trip the hopper
Austin Schuh10475d72017-04-16 19:17:48 -0700195 StartDrive(0.0, kDriveDirection * 1.10, kSlowDrive,
196 kSmashTurn);
197
198 if (!WaitForDriveNear(0.2, 0.35)) return true;
199 set_vision_track(true);
200 set_use_vision_for_shots(true);
201 SendSuperstructureGoal();
202
203 if (!WaitForDriveNear(0.2, 0.2)) return true;
204 StartDrive(0.0, -kDriveDirection * 0.15, kSlowDrive,
205 kSmashTurn);
206
207 LOG(INFO, "Starting second shot %f\n",
208 DoubleSeconds(monotonic_clock::now() - start_time));
209 set_indexer_angular_velocity(-2.15 * M_PI);
210 SendSuperstructureGoal();
211 if (!WaitForDriveNear(0.2, 0.1)) return true;
212
213 this_thread::sleep_for(start_time + chrono::seconds(11) -
214 monotonic_clock::now());
215 if (ShouldCancel()) return true;
216 set_intake_max_velocity(0.05);
217 set_intake_goal(0.08);
218
219 this_thread::sleep_for(start_time + chrono::seconds(15) -
220 monotonic_clock::now());
221 if (ShouldCancel()) return true;
222
223 set_intake_max_velocity(0.50);
224 set_intake_goal(0.18);
225 set_shooter_velocity(0.0);
226 set_indexer_angular_velocity(0.0);
227 SendSuperstructureGoal();
228
229 } break;
230
Austin Schuh19e15d72017-06-24 13:34:47 -0700231 default: {
232 // hopper auto
Austin Schuh10475d72017-04-16 19:17:48 -0700233 // Red is positive.
Austin Schuh624088a2017-03-22 22:36:16 -0700234 constexpr double kDriveDirection = 1.0;
Austin Schuh624088a2017-03-22 22:36:16 -0700235 set_intake_goal(0.07);
236 SendSuperstructureGoal();
Austin Schuh10475d72017-04-16 19:17:48 -0700237
Austin Schuh19e15d72017-06-24 13:34:47 -0700238 StartDrive(-3.42, kDriveDirection * (M_PI / 10 - 0.057) , kSlowDrive, kFirstTurn);
Austin Schuh10475d72017-04-16 19:17:48 -0700239 if (!WaitForDriveNear(3.30, 0.0)) return true;
240 LOG(INFO, "Turn ended: %f left to go\n", DriveDistanceLeft());
241 // We can go to 2.50 before we hit the previous profile.
242
Austin Schuh19e15d72017-06-24 13:34:47 -0700243 if (!WaitForDriveNear(2.48, 0.0)) return true;
Austin Schuh10475d72017-04-16 19:17:48 -0700244 LOG(INFO, "%f left to go\n", DriveDistanceLeft());
Austin Schuh366f7ed2017-03-11 21:57:14 -0800245
Austin Schuh624088a2017-03-22 22:36:16 -0700246 set_intake_goal(0.23);
Austin Schuh76af51e2017-04-09 18:32:38 -0700247 set_turret_goal(0.0);
248 // Values good for blue:
249 // TODO(austin): Drive these off the auto switch.
Austin Schuh10475d72017-04-16 19:17:48 -0700250
251 set_hood_goal(0.37);
252 set_shooter_velocity(353.0);
Austin Schuh624088a2017-03-22 22:36:16 -0700253 SendSuperstructureGoal();
Austin Schuh366f7ed2017-03-11 21:57:14 -0800254
Austin Schuh10475d72017-04-16 19:17:48 -0700255 StartDrive(0.0, -M_PI / 8.0 * kDriveDirection, kSlowDrive, kSlowTurn);
256 if (!WaitForDriveNear(0.20, 0.0)) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800257
Austin Schuh10475d72017-04-16 19:17:48 -0700258 this_thread::sleep_for(chrono::milliseconds(300));
259
Austin Schuh19e15d72017-06-24 13:34:47 -0700260 // Turn to trigger the hopper.
Austin Schuh10475d72017-04-16 19:17:48 -0700261 StartDrive(0.0, (M_PI / 8.0 + 0.20) * kDriveDirection, kSlowDrive,
Austin Schuh624088a2017-03-22 22:36:16 -0700262 kSmashTurn);
263 if (!WaitForDriveNear(0.05, 0.2)) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800264
Austin Schuh624088a2017-03-22 22:36:16 -0700265 set_vision_track(true);
Austin Schuh10475d72017-04-16 19:17:48 -0700266 set_use_vision_for_shots(true);
Austin Schuh624088a2017-03-22 22:36:16 -0700267 SendSuperstructureGoal();
Austin Schuh366f7ed2017-03-11 21:57:14 -0800268
Austin Schuh19e15d72017-06-24 13:34:47 -0700269 // Now that everything is tracking, wait for the hood to zero before
270 // trying to shoot.
271 WaitForHoodZeroed();
272 if (ShouldCancel()) return true;
273
Austin Schuh624088a2017-03-22 22:36:16 -0700274 this_thread::sleep_for(chrono::milliseconds(200));
Austin Schuh366f7ed2017-03-11 21:57:14 -0800275
Austin Schuh19e15d72017-06-24 13:34:47 -0700276 // Turn back.
Austin Schuh624088a2017-03-22 22:36:16 -0700277 StartDrive(0.0, (-0.15) * kDriveDirection, kSlowDrive, kSlowTurn);
278 if (!WaitForDriveNear(0.05, 0.02)) return true;
Austin Schuh76af51e2017-04-09 18:32:38 -0700279
Austin Schuh19e15d72017-06-24 13:34:47 -0700280 set_indexer_angular_velocity(-2.1 * M_PI);
281 SendSuperstructureGoal();
282
Austin Schuh624088a2017-03-22 22:36:16 -0700283 LOG(INFO, "Started shooting at %f\n",
284 DoubleSeconds(monotonic_clock::now() - start_time));
Austin Schuh366f7ed2017-03-11 21:57:14 -0800285
Austin Schuh624088a2017-03-22 22:36:16 -0700286 this_thread::sleep_for(start_time + chrono::seconds(9) -
287 monotonic_clock::now());
Austin Schuh624088a2017-03-22 22:36:16 -0700288 if (ShouldCancel()) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800289
Austin Schuh624088a2017-03-22 22:36:16 -0700290 set_intake_max_velocity(0.05);
291 set_intake_goal(0.08);
292 SendSuperstructureGoal();
Tyler Chatowf31da682017-01-22 01:39:40 +0000293
Austin Schuh624088a2017-03-22 22:36:16 -0700294 this_thread::sleep_for(start_time + chrono::seconds(15) -
295 monotonic_clock::now());
296 if (ShouldCancel()) return true;
297
298 set_shooter_velocity(0.0);
299 set_indexer_angular_velocity(0.0);
300 SendSuperstructureGoal();
301
302 } break;
Tyler Chatowf31da682017-01-22 01:39:40 +0000303 }
304
305 LOG(INFO, "Done %f\n", DoubleSeconds(monotonic_clock::now() - start_time));
306
307 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
308 ::std::chrono::milliseconds(5) / 2);
309
310 while (!ShouldCancel()) {
311 phased_loop.SleepUntilNext();
312 }
313 LOG(DEBUG, "Done running\n");
314
315 return true;
316}
317
Tyler Chatowf31da682017-01-22 01:39:40 +0000318} // namespace actors
319} // namespace y2017