gclib  2.0.8
Communications API for Galil controllers and PLCs
record_position.cpp
Go to the documentation of this file.
1 
8 #include "examples.h"
9 
10 #include <iostream> //std::cout
11 #include <fstream>
12 
13 #define G_LASTINDEX 999
14 
15 using namespace std;
16 
17 void write_array_to_file(GCon g, ofstream& os, const char* array_name,
18  int previous_rd, int rd);
19 
20 GReturn record_position(GCon g, char* fileA, char* fileB)
21 {
22  ofstream osA, osB;
23  osA.open(fileA, ios::out | ios::trunc);
24  osB.open(fileB, ios::out | ios::trunc);
25 
26  int recording = 1;
27 
29  "RC 0;' Disable Recording\n"
30  "DP 0, 0;' Set current position to 0\r"
31  "DM posA[1000], posB[1000];' Define a new array that will hold positional data\r"
32  "RA posA[], posB[];' Sets position array to be where recorded data will be stored\r"
33  "RD _TPA, _TPB;' Defines Position to be the type of data that will be recorded\r"
34  "RC 1,-1000;' Begins recording at 512Hz in continuous mode\r"
35  "MO AB;' Turns motors off\r"
36  "AI -1;' Waits for active low on Input 1\r"
37  "RC 0;' Disable Recording after Input 1 goes low\r"
38  "EN;' End program", 0));
39  e(GCmd(g, "XQ")); //Begins execution of program
40 
41  int rd = 0;
42  int previous_rd = 0;
43 
44  do
45  {
46  GSleep(1000); //Sleep while we wait for roughly half the array to be written
47  e(GCmdI(g, "MG _RD", &rd)); //Gets address of next value in the position array
48 
49  //Get values from posA[] array and write to file
50  write_array_to_file(g, osA, "posA[]", previous_rd, rd);
51 
52  //Get values from posB[] array and write to file
53  write_array_to_file(g, osB, "posB[]", previous_rd, rd);
54 
55  e(GCmdI(g, "MG _RC", &recording)); // Check status of RC
56 
57  previous_rd = rd;
58  } while (recording); //While recording is active
59 
60  osA.close();
61  osB.close();
62 
63  return GALIL_EXAMPLE_OK;
64 }
65 
67 void write_array_to_file(GCon g, ofstream& os, const char* array_name,
68  int previous_rd, int rd)
69 {
70  char buf[G_HUGE_BUFFER]; //traffic buffer
71 
72  if (previous_rd < rd) // No array wrap around
73  {
74  e(GArrayUpload(g, array_name, previous_rd, rd - 1, G_COMMA, buf, G_HUGE_BUFFER));
75  os << ',' << buf;
76  }
77  else // Array wrapped around - grab two separate chunks from the array
78  {
79  e(GArrayUpload(g, array_name, previous_rd, G_LASTINDEX, G_COMMA, buf, G_HUGE_BUFFER));
80  os << ',' << buf;
81 
82  if (rd != 0)
83  {
84  e(GArrayUpload(g, array_name, 0, rd - 1, G_COMMA, buf, G_HUGE_BUFFER));
85  os << ',' << buf;
86  }
87  }
88 }
GCLIB_DLL_EXPORTED void GCALL GSleep(unsigned int timeout_ms)
Uses GUtility() and G_UTIL_SLEEP to provide a blocking sleep call which can be useful for timing-base...
Definition: gclibo.c:24
GCLIB_DLL_EXPORTED GReturn GCALL GCmdI(GCon g, GCStringIn command, int *value)
Wrapper around GCommand that provides the return value of a command parsed into an int.
Definition: gclibo.c:278
GCLIB_DLL_EXPORTED GReturn GCALL GProgramDownload(GCon g, GCStringIn program, GCStringIn preprocessor)
Downloads a program to the controller's program buffer.
GCLIB_DLL_EXPORTED GReturn GCALL GCmd(GCon g, GCStringIn command)
Wrapper around GCommand for use when the return value is not desired.
Definition: gclibo.c:237
GCLIB_DLL_EXPORTED GReturn GCALL GArrayUpload(GCon g, const GCStringIn array_name, GOption first, GOption last, GOption delim, GBufOut buffer, GSize buffer_len)
Uploads array data from the controller's array table.
#define G_COMMA
For GArrayUpload(), use this value in the delim field to delimit with commas.
Definition: gclib.h:54
int GReturn
Every function returns a value of type GReturn. See gclib_errors.h for possible values.
Definition: gclib.h:93
#define G_HUGE_BUFFER
Most reads from Galil hardware are small. This value will hold the largest array or program upload/do...
Definition: gclib.h:90
void * GCon
Connection handle. Unique for each connection in process. Assigned a non-zero value in GOpen().
Definition: gclib.h:94
void e(GReturn rc)
A trivial, C++ style return code check used in Galil's examples and demos.
Definition: examples.h:33
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.