blob: da77d9465701b7d2a6180c113cb25cec18477f3c [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
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
17using namespace frc;
18
19constexpr double kPi = 3.14159265358979323846;
20
21Joystick::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
31void Joystick::SetXChannel(int channel) { m_axes[Axis::kX] = channel; }
32
33void Joystick::SetYChannel(int channel) { m_axes[Axis::kY] = channel; }
34
35void Joystick::SetZChannel(int channel) { m_axes[Axis::kZ] = channel; }
36
37void Joystick::SetTwistChannel(int channel) { m_axes[Axis::kTwist] = channel; }
38
39void Joystick::SetThrottleChannel(int channel) {
40 m_axes[Axis::kThrottle] = channel;
41}
42
43void Joystick::SetAxisChannel(AxisType axis, int channel) {
44 m_axes[axis] = channel;
45}
46
47int Joystick::GetXChannel() const { return m_axes[Axis::kX]; }
48
49int Joystick::GetYChannel() const { return m_axes[Axis::kY]; }
50
51int Joystick::GetZChannel() const { return m_axes[Axis::kZ]; }
52
53int Joystick::GetTwistChannel() const { return m_axes[Axis::kTwist]; }
54
55int Joystick::GetThrottleChannel() const { return m_axes[Axis::kThrottle]; }
56
57double Joystick::GetX(JoystickHand hand) const {
58 return GetRawAxis(m_axes[Axis::kX]);
59}
60
61double Joystick::GetY(JoystickHand hand) const {
62 return GetRawAxis(m_axes[Axis::kY]);
63}
64
65double Joystick::GetZ() const { return GetRawAxis(m_axes[Axis::kZ]); }
66
67double Joystick::GetTwist() const { return GetRawAxis(m_axes[Axis::kTwist]); }
68
69double Joystick::GetThrottle() const {
70 return GetRawAxis(m_axes[Axis::kThrottle]);
71}
72
73double 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
91bool Joystick::GetTrigger() const { return GetRawButton(Button::kTrigger); }
92
93bool Joystick::GetTriggerPressed() {
94 return GetRawButtonPressed(Button::kTrigger);
95}
96
97bool Joystick::GetTriggerReleased() {
98 return GetRawButtonReleased(Button::kTrigger);
99}
100
101bool Joystick::GetTop() const { return GetRawButton(Button::kTop); }
102
103bool Joystick::GetTopPressed() { return GetRawButtonPressed(Button::kTop); }
104
105bool Joystick::GetTopReleased() { return GetRawButtonReleased(Button::kTop); }
106
107Joystick* 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
118bool Joystick::GetButton(ButtonType button) const {
119 int temp = button;
120 return GetRawButton(static_cast<Button>(temp));
121}
122
123double Joystick::GetMagnitude() const {
124 return std::sqrt(std::pow(GetX(), 2) + std::pow(GetY(), 2));
125}
126
127double Joystick::GetDirectionRadians() const {
128 return std::atan2(GetX(), -GetY());
129}
130
131double Joystick::GetDirectionDegrees() const {
132 return (180 / kPi) * GetDirectionRadians();
133}