Interfacing USB Game Controller with VB6
Hi guys,
I posted my robot in the completed projects forum: http://forums.parallax.com/showthread.php?p=676444
A friend of mine showed me his USB game controller for the PC like in the attached image and suggested that it would be awesome to have that control the rover and the robotic arm.
Since my remote control software is written in Visual Basic, I can't figure out how to interface the USB game controller with VB6. Any help in this regard would be highly appreciated. I am documenting my robot right now so that others can benefit from this. You can read it all here:
www.aaqilkhan.blogspot.com
Game Controller in VB6 ?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
E=mc^2
Post Edited (Aaqil Khan) : 9/19/2007 11:12:24 PM GMT
I posted my robot in the completed projects forum: http://forums.parallax.com/showthread.php?p=676444
A friend of mine showed me his USB game controller for the PC like in the attached image and suggested that it would be awesome to have that control the rover and the robotic arm.
Since my remote control software is written in Visual Basic, I can't figure out how to interface the USB game controller with VB6. Any help in this regard would be highly appreciated. I am documenting my robot right now so that others can benefit from this. You can read it all here:
www.aaqilkhan.blogspot.com
Game Controller in VB6 ?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
E=mc^2
Post Edited (Aaqil Khan) : 9/19/2007 11:12:24 PM GMT
Comments
does your usb game controller show up as a game controller in windows?
if so, post back, i can help you [noparse]:)[/noparse]
Yes it does. I can see the USB game controller in Windows' Device Manager list.
I will wait for your response. Thanks.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
E=mc^2
Here's the code I use in vb.net 2008 Express Edition(I think you have vb 6 but I don't have that one on this computer)
I use Microsoft Direct X, Looking at the code now i realize you probably can't use this in vb6, If i get time tonight, i'll try to port it
This code has been customized for the use of a microsoft xbox wireless controller(Hence the compensation for the dead zone)
This is before the namespace
Imports Microsoft.DirectX.DirectInput Imports Microsoft.DirectX
Now we are in the namespace
In the "Public Class MainClass":
Public Shared state As New JoystickState() Private applicationDevice As Device = Nothing Public Shared numPOVs As Integer = -1 Private SliderCount As Integer = -1 ' Number of returned slider controls
This next code is in the namsepace but not in the mainclass sub
Public Function InitDirectInput() As Boolean ' Enumerate joysticks in the system. Dim instance As DeviceInstance For Each instance In Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly) ' Create the device. applicationDevice = New Device(instance.InstanceGuid) 'Just pick the first one Exit For Next instance If (Nothing Is applicationDevice) Then MessageBox.Show("Unable to create a joystick device. Sample will exit.", "No joystick found") Return False End If ' Set the data format to the c_dfDIJoystick pre-defined format. applicationDevice.SetDataFormat(DeviceDataFormat.Joystick) ' Set the cooperative level for the device. applicationDevice.SetCooperativeLevel(Me, CooperativeLevelFlags.Exclusive Or CooperativeLevelFlags.Foreground) ' Enumerate all the objects on the device. Dim d As DeviceObjectInstance For Each d In applicationDevice.Objects ' For axes that are returned, set the DIPROP_RANGE property for the ' enumerated axis in order to scale min/max values. If 0 <> (d.ObjectId And CInt(DeviceObjectTypeFlags.Axis)) Then ' Set the range for the axis. applicationDevice.Properties.SetRange(ParameterHow.ById, d.ObjectId, New InputRange(-1000, +1000)) End If ' Update the controls to reflect what ' objects the device supports. UpdateControls(d) Next d Return True End Function 'InitDirectInput Protected Overloads Sub Dispose(ByVal disposing As Boolean) ' Unacquire all DirectInput objects. If Not Nothing Is applicationDevice Then applicationDevice.Unacquire() End If If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Dispose Public Sub GetData() ' Make sure there is a valid device. If Nothing Is applicationDevice Then Return End If Try ' Poll the device for info. applicationDevice.Poll() Catch inputex As InputException If TypeOf inputex Is NotAcquiredException Or TypeOf inputex Is InputLostException Then ' Check to see if either the app ' needs to acquire the device, or ' if the app lost the device to another ' process. Try ' Acquire the device. applicationDevice.Acquire() Catch ' Failed to acquire the device. ' This could be because the app ' doesn't have focus. Return End Try End If End Try 'catch(InputException inputex) ' Get the state of the device. Try state = applicationDevice.CurrentJoystickState ' Catch any exceptions. None will be handled here, ' any device re-aquisition will be handled above. Catch Return End Try UpdateUI() End Sub 'GetData Private Sub UpdateUI() Dim precision As Integer ' This function updated the UI with ' joystick state information. Dim strText As String = Nothing labelXAxis.Text = state.X.ToString() labelYAxis.Text = state.Y.ToString() labelZAxis.Text = state.Z.ToString() If (state.Z > 0) Then precision = 7 ElseIf (state.Z < 0) Then precision = 2 Else precision = 15 End If If (state.X > 60) Then radBtnLeftJoy.Left = state.X / 940 * 20 + 40 If (boolMenuActive = False) Then SetCursorPos(Cursor.Position.X + state.X / 940 * precision, Cursor.Position.Y) End If ElseIf (state.X < -160) Then radBtnLeftJoy.Left = state.X / 860 * 20 + 40 '-70, If (boolMenuActive = False) Then SetCursorPos(Cursor.Position.X + state.X / 860 * precision, Cursor.Position.Y) End If Else radBtnLeftJoy.Left = 40 '-70, 60 End If If (state.Y > 60) Then radBtnLeftJoy.Top = state.Y / 940 * 20 + 40 If (boolMenuActive = False) Then SetCursorPos(Cursor.Position.X, Cursor.Position.Y + state.Y / 940 * precision) End If ElseIf (state.Y < -70) Then radBtnLeftJoy.Top = state.Y / 860 * 20 + 40 '-70, 60 If (boolMenuActive = False) Then SetCursorPos(Cursor.Position.X, Cursor.Position.Y + state.Y / 860 * precision) End If Else radBtnLeftJoy.Top = 40 '-70, 60 End If If (state.Rx > 120) Then radBtnRightJoy.Left = state.Rx / 880 * 20 + 40 'SetCursorPos(Cursor.Position.X + state.X / 940 * precision, Cursor.Position.Y) ElseIf (state.Rx < -120) Then radBtnRightJoy.Left = state.Rx / 880 * 20 + 40 '-70, 'SetCursorPos(Cursor.Position.X + state.X / 860 * precision, Cursor.Position.Y) Else radBtnRightJoy.Left = 40 '-70, 60 End If If (state.Ry > 120) Then radBtnRightJoy.Top = state.Ry / 880 * 20 + 40 'SetCursorPos(Cursor.Position.X, Cursor.Position.Y + state.Y / 940 * precision) ElseIf (state.Ry < -30) Then radBtnRightJoy.Top = state.Ry / 970 * 20 + 40 '-70, 60 'SetCursorPos(Cursor.Position.X, Cursor.Position.Y + state.Y / 860 * precision) Else radBtnRightJoy.Top = 40 '-70, 60 End If labelXRotation.Text = state.Rx.ToString() labelYRotation.Text = state.Ry.ToString() labelZRotation.Text = state.Rz.ToString() Dim slider As Integer() = state.GetSlider() labelSlider0.Text = slider(0).ToString() labelSlider1.Text = slider(1).ToString() Dim pov As Integer() = state.GetPointOfView() btnPovTopLeft.BackColor = Color.White btnPovTopCenter.BackColor = Color.White btnPovTopRight.BackColor = Color.White btnPovMiddleLeft.BackColor = Color.White btnPovMiddleCenter.BackColor = Color.White btnPovMiddleRight.BackColor = Color.White btnPovBottomLeft.BackColor = Color.White btnPovBottomCenter.BackColor = Color.White btnPovBottomRight.BackColor = Color.White If (pov(0) = -1) Then lpower = 770 rpower = 775 btnPovMiddleCenter.BackColor = Color.Red ElseIf (pov(0) = 0) Then 'If (moRS232.IsOpen) Then lpower = 1000 rpower = 700 btnPovTopCenter.BackColor = Color.Red 'End If ElseIf (pov(0) = 4500) Then btnPovTopRight.BackColor = Color.Red ElseIf (pov(0) = 9000) Then btnPovMiddleRight.BackColor = Color.Red lpower = 1000 rpower = 1000 ElseIf (pov(0) = 13500) Then btnPovBottomRight.BackColor = Color.Red ElseIf (pov(0) = 18000) Then lpower = 700 rpower = 1000 btnPovBottomCenter.BackColor = Color.Red ElseIf (pov(0) = 22500) Then btnPovBottomLeft.BackColor = Color.Red ElseIf (pov(0) = 27000) Then btnPovMiddleLeft.BackColor = Color.Red lpower = 700 rpower = 700 ElseIf (pov(0) = 31500) Then btnPovTopLeft.BackColor = Color.Red End If 'btnPovMiddleCenter.BackColor = Color.Red labelPOV0.Text = pov(0).ToString() labelPOV1.Text = pov(1).ToString() labelPOV2.Text = pov(2).ToString() labelPOV3.Text = pov(3).ToString() ' Fill up text with which buttons are pressed Dim buttons As Byte() = state.GetButtons() Dim button As Integer = 0 Dim b As Byte Dim boolMenuButtonWasPressed As Boolean = False For Each b In buttons If 0 <> (b And &H80) Then strText += button.ToString("00 ") If button = 0 Then ClickMouse(Cursor.Position.X, Cursor.Position.Y) If button = 7 Then boolMenuButtonWasPressed = True End If End If button += 1 Next b If (boolMenuButtonWasPressed) Then 'Menu Button was pressed If (boolPrevMenuButton) Then 'Last time the menu button was pressed 'Do nothing Else boolPrevMenuButton = True If (boolMenuActive = False) Then boolMenuActive = True intMenuTab = TabControl1.SelectedIndex TabControl1.SelectTab(2) 'MsgBox(boolMenuButtonIsUp.ToString + "|" + boolMenuActive.ToString) Else boolMenuActive = False TabControl1.SelectTab(intMenuTab) 'MsgBox(boolMenuButtonIsUp.ToString + "|" + boolMenuActive.ToString) End If End If Else boolPrevMenuButton = False End If labelButtons.Text = strText End Sub 'UpdateUI Public Sub UpdateControls(ByVal d As DeviceObjectInstance) ' Set the UI to reflect what objects the joystick supports. If ObjectTypeGuid.XAxis.Equals(d.ObjectType) Then labelXAxis.Enabled = True labelXAxisText.Enabled = True End If If ObjectTypeGuid.YAxis.Equals(d.ObjectType) Then labelYAxis.Enabled = True labelYAxisText.Enabled = True End If If ObjectTypeGuid.ZAxis.Equals(d.ObjectType) Then labelZAxis.Enabled = True labelZAxisText.Enabled = True End If If ObjectTypeGuid.RxAxis.Equals(d.ObjectType) Then labelXRotation.Enabled = True labelXRotationText.Enabled = True End If If ObjectTypeGuid.RyAxis.Equals(d.ObjectType) Then labelYRotation.Enabled = True labelYRotationText.Enabled = True End If If ObjectTypeGuid.RzAxis.Equals(d.ObjectType) Then labelZRotation.Enabled = True labelZRotationText.Enabled = True End If If ObjectTypeGuid.Slider.Equals(d.ObjectType) Then SliderCount += 1 Select Case SliderCount Case 0 labelSlider0.Enabled = True labelSlider0Text.Enabled = True Case 1 labelSlider1.Enabled = True labelSlider1Text.Enabled = True End Select End If If ObjectTypeGuid.PointOfView.Equals(d.ObjectType) Then numPOVs += 1 Select Case numPOVs Case 0 labelPOV0.Enabled = True labelPOV0Text.Enabled = True Case 1 labelPOV1.Enabled = True labelPOV1Text.Enabled = True Case 2 labelPOV2.Enabled = True labelPOV2Text.Enabled = True Case 3 labelPOV3.Enabled = True labelPOV3Text.Enabled = True End Select End If End Sub 'UpdateControls Private Sub timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timer1.Tick GetData() End Sub
As I said up top, If i get time i'll try to port this to visual basic 6 tonight.
I hope I can help, i've looked at your web link and I was blown away at how profeshional your vb program looks! I've been having a lot of trouble sending anything but simplistic data packets to the stamp, I'd love to see how you do it.
-Regards
Post Edited (DiablodeMorte) : 9/20/2007 12:56:59 PM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
E=mc^2
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support