main_csvlogger.c
Go to the documentation of this file.
1 //==============================================================================
2 //
3 // main_csvlogger.c - Epson IMU sensor test application
4 // - This program initializes the Epson IMU and
5 // sends sensor output to CSV file
6 //
7 //
8 // THE SOFTWARE IS RELEASED INTO THE PUBLIC DOMAIN.
9 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
10 // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT,
11 // SECURITY, SATISFACTORY QUALITY, AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
12 // SHALL EPSON BE LIABLE FOR ANY LOSS, DAMAGE OR CLAIM, ARISING FROM OR IN CONNECTION
13 // WITH THE SOFTWARE OR THE USE OF THE SOFTWARE.
14 //
15 //==============================================================================
16 #include <stdint.h>
17 #include <stdio.h>
18 #include <time.h>
19 
20 #include "hcl.h"
21 #include "hcl_gpio.h"
22 #include "sensor_epsonCommon.h"
23 
24 #include <termios.h> // Low-level functions for UART communication
25 #include "hcl_uart.h"
26 int comPort;
27 // Modify below as needed for hardware
28 const char *IMUSERIAL = "/dev/ttyUSB0";
29 const int IMUBAUD = B460800;
30 
31 // Determines the number of samples to readout before exiting the program
32 const unsigned int NUM_SAMPLES = 1000;
33 
34 int main(int argc, char *argv[])
35 {
36  int i;
37  unsigned int sample = 0;
38  signed short readData[SENSOR_READ_LEN];
39  double ppSensorData[6];
40  unsigned short chksum16_ver;
41 
42  // 1) Initialize the Seiko Epson HCL layer
43  printf("\r\nInitializing HCL layer...");
44  if (!seInit()){
45  printf("\r\nError: could not initialize the Seiko Epson HCL layer. Exiting...\r\n");
46  return -1;
47  }
48  printf("...done.\r\n");
49 
50  // 2) Initialize the GPIO interfaces, For GPIO control of pins SPI CS, RESET, DRDY
51  printf("\r\nInitializing GPIO interface...");
52  if (!gpioInit()){
53  printf("\r\nError: could not initialize the GPIO layer. Exiting...\r\n");
54  return -1;
55  }
56  printf("...done.");
57 
58  // 3) Initialize UART Interface
59  printf("\r\nInitializing UART interface...");
61  if(comPort == -1)
62  {
63  printf("\r\nError: could not initialize UART interface. Exiting...\r\n");
64  return -1;
65  }
66  printf("...done.");
67 
68  // 4) Power on sequence - force sensor to config mode, HW reset sensor
69  // Check for errors
70  printf("\r\nChecking sensor NOT_READY status...");
71  if(!sensorPowerOn())
72  {
73  printf("\r\nError: failed to power on Sensor. Exiting...\r\n");
75  return -1;
76  }
77  printf("...done.");
78 
79  // Initialize sensor with desired settings
80  printf("\r\nInitializing Sensor...");
81  if(!sensorInit())
82  {
83  printf("\r\nError: could not initialize Epson Sensor. Exiting...\r\n");
85  return -1;
86  }
87  else
88  {
89  printf("...Epson IMU initialized.");
90  }
91  // Initialize text files for data logs
92  const time_t date = time(NULL); // Functions for obtaining and printing time and date
93  struct tm tm = *localtime(&date);
94  char EpsonlogName[128];
95  char EpsonRawName[128];
96 
97  // Create Epson IMU Data Log
98  sprintf(EpsonlogName, "EpsonLog-%4d-%02d-%02d_T%d:%d:%d.csv", tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
99  FILE *EpsonLog = fopen(EpsonlogName, "w");
100  fprintf(EpsonLog,"Date: ");
101  fprintf(EpsonLog, "%s", ctime(&date));
102  fprintf(EpsonLog,"\r\nEpson IMU\r\nSAMPLE,\tXGYRO,\tYGYRO,\tZGYRO,\tXACCL,\tYACCL,\tZACCL,\tCount");
103 
104  //sprintf(EpsonRawName, "EpsonRaw-%d-%d-%dT%d%d%d.csv", tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
105  //FILE *EpsonRaw = fopen(EpsonRawName, "w");
106  //fprintf(EpsonRaw,"Date: ");
107  //fprintf(EpsonRaw, "%s", ctime(&date));
108 
109 #if defined G350 || defined V340
110  //fprintf(EpsonRaw,"Epson IMU\r\nSAMPLE,\tND_FLAG,\tTEMPC,\tX_GYRO,\tY_GYRO,\tZ_GYRO,\tX_ACCL,\tY_ACCL,\tZ_ACCL,\tGPIO,\tCOUNT");
111 #else //G354/G364/G352/G362/G320
112  //fprintf(EpsonRaw,"\r\nEpson M-IMU\r\nSAMPLE,\tX_GYRO1,\tX_GYRO0,\tY_GYRO1,\tY_GYRO0,\tZ_GYRO1,\tZ_GYRO0,\tX_ACCL1,\tX_ACCL0,\tY_ACCL1,\tY_ACCL0,\tZ_ACCL1,\tZ_ACCL0,\tCOUNT,\tIMU_CHKSM16,\tHOST_CHKSM16");
113 #endif
114 
115  printf("\r\n...Epson IMU Logging.\r\n");
116  sensorStart();
117 
118  while (1)
119  {
120  // For SPI interface, check if DRDY pin asserted
121  // For UART interface, check if UART recv buffer contains a sensor sample packet
122  if(sensorDataReady())
123  {
124 #if defined G350 || defined V340 // 16-bit Sensor Output
126 
127  ppSensorDataRead16N(ppSensorData, readData, 2); // GyroX is located at index 2 of readData
128  //fprintf(EpsonRaw, "\r\n%u,\t0x%04hX,\t0x%04hX,\t0x%04hX,\t0x%04hX,\t0x%04hX,\t0x%04hX,\t0x%04hX,\t0x%04hX,\t0x%04hX,\t0x%04hX", sample, readData[0], readData[1], readData[2], readData[3], readData[4], readData[5], readData[6], readData[7], readData[8], (unsigned short)readData[9]);
129  fprintf(EpsonLog, "\r\n%u,\t%+08f,\t%+08f,\t%+08f,\t%+08f,\t%+08f,\t%+08f,\t%5d", sample, ppSensorData[0], ppSensorData[1], ppSensorData[2], ppSensorData[3], ppSensorData[4], ppSensorData[5], (unsigned short)readData[9]);
130 #endif
131 
132 #if defined G354 || defined G364 || defined G352 || defined G362 || defined G320 // 32-bit Sensor Output
134  unsigned short tmp16 = calChecksum16(readData, SENSOR_READ_LEN-1);
135  ppSensorDataRead32N(ppSensorData, readData, 0);
136 
137  fprintf(EpsonLog, "\r\n%u,\t%+08f,\t%+08f,\t%+08f,\t%+08f,\t%+08f,\t%+08f,\t%5d,\t0x%04hX,\t0x%04hX", sample, ppSensorData[0], ppSensorData[1], ppSensorData[2], ppSensorData[3], ppSensorData[4], ppSensorData[5], (unsigned short)readData[12],
138  (unsigned short)readData[13], tmp16);
139  if (tmp16 == (unsigned short)readData[SENSOR_READ_LEN-1])
140  fprintf(EpsonLog,", OK");
141  else
142  fprintf(EpsonLog,", ERROR");
143 #endif
144 
145  sample++;
146  }
147  if (sample > (NUM_SAMPLES-1))
148  break;
149  }
150 
151  const time_t end = time(NULL); // Functions for obtaining and printing time and data
152  tm = *localtime(&end);
153  fprintf(EpsonLog, "\r\nEnd: ");
154  fprintf(EpsonLog, "%s", ctime(&end));
155  //fprintf(EpsonRaw, "\r\nEnd: ");
156  //fprintf(EpsonRaw, "%s", ctime(&end));
157 
158  sensorStop();
159  sleep(1);
161  gpioRelease();
162  seRelease();
163  fclose(EpsonLog);
164  //fclose(EpsonRaw);
165  printf("\r\nThe following arguments were passed to main(): ");
166  for(i=1; i<argc; i++) printf("%s ", argv[i]);
167  printf("\r\n");
168  return 0;
169 }
#define SENSOR_READ_LEN
int seInit(void)
Definition: hcl_linux.c:28
int gpioInit(void)
Definition: hcl_gpio.c:29
int sensorDataReady(void)
int uartInit(const char *comPortPath, int baudRate)
Definition: hcl_uart.c:222
void sensorStop(void)
int gpioRelease(void)
Definition: hcl_gpio.c:41
int comPort
unsigned short calChecksum16(unsigned short sensorReadData[], unsigned int endOffset)
const unsigned int NUM_SAMPLES
int uartRelease(ComPortHandle comPort)
Definition: hcl_uart.c:236
void ppSensorDataRead32N(double ppSensorReadData[], signed short sensorReadData[], unsigned char startIndex)
int sensorInit(void)
void sensorStart(void)
const int IMUBAUD
int seRelease(void)
Definition: hcl_linux.c:42
void sensorDataReadBurstN(signed short[], unsigned int)
int sensorPowerOn(void)
const char * IMUSERIAL
int main(int argc, char *argv[])
void ppSensorDataRead16N(double ppSensorReadData[], signed short sensorReadData[], unsigned char startIndex)


epson_g364_imu_driver
Author(s):
autogenerated on Mon Jun 10 2019 13:12:32