sensor_epsonUart.c
Go to the documentation of this file.
1 //==============================================================================
2 //
3 // sensor_epsonUart.c - Epson IMU sensor protocol UART specific code
4 //
5 //
6 // THE SOFTWARE IS RELEASED INTO THE PUBLIC DOMAIN.
7 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9 // NONINFRINGEMENT, SECURITY, SATISFACTORY QUALITY, AND FITNESS FOR A
10 // PARTICULAR PURPOSE. IN NO EVENT SHALL EPSON BE LIABLE FOR ANY LOSS, DAMAGE
11 // OR CLAIM, ARISING FROM OR IN CONNECTION WITH THE SOFTWARE OR THE USE OF THE
12 // SOFTWARE.
13 //
14 //==============================================================================
15 #include "hcl.h"
16 #include "hcl_gpio.h"
17 #include "hcl_uart.h"
18 #include "sensor_epsonCommon.h"
19 
20 // These are declared by the main() application for UART IF
21 extern const char* IMUSERIAL; // COM port device name
22 extern int fd_serial; // COM port handle
23 
24 // UART Interface Timing
25 // TWRITERATE/TREADRATE = 200us min @ 460800 BAUD, 1 command = 3 bytes = 3 *
26 // 22us = 66us TSTALL = 200us - 66us = 134us
27 #define EPSON_STALL 134 // Microseconds
28 
29 // UART Byte Markers
30 #ifdef V340PDD0
31 const unsigned char UART_HEADER =
32  0x20; // Placed at the start of all UART cycles
33 #else
34 const unsigned char UART_HEADER =
35  0x80; // Placed at the start of all UART cycles
36 #endif
37 const unsigned char UART_DELIMITER =
38  0x0D; // Placed at the end of all UART cycles
39 
40 static unsigned char rxByteBuf[256]; // COM port receive buffer
41 
42 // Macros & variables used by state machine for processing UART burst read data
43 #define START 0
44 #define DATA 1
45 #define END 2
46 static int state = START;
47 static int data_count = 0;
48 
49 // Flag and variables to store Delta Velocity & Delta Angle scale factors
51 static double dv_scale_factors[16];
52 static double da_scale_factors[16];
53 
54 /*****************************************************************************
55 ** Function name: sensorDataReadyOptions
56 ** Description: For UART interface check if comport recv buffer
57 ** contains a burst of data based on expected byte length
58 ** from sensorDataByteLength()
59 ** Parameters: None
60 ** Return value: OK or NG
61 *****************************************************************************/
62 int sensorDataReadyOptions(struct EpsonOptions options) {
63  seDelayMicroSecs(100);
64  unsigned int count = numBytesReadComPort(fd_serial);
65 
66  if (count >= sensorDataByteLength(options)) return OK;
67  return NG;
68 }
69 
70 /*****************************************************************************
71 ** Function name: registerWriteByteNoID
72 ** Description: Write Byte to Register = Write Data
73 ** to Register (no WIN_ID)
74 ** NOTE: G350/V340 does not have WINDOW_ID function
75 ** winNumber input parameter is ignored
76 ** Parameters: Register Address, Register Write Byte,
77 ** Verbose Flag
78 ** Return value: None
79 *****************************************************************************/
80 void registerWriteByteNoId(unsigned char regAddr, unsigned char regByte,
81  unsigned int verbose) {
82  unsigned char txData[3];
83 
84  txData[0] = regAddr | 0x80; // msb is 1b for register writes
85  txData[1] = regByte;
86  txData[2] = UART_DELIMITER;
87  writeComPort(fd_serial, txData, 3);
88  EpsonStall();
89 
90  if (verbose) {
91  printf("\r\nREG[0x%02X] < 0x%02X\t", regAddr, regByte);
92  }
93 }
94 
95 /*****************************************************************************
96 ** Function name: registerWriteByte
97 ** Description: Write Byte to Register = Set WIN_ID, Write Data
98 ** to Register
99 ** NOTE: G350/V340 does not have WINDOW_ID function
100 ** winNumber input parameter is ignored
101 ** Parameters: Window Number, Register Address, Register Write Byte,
102 ** Verbose Flag
103 ** Return value: None
104 *****************************************************************************/
105 void registerWriteByte(unsigned char winNumber, unsigned char regAddr,
106  unsigned char regByte, unsigned int verbose) {
107 #if !(defined G350 || defined V340PDD0)
108  unsigned char txData[3];
109 
110  txData[0] = ADDR_WIN_CTRL | 0x80; // msb is 1b for register writes
111  txData[1] = winNumber;
112  txData[2] = UART_DELIMITER;
113  writeComPort(fd_serial, txData, 3);
114  EpsonStall();
115 #endif
116 
117  registerWriteByteNoId(regAddr, regByte, verbose);
118 }
119 
120 /*****************************************************************************
121 ** Function name: registerRead16NoId
122 ** Description: Read 16-bit from Register (No WIN_ID)
123 ** Parameters: Register Address, Verbose Flag
124 ** Return value: Register Read Value 16-bit
125 *****************************************************************************/
126 unsigned short registerRead16NoId(unsigned char regAddr, unsigned int verbose) {
127  unsigned char response[4] = {0};
128  int size;
129  unsigned char txData[3];
130 
131  txData[0] =
132  regAddr & 0x7E; // msb is 0b for register reads & address must be even
133  txData[1] = 0x00;
134  txData[2] = UART_DELIMITER;
135  writeComPort(fd_serial, txData, 3);
136 
137  EpsonStall();
138 
139  // Attempt to read 4 bytes from serial port
140  // Validation check: Should be atleast 4 bytes, First byte should be Register
141  // Address,
142  // Last byte should be delimiter
143  size = readComPort(fd_serial, &response[0], 4);
144  EpsonStall();
145 
146  if ((size < 4) || (response[0] != txData[0]) ||
147  (response[3] != UART_DELIMITER)) {
148  printf("Returned less data or unexpected data from previous command.\n");
149  printf("Return data: 0x%02X, 0x%02X, 0x%02X, 0x%02X\n", response[0],
150  response[1], response[2], response[3]);
151  }
152 
153  if (verbose) {
154  printf("REG[0x%02X] > 0x%02X%02X\t", regAddr, response[1], response[2]);
155  }
156  return (unsigned short)response[1] << 8 | (unsigned short)response[2];
157 }
158 
159 /*****************************************************************************
160 ** Function name: registerRead16
161 ** Description: Read 16-bit from Register
162 ** NOTE: G350/V340 does not have WINDOW_ID function
163 ** winNumber input parameter is ignored
164 ** Parameters: Window Number, Register Address, Verbose Flag
165 ** Return value: Register Read Value 16-bit
166 *****************************************************************************/
167 unsigned short registerRead16(unsigned char winNumber, unsigned char regAddr,
168  unsigned int verbose) {
169 #if !(defined G350 || defined V340PDD0)
170  unsigned char txData[3];
171 
172  txData[0] = ADDR_WIN_CTRL | 0x80;
173  txData[1] = winNumber;
174  txData[2] = UART_DELIMITER;
175  writeComPort(fd_serial, txData, 3);
176  EpsonStall();
177 #endif
178 
179  return registerRead16NoId(regAddr, verbose);
180 }
181 
182 /*****************************************************************************
183 ** Function name: populateEpsonData
184 ** Description: Loads Delta Angle/Velocity scale factors the one time
185 ** Retrieves burst data buffer and converts/parses into
186 *struct
187 ** based on configuration.
188 ** Parameters: options - struct describing IMU configuration.
189 ** epson_data - struct that is filled with converted data.
190 ** Return value: none
191 ** Notes:
192 ******************************************************************************/
193 void populateEpsonData(struct EpsonOptions options,
194  struct EpsonData* epson_data) {
195  // If not done so, store Delta Angle/Velocity scale factors
213 
231  }
232 
233  // stores the sensor data array index when parsing out data fields
234  int idx = 0;
235 
236 #ifdef V340PDD0
237  // Fixed packet format except for enabling/disabling count_out
238  unsigned short ndflags = (rxByteBuf[idx] << 8) + rxByteBuf[idx + 1];
239  epson_data->ndflags = ndflags;
240  idx += 2;
241 
242  short temp = (rxByteBuf[idx] << 8) + rxByteBuf[idx + 1];
243  epson_data->temperature = (temp - 2634) * EPSON_TEMP_SF + 25;
244  idx += 2;
245 
246  short gyro_x = (rxByteBuf[idx] << 8) + rxByteBuf[idx + 1];
247  short gyro_y = (rxByteBuf[idx + 2] << 8) + rxByteBuf[idx + 3];
248  short gyro_z = (rxByteBuf[idx + 4] << 8) + rxByteBuf[idx + 5];
249  epson_data->gyro_x = EPSON_GYRO_SF * 3.14159 / 180.0 * gyro_x;
250  epson_data->gyro_y = EPSON_GYRO_SF * 3.14159 / 180.0 * gyro_y;
251  epson_data->gyro_z = EPSON_GYRO_SF * 3.14159 / 180.0 * gyro_z;
252  idx += 6;
253 
254  short accel_x = (rxByteBuf[idx] << 8) + (rxByteBuf[idx] + 1);
255  short accel_y = (rxByteBuf[idx + 2] << 8) + (rxByteBuf[idx + 3]);
256  short accel_z = (rxByteBuf[idx + 4] << 8) + (rxByteBuf[idx + 5]);
257  epson_data->accel_x = (EPSON_ACCL_SF)*9.80665 / 1000.0 * accel_x;
258  epson_data->accel_y = (EPSON_ACCL_SF)*9.80665 / 1000.0 * accel_y;
259  epson_data->accel_z = (EPSON_ACCL_SF)*9.80665 / 1000.0 * accel_z;
260  idx += 6;
261  unsigned short gpio = (rxByteBuf[idx] << 8) + rxByteBuf[idx + 1];
262  idx += 2;
263  epson_data->gpio = gpio;
264 
265 #else
266  // parsing of data fields applying conversion factor if applicable
267  if (options.flag_out) {
268  unsigned short ndflags = (rxByteBuf[idx] << 8) + rxByteBuf[idx + 1];
269  epson_data->ndflags = ndflags;
270  idx += 2;
271 #ifdef DEBUG
272  printf("ndflag: %04x\t", epson_data->ndflags);
273 #endif
274  }
275 
276  if (options.temp_out) {
277  if (options.temp_bit) {
278  int temp = (rxByteBuf[idx] << 8 * 3) + (rxByteBuf[idx + 1] << 8 * 2) +
279  (rxByteBuf[idx + 2] << 8) + rxByteBuf[idx + 3];
280 #if defined G330PDG0 || defined G366PDG0 || defined G370PDG0 || defined G370PDT0
281  epson_data->temperature = (temp)*EPSON_TEMP_SF / 65536 + 25;
282 #else
283  epson_data->temperature = (temp - 172621824) * EPSON_TEMP_SF / 65536 + 25;
284 #endif // defined G330PDG0 || defined G366PDG0 || defined G370PDG0 || defined
285  // G370PDT0
286  idx += 4;
287  } else {
288  short temp = (rxByteBuf[idx] << 8) + rxByteBuf[idx + 1];
289 #if defined G330PDG0 || defined G366PDG0 || defined G370PDG0 || defined G370PDT0
290  epson_data->temperature = (temp)*EPSON_TEMP_SF + 25;
291 #else
292  epson_data->temperature = (temp - 2634) * EPSON_TEMP_SF + 25;
293 #endif // defined G330PDG0 || defined G366PDG0 || defined G370PDG0 || defined
294  // G370PDT0
295  idx += 2;
296  }
297 #ifdef DEBUG
298  printf("tempC: %8.3f\t", epson_data->temperature);
299 #endif
300  }
301 
302  if (options.gyro_out) {
303  if (options.gyro_bit) {
304  int gyro_x = (rxByteBuf[idx] << 8 * 3) + (rxByteBuf[idx + 1] << 8 * 2) +
305  (rxByteBuf[idx + 2] << 8) + rxByteBuf[idx + 3];
306  int gyro_y = (rxByteBuf[idx + 4] << 8 * 3) +
307  (rxByteBuf[idx + 5] << 8 * 2) + (rxByteBuf[idx + 6] << 8) +
308  rxByteBuf[idx + 7];
309  int gyro_z = (rxByteBuf[idx + 8] << 8 * 3) +
310  (rxByteBuf[idx + 9] << 8 * 2) + (rxByteBuf[idx + 10] << 8) +
311  rxByteBuf[idx + 11];
312  epson_data->gyro_x = (EPSON_GYRO_SF / 65536) * 3.14159 / 180.0 * gyro_x;
313  epson_data->gyro_y = (EPSON_GYRO_SF / 65536) * 3.14159 / 180.0 * gyro_y;
314  epson_data->gyro_z = (EPSON_GYRO_SF / 65536) * 3.14159 / 180.0 * gyro_z;
315  idx += 12;
316  } else {
317  short gyro_x = (rxByteBuf[idx] << 8) + rxByteBuf[idx + 1];
318  short gyro_y = (rxByteBuf[idx + 2] << 8) + rxByteBuf[idx + 3];
319  short gyro_z = (rxByteBuf[idx + 4] << 8) + rxByteBuf[idx + 5];
320  epson_data->gyro_x = EPSON_GYRO_SF * 3.14159 / 180.0 * gyro_x;
321  epson_data->gyro_y = EPSON_GYRO_SF * 3.14159 / 180.0 * gyro_y;
322  epson_data->gyro_z = EPSON_GYRO_SF * 3.14159 / 180.0 * gyro_z;
323  idx += 6;
324  }
325 #ifdef DEBUG
326  printf("gx: %8.5f\tgy: %8.5f\tgz: %8.5f\t",
327  epson_data->gyro_x * 180.0 / 3.14159,
328  epson_data->gyro_y * 180.0 / 3.14159,
329  epson_data->gyro_z * 180.0 / 3.14159);
330 #endif
331  }
332 
333  if (options.accel_out) {
334  if (options.accel_bit) {
335  int accel_x = (rxByteBuf[idx] << 8 * 3) + (rxByteBuf[idx + 1] << 8 * 2) +
336  (rxByteBuf[idx + 2] << 8) + rxByteBuf[idx + 3];
337  int accel_y = (rxByteBuf[idx + 4] << 8 * 3) +
338  (rxByteBuf[idx + 5] << 8 * 2) + (rxByteBuf[idx + 6] << 8) +
339  rxByteBuf[idx + 7];
340  int accel_z = (rxByteBuf[idx + 8] << 8 * 3) +
341  (rxByteBuf[idx + 9] << 8 * 2) + (rxByteBuf[idx + 10] << 8) +
342  rxByteBuf[idx + 11];
343  epson_data->accel_x =
344  (EPSON_ACCL_SF / 65536) * 9.80665 / 1000.0 * accel_x;
345  epson_data->accel_y =
346  (EPSON_ACCL_SF / 65536) * 9.80665 / 1000.0 * accel_y;
347  epson_data->accel_z =
348  (EPSON_ACCL_SF / 65536) * 9.80665 / 1000.0 * accel_z;
349  idx += 12;
350  } else {
351  short accel_x = (rxByteBuf[idx] << 8) + rxByteBuf[idx + 1];
352  short accel_y = (rxByteBuf[idx + 2] << 8) + rxByteBuf[idx + 3];
353  short accel_z = (rxByteBuf[idx + 4] << 8) + rxByteBuf[idx + 5];
354  epson_data->accel_x = (EPSON_ACCL_SF)*9.80665 / 1000.0 * accel_x;
355  epson_data->accel_y = (EPSON_ACCL_SF)*9.80665 / 1000.0 * accel_y;
356  epson_data->accel_z = (EPSON_ACCL_SF)*9.80665 / 1000.0 * accel_z;
357  idx += 6;
358  }
359 #ifdef DEBUG
360  printf("ax: %8.5f\tay: %8.5f\taz: %8.5f\t",
361  epson_data->accel_x * 1000 / 9.80665,
362  epson_data->accel_y * 1000 / 9.80665,
363  epson_data->accel_z * 1000 / 9.80665);
364 #endif
365  }
366 
367  if (options.gyro_delta_out) {
368  if (options.gyro_delta_bit) {
369  int gyro_delta_x = (rxByteBuf[idx] << 8 * 3) +
370  (rxByteBuf[idx + 1] << 8 * 2) +
371  (rxByteBuf[idx + 2] << 8) + rxByteBuf[idx + 3];
372  int gyro_delta_y = (rxByteBuf[idx + 4] << 8 * 3) +
373  (rxByteBuf[idx + 5] << 8 * 2) +
374  (rxByteBuf[idx + 6] << 8) + rxByteBuf[idx + 7];
375  int gyro_delta_z = (rxByteBuf[idx + 8] << 8 * 3) +
376  (rxByteBuf[idx + 9] << 8 * 2) +
377  (rxByteBuf[idx + 10] << 8) + rxByteBuf[idx + 11];
378  double da_sf = da_scale_factors[options.dlt_range_ctrl];
379  epson_data->gyro_delta_x = gyro_delta_x * (da_sf) / 65536;
380  epson_data->gyro_delta_y = gyro_delta_y * (da_sf) / 65536;
381  epson_data->gyro_delta_z = gyro_delta_z * (da_sf) / 65536;
382  idx += 12;
383  } else {
384  short gyro_delta_x = (rxByteBuf[idx] << 8) + rxByteBuf[idx + 1];
385  short gyro_delta_y = (rxByteBuf[idx + 2] << 8) + rxByteBuf[idx + 3];
386  short gyro_delta_z = (rxByteBuf[idx + 4] << 8) + rxByteBuf[idx + 5];
387  double da_sf = da_scale_factors[options.dlt_range_ctrl];
388  epson_data->gyro_delta_x = gyro_delta_x * (da_sf);
389  epson_data->gyro_delta_y = gyro_delta_y * (da_sf);
390  epson_data->gyro_delta_z = gyro_delta_z * (da_sf);
391  idx += 6;
392  }
393 #ifdef DEBUG
394  printf("dax: %8.5f\tday: %8.5f\tdaz: %8.5f\t", epson_data->gyro_delta_x,
395  epson_data->gyro_delta_y, epson_data->gyro_delta_z);
396 #endif
397  }
398 
399  if (options.accel_delta_out) {
400  if (options.accel_delta_bit) {
401  int accel_delta_x = (rxByteBuf[idx] << 8 * 3) +
402  (rxByteBuf[idx + 1] << 8 * 2) +
403  (rxByteBuf[idx + 2] << 8) + rxByteBuf[idx + 3];
404  int accel_delta_y = (rxByteBuf[idx + 4] << 8 * 3) +
405  (rxByteBuf[idx + 5] << 8 * 2) +
406  (rxByteBuf[idx + 6] << 8) + rxByteBuf[idx + 7];
407  int accel_delta_z = (rxByteBuf[idx + 8] << 8 * 3) +
408  (rxByteBuf[idx + 9] << 8 * 2) +
409  (rxByteBuf[idx + 10] << 8) + rxByteBuf[idx + 11];
410  double dv_sf = dv_scale_factors[options.dlt_range_ctrl];
411  epson_data->accel_delta_x = accel_delta_x * (dv_sf) / 65536;
412  epson_data->accel_delta_y = accel_delta_y * (dv_sf) / 65536;
413  epson_data->accel_delta_z = accel_delta_z * (dv_sf) / 65536;
414  idx += 12;
415  } else {
416  short accel_delta_x = (rxByteBuf[idx] << 8) + rxByteBuf[idx + 1];
417  short accel_delta_y = (rxByteBuf[idx + 2] << 8) + rxByteBuf[idx + 3];
418  short accel_delta_z = (rxByteBuf[idx + 4] << 8) + rxByteBuf[idx + 5];
419  double dv_sf = dv_scale_factors[options.dlt_range_ctrl];
420  epson_data->accel_delta_x = accel_delta_x * (dv_sf);
421  epson_data->accel_delta_y = accel_delta_y * (dv_sf);
422  epson_data->accel_delta_z = accel_delta_z * (dv_sf);
423  idx += 6;
424  }
425 #ifdef DEBUG
426  printf("dvx: %8.5f\tdvy: %8.5f\tdvz: %8.5f\t", epson_data->accel_delta_x,
427  epson_data->accel_delta_y, epson_data->accel_delta_z);
428 #endif
429  }
430 
431  if (options.qtn_out) {
432  if (options.qtn_bit) {
433  int qtn0 = (rxByteBuf[idx] << 8 * 3) + (rxByteBuf[idx + 1] << 8 * 2) +
434  (rxByteBuf[idx + 2] << 8) + rxByteBuf[idx + 3];
435  int qtn1 = (rxByteBuf[idx + 4] << 8 * 3) + (rxByteBuf[idx + 5] << 8 * 2) +
436  (rxByteBuf[idx + 6] << 8) + rxByteBuf[idx + 7];
437  int qtn2 = (rxByteBuf[idx + 8] << 8 * 3) + (rxByteBuf[idx + 9] << 8 * 2) +
438  (rxByteBuf[idx + 10] << 8) + rxByteBuf[idx + 11];
439  int qtn3 = (rxByteBuf[idx + 12] << 8 * 3) +
440  (rxByteBuf[idx + 13] << 8 * 2) + (rxByteBuf[idx + 14] << 8) +
441  rxByteBuf[idx + 15];
442  epson_data->qtn0 = (double)qtn0 / 1073741824;
443  epson_data->qtn1 = (double)qtn1 / 1073741824;
444  epson_data->qtn2 = (double)qtn2 / 1073741824;
445  epson_data->qtn3 = (double)qtn3 / 1073741824;
446  idx += 16;
447  } else {
448  short qtn0 = (rxByteBuf[idx] << 8) + rxByteBuf[idx + 1];
449  short qtn1 = (rxByteBuf[idx + 2] << 8) + rxByteBuf[idx + 3];
450  short qtn2 = (rxByteBuf[idx + 4] << 8) + rxByteBuf[idx + 5];
451  short qtn3 = (rxByteBuf[idx + 6] << 8) + rxByteBuf[idx + 7];
452  epson_data->qtn0 = (double)qtn0 / 16384;
453  epson_data->qtn1 = (double)qtn1 / 16384;
454  epson_data->qtn2 = (double)qtn2 / 16384;
455  epson_data->qtn3 = (double)qtn3 / 16384;
456  idx += 8;
457  }
458 #ifdef DEBUG
459  printf("qtn0: %8.5f\tqtn1: %8.5f\tqtn2: %8.5f\tqtn3: %8.5f\t",
460  epson_data->qtn0, epson_data->qtn1, epson_data->qtn2,
461  epson_data->qtn3);
462 #endif
463  }
464 
465  if (options.atti_out) {
466  if (options.atti_bit) {
467  int roll = (rxByteBuf[idx] << 8 * 3) + (rxByteBuf[idx + 1] << 8 * 2) +
468  (rxByteBuf[idx + 2] << 8) + rxByteBuf[idx + 3];
469  int pitch = (rxByteBuf[idx + 4] << 8 * 3) +
470  (rxByteBuf[idx + 5] << 8 * 2) + (rxByteBuf[idx + 6] << 8) +
471  rxByteBuf[idx + 7];
472  int yaw = (rxByteBuf[idx + 8] << 8 * 3) + (rxByteBuf[idx + 9] << 8 * 2) +
473  (rxByteBuf[idx + 10] << 8) + rxByteBuf[idx + 11];
474  epson_data->roll = (EPSON_ATTI_SF / 65536) * 3.14159 / 180.0 * roll;
475  epson_data->pitch = (EPSON_ATTI_SF / 65536) * 3.14159 / 180.0 * pitch;
476  epson_data->yaw = (EPSON_ATTI_SF / 65536) * 3.14159 / 180.0 * yaw;
477  idx += 12;
478  } else {
479  short roll = (rxByteBuf[idx] << 8) + rxByteBuf[idx + 1];
480  short pitch = (rxByteBuf[idx + 2] << 8) + rxByteBuf[idx + 3];
481  short yaw = (rxByteBuf[idx + 4] << 8) + rxByteBuf[idx + 5];
482  epson_data->roll = EPSON_ATTI_SF * 3.14159 / 180.0 * roll;
483  epson_data->pitch = EPSON_ATTI_SF * 3.14159 / 180.0 * pitch;
484  epson_data->yaw = EPSON_ATTI_SF * 3.14159 / 180.0 * yaw;
485  idx += 6;
486  }
487 #ifdef DEBUG
488  printf("roll: %8.3f\tpitch: %8.3f\tyaw: %8.3f\t",
489  epson_data->roll * 180.0 / 3.14159,
490  epson_data->pitch * 180.0 / 3.14159,
491  epson_data->yaw * 180.0 / 3.14159);
492 #endif
493  }
494 
495  if (options.gpio_out) {
496  unsigned short gpio = (rxByteBuf[idx] << 8) + rxByteBuf[idx + 1];
497  epson_data->gpio = gpio;
498  idx += 2;
499 #ifdef DEBUG
500  printf("gpio: %04x\t", epson_data->gpio);
501 #endif
502  }
503 
504 #endif
505  if (options.count_out) {
506  int count = (rxByteBuf[idx] << 8) + rxByteBuf[idx + 1];
507  if (options.ext_sel == 1)
508  epson_data->count = count * EPSON_COUNT_SF;
509  else
510  epson_data->count = count;
511 #ifdef DEBUG
512  printf("count: %09d\t", epson_data->count);
513 #endif
514  }
515 }
516 
517 /*****************************************************************************
518 ** Function name: sensorDataReadBurstNOptions
519 ** Description: Retrieves 1 packet from the incoming IMU stream of UART
520 ** based on expected burst length and searching for START
521 ** and END markers. Then calls populateEpsonData() to
522 ** post process into struct.
523 ** Parameters: options - struct describing IMU configuration.
524 ** epson_data - struct that is filled with data.
525 ** Return value: OK or NG
526 ** Notes:
527 ******************************************************************************/
529  struct EpsonData* epson_data) {
530  int byte_length = sensorDataByteLength(options);
531 
532 #ifdef DEBUG
533  printf("Expecting: %d bytes\n", byte_length);
534 #endif
535 
536  int data_length = byte_length - 2; // exclude the START and END markers
537  unsigned char byte;
538 
539  while (readComPort(fd_serial, &byte, 1) > 0) {
540 #ifdef DEBUG
541  // printf("state: %d, byte: 0x%02X\n", state, byte);
542 #endif
543  // State machine to seek out START & END markers and then
544  // call to populateEpsonData()
545  switch (state) {
546  case START:
547  if (byte == UART_HEADER) state = DATA;
548  break;
549  case DATA:
550  rxByteBuf[data_count] = byte;
551  data_count++;
552  if (data_count == data_length) state = END;
553  break;
554  case END:
555  data_count = 0;
556  state = START;
557  if (byte == UART_DELIMITER) {
558 #ifdef DEBUG
559  for (int i = 0; i < data_length; i++) printf("0x%02X ", rxByteBuf[i]);
560  printf("\n");
561 #endif
562 
563 // V340 does not support checksum, so just populate sensor data in structure
564 #ifdef V340PDD0
565  populateEpsonData(options, epson_data);
566  return OK;
567 #endif // V340PDD0
568  // All other supported models support checksum
569  // If checksum enabled, validate
570  // match = populate sensor data structure
571  // no match = print error msg and skip current sensor burst data
572  if (options.checksum_out == 1) {
573  unsigned short checksum = 0;
574  for (int i = 0; i < data_length - 2; i += 2) {
575  checksum += (rxByteBuf[i] << 8) + rxByteBuf[i + 1];
576  }
577  unsigned short epson_checksum =
578  (rxByteBuf[data_length - 2] << 8) + rxByteBuf[data_length - 1];
579 
580  if (checksum == epson_checksum) {
581  populateEpsonData(options, epson_data);
582  return OK;
583  }
584  } else {
585  printf("\nScaling Data");
586  populateEpsonData(options, epson_data);
587  return OK;
588  }
589  printf("checksum NG\n");
590  }
591  // Reaches here means checksum validation NGs
592  return NG;
593  break;
594  default:
595  // Should never get here
596  printf("Invalid State in Read Burst Processing\n");
597  }
598  }
599  // No byte received in serial port yet
600  return NG;
601 }
response
const std::string response
EpsonData::qtn3
float qtn3
Definition: sensor_epsonCommon.h:133
EpsonData::accel_delta_z
float accel_delta_z
Definition: sensor_epsonCommon.h:132
seDelayMicroSecs
void seDelayMicroSecs(uint32_t micros)
Definition: hcl_linux.c:57
EpsonOptions::accel_delta_bit
int accel_delta_bit
Definition: sensor_epsonCommon.h:102
EPSON_DA_SF15
#define EPSON_DA_SF15
Definition: sensor_epsonG320.h:39
EPSON_DA_SF6
#define EPSON_DA_SF6
Definition: sensor_epsonG320.h:30
IMUSERIAL
const char * IMUSERIAL
Definition: main_csvlogger.c:32
sensor_epsonCommon.h
EpsonData::accel_y
float accel_y
Definition: sensor_epsonCommon.h:130
state
static int state
Definition: sensor_epsonUart.c:46
EpsonOptions::ext_sel
int ext_sel
Definition: sensor_epsonCommon.h:72
rxByteBuf
static unsigned char rxByteBuf[256]
Definition: sensor_epsonUart.c:40
EPSON_DV_SF10
#define EPSON_DV_SF10
Definition: sensor_epsonG320.h:51
readComPort
int readComPort(ComPortHandle fd_serial, unsigned char *bytesToRead, int size)
Definition: hcl_uart.c:97
registerRead16
unsigned short registerRead16(unsigned char winNumber, unsigned char regAddr, unsigned int verbose)
Definition: sensor_epsonUart.c:167
EPSON_DA_SF2
#define EPSON_DA_SF2
Definition: sensor_epsonG320.h:26
DATA
#define DATA
Definition: sensor_epsonUart.c:44
EPSON_DV_SF5
#define EPSON_DV_SF5
Definition: sensor_epsonG320.h:46
sensorDataReadBurstNOptions
int sensorDataReadBurstNOptions(struct EpsonOptions options, struct EpsonData *epson_data)
Definition: sensor_epsonUart.c:528
EPSON_DV_SF7
#define EPSON_DV_SF7
Definition: sensor_epsonG320.h:48
registerWriteByteNoId
void registerWriteByteNoId(unsigned char regAddr, unsigned char regByte, unsigned int verbose)
Definition: sensor_epsonUart.c:80
EpsonOptions::temp_bit
int temp_bit
Definition: sensor_epsonCommon.h:98
EPSON_DA_SF9
#define EPSON_DA_SF9
Definition: sensor_epsonG320.h:33
EPSON_ATTI_SF
#define EPSON_ATTI_SF
Definition: sensor_epsonG320.h:21
hcl_gpio.h
EPSON_TEMP_SF
#define EPSON_TEMP_SF
Definition: sensor_epsonG320.h:20
EPSON_DA_SF11
#define EPSON_DA_SF11
Definition: sensor_epsonG320.h:35
EpsonData::accel_x
float accel_x
Definition: sensor_epsonCommon.h:130
EpsonOptions::gpio_out
int gpio_out
Definition: sensor_epsonCommon.h:93
EpsonOptions
Definition: sensor_epsonCommon.h:70
sensorDataReadyOptions
int sensorDataReadyOptions(struct EpsonOptions options)
Definition: sensor_epsonUart.c:62
EPSON_DA_SF13
#define EPSON_DA_SF13
Definition: sensor_epsonG320.h:37
EpsonData::yaw
float yaw
Definition: sensor_epsonCommon.h:134
hcl.h
EpsonOptions::gyro_bit
int gyro_bit
Definition: sensor_epsonCommon.h:99
sensorDataByteLength
unsigned int sensorDataByteLength(struct EpsonOptions options)
Definition: sensor_epsonCommon.c:247
EpsonOptions::qtn_out
int qtn_out
Definition: sensor_epsonCommon.h:90
EpsonOptions::accel_out
int accel_out
Definition: sensor_epsonCommon.h:87
fd_serial
int fd_serial
Definition: epson_imu_uart_driver_node.cpp:71
writeComPort
int writeComPort(ComPortHandle fd_serial, unsigned char *bytesToWrite, int size)
Definition: hcl_uart.c:108
EpsonData::gyro_y
float gyro_y
Definition: sensor_epsonCommon.h:129
EPSON_DV_SF14
#define EPSON_DV_SF14
Definition: sensor_epsonG320.h:55
UART_HEADER
const unsigned char UART_HEADER
Definition: sensor_epsonUart.c:34
END
#define END
Definition: sensor_epsonUart.c:45
registerRead16NoId
unsigned short registerRead16NoId(unsigned char regAddr, unsigned int verbose)
Definition: sensor_epsonUart.c:126
EpsonData::gyro_delta_z
float gyro_delta_z
Definition: sensor_epsonCommon.h:131
EpsonOptions::count_out
int count_out
Definition: sensor_epsonCommon.h:94
EpsonData::gpio
unsigned short gpio
Definition: sensor_epsonCommon.h:135
EpsonOptions::flag_out
int flag_out
Definition: sensor_epsonCommon.h:84
EPSON_DA_SF14
#define EPSON_DA_SF14
Definition: sensor_epsonG320.h:38
EpsonOptions::checksum_out
int checksum_out
Definition: sensor_epsonCommon.h:95
EpsonData
Definition: sensor_epsonCommon.h:126
EpsonOptions::atti_bit
int atti_bit
Definition: sensor_epsonCommon.h:104
scale_factors_initialized
static int scale_factors_initialized
Definition: sensor_epsonUart.c:50
EPSON_DV_SF1
#define EPSON_DV_SF1
Definition: sensor_epsonG320.h:42
EpsonData::count
int count
Definition: sensor_epsonCommon.h:136
EPSON_DV_SF9
#define EPSON_DV_SF9
Definition: sensor_epsonG320.h:50
EPSON_DA_SF4
#define EPSON_DA_SF4
Definition: sensor_epsonG320.h:28
EPSON_DV_SF15
#define EPSON_DV_SF15
Definition: sensor_epsonG320.h:56
EpsonData::accel_z
float accel_z
Definition: sensor_epsonCommon.h:130
EPSON_ACCL_SF
#define EPSON_ACCL_SF
Definition: sensor_epsonG320.h:18
EpsonData::ndflags
unsigned short ndflags
Definition: sensor_epsonCommon.h:127
START
#define START
Definition: sensor_epsonUart.c:43
EpsonData::qtn1
float qtn1
Definition: sensor_epsonCommon.h:133
EPSON_DV_SF11
#define EPSON_DV_SF11
Definition: sensor_epsonG320.h:52
EPSON_DA_SF10
#define EPSON_DA_SF10
Definition: sensor_epsonG320.h:34
EPSON_DA_SF8
#define EPSON_DA_SF8
Definition: sensor_epsonG320.h:32
EPSON_DA_SF0
#define EPSON_DA_SF0
Definition: sensor_epsonG320.h:24
da_scale_factors
static double da_scale_factors[16]
Definition: sensor_epsonUart.c:52
EPSON_DA_SF5
#define EPSON_DA_SF5
Definition: sensor_epsonG320.h:29
EpsonData::qtn0
float qtn0
Definition: sensor_epsonCommon.h:133
EPSON_DV_SF0
#define EPSON_DV_SF0
Definition: sensor_epsonG320.h:41
EpsonData::gyro_delta_x
float gyro_delta_x
Definition: sensor_epsonCommon.h:131
UART_DELIMITER
const unsigned char UART_DELIMITER
Definition: sensor_epsonUart.c:37
EpsonData::gyro_x
float gyro_x
Definition: sensor_epsonCommon.h:129
EPSON_DA_SF1
#define EPSON_DA_SF1
Definition: sensor_epsonG320.h:25
EPSON_DV_SF2
#define EPSON_DV_SF2
Definition: sensor_epsonG320.h:43
EPSON_DA_SF12
#define EPSON_DA_SF12
Definition: sensor_epsonG320.h:36
EpsonOptions::atti_out
int atti_out
Definition: sensor_epsonCommon.h:91
EpsonOptions::gyro_delta_out
int gyro_delta_out
Definition: sensor_epsonCommon.h:88
EpsonOptions::gyro_out
int gyro_out
Definition: sensor_epsonCommon.h:86
dv_scale_factors
static double dv_scale_factors[16]
Definition: sensor_epsonUart.c:51
EpsonData::temperature
float temperature
Definition: sensor_epsonCommon.h:128
EPSON_COUNT_SF
#define EPSON_COUNT_SF
Definition: sensor_epsonG320.h:22
EpsonData::pitch
float pitch
Definition: sensor_epsonCommon.h:134
ADDR_WIN_CTRL
#define ADDR_WIN_CTRL
Definition: sensor_epsonG320.h:139
EpsonOptions::temp_out
int temp_out
Definition: sensor_epsonCommon.h:85
EpsonOptions::accel_bit
int accel_bit
Definition: sensor_epsonCommon.h:100
numBytesReadComPort
int numBytesReadComPort(ComPortHandle fd_serial)
Definition: hcl_uart.c:120
EPSON_DA_SF7
#define EPSON_DA_SF7
Definition: sensor_epsonG320.h:31
NG
#define NG
Definition: hcl.h:33
EpsonData::gyro_z
float gyro_z
Definition: sensor_epsonCommon.h:129
EPSON_DV_SF3
#define EPSON_DV_SF3
Definition: sensor_epsonG320.h:44
EpsonStall
#define EpsonStall()
Definition: sensor_epsonCommon.h:68
EpsonData::roll
float roll
Definition: sensor_epsonCommon.h:134
EpsonOptions::accel_delta_out
int accel_delta_out
Definition: sensor_epsonCommon.h:89
EpsonData::gyro_delta_y
float gyro_delta_y
Definition: sensor_epsonCommon.h:131
EPSON_DV_SF6
#define EPSON_DV_SF6
Definition: sensor_epsonG320.h:47
EPSON_DV_SF4
#define EPSON_DV_SF4
Definition: sensor_epsonG320.h:45
EpsonData::qtn2
float qtn2
Definition: sensor_epsonCommon.h:133
EPSON_DA_SF3
#define EPSON_DA_SF3
Definition: sensor_epsonG320.h:27
EpsonOptions::qtn_bit
int qtn_bit
Definition: sensor_epsonCommon.h:103
data_count
static int data_count
Definition: sensor_epsonUart.c:47
EpsonData::accel_delta_x
float accel_delta_x
Definition: sensor_epsonCommon.h:132
EPSON_GYRO_SF
#define EPSON_GYRO_SF
Definition: sensor_epsonG320.h:19
OK
#define OK
Definition: hcl.h:29
EpsonData::accel_delta_y
float accel_delta_y
Definition: sensor_epsonCommon.h:132
EpsonOptions::gyro_delta_bit
int gyro_delta_bit
Definition: sensor_epsonCommon.h:101
registerWriteByte
void registerWriteByte(unsigned char winNumber, unsigned char regAddr, unsigned char regByte, unsigned int verbose)
Definition: sensor_epsonUart.c:105
EPSON_DV_SF13
#define EPSON_DV_SF13
Definition: sensor_epsonG320.h:54
EPSON_DV_SF12
#define EPSON_DV_SF12
Definition: sensor_epsonG320.h:53
EPSON_DV_SF8
#define EPSON_DV_SF8
Definition: sensor_epsonG320.h:49
EpsonOptions::dlt_range_ctrl
int dlt_range_ctrl
Definition: sensor_epsonCommon.h:116
populateEpsonData
void populateEpsonData(struct EpsonOptions options, struct EpsonData *epson_data)
Definition: sensor_epsonUart.c:193
hcl_uart.h


ess_imu_ros1_uart_driver
Author(s):
autogenerated on Sun Dec 3 2023 03:11:33