BirdTrack_impl.cc
Go to the documentation of this file.
1 
19 /* system includes */
20 /* (none) */
21 #include <iostream>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <time.h>
25 #include <fstream>
26 #include <math.h>
27 
28 /* my includes */
29 #include "BirdTrack_impl.h"
30 
31 /* my defines */
32 #define ANGK (double)(180.0/32758.0)
33 
34 // Achtung: nicht umdefinieren, ein enum interessiert sich dafür!!
35 #define RIGHT_TRACKER_ID 1
36 #define LEFT_TRACKER_ID 2
37 
38 // Emitter unter dem Tisch auf nem Wasserkasten
39 #define POLHEMUS_POS_X 1284.6
40 #define POLHEMUS_POS_Y 1319.2
41 #define POLHEMUS_POS_Z -553.0
42 #define KOORD_OFFSET_X -16.0
43 #define KOORD_OFFSET_Y 65.0
44 #define KOORD_OFFSET_Z 0.0
45 
46 
47 //#define _DEBUG_
48 
49 struct data {
50  unsigned char lSigB;
51  unsigned char mSigB;
52 };
53 
54 
55 union myUnion {
57  short shortPoint;
58 };
59 
60 BirdTrack_impl::BirdTrack_impl(std::string serial_port)
61 {
62  printf( "try to open serialdev\n");
63  serial_object = new SerialDevice();
64 
65  fd = serial_object->open_serial(serial_port.c_str());
66 
67  pthread_mutex_init(&fobMutex,NULL);
68 
69  initialize_Tracker();
70 }
71 
73 {
74  while (refcount != 0)
75  stop();
76  serial_object->empty_serial(fd);
77  serial_object->close_serial(fd);
78  delete serial_object;
79  pthread_mutex_destroy(&fobMutex);
80 }
81 
82 void
84  _erc = (unsigned char)(0xF0 | 1);
85  _sensor1 = (unsigned char)(0xF0 | 2);
86  _sensor2 = (unsigned char)(0xF0 | 3);
87 
88  // see pdf
89  static unsigned char birdchangevaluecmd[] = {'P',50,3}; // auto configuration, 1 master at address 1 and 2 slaves at address 2 and 3
90  //static unsigned char hemispherecmd[] = {'L',0x0C,0x01}; // tracker have to be above the sensor
91  static unsigned char hemispherecmd[] = {'L',0x00,0x00}; // tracker have to be in front of the sensor
92  static unsigned char align2cmd[] = {'q',0xFF,0x3F, 0x00,0x00, 0x00,0x80};
93  static unsigned char reframecmd[] = {'P',0x18, 0x00,0x00, 0x00,0x80, 0x00,0x00};
94  static unsigned char birdsleep[] = {'G'};
95 
96  cout << "Autoconfig command wird an den ERC gesendet." << endl;
97  cout << "Tracker wird (werden) kurz gestartet..." << endl;
98  serial_object->write_serial(fd,&_erc, 1);
99  serial_object->write_serial(fd,birdchangevaluecmd,3);
100  sleep(1);
101 
102  cout << "Hemisphere command wird an die Snsrs gesendet." << endl;
103  serial_object->write_serial(fd,&_sensor1, 1);
104  serial_object->write_serial(fd,hemispherecmd, 3);
105  serial_object->write_serial(fd,&_sensor2, 1);
106  serial_object->write_serial(fd,hemispherecmd, 3);
107  sleep(1);
108 
109  cout << "ref frame command wird an die Snsrs gesendet." << endl;
110  serial_object->write_serial(fd,&_sensor1, 1);
111  serial_object->write_serial(fd,align2cmd, 7);
112  serial_object->write_serial(fd,&_sensor2, 1);
113  serial_object->write_serial(fd,align2cmd, 7);
114  sleep(1);
115 
116 
117  cout << "ref frame command wird an den ERC gesendet." << endl;
118  serial_object->write_serial(fd,reframecmd, 8);
119  sleep(1);
120 
121 
122  cout << "... und wieder schlafen gelegt, damit wir später nur noch sleep und wakeup machen müssen." << endl;
123  serial_object->empty_serial(fd);
124  serial_object->write_serial(fd,birdsleep,1);
125 
126  refcount = 0;
127 }
128 
129 int
131 {
132  pthread_mutex_lock(&fobMutex);
133  if (refcount == 0)
134  {
135  static unsigned char birdwakeup[] = {'F'};
136  serial_object->empty_serial(fd);
137  usleep(10000);
138  serial_object->write_serial(fd,birdwakeup,1);
139  printf("Schicke wakeup.\n");
140  }
141  refcount++;
142 
143  pthread_mutex_unlock(&fobMutex);
144 
145  return refcount;
146 }
147 
148 int
150 {
151  pthread_mutex_lock(&fobMutex);
152  cout << "Stopping Tracker" << endl;
153 
154  if (refcount == 0){
155  pthread_mutex_unlock(&fobMutex);
156  return 0;
157  }
158  if ((refcount-1) == 0){
159  static unsigned char birdsleep[] = {'G'};
160  serial_object->empty_serial(fd);
161  serial_object->write_serial(fd,birdsleep,1);
162  printf("Schicke sleep.\n");
163  }
164  refcount--;
165 
166  pthread_mutex_unlock(&fobMutex);
167  return refcount;
168 }
169 
170 void
171 BirdTrack_impl::write_to_file(double *v, char *argv)
172 {
173  double x, y, z;
174  ofstream inf(argv, ios::ate | ios::app);
175  cout << "rele koordinaten [x y z]: ";
176  cin >> x;
177  cin >> y;
178  cin >> z;
179  if (inf.is_open())
180  {
181 
182  inf << x << '\t'
183  << y << '\t'
184  << z << '\t'
185  << v[0] << "\t"
186  << v[1] << "\t"
187  << v[2] << "\t"
188  << endl;
189  // << v[3] << " "
190  // << v[4] << " "
191  // << v[5] << endl;
192 
193  inf.close();
194  }
195 }
196 
197 int
198 BirdTrack_impl::get_rawposangles(int whichtracker, int *raw){
199 
200  //DEBUG
201  //cout << "Starting with get_rawposangles" <<endl;
202  pthread_mutex_lock(&fobMutex);
203 
204  if (refcount == 0){
205  cout << "get_rawposangles sagt refcount = " << refcount << endl;
206  pthread_mutex_unlock(&fobMutex);
207  return -1;
208  }
209 
210  short i;
211  short realdata[6];
212  short shortdata[12];
213  float floatdata[6];
214  unsigned char rs232tofbbcmd, pos_angl_cmd[4];
215  char birddata[12]; //Bird-Data-Buffer
216 
217  for (i=0; i<12; i++) {
218  birddata[i] = 0;
219  shortdata[i] = 0;
220  }
221  for (i=0; i<6; i++) {
222  realdata[i] = 0;
223  floatdata[i] = 0.0;
224  }
225 
226 
227  if (whichtracker == LEFT_TRACKER_ID)
228  rs232tofbbcmd = (unsigned char)(0xF0 | LEFT_TRACKER_ID + 1);
229 
230  else if (whichtracker == RIGHT_TRACKER_ID)
231  rs232tofbbcmd = (unsigned char)(0xF0 | RIGHT_TRACKER_ID + 1);
232 
233  else{
234  cout << "Falsche id abgefragt: " << whichtracker << endl;
235  pthread_mutex_unlock(&fobMutex);
236  return (-1);
237  }
238 
239  pos_angl_cmd[0] = rs232tofbbcmd;
240  pos_angl_cmd[1] = 'Y';
241  pos_angl_cmd[2] = rs232tofbbcmd;
242  pos_angl_cmd[3] = 0x42;
243 
244  /* Es wird solange abgefragt, bis das erste Bit des ersten Bytes eine 1 hat.
245  Erst dann sind Daten vom Flock gesendet worden!
246  */
247  while(1)
248  {
249  serial_object->write_serial(fd,pos_angl_cmd,4);
250  serial_object->empty_serial(fd);
251  serial_object->read_serial(fd, (unsigned char*)birddata, 12);
252  if (birddata[0] & 0x80) break;
253  cout << "waiting..." <<endl;
254  }
255  birddata[0] &= 0x7F; // setze die erste 1 auf 0; jetzt steht vor jedem Byte als erstes eine Null
256  myUnion myPoint;
257 
258  for (i=0; i<12; i=i+2)
259  birddata[i] <<= 1; // verschiebe alle LS Byte um eins nach links!
260 
261  for (i=0; i<=10; i=i+2) {
262  myPoint.point.mSigB = birddata[i+1];
263  myPoint.point.lSigB = birddata[i];
264  shortdata[i/2] = myPoint.shortPoint; // mache LS Byte und MS Byte
265  }
266 
267  for (i=0; i<6; i++){
268  //das ganze nochmal um eins nach links, da sonst an erster Stelle eine Null stehen würde
269  shortdata[i] <<= 1;
270  raw[i] = (int) shortdata[i]; // caste alles als int
271  }
272 
273  pthread_mutex_unlock(&fobMutex);
274  return 1;
275 
276 }
277 
278 
279 int
280 BirdTrack_impl::get_posangles(int whichtracker, double *v){
281 
282  short i;
283  int intdata[6];
284  //DEBUG
285  //cout << "get_posangles called" <<endl;
286 
287  get_rawposangles(whichtracker, intdata);
288 
289 
290  for (i=0; i<3; i++){
291  //siehe Dokumentation von FloB (Umrechnung ist individuell) // millimeter
292  v[i] = ( (double)intdata[i]) * 0.11162109375;//( ( ( (double)intdata[i])*144.0) / 32768.0) * 2.54;
293  }
294 
295  for (i=3; i<6; i++)
296  {
297  // grad
298  v[i] = ( (double)intdata[i]) * 0.0054931640625;//(double) ((intdata[i] * 180) / 32768);
299  }
300 
301 
302  pthread_mutex_unlock(&fobMutex);
303  //DEBUG
304  //cout << "get_posangles finished" <<endl;
305  return 1;
306 }
307 
308 int
310 {
311  double x, y, z, rx, ry, rz;
312 // double InterTabelleX[1000];
313  x = (-1.0)*vec[0] * 10;
314  y = (+1.0)*vec[1] * 10;
315  z = (-1.0)*vec[2] * 10;
316  vec[0] = x; //+ KOORD_OFFSET_X;
317  vec[1] = y; // + KOORD_OFFSET_Y;
318  vec[2] = z; // + KOORD_OFFSET_Z;
319 
320  rx = vec[5];
321  ry = vec[4];
322  rz = vec[3];
323 
324  /* if (transform_transformation(&rx, &ry, &rz) == 0)
325  {
326  printf("Transform Transformation abkack - not my problem. Bye, Steffen.\n");
327  exit(1);
328  }
329  */
330 
331  vec[3] = rx * M_PI / 180.0;
332  vec[4] = ry * M_PI / 180.0;
333  vec[5] = rz * M_PI / 180.0;
334 
335 
336 
337  //Interpolation
338 
339  // interTabelleX
340 
341  return(1);
342 }
343 
344 
345 
346 /* end of not implemented yet :-) *g* */
347 
348 
349 
350 #if BirdTrack_impl_test
351 
352 #include <bgcorba_impl.h>
353 #include "IPRFilter.hh"
354 #include "NotificationManager.hh"
355 #include "NotificationReceiver.hh"
356 
357 int main(int argc, char **argv)
358 {
359  bgcorba::init(argc, argv);
360 
361  // Create a new instance of the implementation
362  BirdTrack_impl *impl = new BirdTrack_impl;
363 
364  int retval = bgcorba::main(impl);
365 
366  delete impl;
367 
368  return retval;
369 }
370 #endif /* BirdTrack_impl_test */
371 
int main(int argc, char **argv)
void initialize_Tracker()
unsigned char lSigB
TFSIMD_FORCE_INLINE const tfScalar & y() const
#define LEFT_TRACKER_ID
int polhemus_coord_to_world_coord(double *vec)
#define RIGHT_TRACKER_ID
void write_to_file(double *v, char *argv)
int get_rawposangles(int whichtracker, int *raw)
TFSIMD_FORCE_INLINE const tfScalar & x() const
BirdTrack_impl(std::string serial_device)
TFSIMD_FORCE_INLINE const tfScalar & z() const
int get_posangles(int whichtracker, double *v)
short shortPoint
unsigned char mSigB


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