Now you can download a copy of these docs so you can use them offline! Download now
Joystick.cpp
1 /*----------------------------------------------------------------------------*/
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 
13 const uint32_t Joystick::kDefaultXAxis;
14 const uint32_t Joystick::kDefaultYAxis;
15 const uint32_t Joystick::kDefaultZAxis;
16 const uint32_t Joystick::kDefaultTwistAxis;
17 const uint32_t Joystick::kDefaultThrottleAxis;
18 const uint32_t Joystick::kDefaultTriggerButton;
19 const uint32_t Joystick::kDefaultTopButton;
20 static Joystick *joysticks[DriverStation::kJoystickPorts];
21 static bool joySticksInitialized = false;
22 
29 Joystick::Joystick(uint32_t 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 
59 Joystick::Joystick(uint32_t port, uint32_t numAxisTypes, uint32_t numButtonTypes)
60  : m_ds (NULL)
61  , m_port (port)
62  , m_axes (NULL)
63  , m_buttons (NULL)
64 {
65  InitJoystick(numAxisTypes, numButtonTypes);
66 }
67 
68 void Joystick::InitJoystick(uint32_t numAxisTypes, uint32_t 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 
79  m_axes = new uint32_t[numAxisTypes];
80  m_buttons = new uint32_t[numButtonTypes];
81 }
82 
83 Joystick * Joystick::GetStickForPort(uint32_t 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 
94 Joystick::~Joystick()
95 {
96  delete [] m_buttons;
97  delete [] m_axes;
98 }
99 
104 float Joystick::GetX(JoystickHand hand)
105 {
106  return GetRawAxis(m_axes[kXAxis]);
107 }
108 
113 float Joystick::GetY(JoystickHand hand)
114 {
115  return GetRawAxis(m_axes[kYAxis]);
116 }
117 
123 {
124  return GetRawAxis(m_axes[kZAxis]);
125 }
126 
132 {
133  return GetRawAxis(m_axes[kTwistAxis]);
134 }
135 
141 {
142  return GetRawAxis(m_axes[kThrottleAxis]);
143 }
144 
151 float Joystick::GetRawAxis(uint32_t axis)
152 {
153  return m_ds->GetStickAxis(m_port, axis);
154 }
155 
165 float 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 
188 bool Joystick::GetTrigger(JoystickHand hand)
189 {
190  return GetRawButton(m_buttons[kTriggerButton]);
191 }
192 
201 bool Joystick::GetTop(JoystickHand hand)
202 {
203  return GetRawButton(m_buttons[kTopButton]);
204 }
205 
210 bool Joystick::GetBumper(JoystickHand hand)
211 {
212  // Joysticks don't have bumpers.
213  return false;
214 }
215 
225 bool Joystick::GetRawButton(uint32_t button)
226 {
227  return ((0x1 << (button-1)) & m_ds->GetStickButtons(m_port)) != 0;
228 }
229 
238 bool 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 
255 uint32_t Joystick::GetAxisChannel(AxisType axis)
256 {
257  return m_axes[axis];
258 }
259 
266 void Joystick::SetAxisChannel(AxisType axis, uint32_t channel)
267 {
268  m_axes[axis] = channel;
269 }
270 
278  return sqrt(pow(GetX(),2) + pow(GetY(),2) );
279 }
280 
288  return atan2(GetX(), -GetY());
289 }
290 
301  return (180/acos(-1))*GetDirectionRadians();
302 }
virtual float GetAxis(AxisType axis)
Definition: Joystick.cpp:165
Joystick(uint32_t port)
Definition: Joystick.cpp:29
virtual float GetX(JoystickHand hand=kRightHand)
Definition: Joystick.cpp:104
virtual bool GetTrigger(JoystickHand hand=kRightHand)
Definition: Joystick.cpp:188
virtual bool GetTop(JoystickHand hand=kRightHand)
Definition: Joystick.cpp:201
virtual float GetZ()
Definition: Joystick.cpp:122
void SetAxisChannel(AxisType axis, uint32_t channel)
Definition: Joystick.cpp:266
virtual bool GetButton(ButtonType button)
Definition: Joystick.cpp:238
uint32_t GetAxisChannel(AxisType axis)
Definition: Joystick.cpp:255
bool GetRawButton(uint32_t button)
Definition: Joystick.cpp:225
virtual float GetTwist()
Definition: Joystick.cpp:131
virtual float GetDirectionDegrees()
Definition: Joystick.cpp:300
virtual float GetY(JoystickHand hand=kRightHand)
Definition: Joystick.cpp:113
virtual float GetMagnitude()
Definition: Joystick.cpp:277
static DriverStation * GetInstance()
virtual float GetThrottle()
Definition: Joystick.cpp:140
virtual float GetDirectionRadians()
Definition: Joystick.cpp:287
short GetStickButtons(uint32_t stick)
float GetStickAxis(uint32_t stick, uint32_t axis)
virtual bool GetBumper(JoystickHand hand=kRightHand)
Definition: Joystick.cpp:210
float GetRawAxis(uint32_t axis)
Definition: Joystick.cpp:151

Generated on Sat Apr 26 2014 12:26:45 for WPILibC++ by doxygen 1.8.6