blob: 0f21cb6358750995e4abf0d3c9b668c25624bbdc [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
Stephan Pleinesf63bde82024-01-13 15:59:33 -080011namespace y2017::actors {
Tyler Chatowf31da682017-01-22 01:39:40 +000012using ::aos::monotonic_clock;
Alex Perrycb7da4b2019-08-28 19:35:56 -070013using ::frc971::ProfileParametersT;
Tyler Chatowf31da682017-01-22 01:39:40 +000014namespace chrono = ::std::chrono;
15namespace this_thread = ::std::this_thread;
16
17namespace {
Philipp Schrader85fca552017-03-05 00:30:50 +000018
Alex Perrycb7da4b2019-08-28 19:35:56 -070019ProfileParametersT MakeProfileParameters(float max_velocity,
20 float max_acceleration) {
21 ProfileParametersT result;
22 result.max_velocity = max_velocity;
23 result.max_acceleration = max_acceleration;
24 return result;
25}
26
27const ProfileParametersT kGearBallBackDrive = MakeProfileParameters(3.0, 3.5);
28const ProfileParametersT kGearDrive = MakeProfileParameters(1.5, 2.0);
29const ProfileParametersT kGearFastDrive = MakeProfileParameters(2.0, 2.5);
30const ProfileParametersT kGearSlowDrive = MakeProfileParameters(1.0, 2.0);
31const ProfileParametersT kGearPlaceDrive = MakeProfileParameters(0.40, 2.0);
32const ProfileParametersT kSlowDrive = MakeProfileParameters(3.0, 2.0);
33const ProfileParametersT kSlowTurn = MakeProfileParameters(3.0, 3.0);
34const ProfileParametersT kFirstTurn = MakeProfileParameters(1.0, 1.5);
35const ProfileParametersT kFirstGearStartTurn = MakeProfileParameters(2.0, 3.0);
36const ProfileParametersT kFirstGearTurn = MakeProfileParameters(2.0, 5.0);
37const ProfileParametersT kSecondGearTurn = MakeProfileParameters(3.0, 5.0);
38const ProfileParametersT kSmashTurn = MakeProfileParameters(1.5, 5.0);
Philipp Schrader85fca552017-03-05 00:30:50 +000039
Tyler Chatowf31da682017-01-22 01:39:40 +000040} // namespace
41
Austin Schuh1bf8a212019-05-26 22:13:14 -070042AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
Philipp Schrader996a2a22017-02-22 05:02:48 +000043 : frc971::autonomous::BaseAutonomousActor(
Austin Schuh402722c2019-06-29 21:27:06 -070044 event_loop, control_loops::drivetrain::GetDrivetrainConfig()),
45 superstructure_status_fetcher_(
Austin Schuh402722c2019-06-29 21:27:06 -070046 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -070047 ->MakeFetcher<::y2017::control_loops::superstructure::Status>(
48 "/superstructure")),
49 superstructure_goal_sender_(
50 event_loop->MakeSender<::y2017::control_loops::superstructure::Goal>(
51 "/superstructure")) {}
Tyler Chatowf31da682017-01-22 01:39:40 +000052
Philipp Schrader996a2a22017-02-22 05:02:48 +000053bool AutonomousActor::RunAction(
Alex Perrycb7da4b2019-08-28 19:35:56 -070054 const ::frc971::autonomous::AutonomousActionParams *params) {
Austin Schuh77ed5432019-07-07 20:41:36 -070055 const monotonic_clock::time_point start_time = monotonic_now();
Austin Schuhf257f3c2019-10-27 21:00:43 -070056 AOS_LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -070057 params->mode());
Austin Schuh624088a2017-03-22 22:36:16 -070058 Reset();
Tyler Chatowf31da682017-01-22 01:39:40 +000059
Alex Perrycb7da4b2019-08-28 19:35:56 -070060 switch (params->mode()) {
Austin Schuh19e15d72017-06-24 13:34:47 -070061 case 503: {
62 // Middle gear auto.
63 // Red is positive.
64 // Line up on boiler side edge.
65 constexpr double kDriveDirection = 1.0;
66
67 set_intake_goal(0.18);
68 set_turret_goal(0.0);
69 SendSuperstructureGoal();
70
71 set_turret_goal(-M_PI / 4.0 * kDriveDirection);
72 SendSuperstructureGoal();
73
74 // Turn towards the peg.
75 StartDrive(0.0, kDriveDirection * 0.162, kGearDrive, kSlowTurn);
76 if (!WaitForDriveNear(100.0, 0.02)) return true;
77 if (!WaitForTurnProfileDone()) return true;
78
79 // Drive, but get within 0.3 meters
80 StartDrive(1.73, 0.0, kGearFastDrive, kSlowTurn);
81 if (!WaitForDriveNear(0.3, 0.0)) return true;
82
83 // Now, add a slow, short move to actually place the gear.
84 StartDrive(0.18, 0.0, kGearPlaceDrive, kSecondGearTurn);
85 if (!WaitForDriveNear(0.07, 0.0)) return true;
86
87 set_gear_servo(0.3);
88 SendSuperstructureGoal();
89
90 // Slow down and then pause to let Chris pull the gear off.
91 this_thread::sleep_for(chrono::milliseconds(1000));
92
93 // Back up
94 StartDrive(-1.00, 0.0, kGearFastDrive, kSlowTurn);
95 if (!WaitForDriveNear(0.1, 0.1)) return true;
96
97 // Turn towards the boiler.
98 StartDrive(0.0, -kDriveDirection * M_PI / 2.0, kGearFastDrive, kSlowTurn);
99 if (!WaitForDriveNear(0.1, 0.1)) return true;
100
101 // Drive up near it.
102 StartDrive(1.8, 0.0, kGearFastDrive, kSlowTurn);
103 if (!WaitForDriveNear(0.1, 0.1)) return true;
104
105 set_hood_goal(0.37);
106 set_intake_goal(0.23);
107 set_shooter_velocity(353.0);
108 set_vision_track(true);
109 set_use_vision_for_shots(true);
110 set_indexer_angular_velocity(-1.1 * M_PI);
111 SendSuperstructureGoal();
112
113 this_thread::sleep_for(start_time + chrono::seconds(15) -
Austin Schuh77ed5432019-07-07 20:41:36 -0700114 monotonic_now());
Austin Schuh19e15d72017-06-24 13:34:47 -0700115 if (ShouldCancel()) return true;
116
117 set_shooter_velocity(0.0);
118 set_indexer_angular_velocity(0.0);
119 SendSuperstructureGoal();
120
121 } break;
122
123 case 500: {
Austin Schuh10475d72017-04-16 19:17:48 -0700124 // Red is positive.
125 constexpr double kDriveDirection = -1.0;
126 // Side peg + hopper auto.
127
128 set_intake_goal(0.23);
129 set_turret_goal(-M_PI * kDriveDirection);
130 SendSuperstructureGoal();
131
132 constexpr double kLongPegDrive = 3.025;
133
134 StartDrive(kLongPegDrive, -kDriveDirection * M_PI / 4, kGearDrive,
135 kFirstGearStartTurn);
136 if (!WaitForDriveNear(100.0, M_PI / 8.0)) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700137 AOS_LOG(INFO, "Turn Middle: %f left to go\n", DriveDistanceLeft());
Austin Schuh10475d72017-04-16 19:17:48 -0700138
139 StartDrive(0.0, 0.0, kGearDrive, kFirstGearTurn);
140
141 if (!WaitForTurnProfileDone()) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700142 AOS_LOG(INFO, "Turn profile ended: %f left to go\n", DriveDistanceLeft());
Austin Schuh10475d72017-04-16 19:17:48 -0700143
144 set_hood_goal(0.43);
145 set_shooter_velocity(364.0);
146 SendSuperstructureGoal();
147
148 constexpr double kTurnDistanceFromStart = 1.08;
149 if (!WaitForDriveNear(kLongPegDrive - kTurnDistanceFromStart, 10.0)) {
150 return true;
151 }
152
153 StartDrive(0.0, kDriveDirection * (M_PI / 4 + 0.3), kGearSlowDrive,
154 kSecondGearTurn);
155 if (!WaitForTurnProfileDone()) return true;
156
157 StartDrive(0.0, 0.0, kGearFastDrive, kSecondGearTurn);
158
159 if (!WaitForDriveNear(0.3, 0.0)) return true;
160
161 set_vision_track(true);
162 SendSuperstructureGoal();
163
164 StartDrive(0.19, 0.0, kGearPlaceDrive, kSecondGearTurn);
165
166 if (!WaitForDriveNear(0.07, 0.0)) return true;
167 set_gear_servo(0.3);
168 SendSuperstructureGoal();
169
170 // Shoot from the peg.
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700171 // set_indexer_angular_velocity(-2.1 * M_PI);
172 // SendSuperstructureGoal();
173 // this_thread::sleep_for(chrono::milliseconds(1750));
Austin Schuh10475d72017-04-16 19:17:48 -0700174
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700175 // this_thread::sleep_for(chrono::milliseconds(500));
Austin Schuh10475d72017-04-16 19:17:48 -0700176 this_thread::sleep_for(chrono::milliseconds(750));
177 set_indexer_angular_velocity(0.0);
178 set_vision_track(false);
179 set_turret_goal(0.0);
180
181 set_hood_goal(0.37);
182 set_shooter_velocity(351.0);
183 set_intake_goal(0.18);
184 set_gear_servo(0.4);
185
186 SendSuperstructureGoal();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700187 AOS_LOG(INFO, "Starting drive back %f\n",
188 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh10475d72017-04-16 19:17:48 -0700189
Austin Schuh19e15d72017-06-24 13:34:47 -0700190 StartDrive(-2.75, kDriveDirection * 1.24, kSlowDrive,
Austin Schuh10475d72017-04-16 19:17:48 -0700191 kFirstGearStartTurn);
192 if (!WaitForDriveNear(2.4, 0.0)) return true;
193
194 if (!WaitForTurnProfileDone()) return true;
195 StartDrive(0.0, 0.0, kGearBallBackDrive, kFirstGearStartTurn);
196
197 if (!WaitForDriveNear(0.2, 0.0)) return true;
198 this_thread::sleep_for(chrono::milliseconds(200));
Austin Schuh19e15d72017-06-24 13:34:47 -0700199 // Trip the hopper
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700200 StartDrive(0.0, kDriveDirection * 1.10, kSlowDrive, kSmashTurn);
Austin Schuh10475d72017-04-16 19:17:48 -0700201
202 if (!WaitForDriveNear(0.2, 0.35)) return true;
203 set_vision_track(true);
204 set_use_vision_for_shots(true);
205 SendSuperstructureGoal();
206
207 if (!WaitForDriveNear(0.2, 0.2)) return true;
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700208 StartDrive(0.0, -kDriveDirection * 0.15, kSlowDrive, kSmashTurn);
Austin Schuh10475d72017-04-16 19:17:48 -0700209
Austin Schuhf257f3c2019-10-27 21:00:43 -0700210 AOS_LOG(INFO, "Starting second shot %f\n",
211 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh10475d72017-04-16 19:17:48 -0700212 set_indexer_angular_velocity(-2.15 * M_PI);
213 SendSuperstructureGoal();
214 if (!WaitForDriveNear(0.2, 0.1)) return true;
215
216 this_thread::sleep_for(start_time + chrono::seconds(11) -
Austin Schuh77ed5432019-07-07 20:41:36 -0700217 monotonic_now());
Austin Schuh10475d72017-04-16 19:17:48 -0700218 if (ShouldCancel()) return true;
219 set_intake_max_velocity(0.05);
220 set_intake_goal(0.08);
221
222 this_thread::sleep_for(start_time + chrono::seconds(15) -
Austin Schuh77ed5432019-07-07 20:41:36 -0700223 monotonic_now());
Austin Schuh10475d72017-04-16 19:17:48 -0700224 if (ShouldCancel()) return true;
225
226 set_intake_max_velocity(0.50);
227 set_intake_goal(0.18);
228 set_shooter_velocity(0.0);
229 set_indexer_angular_velocity(0.0);
230 SendSuperstructureGoal();
231
232 } break;
233
Austin Schuh19e15d72017-06-24 13:34:47 -0700234 default: {
235 // hopper auto
Austin Schuh10475d72017-04-16 19:17:48 -0700236 // Red is positive.
Austin Schuh624088a2017-03-22 22:36:16 -0700237 constexpr double kDriveDirection = 1.0;
Austin Schuh624088a2017-03-22 22:36:16 -0700238 set_intake_goal(0.07);
239 SendSuperstructureGoal();
Austin Schuh10475d72017-04-16 19:17:48 -0700240
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700241 StartDrive(-3.42, kDriveDirection * (M_PI / 10 - 0.057), kSlowDrive,
242 kFirstTurn);
Austin Schuh10475d72017-04-16 19:17:48 -0700243 if (!WaitForDriveNear(3.30, 0.0)) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700244 AOS_LOG(INFO, "Turn ended: %f left to go\n", DriveDistanceLeft());
Austin Schuh10475d72017-04-16 19:17:48 -0700245 // We can go to 2.50 before we hit the previous profile.
246
Austin Schuh19e15d72017-06-24 13:34:47 -0700247 if (!WaitForDriveNear(2.48, 0.0)) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700248 AOS_LOG(INFO, "%f left to go\n", DriveDistanceLeft());
Austin Schuh366f7ed2017-03-11 21:57:14 -0800249
Austin Schuh624088a2017-03-22 22:36:16 -0700250 set_intake_goal(0.23);
Austin Schuh76af51e2017-04-09 18:32:38 -0700251 set_turret_goal(0.0);
252 // Values good for blue:
253 // TODO(austin): Drive these off the auto switch.
Austin Schuh10475d72017-04-16 19:17:48 -0700254
255 set_hood_goal(0.37);
256 set_shooter_velocity(353.0);
Austin Schuh624088a2017-03-22 22:36:16 -0700257 SendSuperstructureGoal();
Austin Schuh366f7ed2017-03-11 21:57:14 -0800258
Austin Schuh10475d72017-04-16 19:17:48 -0700259 StartDrive(0.0, -M_PI / 8.0 * kDriveDirection, kSlowDrive, kSlowTurn);
260 if (!WaitForDriveNear(0.20, 0.0)) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800261
Austin Schuh10475d72017-04-16 19:17:48 -0700262 this_thread::sleep_for(chrono::milliseconds(300));
263
Austin Schuh19e15d72017-06-24 13:34:47 -0700264 // Turn to trigger the hopper.
Austin Schuh10475d72017-04-16 19:17:48 -0700265 StartDrive(0.0, (M_PI / 8.0 + 0.20) * kDriveDirection, kSlowDrive,
Austin Schuh624088a2017-03-22 22:36:16 -0700266 kSmashTurn);
267 if (!WaitForDriveNear(0.05, 0.2)) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800268
Austin Schuh624088a2017-03-22 22:36:16 -0700269 set_vision_track(true);
Austin Schuh10475d72017-04-16 19:17:48 -0700270 set_use_vision_for_shots(true);
Austin Schuh624088a2017-03-22 22:36:16 -0700271 SendSuperstructureGoal();
Austin Schuh366f7ed2017-03-11 21:57:14 -0800272
Austin Schuh19e15d72017-06-24 13:34:47 -0700273 // Now that everything is tracking, wait for the hood to zero before
274 // trying to shoot.
275 WaitForHoodZeroed();
276 if (ShouldCancel()) return true;
277
Austin Schuh624088a2017-03-22 22:36:16 -0700278 this_thread::sleep_for(chrono::milliseconds(200));
Austin Schuh366f7ed2017-03-11 21:57:14 -0800279
Austin Schuh19e15d72017-06-24 13:34:47 -0700280 // Turn back.
Austin Schuh624088a2017-03-22 22:36:16 -0700281 StartDrive(0.0, (-0.15) * kDriveDirection, kSlowDrive, kSlowTurn);
282 if (!WaitForDriveNear(0.05, 0.02)) return true;
Austin Schuh76af51e2017-04-09 18:32:38 -0700283
Austin Schuh19e15d72017-06-24 13:34:47 -0700284 set_indexer_angular_velocity(-2.1 * M_PI);
285 SendSuperstructureGoal();
286
Austin Schuhf257f3c2019-10-27 21:00:43 -0700287 AOS_LOG(INFO, "Started shooting at %f\n",
288 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh366f7ed2017-03-11 21:57:14 -0800289
Austin Schuh77ed5432019-07-07 20:41:36 -0700290 this_thread::sleep_for(start_time + chrono::seconds(9) - monotonic_now());
Austin Schuh624088a2017-03-22 22:36:16 -0700291 if (ShouldCancel()) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800292
Austin Schuh624088a2017-03-22 22:36:16 -0700293 set_intake_max_velocity(0.05);
294 set_intake_goal(0.08);
295 SendSuperstructureGoal();
Tyler Chatowf31da682017-01-22 01:39:40 +0000296
Austin Schuh624088a2017-03-22 22:36:16 -0700297 this_thread::sleep_for(start_time + chrono::seconds(15) -
Austin Schuh77ed5432019-07-07 20:41:36 -0700298 monotonic_now());
Austin Schuh624088a2017-03-22 22:36:16 -0700299 if (ShouldCancel()) return true;
300
301 set_shooter_velocity(0.0);
302 set_indexer_angular_velocity(0.0);
303 SendSuperstructureGoal();
304
305 } break;
Tyler Chatowf31da682017-01-22 01:39:40 +0000306 }
307
Austin Schuhf257f3c2019-10-27 21:00:43 -0700308 AOS_LOG(INFO, "Done %f\n",
309 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Tyler Chatowf31da682017-01-22 01:39:40 +0000310
Yash Chainania6fe97b2021-12-15 21:01:11 -0800311 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
Austin Schuhd32b3622019-06-23 18:49:06 -0700312 event_loop()->monotonic_now(),
Stephan Pleines743f83a2024-02-02 18:32:09 -0800313 aos::common::actions::kLoopOffset);
Tyler Chatowf31da682017-01-22 01:39:40 +0000314
315 while (!ShouldCancel()) {
316 phased_loop.SleepUntilNext();
317 }
Austin Schuhf257f3c2019-10-27 21:00:43 -0700318 AOS_LOG(DEBUG, "Done running\n");
Tyler Chatowf31da682017-01-22 01:39:40 +0000319
320 return true;
321}
322
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800323} // namespace y2017::actors