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
00022
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
00043 m_ptrToReadData = new adat_t[m_tempBuffSize];
00044
00045
00046 cout << " Initializing the DMU... " << endl;
00047
00048
00049
00050
00051
00052
00053
00054 cout << " ok, 'dmuInit()' successfull " << endl;
00055 return 0;
00056 }
00057
00058 int DMU::Initialize(const string& _sInitString)
00059 {
00060
00061
00062
00063 unsigned int find = 0;
00064 int efind;
00065 string sParameter;
00066 string stringTemp;
00067
00068 Vector tempVector3(3);
00069 Vector tempQuat4(4);
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
00129
00130 m_prate = 200;
00131 m_tempBuffSize = 100;
00132
00133
00134
00135 m_ptrToReadData = new adat_t[m_tempBuffSize];
00136
00137
00138 cout << " Initializing the DMU... " << endl;
00139
00140
00141
00142
00143
00144
00145 cout << " ok, 'dmuInit()' successfull " << endl;
00146 return 0;
00147 }
00148
00149 DMU* DMU::Instance()
00150 {
00151
00152
00153
00154
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
00166
00167
00168
00169
00170
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
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212 Quaternion q1( 0.0, 0.0, 0.0, 1.0);
00213
00214
00215
00216
00217 Quaternion q2( 0.5, 0.5, 0.5, -0.5);
00218
00219
00220
00221
00222 Quaternion q3( 0.5, 0.5, 0.5, 0.5);
00223
00224
00225 Rotation R_rg1_to_dmu(q1);
00226
00227
00228 Rotation R_rg2_to_dmu(q2);
00229
00230
00231 Rotation R_rg3_to_dmu(q3);
00232
00233
00234 Quaternion q0(m_sensorToBodyQuaternion);
00235 Rotation R_dmu_to_b(q0);
00236
00237
00238 Rotation R_rg1_to_b;
00239 R_rg1_to_b = R_dmu_to_b * R_rg1_to_dmu;
00240
00241
00242 Rotation R_rg2_to_b;
00243 R_rg2_to_b = R_dmu_to_b * R_rg2_to_dmu;
00244
00245
00246 Rotation R_rg3_to_b;
00247 R_rg3_to_b = R_dmu_to_b * R_rg3_to_dmu;
00248
00249
00250
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
00256
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
00262
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
00275 return m_dmuXToBodyQuaternion;
00276 }
00277 else if ( id == 2 | id == 5 )
00278 {
00279
00280 return m_dmuYToBodyQuaternion;
00281 }
00282 else if ( id == 3 | id == 6 )
00283 {
00284
00285 return m_dmuZToBodyQuaternion;
00286 }
00287
00288
00289
00290
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
00306
00307 static int numberOfTimesCalled = 0;
00308
00309
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
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
00353
00354 gettimeofday(&_timeStamp, 0);
00355
00356 }
00357
00358 DMU::~DMU()
00359 {
00360
00361 delete[] m_ptrToReadData;
00362 }
00363
00364
00365