| gclib 1.35.5 Communications API for Galil controllers and PLCs |

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