gclib examples  0
Example Projects for the Communications API for Galil controllers and PLCs
record_position.cpp
Go to the documentation of this file.
1 
4 #include "examples.h"
5 
6 #include <iostream> //std::cout
7 #include <fstream>
8 
9 #define G_LASTINDEX 999
10 
11 using namespace std;
12 
13 void write_array_to_file(GCon g, ofstream& os, const char* array_name,
14  int previous_rd, int rd);
15 
16 GReturn record_position(GCon g, char* fileA, char* fileB)
17 {
18  ofstream osA, osB;
19  osA.open(fileA, ios::out | ios::trunc);
20  osB.open(fileB, ios::out | ios::trunc);
21 
22  int recording = 1;
23  int position;
24 
25  e(GProgramDownload(g,
26  "RC 0;' Disable Recording\n"
27  "DP 0, 0;' Set current position to 0\r"
28  "DM posA[1000], posB[1000];' Define a new array that will hold positional data\r"
29  "RA posA[], posB[];' Sets position array to be where recorded data will be stored\r"
30  "RD _TPA, _TPB;' Defines Position to be the type of data that will be recorded\r"
31  "RC 1,-1000;' Begins recording at 512Hz in continuous mode\r"
32  "MO AB;' Turns motors off\r"
33  "AI -1;' Waits for active low on Input 1\r"
34  "RC 0;' Disable Recording after Input 1 goes low\r"
35  "EN;' End program", 0));
36  e(GCmd(g, "XQ")); //Begins execution of program
37 
38  int rd = 0;
39  int previous_rd = 0;
40 
41  do
42  {
43  GSleep(1000); //Sleep while we wait for roughly half the array to be written
44  e(GCmdI(g, "MG _RD", &rd)); //Gets address of next value in the position array
45 
46  //Get values from posA[] array and write to file
47  write_array_to_file(g, osA, "posA[]", previous_rd, rd);
48 
49  //Get values from posB[] array and write to file
50  write_array_to_file(g, osB, "posB[]", previous_rd, rd);
51 
52  e(GCmdI(g, "MG _RC", &recording)); // Check status of RC
53 
54  previous_rd = rd;
55  } while (recording); //While recording is active
56 
57  osA.close();
58  osB.close();
59 
60  return GALIL_EXAMPLE_OK;
61 }
62 
64 void write_array_to_file(GCon g, ofstream& os, const char* array_name,
65  int previous_rd, int rd)
66 {
67  char buf[G_HUGE_BUFFER]; //traffic buffer
68 
69  if (previous_rd < rd) // No array wrap around
70  {
71  e(GArrayUpload(g, array_name, previous_rd, rd - 1, G_COMMA, buf, G_HUGE_BUFFER));
72  os << ',' << buf;
73  }
74  else // Array wrapped around - grab two separate chunks from the array
75  {
76  e(GArrayUpload(g, array_name, previous_rd, G_LASTINDEX, G_COMMA, buf, G_HUGE_BUFFER));
77  os << ',' << buf;
78 
79  if (rd != 0)
80  {
81  e(GArrayUpload(g, array_name, 0, rd - 1, G_COMMA, buf, G_HUGE_BUFFER));
82  os << ',' << buf;
83  }
84  }
85 }
void e(GReturn rc)
A trivial, C++ style return code check used in Galil's examples and demos.
Definition: examples.h:22
GReturn record_position(GCon g, char *fileA, char *fileB)
Record user's training and saves to a text file.
void write_array_to_file(GCon g, ofstream &os, const char *array_name, int previous_rd, int rd)
Grabs data from array on controller and writes it to the given text file.