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

fakeDMU.cpp

Go to the documentation of this file.
00001 #include <iostream>
00002 #include "DMU.h"
00003 
00004 using namespace O_SESSAME;
00005 using namespace std;
00006 
00007 DMU* DMU::s_instance = NULL;
00008 
00009 DMU::DMU()
00010 {
00011         Initialize();
00012 }
00013 
00014 DMU::DMU(const string& _sInitString)
00015 {
00016         Initialize(_sInitString);
00017 }
00018 
00019 int DMU::Initialize()
00020 {
00021         // this is temporary, just used because the parsing code 
00022         // is not ready yet
00023         
00024         Vector tempVector3(3);
00025         Vector tempQuat4(4);
00026 
00027         tempVector3(1) = 0.0;
00028         tempVector3(2) = 0.0;
00029         tempVector3(3) = 0.0;
00030 
00031         tempQuat4(1) = 0.0;
00032         tempQuat4(2) = 0.0;
00033         tempQuat4(3) = 0.0;
00034         tempQuat4(4) = 1.0;
00035 
00036         SetLocation(tempVector3);
00037         SetSensorToBodyQuaternions(tempQuat4);
00038         
00039         m_prate = 200;
00040         m_tempBuffSize = 100;
00041          
00042         // allocate the memory that holds the temporary read data buffer 
00043         m_ptrToReadData = new adat_t[m_tempBuffSize];
00044         
00045         // OK, pretend to initialize the hardware DMU.
00046         cout << " Initializing the DMU... " << endl;
00047         
00048         //if (dmuInit(1024, m_prate, 0))
00049         //{
00050           //  cout << " oops, 'dmuInit()' failed " << endl;         
00051             // some error return condition?
00052             //return 1;
00053         // }
00054         cout << " ok, 'dmuInit()' successfull " << endl;
00055         return 0;
00056 }
00057 
00058 int DMU::Initialize(const string& _sInitString)
00059 {
00060         // code to parse info from config file
00061         // ... here...
00062         
00063     unsigned int find = 0; 
00064     int efind;
00065     string sParameter;
00066     string stringTemp; 
00067     //stringstream sStream;//(_sInitString.rdbuf());
00068     Vector tempVector3(3); // temp location vector
00069     Vector tempQuat4(4); // temp quaternion vector
00070     
00071     while(find < _sInitString.size())
00072     {
00073         efind = _sInitString.find(':', find);
00074         sParameter = _sInitString.substr(find, efind-find);
00075             find = efind + 1;
00076 
00077         if((sParameter == "Name") || (sParameter == "Number"))
00078         {
00079             efind = _sInitString.find('\n', find);
00080                 find = efind + 1;           
00081         }
00082         else if(sParameter == "Quaternion")
00083         {       
00084             efind = _sInitString.find(',', find);
00085             stringTemp = _sInitString.substr(find, efind-find);
00086                 find = efind + 1;     
00087             tempQuat4(1) = strtod(stringTemp.c_str(), (char **)NULL);
00088         
00089             efind = _sInitString.find(',', find);
00090             stringTemp = _sInitString.substr(find, efind-find);
00091                 find = efind + 1;     
00092             tempQuat4(2) = strtod(stringTemp.c_str(), (char **)NULL);
00093         
00094             efind = _sInitString.find(',', find);
00095             stringTemp = _sInitString.substr(find, efind-find);
00096                 find = efind + 1;     
00097             tempQuat4(3) = strtod(stringTemp.c_str(), (char **)NULL);
00098             
00099             efind = _sInitString.find('\n', find);
00100             stringTemp = _sInitString.substr(find, efind-find);
00101                 find = efind + 1;     
00102             tempQuat4(4) = strtod(stringTemp.c_str(), (char **)NULL);
00103     
00104             SetSensorToBodyQuaternions(tempQuat4);
00105         }
00106         
00107         else if(sParameter == "Location")
00108         {
00109             efind = _sInitString.find(',', find);
00110             stringTemp = _sInitString.substr(find, efind-find);
00111                 find = efind + 1;
00112             tempVector3(1) = strtod(stringTemp.c_str(), (char **)NULL);
00113 
00114             efind = _sInitString.find(',', find);
00115             stringTemp = _sInitString.substr(find, efind-find);
00116                 find = efind + 1;
00117             tempVector3(2) = strtod(stringTemp.c_str(), (char **)NULL);
00118 
00119             efind = _sInitString.find('\n', find);
00120             stringTemp = _sInitString.substr(find, efind-find);
00121                 find = efind + 1;
00122             tempVector3(3) = strtod(stringTemp.c_str(), (char **)NULL);
00123 
00124             SetLocation(tempVector3);
00125         }
00126     }
00127 
00128     // could also parse out following info from config file.
00129     // for now it's hard-coded
00130     m_prate = 200;
00131     m_tempBuffSize = 100;
00132     
00133 
00134     // allocate the memory that holds the temporary read data buffer 
00135     m_ptrToReadData = new adat_t[m_tempBuffSize];
00136         
00137     // OK, now pretend to initialize the hardware DMU.
00138     cout << " Initializing the DMU... " << endl;
00139     //if (dmuInit(1024, m_prate, 0))
00140     //{
00141 //          cout << " oops, 'dmuInit()' failed " << endl;           
00142             // some error return condition?
00143 //          return 1;
00144 //    }
00145     cout << " ok, 'dmuInit()' successfull " << endl;
00146     return 0;
00147 }
00148 
00149 DMU* DMU::Instance()
00150 {
00151         /* If first time being called, return a pointer 
00152          to an instance of a new DMU.  Otherwise return
00153          a pointer to an instance of a DMU which was made
00154          earlier.  Essentially, there can be only one DMU. */
00155         if (s_instance==NULL)
00156         {
00157                 cout << " you have an instance of a new dmu " << endl;
00158                 s_instance = new DMU();
00159         }
00160         return s_instance;
00161 }
00162 
00163 DMU* DMU::Instance(const string& _sInitString)
00164 {
00165         /* If first time being called, return a pointer 
00166          to an instance of a new DMU.  Otherwise return
00167          a pointer to an instance of a DMU which was made
00168          earlier.  Essentially, there can be only one DMU. 
00169          
00170          Takes the initialization string as its argument*/
00171         if (s_instance==NULL) 
00172         {
00173                 cout << " you have an instance of a new dmu " << endl;
00174                 s_instance = new DMU(_sInitString);
00175         }
00176         return s_instance;
00177 }
00178 
00179 void DMU::SetLocation(const Vector& _newDeviceLocation)
00180 {
00181         m_location = _newDeviceLocation;
00182 }
00183 
00184 Vector DMU::GetLocation()
00185 {       
00186         return m_location;
00187 }
00188 
00189 void DMU::SetSensorToBodyQuaternions(const Vector& _newDeviceQuaternion)
00190 {
00191         m_sensorToBodyQuaternion = _newDeviceQuaternion;
00192         
00193         // m_sensorToBodyQuaternion is given in config file,
00194         // where the frame of the DMU, F_dmu, is defined on the DMU itself,
00195         // the body-fixed frame, F_b, is defined elsewhere.
00196         
00197         // let each rate gyro (RG) have its own frame,
00198         // where the RG is defined as being located on the 1-axis,
00199         // and the other two axis are taken from F_dmu,
00200         // keeping the right-handedness
00201         //
00202         // likewise let each linear accelerometer (LAmeter) have its own frame,
00203         // where the LAmeter is defined as measuring positve acceleration along
00204         // the frame's 1-axis, and the other axes same as above.
00205         // (note: just "LA" by itself might be confused with linear actuator)
00206 
00207         
00208         
00209         // quaternion that describes both the 
00210         // orientation of "x" RG frame wrt F_dmu and the
00211         // orientation of "x" LAmeter frame wrt F_dmu
00212         Quaternion q1( 0.0, 0.0, 0.0, 1.0);
00213         
00214         // quaternion that describes both the
00215         // orientation of "y" RG frame wrt F_dmu and the
00216         // orientation of "y" LAmeter frame wrt F_dmu
00217         Quaternion q2( 0.5, 0.5, 0.5, -0.5);
00218                 
00219         // quaternion that describes both the
00220         // orientation of "z" RG frame wrt F_dmu and the
00221         // orientation of "z" LAmeter frame wrt F_dmu
00222         Quaternion q3( 0.5, 0.5, 0.5, 0.5);
00223         
00224         // convert q1 into R_rg1_to_dmu
00225         Rotation R_rg1_to_dmu(q1);
00226 
00227         // convert q2 into R_rg2_to_dmu
00228         Rotation R_rg2_to_dmu(q2);
00229         
00230         // convert q3 into R_rg3_to_dmu
00231         Rotation R_rg3_to_dmu(q3);
00232 
00233         // convert m_dmuToBodyQuaternion to R_dmu_to_b;
00234         Quaternion q0(m_sensorToBodyQuaternion);
00235         Rotation R_dmu_to_b(q0);
00236         
00237         // calculate rotation from rg1 to body frame:
00238         Rotation R_rg1_to_b;
00239         R_rg1_to_b = R_dmu_to_b * R_rg1_to_dmu;
00240         
00241         // calculate rotation from rg2 to body frame:
00242         Rotation R_rg2_to_b;
00243         R_rg2_to_b = R_dmu_to_b * R_rg2_to_dmu;
00244         
00245         // calculate rotation from rg3 to body frame:
00246         Rotation R_rg3_to_b;
00247         R_rg3_to_b = R_dmu_to_b * R_rg3_to_dmu;
00248         
00249         // convert R_rg1_to_b into q_rg1_to_b
00250         // and set as m_dmuXToBodyQuaternion
00251         Quaternion q_rg1_to_b;
00252         q_rg1_to_b = R_rg1_to_b.GetQuaternion();
00253         m_dmuXToBodyQuaternion = q_rg1_to_b;
00254         
00255         // convert R_rg2_to_b into q_rg2_to_b
00256         // and set as m_dYoBodyQuaternion
00257         Quaternion q_rg2_to_b;
00258         q_rg2_to_b = R_rg2_to_b.GetQuaternion();
00259         m_dmuYToBodyQuaternion = q_rg2_to_b;
00260         
00261         // convert R_rg3to_b into q_rg3_to_b
00262         // and set as m_dmuXToBodyQuaternion
00263         Quaternion q_rg3_to_b;
00264         q_rg3_to_b = R_rg3_to_b.GetQuaternion();
00265         m_dmuZToBodyQuaternion = q_rg3_to_b;
00266         
00267 }
00268         
00269 Vector DMU::GetSensorToBodyQuaternion( int id )
00270 {
00271         
00272         if ( id == 1 | id == 4 )
00273         {
00274                 // xRG or xLAmeter
00275                 return m_dmuXToBodyQuaternion;
00276         }
00277         else if ( id == 2 | id == 5 )
00278         {
00279                 // yRG or yLAmeter
00280                 return m_dmuYToBodyQuaternion;
00281         }
00282         else if ( id == 3 | id == 6 )
00283         {
00284                 // zRG or zLAmeter
00285                 return m_dmuZToBodyQuaternion;
00286         }
00287         // no default case?
00288         // what about error?
00289         //
00290         // do something more elegant and logical in future
00291         Vector errorQuat4;
00292         errorQuat4(1) = 0.0;
00293         errorQuat4(2) = 0.0;
00294         errorQuat4(3) = 0.0;
00295         errorQuat4(4) = 1.0;
00296         cout << " you gave bad channel, here's a dummy quaternion " << endl;
00297         return errorQuat4;
00298 }
00299                         
00300 void DMU::GetReading( int _channelID, timeval& _timeStamp, double& _rateData )
00301 {
00302         cout << " you just called DMU::GetReading(...) " << endl;
00303         
00304         int numread = 0;
00305         //numread = dmuReadData(&m_ptrToReadData, m_tempBuffSize);
00306         
00307         static int numberOfTimesCalled = 0;
00308         
00309         // pretend dmuReadData reads no more than 10 from polled data
00310         for (int i=0; i<10; i++)
00311         {
00312                 m_ptrToReadData[i].xrate = 0.0 + 0.1*numberOfTimesCalled + (float)i*0.01;
00313                 m_ptrToReadData[i].yrate = 0.0 + 0.5*numberOfTimesCalled + (float)i*0.01;
00314                 m_ptrToReadData[i].zrate = 0.0 + 2.0*numberOfTimesCalled + (float)i*0.01;
00315                 m_ptrToReadData[i].xaccel = 0.0 + numberOfTimesCalled + (float)i*0.01;
00316                 m_ptrToReadData[i].yaccel = 0.0 + numberOfTimesCalled + (float)i*0.01;
00317                 m_ptrToReadData[i].zaccel = 0.0 + numberOfTimesCalled + (float)i*0.01;
00318                 m_ptrToReadData[i].temp = 0.0 + numberOfTimesCalled + (float)i*0.01;
00319                 numread++;
00320                 i++;
00321         }
00322         numberOfTimesCalled++;
00323         
00324         cout << " you read " << numread << " sets of data " << endl;
00325         // will give the first set
00326         switch (_channelID)
00327         {
00328                 case 1:         
00329                         _rateData = m_ptrToReadData[0].xrate;
00330                         break;
00331                 case 2:
00332                         _rateData = m_ptrToReadData[0].yrate;
00333                         break;
00334                 case 3:
00335                         _rateData = m_ptrToReadData[0].zrate;
00336                         break;
00337                 case 4:
00338                         _rateData = m_ptrToReadData[0].xaccel;
00339                         break;
00340                 case 5:
00341                         _rateData = m_ptrToReadData[0].yaccel;
00342                         break;
00343                 case 6:
00344                         _rateData = m_ptrToReadData[0].zaccel;
00345                         break;
00346                 case 7:
00347                         _rateData = m_ptrToReadData[0].temp;
00348                         break;
00349                 default:
00350                         cout << " illegal channel value! " << endl;
00351         }
00352         // for now just give deltat as timestamp
00353         //_timeStamp = m_ptrToReadData[0].deltat;
00354         gettimeofday(&_timeStamp, 0);
00355         
00356 }
00357 
00358 DMU::~DMU()
00359 {
00360         // Deconstructor
00361         delete[] m_ptrToReadData; /* release the memory used for the temporary read data buffer */
00362 }
00363 
00364                         
00365         

Generated on Wed Sep 5 12:54:21 2007 for DSACSS Operational Code by  doxygen 1.3.9.1