blob: 33c29a897b7e9633bc7b3690459d4ffd0414d695 [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;
17namespace chrono = ::std::chrono;
18namespace this_thread = ::std::this_thread;
19
20namespace {
Philipp Schrader85fca552017-03-05 00:30:50 +000021
Austin Schuh10475d72017-04-16 19:17:48 -070022const ProfileParameters kGearBallBackDrive = {3.0, 3.5};
23const ProfileParameters kGearDrive = {1.5, 2.0};
24const ProfileParameters kGearFastDrive = {2.0, 2.5};
25const ProfileParameters kGearSlowDrive = {1.0, 2.0};
26const ProfileParameters kGearPlaceDrive = {0.40, 2.0};
Austin Schuh624088a2017-03-22 22:36:16 -070027const ProfileParameters kSlowDrive = {3.0, 2.0};
Austin Schuh366f7ed2017-03-11 21:57:14 -080028const ProfileParameters kSlowTurn = {3.0, 3.0};
Austin Schuh10475d72017-04-16 19:17:48 -070029const ProfileParameters kFirstTurn = {1.0, 1.5};
30const ProfileParameters kFirstGearStartTurn = {2.0, 3.0};
31const ProfileParameters kFirstGearTurn = {2.0, 5.0};
32const ProfileParameters kSecondGearTurn = {3.0, 5.0};
Austin Schuh19e15d72017-06-24 13:34:47 -070033const ProfileParameters kSmashTurn = {1.5, 5.0};
Philipp Schrader85fca552017-03-05 00:30:50 +000034
Tyler Chatowf31da682017-01-22 01:39:40 +000035} // namespace
36
Austin Schuh1bf8a212019-05-26 22:13:14 -070037AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
Philipp Schrader996a2a22017-02-22 05:02:48 +000038 : frc971::autonomous::BaseAutonomousActor(
Austin Schuh402722c2019-06-29 21:27:06 -070039 event_loop, control_loops::drivetrain::GetDrivetrainConfig()),
40 superstructure_status_fetcher_(
41 event_loop->MakeFetcher<
42 ::y2017::control_loops::SuperstructureQueue::Status>(
43 ".y2017.control_loops.superstructure_queue.status")),
44 superstructure_goal_sender_(
45 event_loop
46 ->MakeSender<::y2017::control_loops::SuperstructureQueue::Goal>(
47 ".y2017.control_loops.superstructure_queue.goal")) {}
Tyler Chatowf31da682017-01-22 01:39:40 +000048
Philipp Schrader996a2a22017-02-22 05:02:48 +000049bool AutonomousActor::RunAction(
50 const ::frc971::autonomous::AutonomousActionParams &params) {
Tyler Chatowf31da682017-01-22 01:39:40 +000051 monotonic_clock::time_point start_time = monotonic_clock::now();
52 LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n", params.mode);
Austin Schuh624088a2017-03-22 22:36:16 -070053 Reset();
Tyler Chatowf31da682017-01-22 01:39:40 +000054
55 switch (params.mode) {
Austin Schuh19e15d72017-06-24 13:34:47 -070056 case 503: {
57 // Middle gear auto.
58 // Red is positive.
59 // Line up on boiler side edge.
60 constexpr double kDriveDirection = 1.0;
61
62 set_intake_goal(0.18);
63 set_turret_goal(0.0);
64 SendSuperstructureGoal();
65
66 set_turret_goal(-M_PI / 4.0 * kDriveDirection);
67 SendSuperstructureGoal();
68
69 // Turn towards the peg.
70 StartDrive(0.0, kDriveDirection * 0.162, kGearDrive, kSlowTurn);
71 if (!WaitForDriveNear(100.0, 0.02)) return true;
72 if (!WaitForTurnProfileDone()) return true;
73
74 // Drive, but get within 0.3 meters
75 StartDrive(1.73, 0.0, kGearFastDrive, kSlowTurn);
76 if (!WaitForDriveNear(0.3, 0.0)) return true;
77
78 // Now, add a slow, short move to actually place the gear.
79 StartDrive(0.18, 0.0, kGearPlaceDrive, kSecondGearTurn);
80 if (!WaitForDriveNear(0.07, 0.0)) return true;
81
82 set_gear_servo(0.3);
83 SendSuperstructureGoal();
84
85 // Slow down and then pause to let Chris pull the gear off.
86 this_thread::sleep_for(chrono::milliseconds(1000));
87
88 // Back up
89 StartDrive(-1.00, 0.0, kGearFastDrive, kSlowTurn);
90 if (!WaitForDriveNear(0.1, 0.1)) return true;
91
92 // Turn towards the boiler.
93 StartDrive(0.0, -kDriveDirection * M_PI / 2.0, kGearFastDrive, kSlowTurn);
94 if (!WaitForDriveNear(0.1, 0.1)) return true;
95
96 // Drive up near it.
97 StartDrive(1.8, 0.0, kGearFastDrive, kSlowTurn);
98 if (!WaitForDriveNear(0.1, 0.1)) return true;
99
100 set_hood_goal(0.37);
101 set_intake_goal(0.23);
102 set_shooter_velocity(353.0);
103 set_vision_track(true);
104 set_use_vision_for_shots(true);
105 set_indexer_angular_velocity(-1.1 * M_PI);
106 SendSuperstructureGoal();
107
108 this_thread::sleep_for(start_time + chrono::seconds(15) -
109 monotonic_clock::now());
110 if (ShouldCancel()) return true;
111
112 set_shooter_velocity(0.0);
113 set_indexer_angular_velocity(0.0);
114 SendSuperstructureGoal();
115
116 } break;
117
118 case 500: {
Austin Schuh10475d72017-04-16 19:17:48 -0700119 // Red is positive.
120 constexpr double kDriveDirection = -1.0;
121 // Side peg + hopper auto.
122
123 set_intake_goal(0.23);
124 set_turret_goal(-M_PI * kDriveDirection);
125 SendSuperstructureGoal();
126
127 constexpr double kLongPegDrive = 3.025;
128
129 StartDrive(kLongPegDrive, -kDriveDirection * M_PI / 4, kGearDrive,
130 kFirstGearStartTurn);
131 if (!WaitForDriveNear(100.0, M_PI / 8.0)) return true;
132 LOG(INFO, "Turn Middle: %f left to go\n", DriveDistanceLeft());
133
134 StartDrive(0.0, 0.0, kGearDrive, kFirstGearTurn);
135
136 if (!WaitForTurnProfileDone()) return true;
137 LOG(INFO, "Turn profile ended: %f left to go\n", DriveDistanceLeft());
138
139 set_hood_goal(0.43);
140 set_shooter_velocity(364.0);
141 SendSuperstructureGoal();
142
143 constexpr double kTurnDistanceFromStart = 1.08;
144 if (!WaitForDriveNear(kLongPegDrive - kTurnDistanceFromStart, 10.0)) {
145 return true;
146 }
147
148 StartDrive(0.0, kDriveDirection * (M_PI / 4 + 0.3), kGearSlowDrive,
149 kSecondGearTurn);
150 if (!WaitForTurnProfileDone()) return true;
151
152 StartDrive(0.0, 0.0, kGearFastDrive, kSecondGearTurn);
153
154 if (!WaitForDriveNear(0.3, 0.0)) return true;
155
156 set_vision_track(true);
157 SendSuperstructureGoal();
158
159 StartDrive(0.19, 0.0, kGearPlaceDrive, kSecondGearTurn);
160
161 if (!WaitForDriveNear(0.07, 0.0)) return true;
162 set_gear_servo(0.3);
163 SendSuperstructureGoal();
164
165 // Shoot from the peg.
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700166 // set_indexer_angular_velocity(-2.1 * M_PI);
167 // SendSuperstructureGoal();
168 // this_thread::sleep_for(chrono::milliseconds(1750));
Austin Schuh10475d72017-04-16 19:17:48 -0700169
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700170 // this_thread::sleep_for(chrono::milliseconds(500));
Austin Schuh10475d72017-04-16 19:17:48 -0700171 this_thread::sleep_for(chrono::milliseconds(750));
172 set_indexer_angular_velocity(0.0);
173 set_vision_track(false);
174 set_turret_goal(0.0);
175
176 set_hood_goal(0.37);
177 set_shooter_velocity(351.0);
178 set_intake_goal(0.18);
179 set_gear_servo(0.4);
180
181 SendSuperstructureGoal();
182 LOG(INFO, "Starting drive back %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700183 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh10475d72017-04-16 19:17:48 -0700184
Austin Schuh19e15d72017-06-24 13:34:47 -0700185 StartDrive(-2.75, kDriveDirection * 1.24, kSlowDrive,
Austin Schuh10475d72017-04-16 19:17:48 -0700186 kFirstGearStartTurn);
187 if (!WaitForDriveNear(2.4, 0.0)) return true;
188
189 if (!WaitForTurnProfileDone()) return true;
190 StartDrive(0.0, 0.0, kGearBallBackDrive, kFirstGearStartTurn);
191
192 if (!WaitForDriveNear(0.2, 0.0)) return true;
193 this_thread::sleep_for(chrono::milliseconds(200));
Austin Schuh19e15d72017-06-24 13:34:47 -0700194 // Trip the hopper
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700195 StartDrive(0.0, kDriveDirection * 1.10, kSlowDrive, kSmashTurn);
Austin Schuh10475d72017-04-16 19:17:48 -0700196
197 if (!WaitForDriveNear(0.2, 0.35)) return true;
198 set_vision_track(true);
199 set_use_vision_for_shots(true);
200 SendSuperstructureGoal();
201
202 if (!WaitForDriveNear(0.2, 0.2)) return true;
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700203 StartDrive(0.0, -kDriveDirection * 0.15, kSlowDrive, kSmashTurn);
Austin Schuh10475d72017-04-16 19:17:48 -0700204
205 LOG(INFO, "Starting second shot %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700206 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh10475d72017-04-16 19:17:48 -0700207 set_indexer_angular_velocity(-2.15 * M_PI);
208 SendSuperstructureGoal();
209 if (!WaitForDriveNear(0.2, 0.1)) return true;
210
211 this_thread::sleep_for(start_time + chrono::seconds(11) -
212 monotonic_clock::now());
213 if (ShouldCancel()) return true;
214 set_intake_max_velocity(0.05);
215 set_intake_goal(0.08);
216
217 this_thread::sleep_for(start_time + chrono::seconds(15) -
218 monotonic_clock::now());
219 if (ShouldCancel()) return true;
220
221 set_intake_max_velocity(0.50);
222 set_intake_goal(0.18);
223 set_shooter_velocity(0.0);
224 set_indexer_angular_velocity(0.0);
225 SendSuperstructureGoal();
226
227 } break;
228
Austin Schuh19e15d72017-06-24 13:34:47 -0700229 default: {
230 // hopper auto
Austin Schuh10475d72017-04-16 19:17:48 -0700231 // Red is positive.
Austin Schuh624088a2017-03-22 22:36:16 -0700232 constexpr double kDriveDirection = 1.0;
Austin Schuh624088a2017-03-22 22:36:16 -0700233 set_intake_goal(0.07);
234 SendSuperstructureGoal();
Austin Schuh10475d72017-04-16 19:17:48 -0700235
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700236 StartDrive(-3.42, kDriveDirection * (M_PI / 10 - 0.057), kSlowDrive,
237 kFirstTurn);
Austin Schuh10475d72017-04-16 19:17:48 -0700238 if (!WaitForDriveNear(3.30, 0.0)) return true;
239 LOG(INFO, "Turn ended: %f left to go\n", DriveDistanceLeft());
240 // We can go to 2.50 before we hit the previous profile.
241
Austin Schuh19e15d72017-06-24 13:34:47 -0700242 if (!WaitForDriveNear(2.48, 0.0)) return true;
Austin Schuh10475d72017-04-16 19:17:48 -0700243 LOG(INFO, "%f left to go\n", DriveDistanceLeft());
Austin Schuh366f7ed2017-03-11 21:57:14 -0800244
Austin Schuh624088a2017-03-22 22:36:16 -0700245 set_intake_goal(0.23);
Austin Schuh76af51e2017-04-09 18:32:38 -0700246 set_turret_goal(0.0);
247 // Values good for blue:
248 // TODO(austin): Drive these off the auto switch.
Austin Schuh10475d72017-04-16 19:17:48 -0700249
250 set_hood_goal(0.37);
251 set_shooter_velocity(353.0);
Austin Schuh624088a2017-03-22 22:36:16 -0700252 SendSuperstructureGoal();
Austin Schuh366f7ed2017-03-11 21:57:14 -0800253
Austin Schuh10475d72017-04-16 19:17:48 -0700254 StartDrive(0.0, -M_PI / 8.0 * kDriveDirection, kSlowDrive, kSlowTurn);
255 if (!WaitForDriveNear(0.20, 0.0)) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800256
Austin Schuh10475d72017-04-16 19:17:48 -0700257 this_thread::sleep_for(chrono::milliseconds(300));
258
Austin Schuh19e15d72017-06-24 13:34:47 -0700259 // Turn to trigger the hopper.
Austin Schuh10475d72017-04-16 19:17:48 -0700260 StartDrive(0.0, (M_PI / 8.0 + 0.20) * kDriveDirection, kSlowDrive,
Austin Schuh624088a2017-03-22 22:36:16 -0700261 kSmashTurn);
262 if (!WaitForDriveNear(0.05, 0.2)) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800263
Austin Schuh624088a2017-03-22 22:36:16 -0700264 set_vision_track(true);
Austin Schuh10475d72017-04-16 19:17:48 -0700265 set_use_vision_for_shots(true);
Austin Schuh624088a2017-03-22 22:36:16 -0700266 SendSuperstructureGoal();
Austin Schuh366f7ed2017-03-11 21:57:14 -0800267
Austin Schuh19e15d72017-06-24 13:34:47 -0700268 // Now that everything is tracking, wait for the hood to zero before
269 // trying to shoot.
270 WaitForHoodZeroed();
271 if (ShouldCancel()) return true;
272
Austin Schuh624088a2017-03-22 22:36:16 -0700273 this_thread::sleep_for(chrono::milliseconds(200));
Austin Schuh366f7ed2017-03-11 21:57:14 -0800274
Austin Schuh19e15d72017-06-24 13:34:47 -0700275 // Turn back.
Austin Schuh624088a2017-03-22 22:36:16 -0700276 StartDrive(0.0, (-0.15) * kDriveDirection, kSlowDrive, kSlowTurn);
277 if (!WaitForDriveNear(0.05, 0.02)) return true;
Austin Schuh76af51e2017-04-09 18:32:38 -0700278
Austin Schuh19e15d72017-06-24 13:34:47 -0700279 set_indexer_angular_velocity(-2.1 * M_PI);
280 SendSuperstructureGoal();
281
Austin Schuh624088a2017-03-22 22:36:16 -0700282 LOG(INFO, "Started shooting at %f\n",
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700283 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Austin Schuh366f7ed2017-03-11 21:57:14 -0800284
Austin Schuh624088a2017-03-22 22:36:16 -0700285 this_thread::sleep_for(start_time + chrono::seconds(9) -
286 monotonic_clock::now());
Austin Schuh624088a2017-03-22 22:36:16 -0700287 if (ShouldCancel()) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800288
Austin Schuh624088a2017-03-22 22:36:16 -0700289 set_intake_max_velocity(0.05);
290 set_intake_goal(0.08);
291 SendSuperstructureGoal();
Tyler Chatowf31da682017-01-22 01:39:40 +0000292
Austin Schuh624088a2017-03-22 22:36:16 -0700293 this_thread::sleep_for(start_time + chrono::seconds(15) -
294 monotonic_clock::now());
295 if (ShouldCancel()) return true;
296
297 set_shooter_velocity(0.0);
298 set_indexer_angular_velocity(0.0);
299 SendSuperstructureGoal();
300
301 } break;
Tyler Chatowf31da682017-01-22 01:39:40 +0000302 }
303
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700304 LOG(INFO, "Done %f\n",
305 ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
Tyler Chatowf31da682017-01-22 01:39:40 +0000306
307 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700308 event_loop()->monotonic_now(),
Tyler Chatowf31da682017-01-22 01:39:40 +0000309 ::std::chrono::milliseconds(5) / 2);
310
311 while (!ShouldCancel()) {
312 phased_loop.SleepUntilNext();
313 }
314 LOG(DEBUG, "Done running\n");
315
316 return true;
317}
318
Tyler Chatowf31da682017-01-22 01:39:40 +0000319} // namespace actors
320} // namespace y2017