blob: ee17f49403efff7ec7b4925a379796f80ab5d6d8 [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/logging/logging.h"
James Kuszmaul651fc3f2019-05-15 21:14:25 -07009#include "aos/util/phased_loop.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 {
Tyler Chatowf31da682017-01-22 01:39:40 +000016using ::aos::monotonic_clock;
James Kuszmaul651fc3f2019-05-15 21:14:25 -070017using ::frc971::control_loops::drivetrain_queue;
Tyler Chatowf31da682017-01-22 01:39:40 +000018namespace chrono = ::std::chrono;
19namespace this_thread = ::std::this_thread;
20
21namespace {
Philipp Schrader85fca552017-03-05 00:30:50 +000022
Austin Schuh10475d72017-04-16 19:17:48 -070023const ProfileParameters kGearBallBackDrive = {3.0, 3.5};
24const ProfileParameters kGearDrive = {1.5, 2.0};
25const ProfileParameters kGearFastDrive = {2.0, 2.5};
26const ProfileParameters kGearSlowDrive = {1.0, 2.0};
27const ProfileParameters kGearPlaceDrive = {0.40, 2.0};
Austin Schuh624088a2017-03-22 22:36:16 -070028const ProfileParameters kSlowDrive = {3.0, 2.0};
Austin Schuh366f7ed2017-03-11 21:57:14 -080029const ProfileParameters kSlowTurn = {3.0, 3.0};
Austin Schuh10475d72017-04-16 19:17:48 -070030const ProfileParameters kFirstTurn = {1.0, 1.5};
31const ProfileParameters kFirstGearStartTurn = {2.0, 3.0};
32const ProfileParameters kFirstGearTurn = {2.0, 5.0};
33const ProfileParameters kSecondGearTurn = {3.0, 5.0};
Austin Schuh19e15d72017-06-24 13:34:47 -070034const ProfileParameters kSmashTurn = {1.5, 5.0};
Philipp Schrader85fca552017-03-05 00:30:50 +000035
Tyler Chatowf31da682017-01-22 01:39:40 +000036} // namespace
37
Philipp Schrader996a2a22017-02-22 05:02:48 +000038AutonomousActor::AutonomousActor(
Austin Schuheb99d072019-05-12 21:03:38 -070039 ::aos::EventLoop *event_loop,
Philipp Schrader996a2a22017-02-22 05:02:48 +000040 ::frc971::autonomous::AutonomousActionQueueGroup *s)
41 : frc971::autonomous::BaseAutonomousActor(
Austin Schuheb99d072019-05-12 21:03:38 -070042 event_loop, s, control_loops::drivetrain::GetDrivetrainConfig()) {}
Tyler Chatowf31da682017-01-22 01:39:40 +000043
Philipp Schrader996a2a22017-02-22 05:02:48 +000044bool AutonomousActor::RunAction(
45 const ::frc971::autonomous::AutonomousActionParams &params) {
Tyler Chatowf31da682017-01-22 01:39:40 +000046 monotonic_clock::time_point start_time = monotonic_clock::now();
47 LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode);
Austin Schuh624088a2017-03-22 22:36:16 -070048 Reset();
Tyler Chatowf31da682017-01-22 01:39:40 +000049
50 switch (params.mode) {
Austin Schuh19e15d72017-06-24 13:34:47 -070051 case 503: {
52 // Middle gear auto.
53 // Red is positive.
54 // Line up on boiler side edge.
55 constexpr double kDriveDirection = 1.0;
56
57 set_intake_goal(0.18);
58 set_turret_goal(0.0);
59 SendSuperstructureGoal();
60
61 set_turret_goal(-M_PI / 4.0 * kDriveDirection);
62 SendSuperstructureGoal();
63
64 // Turn towards the peg.
65 StartDrive(0.0, kDriveDirection * 0.162, kGearDrive, kSlowTurn);
66 if (!WaitForDriveNear(100.0, 0.02)) return true;
67 if (!WaitForTurnProfileDone()) return true;
68
69 // Drive, but get within 0.3 meters
70 StartDrive(1.73, 0.0, kGearFastDrive, kSlowTurn);
71 if (!WaitForDriveNear(0.3, 0.0)) return true;
72
73 // Now, add a slow, short move to actually place the gear.
74 StartDrive(0.18, 0.0, kGearPlaceDrive, kSecondGearTurn);
75 if (!WaitForDriveNear(0.07, 0.0)) return true;
76
77 set_gear_servo(0.3);
78 SendSuperstructureGoal();
79
80 // Slow down and then pause to let Chris pull the gear off.
81 this_thread::sleep_for(chrono::milliseconds(1000));
82
83 // Back up
84 StartDrive(-1.00, 0.0, kGearFastDrive, kSlowTurn);
85 if (!WaitForDriveNear(0.1, 0.1)) return true;
86
87 // Turn towards the boiler.
88 StartDrive(0.0, -kDriveDirection * M_PI / 2.0, kGearFastDrive, kSlowTurn);
89 if (!WaitForDriveNear(0.1, 0.1)) return true;
90
91 // Drive up near it.
92 StartDrive(1.8, 0.0, kGearFastDrive, kSlowTurn);
93 if (!WaitForDriveNear(0.1, 0.1)) return true;
94
95 set_hood_goal(0.37);
96 set_intake_goal(0.23);
97 set_shooter_velocity(353.0);
98 set_vision_track(true);
99 set_use_vision_for_shots(true);
100 set_indexer_angular_velocity(-1.1 * M_PI);
101 SendSuperstructureGoal();
102
103 this_thread::sleep_for(start_time + chrono::seconds(15) -
104 monotonic_clock::now());
105 if (ShouldCancel()) return true;
106
107 set_shooter_velocity(0.0);
108 set_indexer_angular_velocity(0.0);
109 SendSuperstructureGoal();
110
111 } break;
112
113 case 500: {
Austin Schuh10475d72017-04-16 19:17:48 -0700114 // Red is positive.
115 constexpr double kDriveDirection = -1.0;
116 // Side peg + hopper auto.
117
118 set_intake_goal(0.23);
119 set_turret_goal(-M_PI * kDriveDirection);
120 SendSuperstructureGoal();
121
122 constexpr double kLongPegDrive = 3.025;
123
124 StartDrive(kLongPegDrive, -kDriveDirection * M_PI / 4, kGearDrive,
125 kFirstGearStartTurn);
126 if (!WaitForDriveNear(100.0, M_PI / 8.0)) return true;
127 LOG(INFO, "Turn Middle: %f left to go\n", DriveDistanceLeft());
128
129 StartDrive(0.0, 0.0, kGearDrive, kFirstGearTurn);
130
131 if (!WaitForTurnProfileDone()) return true;
132 LOG(INFO, "Turn profile ended: %f left to go\n", DriveDistanceLeft());
133
134 set_hood_goal(0.43);
135 set_shooter_velocity(364.0);
136 SendSuperstructureGoal();
137
138 constexpr double kTurnDistanceFromStart = 1.08;
139 if (!WaitForDriveNear(kLongPegDrive - kTurnDistanceFromStart, 10.0)) {
140 return true;
141 }
142
143 StartDrive(0.0, kDriveDirection * (M_PI / 4 + 0.3), kGearSlowDrive,
144 kSecondGearTurn);
145 if (!WaitForTurnProfileDone()) return true;
146
147 StartDrive(0.0, 0.0, kGearFastDrive, kSecondGearTurn);
148
149 if (!WaitForDriveNear(0.3, 0.0)) return true;
150
151 set_vision_track(true);
152 SendSuperstructureGoal();
153
154 StartDrive(0.19, 0.0, kGearPlaceDrive, kSecondGearTurn);
155
156 if (!WaitForDriveNear(0.07, 0.0)) return true;
157 set_gear_servo(0.3);
158 SendSuperstructureGoal();
159
160 // Shoot from the peg.
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700161 // set_indexer_angular_velocity(-2.1 * M_PI);
162 // SendSuperstructureGoal();
163 // this_thread::sleep_for(chrono::milliseconds(1750));
Austin Schuh10475d72017-04-16 19:17:48 -0700164
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700165 // this_thread::sleep_for(chrono::milliseconds(500));
Austin Schuh10475d72017-04-16 19:17:48 -0700166 this_thread::sleep_for(chrono::milliseconds(750));
167 set_indexer_angular_velocity(0.0);
168 set_vision_track(false);
169 set_turret_goal(0.0);
170
171 set_hood_goal(0.37);
172 set_shooter_velocity(351.0);
173 set_intake_goal(0.18);
174 set_gear_servo(0.4);
175
176 SendSuperstructureGoal();
177 LOG(INFO, "Starting drive back %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700178 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh10475d72017-04-16 19:17:48 -0700179
Austin Schuh19e15d72017-06-24 13:34:47 -0700180 StartDrive(-2.75, kDriveDirection * 1.24, kSlowDrive,
Austin Schuh10475d72017-04-16 19:17:48 -0700181 kFirstGearStartTurn);
182 if (!WaitForDriveNear(2.4, 0.0)) return true;
183
184 if (!WaitForTurnProfileDone()) return true;
185 StartDrive(0.0, 0.0, kGearBallBackDrive, kFirstGearStartTurn);
186
187 if (!WaitForDriveNear(0.2, 0.0)) return true;
188 this_thread::sleep_for(chrono::milliseconds(200));
Austin Schuh19e15d72017-06-24 13:34:47 -0700189 // Trip the hopper
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700190 StartDrive(0.0, kDriveDirection * 1.10, kSlowDrive, kSmashTurn);
Austin Schuh10475d72017-04-16 19:17:48 -0700191
192 if (!WaitForDriveNear(0.2, 0.35)) return true;
193 set_vision_track(true);
194 set_use_vision_for_shots(true);
195 SendSuperstructureGoal();
196
197 if (!WaitForDriveNear(0.2, 0.2)) return true;
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700198 StartDrive(0.0, -kDriveDirection * 0.15, kSlowDrive, kSmashTurn);
Austin Schuh10475d72017-04-16 19:17:48 -0700199
200 LOG(INFO, "Starting second shot %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700201 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh10475d72017-04-16 19:17:48 -0700202 set_indexer_angular_velocity(-2.15 * M_PI);
203 SendSuperstructureGoal();
204 if (!WaitForDriveNear(0.2, 0.1)) return true;
205
206 this_thread::sleep_for(start_time + chrono::seconds(11) -
207 monotonic_clock::now());
208 if (ShouldCancel()) return true;
209 set_intake_max_velocity(0.05);
210 set_intake_goal(0.08);
211
212 this_thread::sleep_for(start_time + chrono::seconds(15) -
213 monotonic_clock::now());
214 if (ShouldCancel()) return true;
215
216 set_intake_max_velocity(0.50);
217 set_intake_goal(0.18);
218 set_shooter_velocity(0.0);
219 set_indexer_angular_velocity(0.0);
220 SendSuperstructureGoal();
221
222 } break;
223
Austin Schuh19e15d72017-06-24 13:34:47 -0700224 default: {
225 // hopper auto
Austin Schuh10475d72017-04-16 19:17:48 -0700226 // Red is positive.
Austin Schuh624088a2017-03-22 22:36:16 -0700227 constexpr double kDriveDirection = 1.0;
Austin Schuh624088a2017-03-22 22:36:16 -0700228 set_intake_goal(0.07);
229 SendSuperstructureGoal();
Austin Schuh10475d72017-04-16 19:17:48 -0700230
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700231 StartDrive(-3.42, kDriveDirection * (M_PI / 10 - 0.057), kSlowDrive,
232 kFirstTurn);
Austin Schuh10475d72017-04-16 19:17:48 -0700233 if (!WaitForDriveNear(3.30, 0.0)) return true;
234 LOG(INFO, "Turn ended: %f left to go\n", DriveDistanceLeft());
235 // We can go to 2.50 before we hit the previous profile.
236
Austin Schuh19e15d72017-06-24 13:34:47 -0700237 if (!WaitForDriveNear(2.48, 0.0)) return true;
Austin Schuh10475d72017-04-16 19:17:48 -0700238 LOG(INFO, "%f left to go\n", DriveDistanceLeft());
Austin Schuh366f7ed2017-03-11 21:57:14 -0800239
Austin Schuh624088a2017-03-22 22:36:16 -0700240 set_intake_goal(0.23);
Austin Schuh76af51e2017-04-09 18:32:38 -0700241 set_turret_goal(0.0);
242 // Values good for blue:
243 // TODO(austin): Drive these off the auto switch.
Austin Schuh10475d72017-04-16 19:17:48 -0700244
245 set_hood_goal(0.37);
246 set_shooter_velocity(353.0);
Austin Schuh624088a2017-03-22 22:36:16 -0700247 SendSuperstructureGoal();
Austin Schuh366f7ed2017-03-11 21:57:14 -0800248
Austin Schuh10475d72017-04-16 19:17:48 -0700249 StartDrive(0.0, -M_PI / 8.0 * kDriveDirection, kSlowDrive, kSlowTurn);
250 if (!WaitForDriveNear(0.20, 0.0)) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800251
Austin Schuh10475d72017-04-16 19:17:48 -0700252 this_thread::sleep_for(chrono::milliseconds(300));
253
Austin Schuh19e15d72017-06-24 13:34:47 -0700254 // Turn to trigger the hopper.
Austin Schuh10475d72017-04-16 19:17:48 -0700255 StartDrive(0.0, (M_PI / 8.0 + 0.20) * kDriveDirection, kSlowDrive,
Austin Schuh624088a2017-03-22 22:36:16 -0700256 kSmashTurn);
257 if (!WaitForDriveNear(0.05, 0.2)) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800258
Austin Schuh624088a2017-03-22 22:36:16 -0700259 set_vision_track(true);
Austin Schuh10475d72017-04-16 19:17:48 -0700260 set_use_vision_for_shots(true);
Austin Schuh624088a2017-03-22 22:36:16 -0700261 SendSuperstructureGoal();
Austin Schuh366f7ed2017-03-11 21:57:14 -0800262
Austin Schuh19e15d72017-06-24 13:34:47 -0700263 // Now that everything is tracking, wait for the hood to zero before
264 // trying to shoot.
265 WaitForHoodZeroed();
266 if (ShouldCancel()) return true;
267
Austin Schuh624088a2017-03-22 22:36:16 -0700268 this_thread::sleep_for(chrono::milliseconds(200));
Austin Schuh366f7ed2017-03-11 21:57:14 -0800269
Austin Schuh19e15d72017-06-24 13:34:47 -0700270 // Turn back.
Austin Schuh624088a2017-03-22 22:36:16 -0700271 StartDrive(0.0, (-0.15) * kDriveDirection, kSlowDrive, kSlowTurn);
272 if (!WaitForDriveNear(0.05, 0.02)) return true;
Austin Schuh76af51e2017-04-09 18:32:38 -0700273
Austin Schuh19e15d72017-06-24 13:34:47 -0700274 set_indexer_angular_velocity(-2.1 * M_PI);
275 SendSuperstructureGoal();
276
Austin Schuh624088a2017-03-22 22:36:16 -0700277 LOG(INFO, "Started shooting at %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700278 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh366f7ed2017-03-11 21:57:14 -0800279
Austin Schuh624088a2017-03-22 22:36:16 -0700280 this_thread::sleep_for(start_time + chrono::seconds(9) -
281 monotonic_clock::now());
Austin Schuh624088a2017-03-22 22:36:16 -0700282 if (ShouldCancel()) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800283
Austin Schuh624088a2017-03-22 22:36:16 -0700284 set_intake_max_velocity(0.05);
285 set_intake_goal(0.08);
286 SendSuperstructureGoal();
Tyler Chatowf31da682017-01-22 01:39:40 +0000287
Austin Schuh624088a2017-03-22 22:36:16 -0700288 this_thread::sleep_for(start_time + chrono::seconds(15) -
289 monotonic_clock::now());
290 if (ShouldCancel()) return true;
291
292 set_shooter_velocity(0.0);
293 set_indexer_angular_velocity(0.0);
294 SendSuperstructureGoal();
295
296 } break;
Tyler Chatowf31da682017-01-22 01:39:40 +0000297 }
298
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700299 LOG(INFO, "Done %f\n",
300 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Tyler Chatowf31da682017-01-22 01:39:40 +0000301
302 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
303 ::std::chrono::milliseconds(5) / 2);
304
305 while (!ShouldCancel()) {
306 phased_loop.SleepUntilNext();
307 }
308 LOG(DEBUG, "Done running\n");
309
310 return true;
311}
312
Tyler Chatowf31da682017-01-22 01:39:40 +0000313} // namespace actors
314} // namespace y2017