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.

972 lines
31 KiB

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