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) |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 173 | .highgear(false) |
| 174 | .steering(0.0) |
| 175 | .throttle(0.0) |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 176 | .left_goal(left_initial_position) |
| 177 | .right_goal(right_initial_position) |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 178 | .quickturn(false) |
| 179 | .Send(); |
| 180 | } |
| 181 | |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 182 | void SetDriveGoal(double yoffset, double maximum_velocity = 1.5) { |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 183 | LOG(INFO, "Going to move %f\n", yoffset); |
| 184 | |
| 185 | // Measured conversion to get the distance right. |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 186 | ::aos::util::TrapezoidProfile profile(::aos::time::Time::InMS(10)); |
| 187 | ::Eigen::Matrix<double, 2, 1> driveTrainState; |
| 188 | const double goal_velocity = 0.0; |
| 189 | const double epsilon = 0.01; |
| 190 | |
Austin Schuh | fae5336 | 2013-03-23 04:39:48 +0000 | [diff] [blame] | 191 | profile.set_maximum_acceleration(2.0); |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 192 | profile.set_maximum_velocity(maximum_velocity); |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 193 | |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 194 | while (true) { |
Brian Silverman | 7f09f97 | 2013-03-22 23:11:39 -0700 | [diff] [blame] | 195 | ::aos::time::PhasedLoop10MS(5000); // wait until next 10ms tick |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 196 | driveTrainState = profile.Update(yoffset, goal_velocity); |
| 197 | |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 198 | if (::std::abs(driveTrainState(0, 0) - yoffset) < epsilon) break; |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 199 | if (ShouldExitAuto()) return; |
| 200 | |
| 201 | LOG(DEBUG, "Driving left to %f, right to %f\n", |
| 202 | driveTrainState(0, 0) + left_initial_position, |
| 203 | driveTrainState(0, 0) + right_initial_position); |
| 204 | control_loops::drivetrain.goal.MakeWithBuilder() |
| 205 | .control_loop_driving(true) |
| 206 | .highgear(false) |
| 207 | .left_goal(driveTrainState(0, 0) + left_initial_position) |
| 208 | .right_goal(driveTrainState(0, 0) + right_initial_position) |
| 209 | .left_velocity_goal(driveTrainState(1, 0)) |
| 210 | .right_velocity_goal(driveTrainState(1, 0)) |
| 211 | .Send(); |
| 212 | } |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 213 | left_initial_position += yoffset; |
| 214 | right_initial_position += yoffset; |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 215 | LOG(INFO, "Done moving\n"); |
| 216 | } |
| 217 | |
Brian Silverman | 13be668 | 2013-03-22 21:02:07 -0700 | [diff] [blame] | 218 | // 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] | 219 | void DriveForwardPickUp(double max_distance, double wrist_angle) { |
Brian Silverman | 13be668 | 2013-03-22 21:02:07 -0700 | [diff] [blame] | 220 | 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] | 221 | |
| 222 | static const ::aos::time::Time kPeriod = ::aos::time::Time::InMS(10); |
| 223 | ::aos::util::TrapezoidProfile profile(kPeriod); |
| 224 | ::Eigen::Matrix<double, 2, 1> driveTrainState; |
| 225 | const double goal_velocity = 0.0; |
| 226 | const double epsilon = 0.01; |
| 227 | static const double kMaximumAcceleration = 1.0; |
| 228 | |
| 229 | profile.set_maximum_acceleration(kMaximumAcceleration); |
| 230 | profile.set_maximum_velocity(0.6); |
| 231 | |
Brian Silverman | 13be668 | 2013-03-22 21:02:07 -0700 | [diff] [blame] | 232 | bool driving_back = false; |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 233 | const double kDestination = -0.20; |
Brian Silverman | 13be668 | 2013-03-22 21:02:07 -0700 | [diff] [blame] | 234 | |
| 235 | while (true) { |
Brian Silverman | 7f09f97 | 2013-03-22 23:11:39 -0700 | [diff] [blame] | 236 | ::aos::time::PhasedLoop10MS(5000); // wait until next 10ms tick |
Brian Silverman | c6ba6ee | 2013-03-22 21:10:38 -0700 | [diff] [blame] | 237 | driveTrainState = profile.Update(driving_back ? kDestination : max_distance, |
| 238 | goal_velocity); |
Brian Silverman | 13be668 | 2013-03-22 21:02:07 -0700 | [diff] [blame] | 239 | |
| 240 | if (ShouldExitAuto()) return; |
| 241 | |
Brian Silverman | c6ba6ee | 2013-03-22 21:10:38 -0700 | [diff] [blame] | 242 | if (driving_back) { |
| 243 | if (::std::abs(driveTrainState(0, 0)) < epsilon) break; |
| 244 | } else if (::std::abs(driveTrainState(0, 0) - max_distance) < epsilon) { |
| 245 | LOG(INFO, "went the max distance; driving back\n"); |
| 246 | driving_back = true; |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 247 | profile.set_maximum_velocity(2.5); |
| 248 | SetWristGoal(wrist_angle); |
Brian Silverman | 13be668 | 2013-03-22 21:02:07 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | if (control_loops::index_loop.status.FetchLatest()) { |
| 252 | if (control_loops::index_loop.status->hopper_disc_count >= 4) { |
| 253 | LOG(INFO, "done intaking; driving back\n"); |
| 254 | driving_back = true; |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 255 | profile.set_maximum_velocity(2.5); |
| 256 | SetWristGoal(wrist_angle); |
Brian Silverman | 13be668 | 2013-03-22 21:02:07 -0700 | [diff] [blame] | 257 | } |
| 258 | } else { |
| 259 | LOG(WARNING, "getting index status failed\n"); |
| 260 | } |
| 261 | |
| 262 | LOG(DEBUG, "Driving left to %f, right to %f\n", |
| 263 | driveTrainState(0, 0) + left_initial_position, |
| 264 | driveTrainState(0, 0) + right_initial_position); |
| 265 | control_loops::drivetrain.goal.MakeWithBuilder() |
| 266 | .control_loop_driving(true) |
| 267 | .highgear(false) |
| 268 | .left_goal(driveTrainState(0, 0) + left_initial_position) |
| 269 | .right_goal(driveTrainState(0, 0) + right_initial_position) |
| 270 | .left_velocity_goal(driveTrainState(1, 0)) |
| 271 | .right_velocity_goal(driveTrainState(1, 0)) |
| 272 | .Send(); |
| 273 | } |
Austin Schuh | fae5336 | 2013-03-23 04:39:48 +0000 | [diff] [blame] | 274 | left_initial_position += kDestination; |
| 275 | right_initial_position += kDestination; |
Brian Silverman | 13be668 | 2013-03-22 21:02:07 -0700 | [diff] [blame] | 276 | LOG(INFO, "Done moving\n"); |
| 277 | } |
| 278 | |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 279 | void DriveSpin(double radians) { |
| 280 | LOG(INFO, "going to spin %f\n", radians); |
| 281 | |
| 282 | ::aos::util::TrapezoidProfile profile(::aos::time::Time::InMS(10)); |
| 283 | ::Eigen::Matrix<double, 2, 1> driveTrainState; |
| 284 | const double goal_velocity = 0.0; |
| 285 | const double epsilon = 0.01; |
Brian Silverman | 13be668 | 2013-03-22 21:02:07 -0700 | [diff] [blame] | 286 | // in drivetrain "meters" |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 287 | const double kRobotWidth = 0.4544; |
| 288 | |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 289 | profile.set_maximum_acceleration(1.5); |
| 290 | profile.set_maximum_velocity(0.8); |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 291 | |
| 292 | const double side_offset = kRobotWidth * radians / 2.0; |
| 293 | |
| 294 | while (true) { |
Brian Silverman | 7f09f97 | 2013-03-22 23:11:39 -0700 | [diff] [blame] | 295 | ::aos::time::PhasedLoop10MS(5000); // wait until next 10ms tick |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 296 | driveTrainState = profile.Update(side_offset, goal_velocity); |
| 297 | |
| 298 | if (::std::abs(driveTrainState(0, 0) - side_offset) < epsilon) break; |
| 299 | if (ShouldExitAuto()) return; |
| 300 | |
| 301 | LOG(DEBUG, "Driving left to %f, right to %f\n", |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 302 | left_initial_position - driveTrainState(0, 0), |
| 303 | right_initial_position + driveTrainState(0, 0)); |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 304 | control_loops::drivetrain.goal.MakeWithBuilder() |
| 305 | .control_loop_driving(true) |
| 306 | .highgear(false) |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 307 | .left_goal(left_initial_position - driveTrainState(0, 0)) |
| 308 | .right_goal(right_initial_position + driveTrainState(0, 0)) |
| 309 | .left_velocity_goal(-driveTrainState(1, 0)) |
| 310 | .right_velocity_goal(driveTrainState(1, 0)) |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 311 | .Send(); |
| 312 | } |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 313 | left_initial_position -= side_offset; |
| 314 | right_initial_position += side_offset; |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 315 | LOG(INFO, "Done moving\n"); |
| 316 | } |
| 317 | |
| 318 | // start with N discs in the indexer |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 319 | void HandleAuto() { |
| 320 | LOG(INFO, "Handling auto mode\n"); |
| 321 | double WRIST_UP; |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 322 | ::aos::time::SleepFor(::aos::time::Time::InSeconds(20)); |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 323 | |
| 324 | ::aos::robot_state.FetchLatest(); |
Brian Silverman | c527754 | 2013-03-22 13:33:07 -0700 | [diff] [blame] | 325 | if (!::aos::robot_state.get() || |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 326 | !constants::wrist_hall_effect_start_angle(&WRIST_UP)) { |
| 327 | LOG(ERROR, "Constants not ready\n"); |
| 328 | return; |
| 329 | } |
| 330 | WRIST_UP -= 0.4; |
| 331 | LOG(INFO, "Got constants\n"); |
| 332 | const double WRIST_DOWN = -0.633; |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 333 | const double ANGLE_ONE = 0.5434; |
| 334 | const double ANGLE_TWO = 0.685; |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 335 | |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 336 | control_loops::drivetrain.position.FetchLatest(); |
| 337 | while (!control_loops::drivetrain.position.get()) { |
| 338 | LOG(WARNING, "No previous drivetrain position packet, trying to fetch again\n"); |
| 339 | control_loops::drivetrain.position.FetchNextBlocking(); |
| 340 | } |
| 341 | left_initial_position = |
| 342 | control_loops::drivetrain.position->left_encoder; |
| 343 | right_initial_position = |
| 344 | control_loops::drivetrain.position->right_encoder; |
| 345 | |
Brian Silverman | b8d389f | 2013-03-19 22:54:06 -0700 | [diff] [blame] | 346 | ResetIndex(); |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 347 | StopDrivetrain(); |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 348 | |
Brian Silverman | 7f09f97 | 2013-03-22 23:11:39 -0700 | [diff] [blame] | 349 | SetWristGoal(WRIST_UP); // wrist must calibrate itself on power-up |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 350 | SetAngle_AdjustGoal(ANGLE_ONE); |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 351 | SetShooterVelocity(400.0); |
Brian Silverman | b8d389f | 2013-03-19 22:54:06 -0700 | [diff] [blame] | 352 | WaitForIndexReset(); |
Brian Silverman | 7f09f97 | 2013-03-22 23:11:39 -0700 | [diff] [blame] | 353 | if (ShouldExitAuto()) return; |
| 354 | PreloadIndex(); // spin to top and put 1 disc into loader |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 355 | |
| 356 | if (ShouldExitAuto()) return; |
| 357 | WaitForWrist(); |
| 358 | if (ShouldExitAuto()) return; |
| 359 | WaitForAngle_Adjust(); |
Brian Silverman | 7f09f97 | 2013-03-22 23:11:39 -0700 | [diff] [blame] | 360 | ShootIndex(); // tilt up, shoot, repeat until empty |
| 361 | // calls WaitForShooter |
| 362 | ShootNDiscs(3); // ShootNDiscs returns if ShouldExitAuto |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 363 | if (ShouldExitAuto()) return; |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 364 | |
Brian Silverman | 7f09f97 | 2013-03-22 23:11:39 -0700 | [diff] [blame] | 365 | StartIndex(); // take in up to 4 discs |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 366 | |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 367 | if (false) { |
| 368 | const double kDistanceToCenterMeters = 3.11023; |
| 369 | const double kMaxPickupDistance = 2.5; |
| 370 | const double kTurnToCenterDegrees = 78.2; |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 371 | |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 372 | // Drive back to the center line. |
| 373 | SetDriveGoal(-kDistanceToCenterMeters); |
| 374 | if (ShouldExitAuto()) return; |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 375 | |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 376 | SetWristGoal(WRIST_DOWN); |
| 377 | // Turn towards the center. |
| 378 | DriveSpin(kTurnToCenterDegrees * M_PI / 180.0); |
| 379 | if (ShouldExitAuto()) return; |
| 380 | WaitForWrist(); |
| 381 | if (ShouldExitAuto()) return; |
Austin Schuh | fae5336 | 2013-03-23 04:39:48 +0000 | [diff] [blame] | 382 | |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 383 | // Pick up at most 4 discs and drive at most kMaxPickupDistance. |
| 384 | DriveForwardPickUp(kMaxPickupDistance, WRIST_UP); |
Austin Schuh | fae5336 | 2013-03-23 04:39:48 +0000 | [diff] [blame] | 385 | |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 386 | SetWristGoal(WRIST_UP); |
| 387 | DriveSpin(-kTurnToCenterDegrees * M_PI / 180.0); |
| 388 | if (ShouldExitAuto()) return; |
| 389 | // Drive back to where we were. |
| 390 | SetDriveGoal(kDistanceToCenterMeters); |
| 391 | if (ShouldExitAuto()) return; |
Austin Schuh | fae5336 | 2013-03-23 04:39:48 +0000 | [diff] [blame] | 392 | |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 393 | return; |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 394 | |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 395 | ShootNDiscs(4); |
| 396 | if (ShouldExitAuto()) return; |
| 397 | } else { |
| 398 | SetWristGoal(WRIST_DOWN); |
| 399 | SetAngle_AdjustGoal(ANGLE_TWO); |
| 400 | SetShooterVelocity(375.0); |
| 401 | StartIndex(); // take in up to 4 discs |
| 402 | |
| 403 | if (ShouldExitAuto()) return; |
| 404 | WaitForWrist(); // wrist must be down before moving |
| 405 | ::aos::time::SleepFor(::aos::time::Time::InSeconds(0.25)); |
| 406 | |
| 407 | if (ShouldExitAuto()) return; |
| 408 | WaitForIndex(); // ready to pick up discs |
| 409 | |
| 410 | static const double kDriveDistance = 3.2; |
| 411 | static const double kFirstDrive = 0.3; |
| 412 | SetDriveGoal(kFirstDrive, 0.6); |
| 413 | ::aos::time::SleepFor(::aos::time::Time::InSeconds(0.25)); |
| 414 | SetDriveGoal(kDriveDistance - kFirstDrive, 0.6); |
| 415 | if (ShouldExitAuto()) return; |
| 416 | |
| 417 | PreloadIndex(); |
| 418 | ::aos::time::SleepFor(::aos::time::Time::InSeconds(0.4)); |
| 419 | SetDriveGoal(-1.3); |
| 420 | |
| 421 | if (ShouldExitAuto()) return; |
| 422 | WaitForAngle_Adjust(); |
| 423 | if (ShouldExitAuto()) return; |
| 424 | ShootIndex(); |
| 425 | if (ShouldExitAuto()) return; |
| 426 | } |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | } // namespace autonomous |
| 430 | } // namespace frc971 |