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