bird_track_impl.cc
Go to the documentation of this file.
00001 
00019 /* system includes */
00020 /* (none) */
00021 #include <iostream>
00022 #include <stdio.h>
00023 #include <unistd.h>
00024 #include <time.h>
00025 #include <fstream>
00026 #include <math.h>
00027 
00028 /* my includes */
00029 #include "bird_track_impl.h"
00030 
00031 /* my defines */
00032 #define ANGK (double)(180.0/32758.0)
00033 
00034 // Achtung: nicht umdefinieren, ein enum interessiert sich dafür!!
00035 #define RIGHT_TRACKER_ID 1
00036 #define LEFT_TRACKER_ID  2
00037 
00038 // Emitter unter dem Tisch auf nem Wasserkasten
00039 #define POLHEMUS_POS_X 1284.6
00040 #define POLHEMUS_POS_Y 1319.2
00041 #define POLHEMUS_POS_Z -553.0
00042 #define KOORD_OFFSET_X -16.0
00043 #define KOORD_OFFSET_Y 65.0
00044 #define KOORD_OFFSET_Z 0.0
00045 
00046 
00047 //#define _DEBUG_
00048 
00049 struct data {
00050     unsigned char lSigB;
00051     unsigned char mSigB;
00052 };
00053          
00054 
00055 union myUnion {
00056     data point;
00057     short shortPoint;
00058 };
00059 
00060 BirdTrack_impl::BirdTrack_impl(std::string serial_port)
00061 {
00062   printf( "try to open serialdev\n");
00063   serial_object = new SerialDevice();
00064   
00065   fd = serial_object->open_serial(serial_port.c_str());
00066 
00067   pthread_mutex_init(&fobMutex,NULL);
00068 
00069   initialize_Tracker();
00070 }
00071 
00072 BirdTrack_impl::~BirdTrack_impl()
00073 {
00074     while (refcount != 0)
00075         stop();
00076     serial_object->empty_serial(fd);
00077     serial_object->close_serial(fd);    
00078     delete serial_object;
00079     pthread_mutex_destroy(&fobMutex);
00080 }
00081 
00082 void
00083 BirdTrack_impl::initialize_Tracker(void){
00084     _erc = (unsigned char)(0xF0 | 1);
00085     _sensor1 = (unsigned char)(0xF0 | 2);
00086     _sensor2 = (unsigned char)(0xF0 | 3);
00087 
00088     // see pdf
00089     static unsigned char birdchangevaluecmd[] = {'P',50,3}; // auto configuration, 1 master at address 1 and 2 slaves at address 2 and 3
00090     //static unsigned char hemispherecmd[] = {'L',0x0C,0x01}; // tracker have to be above the sensor
00091     static unsigned char hemispherecmd[] = {'L',0x00,0x00}; // tracker have to be in front of the sensor
00092     //static unsigned char hemispherecmd[] = {'L',0x00,0x00}; // tracker have to be in front of the sensor
00093     static unsigned char align2cmd[] = {'q',0xFF,0x3F, 0x00,0x00, 0x00,0x80};
00094     static unsigned char reframecmd[] = {'P',0x18, 0x00,0x00, 0x00,0x80, 0x00,0x00};
00095     static unsigned char birdsleep[] = {'G'};
00096    
00097 
00098     cout << "Autoconfig command wird an den ERC gesendet." << endl;
00099     cout << "Tracker wird (werden) kurz gestartet..." << endl;
00100     serial_object->write_serial(fd,&_erc, 1);
00101     serial_object->write_serial(fd,birdchangevaluecmd,3);
00102     sleep(1);
00103 
00104     cout << "Hemisphere command wird an die Snsrs gesendet." << endl;
00105     serial_object->write_serial(fd,&_sensor1, 1);
00106     serial_object->write_serial(fd,hemispherecmd, 3);
00107     serial_object->write_serial(fd,&_sensor2, 1);
00108     serial_object->write_serial(fd,hemispherecmd, 3);
00109     sleep(1);
00110     
00111     cout << "ref frame command wird an die Snsrs gesendet." << endl;
00112     serial_object->write_serial(fd,&_sensor1, 1);
00113     serial_object->write_serial(fd,align2cmd, 7);
00114     serial_object->write_serial(fd,&_sensor2, 1);
00115     serial_object->write_serial(fd,align2cmd, 7);
00116     sleep(1);
00117     
00118 
00119     cout << "ref frame command wird an den ERC gesendet." << endl;
00120     serial_object->write_serial(fd,reframecmd, 8);
00121     sleep(1);
00122     
00123 
00124     cout << "... und wieder schlafen gelegt, damit wir später nur noch sleep und wakeup machen müssen." << endl;
00125     serial_object->empty_serial(fd);
00126     serial_object->write_serial(fd,birdsleep,1);
00127         
00128     refcount = 0;
00129 }
00130 
00131 int
00132 BirdTrack_impl::start(void) 
00133 {
00134   pthread_mutex_lock(&fobMutex);
00135   if (refcount == 0)
00136     {
00137       static unsigned char birdwakeup[] = {'F'};
00138       serial_object->empty_serial(fd);
00139       usleep(10000);
00140       serial_object->write_serial(fd,birdwakeup,1);
00141       printf("Schicke wakeup.\n");
00142     }
00143   refcount++;
00144   
00145   pthread_mutex_unlock(&fobMutex);
00146 
00147   return refcount;
00148 }
00149 
00150 int
00151 BirdTrack_impl::stop(void)
00152 {
00153     pthread_mutex_lock(&fobMutex);
00154     cout << "Stopping Tracker" << endl; 
00155 
00156     if (refcount == 0){
00157         pthread_mutex_unlock(&fobMutex);
00158         return 0;
00159     }
00160     if ((refcount-1) == 0){
00161         static unsigned char birdsleep[] = {'G'};
00162         serial_object->empty_serial(fd);
00163         serial_object->write_serial(fd,birdsleep,1);
00164         printf("Schicke sleep.\n");
00165     }
00166     refcount--;
00167   
00168     pthread_mutex_unlock(&fobMutex);
00169     return refcount;
00170 }
00171 
00172 void
00173 BirdTrack_impl::write_to_file(double *v, char *argv)
00174 {
00175     double x, y, z;
00176     ofstream inf(argv, ios::ate | ios::app);
00177     cout << "rele koordinaten [x y z]: ";
00178     cin >> x;
00179     cin >> y;
00180     cin >> z;
00181     if (inf.is_open())
00182     {
00183 
00184         inf << x << '\t'
00185             << y << '\t'
00186             << z << '\t'
00187             << v[0] << "\t"
00188             << v[1] << "\t"
00189             << v[2] << "\t"
00190             << endl;
00191         //        << v[3] << " "
00192         //        << v[4] << " "
00193         //        << v[5] << endl;
00194  
00195         inf.close();
00196     }
00197 }
00198 
00199 int
00200 BirdTrack_impl::get_rawposangles(int whichtracker, int *raw){
00201 
00202   //DEBUG
00203   //cout << "Starting with get_rawposangles" <<endl;
00204   pthread_mutex_lock(&fobMutex);
00205 
00206     if (refcount == 0){
00207         cout << "get_rawposangles sagt refcount = " << refcount << endl;
00208         pthread_mutex_unlock(&fobMutex);
00209         return -1;
00210     }
00211         
00212     short i;
00213     short shortdata[12];
00214     unsigned char rs232tofbbcmd, pos_angl_cmd[4];
00215     char birddata[12]; //Bird-Data-Buffer
00216         
00217     for (i=0; i<12; i++) {
00218         birddata[i] = 0;
00219         shortdata[i] = 0;
00220     }
00221  
00222         
00223         
00224     if (whichtracker == LEFT_TRACKER_ID)
00225         rs232tofbbcmd = (unsigned char)(0xF0 | (LEFT_TRACKER_ID + 1));
00226                 
00227     else if (whichtracker == RIGHT_TRACKER_ID)
00228         rs232tofbbcmd = (unsigned char)(0xF0 | (RIGHT_TRACKER_ID + 1));
00229         
00230     else{
00231         cout << "Falsche id abgefragt: " << whichtracker << endl;
00232         pthread_mutex_unlock(&fobMutex);
00233         return (-1);
00234     }
00235                 
00236     pos_angl_cmd[0] = rs232tofbbcmd;
00237     pos_angl_cmd[1] = 'Y';
00238     pos_angl_cmd[2] = rs232tofbbcmd;
00239     pos_angl_cmd[3] = 0x42;
00240 
00241     /* Es wird solange abgefragt, bis das erste Bit des ersten Bytes eine 1 hat.
00242        Erst dann sind Daten vom Flock gesendet worden!
00243     */
00244     while(1)
00245     {
00246         serial_object->write_serial(fd,pos_angl_cmd,4);
00247         serial_object->empty_serial(fd);
00248         serial_object->read_serial(fd, (unsigned char*)birddata, 12);
00249         if (birddata[0] & 0x80) break;  
00250         cout << "waiting..." <<endl;  
00251     }
00252     birddata[0] &= 0x7F;  // setze die erste 1 auf 0; jetzt steht vor jedem Byte als erstes eine Null
00253     myUnion myPoint;
00254 
00255     for (i=0; i<12; i=i+2)
00256         birddata[i] <<= 1;  // verschiebe alle LS Byte um eins nach links!
00257 
00258     for (i=0; i<=10; i=i+2) {
00259         myPoint.point.mSigB = birddata[i+1];
00260         myPoint.point.lSigB = birddata[i];
00261         shortdata[i/2] = myPoint.shortPoint;  // mache LS Byte und MS Byte
00262     }
00263         
00264     for (i=0; i<6; i++){
00265         //das ganze nochmal um eins nach links, da sonst an erster Stelle eine Null stehen würde
00266         shortdata[i] <<= 1; 
00267         raw[i] = (int) shortdata[i];  // caste alles als int
00268     }
00269     
00270     pthread_mutex_unlock(&fobMutex);
00271     return 1;
00272 
00273 }
00274 
00275 
00276 int
00277 BirdTrack_impl::get_posangles(int whichtracker, double *v){
00278 
00279     short i;
00280     int intdata[6];
00281     //DEBUG
00282     //cout <<  "get_posangles called" <<endl;
00283 
00284     get_rawposangles(whichtracker, intdata);
00285 
00286         
00287     for (i=0; i<3; i++){
00288         //siehe Dokumentation von FloB (Umrechnung ist individuell) // millimeter
00289       v[i] = ( (double)intdata[i]) * 0.11162109375;//( ( ( (double)intdata[i])*144.0) / 32768.0) * 2.54;  
00290     }
00291 
00292     for (i=3; i<6; i++)
00293     {
00294       // grad
00295       v[i] = ( (double)intdata[i]) * 0.0054931640625;//(double) ((intdata[i] * 180) / 32768);
00296     }
00297 
00298     
00299     pthread_mutex_unlock(&fobMutex);
00300     //DEBUG
00301     //cout << "get_posangles finished" <<endl;
00302     return 1;
00303 }
00304 
00305 int
00306 BirdTrack_impl::polhemus_coord_to_world_coord(double *vec)
00307 {
00308     double x, y, z, rx, ry, rz;
00309 //  double InterTabelleX[1000];
00310     x = (-1.0)*vec[0] * 10;
00311     y = (+1.0)*vec[1] * 10;
00312     z = (-1.0)*vec[2] * 10;
00313     vec[0] = x; //+ KOORD_OFFSET_X;
00314     vec[1] = y; // + KOORD_OFFSET_Y;
00315     vec[2] = z; // + KOORD_OFFSET_Z;
00316    
00317     rx = vec[5];
00318     ry = vec[4];
00319     rz = vec[3];
00320     
00321     /*   if (transform_transformation(&rx, &ry, &rz) == 0)
00322          {
00323          printf("Transform Transformation abkack - not my problem. Bye, Steffen.\n");
00324          exit(1);
00325       }
00326     */
00327     
00328     vec[3] = rx *  M_PI / 180.0;
00329     vec[4] = ry *  M_PI / 180.0;
00330     vec[5] = rz *  M_PI / 180.0;
00331     
00332 
00333     
00334     //Interpolation
00335     
00336     //  interTabelleX
00337     
00338     return(1);
00339 }
00340 
00341   
00342 
00343 /* end of not implemented yet :-) *g*  */
00344 
00345 
00346 
00347 #if BirdTrack_impl_test
00348 
00349 #include <bgcorba_impl.h>
00350 #include "IPRFilter.hh"
00351 #include "NotificationManager.hh"
00352 #include "NotificationReceiver.hh"
00353 
00354 int main(int argc, char **argv)
00355 {
00356     bgcorba::init(argc, argv);
00357   
00358     // Create a new instance of the implementation 
00359     BirdTrack_impl *impl = new BirdTrack_impl;
00360 
00361     int retval =  bgcorba::main(impl);
00362 
00363     delete impl;
00364 
00365     return retval;
00366 }
00367 #endif /* BirdTrack_impl_test */
00368 


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 Sat Jun 8 2019 19:36:21