Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 1 | #include "stdio.h" |
| 2 | |
| 3 | #include "aos/aos_core.h" |
| 4 | #include "aos/common/control_loop/Timing.h" |
| 5 | #include "aos/common/time.h" |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 6 | #include "aos/common/util/trapezoid_profile.h" |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 7 | #include "frc971/autonomous/auto.q.h" |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 8 | #include "aos/common/messages/RobotState.q.h" |
| 9 | #include "frc971/constants.h" |
| 10 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 11 | #include "frc971/control_loops/wrist/wrist_motor.q.h" |
| 12 | #include "frc971/control_loops/index/index_motor.q.h" |
| 13 | #include "frc971/control_loops/shooter/shooter_motor.q.h" |
| 14 | #include "frc971/control_loops/angle_adjust/angle_adjust_motor.q.h" |
| 15 | |
| 16 | using ::aos::time::Time; |
| 17 | |
| 18 | namespace frc971 { |
| 19 | namespace autonomous { |
| 20 | |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 21 | static double left_initial_position, right_initial_position; |
| 22 | |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 23 | bool ShouldExitAuto() { |
| 24 | ::frc971::autonomous::autonomous.FetchLatest(); |
| 25 | bool ans = !::frc971::autonomous::autonomous->run_auto; |
| 26 | if (ans) { |
| 27 | LOG(INFO, "Time to exit auto mode\n"); |
| 28 | } |
| 29 | return ans; |
| 30 | } |
| 31 | |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 32 | void SetShooterVelocity(double velocity) { |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 33 | LOG(INFO, "Setting shooter velocity to %f\n", velocity); |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 34 | control_loops::shooter.goal.MakeWithBuilder() |
| 35 | .velocity(velocity).Send(); |
| 36 | } |
| 37 | |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 38 | void SetWristGoal(double goal) { |
| 39 | LOG(INFO, "Setting wrist to %f\n", goal); |
| 40 | control_loops::wrist.goal.MakeWithBuilder() |
| 41 | .goal(goal).Send(); |
| 42 | } |
| 43 | |
| 44 | void SetAngle_AdjustGoal(double goal) { |
| 45 | LOG(INFO, "Setting angle adjust to %f\n", goal); |
| 46 | control_loops::angle_adjust.goal.MakeWithBuilder() |
| 47 | .goal(goal).Send(); |
| 48 | } |
| 49 | |
| 50 | void StartIndex() { |
| 51 | LOG(INFO, "Starting index\n"); |
| 52 | control_loops::index_loop.goal.MakeWithBuilder() |
Brian Silverman | 180e2b8 | 2013-04-08 14:29:56 -0700 | [diff] [blame] | 53 | .goal_state(2) |
| 54 | .force_fire(false) |
| 55 | .override_index(false) |
| 56 | .Send(); |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 59 | void PreloadIndex() { |
| 60 | LOG(INFO, "Preloading index\n"); |
| 61 | control_loops::index_loop.goal.MakeWithBuilder() |
Brian Silverman | 180e2b8 | 2013-04-08 14:29:56 -0700 | [diff] [blame] | 62 | .goal_state(3) |
| 63 | .force_fire(false) |
| 64 | .override_index(false) |
| 65 | .Send(); |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 68 | void ShootIndex() { |
| 69 | LOG(INFO, "Shooting index\n"); |
| 70 | control_loops::index_loop.goal.MakeWithBuilder() |
Brian Silverman | 180e2b8 | 2013-04-08 14:29:56 -0700 | [diff] [blame] | 71 | .goal_state(4) |
| 72 | .force_fire(false) |
| 73 | .override_index(false) |
| 74 | .Send(); |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Brian Silverman | b8d389f | 2013-03-19 22:54:06 -0700 | [diff] [blame] | 77 | void ResetIndex() { |
| 78 | LOG(INFO, "Resetting index\n"); |
| 79 | control_loops::index_loop.goal.MakeWithBuilder() |
Brian Silverman | 180e2b8 | 2013-04-08 14:29:56 -0700 | [diff] [blame] | 80 | .goal_state(5) |
| 81 | .force_fire(false) |
| 82 | .override_index(false) |
| 83 | .Send(); |
Brian Silverman | b8d389f | 2013-03-19 22:54:06 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void WaitForIndexReset() { |
| 87 | LOG(INFO, "Waiting for the indexer to reset\n"); |
| 88 | control_loops::index_loop.status.FetchLatest(); |
| 89 | |
| 90 | // Fetch a couple index status packets to make sure that the indexer has run. |
| 91 | for (int i = 0; i < 5; ++i) { |
| 92 | LOG(DEBUG, "Fetching another index status packet\n"); |
| 93 | control_loops::index_loop.status.FetchNextBlocking(); |
| 94 | if (ShouldExitAuto()) return; |
| 95 | } |
| 96 | LOG(INFO, "Indexer is now reset.\n"); |
| 97 | } |
| 98 | |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 99 | void WaitForWrist() { |
| 100 | LOG(INFO, "Waiting for the wrist\n"); |
| 101 | control_loops::wrist.status.FetchLatest(); |
| 102 | |
| 103 | while (!control_loops::wrist.status.get()) { |
| 104 | LOG(WARNING, "No previous wrist packet, trying to fetch again\n"); |
| 105 | control_loops::wrist.status.FetchNextBlocking(); |
| 106 | } |
| 107 | |
| 108 | while (!control_loops::wrist.status->done) { |
| 109 | control_loops::wrist.status.FetchNextBlocking(); |
| 110 | LOG(DEBUG, "Got a new wrist status packet\n"); |
| 111 | if (ShouldExitAuto()) return; |
| 112 | } |
| 113 | LOG(INFO, "Done waiting for the wrist\n"); |
| 114 | } |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 115 | void WaitForIndex() { |
| 116 | LOG(INFO, "Waiting for the indexer to be ready to intake\n"); |
| 117 | control_loops::index_loop.status.FetchLatest(); |
| 118 | |
| 119 | while (!control_loops::index_loop.status.get()) { |
| 120 | LOG(WARNING, "No previous index packet, trying to fetch again\n"); |
| 121 | control_loops::index_loop.status.FetchNextBlocking(); |
| 122 | } |
| 123 | |
| 124 | while (!control_loops::index_loop.status->ready_to_intake) { |
| 125 | control_loops::index_loop.status.FetchNextBlocking(); |
| 126 | if (ShouldExitAuto()) return; |
| 127 | } |
| 128 | LOG(INFO, "Indexer ready to intake\n"); |
| 129 | } |
| 130 | |
| 131 | void WaitForAngle_Adjust() { |
| 132 | LOG(INFO, "Waiting for the angle adjuster to finish\n"); |
| 133 | control_loops::angle_adjust.status.FetchLatest(); |
| 134 | |
| 135 | while (!control_loops::angle_adjust.status.get()) { |
| 136 | LOG(WARNING, "No previous angle adjust packet, trying to fetch again\n"); |
| 137 | control_loops::angle_adjust.status.FetchNextBlocking(); |
| 138 | } |
| 139 | |
| 140 | while (!control_loops::angle_adjust.status->done) { |
| 141 | control_loops::angle_adjust.status.FetchNextBlocking(); |
| 142 | if (ShouldExitAuto()) return; |
| 143 | } |
| 144 | LOG(INFO, "Angle adjuster done\n"); |
| 145 | } |
| 146 | |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 147 | void WaitForShooter() { |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 148 | LOG(INFO, "Waiting for the shooter to spin up\n"); |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 149 | control_loops::shooter.status.FetchLatest(); |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 150 | |
| 151 | while (!control_loops::shooter.status.get()) { |
| 152 | LOG(WARNING, "No previous shooteracket, trying to fetch again\n"); |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 153 | control_loops::shooter.status.FetchNextBlocking(); |
| 154 | } |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 155 | |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 156 | while (!control_loops::shooter.status->ready) { |
| 157 | control_loops::shooter.status.FetchNextBlocking(); |
| 158 | if (ShouldExitAuto()) return; |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 159 | } |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 160 | LOG(INFO, "Shooter ready to shoot\n"); |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 163 | void ShootNDiscs(int n) { |
| 164 | LOG(INFO, "Waiting until %d discs have been shot\n", n); |
| 165 | control_loops::index_loop.status.FetchLatest(); |
| 166 | |
| 167 | while (!control_loops::index_loop.status.get()) { |
| 168 | LOG(WARNING, "No previous index_loop packet, trying to fetch again\n"); |
| 169 | control_loops::index_loop.status.FetchNextBlocking(); |
| 170 | } |
| 171 | |
| 172 | int final_disc_count = control_loops::index_loop.status->shot_disc_count + n; |
| 173 | LOG(DEBUG, "Disc count should be %d when done\n", final_disc_count); |
| 174 | while (final_disc_count > control_loops::index_loop.status->shot_disc_count) { |
| 175 | control_loops::index_loop.status.FetchNextBlocking(); |
| 176 | if (ShouldExitAuto()) return; |
| 177 | } |
| 178 | LOG(INFO, "Shot %d discs\n", n); |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 181 | void StopDrivetrain() { |
| 182 | LOG(INFO, "Stopping the drivetrain\n"); |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 183 | control_loops::drivetrain.goal.MakeWithBuilder() |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 184 | .control_loop_driving(true) |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 185 | .left_goal(left_initial_position) |
| 186 | .left_velocity_goal(0) |
| 187 | .right_goal(right_initial_position) |
| 188 | .right_velocity_goal(0) |
| 189 | .quickturn(false) |
| 190 | .Send(); |
| 191 | } |
| 192 | |
| 193 | void ResetDrivetrain() { |
| 194 | LOG(INFO, "resetting the drivetrain\n"); |
| 195 | control_loops::drivetrain.goal.MakeWithBuilder() |
| 196 | .control_loop_driving(false) |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 197 | .highgear(false) |
| 198 | .steering(0.0) |
| 199 | .throttle(0.0) |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 200 | .Send(); |
| 201 | } |
| 202 | |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 203 | void SetDriveGoal(double yoffset, double maximum_velocity = 1.5) { |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 204 | LOG(INFO, "Going to move %f\n", yoffset); |
| 205 | |
| 206 | // Measured conversion to get the distance right. |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 207 | ::aos::util::TrapezoidProfile profile(::aos::time::Time::InMS(10)); |
| 208 | ::Eigen::Matrix<double, 2, 1> driveTrainState; |
| 209 | const double goal_velocity = 0.0; |
| 210 | const double epsilon = 0.01; |
| 211 | |
Austin Schuh | fae5336 | 2013-03-23 04:39:48 +0000 | [diff] [blame] | 212 | profile.set_maximum_acceleration(2.0); |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 213 | profile.set_maximum_velocity(maximum_velocity); |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 214 | |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 215 | while (true) { |
Brian Silverman | 7f09f97 | 2013-03-22 23:11:39 -0700 | [diff] [blame] | 216 | ::aos::time::PhasedLoop10MS(5000); // wait until next 10ms tick |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 217 | driveTrainState = profile.Update(yoffset, goal_velocity); |
| 218 | |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 219 | if (::std::abs(driveTrainState(0, 0) - yoffset) < epsilon) break; |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 220 | if (ShouldExitAuto()) return; |
| 221 | |
| 222 | LOG(DEBUG, "Driving left to %f, right to %f\n", |
| 223 | driveTrainState(0, 0) + left_initial_position, |
| 224 | driveTrainState(0, 0) + right_initial_position); |
| 225 | control_loops::drivetrain.goal.MakeWithBuilder() |
| 226 | .control_loop_driving(true) |
| 227 | .highgear(false) |
| 228 | .left_goal(driveTrainState(0, 0) + left_initial_position) |
| 229 | .right_goal(driveTrainState(0, 0) + right_initial_position) |
| 230 | .left_velocity_goal(driveTrainState(1, 0)) |
| 231 | .right_velocity_goal(driveTrainState(1, 0)) |
| 232 | .Send(); |
| 233 | } |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 234 | left_initial_position += yoffset; |
| 235 | right_initial_position += yoffset; |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 236 | LOG(INFO, "Done moving\n"); |
| 237 | } |
| 238 | |
Brian Silverman | 13be668 | 2013-03-22 21:02:07 -0700 | [diff] [blame] | 239 | // Drives forward while we can pick up discs up to max_distance (in meters). |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 240 | void DriveForwardPickUp(double max_distance, double wrist_angle) { |
Brian Silverman | 13be668 | 2013-03-22 21:02:07 -0700 | [diff] [blame] | 241 | LOG(INFO, "going to pick up at a max distance of %f\n", max_distance); |
Brian Silverman | 13be668 | 2013-03-22 21:02:07 -0700 | [diff] [blame] | 242 | |
| 243 | static const ::aos::time::Time kPeriod = ::aos::time::Time::InMS(10); |
| 244 | ::aos::util::TrapezoidProfile profile(kPeriod); |
| 245 | ::Eigen::Matrix<double, 2, 1> driveTrainState; |
| 246 | const double goal_velocity = 0.0; |
| 247 | const double epsilon = 0.01; |
| 248 | static const double kMaximumAcceleration = 1.0; |
| 249 | |
| 250 | profile.set_maximum_acceleration(kMaximumAcceleration); |
| 251 | profile.set_maximum_velocity(0.6); |
| 252 | |
Brian Silverman | 13be668 | 2013-03-22 21:02:07 -0700 | [diff] [blame] | 253 | bool driving_back = false; |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 254 | const double kDestination = -0.20; |
Brian Silverman | 13be668 | 2013-03-22 21:02:07 -0700 | [diff] [blame] | 255 | |
| 256 | while (true) { |
Brian Silverman | 7f09f97 | 2013-03-22 23:11:39 -0700 | [diff] [blame] | 257 | ::aos::time::PhasedLoop10MS(5000); // wait until next 10ms tick |
Brian Silverman | c6ba6ee | 2013-03-22 21:10:38 -0700 | [diff] [blame] | 258 | driveTrainState = profile.Update(driving_back ? kDestination : max_distance, |
| 259 | goal_velocity); |
Brian Silverman | 13be668 | 2013-03-22 21:02:07 -0700 | [diff] [blame] | 260 | |
| 261 | if (ShouldExitAuto()) return; |
| 262 | |
Brian Silverman | c6ba6ee | 2013-03-22 21:10:38 -0700 | [diff] [blame] | 263 | if (driving_back) { |
| 264 | if (::std::abs(driveTrainState(0, 0)) < epsilon) break; |
| 265 | } else if (::std::abs(driveTrainState(0, 0) - max_distance) < epsilon) { |
| 266 | LOG(INFO, "went the max distance; driving back\n"); |
| 267 | driving_back = true; |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 268 | profile.set_maximum_velocity(2.5); |
| 269 | SetWristGoal(wrist_angle); |
Brian Silverman | 13be668 | 2013-03-22 21:02:07 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | if (control_loops::index_loop.status.FetchLatest()) { |
| 273 | if (control_loops::index_loop.status->hopper_disc_count >= 4) { |
| 274 | LOG(INFO, "done intaking; driving back\n"); |
| 275 | driving_back = true; |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 276 | profile.set_maximum_velocity(2.5); |
| 277 | SetWristGoal(wrist_angle); |
Brian Silverman | 13be668 | 2013-03-22 21:02:07 -0700 | [diff] [blame] | 278 | } |
| 279 | } else { |
| 280 | LOG(WARNING, "getting index status failed\n"); |
| 281 | } |
| 282 | |
| 283 | LOG(DEBUG, "Driving left to %f, right to %f\n", |
| 284 | driveTrainState(0, 0) + left_initial_position, |
| 285 | driveTrainState(0, 0) + right_initial_position); |
| 286 | control_loops::drivetrain.goal.MakeWithBuilder() |
| 287 | .control_loop_driving(true) |
| 288 | .highgear(false) |
| 289 | .left_goal(driveTrainState(0, 0) + left_initial_position) |
| 290 | .right_goal(driveTrainState(0, 0) + right_initial_position) |
| 291 | .left_velocity_goal(driveTrainState(1, 0)) |
| 292 | .right_velocity_goal(driveTrainState(1, 0)) |
| 293 | .Send(); |
| 294 | } |
Austin Schuh | fae5336 | 2013-03-23 04:39:48 +0000 | [diff] [blame] | 295 | left_initial_position += kDestination; |
| 296 | right_initial_position += kDestination; |
Brian Silverman | 13be668 | 2013-03-22 21:02:07 -0700 | [diff] [blame] | 297 | LOG(INFO, "Done moving\n"); |
| 298 | } |
| 299 | |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 300 | void DriveSpin(double radians) { |
| 301 | LOG(INFO, "going to spin %f\n", radians); |
| 302 | |
| 303 | ::aos::util::TrapezoidProfile profile(::aos::time::Time::InMS(10)); |
| 304 | ::Eigen::Matrix<double, 2, 1> driveTrainState; |
| 305 | const double goal_velocity = 0.0; |
| 306 | const double epsilon = 0.01; |
Brian Silverman | 13be668 | 2013-03-22 21:02:07 -0700 | [diff] [blame] | 307 | // in drivetrain "meters" |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 308 | const double kRobotWidth = 0.4544; |
| 309 | |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 310 | profile.set_maximum_acceleration(1.5); |
| 311 | profile.set_maximum_velocity(0.8); |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 312 | |
| 313 | const double side_offset = kRobotWidth * radians / 2.0; |
| 314 | |
| 315 | while (true) { |
Brian Silverman | 7f09f97 | 2013-03-22 23:11:39 -0700 | [diff] [blame] | 316 | ::aos::time::PhasedLoop10MS(5000); // wait until next 10ms tick |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 317 | driveTrainState = profile.Update(side_offset, goal_velocity); |
| 318 | |
| 319 | if (::std::abs(driveTrainState(0, 0) - side_offset) < epsilon) break; |
| 320 | if (ShouldExitAuto()) return; |
| 321 | |
| 322 | LOG(DEBUG, "Driving left to %f, right to %f\n", |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 323 | left_initial_position - driveTrainState(0, 0), |
| 324 | right_initial_position + driveTrainState(0, 0)); |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 325 | control_loops::drivetrain.goal.MakeWithBuilder() |
| 326 | .control_loop_driving(true) |
| 327 | .highgear(false) |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 328 | .left_goal(left_initial_position - driveTrainState(0, 0)) |
| 329 | .right_goal(right_initial_position + driveTrainState(0, 0)) |
| 330 | .left_velocity_goal(-driveTrainState(1, 0)) |
| 331 | .right_velocity_goal(driveTrainState(1, 0)) |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 332 | .Send(); |
| 333 | } |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 334 | left_initial_position -= side_offset; |
| 335 | right_initial_position += side_offset; |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 336 | LOG(INFO, "Done moving\n"); |
| 337 | } |
| 338 | |
| 339 | // start with N discs in the indexer |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 340 | void HandleAuto() { |
| 341 | LOG(INFO, "Handling auto mode\n"); |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 342 | |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 343 | double WRIST_UP; |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 344 | const double WRIST_DOWN = -0.580; |
Brian Silverman | e296ebd | 2013-04-05 13:51:13 -0700 | [diff] [blame] | 345 | const double WRIST_DOWN_TWO = WRIST_DOWN - 0.005; |
| 346 | const double ANGLE_ONE = 0.520; |
Brian Silverman | 304b2bf | 2013-04-04 17:54:41 -0700 | [diff] [blame] | 347 | const double ANGLE_TWO = 0.677; |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 348 | |
| 349 | ResetIndex(); |
| 350 | SetWristGoal(1.0); // wrist must calibrate itself on power-up |
| 351 | SetAngle_AdjustGoal(ANGLE_TWO); // make it still move a bit |
| 352 | SetShooterVelocity(0.0); // or else it keeps spinning from last time |
| 353 | ResetDrivetrain(); |
| 354 | |
Brian Silverman | 304b2bf | 2013-04-04 17:54:41 -0700 | [diff] [blame] | 355 | //::aos::time::SleepFor(::aos::time::Time::InSeconds(20)); |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 356 | if (ShouldExitAuto()) return; |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 357 | |
| 358 | ::aos::robot_state.FetchLatest(); |
Brian Silverman | c527754 | 2013-03-22 13:33:07 -0700 | [diff] [blame] | 359 | if (!::aos::robot_state.get() || |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 360 | !constants::wrist_hall_effect_start_angle(&WRIST_UP)) { |
| 361 | LOG(ERROR, "Constants not ready\n"); |
| 362 | return; |
| 363 | } |
| 364 | WRIST_UP -= 0.4; |
| 365 | LOG(INFO, "Got constants\n"); |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 366 | |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 367 | control_loops::drivetrain.position.FetchLatest(); |
| 368 | while (!control_loops::drivetrain.position.get()) { |
| 369 | LOG(WARNING, "No previous drivetrain position packet, trying to fetch again\n"); |
| 370 | control_loops::drivetrain.position.FetchNextBlocking(); |
| 371 | } |
| 372 | left_initial_position = |
| 373 | control_loops::drivetrain.position->left_encoder; |
| 374 | right_initial_position = |
| 375 | control_loops::drivetrain.position->right_encoder; |
| 376 | |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 377 | StopDrivetrain(); |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 378 | |
Brian Silverman | 7f09f97 | 2013-03-22 23:11:39 -0700 | [diff] [blame] | 379 | SetWristGoal(WRIST_UP); // wrist must calibrate itself on power-up |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 380 | SetAngle_AdjustGoal(ANGLE_ONE); |
Brian Silverman | e296ebd | 2013-04-05 13:51:13 -0700 | [diff] [blame] | 381 | SetShooterVelocity(395.0); |
Brian Silverman | b8d389f | 2013-03-19 22:54:06 -0700 | [diff] [blame] | 382 | WaitForIndexReset(); |
Brian Silverman | 7f09f97 | 2013-03-22 23:11:39 -0700 | [diff] [blame] | 383 | if (ShouldExitAuto()) return; |
| 384 | PreloadIndex(); // spin to top and put 1 disc into loader |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 385 | |
| 386 | if (ShouldExitAuto()) return; |
| 387 | WaitForWrist(); |
| 388 | if (ShouldExitAuto()) return; |
| 389 | WaitForAngle_Adjust(); |
Brian Silverman | 7f09f97 | 2013-03-22 23:11:39 -0700 | [diff] [blame] | 390 | ShootIndex(); // tilt up, shoot, repeat until empty |
| 391 | // calls WaitForShooter |
| 392 | ShootNDiscs(3); // ShootNDiscs returns if ShouldExitAuto |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 393 | if (ShouldExitAuto()) return; |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 394 | |
Brian Silverman | 7f09f97 | 2013-03-22 23:11:39 -0700 | [diff] [blame] | 395 | StartIndex(); // take in up to 4 discs |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 396 | |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 397 | if (false) { |
| 398 | const double kDistanceToCenterMeters = 3.11023; |
| 399 | const double kMaxPickupDistance = 2.5; |
| 400 | const double kTurnToCenterDegrees = 78.2; |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 401 | |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 402 | // Drive back to the center line. |
| 403 | SetDriveGoal(-kDistanceToCenterMeters); |
| 404 | if (ShouldExitAuto()) return; |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 405 | |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 406 | SetWristGoal(WRIST_DOWN); |
| 407 | // Turn towards the center. |
| 408 | DriveSpin(kTurnToCenterDegrees * M_PI / 180.0); |
| 409 | if (ShouldExitAuto()) return; |
| 410 | WaitForWrist(); |
| 411 | if (ShouldExitAuto()) return; |
Austin Schuh | fae5336 | 2013-03-23 04:39:48 +0000 | [diff] [blame] | 412 | |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 413 | // Pick up at most 4 discs and drive at most kMaxPickupDistance. |
| 414 | DriveForwardPickUp(kMaxPickupDistance, WRIST_UP); |
Austin Schuh | fae5336 | 2013-03-23 04:39:48 +0000 | [diff] [blame] | 415 | |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 416 | SetWristGoal(WRIST_UP); |
| 417 | DriveSpin(-kTurnToCenterDegrees * M_PI / 180.0); |
| 418 | if (ShouldExitAuto()) return; |
| 419 | // Drive back to where we were. |
| 420 | SetDriveGoal(kDistanceToCenterMeters); |
| 421 | if (ShouldExitAuto()) return; |
Austin Schuh | fae5336 | 2013-03-23 04:39:48 +0000 | [diff] [blame] | 422 | |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 423 | return; |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 424 | |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 425 | ShootNDiscs(4); |
| 426 | if (ShouldExitAuto()) return; |
| 427 | } else { |
Austin Schuh | 9f97ce6 | 2013-04-04 05:48:28 +0000 | [diff] [blame] | 428 | // Delay to let the disc out of the shooter. |
Brian Silverman | 304b2bf | 2013-04-04 17:54:41 -0700 | [diff] [blame] | 429 | ::aos::time::SleepFor(::aos::time::Time::InSeconds(0.25)); |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 430 | SetWristGoal(WRIST_DOWN); |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 431 | StartIndex(); // take in up to 4 discs |
| 432 | |
| 433 | if (ShouldExitAuto()) return; |
| 434 | WaitForWrist(); // wrist must be down before moving |
| 435 | ::aos::time::SleepFor(::aos::time::Time::InSeconds(0.25)); |
Austin Schuh | 9f97ce6 | 2013-04-04 05:48:28 +0000 | [diff] [blame] | 436 | SetAngle_AdjustGoal(ANGLE_TWO); |
| 437 | SetShooterVelocity(375.0); |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 438 | |
| 439 | if (ShouldExitAuto()) return; |
| 440 | WaitForIndex(); // ready to pick up discs |
| 441 | |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 442 | static const double kDriveDistance = 2.8; |
| 443 | static const double kFirstDrive = 0.27; |
| 444 | static const double kSecondShootDistance = 2.0; |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 445 | SetDriveGoal(kFirstDrive, 0.6); |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 446 | SetWristGoal(WRIST_DOWN_TWO); |
| 447 | SetDriveGoal(kDriveDistance - kFirstDrive, 2.0); |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 448 | if (ShouldExitAuto()) return; |
| 449 | |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 450 | ::aos::time::SleepFor(::aos::time::Time::InSeconds(0.5)); |
| 451 | SetDriveGoal(kSecondShootDistance - kDriveDistance, 2.0); |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 452 | PreloadIndex(); |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 453 | |
| 454 | if (ShouldExitAuto()) return; |
| 455 | WaitForAngle_Adjust(); |
| 456 | if (ShouldExitAuto()) return; |
| 457 | ShootIndex(); |
| 458 | if (ShouldExitAuto()) return; |
| 459 | } |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | } // namespace autonomous |
| 463 | } // namespace frc971 |