VERSION 5.00 Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX" Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX" Object = "{5E9E78A0-531B-11CF-91F6-C2863C385E30}#1.0#0"; "MSFLXGRD.OCX" Begin VB.Form frmMain Caption = "Servo Script Interpreter rev1r0" ClientHeight = 8190 ClientLeft = 165 ClientTop = 855 ClientWidth = 11640 LinkTopic = "Form1" ScaleHeight = 8190 ScaleWidth = 11640 StartUpPosition = 3 'Windows Default Begin VB.Frame Frame6 Caption = "Position Adjust" Height = 2535 Left = 1680 TabIndex = 38 Top = 4080 Width = 1815 Begin VB.CommandButton cmdUpdate Caption = "Update" Height = 255 Left = 360 TabIndex = 44 Top = 2160 Width = 1095 End Begin VB.HScrollBar hsAdj Height = 255 LargeChange = 90 Left = 240 Max = 900 Min = -900 SmallChange = 9 TabIndex = 41 Top = 1080 Width = 1335 End Begin VB.Label Label2 Caption = "Position" Height = 255 Left = 600 TabIndex = 43 Top = 1800 Width = 615 End Begin VB.Label lblPos Alignment = 2 'Center BackColor = &H00C0FFC0& Caption = "0" BeginProperty Font Name = "MS Sans Serif" Size = 12 Charset = 0 Weight = 700 Underline = 0 'False Italic = 0 'False Strikethrough = 0 'False EndProperty Height = 375 Left = 360 TabIndex = 42 Top = 1440 Width = 975 End Begin VB.Label Label1 Alignment = 2 'Center Caption = "Motor No:" Height = 255 Left = 360 TabIndex = 40 Top = 720 Width = 975 End Begin VB.Label lblMotor Alignment = 2 'Center BackColor = &H00C0C0FF& Caption = "0" BeginProperty Font Name = "MS Sans Serif" Size = 13.5 Charset = 0 Weight = 400 Underline = 0 'False Italic = 0 'False Strikethrough = 0 'False EndProperty Height = 375 Left = 360 TabIndex = 39 Top = 240 Width = 975 End End Begin VB.TextBox txtIn Height = 285 Left = 4560 MultiLine = -1 'True TabIndex = 36 Top = 7080 Width = 5775 End Begin VB.Frame Frame5 Caption = "Edit" Height = 1815 Left = 10440 TabIndex = 33 Top = 240 Width = 975 Begin VB.CommandButton cmdDel Caption = "Delete" Height = 255 Left = 120 TabIndex = 35 Top = 1080 Width = 735 End Begin VB.CommandButton cmdIns Caption = "Insert" Height = 255 Left = 120 TabIndex = 34 Top = 360 Width = 735 End End Begin MSFlexGridLib.MSFlexGrid Grid Height = 6855 Left = 3840 TabIndex = 32 Top = 120 Width = 6495 _ExtentX = 11456 _ExtentY = 12091 _Version = 393216 FixedCols = 0 AllowUserResizing= 1 End Begin VB.Frame Frame4 Caption = "Stepping" Height = 2535 Left = 240 TabIndex = 28 Top = 4080 Width = 1335 Begin VB.CommandButton cmdWait Caption = "Till Wait" Height = 375 Left = 120 TabIndex = 37 Top = 1800 Width = 1095 End Begin VB.CommandButton cmdGo Caption = "Single Step" Height = 375 Left = 120 TabIndex = 31 Top = 1320 Width = 1095 End Begin VB.CheckBox chkStep Caption = "Step" Height = 195 Left = 240 TabIndex = 30 Top = 480 Width = 855 End Begin VB.Label lblCounter Alignment = 2 'Center BackColor = &H00C0FFFF& Caption = "0" BeginProperty Font Name = "MS Sans Serif" Size = 12 Charset = 0 Weight = 700 Underline = 0 'False Italic = 0 'False Strikethrough = 0 'False EndProperty Height = 375 Left = 240 TabIndex = 29 Top = 840 Width = 855 End End Begin VB.Frame Frame2 Caption = "User Button" Height = 3975 Left = 240 TabIndex = 5 Top = 0 Width = 3255 Begin VB.Frame Frame3 Height = 1935 Left = 240 TabIndex = 22 Top = 360 Width = 2655 Begin VB.CommandButton cmdUser BackColor = &H00C0C0FF& Caption = "0" Height = 375 Index = 0 Left = 840 MaskColor = &H00C0C0FF& TabIndex = 27 Top = 360 Width = 495 End Begin VB.CommandButton cmdUser Caption = "1" Height = 375 Index = 1 Left = 240 TabIndex = 26 Top = 840 Width = 495 End Begin VB.CommandButton cmdUser Caption = "2" Height = 375 Index = 2 Left = 1440 TabIndex = 25 Top = 840 Width = 495 End Begin VB.CommandButton cmdUser Caption = "3" Height = 375 Index = 3 Left = 840 TabIndex = 24 Top = 1320 Width = 495 End Begin VB.CheckBox chkSticky Caption = "Sticky" Height = 255 Left = 1800 TabIndex = 23 Top = 240 Width = 735 End End Begin VB.CommandButton cmdUser Caption = "11" Height = 375 Index = 11 Left = 2400 TabIndex = 13 Top = 3360 Width = 495 End Begin VB.CommandButton cmdUser Caption = "10" Height = 375 Index = 10 Left = 1680 TabIndex = 12 Top = 3360 Width = 495 End Begin VB.CommandButton cmdUser Caption = "9" Height = 375 Index = 9 Left = 960 TabIndex = 11 Top = 3360 Width = 495 End Begin VB.CommandButton cmdUser Caption = "8" Height = 375 Index = 8 Left = 240 TabIndex = 10 Top = 3360 Width = 495 End Begin VB.CommandButton cmdUser Caption = "7" Height = 375 Index = 7 Left = 2400 TabIndex = 9 Top = 2760 Width = 495 End Begin VB.CommandButton cmdUser Caption = "6" Height = 375 Index = 6 Left = 1680 TabIndex = 8 Top = 2760 Width = 495 End Begin VB.CommandButton cmdUser Caption = "5" Height = 375 Index = 5 Left = 960 TabIndex = 7 Top = 2760 Width = 495 End Begin VB.CommandButton cmdUser Caption = "4" Height = 375 Index = 4 Left = 240 TabIndex = 6 Top = 2760 Width = 495 End Begin VB.Label lblInd BackColor = &H00000000& BorderStyle = 1 'Fixed Single Height = 135 Index = 11 Left = 2640 TabIndex = 21 Top = 3240 Width = 255 End Begin VB.Label lblInd BackColor = &H00000000& BorderStyle = 1 'Fixed Single Height = 135 Index = 10 Left = 1920 TabIndex = 20 Top = 3240 Width = 255 End Begin VB.Label lblInd BackColor = &H00000000& BorderStyle = 1 'Fixed Single Height = 135 Index = 9 Left = 1200 TabIndex = 19 Top = 3240 Width = 255 End Begin VB.Label lblInd BackColor = &H00000000& BorderStyle = 1 'Fixed Single Height = 135 Index = 8 Left = 480 TabIndex = 18 Top = 3240 Width = 255 End Begin VB.Label lblInd BackColor = &H00000000& BorderStyle = 1 'Fixed Single Height = 135 Index = 7 Left = 2640 TabIndex = 17 Top = 2640 Width = 255 End Begin VB.Label lblInd BackColor = &H00000000& BorderStyle = 1 'Fixed Single Height = 135 Index = 6 Left = 1920 TabIndex = 16 Top = 2640 Width = 255 End Begin VB.Label lblInd BackColor = &H00000000& BorderStyle = 1 'Fixed Single Height = 135 Index = 5 Left = 1200 TabIndex = 15 Top = 2640 Width = 255 End Begin VB.Label lblInd BackColor = &H00000000& BorderStyle = 1 'Fixed Single Height = 135 Index = 4 Left = 480 TabIndex = 14 Top = 2640 Width = 255 End End Begin VB.CommandButton cmdStop Caption = "Stop" Height = 495 Left = 1200 TabIndex = 4 Top = 6960 Width = 855 End Begin VB.Frame Frame1 Height = 450 Left = 0 TabIndex = 2 Top = 7680 Width = 11595 Begin VB.Label lblSequence Caption = "Label1" Height = 210 Left = 120 TabIndex = 3 Top = 120 Width = 11235 End End Begin MSComDlg.CommonDialog cDialog Left = 0 Top = 360 _ExtentX = 847 _ExtentY = 847 _Version = 393216 End Begin VB.CommandButton cmdStart Caption = "Start" Height = 495 Left = 240 TabIndex = 1 Top = 6960 Width = 735 End Begin VB.Timer tmrSequencer Enabled = 0 'False Interval = 100 Left = 0 Top = 0 End Begin VB.CommandButton cmdServoOFF Caption = "Servo OFF" Height = 495 Left = 2280 TabIndex = 0 Top = 6960 Width = 1215 End Begin MSCommLib.MSComm MSComm1 Left = 0 Top = 2505 _ExtentX = 1032 _ExtentY = 1032 _Version = 393216 CommPort = 6 DTREnable = -1 'True RTSEnable = -1 'True BaudRate = 115200 End Begin VB.Menu mnuFile Caption = "File" Begin VB.Menu mnuNew Caption = "New" End Begin VB.Menu mnuOpen Caption = "Open" End Begin VB.Menu mnuSave Caption = "Save" End Begin VB.Menu mnuBye Caption = "Bye" End End Begin VB.Menu mnuTest Caption = "Test" Begin VB.Menu mnuServo Caption = "Test Servo" End End End Attribute VB_Name = "frmMain" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False Option Explicit Dim Timing, Motor, Pos, Speed As Integer Dim Milestone As Integer Dim ActiveFile As String Dim Tmr As Integer Dim OFile As Integer Dim Rxd, DSR As Boolean Dim TxReady, Tigil, Go As Boolean Dim Dlay As Single Dim Forptr, Stackptr As Integer Dim execs, Sandali As Boolean Dim Load As Boolean Dim Lastrow As Integer ' Fine tune positions Dim fmotor, fpos, fspeed As Integer Private Sub cmdDel_Click() Dim i As Integer On Error GoTo OKlang i = Grid.Row Grid.RemoveItem (i) OKlang: Call FillNumber Grid.Row = i Grid.Col = 1 End Sub Private Sub cmdGo_Click() Sandali = True Go = True End Sub Private Sub cmdIns_Click() Dim i As Integer Grid.CellBackColor = vbWhite i = Grid.Row + 1 Grid.AddItem (""), i Call FillNumber Grid.Row = i Grid.Col = 1 End Sub Private Sub cmdServoOFF_Click() Send (Chr(2) + "18" + "i00") Send (Chr(2) + "27" + "i00") End Sub Private Sub cmdStop_Click() Tigil = True End Sub Private Sub cmdUpdate_Click() Dim iset As String iset = "move(" Grid.Text = iset + Str(fmotor) + "," + Str(fpos) + "," + Str(fspeed) + ")" var2(Grid.Row) = fpos End Sub Private Sub cmdUser_Click(index As Integer) If index > 3 Then If lblInd(index).BackColor = vbBlack Then lblInd(index).BackColor = vbRed Bton(index) = True Else lblInd(index).BackColor = vbBlack Bton(index) = False End If End If End Sub Private Sub cmdUser_MouseDown(index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single) If chkSticky.Value = 1 Then If index <= 3 Then If Bton(index) = True Then Bton(index) = False Else Bton(index) = True End If End If Else If index <= 3 Then Bton(index) = True End If End Sub Private Sub cmdUser_MouseUp(index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single) If chkSticky.Value = 0 Then If index <= 3 Then Bton(index) = False End If End Sub Private Sub CmdStart_Click() Dim Command, Pointer, mask As Integer Dim PCounter As Integer Dim Motor, Param As String Dim Repeat, Tapos As Boolean Dim rung, instruct As String Dim i As Integer 'For i = 0 To 120 ' MotPos(i) = 1000 ' Next i If ActiveFile = "" Then MsgBox "No open file!", vbCritical Exit Sub End If cmdStart.Enabled = False Tapos = False Grid.Col = 1 For PCounter = 1 To Grid.Rows - 1 ' compile source code Grid.Row = PCounter rung = Grid.Text ' remove leading white space opcode(PCounter) = &HFF ' default to NOP While Left(rung, 1) <= " " And rung <> "" rung = Right(rung, Len(rung) - 1) Wend If rung <> "" And Left(rung, 1) <> ";" Then ' if not blank or comment ' find opening parentheses Pointer = 1 While Mid(rung, Pointer, 1) <> "(" And Pointer < Len(rung) Pointer = Pointer + 1 Wend ' extract instruction If Pointer = Len(rung) Then instruct = rung ' instruction w/o parentheses Else instruct = Left(rung, Pointer - 1) End If ' move instruction If instruct = "move" Then opcode(PCounter) = 1 rung = Right(rung, Len(rung) - Pointer) ' extract variables var1(PCounter) = Val(rung) Pointer = 1 While Mid(rung, Pointer, 1) <> "," Pointer = Pointer + 1 Wend rung = Right(rung, Len(rung) - Pointer) var2(PCounter) = Val(rung) Pointer = 1 While Mid(rung, Pointer, 1) <> "," Pointer = Pointer + 1 Wend rung = Right(rung, Len(rung) - Pointer) var3(PCounter) = Val(rung) End If ' wait instruction If instruct = "wait" Then opcode(PCounter) = 2 rung = Right(rung, Len(rung) - Pointer) ' extract variables var1(PCounter) = Val(rung) * 10 End If 'servo instruction If instruct = "servo" Then opcode(PCounter) = 3 rung = Right(rung, Len(rung) - Pointer) ' extract variables var1(PCounter) = Val(rung) Pointer = 1 While Mid(rung, Pointer, 1) <> "," Pointer = Pointer + 1 Wend rung = Right(rung, Len(rung) - Pointer) var2(PCounter) = Val(rung) End If 'servo instruction If instruct = "for" Then opcode(PCounter) = 6 rung = Right(rung, Len(rung) - Pointer) ' extract variables var1(PCounter) = Val(rung) End If 'call instruction If instruct = "call" Then opcode(PCounter) = 8 rung = Right(rung, Len(rung) - Pointer) ' extract variables var1(PCounter) = Val(rung) End If 'sub instruction If instruct = "sub" Then opcode(PCounter) = 9 rung = Right(rung, Len(rung) - Pointer) ' extract variables Subaddr(Val(rung)) = PCounter 'save this subroutine address End If 'button instruction If instruct = "button" Then opcode(PCounter) = 11 rung = Right(rung, Len(rung) - Pointer) ' extract variables var1(PCounter) = Val(rung) End If ' bend button end instruction If instruct = "bend" Then opcode(PCounter) = 12 End If ' next instruction If instruct = "next" Then opcode(PCounter) = 7 End If ' return instruction If instruct = "return" Then opcode(PCounter) = 10 End If ' center instruction If instruct = "center" Then opcode(PCounter) = 13 End If ' end instruction If instruct = "end" Then opcode(PCounter) = 0 End If End If Next PCounter ' Execution Phase Tigil = False PCounter = 1 Forptr = 0 Stackptr = 0 execs = True While Tigil = False If opcode(PCounter) = 0 Then PCounter = 0 ' repeat program Forptr = 0 Stackptr = 0 End If DoEvents lblCounter.Caption = PCounter ' check if bend If opcode(PCounter) = 12 Then execs = True ' if bend is ecountered,execute all functions If execs = True Then 'wait for Go key if single stepping ' single stepping If chkStep.Value = 1 Then Call ShowRow(PCounter) While (chkStep.Value = 1 And Go = False) DoEvents Wend Select Case opcode(PCounter) Case 1: Speed = var3(PCounter) Pos = (var2(PCounter) * 100 / 90) + 150 ' 2 char string equivalent of motor number Motor = Str(var1(PCounter)) Motor = Right("0" + Right(Motor, Len(Motor) - 1), 2) MotPos(var1(PCounter)) = var2(PCounter) ' save last motor position If Speed > 0 Then Param = Right("0" + Hex(Speed + 128), 2) Send (Chr(2) + Motor + "s" + Param) End If Param = Right("0" + Hex(Pos), 2) Send (Chr(2) + Motor + "m" + Param) Case 2: ' wait instruction Dlay = Val(var1(PCounter)) Tmr = 0 tmrSequencer.Enabled = True Do DoEvents Loop Until tmrSequencer.Enabled = False If Sandali = False Then Go = False Case 3: 'servo instruction Motor = Str(var1(PCounter)) Motor = Right("0" + Right(Motor, Len(Motor) - 1), 2) Param = Right("0" + Hex(var2(PCounter)), 2) Send (Chr(2) + Motor + "i" + Param) Case 6: ' For Function Forptr = Forptr + 1 Foraddr(Forptr) = PCounter 'save for return address Forval(Forptr) = var1(PCounter) 'save repeat varaiable Case 7: ' Next Function Forval(Forptr) = Forval(Forptr) - 1 If Forval(Forptr) > 0 Then PCounter = Foraddr(Forptr) ' repeat loop Case 8: ' Call Function Stackptr = Stackptr + 1 Stack(Stackptr) = PCounter 'save program counter to stack PCounter = Subaddr(var1(PCounter)) 'goto subroutine Case 10: 'Return PCounter = Stack(Stackptr) 'retrieve return address Stackptr = Stackptr - 1 If Stackptr < 0 Then MsgBox "return without call!", vbCritical Exit Sub End If Case 11: 'button function If Bton(var1(PCounter)) = True Then execs = True Else execs = False End If ' If button false, do not execute ' until bend is encountered Case 13: 'Center servos Send (Chr(&HF3) + Chr(128)) End Select If Sandali = True Then Go = False ' single stepping End If PCounter = PCounter + 1 Wend cmdStart.Enabled = True End Sub Private Sub cmdWait_Click() Sandali = False Go = True End Sub Private Sub Form_Load() MSComm1.PortOpen = True Grid.ColWidth(1) = 5000 CurrentDir = CurDir + "\settings.ini" End Sub Private Sub Frame2_DragDrop(Source As Control, X As Single, Y As Single) End Sub Private Sub Grid_KeyPress(KeyAscii As Integer) With Grid Select Case KeyAscii Case 8 If Not .Text = "" Then .Text = Left(.Text, Len(.Text) - 1) End If Case 13 ' Enter Call cmdIns_Click Case Else .Text = .Text & Chr(KeyAscii) End Select End With txtIn.Text = Grid.Text End Sub Private Sub Grid_EnterCell() Dim rung, instruct As String Dim Pointer As Integer If Load = False Then ' Grid.Col = 1 Grid.CellBackColor = vbYellow rung = Grid.Text txtIn.Text = Grid.Text cmdUpdate.Enabled = False While Left(rung, 1) <= " " And rung <> "" rung = Right(rung, Len(rung) - 1) Wend If rung <> "" And Left(rung, 1) <> ";" Then ' if not blank or comment ' find opening parentheses Pointer = 1 While Mid(rung, Pointer, 1) <> "(" And Pointer < Len(rung) Pointer = Pointer + 1 Wend ' extract instruction If Pointer = Len(rung) Then instruct = rung ' instruction w/o parentheses Else instruct = Left(rung, Pointer - 1) End If ' move instruction If instruct = "move" Then rung = Right(rung, Len(rung) - Pointer) ' extract variables fmotor = Val(rung) lblMotor.Caption = fmotor Pointer = 1 While Mid(rung, Pointer, 1) <> "," Pointer = Pointer + 1 Wend rung = Right(rung, Len(rung) - Pointer) fpos = Val(rung) lblPos.Caption = fpos hsAdj.Value = fpos * 10 Pointer = 1 While Mid(rung, Pointer, 1) <> "," Pointer = Pointer + 1 Wend rung = Right(rung, Len(rung) - Pointer) fspeed = Val(rung) cmdUpdate.Enabled = True End If End If End If End Sub Private Sub Grid_LeaveCell() If Load = False Then ' Grid.Col = 1 Grid.CellBackColor = vbWhite End If End Sub Private Sub hsAdj_Change() Dim Position As Single Dim Mstring As String If chkStep.Value = 1 Then lblPos.Caption = hsAdj.Value / 10 fpos = Val(lblPos.Caption) Position = (fpos * 100 / 90) + 150 Mstring = Right("0" + lblMotor.Caption, 2) Send (Chr(2) + Mstring + "m" + Hex(Position)) End If End Sub Private Sub mnuBye_Click() MSComm1.PortOpen = False End End Sub Private Sub mnuNew_Click() Dim answer As Integer answer = MsgBox("This will discard the program currently loaded! Are you sure you want to do this?", vbOKCancel, "New Program") If answer = vbOK Then Grid.Clear Grid.Rows = 2 End If End Sub Private Sub mnuOpen_Click() Dim i As Integer Dim OFile As Integer Dim rung As String cDialog.Filter = "Sequence *.seq|*.seq" cDialog.ShowOpen On Error GoTo ByeFile ActiveFile = cDialog.FileName lblSequence.Caption = "Current Sequence : " + ActiveFile Grid.Clear Grid.Rows = 1 OFile = FreeFile(0) Open ActiveFile For Input As #OFile i = 1 ' compile source code Load = True While Not EOF(OFile) Line Input #OFile, rung ' Grid.Row = i ' Grid.Text = rung Grid.AddItem ("") Grid.Row = Grid.Rows - 1 Grid.Col = 0 Grid.Text = Grid.Row Grid.Col = 1 Grid.Text = rung Wend Load = False Close #OFile Exit Sub ByeFile: Load = False MsgBox "File Access Error", vbExclamation Close #OFile End Sub Private Sub mnuSave_Click() Dim SFile As Integer Dim i As Integer cDialog.Filter = "Sequence *.seq|*.seq" cDialog.ShowSave On Error GoTo Male ActiveFile = cDialog.FileName lblSequence.Caption = "Current Sequence : " + ActiveFile OFile = FreeFile(0) Load = True Open ActiveFile For Output As #OFile Grid.Col = 1 For i = 1 To Grid.Rows - 1 Grid.Row = i Print #OFile, Grid.Text Next i Close #OFile Load = False Exit Sub Male: Load = False MsgBox "File error occurred!", vbCritical End Sub Private Sub mnuServo_Click() frmServo.Show End Sub Private Sub tmrSequencer_Timer() Tmr = Tmr + 1 If Tmr >= Dlay Then tmrSequencer.Enabled = False End Sub Public Function Send(S As String) Dim test As String Dim Checksum As Integer Dim i As Integer Checksum = 0 For i = 1 To Len(S) Checksum = Checksum + Asc(Mid(S, i, 1)) Next i Checksum = Checksum And &HFF Checksum = Checksum Or &H80 MSComm1.Output = S + Chr(Checksum) test = MSComm1.Input Do DoEvents test = MSComm1.Input If test = Chr(4) Then Print "Checksum Error" End If Loop Until test = Chr(3) End Function Public Sub FillNumber() ' Fill first column with line numbers Dim i As Integer Load = True Grid.Col = 0 For i = 1 To Grid.Rows - 1 Grid.Row = i Grid.Text = i Next i Load = False End Sub Private Sub txtIn_Change() 'Reflect changes to grid Grid.Text = txtIn.Text End Sub Private Sub txtIn_KeyPress(KeyAscii As Integer) ' If enter is pressed, insert and go to new line If KeyAscii = 13 Then Call cmdIns_Click KeyAscii = 0 'do not use ENTER End If End Sub Public Sub ShowRow(PCounter As Integer) If PCounter - Grid.TopRow > 20 Then Grid.TopRow = PCounter If Grid.TopRow - PCounter > 20 Then Grid.TopRow = PCounter Grid.Row = PCounter End Sub