1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
|
#include "Cins.h"
#include <sys/time.h>
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef __cplusplus
#define EXT_C_DECL extern "C"
#else
#define EXT_C_DEC
#endif
# include <iostream>
using namespace std;
// Debug
static int trace = 0;
static const char serial_name[10] = "dummy";
static int PthreadValue;
static Cins *p;
static int cond = 1;
static int insmutex;
static void *readmutex;
static char answer[ANSWER_LENGTH]; // global byte array for the INS messages
static int expected_answer_len;
static int answer_len;
// Make sure the complete message is received in Callback function
static char curr_answer[ANSWER_LENGTH]; // global byte array for current message(s) received
static int tot_pos = 0;
static bool complete = false;
static int pos = 0;
EXT_C_DECL void *insWatchDog(void* );
#define BITDATAANSWERSIZE 5
#define POINTINGDEVICEANSWERSIZE 9
#define POSITIONDATAANSWERSIZE 13
// Statusbit definition
#define DRU_NEEDS_ZERO_VELOCITY_UPDATE 10
#define DRU_NEEDS_POSITION_UPDATE 9
#define BIT_MALFUNCTION_DETECTED 5
#define DRU_ALERT 1
// Errorbit definition
#define VMS_DRIVE_FAILURE 0
#define VMS_FAILURE 1
#define DRU_FAILURE 15
static void Print_HexMsg(char * msg, int len)
{
for (int i = 0; i < len; i++)
{
printf("0x%X ", msg[i]);
}
printf("\n");
}
static void Get_Message(char * tmp_answer, int tmp_size)
{
for ( int i=0;i<tmp_size;i++ )
{
answer[pos] = tmp_answer[i];
pos++;
if ( pos == ANSWER_LENGTH )
{
break;
}
} // for
if ( pos == expected_answer_len )
{
if (trace) printf("Cins::Get_Message: Message complete.\n");
complete = true;
answer_len = pos;
pos = 0;
tot_pos = 0;
}
else if ( pos > expected_answer_len )
{
Print_HexMsg(answer, pos);
complete = true;
answer_len = -1;
pos = 0;
tot_pos = 0;
}
};
// CLASS
void Cins::Serial_Callback(const char* signal,const char* bytes)
{
char tmp_answer[ANSWER_LENGTH];
int tmp_size = 0;
if (complete == false)
{
memcpy(tmp_answer,curr_answer,tot_pos);
tmp_size = strlen(bytes);
memcpy(tmp_answer + tot_pos, bytes, tmp_size);
tmp_size = tmp_size + tot_pos;
tmp_answer[tmp_size] = '\0';
tot_pos = tmp_size;
if (trace) {
printf("Callback_ins::Serial_Callback: We got a callback from ( %s ), size = %d, message = ",signal,tmp_size);
Print_HexMsg(tmp_answer, tmp_size);
}
Get_Message(tmp_answer, tmp_size);
// Signal
if (complete == true)
{
if (trace) printf("Callback_ins::Serial_Callback: Just sent signal.\n");
}
}
else
{
tmp_size = strlen(bytes);
memcpy(curr_answer + tot_pos, bytes, tmp_size);
tot_pos = tmp_size + tot_pos;
printf("Callback_ins::Serial_Callback: WARNING - message overflow! current message queue size = %d\n", tot_pos);
Print_HexMsg(curr_answer, tot_pos);
}
};
Cins::Cins(char *Device,bool ErrorReported, void *rmutex)
{
int retval;
bool bNRZI=false;
m_ant_ttu_inactive tmp1;
M_ANTENNA_IDENT tmp2;
m_req_ant_position_status tmp3;
m_ins_attitude_data tmp4;
int rt, rp, ra;
nummeasured=0;
filterB=(1.0-FILTERA)/(1/20.0); //precalculated value for velocity calculation
readmutex = rmutex;
//DeviceName=Device;
InsUsed=false;
bTimedOut=false;
StopWatchDog=false;
WatchDogSleepTime = 10;
debug=0;
debughangup=0;
OpenErrorReported = ErrorReported;
p=this;
char buffer[20];
myParam = new CGetParam("file.dat");
// No problems with overloaded functions below
myParam->GetString("GetString");
myParam->GetString("GetString", false);
retval = myParam->do_antenna(tmp1);
retval = myParam->do_antenna(tmp2);
retval = myParam->do_antenna(tmp3, rt, rp, ra);
retval = myParam->do_antenna(tmp4);
delete myParam;
// retval = pthread_mutex_init(&insmutex,NULL);
//PthreadValue=pthread_create(&insWatchDogId,NULL,insWatchDog,p);
OpenErrorReported=false;
/* if(pthread_mutex_unlock(&insmutex) != 0)
{
printf("************************************************\n");
printf("************************************************\n");
printf("***** Constructor unlock ********\n");
perror("*****:");
printf("************************************************\n");
printf("************************************************\n");
}
if (OpenErrorReported == false)
{
printf("INS Constructor completed OK\n");
} */
}
Cins::~Cins()
{
StopWatchDog=true;
//pthread_mutex_lock(&insmutex);
//pthread_mutex_destroy(&insmutex);
//pthread_cond_destroy(&cond);
//pthread_exit (NULL);
debug = 0;
}
void Cins::INS_SetDebug(bool newdebug)
{
debug=newdebug;
}
void Cins::INS_SetDebugHangup(bool newdebug)
{
debughangup=newdebug;
}
bool Cins::INS_ERROR(void)
{
return bTimedOut;
}
bool Cins::INS_GetBitData(void)
{
int *phelp;
int help;
int nwrote,ndata;;
char chelp[4];
bool success = false;
char command[1];
struct timespec ts1, ts2;
ts1.tv_sec = 0;
ts1.tv_nsec = INSBitLatency * 1000000;
ts2.tv_sec = 0;
ts2.tv_nsec = INSContextSwitchLatency * 1000;//Microseconds
// printf("------ INS BIT: starting\n");
/* if(pthread_mutex_lock(&insmutex) != 0)
{
printf("************************************************\n");
printf("************************************************\n");
printf("***** INS_GetBitData lock error\n");
perror("*****:");
printf("************************************************\n");
printf("************************************************\n");
}*/
if (trace) printf("\nCins::INS_GetBitData ...\n");
command[0]=0x1f;
nwrote=0;
//nanosleep(&ts1,NULL);
// Request new message
complete = false;
nwrote = Write_Serial(command, 1);
if ((nwrote == -1) || (nwrote < 1))
{
printf("Cins::INS_GetBitData: Write_Serial failed!\n");
//pthread_mutex_unlock(&insmutex);
return success;
}
if (debug==1)
printf("\nCins::INS_GetBitData - wrote %d bytes - waiting for answer ...\n", nwrote);
//nanosleep(&ts2,NULL);
if (debug==1)
{
ndata=INS_GetAnswer(BITDATAANSWERSIZE);
}
if(ndata > 0)
{
InsUsed=true;
}
else
{
InsUsed=false;
}
/*if(pthread_mutex_unlock(&insmutex) != 0)
{
printf("************************************************\n");
printf("************************************************\n");
printf("***** INS_GetBitData unlock error\n");
perror("*****:");
printf("************************************************\n");
printf("************************************************\n");
}*/
return success;
}
bool Cins::INS_GetPositionData(unsigned short int *Spheroid,
short int *Hemisphere,
unsigned int *Easting,
unsigned int *Northing,
short int *Altitude)
{
int nwrote,ndata;
int help;
int *phelp;
unsigned char chelp[4];
short int *pshelp;
short int shelp;
bool success=false;
char command[1];
struct timespec ts1, ts2;
ts1.tv_sec = 0;
ts1.tv_nsec = INSPosLatency * 1000000;
ts2.tv_sec = 0;
ts2.tv_nsec = INSContextSwitchLatency * 1000;//Microseconds
/*if(pthread_mutex_lock(&insmutex) != 0)
{
printf("************************************************\n");
printf("************************************************\n");
printf("***** INS_GetPositionData lock error **********\n");
perror("*****:");
printf("************************************************\n");
printf("************************************************\n");
}*/
if (trace) printf("\nCins::INS_GetPositionData ...\n");
command[0]=0x09;
nwrote=0;
//nanosleep(&ts1,NULL);
// Request new message
complete = false;
nwrote = Write_Serial(command, 1);
if ((nwrote == -1) || (nwrote < 1))
{
printf("Cins::INS_GetPositionData: Write_Serial failed!\n");
//pthread_mutex_unlock(&insmutex);
return success;
}
if (debug==1)
printf("\nCins::INS_GetPositionData - wrote %d bytes - waiting for answer ...\n", nwrote);
//nanosleep(&ts2,NULL);
ndata=INS_GetAnswer(POSITIONDATAANSWERSIZE);
if (debug==1)
{
printf("INS: We got:");
for (int i=0;i<ndata;i++)
{
printf(" 0x%X",(unsigned char)answer[i]);
}
printf("\n");
}
if(ndata > 0)
{
InsUsed=true;
}
else
{
InsUsed=false;
}
/*if(pthread_mutex_unlock(&insmutex) != 0)
{
printf("************************************************\n");
printf("************************************************\n");
printf("***** INS_GetPositionData unlock error ********\n");
perror("*****:");
printf("************************************************\n");
printf("************************************************\n");
}*/
return success;
}
bool Cins::INS_GetPointingDeviceAttitudeData(float *azim,float *tilt,float *roll)
{
unsigned short int Azimuth;
short int Tilt;
short int Roll;
int nwrote,ndata;
int help;
int *phelp;
unsigned char chelp[4];
short int *pshelp;
short unsigned int *psuhelp;
bool success = false;
char command[1];
struct timespec ts1, ts2;
ts1.tv_sec = 0;
ts1.tv_nsec = INSAttLatency * 1000000;
ts2.tv_sec = 0;
ts2.tv_nsec = INSContextSwitchLatency * 1000;//Microseconds
/*if(pthread_mutex_lock(&insmutex) != 0)
{
printf("************************************************\n");
printf("************************************************\n");
printf("***** INS_GetPointingDeviceAttitudeData lock error\n");
perror("*****:");
printf("************************************************\n");
printf("************************************************\n");
}*/
if (trace) printf("\nCins::INS_GetPointingDeviceAttitudeData ...\n");
command[0]=0x0c;
nwrote=0;
//nanosleep(&ts1,NULL);
// Request new message
complete = false;
nwrote = Write_Serial(command, 1);
if ((nwrote == -1) || (nwrote < 1))
{
printf("Cins::INS_GetPointingDeviceAttitudeData: Write_Serial failed!\n");
//pthread_mutex_unlock(&insmutex);
return success;
}
if (debug==1)
printf("\nCins::INS_GetPointingDeviceAttitudeData - wrote %d bytes - waiting for answer ...\n", nwrote);
//nanosleep(&ts2,NULL);
ndata=INS_GetAnswer(POINTINGDEVICEANSWERSIZE);
if (debug==1)
{
printf("INS: We got:");
for (int i=0;i<ndata;i++)
{
printf(" 0x%X",(unsigned char)answer[i]);
}
printf("\n");
}
if(ndata > 0)
{
InsUsed=true;
}
else
{
InsUsed=false;
}
/*if(pthread_mutex_unlock(&insmutex) != 0)
{
printf("************************************************\n");
printf("************************************************\n");
printf("***** INS_GetPointingDeviceAttitudeData unlock error\n");
perror("*****:");
printf("************************************************\n");
printf("************************************************\n");
}*/
//pthread_mutex_unlock(&insmutex);
return success;
}
void Cins::INS_GetVelocityData(float *velazim,float *veltilt,float *velroll)
{
*velazim=vazim;
*veltilt=vtilt;
*velroll=vroll;
}
bool Cins::INS_GetDruNeedsZeroVelocityUpdate(void)
{
return StatusBit[DRU_NEEDS_ZERO_VELOCITY_UPDATE];
}
bool Cins::INS_GetDruNeedsPositionUpdate(void)
{
return StatusBit[DRU_NEEDS_POSITION_UPDATE];
}
bool Cins::INS_GetBitMalFunction(void)
{
return StatusBit[BIT_MALFUNCTION_DETECTED];
}
bool Cins::INS_GetDruAlert(void)
{
return StatusBit[DRU_ALERT];
}
bool Cins::INS_GetDruFailure()
{
return BitData[DRU_FAILURE];
}
bool Cins::INS_GetVmsFailure()
{
return BitData[VMS_FAILURE];
}
bool Cins::INS_GetVmsDriveFailure()
{
return BitData[VMS_DRIVE_FAILURE];
}
int Cins::INS_GetAnswer(int num)
{
struct timeval now;
struct timespec timeout;
int retcode = 0;
char tmp_answer[ANSWER_LENGTH];
//pthread_mutex_lock(readmutex);
gettimeofday(&now, NULL);
timeout.tv_sec = now.tv_sec + 5;
timeout.tv_nsec = now.tv_usec * 1000;
expected_answer_len = num;
if (complete == false)
{
if (trace) printf("Cins::INS_GetAnswer: Waiting ...\n");
//retcode = pthread_cond_timedwait(&cond, readmutex, &timeout);
if (trace) printf("Cins::INS_GetAnswer: Condition signal received.\n");
}
//if (retcode == ETIMEDOUT)
// {
// /* Timeout occurred */
// printf("Cins::INS_GetAnswer: TIMEOUT!\n");
// }
if (complete == false)
{
answer_len = -1;
// Reset receive buffer pointers
pos = 0;
tot_pos = 0;
}
if (trace) printf("Cins::INS_GetAnswer: Message received, size = %d\n", answer_len);
//pthread_mutex_unlock(readmutex);
return answer_len;
};
bool Cins::INS_GetOpenErrorReported(void)
{
return OpenErrorReported;
}
void Cins::INS_GetStatusBit(unsigned short int Status)
{
StatusBit[0]= ((Status & 1) == 1);
StatusBit[1]= (((Status>>1) & 1) == 1);
StatusBit[2]= (((Status>>2) & 1) == 1);
StatusBit[3]= (((Status>>3) & 1) == 1);
StatusBit[4]= (((Status>>4) & 1) == 1);
StatusBit[5]= (((Status>>5) & 1) == 1);
StatusBit[6]= (((Status>>6) & 1) == 1);
StatusBit[7]= (((Status>>7) & 1) == 1);
StatusBit[8]= (((Status>>8) & 1) == 1);
StatusBit[9]= (((Status>>9) & 1) == 1);
StatusBit[10]= (((Status>>10) & 1) == 1);
StatusBit[11]= (((Status>>11) & 1) == 1);
StatusBit[12]= (((Status>>12) & 1) == 1);
StatusBit[13]= (((Status>>13) & 1) == 1);
StatusBit[14]= (((Status>>14) & 1) == 1);
StatusBit[15]= (((Status>>15) & 1) == 1);
}
void Cins::INS_GetBitData(unsigned short int Status)
{
BitData[0]= ((Status & 1) == 1);
BitData[1]= (((Status>>1) & 1) == 1);
BitData[2]= (((Status>>2) & 1) == 1);
BitData[3]= (((Status>>3) & 1) == 1);
BitData[4]= (((Status>>4) & 1) == 1);
BitData[5]= (((Status>>5) & 1) == 1);
BitData[6]= (((Status>>6) & 1) == 1);
BitData[7]= (((Status>>7) & 1) == 1);
BitData[8]= (((Status>>8) & 1) == 1);
BitData[9]= (((Status>>9) & 1) == 1);
BitData[10]= (((Status>>10) & 1) == 1);
BitData[11]= (((Status>>11) & 1) == 1);
BitData[12]= (((Status>>12) & 1) == 1);
BitData[13]= (((Status>>13) & 1) == 1);
BitData[14]= (((Status>>14) & 1) == 1);
BitData[15]= (((Status>>15) & 1) == 1);
}
EXT_C_DECL void *insWatchDog(void *value)
{
Cins *p;
p=(Cins *)value;
// Wait until the system at least has tried to read position data once .....
//sleep(60);
while(p->StopWatchDog==false)
{
if (p->debug)
{
if(p->InsUsed)
{
printf("ECOM_INS::WatchDog for INS - Status OK\n");
}
else
{
printf("ECOM_INS::WatchDog for INS - Status NOT OK\n");
}
}
// check for open link error
//sleep(p->WatchDogSleepTime);
if (p->InsUsed == false)
{
p->bTimedOut=true;
}
else
{
p->bTimedOut=false;
}
}
};
int Cins::Write_Serial(char * msg, int datalen)
{
char value_buf[20];
if (trace)
{
printf("Writing to serial channel ( %s )\n", serial_name);
Print_HexMsg(msg, datalen);
}
memcpy(value_buf, msg, datalen);
return datalen;
};
|