00001
00019
00020
00021 #include <iostream>
00022 #include <stdio.h>
00023 #include <unistd.h>
00024 #include <time.h>
00025 #include <fstream>
00026 #include <math.h>
00027
00028
00029 #include "BirdTrack_impl.h"
00030
00031
00032 #define ANGK (double)(180.0/32758.0)
00033
00034
00035 #define RIGHT_TRACKER_ID 1
00036 #define LEFT_TRACKER_ID 2
00037
00038
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
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
00089 static unsigned char birdchangevaluecmd[] = {'P',50,3};
00090
00091 static unsigned char hemispherecmd[] = {'L',0x00,0x00};
00092 static unsigned char align2cmd[] = {'q',0xFF,0x3F, 0x00,0x00, 0x00,0x80};
00093 static unsigned char reframecmd[] = {'P',0x18, 0x00,0x00, 0x00,0x80, 0x00,0x00};
00094 static unsigned char birdsleep[] = {'G'};
00095
00096 cout << "Autoconfig command wird an den ERC gesendet." << endl;
00097 cout << "Tracker wird (werden) kurz gestartet..." << endl;
00098 serial_object->write_serial(fd,&_erc, 1);
00099 serial_object->write_serial(fd,birdchangevaluecmd,3);
00100 sleep(1);
00101
00102 cout << "Hemisphere command wird an die Snsrs gesendet." << endl;
00103 serial_object->write_serial(fd,&_sensor1, 1);
00104 serial_object->write_serial(fd,hemispherecmd, 3);
00105 serial_object->write_serial(fd,&_sensor2, 1);
00106 serial_object->write_serial(fd,hemispherecmd, 3);
00107 sleep(1);
00108
00109 cout << "ref frame command wird an die Snsrs gesendet." << endl;
00110 serial_object->write_serial(fd,&_sensor1, 1);
00111 serial_object->write_serial(fd,align2cmd, 7);
00112 serial_object->write_serial(fd,&_sensor2, 1);
00113 serial_object->write_serial(fd,align2cmd, 7);
00114 sleep(1);
00115
00116
00117 cout << "ref frame command wird an den ERC gesendet." << endl;
00118 serial_object->write_serial(fd,reframecmd, 8);
00119 sleep(1);
00120
00121
00122 cout << "... und wieder schlafen gelegt, damit wir später nur noch sleep und wakeup machen müssen." << endl;
00123 serial_object->empty_serial(fd);
00124 serial_object->write_serial(fd,birdsleep,1);
00125
00126 refcount = 0;
00127 }
00128
00129 int
00130 BirdTrack_impl::start(void)
00131 {
00132 pthread_mutex_lock(&fobMutex);
00133 if (refcount == 0)
00134 {
00135 static unsigned char birdwakeup[] = {'F'};
00136 serial_object->empty_serial(fd);
00137 usleep(10000);
00138 serial_object->write_serial(fd,birdwakeup,1);
00139 printf("Schicke wakeup.\n");
00140 }
00141 refcount++;
00142
00143 pthread_mutex_unlock(&fobMutex);
00144
00145 return refcount;
00146 }
00147
00148 int
00149 BirdTrack_impl::stop(void)
00150 {
00151 pthread_mutex_lock(&fobMutex);
00152 cout << "Stopping Tracker" << endl;
00153
00154 if (refcount == 0){
00155 pthread_mutex_unlock(&fobMutex);
00156 return 0;
00157 }
00158 if ((refcount-1) == 0){
00159 static unsigned char birdsleep[] = {'G'};
00160 serial_object->empty_serial(fd);
00161 serial_object->write_serial(fd,birdsleep,1);
00162 printf("Schicke sleep.\n");
00163 }
00164 refcount--;
00165
00166 pthread_mutex_unlock(&fobMutex);
00167 return refcount;
00168 }
00169
00170 void
00171 BirdTrack_impl::write_to_file(double *v, char *argv)
00172 {
00173 double x, y, z;
00174 ofstream inf(argv, ios::ate | ios::app);
00175 cout << "rele koordinaten [x y z]: ";
00176 cin >> x;
00177 cin >> y;
00178 cin >> z;
00179 if (inf.is_open())
00180 {
00181
00182 inf << x << '\t'
00183 << y << '\t'
00184 << z << '\t'
00185 << v[0] << "\t"
00186 << v[1] << "\t"
00187 << v[2] << "\t"
00188 << endl;
00189
00190
00191
00192
00193 inf.close();
00194 }
00195 }
00196
00197 int
00198 BirdTrack_impl::get_rawposangles(int whichtracker, int *raw){
00199
00200
00201
00202 pthread_mutex_lock(&fobMutex);
00203
00204 if (refcount == 0){
00205 cout << "get_rawposangles sagt refcount = " << refcount << endl;
00206 pthread_mutex_unlock(&fobMutex);
00207 return -1;
00208 }
00209
00210 short i;
00211 short realdata[6];
00212 short shortdata[12];
00213 float floatdata[6];
00214 unsigned char rs232tofbbcmd, pos_angl_cmd[4];
00215 char birddata[12];
00216
00217 for (i=0; i<12; i++) {
00218 birddata[i] = 0;
00219 shortdata[i] = 0;
00220 }
00221 for (i=0; i<6; i++) {
00222 realdata[i] = 0;
00223 floatdata[i] = 0.0;
00224 }
00225
00226
00227 if (whichtracker == LEFT_TRACKER_ID)
00228 rs232tofbbcmd = (unsigned char)(0xF0 | LEFT_TRACKER_ID + 1);
00229
00230 else if (whichtracker == RIGHT_TRACKER_ID)
00231 rs232tofbbcmd = (unsigned char)(0xF0 | RIGHT_TRACKER_ID + 1);
00232
00233 else{
00234 cout << "Falsche id abgefragt: " << whichtracker << endl;
00235 pthread_mutex_unlock(&fobMutex);
00236 return (-1);
00237 }
00238
00239 pos_angl_cmd[0] = rs232tofbbcmd;
00240 pos_angl_cmd[1] = 'Y';
00241 pos_angl_cmd[2] = rs232tofbbcmd;
00242 pos_angl_cmd[3] = 0x42;
00243
00244
00245
00246
00247 while(1)
00248 {
00249 serial_object->write_serial(fd,pos_angl_cmd,4);
00250 serial_object->empty_serial(fd);
00251 serial_object->read_serial(fd, (unsigned char*)birddata, 12);
00252 if (birddata[0] & 0x80) break;
00253 cout << "waiting..." <<endl;
00254 }
00255 birddata[0] &= 0x7F;
00256 myUnion myPoint;
00257
00258 for (i=0; i<12; i=i+2)
00259 birddata[i] <<= 1;
00260
00261 for (i=0; i<=10; i=i+2) {
00262 myPoint.point.mSigB = birddata[i+1];
00263 myPoint.point.lSigB = birddata[i];
00264 shortdata[i/2] = myPoint.shortPoint;
00265 }
00266
00267 for (i=0; i<6; i++){
00268
00269 shortdata[i] <<= 1;
00270 raw[i] = (int) shortdata[i];
00271 }
00272
00273 pthread_mutex_unlock(&fobMutex);
00274 return 1;
00275
00276 }
00277
00278
00279 int
00280 BirdTrack_impl::get_posangles(int whichtracker, double *v){
00281
00282 short i;
00283 int intdata[6];
00284
00285
00286
00287 get_rawposangles(whichtracker, intdata);
00288
00289
00290 for (i=0; i<3; i++){
00291
00292 v[i] = ( (double)intdata[i]) * 0.11162109375;
00293 }
00294
00295 for (i=3; i<6; i++)
00296 {
00297
00298 v[i] = ( (double)intdata[i]) * 0.0054931640625;
00299 }
00300
00301
00302 pthread_mutex_unlock(&fobMutex);
00303
00304
00305 return 1;
00306 }
00307
00308 int
00309 BirdTrack_impl::polhemus_coord_to_world_coord(double *vec)
00310 {
00311 double x, y, z, rx, ry, rz;
00312
00313 x = (-1.0)*vec[0] * 10;
00314 y = (+1.0)*vec[1] * 10;
00315 z = (-1.0)*vec[2] * 10;
00316 vec[0] = x;
00317 vec[1] = y;
00318 vec[2] = z;
00319
00320 rx = vec[5];
00321 ry = vec[4];
00322 rz = vec[3];
00323
00324
00325
00326
00327
00328
00329
00330
00331 vec[3] = rx * M_PI / 180.0;
00332 vec[4] = ry * M_PI / 180.0;
00333 vec[5] = rz * M_PI / 180.0;
00334
00335
00336
00337
00338
00339
00340
00341 return(1);
00342 }
00343
00344
00345
00346
00347
00348
00349
00350 #if BirdTrack_impl_test
00351
00352 #include <bgcorba_impl.h>
00353 #include "IPRFilter.hh"
00354 #include "NotificationManager.hh"
00355 #include "NotificationReceiver.hh"
00356
00357 int main(int argc, char **argv)
00358 {
00359 bgcorba::init(argc, argv);
00360
00361
00362 BirdTrack_impl *impl = new BirdTrack_impl;
00363
00364 int retval = bgcorba::main(impl);
00365
00366 delete impl;
00367
00368 return retval;
00369 }
00370 #endif
00371