blob: 7486f47cc6ed8eb2113f130c461ad753f50698e3 [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
Stephan Pleinesf63bde82024-01-13 15:59:33 -080020namespace y2014::actors {
Austin Schuhbfb04122019-05-22 21:16:51 -070021
22namespace chrono = ::std::chrono;
23namespace this_thread = ::std::this_thread;
24using ::aos::monotonic_clock;
Alex Perrycb7da4b2019-08-28 19:35:56 -070025using ::frc971::ProfileParametersT;
Austin Schuhbfb04122019-05-22 21:16:51 -070026
Austin Schuh1bf8a212019-05-26 22:13:14 -070027AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop)
Austin Schuhbfb04122019-05-22 21:16:51 -070028 : frc971::autonomous::BaseAutonomousActor(
Austin Schuh1bf8a212019-05-26 22:13:14 -070029 event_loop, control_loops::GetDrivetrainConfig()),
Alex Perrycb7da4b2019-08-28 19:35:56 -070030 auto_mode_fetcher_(
31 event_loop->MakeFetcher<::y2014::sensors::AutoMode>("/aos")),
32 hot_goal_fetcher_(event_loop->MakeFetcher<::y2014::HotGoal>("/")),
Austin Schuhb2461f42019-06-29 18:17:06 -070033 claw_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070034 event_loop->MakeSender<::y2014::control_loops::claw::Goal>("/claw")),
Austin Schuhb2461f42019-06-29 18:17:06 -070035 claw_goal_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070036 event_loop->MakeFetcher<::y2014::control_loops::claw::Goal>("/claw")),
Austin Schuhb2461f42019-06-29 18:17:06 -070037 claw_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070038 event_loop->MakeFetcher<::y2014::control_loops::claw::Status>(
39 "/claw")),
Austin Schuhb2461f42019-06-29 18:17:06 -070040 shooter_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070041 event_loop->MakeSender<::y2014::control_loops::shooter::Goal>(
42 "/shooter")),
Austin Schuh1bf8a212019-05-26 22:13:14 -070043 shoot_action_factory_(actors::ShootActor::MakeFactory(event_loop)) {}
Austin Schuhbfb04122019-05-22 21:16:51 -070044
45void AutonomousActor::PositionClawVertically(double intake_power,
46 double centering_power) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070047 auto builder = claw_goal_sender_.MakeBuilder();
48 control_loops::claw::Goal::Builder goal_builder =
49 builder.MakeBuilder<control_loops::claw::Goal>();
50 goal_builder.add_bottom_angle(0.0);
51 goal_builder.add_separation_angle(0.0);
52 goal_builder.add_intake(intake_power);
53 goal_builder.add_centering(centering_power);
Austin Schuhb2461f42019-06-29 18:17:06 -070054
milind1f1dca32021-07-03 13:50:07 -070055 if (builder.Send(goal_builder.Finish()) != aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070056 AOS_LOG(WARNING, "sending claw goal failed\n");
Austin Schuhbfb04122019-05-22 21:16:51 -070057 }
58}
59
60void AutonomousActor::PositionClawBackIntake() {
Alex Perrycb7da4b2019-08-28 19:35:56 -070061 auto builder = claw_goal_sender_.MakeBuilder();
62 control_loops::claw::Goal::Builder goal_builder =
63 builder.MakeBuilder<control_loops::claw::Goal>();
64 goal_builder.add_bottom_angle(-2.273474);
65 goal_builder.add_separation_angle(0.0);
66 goal_builder.add_intake(12.0);
67 goal_builder.add_centering(12.0);
milind1f1dca32021-07-03 13:50:07 -070068 if (builder.Send(goal_builder.Finish()) != aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070069 AOS_LOG(WARNING, "sending claw goal failed\n");
Austin Schuhbfb04122019-05-22 21:16:51 -070070 }
71}
72
73void AutonomousActor::PositionClawUpClosed() {
74 // Move the claw to where we're going to shoot from but keep it closed until
75 // it gets there.
Alex Perrycb7da4b2019-08-28 19:35:56 -070076 auto builder = claw_goal_sender_.MakeBuilder();
77 control_loops::claw::Goal::Builder goal_builder =
78 builder.MakeBuilder<control_loops::claw::Goal>();
79 goal_builder.add_bottom_angle(0.86);
80 goal_builder.add_separation_angle(0.0);
81 goal_builder.add_intake(4.0);
82 goal_builder.add_centering(1.0);
milind1f1dca32021-07-03 13:50:07 -070083 if (builder.Send(goal_builder.Finish()) != aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070084 AOS_LOG(WARNING, "sending claw goal failed\n");
Austin Schuhbfb04122019-05-22 21:16:51 -070085 }
86}
87
88void AutonomousActor::PositionClawForShot() {
Alex Perrycb7da4b2019-08-28 19:35:56 -070089 auto builder = claw_goal_sender_.MakeBuilder();
90 control_loops::claw::Goal::Builder goal_builder =
91 builder.MakeBuilder<control_loops::claw::Goal>();
92 goal_builder.add_bottom_angle(0.86);
93 goal_builder.add_separation_angle(0.10);
94 goal_builder.add_intake(4.0);
95 goal_builder.add_centering(1.0);
milind1f1dca32021-07-03 13:50:07 -070096 if (builder.Send(goal_builder.Finish()) != aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070097 AOS_LOG(WARNING, "sending claw goal failed\n");
Austin Schuhbfb04122019-05-22 21:16:51 -070098 }
99}
100
101void AutonomousActor::SetShotPower(double power) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700102 AOS_LOG(INFO, "Setting shot power to %f\n", power);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700103 auto builder = shooter_goal_sender_.MakeBuilder();
104 control_loops::shooter::Goal::Builder goal_builder =
105 builder.MakeBuilder<control_loops::shooter::Goal>();
106 goal_builder.add_shot_power(power);
107 goal_builder.add_shot_requested(false);
108 goal_builder.add_unload_requested(false);
109 goal_builder.add_load_requested(false);
milind1f1dca32021-07-03 13:50:07 -0700110 if (builder.Send(goal_builder.Finish()) != aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700111 AOS_LOG(WARNING, "sending shooter goal failed\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700112 }
113}
114
Alex Perrycb7da4b2019-08-28 19:35:56 -0700115ProfileParametersT MakeProfileParameters(float max_velocity,
116 float max_acceleration) {
117 ProfileParametersT result;
118 result.max_velocity = max_velocity;
119 result.max_acceleration = max_acceleration;
120 return result;
121}
122
123const ProfileParametersT kFastDrive = MakeProfileParameters(3.0, 2.5);
124const ProfileParametersT kSlowDrive = MakeProfileParameters(2.5, 2.5);
125const ProfileParametersT kFastWithBallDrive = MakeProfileParameters(3.0, 2.0);
126const ProfileParametersT kSlowWithBallDrive = MakeProfileParameters(2.5, 2.0);
127const ProfileParametersT kFastTurn = MakeProfileParameters(3.0, 10.0);
Austin Schuhbfb04122019-05-22 21:16:51 -0700128
129void AutonomousActor::Shoot() {
130 // Shoot.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700131 aos::common::actions::DoubleParamT param;
132 auto shoot_action = shoot_action_factory_.Make(param);
Austin Schuhbfb04122019-05-22 21:16:51 -0700133 shoot_action->Start();
134 WaitUntilDoneOrCanceled(::std::move(shoot_action));
135}
136
137bool AutonomousActor::WaitUntilClawDone() {
138 while (true) {
139 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(10),
Austin Schuhd32b3622019-06-23 18:49:06 -0700140 event_loop()->monotonic_now(),
Austin Schuhbfb04122019-05-22 21:16:51 -0700141 ::std::chrono::milliseconds(10) / 2);
142 // Poll the running bit and auto done bits.
143 phased_loop.SleepUntilNext();
Austin Schuhb2461f42019-06-29 18:17:06 -0700144 claw_status_fetcher_.Fetch();
145 claw_goal_fetcher_.Fetch();
Austin Schuhbfb04122019-05-22 21:16:51 -0700146 if (ShouldCancel()) {
147 return false;
148 }
Austin Schuhb2461f42019-06-29 18:17:06 -0700149 if (claw_status_fetcher_.get() == nullptr ||
150 claw_goal_fetcher_.get() == nullptr) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700151 continue;
152 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700153 bool ans = claw_status_fetcher_->zeroed() &&
154 (::std::abs(claw_status_fetcher_->bottom_velocity()) < 1.0) &&
155 (::std::abs(claw_status_fetcher_->bottom() -
156 claw_goal_fetcher_->bottom_angle()) < 0.10) &&
157 (::std::abs(claw_status_fetcher_->separation() -
158 claw_goal_fetcher_->separation_angle()) < 0.4);
Austin Schuhbfb04122019-05-22 21:16:51 -0700159 if (ans) {
160 return true;
161 }
162 }
163}
164
165class HotGoalDecoder {
166 public:
Austin Schuha3e576b2019-05-22 21:22:23 -0700167 HotGoalDecoder(::aos::Fetcher<::y2014::HotGoal> *hot_goal_fetcher)
168 : hot_goal_fetcher_(hot_goal_fetcher) {
169 ResetCounts();
170 }
Austin Schuhbfb04122019-05-22 21:16:51 -0700171
172 void ResetCounts() {
Austin Schuha3e576b2019-05-22 21:22:23 -0700173 hot_goal_fetcher_->Fetch();
174 if (hot_goal_fetcher_->get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700175 hot_goal_fetcher_->get()->UnPackTo(&start_counts_);
Austin Schuhbfb04122019-05-22 21:16:51 -0700176 start_counts_valid_ = true;
177 } else {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700178 AOS_LOG(WARNING, "no hot goal message. ignoring\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700179 start_counts_valid_ = false;
180 }
181 }
182
Alex Perrycb7da4b2019-08-28 19:35:56 -0700183 void Update() { hot_goal_fetcher_->Fetch(); }
Austin Schuhbfb04122019-05-22 21:16:51 -0700184
185 bool left_triggered() const {
Austin Schuha3e576b2019-05-22 21:22:23 -0700186 if (!start_counts_valid_ || !hot_goal_fetcher_->get()) return false;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700187 return (hot_goal_fetcher_->get()->left_count() - start_counts_.left_count) >
Austin Schuha3e576b2019-05-22 21:22:23 -0700188 kThreshold;
Austin Schuhbfb04122019-05-22 21:16:51 -0700189 }
190
191 bool right_triggered() const {
Austin Schuha3e576b2019-05-22 21:22:23 -0700192 if (!start_counts_valid_ || !hot_goal_fetcher_->get()) return false;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700193 return (hot_goal_fetcher_->get()->right_count() -
194 start_counts_.right_count) > kThreshold;
Austin Schuhbfb04122019-05-22 21:16:51 -0700195 }
196
197 bool is_left() const {
Austin Schuha3e576b2019-05-22 21:22:23 -0700198 if (!start_counts_valid_ || !hot_goal_fetcher_->get()) return false;
Austin Schuhbfb04122019-05-22 21:16:51 -0700199 const uint64_t left_difference =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700200 hot_goal_fetcher_->get()->left_count() - start_counts_.left_count;
Austin Schuhbfb04122019-05-22 21:16:51 -0700201 const uint64_t right_difference =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700202 hot_goal_fetcher_->get()->right_count() - start_counts_.right_count;
Austin Schuhbfb04122019-05-22 21:16:51 -0700203 if (left_difference > kThreshold) {
204 if (right_difference > kThreshold) {
205 // We've seen a lot of both, so pick the one we've seen the most of.
206 return left_difference > right_difference;
207 } else {
208 // We've seen enough left but not enough right, so go with it.
209 return true;
210 }
211 } else {
212 // We haven't seen enough left, so it's not left.
213 return false;
214 }
215 }
216
217 bool is_right() const {
Austin Schuha3e576b2019-05-22 21:22:23 -0700218 if (!start_counts_valid_ || !hot_goal_fetcher_->get()) return false;
Austin Schuhbfb04122019-05-22 21:16:51 -0700219 const uint64_t left_difference =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700220 hot_goal_fetcher_->get()->left_count() - start_counts_.left_count;
Austin Schuhbfb04122019-05-22 21:16:51 -0700221 const uint64_t right_difference =
Alex Perrycb7da4b2019-08-28 19:35:56 -0700222 hot_goal_fetcher_->get()->right_count() - start_counts_.right_count;
Austin Schuhbfb04122019-05-22 21:16:51 -0700223 if (right_difference > kThreshold) {
224 if (left_difference > kThreshold) {
225 // We've seen a lot of both, so pick the one we've seen the most of.
226 return right_difference > left_difference;
227 } else {
228 // We've seen enough right but not enough left, so go with it.
229 return true;
230 }
231 } else {
232 // We haven't seen enough right, so it's not right.
233 return false;
234 }
235 }
236
237 private:
238 static const uint64_t kThreshold = 5;
239
Alex Perrycb7da4b2019-08-28 19:35:56 -0700240 ::y2014::HotGoalT start_counts_;
Austin Schuhbfb04122019-05-22 21:16:51 -0700241 bool start_counts_valid_;
Austin Schuha3e576b2019-05-22 21:22:23 -0700242
243 ::aos::Fetcher<::y2014::HotGoal> *hot_goal_fetcher_;
Austin Schuhbfb04122019-05-22 21:16:51 -0700244};
245
246bool AutonomousActor::RunAction(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700247 const ::frc971::autonomous::AutonomousActionParams * /*params*/) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700248 enum class AutoVersion : uint8_t {
249 kStraight,
250 kDoubleHot,
251 kSingleHot,
252 };
253
254 // The front of the robot is 1.854 meters from the wall
255 static const double kShootDistance = 3.15;
256 static const double kPickupDistance = 0.5;
257 static const double kTurnAngle = 0.3;
258
Austin Schuh77ed5432019-07-07 20:41:36 -0700259 const monotonic_clock::time_point start_time = monotonic_now();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700260 AOS_LOG(INFO, "Handling auto mode\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700261
262 AutoVersion auto_version;
Austin Schuh7eed2de2019-05-25 14:34:40 -0700263 auto_mode_fetcher_.Fetch();
264 if (!auto_mode_fetcher_.get()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700265 AOS_LOG(WARNING, "not sure which auto mode to use\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700266 auto_version = AutoVersion::kStraight;
267 } else {
268 static const double kSelectorMin = 0.2, kSelectorMax = 4.4;
269
270 const double kSelectorStep = (kSelectorMax - kSelectorMin) / 3.0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700271 if (auto_mode_fetcher_->voltage() < kSelectorStep + kSelectorMin) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700272 auto_version = AutoVersion::kSingleHot;
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700273 } else if (auto_mode_fetcher_->voltage() <
274 2 * kSelectorStep + kSelectorMin) {
Austin Schuhbfb04122019-05-22 21:16:51 -0700275 auto_version = AutoVersion::kStraight;
276 } else {
277 auto_version = AutoVersion::kDoubleHot;
278 }
279 }
Austin Schuhf257f3c2019-10-27 21:00:43 -0700280 AOS_LOG(INFO, "running auto %" PRIu8 "\n",
281 static_cast<uint8_t>(auto_version));
Austin Schuhbfb04122019-05-22 21:16:51 -0700282
Alex Perrycb7da4b2019-08-28 19:35:56 -0700283 const ProfileParametersT drive_params =
Austin Schuhbfb04122019-05-22 21:16:51 -0700284 (auto_version == AutoVersion::kStraight) ? kFastDrive : kSlowDrive;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700285 const ProfileParametersT drive_with_ball_params =
Austin Schuhbfb04122019-05-22 21:16:51 -0700286 (auto_version == AutoVersion::kStraight) ? kFastWithBallDrive
287 : kSlowWithBallDrive;
288
Austin Schuha3e576b2019-05-22 21:22:23 -0700289 HotGoalDecoder hot_goal_decoder(&hot_goal_fetcher_);
Austin Schuhbfb04122019-05-22 21:16:51 -0700290 // True for left, false for right.
291 bool first_shot_left, second_shot_left_default, second_shot_left;
292
293 Reset();
294
295 // Turn the claw on, keep it straight up until the ball has been grabbed.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700296 AOS_LOG(INFO, "Claw going up at %f\n",
297 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuhbfb04122019-05-22 21:16:51 -0700298 PositionClawVertically(12.0, 4.0);
299 SetShotPower(115.0);
300
301 // Wait for the ball to enter the claw.
302 this_thread::sleep_for(chrono::milliseconds(250));
303 if (ShouldCancel()) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700304 AOS_LOG(INFO, "Readying claw for shot at %f\n",
305 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuhbfb04122019-05-22 21:16:51 -0700306
307 if (ShouldCancel()) return true;
308 // Drive to the goal.
309 StartDrive(-kShootDistance, 0.0, drive_params, kFastTurn);
310 this_thread::sleep_for(chrono::milliseconds(750));
311 PositionClawForShot();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700312 AOS_LOG(INFO, "Waiting until drivetrain is finished\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700313 WaitForDriveProfileDone();
314 if (ShouldCancel()) return true;
315
316 hot_goal_decoder.Update();
317 if (hot_goal_decoder.is_left()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700318 AOS_LOG(INFO, "first shot left\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700319 first_shot_left = true;
320 second_shot_left_default = false;
321 } else if (hot_goal_decoder.is_right()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700322 AOS_LOG(INFO, "first shot right\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700323 first_shot_left = false;
324 second_shot_left_default = true;
325 } else {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700326 AOS_LOG(INFO, "first shot defaulting left\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700327 first_shot_left = true;
328 second_shot_left_default = true;
329 }
330 if (auto_version == AutoVersion::kDoubleHot) {
331 if (ShouldCancel()) return true;
332 StartDrive(0, first_shot_left ? kTurnAngle : -kTurnAngle,
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700333 drive_with_ball_params, kFastTurn);
Austin Schuhbfb04122019-05-22 21:16:51 -0700334 WaitForDriveProfileDone();
335 if (ShouldCancel()) return true;
336 } else if (auto_version == AutoVersion::kSingleHot) {
337 do {
338 // TODO(brians): Wait for next message with timeout or something.
339 this_thread::sleep_for(chrono::milliseconds(3));
Austin Schuha3e576b2019-05-22 21:22:23 -0700340 hot_goal_decoder.Update();
Austin Schuhbfb04122019-05-22 21:16:51 -0700341 if (ShouldCancel()) return true;
342 } while (!hot_goal_decoder.left_triggered() &&
Austin Schuh77ed5432019-07-07 20:41:36 -0700343 (monotonic_now() - start_time) < chrono::seconds(9));
Austin Schuhbfb04122019-05-22 21:16:51 -0700344 } else if (auto_version == AutoVersion::kStraight) {
345 this_thread::sleep_for(chrono::milliseconds(400));
346 }
347
348 // Shoot.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700349 AOS_LOG(INFO, "Shooting at %f\n",
350 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuhbfb04122019-05-22 21:16:51 -0700351 Shoot();
352 this_thread::sleep_for(chrono::milliseconds(50));
353
354 if (auto_version == AutoVersion::kDoubleHot) {
355 if (ShouldCancel()) return true;
356 StartDrive(0, first_shot_left ? -kTurnAngle : kTurnAngle,
357 drive_with_ball_params, kFastTurn);
358 WaitForDriveProfileDone();
359 if (ShouldCancel()) return true;
360 } else if (auto_version == AutoVersion::kSingleHot) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700361 AOS_LOG(INFO, "auto done at %f\n",
362 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuhbfb04122019-05-22 21:16:51 -0700363 PositionClawVertically(0.0, 0.0);
364 return true;
365 }
366
367 {
368 if (ShouldCancel()) return true;
369 // Intake the new ball.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700370 AOS_LOG(INFO, "Claw ready for intake at %f\n",
371 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuhbfb04122019-05-22 21:16:51 -0700372 PositionClawBackIntake();
373 StartDrive(kShootDistance + kPickupDistance, 0.0, drive_params, kFastTurn);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700374 AOS_LOG(INFO, "Waiting until drivetrain is finished\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700375 WaitForDriveProfileDone();
376 if (ShouldCancel()) return true;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700377 AOS_LOG(INFO, "Wait for the claw at %f\n",
378 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuhbfb04122019-05-22 21:16:51 -0700379 if (!WaitUntilClawDone()) return true;
380 }
381
382 // Drive back.
383 {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700384 AOS_LOG(INFO, "Driving back at %f\n",
385 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuhbfb04122019-05-22 21:16:51 -0700386 StartDrive(-(kShootDistance + kPickupDistance), 0.0, drive_params,
387 kFastTurn);
388 this_thread::sleep_for(chrono::milliseconds(300));
389 hot_goal_decoder.ResetCounts();
390 if (ShouldCancel()) return true;
391 PositionClawUpClosed();
392 if (!WaitUntilClawDone()) return true;
393 PositionClawForShot();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700394 AOS_LOG(INFO, "Waiting until drivetrain is finished\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700395 WaitForDriveProfileDone();
396 if (ShouldCancel()) return true;
397 if (!WaitUntilClawDone()) return true;
398 }
399
400 hot_goal_decoder.Update();
401 if (hot_goal_decoder.is_left()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700402 AOS_LOG(INFO, "second shot left\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700403 second_shot_left = true;
404 } else if (hot_goal_decoder.is_right()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700405 AOS_LOG(INFO, "second shot right\n");
Austin Schuhbfb04122019-05-22 21:16:51 -0700406 second_shot_left = false;
407 } else {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700408 AOS_LOG(INFO, "second shot defaulting %s\n",
409 second_shot_left_default ? "left" : "right");
Austin Schuhbfb04122019-05-22 21:16:51 -0700410 second_shot_left = second_shot_left_default;
411 }
412 if (auto_version == AutoVersion::kDoubleHot) {
413 if (ShouldCancel()) return true;
414 StartDrive(0, second_shot_left ? kTurnAngle : -kTurnAngle, drive_params,
415 kFastTurn);
416 WaitForDriveProfileDone();
417 if (ShouldCancel()) return true;
418 } else if (auto_version == AutoVersion::kStraight) {
419 this_thread::sleep_for(chrono::milliseconds(400));
420 }
421
Austin Schuhf257f3c2019-10-27 21:00:43 -0700422 AOS_LOG(INFO, "Shooting at %f\n",
423 ::aos::time::DurationInSeconds(monotonic_now() - start_time));
Austin Schuhbfb04122019-05-22 21:16:51 -0700424 // Shoot
425 Shoot();
426 if (ShouldCancel()) return true;
427
428 // Get ready to zero when we come back up.
429 this_thread::sleep_for(chrono::milliseconds(50));
430 PositionClawVertically(0.0, 0.0);
431 return true;
432}
433
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800434} // namespace y2014::actors