blob: 7d1eb3f1e253f17fc2ea0965f4b5b4b75dfcf1b5 [file] [log] [blame]
Austin Schuhbfb04122019-05-22 21:16:51 -07001#include "y2014/actors/autonomous_actor.h"
2
3#include <stdio.h>
4
5#include <chrono>
6#include <memory>
7
8#include "aos/actions/actions.h"
9#include "aos/logging/logging.h"
Austin Schuhbfb04122019-05-22 21:16:51 -070010#include "aos/time/time.h"
11#include "aos/util/phased_loop.h"
Austin Schuhbfb04122019-05-22 21:16:51 -070012#include "y2014/actors/shoot_actor.h"
13#include "y2014/constants.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070014#include "y2014/control_loops/claw/claw_goal_generated.h"
15#include "y2014/control_loops/claw/claw_status_generated.h"
Austin Schuhbfb04122019-05-22 21:16:51 -070016#include "y2014/control_loops/drivetrain/drivetrain_base.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070017#include "y2014/control_loops/shooter/shooter_goal_generated.h"
18#include "y2014/queues/auto_mode_generated.h"
19#include "y2014/queues/hot_goal_generated.h"
Austin Schuhbfb04122019-05-22 21:16:51 -070020
21namespace y2014 {
22namespace actors {
23
24namespace chrono = ::std::chrono;
25namespace this_thread = ::std::this_thread;
26using ::aos::monotonic_clock;
Alex Perrycb7da4b2019-08-28 19:35:56 -070027using ::frc971::ProfileParametersT;
Austin Schuhbfb04122019-05-22 21:16:51 -070028
Austin Schuh1bf8a212019-05-26 22:13:14 -070029AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
Austin Schuhbfb04122019-05-22 21:16:51 -070030 : frc971::autonomous::BaseAutonomousActor(
Austin Schuh1bf8a212019-05-26 22:13:14 -070031 event_loop, control_loops::GetDrivetrainConfig()),
Alex Perrycb7da4b2019-08-28 19:35:56 -070032 auto_mode_fetcher_(
33 event_loop->MakeFetcher<::y2014::sensors::AutoMode>("/aos")),
34 hot_goal_fetcher_(event_loop->MakeFetcher<::y2014::HotGoal>("/")),
Austin Schuhb2461f42019-06-29 18:17:06 -070035 claw_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070036 event_loop->MakeSender<::y2014::control_loops::claw::Goal>("/claw")),
Austin Schuhb2461f42019-06-29 18:17:06 -070037 claw_goal_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070038 event_loop->MakeFetcher<::y2014::control_loops::claw::Goal>("/claw")),
Austin Schuhb2461f42019-06-29 18:17:06 -070039 claw_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070040 event_loop->MakeFetcher<::y2014::control_loops::claw::Status>(
41 "/claw")),
Austin Schuhb2461f42019-06-29 18:17:06 -070042 shooter_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070043 event_loop->MakeSender<::y2014::control_loops::shooter::Goal>(
44 "/shooter")),
Austin Schuh1bf8a212019-05-26 22:13:14 -070045 shoot_action_factory_(actors::ShootActor::MakeFactory(event_loop)) {}
Austin Schuhbfb04122019-05-22 21:16:51 -070046
47void AutonomousActor::PositionClawVertically(double intake_power,
48 double centering_power) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070049 auto builder = claw_goal_sender_.MakeBuilder();
50 control_loops::claw::Goal::Builder goal_builder =
51 builder.MakeBuilder<control_loops::claw::Goal>();
52 goal_builder.add_bottom_angle(0.0);
53 goal_builder.add_separation_angle(0.0);
54 goal_builder.add_intake(intake_power);
55 goal_builder.add_centering(centering_power);
Austin Schuhb2461f42019-06-29 18:17:06 -070056
Alex Perrycb7da4b2019-08-28 19:35:56 -070057 if (!builder.Send(goal_builder.Finish())) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070058 AOS_LOG(WARNING, "sending claw goal failed\n");
Austin Schuhbfb04122019-05-22 21:16:51 -070059 }
60}
61
62void AutonomousActor::PositionClawBackIntake() {
Alex Perrycb7da4b2019-08-28 19:35:56 -070063 auto builder = claw_goal_sender_.MakeBuilder();
64 control_loops::claw::Goal::Builder goal_builder =
65 builder.MakeBuilder<control_loops::claw::Goal>();
66 goal_builder.add_bottom_angle(-2.273474);
67 goal_builder.add_separation_angle(0.0);
68 goal_builder.add_intake(12.0);
69 goal_builder.add_centering(12.0);
70 if (!builder.Send(goal_builder.Finish())) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070071 AOS_LOG(WARNING, "sending claw goal failed\n");
Austin Schuhbfb04122019-05-22 21:16:51 -070072 }
73}
74
75void AutonomousActor::PositionClawUpClosed() {
76 // Move the claw to where we're going to shoot from but keep it closed until
77 // it gets there.
Alex Perrycb7da4b2019-08-28 19:35:56 -070078 auto builder = claw_goal_sender_.MakeBuilder();
79 control_loops::claw::Goal::Builder goal_builder =
80 builder.MakeBuilder<control_loops::claw::Goal>();
81 goal_builder.add_bottom_angle(0.86);
82 goal_builder.add_separation_angle(0.0);
83 goal_builder.add_intake(4.0);
84 goal_builder.add_centering(1.0);
85 if (!builder.Send(goal_builder.Finish())) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070086 AOS_LOG(WARNING, "sending claw goal failed\n");
Austin Schuhbfb04122019-05-22 21:16:51 -070087 }
88}
89
90void AutonomousActor::PositionClawForShot() {
Alex Perrycb7da4b2019-08-28 19:35:56 -070091 auto builder = claw_goal_sender_.MakeBuilder();
92 control_loops::claw::Goal::Builder goal_builder =
93 builder.MakeBuilder<control_loops::claw::Goal>();
94 goal_builder.add_bottom_angle(0.86);
95 goal_builder.add_separation_angle(0.10);
96 goal_builder.add_intake(4.0);
97 goal_builder.add_centering(1.0);
98 if (!builder.Send(goal_builder.Finish())) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070099 AOS_LOG(WARNING, "sending claw goal failed\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700100 }
101}
102
103void AutonomousActor::SetShotPower(double power) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700104 AOS_LOG(INFO, "Setting shot power to %f\n", power);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700105 auto builder = shooter_goal_sender_.MakeBuilder();
106 control_loops::shooter::Goal::Builder goal_builder =
107 builder.MakeBuilder<control_loops::shooter::Goal>();
108 goal_builder.add_shot_power(power);
109 goal_builder.add_shot_requested(false);
110 goal_builder.add_unload_requested(false);
111 goal_builder.add_load_requested(false);
112 if (!builder.Send(goal_builder.Finish())) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700113 AOS_LOG(WARNING, "sending shooter goal failed\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700114 }
115}
116
Alex Perrycb7da4b2019-08-28 19:35:56 -0700117ProfileParametersT MakeProfileParameters(float max_velocity,
118 float max_acceleration) {
119 ProfileParametersT result;
120 result.max_velocity = max_velocity;
121 result.max_acceleration = max_acceleration;
122 return result;
123}
124
125const ProfileParametersT kFastDrive = MakeProfileParameters(3.0, 2.5);
126const ProfileParametersT kSlowDrive = MakeProfileParameters(2.5, 2.5);
127const ProfileParametersT kFastWithBallDrive = MakeProfileParameters(3.0, 2.0);
128const ProfileParametersT kSlowWithBallDrive = MakeProfileParameters(2.5, 2.0);
129const ProfileParametersT kFastTurn = MakeProfileParameters(3.0, 10.0);
Austin Schuhbfb04122019-05-22 21:16:51 -0700130
131void AutonomousActor::Shoot() {
132 // Shoot.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700133 aos::common::actions::DoubleParamT param;
134 auto shoot_action = shoot_action_factory_.Make(param);
Austin Schuhbfb04122019-05-22 21:16:51 -0700135 shoot_action->Start();
136 WaitUntilDoneOrCanceled(::std::move(shoot_action));
137}
138
139bool AutonomousActor::WaitUntilClawDone() {
140 while (true) {
141 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(10),
Austin Schuhd32b3622019-06-23 18:49:06 -0700142 event_loop()->monotonic_now(),
Austin Schuhbfb04122019-05-22 21:16:51 -0700143 ::std::chrono::milliseconds(10) / 2);
144 // Poll the running bit and auto done bits.
145 phased_loop.SleepUntilNext();
Austin Schuhb2461f42019-06-29 18:17:06 -0700146 claw_status_fetcher_.Fetch();
147 claw_goal_fetcher_.Fetch();
Austin Schuhbfb04122019-05-22 21:16:51 -0700148 if (ShouldCancel()) {
149 return false;
150 }
Austin Schuhb2461f42019-06-29 18:17:06 -0700151 if (claw_status_fetcher_.get() == nullptr ||
152 claw_goal_fetcher_.get() == nullptr) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700153 continue;
154 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700155 bool ans = claw_status_fetcher_->zeroed() &&
156 (::std::abs(claw_status_fetcher_->bottom_velocity()) < 1.0) &&
157 (::std::abs(claw_status_fetcher_->bottom() -
158 claw_goal_fetcher_->bottom_angle()) < 0.10) &&
159 (::std::abs(claw_status_fetcher_->separation() -
160 claw_goal_fetcher_->separation_angle()) < 0.4);
Austin Schuhbfb04122019-05-22 21:16:51 -0700161 if (ans) {
162 return true;
163 }
164 }
165}
166
167class HotGoalDecoder {
168 public:
Austin Schuha3e576b2019-05-22 21:22:23 -0700169 HotGoalDecoder(::aos::Fetcher<::y2014::HotGoal> *hot_goal_fetcher)
170 : hot_goal_fetcher_(hot_goal_fetcher) {
171 ResetCounts();
172 }
Austin Schuhbfb04122019-05-22 21:16:51 -0700173
174 void ResetCounts() {
Austin Schuha3e576b2019-05-22 21:22:23 -0700175 hot_goal_fetcher_->Fetch();
176 if (hot_goal_fetcher_->get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700177 hot_goal_fetcher_->get()->UnPackTo(&start_counts_);
Austin Schuhbfb04122019-05-22 21:16:51 -0700178 start_counts_valid_ = true;
179 } else {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700180 AOS_LOG(WARNING, "no hot goal message. ignoring\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700181 start_counts_valid_ = false;
182 }
183 }
184
Alex Perrycb7da4b2019-08-28 19:35:56 -0700185 void Update() { hot_goal_fetcher_->Fetch(); }
Austin Schuhbfb04122019-05-22 21:16:51 -0700186
187 bool left_triggered() const {
Austin Schuha3e576b2019-05-22 21:22:23 -0700188 if (!start_counts_valid_ || !hot_goal_fetcher_->get()) return false;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700189 return (hot_goal_fetcher_->get()->left_count() - start_counts_.left_count) >
Austin Schuha3e576b2019-05-22 21:22:23 -0700190 kThreshold;
Austin Schuhbfb04122019-05-22 21:16:51 -0700191 }
192
193 bool right_triggered() const {
Austin Schuha3e576b2019-05-22 21:22:23 -0700194 if (!start_counts_valid_ || !hot_goal_fetcher_->get()) return false;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700195 return (hot_goal_fetcher_->get()->right_count() -
196 start_counts_.right_count) > kThreshold;
Austin Schuhbfb04122019-05-22 21:16:51 -0700197 }
198
199 bool is_left() const {
Austin Schuha3e576b2019-05-22 21:22:23 -0700200 if (!start_counts_valid_ || !hot_goal_fetcher_->get()) return false;
Austin Schuhbfb04122019-05-22 21:16:51 -0700201 const uint64_t left_difference =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700202 hot_goal_fetcher_->get()->left_count() - start_counts_.left_count;
Austin Schuhbfb04122019-05-22 21:16:51 -0700203 const uint64_t right_difference =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700204 hot_goal_fetcher_->get()->right_count() - start_counts_.right_count;
Austin Schuhbfb04122019-05-22 21:16:51 -0700205 if (left_difference > kThreshold) {
206 if (right_difference > kThreshold) {
207 // We've seen a lot of both, so pick the one we've seen the most of.
208 return left_difference > right_difference;
209 } else {
210 // We've seen enough left but not enough right, so go with it.
211 return true;
212 }
213 } else {
214 // We haven't seen enough left, so it's not left.
215 return false;
216 }
217 }
218
219 bool is_right() const {
Austin Schuha3e576b2019-05-22 21:22:23 -0700220 if (!start_counts_valid_ || !hot_goal_fetcher_->get()) return false;
Austin Schuhbfb04122019-05-22 21:16:51 -0700221 const uint64_t left_difference =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700222 hot_goal_fetcher_->get()->left_count() - start_counts_.left_count;
Austin Schuhbfb04122019-05-22 21:16:51 -0700223 const uint64_t right_difference =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700224 hot_goal_fetcher_->get()->right_count() - start_counts_.right_count;
Austin Schuhbfb04122019-05-22 21:16:51 -0700225 if (right_difference > kThreshold) {
226 if (left_difference > kThreshold) {
227 // We've seen a lot of both, so pick the one we've seen the most of.
228 return right_difference > left_difference;
229 } else {
230 // We've seen enough right but not enough left, so go with it.
231 return true;
232 }
233 } else {
234 // We haven't seen enough right, so it's not right.
235 return false;
236 }
237 }
238
239 private:
240 static const uint64_t kThreshold = 5;
241
Alex Perrycb7da4b2019-08-28 19:35:56 -0700242 ::y2014::HotGoalT start_counts_;
Austin Schuhbfb04122019-05-22 21:16:51 -0700243 bool start_counts_valid_;
Austin Schuha3e576b2019-05-22 21:22:23 -0700244
245 ::aos::Fetcher<::y2014::HotGoal> *hot_goal_fetcher_;
Austin Schuhbfb04122019-05-22 21:16:51 -0700246};
247
248bool AutonomousActor::RunAction(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700249 const ::frc971::autonomous::AutonomousActionParams * /*params*/) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700250 enum class AutoVersion : uint8_t {
251 kStraight,
252 kDoubleHot,
253 kSingleHot,
254 };
255
256 // The front of the robot is 1.854 meters from the wall
257 static const double kShootDistance = 3.15;
258 static const double kPickupDistance = 0.5;
259 static const double kTurnAngle = 0.3;
260
Austin Schuh77ed5432019-07-07 20:41:36 -0700261 const monotonic_clock::time_point start_time = monotonic_now();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700262 AOS_LOG(INFO, "Handling auto mode\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700263
264 AutoVersion auto_version;
Austin Schuh7eed2de2019-05-25 14:34:40 -0700265 auto_mode_fetcher_.Fetch();
266 if (!auto_mode_fetcher_.get()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700267 AOS_LOG(WARNING, "not sure which auto mode to use\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700268 auto_version = AutoVersion::kStraight;
269 } else {
270 static const double kSelectorMin = 0.2, kSelectorMax = 4.4;
271
272 const double kSelectorStep = (kSelectorMax - kSelectorMin) / 3.0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700273 if (auto_mode_fetcher_->voltage() < kSelectorStep + kSelectorMin) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700274 auto_version = AutoVersion::kSingleHot;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700275 } else if (auto_mode_fetcher_->voltage() < 2 * kSelectorStep + kSelectorMin) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700276 auto_version = AutoVersion::kStraight;
277 } else {
278 auto_version = AutoVersion::kDoubleHot;
279 }
280 }
Austin Schuhf257f3c2019-10-27 21:00:43 -0700281 AOS_LOG(INFO, "running auto %" PRIu8 "\n",
282 static_cast<uint8_t>(auto_version));
Austin Schuhbfb04122019-05-22 21:16:51 -0700283
Alex Perrycb7da4b2019-08-28 19:35:56 -0700284 const ProfileParametersT drive_params =
Austin Schuhbfb04122019-05-22 21:16:51 -0700285 (auto_version == AutoVersion::kStraight) ? kFastDrive : kSlowDrive;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700286 const ProfileParametersT drive_with_ball_params =
Austin Schuhbfb04122019-05-22 21:16:51 -0700287 (auto_version == AutoVersion::kStraight) ? kFastWithBallDrive
288 : kSlowWithBallDrive;
289
Austin Schuha3e576b2019-05-22 21:22:23 -0700290 HotGoalDecoder hot_goal_decoder(&hot_goal_fetcher_);
Austin Schuhbfb04122019-05-22 21:16:51 -0700291 // True for left, false for right.
292 bool first_shot_left, second_shot_left_default, second_shot_left;
293
294 Reset();
295
296 // Turn the claw on, keep it straight up until the ball has been grabbed.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700297 AOS_LOG(INFO, "Claw going up at %f\n",
298 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuhbfb04122019-05-22 21:16:51 -0700299 PositionClawVertically(12.0, 4.0);
300 SetShotPower(115.0);
301
302 // Wait for the ball to enter the claw.
303 this_thread::sleep_for(chrono::milliseconds(250));
304 if (ShouldCancel()) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700305 AOS_LOG(INFO, "Readying claw for shot at %f\n",
306 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuhbfb04122019-05-22 21:16:51 -0700307
308 if (ShouldCancel()) return true;
309 // Drive to the goal.
310 StartDrive(-kShootDistance, 0.0, drive_params, kFastTurn);
311 this_thread::sleep_for(chrono::milliseconds(750));
312 PositionClawForShot();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700313 AOS_LOG(INFO, "Waiting until drivetrain is finished\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700314 WaitForDriveProfileDone();
315 if (ShouldCancel()) return true;
316
317 hot_goal_decoder.Update();
318 if (hot_goal_decoder.is_left()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700319 AOS_LOG(INFO, "first shot left\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700320 first_shot_left = true;
321 second_shot_left_default = false;
322 } else if (hot_goal_decoder.is_right()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700323 AOS_LOG(INFO, "first shot right\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700324 first_shot_left = false;
325 second_shot_left_default = true;
326 } else {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700327 AOS_LOG(INFO, "first shot defaulting left\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700328 first_shot_left = true;
329 second_shot_left_default = true;
330 }
331 if (auto_version == AutoVersion::kDoubleHot) {
332 if (ShouldCancel()) return true;
333 StartDrive(0, first_shot_left ? kTurnAngle : -kTurnAngle,
334 drive_with_ball_params, kFastTurn);
335 WaitForDriveProfileDone();
336 if (ShouldCancel()) return true;
337 } else if (auto_version == AutoVersion::kSingleHot) {
338 do {
339 // TODO(brians): Wait for next message with timeout or something.
340 this_thread::sleep_for(chrono::milliseconds(3));
Austin Schuha3e576b2019-05-22 21:22:23 -0700341 hot_goal_decoder.Update();
Austin Schuhbfb04122019-05-22 21:16:51 -0700342 if (ShouldCancel()) return true;
343 } while (!hot_goal_decoder.left_triggered() &&
Austin Schuh77ed5432019-07-07 20:41:36 -0700344 (monotonic_now() - start_time) < chrono::seconds(9));
Austin Schuhbfb04122019-05-22 21:16:51 -0700345 } else if (auto_version == AutoVersion::kStraight) {
346 this_thread::sleep_for(chrono::milliseconds(400));
347 }
348
349 // Shoot.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700350 AOS_LOG(INFO, "Shooting at %f\n",
351 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuhbfb04122019-05-22 21:16:51 -0700352 Shoot();
353 this_thread::sleep_for(chrono::milliseconds(50));
354
355 if (auto_version == AutoVersion::kDoubleHot) {
356 if (ShouldCancel()) return true;
357 StartDrive(0, first_shot_left ? -kTurnAngle : kTurnAngle,
358 drive_with_ball_params, kFastTurn);
359 WaitForDriveProfileDone();
360 if (ShouldCancel()) return true;
361 } else if (auto_version == AutoVersion::kSingleHot) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700362 AOS_LOG(INFO, "auto done at %f\n",
363 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuhbfb04122019-05-22 21:16:51 -0700364 PositionClawVertically(0.0, 0.0);
365 return true;
366 }
367
368 {
369 if (ShouldCancel()) return true;
370 // Intake the new ball.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700371 AOS_LOG(INFO, "Claw ready for intake at %f\n",
372 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuhbfb04122019-05-22 21:16:51 -0700373 PositionClawBackIntake();
374 StartDrive(kShootDistance + kPickupDistance, 0.0, drive_params, kFastTurn);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700375 AOS_LOG(INFO, "Waiting until drivetrain is finished\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700376 WaitForDriveProfileDone();
377 if (ShouldCancel()) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700378 AOS_LOG(INFO, "Wait for the claw at %f\n",
379 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuhbfb04122019-05-22 21:16:51 -0700380 if (!WaitUntilClawDone()) return true;
381 }
382
383 // Drive back.
384 {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700385 AOS_LOG(INFO, "Driving back at %f\n",
386 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuhbfb04122019-05-22 21:16:51 -0700387 StartDrive(-(kShootDistance + kPickupDistance), 0.0, drive_params,
388 kFastTurn);
389 this_thread::sleep_for(chrono::milliseconds(300));
390 hot_goal_decoder.ResetCounts();
391 if (ShouldCancel()) return true;
392 PositionClawUpClosed();
393 if (!WaitUntilClawDone()) return true;
394 PositionClawForShot();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700395 AOS_LOG(INFO, "Waiting until drivetrain is finished\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700396 WaitForDriveProfileDone();
397 if (ShouldCancel()) return true;
398 if (!WaitUntilClawDone()) return true;
399 }
400
401 hot_goal_decoder.Update();
402 if (hot_goal_decoder.is_left()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700403 AOS_LOG(INFO, "second shot left\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700404 second_shot_left = true;
405 } else if (hot_goal_decoder.is_right()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700406 AOS_LOG(INFO, "second shot right\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700407 second_shot_left = false;
408 } else {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700409 AOS_LOG(INFO, "second shot defaulting %s\n",
410 second_shot_left_default ? "left" : "right");
Austin Schuhbfb04122019-05-22 21:16:51 -0700411 second_shot_left = second_shot_left_default;
412 }
413 if (auto_version == AutoVersion::kDoubleHot) {
414 if (ShouldCancel()) return true;
415 StartDrive(0, second_shot_left ? kTurnAngle : -kTurnAngle, drive_params,
416 kFastTurn);
417 WaitForDriveProfileDone();
418 if (ShouldCancel()) return true;
419 } else if (auto_version == AutoVersion::kStraight) {
420 this_thread::sleep_for(chrono::milliseconds(400));
421 }
422
Austin Schuhf257f3c2019-10-27 21:00:43 -0700423 AOS_LOG(INFO, "Shooting at %f\n",
424 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuhbfb04122019-05-22 21:16:51 -0700425 // Shoot
426 Shoot();
427 if (ShouldCancel()) return true;
428
429 // Get ready to zero when we come back up.
430 this_thread::sleep_for(chrono::milliseconds(50));
431 PositionClawVertically(0.0, 0.0);
432 return true;
433}
434
435} // namespace actors
436} // namespace y2014