//---------------------------------------------------------------------------- // 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 = 8; m_Settings.nWidth = 1000; m_Settings.nDelta = 5; m_Settings.nShift = 0; m_Settings.nSun = 32; m_nAngleDivider = LGS_BOARDSAMPLE_RATIO; for( INT 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, INT nShift, PCHAR pFileName, CWatchDog *pWD = NULL, BYTE LRFID=LRF_ID_MASTER ){ #else BOOL CDetector::Create( BOOL bDisabled, CTracker *pTracker, CShadow *pShadow, INT nShift, PCHAR pFileName, BYTE LRFID=LRF_ID_MASTER ){ #endif #else BOOL CDetector::Create( BOOL bDisabled, CTracker *pTracker, CShadow *pShadow, INT nShift, PCHAR pFileName ){ #endif #ifndef COMPILE_AS_SICK_PROXY INT nOrder = 10; INT nOffset = 20; INT nDelta = 60; INT nWidth = 4400; INT nFlip = 32; INT nSun = 32; INT nLevelSST = 1; m_nAngleDivider = LGS_BOARDSAMPLE_RATIO; #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 INT nOrder = 140; INT nOffset = 60; INT nDelta = 120; INT nWidth = 3500; INT nFlip = 255; INT nSun = 32; INT nLevelSST = 0; m_nAngleDivider = LGS_BOARDSAMPLE_RATIO; #endif 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 ){ WORD Max; WORD 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, INT nShift, INT nOrder, INT nOffset, INT nDelta, INT nWidth, INT nFlip, INT nSun, INT 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; m_Settings.nWidth = nWidth; 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 for( INT i = 0; i < 3; i++ ){ // alloc memory for SAF m_ppSAF[i] = (LPSAF) farmalloc( 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)farmalloc( sizeof( QPD ) * LGS_MAX_DETECTOR_ANGLES ); if( !m_ppQPD[i] ){ #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd( 0 ); #endif return FALSE; }; // reset QPD data for( INT j = 0; j < LGS_MAX_DETECTOR_ANGLES; j++ ){ m_ppQPD[i][j].nOffset = (BYTE)m_Settings.nOffset; m_ppQPD[i][j].nOrder = (BYTE)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)farmalloc( sizeof( WORD ) * LGS_MAX_BORDER_ANGLES ); if( !m_lpMinBorder ){ #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd( 0 ); debugProcEnd( 0 ); #endif return FALSE; }; m_lpMaxBorder = (LPWORD)farmalloc( sizeof( WORD ) * LGS_MAX_BORDER_ANGLES ); if( !m_lpMaxBorder ){ #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd( 0 ); debugProcEnd( 0 ); #endif return FALSE; }; #else m_lpBorder = (LPWORD)farmalloc( sizeof( WORD ) * LGS_MAX_BORDER_ANGLES ); if( !m_lpBorder ){ #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd( 0 ); debugProcEnd( 0 ); #endif return FALSE; }; #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() { for( INT i = 0; i < 3; i++ ) { if( m_ppSAF[i] ) { farfree( m_ppSAF[i] ); m_ppSAF[i] = NULL; } } for( i = 0; i < 2; i++ ) { if( m_ppQPD[i] ) { farfree( 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 ) { farfree( m_lpMinBorder ); m_lpMinBorder = NULL; } if( m_lpMaxBorder ) { farfree( m_lpMaxBorder ); m_lpMaxBorder = NULL; } #else if( m_lpBorder ) { farfree( m_lpBorder ); m_lpBorder = NULL; } #endif #ifndef NO_DETECTOR_FILTERS m_FilterMediumRange.Destroy(); #endif Reset( TRUE ); }