Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages | Examples

vos/corelibs/tracking/intersenseorientation.cc

Go to the documentation of this file.
00001 /*
00002     This file is part of the Virtual Object System of
00003     the Interreality project (http://interreality.org).
00004 
00005     Copyright (C) 2001, 2002 Peter Amstutz
00006 
00007     This software was written at the University of Massachusetts with
00008     support from NSF grant #EIA 9703217
00009 
00010     This library is free software; you can redistribute it and/or
00011     modify it under the terms of the GNU Lesser General Public
00012     License as published by the Free Software Foundation; either
00013     version 2 of the License, or (at your option) any later version.
00014 
00015     This library is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018     Lesser General Public License for more details.
00019 
00020     You should have received a copy of the GNU Lesser General Public
00021     License along with this library; if not, write to the Free Software
00022     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00023 
00024     Peter Amstutz <tetron@interreality.org>
00025 */
00026 
00027 /*
00028    setting serial port options with tcsettr is covered in the Linux Serial Programming HOWTO:
00029    http://linuxdoc.org/HOWTO/Serial-Programming-HOWTO.html
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); /* save current serial port settings */
00063     memset(&newtio, 0, sizeof(newtio)); /* clear struct for new port settings */
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;   /* inter-character timer */
00086 //  newtio.c_cc[VMIN]     = 1;   /* blocking read until at least 1 char received */
00087 
00088     tcflush(fd, TCIFLUSH);
00089     tcsetattr(fd,TCSANOW,&newtio);
00090 
00091     filep=fdopen(fd, "r+");
00092 
00093     // specify that we're only interested in the orientation cosines
00094     write(fd, "\r\n\r\n", 4); // get us out of any inconsistant protocol states
00095     write(fd, "c", 1); // set to polled mode
00096     write(fd, "O1,4,0,5,0,6,0,7,1\r\n", 20); // specify we want the cosines to fill in our matrix
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     /* let's see, matrices are generally indexed [row][column] */
00109     int station;
00110     double t;
00111     static double m[3][3];  // use a temporary, just to be safe. necesary?
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;  // use a temporary, just to be safe. necesary?
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 

Generated on Tue Aug 12 03:55:38 2003 for Interreality Project - VOS by doxygen 1.3.2