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.

480 lines
11 KiB

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