Now you can download a copy of these docs so you can use them offline! Download now
KinectStick.cpp
00001 /*----------------------------------------------------------------------------*/ 00002 /* Copyright (c) FIRST 2011. All Rights Reserved. */ 00003 /* Open Source Software - may be modified and shared by FRC teams. The code */ 00004 /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ 00005 /*----------------------------------------------------------------------------*/ 00006 00007 #include "KinectStick.h" 00008 00009 #include "DriverStation.h" 00010 #include "Joystick.h" 00011 #include "NetworkCommunication/FRCComm.h" 00012 #include "Utility.h" 00013 #include "WPIErrors.h" 00014 00015 UINT32 KinectStick::_recentPacketNumber = 0; 00016 KinectStick::KinectStickData KinectStick::_sticks; 00017 00018 #define kJoystickBundleID kFRC_NetworkCommunication_DynamicType_Kinect_Joystick 00019 #define kTriggerMask 1 00020 #define kTopMask 2 00021 00027 KinectStick::KinectStick(int id) 00028 { 00029 if (id != 1 && id != 2) 00030 { 00031 wpi_setWPIErrorWithContext(ParameterOutOfRange, "KinectStick ID must be 1 or 2"); 00032 return; 00033 } 00034 m_id = id; 00035 } 00036 00041 float KinectStick::GetX(JoystickHand hand) 00042 { 00043 return GetRawAxis(Joystick::kDefaultXAxis); 00044 } 00045 00051 float KinectStick::GetY(JoystickHand hand) 00052 { 00053 return GetRawAxis(Joystick::kDefaultYAxis); 00054 } 00055 00060 float KinectStick::GetZ() 00061 { 00062 return GetRawAxis(Joystick::kDefaultZAxis); 00063 } 00064 00069 float KinectStick::GetTwist() 00070 { 00071 return GetRawAxis(Joystick::kDefaultTwistAxis); 00072 } 00073 00078 float KinectStick::GetThrottle() 00079 { 00080 return GetRawAxis(Joystick::kDefaultThrottleAxis); 00081 } 00082 00087 float KinectStick::GetRawAxis(UINT32 axis) 00088 { 00089 if (StatusIsFatal()) return 0.0; 00090 00091 GetData(); 00092 float value = ConvertRawToFloat(_sticks.formatted.rawSticks[m_id - 1].axis[axis-1]); 00093 return value; 00094 } 00095 00101 bool KinectStick::GetTrigger(JoystickHand hand) 00102 { 00103 return GetRawButton(kTriggerMask); 00104 } 00105 00111 bool KinectStick::GetTop(JoystickHand hand) 00112 { 00113 return GetRawButton(kTopMask); 00114 } 00115 00116 bool KinectStick::GetBumper(JoystickHand hand) 00117 { 00118 // TODO: Should this even be in GenericHID? Is 4 an appropriate mask value (button 3)? 00119 return GetRawButton(4); 00120 } 00121 00122 bool KinectStick::GetRawButton(UINT32 button) 00123 { 00124 if (StatusIsFatal()) return false; 00125 00126 GetData(); 00127 return (_sticks.formatted.rawSticks[m_id - 1].buttons & (1 << button)) != 0; 00128 } 00129 00133 void KinectStick::GetData() 00134 { 00135 UINT32 packetNumber = DriverStation::GetInstance()->GetPacketNumber(); 00136 if (_recentPacketNumber != packetNumber) 00137 { 00138 _recentPacketNumber = packetNumber; 00139 int retVal = getDynamicControlData(kJoystickBundleID, _sticks.data, sizeof(_sticks.data), 5); 00140 if (retVal == 0) 00141 { 00142 wpi_assert(_sticks.formatted.size == sizeof(_sticks.data) - 1); 00143 } 00144 } 00145 } 00146 00151 float KinectStick::ConvertRawToFloat(INT8 value) 00152 { 00153 float result; 00154 00155 if (value < 0) 00156 result = ((float) value) / 128.0; 00157 else 00158 result = ((float) value) / 127.0; 00159 00160 wpi_assert(result <= 1.0 && result >= -1.0); 00161 00162 if (result > 1.0) 00163 result = 1.0; 00164 else if (result < -1.0) 00165 result = -1.0; 00166 00167 return result; 00168 }
Generated on Thu Jan 12 2012 22:35:20 for WPILibC++ by
1.7.1