blob: 19aa329b1bffe7bda70f11df2a4c8e245204b068 [file] [log] [blame]
Tyler Chatowf31da682017-01-22 01:39:40 +00001#include "y2017/actors/autonomous_actor.h"
2
Tyler Chatowf31da682017-01-22 01:39:40 +00003#include <chrono>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07004#include <cinttypes>
Tyler Chatowf31da682017-01-22 01:39:40 +00005#include <cmath>
6
John Park33858a32018-09-28 23:05:48 -07007#include "aos/logging/logging.h"
James Kuszmaul651fc3f2019-05-15 21:14:25 -07008#include "aos/util/phased_loop.h"
Philipp Schrader996a2a22017-02-22 05:02:48 +00009#include "y2017/control_loops/drivetrain/drivetrain_base.h"
Tyler Chatowf31da682017-01-22 01:39:40 +000010
11namespace y2017 {
12namespace actors {
Tyler Chatowf31da682017-01-22 01:39:40 +000013using ::aos::monotonic_clock;
Alex Perrycb7da4b2019-08-28 19:35:56 -070014using ::frc971::ProfileParametersT;
Tyler Chatowf31da682017-01-22 01:39:40 +000015namespace chrono = ::std::chrono;
16namespace this_thread = ::std::this_thread;
17
18namespace {
Philipp Schrader85fca552017-03-05 00:30:50 +000019
Alex Perrycb7da4b2019-08-28 19:35:56 -070020ProfileParametersT MakeProfileParameters(float max_velocity,
21 float max_acceleration) {
22 ProfileParametersT result;
23 result.max_velocity = max_velocity;
24 result.max_acceleration = max_acceleration;
25 return result;
26}
27
28const ProfileParametersT kGearBallBackDrive = MakeProfileParameters(3.0, 3.5);
29const ProfileParametersT kGearDrive = MakeProfileParameters(1.5, 2.0);
30const ProfileParametersT kGearFastDrive = MakeProfileParameters(2.0, 2.5);
31const ProfileParametersT kGearSlowDrive = MakeProfileParameters(1.0, 2.0);
32const ProfileParametersT kGearPlaceDrive = MakeProfileParameters(0.40, 2.0);
33const ProfileParametersT kSlowDrive = MakeProfileParameters(3.0, 2.0);
34const ProfileParametersT kSlowTurn = MakeProfileParameters(3.0, 3.0);
35const ProfileParametersT kFirstTurn = MakeProfileParameters(1.0, 1.5);
36const ProfileParametersT kFirstGearStartTurn = MakeProfileParameters(2.0, 3.0);
37const ProfileParametersT kFirstGearTurn = MakeProfileParameters(2.0, 5.0);
38const ProfileParametersT kSecondGearTurn = MakeProfileParameters(3.0, 5.0);
39const ProfileParametersT kSmashTurn = MakeProfileParameters(1.5, 5.0);
Philipp Schrader85fca552017-03-05 00:30:50 +000040
Tyler Chatowf31da682017-01-22 01:39:40 +000041} // namespace
42
Austin Schuh1bf8a212019-05-26 22:13:14 -070043AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
Philipp Schrader996a2a22017-02-22 05:02:48 +000044 : frc971::autonomous::BaseAutonomousActor(
Austin Schuh402722c2019-06-29 21:27:06 -070045 event_loop, control_loops::drivetrain::GetDrivetrainConfig()),
46 superstructure_status_fetcher_(
Austin Schuh402722c2019-06-29 21:27:06 -070047 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -070048 ->MakeFetcher<::y2017::control_loops::superstructure::Status>(
49 "/superstructure")),
50 superstructure_goal_sender_(
51 event_loop->MakeSender<::y2017::control_loops::superstructure::Goal>(
52 "/superstructure")) {}
Tyler Chatowf31da682017-01-22 01:39:40 +000053
Philipp Schrader996a2a22017-02-22 05:02:48 +000054bool AutonomousActor::RunAction(
Alex Perrycb7da4b2019-08-28 19:35:56 -070055 const ::frc971::autonomous::AutonomousActionParams *params) {
Austin Schuh77ed5432019-07-07 20:41:36 -070056 const monotonic_clock::time_point start_time = monotonic_now();
Austin Schuhf257f3c2019-10-27 21:00:43 -070057 AOS_LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -070058 params->mode());
Austin Schuh624088a2017-03-22 22:36:16 -070059 Reset();
Tyler Chatowf31da682017-01-22 01:39:40 +000060
Alex Perrycb7da4b2019-08-28 19:35:56 -070061 switch (params->mode()) {
Austin Schuh19e15d72017-06-24 13:34:47 -070062 case 503: {
63 // Middle gear auto.
64 // Red is positive.
65 // Line up on boiler side edge.
66 constexpr double kDriveDirection = 1.0;
67
68 set_intake_goal(0.18);
69 set_turret_goal(0.0);
70 SendSuperstructureGoal();
71
72 set_turret_goal(-M_PI / 4.0 * kDriveDirection);
73 SendSuperstructureGoal();
74
75 // Turn towards the peg.
76 StartDrive(0.0, kDriveDirection * 0.162, kGearDrive, kSlowTurn);
77 if (!WaitForDriveNear(100.0, 0.02)) return true;
78 if (!WaitForTurnProfileDone()) return true;
79
80 // Drive, but get within 0.3 meters
81 StartDrive(1.73, 0.0, kGearFastDrive, kSlowTurn);
82 if (!WaitForDriveNear(0.3, 0.0)) return true;
83
84 // Now, add a slow, short move to actually place the gear.
85 StartDrive(0.18, 0.0, kGearPlaceDrive, kSecondGearTurn);
86 if (!WaitForDriveNear(0.07, 0.0)) return true;
87
88 set_gear_servo(0.3);
89 SendSuperstructureGoal();
90
91 // Slow down and then pause to let Chris pull the gear off.
92 this_thread::sleep_for(chrono::milliseconds(1000));
93
94 // Back up
95 StartDrive(-1.00, 0.0, kGearFastDrive, kSlowTurn);
96 if (!WaitForDriveNear(0.1, 0.1)) return true;
97
98 // Turn towards the boiler.
99 StartDrive(0.0, -kDriveDirection * M_PI / 2.0, kGearFastDrive, kSlowTurn);
100 if (!WaitForDriveNear(0.1, 0.1)) return true;
101
102 // Drive up near it.
103 StartDrive(1.8, 0.0, kGearFastDrive, kSlowTurn);
104 if (!WaitForDriveNear(0.1, 0.1)) return true;
105
106 set_hood_goal(0.37);
107 set_intake_goal(0.23);
108 set_shooter_velocity(353.0);
109 set_vision_track(true);
110 set_use_vision_for_shots(true);
111 set_indexer_angular_velocity(-1.1 * M_PI);
112 SendSuperstructureGoal();
113
114 this_thread::sleep_for(start_time + chrono::seconds(15) -
Austin Schuh77ed5432019-07-07 20:41:36 -0700115 monotonic_now());
Austin Schuh19e15d72017-06-24 13:34:47 -0700116 if (ShouldCancel()) return true;
117
118 set_shooter_velocity(0.0);
119 set_indexer_angular_velocity(0.0);
120 SendSuperstructureGoal();
121
122 } break;
123
124 case 500: {
Austin Schuh10475d72017-04-16 19:17:48 -0700125 // Red is positive.
126 constexpr double kDriveDirection = -1.0;
127 // Side peg + hopper auto.
128
129 set_intake_goal(0.23);
130 set_turret_goal(-M_PI * kDriveDirection);
131 SendSuperstructureGoal();
132
133 constexpr double kLongPegDrive = 3.025;
134
135 StartDrive(kLongPegDrive, -kDriveDirection * M_PI / 4, kGearDrive,
136 kFirstGearStartTurn);
137 if (!WaitForDriveNear(100.0, M_PI / 8.0)) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700138 AOS_LOG(INFO, "Turn Middle: %f left to go\n", DriveDistanceLeft());
Austin Schuh10475d72017-04-16 19:17:48 -0700139
140 StartDrive(0.0, 0.0, kGearDrive, kFirstGearTurn);
141
142 if (!WaitForTurnProfileDone()) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700143 AOS_LOG(INFO, "Turn profile ended: %f left to go\n", DriveDistanceLeft());
Austin Schuh10475d72017-04-16 19:17:48 -0700144
145 set_hood_goal(0.43);
146 set_shooter_velocity(364.0);
147 SendSuperstructureGoal();
148
149 constexpr double kTurnDistanceFromStart = 1.08;
150 if (!WaitForDriveNear(kLongPegDrive - kTurnDistanceFromStart, 10.0)) {
151 return true;
152 }
153
154 StartDrive(0.0, kDriveDirection * (M_PI / 4 + 0.3), kGearSlowDrive,
155 kSecondGearTurn);
156 if (!WaitForTurnProfileDone()) return true;
157
158 StartDrive(0.0, 0.0, kGearFastDrive, kSecondGearTurn);
159
160 if (!WaitForDriveNear(0.3, 0.0)) return true;
161
162 set_vision_track(true);
163 SendSuperstructureGoal();
164
165 StartDrive(0.19, 0.0, kGearPlaceDrive, kSecondGearTurn);
166
167 if (!WaitForDriveNear(0.07, 0.0)) return true;
168 set_gear_servo(0.3);
169 SendSuperstructureGoal();
170
171 // Shoot from the peg.
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700172 // set_indexer_angular_velocity(-2.1 * M_PI);
173 // SendSuperstructureGoal();
174 // this_thread::sleep_for(chrono::milliseconds(1750));
Austin Schuh10475d72017-04-16 19:17:48 -0700175
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700176 // this_thread::sleep_for(chrono::milliseconds(500));
Austin Schuh10475d72017-04-16 19:17:48 -0700177 this_thread::sleep_for(chrono::milliseconds(750));
178 set_indexer_angular_velocity(0.0);
179 set_vision_track(false);
180 set_turret_goal(0.0);
181
182 set_hood_goal(0.37);
183 set_shooter_velocity(351.0);
184 set_intake_goal(0.18);
185 set_gear_servo(0.4);
186
187 SendSuperstructureGoal();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700188 AOS_LOG(INFO, "Starting drive back %f\n",
189 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh10475d72017-04-16 19:17:48 -0700190
Austin Schuh19e15d72017-06-24 13:34:47 -0700191 StartDrive(-2.75, kDriveDirection * 1.24, kSlowDrive,
Austin Schuh10475d72017-04-16 19:17:48 -0700192 kFirstGearStartTurn);
193 if (!WaitForDriveNear(2.4, 0.0)) return true;
194
195 if (!WaitForTurnProfileDone()) return true;
196 StartDrive(0.0, 0.0, kGearBallBackDrive, kFirstGearStartTurn);
197
198 if (!WaitForDriveNear(0.2, 0.0)) return true;
199 this_thread::sleep_for(chrono::milliseconds(200));
Austin Schuh19e15d72017-06-24 13:34:47 -0700200 // Trip the hopper
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700201 StartDrive(0.0, kDriveDirection * 1.10, kSlowDrive, kSmashTurn);
Austin Schuh10475d72017-04-16 19:17:48 -0700202
203 if (!WaitForDriveNear(0.2, 0.35)) return true;
204 set_vision_track(true);
205 set_use_vision_for_shots(true);
206 SendSuperstructureGoal();
207
208 if (!WaitForDriveNear(0.2, 0.2)) return true;
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700209 StartDrive(0.0, -kDriveDirection * 0.15, kSlowDrive, kSmashTurn);
Austin Schuh10475d72017-04-16 19:17:48 -0700210
Austin Schuhf257f3c2019-10-27 21:00:43 -0700211 AOS_LOG(INFO, "Starting second shot %f\n",
212 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh10475d72017-04-16 19:17:48 -0700213 set_indexer_angular_velocity(-2.15 * M_PI);
214 SendSuperstructureGoal();
215 if (!WaitForDriveNear(0.2, 0.1)) return true;
216
217 this_thread::sleep_for(start_time + chrono::seconds(11) -
Austin Schuh77ed5432019-07-07 20:41:36 -0700218 monotonic_now());
Austin Schuh10475d72017-04-16 19:17:48 -0700219 if (ShouldCancel()) return true;
220 set_intake_max_velocity(0.05);
221 set_intake_goal(0.08);
222
223 this_thread::sleep_for(start_time + chrono::seconds(15) -
Austin Schuh77ed5432019-07-07 20:41:36 -0700224 monotonic_now());
Austin Schuh10475d72017-04-16 19:17:48 -0700225 if (ShouldCancel()) return true;
226
227 set_intake_max_velocity(0.50);
228 set_intake_goal(0.18);
229 set_shooter_velocity(0.0);
230 set_indexer_angular_velocity(0.0);
231 SendSuperstructureGoal();
232
233 } break;
234
Austin Schuh19e15d72017-06-24 13:34:47 -0700235 default: {
236 // hopper auto
Austin Schuh10475d72017-04-16 19:17:48 -0700237 // Red is positive.
Austin Schuh624088a2017-03-22 22:36:16 -0700238 constexpr double kDriveDirection = 1.0;
Austin Schuh624088a2017-03-22 22:36:16 -0700239 set_intake_goal(0.07);
240 SendSuperstructureGoal();
Austin Schuh10475d72017-04-16 19:17:48 -0700241
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700242 StartDrive(-3.42, kDriveDirection * (M_PI / 10 - 0.057), kSlowDrive,
243 kFirstTurn);
Austin Schuh10475d72017-04-16 19:17:48 -0700244 if (!WaitForDriveNear(3.30, 0.0)) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700245 AOS_LOG(INFO, "Turn ended: %f left to go\n", DriveDistanceLeft());
Austin Schuh10475d72017-04-16 19:17:48 -0700246 // We can go to 2.50 before we hit the previous profile.
247
Austin Schuh19e15d72017-06-24 13:34:47 -0700248 if (!WaitForDriveNear(2.48, 0.0)) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700249 AOS_LOG(INFO, "%f left to go\n", DriveDistanceLeft());
Austin Schuh366f7ed2017-03-11 21:57:14 -0800250
Austin Schuh624088a2017-03-22 22:36:16 -0700251 set_intake_goal(0.23);
Austin Schuh76af51e2017-04-09 18:32:38 -0700252 set_turret_goal(0.0);
253 // Values good for blue:
254 // TODO(austin): Drive these off the auto switch.
Austin Schuh10475d72017-04-16 19:17:48 -0700255
256 set_hood_goal(0.37);
257 set_shooter_velocity(353.0);
Austin Schuh624088a2017-03-22 22:36:16 -0700258 SendSuperstructureGoal();
Austin Schuh366f7ed2017-03-11 21:57:14 -0800259
Austin Schuh10475d72017-04-16 19:17:48 -0700260 StartDrive(0.0, -M_PI / 8.0 * kDriveDirection, kSlowDrive, kSlowTurn);
261 if (!WaitForDriveNear(0.20, 0.0)) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800262
Austin Schuh10475d72017-04-16 19:17:48 -0700263 this_thread::sleep_for(chrono::milliseconds(300));
264
Austin Schuh19e15d72017-06-24 13:34:47 -0700265 // Turn to trigger the hopper.
Austin Schuh10475d72017-04-16 19:17:48 -0700266 StartDrive(0.0, (M_PI / 8.0 + 0.20) * kDriveDirection, kSlowDrive,
Austin Schuh624088a2017-03-22 22:36:16 -0700267 kSmashTurn);
268 if (!WaitForDriveNear(0.05, 0.2)) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800269
Austin Schuh624088a2017-03-22 22:36:16 -0700270 set_vision_track(true);
Austin Schuh10475d72017-04-16 19:17:48 -0700271 set_use_vision_for_shots(true);
Austin Schuh624088a2017-03-22 22:36:16 -0700272 SendSuperstructureGoal();
Austin Schuh366f7ed2017-03-11 21:57:14 -0800273
Austin Schuh19e15d72017-06-24 13:34:47 -0700274 // Now that everything is tracking, wait for the hood to zero before
275 // trying to shoot.
276 WaitForHoodZeroed();
277 if (ShouldCancel()) return true;
278
Austin Schuh624088a2017-03-22 22:36:16 -0700279 this_thread::sleep_for(chrono::milliseconds(200));
Austin Schuh366f7ed2017-03-11 21:57:14 -0800280
Austin Schuh19e15d72017-06-24 13:34:47 -0700281 // Turn back.
Austin Schuh624088a2017-03-22 22:36:16 -0700282 StartDrive(0.0, (-0.15) * kDriveDirection, kSlowDrive, kSlowTurn);
283 if (!WaitForDriveNear(0.05, 0.02)) return true;
Austin Schuh76af51e2017-04-09 18:32:38 -0700284
Austin Schuh19e15d72017-06-24 13:34:47 -0700285 set_indexer_angular_velocity(-2.1 * M_PI);
286 SendSuperstructureGoal();
287
Austin Schuhf257f3c2019-10-27 21:00:43 -0700288 AOS_LOG(INFO, "Started shooting at %f\n",
289 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh366f7ed2017-03-11 21:57:14 -0800290
Austin Schuh77ed5432019-07-07 20:41:36 -0700291 this_thread::sleep_for(start_time + chrono::seconds(9) - monotonic_now());
Austin Schuh624088a2017-03-22 22:36:16 -0700292 if (ShouldCancel()) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800293
Austin Schuh624088a2017-03-22 22:36:16 -0700294 set_intake_max_velocity(0.05);
295 set_intake_goal(0.08);
296 SendSuperstructureGoal();
Tyler Chatowf31da682017-01-22 01:39:40 +0000297
Austin Schuh624088a2017-03-22 22:36:16 -0700298 this_thread::sleep_for(start_time + chrono::seconds(15) -
Austin Schuh77ed5432019-07-07 20:41:36 -0700299 monotonic_now());
Austin Schuh624088a2017-03-22 22:36:16 -0700300 if (ShouldCancel()) return true;
301
302 set_shooter_velocity(0.0);
303 set_indexer_angular_velocity(0.0);
304 SendSuperstructureGoal();
305
306 } break;
Tyler Chatowf31da682017-01-22 01:39:40 +0000307 }
308
Austin Schuhf257f3c2019-10-27 21:00:43 -0700309 AOS_LOG(INFO, "Done %f\n",
310 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Tyler Chatowf31da682017-01-22 01:39:40 +0000311
Yash Chainania6fe97b2021-12-15 21:01:11 -0800312 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
Austin Schuhd32b3622019-06-23 18:49:06 -0700313 event_loop()->monotonic_now(),
Yash Chainania6fe97b2021-12-15 21:01:11 -0800314 ActorBase::kLoopOffset);
Tyler Chatowf31da682017-01-22 01:39:40 +0000315
316 while (!ShouldCancel()) {
317 phased_loop.SleepUntilNext();
318 }
Austin Schuhf257f3c2019-10-27 21:00:43 -0700319 AOS_LOG(DEBUG, "Done running\n");
Tyler Chatowf31da682017-01-22 01:39:40 +0000320
321 return true;
322}
323
Tyler Chatowf31da682017-01-22 01:39:40 +0000324} // namespace actors
325} // namespace y2017