gclib examples  0
Example Projects for the Communications API for Galil controllers and PLCs
vector.cpp
Go to the documentation of this file.
1 
6 #include "examples.h"
7 #include <iostream> //std::cout
8 #include <string> //to_string, string, etc.
9 #include <fstream>
10 using namespace std;
11 
12 bool load_buffer(GCon g, ifstream& fs, int capacity);
13 
32 GReturn vector(GCon g, char* file)
33 {
34  if (g == 0) //Bad connection
35  {
36  cerr << "There was an error with the connection." << endl;
37  return G_CONNECTION_NOT_ESTABLISHED;
38  }
39 
40  e(GCmd(g, "ST")); // stop all motors
41  e(GCmd(g, "SH AB")); // Set servo here
42  e(GCmd(g, "DP 0,0")); // Start position at absolute zero
43 
44  e(GCmd(g, "CAS")); //Defines S as active coordinate system
45  e(GCmd(g, "VS 20000")); //Defines vector speed
46  e(GCmd(g, "VA 200000")); //Defines vector acceleration
47  e(GCmd(g, "VD 200000")); //Defines vector deceleration
48  e(GCmd(g, "VM AB")); //Begin Vector Segment
49 
50  ifstream fs;
51  fs.open(file); //Open user defined text file
52  if (!fs) //If file could not be opened
53  {
54  cout << "Unable to open file\n";
55  return G_BAD_FILE;
56  }
57 
58  int capacity;
59  //Stores the available space of the vector buffer in the capacity variable
60  e(GCmdI(g, "MG _LMS", &capacity));
61  load_buffer(g, fs, capacity);
62 
63  e(GCmd(g, "BG S")); //Begin Motion
64 
65  do //Load buffer with more commands
66  {
67  GSleep(100); //Sleep a bit while buffer is emptying
68 
69  //Stores the available space of the vector buffer in the capacity variable
70  e(GCmdI(g, "MG _LMS", &capacity));
71  } while (load_buffer(g, fs, capacity));
72 
73  fs.close(); //Close the file
74 
75  e(GCmd(g, "VE")); //Segment End
76  e(GMotionComplete(g, "S"));
77 
78  return GALIL_EXAMPLE_OK;
79 }
80 
84 bool load_buffer(GCon g, ifstream& fs, int capacity)
85 {
86  string s_cmd;
87  // Fully load the vector buffer leaving room for one VE command
88  for (capacity; capacity > 1; capacity--)
89  {
90  if (getline(fs, s_cmd)) //if there is another line of the text file
91  {
92  //Run the command on each line of the text file
93  e(GCmd(g, s_cmd.c_str()));
94  }
95  else
96  return false;
97  }
98 
99  return true;
100 }
bool load_buffer(GCon g, ifstream &fs, int capacity)
Definition: vector.cpp:84
GReturn vector(GCon g, char *file)
Puts controller into Vector Mode and accepts a file defining vector points.
Definition: vector.cpp:32
void e(GReturn rc)
A trivial, C++ style return code check used in Galil's examples and demos.
Definition: examples.h:22