Now you can download a copy of these docs so you can use them offline! Download now
Joystick.cpp
00001 /*----------------------------------------------------------------------------*/ 00002 /* Copyright (c) FIRST 2008. 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 "Joystick.h" 00008 #include "DriverStation.h" 00009 #include "WPIErrors.h" 00010 #include <math.h> 00011 00012 const UINT32 Joystick::kDefaultXAxis; 00013 const UINT32 Joystick::kDefaultYAxis; 00014 const UINT32 Joystick::kDefaultZAxis; 00015 const UINT32 Joystick::kDefaultTwistAxis; 00016 const UINT32 Joystick::kDefaultThrottleAxis; 00017 const UINT32 Joystick::kDefaultTriggerButton; 00018 const UINT32 Joystick::kDefaultTopButton; 00019 static Joystick *joysticks[DriverStation::kJoystickPorts]; 00020 static bool joySticksInitialized = false; 00021 00028 Joystick::Joystick(UINT32 port) 00029 : m_ds (NULL) 00030 , m_port (port) 00031 , m_axes (NULL) 00032 , m_buttons (NULL) 00033 { 00034 InitJoystick(kNumAxisTypes, kNumButtonTypes); 00035 00036 m_axes[kXAxis] = kDefaultXAxis; 00037 m_axes[kYAxis] = kDefaultYAxis; 00038 m_axes[kZAxis] = kDefaultZAxis; 00039 m_axes[kTwistAxis] = kDefaultTwistAxis; 00040 m_axes[kThrottleAxis] = kDefaultThrottleAxis; 00041 00042 m_buttons[kTriggerButton] = kDefaultTriggerButton; 00043 m_buttons[kTopButton] = kDefaultTopButton; 00044 } 00045 00056 Joystick::Joystick(UINT32 port, UINT32 numAxisTypes, UINT32 numButtonTypes) 00057 : m_ds (NULL) 00058 , m_port (port) 00059 , m_axes (NULL) 00060 , m_buttons (NULL) 00061 { 00062 InitJoystick(numAxisTypes, numButtonTypes); 00063 } 00064 00065 void Joystick::InitJoystick(UINT32 numAxisTypes, UINT32 numButtonTypes) 00066 { 00067 if ( !joySticksInitialized ) 00068 { 00069 for (unsigned i = 0; i < DriverStation::kJoystickPorts; i++) 00070 joysticks[i] = NULL; 00071 joySticksInitialized = true; 00072 } 00073 joysticks[m_port - 1] = this; 00074 00075 m_ds = DriverStation::GetInstance(); 00076 m_axes = new UINT32[numAxisTypes]; 00077 m_buttons = new UINT32[numButtonTypes]; 00078 } 00079 00080 Joystick * Joystick::GetStickForPort(UINT32 port) 00081 { 00082 Joystick *stick = joysticks[port - 1]; 00083 if (stick == NULL) 00084 { 00085 stick = new Joystick(port); 00086 joysticks[port - 1] = stick; 00087 } 00088 return stick; 00089 } 00090 00091 Joystick::~Joystick() 00092 { 00093 delete [] m_buttons; 00094 delete [] m_axes; 00095 } 00096 00101 float Joystick::GetX(JoystickHand hand) 00102 { 00103 return GetRawAxis(m_axes[kXAxis]); 00104 } 00105 00110 float Joystick::GetY(JoystickHand hand) 00111 { 00112 return GetRawAxis(m_axes[kYAxis]); 00113 } 00114 00119 float Joystick::GetZ() 00120 { 00121 return GetRawAxis(m_axes[kZAxis]); 00122 } 00123 00128 float Joystick::GetTwist() 00129 { 00130 return GetRawAxis(m_axes[kTwistAxis]); 00131 } 00132 00137 float Joystick::GetThrottle() 00138 { 00139 return GetRawAxis(m_axes[kThrottleAxis]); 00140 } 00141 00148 float Joystick::GetRawAxis(UINT32 axis) 00149 { 00150 return m_ds->GetStickAxis(m_port, axis); 00151 } 00152 00162 float Joystick::GetAxis(AxisType axis) 00163 { 00164 switch(axis) 00165 { 00166 case kXAxis: return this->GetX(); 00167 case kYAxis: return this->GetY(); 00168 case kZAxis: return this->GetZ(); 00169 case kTwistAxis: return this->GetTwist(); 00170 case kThrottleAxis: return this->GetThrottle(); 00171 default: 00172 wpi_setWPIError(BadJoystickAxis); 00173 return 0.0; 00174 } 00175 } 00176 00185 bool Joystick::GetTrigger(JoystickHand hand) 00186 { 00187 return GetRawButton(m_buttons[kTriggerButton]); 00188 } 00189 00198 bool Joystick::GetTop(JoystickHand hand) 00199 { 00200 return GetRawButton(m_buttons[kTopButton]); 00201 } 00202 00207 bool Joystick::GetBumper(JoystickHand hand) 00208 { 00209 // Joysticks don't have bumpers. 00210 return false; 00211 } 00212 00222 bool Joystick::GetRawButton(UINT32 button) 00223 { 00224 return ((0x1 << (button-1)) & m_ds->GetStickButtons(m_port)) != 0; 00225 } 00226 00235 bool Joystick::GetButton(ButtonType button) 00236 { 00237 switch (button) 00238 { 00239 case kTriggerButton: return GetTrigger(); 00240 case kTopButton: return GetTop(); 00241 default: 00242 return false; 00243 } 00244 } 00245 00252 UINT32 Joystick::GetAxisChannel(AxisType axis) 00253 { 00254 return m_axes[axis]; 00255 } 00256 00263 void Joystick::SetAxisChannel(AxisType axis, UINT32 channel) 00264 { 00265 m_axes[axis] = channel; 00266 } 00267 00274 float Joystick::GetMagnitude(){ 00275 return sqrt(pow(GetX(),2) + pow(GetY(),2) ); 00276 } 00277 00284 float Joystick::GetDirectionRadians(){ 00285 return atan2(GetX(), -GetY()); 00286 } 00287 00297 float Joystick::GetDirectionDegrees(){ 00298 return (180/acos(-1))*GetDirectionRadians(); 00299 }
Generated on Thu Jan 12 2012 22:35:20 for WPILibC++ by
1.7.1