Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <unistd.h> |
| 4 | #include <math.h> |
| 5 | |
| 6 | #include "aos/linux_code/init.h" |
Brian Silverman | c206573 | 2015-11-28 22:55:30 +0000 | [diff] [blame] | 7 | #include "aos/input/joystick_input.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 8 | #include "aos/input/driver_station_data.h" |
| 9 | #include "aos/logging/logging.h" |
| 10 | #include "aos/util/log_interval.h" |
| 11 | #include "aos/time/time.h" |
| 12 | #include "aos/actions/actions.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 13 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 14 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 15 | #include "y2014/constants.h" |
| 16 | #include "frc971/queues/gyro.q.h" |
| 17 | #include "frc971/autonomous/auto.q.h" |
| 18 | #include "y2014/control_loops/claw/claw.q.h" |
| 19 | #include "y2014/control_loops/shooter/shooter.q.h" |
| 20 | #include "y2014/actors/shoot_actor.h" |
| 21 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 22 | using ::frc971::control_loops::drivetrain_queue; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 23 | using ::frc971::sensors::gyro_reading; |
| 24 | |
| 25 | using ::aos::input::driver_station::ButtonLocation; |
| 26 | using ::aos::input::driver_station::JoystickAxis; |
| 27 | using ::aos::input::driver_station::ControlBit; |
| 28 | |
| 29 | #define OLD_DS 0 |
| 30 | |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 31 | namespace y2014 { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 32 | namespace input { |
| 33 | namespace joysticks { |
| 34 | |
| 35 | const ButtonLocation kDriveControlLoopEnable1(1, 7), |
| 36 | kDriveControlLoopEnable2(1, 11); |
| 37 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
Campbell Crowley | 5b27f02 | 2016-02-20 16:55:35 -0800 | [diff] [blame] | 38 | const ButtonLocation kShiftHigh(2, 3), kShiftLow(2, 1); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 39 | const ButtonLocation kQuickTurn(1, 5); |
| 40 | |
| 41 | const ButtonLocation kCatch(3, 10); |
| 42 | |
| 43 | #if OLD_DS |
| 44 | const ButtonLocation kFire(3, 11); |
| 45 | const ButtonLocation kUnload(1, 4); |
| 46 | const ButtonLocation kReload(1, 2); |
| 47 | |
| 48 | const ButtonLocation kRollersOut(3, 12); |
| 49 | const ButtonLocation kRollersIn(3, 7); |
| 50 | |
| 51 | const ButtonLocation kTuck(3, 9); |
| 52 | const ButtonLocation kIntakePosition(3, 8); |
| 53 | const ButtonLocation kIntakeOpenPosition(3, 10); |
| 54 | const ButtonLocation kVerticalTuck(3, 1); |
| 55 | const JoystickAxis kFlipRobot(3, 3); |
| 56 | |
| 57 | const ButtonLocation kLongShot(3, 5); |
| 58 | const ButtonLocation kCloseShot(3, 2); |
| 59 | const ButtonLocation kFenderShot(3, 3); |
| 60 | const ButtonLocation kTrussShot(2, 11); |
| 61 | const ButtonLocation kHumanPlayerShot(3, 2); |
| 62 | #else |
| 63 | const ButtonLocation kFire(3, 9); |
| 64 | const ButtonLocation kUnload(1, 4); |
| 65 | const ButtonLocation kReload(1, 2); |
| 66 | |
| 67 | const ButtonLocation kRollersOut(3, 8); |
| 68 | const ButtonLocation kRollersIn(3, 3); |
| 69 | |
| 70 | const ButtonLocation kTuck(3, 4); |
| 71 | const ButtonLocation kIntakePosition(3, 5); |
| 72 | const ButtonLocation kIntakeOpenPosition(3, 11); |
| 73 | const ButtonLocation kVerticalTuck(2, 6); |
| 74 | const JoystickAxis kFlipRobot(3, 3); |
| 75 | |
| 76 | const ButtonLocation kLongShot(3, 7); |
| 77 | const ButtonLocation kCloseShot(3, 6); |
| 78 | const ButtonLocation kFenderShot(3, 2); |
| 79 | const ButtonLocation kTrussShot(2, 11); |
| 80 | const ButtonLocation kHumanPlayerShot(3, 1); |
| 81 | #endif |
| 82 | |
| 83 | const ButtonLocation kUserLeft(2, 7); |
| 84 | const ButtonLocation kUserRight(2, 10); |
| 85 | |
| 86 | const JoystickAxis kAdjustClawGoal(3, 2); |
| 87 | const JoystickAxis kAdjustClawSeparation(3, 1); |
| 88 | |
| 89 | struct ClawGoal { |
| 90 | double angle; |
| 91 | double separation; |
| 92 | }; |
| 93 | |
| 94 | struct ShotGoal { |
| 95 | ClawGoal claw; |
| 96 | double shot_power; |
| 97 | double velocity_compensation; |
| 98 | double intake_power; |
| 99 | }; |
| 100 | |
| 101 | const double kIntakePower = 4.0; |
| 102 | // In case we have to quickly adjust it. |
| 103 | const double kGrabSeparation = 0; |
| 104 | const double kShootSeparation = 0.11 + kGrabSeparation; |
| 105 | |
| 106 | const ClawGoal kTuckGoal = {-2.273474, -0.749484}; |
| 107 | const ClawGoal kVerticalTuckGoal = {0, kGrabSeparation}; |
| 108 | const ClawGoal kIntakeGoal = {-2.24, kGrabSeparation}; |
| 109 | const ClawGoal kIntakeOpenGoal = {-2.0, 1.1}; |
| 110 | |
| 111 | // TODO(austin): Tune these by hand... |
| 112 | const ClawGoal kFlippedTuckGoal = {2.733474, -0.75}; |
| 113 | const ClawGoal kFlippedIntakeGoal = {2.0, kGrabSeparation}; |
| 114 | const ClawGoal kFlippedIntakeOpenGoal = {0.95, 1.0}; |
| 115 | |
| 116 | // 34" between near edge of colored line and rear edge of bumper. |
| 117 | // Only works running? |
| 118 | const ShotGoal kLongShotGoal = { |
| 119 | {-1.08, kShootSeparation}, 145, 0.04, kIntakePower}; |
| 120 | // old 34" {-1.06, kShootSeparation}, 140, 0.04, kIntakePower}; |
| 121 | const ShotGoal kFlippedLongShotGoal = { |
| 122 | {0.96, kShootSeparation}, 145, 0.09, kIntakePower}; |
| 123 | // old 34" {0.96, kShootSeparation}, 140, 0.09, kIntakePower}; |
| 124 | |
| 125 | // 78" between near edge of colored line and rear edge of bumper. |
| 126 | const ShotGoal kCloseShotGoal = { |
| 127 | {-0.95, kShootSeparation}, 105, 0.2, kIntakePower}; |
| 128 | // 3/4" plunger {-0.90, kShootSeparation}, 105, 0.2, kIntakePower}; |
| 129 | const ShotGoal kFlippedMediumShotGoal = { |
| 130 | {0.865, kShootSeparation}, 120, 0.2, kIntakePower}; |
| 131 | // 3/4" plunger {0.80, kShootSeparation}, 105, 0.2, kIntakePower}; |
| 132 | |
| 133 | // Shot from the fender. |
| 134 | const ShotGoal kFenderShotGoal = { |
| 135 | {-0.68, kShootSeparation}, 115.0, 0.0, kIntakePower}; |
| 136 | const ShotGoal kFlippedShortShotGoal = { |
| 137 | {0.63, kShootSeparation}, 115.0, 0.0, kIntakePower}; |
| 138 | |
| 139 | const ShotGoal kHumanShotGoal = { |
| 140 | {-0.90, kShootSeparation}, 140, 0.04, kIntakePower}; |
| 141 | const ShotGoal kFlippedHumanShotGoal = { |
| 142 | {0.90, kShootSeparation}, 140, 0, kIntakePower}; |
| 143 | const ShotGoal kTrussShotGoal = { |
| 144 | {-0.68, kShootSeparation}, 88.0, 0.4, kIntakePower}; |
| 145 | const ShotGoal kFlippedTrussShotGoal = { |
| 146 | {0.68, kShootSeparation}, 92.0, 0.4, kIntakePower}; |
| 147 | |
| 148 | const ShotGoal kFlippedDemoShotGoal = { |
| 149 | {1.0, kShootSeparation}, 65.0, 0.0, kIntakePower}; |
| 150 | const ShotGoal kDemoShotGoal = { |
| 151 | {-1.0, kShootSeparation}, 50.0, 0.0, kIntakePower}; |
| 152 | |
| 153 | const ClawGoal k254PassGoal = {-1.95, kGrabSeparation}; |
| 154 | const ClawGoal kFlipped254PassGoal = {1.96, kGrabSeparation}; |
| 155 | |
| 156 | class Reader : public ::aos::input::JoystickInput { |
| 157 | public: |
| 158 | Reader() |
| 159 | : is_high_gear_(false), |
| 160 | shot_power_(80.0), |
| 161 | goal_angle_(0.0), |
| 162 | separation_angle_(kGrabSeparation), |
| 163 | velocity_compensation_(0.0), |
| 164 | intake_power_(0.0), |
| 165 | was_running_(false) {} |
| 166 | |
| 167 | void RunIteration(const ::aos::input::driver_station::Data &data) override { |
| 168 | bool last_auto_running = auto_running_; |
| 169 | auto_running_ = data.GetControlBit(ControlBit::kAutonomous) && |
| 170 | data.GetControlBit(ControlBit::kEnabled); |
| 171 | if (auto_running_ != last_auto_running) { |
| 172 | if (auto_running_) { |
| 173 | StartAuto(); |
| 174 | } else { |
| 175 | StopAuto(); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if (!data.GetControlBit(ControlBit::kAutonomous)) { |
| 180 | HandleDrivetrain(data); |
| 181 | HandleTeleop(data); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
| 186 | bool is_control_loop_driving = false; |
| 187 | double left_goal = 0.0; |
| 188 | double right_goal = 0.0; |
| 189 | const double wheel = -data.GetAxis(kSteeringWheel); |
| 190 | const double throttle = -data.GetAxis(kDriveThrottle); |
| 191 | const double kThrottleGain = 1.0 / 2.5; |
| 192 | if (false && (data.IsPressed(kDriveControlLoopEnable1) || |
| 193 | data.IsPressed(kDriveControlLoopEnable2))) { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 194 | if (data.PosEdge(kDriveControlLoopEnable1) || |
| 195 | data.PosEdge(kDriveControlLoopEnable2)) { |
| 196 | if (drivetrain_queue.position.FetchLatest() && |
| 197 | gyro_reading.FetchLatest()) { |
Adam Snaider | 418bd82 | 2016-11-26 14:49:23 -0800 | [diff] [blame] | 198 | distance_ = (drivetrain_queue.position->left_encoder + |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 199 | drivetrain_queue.position->right_encoder) / |
| 200 | 2.0 - |
| 201 | throttle * kThrottleGain / 2.0; |
Adam Snaider | 418bd82 | 2016-11-26 14:49:23 -0800 | [diff] [blame] | 202 | angle_ = gyro_reading->angle; |
| 203 | filtered_goal_distance_ = distance_; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | is_control_loop_driving = true; |
| 207 | |
| 208 | // const double gyro_angle = Gyro.View().angle; |
Adam Snaider | 418bd82 | 2016-11-26 14:49:23 -0800 | [diff] [blame] | 209 | const double goal_theta = angle_ - wheel * 0.27; |
| 210 | const double goal_distance = distance_ + throttle * kThrottleGain; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 211 | const double robot_width = 22.0 / 100.0 * 2.54; |
| 212 | const double kMaxVelocity = 0.6; |
Adam Snaider | 418bd82 | 2016-11-26 14:49:23 -0800 | [diff] [blame] | 213 | if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance_) { |
| 214 | filtered_goal_distance_ += kMaxVelocity * 0.03; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 215 | } else if (goal_distance < |
Adam Snaider | 418bd82 | 2016-11-26 14:49:23 -0800 | [diff] [blame] | 216 | -kMaxVelocity * 0.02 + filtered_goal_distance_) { |
| 217 | filtered_goal_distance_ -= kMaxVelocity * 0.02; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 218 | } else { |
Adam Snaider | 418bd82 | 2016-11-26 14:49:23 -0800 | [diff] [blame] | 219 | filtered_goal_distance_ = goal_distance; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 220 | } |
Adam Snaider | 418bd82 | 2016-11-26 14:49:23 -0800 | [diff] [blame] | 221 | left_goal = filtered_goal_distance_ - robot_width * goal_theta / 2.0; |
| 222 | right_goal = filtered_goal_distance_ + robot_width * goal_theta / 2.0; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 223 | is_high_gear_ = false; |
| 224 | |
| 225 | LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal); |
| 226 | } |
| 227 | if (!drivetrain_queue.goal.MakeWithBuilder() |
Austin Schuh | 2b1fce0 | 2018-03-02 20:05:20 -0800 | [diff] [blame] | 228 | .wheel(wheel) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 229 | .throttle(throttle) |
| 230 | .highgear(is_high_gear_) |
| 231 | .quickturn(data.IsPressed(kQuickTurn)) |
| 232 | .control_loop_driving(is_control_loop_driving) |
| 233 | .left_goal(left_goal) |
| 234 | .right_goal(right_goal) |
| 235 | .left_velocity_goal(0) |
| 236 | .right_velocity_goal(0) |
| 237 | .Send()) { |
| 238 | LOG(WARNING, "sending stick values failed\n"); |
| 239 | } |
Campbell Crowley | 5b27f02 | 2016-02-20 16:55:35 -0800 | [diff] [blame] | 240 | if (data.PosEdge(kShiftLow)) { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 241 | is_high_gear_ = false; |
| 242 | } |
Campbell Crowley | 5b27f02 | 2016-02-20 16:55:35 -0800 | [diff] [blame] | 243 | if (data.PosEdge(kShiftHigh)) { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 244 | is_high_gear_ = true; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | void SetGoal(ClawGoal goal) { |
| 249 | goal_angle_ = goal.angle; |
| 250 | separation_angle_ = goal.separation; |
| 251 | moving_for_shot_ = false; |
| 252 | velocity_compensation_ = 0.0; |
| 253 | intake_power_ = 0.0; |
| 254 | } |
| 255 | |
| 256 | void SetGoal(ShotGoal goal) { |
| 257 | goal_angle_ = goal.claw.angle; |
| 258 | shot_separation_angle_ = goal.claw.separation; |
| 259 | separation_angle_ = kGrabSeparation; |
| 260 | moving_for_shot_ = true; |
| 261 | shot_power_ = goal.shot_power; |
| 262 | velocity_compensation_ = goal.velocity_compensation; |
| 263 | intake_power_ = goal.intake_power; |
| 264 | } |
| 265 | |
| 266 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 267 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 268 | action_queue_.CancelAllActions(); |
| 269 | LOG(DEBUG, "Canceling\n"); |
| 270 | } |
| 271 | if (data.IsPressed(kRollersIn) || data.IsPressed(kRollersOut)) { |
| 272 | intake_power_ = 0.0; |
| 273 | separation_angle_ = kGrabSeparation; |
| 274 | moving_for_shot_ = false; |
| 275 | } |
| 276 | |
| 277 | static const double kAdjustClawGoalDeadband = 0.08; |
| 278 | double claw_goal_adjust = data.GetAxis(kAdjustClawGoal); |
| 279 | if (OLD_DS || ::std::abs(claw_goal_adjust) < kAdjustClawGoalDeadband) { |
| 280 | claw_goal_adjust = 0; |
| 281 | } else { |
| 282 | claw_goal_adjust = (claw_goal_adjust - |
| 283 | ((claw_goal_adjust < 0) ? -kAdjustClawGoalDeadband |
| 284 | : kAdjustClawGoalDeadband)) * |
| 285 | 0.035; |
| 286 | } |
| 287 | double claw_separation_adjust = data.GetAxis(kAdjustClawSeparation); |
| 288 | if (OLD_DS || |
| 289 | ::std::abs(claw_separation_adjust) < kAdjustClawGoalDeadband) { |
| 290 | claw_separation_adjust = 0; |
| 291 | } else { |
| 292 | claw_separation_adjust = |
| 293 | (claw_separation_adjust - |
| 294 | ((claw_separation_adjust < 0) ? -kAdjustClawGoalDeadband |
| 295 | : kAdjustClawGoalDeadband)) * |
| 296 | -0.035; |
| 297 | } |
| 298 | |
| 299 | #if OLD_DS |
| 300 | if (data.IsPressed(kFenderShot)) { |
| 301 | #else |
| 302 | if (data.GetAxis(kFlipRobot) > 0.9) { |
| 303 | #endif |
| 304 | claw_goal_adjust += claw_separation_adjust; |
| 305 | claw_goal_adjust *= -1; |
| 306 | |
| 307 | if (data.IsPressed(kIntakeOpenPosition)) { |
| 308 | action_queue_.CancelAllActions(); |
| 309 | LOG(DEBUG, "Canceling\n"); |
| 310 | SetGoal(kFlippedIntakeOpenGoal); |
| 311 | } else if (data.IsPressed(kIntakePosition)) { |
| 312 | action_queue_.CancelAllActions(); |
| 313 | LOG(DEBUG, "Canceling\n"); |
| 314 | SetGoal(kFlippedIntakeGoal); |
| 315 | } else if (data.IsPressed(kVerticalTuck)) { |
| 316 | action_queue_.CancelAllActions(); |
| 317 | LOG(DEBUG, "Canceling\n"); |
| 318 | SetGoal(kVerticalTuckGoal); |
| 319 | } else if (data.IsPressed(kTuck)) { |
| 320 | action_queue_.CancelAllActions(); |
| 321 | LOG(DEBUG, "Canceling\n"); |
| 322 | SetGoal(kFlippedTuckGoal); |
| 323 | } else if (data.PosEdge(kLongShot)) { |
| 324 | action_queue_.CancelAllActions(); |
| 325 | LOG(DEBUG, "Canceling\n"); |
| 326 | SetGoal(kFlippedLongShotGoal); |
| 327 | } else if (data.PosEdge(kCloseShot)) { |
| 328 | action_queue_.CancelAllActions(); |
| 329 | LOG(DEBUG, "Canceling\n"); |
| 330 | SetGoal(kFlippedMediumShotGoal); |
| 331 | } else if (data.PosEdge(kFenderShot)) { |
| 332 | action_queue_.CancelAllActions(); |
| 333 | LOG(DEBUG, "Canceling\n"); |
| 334 | SetGoal(kFlippedShortShotGoal); |
| 335 | } else if (data.PosEdge(kHumanPlayerShot)) { |
| 336 | action_queue_.CancelAllActions(); |
| 337 | LOG(DEBUG, "Canceling\n"); |
| 338 | SetGoal(kFlippedHumanShotGoal); |
| 339 | } else if (data.PosEdge(kUserLeft)) { |
| 340 | action_queue_.CancelAllActions(); |
| 341 | LOG(DEBUG, "Canceling\n"); |
| 342 | SetGoal(kFlipped254PassGoal); |
| 343 | } else if (data.PosEdge(kUserRight)) { |
| 344 | action_queue_.CancelAllActions(); |
| 345 | LOG(DEBUG, "Canceling\n"); |
| 346 | SetGoal(kFlippedDemoShotGoal); |
| 347 | } else if (data.PosEdge(kTrussShot)) { |
| 348 | action_queue_.CancelAllActions(); |
| 349 | LOG(DEBUG, "Canceling\n"); |
| 350 | SetGoal(kFlippedTrussShotGoal); |
| 351 | } |
| 352 | } else { |
| 353 | if (data.IsPressed(kIntakeOpenPosition)) { |
| 354 | action_queue_.CancelAllActions(); |
| 355 | LOG(DEBUG, "Canceling\n"); |
| 356 | SetGoal(kIntakeOpenGoal); |
| 357 | } else if (data.IsPressed(kIntakePosition)) { |
| 358 | action_queue_.CancelAllActions(); |
| 359 | LOG(DEBUG, "Canceling\n"); |
| 360 | SetGoal(kIntakeGoal); |
| 361 | } else if (data.IsPressed(kVerticalTuck)) { |
| 362 | action_queue_.CancelAllActions(); |
| 363 | LOG(DEBUG, "Canceling\n"); |
| 364 | SetGoal(kVerticalTuckGoal); |
| 365 | } else if (data.IsPressed(kTuck)) { |
| 366 | action_queue_.CancelAllActions(); |
| 367 | LOG(DEBUG, "Canceling\n"); |
| 368 | SetGoal(kTuckGoal); |
| 369 | } else if (data.PosEdge(kLongShot)) { |
| 370 | action_queue_.CancelAllActions(); |
| 371 | LOG(DEBUG, "Canceling\n"); |
| 372 | SetGoal(kLongShotGoal); |
| 373 | } else if (data.PosEdge(kCloseShot)) { |
| 374 | action_queue_.CancelAllActions(); |
| 375 | LOG(DEBUG, "Canceling\n"); |
| 376 | SetGoal(kCloseShotGoal); |
| 377 | } else if (data.PosEdge(kFenderShot)) { |
| 378 | action_queue_.CancelAllActions(); |
| 379 | LOG(DEBUG, "Canceling\n"); |
| 380 | SetGoal(kFenderShotGoal); |
| 381 | } else if (data.PosEdge(kHumanPlayerShot)) { |
| 382 | action_queue_.CancelAllActions(); |
| 383 | LOG(DEBUG, "Canceling\n"); |
| 384 | SetGoal(kHumanShotGoal); |
| 385 | } else if (data.PosEdge(kUserLeft)) { |
| 386 | action_queue_.CancelAllActions(); |
| 387 | LOG(DEBUG, "Canceling\n"); |
| 388 | SetGoal(k254PassGoal); |
| 389 | } else if (data.PosEdge(kUserRight)) { |
| 390 | action_queue_.CancelAllActions(); |
| 391 | LOG(DEBUG, "Canceling\n"); |
| 392 | SetGoal(kDemoShotGoal); |
| 393 | } else if (data.PosEdge(kTrussShot)) { |
| 394 | action_queue_.CancelAllActions(); |
| 395 | LOG(DEBUG, "Canceling\n"); |
| 396 | SetGoal(kTrussShotGoal); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | if (data.PosEdge(kFire)) { |
| 401 | action_queue_.EnqueueAction(actors::MakeShootAction()); |
| 402 | } else if (data.NegEdge(kFire)) { |
| 403 | action_queue_.CancelCurrentAction(); |
| 404 | } |
| 405 | |
| 406 | action_queue_.Tick(); |
| 407 | if (data.IsPressed(kUnload) || data.IsPressed(kReload)) { |
| 408 | action_queue_.CancelAllActions(); |
| 409 | LOG(DEBUG, "Canceling\n"); |
| 410 | intake_power_ = 0.0; |
| 411 | velocity_compensation_ = 0.0; |
| 412 | } |
| 413 | |
| 414 | // Send out the claw and shooter goals if no actions are running. |
| 415 | if (!action_queue_.Running()) { |
| 416 | goal_angle_ += claw_goal_adjust; |
| 417 | separation_angle_ += claw_separation_adjust; |
| 418 | |
| 419 | // If the action just ended, turn the intake off and stop velocity |
| 420 | // compensating. |
| 421 | if (was_running_) { |
| 422 | intake_power_ = 0.0; |
| 423 | velocity_compensation_ = 0.0; |
| 424 | } |
| 425 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 426 | ::frc971::control_loops::drivetrain_queue.status.FetchLatest(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 427 | double goal_angle = goal_angle_; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 428 | if (::frc971::control_loops::drivetrain_queue.status.get()) { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 429 | goal_angle += SpeedToAngleOffset( |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 430 | ::frc971::control_loops::drivetrain_queue.status->robot_speed); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 431 | } else { |
| 432 | LOG_INTERVAL(no_drivetrain_status_); |
| 433 | } |
| 434 | |
| 435 | if (moving_for_shot_) { |
| 436 | auto &claw_status = control_loops::claw_queue.status; |
| 437 | claw_status.FetchLatest(); |
| 438 | if (claw_status.get()) { |
| 439 | if (::std::abs(claw_status->bottom - goal_angle) < 0.2) { |
| 440 | moving_for_shot_ = false; |
| 441 | separation_angle_ = shot_separation_angle_; |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | double separation_angle = separation_angle_; |
| 447 | |
| 448 | if (data.IsPressed(kCatch)) { |
| 449 | const double kCatchSeparation = 1.0; |
| 450 | goal_angle -= kCatchSeparation / 2.0; |
| 451 | separation_angle = kCatchSeparation; |
| 452 | } |
| 453 | |
| 454 | bool intaking = |
| 455 | data.IsPressed(kRollersIn) || data.IsPressed(kIntakePosition) || |
| 456 | data.IsPressed(kIntakeOpenPosition) || data.IsPressed(kCatch); |
| 457 | if (!control_loops::claw_queue.goal.MakeWithBuilder() |
| 458 | .bottom_angle(goal_angle) |
| 459 | .separation_angle(separation_angle) |
| 460 | .intake(intaking ? 12.0 |
| 461 | : (data.IsPressed(kRollersOut) ? -12.0 |
| 462 | : intake_power_)) |
| 463 | .centering(intaking ? 12.0 : 0.0) |
| 464 | .Send()) { |
| 465 | LOG(WARNING, "sending claw goal failed\n"); |
| 466 | } |
| 467 | |
| 468 | if (!control_loops::shooter_queue.goal.MakeWithBuilder() |
| 469 | .shot_power(shot_power_) |
| 470 | .shot_requested(data.IsPressed(kFire)) |
| 471 | .unload_requested(data.IsPressed(kUnload)) |
| 472 | .load_requested(data.IsPressed(kReload)) |
| 473 | .Send()) { |
| 474 | LOG(WARNING, "sending shooter goal failed\n"); |
| 475 | } |
| 476 | } |
| 477 | was_running_ = action_queue_.Running(); |
| 478 | } |
| 479 | |
| 480 | double SpeedToAngleOffset(double speed) { |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 481 | const ::y2014::constants::Values &values = ::y2014::constants::GetValues(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 482 | // scale speed to a [0.0-1.0] on something close to the max |
| 483 | // TODO(austin): Change the scale factor for different shots. |
| 484 | return (speed / values.drivetrain_max_speed) * velocity_compensation_; |
| 485 | } |
| 486 | |
| 487 | private: |
| 488 | void StartAuto() { |
| 489 | LOG(INFO, "Starting auto mode\n"); |
| 490 | ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send(); |
| 491 | } |
| 492 | |
| 493 | void StopAuto() { |
| 494 | LOG(INFO, "Stopping auto mode\n"); |
| 495 | ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send(); |
| 496 | } |
| 497 | |
| 498 | bool is_high_gear_; |
| 499 | double shot_power_; |
| 500 | double goal_angle_; |
| 501 | double separation_angle_, shot_separation_angle_; |
| 502 | double velocity_compensation_; |
Adam Snaider | a3271fe | 2016-10-26 21:03:38 -0700 | [diff] [blame] | 503 | // Distance, angle, and filtered goal for closed loop driving. |
Adam Snaider | 418bd82 | 2016-11-26 14:49:23 -0800 | [diff] [blame] | 504 | double distance_; |
| 505 | double angle_; |
| 506 | double filtered_goal_distance_; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 507 | double intake_power_; |
| 508 | bool was_running_; |
| 509 | bool moving_for_shot_ = false; |
| 510 | |
| 511 | bool auto_running_ = false; |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 512 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 513 | ::aos::common::actions::ActionQueue action_queue_; |
| 514 | |
| 515 | ::aos::util::SimpleLogInterval no_drivetrain_status_ = |
Austin Schuh | 61bdc60 | 2016-12-04 19:10:10 -0800 | [diff] [blame] | 516 | ::aos::util::SimpleLogInterval(::std::chrono::milliseconds(200), WARNING, |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 517 | "no drivetrain status"); |
| 518 | }; |
| 519 | |
| 520 | } // namespace joysticks |
| 521 | } // namespace input |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 522 | } // namespace y2014 |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 523 | |
| 524 | int main() { |
Brian Silverman | 5090c43 | 2016-01-02 14:44:26 -0800 | [diff] [blame] | 525 | ::aos::Init(-1); |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 526 | ::y2014::input::joysticks::Reader reader; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 527 | reader.Run(); |
| 528 | ::aos::Cleanup(); |
| 529 | } |