blob: 07c68550ebedf3c04b166b9337ecf2ab1c2ea52f [file] [log] [blame]
Austin Schuhbfb04122019-05-22 21:16:51 -07001#include "y2014/actors/autonomous_actor.h"
2
Austin Schuhbfb04122019-05-22 21:16:51 -07003#include <chrono>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07004#include <cstdio>
Austin Schuhbfb04122019-05-22 21:16:51 -07005#include <memory>
6
7#include "aos/actions/actions.h"
8#include "aos/logging/logging.h"
Austin Schuhbfb04122019-05-22 21:16:51 -07009#include "aos/time/time.h"
10#include "aos/util/phased_loop.h"
Austin Schuhbfb04122019-05-22 21:16:51 -070011#include "y2014/actors/shoot_actor.h"
12#include "y2014/constants.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070013#include "y2014/control_loops/claw/claw_goal_generated.h"
14#include "y2014/control_loops/claw/claw_status_generated.h"
Austin Schuhbfb04122019-05-22 21:16:51 -070015#include "y2014/control_loops/drivetrain/drivetrain_base.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070016#include "y2014/control_loops/shooter/shooter_goal_generated.h"
17#include "y2014/queues/auto_mode_generated.h"
18#include "y2014/queues/hot_goal_generated.h"
Austin Schuhbfb04122019-05-22 21:16:51 -070019
20namespace y2014 {
21namespace actors {
22
23namespace chrono = ::std::chrono;
24namespace this_thread = ::std::this_thread;
25using ::aos::monotonic_clock;
Alex Perrycb7da4b2019-08-28 19:35:56 -070026using ::frc971::ProfileParametersT;
Austin Schuhbfb04122019-05-22 21:16:51 -070027
Austin Schuh1bf8a212019-05-26 22:13:14 -070028AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
Austin Schuhbfb04122019-05-22 21:16:51 -070029 : frc971::autonomous::BaseAutonomousActor(
Austin Schuh1bf8a212019-05-26 22:13:14 -070030 event_loop, control_loops::GetDrivetrainConfig()),
Alex Perrycb7da4b2019-08-28 19:35:56 -070031 auto_mode_fetcher_(
32 event_loop->MakeFetcher<::y2014::sensors::AutoMode>("/aos")),
33 hot_goal_fetcher_(event_loop->MakeFetcher<::y2014::HotGoal>("/")),
Austin Schuhb2461f42019-06-29 18:17:06 -070034 claw_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070035 event_loop->MakeSender<::y2014::control_loops::claw::Goal>("/claw")),
Austin Schuhb2461f42019-06-29 18:17:06 -070036 claw_goal_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070037 event_loop->MakeFetcher<::y2014::control_loops::claw::Goal>("/claw")),
Austin Schuhb2461f42019-06-29 18:17:06 -070038 claw_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070039 event_loop->MakeFetcher<::y2014::control_loops::claw::Status>(
40 "/claw")),
Austin Schuhb2461f42019-06-29 18:17:06 -070041 shooter_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070042 event_loop->MakeSender<::y2014::control_loops::shooter::Goal>(
43 "/shooter")),
Austin Schuh1bf8a212019-05-26 22:13:14 -070044 shoot_action_factory_(actors::ShootActor::MakeFactory(event_loop)) {}
Austin Schuhbfb04122019-05-22 21:16:51 -070045
46void AutonomousActor::PositionClawVertically(double intake_power,
47 double centering_power) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070048 auto builder = claw_goal_sender_.MakeBuilder();
49 control_loops::claw::Goal::Builder goal_builder =
50 builder.MakeBuilder<control_loops::claw::Goal>();
51 goal_builder.add_bottom_angle(0.0);
52 goal_builder.add_separation_angle(0.0);
53 goal_builder.add_intake(intake_power);
54 goal_builder.add_centering(centering_power);
Austin Schuhb2461f42019-06-29 18:17:06 -070055
milind1f1dca32021-07-03 13:50:07 -070056 if (builder.Send(goal_builder.Finish()) != aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070057 AOS_LOG(WARNING, "sending claw goal failed\n");
Austin Schuhbfb04122019-05-22 21:16:51 -070058 }
59}
60
61void AutonomousActor::PositionClawBackIntake() {
Alex Perrycb7da4b2019-08-28 19:35:56 -070062 auto builder = claw_goal_sender_.MakeBuilder();
63 control_loops::claw::Goal::Builder goal_builder =
64 builder.MakeBuilder<control_loops::claw::Goal>();
65 goal_builder.add_bottom_angle(-2.273474);
66 goal_builder.add_separation_angle(0.0);
67 goal_builder.add_intake(12.0);
68 goal_builder.add_centering(12.0);
milind1f1dca32021-07-03 13:50:07 -070069 if (builder.Send(goal_builder.Finish()) != aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070070 AOS_LOG(WARNING, "sending claw goal failed\n");
Austin Schuhbfb04122019-05-22 21:16:51 -070071 }
72}
73
74void AutonomousActor::PositionClawUpClosed() {
75 // Move the claw to where we're going to shoot from but keep it closed until
76 // it gets there.
Alex Perrycb7da4b2019-08-28 19:35:56 -070077 auto builder = claw_goal_sender_.MakeBuilder();
78 control_loops::claw::Goal::Builder goal_builder =
79 builder.MakeBuilder<control_loops::claw::Goal>();
80 goal_builder.add_bottom_angle(0.86);
81 goal_builder.add_separation_angle(0.0);
82 goal_builder.add_intake(4.0);
83 goal_builder.add_centering(1.0);
milind1f1dca32021-07-03 13:50:07 -070084 if (builder.Send(goal_builder.Finish()) != aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070085 AOS_LOG(WARNING, "sending claw goal failed\n");
Austin Schuhbfb04122019-05-22 21:16:51 -070086 }
87}
88
89void AutonomousActor::PositionClawForShot() {
Alex Perrycb7da4b2019-08-28 19:35:56 -070090 auto builder = claw_goal_sender_.MakeBuilder();
91 control_loops::claw::Goal::Builder goal_builder =
92 builder.MakeBuilder<control_loops::claw::Goal>();
93 goal_builder.add_bottom_angle(0.86);
94 goal_builder.add_separation_angle(0.10);
95 goal_builder.add_intake(4.0);
96 goal_builder.add_centering(1.0);
milind1f1dca32021-07-03 13:50:07 -070097 if (builder.Send(goal_builder.Finish()) != aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070098 AOS_LOG(WARNING, "sending claw goal failed\n");
Austin Schuhbfb04122019-05-22 21:16:51 -070099 }
100}
101
102void AutonomousActor::SetShotPower(double power) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700103 AOS_LOG(INFO, "Setting shot power to %f\n", power);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700104 auto builder = shooter_goal_sender_.MakeBuilder();
105 control_loops::shooter::Goal::Builder goal_builder =
106 builder.MakeBuilder<control_loops::shooter::Goal>();
107 goal_builder.add_shot_power(power);
108 goal_builder.add_shot_requested(false);
109 goal_builder.add_unload_requested(false);
110 goal_builder.add_load_requested(false);
milind1f1dca32021-07-03 13:50:07 -0700111 if (builder.Send(goal_builder.Finish()) != aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700112 AOS_LOG(WARNING, "sending shooter goal failed\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700113 }
114}
115
Alex Perrycb7da4b2019-08-28 19:35:56 -0700116ProfileParametersT MakeProfileParameters(float max_velocity,
117 float max_acceleration) {
118 ProfileParametersT result;
119 result.max_velocity = max_velocity;
120 result.max_acceleration = max_acceleration;
121 return result;
122}
123
124const ProfileParametersT kFastDrive = MakeProfileParameters(3.0, 2.5);
125const ProfileParametersT kSlowDrive = MakeProfileParameters(2.5, 2.5);
126const ProfileParametersT kFastWithBallDrive = MakeProfileParameters(3.0, 2.0);
127const ProfileParametersT kSlowWithBallDrive = MakeProfileParameters(2.5, 2.0);
128const ProfileParametersT kFastTurn = MakeProfileParameters(3.0, 10.0);
Austin Schuhbfb04122019-05-22 21:16:51 -0700129
130void AutonomousActor::Shoot() {
131 // Shoot.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700132 aos::common::actions::DoubleParamT param;
133 auto shoot_action = shoot_action_factory_.Make(param);
Austin Schuhbfb04122019-05-22 21:16:51 -0700134 shoot_action->Start();
135 WaitUntilDoneOrCanceled(::std::move(shoot_action));
136}
137
138bool AutonomousActor::WaitUntilClawDone() {
139 while (true) {
140 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(10),
Austin Schuhd32b3622019-06-23 18:49:06 -0700141 event_loop()->monotonic_now(),
Austin Schuhbfb04122019-05-22 21:16:51 -0700142 ::std::chrono::milliseconds(10) / 2);
143 // Poll the running bit and auto done bits.
144 phased_loop.SleepUntilNext();
Austin Schuhb2461f42019-06-29 18:17:06 -0700145 claw_status_fetcher_.Fetch();
146 claw_goal_fetcher_.Fetch();
Austin Schuhbfb04122019-05-22 21:16:51 -0700147 if (ShouldCancel()) {
148 return false;
149 }
Austin Schuhb2461f42019-06-29 18:17:06 -0700150 if (claw_status_fetcher_.get() == nullptr ||
151 claw_goal_fetcher_.get() == nullptr) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700152 continue;
153 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700154 bool ans = claw_status_fetcher_->zeroed() &&
155 (::std::abs(claw_status_fetcher_->bottom_velocity()) < 1.0) &&
156 (::std::abs(claw_status_fetcher_->bottom() -
157 claw_goal_fetcher_->bottom_angle()) < 0.10) &&
158 (::std::abs(claw_status_fetcher_->separation() -
159 claw_goal_fetcher_->separation_angle()) < 0.4);
Austin Schuhbfb04122019-05-22 21:16:51 -0700160 if (ans) {
161 return true;
162 }
163 }
164}
165
166class HotGoalDecoder {
167 public:
Austin Schuha3e576b2019-05-22 21:22:23 -0700168 HotGoalDecoder(::aos::Fetcher<::y2014::HotGoal> *hot_goal_fetcher)
169 : hot_goal_fetcher_(hot_goal_fetcher) {
170 ResetCounts();
171 }
Austin Schuhbfb04122019-05-22 21:16:51 -0700172
173 void ResetCounts() {
Austin Schuha3e576b2019-05-22 21:22:23 -0700174 hot_goal_fetcher_->Fetch();
175 if (hot_goal_fetcher_->get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700176 hot_goal_fetcher_->get()->UnPackTo(&start_counts_);
Austin Schuhbfb04122019-05-22 21:16:51 -0700177 start_counts_valid_ = true;
178 } else {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700179 AOS_LOG(WARNING, "no hot goal message. ignoring\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700180 start_counts_valid_ = false;
181 }
182 }
183
Alex Perrycb7da4b2019-08-28 19:35:56 -0700184 void Update() { hot_goal_fetcher_->Fetch(); }
Austin Schuhbfb04122019-05-22 21:16:51 -0700185
186 bool left_triggered() const {
Austin Schuha3e576b2019-05-22 21:22:23 -0700187 if (!start_counts_valid_ || !hot_goal_fetcher_->get()) return false;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700188 return (hot_goal_fetcher_->get()->left_count() - start_counts_.left_count) >
Austin Schuha3e576b2019-05-22 21:22:23 -0700189 kThreshold;
Austin Schuhbfb04122019-05-22 21:16:51 -0700190 }
191
192 bool right_triggered() const {
Austin Schuha3e576b2019-05-22 21:22:23 -0700193 if (!start_counts_valid_ || !hot_goal_fetcher_->get()) return false;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700194 return (hot_goal_fetcher_->get()->right_count() -
195 start_counts_.right_count) > kThreshold;
Austin Schuhbfb04122019-05-22 21:16:51 -0700196 }
197
198 bool is_left() const {
Austin Schuha3e576b2019-05-22 21:22:23 -0700199 if (!start_counts_valid_ || !hot_goal_fetcher_->get()) return false;
Austin Schuhbfb04122019-05-22 21:16:51 -0700200 const uint64_t left_difference =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700201 hot_goal_fetcher_->get()->left_count() - start_counts_.left_count;
Austin Schuhbfb04122019-05-22 21:16:51 -0700202 const uint64_t right_difference =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700203 hot_goal_fetcher_->get()->right_count() - start_counts_.right_count;
Austin Schuhbfb04122019-05-22 21:16:51 -0700204 if (left_difference > kThreshold) {
205 if (right_difference > kThreshold) {
206 // We've seen a lot of both, so pick the one we've seen the most of.
207 return left_difference > right_difference;
208 } else {
209 // We've seen enough left but not enough right, so go with it.
210 return true;
211 }
212 } else {
213 // We haven't seen enough left, so it's not left.
214 return false;
215 }
216 }
217
218 bool is_right() const {
Austin Schuha3e576b2019-05-22 21:22:23 -0700219 if (!start_counts_valid_ || !hot_goal_fetcher_->get()) return false;
Austin Schuhbfb04122019-05-22 21:16:51 -0700220 const uint64_t left_difference =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700221 hot_goal_fetcher_->get()->left_count() - start_counts_.left_count;
Austin Schuhbfb04122019-05-22 21:16:51 -0700222 const uint64_t right_difference =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700223 hot_goal_fetcher_->get()->right_count() - start_counts_.right_count;
Austin Schuhbfb04122019-05-22 21:16:51 -0700224 if (right_difference > kThreshold) {
225 if (left_difference > kThreshold) {
226 // We've seen a lot of both, so pick the one we've seen the most of.
227 return right_difference > left_difference;
228 } else {
229 // We've seen enough right but not enough left, so go with it.
230 return true;
231 }
232 } else {
233 // We haven't seen enough right, so it's not right.
234 return false;
235 }
236 }
237
238 private:
239 static const uint64_t kThreshold = 5;
240
Alex Perrycb7da4b2019-08-28 19:35:56 -0700241 ::y2014::HotGoalT start_counts_;
Austin Schuhbfb04122019-05-22 21:16:51 -0700242 bool start_counts_valid_;
Austin Schuha3e576b2019-05-22 21:22:23 -0700243
244 ::aos::Fetcher<::y2014::HotGoal> *hot_goal_fetcher_;
Austin Schuhbfb04122019-05-22 21:16:51 -0700245};
246
247bool AutonomousActor::RunAction(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700248 const ::frc971::autonomous::AutonomousActionParams * /*params*/) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700249 enum class AutoVersion : uint8_t {
250 kStraight,
251 kDoubleHot,
252 kSingleHot,
253 };
254
255 // The front of the robot is 1.854 meters from the wall
256 static const double kShootDistance = 3.15;
257 static const double kPickupDistance = 0.5;
258 static const double kTurnAngle = 0.3;
259
Austin Schuh77ed5432019-07-07 20:41:36 -0700260 const monotonic_clock::time_point start_time = monotonic_now();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700261 AOS_LOG(INFO, "Handling auto mode\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700262
263 AutoVersion auto_version;
Austin Schuh7eed2de2019-05-25 14:34:40 -0700264 auto_mode_fetcher_.Fetch();
265 if (!auto_mode_fetcher_.get()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700266 AOS_LOG(WARNING, "not sure which auto mode to use\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700267 auto_version = AutoVersion::kStraight;
268 } else {
269 static const double kSelectorMin = 0.2, kSelectorMax = 4.4;
270
271 const double kSelectorStep = (kSelectorMax - kSelectorMin) / 3.0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700272 if (auto_mode_fetcher_->voltage() < kSelectorStep + kSelectorMin) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700273 auto_version = AutoVersion::kSingleHot;
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700274 } else if (auto_mode_fetcher_->voltage() <
275 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,
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700334 drive_with_ball_params, kFastTurn);
Austin Schuhbfb04122019-05-22 21:16:51 -0700335 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