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