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.

946 lines
34 KiB

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