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