00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <sys/types.h>
00033 #include <sys/stat.h>
00034 #include <fcntl.h>
00035
00036 #include <termios.h>
00037
00038 #include <stdio.h>
00039 #include <string.h>
00040 #include <unistd.h>
00041 #include <errno.h>
00042
00043 #include <string>
00044
00045 #include "intersenseorientation.hh"
00046
00047 using namespace std;
00048
00049 IntersenseOrientation::~IntersenseOrientation()
00050 {
00051 tcsetattr(fd,TCSANOW,&oldtio);
00052 }
00053
00054 IntersenseOrientation::IntersenseOrientation(const char* device, int baud) throw (DeviceError)
00055 {
00056 fd = open(device, O_RDWR | O_NOCTTY );
00057 if (fd < 0) {
00058 string err("Cannot open device: ");
00059 err += strerror(errno);
00060 throw DeviceError(err.c_str());
00061 }
00062 tcgetattr(fd, &oldtio);
00063 memset(&newtio, 0, sizeof(newtio));
00064
00065 int baudflag=0;
00066 switch(baud) {
00067 case 9600:
00068 baudflag = B9600;
00069 break;
00070 case 19200:
00071 baudflag = B19200;
00072 break;
00073 case 38400:
00074 baudflag = B38400;
00075 break;
00076 case 115200:
00077 baudflag = B115200;
00078 break;
00079 }
00080
00081 newtio.c_cflag = baudflag | CRTSCTS | CS8 | CLOCAL | CREAD;
00082 newtio.c_iflag = IGNPAR;
00083 newtio.c_oflag = 0;
00084 newtio.c_lflag = 0;
00085 newtio.c_cc[VTIME] = 1;
00086
00087
00088 tcflush(fd, TCIFLUSH);
00089 tcsetattr(fd,TCSANOW,&newtio);
00090
00091 filep=fdopen(fd, "r+");
00092
00093
00094 write(fd, "\r\n\r\n", 4);
00095 write(fd, "c", 1);
00096 write(fd, "O1,4,0,5,0,6,0,7,1\r\n", 20);
00097 }
00098
00099 void IntersenseOrientation::getOrientationMatrix(double mat[3][3])
00100 throw(DeviceError)
00101 {
00102 write(fd, "P", 1);
00103
00104 char info[256];
00105 fgets(info, sizeof(info), filep);
00106
00107
00108
00109 int station;
00110 double t;
00111 static double m[3][3];
00112 if(sscanf(info, "%i %lf%lf%lf %lf%lf%lf %lf%lf%lf %lf%lf%lf",
00113 &station, &t, &t, &t,
00114 &m[0][0], &m[0][1], &m[0][2],
00115 &m[1][0], &m[1][1], &m[1][2],
00116 &m[2][0], &m[2][1], &m[2][2]) < 13) {
00117 throw DeviceError("Misparse of data from Intersense IS300\n");
00118 }
00119 for(int i = 0; i < 3; i++)
00120 for(int j = 0; j < 3; j++)
00121 mat[i][j] = m[i][j];
00122 }
00123
00124 void IntersenseOrientation::getEulerAngles(double* yaw, double* pitch, double* roll) throw(DeviceError)
00125 {
00126 char info[256];
00127 static int station;
00128 static double y, p, r;
00129 write(fd, "P", 1);
00130 fgets(info, sizeof(info), filep);
00131 if(sscanf(info, "%i %lf%lf%lf",
00132 &station,
00133 &y, &p, &r) < 4) {
00134 throw(DeviceError("Misparse of data from Intersense IS-300 device"));
00135 } else {
00136 *yaw = y + eulerYawOffset;
00137 *pitch = p + eulerPitchOffset;
00138 *roll = r + eulerRollOffset;
00139 }
00140 }
00141
00142