Now you can download a copy of these docs so you can use them offline! Download now
KinectStick.cpp
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2011. 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 "KinectStick.h"
8 
9 #include "DriverStation.h"
10 #include "Joystick.h"
11 #include "NetworkCommunication/FRCComm.h"
12 #include "NetworkCommunication/UsageReporting.h"
13 #include "Utility.h"
14 #include "WPIErrors.h"
15 
16 uint32_t KinectStick::_recentPacketNumber = 0;
17 KinectStick::KinectStickData KinectStick::_sticks;
18 
19 #define kJoystickBundleID kFRC_NetworkCommunication_DynamicType_Kinect_Joystick
20 #define kTriggerMask 1
21 #define kTopMask 2
22 
29 {
30  if (id != 1 && id != 2)
31  {
32  wpi_setWPIErrorWithContext(ParameterOutOfRange, "KinectStick ID must be 1 or 2");
33  return;
34  }
35  m_id = id;
36 
37  nUsageReporting::report(nUsageReporting::kResourceType_KinectStick, id);
38 }
39 
47 float KinectStick::GetX(JoystickHand hand)
48 {
49  return GetRawAxis(Joystick::kDefaultXAxis);
50 }
51 
58 float KinectStick::GetY(JoystickHand hand)
59 {
60  return GetRawAxis(Joystick::kDefaultYAxis);
61 }
62 
71 {
72  return GetRawAxis(Joystick::kDefaultZAxis);
73 }
74 
82 {
83  return GetRawAxis(Joystick::kDefaultTwistAxis);
84 }
85 
93 {
94  return GetRawAxis(Joystick::kDefaultThrottleAxis);
95 }
96 
103 float KinectStick::GetRawAxis(uint32_t axis)
104 {
105  if (StatusIsFatal()) return 0.0;
106 
107  GetData();
108  float value = ConvertRawToFloat(_sticks.formatted.rawSticks[m_id - 1].axis[axis-1]);
109  return value;
110 }
111 
118 bool KinectStick::GetTrigger(JoystickHand hand)
119 {
120  return GetRawButton(kTriggerMask);
121 }
122 
129 bool KinectStick::GetTop(JoystickHand hand)
130 {
131  return GetRawButton(kTopMask);
132 }
133 
140 bool KinectStick::GetBumper(JoystickHand hand)
141 {
142  // TODO: Should this even be in GenericHID? Is 4 an appropriate mask value (button 3)?
143  return GetRawButton(4);
144 }
145 
155 bool KinectStick::GetRawButton(uint32_t button)
156 {
157  if (StatusIsFatal()) return false;
158 
159  GetData();
160  return (_sticks.formatted.rawSticks[m_id - 1].buttons & (1 << button)) != 0;
161 }
162 
166 void KinectStick::GetData()
167 {
168  uint32_t packetNumber = DriverStation::GetInstance()->GetPacketNumber();
169  if (_recentPacketNumber != packetNumber)
170  {
171  _recentPacketNumber = packetNumber;
172  int retVal = getDynamicControlData(kJoystickBundleID, _sticks.data, sizeof(_sticks.data), 5);
173  if (retVal == 0)
174  {
175  wpi_assert(_sticks.formatted.size == sizeof(_sticks.data) - 1);
176  }
177  }
178 }
179 
184 float KinectStick::ConvertRawToFloat(int8_t value)
185 {
186  float result;
187 
188  if (value < 0)
189  result = ((float) value) / 128.0;
190  else
191  result = ((float) value) / 127.0;
192 
193  wpi_assert(result <= 1.0 && result >= -1.0);
194 
195  if (result > 1.0)
196  result = 1.0;
197  else if (result < -1.0)
198  result = -1.0;
199 
200  return result;
201 }
virtual float GetY(JoystickHand hand=kRightHand)
Definition: KinectStick.cpp:58
virtual float GetZ()
Definition: KinectStick.cpp:70
virtual float GetRawAxis(uint32_t axis)
virtual bool GetBumper(JoystickHand hand=kRightHand)
virtual float GetTwist()
Definition: KinectStick.cpp:81
uint32_t GetPacketNumber()
virtual bool GetTop(JoystickHand hand=kRightHand)
virtual bool StatusIsFatal() const
Check if the current error code represents a fatal error.
Definition: ErrorBase.cpp:178
static DriverStation * GetInstance()
virtual bool GetRawButton(uint32_t button)
KinectStick(int id)
Definition: KinectStick.cpp:28
virtual bool GetTrigger(JoystickHand hand=kRightHand)
virtual float GetX(JoystickHand hand=kRightHand)
Definition: KinectStick.cpp:47
virtual float GetThrottle()
Definition: KinectStick.cpp:92

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