blob: af29c464caafc4f0c5259a5f981da96ace314ca5 [file] [log] [blame]
Austin Schuh80ff2e12014-03-08 12:06:19 -08001#include <stdio.h>
2
3#include <memory>
Austin Schuh47017412013-03-10 11:50:46 -07004
Brian3afd6fc2014-04-02 20:41:49 -07005#include "aos/common/util/phased_loop.h"
Austin Schuh47017412013-03-10 11:50:46 -07006#include "aos/common/time.h"
Austin Schuh6be011a2013-03-19 10:07:02 +00007#include "aos/common/util/trapezoid_profile.h"
Brian Silverman598800f2013-05-09 17:08:42 -07008#include "aos/common/logging/logging.h"
Brian Silverman6f621542014-04-06 16:00:41 -07009#include "aos/common/logging/queue_logging.h"
Brian Silverman598800f2013-05-09 17:08:42 -070010
11#include "frc971/autonomous/auto.q.h"
Austin Schuh6be011a2013-03-19 10:07:02 +000012#include "frc971/constants.h"
13#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Austin Schuh80ff2e12014-03-08 12:06:19 -080014#include "frc971/control_loops/shooter/shooter.q.h"
15#include "frc971/control_loops/claw/claw.q.h"
16#include "frc971/actions/action_client.h"
17#include "frc971/actions/shoot_action.h"
18#include "frc971/actions/drivetrain_action.h"
Brian Silverman45ceeb52014-04-17 15:15:18 -070019#include "frc971/queues/other_sensors.q.h"
Brian Silverman6f621542014-04-06 16:00:41 -070020#include "frc971/queues/hot_goal.q.h"
Austin Schuh47017412013-03-10 11:50:46 -070021
22using ::aos::time::Time;
23
24namespace frc971 {
25namespace autonomous {
26
Austin Schuh80ff2e12014-03-08 12:06:19 -080027namespace time = ::aos::time;
28
Brian Silverman3b89ed82013-03-22 18:59:16 -070029static double left_initial_position, right_initial_position;
30
Austin Schuh6be011a2013-03-19 10:07:02 +000031bool ShouldExitAuto() {
32 ::frc971::autonomous::autonomous.FetchLatest();
33 bool ans = !::frc971::autonomous::autonomous->run_auto;
34 if (ans) {
35 LOG(INFO, "Time to exit auto mode\n");
36 }
37 return ans;
38}
39
Austin Schuh6be011a2013-03-19 10:07:02 +000040void StopDrivetrain() {
41 LOG(INFO, "Stopping the drivetrain\n");
Austin Schuh47017412013-03-10 11:50:46 -070042 control_loops::drivetrain.goal.MakeWithBuilder()
Brian Silverman3b89ed82013-03-22 18:59:16 -070043 .control_loop_driving(true)
Brian Silvermance86bac2013-03-31 19:07:24 -070044 .left_goal(left_initial_position)
45 .left_velocity_goal(0)
46 .right_goal(right_initial_position)
47 .right_velocity_goal(0)
48 .quickturn(false)
49 .Send();
50}
51
52void ResetDrivetrain() {
53 LOG(INFO, "resetting the drivetrain\n");
54 control_loops::drivetrain.goal.MakeWithBuilder()
55 .control_loop_driving(false)
Austin Schuh6be011a2013-03-19 10:07:02 +000056 .highgear(false)
57 .steering(0.0)
58 .throttle(0.0)
Brian Silverman38ea9bf2014-04-19 22:57:54 -070059 .left_goal(left_initial_position)
60 .left_velocity_goal(0)
61 .right_goal(right_initial_position)
62 .right_velocity_goal(0)
Austin Schuh6be011a2013-03-19 10:07:02 +000063 .Send();
64}
65
Brian Silverman3b89ed82013-03-22 18:59:16 -070066void DriveSpin(double radians) {
67 LOG(INFO, "going to spin %f\n", radians);
68
69 ::aos::util::TrapezoidProfile profile(::aos::time::Time::InMS(10));
70 ::Eigen::Matrix<double, 2, 1> driveTrainState;
71 const double goal_velocity = 0.0;
72 const double epsilon = 0.01;
Brian Silverman13be6682013-03-22 21:02:07 -070073 // in drivetrain "meters"
Brian Silverman3b89ed82013-03-22 18:59:16 -070074 const double kRobotWidth = 0.4544;
75
Brian Silverman7992d6e2013-03-24 19:20:54 -070076 profile.set_maximum_acceleration(1.5);
77 profile.set_maximum_velocity(0.8);
Brian Silverman3b89ed82013-03-22 18:59:16 -070078
79 const double side_offset = kRobotWidth * radians / 2.0;
80
81 while (true) {
Brian Silverman7f09f972013-03-22 23:11:39 -070082 ::aos::time::PhasedLoop10MS(5000); // wait until next 10ms tick
Brian Silverman3b89ed82013-03-22 18:59:16 -070083 driveTrainState = profile.Update(side_offset, goal_velocity);
84
85 if (::std::abs(driveTrainState(0, 0) - side_offset) < epsilon) break;
86 if (ShouldExitAuto()) return;
87
88 LOG(DEBUG, "Driving left to %f, right to %f\n",
Brian Silverman7992d6e2013-03-24 19:20:54 -070089 left_initial_position - driveTrainState(0, 0),
90 right_initial_position + driveTrainState(0, 0));
Brian Silverman3b89ed82013-03-22 18:59:16 -070091 control_loops::drivetrain.goal.MakeWithBuilder()
92 .control_loop_driving(true)
93 .highgear(false)
Brian Silverman7992d6e2013-03-24 19:20:54 -070094 .left_goal(left_initial_position - driveTrainState(0, 0))
95 .right_goal(right_initial_position + driveTrainState(0, 0))
96 .left_velocity_goal(-driveTrainState(1, 0))
97 .right_velocity_goal(driveTrainState(1, 0))
Brian Silverman3b89ed82013-03-22 18:59:16 -070098 .Send();
99 }
Brian Silverman7992d6e2013-03-24 19:20:54 -0700100 left_initial_position -= side_offset;
101 right_initial_position += side_offset;
Brian Silverman3b89ed82013-03-22 18:59:16 -0700102 LOG(INFO, "Done moving\n");
103}
104
Austin Schuh80ff2e12014-03-08 12:06:19 -0800105void PositionClawVertically(double intake_power = 0.0, double centering_power = 0.0) {
106 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
107 .bottom_angle(0.0)
108 .separation_angle(0.0)
109 .intake(intake_power)
110 .centering(centering_power)
111 .Send()) {
112 LOG(WARNING, "sending claw goal failed\n");
113 }
114}
Brian Silvermance86bac2013-03-31 19:07:24 -0700115
Austin Schuh80ff2e12014-03-08 12:06:19 -0800116void PositionClawBackIntake() {
117 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
118 .bottom_angle(-2.273474)
119 .separation_angle(0.0)
120 .intake(12.0)
121 .centering(12.0)
122 .Send()) {
123 LOG(WARNING, "sending claw goal failed\n");
124 }
125}
Brian Silvermance86bac2013-03-31 19:07:24 -0700126
Brian Silverman2bd5a062014-04-26 07:35:56 -0500127void PositionClawUpClosed() {
128 // Move the claw to where we're going to shoot from but keep it closed until
129 // it gets there.
130 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
131 .bottom_angle(0.86)
132 .separation_angle(0.0)
133 .intake(4.0)
134 .centering(1.0)
135 .Send()) {
136 LOG(WARNING, "sending claw goal failed\n");
137 }
138}
139
Austin Schuh80ff2e12014-03-08 12:06:19 -0800140void PositionClawForShot() {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800141 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
Brian Silverman31b5b822014-03-14 18:50:39 -0700142 .bottom_angle(0.86)
Austin Schuha4faacc2014-03-09 00:50:50 -0800143 .separation_angle(0.10)
144 .intake(4.0)
Austin Schuh80ff2e12014-03-08 12:06:19 -0800145 .centering(1.0)
146 .Send()) {
147 LOG(WARNING, "sending claw goal failed\n");
148 }
149}
150
151void SetShotPower(double power) {
Austin Schuha4faacc2014-03-09 00:50:50 -0800152 LOG(INFO, "Setting shot power to %f\n", power);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800153 if (!control_loops::shooter_queue_group.goal.MakeWithBuilder()
154 .shot_power(power)
155 .shot_requested(false)
156 .unload_requested(false)
157 .load_requested(false)
158 .Send()) {
159 LOG(WARNING, "sending shooter goal failed\n");
160 }
161}
162
163void WaitUntilDoneOrCanceled(Action *action) {
164 while (true) {
165 // Poll the running bit and auto done bits.
166 ::aos::time::PhasedLoop10MS(5000);
167 if (!action->Running() || ShouldExitAuto()) {
168 return;
169 }
170 }
171}
172
173void Shoot() {
174 // Shoot.
175 auto shoot_action = actions::MakeShootAction();
176 shoot_action->Start();
177 WaitUntilDoneOrCanceled(shoot_action.get());
178}
179
180::std::unique_ptr<TypedAction< ::frc971::actions::DrivetrainActionQueueGroup>>
Natalia Frumkind1fa52f2014-06-21 15:24:25 -0700181SetDriveGoal(double distance, bool slow_acceleration,
182 double maximum_velocity = 1.7, double theta = 0) {
Austin Schuha4faacc2014-03-09 00:50:50 -0800183 LOG(INFO, "Driving to %f\n", distance);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800184 auto drivetrain_action = actions::MakeDrivetrainAction();
185 drivetrain_action->GetGoal()->left_initial_position = left_initial_position;
186 drivetrain_action->GetGoal()->right_initial_position = right_initial_position;
187 drivetrain_action->GetGoal()->y_offset = distance;
Brian Silvermanad9e0002014-04-13 14:55:57 -0700188 drivetrain_action->GetGoal()->theta_offset = theta;
Austin Schuh80ff2e12014-03-08 12:06:19 -0800189 drivetrain_action->GetGoal()->maximum_velocity = maximum_velocity;
Natalia Frumkind1fa52f2014-06-21 15:24:25 -0700190 drivetrain_action->GetGoal()->maximum_acceleration =
191 slow_acceleration ? 2.5 : 3.0;
Austin Schuh80ff2e12014-03-08 12:06:19 -0800192 drivetrain_action->Start();
Brian Silvermanad9e0002014-04-13 14:55:57 -0700193 left_initial_position +=
194 distance - theta * constants::GetValues().turn_width / 2.0;
195 right_initial_position +=
196 distance + theta * constants::GetValues().turn_width / 2. -
197 theta * constants::GetValues().turn_width / 2.00;
Austin Schuh80ff2e12014-03-08 12:06:19 -0800198 return ::std::move(drivetrain_action);
199}
200
201void InitializeEncoders() {
Austin Schuh577edf62014-04-13 10:33:05 -0700202 control_loops::drivetrain.status.FetchLatest();
203 while (!control_loops::drivetrain.status.get()) {
Brian Silverman2c1e0342014-04-11 16:15:01 -0700204 LOG(WARNING,
205 "No previous drivetrain position packet, trying to fetch again\n");
Austin Schuh577edf62014-04-13 10:33:05 -0700206 control_loops::drivetrain.status.FetchNextBlocking();
Brian Silverman3b89ed82013-03-22 18:59:16 -0700207 }
208 left_initial_position =
Austin Schuh577edf62014-04-13 10:33:05 -0700209 control_loops::drivetrain.status->filtered_left_position;
Brian Silverman3b89ed82013-03-22 18:59:16 -0700210 right_initial_position =
Austin Schuh577edf62014-04-13 10:33:05 -0700211 control_loops::drivetrain.status->filtered_right_position;
Brian Silverman3b89ed82013-03-22 18:59:16 -0700212
Austin Schuh80ff2e12014-03-08 12:06:19 -0800213}
214
Austin Schuha4faacc2014-03-09 00:50:50 -0800215void WaitUntilClawDone() {
216 while (true) {
217 // Poll the running bit and auto done bits.
218 ::aos::time::PhasedLoop10MS(5000);
219 control_loops::claw_queue_group.status.FetchLatest();
220 control_loops::claw_queue_group.goal.FetchLatest();
221 if (ShouldExitAuto()) {
222 return;
223 }
224 if (control_loops::claw_queue_group.status.get() == nullptr ||
225 control_loops::claw_queue_group.goal.get() == nullptr) {
226 continue;
227 }
228 bool ans =
229 control_loops::claw_queue_group.status->zeroed &&
230 (::std::abs(control_loops::claw_queue_group.status->bottom_velocity) <
231 1.0) &&
232 (::std::abs(control_loops::claw_queue_group.status->bottom -
233 control_loops::claw_queue_group.goal->bottom_angle) <
234 0.10) &&
235 (::std::abs(control_loops::claw_queue_group.status->separation -
236 control_loops::claw_queue_group.goal->separation_angle) <
237 0.4);
238 if (ans) {
239 return;
240 }
241 }
242}
243
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700244class HotGoalDecoder {
245 public:
246 HotGoalDecoder() {
247 ResetCounts();
248 }
249
250 void ResetCounts() {
251 hot_goal.FetchLatest();
252 if (hot_goal.get()) {
Brian Silverman38ea9bf2014-04-19 22:57:54 -0700253 start_counts_ = *hot_goal;
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700254 LOG_STRUCT(INFO, "counts reset to", start_counts_);
255 start_counts_valid_ = true;
256 } else {
257 LOG(WARNING, "no hot goal message. ignoring\n");
258 start_counts_valid_ = false;
259 }
260 }
261
Brian Silverman38ea9bf2014-04-19 22:57:54 -0700262 void Update(bool block = false) {
263 if (block) {
264 hot_goal.FetchAnother();
265 } else {
266 hot_goal.FetchLatest();
267 }
Brian Silvermanfa577e42014-04-19 12:03:00 -0700268 if (hot_goal.get()) LOG_STRUCT(INFO, "new counts", *hot_goal);
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700269 }
270
Brian Silverman38ea9bf2014-04-19 22:57:54 -0700271 bool left_triggered() const {
272 if (!start_counts_valid_ || !hot_goal.get()) return false;
273 return (hot_goal->left_count - start_counts_.left_count) > kThreshold;
274 }
275
276 bool right_triggered() const {
277 if (!start_counts_valid_ || !hot_goal.get()) return false;
278 return (hot_goal->right_count - start_counts_.right_count) > kThreshold;
279 }
280
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700281 bool is_left() const {
282 if (!start_counts_valid_ || !hot_goal.get()) return false;
283 const uint64_t left_difference =
284 hot_goal->left_count - start_counts_.left_count;
285 const uint64_t right_difference =
286 hot_goal->right_count - start_counts_.right_count;
287 if (left_difference > kThreshold) {
288 if (right_difference > kThreshold) {
Brian Silverman38ea9bf2014-04-19 22:57:54 -0700289 // We've seen a lot of both, so pick the one we've seen the most of.
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700290 return left_difference > right_difference;
291 } else {
292 // We've seen enough left but not enough right, so go with it.
293 return true;
294 }
295 } else {
296 // We haven't seen enough left, so it's not left.
297 return false;
298 }
299 }
300
301 bool is_right() const {
302 if (!start_counts_valid_ || !hot_goal.get()) return false;
303 const uint64_t left_difference =
304 hot_goal->left_count - start_counts_.left_count;
305 const uint64_t right_difference =
306 hot_goal->right_count - start_counts_.right_count;
307 if (right_difference > kThreshold) {
308 if (left_difference > kThreshold) {
Brian Silverman38ea9bf2014-04-19 22:57:54 -0700309 // We've seen a lot of both, so pick the one we've seen the most of.
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700310 return right_difference > left_difference;
311 } else {
312 // We've seen enough right but not enough left, so go with it.
313 return true;
314 }
315 } else {
316 // We haven't seen enough right, so it's not right.
317 return false;
318 }
319 }
320
321 private:
Brian Silverman38ea9bf2014-04-19 22:57:54 -0700322 static const uint64_t kThreshold = 5;
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700323
324 ::frc971::HotGoal start_counts_;
325 bool start_counts_valid_;
326};
327
Austin Schuh80ff2e12014-03-08 12:06:19 -0800328void HandleAuto() {
Brian Silverman45ceeb52014-04-17 15:15:18 -0700329 enum class AutoVersion : uint8_t {
330 kStraight,
331 kDoubleHot,
Brian Silverman38ea9bf2014-04-19 22:57:54 -0700332 kSingleHot,
Brian Silverman45ceeb52014-04-17 15:15:18 -0700333 };
334
Austin Schuha4faacc2014-03-09 00:50:50 -0800335 // The front of the robot is 1.854 meters from the wall
Brian Silvermanad9e0002014-04-13 14:55:57 -0700336 static const double kShootDistance = 3.15;
337 static const double kPickupDistance = 0.5;
338 static const double kTurnAngle = 0.3;
Brian Silverman45ceeb52014-04-17 15:15:18 -0700339
Austin Schuh577edf62014-04-13 10:33:05 -0700340 ::aos::time::Time start_time = ::aos::time::Time::Now();
Austin Schuh80ff2e12014-03-08 12:06:19 -0800341 LOG(INFO, "Handling auto mode\n");
Brian Silvermanb36ae1f2014-04-17 15:13:41 -0700342
Brian Silverman45ceeb52014-04-17 15:15:18 -0700343 AutoVersion auto_version;
344 ::frc971::sensors::auto_mode.FetchLatest();
345 if (!::frc971::sensors::auto_mode.get()) {
346 LOG(WARNING, "not sure which auto mode to use\n");
347 auto_version = AutoVersion::kStraight;
348 } else {
Brian Silverman38ea9bf2014-04-19 22:57:54 -0700349 static const double kSelectorMin = 0.2, kSelectorMax = 4.4;
350
351 const double kSelectorStep = (kSelectorMax - kSelectorMin) / 3.0;
352 if (::frc971::sensors::auto_mode->voltage < kSelectorStep + kSelectorMin) {
353 auto_version = AutoVersion::kSingleHot;
354 } else if (::frc971::sensors::auto_mode->voltage <
355 2 * kSelectorStep + kSelectorMin) {
356 auto_version = AutoVersion::kStraight;
357 } else {
358 auto_version = AutoVersion::kDoubleHot;
359 }
Brian Silverman45ceeb52014-04-17 15:15:18 -0700360 }
Brian Silvermanf7986142014-04-21 17:42:35 -0700361 LOG(INFO, "running auto %" PRIu8 "\n", static_cast<uint8_t>(auto_version));
Brian Silverman45ceeb52014-04-17 15:15:18 -0700362
Natalia Frumkind1fa52f2014-06-21 15:24:25 -0700363 const bool drive_slow_acceleration = auto_version == AutoVersion::kStraight;
364
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700365 HotGoalDecoder hot_goal_decoder;
366 // True for left, false for right.
367 bool first_shot_left, second_shot_left_default, second_shot_left;
Brian Silverman6f621542014-04-06 16:00:41 -0700368
Austin Schuh80ff2e12014-03-08 12:06:19 -0800369 ResetDrivetrain();
370
371 if (ShouldExitAuto()) return;
372 InitializeEncoders();
373
374 // Turn the claw on, keep it straight up until the ball has been grabbed.
Austin Schuh577edf62014-04-13 10:33:05 -0700375 LOG(INFO, "Claw going up at %f\n",
376 (::aos::time::Time::Now() - start_time).ToSeconds());
Austin Schuh80ff2e12014-03-08 12:06:19 -0800377 PositionClawVertically(12.0, 4.0);
Austin Schuha4faacc2014-03-09 00:50:50 -0800378 SetShotPower(115.0);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800379
380 // Wait for the ball to enter the claw.
Austin Schuha4faacc2014-03-09 00:50:50 -0800381 time::SleepFor(time::Time::InSeconds(0.25));
Austin Schuh80ff2e12014-03-08 12:06:19 -0800382 if (ShouldExitAuto()) return;
Austin Schuh577edf62014-04-13 10:33:05 -0700383 LOG(INFO, "Readying claw for shot at %f\n",
384 (::aos::time::Time::Now() - start_time).ToSeconds());
Austin Schuh80ff2e12014-03-08 12:06:19 -0800385
386 {
387 if (ShouldExitAuto()) return;
388 // Drive to the goal.
Natalia Frumkind1fa52f2014-06-21 15:24:25 -0700389 auto drivetrain_action = SetDriveGoal(-kShootDistance,
390 drive_slow_acceleration, 2.5);
Austin Schuha4faacc2014-03-09 00:50:50 -0800391 time::SleepFor(time::Time::InSeconds(0.75));
392 PositionClawForShot();
393 LOG(INFO, "Waiting until drivetrain is finished\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800394 WaitUntilDoneOrCanceled(drivetrain_action.get());
395 if (ShouldExitAuto()) return;
396 }
Brian Silvermanad9e0002014-04-13 14:55:57 -0700397
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700398 hot_goal_decoder.Update();
399 if (hot_goal_decoder.is_left()) {
400 LOG(INFO, "first shot left\n");
401 first_shot_left = true;
402 second_shot_left_default = false;
403 } else if (hot_goal_decoder.is_right()) {
404 LOG(INFO, "first shot right\n");
405 first_shot_left = false;
406 second_shot_left_default = true;
407 } else {
408 LOG(INFO, "first shot defaulting left\n");
409 first_shot_left = true;
410 second_shot_left_default = true;
411 }
412 if (auto_version == AutoVersion::kDoubleHot) {
Brian Silvermanad9e0002014-04-13 14:55:57 -0700413 if (ShouldExitAuto()) return;
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700414 auto drivetrain_action =
Natalia Frumkind1fa52f2014-06-21 15:24:25 -0700415 SetDriveGoal(0, drive_slow_acceleration, 2,
416 first_shot_left ? kTurnAngle : -kTurnAngle);
Brian Silvermanad9e0002014-04-13 14:55:57 -0700417 WaitUntilDoneOrCanceled(drivetrain_action.get());
418 if (ShouldExitAuto()) return;
Brian Silverman38ea9bf2014-04-19 22:57:54 -0700419 } else if (auto_version == AutoVersion::kSingleHot) {
420 do {
Brian Silvermanaf4a5852014-04-23 22:27:59 -0500421 // TODO(brians): Wait for next message with timeout or something.
422 ::aos::time::SleepFor(::aos::time::Time::InSeconds(0.003));
423 hot_goal_decoder.Update(false);
Brian Silverman38ea9bf2014-04-19 22:57:54 -0700424 if (ShouldExitAuto()) return;
425 } while (!hot_goal_decoder.left_triggered() &&
426 (::aos::time::Time::Now() - start_time) <
427 ::aos::time::Time::InSeconds(9));
Natalia Frumkind1fa52f2014-06-21 15:24:25 -0700428 } else if (auto_version == AutoVersion::kStraight) {
429 time::SleepFor(time::Time::InSeconds(0.4));
Brian Silvermanad9e0002014-04-13 14:55:57 -0700430 }
Austin Schuh80ff2e12014-03-08 12:06:19 -0800431
432 // Shoot.
Brian Silvermanad9e0002014-04-13 14:55:57 -0700433 LOG(INFO, "Shooting at %f\n",
434 (::aos::time::Time::Now() - start_time).ToSeconds());
Austin Schuh80ff2e12014-03-08 12:06:19 -0800435 Shoot();
Austin Schuh577edf62014-04-13 10:33:05 -0700436 time::SleepFor(time::Time::InSeconds(0.05));
Austin Schuh80ff2e12014-03-08 12:06:19 -0800437
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700438 if (auto_version == AutoVersion::kDoubleHot) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800439 if (ShouldExitAuto()) return;
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700440 auto drivetrain_action =
Natalia Frumkind1fa52f2014-06-21 15:24:25 -0700441 SetDriveGoal(0, drive_slow_acceleration, 2,
442 first_shot_left ? -kTurnAngle : kTurnAngle);
Brian Silvermanad9e0002014-04-13 14:55:57 -0700443 WaitUntilDoneOrCanceled(drivetrain_action.get());
444 if (ShouldExitAuto()) return;
Brian Silverman38ea9bf2014-04-19 22:57:54 -0700445 } else if (auto_version == AutoVersion::kSingleHot) {
446 LOG(INFO, "auto done at %f\n",
447 (::aos::time::Time::Now() - start_time).ToSeconds());
448 PositionClawVertically(0.0, 0.0);
449 return;
Brian Silvermanad9e0002014-04-13 14:55:57 -0700450 }
Natalia Frumkind1fa52f2014-06-21 15:24:25 -0700451
Brian Silvermanad9e0002014-04-13 14:55:57 -0700452 {
453 if (ShouldExitAuto()) return;
Austin Schuh80ff2e12014-03-08 12:06:19 -0800454 // Intake the new ball.
Austin Schuh577edf62014-04-13 10:33:05 -0700455 LOG(INFO, "Claw ready for intake at %f\n",
456 (::aos::time::Time::Now() - start_time).ToSeconds());
Austin Schuh80ff2e12014-03-08 12:06:19 -0800457 PositionClawBackIntake();
Brian Silvermanb94069c2014-04-17 14:34:24 -0700458 auto drivetrain_action =
Natalia Frumkind1fa52f2014-06-21 15:24:25 -0700459 SetDriveGoal(kShootDistance + kPickupDistance,
460 drive_slow_acceleration, 2.5);
Austin Schuha4faacc2014-03-09 00:50:50 -0800461 LOG(INFO, "Waiting until drivetrain is finished\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800462 WaitUntilDoneOrCanceled(drivetrain_action.get());
463 if (ShouldExitAuto()) return;
Austin Schuh577edf62014-04-13 10:33:05 -0700464 LOG(INFO, "Wait for the claw at %f\n",
465 (::aos::time::Time::Now() - start_time).ToSeconds());
Austin Schuha4faacc2014-03-09 00:50:50 -0800466 WaitUntilClawDone();
467 if (ShouldExitAuto()) return;
Austin Schuh80ff2e12014-03-08 12:06:19 -0800468 }
469
470 // Drive back.
471 {
Austin Schuh577edf62014-04-13 10:33:05 -0700472 LOG(INFO, "Driving back at %f\n",
473 (::aos::time::Time::Now() - start_time).ToSeconds());
Brian Silvermanb94069c2014-04-17 14:34:24 -0700474 auto drivetrain_action =
Natalia Frumkind1fa52f2014-06-21 15:24:25 -0700475 SetDriveGoal(-(kShootDistance + kPickupDistance),
476 drive_slow_acceleration, 2.5);
Austin Schuh577edf62014-04-13 10:33:05 -0700477 time::SleepFor(time::Time::InSeconds(0.3));
Brian Silverman38ea9bf2014-04-19 22:57:54 -0700478 hot_goal_decoder.ResetCounts();
Austin Schuh80ff2e12014-03-08 12:06:19 -0800479 if (ShouldExitAuto()) return;
Brian Silverman2bd5a062014-04-26 07:35:56 -0500480 PositionClawUpClosed();
481 WaitUntilClawDone();
482 if (ShouldExitAuto()) return;
Austin Schuh80ff2e12014-03-08 12:06:19 -0800483 PositionClawForShot();
Austin Schuha4faacc2014-03-09 00:50:50 -0800484 LOG(INFO, "Waiting until drivetrain is finished\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800485 WaitUntilDoneOrCanceled(drivetrain_action.get());
Brian Silverman2bd5a062014-04-26 07:35:56 -0500486 if (ShouldExitAuto()) return;
Austin Schuha4faacc2014-03-09 00:50:50 -0800487 WaitUntilClawDone();
Austin Schuh80ff2e12014-03-08 12:06:19 -0800488 if (ShouldExitAuto()) return;
489 }
490
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700491 hot_goal_decoder.Update();
492 if (hot_goal_decoder.is_left()) {
493 LOG(INFO, "second shot left\n");
494 second_shot_left = true;
495 } else if (hot_goal_decoder.is_right()) {
496 LOG(INFO, "second shot right\n");
497 second_shot_left = false;
498 } else {
499 LOG(INFO, "second shot defaulting %s\n",
500 second_shot_left_default ? "left" : "right");
501 second_shot_left = second_shot_left_default;
502 }
503 if (auto_version == AutoVersion::kDoubleHot) {
Brian Silvermanad9e0002014-04-13 14:55:57 -0700504 if (ShouldExitAuto()) return;
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700505 auto drivetrain_action =
Natalia Frumkind1fa52f2014-06-21 15:24:25 -0700506 SetDriveGoal(0, drive_slow_acceleration, 2,
507 second_shot_left ? kTurnAngle : -kTurnAngle);
Brian Silvermanad9e0002014-04-13 14:55:57 -0700508 WaitUntilDoneOrCanceled(drivetrain_action.get());
509 if (ShouldExitAuto()) return;
Natalia Frumkind1fa52f2014-06-21 15:24:25 -0700510 } else if (auto_version == AutoVersion::kStraight) {
511 time::SleepFor(time::Time::InSeconds(0.4));
Brian Silvermanad9e0002014-04-13 14:55:57 -0700512 }
513
Austin Schuh577edf62014-04-13 10:33:05 -0700514 LOG(INFO, "Shooting at %f\n",
515 (::aos::time::Time::Now() - start_time).ToSeconds());
Austin Schuh80ff2e12014-03-08 12:06:19 -0800516 // Shoot
517 Shoot();
518 if (ShouldExitAuto()) return;
519
520 // Get ready to zero when we come back up.
Austin Schuh577edf62014-04-13 10:33:05 -0700521 time::SleepFor(time::Time::InSeconds(0.05));
Austin Schuh80ff2e12014-03-08 12:06:19 -0800522 PositionClawVertically(0.0, 0.0);
Austin Schuh47017412013-03-10 11:50:46 -0700523}
524
525} // namespace autonomous
526} // namespace frc971