Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 2 | /* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */ |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "frc/Joystick.h" |
| 9 | |
| 10 | #include <cmath> |
| 11 | |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame^] | 12 | #include <hal/FRCUsageReporting.h> |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 13 | #include <wpi/math> |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 14 | |
| 15 | #include "frc/DriverStation.h" |
| 16 | #include "frc/WPIErrors.h" |
| 17 | |
| 18 | using namespace frc; |
| 19 | |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 20 | Joystick::Joystick(int port) : GenericHID(port) { |
| 21 | m_axes[Axis::kX] = kDefaultXChannel; |
| 22 | m_axes[Axis::kY] = kDefaultYChannel; |
| 23 | m_axes[Axis::kZ] = kDefaultZChannel; |
| 24 | m_axes[Axis::kTwist] = kDefaultTwistChannel; |
| 25 | m_axes[Axis::kThrottle] = kDefaultThrottleChannel; |
| 26 | |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame^] | 27 | HAL_Report(HALUsageReporting::kResourceType_Joystick, port + 1); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void Joystick::SetXChannel(int channel) { m_axes[Axis::kX] = channel; } |
| 31 | |
| 32 | void Joystick::SetYChannel(int channel) { m_axes[Axis::kY] = channel; } |
| 33 | |
| 34 | void Joystick::SetZChannel(int channel) { m_axes[Axis::kZ] = channel; } |
| 35 | |
| 36 | void Joystick::SetTwistChannel(int channel) { m_axes[Axis::kTwist] = channel; } |
| 37 | |
| 38 | void Joystick::SetThrottleChannel(int channel) { |
| 39 | m_axes[Axis::kThrottle] = channel; |
| 40 | } |
| 41 | |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 42 | int Joystick::GetXChannel() const { return m_axes[Axis::kX]; } |
| 43 | |
| 44 | int Joystick::GetYChannel() const { return m_axes[Axis::kY]; } |
| 45 | |
| 46 | int Joystick::GetZChannel() const { return m_axes[Axis::kZ]; } |
| 47 | |
| 48 | int Joystick::GetTwistChannel() const { return m_axes[Axis::kTwist]; } |
| 49 | |
| 50 | int Joystick::GetThrottleChannel() const { return m_axes[Axis::kThrottle]; } |
| 51 | |
| 52 | double Joystick::GetX(JoystickHand hand) const { |
| 53 | return GetRawAxis(m_axes[Axis::kX]); |
| 54 | } |
| 55 | |
| 56 | double Joystick::GetY(JoystickHand hand) const { |
| 57 | return GetRawAxis(m_axes[Axis::kY]); |
| 58 | } |
| 59 | |
| 60 | double Joystick::GetZ() const { return GetRawAxis(m_axes[Axis::kZ]); } |
| 61 | |
| 62 | double Joystick::GetTwist() const { return GetRawAxis(m_axes[Axis::kTwist]); } |
| 63 | |
| 64 | double Joystick::GetThrottle() const { |
| 65 | return GetRawAxis(m_axes[Axis::kThrottle]); |
| 66 | } |
| 67 | |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 68 | bool Joystick::GetTrigger() const { return GetRawButton(Button::kTrigger); } |
| 69 | |
| 70 | bool Joystick::GetTriggerPressed() { |
| 71 | return GetRawButtonPressed(Button::kTrigger); |
| 72 | } |
| 73 | |
| 74 | bool Joystick::GetTriggerReleased() { |
| 75 | return GetRawButtonReleased(Button::kTrigger); |
| 76 | } |
| 77 | |
| 78 | bool Joystick::GetTop() const { return GetRawButton(Button::kTop); } |
| 79 | |
| 80 | bool Joystick::GetTopPressed() { return GetRawButtonPressed(Button::kTop); } |
| 81 | |
| 82 | bool Joystick::GetTopReleased() { return GetRawButtonReleased(Button::kTop); } |
| 83 | |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 84 | double Joystick::GetMagnitude() const { |
| 85 | return std::sqrt(std::pow(GetX(), 2) + std::pow(GetY(), 2)); |
| 86 | } |
| 87 | |
| 88 | double Joystick::GetDirectionRadians() const { |
| 89 | return std::atan2(GetX(), -GetY()); |
| 90 | } |
| 91 | |
| 92 | double Joystick::GetDirectionDegrees() const { |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 93 | return (180 / wpi::math::pi) * GetDirectionRadians(); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 94 | } |