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