For DOS Protected Mode version of sensor core software
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

497 lines
12 KiB

3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
3 years ago
3 years ago
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //------------------------------C:\Sources\lgs\detecto_.cpp----------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: detecto_.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of detecto_.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "detector.h"
  16. //static DWORD far* g_Timer = (DWORD far*)0x46CL;
  17. //#ifdef INCLUDE_WINDOWS_DEFINITIONS
  18. // #define TIMER() *g_Timer
  19. //#else
  20. // #define TIMER() (GetTickCount() * 10 / 182)
  21. //#endif
  22. CDetector::CDetector(){
  23. // initiate default settings
  24. m_Settings.nOrder = 5;
  25. m_Settings.nOffset = 5;
  26. m_Settings.nFlip = LGS_MIN_DETECTOR_FLIP;
  27. #ifdef LGS_MND
  28. m_Settings.nWidth = 250;
  29. #else
  30. m_Settings.nWidth = 1000;
  31. #endif
  32. m_Settings.nDelta = 5;
  33. m_Settings.nShift = 0;
  34. m_Settings.nSun = 32;
  35. #ifdef LGS_BOARDSAMPLE_RATIO
  36. m_nAngleDivider = LGS_BOARDSAMPLE_RATIO;
  37. #endif
  38. for( uint8_t i = 0; i < 3; i++ ) m_ppSAF[i] = NULL;
  39. m_ppQPD[0] = NULL;
  40. m_ppQPD[1] = NULL;
  41. m_pShadow = NULL;
  42. m_pTracker = NULL;
  43. #ifdef INFINITE_SAF_STUDY
  44. m_lpMinBorder = NULL;
  45. m_lpMaxBorder = NULL;
  46. #else
  47. m_lpBorder = NULL;
  48. #endif
  49. m_bBorderEnable = false;
  50. #ifdef STORE_FIRST_SAF
  51. m_bFreezingSAF = false;
  52. m_bSAFSaveStage = 0;
  53. #ifdef SKIP_FIRST_N_FLIPS
  54. m_bInitialFlipsToSkip = SKIP_FIRST_N_FLIPS;
  55. #endif
  56. #endif
  57. m_bDisabled = false;
  58. m_bSSTPassed = false;
  59. memset( m_pSettingsFileName, 0, sizeof( m_pSettingsFileName ) );
  60. // make full reset
  61. Reset( true );
  62. }
  63. CDetector::~CDetector(){
  64. #ifdef STORE_FIRST_SAF
  65. //if( m_bSAFSaveStage < 255 && m_bSAFSaveStage > 0 )
  66. //save_current_SAF();
  67. #endif
  68. Destroy();
  69. }
  70. #ifdef STORE_FIRST_SAF
  71. #ifdef WATCHDOG_ENABLED
  72. bool CDetector::Create( bool bDisabled, CTracker *pTracker, CShadow *pShadow, int16_t nShift, PCHAR pFileName, CWatchDog *pWD = NULL, uint8_t LRFID=LRF_ID_MASTER ){
  73. #else
  74. bool CDetector::Create( bool bDisabled, CTracker *pTracker, CShadow *pShadow, int16_t nShift, PCHAR pFileName, uint8_t LRFID=LRF_ID_MASTER ){
  75. #endif
  76. #else
  77. bool CDetector::Create( bool bDisabled, CTracker *pTracker, CShadow *pShadow, int16_t nShift, PCHAR pFileName ){
  78. #endif
  79. #ifndef COMPILE_AS_SICK_PROXY
  80. int32_t nOrder = 10;
  81. int32_t nOffset = 20;
  82. int32_t nDelta = 60;
  83. int32_t nWidth = 4400;
  84. int32_t nFlip = 32;
  85. int32_t nSun = 32;
  86. int32_t nLevelSST = 1;
  87. # ifdef LGS_BOARDSAMPLE_RATIO
  88. m_nAngleDivider = LGS_BOARDSAMPLE_RATIO;
  89. # endif
  90. # ifdef DEBUG_PRINTOUT_MSGS
  91. CHAR tmp[255];
  92. sprintf( tmp, "Load Detector Settings [%s]", pFileName );
  93. debugProcBegin( tmp );
  94. # endif
  95. # ifdef STORE_FIRST_SAF
  96. # ifdef WATCHDOG_ENABLED
  97. m_pWatchDog = pWD;
  98. # endif
  99. m_nLRFID = LRFID;
  100. # endif
  101. if( pFileName ){
  102. // store file name for future use
  103. strncpy( m_pSettingsFileName, pFileName, LGS_MAX_SETTINGS_FILENAME );
  104. #ifdef DEBUG_PRINTOUT_MSGS
  105. debug("File Name Stored");
  106. #endif
  107. // read parameters from file
  108. FILE *pFile = fopen( pFileName, "rt" );
  109. #ifdef DEBUG_PRINTOUT_MSGS
  110. debugProcBegin("Opening File");
  111. #endif
  112. if( pFile ){
  113. fscanf( pFile, "offset=%d delta=%d width=%d order=%d sst=%d flip=%d sun=%d",
  114. &nOffset, &nDelta, &nWidth, &nOrder, &nLevelSST, &nFlip, &nSun );
  115. fclose( pFile );
  116. #ifdef DEBUG_PRINTOUT_MSGS
  117. debugProcEnd();
  118. debugProcEnd();
  119. #endif
  120. } else {
  121. #ifdef DEBUG_PRINTOUT_MSGS
  122. debugProcEnd( 0 );
  123. debugProcEnd( 0 );
  124. #endif
  125. };
  126. } else {
  127. #ifdef DEBUG_PRINTOUT_MSGS
  128. debugProcEnd( 0 );
  129. #endif
  130. };
  131. #else //COMPILE_AS_SICK_PROXY
  132. int32_t nOrder = 140;
  133. int32_t nOffset = 60;
  134. int32_t nDelta = 120;
  135. int32_t nWidth = 3500;
  136. int32_t nFlip = 255;
  137. int32_t nSun = 32;
  138. int32_t nLevelSST = 0;
  139. m_nAngleDivider = LGS_BOARDSAMPLE_RATIO;
  140. #endif // COMPILE_AS_SICK_PROXY
  141. return Create( bDisabled, pTracker, pShadow, nShift, nOrder, nOffset, nDelta, nWidth, nFlip, nSun, nLevelSST );
  142. }
  143. #ifdef STORE_FIRST_SAF
  144. bool CDetector::load_stored_SAF( void ){
  145. // m_bFreezingSAF g_bSAFFreezEnable
  146. char PathBuffer[18];
  147. FILE *tFileID;
  148. sprintf( PathBuffer, "%c:\\saf%02d.thd", (char)(65+g_bFlashDriveID), m_nLRFID );
  149. tFileID = fopen( PathBuffer, "rb" );
  150. if(tFileID!=NULL){
  151. if( g_bSAFFreezEnable ){ // check g_bSAFFileEnable to Always delete file on disk!
  152. for(int index=0; index < LGS_MAX_DETECTOR_ANGLES; index++){
  153. #ifdef WATCHDOG_ENABLED
  154. if( g_bWatchDogEnable ){ // update watchdog timer
  155. m_pWatchDog->Update( TIMER() );
  156. };
  157. #endif
  158. fread( &(m_ppSAF[1][index]), 4, 1, tFileID );
  159. m_ppSAF[2][index].nMax = m_ppSAF[0][index].nMax = m_ppSAF[1][index].nMax = m_ppSAF[1][index].nMax;
  160. m_ppSAF[2][index].nMin = m_ppSAF[0][index].nMin = m_ppSAF[1][index].nMin = m_ppSAF[1][index].nMin;
  161. };
  162. fclose(tFileID);
  163. m_bFreezingSAF=true;
  164. m_bSAFSaveStage = 255;
  165. #ifdef SKIP_FIRST_N_FLIPS
  166. m_bInitialFlipsToSkip = 0;
  167. #endif
  168. } else {
  169. fclose(tFileID);
  170. unlink(PathBuffer);
  171. m_bFreezingSAF = false;
  172. m_bSAFSaveStage = 0;
  173. #ifdef SKIP_FIRST_N_FLIPS
  174. m_bInitialFlipsToSkip = SKIP_FIRST_N_FLIPS;
  175. #endif
  176. };
  177. } else {
  178. m_bFreezingSAF = false;
  179. m_bSAFSaveStage = 0;
  180. return false;
  181. };
  182. return true;
  183. }
  184. bool CDetector::save_current_SAF( void ){
  185. char PathBuffer[18];
  186. FILE *tFileID;
  187. struct ffblk bb;
  188. if( m_bSAFSaveStage < 255 && m_bSAFSaveStage > 0 ){
  189. unifyDataSAF();
  190. sprintf( PathBuffer, "%c:\\saf%02d.thd", (char)(65+g_bFlashDriveID), m_nLRFID );
  191. if( findfirst( PathBuffer, &bb , 0 ) != -1 ) return false;
  192. tFileID = fopen( PathBuffer, "wb" );
  193. if(tFileID!=NULL){
  194. for( int index=0; index < LGS_MAX_DETECTOR_ANGLES; index++ ){
  195. // The ready SAF is the second one!!
  196. #ifdef WATCHDOG_ENABLED
  197. if( g_bWatchDogEnable ){ // update watchdog timer
  198. m_pWatchDog->Update( TIMER() );
  199. };
  200. #endif
  201. fwrite( &(m_ppSAF[1][index]), 4, 1, tFileID );
  202. };
  203. fclose(tFileID);
  204. } else return false;
  205. } else return false;
  206. return true;
  207. }
  208. void CDetector::unifyDataSAF( void ){
  209. uint16_t Max;
  210. uint16_t Min;
  211. for( int index=0; index < LGS_MAX_DETECTOR_ANGLES; index++ ){
  212. // The ready SAF is the second one!!
  213. #ifdef WATCHDOG_ENABLED
  214. if( g_bWatchDogEnable ){ // update watchdog timer
  215. m_pWatchDog->Update( TIMER() );
  216. };
  217. #endif
  218. Max = 0;
  219. Min = LGS_MAX_DETECTOR_RANGE;
  220. /* Minimums */
  221. if( m_ppSAF[0][index].nMin ) if( Min > m_ppSAF[0][index].nMin ) Min = m_ppSAF[0][index].nMin;
  222. if( m_ppSAF[1][index].nMin ) if( Min > m_ppSAF[1][index].nMin ) Min = m_ppSAF[1][index].nMin;
  223. //if( m_ppSAF[2][index].nMin ) if( Min > m_ppSAF[2][index].nMin ) Min = m_ppSAF[2][index].nMin;
  224. /* Maximums */
  225. if( m_ppSAF[0][index].nMax > Max ) Max = m_ppSAF[0][index].nMax;
  226. if( m_ppSAF[1][index].nMax > Max ) Max = m_ppSAF[1][index].nMax;
  227. //if( m_ppSAF[2][index].nMax > Max ) Max = m_ppSAF[2][index].nMax;
  228. /* Populate Data */
  229. m_ppSAF[2][index].nMax = m_ppSAF[0][index].nMax = m_ppSAF[1][index].nMax = Max;
  230. m_ppSAF[2][index].nMin = m_ppSAF[0][index].nMin = m_ppSAF[1][index].nMin = Min;
  231. };
  232. }
  233. #endif
  234. 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 )
  235. {
  236. #ifdef DEBUG_PRINTOUT_MSGS
  237. debugProcBegin( "Clear" );
  238. #endif
  239. Destroy();
  240. #ifdef DEBUG_PRINTOUT_MSGS
  241. debugProcEnd();
  242. #endif
  243. m_pTracker = pTracker;
  244. m_pShadow = pShadow;
  245. m_Settings.nShift = nShift;
  246. m_Settings.nOrder = nOrder;
  247. #ifdef LGS_REDUCED_PRF
  248. m_Settings.nOffset = nOffset * LGS_PRF_REDUCE_RATIO;
  249. #else
  250. m_Settings.nOffset = nOffset;
  251. #endif
  252. m_Settings.nDelta = nDelta;
  253. #ifdef LGS_MND
  254. m_Settings.nWidth = nWidth >> 2;
  255. #else
  256. m_Settings.nWidth = nWidth;
  257. #endif
  258. m_Settings.nFlip = nFlip;
  259. m_Settings.nSun = nSun;
  260. pTracker->SetLevel( nLevelSST );
  261. // if detector disabled no creation
  262. m_bDisabled = bDisabled;
  263. if( m_bDisabled )
  264. return true;
  265. #ifdef DEBUG_PRINTOUT_MSGS
  266. debugProcBegin( "SAF Memory");
  267. #endif
  268. int16_t i;
  269. for( i = 0; i < 3; i++ ){
  270. // alloc memory for SAF
  271. m_ppSAF[i] = (LPSAF) malloc( sizeof( SAF ) * LGS_MAX_DETECTOR_ANGLES );
  272. if( !m_ppSAF[i] ){
  273. #ifdef DEBUG_PRINTOUT_MSGS
  274. debugProcEnd( 0 );
  275. #endif
  276. return false;
  277. };
  278. }
  279. #ifdef DEBUG_PRINTOUT_MSGS
  280. debugProcEnd();
  281. debugProcBegin("Quality Points");
  282. #endif
  283. for( i = 0; i < 2; i++ ){ // 2??
  284. // alloc memory for quality point descriptors
  285. m_ppQPD[i] = (LPQPD) malloc( sizeof( QPD ) * LGS_MAX_DETECTOR_ANGLES );
  286. if( !m_ppQPD[i] ){
  287. #ifdef DEBUG_PRINTOUT_MSGS
  288. debugProcEnd( 0 );
  289. #endif
  290. return false;
  291. };
  292. // reset QPD data
  293. for( int16_t j = 0; j < LGS_MAX_DETECTOR_ANGLES; j++ ){
  294. m_ppQPD[i][j].nOffset = (uint8_t)m_Settings.nOffset;
  295. m_ppQPD[i][j].nOrder = (uint8_t)m_Settings.nOrder;
  296. }
  297. }
  298. #ifndef NO_DETECTOR_FILTERS
  299. #ifdef DEBUG_PRINTOUT_MSGS
  300. debugProcEnd();
  301. debugProcBegin( "Filters" );
  302. debugProcBegin( "Filter PD");
  303. #endif
  304. // create filer PD
  305. if( !m_FilterPD.Create( m_pTracker ) ){
  306. #ifdef DEBUG_PRINTOUT_MSGS
  307. debugProcEnd( 0 );
  308. debugProcEnd( 0 );
  309. #endif
  310. return false;
  311. };
  312. #ifdef DEBUG_PRINTOUT_MSGS
  313. debugProcEnd();
  314. debugProcBegin("Filter Fog");
  315. #endif
  316. // create filer Fog
  317. if( !m_FilterFog.Create( m_pTracker ) ){
  318. #ifdef DEBUG_PRINTOUT_MSGS
  319. debugProcEnd( 0 );
  320. debugProcEnd( 0 );
  321. #endif
  322. return false;
  323. };
  324. //Enable Fog Filter
  325. m_FilterFog.Enable();
  326. m_bSSTPassed = false;
  327. #ifdef DEBUG_PRINTOUT_MSGS
  328. debugProcEnd();
  329. debugProcBegin( "Filter Sun");
  330. #endif
  331. // allocate data for sun bit checking
  332. if( !m_FilterSun.Create( m_pTracker ) ){
  333. #ifdef DEBUG_PRINTOUT_MSGS
  334. debugProcEnd( 0 );
  335. debugProcEnd( 0 );
  336. #endif
  337. return false;
  338. };
  339. #endif //NO_DETECTOR_FILTERS
  340. #ifdef DEBUG_PRINTOUT_MSGS
  341. debugProcEnd();
  342. debugProcBegin( "Alloc Data for Border");
  343. #endif
  344. // allocate data for border
  345. #ifdef INFINITE_SAF_STUDY
  346. m_lpMinBorder = (LPWORD) malloc( sizeof( uint16_t ) * LGS_MAX_BORDER_ANGLES );
  347. if( !m_lpMinBorder ){
  348. #ifdef DEBUG_PRINTOUT_MSGS
  349. debugProcEnd( 0 );
  350. debugProcEnd( 0 );
  351. #endif
  352. return false;
  353. };
  354. memset( m_lpMinBorder, 0, sizeof( uint16_t ) * LGS_MAX_BORDER_ANGLES );
  355. m_lpMaxBorder = (LPWORD) malloc( sizeof( uint16_t ) * LGS_MAX_BORDER_ANGLES );
  356. if( !m_lpMaxBorder ){
  357. #ifdef DEBUG_PRINTOUT_MSGS
  358. debugProcEnd( 0 );
  359. debugProcEnd( 0 );
  360. #endif
  361. return false;
  362. };
  363. memset( m_lpMaxBorder, 0 , sizeof( uint16_t ) * LGS_MAX_BORDER_ANGLES );
  364. #else
  365. m_lpBorder = (LPWORD) malloc( sizeof( uint16_t ) * LGS_MAX_BORDER_ANGLES );
  366. if( !m_lpBorder ){
  367. #ifdef DEBUG_PRINTOUT_MSGS
  368. debugProcEnd( 0 );
  369. debugProcEnd( 0 );
  370. #endif
  371. return false;
  372. };
  373. memset( m_lpBorder, 0 , sizeof( uint16_t ) * LGS_MAX_BORDER_ANGLES );
  374. #endif
  375. #ifndef NO_DETECTOR_FILTERS
  376. #ifdef DEBUG_PRINTOUT_MSGS
  377. debugProcEnd();
  378. debugProcBegin( "Filter Medium");
  379. #endif
  380. if( !m_FilterMediumRange.Create( m_pTracker ) ){
  381. #ifdef DEBUG_PRINTOUT_MSGS
  382. debugProcEnd( 0 );
  383. debugProcEnd( 0 );
  384. #endif
  385. return false;
  386. };
  387. #endif // NO_DETECTOR_FILTERS
  388. #ifdef DEBUG_PRINTOUT_MSGS
  389. debugProcEnd();
  390. debugProcBegin( "Filter Width" );
  391. #endif
  392. if( !m_FilterWidth.Create( m_pTracker, "width.ini" ) ){
  393. #ifdef DEBUG_PRINTOUT_MSGS
  394. debugProcEnd( 0 );
  395. debugProcEnd( 0 );
  396. #endif
  397. return false;
  398. };
  399. #ifdef DEBUG_PRINTOUT_MSGS
  400. debugProcEnd();
  401. debugProcEnd();
  402. #endif
  403. Reset( true );
  404. return true;
  405. }
  406. void CDetector::Destroy()
  407. {
  408. int16_t i;
  409. for( i = 0; i < 3; i++ )
  410. {
  411. if( m_ppSAF[i] )
  412. {
  413. free( m_ppSAF[i] );
  414. m_ppSAF[i] = NULL;
  415. }
  416. }
  417. for( i = 0; i < 2; i++ )
  418. {
  419. if( m_ppQPD[i] )
  420. {
  421. free( m_ppQPD[i] );
  422. m_ppQPD[i] = NULL;
  423. }
  424. }
  425. #ifndef NO_DETECTOR_FILTERS
  426. // destroy filter PD
  427. m_FilterPD.Destroy();
  428. // destroy sun filter
  429. m_FilterSun.Destroy();
  430. #endif
  431. // destroy filter width
  432. m_FilterWidth.Destroy();
  433. m_pShadow = NULL;
  434. #ifdef INFINITE_SAF_STUDY
  435. if( m_lpMinBorder )
  436. {
  437. free( m_lpMinBorder );
  438. m_lpMinBorder = NULL;
  439. }
  440. if( m_lpMaxBorder )
  441. {
  442. free( m_lpMaxBorder );
  443. m_lpMaxBorder = NULL;
  444. }
  445. #else
  446. if( m_lpBorder )
  447. {
  448. free( m_lpBorder );
  449. m_lpBorder = NULL;
  450. }
  451. #endif
  452. #ifndef NO_DETECTOR_FILTERS
  453. m_FilterMediumRange.Destroy();
  454. #endif
  455. Reset( true );
  456. }