gclib  2.0.8
Communications API for Galil controllers and PLCs
x_ginterrupt.cpp
Go to the documentation of this file.
1 
7 #include "x_examples.h"
8 
10 {
11  cout << "\n************************************************************************\n";
12  cout << "Example GInterrupt() usage\n";
13  cout << "************************************************************************\n";
14 
15  char buf[1024]; //traffic buffer
16  GSize bytes_read;
17 
18  //-------------------------------------------------------------------------
19  //simple check for a interrupt support
20  bool ei_support = true;
21  if (GCommand(g, "WH", buf, 1024, &bytes_read) == G_NO_ERROR)
22  {
23  ei_support = (strstr(buf, "IH") != 0); //for this example, assume Ethernet connections supports interrupts.
24  }//else assume PCI supports
25 
26  if (!ei_support)
27  {
28  cout << "No support on this bus\n";
29  return G_NO_ERROR;
30  }
31 
32 
33 
34  //-------------------------------------------------------------------------
35  //Flush interrupts
36  x_e(GCmd(g, "EI0,0")); //turn off interrupts
37  GStatus status;
38  x_e(GTimeout(g, 0)); //zero timeout
39  while ((GInterrupt(g, &status) == G_NO_ERROR) && status); //flush interrupts, status will be zero when queue is empty
40  x_e(GTimeout(g, -1)); //restore timeout
41 
42  //-------------------------------------------------------------------------
43  // User Interrupts, UI
44  GTimeout(g, 1000); //set timeout to 1 sec
45  x_e(GProgramDownload(g, "WT500\rUI8\rEN", 0));
46 
47  x_e(GCmd(g, "XQ")); //execute program
48 
49  x_e(GInterrupt(g, &status));
50  GTimeout(g, -1); //restore timeout
51 
52  if ((status & 0xF0) == 0xF0) //UI are F0 - FF
53  {
54  cout << "\"UI " << (int)(status & 0x0F) << "\" executed.\n";
55  }
56  else
57  {
58  cout << "Unexpected interrupt, " << hex << (int)status << dec << '\n';
59  return GALIL_EXAMPLE_ERROR;
60  }
61 
62 
63 #if 0
64  //-------------------------------------------------------------------------
65  // Independent motion
66  x_e(GCmd(g, "DP 0,0")); //define position zero on A and B
67  x_e(GCmdT(g, "RP", buf, sizeof(buf), &trimmed));
68  cout << "\nPosition: " << trimmed << '\n';
69  x_e(GCmd(g, "SP 4000,4000")); //set up speed
70  x_e(GCmd(g, "AC 1280000,1280000")); //acceleration
71  x_e(GCmd(g, "DC 1280000,1280000")); //deceleration
72  x_e(GCmd(g, "PR 8000,10000")); //Postion Relative. B will take longer to make its move.
73  x_e(GCmd(g, "SH AB")); //Servo Here
74  cout << "Beginning independent motion... ";
75  x_e(GCmd(g, "BG AB")); //Begin motion
76  x_e(x_ei_motioncomplete(g, "AB")); //Block until motion is complete on axes A and B
77  cout << "Motion Complete on A and B\n";
78  x_e(GCmdT(g, "RP", buf, sizeof(buf), &trimmed));
79  cout << "Position: " << trimmed << '\n';;
80 #endif
81 
82  return GALIL_EXAMPLE_OK;
83 }
84 
85 int x_ei_motioncomplete(GCon g, GCStringIn axes) //Motion Complete with interrupts.
86 {
87  char buf[1024]; //traffic buffer
88  GReturn rc;
89  GStatus status;
90  unsigned char axis_mask = 0xFF; //bit mask of running axes, axes arg is trusted to provide running axes. Low bit indicates running.
91 
92  int len = strlen(axes);
93  for (int i = 0; i < len; i++) //iterate through all chars in axes to make the axis mask
94  {
95  //support just A-H
96  switch (axes[i])
97  {
98  case 'A':
99  axis_mask &= 0xFE;
100  break;
101  case 'B':
102  axis_mask &= 0xFD;
103  break;
104  case 'C':
105  axis_mask &= 0xFB;
106  break;
107  case 'D':
108  axis_mask &= 0xF7;
109  break;
110  case 'E':
111  axis_mask &= 0xEF;
112  break;
113  case 'F':
114  axis_mask &= 0xDF;
115  break;
116  case 'G':
117  axis_mask &= 0xBF;
118  break;
119  case 'H':
120  axis_mask &= 0x7F;
121  break;
122  }
123  }
124  sprintf(buf, "EI %u", (unsigned char) ~axis_mask);
125  x_e(GCmd(g, buf)); //send EI axis mask to set up interrupt events.
126 
127  while (axis_mask != 0xFF) //wait for all interrupts to come in
128  {
129  if ((rc = GInterrupt(g, &status)) == G_NO_ERROR)
130  {
131  switch (status)
132  {
133  case 0xD0: //Axis A complete
134  axis_mask |= 0x01;
135  break;
136  case 0xD1: //Axis B complete
137  axis_mask |= 0x02;
138  break;
139  case 0xD2: //Axis C complete
140  axis_mask |= 0x04;
141  break;
142  case 0xD3: //Axis D complete
143  axis_mask |= 0x08;
144  break;
145  case 0xD4: //Axis E complete
146  axis_mask |= 0x10;
147  break;
148  case 0xD5: //Axis F complete
149  axis_mask |= 0x20;
150  break;
151  case 0xD6: //Axis G complete
152  axis_mask |= 0x40;
153  break;
154  case 0xD7: //Axis H complete
155  axis_mask |= 0x80;
156  break;
157  }
158  }
159  }
160 
161  return rc;
162 }
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 GProgramDownload(GCon g, GCStringIn program, GCStringIn preprocessor)
Downloads a program to the controller's program buffer.
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
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
const char * GCStringIn
C-string input to the library. Implies null-termination.
Definition: gclib.h:98
void x_e(GReturn rc)
A trivial, C++ style return code check used in Galil's examples and demos.
Definition: x_examples.h:30
int x_ginterrupt(GCon g)
Example GInterrupt() usage.
Definition: x_ginterrupt.cpp:9
int x_ei_motioncomplete(GCon g, GCStringIn axes)
Example of MotionComplete with interrupts.