Go to the documentation of this file.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
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 #include <math.h>
00055 #include <vector>
00056 #include <stdio.h>
00057 #include <sstream>
00058 #include <cob_canopen_motor/ElmoRecorder.h>
00059 #include <cob_canopen_motor/CanDriveHarmonica.h>
00060
00061 ElmoRecorder::ElmoRecorder(CanDriveHarmonica * pParentHarmonicaDrive) {
00062 m_pHarmonicaDrive = pParentHarmonicaDrive;
00063
00064 m_bIsInitialized = false;
00065 m_iReadoutRecorderTry = 0;
00066 }
00067
00068 ElmoRecorder::~ElmoRecorder() {
00069 }
00070
00071 bool ElmoRecorder::isInitialized(bool initNow) {
00072 if(initNow) m_bIsInitialized = true;
00073 return m_bIsInitialized;
00074 }
00075
00076 int ElmoRecorder::configureElmoRecorder(int iRecordingGap, int driveID, int startImmediately){
00077 m_iDriveID = driveID;
00078
00079 if(startImmediately >=2 ) startImmediately = 1;
00080
00081 m_pHarmonicaDrive->IntprtSetInt(8, 'R', 'R', 0, 0);
00082
00083
00084
00085
00086
00087 m_pHarmonicaDrive->IntprtSetInt(8, 'R', 'C', 0, 33283);
00088
00089 m_pHarmonicaDrive->IntprtSetInt(8, 'R', 'P', 3, 0);
00090
00091 m_pHarmonicaDrive->IntprtSetInt(8, 'R', 'G', 0, iRecordingGap);
00092
00093
00094 m_pHarmonicaDrive->IntprtSetInt(8, 'R', 'L', 0, 1024);
00095
00096
00097
00098
00099
00100 m_pHarmonicaDrive->IntprtSetInt(8, 'R', 'R', 0, startImmediately + 1);
00101
00102 m_fRecordingStepSec = 0.000090 * 4 * iRecordingGap;
00103
00104 return 0;
00105 }
00106
00107 int ElmoRecorder::readoutRecorderTry(int iObjSubIndex) {
00108
00109
00110
00111 m_iReadoutRecorderTry = 1;
00112 m_iCurrentObject = iObjSubIndex;
00113
00114 m_pHarmonicaDrive->requestStatus();
00115
00116 return 0;
00117 }
00118
00119 int ElmoRecorder::readoutRecorderTryStatus(int iStatusReg, segData& SDOData) {
00120 if(m_iReadoutRecorderTry == 0) return 0;
00121
00122 m_iReadoutRecorderTry = 0;
00123
00124
00125 int iRecorderStatus = (0x30000 & iStatusReg) >> 16;
00126
00127 if(iRecorderStatus == 0) {
00128 std::cout << "Recorder " << m_iDriveID << " inactive with no valid data to upload" << std::endl;
00129 SDOData.statusFlag = segData::SDO_SEG_FREE;
00130 } else if(iRecorderStatus == 1) {
00131 std::cout << "Recorder " << m_iDriveID << " waiting for a trigger event" << std::endl;
00132 SDOData.statusFlag = segData::SDO_SEG_FREE;
00133 } else if(iRecorderStatus == 2) {
00134 std::cout << "Recorder " << m_iDriveID << " finished, valid data ready for use" << std::endl;
00135 readoutRecorder(m_iCurrentObject);
00136
00137 } else if(iRecorderStatus == 3) {
00138 std::cout << "Recorder " << m_iDriveID << " is still recording" << std::endl;
00139 SDOData.statusFlag = segData::SDO_SEG_FREE;
00140 }
00141
00142 return 0;
00143 }
00144
00145 int ElmoRecorder::readoutRecorder(int iObjSubIndex){
00146
00147 int iObjIndex = 0x2030;
00148
00149 m_pHarmonicaDrive->sendSDOUpload(iObjIndex, iObjSubIndex);
00150 m_iCurrentObject = iObjSubIndex;
00151
00152 return 0;
00153 }
00154
00155 int ElmoRecorder::processData(segData& SDOData) {
00156 int iItemSize = 4;
00157 int iItemCount = 0;
00158 unsigned int iNumDataItems = 0;
00159 bool bCollectFloats = true;
00160 float fFloatingPointFactor = 0;
00161
00162 std::vector<float> vfResData[2];
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 switch ((SDOData.data[0] >> 4) ) {
00178 case 4:
00179 bCollectFloats = false;
00180 iItemSize = 4;
00181 break;
00182 case 5:
00183 bCollectFloats = true;
00184 iItemSize = 4;
00185 break;
00186 case 1:
00187 bCollectFloats = true;
00188 iItemSize = 2;
00189 break;
00190 default:
00191 bCollectFloats = false;
00192 iItemSize = 4;
00193 break;
00194 }
00195 std::cout << ">>>>>ElmoRec: HEADER INFOS<<<<<\nData type is: " << (SDOData.data[0] >> 4) << std::endl;
00196
00197
00198
00199
00200
00201 iNumDataItems = (SDOData.data[2] << 8 | SDOData.data[1]);
00202
00203
00204
00205 fFloatingPointFactor = convertBinaryToFloat( (SDOData.data[6] << 24) | (SDOData.data[5] << 16) | (SDOData.data[4] << 8) | (SDOData.data[3]) );
00206 std::cout << "Floating point factor for recorded values is: " << fFloatingPointFactor << std::endl;
00207
00208
00209 if( ((SDOData.numTotalBytes-7)/iItemSize) != iNumDataItems)
00210 std::cout << "SDODataSize announced in SDO-Header" << ((SDOData.numTotalBytes-7)/iItemSize) << " differs from NumDataItems by ElmoData-Header" << iNumDataItems << std::endl;
00211
00212
00213
00214 vfResData[0].assign(iNumDataItems, 0.0);
00215 vfResData[1].assign(iNumDataItems, 0.0);
00216 iItemCount = 0;
00217
00218
00219 for(unsigned int i=7;i<=SDOData.data.size() - iItemSize; i=i+iItemSize) {
00220 if(bCollectFloats) {
00221 if(iItemSize == 4)
00222 vfResData[1][iItemCount] = fFloatingPointFactor * convertBinaryToFloat( (SDOData.data[i] << 0) | (SDOData.data[i+1] << 8) | (SDOData.data[i+2] << 16) | (SDOData.data[i+3] << 24) );
00223
00224
00225 if(iItemCount == 120)
00226 std::cout << (unsigned int)( (SDOData.data[i] << 0) | (SDOData.data[i+1] << 8) | (SDOData.data[i+2] << 16) | (SDOData.data[i+3] << 24) ) << std::endl;
00227
00228 else vfResData[1][iItemCount] = fFloatingPointFactor * convertBinaryToHalfFloat( (SDOData.data[i] << 0) | (SDOData.data[i+1] << 8) | (SDOData.data[i+2] << 16) | (SDOData.data[i+3] << 24) );
00229 iItemCount ++;
00230 } else {
00231 vfResData[1][iItemCount] = fFloatingPointFactor * (float)( (SDOData.data[i] << 0) | (SDOData.data[i+1] << 8) | (SDOData.data[i+2] << 16) | (SDOData.data[i+3] << 24) );
00232 iItemCount ++;
00233 }
00234
00235 vfResData[0][iItemCount] = m_fRecordingStepSec * iItemCount;
00236 }
00237
00238 logToFile(m_sLogFilename, vfResData);
00239
00240 SDOData.statusFlag = segData::SDO_SEG_FREE;
00241 return 0;
00242 }
00243
00244 int ElmoRecorder::setLogFilename(std::string sLogFileprefix) {
00245 m_sLogFilename = sLogFileprefix;
00246 return 0;
00247 }
00248
00249
00250
00251 float ElmoRecorder::convertBinaryToFloat(unsigned int iBinaryRepresentation) {
00252
00253 int iSign;
00254 int iExponent;
00255 unsigned int iMantissa;
00256 float iNumMantissa = 0.0f;
00257
00258 if((iBinaryRepresentation & (1 << 31)) == 0)
00259 iSign = 1;
00260 else
00261 iSign = -1;
00262
00263 iExponent = ((iBinaryRepresentation >> 23) & 0xFF) - 127;
00264
00265 iMantissa = (iBinaryRepresentation & 0x7FFFFF);
00266
00267 iNumMantissa = 1.0f;
00268
00269 for(int i=1; i<=23; i++) {
00270 if((iMantissa & (1 << (23-i))) > 0) {
00271 iNumMantissa = iNumMantissa + pow(2,(-1)*i);
00272 }
00273 }
00274
00275 return iSign * pow(2,iExponent) * iNumMantissa;
00276 }
00277
00278 float ElmoRecorder::convertBinaryToHalfFloat(unsigned int iBinaryRepresentation) {
00279
00280 int iSign;
00281 int iExponent;
00282 unsigned int iMantissa;
00283 float iNumMantissa = 0.0f;
00284
00285 if((iBinaryRepresentation & (1 << 15)) == 0)
00286 iSign = 1;
00287 else
00288 iSign = -1;
00289
00290 iExponent = ((iBinaryRepresentation >> 10) & 0x1F) - 15;
00291
00292 iMantissa = (iBinaryRepresentation & 0x3FF);
00293
00294 iNumMantissa = 1.0f;
00295
00296 for(int i=1; i<=10; i++) {
00297 if((iMantissa & (1 << (10-i))) > 0) {
00298 iNumMantissa = iNumMantissa + pow(2,(-1)*i);
00299 }
00300 }
00301
00302 return iSign * pow(2,iExponent) * iNumMantissa;
00303 }
00304
00305
00306 int ElmoRecorder::logToFile(std::string filename, std::vector<float> vtValues[]) {
00307 std::stringstream outputFileName;
00308 outputFileName << filename << "mot_" << m_iDriveID << "_" << m_iCurrentObject << ".log";
00309
00310 FILE* pFile;
00311
00312 pFile = fopen(outputFileName.str().c_str(), "w");
00313
00314
00315 if( pFile == NULL )
00316 {
00317 std::cout << "Error while writing file: " << outputFileName.str() << " Maybe the selected folder does'nt exist." << std::endl;
00318 }
00319 else
00320 {
00321
00322 for (unsigned int i = 0; i < vtValues[0].size(); i++)
00323 fprintf(pFile, "%e %e\n", vtValues[0][i], vtValues[1][i]);
00324 fclose(pFile);
00325 }
00326
00327 return true;
00328 }