bird_track_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 "bird_track_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 {
56  data point;
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 hemispherecmd[] = {'L',0x00,0x00}; // tracker have to be in front of the sensor
93  static unsigned char align2cmd[] = {'q',0xFF,0x3F, 0x00,0x00, 0x00,0x80};
94  static unsigned char reframecmd[] = {'P',0x18, 0x00,0x00, 0x00,0x80, 0x00,0x00};
95  static unsigned char birdsleep[] = {'G'};
96 
97 
98  cout << "Autoconfig command wird an den ERC gesendet." << endl;
99  cout << "Tracker wird (werden) kurz gestartet..." << endl;
100  serial_object->write_serial(fd,&_erc, 1);
101  serial_object->write_serial(fd,birdchangevaluecmd,3);
102  sleep(1);
103 
104  cout << "Hemisphere command wird an die Snsrs gesendet." << endl;
105  serial_object->write_serial(fd,&_sensor1, 1);
106  serial_object->write_serial(fd,hemispherecmd, 3);
107  serial_object->write_serial(fd,&_sensor2, 1);
108  serial_object->write_serial(fd,hemispherecmd, 3);
109  sleep(1);
110 
111  cout << "ref frame command wird an die Snsrs gesendet." << endl;
112  serial_object->write_serial(fd,&_sensor1, 1);
113  serial_object->write_serial(fd,align2cmd, 7);
114  serial_object->write_serial(fd,&_sensor2, 1);
115  serial_object->write_serial(fd,align2cmd, 7);
116  sleep(1);
117 
118 
119  cout << "ref frame command wird an den ERC gesendet." << endl;
120  serial_object->write_serial(fd,reframecmd, 8);
121  sleep(1);
122 
123 
124  cout << "... und wieder schlafen gelegt, damit wir später nur noch sleep und wakeup machen müssen." << endl;
125  serial_object->empty_serial(fd);
126  serial_object->write_serial(fd,birdsleep,1);
127 
128  refcount = 0;
129 }
130 
131 int
133 {
134  pthread_mutex_lock(&fobMutex);
135  if (refcount == 0)
136  {
137  static unsigned char birdwakeup[] = {'F'};
138  serial_object->empty_serial(fd);
139  usleep(10000);
140  serial_object->write_serial(fd,birdwakeup,1);
141  printf("Schicke wakeup.\n");
142  }
143  refcount++;
144 
145  pthread_mutex_unlock(&fobMutex);
146 
147  return refcount;
148 }
149 
150 int
152 {
153  pthread_mutex_lock(&fobMutex);
154  cout << "Stopping Tracker" << endl;
155 
156  if (refcount == 0){
157  pthread_mutex_unlock(&fobMutex);
158  return 0;
159  }
160  if ((refcount-1) == 0){
161  static unsigned char birdsleep[] = {'G'};
162  serial_object->empty_serial(fd);
163  serial_object->write_serial(fd,birdsleep,1);
164  printf("Schicke sleep.\n");
165  }
166  refcount--;
167 
168  pthread_mutex_unlock(&fobMutex);
169  return refcount;
170 }
171 
172 void
173 BirdTrack_impl::write_to_file(double *v, char *argv)
174 {
175  double x, y, z;
176  ofstream inf(argv, ios::ate | ios::app);
177  cout << "rele koordinaten [x y z]: ";
178  cin >> x;
179  cin >> y;
180  cin >> z;
181  if (inf.is_open())
182  {
183 
184  inf << x << '\t'
185  << y << '\t'
186  << z << '\t'
187  << v[0] << "\t"
188  << v[1] << "\t"
189  << v[2] << "\t"
190  << endl;
191  // << v[3] << " "
192  // << v[4] << " "
193  // << v[5] << endl;
194 
195  inf.close();
196  }
197 }
198 
199 int
200 BirdTrack_impl::get_rawposangles(int whichtracker, int *raw){
201 
202  //DEBUG
203  //cout << "Starting with get_rawposangles" <<endl;
204  pthread_mutex_lock(&fobMutex);
205 
206  if (refcount == 0){
207  cout << "get_rawposangles sagt refcount = " << refcount << endl;
208  pthread_mutex_unlock(&fobMutex);
209  return -1;
210  }
211 
212  short i;
213  short shortdata[12];
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 
222 
223 
224  if (whichtracker == LEFT_TRACKER_ID)
225  rs232tofbbcmd = (unsigned char)(0xF0 | (LEFT_TRACKER_ID + 1));
226 
227  else if (whichtracker == RIGHT_TRACKER_ID)
228  rs232tofbbcmd = (unsigned char)(0xF0 | (RIGHT_TRACKER_ID + 1));
229 
230  else{
231  cout << "Falsche id abgefragt: " << whichtracker << endl;
232  pthread_mutex_unlock(&fobMutex);
233  return (-1);
234  }
235 
236  pos_angl_cmd[0] = rs232tofbbcmd;
237  pos_angl_cmd[1] = 'Y';
238  pos_angl_cmd[2] = rs232tofbbcmd;
239  pos_angl_cmd[3] = 0x42;
240 
241  /* Es wird solange abgefragt, bis das erste Bit des ersten Bytes eine 1 hat.
242  Erst dann sind Daten vom Flock gesendet worden!
243  */
244  while(1)
245  {
246  serial_object->write_serial(fd,pos_angl_cmd,4);
247  serial_object->empty_serial(fd);
248  serial_object->read_serial(fd, (unsigned char*)birddata, 12);
249  if (birddata[0] & 0x80) break;
250  cout << "waiting..." <<endl;
251  }
252  birddata[0] &= 0x7F; // setze die erste 1 auf 0; jetzt steht vor jedem Byte als erstes eine Null
253  myUnion myPoint;
254 
255  for (i=0; i<12; i=i+2)
256  birddata[i] <<= 1; // verschiebe alle LS Byte um eins nach links!
257 
258  for (i=0; i<=10; i=i+2) {
259  myPoint.point.mSigB = birddata[i+1];
260  myPoint.point.lSigB = birddata[i];
261  shortdata[i/2] = myPoint.shortPoint; // mache LS Byte und MS Byte
262  }
263 
264  for (i=0; i<6; i++){
265  //das ganze nochmal um eins nach links, da sonst an erster Stelle eine Null stehen würde
266  shortdata[i] <<= 1;
267  raw[i] = (int) shortdata[i]; // caste alles als int
268  }
269 
270  pthread_mutex_unlock(&fobMutex);
271  return 1;
272 
273 }
274 
275 
276 int
277 BirdTrack_impl::get_posangles(int whichtracker, double *v){
278 
279  short i;
280  int intdata[6];
281  //DEBUG
282  //cout << "get_posangles called" <<endl;
283 
284  get_rawposangles(whichtracker, intdata);
285 
286 
287  for (i=0; i<3; i++){
288  //siehe Dokumentation von FloB (Umrechnung ist individuell) // millimeter
289  v[i] = ( (double)intdata[i]) * 0.11162109375;//( ( ( (double)intdata[i])*144.0) / 32768.0) * 2.54;
290  }
291 
292  for (i=3; i<6; i++)
293  {
294  // grad
295  v[i] = ( (double)intdata[i]) * 0.0054931640625;//(double) ((intdata[i] * 180) / 32768);
296  }
297 
298 
299  pthread_mutex_unlock(&fobMutex);
300  //DEBUG
301  //cout << "get_posangles finished" <<endl;
302  return 1;
303 }
304 
305 int
307 {
308  double x, y, z, rx, ry, rz;
309 // double InterTabelleX[1000];
310  x = (-1.0)*vec[0] * 10;
311  y = (+1.0)*vec[1] * 10;
312  z = (-1.0)*vec[2] * 10;
313  vec[0] = x; //+ KOORD_OFFSET_X;
314  vec[1] = y; // + KOORD_OFFSET_Y;
315  vec[2] = z; // + KOORD_OFFSET_Z;
316 
317  rx = vec[5];
318  ry = vec[4];
319  rz = vec[3];
320 
321  /* if (transform_transformation(&rx, &ry, &rz) == 0)
322  {
323  printf("Transform Transformation abkack - not my problem. Bye, Steffen.\n");
324  exit(1);
325  }
326  */
327 
328  vec[3] = rx * M_PI / 180.0;
329  vec[4] = ry * M_PI / 180.0;
330  vec[5] = rz * M_PI / 180.0;
331 
332 
333 
334  //Interpolation
335 
336  // interTabelleX
337 
338  return(1);
339 }
340 
341 
342 
343 /* end of not implemented yet :-) *g* */
344 
345 
346 
347 #if BirdTrack_impl_test
348 
349 #include <bgcorba_impl.h>
350 #include "IPRFilter.hh"
351 #include "NotificationManager.hh"
352 #include "NotificationReceiver.hh"
353 
354 int main(int argc, char **argv)
355 {
356  bgcorba::init(argc, argv);
357 
358  // Create a new instance of the implementation
359  BirdTrack_impl *impl = new BirdTrack_impl;
360 
361  int retval = bgcorba::main(impl);
362 
363  delete impl;
364 
365  return retval;
366 }
367 #endif /* BirdTrack_impl_test */
368 
int main(int argc, char **argv)
void initialize_Tracker()
unsigned char lSigB
#define LEFT_TRACKER_ID
TFSIMD_FORCE_INLINE const tfScalar & y() const
int polhemus_coord_to_world_coord(double *vec)
void write_to_file(double *v, char *argv)
int get_rawposangles(int whichtracker, int *raw)
TFSIMD_FORCE_INLINE const tfScalar & x() const
#define RIGHT_TRACKER_ID
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