cData.cc
Go to the documentation of this file.
1 
18 /* system includes */
19 #include <sstream>
20 #include <fstream>
21 #include <cmath>
22 
23 /* my includes */
24 #include "cData.h"
25 #include "trackerConstants.h"
26 
27 #define NUM_SENSORS 22
28 #define NUM_TOTAL_STEPS 8
29 #define MIN_JOINT_RESOLUTION 20
30 
31 const char *CAL_DEFAULT_TEXT = "Data tracker calibration (c) 2004 by Steffen.";
32 const char *CAL_STEP_TEXT[] = {"Press your hand against a flat surface, all fingers pointing parallel, and press \"Record Sample\" when ready.",
33  "Press your hand against a flat surface, open all fingers, and press \"Record Sample\" when ready.",
34  "Close your index, middle, ring, pinky finger as much as possible, and press \"Record Sample\" when ready.",
35  "Do an ok gesture by touching the thumb tip with the index tip, and press \"Record Sample\" when ready.",
36  "Bend your wrist up, and press Enter when ready.",
37  "Bend your wrist down, and press Enter when ready.",
38  "Bend your wrist inside, with your thumb towards the forearm. Press Enter when ready.",
39  "Bend your wrist outside, and press Enter when ready.",
40  "Calibration finished. Now save and/or load it to the server."
41 };
42 
43 const char *CAL_DEFAULT_PIC = "images/00glove-calibration.jpg";
44 const char *CAL_STEP_PIC[] = {"images/0hand-flat-closed.jpg",
45  "images/1hand-flat-open.jpg",
46  "images/2hand-fingers-closed.jpg",
47  "images/3hand-ok-gesture.jpg",
48  "images/4hand-wrist-up.jpg",
49  "images/5hand-wrist-down.jpg",
50  "images/6hand-wirst-in.jpg",
51  "images/7hand-wirst-out.jpg",
52  "images/00glove-calibration.jpg"
53 };
54 
55 
57 {
59  reset();
60 }
61 
63 {
64 }
65 
66 bool
67 cData::connectToTrackerServer(std::string trackerServerName){
68 
69  CORBA::Object_ptr newObjectPointer = bgcorba::resolveObject(trackerServerName);
70  myTracker = tracker::trackerServer::_narrow(newObjectPointer);
71  //myTracker = trackerServer::_narrow(newObjectPointer);
72  if (CORBA::is_nil(myTracker)) {
73  BGDBG(0, "Could not find a trackerServer.\n");
74  return false;
75  }
76  else
77  BGDBG(0, "trackerServer found!\n");
78 
79  return true;
80 }
81 
82 void
84 
85  data.clear();
86  rawData.clear();
87  currStep = 0;
88 }
89 
90 unsigned int
92 
93  return numTotalSteps;
94 }
95 
96 unsigned int
98 
99  return currStep;
100 }
101 
102 std::string
104 
105  if(step <= NUM_TOTAL_STEPS && step >= 0)
106  return CAL_STEP_TEXT[step];
107  else{
108  return CAL_DEFAULT_TEXT;
109  }
110 }
111 
112 std::string
114 
115  if(step <= NUM_TOTAL_STEPS && step >= 0)
116  return CAL_STEP_PIC[step];
117  else{
118  return CAL_DEFAULT_PIC;
119  }
120 }
121 
122 std::vector<double>
124 
125  currStep++;
126  std::vector<double> c;
127  IPRFilterModule::TimeDataVector measure;
128 
130  try{
131  measure = *(myTracker->getRawData());
132  }catch(...){
133  BGDBG(0, "Glove server dead? Could not connect.\n");
134  return c;
135  }
136 
137  for (unsigned int i=0; i<measure.v.vector.length(); i++)
138  c.push_back(measure.v.vector[i]);
139 
140  rawData.push_back(c);
141 
142  return c;
143 }
144 
147 
148  return data;
149 }
150 
151 bool
153 
154  data = g;
155  return true;
156 }
157 
158 bool
160 
162  if(data.size() != NUM_SENSORS)
163  return false;
164 
165  return true;
166 }
167 
168 bool
169 cData::saveCalibration(std::string fileName){
170 
171  FILE *f;
172 
173  // open file
174  if ((f=fopen(fileName.c_str(), "w")) == NULL)
175  {
176  BGDBG(0, "save calibration file - error: open file !\n");
177  return false;
178  }
179 
180  for (unsigned int i=0; i<data.size(); i++){
181 
182  for (unsigned int j=0; j<data[i].size(); j++){
183  fprintf(f, "%f:%f", (data[i])[j].first, (data[i])[j].second);
184  if(j < data[i].size()-1)
185  fprintf(f, " ");
186  else
187  fprintf(f, "\n");
188  }
189  }
190  fclose(f);
191 
192  BGDBG(0, "Saved calibration successfully!\n");
193 
194  return true;
195 }
196 
197 
198 bool
199 cData::openCalibration(std::string fileName){
200 
201  std::ifstream inputFile (fileName.c_str());
202  if (!inputFile.is_open()){
203  BGDBG(0, "open calibration file - error while opening file !\n");
204  return false;
205  }
206 
207 
208  // split into lines
209  bg::strlist lines = bg::string(inputFile).split("\n");
210  trackerCal tcal;
211 
212  // parse lines
213  for (unsigned int lineNo=0; lineNo<lines.size(); lineNo++){
214 
215  // split into fulcri
216  bg::strlist fulcs = lines[lineNo].split(" ");
217  sensorCal scal;
218 
219  // parse fulcri
220  for(unsigned int fulcNo=0; fulcNo<fulcs.size(); fulcNo++){
221 
222  fulcrum f;
223  bg::strlist oneF = fulcs[fulcNo].split(":");
224  if(oneF.size() != 2){
225  BGDBG(0, "Read calibration file - error while reading file. Incomplete data.\n");
226  return false;
227  }
228  f.first = oneF[0].toDouble();
229  f.second = oneF[1].toDouble();
230  scal.push_back(f);
231  }
232  tcal.push_back(scal);
233  }
234 
235 
236  BGDBG(0, "Opened calibration file successfully!\n");
237  BGDBG(3, "File contained %d lines, first line contained %d entries.\n", tcal.size(), tcal[0].size());
238 
239  data = tcal;
240 
241  return true;
242 }
243 
244 bool
245 cData::checkConsistency(std::string &print){
246 
247  std::vector<unsigned int> bad;
248  std::stringstream s;
249 
250  for (unsigned int i=0; i<data.size(); i++){
251 
252  if(fabs(data[i][1].second - data[i][0].second) < MIN_JOINT_RESOLUTION){
253 
254  bad.push_back(i);
255  }
256  }
257 
258  if(!bad.empty()){
259 
260  BGDBG(3, "Calibration data not ok.\n");
261  s << "********************************************************************\n";
262  s << "Bad calibration data.\n";
263  for(unsigned int i=0; i<bad.size(); i++){
264  s << "Joint " << JOINT_NAME[bad[i]] << " does not achieve minimum resolution.\n";
265  s << "Joint resolution: " << (int)fabs(data[bad[i]][1].second - data[bad[i]][0].second)
266  << " Minimum resolution: " << MIN_JOINT_RESOLUTION << "\n";
267  if(fabs(data[bad[i]][1].second - data[bad[i]][0].second) < 2)
268  s << "GloveServer will bail out when reading this data!\n";
269  s << "\n";
270  }
271  s << "It is strongly recommended to repeat the calibration.\n";
272  s << "********************************************************************\n";
273 
274  print = std::string(s.str());
275  return false;
276  }
277  else{
278  s << "Calibration data ok.\n";
279  print = std::string(s.str());
280  BGDBG(3, "Calibration data ok.\n");
281  }
282 
283  return true;
284 }
285 
286 bool
288  /*
289  tracker::allSensorCal calib;
290  calib.length(data.size());
291 
293  for (unsigned int i=0; i<data.size(); i++){
294 
295  tracker::oneSensorCal osc;
296  osc.length(data[i].size());
297 
299  for (unsigned int j=0; j<data[i].size(); j++){
300 
301  tracker::fulcrum f;
302  f.angle = data[i][j].first;
303  f.sensorVal = data[i][j].second;
304  osc[j] = f;
305  }
306  calib[i] = osc;
307  }
308  bool ret;
309  try{
310  ret = myTracker->loadCalibration(calib);
311  }
312  catch(...){
313  BGDBG(0, "TracerServer dead? Could not connect.\n");
314  ret = false;
315  }
316  return ret;*/
317  return false;
318 }
319 
320 
322 cData::computeCalibration(std::vector<std::vector<double> > r){
323 
325  trackerCal c;
326  sensorCal sc;
327  fulcrum f;
328 
330  std::vector<double> a1, a2;
331  for (int i=0; i<NUM_SENSORS; i++){
332  // a1.push_back(GLOVE_RIGHT_VEC_1[i]);
333  // a2.push_back(GLOVE_RIGHT_VEC_2[i]);
334  }
335 
337  if(r.size() != NUM_TOTAL_STEPS){
338 
339  BGDBG(0, "Error in computeCalibration(). Input data not ok.\n");
340  return c;
341  }
342 
345  sc.clear();
346  f.first = a1[0];
347  f.second = r[6][0];
348  sc.push_back(f);
349  f.first = a2[0];
350  f.second = r[7][0];
351  sc.push_back(f);
352  c.push_back(sc);
353 
355  sc.clear();
356  f.first = a1[1];
357  f.second = r[4][1];
358  sc.push_back(f);
359  f.first = a2[1];
360  f.second = r[5][1];
361  sc.push_back(f);
362  c.push_back(sc);
363 
366  sc.clear();
367  f.first = a1[2];
368  f.second = r[0][2];
369  sc.push_back(f);
370  f.first = a2[2];
371  f.second = r[3][2];
372  sc.push_back(f);
373  c.push_back(sc);
374 
376  sc.clear();
377  f.first = a1[3];
378  f.second = r[1][3];
379  sc.push_back(f);
380  f.first = a2[3];
381  f.second = r[3][3];
382  sc.push_back(f);
383  c.push_back(sc);
384 
385  sc.clear();
386  f.first = a1[4];
387  f.second = r[1][4];
388  sc.push_back(f);
389  f.first = a2[4];
390  f.second = r[3][4];
391  sc.push_back(f);
392  c.push_back(sc);
393 
394  sc.clear();
395  f.first = a1[5];
396  f.second = r[1][5];
397  sc.push_back(f);
398  f.first = a2[5];
399  f.second = r[3][5];
400  sc.push_back(f);
401  c.push_back(sc);
402 
404  sc.clear();
405  f.first = a1[6];
406  f.second = r[0][6];
407  sc.push_back(f);
408  f.first = a2[6];
409  f.second = r[2][6];
410  sc.push_back(f);
411  c.push_back(sc);
412 
414  sc.clear();
415  f.first = a1[7];
416  f.second = r[0][7];
417  sc.push_back(f);
418  f.first = a2[7];
419  f.second = r[1][7];
420  sc.push_back(f);
421  c.push_back(sc);
422 
423  sc.clear();
424  f.first = a1[8];
425  f.second = r[0][8];
426  sc.push_back(f);
427  f.first = a2[8];
428  f.second = r[2][8];
429  sc.push_back(f);
430  c.push_back(sc);
431 
432  sc.clear();
433  f.first = a1[9];
434  f.second = r[0][9];
435  sc.push_back(f);
436  f.first = a2[9];
437  f.second = r[2][9];
438  sc.push_back(f);
439  c.push_back(sc);
440 
442  sc.clear();
443  f.first = a1[10];
444  f.second = r[0][10];
445  sc.push_back(f);
446  f.first = a2[10];
447  f.second = r[2][10];
448  sc.push_back(f);
449  c.push_back(sc);
450 
452  sc.clear();
453  f.first = a1[11];
454  f.second = r[0][11];
455  sc.push_back(f);
456  f.first = a2[11];
457  f.second = r[1][11];
458  sc.push_back(f);
459  c.push_back(sc);
460 
461  sc.clear();
462  f.first = a1[12];
463  f.second = r[0][12];
464  sc.push_back(f);
465  f.first = a2[12];
466  f.second = r[2][12];
467  sc.push_back(f);
468  c.push_back(sc);
469 
470  sc.clear();
471  f.first = a1[13];
472  f.second = r[0][13];
473  sc.push_back(f);
474  f.first = a2[13];
475  f.second = r[2][13];
476  sc.push_back(f);
477  c.push_back(sc);
478 
480  sc.clear();
481  f.first = a1[14];
482  f.second = r[0][14];
483  sc.push_back(f);
484  f.first = a2[14];
485  f.second = r[2][14];
486  sc.push_back(f);
487  c.push_back(sc);
488 
490  sc.clear();
491  f.first = a1[15];
492  f.second = r[0][15];
493  sc.push_back(f);
494  f.first = a2[15];
495  f.second = r[1][15];
496  sc.push_back(f);
497  c.push_back(sc);
498 
499  sc.clear();
500  f.first = a1[16];
501  f.second = r[0][16];
502  sc.push_back(f);
503  f.first = a2[16];
504  f.second = r[2][16];
505  sc.push_back(f);
506  c.push_back(sc);
507 
508  sc.clear();
509  f.first = a1[17];
510  f.second = r[0][17];
511  sc.push_back(f);
512  f.first = a2[17];
513  f.second = r[2][17];
514  sc.push_back(f);
515  c.push_back(sc);
516 
518  sc.clear();
519  f.first = a1[18];
520  f.second = r[0][18];
521  sc.push_back(f);
522  f.first = a2[18];
523  f.second = r[2][18];
524  sc.push_back(f);
525  c.push_back(sc);
526 
528  sc.clear();
529  f.first = a1[19];
530  f.second = r[0][19];
531  sc.push_back(f);
532  f.first = a2[19];
533  f.second = r[1][19];
534  sc.push_back(f);
535  c.push_back(sc);
536 
537  sc.clear();
538  f.first = a1[20];
539  f.second = r[0][20];
540  sc.push_back(f);
541  f.first = a2[20];
542  f.second = r[2][20];
543  sc.push_back(f);
544  c.push_back(sc);
545 
546  sc.clear();
547  f.first = a1[21];
548  f.second = r[0][21];
549  sc.push_back(f);
550  f.first = a2[21];
551  f.second = r[2][21];
552  sc.push_back(f);
553  c.push_back(sc);
554 
555  return c;
556 }
557 
558 
559 
560 #if cData_test
561 #include <stdio.h>
562 int main(int argc, char **argv)
563 {
564  // This is a module-test block. You can put code here that tests
565  // just the contents of this C file, and build it by saying
566  // make cData_test
567  // Then, run the resulting executable (cData_test).
568  // If it works as expected, the module is probably correct. ;-)
569 
570  fprintf(stderr, "Testing cData\n");
571 
572  return 0;
573 }
574 #endif /* cData_test */
trackerCal data
calibration data
Definition: cData.h:77
const char * CAL_STEP_PIC[]
Definition: cData.cc:44
f
void reset()
Definition: cData.cc:83
#define MIN_JOINT_RESOLUTION
Definition: cData.cc:29
const char * CAL_STEP_TEXT[]
Definition: cData.cc:32
#define NUM_TOTAL_STEPS
Definition: cData.cc:28
bool openCalibration(std::string fileName)
Definition: cData.cc:199
int main(int argc, char **argv)
XmlRpcServer s
cData()
Definition: cData.cc:56
trackerCal getCalibration()
Definition: cData.cc:146
unsigned int currStep
Definition: cData.h:81
std::pair< double, double > fulcrum
Definition: cData.h:44
bool setCalibration(trackerCal g)
Definition: cData.cc:152
const char * JOINT_NAME[]
for debug output
bool checkConsistency(std::string &print)
Definition: cData.cc:245
unsigned int numTotalSteps
current and total step number
Definition: cData.h:80
bool saveCalibration(std::string fileName)
Definition: cData.cc:169
trackerCal computeCalibration(std::vector< std::vector< double > > r)
Definition: cData.cc:322
unsigned int getNumTotalSteps()
Definition: cData.cc:91
bool connectToTrackerServer(std::string trackerServerName)
Definition: cData.cc:67
std::string getPicForStep(int)
Definition: cData.cc:113
std::vector< sensorCal > trackerCal
Definition: cData.h:46
const char * CAL_DEFAULT_PIC
Definition: cData.cc:43
~cData()
Definition: cData.cc:62
#define NUM_SENSORS
Definition: cData.cc:27
std::vector< double > calibrateNextStep()
Definition: cData.cc:123
unsigned int getCurrentStep()
Definition: cData.cc:97
std::vector< fulcrum > sensorCal
Definition: cData.h:45
std::string getTextForStep(int)
Definition: cData.cc:103
const char * CAL_DEFAULT_TEXT
Definition: cData.cc:31
std::vector< std::vector< double > > rawData
recorded raw data
Definition: cData.h:74
bool calibrate()
Definition: cData.cc:159
bool tellServerLoadCurrentCalibration()
Definition: cData.cc:287


asr_flock_of_birds
Author(s): Bernhardt Andre, Engelmann Stephan, Giesler Björn, Heller Florian, Jäkel Rainer, Nguyen Trung, Pardowitz Michael, Weckesser Peter, Yi Xie, Zöllner Raoul
autogenerated on Mon Jun 10 2019 12:44:40