00001
00018
00019 #include <sstream>
00020 #include <fstream>
00021 #include <cmath>
00022
00023
00024 #include "cData.h"
00025 #include "trackerConstants.h"
00026
00027 #define NUM_SENSORS 22
00028 #define NUM_TOTAL_STEPS 8
00029 #define MIN_JOINT_RESOLUTION 20
00030
00031 const char *CAL_DEFAULT_TEXT = "Data tracker calibration (c) 2004 by Steffen.";
00032 const char *CAL_STEP_TEXT[] = {"Press your hand against a flat surface, all fingers pointing parallel, and press \"Record Sample\" when ready.",
00033 "Press your hand against a flat surface, open all fingers, and press \"Record Sample\" when ready.",
00034 "Close your index, middle, ring, pinky finger as much as possible, and press \"Record Sample\" when ready.",
00035 "Do an ok gesture by touching the thumb tip with the index tip, and press \"Record Sample\" when ready.",
00036 "Bend your wrist up, and press Enter when ready.",
00037 "Bend your wrist down, and press Enter when ready.",
00038 "Bend your wrist inside, with your thumb towards the forearm. Press Enter when ready.",
00039 "Bend your wrist outside, and press Enter when ready.",
00040 "Calibration finished. Now save and/or load it to the server."
00041 };
00042
00043 const char *CAL_DEFAULT_PIC = "images/00glove-calibration.jpg";
00044 const char *CAL_STEP_PIC[] = {"images/0hand-flat-closed.jpg",
00045 "images/1hand-flat-open.jpg",
00046 "images/2hand-fingers-closed.jpg",
00047 "images/3hand-ok-gesture.jpg",
00048 "images/4hand-wrist-up.jpg",
00049 "images/5hand-wrist-down.jpg",
00050 "images/6hand-wirst-in.jpg",
00051 "images/7hand-wirst-out.jpg",
00052 "images/00glove-calibration.jpg"
00053 };
00054
00055
00056 cData::cData()
00057 {
00058 numTotalSteps = NUM_TOTAL_STEPS;
00059 reset();
00060 }
00061
00062 cData::~cData()
00063 {
00064 }
00065
00066 bool
00067 cData::connectToTrackerServer(std::string trackerServerName){
00068
00069 CORBA::Object_ptr newObjectPointer = bgcorba::resolveObject(trackerServerName);
00070 myTracker = tracker::trackerServer::_narrow(newObjectPointer);
00071
00072 if (CORBA::is_nil(myTracker)) {
00073 BGDBG(0, "Could not find a trackerServer.\n");
00074 return false;
00075 }
00076 else
00077 BGDBG(0, "trackerServer found!\n");
00078
00079 return true;
00080 }
00081
00082 void
00083 cData::reset(){
00084
00085 data.clear();
00086 rawData.clear();
00087 currStep = 0;
00088 }
00089
00090 unsigned int
00091 cData::getNumTotalSteps(){
00092
00093 return numTotalSteps;
00094 }
00095
00096 unsigned int
00097 cData::getCurrentStep(){
00098
00099 return currStep;
00100 }
00101
00102 std::string
00103 cData::getTextForStep(int step){
00104
00105 if(step <= NUM_TOTAL_STEPS && step >= 0)
00106 return CAL_STEP_TEXT[step];
00107 else{
00108 return CAL_DEFAULT_TEXT;
00109 }
00110 }
00111
00112 std::string
00113 cData::getPicForStep(int step){
00114
00115 if(step <= NUM_TOTAL_STEPS && step >= 0)
00116 return CAL_STEP_PIC[step];
00117 else{
00118 return CAL_DEFAULT_PIC;
00119 }
00120 }
00121
00122 std::vector<double>
00123 cData::calibrateNextStep(){
00124
00125 currStep++;
00126 std::vector<double> c;
00127 IPRFilterModule::TimeDataVector measure;
00128
00130 try{
00131 measure = *(myTracker->getRawData());
00132 }catch(...){
00133 BGDBG(0, "Glove server dead? Could not connect.\n");
00134 return c;
00135 }
00136
00137 for (unsigned int i=0; i<measure.v.vector.length(); i++)
00138 c.push_back(measure.v.vector[i]);
00139
00140 rawData.push_back(c);
00141
00142 return c;
00143 }
00144
00145 cData::trackerCal
00146 cData::getCalibration(){
00147
00148 return data;
00149 }
00150
00151 bool
00152 cData::setCalibration(cData::trackerCal g){
00153
00154 data = g;
00155 return true;
00156 }
00157
00158 bool
00159 cData::calibrate(){
00160
00161 data = computeCalibration(rawData);
00162 if(data.size() != NUM_SENSORS)
00163 return false;
00164
00165 return true;
00166 }
00167
00168 bool
00169 cData::saveCalibration(std::string fileName){
00170
00171 FILE *f;
00172
00173
00174 if ((f=fopen(fileName.c_str(), "w")) == NULL)
00175 {
00176 BGDBG(0, "save calibration file - error: open file !\n");
00177 return false;
00178 }
00179
00180 for (unsigned int i=0; i<data.size(); i++){
00181
00182 for (unsigned int j=0; j<data[i].size(); j++){
00183 fprintf(f, "%f:%f", (data[i])[j].first, (data[i])[j].second);
00184 if(j < data[i].size()-1)
00185 fprintf(f, " ");
00186 else
00187 fprintf(f, "\n");
00188 }
00189 }
00190 fclose(f);
00191
00192 BGDBG(0, "Saved calibration successfully!\n");
00193
00194 return true;
00195 }
00196
00197
00198 bool
00199 cData::openCalibration(std::string fileName){
00200
00201 std::ifstream inputFile (fileName.c_str());
00202 if (!inputFile.is_open()){
00203 BGDBG(0, "open calibration file - error while opening file !\n");
00204 return false;
00205 }
00206
00207
00208
00209 bg::strlist lines = bg::string(inputFile).split("\n");
00210 trackerCal tcal;
00211
00212
00213 for (unsigned int lineNo=0; lineNo<lines.size(); lineNo++){
00214
00215
00216 bg::strlist fulcs = lines[lineNo].split(" ");
00217 sensorCal scal;
00218
00219
00220 for(unsigned int fulcNo=0; fulcNo<fulcs.size(); fulcNo++){
00221
00222 fulcrum f;
00223 bg::strlist oneF = fulcs[fulcNo].split(":");
00224 if(oneF.size() != 2){
00225 BGDBG(0, "Read calibration file - error while reading file. Incomplete data.\n");
00226 return false;
00227 }
00228 f.first = oneF[0].toDouble();
00229 f.second = oneF[1].toDouble();
00230 scal.push_back(f);
00231 }
00232 tcal.push_back(scal);
00233 }
00234
00235
00236 BGDBG(0, "Opened calibration file successfully!\n");
00237 BGDBG(3, "File contained %d lines, first line contained %d entries.\n", tcal.size(), tcal[0].size());
00238
00239 data = tcal;
00240
00241 return true;
00242 }
00243
00244 bool
00245 cData::checkConsistency(std::string &print){
00246
00247 std::vector<unsigned int> bad;
00248 std::stringstream s;
00249
00250 for (unsigned int i=0; i<data.size(); i++){
00251
00252 if(fabs(data[i][1].second - data[i][0].second) < MIN_JOINT_RESOLUTION){
00253
00254 bad.push_back(i);
00255 }
00256 }
00257
00258 if(!bad.empty()){
00259
00260 BGDBG(3, "Calibration data not ok.\n");
00261 s << "********************************************************************\n";
00262 s << "Bad calibration data.\n";
00263 for(unsigned int i=0; i<bad.size(); i++){
00264 s << "Joint " << JOINT_NAME[bad[i]] << " does not achieve minimum resolution.\n";
00265 s << "Joint resolution: " << (int)fabs(data[bad[i]][1].second - data[bad[i]][0].second)
00266 << " Minimum resolution: " << MIN_JOINT_RESOLUTION << "\n";
00267 if(fabs(data[bad[i]][1].second - data[bad[i]][0].second) < 2)
00268 s << "GloveServer will bail out when reading this data!\n";
00269 s << "\n";
00270 }
00271 s << "It is strongly recommended to repeat the calibration.\n";
00272 s << "********************************************************************\n";
00273
00274 print = std::string(s.str());
00275 return false;
00276 }
00277 else{
00278 s << "Calibration data ok.\n";
00279 print = std::string(s.str());
00280 BGDBG(3, "Calibration data ok.\n");
00281 }
00282
00283 return true;
00284 }
00285
00286 bool
00287 cData::tellServerLoadCurrentCalibration(){
00288
00289
00290
00291
00293
00294
00295
00296
00297
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317 return false;
00318 }
00319
00320
00321 cData::trackerCal
00322 cData::computeCalibration(std::vector<std::vector<double> > r){
00323
00325 trackerCal c;
00326 sensorCal sc;
00327 fulcrum f;
00328
00330 std::vector<double> a1, a2;
00331 for (int i=0; i<NUM_SENSORS; i++){
00332
00333
00334 }
00335
00337 if(r.size() != NUM_TOTAL_STEPS){
00338
00339 BGDBG(0, "Error in computeCalibration(). Input data not ok.\n");
00340 return c;
00341 }
00342
00345 sc.clear();
00346 f.first = a1[0];
00347 f.second = r[6][0];
00348 sc.push_back(f);
00349 f.first = a2[0];
00350 f.second = r[7][0];
00351 sc.push_back(f);
00352 c.push_back(sc);
00353
00355 sc.clear();
00356 f.first = a1[1];
00357 f.second = r[4][1];
00358 sc.push_back(f);
00359 f.first = a2[1];
00360 f.second = r[5][1];
00361 sc.push_back(f);
00362 c.push_back(sc);
00363
00366 sc.clear();
00367 f.first = a1[2];
00368 f.second = r[0][2];
00369 sc.push_back(f);
00370 f.first = a2[2];
00371 f.second = r[3][2];
00372 sc.push_back(f);
00373 c.push_back(sc);
00374
00376 sc.clear();
00377 f.first = a1[3];
00378 f.second = r[1][3];
00379 sc.push_back(f);
00380 f.first = a2[3];
00381 f.second = r[3][3];
00382 sc.push_back(f);
00383 c.push_back(sc);
00384
00385 sc.clear();
00386 f.first = a1[4];
00387 f.second = r[1][4];
00388 sc.push_back(f);
00389 f.first = a2[4];
00390 f.second = r[3][4];
00391 sc.push_back(f);
00392 c.push_back(sc);
00393
00394 sc.clear();
00395 f.first = a1[5];
00396 f.second = r[1][5];
00397 sc.push_back(f);
00398 f.first = a2[5];
00399 f.second = r[3][5];
00400 sc.push_back(f);
00401 c.push_back(sc);
00402
00404 sc.clear();
00405 f.first = a1[6];
00406 f.second = r[0][6];
00407 sc.push_back(f);
00408 f.first = a2[6];
00409 f.second = r[2][6];
00410 sc.push_back(f);
00411 c.push_back(sc);
00412
00414 sc.clear();
00415 f.first = a1[7];
00416 f.second = r[0][7];
00417 sc.push_back(f);
00418 f.first = a2[7];
00419 f.second = r[1][7];
00420 sc.push_back(f);
00421 c.push_back(sc);
00422
00423 sc.clear();
00424 f.first = a1[8];
00425 f.second = r[0][8];
00426 sc.push_back(f);
00427 f.first = a2[8];
00428 f.second = r[2][8];
00429 sc.push_back(f);
00430 c.push_back(sc);
00431
00432 sc.clear();
00433 f.first = a1[9];
00434 f.second = r[0][9];
00435 sc.push_back(f);
00436 f.first = a2[9];
00437 f.second = r[2][9];
00438 sc.push_back(f);
00439 c.push_back(sc);
00440
00442 sc.clear();
00443 f.first = a1[10];
00444 f.second = r[0][10];
00445 sc.push_back(f);
00446 f.first = a2[10];
00447 f.second = r[2][10];
00448 sc.push_back(f);
00449 c.push_back(sc);
00450
00452 sc.clear();
00453 f.first = a1[11];
00454 f.second = r[0][11];
00455 sc.push_back(f);
00456 f.first = a2[11];
00457 f.second = r[1][11];
00458 sc.push_back(f);
00459 c.push_back(sc);
00460
00461 sc.clear();
00462 f.first = a1[12];
00463 f.second = r[0][12];
00464 sc.push_back(f);
00465 f.first = a2[12];
00466 f.second = r[2][12];
00467 sc.push_back(f);
00468 c.push_back(sc);
00469
00470 sc.clear();
00471 f.first = a1[13];
00472 f.second = r[0][13];
00473 sc.push_back(f);
00474 f.first = a2[13];
00475 f.second = r[2][13];
00476 sc.push_back(f);
00477 c.push_back(sc);
00478
00480 sc.clear();
00481 f.first = a1[14];
00482 f.second = r[0][14];
00483 sc.push_back(f);
00484 f.first = a2[14];
00485 f.second = r[2][14];
00486 sc.push_back(f);
00487 c.push_back(sc);
00488
00490 sc.clear();
00491 f.first = a1[15];
00492 f.second = r[0][15];
00493 sc.push_back(f);
00494 f.first = a2[15];
00495 f.second = r[1][15];
00496 sc.push_back(f);
00497 c.push_back(sc);
00498
00499 sc.clear();
00500 f.first = a1[16];
00501 f.second = r[0][16];
00502 sc.push_back(f);
00503 f.first = a2[16];
00504 f.second = r[2][16];
00505 sc.push_back(f);
00506 c.push_back(sc);
00507
00508 sc.clear();
00509 f.first = a1[17];
00510 f.second = r[0][17];
00511 sc.push_back(f);
00512 f.first = a2[17];
00513 f.second = r[2][17];
00514 sc.push_back(f);
00515 c.push_back(sc);
00516
00518 sc.clear();
00519 f.first = a1[18];
00520 f.second = r[0][18];
00521 sc.push_back(f);
00522 f.first = a2[18];
00523 f.second = r[2][18];
00524 sc.push_back(f);
00525 c.push_back(sc);
00526
00528 sc.clear();
00529 f.first = a1[19];
00530 f.second = r[0][19];
00531 sc.push_back(f);
00532 f.first = a2[19];
00533 f.second = r[1][19];
00534 sc.push_back(f);
00535 c.push_back(sc);
00536
00537 sc.clear();
00538 f.first = a1[20];
00539 f.second = r[0][20];
00540 sc.push_back(f);
00541 f.first = a2[20];
00542 f.second = r[2][20];
00543 sc.push_back(f);
00544 c.push_back(sc);
00545
00546 sc.clear();
00547 f.first = a1[21];
00548 f.second = r[0][21];
00549 sc.push_back(f);
00550 f.first = a2[21];
00551 f.second = r[2][21];
00552 sc.push_back(f);
00553 c.push_back(sc);
00554
00555 return c;
00556 }
00557
00558
00559
00560 #if cData_test
00561 #include <stdio.h>
00562 int main(int argc, char **argv)
00563 {
00564
00565
00566
00567
00568
00569
00570 fprintf(stderr, "Testing cData\n");
00571
00572 return 0;
00573 }
00574 #endif