| gclib 1.34.15 Communications API for Galil controllers and PLCs |

contour.cpp
Go to the documentation of this file.
1
8 #include "examples.h"
9
10 #include <iostream> //std::cout
11 #include <fstream>
12 #include <string> //to_string, string, etc.
13 #include <vector>
14
15 using namespace std;
16 bool load_buf(GCon g, const std::vector<int>& positions_A,
17 const std::vector<int>& positions_B, int capacity, int& cmd);
18 std::vector<int> csv_to_vector(ifstream& is);
19
20 GReturn contour(GCon g, char* fileA, char* fileB)
21 {
22 char buf[G_SMALL_BUFFER]; //traffic buffer
23 int rc; //Holds the status of the RC command
24 int rd;
25 int positionA = 0, positionB = 0;
26
27 record_position(g, fileA, fileB); //Record positional data on Axis A and B
28
29 ifstream isA, isB;
30 isA.open(fileA);
31 isB.open(fileB);
32
33 std::vector<int> positions_A = csv_to_vector(isA);
34 std::vector<int> positions_B = csv_to_vector(isB);
35
36 e(GCmd(g, "SH AB")); //Set servo here
37 e(GCmd(g, "PA 0, 0")); //Set current position to 0
38 e(GMotionComplete(g, "AB")); //Wait for motion to complete
39 e(GCmd(g, "CM AB")); //Put axis A & B in Contour Mode
40 e(GCmd(g, "DT -1")); //Pauses contour mode to pre-load buffer
41 e(GCmd(g, "CD 0,0")); //Pre load buffer with zeros to prevent under buffering
42 e(GCmd(g, "CD 0,0")); //Pre load buffer with zeros to prevent under buffering
43 e(GCmd(g, "CD 0,0")); //Pre load buffer with zeros to prevent under buffering
44 e(GCmd(g, "DT 1")); //Sets time interval for contour mode to be 2 samples
45
46 int capacity = 0; //Holds the capacity of the contour buffer
47 int cmd = 0; //Holds the counter for which position to send next
48
49 if (positions_A.size() != positions_B.size())
50 {
51 cout << "Error: The two datasets are not the same size\n";
52 return GALIL_EXAMPLE_ERROR;
53 }
54
55 do //loop while there are still commands in the buffer
56 {
57 //sleep while buffer is emptying
58 GSleep(400);
59
60 //Stores the available space of the contour buffer in the capacity variable
61 e(GCmdI(g, "CM?", &capacity));
62
63 } while (load_buf(g, positions_A, positions_B, capacity, cmd));
64
65 e(GCmd(g, "CD 0,0=0")); //End contour mode
66
67 isA.close();
68 isB.close();
69
70 return GALIL_EXAMPLE_OK;
71 }
72
74 bool load_buf(GCon g, const std::vector<int>& positions_A,
75 const std::vector<int>& positions_B, int capacity, int& cmd)
76 {
77 char buf[G_SMALL_BUFFER]; //traffic buffer
78 for (capacity; capacity > 0; capacity--) // Fully load contour buffer
79 {
80 // While there are commands in the position vectors
81 if (cmd + 1 < positions_A.size())
82 {
83 //Subtract previous position from new position to get how far of a move to make
84 int cdA = positions_A[cmd + 1] - positions_A[cmd];
85
86 //Subtract previous position from new position to get how far of a move to make
87 int cdB = positions_B[cmd + 1] - positions_B[cmd];
88
89 sprintf(buf, "CD %d,%d", cdA, cdB);
90 e(GCmd(g, buf)); //Send contour distance command
91
92 cmd++;
93 }
94 else
95 return false;
96 }
97
98 return true;
99 }
100
102 std::vector<int> csv_to_vector(ifstream& is)
103 {
104 std::vector<int> positions;
105 while (is.good()) //While there are still characters in the file
106 {
107 char position[G_SMALL_BUFFER];
108
109 //Get a char array of all the characters leading up to the next ','
110 is.getline(position, 16, ',');
111
112 //Convert read value to an integer
113 char* end;
114 int i_position = strtol(position, &end, 10);
115 positions.push_back(i_position); //Add value to the vector
116 }
117
118 return positions;
119 }
examples.h
GMotionComplete
GCLIB_DLL_EXPORTED GReturn GCALL GMotionComplete(GCon g, GCStringIn axes)
Blocking call that returns once all axes specified have completed their motion.
Definition: gclibo.c:300
GSleep
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
GCmdI
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
GCmd
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
GReturn
int GReturn
Every function returns a value of type GReturn. See gclib_errors.h for possible values.
Definition: gclib.h:93
G_SMALL_BUFFER
#define G_SMALL_BUFFER
Most reads from Galil are small. This value will easily hold most, e.g. TH, TZ, etc.
Definition: gclib.h:89
GCon
void * GCon
Connection handle. Unique for each connection in process. Assigned a non-zero value in GOpen().
Definition: gclib.h:94
e
void e(GReturn rc)
A trivial, C++ style return code check used in Galil's examples and demos.
Definition: examples.h:33
contour
GReturn contour(GCon g, char *fileA, char *fileB)
Record user's training and plays back training through contour mode.
Definition: contour.cpp:20
record_position
GReturn record_position(GCon g, char *fileA, char *fileB)
Record user's training and saves to a text file.
Definition: record_position.cpp:20
load_buf
bool load_buf(GCon g, const std::vector< int > &positions_A, const std::vector< int > &positions_B, int capacity, int &cmd)
Loads contour buffer with commands from the given text file.
Definition: contour.cpp:74
csv_to_vector
std::vector< int > csv_to_vector(ifstream &is)
Converts a file of comma separated values to a vector.
Definition: contour.cpp:102