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