blob: 841b18c5e01fad131998e829466270d122565024 [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
Austin Schuh1bf8a212019-05-26 22:13:14 -070038AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
Philipp Schrader996a2a22017-02-22 05:02:48 +000039 : frc971::autonomous::BaseAutonomousActor(
Austin Schuh1bf8a212019-05-26 22:13:14 -070040 event_loop, control_loops::drivetrain::GetDrivetrainConfig()) {}
Tyler Chatowf31da682017-01-22 01:39:40 +000041
Philipp Schrader996a2a22017-02-22 05:02:48 +000042bool AutonomousActor::RunAction(
43 const ::frc971::autonomous::AutonomousActionParams &params) {
Tyler Chatowf31da682017-01-22 01:39:40 +000044 monotonic_clock::time_point start_time = monotonic_clock::now();
45 LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode);
Austin Schuh624088a2017-03-22 22:36:16 -070046 Reset();
Tyler Chatowf31da682017-01-22 01:39:40 +000047
48 switch (params.mode) {
Austin Schuh19e15d72017-06-24 13:34:47 -070049 case 503: {
50 // Middle gear auto.
51 // Red is positive.
52 // Line up on boiler side edge.
53 constexpr double kDriveDirection = 1.0;
54
55 set_intake_goal(0.18);
56 set_turret_goal(0.0);
57 SendSuperstructureGoal();
58
59 set_turret_goal(-M_PI / 4.0 * kDriveDirection);
60 SendSuperstructureGoal();
61
62 // Turn towards the peg.
63 StartDrive(0.0, kDriveDirection * 0.162, kGearDrive, kSlowTurn);
64 if (!WaitForDriveNear(100.0, 0.02)) return true;
65 if (!WaitForTurnProfileDone()) return true;
66
67 // Drive, but get within 0.3 meters
68 StartDrive(1.73, 0.0, kGearFastDrive, kSlowTurn);
69 if (!WaitForDriveNear(0.3, 0.0)) return true;
70
71 // Now, add a slow, short move to actually place the gear.
72 StartDrive(0.18, 0.0, kGearPlaceDrive, kSecondGearTurn);
73 if (!WaitForDriveNear(0.07, 0.0)) return true;
74
75 set_gear_servo(0.3);
76 SendSuperstructureGoal();
77
78 // Slow down and then pause to let Chris pull the gear off.
79 this_thread::sleep_for(chrono::milliseconds(1000));
80
81 // Back up
82 StartDrive(-1.00, 0.0, kGearFastDrive, kSlowTurn);
83 if (!WaitForDriveNear(0.1, 0.1)) return true;
84
85 // Turn towards the boiler.
86 StartDrive(0.0, -kDriveDirection * M_PI / 2.0, kGearFastDrive, kSlowTurn);
87 if (!WaitForDriveNear(0.1, 0.1)) return true;
88
89 // Drive up near it.
90 StartDrive(1.8, 0.0, kGearFastDrive, kSlowTurn);
91 if (!WaitForDriveNear(0.1, 0.1)) return true;
92
93 set_hood_goal(0.37);
94 set_intake_goal(0.23);
95 set_shooter_velocity(353.0);
96 set_vision_track(true);
97 set_use_vision_for_shots(true);
98 set_indexer_angular_velocity(-1.1 * M_PI);
99 SendSuperstructureGoal();
100
101 this_thread::sleep_for(start_time + chrono::seconds(15) -
102 monotonic_clock::now());
103 if (ShouldCancel()) return true;
104
105 set_shooter_velocity(0.0);
106 set_indexer_angular_velocity(0.0);
107 SendSuperstructureGoal();
108
109 } break;
110
111 case 500: {
Austin Schuh10475d72017-04-16 19:17:48 -0700112 // Red is positive.
113 constexpr double kDriveDirection = -1.0;
114 // Side peg + hopper auto.
115
116 set_intake_goal(0.23);
117 set_turret_goal(-M_PI * kDriveDirection);
118 SendSuperstructureGoal();
119
120 constexpr double kLongPegDrive = 3.025;
121
122 StartDrive(kLongPegDrive, -kDriveDirection * M_PI / 4, kGearDrive,
123 kFirstGearStartTurn);
124 if (!WaitForDriveNear(100.0, M_PI / 8.0)) return true;
125 LOG(INFO, "Turn Middle: %f left to go\n", DriveDistanceLeft());
126
127 StartDrive(0.0, 0.0, kGearDrive, kFirstGearTurn);
128
129 if (!WaitForTurnProfileDone()) return true;
130 LOG(INFO, "Turn profile ended: %f left to go\n", DriveDistanceLeft());
131
132 set_hood_goal(0.43);
133 set_shooter_velocity(364.0);
134 SendSuperstructureGoal();
135
136 constexpr double kTurnDistanceFromStart = 1.08;
137 if (!WaitForDriveNear(kLongPegDrive - kTurnDistanceFromStart, 10.0)) {
138 return true;
139 }
140
141 StartDrive(0.0, kDriveDirection * (M_PI / 4 + 0.3), kGearSlowDrive,
142 kSecondGearTurn);
143 if (!WaitForTurnProfileDone()) return true;
144
145 StartDrive(0.0, 0.0, kGearFastDrive, kSecondGearTurn);
146
147 if (!WaitForDriveNear(0.3, 0.0)) return true;
148
149 set_vision_track(true);
150 SendSuperstructureGoal();
151
152 StartDrive(0.19, 0.0, kGearPlaceDrive, kSecondGearTurn);
153
154 if (!WaitForDriveNear(0.07, 0.0)) return true;
155 set_gear_servo(0.3);
156 SendSuperstructureGoal();
157
158 // Shoot from the peg.
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700159 // set_indexer_angular_velocity(-2.1 * M_PI);
160 // SendSuperstructureGoal();
161 // this_thread::sleep_for(chrono::milliseconds(1750));
Austin Schuh10475d72017-04-16 19:17:48 -0700162
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700163 // this_thread::sleep_for(chrono::milliseconds(500));
Austin Schuh10475d72017-04-16 19:17:48 -0700164 this_thread::sleep_for(chrono::milliseconds(750));
165 set_indexer_angular_velocity(0.0);
166 set_vision_track(false);
167 set_turret_goal(0.0);
168
169 set_hood_goal(0.37);
170 set_shooter_velocity(351.0);
171 set_intake_goal(0.18);
172 set_gear_servo(0.4);
173
174 SendSuperstructureGoal();
175 LOG(INFO, "Starting drive back %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700176 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh10475d72017-04-16 19:17:48 -0700177
Austin Schuh19e15d72017-06-24 13:34:47 -0700178 StartDrive(-2.75, kDriveDirection * 1.24, kSlowDrive,
Austin Schuh10475d72017-04-16 19:17:48 -0700179 kFirstGearStartTurn);
180 if (!WaitForDriveNear(2.4, 0.0)) return true;
181
182 if (!WaitForTurnProfileDone()) return true;
183 StartDrive(0.0, 0.0, kGearBallBackDrive, kFirstGearStartTurn);
184
185 if (!WaitForDriveNear(0.2, 0.0)) return true;
186 this_thread::sleep_for(chrono::milliseconds(200));
Austin Schuh19e15d72017-06-24 13:34:47 -0700187 // Trip the hopper
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700188 StartDrive(0.0, kDriveDirection * 1.10, kSlowDrive, kSmashTurn);
Austin Schuh10475d72017-04-16 19:17:48 -0700189
190 if (!WaitForDriveNear(0.2, 0.35)) return true;
191 set_vision_track(true);
192 set_use_vision_for_shots(true);
193 SendSuperstructureGoal();
194
195 if (!WaitForDriveNear(0.2, 0.2)) return true;
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700196 StartDrive(0.0, -kDriveDirection * 0.15, kSlowDrive, kSmashTurn);
Austin Schuh10475d72017-04-16 19:17:48 -0700197
198 LOG(INFO, "Starting second shot %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700199 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh10475d72017-04-16 19:17:48 -0700200 set_indexer_angular_velocity(-2.15 * M_PI);
201 SendSuperstructureGoal();
202 if (!WaitForDriveNear(0.2, 0.1)) return true;
203
204 this_thread::sleep_for(start_time + chrono::seconds(11) -
205 monotonic_clock::now());
206 if (ShouldCancel()) return true;
207 set_intake_max_velocity(0.05);
208 set_intake_goal(0.08);
209
210 this_thread::sleep_for(start_time + chrono::seconds(15) -
211 monotonic_clock::now());
212 if (ShouldCancel()) return true;
213
214 set_intake_max_velocity(0.50);
215 set_intake_goal(0.18);
216 set_shooter_velocity(0.0);
217 set_indexer_angular_velocity(0.0);
218 SendSuperstructureGoal();
219
220 } break;
221
Austin Schuh19e15d72017-06-24 13:34:47 -0700222 default: {
223 // hopper auto
Austin Schuh10475d72017-04-16 19:17:48 -0700224 // Red is positive.
Austin Schuh624088a2017-03-22 22:36:16 -0700225 constexpr double kDriveDirection = 1.0;
Austin Schuh624088a2017-03-22 22:36:16 -0700226 set_intake_goal(0.07);
227 SendSuperstructureGoal();
Austin Schuh10475d72017-04-16 19:17:48 -0700228
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700229 StartDrive(-3.42, kDriveDirection * (M_PI / 10 - 0.057), kSlowDrive,
230 kFirstTurn);
Austin Schuh10475d72017-04-16 19:17:48 -0700231 if (!WaitForDriveNear(3.30, 0.0)) return true;
232 LOG(INFO, "Turn ended: %f left to go\n", DriveDistanceLeft());
233 // We can go to 2.50 before we hit the previous profile.
234
Austin Schuh19e15d72017-06-24 13:34:47 -0700235 if (!WaitForDriveNear(2.48, 0.0)) return true;
Austin Schuh10475d72017-04-16 19:17:48 -0700236 LOG(INFO, "%f left to go\n", DriveDistanceLeft());
Austin Schuh366f7ed2017-03-11 21:57:14 -0800237
Austin Schuh624088a2017-03-22 22:36:16 -0700238 set_intake_goal(0.23);
Austin Schuh76af51e2017-04-09 18:32:38 -0700239 set_turret_goal(0.0);
240 // Values good for blue:
241 // TODO(austin): Drive these off the auto switch.
Austin Schuh10475d72017-04-16 19:17:48 -0700242
243 set_hood_goal(0.37);
244 set_shooter_velocity(353.0);
Austin Schuh624088a2017-03-22 22:36:16 -0700245 SendSuperstructureGoal();
Austin Schuh366f7ed2017-03-11 21:57:14 -0800246
Austin Schuh10475d72017-04-16 19:17:48 -0700247 StartDrive(0.0, -M_PI / 8.0 * kDriveDirection, kSlowDrive, kSlowTurn);
248 if (!WaitForDriveNear(0.20, 0.0)) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800249
Austin Schuh10475d72017-04-16 19:17:48 -0700250 this_thread::sleep_for(chrono::milliseconds(300));
251
Austin Schuh19e15d72017-06-24 13:34:47 -0700252 // Turn to trigger the hopper.
Austin Schuh10475d72017-04-16 19:17:48 -0700253 StartDrive(0.0, (M_PI / 8.0 + 0.20) * kDriveDirection, kSlowDrive,
Austin Schuh624088a2017-03-22 22:36:16 -0700254 kSmashTurn);
255 if (!WaitForDriveNear(0.05, 0.2)) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800256
Austin Schuh624088a2017-03-22 22:36:16 -0700257 set_vision_track(true);
Austin Schuh10475d72017-04-16 19:17:48 -0700258 set_use_vision_for_shots(true);
Austin Schuh624088a2017-03-22 22:36:16 -0700259 SendSuperstructureGoal();
Austin Schuh366f7ed2017-03-11 21:57:14 -0800260
Austin Schuh19e15d72017-06-24 13:34:47 -0700261 // Now that everything is tracking, wait for the hood to zero before
262 // trying to shoot.
263 WaitForHoodZeroed();
264 if (ShouldCancel()) return true;
265
Austin Schuh624088a2017-03-22 22:36:16 -0700266 this_thread::sleep_for(chrono::milliseconds(200));
Austin Schuh366f7ed2017-03-11 21:57:14 -0800267
Austin Schuh19e15d72017-06-24 13:34:47 -0700268 // Turn back.
Austin Schuh624088a2017-03-22 22:36:16 -0700269 StartDrive(0.0, (-0.15) * kDriveDirection, kSlowDrive, kSlowTurn);
270 if (!WaitForDriveNear(0.05, 0.02)) return true;
Austin Schuh76af51e2017-04-09 18:32:38 -0700271
Austin Schuh19e15d72017-06-24 13:34:47 -0700272 set_indexer_angular_velocity(-2.1 * M_PI);
273 SendSuperstructureGoal();
274
Austin Schuh624088a2017-03-22 22:36:16 -0700275 LOG(INFO, "Started shooting at %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700276 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh366f7ed2017-03-11 21:57:14 -0800277
Austin Schuh624088a2017-03-22 22:36:16 -0700278 this_thread::sleep_for(start_time + chrono::seconds(9) -
279 monotonic_clock::now());
Austin Schuh624088a2017-03-22 22:36:16 -0700280 if (ShouldCancel()) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800281
Austin Schuh624088a2017-03-22 22:36:16 -0700282 set_intake_max_velocity(0.05);
283 set_intake_goal(0.08);
284 SendSuperstructureGoal();
Tyler Chatowf31da682017-01-22 01:39:40 +0000285
Austin Schuh624088a2017-03-22 22:36:16 -0700286 this_thread::sleep_for(start_time + chrono::seconds(15) -
287 monotonic_clock::now());
288 if (ShouldCancel()) return true;
289
290 set_shooter_velocity(0.0);
291 set_indexer_angular_velocity(0.0);
292 SendSuperstructureGoal();
293
294 } break;
Tyler Chatowf31da682017-01-22 01:39:40 +0000295 }
296
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700297 LOG(INFO, "Done %f\n",
298 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Tyler Chatowf31da682017-01-22 01:39:40 +0000299
300 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
301 ::std::chrono::milliseconds(5) / 2);
302
303 while (!ShouldCancel()) {
304 phased_loop.SleepUntilNext();
305 }
306 LOG(DEBUG, "Done running\n");
307
308 return true;
309}
310
Tyler Chatowf31da682017-01-22 01:39:40 +0000311} // namespace actors
312} // namespace y2017