gclib examples  0
Example Projects for the Communications API for Galil controllers and PLCs
position_tracking_example.cpp
Go to the documentation of this file.
1 
29 #include "examples.h"
30 
31 #include <iostream> //std::cout
32 using namespace std;
33 
35 
40 int main(int argc, char * argv[])
41 {
42  GReturn rc = GALIL_EXAMPLE_OK;
43  char buf[G_SMALL_BUFFER];
44 
45  //var used to refer to a unique connection. A valid connection is nonzero.
46  GCon g = 0;
47 
48  try
49  {
50  if (argc < 2 || argc > 3) //Invalid number of arguments
51  {
52  cerr << "Incorrect number of arguments provided\n";
53  cerr << "Usage: position_tracking_example.exe <ADDRESS> <SPEED=5000>\n";
54  return GALIL_EXAMPLE_ERROR;
55  }
56 
57  char* address = argv[1]; //Retrieve address from command line
58  e(GOpen(address, &g)); //Opens a connection at the provided address
59 
60  if (argc == 3) //Position tracking with custom speed
61  {
62  //Retrieve speed from command line and convert to int
63  char* end;
64  int speed = strtol(argv[2], &end, 10);
65  //If this character is not a null character,
66  //the user did not enter a valid integer for speed
67  if (*end != '\0')
68  {
69  cerr << "An invalid speed was entered. "
70  "Please enter a valid integer for speed.\n"
71  "Usage: position_tracking_example.exe <ADDRESS> <SPEED=5000>\n";
72  return GALIL_EXAMPLE_ERROR;
73  }
74 
75  //Puts controller into Position Tracking Mode and accepts user-entered positions
76  rc = position_tracking(g, speed);
77  }
78  else if (argc == 2) //Position tracking with default speed
79  {
80  //Puts controller into Position Tracking Mode and accepts user - entered positions
81  rc = position_tracking(g);
82  }
83  }
84  catch (GReturn gr)
85  {
86  GError(gr, buf, G_SMALL_BUFFER); //Get Error Information
87  cout << buf << '\n';
88  if (g)
89  {
90  GSize size = sizeof(buf);
91  GUtility(g, G_UTIL_ERROR_CONTEXT, buf, &size);
92  cout << buf << '\n'; //further context
93  }
94  return GALIL_EXAMPLE_ERROR;
95  }
96 
97  return rc;
98 }
GReturn position_tracking(GCon g, int speed=5000)
Puts controller into Position Tracking Mode and accepts user-entered positions.
void e(GReturn rc)
A trivial, C++ style return code check used in Galil's examples and demos.
Definition: examples.h:22
int main(int argc, char *argv[])
Main function for Position Tracking Example.