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 | |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 6 | #include "aos/actions/actions.h" |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 7 | #include "aos/init.h" |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 8 | #include "aos/input/action_joystick_input.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 9 | #include "aos/input/driver_station_data.h" |
| 10 | #include "aos/logging/logging.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 11 | #include "aos/time/time.h" |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 12 | #include "aos/util/log_interval.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 13 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 14 | #include "frc971/autonomous/auto.q.h" |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 15 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 16 | #include "frc971/queues/gyro.q.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 17 | #include "y2014/actors/shoot_actor.h" |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 18 | #include "y2014/constants.h" |
| 19 | #include "y2014/control_loops/claw/claw.q.h" |
| 20 | #include "y2014/control_loops/drivetrain/drivetrain_base.h" |
| 21 | #include "y2014/control_loops/shooter/shooter.q.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 22 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 23 | using ::frc971::control_loops::drivetrain_queue; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 24 | using ::frc971::sensors::gyro_reading; |
| 25 | |
| 26 | using ::aos::input::driver_station::ButtonLocation; |
| 27 | using ::aos::input::driver_station::JoystickAxis; |
| 28 | using ::aos::input::driver_station::ControlBit; |
| 29 | |
| 30 | #define OLD_DS 0 |
| 31 | |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 32 | namespace y2014 { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 33 | namespace input { |
| 34 | namespace joysticks { |
| 35 | |
| 36 | const ButtonLocation kDriveControlLoopEnable1(1, 7), |
| 37 | kDriveControlLoopEnable2(1, 11); |
| 38 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
Campbell Crowley | 5b27f02 | 2016-02-20 16:55:35 -0800 | [diff] [blame] | 39 | const ButtonLocation kShiftHigh(2, 3), kShiftLow(2, 1); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 40 | const ButtonLocation kQuickTurn(1, 5); |
| 41 | |
| 42 | const ButtonLocation kCatch(3, 10); |
| 43 | |
| 44 | #if OLD_DS |
| 45 | const ButtonLocation kFire(3, 11); |
| 46 | const ButtonLocation kUnload(1, 4); |
| 47 | const ButtonLocation kReload(1, 2); |
| 48 | |
| 49 | const ButtonLocation kRollersOut(3, 12); |
| 50 | const ButtonLocation kRollersIn(3, 7); |
| 51 | |
| 52 | const ButtonLocation kTuck(3, 9); |
| 53 | const ButtonLocation kIntakePosition(3, 8); |
| 54 | const ButtonLocation kIntakeOpenPosition(3, 10); |
| 55 | const ButtonLocation kVerticalTuck(3, 1); |
| 56 | const JoystickAxis kFlipRobot(3, 3); |
| 57 | |
| 58 | const ButtonLocation kLongShot(3, 5); |
| 59 | const ButtonLocation kCloseShot(3, 2); |
| 60 | const ButtonLocation kFenderShot(3, 3); |
| 61 | const ButtonLocation kTrussShot(2, 11); |
| 62 | const ButtonLocation kHumanPlayerShot(3, 2); |
| 63 | #else |
| 64 | const ButtonLocation kFire(3, 9); |
| 65 | const ButtonLocation kUnload(1, 4); |
| 66 | const ButtonLocation kReload(1, 2); |
| 67 | |
| 68 | const ButtonLocation kRollersOut(3, 8); |
| 69 | const ButtonLocation kRollersIn(3, 3); |
| 70 | |
| 71 | const ButtonLocation kTuck(3, 4); |
| 72 | const ButtonLocation kIntakePosition(3, 5); |
| 73 | const ButtonLocation kIntakeOpenPosition(3, 11); |
| 74 | const ButtonLocation kVerticalTuck(2, 6); |
| 75 | const JoystickAxis kFlipRobot(3, 3); |
| 76 | |
| 77 | const ButtonLocation kLongShot(3, 7); |
| 78 | const ButtonLocation kCloseShot(3, 6); |
| 79 | const ButtonLocation kFenderShot(3, 2); |
| 80 | const ButtonLocation kTrussShot(2, 11); |
| 81 | const ButtonLocation kHumanPlayerShot(3, 1); |
| 82 | #endif |
| 83 | |
| 84 | const ButtonLocation kUserLeft(2, 7); |
| 85 | const ButtonLocation kUserRight(2, 10); |
| 86 | |
| 87 | const JoystickAxis kAdjustClawGoal(3, 2); |
| 88 | const JoystickAxis kAdjustClawSeparation(3, 1); |
| 89 | |
| 90 | struct ClawGoal { |
| 91 | double angle; |
| 92 | double separation; |
| 93 | }; |
| 94 | |
| 95 | struct ShotGoal { |
| 96 | ClawGoal claw; |
| 97 | double shot_power; |
| 98 | double velocity_compensation; |
| 99 | double intake_power; |
| 100 | }; |
| 101 | |
| 102 | const double kIntakePower = 4.0; |
| 103 | // In case we have to quickly adjust it. |
| 104 | const double kGrabSeparation = 0; |
| 105 | const double kShootSeparation = 0.11 + kGrabSeparation; |
| 106 | |
| 107 | const ClawGoal kTuckGoal = {-2.273474, -0.749484}; |
| 108 | const ClawGoal kVerticalTuckGoal = {0, kGrabSeparation}; |
| 109 | const ClawGoal kIntakeGoal = {-2.24, kGrabSeparation}; |
| 110 | const ClawGoal kIntakeOpenGoal = {-2.0, 1.1}; |
| 111 | |
| 112 | // TODO(austin): Tune these by hand... |
| 113 | const ClawGoal kFlippedTuckGoal = {2.733474, -0.75}; |
| 114 | const ClawGoal kFlippedIntakeGoal = {2.0, kGrabSeparation}; |
| 115 | const ClawGoal kFlippedIntakeOpenGoal = {0.95, 1.0}; |
| 116 | |
| 117 | // 34" between near edge of colored line and rear edge of bumper. |
| 118 | // Only works running? |
| 119 | const ShotGoal kLongShotGoal = { |
| 120 | {-1.08, kShootSeparation}, 145, 0.04, kIntakePower}; |
| 121 | // old 34" {-1.06, kShootSeparation}, 140, 0.04, kIntakePower}; |
| 122 | const ShotGoal kFlippedLongShotGoal = { |
| 123 | {0.96, kShootSeparation}, 145, 0.09, kIntakePower}; |
| 124 | // old 34" {0.96, kShootSeparation}, 140, 0.09, kIntakePower}; |
| 125 | |
| 126 | // 78" between near edge of colored line and rear edge of bumper. |
| 127 | const ShotGoal kCloseShotGoal = { |
| 128 | {-0.95, kShootSeparation}, 105, 0.2, kIntakePower}; |
| 129 | // 3/4" plunger {-0.90, kShootSeparation}, 105, 0.2, kIntakePower}; |
| 130 | const ShotGoal kFlippedMediumShotGoal = { |
| 131 | {0.865, kShootSeparation}, 120, 0.2, kIntakePower}; |
| 132 | // 3/4" plunger {0.80, kShootSeparation}, 105, 0.2, kIntakePower}; |
| 133 | |
| 134 | // Shot from the fender. |
| 135 | const ShotGoal kFenderShotGoal = { |
| 136 | {-0.68, kShootSeparation}, 115.0, 0.0, kIntakePower}; |
| 137 | const ShotGoal kFlippedShortShotGoal = { |
| 138 | {0.63, kShootSeparation}, 115.0, 0.0, kIntakePower}; |
| 139 | |
| 140 | const ShotGoal kHumanShotGoal = { |
| 141 | {-0.90, kShootSeparation}, 140, 0.04, kIntakePower}; |
| 142 | const ShotGoal kFlippedHumanShotGoal = { |
| 143 | {0.90, kShootSeparation}, 140, 0, kIntakePower}; |
| 144 | const ShotGoal kTrussShotGoal = { |
| 145 | {-0.68, kShootSeparation}, 88.0, 0.4, kIntakePower}; |
| 146 | const ShotGoal kFlippedTrussShotGoal = { |
| 147 | {0.68, kShootSeparation}, 92.0, 0.4, kIntakePower}; |
| 148 | |
| 149 | const ShotGoal kFlippedDemoShotGoal = { |
| 150 | {1.0, kShootSeparation}, 65.0, 0.0, kIntakePower}; |
| 151 | const ShotGoal kDemoShotGoal = { |
| 152 | {-1.0, kShootSeparation}, 50.0, 0.0, kIntakePower}; |
| 153 | |
| 154 | const ClawGoal k254PassGoal = {-1.95, kGrabSeparation}; |
| 155 | const ClawGoal kFlipped254PassGoal = {1.96, kGrabSeparation}; |
| 156 | |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 157 | class Reader : public ::aos::input::ActionJoystickInput { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 158 | public: |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 159 | Reader(::aos::EventLoop *event_loop) |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 160 | : ::aos::input::ActionJoystickInput( |
| 161 | event_loop, control_loops::GetDrivetrainConfig(), |
| 162 | ::aos::input::DrivetrainInputReader::InputType::kSteeringWheel, {}), |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 163 | shot_power_(80.0), |
| 164 | goal_angle_(0.0), |
| 165 | separation_angle_(kGrabSeparation), |
| 166 | velocity_compensation_(0.0), |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 167 | intake_power_(0.0), |
| 168 | shoot_action_factory_(actors::ShootActor::MakeFactory(event_loop)) {} |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 169 | |
| 170 | void SetGoal(ClawGoal goal) { |
| 171 | goal_angle_ = goal.angle; |
| 172 | separation_angle_ = goal.separation; |
| 173 | moving_for_shot_ = false; |
| 174 | velocity_compensation_ = 0.0; |
| 175 | intake_power_ = 0.0; |
| 176 | } |
| 177 | |
| 178 | void SetGoal(ShotGoal goal) { |
| 179 | goal_angle_ = goal.claw.angle; |
| 180 | shot_separation_angle_ = goal.claw.separation; |
| 181 | separation_angle_ = kGrabSeparation; |
| 182 | moving_for_shot_ = true; |
| 183 | shot_power_ = goal.shot_power; |
| 184 | velocity_compensation_ = goal.velocity_compensation; |
| 185 | intake_power_ = goal.intake_power; |
| 186 | } |
| 187 | |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 188 | void HandleTeleop(const ::aos::input::driver_station::Data &data) override { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 189 | if (data.IsPressed(kRollersIn) || data.IsPressed(kRollersOut)) { |
| 190 | intake_power_ = 0.0; |
| 191 | separation_angle_ = kGrabSeparation; |
| 192 | moving_for_shot_ = false; |
| 193 | } |
| 194 | |
| 195 | static const double kAdjustClawGoalDeadband = 0.08; |
| 196 | double claw_goal_adjust = data.GetAxis(kAdjustClawGoal); |
| 197 | if (OLD_DS || ::std::abs(claw_goal_adjust) < kAdjustClawGoalDeadband) { |
| 198 | claw_goal_adjust = 0; |
| 199 | } else { |
| 200 | claw_goal_adjust = (claw_goal_adjust - |
| 201 | ((claw_goal_adjust < 0) ? -kAdjustClawGoalDeadband |
| 202 | : kAdjustClawGoalDeadband)) * |
| 203 | 0.035; |
| 204 | } |
| 205 | double claw_separation_adjust = data.GetAxis(kAdjustClawSeparation); |
| 206 | if (OLD_DS || |
| 207 | ::std::abs(claw_separation_adjust) < kAdjustClawGoalDeadband) { |
| 208 | claw_separation_adjust = 0; |
| 209 | } else { |
| 210 | claw_separation_adjust = |
| 211 | (claw_separation_adjust - |
| 212 | ((claw_separation_adjust < 0) ? -kAdjustClawGoalDeadband |
| 213 | : kAdjustClawGoalDeadband)) * |
| 214 | -0.035; |
| 215 | } |
| 216 | |
| 217 | #if OLD_DS |
| 218 | if (data.IsPressed(kFenderShot)) { |
| 219 | #else |
| 220 | if (data.GetAxis(kFlipRobot) > 0.9) { |
| 221 | #endif |
| 222 | claw_goal_adjust += claw_separation_adjust; |
| 223 | claw_goal_adjust *= -1; |
| 224 | |
| 225 | if (data.IsPressed(kIntakeOpenPosition)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 226 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 227 | LOG(DEBUG, "Canceling\n"); |
| 228 | SetGoal(kFlippedIntakeOpenGoal); |
| 229 | } else if (data.IsPressed(kIntakePosition)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 230 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 231 | LOG(DEBUG, "Canceling\n"); |
| 232 | SetGoal(kFlippedIntakeGoal); |
| 233 | } else if (data.IsPressed(kVerticalTuck)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 234 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 235 | LOG(DEBUG, "Canceling\n"); |
| 236 | SetGoal(kVerticalTuckGoal); |
| 237 | } else if (data.IsPressed(kTuck)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 238 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 239 | LOG(DEBUG, "Canceling\n"); |
| 240 | SetGoal(kFlippedTuckGoal); |
| 241 | } else if (data.PosEdge(kLongShot)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 242 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 243 | LOG(DEBUG, "Canceling\n"); |
| 244 | SetGoal(kFlippedLongShotGoal); |
| 245 | } else if (data.PosEdge(kCloseShot)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 246 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 247 | LOG(DEBUG, "Canceling\n"); |
| 248 | SetGoal(kFlippedMediumShotGoal); |
| 249 | } else if (data.PosEdge(kFenderShot)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 250 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 251 | LOG(DEBUG, "Canceling\n"); |
| 252 | SetGoal(kFlippedShortShotGoal); |
| 253 | } else if (data.PosEdge(kHumanPlayerShot)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 254 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 255 | LOG(DEBUG, "Canceling\n"); |
| 256 | SetGoal(kFlippedHumanShotGoal); |
| 257 | } else if (data.PosEdge(kUserLeft)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 258 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 259 | LOG(DEBUG, "Canceling\n"); |
| 260 | SetGoal(kFlipped254PassGoal); |
| 261 | } else if (data.PosEdge(kUserRight)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 262 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 263 | LOG(DEBUG, "Canceling\n"); |
| 264 | SetGoal(kFlippedDemoShotGoal); |
| 265 | } else if (data.PosEdge(kTrussShot)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 266 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 267 | LOG(DEBUG, "Canceling\n"); |
| 268 | SetGoal(kFlippedTrussShotGoal); |
| 269 | } |
| 270 | } else { |
| 271 | if (data.IsPressed(kIntakeOpenPosition)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 272 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 273 | LOG(DEBUG, "Canceling\n"); |
| 274 | SetGoal(kIntakeOpenGoal); |
| 275 | } else if (data.IsPressed(kIntakePosition)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 276 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 277 | LOG(DEBUG, "Canceling\n"); |
| 278 | SetGoal(kIntakeGoal); |
| 279 | } else if (data.IsPressed(kVerticalTuck)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 280 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 281 | LOG(DEBUG, "Canceling\n"); |
| 282 | SetGoal(kVerticalTuckGoal); |
| 283 | } else if (data.IsPressed(kTuck)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 284 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 285 | LOG(DEBUG, "Canceling\n"); |
| 286 | SetGoal(kTuckGoal); |
| 287 | } else if (data.PosEdge(kLongShot)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 288 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 289 | LOG(DEBUG, "Canceling\n"); |
| 290 | SetGoal(kLongShotGoal); |
| 291 | } else if (data.PosEdge(kCloseShot)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 292 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 293 | LOG(DEBUG, "Canceling\n"); |
| 294 | SetGoal(kCloseShotGoal); |
| 295 | } else if (data.PosEdge(kFenderShot)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 296 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 297 | LOG(DEBUG, "Canceling\n"); |
| 298 | SetGoal(kFenderShotGoal); |
| 299 | } else if (data.PosEdge(kHumanPlayerShot)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 300 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 301 | LOG(DEBUG, "Canceling\n"); |
| 302 | SetGoal(kHumanShotGoal); |
| 303 | } else if (data.PosEdge(kUserLeft)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 304 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 305 | LOG(DEBUG, "Canceling\n"); |
| 306 | SetGoal(k254PassGoal); |
| 307 | } else if (data.PosEdge(kUserRight)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 308 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 309 | LOG(DEBUG, "Canceling\n"); |
| 310 | SetGoal(kDemoShotGoal); |
| 311 | } else if (data.PosEdge(kTrussShot)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 312 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 313 | LOG(DEBUG, "Canceling\n"); |
| 314 | SetGoal(kTrussShotGoal); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | if (data.PosEdge(kFire)) { |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 319 | EnqueueAction(shoot_action_factory_.Make(0.0)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 320 | } else if (data.NegEdge(kFire)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 321 | CancelCurrentAction(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 324 | if (data.IsPressed(kUnload) || data.IsPressed(kReload)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 325 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 326 | LOG(DEBUG, "Canceling\n"); |
| 327 | intake_power_ = 0.0; |
| 328 | velocity_compensation_ = 0.0; |
| 329 | } |
| 330 | |
| 331 | // Send out the claw and shooter goals if no actions are running. |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 332 | if (!ActionRunning()) { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 333 | goal_angle_ += claw_goal_adjust; |
| 334 | separation_angle_ += claw_separation_adjust; |
| 335 | |
| 336 | // If the action just ended, turn the intake off and stop velocity |
| 337 | // compensating. |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 338 | if (was_running_action()) { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 339 | intake_power_ = 0.0; |
| 340 | velocity_compensation_ = 0.0; |
| 341 | } |
| 342 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 343 | ::frc971::control_loops::drivetrain_queue.status.FetchLatest(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 344 | double goal_angle = goal_angle_; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 345 | if (::frc971::control_loops::drivetrain_queue.status.get()) { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 346 | goal_angle += SpeedToAngleOffset( |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 347 | ::frc971::control_loops::drivetrain_queue.status->robot_speed); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 348 | } else { |
| 349 | LOG_INTERVAL(no_drivetrain_status_); |
| 350 | } |
| 351 | |
| 352 | if (moving_for_shot_) { |
| 353 | auto &claw_status = control_loops::claw_queue.status; |
| 354 | claw_status.FetchLatest(); |
| 355 | if (claw_status.get()) { |
| 356 | if (::std::abs(claw_status->bottom - goal_angle) < 0.2) { |
| 357 | moving_for_shot_ = false; |
| 358 | separation_angle_ = shot_separation_angle_; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | double separation_angle = separation_angle_; |
| 364 | |
| 365 | if (data.IsPressed(kCatch)) { |
| 366 | const double kCatchSeparation = 1.0; |
| 367 | goal_angle -= kCatchSeparation / 2.0; |
| 368 | separation_angle = kCatchSeparation; |
| 369 | } |
| 370 | |
| 371 | bool intaking = |
| 372 | data.IsPressed(kRollersIn) || data.IsPressed(kIntakePosition) || |
| 373 | data.IsPressed(kIntakeOpenPosition) || data.IsPressed(kCatch); |
| 374 | if (!control_loops::claw_queue.goal.MakeWithBuilder() |
| 375 | .bottom_angle(goal_angle) |
| 376 | .separation_angle(separation_angle) |
| 377 | .intake(intaking ? 12.0 |
| 378 | : (data.IsPressed(kRollersOut) ? -12.0 |
| 379 | : intake_power_)) |
| 380 | .centering(intaking ? 12.0 : 0.0) |
| 381 | .Send()) { |
| 382 | LOG(WARNING, "sending claw goal failed\n"); |
| 383 | } |
| 384 | |
| 385 | if (!control_loops::shooter_queue.goal.MakeWithBuilder() |
| 386 | .shot_power(shot_power_) |
| 387 | .shot_requested(data.IsPressed(kFire)) |
| 388 | .unload_requested(data.IsPressed(kUnload)) |
| 389 | .load_requested(data.IsPressed(kReload)) |
| 390 | .Send()) { |
| 391 | LOG(WARNING, "sending shooter goal failed\n"); |
| 392 | } |
| 393 | } |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | double SpeedToAngleOffset(double speed) { |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 397 | const ::y2014::constants::Values &values = ::y2014::constants::GetValues(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 398 | // scale speed to a [0.0-1.0] on something close to the max |
| 399 | // TODO(austin): Change the scale factor for different shots. |
| 400 | return (speed / values.drivetrain_max_speed) * velocity_compensation_; |
| 401 | } |
| 402 | |
| 403 | private: |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 404 | double shot_power_; |
| 405 | double goal_angle_; |
| 406 | double separation_angle_, shot_separation_angle_; |
| 407 | double velocity_compensation_; |
| 408 | double intake_power_; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 409 | bool moving_for_shot_ = false; |
| 410 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 411 | actors::ShootActor::Factory shoot_action_factory_; |
| 412 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 413 | ::aos::util::SimpleLogInterval no_drivetrain_status_ = |
Austin Schuh | 61bdc60 | 2016-12-04 19:10:10 -0800 | [diff] [blame] | 414 | ::aos::util::SimpleLogInterval(::std::chrono::milliseconds(200), WARNING, |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 415 | "no drivetrain status"); |
| 416 | }; |
| 417 | |
| 418 | } // namespace joysticks |
| 419 | } // namespace input |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 420 | } // namespace y2014 |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 421 | |
| 422 | int main() { |
Brian Silverman | 5090c43 | 2016-01-02 14:44:26 -0800 | [diff] [blame] | 423 | ::aos::Init(-1); |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 424 | ::aos::ShmEventLoop event_loop; |
| 425 | ::y2014::input::joysticks::Reader reader(&event_loop); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 426 | reader.Run(); |
| 427 | ::aos::Cleanup(); |
| 428 | } |