kogmo_fob/trackerServer/tracker_impl.cc
Go to the documentation of this file.
1 
18 /* system includes */
19 #include <unistd.h>
20 #include <stdio.h>
21 #include <sys/time.h>
22 #include <sstream>
23 #include <vector>
24 #include <algorithm>
25 /* my includes */
26 
27 #include "BirdTrack_impl.h"
28 #include "tracker_impl.h"
29 #include "transformCoords.h"
30 
31 #define DOUBLE_VECTOR_LENGTH 6
32 
33 #define NOTE_KEY_1 "X"
34 #define NOTE_KEY_2 "Y"
35 #define NOTE_KEY_3 "Z"
36 #define NOTE_KEY_4 "RX"
37 #define NOTE_KEY_5 "RY"
38 #define NOTE_KEY_6 "RZ"
39 
40 extern unsigned int debugLevel;
41 
42 tracker_impl::tracker_impl(trackerID t, BirdTrack_impl* b, std::string trackerName)
43 {
44  unsigned int err;
46  printf("connecting to rtdb...\n");
47  //prepare dbinfo structure
48  err = kogmo_rtdb_connect_initinfo (&dbinfo, "", trackerName.c_str(), 0.02); DIEonERR(err);
49  //use dbinfo structure to connect and save connection handle in dbc
50  pid = kogmo_rtdb_connect (&dbc, &dbinfo); DIEonERR(pid);
51  //prepare the dataobj_info structure
52  err = kogmo_rtdb_obj_initinfo (dbc, &dataobj_info_data,trackerName.c_str(), KOGMO_RTDB_OBJTYPE_E1_FOBTRACKER,
53  sizeof (kogmo_rtdb_obj_e1_fobtracker_t)); DIEonERR(err);
54  std::string trackerName_raw = trackerName;
55  trackerName_raw+="_raw";
56  err = kogmo_rtdb_obj_initinfo (dbc, &dataobj_info_raw,trackerName_raw.c_str(), KOGMO_RTDB_OBJTYPE_E1_FOBTRACKER_RAW,
57  sizeof (kogmo_rtdb_obj_e1_fobtracker_t)); DIEonERR(err);
58 
59  // send it to the database -> tell the database what kind of data we send
60  oid = kogmo_rtdb_obj_insert (dbc, &dataobj_info_data); DIEonERR(oid);
61  oid = kogmo_rtdb_obj_insert (dbc, &dataobj_info_raw); DIEonERR(oid);
62  // prepare the actual data object
63  dataobj_data = (kogmo_rtdb_obj_e1_fobtracker_t *) malloc(dataobj_info_data.size_max);
64  err = kogmo_rtdb_obj_initdata (dbc, &dataobj_info_data, dataobj_data); DIEonERR(err);
65 
66 
67  printf("done!\n");
68 
70  myID = t;
71  myBird = b;
72 
74  threadRunning = false;
76 
77 
78 }
79 
81  threadRunning = false;
82  // disconnect database connection and remove the data object
83  kogmo_rtdb_obj_delete (dbc, &dataobj_info_data);
84  kogmo_rtdb_obj_delete (dbc, &dataobj_info_raw);
85  kogmo_rtdb_disconnect (dbc, NULL);
86 
87  free(dataobj_raw);
88  free(dataobj_data);
89 }
90 
91 
92 void
94 {
95  tracker_impl* myobj = (tracker_impl*)arg;
96  printf("static WorkerThread method called!\n");
97 
98  myobj->workerThread();
99  printf( "Worker thread exit.\n");
100  return NULL;
101 }
102 
103 
104 
105 #define MAX_SETS 1
106 #define PAUSE_SET 30000
107 #define PAUSE_WRITE 30000
108 
109 double getMedian(double (&vAcc)[MAX_SETS][6], unsigned int index){
110  if (MAX_SETS == 1)
111  return vAcc[0][index];
112 
113  std::vector<double> data;
114  for (unsigned int i=0; i<MAX_SETS; i++)
115  data.push_back(vAcc[i][index]);
116  std::sort(data.begin(), data.end());
117 
118  if (MAX_SETS % 2 == 0)
119  return ((data[MAX_SETS / 2 - 1] + data[MAX_SETS / 2]) / 2);
120  else
121  return data[(MAX_SETS - 1) / 2];
122 }
123 
124 
125 void
127 
128  double v[6];
129  double vAcc[MAX_SETS][6];
130 
131  while(threadRunning){
132 
133 
135  for (unsigned int i=0; i<MAX_SETS; i++){
137  myBird->get_posangles(myID, vAcc[i]);
138 
139  if (i == 0)
140  {
141  //post raw data for the calibration app
142  if (debugLevel > 2)
143  printf("RAW ");
145  }
146 
147  if (i+1 < MAX_SETS)
148  usleep(PAUSE_SET);
149  }
150 
151  for (unsigned int i=0; i<6; i++){
152  v[i] = getMedian(vAcc, i);
153  }
154 
155  //apply calibration
157 
158 
159  if (debugLevel > 0 )
160  {
161  printf( "%s %3.3lf %3.3lf %3.3lf %3.3lf %3.3lf %3.3lf ",
162  myID == leftTracker ? "LEFT" : "RIGHT",
163  v[0], v[1], v[2], v[3], v[4], v[5]);
164 
165  if (myID == rightTracker)
166  printf("\n");
167  }
168  //Vertauschen der beiden Achsen - ist halt so ;-)
169  double temp = v[5];
170  v[5] = v[3];
171  v[3] = temp;
172 
173 
174 
175 
176  //post calibrated data for regular usage
177  if (debugLevel > 2)
178  printf("CAL ");
180  printf( "Couldn't write Notification to NotificationManager. Error!\n");
181  exit(-1);
182  }
183 
184  usleep(PAUSE_WRITE);
185  }
186  threadRunning = false;
187 }
188 
189 void
191  printf( "Starting myBird\n");
192  myBird->start();
193 
195  if(!threadRunning){
196  threadRunning = true;
197  pthread_create(&workerThreadHandle,NULL, &s_workerThread, (void*)this);
198  }
199  else
200  printf( "Thread started, but was already running.\n");
201  printf( "Thread running.\n");
202 }
203 
204 void
206  printf( "Stopping myBird\n");
207  myBird->stop();
208 
210  threadRunning = false;
211 }
212 
213 
214 bool
215 tracker_impl::loadCalibFile(const char *srcFileName)
216 {
217  //original: return myTransformer->loadCalibFile(srcFileName);
218  //original: myTransformer->loadCalibFile: no implemented
219  //this code taken from trackerCalibration
220  bool res;
221  std::string filename;
222  std::stringstream s;
223 
224  int fcountLeft, fcountRight;
225  fulcrum fulcrumarrayLeft[255], fulcrumarrayRight[255];
226  FILE *f;
227  res = ((f=fopen(srcFileName, "r"))!=NULL);
228  if (res)
229  {
230  fread(&fcountLeft,sizeof(fcountLeft),1,f);
231  fread(&fcountRight,sizeof(fcountRight),1,f);
232 
233  if (debugLevel > 0) {
234  printf("loadCalibFile %s\n",srcFileName);
235  printf("fcountLeft:%i, fcountRight:%i\n",fcountLeft,fcountRight);
236  }
237  fread(&fulcrumarrayLeft,sizeof(fulcrumarrayLeft[0]),fcountLeft,f);
238  fread(&fulcrumarrayRight,sizeof(fulcrumarrayRight[0]),fcountRight,f);
239 
240  if (debugLevel > 1) {
241  printf("fulcrumyArrayLeft(%i):\n",fcountLeft);
242  for(int i=0;i < fcountLeft;i++) {
243  printf("\nentry %i:\n",i);
244  printFulcrum(fulcrumarrayLeft[i]);
245  }
246  printf("fulcrumyArrayRight(%i):\n",fcountRight);
247  for(int i=0;i < fcountRight;i++) {
248  printf("\nentry %i:\n",i);
249  printFulcrum(fulcrumarrayRight[i]);
250  }
251  }
252 //loading from file works. storage in array works
253  s << "Loaded " << (fcountLeft+fcountRight) << " fulcrums\n";
254  fclose(f);
255  }
256  else {
257  printf("error loading calibration data: 2%s:%u\n",__FILE__,__LINE__);
258  return false;
259  }
260 
261  if (myID == rightTracker) {
262  if (debugLevel > 0)
263  printf("loading Calibration into rightTracker\n");
264  res = loadCalibration(fulcrumarrayRight, fcountRight);
265  } else {
266  if (debugLevel > 0)
267  printf("loading Calibration into leftTracker\n");
268  res = loadCalibration(fulcrumarrayLeft, fcountLeft);
269  }
270  if(res)
271  printf("Success! Calibration data loaded\n");
272  else
273  printf("error loading calibration data: %s:%u\n",__FILE__,__LINE__);
274 
275  return res;
276 }
277 
278 
280  printf("sensorVal: %3.3f, %3.3f, %3.3f\n",fulc.sensorVal[0] ,fulc.sensorVal[1], fulc.sensorVal[2]);
281  printf("sensorAngle: %3.3f, %3.3f, %3.3f\n",fulc.sensorAngle[0] ,fulc.sensorAngle[1], fulc.sensorAngle[2]);
282  printf("worldVal: %3.3f, %3.3f, %3.3f\n",fulc.worldVal[0] ,fulc.worldVal[1], fulc.worldVal[2]);
283  printf("worldAngle: %3.3f, %3.3f, %3.3f\n",fulc.worldAngle[0] ,fulc.worldAngle[1], fulc.worldAngle[2]);
284  }
285 
286 bool
287 tracker_impl::loadCalibration(const fulcrum cal[255], unsigned int length)
288 {
289  double world[6],sensor[6];
291  for (unsigned int i=0; i < length; i++)
292  {
293  for (int j=0;j<3;j++)
294  {
295  sensor[j]=cal[i].sensorVal[j];
296  sensor[j+3]=cal[i].sensorAngle[j];
297  world[j]=cal[i].worldVal[j];
298  world[j+3]=cal[i].worldAngle[j];
299  }
300  myTransformer->addCalibrationData(sensor,world);
301  }
302  return myTransformer->isCalibrated();
303 }
304 
305 bool
306 tracker_impl::writeNotification(kogmo_rtdb_obj_info_t &dataobj_info, double* data) { //double *data) {
307  unsigned int i;
308  unsigned int err;
309 
310 
311 
312  dataobj_data->data.trackerID = myID;
313  for (i=0; i<6; i++) {
314  dataobj_data->data.data[i] = data[i];
315  }
316 
317  if (myID == leftTracker)
318  if (debugLevel > 2 )
319  printf( "LEFT %3.3lf %3.3lf %3.3lf %3.3lf %3.3lf %3.3lf\n",
320  data[0], data[1], data[2], data[3], data[4], data[5]);
321  if (myID == rightTracker)
322  if (debugLevel > 2 )
323  printf( "RIGHT %3.3lf %3.3lf %3.3lf %3.3lf %3.3lf %3.3lf\n",
324  data[0], data[1], data[2], data[3], data[4], data[5]);
325 
326  // write data to database - if this crashes choose dataobj_raw for dataobj_info_raw
327  err = kogmo_rtdb_obj_writedata (dbc, dataobj_info.oid, dataobj_data); DIEonERR(err);
328 
329  //tell the database that one cycle is finished and that we are still alive. This is important!
330  kogmo_rtdb_cycle_done(dbc,0);
331 
332  return true;
333 }
334 
335 
336 
337 
338 
339 #if tracker_impl_test
340 int main(int argc, char **argv)
341 {
342  bgcorba::init(argc, argv);
343 
344  // Create a new instance of the implementation
345  tracker_impl *impl = new tracker_impl;
346 
347  int retval = bgcorba::main(impl);
348 
349  delete impl;
350 
351  return retval;
352 }
353 #endif /* tracker_impl_test */
354 
filename
f
kogmo_rtdb_obj_info_t dataobj_info_data
int main(int argc, char **argv)
XmlRpcServer s
bool threadRunning
flag switching on/off thread
kogmo_rtdb_obj_info_t dataobj_info_raw
void workerThread()
this method is called by static method s_workerthread
static void * s_workerThread(void *arg)
Thread is bound to this method.
bool loadCalibration(const fulcrum cal[255], unsigned int length)
#define DIEonERR(value)
bool loadCalibFile(const char *srcFileName)
kogmo_rtdb_obj_e1_fobtracker_t * dataobj_data
tracker_impl(trackerID t, BirdTrack_impl *b, std::string trackerName)
kogmo_rtdb_connect_info_t dbinfo
bool addCalibrationData(double *fobCoords, double *worldCoords)
pthread_t workerThreadHandle
Thread for delivering tracker data to nCenter.
bool writeNotification(trackerID id, double *raw, double *calibrated)
void transform(double *v)
unsigned int debugLevel
BirdTrack_impl * myBird
kogmo_rtdb_obj_e1_fobtracker_t * dataobj_raw
int get_posangles(int whichtracker, double *v)
double getMedian(double(&vAcc)[MAX_SETS][6], unsigned int index)
transformCoords * myTransformer


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