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