| gclib 1.35.5 Communications API for Galil controllers and PLCs |

Record_Position.vb
1 Partial Public Module Examples
2 Public Function Record_Position(gclib As Gclib, fileA As String, fileB As String)
3 Dim writerA = New System.IO.StreamWriter(fileA, False)
4 Dim writerB = New System.IO.StreamWriter(fileB, False)
5
6 Dim recording = 1
7
8 gclib.GProgramDownload(
9 "RC 0;' Disable Recording" + vbCr +
10 "DP 0, 0;' Set current position to 0" + vbCr +
11 "DM posA[1000], posB[1000];' Define a new array that will hold positional data" + vbCr +
12 "RA posA[], posB[];' Sets position array to be where recorded data will be stored" + vbCr +
13 "RD _TPA, _TPB;' Defines Position to be the type of data that will be recorded" + vbCr +
14 "RC 1,-1000;' Begins recording at 512Hz in continuous mode" + vbCr +
15 "MO AB;' Turns motors off" + vbCr +
16 "AI -1;' Waits for active low on Input 1" + vbCr +
17 "RC 0;' Disable Recording after Input 1 goes low" + vbCr +
18 "EN;' End program"
19 )
20
21 gclib.GCommand("XQ")
22
23 Dim rd = 0
24 Dim previous_rd = 0
25 Dim leading_comma = False
26
27 Console.WriteLine("Begin recording")
28
29 Do
30 System.Threading.Thread.Sleep(1000) 'Sleep while we wait for roughly half the array to be written
31 rd = gclib.GCmdI("MG _RD") 'Gets address of next value in the position array
32
33 'Get values from posA[] array and write to file
34 Write_Array_To_File(gclib, writerA, "posA", previous_rd, rd, leading_comma)
35
36 'Get values from posB[] array and write to file
37 Write_Array_To_File(gclib, writerB, "posB", previous_rd, rd, leading_comma)
38
39 leading_comma = True
40
41 recording = gclib.GCmdI("MG _RC") 'Check status of RC
42
43 previous_rd = rd
44
45 Loop While recording > 0 'While recording is active
46
47 Console.WriteLine("End recording")
48
49 writerA.Close()
50 writerB.Close()
51
52 Return Examples.GALIL_EXAMPLE_OK
53 End Function
54
55 Private Sub Write_Array_To_File(gclib As Gclib, writer As System.IO.StreamWriter, array_name As String, previous_rd As Integer, rd As Integer, leading_comma As Boolean)
56 Dim values = New List(Of Double)
57
58 If previous_rd < rd Then 'No array wrap around
59 'Grab list of doubles from controller and add it to values
60 values.AddRange(gclib.GArrayUpload(array_name, previous_rd, rd - 1))
61 Else
62 'Grab list of doubles from controller and add it to values
63 values.AddRange(gclib.GArrayUpload(array_name, previous_rd, 999))
64
65 If rd <> 0 Then
66 'Grab list of doubles from controller and add it to values
67 values.AddRange(gclib.GArrayUpload(array_name, 0, rd - 1))
68 End If
69 End If
70
71 For i = 0 To values.Count() - 1
72 If leading_comma Then
73 writer.Write(", ")
74 End If
75
76 leading_comma = True
77
78 writer.Write(String.Format("{0:0.000}", values(i)))
79 Next
80 End Sub
81 End Module