/Users/craigcornelius/Projects/SPRING Mac Release 0.2/intersense.cpp

Go to the documentation of this file.
00001 // $Id: intersense.cpp,v 1.9 2001/10/17 16:49:35 kevin Exp $
00002 //
00003 //  Driver for Intersense InterTrax30 inertial tracker
00004 //
00005 
00006 #include "intersense.h"
00007 #include <stdio.h>
00008 #include <math.h>
00009 
00010 #ifdef _WIN32
00011 #define M_PI 3.1415927
00012 #endif
00013 
00014 const char* Intersense::rcsid = "@(#) $Id: intersense.cpp,v 1.9 2001/10/17 16:49:35 kevin Exp $ $Copyright: (c)2001 National Biocomputation Center, Stanford University $";
00015 int Intersense::debug = 0;
00016 
00017 short Intersense::get_shorty()
00018 {
00019     short val;
00020     GetBuffer((char*)&val, 2);
00021     
00022 #ifdef _WIN32
00023     return(val);
00024 #else
00025     // swab bytes (little endian-big endian thing)
00026     return( ((val & 0x00ff) << 8) | ((val & 0xff00) >> 8) ); 
00027 #endif
00028     
00029 }
00030 
00031 Intersense::Intersense(char* port_name, int baud)
00032 : Sensor(Sensor::isense_sensor), SerialDevice(port_name, baud)
00033 {
00034     if (debug) 
00035         cerr << "Intersense::constructor(" << port_name << ", " << baud << ")\n";
00036     
00037     // setup for transmit at 1200 bps
00038     // Note: Documentation sez to start at 1200bps, initialize, then jump to
00039     //  38400 during data mode, but it looks like it inits fine at 38400
00040     
00041     // flush input buffer to clear out any leftovers
00042     FlushInputQueue();
00043     
00044     // command: send "command-mode" command ('c')
00045     SendByte('c');
00046     
00047     // ack: read back the acknowledgement ('o')
00048     while (GetByte() != 'o') ;
00049     
00050     { // get initialization info
00051         
00052         // command: read version info ("v\000" for version of device 0)
00053         SendByte('v'); SendByte('\000');
00054         
00055         // ack: read back acknowledged "v\000"
00056         char ackbuf[2];
00057         GetBuffer((char*)&ackbuf, 2);
00058         
00059         // read device type (short)- if 0, is normal
00060         short device_type = get_shorty();
00061         
00062         // read firmware version (short)- divide by 100
00063         short version = get_shorty()/100;
00064         
00065         // read product string (50 characters)
00066         char prodid[50];
00067         GetBuffer((char*)&prodid, 50);
00068         
00069         // command: read number of devices present ('p')
00070         SendByte('p');
00071         
00072         // ack: read number of devices present (char)
00073         char numdevs;
00074         GetBuffer(&numdevs, 1);
00075         
00076         // if in debug mode, print info
00077         if (debug) {
00078             cerr << "device_type = " << device_type << endl;
00079             cerr << "version = " << version << endl;
00080             cerr << "product obj_id = [" << prodid << "]\n";
00081             cerr << "number of devices = " << int(numdevs) << endl;
00082         }
00083     }
00084     
00085     // command: go into data mode ('d')
00086     SendByte('d');
00087     
00088     // ack: read back echo ('d')
00089     while (GetByte() != 'd') ;
00090     
00091     // switch to 38400 bps data mode
00092 }
00093 
00094 Intersense::~Intersense()
00095 {
00096     if (debug) 
00097         cerr << "Intersense::destructor()\n";
00098 }
00099 
00100 void Intersense::Update()
00101 {
00102     if (debug) cerr << "Intersense::Update()\n";
00103     
00104     // flush input queue of any old data
00105     FlushInputQueue();
00106     
00107     // command: request data ('3')
00108     SendByte('3');
00109     
00110     // FYI: there is a 1.5ms delay to get data
00111     
00112     // read the azimuth, elevation, twist
00113     short azimuth = get_shorty();
00114     short elevation = get_shorty();
00115     short twist = get_shorty();
00116     
00117     // assign the values into our sensor
00118     pos.x = pos.y = pos.z = 0.0;                // no need to recenter or scale
00119 
00120     Point3D rot;
00121     rot.x = elevation/32768.0 * 180.0;
00122     rot.y = azimuth/32768.0 * 180.0;
00123     rot.z = twist/32768.0 * 180.0;
00124     
00125     { // compute the rotation matrix
00126         double e = -rot.y * M_PI/180.0;
00127         double a = -rot.z * M_PI/180.0;
00128         double t = -rot.x * M_PI/180.0;
00129         m.m[0][0] = cos(e)*cos(a);
00130         m.m[0][1] = cos(e)*sin(a);
00131         m.m[0][2] = -sin(e);
00132         m.m[0][3] = 0.0;
00133         m.m[1][0] = -cos(t)*sin(a) + sin(t)*sin(e)*cos(a);
00134         m.m[1][1] = cos(t)*cos(a) + sin(t)*sin(e)*sin(a);
00135         m.m[1][2] = sin(t)*cos(e);
00136         m.m[1][3] = 0.0;
00137         m.m[2][0] = sin(t)*sin(a) + cos(t)*sin(e)*cos(a);
00138         m.m[2][1] = -sin(t)*cos(a) + cos(t)*sin(e)*sin(a);
00139         m.m[2][2] = cos(t)*cos(e);
00140         m.m[2][3] = 0.0;
00141         m.m[3][0] = m.m[3][1] = m.m[3][2] = 0.0;
00142         m.m[3][3] = 1.0;
00143     }
00144     
00145     // and set active??
00146     active[0] = 0.0;
00147 }

Generated on Thu Aug 30 11:03:14 2007 for SPRING Mac by  doxygen 1.5.3