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