blob: a678e79e88e3e40873ebc58747db88ebf75f7019 [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) {
Austin Schuh77ed5432019-07-07 20:41:36 -070051 const monotonic_clock::time_point start_time = monotonic_now();
Austin Schuhf257f3c2019-10-27 21:00:43 -070052 AOS_LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n",
53 params.mode);
Austin Schuh624088a2017-03-22 22:36:16 -070054 Reset();
Tyler Chatowf31da682017-01-22 01:39:40 +000055
56 switch (params.mode) {
Austin Schuh19e15d72017-06-24 13:34:47 -070057 case 503: {
58 // Middle gear auto.
59 // Red is positive.
60 // Line up on boiler side edge.
61 constexpr double kDriveDirection = 1.0;
62
63 set_intake_goal(0.18);
64 set_turret_goal(0.0);
65 SendSuperstructureGoal();
66
67 set_turret_goal(-M_PI / 4.0 * kDriveDirection);
68 SendSuperstructureGoal();
69
70 // Turn towards the peg.
71 StartDrive(0.0, kDriveDirection * 0.162, kGearDrive, kSlowTurn);
72 if (!WaitForDriveNear(100.0, 0.02)) return true;
73 if (!WaitForTurnProfileDone()) return true;
74
75 // Drive, but get within 0.3 meters
76 StartDrive(1.73, 0.0, kGearFastDrive, kSlowTurn);
77 if (!WaitForDriveNear(0.3, 0.0)) return true;
78
79 // Now, add a slow, short move to actually place the gear.
80 StartDrive(0.18, 0.0, kGearPlaceDrive, kSecondGearTurn);
81 if (!WaitForDriveNear(0.07, 0.0)) return true;
82
83 set_gear_servo(0.3);
84 SendSuperstructureGoal();
85
86 // Slow down and then pause to let Chris pull the gear off.
87 this_thread::sleep_for(chrono::milliseconds(1000));
88
89 // Back up
90 StartDrive(-1.00, 0.0, kGearFastDrive, kSlowTurn);
91 if (!WaitForDriveNear(0.1, 0.1)) return true;
92
93 // Turn towards the boiler.
94 StartDrive(0.0, -kDriveDirection * M_PI / 2.0, kGearFastDrive, kSlowTurn);
95 if (!WaitForDriveNear(0.1, 0.1)) return true;
96
97 // Drive up near it.
98 StartDrive(1.8, 0.0, kGearFastDrive, kSlowTurn);
99 if (!WaitForDriveNear(0.1, 0.1)) return true;
100
101 set_hood_goal(0.37);
102 set_intake_goal(0.23);
103 set_shooter_velocity(353.0);
104 set_vision_track(true);
105 set_use_vision_for_shots(true);
106 set_indexer_angular_velocity(-1.1 * M_PI);
107 SendSuperstructureGoal();
108
109 this_thread::sleep_for(start_time + chrono::seconds(15) -
Austin Schuh77ed5432019-07-07 20:41:36 -0700110 monotonic_now());
Austin Schuh19e15d72017-06-24 13:34:47 -0700111 if (ShouldCancel()) return true;
112
113 set_shooter_velocity(0.0);
114 set_indexer_angular_velocity(0.0);
115 SendSuperstructureGoal();
116
117 } break;
118
119 case 500: {
Austin Schuh10475d72017-04-16 19:17:48 -0700120 // Red is positive.
121 constexpr double kDriveDirection = -1.0;
122 // Side peg + hopper auto.
123
124 set_intake_goal(0.23);
125 set_turret_goal(-M_PI * kDriveDirection);
126 SendSuperstructureGoal();
127
128 constexpr double kLongPegDrive = 3.025;
129
130 StartDrive(kLongPegDrive, -kDriveDirection * M_PI / 4, kGearDrive,
131 kFirstGearStartTurn);
132 if (!WaitForDriveNear(100.0, M_PI / 8.0)) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700133 AOS_LOG(INFO, "Turn Middle: %f left to go\n", DriveDistanceLeft());
Austin Schuh10475d72017-04-16 19:17:48 -0700134
135 StartDrive(0.0, 0.0, kGearDrive, kFirstGearTurn);
136
137 if (!WaitForTurnProfileDone()) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700138 AOS_LOG(INFO, "Turn profile ended: %f left to go\n", DriveDistanceLeft());
Austin Schuh10475d72017-04-16 19:17:48 -0700139
140 set_hood_goal(0.43);
141 set_shooter_velocity(364.0);
142 SendSuperstructureGoal();
143
144 constexpr double kTurnDistanceFromStart = 1.08;
145 if (!WaitForDriveNear(kLongPegDrive - kTurnDistanceFromStart, 10.0)) {
146 return true;
147 }
148
149 StartDrive(0.0, kDriveDirection * (M_PI / 4 + 0.3), kGearSlowDrive,
150 kSecondGearTurn);
151 if (!WaitForTurnProfileDone()) return true;
152
153 StartDrive(0.0, 0.0, kGearFastDrive, kSecondGearTurn);
154
155 if (!WaitForDriveNear(0.3, 0.0)) return true;
156
157 set_vision_track(true);
158 SendSuperstructureGoal();
159
160 StartDrive(0.19, 0.0, kGearPlaceDrive, kSecondGearTurn);
161
162 if (!WaitForDriveNear(0.07, 0.0)) return true;
163 set_gear_servo(0.3);
164 SendSuperstructureGoal();
165
166 // Shoot from the peg.
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700167 // set_indexer_angular_velocity(-2.1 * M_PI);
168 // SendSuperstructureGoal();
169 // this_thread::sleep_for(chrono::milliseconds(1750));
Austin Schuh10475d72017-04-16 19:17:48 -0700170
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700171 // this_thread::sleep_for(chrono::milliseconds(500));
Austin Schuh10475d72017-04-16 19:17:48 -0700172 this_thread::sleep_for(chrono::milliseconds(750));
173 set_indexer_angular_velocity(0.0);
174 set_vision_track(false);
175 set_turret_goal(0.0);
176
177 set_hood_goal(0.37);
178 set_shooter_velocity(351.0);
179 set_intake_goal(0.18);
180 set_gear_servo(0.4);
181
182 SendSuperstructureGoal();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700183 AOS_LOG(INFO, "Starting drive back %f\n",
184 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh10475d72017-04-16 19:17:48 -0700185
Austin Schuh19e15d72017-06-24 13:34:47 -0700186 StartDrive(-2.75, kDriveDirection * 1.24, kSlowDrive,
Austin Schuh10475d72017-04-16 19:17:48 -0700187 kFirstGearStartTurn);
188 if (!WaitForDriveNear(2.4, 0.0)) return true;
189
190 if (!WaitForTurnProfileDone()) return true;
191 StartDrive(0.0, 0.0, kGearBallBackDrive, kFirstGearStartTurn);
192
193 if (!WaitForDriveNear(0.2, 0.0)) return true;
194 this_thread::sleep_for(chrono::milliseconds(200));
Austin Schuh19e15d72017-06-24 13:34:47 -0700195 // Trip the hopper
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700196 StartDrive(0.0, kDriveDirection * 1.10, kSlowDrive, kSmashTurn);
Austin Schuh10475d72017-04-16 19:17:48 -0700197
198 if (!WaitForDriveNear(0.2, 0.35)) return true;
199 set_vision_track(true);
200 set_use_vision_for_shots(true);
201 SendSuperstructureGoal();
202
203 if (!WaitForDriveNear(0.2, 0.2)) return true;
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700204 StartDrive(0.0, -kDriveDirection * 0.15, kSlowDrive, kSmashTurn);
Austin Schuh10475d72017-04-16 19:17:48 -0700205
Austin Schuhf257f3c2019-10-27 21:00:43 -0700206 AOS_LOG(INFO, "Starting second shot %f\n",
207 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh10475d72017-04-16 19:17:48 -0700208 set_indexer_angular_velocity(-2.15 * M_PI);
209 SendSuperstructureGoal();
210 if (!WaitForDriveNear(0.2, 0.1)) return true;
211
212 this_thread::sleep_for(start_time + chrono::seconds(11) -
Austin Schuh77ed5432019-07-07 20:41:36 -0700213 monotonic_now());
Austin Schuh10475d72017-04-16 19:17:48 -0700214 if (ShouldCancel()) return true;
215 set_intake_max_velocity(0.05);
216 set_intake_goal(0.08);
217
218 this_thread::sleep_for(start_time + chrono::seconds(15) -
Austin Schuh77ed5432019-07-07 20:41:36 -0700219 monotonic_now());
Austin Schuh10475d72017-04-16 19:17:48 -0700220 if (ShouldCancel()) return true;
221
222 set_intake_max_velocity(0.50);
223 set_intake_goal(0.18);
224 set_shooter_velocity(0.0);
225 set_indexer_angular_velocity(0.0);
226 SendSuperstructureGoal();
227
228 } break;
229
Austin Schuh19e15d72017-06-24 13:34:47 -0700230 default: {
231 // hopper auto
Austin Schuh10475d72017-04-16 19:17:48 -0700232 // Red is positive.
Austin Schuh624088a2017-03-22 22:36:16 -0700233 constexpr double kDriveDirection = 1.0;
Austin Schuh624088a2017-03-22 22:36:16 -0700234 set_intake_goal(0.07);
235 SendSuperstructureGoal();
Austin Schuh10475d72017-04-16 19:17:48 -0700236
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700237 StartDrive(-3.42, kDriveDirection * (M_PI / 10 - 0.057), kSlowDrive,
238 kFirstTurn);
Austin Schuh10475d72017-04-16 19:17:48 -0700239 if (!WaitForDriveNear(3.30, 0.0)) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700240 AOS_LOG(INFO, "Turn ended: %f left to go\n", DriveDistanceLeft());
Austin Schuh10475d72017-04-16 19:17:48 -0700241 // We can go to 2.50 before we hit the previous profile.
242
Austin Schuh19e15d72017-06-24 13:34:47 -0700243 if (!WaitForDriveNear(2.48, 0.0)) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700244 AOS_LOG(INFO, "%f left to go\n", DriveDistanceLeft());
Austin Schuh366f7ed2017-03-11 21:57:14 -0800245
Austin Schuh624088a2017-03-22 22:36:16 -0700246 set_intake_goal(0.23);
Austin Schuh76af51e2017-04-09 18:32:38 -0700247 set_turret_goal(0.0);
248 // Values good for blue:
249 // TODO(austin): Drive these off the auto switch.
Austin Schuh10475d72017-04-16 19:17:48 -0700250
251 set_hood_goal(0.37);
252 set_shooter_velocity(353.0);
Austin Schuh624088a2017-03-22 22:36:16 -0700253 SendSuperstructureGoal();
Austin Schuh366f7ed2017-03-11 21:57:14 -0800254
Austin Schuh10475d72017-04-16 19:17:48 -0700255 StartDrive(0.0, -M_PI / 8.0 * kDriveDirection, kSlowDrive, kSlowTurn);
256 if (!WaitForDriveNear(0.20, 0.0)) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800257
Austin Schuh10475d72017-04-16 19:17:48 -0700258 this_thread::sleep_for(chrono::milliseconds(300));
259
Austin Schuh19e15d72017-06-24 13:34:47 -0700260 // Turn to trigger the hopper.
Austin Schuh10475d72017-04-16 19:17:48 -0700261 StartDrive(0.0, (M_PI / 8.0 + 0.20) * kDriveDirection, kSlowDrive,
Austin Schuh624088a2017-03-22 22:36:16 -0700262 kSmashTurn);
263 if (!WaitForDriveNear(0.05, 0.2)) return true;
Austin Schuh366f7ed2017-03-11 21:57:14 -0800264
Austin Schuh624088a2017-03-22 22:36:16 -0700265 set_vision_track(true);
Austin Schuh10475d72017-04-16 19:17:48 -0700266 set_use_vision_for_shots(true);
Austin Schuh624088a2017-03-22 22:36:16 -0700267 SendSuperstructureGoal();
Austin Schuh366f7ed2017-03-11 21:57:14 -0800268
Austin Schuh19e15d72017-06-24 13:34:47 -0700269 // Now that everything is tracking, wait for the hood to zero before
270 // trying to shoot.
271 WaitForHoodZeroed();
272 if (ShouldCancel()) return true;
273
Austin Schuh624088a2017-03-22 22:36:16 -0700274 this_thread::sleep_for(chrono::milliseconds(200));
Austin Schuh366f7ed2017-03-11 21:57:14 -0800275
Austin Schuh19e15d72017-06-24 13:34:47 -0700276 // Turn back.
Austin Schuh624088a2017-03-22 22:36:16 -0700277 StartDrive(0.0, (-0.15) * kDriveDirection, kSlowDrive, kSlowTurn);
278 if (!WaitForDriveNear(0.05, 0.02)) return true;
Austin Schuh76af51e2017-04-09 18:32:38 -0700279
Austin Schuh19e15d72017-06-24 13:34:47 -0700280 set_indexer_angular_velocity(-2.1 * M_PI);
281 SendSuperstructureGoal();
282
Austin Schuhf257f3c2019-10-27 21:00:43 -0700283 AOS_LOG(INFO, "Started shooting at %f\n",
284 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuh366f7ed2017-03-11 21:57:14 -0800285
Austin Schuh77ed5432019-07-07 20:41:36 -0700286 this_thread::sleep_for(start_time + chrono::seconds(9) - monotonic_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) -
Austin Schuh77ed5432019-07-07 20:41:36 -0700294 monotonic_now());
Austin Schuh624088a2017-03-22 22:36:16 -0700295 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
Austin Schuhf257f3c2019-10-27 21:00:43 -0700304 AOS_LOG(INFO, "Done %f\n",
305 ::aos::time::DurationInSeconds(monotonic_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 }
Austin Schuhf257f3c2019-10-27 21:00:43 -0700314 AOS_LOG(DEBUG, "Done running\n");
Tyler Chatowf31da682017-01-22 01:39:40 +0000315
316 return true;
317}
318
Tyler Chatowf31da682017-01-22 01:39:40 +0000319} // namespace actors
320} // namespace y2017