blob: fc697b15d039631475dcf8e4c52b6003937d9f4a [file] [log] [blame]
Brian Silvermanf7f267a2017-02-04 16:16:08 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008-2017. 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 "Joystick.h"
9
10#include <cmath>
11
12#include "DriverStation.h"
13#include "WPIErrors.h"
14
15using namespace frc;
16
17const int Joystick::kDefaultXAxis;
18const int Joystick::kDefaultYAxis;
19const int Joystick::kDefaultZAxis;
20const int Joystick::kDefaultTwistAxis;
21const int Joystick::kDefaultThrottleAxis;
22const int Joystick::kDefaultTriggerButton;
23const int Joystick::kDefaultTopButton;
24static Joystick* joysticks[DriverStation::kJoystickPorts];
25static bool joySticksInitialized = false;
26
27/**
28 * Construct an instance of a joystick.
29 *
30 * The joystick index is the USB port on the Driver Station.
31 *
32 * @param port The port on the Driver Station that the joystick is plugged into
33 * (0-5).
34 */
35Joystick::Joystick(int port) : Joystick(port, kNumAxisTypes, kNumButtonTypes) {
36 m_axes[kXAxis] = kDefaultXAxis;
37 m_axes[kYAxis] = kDefaultYAxis;
38 m_axes[kZAxis] = kDefaultZAxis;
39 m_axes[kTwistAxis] = kDefaultTwistAxis;
40 m_axes[kThrottleAxis] = kDefaultThrottleAxis;
41
42 m_buttons[kTriggerButton] = kDefaultTriggerButton;
43 m_buttons[kTopButton] = kDefaultTopButton;
44}
45
46/**
47 * Version of the constructor to be called by sub-classes.
48 *
49 * This constructor allows the subclass to configure the number of constants
50 * for axes and buttons.
51 *
52 * @param port The port on the Driver Station that the joystick is
53 * plugged into.
54 * @param numAxisTypes The number of axis types in the enum.
55 * @param numButtonTypes The number of button types in the enum.
56 */
57Joystick::Joystick(int port, int numAxisTypes, int numButtonTypes)
58 : JoystickBase(port),
59 m_ds(DriverStation::GetInstance()),
60 m_axes(numAxisTypes),
61 m_buttons(numButtonTypes) {
62 if (!joySticksInitialized) {
63 for (auto& joystick : joysticks) joystick = nullptr;
64 joySticksInitialized = true;
65 }
66 if (GetPort() >= DriverStation::kJoystickPorts) {
67 wpi_setWPIError(BadJoystickIndex);
68 } else {
69 joysticks[GetPort()] = this;
70 }
71}
72
73Joystick* Joystick::GetStickForPort(int port) {
74 Joystick* stick = joysticks[port];
75 if (stick == nullptr) {
76 stick = new Joystick(port);
77 joysticks[port] = stick;
78 }
79 return stick;
80}
81
82/**
83 * Get the X value of the joystick.
84 *
85 * This depends on the mapping of the joystick connected to the current port.
86 *
87 * @param hand This parameter is ignored for the Joystick class and is only
88 * here to complete the GenericHID interface.
89 */
90double Joystick::GetX(JoystickHand hand) const {
91 return GetRawAxis(m_axes[kXAxis]);
92}
93
94/**
95 * Get the Y value of the joystick.
96 *
97 * This depends on the mapping of the joystick connected to the current port.
98 *
99 * @param hand This parameter is ignored for the Joystick class and is only
100 * here to complete the GenericHID interface.
101 */
102double Joystick::GetY(JoystickHand hand) const {
103 return GetRawAxis(m_axes[kYAxis]);
104}
105
106/**
107 * Get the Z value of the current joystick.
108 *
109 * This depends on the mapping of the joystick connected to the current port.
110 */
111double Joystick::GetZ(JoystickHand hand) const {
112 return GetRawAxis(m_axes[kZAxis]);
113}
114
115/**
116 * Get the twist value of the current joystick.
117 *
118 * This depends on the mapping of the joystick connected to the current port.
119 */
120double Joystick::GetTwist() const { return GetRawAxis(m_axes[kTwistAxis]); }
121
122/**
123 * Get the throttle value of the current joystick.
124 *
125 * This depends on the mapping of the joystick connected to the current port.
126 */
127double Joystick::GetThrottle() const {
128 return GetRawAxis(m_axes[kThrottleAxis]);
129}
130
131/**
132 * For the current joystick, return the axis determined by the argument.
133 *
134 * This is for cases where the joystick axis is returned programatically,
135 * otherwise one of the previous functions would be preferable (for example
136 * GetX()).
137 *
138 * @param axis The axis to read.
139 * @return The value of the axis.
140 */
141double Joystick::GetAxis(AxisType axis) const {
142 switch (axis) {
143 case kXAxis:
144 return this->GetX();
145 case kYAxis:
146 return this->GetY();
147 case kZAxis:
148 return this->GetZ();
149 case kTwistAxis:
150 return this->GetTwist();
151 case kThrottleAxis:
152 return this->GetThrottle();
153 default:
154 wpi_setWPIError(BadJoystickAxis);
155 return 0.0;
156 }
157}
158
159/**
160 * Read the state of the trigger on the joystick.
161 *
162 * Look up which button has been assigned to the trigger and read its state.
163 *
164 * @param hand This parameter is ignored for the Joystick class and is only
165 * here to complete the GenericHID interface.
166 * @return The state of the trigger.
167 */
168bool Joystick::GetTrigger(JoystickHand hand) const {
169 return GetRawButton(m_buttons[kTriggerButton]);
170}
171
172/**
173 * Read the state of the top button on the joystick.
174 *
175 * Look up which button has been assigned to the top and read its state.
176 *
177 * @param hand This parameter is ignored for the Joystick class and is only
178 * here to complete the GenericHID interface.
179 * @return The state of the top button.
180 */
181bool Joystick::GetTop(JoystickHand hand) const {
182 return GetRawButton(m_buttons[kTopButton]);
183}
184
185/**
186 * Get buttons based on an enumerated type.
187 *
188 * The button type will be looked up in the list of buttons and then read.
189 *
190 * @param button The type of button to read.
191 * @return The state of the button.
192 */
193bool Joystick::GetButton(ButtonType button) const {
194 switch (button) {
195 case kTriggerButton:
196 return GetTrigger();
197 case kTopButton:
198 return GetTop();
199 default:
200 return false;
201 }
202}
203
204/**
205 * Get the number of axis for a joystick
206 *
207 * @return the number of axis for the current joystick
208 */
209int Joystick::GetAxisCount() const { return 0; }
210
211/**
212 * Get the axis type of a joystick axis.
213 *
214 * @return the axis type of a joystick axis.
215 */
216int Joystick::GetAxisType(int axis) const { return 0; }
217
218/**
219 * Get the number of buttons for a joystick.
220 *
221 * @return the number of buttons on the current joystick
222 */
223int Joystick::GetButtonCount() const { return 0; }
224
225/**
226 * Get the channel currently associated with the specified axis.
227 *
228 * @param axis The axis to look up the channel for.
229 * @return The channel fr the axis.
230 */
231int Joystick::GetAxisChannel(AxisType axis) const { return m_axes[axis]; }
232
233/**
234 * Set the channel associated with a specified axis.
235 *
236 * @param axis The axis to set the channel for.
237 * @param channel The channel to set the axis to.
238 */
239void Joystick::SetAxisChannel(AxisType axis, int channel) {
240 m_axes[axis] = channel;
241}
242
243/**
244 * Get the magnitude of the direction vector formed by the joystick's
245 * current position relative to its origin.
246 *
247 * @return The magnitude of the direction vector
248 */
249double Joystick::GetMagnitude() const {
250 return std::sqrt(std::pow(GetX(), 2) + std::pow(GetY(), 2));
251}
252
253/**
254 * Get the direction of the vector formed by the joystick and its origin
255 * in radians.
256 *
257 * @return The direction of the vector in radians
258 */
259double Joystick::GetDirectionRadians() const {
260 return std::atan2(GetX(), -GetY());
261}
262
263/**
264 * Get the direction of the vector formed by the joystick and its origin
265 * in degrees.
266 *
267 * uses std::acos(-1) to represent Pi due to absence of readily accessible Pi
268 * constant in C++
269 *
270 * @return The direction of the vector in degrees
271 */
272double Joystick::GetDirectionDegrees() const {
273 return (180 / std::acos(-1)) * GetDirectionRadians();
274}