//------------------------------C:\Sources\lgs\detecto_.cpp---------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 // PROJECT: // SUBSYSTEM: // FILE: detecto_.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of detecto_.cpp //---------------------------------------------------------------------------- #include "stdafx.h" #include "detector.h" //static DWORD far* g_Timer = (DWORD far*)0x46CL; //#ifdef INCLUDE_WINDOWS_DEFINITIONS // #define TIMER() *g_Timer //#else // #define TIMER() (GetTickCount() * 10 / 182) //#endif CDetector::CDetector(){ // initiate default settings m_Settings.nOrder = 5; m_Settings.nOffset = 5; m_Settings.nFlip = LGS_MIN_DETECTOR_FLIP; #ifdef LGS_MND m_Settings.nWidth = 250; #else m_Settings.nWidth = 1000; #endif m_Settings.nDelta = 5; m_Settings.nShift = 0; m_Settings.nSun = 32; #ifdef LGS_BOARDSAMPLE_RATIO m_nAngleDivider = LGS_BOARDSAMPLE_RATIO; #endif for( uint8_t i = 0; i < 3; i++ ) m_ppSAF[i] = NULL; m_ppQPD[0] = NULL; m_ppQPD[1] = NULL; m_pShadow = NULL; m_pTracker = NULL; #ifdef INFINITE_SAF_STUDY m_lpMinBorder = NULL; m_lpMaxBorder = NULL; #else m_lpBorder = NULL; #endif m_bBorderEnable = false; #ifdef STORE_FIRST_SAF m_bFreezingSAF = false; m_bSAFSaveStage = 0; #ifdef SKIP_FIRST_N_FLIPS m_bInitialFlipsToSkip = SKIP_FIRST_N_FLIPS; #endif #endif m_bDisabled = false; m_bSSTPassed = false; memset( m_pSettingsFileName, 0, sizeof( m_pSettingsFileName ) ); // make full reset Reset( true ); } CDetector::~CDetector(){ #ifdef STORE_FIRST_SAF //if( m_bSAFSaveStage < 255 && m_bSAFSaveStage > 0 ) //save_current_SAF(); #endif Destroy(); } #ifdef STORE_FIRST_SAF #ifdef WATCHDOG_ENABLED bool CDetector::Create( bool bDisabled, CTracker *pTracker, CShadow *pShadow, int16_t nShift, PCHAR pFileName, CWatchDog *pWD = NULL, uint8_t LRFID=LRF_ID_MASTER ){ #else bool CDetector::Create( bool bDisabled, CTracker *pTracker, CShadow *pShadow, int16_t nShift, PCHAR pFileName, uint8_t LRFID=LRF_ID_MASTER ){ #endif #else bool CDetector::Create( bool bDisabled, CTracker *pTracker, CShadow *pShadow, int16_t nShift, PCHAR pFileName ){ #endif #ifndef COMPILE_AS_SICK_PROXY int32_t nOrder = 10; int32_t nOffset = 20; int32_t nDelta = 60; int32_t nWidth = 4400; int32_t nFlip = 32; int32_t nSun = 32; int32_t nLevelSST = 1; # ifdef LGS_BOARDSAMPLE_RATIO m_nAngleDivider = LGS_BOARDSAMPLE_RATIO; # endif # ifdef DEBUG_PRINTOUT_MSGS CHAR tmp[255]; sprintf( tmp, "Load Detector Settings [%s]", pFileName ); debugProcBegin( tmp ); # endif # ifdef STORE_FIRST_SAF # ifdef WATCHDOG_ENABLED m_pWatchDog = pWD; # endif m_nLRFID = LRFID; # endif if( pFileName ){ // store file name for future use strncpy( m_pSettingsFileName, pFileName, LGS_MAX_SETTINGS_FILENAME ); #ifdef DEBUG_PRINTOUT_MSGS debug("File Name Stored"); #endif // read parameters from file FILE *pFile = fopen( pFileName, "rt" ); #ifdef DEBUG_PRINTOUT_MSGS debugProcBegin("Opening File"); #endif if( pFile ){ fscanf( pFile, "offset=%d delta=%d width=%d order=%d sst=%d flip=%d sun=%d", &nOffset, &nDelta, &nWidth, &nOrder, &nLevelSST, &nFlip, &nSun ); fclose( pFile ); #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd(); debugProcEnd(); #endif } else { #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd( 0 ); debugProcEnd( 0 ); #endif }; } else { #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd( 0 ); #endif }; #else //COMPILE_AS_SICK_PROXY int32_t nOrder = 140; int32_t nOffset = 60; int32_t nDelta = 120; int32_t nWidth = 3500; int32_t nFlip = 255; int32_t nSun = 32; int32_t nLevelSST = 0; m_nAngleDivider = LGS_BOARDSAMPLE_RATIO; #endif // COMPILE_AS_SICK_PROXY return Create( bDisabled, pTracker, pShadow, nShift, nOrder, nOffset, nDelta, nWidth, nFlip, nSun, nLevelSST ); } #ifdef STORE_FIRST_SAF bool CDetector::load_stored_SAF( void ){ // m_bFreezingSAF g_bSAFFreezEnable char PathBuffer[18]; FILE *tFileID; sprintf( PathBuffer, "%c:\\saf%02d.thd", (char)(65+g_bFlashDriveID), m_nLRFID ); tFileID = fopen( PathBuffer, "rb" ); if(tFileID!=NULL){ if( g_bSAFFreezEnable ){ // check g_bSAFFileEnable to Always delete file on disk! for(int index=0; index < LGS_MAX_DETECTOR_ANGLES; index++){ #ifdef WATCHDOG_ENABLED if( g_bWatchDogEnable ){ // update watchdog timer m_pWatchDog->Update( TIMER() ); }; #endif fread( &(m_ppSAF[1][index]), 4, 1, tFileID ); m_ppSAF[2][index].nMax = m_ppSAF[0][index].nMax = m_ppSAF[1][index].nMax = m_ppSAF[1][index].nMax; m_ppSAF[2][index].nMin = m_ppSAF[0][index].nMin = m_ppSAF[1][index].nMin = m_ppSAF[1][index].nMin; }; fclose(tFileID); m_bFreezingSAF=true; m_bSAFSaveStage = 255; #ifdef SKIP_FIRST_N_FLIPS m_bInitialFlipsToSkip = 0; #endif } else { fclose(tFileID); unlink(PathBuffer); m_bFreezingSAF = false; m_bSAFSaveStage = 0; #ifdef SKIP_FIRST_N_FLIPS m_bInitialFlipsToSkip = SKIP_FIRST_N_FLIPS; #endif }; } else { m_bFreezingSAF = false; m_bSAFSaveStage = 0; return false; }; return true; } bool CDetector::save_current_SAF( void ){ char PathBuffer[18]; FILE *tFileID; struct ffblk bb; if( m_bSAFSaveStage < 255 && m_bSAFSaveStage > 0 ){ unifyDataSAF(); sprintf( PathBuffer, "%c:\\saf%02d.thd", (char)(65+g_bFlashDriveID), m_nLRFID ); if( findfirst( PathBuffer, &bb , 0 ) != -1 ) return false; tFileID = fopen( PathBuffer, "wb" ); if(tFileID!=NULL){ for( int index=0; index < LGS_MAX_DETECTOR_ANGLES; index++ ){ // The ready SAF is the second one!! #ifdef WATCHDOG_ENABLED if( g_bWatchDogEnable ){ // update watchdog timer m_pWatchDog->Update( TIMER() ); }; #endif fwrite( &(m_ppSAF[1][index]), 4, 1, tFileID ); }; fclose(tFileID); } else return false; } else return false; return true; } void CDetector::unifyDataSAF( void ){ uint16_t Max; uint16_t Min; for( int index=0; index < LGS_MAX_DETECTOR_ANGLES; index++ ){ // The ready SAF is the second one!! #ifdef WATCHDOG_ENABLED if( g_bWatchDogEnable ){ // update watchdog timer m_pWatchDog->Update( TIMER() ); }; #endif Max = 0; Min = LGS_MAX_DETECTOR_RANGE; /* Minimums */ if( m_ppSAF[0][index].nMin ) if( Min > m_ppSAF[0][index].nMin ) Min = m_ppSAF[0][index].nMin; if( m_ppSAF[1][index].nMin ) if( Min > m_ppSAF[1][index].nMin ) Min = m_ppSAF[1][index].nMin; //if( m_ppSAF[2][index].nMin ) if( Min > m_ppSAF[2][index].nMin ) Min = m_ppSAF[2][index].nMin; /* Maximums */ if( m_ppSAF[0][index].nMax > Max ) Max = m_ppSAF[0][index].nMax; if( m_ppSAF[1][index].nMax > Max ) Max = m_ppSAF[1][index].nMax; //if( m_ppSAF[2][index].nMax > Max ) Max = m_ppSAF[2][index].nMax; /* Populate Data */ m_ppSAF[2][index].nMax = m_ppSAF[0][index].nMax = m_ppSAF[1][index].nMax = Max; m_ppSAF[2][index].nMin = m_ppSAF[0][index].nMin = m_ppSAF[1][index].nMin = Min; }; } #endif bool CDetector::Create( bool bDisabled, CTracker *pTracker, CShadow *pShadow, int32_t nShift, int32_t nOrder, int32_t nOffset, int32_t nDelta, int32_t nWidth, int32_t nFlip, int32_t nSun, int32_t nLevelSST ) { #ifdef DEBUG_PRINTOUT_MSGS debugProcBegin( "Clear" ); #endif Destroy(); #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd(); #endif m_pTracker = pTracker; m_pShadow = pShadow; m_Settings.nShift = nShift; m_Settings.nOrder = nOrder; #ifdef LGS_REDUCED_PRF m_Settings.nOffset = nOffset * LGS_PRF_REDUCE_RATIO; #else m_Settings.nOffset = nOffset; #endif m_Settings.nDelta = nDelta; #ifdef LGS_MND m_Settings.nWidth = nWidth >> 2; #else m_Settings.nWidth = nWidth; #endif m_Settings.nFlip = nFlip; m_Settings.nSun = nSun; pTracker->SetLevel( nLevelSST ); // if detector disabled no creation m_bDisabled = bDisabled; if( m_bDisabled ) return true; #ifdef DEBUG_PRINTOUT_MSGS debugProcBegin( "SAF Memory"); #endif int16_t i; for( i = 0; i < 3; i++ ){ // alloc memory for SAF m_ppSAF[i] = (LPSAF) malloc( sizeof( SAF ) * LGS_MAX_DETECTOR_ANGLES ); if( !m_ppSAF[i] ){ #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd( 0 ); #endif return false; }; } #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd(); debugProcBegin("Quality Points"); #endif for( i = 0; i < 2; i++ ){ // 2?? // alloc memory for quality point descriptors m_ppQPD[i] = (LPQPD) malloc( sizeof( QPD ) * LGS_MAX_DETECTOR_ANGLES ); if( !m_ppQPD[i] ){ #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd( 0 ); #endif return false; }; // reset QPD data for( int16_t j = 0; j < LGS_MAX_DETECTOR_ANGLES; j++ ){ m_ppQPD[i][j].nOffset = (uint8_t)m_Settings.nOffset; m_ppQPD[i][j].nOrder = (uint8_t)m_Settings.nOrder; } } #ifndef NO_DETECTOR_FILTERS #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd(); debugProcBegin( "Filters" ); debugProcBegin( "Filter PD"); #endif // create filer PD if( !m_FilterPD.Create( m_pTracker ) ){ #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd( 0 ); debugProcEnd( 0 ); #endif return false; }; #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd(); debugProcBegin("Filter Fog"); #endif // create filer Fog if( !m_FilterFog.Create( m_pTracker ) ){ #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd( 0 ); debugProcEnd( 0 ); #endif return false; }; //Enable Fog Filter m_FilterFog.Enable(); m_bSSTPassed = false; #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd(); debugProcBegin( "Filter Sun"); #endif // allocate data for sun bit checking if( !m_FilterSun.Create( m_pTracker ) ){ #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd( 0 ); debugProcEnd( 0 ); #endif return false; }; #endif //NO_DETECTOR_FILTERS #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd(); debugProcBegin( "Alloc Data for Border"); #endif // allocate data for border #ifdef INFINITE_SAF_STUDY m_lpMinBorder = (LPWORD) malloc( sizeof( uint16_t ) * LGS_MAX_BORDER_ANGLES ); if( !m_lpMinBorder ){ #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd( 0 ); debugProcEnd( 0 ); #endif return false; }; memset( m_lpMinBorder, 0, sizeof( uint16_t ) * LGS_MAX_BORDER_ANGLES ); m_lpMaxBorder = (LPWORD) malloc( sizeof( uint16_t ) * LGS_MAX_BORDER_ANGLES ); if( !m_lpMaxBorder ){ #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd( 0 ); debugProcEnd( 0 ); #endif return false; }; memset( m_lpMaxBorder, 0 , sizeof( uint16_t ) * LGS_MAX_BORDER_ANGLES ); #else m_lpBorder = (LPWORD) malloc( sizeof( uint16_t ) * LGS_MAX_BORDER_ANGLES ); if( !m_lpBorder ){ #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd( 0 ); debugProcEnd( 0 ); #endif return false; }; memset( m_lpBorder, 0 , sizeof( uint16_t ) * LGS_MAX_BORDER_ANGLES ); #endif #ifndef NO_DETECTOR_FILTERS #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd(); debugProcBegin( "Filter Medium"); #endif if( !m_FilterMediumRange.Create( m_pTracker ) ){ #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd( 0 ); debugProcEnd( 0 ); #endif return false; }; #endif // NO_DETECTOR_FILTERS #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd(); debugProcBegin( "Filter Width" ); #endif if( !m_FilterWidth.Create( m_pTracker, "width.ini" ) ){ #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd( 0 ); debugProcEnd( 0 ); #endif return false; }; #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd(); debugProcEnd(); #endif Reset( true ); return true; } void CDetector::Destroy() { int16_t i; for( i = 0; i < 3; i++ ) { if( m_ppSAF[i] ) { free( m_ppSAF[i] ); m_ppSAF[i] = NULL; } } for( i = 0; i < 2; i++ ) { if( m_ppQPD[i] ) { free( m_ppQPD[i] ); m_ppQPD[i] = NULL; } } #ifndef NO_DETECTOR_FILTERS // destroy filter PD m_FilterPD.Destroy(); // destroy sun filter m_FilterSun.Destroy(); #endif // destroy filter width m_FilterWidth.Destroy(); m_pShadow = NULL; #ifdef INFINITE_SAF_STUDY if( m_lpMinBorder ) { free( m_lpMinBorder ); m_lpMinBorder = NULL; } if( m_lpMaxBorder ) { free( m_lpMaxBorder ); m_lpMaxBorder = NULL; } #else if( m_lpBorder ) { free( m_lpBorder ); m_lpBorder = NULL; } #endif #ifndef NO_DETECTOR_FILTERS m_FilterMediumRange.Destroy(); #endif Reset( true ); }