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