COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
test_imu.cpp File Reference
#include <termios.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#include <iostream>
Include dependency graph for test_imu.cpp:

Classes

struct  EulerAngles
 

Macros

#define PI   (3.141592653589793)
 

Typedefs

typedef int ComPortHandle
 
typedef unsigned char Byte
 

Functions

int TestByteOrder ()
 
short convert2short (unsigned char *buffer)
 
unsigned short convert2ushort (unsigned char *buffer)
 
float FloatFromBytes (const unsigned char *pBytes)
 
unsigned short i3dmgx2_Checksum (const unsigned char *pBytes, int count)
 
bool Purge (ComPortHandle comPortHandle)
 
ComPortHandle OpenComPort (const char *comPortPath)
 
void CloseComPort (ComPortHandle comPort)
 
int readComPort (ComPortHandle comPort, Byte *bytes, int bytesToRead)
 
int writeComPort (ComPortHandle comPort, const Byte *bytesToWrite, int size)
 
bool ReadEulerAngles (ComPortHandle comPort, EulerAngles &eulerAngles)
 
int main (int argc, char *argv[])
 

Macro Definition Documentation

#define PI   (3.141592653589793)

Typedef Documentation

typedef int ComPortHandle
typedef unsigned char Byte

Function Documentation

int TestByteOrder ( )
49 {
50  short int word = 0x0001;
51  char *byte = (char *) &word;
52  return(byte[0] ? LITTLE_ENDIAN : BIG_ENDIAN);
53 }
short convert2short ( unsigned char *  buffer)
63  {
64  short x;
65  if(TestByteOrder() != BIG_ENDIAN) {
66  x = (buffer[0] <<8) + (buffer[1] & 0xFF);
67  }else{
68  x = (short)buffer;
69  }
70  return x;
71 }
static char buffer[255]
Definition: propagator_simple.cpp:60
x
Definition: inputfile.py:6
int TestByteOrder()
Definition: test_imu.cpp:48
unsigned short convert2ushort ( unsigned char *  buffer)
79  {
80  unsigned short x;
81  if(TestByteOrder() != BIG_ENDIAN) {
82  x = (buffer[0] <<8) + (buffer[1] & 0xFF);
83  }else{
84  x = (unsigned short)buffer;
85  }
86  return x;
87 }
static char buffer[255]
Definition: propagator_simple.cpp:60
x
Definition: inputfile.py:6
int TestByteOrder()
Definition: test_imu.cpp:48
float FloatFromBytes ( const unsigned char *  pBytes)
98 {
99  float f = 0;
100  if(TestByteOrder() != BIG_ENDIAN) {
101  ((Byte*)(&f))[0] = pBytes[3];
102  ((Byte*)(&f))[1] = pBytes[2];
103  ((Byte*)(&f))[2] = pBytes[1];
104  ((Byte*)(&f))[3] = pBytes[0];
105  }else{
106  ((Byte*)(&f))[0] = pBytes[0];
107  ((Byte*)(&f))[1] = pBytes[1];
108  ((Byte*)(&f))[2] = pBytes[2];
109  ((Byte*)(&f))[3] = pBytes[3];
110  }
111 
112  return f;
113 }
unsigned char Byte
Definition: test_imu.cpp:41
int TestByteOrder()
Definition: test_imu.cpp:48
unsigned short i3dmgx2_Checksum ( const unsigned char *  pBytes,
int  count 
)
128 {
129  unsigned short i3_checksum = 0;
130  int i = 0;
131 
132  for(i = 0; i < count; ++i)
133  {
134  i3_checksum+=pBytes[i];
135  }
136 
137  return i3_checksum;
138 }
int i
Definition: rw_test.cpp:37
int count
Definition: rw_test.cpp:36
bool Purge ( ComPortHandle  comPortHandle)
153 {
154  if (tcflush(comPortHandle,TCIOFLUSH)==-1)
155  {
156  cerr << "flush failed" << endl;
157  return false;
158  }
159 
160  return true;
161 }
ComPortHandle OpenComPort ( const char *  comPortPath)
171 {
172  ComPortHandle comPort = open(comPortPath, O_RDWR | O_NOCTTY);
173 
174  if (comPort== -1) //Opening of port failed
175  {
176  cerr << "Unable to open com Port\n"
177  "Error:(" << errno << ") " << strerror(errno) << endl;
178  return -1;
179  }
180 
181  //Get the current options for the port...
182  termios options;
183  tcgetattr(comPort, &options);
184 
185  //set the baud rate to 115200
186  int baudRate = B115200;
187  cfsetospeed(&options, baudRate);
188  cfsetispeed(&options, baudRate);
189 
190  //set the number of data bits.
191  options.c_cflag &= ~CSIZE; // Mask the character size bits
192  options.c_cflag |= CS8;
193 
194  //set the number of stop bits to 1
195  options.c_cflag &= ~CSTOPB;
196 
197  //Set parity to None
198  options.c_cflag &=~PARENB;
199 
200  //set for non-canonical (raw processing, no echo, etc.)
201  options.c_iflag = IGNPAR; // ignore parity check close_port(int
202  options.c_oflag = 0; // raw output
203  options.c_lflag = 0; // raw input
204 
205  //Time-Outs -- won't work with NDELAY option in the call to open
206  options.c_cc[VMIN] = 0; // block reading until RX x characers. If x = 0, it is non-blocking.
207  options.c_cc[VTIME] = 10; // Inter-Character Timer -- i.e. timeout= x*.1 s
208 
209  //Set local mode and enable the receiver
210  options.c_cflag |= (CLOCAL | CREAD);
211 
212  //Purge serial port buffers
213  Purge(comPort);
214 
215  //Set the new options for the port...
216  int status=tcsetattr(comPort, TCSANOW, &options);
217 
218  if (status != 0) //For error message
219  {
220  cerr << "Configuring comport failed" << endl;
221  return status;
222  }
223 
224  //Purge serial port buffers
225  Purge(comPort);
226 
227  return comPort;
228 }
bool Purge(ComPortHandle comPortHandle)
Definition: test_imu.cpp:152
int ComPortHandle
Definition: test_imu.cpp:40
void CloseComPort ( ComPortHandle  comPort)
237 {
238  close(comPort);
239 }
int readComPort ( ComPortHandle  comPort,
Byte bytes,
int  bytesToRead 
)
247 {
248  int bytesRead = read(comPort, bytes, bytesToRead);
249 }
int writeComPort ( ComPortHandle  comPort,
const Byte bytesToWrite,
int  size 
)
257 {
258  return write(comPort, bytesToWrite, size);
259 }
bool ReadEulerAngles ( ComPortHandle  comPort,
EulerAngles eulerAngles 
)
281 {
282  static const Byte COMMAND_BYTE = 0xCE;
283  writeComPort(comPort, &COMMAND_BYTE, 1);
284 
285  static const int RESPONSE_SIZE = 19;
286  Byte response[RESPONSE_SIZE] = {0};
287  int size = readComPort(comPort, &response[0], RESPONSE_SIZE);
288 
289 
290  //cout << size << endl;
291  //cout << RESPONSE_SIZE << endl;
292  //must get all the bytes we want, or it's not a valid read
293  if(size != RESPONSE_SIZE)
294  {
295  cerr << "Invalid response size" << endl;
296  return false;
297  }
298 
299  // verify first byte matches the command byte
300  if(response[0] != COMMAND_BYTE)
301  {
302  cout << response[0] << COMMAND_BYTE << endl;
303  cerr << "Invalid response" << endl;
304  return false;
305  }
306 
307  //Verify the checksum
308  short responseChecksum = convert2ushort(&response[RESPONSE_SIZE-2]);
309  short calculatedChecksum = i3dmgx2_Checksum(&response[0], RESPONSE_SIZE-2);
310 
311  if(calculatedChecksum != responseChecksum)
312  {
313  cerr << "calculatedChecksum" << calculatedChecksum << endl;
314  cerr << "responseChecksum" << responseChecksum << endl;
315  cerr << "Invalid Checksum" << endl;
316  return false;
317  }
318 
319  //conversion factor used to convert the returned values to degrees
320  static const float SCALE_AS_DEGREES = 180.0/PI;
321 
322  eulerAngles.roll = FloatFromBytes(&response[1])*SCALE_AS_DEGREES;
323  eulerAngles.pitch = FloatFromBytes(&response[5])*SCALE_AS_DEGREES;
324  eulerAngles.yaw = FloatFromBytes(&response[9])*SCALE_AS_DEGREES;
325 
326  return true;
327 }
int readComPort(ComPortHandle comPort, Byte *bytes, int bytesToRead)
Definition: test_imu.cpp:246
unsigned short i3dmgx2_Checksum(const unsigned char *pBytes, int count)
Definition: test_imu.cpp:127
unsigned char Byte
Definition: test_imu.cpp:41
int writeComPort(ComPortHandle comPort, const Byte *bytesToWrite, int size)
Definition: test_imu.cpp:256
float FloatFromBytes(const unsigned char *pBytes)
Definition: test_imu.cpp:97
float roll
Definition: test_imu.cpp:271
float pitch
Definition: test_imu.cpp:272
#define PI
Definition: test_imu.cpp:37
unsigned short convert2ushort(unsigned char *buffer)
Definition: test_imu.cpp:79
float yaw
Definition: test_imu.cpp:273
int main ( int  argc,
char *  argv[] 
)
334 {
335  //open the comport
336  printf("Opening comport...\n");
337  ComPortHandle comPort = OpenComPort("/dev/ttyUSB0");
338  if(comPort > 0)
339  {
340 
341  //every second get the euler angles from the sensor, and print the results
342  cout<<"Roll,Pitch,Yaw"<<endl;
343  while(true)
344  {
345  sleep(1);
346  EulerAngles eulerAngles;
347  if(ReadEulerAngles(comPort, eulerAngles))
348  {
349  cout << eulerAngles.roll << "," << eulerAngles.pitch << "," << eulerAngles.yaw << endl;
350  //printf("Roll:%0.2f Pitch:%0.2f Yaw:%0.2f\n", eulerAngles.roll, eulerAngles.pitch, eulerAngles.yaw);
351  }
352  }
353 
354 
355  CloseComPort(comPort);
356  }
357 
358 
359  return 0;
360 }
int ComPortHandle
Definition: test_imu.cpp:40
ComPortHandle OpenComPort(const char *comPortPath)
Definition: test_imu.cpp:170
float roll
Definition: test_imu.cpp:271
float pitch
Definition: test_imu.cpp:272
void CloseComPort(ComPortHandle comPort)
Definition: test_imu.cpp:236
float yaw
Definition: test_imu.cpp:273
bool ReadEulerAngles(ComPortHandle comPort, EulerAngles &eulerAngles)
Definition: test_imu.cpp:280
Definition: test_imu.cpp:269