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.

828 lines
24 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
7 years ago
​
6 years ago
6 years ago
6 years ago
7 years ago
6 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
6 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
7 years ago
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
​
7 years ago
6 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Dr.Frucht Systems Ltd.
  3. // Copyright � 2000-2017. All Rights Reserved.
  4. // Version 2.0 7/20/2017
  5. // Last modification: 7/20/2017
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: LRF_Kernel.cpp
  9. // AUTHOR: gagua
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // LRF_Kernel.cpp
  13. //----------------------------------------------------------------------------
  14. //
  15. #include "LRF_Kernel.h"
  16. #include "LRF_Detector.h"
  17. #define LOG( X ) m_pLog->Log( "Kernel", this, X )
  18. #define LOG_ERROR( X ) m_pLog->Log( "Kernel", this, _LOG_ERROR, X )
  19. // Constructor implementation
  20. LRF_Kernel::LRF_Kernel( LRF_HardwareManager *HM ): basicThread(){
  21. m_bExitCause = 0;
  22. m_pLog = basicLogCollection::getInstance();
  23. m_pEventHeap = basicEventHeap::getInstance();
  24. m_pSettingsProvider = basicSettingsProvider::getInstance();
  25. if( m_pSettingsProvider ) {
  26. m_pSettings = m_pSettingsProvider->getSubNode( NULL, "KERNEL" );
  27. if( m_pSettingsProvider->isCompromised() ) {
  28. m_pSettingsProvider->validate();
  29. };
  30. } else {
  31. m_pSettings = NULL;
  32. };
  33. m_pHardwareManager = HM;
  34. m_nKernelFilter = 0x000000FF;
  35. m_nFlipCounter = 0;
  36. m_nFlipSavierID = 0;
  37. m_pShadow = NULL;
  38. #ifdef __EU_PROJECT__
  39. m_pSafeShoreHandler = NULL;
  40. m_bSafeShoreSupport = false;
  41. m_pDefenderHandler = NULL;
  42. m_bDefenderSupport = false;
  43. m_pKafkaHandler = NULL;
  44. m_bKafkaSupport = false;
  45. #endif
  46. m_pTracker = NULL;
  47. m_pNetClientsServer = NULL;
  48. m_pThreshold = NULL;
  49. m_eWorkMode = WM_NONE;
  50. m_nWidth = 800;
  51. m_nSST = 0;
  52. };
  53. // Destructor implementation
  54. LRF_Kernel::~LRF_Kernel(){
  55. #ifdef DEBUG
  56. LOG("Destructor");
  57. #endif
  58. WaitFinish(true);
  59. #ifdef DEBUG
  60. LOG("Deleting Components");
  61. #endif
  62. #ifdef __EU_PROJECT__
  63. if( m_pSafeShoreHandler ) delete( m_pSafeShoreHandler );
  64. #ifdef DEBUG
  65. LOG("Safe Shore - Done");
  66. #endif
  67. if( m_pDefenderHandler ) delete( m_pDefenderHandler );
  68. #ifdef DEBUG
  69. LOG("Defender - Done");
  70. #endif
  71. if( m_pKafkaHandler ) delete( m_pKafkaHandler );
  72. #ifdef DEBUG
  73. LOG("KAFKA - Done");
  74. #endif
  75. #endif
  76. if( m_pShadow ) delete( m_pShadow );
  77. #ifdef DEBUG
  78. LOG("Shadow Mask - Done");
  79. LOG("Destructor Finished");
  80. #endif
  81. };
  82. bool LRF_Kernel::attachNetServer( networkServer *srvPort ){
  83. if( srvPort != NULL ) {
  84. m_pNetClientsServer = srvPort;
  85. };
  86. if( m_pNetClientsServer!= NULL ) {
  87. return srvPort->attachEngine( (basicEngine *)this );
  88. };
  89. return false;
  90. };
  91. bool LRF_Kernel::create( LRF_HardwareManager *ptr, networkServer *srvPort ){
  92. if( m_pSettings != NULL ) {
  93. config_setting_t *pSetting;
  94. int tVal;
  95. char tBuffer[256];
  96. LOG("Getting Settings ...");
  97. #ifdef __EU_PROJECT__
  98. if( !config_setting_lookup_bool( m_pSettings, KNL_SETTINGS_SAFE_SHORE, &tVal ) ){
  99. tVal = m_bSafeShoreSupport = true;
  100. pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_SAFE_SHORE, CONFIG_TYPE_BOOL );
  101. config_setting_set_bool( pSetting, tVal );
  102. m_pSettingsProvider->compromise();
  103. LOG("Safe-Shore support not defined");
  104. } else {
  105. m_bSafeShoreSupport = (bool)tVal;
  106. sprintf( tBuffer, "Safe-Shore support Read From Settings: %d", tVal );
  107. LOG( tBuffer );
  108. };
  109. #endif
  110. if( !config_setting_lookup_bool( m_pSettings, KNL_SETTINGS_DEFENDER, &tVal ) ){
  111. tVal = m_bDefenderSupport = true;
  112. pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_DEFENDER, CONFIG_TYPE_BOOL );
  113. config_setting_set_bool( pSetting, tVal );
  114. m_pSettingsProvider->compromise();
  115. LOG("Defender support not defined");
  116. } else {
  117. m_bDefenderSupport = (bool)tVal;
  118. sprintf( tBuffer, "Defender support Read From Settings: %d", tVal );
  119. LOG( tBuffer );
  120. };
  121. if (!config_setting_lookup_int(m_pSettings, KNL_SETTINGS_DETECTOR_WIDTH, &tVal )){
  122. tVal = m_nWidth = 1000;
  123. pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_DETECTOR_WIDTH, CONFIG_TYPE_INT);
  124. config_setting_set_int(pSetting, tVal );
  125. m_pSettingsProvider->compromise();
  126. LOG("No Detector Width");
  127. } else {
  128. m_nWidth = tVal;
  129. sprintf( tBuffer, "Detector Width Read From Settings: %d", tVal );
  130. LOG( tBuffer );
  131. };
  132. if (!config_setting_lookup_int(m_pSettings, KNL_SETTINGS_FILTER_KERNELS, &tVal )){
  133. tVal = m_nKernelFilter = 0x000000ff;
  134. pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_FILTER_KERNELS, CONFIG_TYPE_INT);
  135. config_setting_set_int(pSetting, tVal );
  136. m_pSettingsProvider->compromise();
  137. LOG("No Fltered Kernels Defined - All LRF Will Work");
  138. } else {
  139. m_nKernelFilter = tVal;
  140. sprintf( tBuffer, "Filter Kernels Read From Settings: %d", tVal );
  141. LOG( tBuffer );
  142. };
  143. if (!config_setting_lookup_int(m_pSettings, KNL_SETTINGS_DETECTOR_SST, &tVal )){
  144. tVal = m_nSST = 0;
  145. pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_DETECTOR_SST, CONFIG_TYPE_INT);
  146. config_setting_set_int(pSetting, tVal );
  147. m_pSettingsProvider->compromise();
  148. LOG("No Detector SST");
  149. } else {
  150. m_nSST = tVal;
  151. sprintf( tBuffer, "Detector SST Read From Settings: %d", tVal );
  152. LOG( tBuffer );
  153. };
  154. if( m_pSettingsProvider->isCompromised() ) {
  155. LOG("Save Settings");
  156. m_pSettingsProvider->validate();
  157. };
  158. } else {
  159. LOG_ERROR("Failed Read Config Settings!");
  160. return false;
  161. };
  162. m_eWorkMode = WM_INIT;
  163. m_nFlipCounter = 0;
  164. m_nFlipSavierID = 0;
  165. m_pShadow = new ShadowMask();
  166. if( m_pShadow == NULL ) {
  167. LOG_ERROR("Failed to create Shadow Mask!");
  168. return false;
  169. };
  170. if( m_pShadow->isActive() ) {
  171. LOG("Shadow Mask is ACTIVE");
  172. } else {
  173. LOG("Shadow Mask is NOT ACTIVE");
  174. }
  175. if( m_pEventHeap == NULL ){
  176. LOG_ERROR("No Event Heap attached!");
  177. return false;
  178. };
  179. if( ptr != NULL ) {
  180. m_pHardwareManager = ptr;
  181. m_pHardwareManager->setKernelFilter( m_nKernelFilter );
  182. };
  183. if( m_pHardwareManager == NULL ) {
  184. LOG_ERROR("No Hardware manager attached!");
  185. return false;
  186. };
  187. #ifdef HIBERNATE_ENABLE
  188. if( fileExist("hibernate.dat")) {
  189. LOG("Loading Hibernated Data.");
  190. if(!m_pHardwareManager->loadHibernatedTH("hibernate.dat")){
  191. LOG_ERROR("Hibernate Data Load failure!");
  192. };
  193. };
  194. #endif
  195. if( !attachNetServer( srvPort ) ) {
  196. LOG_ERROR("No Network manager attached!");
  197. return false;
  198. };
  199. m_lCycleCounter = 2;
  200. #ifdef __EU_PROJECT__
  201. if( m_bSafeShoreSupport ){
  202. m_pSafeShoreHandler = new LRF_SafeShoreHandler( m_pSettings );
  203. if( m_pSafeShoreHandler == NULL ) {
  204. LOG_ERROR("SafeShore Protocol failed!");
  205. };
  206. };
  207. if( m_bDefenderSupport ) {
  208. m_pDefenderHandler = new LRF_DefenderHandler( m_pSettings );
  209. if( m_pDefenderHandler == NULL ) {
  210. LOG_ERROR("Defender Protocol failed!");
  211. };
  212. };
  213. if( m_bKafkaSupport ) {
  214. m_pKafkaHandler = new LRF_KafkaHandler( m_pSettings );
  215. if( m_pKafkaHandler == NULL ) {
  216. LOG_ERROR("Kafka Protocol failed!");
  217. };
  218. };
  219. #endif
  220. #ifndef PRU_SLAVE
  221. #ifdef DEBUG
  222. LOG( "Target Tracker about to be created" );
  223. #endif
  224. m_pTracker = new LRF_TargetTracker( m_nSST, m_nWidth );
  225. if( m_pTracker == NULL ) {
  226. LOG_ERROR("Error in starting Target Tracker!");
  227. return false;
  228. };
  229. #ifdef DEBUG
  230. LOG( "Created" );
  231. #endif
  232. #endif // !PRU_SLAVE
  233. #ifdef DEBUG
  234. LOG( "Trhead about to be created" );
  235. #endif
  236. if( init() == false ){
  237. LOG_ERROR("Error in starting Kernel Thread!");
  238. return false;
  239. };
  240. #ifdef DEBUG
  241. LOG( "Created" );
  242. #endif
  243. m_eWorkMode = WM_STUDY;
  244. return true;
  245. };
  246. void LRF_Kernel::WaitFinish( bool Force ){
  247. if( Force ){
  248. #ifdef DEBUG
  249. LOG( "Thread End Forced" );
  250. #endif
  251. } else {
  252. // 3 seconds sleep to settle down processes
  253. SLEEP(3000);
  254. };
  255. waitThreadEnd( Force );
  256. #ifdef DEBUG
  257. LOG( "Thread is Detached" );
  258. #endif
  259. };
  260. /* Main Kernel Thread Procedure */
  261. void LRF_Kernel::m_fThreadProc( void ){
  262. #ifdef SIMULATE_TARGETS
  263. uint8_t CCNT = 0;
  264. uint16_t CNNT = 0;
  265. #endif
  266. char Buffer[256];
  267. DT_STATUS tStatus = {0};
  268. m_bThreadStatus = TH_WORKING;
  269. while( m_bThreadStatus < TH_ENDED ){
  270. if( !m_pHardwareManager->validateCores() ){
  271. //Handle Error in PRU Cores
  272. LOG_ERROR("PRU Core Trouble Detected!");
  273. m_eWorkMode = WM_ERROR;
  274. while( ( m_bThreadStatus < TH_ENDED ) && ( !m_pHardwareManager->validateCores()) ) SLEEP( 100 ); //Stop Kernel
  275. #ifdef DEBUG
  276. LOG( "Cores Became OK!" );
  277. #endif
  278. m_eWorkMode = WM_INIT;
  279. }
  280. #ifdef SIMULATE_TARGETS
  281. #ifdef SIMULATE_DOS_TARGETS
  282. if( m_lCycleCounter % 10 == 0 ) {
  283. #else
  284. if( m_lCycleCounter % 1000 == 0 ) {
  285. #endif
  286. // Generate Target
  287. CNNT++;
  288. targetRecord cNode;
  289. cNode.begin_id = ((TH_SIZE_NODES>>1)+CCNT*(TH_LINE_NODES>>3)+CNNT)%TH_SIZE_NODES;
  290. cNode.end_id = (cNode.begin_id+TH_LINE_NODES)%TH_SIZE_NODES;
  291. cNode.status = 0;
  292. cNode.flags.active = 1;
  293. cNode.flags.lrf = CCNT;
  294. cNode.flags.w_cnt = 2;
  295. cNode.width = ((4095>>3)*(8-CCNT))<<1;
  296. cNode.flags.type = TARGET_WHITE;
  297. m_pTracker->validateTarget( &cNode, m_pHardwareManager->isMaster() );
  298. CCNT++;
  299. CCNT &= 7;
  300. };
  301. #endif
  302. // Check Working Mode
  303. if( tStatus.mode != WM_DETECTOR ){
  304. //if( m_eWorkMode != WM_DETECTOR ){
  305. if( m_pHardwareManager->getDetectionStatus( tStatus ) ){
  306. if( tStatus.mode == WM_DETECTOR ) {
  307. // Detection Mode
  308. m_eWorkMode = WM_DETECTOR;
  309. #ifdef DEBUG
  310. LOG( "Switching to detection mode!" );
  311. #ifdef NO_BLACK_TARGETS
  312. LOG( "B-Target Will be Ignored by Build" );
  313. #endif
  314. #endif
  315. #ifdef __EU_PROJECT__
  316. if( m_bSafeShoreSupport ){
  317. if( m_pSafeShoreHandler != NULL ) {
  318. m_pSafeShoreHandler->SetStatus( true );
  319. };
  320. };
  321. #endif
  322. if( m_bDefenderSupport ) {
  323. if( m_pDefenderHandler != NULL ) {
  324. m_pDefenderHandler->SetStatus( true );
  325. };
  326. };
  327. thresholdNode *ptr;
  328. ptr = m_pHardwareManager->getWorkingMemory();
  329. if( ptr!= NULL ){
  330. m_nThresholdSize = TH_SIZE_NODES;
  331. m_pThreshold = (thresholdNode *) malloc( sizeof(thresholdNode)* m_nThresholdSize);
  332. if( m_pThreshold!= NULL ) {
  333. memcpy( m_pThreshold, ptr, m_nThresholdSize );
  334. } else m_nThresholdSize = 0;
  335. };
  336. };
  337. };
  338. } else { //m_eWorkMode == WM_DETECTOR
  339. // Readout Events
  340. int index;
  341. bool bActiveRecordsPresent;
  342. targetRecord cNode;
  343. volatile targetRecord *tNode;
  344. tNode = (volatile targetRecord *) m_pHardwareManager->getEventMemory();
  345. if( tNode != NULL ) {
  346. //LOG( "Event Mem accessed OK");
  347. basicEvent *tEvt = NULL;
  348. uint32_t cRange;
  349. uint32_t cTrackID = 0;
  350. bActiveRecordsPresent = true;
  351. while( bActiveRecordsPresent && ( m_bThreadStatus < TH_ENDED ) ) {
  352. bActiveRecordsPresent = false;
  353. for( index = 0; index < MAX_TARGETS_COUNT; index++){
  354. // Copy record from MEM
  355. //LOG("Checking Event Record");
  356. cNode.width = tNode[index].width;
  357. cNode.begin_id = tNode[index].begin_id;
  358. cNode.end_id = tNode[index].end_id;
  359. cNode.status = tNode[index].status;
  360. // Unlock record for PRU
  361. tNode[index].status = 0;
  362. // Check by Working TH Complete for LRF
  363. // if( cNode.flags.type == TARGET_WHITE || cNode.flags.type == TARGET_BLACK ) {
  364. // // If not -> cancel analyze
  365. // if((m_pHardwareManager->getTargetFilter() & ( 1 << cNode.flags.lrf )) == 0 ) continue;
  366. // };
  367. if( cNode.flags.active ){
  368. // Filter Events from Disabled Kernels -> goes to flip
  369. if( ((1 << cNode.flags.lrf ) & m_nKernelFilter ) == 0 ){
  370. // Abort checking this target since Kernel Disabled
  371. continue;
  372. };
  373. if( cNode.flags.w_cnt ){
  374. cRange = cNode.width / cNode.flags.w_cnt;
  375. #ifdef SHIFT_50
  376. cRange += SHIFT_50;
  377. // width must be updated accordingly
  378. cNode.width = cRange * cNode.flags.w_cnt;
  379. #endif
  380. // Filter Target's Width in Da Tracker!
  381. // if( m_nWidth > 0 ) {
  382. // if( cNode.width < m_nWidth ) {
  383. // Abort Detection of this target since small width
  384. // continue;
  385. // };
  386. // };
  387. };// else {
  388. // Abort checking this target since no width
  389. // continue;
  390. //};
  391. tEvt = NULL;
  392. bActiveRecordsPresent = true;
  393. switch( cNode.flags.type ){
  394. case TARGET_WHITE:{
  395. #ifndef SKIP_TARGET_LOG
  396. #ifdef DEBUG
  397. LOG( "Push W-Target Into Tracker" );
  398. #endif
  399. #endif
  400. if( m_pShadow != NULL ) {
  401. if( m_pShadow->isActive() ) {
  402. uint16_t cPointX, cPointY;
  403. makeXY( cNode.begin_id, cPointX, cPointY, m_pHardwareManager->isMaster() );
  404. if( m_pShadow->isOutside( cPointX, cPointY )){
  405. cTrackID = m_pTracker->validateTarget( &cNode, m_pHardwareManager->isMaster());
  406. #ifndef SKIP_TARGET_LOG
  407. #ifdef DEBUG
  408. sprintf( Buffer, "Assigned ID [%d] area: %d CNT: %d bID: %d eID: %d",
  409. cTrackID,
  410. cNode.width,
  411. cNode.flags.w_cnt,
  412. cNode.begin_id,
  413. cNode.end_id
  414. );
  415. LOG( Buffer );
  416. #endif
  417. #endif
  418. } else {
  419. #ifndef SKIP_TARGET_LOG
  420. LOG( "Target Shadowed" );
  421. #endif
  422. };
  423. } else { // Shadow is Disabled
  424. cTrackID = m_pTracker->validateTarget( &cNode, m_pHardwareManager->isMaster());
  425. #ifndef SKIP_TARGET_LOG
  426. #ifdef DEBUG
  427. sprintf( Buffer, "Assigned ID [%d] area: %d CNT: %d bID: %d eID: %d",
  428. cTrackID,
  429. cNode.width,
  430. cNode.flags.w_cnt,
  431. cNode.begin_id,
  432. cNode.end_id
  433. );
  434. LOG( Buffer );
  435. #endif
  436. #endif
  437. }
  438. } else {
  439. LOG_ERROR("NO SHADOW OBJECT!");
  440. m_bThreadStatus = TH_ENDED;
  441. continue;
  442. }
  443. break;
  444. };
  445. case TARGET_BLACK:{
  446. #ifdef NO_BLACK_TARGETS
  447. #ifndef SKIP_TARGET_LOG
  448. #ifdef DEBUG
  449. LOG( "B-Target Ignored by Build" );
  450. #endif
  451. #endif
  452. };
  453. break;
  454. #else
  455. #ifndef SKIP_TARGET_LOG
  456. #ifdef DEBUG
  457. LOG( "Push B-Target Into Tracker" );
  458. #endif
  459. #endif
  460. if( m_pShadow != NULL ) {
  461. if( m_pShadow->isActive() ) {
  462. uint16_t cPointX, cPointY;
  463. makeXY( cNode.begin_id, cPointX, cPointY, m_pHardwareManager->isMaster() );
  464. if( m_pShadow->isOutside( cPointX, cPointY )){
  465. cTrackID = m_pTracker->validateTarget( &cNode, m_pHardwareManager->isMaster());
  466. #ifndef SKIP_TARGET_LOG
  467. #ifdef DEBUG
  468. sprintf( Buffer, "Assigned ID [%d] area: %d CNT: %d bID: %d eID: %d",
  469. cTrackID,
  470. cNode.width,
  471. cNode.flags.w_cnt,
  472. cNode.begin_id,
  473. cNode.end_id
  474. );
  475. LOG( Buffer );
  476. #endif
  477. #endif
  478. };
  479. } else { // Shadow is Disabled
  480. cTrackID = m_pTracker->validateTarget( &cNode, m_pHardwareManager->isMaster());
  481. #ifndef SKIP_TARGET_LOG
  482. #ifdef DEBUG
  483. sprintf( Buffer, "Assigned ID [%d] area: %d CNT: %d bID: %d eID: %d",
  484. cTrackID,
  485. cNode.width,
  486. cNode.flags.w_cnt,
  487. cNode.begin_id,
  488. cNode.end_id
  489. );
  490. LOG( Buffer );
  491. #endif
  492. #endif
  493. }
  494. } else {
  495. LOG_ERROR("NO SHADOW OBJECT!");
  496. m_bThreadStatus = TH_ENDED;
  497. continue;
  498. }
  499. };
  500. break;
  501. #endif // NO_BLACK_TARGETS
  502. case SCAN_COMPLETE:
  503. m_pTracker->ScanComplete();
  504. sprintf( Buffer,"Scan Complete [%d,%d]", cNode.width, cNode.begin_id );
  505. #ifdef DEBUG
  506. LOG( Buffer );
  507. #endif
  508. tEvt = createBasicEvent( 0, EVT_CLASS_SENSOR, EVT_SCAN, Buffer, NULL );
  509. break;
  510. case SCAN_FROZEN:
  511. m_pTracker->ScanComplete();
  512. sprintf( Buffer,"Scan was Frozen", cNode.width, cNode.begin_id );
  513. #ifdef DEBUG
  514. LOG( Buffer );
  515. #endif
  516. tEvt = createBasicEvent( 0, EVT_CLASS_SENSOR, EVT_SCAN, Buffer, NULL );
  517. break;
  518. case FLIP_COMPLETE:
  519. m_nFlipCounter = cNode.begin_id;
  520. sprintf( Buffer,"Flip Complete [%d,%d]", cNode.width, m_nFlipCounter );
  521. #ifdef DEBUG
  522. LOG( Buffer );
  523. #endif
  524. tEvt = createBasicEvent( 0, EVT_CLASS_SENSOR, EVT_FLIP, Buffer, NULL );
  525. break;
  526. case MIRROR_FINISHED:
  527. //LOG("Mirror");
  528. m_pTracker->MirrorComplete();
  529. break;
  530. case ERROR_ANGLE:
  531. sprintf( Buffer,"Error Angle [%d,%d]", cNode.width, cNode.begin_id );
  532. #ifdef DEBUG
  533. LOG_ERROR( Buffer );
  534. #endif
  535. tEvt = createBasicEvent( 0, EVT_CLASS_SENSOR, EVT_ERROR, Buffer, NULL );
  536. break;
  537. };
  538. if( tEvt != NULL) {
  539. m_pEventHeap->broadcast( tEvt );
  540. // Evt utilisation done by EventHeap thread
  541. tEvt = NULL;
  542. };// else { LOG("No Event Created!" ); };
  543. };
  544. };
  545. };
  546. //LOG("End Event Check");
  547. } else {
  548. LOG_ERROR("No access to Event Memory");
  549. m_bThreadStatus = TH_ENDED;
  550. };
  551. };
  552. SLEEP( SLEEP_KERNEL_INTERVAL );
  553. if(( m_lCycleCounter >> 31 )!= 0 ) {
  554. m_lCycleCounter = 0;
  555. } else m_lCycleCounter++;
  556. };
  557. };
  558. bool LRF_Kernel::setMode( LRF_WORK_MODE nMode ){
  559. if( m_eWorkMode == nMode ) return true;
  560. #ifndef NO_MOTORS
  561. if( m_eWorkMode == WM_DETECTOR || nMode == WM_DETECTOR ){
  562. #endif
  563. if( nMode == WM_BORDER ) {
  564. LOG("Switched to Mode BORDER");
  565. };
  566. if( nMode == WM_DETECTOR ) {
  567. LOG("Switched to Mode DETECTOR");
  568. };
  569. // No other cleints demand specoal mode
  570. m_eWorkMode = nMode;
  571. return true;
  572. #ifndef NO_MOTORS
  573. };
  574. return false;
  575. #endif
  576. };
  577. PLGS_PARAMETERS LRF_Kernel::getDetectionParameters( void ){
  578. PLGS_PARAMETERS tParam = NULL;
  579. if( m_pHardwareManager != NULL ){
  580. tParam = (PLGS_PARAMETERS) malloc(sizeof(LGS_PARAMETERS));
  581. memset( tParam, 0 , sizeof(LGS_PARAMETERS));
  582. if( tParam != NULL ){
  583. /*TH_PARAMS tThParams;
  584. if( m_pHardwareManager->getDetectionParams( tThParams ) ){
  585. tParam->flags.BuildThByCore = tThParams.self_build_ena;
  586. tParam->bSST = m_nSST;
  587. tParam->nDelta = tThParams.delta;
  588. tParam->nFlip = tThParams.flip_size;
  589. tParam->nGPointSamples = tThParams.good_points;
  590. tParam->nOffset = tThParams.offset;
  591. tParam->nOrder = tThParams.order;
  592. tParam->nSun = 0;
  593. tParam->nWidth = m_nWidth;
  594. };
  595. //DT_STATUS tThStatus;
  596. m_pHardwareManager->getHardwareParams( tParam->flags );
  597. //if( m_pHardwareManager->getDetectionStatus( tThStatus ) ) {
  598. // tParam->flags.LaserPulsesOn = 1;
  599. //};
  600. */
  601. if( !m_pHardwareManager->getParams( *tParam ) ) {
  602. LOG_ERROR("getParams ERROR!");
  603. free( tParam );
  604. tParam = NULL;
  605. } else {
  606. char tBuf[123];
  607. for( int i = 0; i < sizeof(HW_PARAMS); i++ ) {
  608. sprintf( tBuf, "FLAGS_BIN[%d]: %d", i, tParam->flags_binary[i] );
  609. LOG( tBuf );
  610. };
  611. sprintf( tBuf, "Sunbit: %d", tParam->flags.Sunbit_filter );
  612. LOG( tBuf );
  613. sprintf( tBuf, "CAN My ID: %d", tParam->flags.TGZ_canSRC );
  614. LOG( tBuf );
  615. sprintf( tBuf, "CAN TGZ ID: %d", tParam->flags.TGZ_canDST );
  616. LOG( tBuf );
  617. };
  618. };
  619. };
  620. return tParam;
  621. };
  622. bool LRF_Kernel::setDetectionParameters( PLGS_PARAMETERS ptr ){
  623. bool isCompromised = false;
  624. HW_PARAMS tHwParams;
  625. TH_PARAMS tThParams;
  626. if( m_pHardwareManager != NULL ){
  627. if( m_pHardwareManager->getDetectionParams(tThParams)){
  628. if( tThParams.delta != ptr->nDelta ){
  629. tThParams.delta = ptr->nDelta; isCompromised = true;
  630. };
  631. if( tThParams.flip_size != ptr->nFlip ){
  632. tThParams.flip_size = ptr->nFlip; isCompromised = true;
  633. };
  634. if( tThParams.good_points != ptr->nGPointSamples ){
  635. tThParams.good_points = ptr->nGPointSamples; isCompromised = true;
  636. };
  637. if( tThParams.offset != ptr->nOffset ){
  638. tThParams.offset = ptr->nOffset; isCompromised = true;
  639. };
  640. if( tThParams.order != ptr->nOrder ){
  641. tThParams.order = ptr->nOrder; isCompromised = true;
  642. };
  643. if( tThParams.self_build_ena != ptr->flags.BuildThByCore ){
  644. tThParams.self_build_ena = ptr->flags.BuildThByCore; isCompromised = true;
  645. };
  646. if( tThParams.master != ptr->flags.isMaster ) {
  647. tThParams.master = ptr->flags.isMaster; isCompromised = true;
  648. };
  649. if( isCompromised ) m_pHardwareManager->setDetectionParams( &tThParams );
  650. } else {
  651. LOG_ERROR("Cannot Get Actual Detection Settings!");
  652. return false;
  653. };
  654. isCompromised = false;
  655. if( m_pHardwareManager->getHardwareParams( tHwParams ) ) {
  656. if( tHwParams.AutostartMotors != ptr->flags.AutostartMotors ){
  657. tHwParams.AutostartMotors = ptr->flags.AutostartMotors; isCompromised = true;
  658. };
  659. if( tHwParams.BaseScanCount != ptr->flags.BaseScanCount ){
  660. tHwParams.BaseScanCount = ptr->flags.BaseScanCount; isCompromised = true;
  661. };
  662. if( tHwParams.BBinterlace != ptr->flags.BBinterlace ){
  663. tHwParams.BBinterlace = ptr->flags.BBinterlace; isCompromised = true;
  664. };
  665. if( tHwParams.BuildThByCore != ptr->flags.BuildThByCore ){
  666. tHwParams.BuildThByCore = ptr->flags.BuildThByCore; isCompromised = true;
  667. };
  668. if( tHwParams.DetectorsCount != ptr->flags.DetectorsCount ){
  669. tHwParams.DetectorsCount = ptr->flags.DetectorsCount; isCompromised = true;
  670. };
  671. if( tHwParams.DryContactMode1 != ptr->flags.DryContactMode1 ){
  672. tHwParams.DryContactMode1 = ptr->flags.DryContactMode1; isCompromised = true;
  673. };
  674. if( tHwParams.DryContactMode2 != ptr->flags.DryContactMode2 ){
  675. tHwParams.DryContactMode2 = ptr->flags.DryContactMode2; isCompromised = true;
  676. };
  677. if( tHwParams.DryContactStatus1 != ptr->flags.DryContactStatus1 ){
  678. tHwParams.DryContactStatus1 = ptr->flags.DryContactStatus1; isCompromised = true;
  679. };
  680. if( tHwParams.DryContactStatus2 != ptr->flags.DryContactStatus2 ){
  681. tHwParams.DryContactStatus2 = ptr->flags.DryContactStatus2; isCompromised = true;
  682. };
  683. if( tHwParams.HorizontalFreq != ptr->flags.HorizontalFreq ){
  684. tHwParams.HorizontalFreq = ptr->flags.HorizontalFreq; isCompromised = true;
  685. };
  686. if( tHwParams.HorizontalMotorOn != ptr->flags.HorizontalMotorOn ){
  687. tHwParams.HorizontalMotorOn = ptr->flags.HorizontalMotorOn; isCompromised = true;
  688. };
  689. if( tHwParams.LaserPulsesOn != ptr->flags.LaserPulsesOn ){
  690. tHwParams.LaserPulsesOn = ptr->flags.LaserPulsesOn; isCompromised = true;
  691. };
  692. if( tHwParams.MirrorScanCount != ptr->flags.MirrorScanCount ){
  693. tHwParams.MirrorScanCount = ptr->flags.MirrorScanCount; isCompromised = true;
  694. };
  695. if( tHwParams.QQinterlace != ptr->flags.QQinterlace ){
  696. tHwParams.QQinterlace = ptr->flags.QQinterlace; isCompromised = true;
  697. };
  698. if( isCompromised ) m_pHardwareManager->setHardwareParams( &tHwParams );
  699. } else {
  700. LOG_ERROR("Cannot Get Actual Hardware Settings!");
  701. return false;
  702. };
  703. return true;
  704. };
  705. return false;
  706. };
  707. thresholdNode *LRF_Kernel::getTreshold( uint32_t &size, uint8_t kID ){
  708. if( m_pHardwareManager!= NULL ) {
  709. thresholdNode *ptr;
  710. ptr = m_pHardwareManager->getWorkingMemory();
  711. #ifdef TH_PER_MIRROR
  712. kID = (kID>39)?39:kID;
  713. #else
  714. kID = (kID>15)?15:kID;
  715. #endif
  716. if( ptr!= NULL ){
  717. m_nThresholdSize = TH_SIZE_NODES;
  718. if( m_pThreshold == NULL ) {
  719. //m_pThreshold = (thresholdNode *) malloc( sizeof(thresholdNode)* m_nThresholdSize );
  720. m_pThreshold = (thresholdNode *) malloc( TH_SIZE_BYTES );
  721. };
  722. if( m_pThreshold != NULL ) {
  723. //memcpy( m_pThreshold, ptr+(kID*TH_SIZE_NODES), sizeof(thresholdNode)* m_nThresholdSize );
  724. memcpy( m_pThreshold, ptr+(kID*TH_SIZE_NODES), TH_SIZE_BYTES );
  725. } else m_nThresholdSize = 0;
  726. };
  727. };
  728. size = m_nThresholdSize;
  729. return m_pThreshold;
  730. };
  731. bool LRF_Kernel::reset( bool FullRestart ){
  732. if( FullRestart ) {
  733. //Shutting down Motors and Hardware
  734. //Starting up Motors and Hardware
  735. shutdown( true );
  736. } else {
  737. m_pHardwareManager->reset( true );
  738. };
  739. return true;
  740. };
  741. bool LRF_Kernel::shutdown( bool Restart ){
  742. if( Restart ) {
  743. LOG("Restarting the Kernel by SHUTDOWN!");
  744. m_bExitCause = 1;
  745. } else {
  746. LOG("Shutting down the Kernel by SHUTDOWN!");
  747. m_bExitCause = 0;
  748. }
  749. setStatus( TH_ENDED );
  750. return true;
  751. };
  752. bool LRF_Kernel::stopMotors( void ){
  753. #ifndef ONPC_TOTTAL_SIMULATOR
  754. if( m_pHardwareManager!= NULL ) {
  755. return m_pHardwareManager->stopMotors( false );
  756. };
  757. return false;
  758. #else
  759. return true;
  760. #endif
  761. };
  762. bool LRF_Kernel::startMotors( void ){
  763. #ifndef ONPC_TOTTAL_SIMULATOR
  764. if( m_pHardwareManager!= NULL ) {
  765. return m_pHardwareManager->startMotors( false );
  766. };
  767. return false;
  768. #else
  769. return true;
  770. #endif
  771. };
  772. /*
  773. bool LRF_Kernel::getTarget( TARGET *t ){
  774. return false;
  775. };
  776. uint32_t LRF_Kernel::getErrorAngle( void ){
  777. return 0;
  778. };
  779. void LRF_Kernel::clearErrorAngle( void ){
  780. };
  781. bool LRF_Kernel::isErrorAngle( void ){
  782. return false;
  783. };
  784. */
  785. #undef LOG
  786. #undef LOG_ERROR