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