Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */ |
| 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/HAL.h> |
| 13 | |
| 14 | #include "frc/DriverStation.h" |
| 15 | #include "frc/WPIErrors.h" |
| 16 | |
| 17 | using namespace frc; |
| 18 | |
| 19 | constexpr double kPi = 3.14159265358979323846; |
| 20 | |
| 21 | Joystick::Joystick(int port) : GenericHID(port) { |
| 22 | m_axes[Axis::kX] = kDefaultXChannel; |
| 23 | m_axes[Axis::kY] = kDefaultYChannel; |
| 24 | m_axes[Axis::kZ] = kDefaultZChannel; |
| 25 | m_axes[Axis::kTwist] = kDefaultTwistChannel; |
| 26 | m_axes[Axis::kThrottle] = kDefaultThrottleChannel; |
| 27 | |
| 28 | HAL_Report(HALUsageReporting::kResourceType_Joystick, port); |
| 29 | } |
| 30 | |
| 31 | void Joystick::SetXChannel(int channel) { m_axes[Axis::kX] = channel; } |
| 32 | |
| 33 | void Joystick::SetYChannel(int channel) { m_axes[Axis::kY] = channel; } |
| 34 | |
| 35 | void Joystick::SetZChannel(int channel) { m_axes[Axis::kZ] = channel; } |
| 36 | |
| 37 | void Joystick::SetTwistChannel(int channel) { m_axes[Axis::kTwist] = channel; } |
| 38 | |
| 39 | void Joystick::SetThrottleChannel(int channel) { |
| 40 | m_axes[Axis::kThrottle] = channel; |
| 41 | } |
| 42 | |
| 43 | void Joystick::SetAxisChannel(AxisType axis, int channel) { |
| 44 | m_axes[axis] = channel; |
| 45 | } |
| 46 | |
| 47 | int Joystick::GetXChannel() const { return m_axes[Axis::kX]; } |
| 48 | |
| 49 | int Joystick::GetYChannel() const { return m_axes[Axis::kY]; } |
| 50 | |
| 51 | int Joystick::GetZChannel() const { return m_axes[Axis::kZ]; } |
| 52 | |
| 53 | int Joystick::GetTwistChannel() const { return m_axes[Axis::kTwist]; } |
| 54 | |
| 55 | int Joystick::GetThrottleChannel() const { return m_axes[Axis::kThrottle]; } |
| 56 | |
| 57 | double Joystick::GetX(JoystickHand hand) const { |
| 58 | return GetRawAxis(m_axes[Axis::kX]); |
| 59 | } |
| 60 | |
| 61 | double Joystick::GetY(JoystickHand hand) const { |
| 62 | return GetRawAxis(m_axes[Axis::kY]); |
| 63 | } |
| 64 | |
| 65 | double Joystick::GetZ() const { return GetRawAxis(m_axes[Axis::kZ]); } |
| 66 | |
| 67 | double Joystick::GetTwist() const { return GetRawAxis(m_axes[Axis::kTwist]); } |
| 68 | |
| 69 | double Joystick::GetThrottle() const { |
| 70 | return GetRawAxis(m_axes[Axis::kThrottle]); |
| 71 | } |
| 72 | |
| 73 | double Joystick::GetAxis(AxisType axis) const { |
| 74 | switch (axis) { |
| 75 | case kXAxis: |
| 76 | return GetX(); |
| 77 | case kYAxis: |
| 78 | return GetY(); |
| 79 | case kZAxis: |
| 80 | return GetZ(); |
| 81 | case kTwistAxis: |
| 82 | return GetTwist(); |
| 83 | case kThrottleAxis: |
| 84 | return GetThrottle(); |
| 85 | default: |
| 86 | wpi_setWPIError(BadJoystickAxis); |
| 87 | return 0.0; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | bool Joystick::GetTrigger() const { return GetRawButton(Button::kTrigger); } |
| 92 | |
| 93 | bool Joystick::GetTriggerPressed() { |
| 94 | return GetRawButtonPressed(Button::kTrigger); |
| 95 | } |
| 96 | |
| 97 | bool Joystick::GetTriggerReleased() { |
| 98 | return GetRawButtonReleased(Button::kTrigger); |
| 99 | } |
| 100 | |
| 101 | bool Joystick::GetTop() const { return GetRawButton(Button::kTop); } |
| 102 | |
| 103 | bool Joystick::GetTopPressed() { return GetRawButtonPressed(Button::kTop); } |
| 104 | |
| 105 | bool Joystick::GetTopReleased() { return GetRawButtonReleased(Button::kTop); } |
| 106 | |
| 107 | Joystick* Joystick::GetStickForPort(int port) { |
| 108 | static std::array<std::unique_ptr<Joystick>, DriverStation::kJoystickPorts> |
| 109 | joysticks{}; |
| 110 | auto stick = joysticks[port].get(); |
| 111 | if (stick == nullptr) { |
| 112 | joysticks[port] = std::make_unique<Joystick>(port); |
| 113 | stick = joysticks[port].get(); |
| 114 | } |
| 115 | return stick; |
| 116 | } |
| 117 | |
| 118 | bool Joystick::GetButton(ButtonType button) const { |
| 119 | int temp = button; |
| 120 | return GetRawButton(static_cast<Button>(temp)); |
| 121 | } |
| 122 | |
| 123 | double Joystick::GetMagnitude() const { |
| 124 | return std::sqrt(std::pow(GetX(), 2) + std::pow(GetY(), 2)); |
| 125 | } |
| 126 | |
| 127 | double Joystick::GetDirectionRadians() const { |
| 128 | return std::atan2(GetX(), -GetY()); |
| 129 | } |
| 130 | |
| 131 | double Joystick::GetDirectionDegrees() const { |
| 132 | return (180 / kPi) * GetDirectionRadians(); |
| 133 | } |