gclib  2.0.8
Communications API for Galil controllers and PLCs
motion_complete.cpp
Go to the documentation of this file.
1 
10 #include "examples.h"
11 
12 #include <iostream> //std::cout
13 #include <string.h>
14 using namespace std;
15 
16 int check_interrupts(GCon g, GCStringIn axes);
17 
19 {
20  char* trimmed;
21 
22  cout << "\n*************************************************************\n";
23  cout << "Example GInterrupt() usage\n";
24  cout << "***************************************************************\n";
25 
26  char buf[G_SMALL_BUFFER]; //traffic buffer
27  GSize bytes_read;
28 
29  //-------------------------------------------------------------------------
30  //simple check for appropriate communication bus
31  //EI will fail below if interrupts are not supported at all
32  bool ei_support = true;
33  if (GCommand(g, "WH", buf, G_SMALL_BUFFER, &bytes_read) == G_NO_ERROR)
34  {
35  //for this example, assume Ethernet connections supports interrupts.
36  ei_support = (strstr(buf, "IH") != 0);
37  }//else assume PCI supports
38 
39  if (!ei_support)
40  {
41  cout << "No support on this bus\n";
43  }
44 
45  //-------------------------------------------------------------------------
46  //Flush interrupts
47  e(GCmd(g, "EI0,0")); //turn off interrupts
48  GStatus status;
49  e(GTimeout(g, 0)); //zero timeout
50 
51  //flush interrupts, status will be zero when queue is empty
52  while ((GInterrupt(g, &status) == G_NO_ERROR) && status);
53  e(GTimeout(g, -1)); //restore timeout
54 
55 
56  //-------------------------------------------------------------------------
57  // Independent motion
58  e(GCmd(g, "DP 0,0")); //define position zero on A and B
59  e(GCmdT(g, "RP", buf, G_SMALL_BUFFER, &trimmed));
60  cout << "\nPosition: " << trimmed << '\n';
61  e(GCmd(g, "SP 4000,4000")); //set up speed
62  e(GCmd(g, "AC 1280000,1280000")); //acceleration
63  e(GCmd(g, "DC 1280000,1280000")); //deceleration
64  e(GCmd(g, "PR 8000,10000")); //Postion Relative. B will take longer to make its move.
65  e(GCmd(g, "SH AB")); //Servo Here
66  cout << "Beginning independent motion...\n";
67  e(GCmd(g, "BG AB")); //Begin motion
68  check_interrupts(g, "AB"); //Block until motion is complete on axes A and B
69  cout << "Motion Complete on A and B\n";
70  e(GCmdT(g, "RP", buf, G_SMALL_BUFFER, &trimmed));
71  cout << "Position: " << trimmed << '\n';
72 
73  return GALIL_EXAMPLE_OK;
74 }
75 
78 {
79  char buf[G_SMALL_BUFFER]; //traffic buffer
80  GReturn rc;
81  GStatus status;
82  //bit mask of running axes, axes arg is trusted to provide running axes.
83  //Low bit indicates running.
84  unsigned char axis_mask = 0xFF;
85 
86  int len = strlen(axes);
87  //iterate through all chars in axes to make the axis mask
88  for (int i = 0; i < len; i++)
89  {
90  //support just A-H
91  switch (axes[i])
92  {
93  case 'A':
94  axis_mask &= 0xFE;
95  break;
96  case 'B':
97  axis_mask &= 0xFD;
98  break;
99  case 'C':
100  axis_mask &= 0xFB;
101  break;
102  case 'D':
103  axis_mask &= 0xF7;
104  break;
105  case 'E':
106  axis_mask &= 0xEF;
107  break;
108  case 'F':
109  axis_mask &= 0xDF;
110  break;
111  case 'G':
112  axis_mask &= 0xBF;
113  break;
114  case 'H':
115  axis_mask &= 0x7F;
116  break;
117  }
118  }
119  sprintf(buf, "EI %u", (unsigned char)~axis_mask);
120  e(GCmd(g, buf)); //send EI axis mask to set up interrupt events.
121 
122  while (axis_mask != 0xFF) //wait for all interrupts to come in
123  {
124  if ((rc = GInterrupt(g, &status)) == G_NO_ERROR)
125  {
126  switch (status)
127  {
128  case 0xD0: //Axis A complete
129  axis_mask |= 0x01;
130  break;
131  case 0xD1: //Axis B complete
132  axis_mask |= 0x02;
133  break;
134  case 0xD2: //Axis C complete
135  axis_mask |= 0x04;
136  break;
137  case 0xD3: //Axis D complete
138  axis_mask |= 0x08;
139  break;
140  case 0xD4: //Axis E complete
141  axis_mask |= 0x10;
142  break;
143  case 0xD5: //Axis F complete
144  axis_mask |= 0x20;
145  break;
146  case 0xD6: //Axis G complete
147  axis_mask |= 0x40;
148  break;
149  case 0xD7: //Axis H complete
150  axis_mask |= 0x80;
151  break;
152  }
153  }
154  }
155 
156  return rc;
157 }
GCLIB_DLL_EXPORTED GReturn GCALL GCmdT(GCon g, GCStringIn command, GCStringOut trimmed_response, GSize response_len, GCStringOut *front)
Wrapper around GCommand that trims the response.
Definition: gclibo.c:243
GCLIB_DLL_EXPORTED GReturn GCALL GTimeout(GCon g, short timeout_ms)
Uses GUtility() and G_UTIL_TIMEOUT_OVERRIDE to set the library timeout.
Definition: gclibo.c:65
GCLIB_DLL_EXPORTED GReturn GCALL GCommand(GCon g, GCStringIn command, GBufOut buffer, GSize buffer_len, GSize *bytes_returned)
Performs a command-and-response transaction on the connection.
GCLIB_DLL_EXPORTED GReturn GCALL GInterrupt(GCon g, GStatus *status_byte)
Provides access to PCI and UDP interrupts from the controller.
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
unsigned char GStatus
Interrupt status byte.
Definition: gclib.h:101
#define G_NO_ERROR
Return value if function succeeded.
Definition: gclib_errors.h:13
unsigned int GSize
Size of buffers, etc.
Definition: gclib.h:95
void * GCon
Connection handle. Unique for each connection in process. Assigned a non-zero value in GOpen().
Definition: gclib.h:94
#define G_UNSUPPORTED_FUNCTION
Function cannot be called on this bus. E.G. GInterrupt() on serial.
Definition: gclib_errors.h:55
const char * GCStringIn
C-string input to the library. Implies null-termination.
Definition: gclib.h:98
void e(GReturn rc)
A trivial, C++ style return code check used in Galil's examples and demos.
Definition: examples.h:33
GReturn motion_complete(GCon g)
Uses interrupts to track when the motion of controller is completed.
int check_interrupts(GCon g, GCStringIn axes)
Monitors interrupt status on the given axes and returns when interrupts are fired.