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