blob: 1f3496d2a4dc5eda4259579af3a72f77191ba12c [file] [log] [blame]
Austin Schuh47017412013-03-10 11:50:46 -07001#include "stdio.h"
2
3#include "aos/aos_core.h"
4#include "aos/common/control_loop/Timing.h"
5#include "aos/common/time.h"
Austin Schuh6be011a2013-03-19 10:07:02 +00006#include "aos/common/util/trapezoid_profile.h"
Austin Schuh47017412013-03-10 11:50:46 -07007#include "frc971/autonomous/auto.q.h"
Austin Schuh6be011a2013-03-19 10:07:02 +00008#include "aos/common/messages/RobotState.q.h"
9#include "frc971/constants.h"
10#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Austin Schuh47017412013-03-10 11:50:46 -070011#include "frc971/control_loops/wrist/wrist_motor.q.h"
12#include "frc971/control_loops/index/index_motor.q.h"
13#include "frc971/control_loops/shooter/shooter_motor.q.h"
14#include "frc971/control_loops/angle_adjust/angle_adjust_motor.q.h"
15
16using ::aos::time::Time;
17
18namespace frc971 {
19namespace autonomous {
20
Brian Silverman3b89ed82013-03-22 18:59:16 -070021static double left_initial_position, right_initial_position;
22
Austin Schuh6be011a2013-03-19 10:07:02 +000023bool ShouldExitAuto() {
24 ::frc971::autonomous::autonomous.FetchLatest();
25 bool ans = !::frc971::autonomous::autonomous->run_auto;
26 if (ans) {
27 LOG(INFO, "Time to exit auto mode\n");
28 }
29 return ans;
30}
31
Austin Schuh47017412013-03-10 11:50:46 -070032void SetShooterVelocity(double velocity) {
Austin Schuh6be011a2013-03-19 10:07:02 +000033 LOG(INFO, "Setting shooter velocity to %f\n", velocity);
Austin Schuh47017412013-03-10 11:50:46 -070034 control_loops::shooter.goal.MakeWithBuilder()
35 .velocity(velocity).Send();
36}
37
Austin Schuh6be011a2013-03-19 10:07:02 +000038void SetWristGoal(double goal) {
39 LOG(INFO, "Setting wrist to %f\n", goal);
40 control_loops::wrist.goal.MakeWithBuilder()
41 .goal(goal).Send();
42}
43
44void SetAngle_AdjustGoal(double goal) {
45 LOG(INFO, "Setting angle adjust to %f\n", goal);
46 control_loops::angle_adjust.goal.MakeWithBuilder()
47 .goal(goal).Send();
48}
49
50void StartIndex() {
51 LOG(INFO, "Starting index\n");
52 control_loops::index_loop.goal.MakeWithBuilder()
Austin Schuh47017412013-03-10 11:50:46 -070053 .goal_state(2).Send();
54}
55
Austin Schuh6be011a2013-03-19 10:07:02 +000056void PreloadIndex() {
57 LOG(INFO, "Preloading index\n");
58 control_loops::index_loop.goal.MakeWithBuilder()
Austin Schuh47017412013-03-10 11:50:46 -070059 .goal_state(3).Send();
60}
61
Austin Schuh6be011a2013-03-19 10:07:02 +000062void ShootIndex() {
63 LOG(INFO, "Shooting index\n");
64 control_loops::index_loop.goal.MakeWithBuilder()
Austin Schuh47017412013-03-10 11:50:46 -070065 .goal_state(4).Send();
66}
67
Brian Silvermanb8d389f2013-03-19 22:54:06 -070068void ResetIndex() {
69 LOG(INFO, "Resetting index\n");
70 control_loops::index_loop.goal.MakeWithBuilder()
71 .goal_state(5).Send();
72}
73
74void WaitForIndexReset() {
75 LOG(INFO, "Waiting for the indexer to reset\n");
76 control_loops::index_loop.status.FetchLatest();
77
78 // Fetch a couple index status packets to make sure that the indexer has run.
79 for (int i = 0; i < 5; ++i) {
80 LOG(DEBUG, "Fetching another index status packet\n");
81 control_loops::index_loop.status.FetchNextBlocking();
82 if (ShouldExitAuto()) return;
83 }
84 LOG(INFO, "Indexer is now reset.\n");
85}
86
Austin Schuh6be011a2013-03-19 10:07:02 +000087void WaitForWrist() {
88 LOG(INFO, "Waiting for the wrist\n");
89 control_loops::wrist.status.FetchLatest();
90
91 while (!control_loops::wrist.status.get()) {
92 LOG(WARNING, "No previous wrist packet, trying to fetch again\n");
93 control_loops::wrist.status.FetchNextBlocking();
94 }
95
96 while (!control_loops::wrist.status->done) {
97 control_loops::wrist.status.FetchNextBlocking();
98 LOG(DEBUG, "Got a new wrist status packet\n");
99 if (ShouldExitAuto()) return;
100 }
101 LOG(INFO, "Done waiting for the wrist\n");
102}
Austin Schuh6be011a2013-03-19 10:07:02 +0000103void WaitForIndex() {
104 LOG(INFO, "Waiting for the indexer to be ready to intake\n");
105 control_loops::index_loop.status.FetchLatest();
106
107 while (!control_loops::index_loop.status.get()) {
108 LOG(WARNING, "No previous index packet, trying to fetch again\n");
109 control_loops::index_loop.status.FetchNextBlocking();
110 }
111
112 while (!control_loops::index_loop.status->ready_to_intake) {
113 control_loops::index_loop.status.FetchNextBlocking();
114 if (ShouldExitAuto()) return;
115 }
116 LOG(INFO, "Indexer ready to intake\n");
117}
118
119void WaitForAngle_Adjust() {
120 LOG(INFO, "Waiting for the angle adjuster to finish\n");
121 control_loops::angle_adjust.status.FetchLatest();
122
123 while (!control_loops::angle_adjust.status.get()) {
124 LOG(WARNING, "No previous angle adjust packet, trying to fetch again\n");
125 control_loops::angle_adjust.status.FetchNextBlocking();
126 }
127
128 while (!control_loops::angle_adjust.status->done) {
129 control_loops::angle_adjust.status.FetchNextBlocking();
130 if (ShouldExitAuto()) return;
131 }
132 LOG(INFO, "Angle adjuster done\n");
133}
134
Austin Schuh47017412013-03-10 11:50:46 -0700135void WaitForShooter() {
Austin Schuh6be011a2013-03-19 10:07:02 +0000136 LOG(INFO, "Waiting for the shooter to spin up\n");
Austin Schuh47017412013-03-10 11:50:46 -0700137 control_loops::shooter.status.FetchLatest();
Austin Schuh6be011a2013-03-19 10:07:02 +0000138
139 while (!control_loops::shooter.status.get()) {
140 LOG(WARNING, "No previous shooteracket, trying to fetch again\n");
Austin Schuh47017412013-03-10 11:50:46 -0700141 control_loops::shooter.status.FetchNextBlocking();
142 }
Austin Schuh47017412013-03-10 11:50:46 -0700143
Austin Schuh6be011a2013-03-19 10:07:02 +0000144 while (!control_loops::shooter.status->ready) {
145 control_loops::shooter.status.FetchNextBlocking();
146 if (ShouldExitAuto()) return;
Austin Schuh47017412013-03-10 11:50:46 -0700147 }
Austin Schuh6be011a2013-03-19 10:07:02 +0000148 LOG(INFO, "Shooter ready to shoot\n");
Austin Schuh47017412013-03-10 11:50:46 -0700149}
150
Austin Schuh6be011a2013-03-19 10:07:02 +0000151void ShootNDiscs(int n) {
152 LOG(INFO, "Waiting until %d discs have been shot\n", n);
153 control_loops::index_loop.status.FetchLatest();
154
155 while (!control_loops::index_loop.status.get()) {
156 LOG(WARNING, "No previous index_loop packet, trying to fetch again\n");
157 control_loops::index_loop.status.FetchNextBlocking();
158 }
159
160 int final_disc_count = control_loops::index_loop.status->shot_disc_count + n;
161 LOG(DEBUG, "Disc count should be %d when done\n", final_disc_count);
162 while (final_disc_count > control_loops::index_loop.status->shot_disc_count) {
163 control_loops::index_loop.status.FetchNextBlocking();
164 if (ShouldExitAuto()) return;
165 }
166 LOG(INFO, "Shot %d discs\n", n);
Austin Schuh47017412013-03-10 11:50:46 -0700167}
168
Austin Schuh6be011a2013-03-19 10:07:02 +0000169void StopDrivetrain() {
170 LOG(INFO, "Stopping the drivetrain\n");
Austin Schuh47017412013-03-10 11:50:46 -0700171 control_loops::drivetrain.goal.MakeWithBuilder()
Brian Silverman3b89ed82013-03-22 18:59:16 -0700172 .control_loop_driving(true)
Austin Schuh6be011a2013-03-19 10:07:02 +0000173 .highgear(false)
174 .steering(0.0)
175 .throttle(0.0)
Brian Silverman3b89ed82013-03-22 18:59:16 -0700176 .left_goal(left_initial_position)
177 .right_goal(right_initial_position)
Austin Schuh6be011a2013-03-19 10:07:02 +0000178 .quickturn(false)
179 .Send();
180}
181
Brian Silvermaned2cbde2013-03-31 01:56:14 -0700182void SetDriveGoal(double yoffset, double maximum_velocity = 1.5) {
Austin Schuh6be011a2013-03-19 10:07:02 +0000183 LOG(INFO, "Going to move %f\n", yoffset);
184
185 // Measured conversion to get the distance right.
Austin Schuh6be011a2013-03-19 10:07:02 +0000186 ::aos::util::TrapezoidProfile profile(::aos::time::Time::InMS(10));
187 ::Eigen::Matrix<double, 2, 1> driveTrainState;
188 const double goal_velocity = 0.0;
189 const double epsilon = 0.01;
190
Austin Schuhfae53362013-03-23 04:39:48 +0000191 profile.set_maximum_acceleration(2.0);
Brian Silvermaned2cbde2013-03-31 01:56:14 -0700192 profile.set_maximum_velocity(maximum_velocity);
Austin Schuh6be011a2013-03-19 10:07:02 +0000193
Austin Schuh6be011a2013-03-19 10:07:02 +0000194 while (true) {
Brian Silverman7f09f972013-03-22 23:11:39 -0700195 ::aos::time::PhasedLoop10MS(5000); // wait until next 10ms tick
Austin Schuh6be011a2013-03-19 10:07:02 +0000196 driveTrainState = profile.Update(yoffset, goal_velocity);
197
Brian Silverman3b89ed82013-03-22 18:59:16 -0700198 if (::std::abs(driveTrainState(0, 0) - yoffset) < epsilon) break;
Austin Schuh6be011a2013-03-19 10:07:02 +0000199 if (ShouldExitAuto()) return;
200
201 LOG(DEBUG, "Driving left to %f, right to %f\n",
202 driveTrainState(0, 0) + left_initial_position,
203 driveTrainState(0, 0) + right_initial_position);
204 control_loops::drivetrain.goal.MakeWithBuilder()
205 .control_loop_driving(true)
206 .highgear(false)
207 .left_goal(driveTrainState(0, 0) + left_initial_position)
208 .right_goal(driveTrainState(0, 0) + right_initial_position)
209 .left_velocity_goal(driveTrainState(1, 0))
210 .right_velocity_goal(driveTrainState(1, 0))
211 .Send();
212 }
Brian Silverman3b89ed82013-03-22 18:59:16 -0700213 left_initial_position += yoffset;
214 right_initial_position += yoffset;
Austin Schuh6be011a2013-03-19 10:07:02 +0000215 LOG(INFO, "Done moving\n");
216}
217
Brian Silverman13be6682013-03-22 21:02:07 -0700218// Drives forward while we can pick up discs up to max_distance (in meters).
Brian Silverman7992d6e2013-03-24 19:20:54 -0700219void DriveForwardPickUp(double max_distance, double wrist_angle) {
Brian Silverman13be6682013-03-22 21:02:07 -0700220 LOG(INFO, "going to pick up at a max distance of %f\n", max_distance);
Brian Silverman13be6682013-03-22 21:02:07 -0700221
222 static const ::aos::time::Time kPeriod = ::aos::time::Time::InMS(10);
223 ::aos::util::TrapezoidProfile profile(kPeriod);
224 ::Eigen::Matrix<double, 2, 1> driveTrainState;
225 const double goal_velocity = 0.0;
226 const double epsilon = 0.01;
227 static const double kMaximumAcceleration = 1.0;
228
229 profile.set_maximum_acceleration(kMaximumAcceleration);
230 profile.set_maximum_velocity(0.6);
231
Brian Silverman13be6682013-03-22 21:02:07 -0700232 bool driving_back = false;
Brian Silvermaned2cbde2013-03-31 01:56:14 -0700233 const double kDestination = -0.20;
Brian Silverman13be6682013-03-22 21:02:07 -0700234
235 while (true) {
Brian Silverman7f09f972013-03-22 23:11:39 -0700236 ::aos::time::PhasedLoop10MS(5000); // wait until next 10ms tick
Brian Silvermanc6ba6ee2013-03-22 21:10:38 -0700237 driveTrainState = profile.Update(driving_back ? kDestination : max_distance,
238 goal_velocity);
Brian Silverman13be6682013-03-22 21:02:07 -0700239
240 if (ShouldExitAuto()) return;
241
Brian Silvermanc6ba6ee2013-03-22 21:10:38 -0700242 if (driving_back) {
243 if (::std::abs(driveTrainState(0, 0)) < epsilon) break;
244 } else if (::std::abs(driveTrainState(0, 0) - max_distance) < epsilon) {
245 LOG(INFO, "went the max distance; driving back\n");
246 driving_back = true;
Brian Silverman7992d6e2013-03-24 19:20:54 -0700247 profile.set_maximum_velocity(2.5);
248 SetWristGoal(wrist_angle);
Brian Silverman13be6682013-03-22 21:02:07 -0700249 }
250
251 if (control_loops::index_loop.status.FetchLatest()) {
252 if (control_loops::index_loop.status->hopper_disc_count >= 4) {
253 LOG(INFO, "done intaking; driving back\n");
254 driving_back = true;
Brian Silverman7992d6e2013-03-24 19:20:54 -0700255 profile.set_maximum_velocity(2.5);
256 SetWristGoal(wrist_angle);
Brian Silverman13be6682013-03-22 21:02:07 -0700257 }
258 } else {
259 LOG(WARNING, "getting index status failed\n");
260 }
261
262 LOG(DEBUG, "Driving left to %f, right to %f\n",
263 driveTrainState(0, 0) + left_initial_position,
264 driveTrainState(0, 0) + right_initial_position);
265 control_loops::drivetrain.goal.MakeWithBuilder()
266 .control_loop_driving(true)
267 .highgear(false)
268 .left_goal(driveTrainState(0, 0) + left_initial_position)
269 .right_goal(driveTrainState(0, 0) + right_initial_position)
270 .left_velocity_goal(driveTrainState(1, 0))
271 .right_velocity_goal(driveTrainState(1, 0))
272 .Send();
273 }
Austin Schuhfae53362013-03-23 04:39:48 +0000274 left_initial_position += kDestination;
275 right_initial_position += kDestination;
Brian Silverman13be6682013-03-22 21:02:07 -0700276 LOG(INFO, "Done moving\n");
277}
278
Brian Silverman3b89ed82013-03-22 18:59:16 -0700279void DriveSpin(double radians) {
280 LOG(INFO, "going to spin %f\n", radians);
281
282 ::aos::util::TrapezoidProfile profile(::aos::time::Time::InMS(10));
283 ::Eigen::Matrix<double, 2, 1> driveTrainState;
284 const double goal_velocity = 0.0;
285 const double epsilon = 0.01;
Brian Silverman13be6682013-03-22 21:02:07 -0700286 // in drivetrain "meters"
Brian Silverman3b89ed82013-03-22 18:59:16 -0700287 const double kRobotWidth = 0.4544;
288
Brian Silverman7992d6e2013-03-24 19:20:54 -0700289 profile.set_maximum_acceleration(1.5);
290 profile.set_maximum_velocity(0.8);
Brian Silverman3b89ed82013-03-22 18:59:16 -0700291
292 const double side_offset = kRobotWidth * radians / 2.0;
293
294 while (true) {
Brian Silverman7f09f972013-03-22 23:11:39 -0700295 ::aos::time::PhasedLoop10MS(5000); // wait until next 10ms tick
Brian Silverman3b89ed82013-03-22 18:59:16 -0700296 driveTrainState = profile.Update(side_offset, goal_velocity);
297
298 if (::std::abs(driveTrainState(0, 0) - side_offset) < epsilon) break;
299 if (ShouldExitAuto()) return;
300
301 LOG(DEBUG, "Driving left to %f, right to %f\n",
Brian Silverman7992d6e2013-03-24 19:20:54 -0700302 left_initial_position - driveTrainState(0, 0),
303 right_initial_position + driveTrainState(0, 0));
Brian Silverman3b89ed82013-03-22 18:59:16 -0700304 control_loops::drivetrain.goal.MakeWithBuilder()
305 .control_loop_driving(true)
306 .highgear(false)
Brian Silverman7992d6e2013-03-24 19:20:54 -0700307 .left_goal(left_initial_position - driveTrainState(0, 0))
308 .right_goal(right_initial_position + driveTrainState(0, 0))
309 .left_velocity_goal(-driveTrainState(1, 0))
310 .right_velocity_goal(driveTrainState(1, 0))
Brian Silverman3b89ed82013-03-22 18:59:16 -0700311 .Send();
312 }
Brian Silverman7992d6e2013-03-24 19:20:54 -0700313 left_initial_position -= side_offset;
314 right_initial_position += side_offset;
Brian Silverman3b89ed82013-03-22 18:59:16 -0700315 LOG(INFO, "Done moving\n");
316}
317
318// start with N discs in the indexer
Austin Schuh6be011a2013-03-19 10:07:02 +0000319void HandleAuto() {
320 LOG(INFO, "Handling auto mode\n");
321 double WRIST_UP;
Brian Silvermaned2cbde2013-03-31 01:56:14 -0700322 ::aos::time::SleepFor(::aos::time::Time::InSeconds(20));
Austin Schuh6be011a2013-03-19 10:07:02 +0000323
324 ::aos::robot_state.FetchLatest();
Brian Silvermanc5277542013-03-22 13:33:07 -0700325 if (!::aos::robot_state.get() ||
Austin Schuh6be011a2013-03-19 10:07:02 +0000326 !constants::wrist_hall_effect_start_angle(&WRIST_UP)) {
327 LOG(ERROR, "Constants not ready\n");
328 return;
329 }
330 WRIST_UP -= 0.4;
331 LOG(INFO, "Got constants\n");
332 const double WRIST_DOWN = -0.633;
Brian Silvermaned2cbde2013-03-31 01:56:14 -0700333 const double ANGLE_ONE = 0.5434;
334 const double ANGLE_TWO = 0.685;
Austin Schuh6be011a2013-03-19 10:07:02 +0000335
Brian Silverman3b89ed82013-03-22 18:59:16 -0700336 control_loops::drivetrain.position.FetchLatest();
337 while (!control_loops::drivetrain.position.get()) {
338 LOG(WARNING, "No previous drivetrain position packet, trying to fetch again\n");
339 control_loops::drivetrain.position.FetchNextBlocking();
340 }
341 left_initial_position =
342 control_loops::drivetrain.position->left_encoder;
343 right_initial_position =
344 control_loops::drivetrain.position->right_encoder;
345
Brian Silvermanb8d389f2013-03-19 22:54:06 -0700346 ResetIndex();
Brian Silverman3b89ed82013-03-22 18:59:16 -0700347 StopDrivetrain();
Austin Schuh6be011a2013-03-19 10:07:02 +0000348
Brian Silverman7f09f972013-03-22 23:11:39 -0700349 SetWristGoal(WRIST_UP); // wrist must calibrate itself on power-up
Austin Schuh6be011a2013-03-19 10:07:02 +0000350 SetAngle_AdjustGoal(ANGLE_ONE);
Brian Silvermaned2cbde2013-03-31 01:56:14 -0700351 SetShooterVelocity(400.0);
Brian Silvermanb8d389f2013-03-19 22:54:06 -0700352 WaitForIndexReset();
Brian Silverman7f09f972013-03-22 23:11:39 -0700353 if (ShouldExitAuto()) return;
354 PreloadIndex(); // spin to top and put 1 disc into loader
Austin Schuh6be011a2013-03-19 10:07:02 +0000355
356 if (ShouldExitAuto()) return;
357 WaitForWrist();
358 if (ShouldExitAuto()) return;
359 WaitForAngle_Adjust();
Brian Silverman7f09f972013-03-22 23:11:39 -0700360 ShootIndex(); // tilt up, shoot, repeat until empty
361 // calls WaitForShooter
362 ShootNDiscs(3); // ShootNDiscs returns if ShouldExitAuto
Austin Schuh6be011a2013-03-19 10:07:02 +0000363 if (ShouldExitAuto()) return;
Austin Schuh6be011a2013-03-19 10:07:02 +0000364
Brian Silverman7f09f972013-03-22 23:11:39 -0700365 StartIndex(); // take in up to 4 discs
Austin Schuh6be011a2013-03-19 10:07:02 +0000366
Brian Silvermaned2cbde2013-03-31 01:56:14 -0700367 if (false) {
368 const double kDistanceToCenterMeters = 3.11023;
369 const double kMaxPickupDistance = 2.5;
370 const double kTurnToCenterDegrees = 78.2;
Austin Schuh6be011a2013-03-19 10:07:02 +0000371
Brian Silvermaned2cbde2013-03-31 01:56:14 -0700372 // Drive back to the center line.
373 SetDriveGoal(-kDistanceToCenterMeters);
374 if (ShouldExitAuto()) return;
Austin Schuh6be011a2013-03-19 10:07:02 +0000375
Brian Silvermaned2cbde2013-03-31 01:56:14 -0700376 SetWristGoal(WRIST_DOWN);
377 // Turn towards the center.
378 DriveSpin(kTurnToCenterDegrees * M_PI / 180.0);
379 if (ShouldExitAuto()) return;
380 WaitForWrist();
381 if (ShouldExitAuto()) return;
Austin Schuhfae53362013-03-23 04:39:48 +0000382
Brian Silvermaned2cbde2013-03-31 01:56:14 -0700383 // Pick up at most 4 discs and drive at most kMaxPickupDistance.
384 DriveForwardPickUp(kMaxPickupDistance, WRIST_UP);
Austin Schuhfae53362013-03-23 04:39:48 +0000385
Brian Silvermaned2cbde2013-03-31 01:56:14 -0700386 SetWristGoal(WRIST_UP);
387 DriveSpin(-kTurnToCenterDegrees * M_PI / 180.0);
388 if (ShouldExitAuto()) return;
389 // Drive back to where we were.
390 SetDriveGoal(kDistanceToCenterMeters);
391 if (ShouldExitAuto()) return;
Austin Schuhfae53362013-03-23 04:39:48 +0000392
Brian Silvermaned2cbde2013-03-31 01:56:14 -0700393 return;
Brian Silverman7992d6e2013-03-24 19:20:54 -0700394
Brian Silvermaned2cbde2013-03-31 01:56:14 -0700395 ShootNDiscs(4);
396 if (ShouldExitAuto()) return;
397 } else {
398 SetWristGoal(WRIST_DOWN);
399 SetAngle_AdjustGoal(ANGLE_TWO);
400 SetShooterVelocity(375.0);
401 StartIndex(); // take in up to 4 discs
402
403 if (ShouldExitAuto()) return;
404 WaitForWrist(); // wrist must be down before moving
405 ::aos::time::SleepFor(::aos::time::Time::InSeconds(0.25));
406
407 if (ShouldExitAuto()) return;
408 WaitForIndex(); // ready to pick up discs
409
410 static const double kDriveDistance = 3.2;
411 static const double kFirstDrive = 0.3;
412 SetDriveGoal(kFirstDrive, 0.6);
413 ::aos::time::SleepFor(::aos::time::Time::InSeconds(0.25));
414 SetDriveGoal(kDriveDistance - kFirstDrive, 0.6);
415 if (ShouldExitAuto()) return;
416
417 PreloadIndex();
418 ::aos::time::SleepFor(::aos::time::Time::InSeconds(0.4));
419 SetDriveGoal(-1.3);
420
421 if (ShouldExitAuto()) return;
422 WaitForAngle_Adjust();
423 if (ShouldExitAuto()) return;
424 ShootIndex();
425 if (ShouldExitAuto()) return;
426 }
Austin Schuh47017412013-03-10 11:50:46 -0700427}
428
429} // namespace autonomous
430} // namespace frc971