gclib  2.0.8
Communications API for Galil controllers and PLCs
position_tracking.cpp
Go to the documentation of this file.
1 
10 #include "examples.h"
11 
12 #include <iostream> //std::cout
13 using namespace std;
14 
16 {
17  char buf[G_SMALL_BUFFER]; //traffic buffer
18  int acc = 100 * speed; //set acceleration/deceleration to 100 times speed
19 
20  if (g == 0) //Bad connection
21  {
22  cerr << "There was an error with the connection." << endl;
24  }
25 
26  e(GCmd(g, "STA")); // stop motor
27  e(GMotionComplete(g, "A")); //Wait for motion to complete
28  e(GCmd(g, "SHA")); // Set servo here
29  e(GCmd(g, "DPA=0")); // Start position at absolute zero
30  e(GCmd(g, "PTA=1")); // Start position tracking mode on A axis
31 
32  sprintf(buf, "SPA=%d", speed);
33  e(GCmd(g, buf)); // Set speed
34 
35  sprintf(buf, "ACA=%d", acc);
36  e(GCmd(g, buf)); // Set acceleration
37 
38  sprintf(buf, "DCA=%d", acc);
39  e(GCmd(g, buf)); // Set deceleration
40 
41  cout << "Begin Position Tracking with speed " << speed;
42  cout << ". Enter a non-number to exit.\n";
43  int position;
44 
45  //Loop asking user for new position. End loop when user enters a non-number
46  while (1)
47  {
48  cout << "Enter a new position:\n";
49  cin >> position;
50 
51  if (cin.fail()) //A non-number was entered
52  {
53  cout << "Position Tracking has exited\n";
54  break;
55  }
56  else
57  {
58  sprintf(buf, "PAA=%d", position);
59  e(GCmd(g, buf)); // Go to new position
60  }
61  }
62 
63  e(GCmd(g, "STA")); //stop motor
64  e(GMotionComplete(g, "A")); //Wait for motion to complete
65 
66  return GALIL_EXAMPLE_OK;
67 }
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
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
int GReturn
Every function returns a value of type GReturn. See gclib_errors.h for possible values.
Definition: gclib.h:93
#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
void * GCon
Connection handle. Unique for each connection in process. Assigned a non-zero value in GOpen().
Definition: gclib.h:94
#define G_CONNECTION_NOT_ESTABLISHED
Function was called with no connection.
Definition: gclib_errors.h:64
void e(GReturn rc)
A trivial, C++ style return code check used in Galil's examples and demos.
Definition: examples.h:33
GReturn position_tracking(GCon g, int speed=5000)
Puts controller into Position Tracking Mode and accepts user-entered positions.