gclib  2.0.8
Communications API for Galil controllers and PLCs
x_grecord.cpp
Go to the documentation of this file.
1 
8 #include "x_examples.h"
9 
11 {
12  cout << "\n************************************************************************\n";
13  cout << "Example GRecord() usage\n";
14  cout << "************************************************************************\n";
15 
16  char buf[1024]; //traffic buffer
17  GSize bytes_read;
18 
19  //-------------------------------------------------------------------------
20  //Grab a data record via QR and pull out some values.
21  cout << "\nQR-based data record\n";
22  GDataRecord r; //user's data record union.
23  x_e(GRecord(g, &r, G_QR)); //poll for data record.
24  cout << r.dmc4000.sample_number << '\n'; //sample number
25  cout << r.dmc4000.axis_a_reference_position << "\n"; //Axis A's reference, "RPA"
26 
27  //-------------------------------------------------------------------------
28  //simple check for a serial connection.
29  bool dr_support = true;
30  if (GCommand(g, "WH", buf, 1024, &bytes_read) == G_NO_ERROR)
31  {
32  dr_support = (strstr(buf, "IH") != 0); //for this example, assume Ethernet connections supports DR.
33  }//else assume PCI supports, note 18x2 doesn't
34 
35 
36  if (dr_support)
37  {
38  //-------------------------------------------------------------------------
39  //Read data records asynchronously for a given interval.
40  //note -s DR must have been specified in GOpen()
41  cout << "\nDR-based data record\n";
42  unsigned short time = 0; //controller's sample time, 1 tick a ms with TM 1000.
43  unsigned short deadline = 1000; //listen time, in ms, at TM 1000
44  int original_dr;
45  x_e(GCmdI(g, "MG_DR", &original_dr)); //grab the current DR value
46  x_e(GRecordRate(g, 100)); //set up DR to 10Hz
47  for (size_t i = 0; deadline > time; i++)
48  {
49  x_e(GRecord(g, &r, G_DR)); //get record
50  time = r.dmc4000.sample_number; //pull out the desired value
51  //time = r.dmc1806.sample_number; //note, these are product-specific
52  cout << time << '\n'; //print time
53  if (!i) deadline = time + deadline; //sample on first iteration to set deadline
54  }
55 
56  sprintf(buf, "DR %d", original_dr);
57  x_e(GCmd(g, buf)); //restore DR
58  }
59 
60  //-------------------------------------------------------------------------
61  //Use pointer arithmetic to pull out arbitrary types and offsets.
62  cout << "\nQR-based data record with offsets\n";
63  x_e(GRecord(g, &r, G_QR)); //poll for data record.
64  cout << r.dmc4000.sample_number << '\n';
65  cout << *((unsigned short*)(r.byte_array + 4)) << '\n'; //equivalent to sample_number for DMC-4000
66 
67 
68 #if 0
69  char* trimmed; //trimmed string pointer
70  //-------------------------------------------------------------------------
71  // Independent motion
72  x_e(GCmd(g, "DP 0")); //define position zero on A
73  x_e(GCmdT(g, "RP", buf, sizeof(buf), &trimmed));
74  cout << "\nPosition: " << trimmed << '\n';
75  x_e(GCmd(g, "SP 4000")); //set up speed
76  x_e(GCmd(g, "AC 1280000")); //acceleration
77  x_e(GCmd(g, "DC 1280000")); //deceleration
78  x_e(GCmd(g, "PR 8000")); //Postion Relative. B will take longer to make its move.
79  x_e(GCmd(g, "SH A")); //Servo Here
80  cout << "Beginning independent motion... ";
81  x_e(GCmd(g, "BG A")); //Begin motion
82  x_e(x_dr_motioncomplete(g, "A")); //Block until motion is complete on axes A and B
83  cout << "Motion Complete on A\n";
84  x_e(GCmdT(g, "RP", buf, sizeof(buf), &trimmed));
85  cout << "Position: " << trimmed << '\n';;
86 # endif
87 
88  return GALIL_EXAMPLE_OK;
89 }
90 
91 
93 {
94 
95  GReturn rc;
96  GDataRecord r;
97 
98 
99  int original_dr;
100  x_e(GCmdI(g, "MG_DR", &original_dr)); //grab the current DR value
101  x_e(GRecordRate(g, 100)); //set up DR to 10Hz
102 
103  int len = strlen(axes);
104  UW* axis_status;
105  for (int i = 0; i < len; /*blank*/) //iterate through all chars in axes to make the axis mask
106  {
107  rc = GRecord(g, &r, G_DR);
108  //support just A-H
109  switch (axes[i])
110  {
111  case 'A':
112  axis_status = &r.dmc4000.axis_a_status;
113  break;
114  case 'B':
115  axis_status = &r.dmc4000.axis_b_status;
116  break;
117  case 'C':
118  axis_status = &r.dmc4000.axis_c_status;
119  break;
120  case 'D':
121  axis_status = &r.dmc4000.axis_d_status;
122  break;
123  case 'E':
124  axis_status = &r.dmc4000.axis_e_status;
125  break;
126  case 'F':
127  axis_status = &r.dmc4000.axis_f_status;
128  break;
129  case 'G':
130  axis_status = &r.dmc4000.axis_g_status;
131  break;
132  case 'H':
133  axis_status = &r.dmc4000.axis_h_status;
134  break;
135  default:
136  axis_status = 0;
137  }
138 
139  if (axis_status)
140  if (!(*axis_status & 0x8000)) //bit 15 is "Move in progress"
141  i++;
142  }
143 
144  char buf[16];
145  sprintf(buf, "DR %d", original_dr);
146  x_e(GCmd(g, buf)); //restore DR
147 
148  return GALIL_EXAMPLE_OK;
149 
150 }
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 GRecord(GCon g, union GDataRecord *record, GOption method)
Provides a fresh copy of the controller's data record. Data is cast into a union, GDataRecord.
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 GRecordRate(GCon g, double period_ms)
Sets the asynchronous data record to a user-specified period via DR.
Definition: gclibo.c:342
GCLIB_DLL_EXPORTED GReturn GCALL GCmdI(GCon g, GCStringIn command, int *value)
Wrapper around GCommand that provides the return value of a command parsed into an int.
Definition: gclibo.c:278
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
#define G_DR
Value for GRecord() method variable for acquiring a data record via DR mode.
Definition: gclib.h:50
int GReturn
Every function returns a value of type GReturn. See gclib_errors.h for possible values.
Definition: gclib.h:93
#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_QR
Value for GRecord() method variable for acquiring a data record via QR mode.
Definition: gclib.h:51
const char * GCStringIn
C-string input to the library. Implies null-termination.
Definition: gclib.h:98
UW axis_e_status
E axis status.
Definition: gclib_record.h:160
UW axis_h_status
H axis status.
Definition: gclib_record.h:202
UW sample_number
sample number.
Definition: gclib_record.h:45
UW axis_f_status
F axis status.
Definition: gclib_record.h:174
UW axis_g_status
G axis status.
Definition: gclib_record.h:188
UW axis_d_status
D axis status.
Definition: gclib_record.h:146
UW axis_a_status
A axis status.
Definition: gclib_record.h:104
SL axis_a_reference_position
A axis reference position.
Definition: gclib_record.h:107
UW axis_b_status
B axis status.
Definition: gclib_record.h:118
UW axis_c_status
C axis status.
Definition: gclib_record.h:132
Data record union, containing all structs and a generic byte array accessor.
unsigned char byte_array[GALILDATARECORDMAXLENGTH]
Generic byte array for offsets.
struct GDataRecord4000 dmc4000
The DMC-4000 data record.
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_grecord(GCon g)
Example GRecord() usage.
Definition: x_grecord.cpp:10
int x_dr_motioncomplete(GCon g, GCStringIn axes)
Example of MotionComplete with data records.
Definition: x_grecord.cpp:92