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.

843 lines
25 KiB

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