blob: 112478045505b3b8117bc7bfde0f8f29f637e4ff [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"
Philipp Schrader996a2a22017-02-22 05:02:48 +000010#include "y2017/control_loops/drivetrain/drivetrain_base.h"
Tyler Chatowf31da682017-01-22 01:39:40 +000011
12namespace y2017 {
13namespace actors {
Tyler Chatowf31da682017-01-22 01:39:40 +000014using ::aos::monotonic_clock;
Alex Perrycb7da4b2019-08-28 19:35:56 -070015using ::frc971::ProfileParametersT;
Tyler Chatowf31da682017-01-22 01:39:40 +000016namespace chrono = ::std::chrono;
17namespace this_thread = ::std::this_thread;
18
19namespace {
Philipp Schrader85fca552017-03-05 00:30:50 +000020
Alex Perrycb7da4b2019-08-28 19:35:56 -070021ProfileParametersT MakeProfileParameters(float max_velocity,
22 float max_acceleration) {
23 ProfileParametersT result;
24 result.max_velocity = max_velocity;
25 result.max_acceleration = max_acceleration;
26 return result;
27}
28
29const ProfileParametersT kGearBallBackDrive = MakeProfileParameters(3.0, 3.5);
30const ProfileParametersT kGearDrive = MakeProfileParameters(1.5, 2.0);
31const ProfileParametersT kGearFastDrive = MakeProfileParameters(2.0, 2.5);
32const ProfileParametersT kGearSlowDrive = MakeProfileParameters(1.0, 2.0);
33const ProfileParametersT kGearPlaceDrive = MakeProfileParameters(0.40, 2.0);
34const ProfileParametersT kSlowDrive = MakeProfileParameters(3.0, 2.0);
35const ProfileParametersT kSlowTurn = MakeProfileParameters(3.0, 3.0);
36const ProfileParametersT kFirstTurn = MakeProfileParameters(1.0, 1.5);
37const ProfileParametersT kFirstGearStartTurn = MakeProfileParameters(2.0, 3.0);
38const ProfileParametersT kFirstGearTurn = MakeProfileParameters(2.0, 5.0);
39const ProfileParametersT kSecondGearTurn = MakeProfileParameters(3.0, 5.0);
40const ProfileParametersT kSmashTurn = MakeProfileParameters(1.5, 5.0);
Philipp Schrader85fca552017-03-05 00:30:50 +000041
Tyler Chatowf31da682017-01-22 01:39:40 +000042} // namespace
43
Austin Schuh1bf8a212019-05-26 22:13:14 -070044AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
Philipp Schrader996a2a22017-02-22 05:02:48 +000045 : frc971::autonomous::BaseAutonomousActor(
Austin Schuh402722c2019-06-29 21:27:06 -070046 event_loop, control_loops::drivetrain::GetDrivetrainConfig()),
47 superstructure_status_fetcher_(
Austin Schuh402722c2019-06-29 21:27:06 -070048 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -070049 ->MakeFetcher<::y2017::control_loops::superstructure::Status>(
50 "/superstructure")),
51 superstructure_goal_sender_(
52 event_loop->MakeSender<::y2017::control_loops::superstructure::Goal>(
53 "/superstructure")) {}
Tyler Chatowf31da682017-01-22 01:39:40 +000054
Philipp Schrader996a2a22017-02-22 05:02:48 +000055bool AutonomousActor::RunAction(
Alex Perrycb7da4b2019-08-28 19:35:56 -070056 const ::frc971::autonomous::AutonomousActionParams *params) {
Austin Schuh77ed5432019-07-07 20:41:36 -070057 const monotonic_clock::time_point start_time = monotonic_now();
Austin Schuhf257f3c2019-10-27 21:00:43 -070058 AOS_LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -070059 params->mode());
Austin Schuh624088a2017-03-22 22:36:16 -070060 Reset();
Tyler Chatowf31da682017-01-22 01:39:40 +000061
Alex Perrycb7da4b2019-08-28 19:35:56 -070062 switch (params->mode()) {
Austin Schuh19e15d72017-06-24 13:34:47 -070063 case 503: {
64 // Middle gear auto.
65 // Red is positive.
66 // Line up on boiler side edge.
67 constexpr double kDriveDirection = 1.0;
68
69 set_intake_goal(0.18);
70 set_turret_goal(0.0);
71 SendSuperstructureGoal();
72
73 set_turret_goal(-M_PI / 4.0 * kDriveDirection);
74 SendSuperstructureGoal();
75
76 // Turn towards the peg.
77 StartDrive(0.0, kDriveDirection * 0.162, kGearDrive, kSlowTurn);
78 if (!WaitForDriveNear(100.0, 0.02)) return true;
79 if (!WaitForTurnProfileDone()) return true;
80
81 // Drive, but get within 0.3 meters
82 StartDrive(1.73, 0.0, kGearFastDrive, kSlowTurn);
83 if (!WaitForDriveNear(0.3, 0.0)) return true;
84
85 // Now, add a slow, short move to actually place the gear.
86 StartDrive(0.18, 0.0, kGearPlaceDrive, kSecondGearTurn);
87 if (!WaitForDriveNear(0.07, 0.0)) return true;
88
89 set_gear_servo(0.3);
90 SendSuperstructureGoal();
91
92 // Slow down and then pause to let Chris pull the gear off.
93 this_thread::sleep_for(chrono::milliseconds(1000));
94
95 // Back up
96 StartDrive(-1.00, 0.0, kGearFastDrive, kSlowTurn);
97 if (!WaitForDriveNear(0.1, 0.1)) return true;
98
99 // Turn towards the boiler.
100 StartDrive(0.0, -kDriveDirection * M_PI / 2.0, kGearFastDrive, kSlowTurn);
101 if (!WaitForDriveNear(0.1, 0.1)) return true;
102
103 // Drive up near it.
104 StartDrive(1.8, 0.0, kGearFastDrive, kSlowTurn);
105 if (!WaitForDriveNear(0.1, 0.1)) return true;
106
107 set_hood_goal(0.37);
108 set_intake_goal(0.23);
109 set_shooter_velocity(353.0);
110 set_vision_track(true);
111 set_use_vision_for_shots(true);
112 set_indexer_angular_velocity(-1.1 * M_PI);
113 SendSuperstructureGoal();
114
115 this_thread::sleep_for(start_time + chrono::seconds(15) -
Austin Schuh77ed5432019-07-07 20:41:36 -0700116 monotonic_now());
Austin Schuh19e15d72017-06-24 13:34:47 -0700117 if (ShouldCancel()) return true;
118
119 set_shooter_velocity(0.0);
120 set_indexer_angular_velocity(0.0);
121 SendSuperstructureGoal();
122
123 } break;
124
125 case 500: {
Austin Schuh10475d72017-04-16 19:17:48 -0700126 // Red is positive.
127 constexpr double kDriveDirection = -1.0;
128 // Side peg + hopper auto.
129
130 set_intake_goal(0.23);
131 set_turret_goal(-M_PI * kDriveDirection);
132 SendSuperstructureGoal();
133
134 constexpr double kLongPegDrive = 3.025;
135
136 StartDrive(kLongPegDrive, -kDriveDirection * M_PI / 4, kGearDrive,
137 kFirstGearStartTurn);
138 if (!WaitForDriveNear(100.0, M_PI / 8.0)) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700139 AOS_LOG(INFO, "Turn Middle: %f left to go\n", DriveDistanceLeft());
Austin Schuh10475d72017-04-16 19:17:48 -0700140
141 StartDrive(0.0, 0.0, kGearDrive, kFirstGearTurn);
142
143 if (!WaitForTurnProfileDone()) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700144 AOS_LOG(INFO, "Turn profile ended: %f left to go\n", DriveDistanceLeft());
Austin Schuh10475d72017-04-16 19:17:48 -0700145
146 set_hood_goal(0.43);
147 set_shooter_velocity(364.0);
148 SendSuperstructureGoal();
149
150 constexpr double kTurnDistanceFromStart = 1.08;
151 if (!WaitForDriveNear(kLongPegDrive - kTurnDistanceFromStart, 10.0)) {
152 return true;
153 }
154
155 StartDrive(0.0, kDriveDirection * (M_PI / 4 + 0.3), kGearSlowDrive,
156 kSecondGearTurn);
157 if (!WaitForTurnProfileDone()) return true;
158
159 StartDrive(0.0, 0.0, kGearFastDrive, kSecondGearTurn);
160
161 if (!WaitForDriveNear(0.3, 0.0)) return true;
162
163 set_vision_track(true);
164 SendSuperstructureGoal();
165
166 StartDrive(0.19, 0.0, kGearPlaceDrive, kSecondGearTurn);
167
168 if (!WaitForDriveNear(0.07, 0.0)) return true;
169 set_gear_servo(0.3);
170 SendSuperstructureGoal();
171
172 // Shoot from the peg.
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700173 // set_indexer_angular_velocity(-2.1 * M_PI);
174 // SendSuperstructureGoal();
175 // this_thread::sleep_for(chrono::milliseconds(1750));
Austin Schuh10475d72017-04-16 19:17:48 -0700176
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700177 // this_thread::sleep_for(chrono::milliseconds(500));
Austin Schuh10475d72017-04-16 19:17:48 -0700178 this_thread::sleep_for(chrono::milliseconds(750));
179 set_indexer_angular_velocity(0.0);
180 set_vision_track(false);
181 set_turret_goal(0.0);
182
183 set_hood_goal(0.37);
184 set_shooter_velocity(351.0);
185 set_intake_goal(0.18);
186 set_gear_servo(0.4);
187
188 SendSuperstructureGoal();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700189 AOS_LOG(INFO, "Starting drive back %f\n",
190 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh10475d72017-04-16 19:17:48 -0700191
Austin Schuh19e15d72017-06-24 13:34:47 -0700192 StartDrive(-2.75, kDriveDirection * 1.24, kSlowDrive,
Austin Schuh10475d72017-04-16 19:17:48 -0700193 kFirstGearStartTurn);
194 if (!WaitForDriveNear(2.4, 0.0)) return true;
195
196 if (!WaitForTurnProfileDone()) return true;
197 StartDrive(0.0, 0.0, kGearBallBackDrive, kFirstGearStartTurn);
198
199 if (!WaitForDriveNear(0.2, 0.0)) return true;
200 this_thread::sleep_for(chrono::milliseconds(200));
Austin Schuh19e15d72017-06-24 13:34:47 -0700201 // Trip the hopper
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700202 StartDrive(0.0, kDriveDirection * 1.10, kSlowDrive, kSmashTurn);
Austin Schuh10475d72017-04-16 19:17:48 -0700203
204 if (!WaitForDriveNear(0.2, 0.35)) return true;
205 set_vision_track(true);
206 set_use_vision_for_shots(true);
207 SendSuperstructureGoal();
208
209 if (!WaitForDriveNear(0.2, 0.2)) return true;
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700210 StartDrive(0.0, -kDriveDirection * 0.15, kSlowDrive, kSmashTurn);
Austin Schuh10475d72017-04-16 19:17:48 -0700211
Austin Schuhf257f3c2019-10-27 21:00:43 -0700212 AOS_LOG(INFO, "Starting second shot %f\n",
213 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh10475d72017-04-16 19:17:48 -0700214 set_indexer_angular_velocity(-2.15 * M_PI);
215 SendSuperstructureGoal();
216 if (!WaitForDriveNear(0.2, 0.1)) return true;
217
218 this_thread::sleep_for(start_time + chrono::seconds(11) -
Austin Schuh77ed5432019-07-07 20:41:36 -0700219 monotonic_now());
Austin Schuh10475d72017-04-16 19:17:48 -0700220 if (ShouldCancel()) return true;
221 set_intake_max_velocity(0.05);
222 set_intake_goal(0.08);
223
224 this_thread::sleep_for(start_time + chrono::seconds(15) -
Austin Schuh77ed5432019-07-07 20:41:36 -0700225 monotonic_now());
Austin Schuh10475d72017-04-16 19:17:48 -0700226 if (ShouldCancel()) return true;
227
228 set_intake_max_velocity(0.50);
229 set_intake_goal(0.18);
230 set_shooter_velocity(0.0);
231 set_indexer_angular_velocity(0.0);
232 SendSuperstructureGoal();
233
234 } break;
235
Austin Schuh19e15d72017-06-24 13:34:47 -0700236 default: {
237 // hopper auto
Austin Schuh10475d72017-04-16 19:17:48 -0700238 // Red is positive.
Austin Schuh624088a2017-03-22 22:36:16 -0700239 constexpr double kDriveDirection = 1.0;
Austin Schuh624088a2017-03-22 22:36:16 -0700240 set_intake_goal(0.07);
241 SendSuperstructureGoal();
Austin Schuh10475d72017-04-16 19:17:48 -0700242
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700243 StartDrive(-3.42, kDriveDirection * (M_PI / 10 - 0.057), kSlowDrive,
244 kFirstTurn);
Austin Schuh10475d72017-04-16 19:17:48 -0700245 if (!WaitForDriveNear(3.30, 0.0)) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700246 AOS_LOG(INFO, "Turn ended: %f left to go\n", DriveDistanceLeft());
Austin Schuh10475d72017-04-16 19:17:48 -0700247 // We can go to 2.50 before we hit the previous profile.
248
Austin Schuh19e15d72017-06-24 13:34:47 -0700249 if (!WaitForDriveNear(2.48, 0.0)) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700250 AOS_LOG(INFO, "%f left to go\n", DriveDistanceLeft());
Austin Schuh366f7ed2017-03-11 21:57:14 -0800251
Austin Schuh624088a2017-03-22 22:36:16 -0700252 set_intake_goal(0.23);
Austin Schuh76af51e2017-04-09 18:32:38 -0700253 set_turret_goal(0.0);
254 // Values good for blue:
255 // TODO(austin): Drive these off the auto switch.
Austin Schuh10475d72017-04-16 19:17:48 -0700256
257 set_hood_goal(0.37);
258 set_shooter_velocity(353.0);
Austin Schuh624088a2017-03-22 22:36:16 -0700259 SendSuperstructureGoal();
Austin Schuh366f7ed2017-03-11 21:57:14 -0800260
Austin Schuh10475d72017-04-16 19:17:48 -0700261 StartDrive(0.0, -M_PI / 8.0 * kDriveDirection, kSlowDrive, kSlowTurn);
262 if (!WaitForDriveNear(0.20, 0.0)) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800263
Austin Schuh10475d72017-04-16 19:17:48 -0700264 this_thread::sleep_for(chrono::milliseconds(300));
265
Austin Schuh19e15d72017-06-24 13:34:47 -0700266 // Turn to trigger the hopper.
Austin Schuh10475d72017-04-16 19:17:48 -0700267 StartDrive(0.0, (M_PI / 8.0 + 0.20) * kDriveDirection, kSlowDrive,
Austin Schuh624088a2017-03-22 22:36:16 -0700268 kSmashTurn);
269 if (!WaitForDriveNear(0.05, 0.2)) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800270
Austin Schuh624088a2017-03-22 22:36:16 -0700271 set_vision_track(true);
Austin Schuh10475d72017-04-16 19:17:48 -0700272 set_use_vision_for_shots(true);
Austin Schuh624088a2017-03-22 22:36:16 -0700273 SendSuperstructureGoal();
Austin Schuh366f7ed2017-03-11 21:57:14 -0800274
Austin Schuh19e15d72017-06-24 13:34:47 -0700275 // Now that everything is tracking, wait for the hood to zero before
276 // trying to shoot.
277 WaitForHoodZeroed();
278 if (ShouldCancel()) return true;
279
Austin Schuh624088a2017-03-22 22:36:16 -0700280 this_thread::sleep_for(chrono::milliseconds(200));
Austin Schuh366f7ed2017-03-11 21:57:14 -0800281
Austin Schuh19e15d72017-06-24 13:34:47 -0700282 // Turn back.
Austin Schuh624088a2017-03-22 22:36:16 -0700283 StartDrive(0.0, (-0.15) * kDriveDirection, kSlowDrive, kSlowTurn);
284 if (!WaitForDriveNear(0.05, 0.02)) return true;
Austin Schuh76af51e2017-04-09 18:32:38 -0700285
Austin Schuh19e15d72017-06-24 13:34:47 -0700286 set_indexer_angular_velocity(-2.1 * M_PI);
287 SendSuperstructureGoal();
288
Austin Schuhf257f3c2019-10-27 21:00:43 -0700289 AOS_LOG(INFO, "Started shooting at %f\n",
290 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh366f7ed2017-03-11 21:57:14 -0800291
Austin Schuh77ed5432019-07-07 20:41:36 -0700292 this_thread::sleep_for(start_time + chrono::seconds(9) - monotonic_now());
Austin Schuh624088a2017-03-22 22:36:16 -0700293 if (ShouldCancel()) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800294
Austin Schuh624088a2017-03-22 22:36:16 -0700295 set_intake_max_velocity(0.05);
296 set_intake_goal(0.08);
297 SendSuperstructureGoal();
Tyler Chatowf31da682017-01-22 01:39:40 +0000298
Austin Schuh624088a2017-03-22 22:36:16 -0700299 this_thread::sleep_for(start_time + chrono::seconds(15) -
Austin Schuh77ed5432019-07-07 20:41:36 -0700300 monotonic_now());
Austin Schuh624088a2017-03-22 22:36:16 -0700301 if (ShouldCancel()) return true;
302
303 set_shooter_velocity(0.0);
304 set_indexer_angular_velocity(0.0);
305 SendSuperstructureGoal();
306
307 } break;
Tyler Chatowf31da682017-01-22 01:39:40 +0000308 }
309
Austin Schuhf257f3c2019-10-27 21:00:43 -0700310 AOS_LOG(INFO, "Done %f\n",
311 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Tyler Chatowf31da682017-01-22 01:39:40 +0000312
313 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700314 event_loop()->monotonic_now(),
Tyler Chatowf31da682017-01-22 01:39:40 +0000315 ::std::chrono::milliseconds(5) / 2);
316
317 while (!ShouldCancel()) {
318 phased_loop.SleepUntilNext();
319 }
Austin Schuhf257f3c2019-10-27 21:00:43 -0700320 AOS_LOG(DEBUG, "Done running\n");
Tyler Chatowf31da682017-01-22 01:39:40 +0000321
322 return true;
323}
324
Tyler Chatowf31da682017-01-22 01:39:40 +0000325} // namespace actors
326} // namespace y2017