Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 1 | #include "y2014/actors/autonomous_actor.h" |
| 2 | |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 3 | #include <chrono> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 4 | #include <cstdio> |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 5 | #include <memory> |
| 6 | |
| 7 | #include "aos/actions/actions.h" |
| 8 | #include "aos/logging/logging.h" |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 9 | #include "aos/time/time.h" |
| 10 | #include "aos/util/phased_loop.h" |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 11 | #include "y2014/actors/shoot_actor.h" |
| 12 | #include "y2014/constants.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 13 | #include "y2014/control_loops/claw/claw_goal_generated.h" |
| 14 | #include "y2014/control_loops/claw/claw_status_generated.h" |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 15 | #include "y2014/control_loops/drivetrain/drivetrain_base.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 16 | #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 Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 19 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 20 | namespace y2014::actors { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 21 | |
| 22 | namespace chrono = ::std::chrono; |
| 23 | namespace this_thread = ::std::this_thread; |
| 24 | using ::aos::monotonic_clock; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 25 | using ::frc971::ProfileParametersT; |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 26 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 27 | AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop) |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 28 | : frc971::autonomous::BaseAutonomousActor( |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 29 | event_loop, control_loops::GetDrivetrainConfig()), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 30 | auto_mode_fetcher_( |
| 31 | event_loop->MakeFetcher<::y2014::sensors::AutoMode>("/aos")), |
| 32 | hot_goal_fetcher_(event_loop->MakeFetcher<::y2014::HotGoal>("/")), |
Austin Schuh | b2461f4 | 2019-06-29 18:17:06 -0700 | [diff] [blame] | 33 | claw_goal_sender_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 34 | event_loop->MakeSender<::y2014::control_loops::claw::Goal>("/claw")), |
Austin Schuh | b2461f4 | 2019-06-29 18:17:06 -0700 | [diff] [blame] | 35 | claw_goal_fetcher_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 36 | event_loop->MakeFetcher<::y2014::control_loops::claw::Goal>("/claw")), |
Austin Schuh | b2461f4 | 2019-06-29 18:17:06 -0700 | [diff] [blame] | 37 | claw_status_fetcher_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 38 | event_loop->MakeFetcher<::y2014::control_loops::claw::Status>( |
| 39 | "/claw")), |
Austin Schuh | b2461f4 | 2019-06-29 18:17:06 -0700 | [diff] [blame] | 40 | shooter_goal_sender_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 41 | event_loop->MakeSender<::y2014::control_loops::shooter::Goal>( |
| 42 | "/shooter")), |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 43 | shoot_action_factory_(actors::ShootActor::MakeFactory(event_loop)) {} |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 44 | |
| 45 | void AutonomousActor::PositionClawVertically(double intake_power, |
| 46 | double centering_power) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 47 | 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 Schuh | b2461f4 | 2019-06-29 18:17:06 -0700 | [diff] [blame] | 54 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 55 | if (builder.Send(goal_builder.Finish()) != aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 56 | AOS_LOG(WARNING, "sending claw goal failed\n"); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
| 60 | void AutonomousActor::PositionClawBackIntake() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 61 | 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); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 68 | if (builder.Send(goal_builder.Finish()) != aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 69 | AOS_LOG(WARNING, "sending claw goal failed\n"); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
| 73 | void AutonomousActor::PositionClawUpClosed() { |
| 74 | // Move the claw to where we're going to shoot from but keep it closed until |
| 75 | // it gets there. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 76 | 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); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 83 | if (builder.Send(goal_builder.Finish()) != aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 84 | AOS_LOG(WARNING, "sending claw goal failed\n"); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | void AutonomousActor::PositionClawForShot() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 89 | 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); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 96 | if (builder.Send(goal_builder.Finish()) != aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 97 | AOS_LOG(WARNING, "sending claw goal failed\n"); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | void AutonomousActor::SetShotPower(double power) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 102 | AOS_LOG(INFO, "Setting shot power to %f\n", power); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 103 | 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); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 110 | if (builder.Send(goal_builder.Finish()) != aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 111 | AOS_LOG(WARNING, "sending shooter goal failed\n"); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 115 | ProfileParametersT 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 | |
| 123 | const ProfileParametersT kFastDrive = MakeProfileParameters(3.0, 2.5); |
| 124 | const ProfileParametersT kSlowDrive = MakeProfileParameters(2.5, 2.5); |
| 125 | const ProfileParametersT kFastWithBallDrive = MakeProfileParameters(3.0, 2.0); |
| 126 | const ProfileParametersT kSlowWithBallDrive = MakeProfileParameters(2.5, 2.0); |
| 127 | const ProfileParametersT kFastTurn = MakeProfileParameters(3.0, 10.0); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 128 | |
| 129 | void AutonomousActor::Shoot() { |
| 130 | // Shoot. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 131 | aos::common::actions::DoubleParamT param; |
| 132 | auto shoot_action = shoot_action_factory_.Make(param); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 133 | shoot_action->Start(); |
| 134 | WaitUntilDoneOrCanceled(::std::move(shoot_action)); |
| 135 | } |
| 136 | |
| 137 | bool AutonomousActor::WaitUntilClawDone() { |
| 138 | while (true) { |
| 139 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(10), |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 140 | event_loop()->monotonic_now(), |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 141 | ::std::chrono::milliseconds(10) / 2); |
| 142 | // Poll the running bit and auto done bits. |
| 143 | phased_loop.SleepUntilNext(); |
Austin Schuh | b2461f4 | 2019-06-29 18:17:06 -0700 | [diff] [blame] | 144 | claw_status_fetcher_.Fetch(); |
| 145 | claw_goal_fetcher_.Fetch(); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 146 | if (ShouldCancel()) { |
| 147 | return false; |
| 148 | } |
Austin Schuh | b2461f4 | 2019-06-29 18:17:06 -0700 | [diff] [blame] | 149 | if (claw_status_fetcher_.get() == nullptr || |
| 150 | claw_goal_fetcher_.get() == nullptr) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 151 | continue; |
| 152 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 153 | 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 Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 159 | if (ans) { |
| 160 | return true; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | class HotGoalDecoder { |
| 166 | public: |
Austin Schuh | a3e576b | 2019-05-22 21:22:23 -0700 | [diff] [blame] | 167 | HotGoalDecoder(::aos::Fetcher<::y2014::HotGoal> *hot_goal_fetcher) |
| 168 | : hot_goal_fetcher_(hot_goal_fetcher) { |
| 169 | ResetCounts(); |
| 170 | } |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 171 | |
| 172 | void ResetCounts() { |
Austin Schuh | a3e576b | 2019-05-22 21:22:23 -0700 | [diff] [blame] | 173 | hot_goal_fetcher_->Fetch(); |
| 174 | if (hot_goal_fetcher_->get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 175 | hot_goal_fetcher_->get()->UnPackTo(&start_counts_); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 176 | start_counts_valid_ = true; |
| 177 | } else { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 178 | AOS_LOG(WARNING, "no hot goal message. ignoring\n"); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 179 | start_counts_valid_ = false; |
| 180 | } |
| 181 | } |
| 182 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 183 | void Update() { hot_goal_fetcher_->Fetch(); } |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 184 | |
| 185 | bool left_triggered() const { |
Austin Schuh | a3e576b | 2019-05-22 21:22:23 -0700 | [diff] [blame] | 186 | if (!start_counts_valid_ || !hot_goal_fetcher_->get()) return false; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 187 | return (hot_goal_fetcher_->get()->left_count() - start_counts_.left_count) > |
Austin Schuh | a3e576b | 2019-05-22 21:22:23 -0700 | [diff] [blame] | 188 | kThreshold; |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | bool right_triggered() const { |
Austin Schuh | a3e576b | 2019-05-22 21:22:23 -0700 | [diff] [blame] | 192 | if (!start_counts_valid_ || !hot_goal_fetcher_->get()) return false; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 193 | return (hot_goal_fetcher_->get()->right_count() - |
| 194 | start_counts_.right_count) > kThreshold; |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | bool is_left() const { |
Austin Schuh | a3e576b | 2019-05-22 21:22:23 -0700 | [diff] [blame] | 198 | if (!start_counts_valid_ || !hot_goal_fetcher_->get()) return false; |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 199 | const uint64_t left_difference = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 200 | hot_goal_fetcher_->get()->left_count() - start_counts_.left_count; |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 201 | const uint64_t right_difference = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 202 | hot_goal_fetcher_->get()->right_count() - start_counts_.right_count; |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 203 | 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 Schuh | a3e576b | 2019-05-22 21:22:23 -0700 | [diff] [blame] | 218 | if (!start_counts_valid_ || !hot_goal_fetcher_->get()) return false; |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 219 | const uint64_t left_difference = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 220 | hot_goal_fetcher_->get()->left_count() - start_counts_.left_count; |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 221 | const uint64_t right_difference = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 222 | hot_goal_fetcher_->get()->right_count() - start_counts_.right_count; |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 223 | 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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 240 | ::y2014::HotGoalT start_counts_; |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 241 | bool start_counts_valid_; |
Austin Schuh | a3e576b | 2019-05-22 21:22:23 -0700 | [diff] [blame] | 242 | |
| 243 | ::aos::Fetcher<::y2014::HotGoal> *hot_goal_fetcher_; |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 244 | }; |
| 245 | |
| 246 | bool AutonomousActor::RunAction( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 247 | const ::frc971::autonomous::AutonomousActionParams * /*params*/) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 248 | 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 Schuh | 77ed543 | 2019-07-07 20:41:36 -0700 | [diff] [blame] | 259 | const monotonic_clock::time_point start_time = monotonic_now(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 260 | AOS_LOG(INFO, "Handling auto mode\n"); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 261 | |
| 262 | AutoVersion auto_version; |
Austin Schuh | 7eed2de | 2019-05-25 14:34:40 -0700 | [diff] [blame] | 263 | auto_mode_fetcher_.Fetch(); |
| 264 | if (!auto_mode_fetcher_.get()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 265 | AOS_LOG(WARNING, "not sure which auto mode to use\n"); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 266 | 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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 271 | if (auto_mode_fetcher_->voltage() < kSelectorStep + kSelectorMin) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 272 | auto_version = AutoVersion::kSingleHot; |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 273 | } else if (auto_mode_fetcher_->voltage() < |
| 274 | 2 * kSelectorStep + kSelectorMin) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 275 | auto_version = AutoVersion::kStraight; |
| 276 | } else { |
| 277 | auto_version = AutoVersion::kDoubleHot; |
| 278 | } |
| 279 | } |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 280 | AOS_LOG(INFO, "running auto %" PRIu8 "\n", |
| 281 | static_cast<uint8_t>(auto_version)); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 282 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 283 | const ProfileParametersT drive_params = |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 284 | (auto_version == AutoVersion::kStraight) ? kFastDrive : kSlowDrive; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 285 | const ProfileParametersT drive_with_ball_params = |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 286 | (auto_version == AutoVersion::kStraight) ? kFastWithBallDrive |
| 287 | : kSlowWithBallDrive; |
| 288 | |
Austin Schuh | a3e576b | 2019-05-22 21:22:23 -0700 | [diff] [blame] | 289 | HotGoalDecoder hot_goal_decoder(&hot_goal_fetcher_); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 290 | // 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 Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 296 | AOS_LOG(INFO, "Claw going up at %f\n", |
| 297 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 298 | 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 Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 304 | AOS_LOG(INFO, "Readying claw for shot at %f\n", |
| 305 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 306 | |
| 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 Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 312 | AOS_LOG(INFO, "Waiting until drivetrain is finished\n"); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 313 | WaitForDriveProfileDone(); |
| 314 | if (ShouldCancel()) return true; |
| 315 | |
| 316 | hot_goal_decoder.Update(); |
| 317 | if (hot_goal_decoder.is_left()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 318 | AOS_LOG(INFO, "first shot left\n"); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 319 | first_shot_left = true; |
| 320 | second_shot_left_default = false; |
| 321 | } else if (hot_goal_decoder.is_right()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 322 | AOS_LOG(INFO, "first shot right\n"); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 323 | first_shot_left = false; |
| 324 | second_shot_left_default = true; |
| 325 | } else { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 326 | AOS_LOG(INFO, "first shot defaulting left\n"); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 327 | 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 Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 333 | drive_with_ball_params, kFastTurn); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 334 | 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 Schuh | a3e576b | 2019-05-22 21:22:23 -0700 | [diff] [blame] | 340 | hot_goal_decoder.Update(); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 341 | if (ShouldCancel()) return true; |
| 342 | } while (!hot_goal_decoder.left_triggered() && |
Austin Schuh | 77ed543 | 2019-07-07 20:41:36 -0700 | [diff] [blame] | 343 | (monotonic_now() - start_time) < chrono::seconds(9)); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 344 | } else if (auto_version == AutoVersion::kStraight) { |
| 345 | this_thread::sleep_for(chrono::milliseconds(400)); |
| 346 | } |
| 347 | |
| 348 | // Shoot. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 349 | AOS_LOG(INFO, "Shooting at %f\n", |
| 350 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 351 | 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 Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 361 | AOS_LOG(INFO, "auto done at %f\n", |
| 362 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 363 | PositionClawVertically(0.0, 0.0); |
| 364 | return true; |
| 365 | } |
| 366 | |
| 367 | { |
| 368 | if (ShouldCancel()) return true; |
| 369 | // Intake the new ball. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 370 | AOS_LOG(INFO, "Claw ready for intake at %f\n", |
| 371 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 372 | PositionClawBackIntake(); |
| 373 | StartDrive(kShootDistance + kPickupDistance, 0.0, drive_params, kFastTurn); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 374 | AOS_LOG(INFO, "Waiting until drivetrain is finished\n"); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 375 | WaitForDriveProfileDone(); |
| 376 | if (ShouldCancel()) return true; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 377 | AOS_LOG(INFO, "Wait for the claw at %f\n", |
| 378 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 379 | if (!WaitUntilClawDone()) return true; |
| 380 | } |
| 381 | |
| 382 | // Drive back. |
| 383 | { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 384 | AOS_LOG(INFO, "Driving back at %f\n", |
| 385 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 386 | 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 Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 394 | AOS_LOG(INFO, "Waiting until drivetrain is finished\n"); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 395 | 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 Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 402 | AOS_LOG(INFO, "second shot left\n"); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 403 | second_shot_left = true; |
| 404 | } else if (hot_goal_decoder.is_right()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 405 | AOS_LOG(INFO, "second shot right\n"); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 406 | second_shot_left = false; |
| 407 | } else { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 408 | AOS_LOG(INFO, "second shot defaulting %s\n", |
| 409 | second_shot_left_default ? "left" : "right"); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 410 | 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 Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 422 | AOS_LOG(INFO, "Shooting at %f\n", |
| 423 | ::aos::time::DurationInSeconds(monotonic_now() - start_time)); |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 424 | // 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 Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 434 | } // namespace y2014::actors |