Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "frc/Joystick.h" |
| 6 | |
| 7 | #include <cmath> |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 8 | #include <numbers> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 9 | |
| 10 | #include <hal/FRCUsageReporting.h> |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 11 | #include <units/dimensionless.h> |
| 12 | #include <units/math.h> |
| 13 | #include <wpi/deprecated.h> |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 14 | |
| 15 | #include "frc/event/BooleanEvent.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 16 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 17 | using namespace frc; |
| 18 | |
| 19 | Joystick::Joystick(int port) : GenericHID(port) { |
| 20 | m_axes[Axis::kX] = kDefaultXChannel; |
| 21 | m_axes[Axis::kY] = kDefaultYChannel; |
| 22 | m_axes[Axis::kZ] = kDefaultZChannel; |
| 23 | m_axes[Axis::kTwist] = kDefaultTwistChannel; |
| 24 | m_axes[Axis::kThrottle] = kDefaultThrottleChannel; |
| 25 | |
| 26 | HAL_Report(HALUsageReporting::kResourceType_Joystick, port + 1); |
| 27 | } |
| 28 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 29 | void Joystick::SetXChannel(int channel) { |
| 30 | m_axes[Axis::kX] = channel; |
| 31 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 32 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 33 | void Joystick::SetYChannel(int channel) { |
| 34 | m_axes[Axis::kY] = channel; |
| 35 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 36 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 37 | void Joystick::SetZChannel(int channel) { |
| 38 | m_axes[Axis::kZ] = channel; |
| 39 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 40 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 41 | void Joystick::SetTwistChannel(int channel) { |
| 42 | m_axes[Axis::kTwist] = channel; |
| 43 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 44 | |
| 45 | void Joystick::SetThrottleChannel(int channel) { |
| 46 | m_axes[Axis::kThrottle] = channel; |
| 47 | } |
| 48 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 49 | int Joystick::GetXChannel() const { |
| 50 | return m_axes[Axis::kX]; |
| 51 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 52 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 53 | int Joystick::GetYChannel() const { |
| 54 | return m_axes[Axis::kY]; |
| 55 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 56 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 57 | int Joystick::GetZChannel() const { |
| 58 | return m_axes[Axis::kZ]; |
| 59 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 60 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 61 | int Joystick::GetTwistChannel() const { |
| 62 | return m_axes[Axis::kTwist]; |
| 63 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 64 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 65 | int Joystick::GetThrottleChannel() const { |
| 66 | return m_axes[Axis::kThrottle]; |
| 67 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 68 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 69 | double Joystick::GetX() const { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 70 | return GetRawAxis(m_axes[Axis::kX]); |
| 71 | } |
| 72 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 73 | double Joystick::GetY() const { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 74 | return GetRawAxis(m_axes[Axis::kY]); |
| 75 | } |
| 76 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 77 | double Joystick::GetZ() const { |
| 78 | return GetRawAxis(m_axes[Axis::kZ]); |
| 79 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 80 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 81 | double Joystick::GetTwist() const { |
| 82 | return GetRawAxis(m_axes[Axis::kTwist]); |
| 83 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 84 | |
| 85 | double Joystick::GetThrottle() const { |
| 86 | return GetRawAxis(m_axes[Axis::kThrottle]); |
| 87 | } |
| 88 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 89 | bool Joystick::GetTrigger() const { |
| 90 | return GetRawButton(Button::kTrigger); |
| 91 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 92 | |
| 93 | bool Joystick::GetTriggerPressed() { |
| 94 | return GetRawButtonPressed(Button::kTrigger); |
| 95 | } |
| 96 | |
| 97 | bool Joystick::GetTriggerReleased() { |
| 98 | return GetRawButtonReleased(Button::kTrigger); |
| 99 | } |
| 100 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 101 | BooleanEvent Joystick::Trigger(EventLoop* loop) const { |
| 102 | return BooleanEvent(loop, [this]() { return this->GetTrigger(); }); |
| 103 | } |
| 104 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 105 | bool Joystick::GetTop() const { |
| 106 | return GetRawButton(Button::kTop); |
| 107 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 108 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 109 | bool Joystick::GetTopPressed() { |
| 110 | return GetRawButtonPressed(Button::kTop); |
| 111 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 112 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 113 | bool Joystick::GetTopReleased() { |
| 114 | return GetRawButtonReleased(Button::kTop); |
| 115 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 116 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 117 | BooleanEvent Joystick::Top(EventLoop* loop) const { |
| 118 | return BooleanEvent(loop, [this]() { return this->GetTop(); }); |
| 119 | } |
| 120 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 121 | double Joystick::GetMagnitude() const { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 122 | return std::hypot(GetX(), GetY()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | double Joystick::GetDirectionRadians() const { |
| 126 | return std::atan2(GetX(), -GetY()); |
| 127 | } |
| 128 | |
| 129 | double Joystick::GetDirectionDegrees() const { |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 130 | WPI_IGNORE_DEPRECATED |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 131 | return (180 / std::numbers::pi) * GetDirectionRadians(); |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 132 | WPI_UNIGNORE_DEPRECATED |
| 133 | } |
| 134 | |
| 135 | units::radian_t Joystick::GetDirection() const { |
| 136 | return units::math::atan2(units::dimensionless::scalar_t{GetX()}, |
| 137 | units::dimensionless::scalar_t{-GetY()}); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 138 | } |