VERSION 5.00 Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX" Begin VB.Form frmMain Caption = "Pololu 8-Servo Controller" ClientHeight = 3825 ClientLeft = 60 ClientTop = 345 ClientWidth = 5970 LinkTopic = "Form1" ScaleHeight = 3825 ScaleWidth = 5970 StartUpPosition = 3 'Windows Default Begin VB.CommandButton cmdZero Caption = "Zero" Height = 495 Left = 5040 TabIndex = 28 Top = 1560 Width = 615 End Begin MSCommLib.MSComm MSComm1 Left = 2280 Top = -240 _ExtentX = 1005 _ExtentY = 1005 _Version = 393216 DTREnable = -1 'True End Begin VB.CommandButton cmdOnOff Caption = "On" Height = 255 Left = 1440 TabIndex = 27 Top = 0 Width = 735 End Begin VB.TextBox txtPort Height = 285 Left = 600 TabIndex = 17 Text = "1" Top = 0 Width = 735 End Begin VB.VScrollBar scrChannel Height = 2175 Index = 7 LargeChange = 25 Left = 4440 Max = 5500 Min = 500 TabIndex = 14 Top = 720 Value = 3000 Width = 495 End Begin VB.VScrollBar scrChannel Height = 2175 Index = 6 LargeChange = 25 Left = 3840 Max = 5500 Min = 500 TabIndex = 12 Top = 720 Value = 3000 Width = 495 End Begin VB.VScrollBar scrChannel Height = 2175 Index = 5 LargeChange = 25 Left = 3240 Max = 5500 Min = 500 TabIndex = 10 Top = 720 Value = 3000 Width = 495 End Begin VB.VScrollBar scrChannel Height = 2175 Index = 4 LargeChange = 25 Left = 2640 Max = 5500 Min = 500 TabIndex = 8 Top = 720 Value = 3000 Width = 495 End Begin VB.VScrollBar scrChannel Height = 2175 Index = 3 LargeChange = 25 Left = 1920 Max = 5500 Min = 500 TabIndex = 6 Top = 720 Value = 3000 Width = 495 End Begin VB.VScrollBar scrChannel Height = 2175 Index = 2 LargeChange = 25 Left = 1320 Max = 5500 Min = 500 TabIndex = 4 Top = 720 Value = 3000 Width = 495 End Begin VB.VScrollBar scrChannel Height = 2175 Index = 1 LargeChange = 25 Left = 720 Max = 5500 Min = 500 TabIndex = 2 Top = 720 Value = 3000 Width = 495 End Begin VB.VScrollBar scrChannel Height = 2175 Index = 0 LargeChange = 25 Left = 120 Max = 5500 Min = 500 TabIndex = 0 Top = 720 Value = 3000 Width = 495 End Begin VB.Label lblPct Alignment = 2 'Center Caption = "-" Height = 255 Index = 0 Left = 120 TabIndex = 36 Top = 3240 Width = 495 End Begin VB.Label lblPct Alignment = 2 'Center Caption = "-" Height = 255 Index = 1 Left = 720 TabIndex = 35 Top = 3240 Width = 495 End Begin VB.Label lblPct Alignment = 2 'Center Caption = "-" Height = 255 Index = 2 Left = 1320 TabIndex = 34 Top = 3240 Width = 495 End Begin VB.Label lblPct Alignment = 2 'Center Caption = "-" Height = 255 Index = 3 Left = 1920 TabIndex = 33 Top = 3240 Width = 495 End Begin VB.Label lblPct Alignment = 2 'Center Caption = "-" Height = 255 Index = 4 Left = 2640 TabIndex = 32 Top = 3240 Width = 495 End Begin VB.Label lblPct Alignment = 2 'Center Caption = "-" Height = 255 Index = 5 Left = 3240 TabIndex = 31 Top = 3240 Width = 495 End Begin VB.Label lblPct Alignment = 2 'Center Caption = "-" Height = 255 Index = 6 Left = 3840 TabIndex = 30 Top = 3240 Width = 495 End Begin VB.Label lblPct Alignment = 2 'Center Caption = "-" Height = 255 Index = 7 Left = 4440 TabIndex = 29 Top = 3240 Width = 495 End Begin VB.Label lblValue Alignment = 2 'Center Caption = "-" Height = 255 Index = 7 Left = 4440 TabIndex = 26 Top = 3000 Width = 495 End Begin VB.Label lblValue Alignment = 2 'Center Caption = "-" Height = 255 Index = 6 Left = 3840 TabIndex = 25 Top = 3000 Width = 495 End Begin VB.Label lblValue Alignment = 2 'Center Caption = "-" Height = 255 Index = 5 Left = 3240 TabIndex = 24 Top = 3000 Width = 495 End Begin VB.Label lblValue Alignment = 2 'Center Caption = "-" Height = 255 Index = 4 Left = 2640 TabIndex = 23 Top = 3000 Width = 495 End Begin VB.Label lblValue Alignment = 2 'Center Caption = "-" Height = 255 Index = 3 Left = 1920 TabIndex = 22 Top = 3000 Width = 495 End Begin VB.Label lblValue Alignment = 2 'Center Caption = "-" Height = 255 Index = 2 Left = 1320 TabIndex = 21 Top = 3000 Width = 495 End Begin VB.Label lblValue Alignment = 2 'Center Caption = "-" Height = 255 Index = 1 Left = 720 TabIndex = 20 Top = 3000 Width = 495 End Begin VB.Label lblValue Alignment = 2 'Center Caption = "-" Height = 255 Index = 0 Left = 120 TabIndex = 19 Top = 3000 Width = 495 End Begin VB.Label Label2 Alignment = 2 'Center Caption = "make sure J1 is open (no shorting block)" Height = 255 Left = 120 TabIndex = 18 Top = 3480 Width = 2895 End Begin VB.Label Label1 Caption = "Port" Height = 255 Left = 120 TabIndex = 16 Top = 120 Width = 495 End Begin VB.Label lblChannel Alignment = 2 'Center Caption = "7" Height = 255 Index = 7 Left = 2760 TabIndex = 15 Top = 480 Width = 255 End Begin VB.Label lblChannel Alignment = 2 'Center Caption = "6" Height = 255 Index = 6 Left = 2400 TabIndex = 13 Top = 480 Width = 255 End Begin VB.Label lblChannel Alignment = 2 'Center Caption = "5" Height = 255 Index = 5 Left = 2040 TabIndex = 11 Top = 480 Width = 255 End Begin VB.Label lblChannel Alignment = 2 'Center Caption = "4" Height = 255 Index = 4 Left = 1680 TabIndex = 9 Top = 480 Width = 255 End Begin VB.Label lblChannel Alignment = 2 'Center Caption = "3" Height = 255 Index = 3 Left = 1200 TabIndex = 7 Top = 480 Width = 255 End Begin VB.Label lblChannel Alignment = 2 'Center Caption = "2" Height = 255 Index = 2 Left = 840 TabIndex = 5 Top = 480 Width = 255 End Begin VB.Label lblChannel Alignment = 2 'Center Caption = "1" Height = 255 Index = 1 Left = 480 TabIndex = 3 Top = 480 Width = 255 End Begin VB.Label lblChannel Alignment = 2 'Center Caption = "0" Height = 255 Index = 0 Left = 120 TabIndex = 1 Top = 480 Width = 255 End End Attribute VB_Name = "frmMain" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Private Sub Form_Load() txtPort.Text = MSComm1.CommPort End Sub Private Sub cmdOnOff_Click() If cmdOnOff.Caption = "Off" Then If MSComm1.PortOpen = True Then MSComm1.PortOpen = False End If MSComm1.CommPort = txtPort.Text cmdOnOff.Caption = "On" Else If MSComm1.PortOpen = True Then MSComm1.PortOpen = False End If MSComm1.PortOpen = True cmdOnOff.Caption = "Off" End If End Sub Private Sub scrChannel_Change(Index As Integer) ' convert it from it's true value into a +- 100.00 value Dim val As Double val = (scrChannel(Index).value - scrChannel(Index).Min) / (scrChannel(Index).Max - scrChannel(Index).Min) val = val * 2 - 1 lblValue(Index).Caption = scrChannel(Index).value lblPct(Index).Caption = FormatNumber(val * 100, 3) SendPololu Index, scrChannel(Index).value End Sub Private Function SendPololu(num As Integer, value As Integer) ' num is 0-7 ' value is 500-5500 ' dec2hex(Asc(Mid(cmd, 1, 1))) & "." & dec2hex(Asc(Mid(cmd, 2, 1))) & "." & dec2hex(Asc(Mid(cmd, 3, 1))) & "." & dec2hex(Asc(Mid(cmd, 4, 1))) & "." & dec2hex(Asc(Mid(cmd, 5, 1))) & "." & dec2hex(Asc(Mid(cmd, 6, 1))) If MSComm1.PortOpen = True Then cmd = Chr(&H80) ' start byte - 0x80 ' always cmd = cmd & Chr(&H1) ' Device ID - 0x01 ' always cmd = cmd & Chr(&H4) ' command - 0x04 ' we want command 4: Set Position, Absolute (2 data bytes) cmd = cmd & Chr(num) ' servo num - 0x.. ' 00-07 ' data1 - 0x.. ' upper 7 bits - range is 500 throguh 5500 ' data2 - 0x.. ' lower 7 bits strbinary = "0000000000000000" & dec2bin(value) lsb = "0" & Mid(strbinary, Len(strbinary) - 6) msb = "0" & Mid(strbinary, Len(strbinary) - 12, 6) cmd = cmd & Chr(bin2dec(msb)) cmd = cmd & Chr(bin2dec(lsb)) MSComm1.Output = cmd & vbNewLine Sleep 60 DoEvents End If End Function Private Sub cmdZero_Click() Dim i As Integer For i = scrChannel.LBound To scrChannel.ubound scrChannel(i).value = scrChannel(i).Min + (scrChannel(i).Max - scrChannel(i).Min) / 2 SendPololu i, scrChannel(i).value Next End Sub Function hex2dec(sHexStr) As Integer hex2dec = CLng("&H" & Trim(sHexStr)) End Function Function dec2hex(val As Integer) As String dec2hex = Hex(val) End Function Function pad(val As String, Optional length As Integer = 1, Optional char As String = "0") As String ' pads a string with chars until len = length Do While Len(val) < length val = char & val Loop pad = val End Function Public Function dec2bin(mynum As Variant) As String ' from http://cuinl.tripod.com/Tips/decimaltobinary.htm Dim loopcounter As Integer If mynum >= 2 ^ 31 Then dec2bin = "Number too big" Exit Function End If Do If (mynum And 2 ^ loopcounter) = 2 ^ loopcounter Then dec2bin = "1" & dec2bin Else dec2bin = "0" & dec2bin End If loopcounter = loopcounter + 1 Loop Until 2 ^ loopcounter > mynum End Function Private Function bin2dec(ByVal BinValue As String) As Long ' from http://cuinl.tripod.com/Tips/math5.htm Dim lngValue As Long Dim x As Long Dim k As Long k = Len(BinValue) ' will only work with 32 or fewer "bits" For x = k To 1 Step -1 ' work backwards down string If Mid$(BinValue, x, 1) = "1" Then If k - x > 30 Then ' bit 31 is the sign bit lngValue = lngValue Or -2147483648# ' avoid overflow error Else lngValue = lngValue + 2 ^ (k - x) End If End If Next x bin2dec = lngValue End Function