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 | |
| 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 | |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 156 | class Reader : public ::aos::input::ActionJoystickInput { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 157 | public: |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 158 | Reader(::aos::EventLoop *event_loop) |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 159 | : ::aos::input::ActionJoystickInput( |
| 160 | event_loop, control_loops::GetDrivetrainConfig(), |
| 161 | ::aos::input::DrivetrainInputReader::InputType::kSteeringWheel, {}), |
Austin Schuh | b2461f4 | 2019-06-29 18:17:06 -0700 | [diff] [blame] | 162 | claw_status_fetcher_( |
| 163 | event_loop->MakeFetcher<::y2014::control_loops::ClawQueue::Status>( |
| 164 | ".y2014.control_loops.claw_queue.status")), |
| 165 | claw_goal_sender_( |
| 166 | event_loop->MakeSender<::y2014::control_loops::ClawQueue::Goal>( |
| 167 | ".y2014.control_loops.claw_queue.goal")), |
Austin Schuh | 493f7af | 2019-06-29 18:42:12 -0700 | [diff] [blame^] | 168 | shooter_goal_sender_( |
| 169 | event_loop->MakeSender<::y2014::control_loops::ShooterQueue::Goal>( |
| 170 | ".y2014.control_loops.shooter_queue.goal")), |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 171 | shot_power_(80.0), |
| 172 | goal_angle_(0.0), |
| 173 | separation_angle_(kGrabSeparation), |
| 174 | velocity_compensation_(0.0), |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 175 | intake_power_(0.0), |
| 176 | shoot_action_factory_(actors::ShootActor::MakeFactory(event_loop)) {} |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 177 | |
| 178 | void SetGoal(ClawGoal goal) { |
| 179 | goal_angle_ = goal.angle; |
| 180 | separation_angle_ = goal.separation; |
| 181 | moving_for_shot_ = false; |
| 182 | velocity_compensation_ = 0.0; |
| 183 | intake_power_ = 0.0; |
| 184 | } |
| 185 | |
| 186 | void SetGoal(ShotGoal goal) { |
| 187 | goal_angle_ = goal.claw.angle; |
| 188 | shot_separation_angle_ = goal.claw.separation; |
| 189 | separation_angle_ = kGrabSeparation; |
| 190 | moving_for_shot_ = true; |
| 191 | shot_power_ = goal.shot_power; |
| 192 | velocity_compensation_ = goal.velocity_compensation; |
| 193 | intake_power_ = goal.intake_power; |
| 194 | } |
| 195 | |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 196 | void HandleTeleop(const ::aos::input::driver_station::Data &data) override { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 197 | if (data.IsPressed(kRollersIn) || data.IsPressed(kRollersOut)) { |
| 198 | intake_power_ = 0.0; |
| 199 | separation_angle_ = kGrabSeparation; |
| 200 | moving_for_shot_ = false; |
| 201 | } |
| 202 | |
| 203 | static const double kAdjustClawGoalDeadband = 0.08; |
| 204 | double claw_goal_adjust = data.GetAxis(kAdjustClawGoal); |
| 205 | if (OLD_DS || ::std::abs(claw_goal_adjust) < kAdjustClawGoalDeadband) { |
| 206 | claw_goal_adjust = 0; |
| 207 | } else { |
| 208 | claw_goal_adjust = (claw_goal_adjust - |
| 209 | ((claw_goal_adjust < 0) ? -kAdjustClawGoalDeadband |
| 210 | : kAdjustClawGoalDeadband)) * |
| 211 | 0.035; |
| 212 | } |
| 213 | double claw_separation_adjust = data.GetAxis(kAdjustClawSeparation); |
| 214 | if (OLD_DS || |
| 215 | ::std::abs(claw_separation_adjust) < kAdjustClawGoalDeadband) { |
| 216 | claw_separation_adjust = 0; |
| 217 | } else { |
| 218 | claw_separation_adjust = |
| 219 | (claw_separation_adjust - |
| 220 | ((claw_separation_adjust < 0) ? -kAdjustClawGoalDeadband |
| 221 | : kAdjustClawGoalDeadband)) * |
| 222 | -0.035; |
| 223 | } |
| 224 | |
| 225 | #if OLD_DS |
| 226 | if (data.IsPressed(kFenderShot)) { |
| 227 | #else |
| 228 | if (data.GetAxis(kFlipRobot) > 0.9) { |
| 229 | #endif |
| 230 | claw_goal_adjust += claw_separation_adjust; |
| 231 | claw_goal_adjust *= -1; |
| 232 | |
| 233 | if (data.IsPressed(kIntakeOpenPosition)) { |
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(kFlippedIntakeOpenGoal); |
| 237 | } else if (data.IsPressed(kIntakePosition)) { |
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(kFlippedIntakeGoal); |
| 241 | } else if (data.IsPressed(kVerticalTuck)) { |
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(kVerticalTuckGoal); |
| 245 | } else if (data.IsPressed(kTuck)) { |
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(kFlippedTuckGoal); |
| 249 | } else if (data.PosEdge(kLongShot)) { |
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(kFlippedLongShotGoal); |
| 253 | } else if (data.PosEdge(kCloseShot)) { |
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(kFlippedMediumShotGoal); |
| 257 | } else if (data.PosEdge(kFenderShot)) { |
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(kFlippedShortShotGoal); |
| 261 | } else if (data.PosEdge(kHumanPlayerShot)) { |
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(kFlippedHumanShotGoal); |
| 265 | } else if (data.PosEdge(kUserLeft)) { |
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(kFlipped254PassGoal); |
| 269 | } else if (data.PosEdge(kUserRight)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 270 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 271 | LOG(DEBUG, "Canceling\n"); |
| 272 | SetGoal(kFlippedDemoShotGoal); |
| 273 | } else if (data.PosEdge(kTrussShot)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 274 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 275 | LOG(DEBUG, "Canceling\n"); |
| 276 | SetGoal(kFlippedTrussShotGoal); |
| 277 | } |
| 278 | } else { |
| 279 | if (data.IsPressed(kIntakeOpenPosition)) { |
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(kIntakeOpenGoal); |
| 283 | } else if (data.IsPressed(kIntakePosition)) { |
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(kIntakeGoal); |
| 287 | } else if (data.IsPressed(kVerticalTuck)) { |
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(kVerticalTuckGoal); |
| 291 | } else if (data.IsPressed(kTuck)) { |
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(kTuckGoal); |
| 295 | } else if (data.PosEdge(kLongShot)) { |
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(kLongShotGoal); |
| 299 | } else if (data.PosEdge(kCloseShot)) { |
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(kCloseShotGoal); |
| 303 | } else if (data.PosEdge(kFenderShot)) { |
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(kFenderShotGoal); |
| 307 | } else if (data.PosEdge(kHumanPlayerShot)) { |
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(kHumanShotGoal); |
| 311 | } else if (data.PosEdge(kUserLeft)) { |
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(k254PassGoal); |
| 315 | } else if (data.PosEdge(kUserRight)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 316 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 317 | LOG(DEBUG, "Canceling\n"); |
| 318 | SetGoal(kDemoShotGoal); |
| 319 | } else if (data.PosEdge(kTrussShot)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 320 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 321 | LOG(DEBUG, "Canceling\n"); |
| 322 | SetGoal(kTrussShotGoal); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | if (data.PosEdge(kFire)) { |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 327 | EnqueueAction(shoot_action_factory_.Make(0.0)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 328 | } else if (data.NegEdge(kFire)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 329 | CancelCurrentAction(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 332 | if (data.IsPressed(kUnload) || data.IsPressed(kReload)) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 333 | CancelAllActions(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 334 | LOG(DEBUG, "Canceling\n"); |
| 335 | intake_power_ = 0.0; |
| 336 | velocity_compensation_ = 0.0; |
| 337 | } |
| 338 | |
| 339 | // Send out the claw and shooter goals if no actions are running. |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 340 | if (!ActionRunning()) { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 341 | goal_angle_ += claw_goal_adjust; |
| 342 | separation_angle_ += claw_separation_adjust; |
| 343 | |
| 344 | // If the action just ended, turn the intake off and stop velocity |
| 345 | // compensating. |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 346 | if (was_running_action()) { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 347 | intake_power_ = 0.0; |
| 348 | velocity_compensation_ = 0.0; |
| 349 | } |
| 350 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 351 | ::frc971::control_loops::drivetrain_queue.status.FetchLatest(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 352 | double goal_angle = goal_angle_; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 353 | if (::frc971::control_loops::drivetrain_queue.status.get()) { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 354 | goal_angle += SpeedToAngleOffset( |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 355 | ::frc971::control_loops::drivetrain_queue.status->robot_speed); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 356 | } else { |
| 357 | LOG_INTERVAL(no_drivetrain_status_); |
| 358 | } |
| 359 | |
| 360 | if (moving_for_shot_) { |
Austin Schuh | b2461f4 | 2019-06-29 18:17:06 -0700 | [diff] [blame] | 361 | claw_status_fetcher_.Fetch(); |
| 362 | if (claw_status_fetcher_.get()) { |
| 363 | if (::std::abs(claw_status_fetcher_->bottom - goal_angle) < 0.2) { |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 364 | moving_for_shot_ = false; |
| 365 | separation_angle_ = shot_separation_angle_; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | double separation_angle = separation_angle_; |
| 371 | |
| 372 | if (data.IsPressed(kCatch)) { |
| 373 | const double kCatchSeparation = 1.0; |
| 374 | goal_angle -= kCatchSeparation / 2.0; |
| 375 | separation_angle = kCatchSeparation; |
| 376 | } |
| 377 | |
| 378 | bool intaking = |
| 379 | data.IsPressed(kRollersIn) || data.IsPressed(kIntakePosition) || |
| 380 | data.IsPressed(kIntakeOpenPosition) || data.IsPressed(kCatch); |
Austin Schuh | b2461f4 | 2019-06-29 18:17:06 -0700 | [diff] [blame] | 381 | { |
| 382 | auto goal_message = claw_goal_sender_.MakeMessage(); |
| 383 | goal_message->bottom_angle = goal_angle; |
| 384 | goal_message->separation_angle = separation_angle; |
| 385 | goal_message->intake = |
| 386 | intaking ? 12.0 |
| 387 | : (data.IsPressed(kRollersOut) ? -12.0 : intake_power_); |
| 388 | goal_message->centering = intaking ? 12.0 : 0.0; |
| 389 | |
| 390 | if (!goal_message.Send()) { |
| 391 | LOG(WARNING, "sending claw goal failed\n"); |
| 392 | } |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Austin Schuh | 493f7af | 2019-06-29 18:42:12 -0700 | [diff] [blame^] | 395 | { |
| 396 | auto goal_message = shooter_goal_sender_.MakeMessage(); |
| 397 | goal_message->shot_power = shot_power_; |
| 398 | goal_message->shot_requested = data.IsPressed(kFire); |
| 399 | goal_message->unload_requested = data.IsPressed(kUnload); |
| 400 | goal_message->load_requested = data.IsPressed(kReload); |
| 401 | if (!goal_message.Send()) { |
| 402 | LOG(WARNING, "sending shooter goal failed\n"); |
| 403 | } |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 404 | } |
| 405 | } |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | double SpeedToAngleOffset(double speed) { |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 409 | const ::y2014::constants::Values &values = ::y2014::constants::GetValues(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 410 | // scale speed to a [0.0-1.0] on something close to the max |
| 411 | // TODO(austin): Change the scale factor for different shots. |
| 412 | return (speed / values.drivetrain_max_speed) * velocity_compensation_; |
| 413 | } |
| 414 | |
| 415 | private: |
Austin Schuh | b2461f4 | 2019-06-29 18:17:06 -0700 | [diff] [blame] | 416 | ::aos::Fetcher<::y2014::control_loops::ClawQueue::Status> |
| 417 | claw_status_fetcher_; |
| 418 | ::aos::Sender<::y2014::control_loops::ClawQueue::Goal> claw_goal_sender_; |
Austin Schuh | 493f7af | 2019-06-29 18:42:12 -0700 | [diff] [blame^] | 419 | ::aos::Sender<::y2014::control_loops::ShooterQueue::Goal> shooter_goal_sender_; |
Austin Schuh | b2461f4 | 2019-06-29 18:17:06 -0700 | [diff] [blame] | 420 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 421 | double shot_power_; |
| 422 | double goal_angle_; |
| 423 | double separation_angle_, shot_separation_angle_; |
| 424 | double velocity_compensation_; |
| 425 | double intake_power_; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 426 | bool moving_for_shot_ = false; |
| 427 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 428 | actors::ShootActor::Factory shoot_action_factory_; |
| 429 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 430 | ::aos::util::SimpleLogInterval no_drivetrain_status_ = |
Austin Schuh | 61bdc60 | 2016-12-04 19:10:10 -0800 | [diff] [blame] | 431 | ::aos::util::SimpleLogInterval(::std::chrono::milliseconds(200), WARNING, |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 432 | "no drivetrain status"); |
| 433 | }; |
| 434 | |
| 435 | } // namespace joysticks |
| 436 | } // namespace input |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 437 | } // namespace y2014 |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 438 | |
| 439 | int main() { |
Brian Silverman | 5090c43 | 2016-01-02 14:44:26 -0800 | [diff] [blame] | 440 | ::aos::Init(-1); |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 441 | ::aos::ShmEventLoop event_loop; |
| 442 | ::y2014::input::joysticks::Reader reader(&event_loop); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 443 | reader.Run(); |
| 444 | ::aos::Cleanup(); |
| 445 | } |