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.

886 lines
26 KiB

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