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> |
| 8 | |
| 9 | #include <hal/FRCUsageReporting.h> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 10 | #include <wpi/numbers> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 11 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 12 | using namespace frc; |
| 13 | |
| 14 | Joystick::Joystick(int port) : GenericHID(port) { |
| 15 | m_axes[Axis::kX] = kDefaultXChannel; |
| 16 | m_axes[Axis::kY] = kDefaultYChannel; |
| 17 | m_axes[Axis::kZ] = kDefaultZChannel; |
| 18 | m_axes[Axis::kTwist] = kDefaultTwistChannel; |
| 19 | m_axes[Axis::kThrottle] = kDefaultThrottleChannel; |
| 20 | |
| 21 | HAL_Report(HALUsageReporting::kResourceType_Joystick, port + 1); |
| 22 | } |
| 23 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 24 | void Joystick::SetXChannel(int channel) { |
| 25 | m_axes[Axis::kX] = channel; |
| 26 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 27 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 28 | void Joystick::SetYChannel(int channel) { |
| 29 | m_axes[Axis::kY] = channel; |
| 30 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 31 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 32 | void Joystick::SetZChannel(int channel) { |
| 33 | m_axes[Axis::kZ] = channel; |
| 34 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 35 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 36 | void Joystick::SetTwistChannel(int channel) { |
| 37 | m_axes[Axis::kTwist] = channel; |
| 38 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 39 | |
| 40 | void Joystick::SetThrottleChannel(int channel) { |
| 41 | m_axes[Axis::kThrottle] = channel; |
| 42 | } |
| 43 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 44 | int Joystick::GetXChannel() const { |
| 45 | return m_axes[Axis::kX]; |
| 46 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 47 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 48 | int Joystick::GetYChannel() const { |
| 49 | return m_axes[Axis::kY]; |
| 50 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 51 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 52 | int Joystick::GetZChannel() const { |
| 53 | return m_axes[Axis::kZ]; |
| 54 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 55 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 56 | int Joystick::GetTwistChannel() const { |
| 57 | return m_axes[Axis::kTwist]; |
| 58 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 59 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 60 | int Joystick::GetThrottleChannel() const { |
| 61 | return m_axes[Axis::kThrottle]; |
| 62 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 63 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 64 | double Joystick::GetX() const { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 65 | return GetRawAxis(m_axes[Axis::kX]); |
| 66 | } |
| 67 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 68 | double Joystick::GetY() const { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 69 | return GetRawAxis(m_axes[Axis::kY]); |
| 70 | } |
| 71 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 72 | double Joystick::GetZ() const { |
| 73 | return GetRawAxis(m_axes[Axis::kZ]); |
| 74 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 75 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 76 | double Joystick::GetTwist() const { |
| 77 | return GetRawAxis(m_axes[Axis::kTwist]); |
| 78 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 79 | |
| 80 | double Joystick::GetThrottle() const { |
| 81 | return GetRawAxis(m_axes[Axis::kThrottle]); |
| 82 | } |
| 83 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 84 | bool Joystick::GetTrigger() const { |
| 85 | return GetRawButton(Button::kTrigger); |
| 86 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 87 | |
| 88 | bool Joystick::GetTriggerPressed() { |
| 89 | return GetRawButtonPressed(Button::kTrigger); |
| 90 | } |
| 91 | |
| 92 | bool Joystick::GetTriggerReleased() { |
| 93 | return GetRawButtonReleased(Button::kTrigger); |
| 94 | } |
| 95 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 96 | bool Joystick::GetTop() const { |
| 97 | return GetRawButton(Button::kTop); |
| 98 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 99 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 100 | bool Joystick::GetTopPressed() { |
| 101 | return GetRawButtonPressed(Button::kTop); |
| 102 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 103 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 104 | bool Joystick::GetTopReleased() { |
| 105 | return GetRawButtonReleased(Button::kTop); |
| 106 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 107 | |
| 108 | double Joystick::GetMagnitude() const { |
| 109 | return std::sqrt(std::pow(GetX(), 2) + std::pow(GetY(), 2)); |
| 110 | } |
| 111 | |
| 112 | double Joystick::GetDirectionRadians() const { |
| 113 | return std::atan2(GetX(), -GetY()); |
| 114 | } |
| 115 | |
| 116 | double Joystick::GetDirectionDegrees() const { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 117 | return (180 / wpi::numbers::pi) * GetDirectionRadians(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 118 | } |