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.

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