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.

851 lines
26 KiB

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