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