blob: 6d807f1aadc2f3d9bf651f11409dcf7b85f0f927 [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 Silverman96d9cea2013-11-12 21:10:50 -08009#include "aos/common/network/team_number.h"
Brian Silverman6f621542014-04-06 16:00:41 -070010#include "aos/common/logging/queue_logging.h"
Brian Silverman598800f2013-05-09 17:08:42 -070011
12#include "frc971/autonomous/auto.q.h"
Austin Schuh6be011a2013-03-19 10:07:02 +000013#include "frc971/constants.h"
14#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Austin Schuh80ff2e12014-03-08 12:06:19 -080015#include "frc971/control_loops/shooter/shooter.q.h"
16#include "frc971/control_loops/claw/claw.q.h"
17#include "frc971/actions/action_client.h"
18#include "frc971/actions/shoot_action.h"
19#include "frc971/actions/drivetrain_action.h"
Brian Silverman45ceeb52014-04-17 15:15:18 -070020#include "frc971/queues/other_sensors.q.h"
Brian Silverman6f621542014-04-06 16:00:41 -070021#include "frc971/queues/hot_goal.q.h"
Austin Schuh47017412013-03-10 11:50:46 -070022
23using ::aos::time::Time;
24
25namespace frc971 {
26namespace autonomous {
27
Austin Schuh80ff2e12014-03-08 12:06:19 -080028namespace time = ::aos::time;
29
Brian Silverman3b89ed82013-03-22 18:59:16 -070030static double left_initial_position, right_initial_position;
31
Austin Schuh6be011a2013-03-19 10:07:02 +000032bool ShouldExitAuto() {
33 ::frc971::autonomous::autonomous.FetchLatest();
34 bool ans = !::frc971::autonomous::autonomous->run_auto;
35 if (ans) {
36 LOG(INFO, "Time to exit auto mode\n");
37 }
38 return ans;
39}
40
Austin Schuh6be011a2013-03-19 10:07:02 +000041void StopDrivetrain() {
42 LOG(INFO, "Stopping the drivetrain\n");
Austin Schuh47017412013-03-10 11:50:46 -070043 control_loops::drivetrain.goal.MakeWithBuilder()
Brian Silverman3b89ed82013-03-22 18:59:16 -070044 .control_loop_driving(true)
Brian Silvermance86bac2013-03-31 19:07:24 -070045 .left_goal(left_initial_position)
46 .left_velocity_goal(0)
47 .right_goal(right_initial_position)
48 .right_velocity_goal(0)
49 .quickturn(false)
50 .Send();
51}
52
53void ResetDrivetrain() {
54 LOG(INFO, "resetting the drivetrain\n");
55 control_loops::drivetrain.goal.MakeWithBuilder()
56 .control_loop_driving(false)
Austin Schuh6be011a2013-03-19 10:07:02 +000057 .highgear(false)
58 .steering(0.0)
59 .throttle(0.0)
Brian Silverman38ea9bf2014-04-19 22:57:54 -070060 .left_goal(left_initial_position)
61 .left_velocity_goal(0)
62 .right_goal(right_initial_position)
63 .right_velocity_goal(0)
Austin Schuh6be011a2013-03-19 10:07:02 +000064 .Send();
65}
66
Brian Silverman3b89ed82013-03-22 18:59:16 -070067void DriveSpin(double radians) {
68 LOG(INFO, "going to spin %f\n", radians);
69
70 ::aos::util::TrapezoidProfile profile(::aos::time::Time::InMS(10));
71 ::Eigen::Matrix<double, 2, 1> driveTrainState;
72 const double goal_velocity = 0.0;
73 const double epsilon = 0.01;
Brian Silverman13be6682013-03-22 21:02:07 -070074 // in drivetrain "meters"
Brian Silverman3b89ed82013-03-22 18:59:16 -070075 const double kRobotWidth = 0.4544;
76
Brian Silverman7992d6e2013-03-24 19:20:54 -070077 profile.set_maximum_acceleration(1.5);
78 profile.set_maximum_velocity(0.8);
Brian Silverman3b89ed82013-03-22 18:59:16 -070079
80 const double side_offset = kRobotWidth * radians / 2.0;
81
82 while (true) {
Brian Silverman7f09f972013-03-22 23:11:39 -070083 ::aos::time::PhasedLoop10MS(5000); // wait until next 10ms tick
Brian Silverman3b89ed82013-03-22 18:59:16 -070084 driveTrainState = profile.Update(side_offset, goal_velocity);
85
86 if (::std::abs(driveTrainState(0, 0) - side_offset) < epsilon) break;
87 if (ShouldExitAuto()) return;
88
89 LOG(DEBUG, "Driving left to %f, right to %f\n",
Brian Silverman7992d6e2013-03-24 19:20:54 -070090 left_initial_position - driveTrainState(0, 0),
91 right_initial_position + driveTrainState(0, 0));
Brian Silverman3b89ed82013-03-22 18:59:16 -070092 control_loops::drivetrain.goal.MakeWithBuilder()
93 .control_loop_driving(true)
94 .highgear(false)
Brian Silverman7992d6e2013-03-24 19:20:54 -070095 .left_goal(left_initial_position - driveTrainState(0, 0))
96 .right_goal(right_initial_position + driveTrainState(0, 0))
97 .left_velocity_goal(-driveTrainState(1, 0))
98 .right_velocity_goal(driveTrainState(1, 0))
Brian Silverman3b89ed82013-03-22 18:59:16 -070099 .Send();
100 }
Brian Silverman7992d6e2013-03-24 19:20:54 -0700101 left_initial_position -= side_offset;
102 right_initial_position += side_offset;
Brian Silverman3b89ed82013-03-22 18:59:16 -0700103 LOG(INFO, "Done moving\n");
104}
105
Austin Schuh80ff2e12014-03-08 12:06:19 -0800106void PositionClawVertically(double intake_power = 0.0, double centering_power = 0.0) {
107 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
108 .bottom_angle(0.0)
109 .separation_angle(0.0)
110 .intake(intake_power)
111 .centering(centering_power)
112 .Send()) {
113 LOG(WARNING, "sending claw goal failed\n");
114 }
115}
Brian Silvermance86bac2013-03-31 19:07:24 -0700116
Austin Schuh80ff2e12014-03-08 12:06:19 -0800117void PositionClawBackIntake() {
118 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
119 .bottom_angle(-2.273474)
120 .separation_angle(0.0)
121 .intake(12.0)
122 .centering(12.0)
123 .Send()) {
124 LOG(WARNING, "sending claw goal failed\n");
125 }
126}
Brian Silvermance86bac2013-03-31 19:07:24 -0700127
Austin Schuh80ff2e12014-03-08 12:06:19 -0800128void PositionClawForShot() {
129 // Turn the claw on, keep it straight up until the ball has been grabbed.
130 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
Brian Silverman31b5b822014-03-14 18:50:39 -0700131 .bottom_angle(0.86)
Austin Schuha4faacc2014-03-09 00:50:50 -0800132 .separation_angle(0.10)
133 .intake(4.0)
Austin Schuh80ff2e12014-03-08 12:06:19 -0800134 .centering(1.0)
135 .Send()) {
136 LOG(WARNING, "sending claw goal failed\n");
137 }
138}
139
140void SetShotPower(double power) {
Austin Schuha4faacc2014-03-09 00:50:50 -0800141 LOG(INFO, "Setting shot power to %f\n", power);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800142 if (!control_loops::shooter_queue_group.goal.MakeWithBuilder()
143 .shot_power(power)
144 .shot_requested(false)
145 .unload_requested(false)
146 .load_requested(false)
147 .Send()) {
148 LOG(WARNING, "sending shooter goal failed\n");
149 }
150}
151
152void WaitUntilDoneOrCanceled(Action *action) {
153 while (true) {
154 // Poll the running bit and auto done bits.
155 ::aos::time::PhasedLoop10MS(5000);
156 if (!action->Running() || ShouldExitAuto()) {
157 return;
158 }
159 }
160}
161
162void Shoot() {
163 // Shoot.
164 auto shoot_action = actions::MakeShootAction();
165 shoot_action->Start();
166 WaitUntilDoneOrCanceled(shoot_action.get());
167}
168
169::std::unique_ptr<TypedAction< ::frc971::actions::DrivetrainActionQueueGroup>>
Brian Silvermanad9e0002014-04-13 14:55:57 -0700170SetDriveGoal(double distance, double maximum_velocity = 1.7, double theta = 0) {
Austin Schuha4faacc2014-03-09 00:50:50 -0800171 LOG(INFO, "Driving to %f\n", distance);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800172 auto drivetrain_action = actions::MakeDrivetrainAction();
173 drivetrain_action->GetGoal()->left_initial_position = left_initial_position;
174 drivetrain_action->GetGoal()->right_initial_position = right_initial_position;
175 drivetrain_action->GetGoal()->y_offset = distance;
Brian Silvermanad9e0002014-04-13 14:55:57 -0700176 drivetrain_action->GetGoal()->theta_offset = theta;
Austin Schuh80ff2e12014-03-08 12:06:19 -0800177 drivetrain_action->GetGoal()->maximum_velocity = maximum_velocity;
178 drivetrain_action->Start();
Brian Silvermanad9e0002014-04-13 14:55:57 -0700179 left_initial_position +=
180 distance - theta * constants::GetValues().turn_width / 2.0;
181 right_initial_position +=
182 distance + theta * constants::GetValues().turn_width / 2. -
183 theta * constants::GetValues().turn_width / 2.00;
Austin Schuh80ff2e12014-03-08 12:06:19 -0800184 return ::std::move(drivetrain_action);
185}
186
187void InitializeEncoders() {
Austin Schuh577edf62014-04-13 10:33:05 -0700188 control_loops::drivetrain.status.FetchLatest();
189 while (!control_loops::drivetrain.status.get()) {
Brian Silverman2c1e0342014-04-11 16:15:01 -0700190 LOG(WARNING,
191 "No previous drivetrain position packet, trying to fetch again\n");
Austin Schuh577edf62014-04-13 10:33:05 -0700192 control_loops::drivetrain.status.FetchNextBlocking();
Brian Silverman3b89ed82013-03-22 18:59:16 -0700193 }
194 left_initial_position =
Austin Schuh577edf62014-04-13 10:33:05 -0700195 control_loops::drivetrain.status->filtered_left_position;
Brian Silverman3b89ed82013-03-22 18:59:16 -0700196 right_initial_position =
Austin Schuh577edf62014-04-13 10:33:05 -0700197 control_loops::drivetrain.status->filtered_right_position;
Brian Silverman3b89ed82013-03-22 18:59:16 -0700198
Austin Schuh80ff2e12014-03-08 12:06:19 -0800199}
200
Austin Schuha4faacc2014-03-09 00:50:50 -0800201void WaitUntilClawDone() {
202 while (true) {
203 // Poll the running bit and auto done bits.
204 ::aos::time::PhasedLoop10MS(5000);
205 control_loops::claw_queue_group.status.FetchLatest();
206 control_loops::claw_queue_group.goal.FetchLatest();
207 if (ShouldExitAuto()) {
208 return;
209 }
210 if (control_loops::claw_queue_group.status.get() == nullptr ||
211 control_loops::claw_queue_group.goal.get() == nullptr) {
212 continue;
213 }
214 bool ans =
215 control_loops::claw_queue_group.status->zeroed &&
216 (::std::abs(control_loops::claw_queue_group.status->bottom_velocity) <
217 1.0) &&
218 (::std::abs(control_loops::claw_queue_group.status->bottom -
219 control_loops::claw_queue_group.goal->bottom_angle) <
220 0.10) &&
221 (::std::abs(control_loops::claw_queue_group.status->separation -
222 control_loops::claw_queue_group.goal->separation_angle) <
223 0.4);
224 if (ans) {
225 return;
226 }
227 }
228}
229
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700230class HotGoalDecoder {
231 public:
232 HotGoalDecoder() {
233 ResetCounts();
234 }
235
236 void ResetCounts() {
237 hot_goal.FetchLatest();
238 if (hot_goal.get()) {
Brian Silverman38ea9bf2014-04-19 22:57:54 -0700239 start_counts_ = *hot_goal;
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700240 LOG_STRUCT(INFO, "counts reset to", start_counts_);
241 start_counts_valid_ = true;
242 } else {
243 LOG(WARNING, "no hot goal message. ignoring\n");
244 start_counts_valid_ = false;
245 }
246 }
247
Brian Silverman38ea9bf2014-04-19 22:57:54 -0700248 void Update(bool block = false) {
249 if (block) {
250 hot_goal.FetchAnother();
251 } else {
252 hot_goal.FetchLatest();
253 }
Brian Silvermanfa577e42014-04-19 12:03:00 -0700254 if (hot_goal.get()) LOG_STRUCT(INFO, "new counts", *hot_goal);
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700255 }
256
Brian Silverman38ea9bf2014-04-19 22:57:54 -0700257 bool left_triggered() const {
258 if (!start_counts_valid_ || !hot_goal.get()) return false;
259 return (hot_goal->left_count - start_counts_.left_count) > kThreshold;
260 }
261
262 bool right_triggered() const {
263 if (!start_counts_valid_ || !hot_goal.get()) return false;
264 return (hot_goal->right_count - start_counts_.right_count) > kThreshold;
265 }
266
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700267 bool is_left() const {
268 if (!start_counts_valid_ || !hot_goal.get()) return false;
269 const uint64_t left_difference =
270 hot_goal->left_count - start_counts_.left_count;
271 const uint64_t right_difference =
272 hot_goal->right_count - start_counts_.right_count;
273 if (left_difference > kThreshold) {
274 if (right_difference > kThreshold) {
Brian Silverman38ea9bf2014-04-19 22:57:54 -0700275 // We've seen a lot of both, so pick the one we've seen the most of.
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700276 return left_difference > right_difference;
277 } else {
278 // We've seen enough left but not enough right, so go with it.
279 return true;
280 }
281 } else {
282 // We haven't seen enough left, so it's not left.
283 return false;
284 }
285 }
286
287 bool is_right() const {
288 if (!start_counts_valid_ || !hot_goal.get()) return false;
289 const uint64_t left_difference =
290 hot_goal->left_count - start_counts_.left_count;
291 const uint64_t right_difference =
292 hot_goal->right_count - start_counts_.right_count;
293 if (right_difference > kThreshold) {
294 if (left_difference > kThreshold) {
Brian Silverman38ea9bf2014-04-19 22:57:54 -0700295 // We've seen a lot of both, so pick the one we've seen the most of.
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700296 return right_difference > left_difference;
297 } else {
298 // We've seen enough right but not enough left, so go with it.
299 return true;
300 }
301 } else {
302 // We haven't seen enough right, so it's not right.
303 return false;
304 }
305 }
306
307 private:
Brian Silverman38ea9bf2014-04-19 22:57:54 -0700308 static const uint64_t kThreshold = 5;
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700309
310 ::frc971::HotGoal start_counts_;
311 bool start_counts_valid_;
312};
313
Austin Schuh80ff2e12014-03-08 12:06:19 -0800314void HandleAuto() {
Brian Silverman45ceeb52014-04-17 15:15:18 -0700315 enum class AutoVersion : uint8_t {
316 kStraight,
317 kDoubleHot,
Brian Silverman38ea9bf2014-04-19 22:57:54 -0700318 kSingleHot,
Brian Silverman45ceeb52014-04-17 15:15:18 -0700319 };
320
Austin Schuha4faacc2014-03-09 00:50:50 -0800321 // The front of the robot is 1.854 meters from the wall
Brian Silvermanad9e0002014-04-13 14:55:57 -0700322 static const double kShootDistance = 3.15;
323 static const double kPickupDistance = 0.5;
324 static const double kTurnAngle = 0.3;
Brian Silverman45ceeb52014-04-17 15:15:18 -0700325
Austin Schuh577edf62014-04-13 10:33:05 -0700326 ::aos::time::Time start_time = ::aos::time::Time::Now();
Austin Schuh80ff2e12014-03-08 12:06:19 -0800327 LOG(INFO, "Handling auto mode\n");
Brian Silvermanb36ae1f2014-04-17 15:13:41 -0700328
Brian Silverman45ceeb52014-04-17 15:15:18 -0700329 AutoVersion auto_version;
330 ::frc971::sensors::auto_mode.FetchLatest();
331 if (!::frc971::sensors::auto_mode.get()) {
332 LOG(WARNING, "not sure which auto mode to use\n");
333 auto_version = AutoVersion::kStraight;
334 } else {
Brian Silverman38ea9bf2014-04-19 22:57:54 -0700335 static const double kSelectorMin = 0.2, kSelectorMax = 4.4;
336
337 const double kSelectorStep = (kSelectorMax - kSelectorMin) / 3.0;
338 if (::frc971::sensors::auto_mode->voltage < kSelectorStep + kSelectorMin) {
339 auto_version = AutoVersion::kSingleHot;
340 } else if (::frc971::sensors::auto_mode->voltage <
341 2 * kSelectorStep + kSelectorMin) {
342 auto_version = AutoVersion::kStraight;
343 } else {
344 auto_version = AutoVersion::kDoubleHot;
345 }
Brian Silverman45ceeb52014-04-17 15:15:18 -0700346 }
347 LOG(INFO, "running auto %" PRIu8 "\n", auto_version);
348
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700349 HotGoalDecoder hot_goal_decoder;
350 // True for left, false for right.
351 bool first_shot_left, second_shot_left_default, second_shot_left;
Brian Silverman6f621542014-04-06 16:00:41 -0700352
Austin Schuh80ff2e12014-03-08 12:06:19 -0800353 ResetDrivetrain();
354
355 if (ShouldExitAuto()) return;
356 InitializeEncoders();
357
358 // Turn the claw on, keep it straight up until the ball has been grabbed.
Austin Schuh577edf62014-04-13 10:33:05 -0700359 LOG(INFO, "Claw going up at %f\n",
360 (::aos::time::Time::Now() - start_time).ToSeconds());
Austin Schuh80ff2e12014-03-08 12:06:19 -0800361 PositionClawVertically(12.0, 4.0);
Austin Schuha4faacc2014-03-09 00:50:50 -0800362 SetShotPower(115.0);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800363
364 // Wait for the ball to enter the claw.
Austin Schuha4faacc2014-03-09 00:50:50 -0800365 time::SleepFor(time::Time::InSeconds(0.25));
Austin Schuh80ff2e12014-03-08 12:06:19 -0800366 if (ShouldExitAuto()) return;
Austin Schuh577edf62014-04-13 10:33:05 -0700367 LOG(INFO, "Readying claw for shot at %f\n",
368 (::aos::time::Time::Now() - start_time).ToSeconds());
Austin Schuh80ff2e12014-03-08 12:06:19 -0800369
370 {
371 if (ShouldExitAuto()) return;
372 // Drive to the goal.
Brian Silvermanb94069c2014-04-17 14:34:24 -0700373 auto drivetrain_action = SetDriveGoal(-kShootDistance, 2.5);
Austin Schuha4faacc2014-03-09 00:50:50 -0800374 time::SleepFor(time::Time::InSeconds(0.75));
375 PositionClawForShot();
376 LOG(INFO, "Waiting until drivetrain is finished\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800377 WaitUntilDoneOrCanceled(drivetrain_action.get());
378 if (ShouldExitAuto()) return;
379 }
Brian Silvermanad9e0002014-04-13 14:55:57 -0700380
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700381 hot_goal_decoder.Update();
382 if (hot_goal_decoder.is_left()) {
383 LOG(INFO, "first shot left\n");
384 first_shot_left = true;
385 second_shot_left_default = false;
386 } else if (hot_goal_decoder.is_right()) {
387 LOG(INFO, "first shot right\n");
388 first_shot_left = false;
389 second_shot_left_default = true;
390 } else {
391 LOG(INFO, "first shot defaulting left\n");
392 first_shot_left = true;
393 second_shot_left_default = true;
394 }
395 if (auto_version == AutoVersion::kDoubleHot) {
Brian Silvermanad9e0002014-04-13 14:55:57 -0700396 if (ShouldExitAuto()) return;
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700397 auto drivetrain_action =
Brian Silvermanfa577e42014-04-19 12:03:00 -0700398 SetDriveGoal(0, 2, first_shot_left ? kTurnAngle : -kTurnAngle);
Brian Silvermanad9e0002014-04-13 14:55:57 -0700399 WaitUntilDoneOrCanceled(drivetrain_action.get());
400 if (ShouldExitAuto()) return;
Brian Silverman38ea9bf2014-04-19 22:57:54 -0700401 } else if (auto_version == AutoVersion::kSingleHot) {
402 do {
Brian Silvermanaf4a5852014-04-23 22:27:59 -0500403 // TODO(brians): Wait for next message with timeout or something.
404 ::aos::time::SleepFor(::aos::time::Time::InSeconds(0.003));
405 hot_goal_decoder.Update(false);
Brian Silverman38ea9bf2014-04-19 22:57:54 -0700406 if (ShouldExitAuto()) return;
407 } while (!hot_goal_decoder.left_triggered() &&
408 (::aos::time::Time::Now() - start_time) <
409 ::aos::time::Time::InSeconds(9));
Brian Silvermanad9e0002014-04-13 14:55:57 -0700410 }
Austin Schuh80ff2e12014-03-08 12:06:19 -0800411
412 // Shoot.
Brian Silvermanad9e0002014-04-13 14:55:57 -0700413 LOG(INFO, "Shooting at %f\n",
414 (::aos::time::Time::Now() - start_time).ToSeconds());
Austin Schuh80ff2e12014-03-08 12:06:19 -0800415 Shoot();
Austin Schuh577edf62014-04-13 10:33:05 -0700416 time::SleepFor(time::Time::InSeconds(0.05));
Austin Schuh80ff2e12014-03-08 12:06:19 -0800417
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700418 if (auto_version == AutoVersion::kDoubleHot) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800419 if (ShouldExitAuto()) return;
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700420 auto drivetrain_action =
Brian Silvermanfa577e42014-04-19 12:03:00 -0700421 SetDriveGoal(0, 2, first_shot_left ? -kTurnAngle : kTurnAngle);
Brian Silvermanad9e0002014-04-13 14:55:57 -0700422 WaitUntilDoneOrCanceled(drivetrain_action.get());
423 if (ShouldExitAuto()) return;
Brian Silverman38ea9bf2014-04-19 22:57:54 -0700424 } else if (auto_version == AutoVersion::kSingleHot) {
425 LOG(INFO, "auto done at %f\n",
426 (::aos::time::Time::Now() - start_time).ToSeconds());
427 PositionClawVertically(0.0, 0.0);
428 return;
Brian Silvermanad9e0002014-04-13 14:55:57 -0700429 }
430
431 {
432 if (ShouldExitAuto()) return;
Austin Schuh80ff2e12014-03-08 12:06:19 -0800433 // Intake the new ball.
Austin Schuh577edf62014-04-13 10:33:05 -0700434 LOG(INFO, "Claw ready for intake at %f\n",
435 (::aos::time::Time::Now() - start_time).ToSeconds());
Austin Schuh80ff2e12014-03-08 12:06:19 -0800436 PositionClawBackIntake();
Brian Silvermanb94069c2014-04-17 14:34:24 -0700437 auto drivetrain_action =
438 SetDriveGoal(kShootDistance + kPickupDistance, 2.5);
Austin Schuha4faacc2014-03-09 00:50:50 -0800439 LOG(INFO, "Waiting until drivetrain is finished\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800440 WaitUntilDoneOrCanceled(drivetrain_action.get());
441 if (ShouldExitAuto()) return;
Austin Schuh577edf62014-04-13 10:33:05 -0700442 LOG(INFO, "Wait for the claw at %f\n",
443 (::aos::time::Time::Now() - start_time).ToSeconds());
Austin Schuha4faacc2014-03-09 00:50:50 -0800444 WaitUntilClawDone();
445 if (ShouldExitAuto()) return;
Austin Schuh80ff2e12014-03-08 12:06:19 -0800446 }
447
448 // Drive back.
449 {
Austin Schuh577edf62014-04-13 10:33:05 -0700450 LOG(INFO, "Driving back at %f\n",
451 (::aos::time::Time::Now() - start_time).ToSeconds());
Brian Silvermanb94069c2014-04-17 14:34:24 -0700452 auto drivetrain_action =
453 SetDriveGoal(-(kShootDistance + kPickupDistance), 2.5);
Austin Schuh577edf62014-04-13 10:33:05 -0700454 time::SleepFor(time::Time::InSeconds(0.3));
Brian Silverman38ea9bf2014-04-19 22:57:54 -0700455 hot_goal_decoder.ResetCounts();
Austin Schuh80ff2e12014-03-08 12:06:19 -0800456 if (ShouldExitAuto()) return;
457 PositionClawForShot();
Austin Schuha4faacc2014-03-09 00:50:50 -0800458 LOG(INFO, "Waiting until drivetrain is finished\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800459 WaitUntilDoneOrCanceled(drivetrain_action.get());
Austin Schuha4faacc2014-03-09 00:50:50 -0800460 WaitUntilClawDone();
Austin Schuh80ff2e12014-03-08 12:06:19 -0800461 if (ShouldExitAuto()) return;
462 }
463
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700464 hot_goal_decoder.Update();
465 if (hot_goal_decoder.is_left()) {
466 LOG(INFO, "second shot left\n");
467 second_shot_left = true;
468 } else if (hot_goal_decoder.is_right()) {
469 LOG(INFO, "second shot right\n");
470 second_shot_left = false;
471 } else {
472 LOG(INFO, "second shot defaulting %s\n",
473 second_shot_left_default ? "left" : "right");
474 second_shot_left = second_shot_left_default;
475 }
476 if (auto_version == AutoVersion::kDoubleHot) {
Brian Silvermanad9e0002014-04-13 14:55:57 -0700477 if (ShouldExitAuto()) return;
Brian Silverman4ac1cb82014-04-17 16:00:06 -0700478 auto drivetrain_action =
Brian Silvermanfa577e42014-04-19 12:03:00 -0700479 SetDriveGoal(0, 2, second_shot_left ? kTurnAngle : -kTurnAngle);
Brian Silvermanad9e0002014-04-13 14:55:57 -0700480 WaitUntilDoneOrCanceled(drivetrain_action.get());
481 if (ShouldExitAuto()) return;
482 }
483
Austin Schuh577edf62014-04-13 10:33:05 -0700484 LOG(INFO, "Shooting at %f\n",
485 (::aos::time::Time::Now() - start_time).ToSeconds());
Austin Schuh80ff2e12014-03-08 12:06:19 -0800486 // Shoot
487 Shoot();
488 if (ShouldExitAuto()) return;
489
490 // Get ready to zero when we come back up.
Austin Schuh577edf62014-04-13 10:33:05 -0700491 time::SleepFor(time::Time::InSeconds(0.05));
Austin Schuh80ff2e12014-03-08 12:06:19 -0800492 PositionClawVertically(0.0, 0.0);
Austin Schuh47017412013-03-10 11:50:46 -0700493}
494
495} // namespace autonomous
496} // namespace frc971