Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 3 | #include <chrono> |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 4 | #include <memory> |
| 5 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 6 | #include "aos/util/phased_loop.h" |
| 7 | #include "aos/time/time.h" |
| 8 | #include "aos/util/trapezoid_profile.h" |
| 9 | #include "aos/logging/logging.h" |
| 10 | #include "aos/logging/queue_logging.h" |
| 11 | #include "aos/actions/actions.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 12 | |
| 13 | #include "frc971/autonomous/auto.q.h" |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 14 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Brian Silverman | bfa0060 | 2015-05-17 01:41:29 -0400 | [diff] [blame] | 15 | #include "y2014/actors/drivetrain_actor.h" |
Austin Schuh | 3130b37 | 2016-02-17 00:34:51 -0800 | [diff] [blame] | 16 | #include "y2014/actors/shoot_actor.h" |
| 17 | #include "y2014/constants.h" |
| 18 | #include "y2014/control_loops/claw/claw.q.h" |
| 19 | #include "y2014/control_loops/drivetrain/drivetrain_dog_motor_plant.h" |
| 20 | #include "y2014/control_loops/shooter/shooter.q.h" |
Brian Silverman | bfa0060 | 2015-05-17 01:41:29 -0400 | [diff] [blame] | 21 | #include "y2014/queues/auto_mode.q.h" |
| 22 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 23 | #include "y2014/queues/hot_goal.q.h" |
Brian Silverman | bfa0060 | 2015-05-17 01:41:29 -0400 | [diff] [blame] | 24 | #include "y2014/queues/profile_params.q.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 25 | |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 26 | namespace y2014 { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 27 | namespace autonomous { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 28 | namespace chrono = ::std::chrono; |
| 29 | namespace this_thread = ::std::this_thread; |
| 30 | using ::aos::monotonic_clock; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 31 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 32 | namespace { |
| 33 | |
| 34 | double DoubleSeconds(monotonic_clock::duration duration) { |
| 35 | return ::std::chrono::duration_cast<::std::chrono::duration<double>>(duration) |
| 36 | .count(); |
| 37 | } |
| 38 | |
| 39 | } // namespace |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 40 | |
| 41 | static double left_initial_position, right_initial_position; |
| 42 | |
| 43 | bool ShouldExitAuto() { |
| 44 | ::frc971::autonomous::autonomous.FetchLatest(); |
| 45 | bool ans = !::frc971::autonomous::autonomous->run_auto; |
| 46 | if (ans) { |
| 47 | LOG(INFO, "Time to exit auto mode\n"); |
| 48 | } |
| 49 | return ans; |
| 50 | } |
| 51 | |
| 52 | void StopDrivetrain() { |
| 53 | LOG(INFO, "Stopping the drivetrain\n"); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 54 | frc971::control_loops::drivetrain_queue.goal.MakeWithBuilder() |
Austin Schuh | 78379ea | 2019-01-04 20:39:45 -0800 | [diff] [blame] | 55 | .controller_type(1) |
Austin Schuh | 86f895e | 2015-11-08 13:40:51 -0800 | [diff] [blame] | 56 | .highgear(true) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 57 | .left_goal(left_initial_position) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 58 | .right_goal(right_initial_position) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 59 | .quickturn(false) |
| 60 | .Send(); |
| 61 | } |
| 62 | |
| 63 | void ResetDrivetrain() { |
| 64 | LOG(INFO, "resetting the drivetrain\n"); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 65 | ::frc971::control_loops::drivetrain_queue.goal.MakeWithBuilder() |
Austin Schuh | 78379ea | 2019-01-04 20:39:45 -0800 | [diff] [blame] | 66 | .controller_type(0) |
Austin Schuh | 86f895e | 2015-11-08 13:40:51 -0800 | [diff] [blame] | 67 | .highgear(true) |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 68 | .wheel(0.0) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 69 | .throttle(0.0) |
| 70 | .left_goal(left_initial_position) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 71 | .right_goal(right_initial_position) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 72 | .Send(); |
| 73 | } |
| 74 | |
| 75 | void WaitUntilDoneOrCanceled( |
| 76 | ::std::unique_ptr<aos::common::actions::Action> action) { |
| 77 | if (!action) { |
| 78 | LOG(ERROR, "No action, not waiting\n"); |
| 79 | return; |
| 80 | } |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 81 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(10), |
| 82 | ::std::chrono::milliseconds(10) / 2); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 83 | while (true) { |
| 84 | // Poll the running bit and auto done bits. |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 85 | phased_loop.SleepUntilNext(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 86 | if (!action->Running() || ShouldExitAuto()) { |
| 87 | return; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void StepDrive(double distance, double theta) { |
| 93 | double left_goal = (left_initial_position + distance - |
Austin Schuh | 3130b37 | 2016-02-17 00:34:51 -0800 | [diff] [blame] | 94 | theta * control_loops::drivetrain::kRobotRadius); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 95 | double right_goal = (right_initial_position + distance + |
Austin Schuh | 3130b37 | 2016-02-17 00:34:51 -0800 | [diff] [blame] | 96 | theta * control_loops::drivetrain::kRobotRadius); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 97 | ::frc971::control_loops::drivetrain_queue.goal.MakeWithBuilder() |
Austin Schuh | 78379ea | 2019-01-04 20:39:45 -0800 | [diff] [blame] | 98 | .controller_type(1) |
Austin Schuh | 86f895e | 2015-11-08 13:40:51 -0800 | [diff] [blame] | 99 | .highgear(true) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 100 | .left_goal(left_goal) |
| 101 | .right_goal(right_goal) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 102 | .Send(); |
| 103 | left_initial_position = left_goal; |
| 104 | right_initial_position = right_goal; |
| 105 | } |
| 106 | |
Brian Silverman | bfa0060 | 2015-05-17 01:41:29 -0400 | [diff] [blame] | 107 | void PositionClawVertically(double intake_power = 0.0, double centering_power = 0.0) { |
| 108 | if (!control_loops::claw_queue.goal.MakeWithBuilder() |
| 109 | .bottom_angle(0.0) |
| 110 | .separation_angle(0.0) |
| 111 | .intake(intake_power) |
| 112 | .centering(centering_power) |
| 113 | .Send()) { |
| 114 | LOG(WARNING, "sending claw goal failed\n"); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void PositionClawBackIntake() { |
| 119 | if (!control_loops::claw_queue.goal.MakeWithBuilder() |
| 120 | .bottom_angle(-2.273474) |
| 121 | .separation_angle(0.0) |
| 122 | .intake(12.0) |
| 123 | .centering(12.0) |
| 124 | .Send()) { |
| 125 | LOG(WARNING, "sending claw goal failed\n"); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void PositionClawUpClosed() { |
| 130 | // Move the claw to where we're going to shoot from but keep it closed until |
| 131 | // it gets there. |
| 132 | if (!control_loops::claw_queue.goal.MakeWithBuilder() |
| 133 | .bottom_angle(0.86) |
| 134 | .separation_angle(0.0) |
| 135 | .intake(4.0) |
| 136 | .centering(1.0) |
| 137 | .Send()) { |
| 138 | LOG(WARNING, "sending claw goal failed\n"); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void PositionClawForShot() { |
| 143 | if (!control_loops::claw_queue.goal.MakeWithBuilder() |
| 144 | .bottom_angle(0.86) |
| 145 | .separation_angle(0.10) |
| 146 | .intake(4.0) |
| 147 | .centering(1.0) |
| 148 | .Send()) { |
| 149 | LOG(WARNING, "sending claw goal failed\n"); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void SetShotPower(double power) { |
| 154 | LOG(INFO, "Setting shot power to %f\n", power); |
| 155 | if (!control_loops::shooter_queue.goal.MakeWithBuilder() |
| 156 | .shot_power(power) |
| 157 | .shot_requested(false) |
| 158 | .unload_requested(false) |
| 159 | .load_requested(false) |
| 160 | .Send()) { |
| 161 | LOG(WARNING, "sending shooter goal failed\n"); |
| 162 | } |
| 163 | } |
| 164 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 165 | void WaitUntilNear(double distance) { |
| 166 | while (true) { |
| 167 | if (ShouldExitAuto()) return; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 168 | ::frc971::control_loops::drivetrain_queue.status.FetchAnother(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 169 | double left_error = ::std::abs( |
| 170 | left_initial_position - |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 171 | ::frc971::control_loops::drivetrain_queue.status->estimated_left_position); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 172 | double right_error = ::std::abs( |
| 173 | right_initial_position - |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 174 | ::frc971::control_loops::drivetrain_queue.status->estimated_right_position); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 175 | const double kPositionThreshold = 0.05 + distance; |
| 176 | if (right_error < kPositionThreshold && left_error < kPositionThreshold) { |
| 177 | LOG(INFO, "At the goal\n"); |
| 178 | return; |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
Brian Silverman | bfa0060 | 2015-05-17 01:41:29 -0400 | [diff] [blame] | 183 | const ProfileParams kFastDrive = {3.0, 2.5}; |
| 184 | const ProfileParams kSlowDrive = {2.5, 2.5}; |
| 185 | const ProfileParams kFastWithBallDrive = {3.0, 2.0}; |
| 186 | const ProfileParams kSlowWithBallDrive = {2.5, 2.0}; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 187 | const ProfileParams kFastTurn = {3.0, 10.0}; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 188 | |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 189 | ::std::unique_ptr<::y2014::actors::DrivetrainAction> SetDriveGoal( |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 190 | double distance, const ProfileParams drive_params, double theta = 0, |
| 191 | const ProfileParams &turn_params = kFastTurn) { |
| 192 | LOG(INFO, "Driving to %f\n", distance); |
| 193 | |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 194 | ::y2014::actors::DrivetrainActionParams params; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 195 | params.left_initial_position = left_initial_position; |
| 196 | params.right_initial_position = right_initial_position; |
| 197 | params.y_offset = distance; |
| 198 | params.theta_offset = theta; |
| 199 | params.maximum_turn_acceleration = turn_params.acceleration; |
| 200 | params.maximum_turn_velocity = turn_params.velocity; |
| 201 | params.maximum_velocity = drive_params.velocity; |
| 202 | params.maximum_acceleration = drive_params.acceleration; |
| 203 | auto drivetrain_action = actors::MakeDrivetrainAction(params); |
| 204 | drivetrain_action->Start(); |
| 205 | left_initial_position += |
Austin Schuh | 3130b37 | 2016-02-17 00:34:51 -0800 | [diff] [blame] | 206 | distance - theta * control_loops::drivetrain::kRobotRadius; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 207 | right_initial_position += |
Austin Schuh | 3130b37 | 2016-02-17 00:34:51 -0800 | [diff] [blame] | 208 | distance + theta * control_loops::drivetrain::kRobotRadius; |
sabina | f558432 | 2017-09-23 18:37:19 -0700 | [diff] [blame] | 209 | return drivetrain_action; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void Shoot() { |
| 213 | // Shoot. |
Brian Silverman | bfa0060 | 2015-05-17 01:41:29 -0400 | [diff] [blame] | 214 | auto shoot_action = actors::MakeShootAction(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 215 | shoot_action->Start(); |
Brian Silverman | bfa0060 | 2015-05-17 01:41:29 -0400 | [diff] [blame] | 216 | WaitUntilDoneOrCanceled(::std::move(shoot_action)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | void InitializeEncoders() { |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 220 | ::frc971::control_loops::drivetrain_queue.status.FetchAnother(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 221 | left_initial_position = |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 222 | ::frc971::control_loops::drivetrain_queue.status->estimated_left_position; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 223 | right_initial_position = |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 224 | ::frc971::control_loops::drivetrain_queue.status->estimated_right_position; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | void WaitUntilClawDone() { |
| 228 | while (true) { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 229 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(10), |
| 230 | ::std::chrono::milliseconds(10) / 2); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 231 | // Poll the running bit and auto done bits. |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 232 | phased_loop.SleepUntilNext(); |
Brian Silverman | be5ded6 | 2015-05-14 00:23:49 -0400 | [diff] [blame] | 233 | control_loops::claw_queue.status.FetchLatest(); |
| 234 | control_loops::claw_queue.goal.FetchLatest(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 235 | if (ShouldExitAuto()) { |
| 236 | return; |
| 237 | } |
Brian Silverman | be5ded6 | 2015-05-14 00:23:49 -0400 | [diff] [blame] | 238 | if (control_loops::claw_queue.status.get() == nullptr || |
| 239 | control_loops::claw_queue.goal.get() == nullptr) { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 240 | continue; |
| 241 | } |
| 242 | bool ans = |
Brian Silverman | be5ded6 | 2015-05-14 00:23:49 -0400 | [diff] [blame] | 243 | control_loops::claw_queue.status->zeroed && |
| 244 | (::std::abs(control_loops::claw_queue.status->bottom_velocity) < |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 245 | 1.0) && |
Brian Silverman | be5ded6 | 2015-05-14 00:23:49 -0400 | [diff] [blame] | 246 | (::std::abs(control_loops::claw_queue.status->bottom - |
| 247 | control_loops::claw_queue.goal->bottom_angle) < |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 248 | 0.10) && |
Brian Silverman | be5ded6 | 2015-05-14 00:23:49 -0400 | [diff] [blame] | 249 | (::std::abs(control_loops::claw_queue.status->separation - |
| 250 | control_loops::claw_queue.goal->separation_angle) < |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 251 | 0.4); |
| 252 | if (ans) { |
| 253 | return; |
| 254 | } |
| 255 | if (ShouldExitAuto()) return; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | class HotGoalDecoder { |
| 260 | public: |
| 261 | HotGoalDecoder() { |
| 262 | ResetCounts(); |
| 263 | } |
| 264 | |
| 265 | void ResetCounts() { |
| 266 | hot_goal.FetchLatest(); |
| 267 | if (hot_goal.get()) { |
| 268 | start_counts_ = *hot_goal; |
| 269 | LOG_STRUCT(INFO, "counts reset to", start_counts_); |
| 270 | start_counts_valid_ = true; |
| 271 | } else { |
| 272 | LOG(WARNING, "no hot goal message. ignoring\n"); |
| 273 | start_counts_valid_ = false; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | void Update(bool block = false) { |
| 278 | if (block) { |
| 279 | hot_goal.FetchAnother(); |
| 280 | } else { |
| 281 | hot_goal.FetchLatest(); |
| 282 | } |
| 283 | if (hot_goal.get()) LOG_STRUCT(INFO, "new counts", *hot_goal); |
| 284 | } |
| 285 | |
| 286 | bool left_triggered() const { |
| 287 | if (!start_counts_valid_ || !hot_goal.get()) return false; |
| 288 | return (hot_goal->left_count - start_counts_.left_count) > kThreshold; |
| 289 | } |
| 290 | |
| 291 | bool right_triggered() const { |
| 292 | if (!start_counts_valid_ || !hot_goal.get()) return false; |
| 293 | return (hot_goal->right_count - start_counts_.right_count) > kThreshold; |
| 294 | } |
| 295 | |
| 296 | bool is_left() const { |
| 297 | if (!start_counts_valid_ || !hot_goal.get()) return false; |
| 298 | const uint64_t left_difference = |
| 299 | hot_goal->left_count - start_counts_.left_count; |
| 300 | const uint64_t right_difference = |
| 301 | hot_goal->right_count - start_counts_.right_count; |
| 302 | if (left_difference > kThreshold) { |
| 303 | if (right_difference > kThreshold) { |
| 304 | // We've seen a lot of both, so pick the one we've seen the most of. |
| 305 | return left_difference > right_difference; |
| 306 | } else { |
| 307 | // We've seen enough left but not enough right, so go with it. |
| 308 | return true; |
| 309 | } |
| 310 | } else { |
| 311 | // We haven't seen enough left, so it's not left. |
| 312 | return false; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | bool is_right() const { |
| 317 | if (!start_counts_valid_ || !hot_goal.get()) return false; |
| 318 | const uint64_t left_difference = |
| 319 | hot_goal->left_count - start_counts_.left_count; |
| 320 | const uint64_t right_difference = |
| 321 | hot_goal->right_count - start_counts_.right_count; |
| 322 | if (right_difference > kThreshold) { |
| 323 | if (left_difference > kThreshold) { |
| 324 | // We've seen a lot of both, so pick the one we've seen the most of. |
| 325 | return right_difference > left_difference; |
| 326 | } else { |
| 327 | // We've seen enough right but not enough left, so go with it. |
| 328 | return true; |
| 329 | } |
| 330 | } else { |
| 331 | // We haven't seen enough right, so it's not right. |
| 332 | return false; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | private: |
| 337 | static const uint64_t kThreshold = 5; |
| 338 | |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 339 | ::y2014::HotGoal start_counts_; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 340 | bool start_counts_valid_; |
| 341 | }; |
| 342 | |
| 343 | void HandleAuto() { |
| 344 | enum class AutoVersion : uint8_t { |
| 345 | kStraight, |
| 346 | kDoubleHot, |
| 347 | kSingleHot, |
| 348 | }; |
| 349 | |
| 350 | // The front of the robot is 1.854 meters from the wall |
| 351 | static const double kShootDistance = 3.15; |
| 352 | static const double kPickupDistance = 0.5; |
| 353 | static const double kTurnAngle = 0.3; |
| 354 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 355 | monotonic_clock::time_point start_time = monotonic_clock::now(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 356 | LOG(INFO, "Handling auto mode\n"); |
| 357 | |
| 358 | AutoVersion auto_version; |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 359 | ::y2014::sensors::auto_mode.FetchLatest(); |
| 360 | if (!::y2014::sensors::auto_mode.get()) { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 361 | LOG(WARNING, "not sure which auto mode to use\n"); |
| 362 | auto_version = AutoVersion::kStraight; |
| 363 | } else { |
| 364 | static const double kSelectorMin = 0.2, kSelectorMax = 4.4; |
| 365 | |
| 366 | const double kSelectorStep = (kSelectorMax - kSelectorMin) / 3.0; |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 367 | if (::y2014::sensors::auto_mode->voltage < kSelectorStep + kSelectorMin) { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 368 | auto_version = AutoVersion::kSingleHot; |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 369 | } else if (::y2014::sensors::auto_mode->voltage < |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 370 | 2 * kSelectorStep + kSelectorMin) { |
| 371 | auto_version = AutoVersion::kStraight; |
| 372 | } else { |
| 373 | auto_version = AutoVersion::kDoubleHot; |
| 374 | } |
| 375 | } |
| 376 | LOG(INFO, "running auto %" PRIu8 "\n", static_cast<uint8_t>(auto_version)); |
| 377 | |
Brian Silverman | bfa0060 | 2015-05-17 01:41:29 -0400 | [diff] [blame] | 378 | const ProfileParams &drive_params = |
| 379 | (auto_version == AutoVersion::kStraight) ? kFastDrive : kSlowDrive; |
| 380 | const ProfileParams &drive_with_ball_params = |
| 381 | (auto_version == AutoVersion::kStraight) ? kFastWithBallDrive |
| 382 | : kSlowWithBallDrive; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 383 | |
| 384 | HotGoalDecoder hot_goal_decoder; |
| 385 | // True for left, false for right. |
| 386 | bool first_shot_left, second_shot_left_default, second_shot_left; |
| 387 | |
| 388 | ResetDrivetrain(); |
| 389 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 390 | this_thread::sleep_for(chrono::milliseconds(100)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 391 | if (ShouldExitAuto()) return; |
| 392 | InitializeEncoders(); |
| 393 | |
| 394 | // Turn the claw on, keep it straight up until the ball has been grabbed. |
| 395 | LOG(INFO, "Claw going up at %f\n", |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 396 | DoubleSeconds(monotonic_clock::now() - start_time)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 397 | PositionClawVertically(12.0, 4.0); |
| 398 | SetShotPower(115.0); |
| 399 | |
| 400 | // Wait for the ball to enter the claw. |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 401 | this_thread::sleep_for(chrono::milliseconds(250)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 402 | if (ShouldExitAuto()) return; |
| 403 | LOG(INFO, "Readying claw for shot at %f\n", |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 404 | DoubleSeconds(monotonic_clock::now() - start_time)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 405 | |
| 406 | { |
| 407 | if (ShouldExitAuto()) return; |
| 408 | // Drive to the goal. |
Brian Silverman | bfa0060 | 2015-05-17 01:41:29 -0400 | [diff] [blame] | 409 | auto drivetrain_action = SetDriveGoal(-kShootDistance, drive_params); |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 410 | this_thread::sleep_for(chrono::milliseconds(750)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 411 | PositionClawForShot(); |
| 412 | LOG(INFO, "Waiting until drivetrain is finished\n"); |
Brian Silverman | bfa0060 | 2015-05-17 01:41:29 -0400 | [diff] [blame] | 413 | WaitUntilDoneOrCanceled(::std::move(drivetrain_action)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 414 | if (ShouldExitAuto()) return; |
| 415 | } |
| 416 | |
| 417 | hot_goal_decoder.Update(); |
| 418 | if (hot_goal_decoder.is_left()) { |
| 419 | LOG(INFO, "first shot left\n"); |
| 420 | first_shot_left = true; |
| 421 | second_shot_left_default = false; |
| 422 | } else if (hot_goal_decoder.is_right()) { |
| 423 | LOG(INFO, "first shot right\n"); |
| 424 | first_shot_left = false; |
| 425 | second_shot_left_default = true; |
| 426 | } else { |
| 427 | LOG(INFO, "first shot defaulting left\n"); |
| 428 | first_shot_left = true; |
| 429 | second_shot_left_default = true; |
| 430 | } |
| 431 | if (auto_version == AutoVersion::kDoubleHot) { |
| 432 | if (ShouldExitAuto()) return; |
Brian Silverman | bfa0060 | 2015-05-17 01:41:29 -0400 | [diff] [blame] | 433 | auto drivetrain_action = SetDriveGoal( |
| 434 | 0, drive_with_ball_params, first_shot_left ? kTurnAngle : -kTurnAngle); |
| 435 | WaitUntilDoneOrCanceled(::std::move(drivetrain_action)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 436 | if (ShouldExitAuto()) return; |
| 437 | } else if (auto_version == AutoVersion::kSingleHot) { |
| 438 | do { |
| 439 | // TODO(brians): Wait for next message with timeout or something. |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 440 | this_thread::sleep_for(chrono::milliseconds(3)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 441 | hot_goal_decoder.Update(false); |
| 442 | if (ShouldExitAuto()) return; |
| 443 | } while (!hot_goal_decoder.left_triggered() && |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 444 | (monotonic_clock::now() - start_time) < chrono::seconds(9)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 445 | } else if (auto_version == AutoVersion::kStraight) { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 446 | this_thread::sleep_for(chrono::milliseconds(400)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | // Shoot. |
| 450 | LOG(INFO, "Shooting at %f\n", |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 451 | DoubleSeconds(monotonic_clock::now() - start_time)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 452 | Shoot(); |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 453 | this_thread::sleep_for(chrono::milliseconds(50)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 454 | |
| 455 | if (auto_version == AutoVersion::kDoubleHot) { |
| 456 | if (ShouldExitAuto()) return; |
Brian Silverman | bfa0060 | 2015-05-17 01:41:29 -0400 | [diff] [blame] | 457 | auto drivetrain_action = SetDriveGoal( |
| 458 | 0, drive_with_ball_params, first_shot_left ? -kTurnAngle : kTurnAngle); |
| 459 | WaitUntilDoneOrCanceled(::std::move(drivetrain_action)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 460 | if (ShouldExitAuto()) return; |
| 461 | } else if (auto_version == AutoVersion::kSingleHot) { |
| 462 | LOG(INFO, "auto done at %f\n", |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 463 | DoubleSeconds(monotonic_clock::now() - start_time)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 464 | PositionClawVertically(0.0, 0.0); |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | { |
| 469 | if (ShouldExitAuto()) return; |
| 470 | // Intake the new ball. |
| 471 | LOG(INFO, "Claw ready for intake at %f\n", |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 472 | DoubleSeconds(monotonic_clock::now() - start_time)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 473 | PositionClawBackIntake(); |
| 474 | auto drivetrain_action = |
Brian Silverman | bfa0060 | 2015-05-17 01:41:29 -0400 | [diff] [blame] | 475 | SetDriveGoal(kShootDistance + kPickupDistance, drive_params); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 476 | LOG(INFO, "Waiting until drivetrain is finished\n"); |
Brian Silverman | bfa0060 | 2015-05-17 01:41:29 -0400 | [diff] [blame] | 477 | WaitUntilDoneOrCanceled(::std::move(drivetrain_action)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 478 | if (ShouldExitAuto()) return; |
| 479 | LOG(INFO, "Wait for the claw at %f\n", |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 480 | DoubleSeconds(monotonic_clock::now() - start_time)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 481 | WaitUntilClawDone(); |
| 482 | if (ShouldExitAuto()) return; |
| 483 | } |
| 484 | |
| 485 | // Drive back. |
| 486 | { |
| 487 | LOG(INFO, "Driving back at %f\n", |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 488 | DoubleSeconds(monotonic_clock::now() - start_time)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 489 | auto drivetrain_action = |
Brian Silverman | bfa0060 | 2015-05-17 01:41:29 -0400 | [diff] [blame] | 490 | SetDriveGoal(-(kShootDistance + kPickupDistance), drive_params); |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 491 | this_thread::sleep_for(chrono::milliseconds(300)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 492 | hot_goal_decoder.ResetCounts(); |
| 493 | if (ShouldExitAuto()) return; |
| 494 | PositionClawUpClosed(); |
| 495 | WaitUntilClawDone(); |
| 496 | if (ShouldExitAuto()) return; |
| 497 | PositionClawForShot(); |
| 498 | LOG(INFO, "Waiting until drivetrain is finished\n"); |
Brian Silverman | bfa0060 | 2015-05-17 01:41:29 -0400 | [diff] [blame] | 499 | WaitUntilDoneOrCanceled(::std::move(drivetrain_action)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 500 | if (ShouldExitAuto()) return; |
| 501 | WaitUntilClawDone(); |
| 502 | if (ShouldExitAuto()) return; |
| 503 | } |
| 504 | |
| 505 | hot_goal_decoder.Update(); |
| 506 | if (hot_goal_decoder.is_left()) { |
| 507 | LOG(INFO, "second shot left\n"); |
| 508 | second_shot_left = true; |
| 509 | } else if (hot_goal_decoder.is_right()) { |
| 510 | LOG(INFO, "second shot right\n"); |
| 511 | second_shot_left = false; |
| 512 | } else { |
| 513 | LOG(INFO, "second shot defaulting %s\n", |
| 514 | second_shot_left_default ? "left" : "right"); |
| 515 | second_shot_left = second_shot_left_default; |
| 516 | } |
| 517 | if (auto_version == AutoVersion::kDoubleHot) { |
| 518 | if (ShouldExitAuto()) return; |
Brian Silverman | bfa0060 | 2015-05-17 01:41:29 -0400 | [diff] [blame] | 519 | auto drivetrain_action = SetDriveGoal( |
| 520 | 0, drive_params, second_shot_left ? kTurnAngle : -kTurnAngle); |
| 521 | WaitUntilDoneOrCanceled(::std::move(drivetrain_action)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 522 | if (ShouldExitAuto()) return; |
| 523 | } else if (auto_version == AutoVersion::kStraight) { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 524 | this_thread::sleep_for(chrono::milliseconds(400)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | LOG(INFO, "Shooting at %f\n", |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 528 | DoubleSeconds(monotonic_clock::now() - start_time)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 529 | // Shoot |
| 530 | Shoot(); |
| 531 | if (ShouldExitAuto()) return; |
| 532 | |
| 533 | // Get ready to zero when we come back up. |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 534 | this_thread::sleep_for(chrono::milliseconds(50)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 535 | PositionClawVertically(0.0, 0.0); |
| 536 | } |
| 537 | |
| 538 | } // namespace autonomous |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 539 | } // namespace y2014 |