gclib  1.36.3
Communications API for Galil controllers and PLCs
record_position.cs
Go to the documentation of this file.
1 
10 using System;
11 using System.Collections.Generic;
12 using System.Linq;
13 using System.IO;
14 using System.Threading;
15 
16 namespace examples
17 {
18  public static partial class Examples
19  {
31 
32  public static int Record_Position(gclib gclib, string fileA, string fileB)
33  {
34  StreamWriter writerA = new StreamWriter(fileA, false);
35  StreamWriter writerB = new StreamWriter(fileB, false);
36 
37  int recording = 1;
38 
39  gclib.GProgramDownload(
40  "RC 0;' Disable Recording\n" +
41  "DP 0, 0;' Set current position to 0\r" +
42  "DM posA[1000], posB[1000];' Define a new array that will hold positional data\r" +
43  "RA posA[], posB[];' Sets position array to be where recorded data will be stored\r" +
44  "RD _TPA, _TPB;' Defines Position to be the type of data that will be recorded\r" +
45  "RC 1,-1000;' Begins recording at 512Hz in continuous mode\r" +
46  "MO AB;' Turns motors off\r" +
47  "AI -1;' Waits for active low on Input 1\r" +
48  "RC 0;' Disable Recording after Input 1 goes low\r" +
49  "EN;' End program"
50  );
51 
52  gclib.GCommand("XQ");
53 
54  int rd = 0;
55  int previous_rd = 0;
56  bool leading_comma = false;
57 
58  do
59  {
60  Thread.Sleep(1000); //Sleep while we wait for roughly half the array to be written
61  rd = gclib.GCmdI("MG _RD"); //Gets address of next value in the position array
62 
63  //Get values from posA[] array and write to file
64  Write_Array_To_File(gclib, writerA, "posA", previous_rd, rd, leading_comma);
65 
66  //Get values from posB[] array and write to file
67  Write_Array_To_File(gclib, writerB, "posB", previous_rd, rd, leading_comma);
68 
69  leading_comma = true;
70 
71  recording = gclib.GCmdI("MG _RC"); //Check status of RC
72 
73  previous_rd = rd;
74 
75  } while (recording > 0); //While recording is active
76 
77  writerA.Close();
78  writerB.Close();
79 
81  }
82 
83  private static void Write_Array_To_File(gclib gclib, StreamWriter writer, string array_name, int previous_rd, int rd, bool leading_comma)
84  {
85  List<double> values = new List<double>();
86  if (previous_rd < rd) // No array wrap around
87  {
88  //Grab list of doubles from controller and add it to values
89  values.AddRange(gclib.GArrayUpload(array_name, (short)previous_rd, (short)(rd - 1)));
90  }
91  else
92  {
93  //Grab list of doubles from controller and add it to values
94  values.AddRange(gclib.GArrayUpload(array_name, (short)previous_rd, 999));
95 
96  if (rd != 0)
97  {
98  //Grab list of doubles from controller and add it to values
99  values.AddRange(gclib.GArrayUpload(array_name, 0, (short)(rd - 1)));
100  }
101  }
102 
103 
104  for (int i = 0; i < values.Count(); i++)
105  {
106  if (leading_comma)
107  writer.Write(", ");
108 
109  leading_comma = true;
110 
111  writer.Write(String.Format(("{0:0.000}"), values[i]));
112  }
113  }
115  }
116 }
Provides a class of shared constants and methods for gclib's example projects.
Definition: commands.cs:16
const int GALIL_EXAMPLE_OK
Examples success code.
Definition: examples.cs:29
static int Record_Position(gclib gclib, string fileA, string fileB)
Record user's training and saves to a text file.