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