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.

942 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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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.width = ((4095>>3)*(8-CCNT))<<1;
  305. cNode.flags.type = TARGET_WHITE;
  306. m_pTracker->validateTarget( &cNode, m_pHardwareManager->isMaster() );
  307. CCNT++;
  308. CCNT &= 7;
  309. };
  310. #endif
  311. if( !m_pHardwareManager->validateCores() ){
  312. if( getWM() != WM_ERROR ){
  313. setWM(WM_ERROR);
  314. LOG_ERROR("PRU Core Trouble Detected!");
  315. };
  316. } else {
  317. // PRU Cores are valid
  318. bActiveTH = m_nKernelFilter & m_pHardwareManager->getDetectionStatus();
  319. bMeasuredTH = m_nKernelFilter & m_pHardwareManager->getTHVisual();
  320. switch( getWM() ) {
  321. case WM_INIT:{
  322. if( m_pHardwareManager->getCoreStatus( tStatus ) ){
  323. m_pHardwareManager->reset( false );
  324. if( tStatus.mode == WM_DETECTOR ){
  325. LOG("**********SWITCHING TO STUDY MODE**********");
  326. setWM( WM_STUDY );
  327. };
  328. };
  329. break;
  330. };
  331. case WM_STUDY:{
  332. if( bActiveTH ){
  333. LOG("**********SWITCHING TO DETECTOR MODE**********");
  334. setWM( WM_DETECTOR );
  335. } else {
  336. if( bMeasuredTH != bDrawedTH ){
  337. LOG("**********SWITCHING TO BORDER MODE**********");
  338. setWM( WM_BORDER );
  339. };
  340. };
  341. break;
  342. };
  343. case WM_DETECTOR:{
  344. bActiveTH = m_nKernelFilter & m_pHardwareManager->getTHVisual(); //m_pHardwareManager->getDetectionStatus();
  345. if( bMeasuredTH != bDrawedTH ) {
  346. LOG("**********SWITCHING TO BORDER MODE**********");
  347. setWM( WM_BORDER );
  348. continue;
  349. };
  350. volatile targetRecord *tNode = (volatile targetRecord *)m_pHardwareManager->getEventMemory();
  351. if( tNode != NULL ) {
  352. bool bActiveRecordsPresent = true;
  353. while( bActiveRecordsPresent && ( m_bThreadStatus < TH_ENDED ) ) {
  354. targetRecord cNode;
  355. uint32_t cRange;
  356. uint32_t cTrackID = 0;
  357. EVENT_CONTAINER * tEvt = NULL;
  358. uint8_t bFilter = m_nKernelFilter & m_pHardwareManager->getTargetFilter();
  359. bActiveRecordsPresent = false;
  360. for( int idx = 0; idx < MAX_TARGETS_COUNT; idx++ ){
  361. // Filter Not Active Events
  362. if ( tNode[idx].flags.active == 0 ) continue;
  363. cNode.width = tNode[idx].width;
  364. cNode.begin_id = tNode[idx].begin_id;
  365. cNode.end_id = tNode[idx].end_id;
  366. cNode.status = tNode[idx].status;
  367. // Unlock record for PRU
  368. tNode[idx].status = 0;
  369. // Filter Events from Disabled Kernels -> goes to flip
  370. //if( ((1 << cNode.flags.lrf ) & m_nKernelFilter ) == 0 ){
  371. // // Abort checking this target since Kernel Disabled
  372. // continue;
  373. //};
  374. // Filter Events from Disabled Kernels -> goes to flip
  375. if( ((1 << cNode.flags.lrf ) & bFilter ) == 0 ){
  376. // Abort checking this target since Kernel not Detecting
  377. continue;
  378. };
  379. // Abort checking this target since no width
  380. if( cNode.flags.w_cnt ){
  381. cRange = cNode.width / cNode.flags.w_cnt;
  382. #ifdef SHIFT_50
  383. cRange += SHIFT_50;
  384. cNode.width = cRange * cNode.flags.w_cnt;
  385. #endif
  386. } else continue;
  387. bActiveRecordsPresent = true;
  388. switch( cNode.flags.type ){
  389. case TARGET_WHITE:{
  390. if( m_pShadow != NULL ) {
  391. if( m_pShadow->isActive() ) {
  392. uint16_t cPointX, cPointY;
  393. #ifdef PRU_SLAVE
  394. makeXY( cNode.begin_id, cPointX, cPointY, false );
  395. #else
  396. makeXY( cNode.begin_id, cPointX, cPointY, true );
  397. #endif
  398. if( m_pShadow->isOutside( cPointX, cPointY )){
  399. #ifdef PRU_SLAVE
  400. m_pTWIC->validateTarget( &cNode );
  401. #else
  402. cTrackID = m_pTracker->validateTarget( &cNode, true );
  403. #endif
  404. };
  405. } else { // Shadow is Disabled
  406. #ifdef PRU_SLAVE
  407. m_pTWIC->validateTarget( &cNode );
  408. #else
  409. cTrackID = m_pTracker->validateTarget( &cNode, true );
  410. #endif
  411. };
  412. } else {
  413. LOG_ERROR("NO SHADOW OBJECT!");
  414. m_bThreadStatus = TH_ENDED;
  415. continue;
  416. };
  417. break;
  418. };
  419. case TARGET_BLACK:{
  420. #ifdef NO_BLACK_TARGETS
  421. break;
  422. };
  423. #else
  424. if( m_pShadow != NULL ) {
  425. if( m_pShadow->isActive() ) {
  426. uint16_t cPointX, cPointY;
  427. #ifdef PRU_SLAVE
  428. makeXY( cNode.begin_id, cPointX, cPointY, false );
  429. #else
  430. makeXY( cNode.begin_id, cPointX, cPointY, true );
  431. #endif
  432. if( m_pShadow->isOutside( cPointX, cPointY )){
  433. #ifdef PRU_SLAVE
  434. m_pTWIC->validateTarget( &cNode );
  435. #else
  436. cTrackID = m_pTracker->validateTarget( &cNode, true );
  437. #endif
  438. };
  439. } else {
  440. #ifdef PRU_SLAVE
  441. m_pTWIC->validateTarget( &cNode );
  442. #else
  443. cTrackID = m_pTracker->validateTarget( &cNode, true );
  444. #endif
  445. };
  446. } else {
  447. LOG_ERROR("NO SHADOW OBJECT!");
  448. m_bThreadStatus = TH_ENDED;
  449. continue;
  450. };
  451. break;
  452. };
  453. #endif // NO_BLACK_TARGETS
  454. // nohup sleep 30 & echo $! > pidfile
  455. #ifndef IGNORE_PRU_SCAN
  456. #ifndef PRU_SLAVE
  457. case SCAN_COMPLETE: m_pTracker->ScanComplete(); break;
  458. case SCAN_FROZEN: m_pTracker->ScanComplete(); break;
  459. case MIRROR_FINISHED: m_pTracker->MirrorComplete(); break;
  460. #endif
  461. #endif //!IGNORE_PRU_SCAN
  462. #ifndef IGNORE_PRU_FLIP
  463. case FLIP_COMPLETE:{
  464. CLIENT_EVENT *tData;
  465. tData = (CLIENT_EVENT *) malloc(sizeof(CLIENT_EVENT));
  466. m_nFlipCounter = cNode.begin_id;
  467. if( tData!= NULL ) {
  468. long long tMS = getMilliseconds();
  469. tData->uts = tMS / 1000;
  470. tData->ums = tMS % 1000;
  471. tData->type = EVT_FLIP;
  472. tData->a_th_node = cNode.width;
  473. tData->th_id = m_nFlipCounter;
  474. tEvt = createBasicEvent( 0, EVT_CLASS_SENSOR_3D, EVT_FLIP, NULL, NULL, tData );
  475. } else {
  476. tEvt = createBasicEvent( 0, EVT_CLASS_SENSOR_3D, EVT_FLIP, NULL, NULL, NULL );
  477. };
  478. break;
  479. };
  480. #endif //!IGNORE_PRU_FLIP
  481. #ifndef IGNORE_ERROR_ANGLE
  482. case ERROR_ANGLE: tEvt = createBasicEvent( 0, EVT_CLASS_SENSOR_3D, EVT_ERROR, NULL, NULL, NULL ); break;
  483. #endif
  484. };
  485. if( tEvt != NULL) {
  486. m_pEventHeap->broadcast( tEvt );
  487. // Evt utilisation done by EventHeap thread
  488. tEvt = NULL;
  489. };
  490. };
  491. };
  492. } else {
  493. LOG_ERROR("No access to Event Memory");
  494. m_bThreadStatus = TH_ENDED;
  495. continue;
  496. };
  497. break;
  498. };
  499. case WM_BORDER:{
  500. if( ( bMeasuredTH == 0 ) && ( m_nForcedDrawTH == 0 ) ){
  501. LOG("No Measured No Force!");
  502. bDrawedTH = bMeasuredTH;
  503. if( bActiveTH ) {
  504. LOG("**********SWITCHING TO DETECTOR MODE**********");
  505. setWM( WM_DETECTOR );
  506. } else {
  507. LOG("**********SWITCHING TO STUDY MODE**********");
  508. setWM( WM_STUDY );
  509. };
  510. } else {
  511. if ( m_nForcedDrawTH ) {
  512. LOG("Forced DRAW");
  513. if( ( m_nForcedDrawTH & 0xFF00 ) != 0x00000 ) { // Force Remote TH Draw
  514. #ifndef PRU_SLAVE
  515. uint8_t chVal = ( m_nForcedDrawTH >> 8 ) & 0xFF;
  516. // Force Local TH Draw
  517. for ( uint8_t idx = 0; idx < 16; idx++ ) {
  518. if( ( chVal & (1<<idx) ) != 0 ) {
  519. if( m_pTWIC ){
  520. if( m_pTWIC->requestRemoteDraw( idx ) ){
  521. m_nForcedDrawTH &=~(1<<(idx+8)); // Clear Flag
  522. break; // from for
  523. };
  524. };
  525. };
  526. };
  527. #endif
  528. } else {
  529. // Force Local TH Draw
  530. for ( uint8_t idx = 0; idx < 16; idx++ ) {
  531. if( ( m_nForcedDrawTH & (1<<idx) ) != 0 ) {
  532. if ( m_pTWIC->readyToConsumeTH() ) {
  533. //Finished Build TH
  534. uint32_t THsize;
  535. thresholdNode *tPTR = getTreshold( THsize, idx );
  536. if ( m_pTWIC->consumeTH( idx, tPTR, THsize ) ) {
  537. m_nForcedDrawTH &=~(1<<idx); // Clear Flag
  538. break; // from for
  539. } else LOG("Error Consuming TH");
  540. } else LOG("TWINC not ready to consume TH");
  541. };
  542. };
  543. };
  544. } else {
  545. LOG("Measured!");
  546. if( bDrawedTH != bMeasuredTH ) {
  547. for( uint8_t idx = 0; idx < 8; idx++ ){
  548. if( ( ( bMeasuredTH & (1<<idx) ) != 0 ) && ( ( bDrawedTH & (1<<idx) ) == 0 ) ){
  549. sprintf( Buffer, "NEW [%d]TH READY!", idx );
  550. LOG( Buffer );
  551. if( m_pTWIC ){
  552. if ( m_pTWIC->readyToConsumeTH() ) {
  553. //Finished Build TH
  554. uint32_t THsize;
  555. thresholdNode *tPTR = getTreshold( THsize, idx );
  556. if ( m_pTWIC->consumeTH( idx, tPTR, THsize ) ) {
  557. //mark as consumed
  558. bDrawedTH |= (1<<idx);
  559. } else LOG("Error Consuming TH");
  560. } else {
  561. LOG("TWINC not ready to consume TH");
  562. SLEEP(10);
  563. break;
  564. }
  565. } else LOG("TWINC not exists!");
  566. } else {
  567. // If Th is cleared - remove old flag to consume newly build TH
  568. if( ( bMeasuredTH & (1<<idx) ) == 0 ) bDrawedTH &= ~(1<<idx);
  569. };
  570. };
  571. //bDrawedTH = bMeasuredTH;
  572. };
  573. };
  574. if( ( m_nForcedDrawTH == 0 ) && ( bDrawedTH == bMeasuredTH ) ) {
  575. if( bActiveTH ) {
  576. LOG("**********SWITCHING TO DETECTOR MODE**********");
  577. setWM( WM_DETECTOR );
  578. } else {
  579. LOG("**********SWITCHING TO STUDY MODE**********");
  580. setWM( WM_STUDY );
  581. };
  582. }
  583. };
  584. break;
  585. };
  586. case WM_ERROR:{
  587. if( m_pHardwareManager->validateCores() ){
  588. LOG("**********SWITCHING TO INIT MODE**********");
  589. setWM( WM_INIT );
  590. }
  591. break;
  592. };
  593. };
  594. };
  595. #ifdef ONPC_TOTTAL_SIMULATOR
  596. //Simulation Stuff
  597. //bActiveTH = 199;
  598. #endif
  599. SLEEP( SLEEP_KERNEL_INTERVAL );
  600. if(( m_lCycleCounter >> 31 )!= 0 ) {
  601. m_lCycleCounter = 0;
  602. } else m_lCycleCounter++;
  603. };
  604. };
  605. PLGS_3D_PARAMETERS LRF_Kernel::getDetectionParameters( void ){
  606. PLGS_3D_PARAMETERS tParam = NULL;
  607. if( m_pHardwareManager != NULL ){
  608. tParam = (PLGS_3D_PARAMETERS) malloc(sizeof(LGS_3D_PARAMETERS));
  609. if( tParam != NULL ){
  610. memset( tParam, 0 , sizeof(LGS_3D_PARAMETERS));
  611. /*TH_PARAMS tThParams;
  612. if( m_pHardwareManager->getDetectionParams( tThParams ) ){
  613. tParam->flags.BuildThByCore = tThParams.self_build_ena;
  614. tParam->bSST = m_nSST;
  615. tParam->nDelta = tThParams.delta;
  616. tParam->nFlip = tThParams.flip_size;
  617. tParam->nGPointSamples = tThParams.good_points;
  618. tParam->nOffset = tThParams.offset;
  619. tParam->nOrder = tThParams.order;
  620. tParam->nSun = 0;
  621. tParam->nWidth = m_nWidth;
  622. };
  623. //DT_STATUS tThStatus;
  624. m_pHardwareManager->getHardwareParams( tParam->flags );
  625. //if( m_pHardwareManager->getDetectionStatus( tThStatus ) ) {
  626. // tParam->flags.LaserPulsesOn = 1;
  627. //};
  628. */
  629. if( !m_pHardwareManager->getParams( *tParam ) ) {
  630. LOG_ERROR("getParams ERROR!");
  631. free( tParam );
  632. tParam = NULL;
  633. }
  634. #ifdef DEBUG
  635. else {
  636. char tBuf[123];
  637. for( int i = 0; i < sizeof(HW_PARAMS); i++ ) {
  638. sprintf( tBuf, "FLAGS_BIN[%d]: %d", i, tParam->flags_binary[i] );
  639. LOG( tBuf );
  640. };
  641. sprintf( tBuf, "Sunbit: %d", tParam->flags.Sunbit_filter );
  642. LOG( tBuf );
  643. sprintf( tBuf, "CAN My ID: %d", tParam->flags.TGZ_canSRC );
  644. LOG( tBuf );
  645. sprintf( tBuf, "CAN TGZ ID: %d", tParam->flags.TGZ_canDST );
  646. LOG( tBuf );
  647. }
  648. #endif
  649. ;
  650. };
  651. };
  652. return tParam;
  653. };
  654. bool LRF_Kernel::setDetectionParameters( PLGS_3D_PARAMETERS ptr ){
  655. bool isCompromised = false;
  656. HW_PARAMS tHwParams;
  657. TH_PARAMS tThParams;
  658. if( m_pHardwareManager != NULL ){
  659. if( m_pHardwareManager->getDetectionParams(tThParams)){
  660. if( tThParams.delta != ptr->nDelta ){
  661. tThParams.delta = ptr->nDelta; isCompromised = true;
  662. };
  663. if( tThParams.flip_size != ptr->nFlip ){
  664. tThParams.flip_size = ptr->nFlip; isCompromised = true;
  665. };
  666. if( tThParams.good_points != ptr->nGPointSamples ){
  667. tThParams.good_points = ptr->nGPointSamples; isCompromised = true;
  668. };
  669. if( tThParams.offset != ptr->nOffset ){
  670. tThParams.offset = ptr->nOffset; isCompromised = true;
  671. };
  672. if( tThParams.order != ptr->nOrder ){
  673. tThParams.order = ptr->nOrder; isCompromised = true;
  674. };
  675. if( tThParams.self_build_ena != ptr->flags.BuildThByCore ){
  676. tThParams.self_build_ena = ptr->flags.BuildThByCore; isCompromised = true;
  677. };
  678. if( tThParams.master != ptr->flags.isMaster ) {
  679. tThParams.master = ptr->flags.isMaster; isCompromised = true;
  680. };
  681. if( isCompromised ) m_pHardwareManager->setDetectionParams( &tThParams );
  682. } else {
  683. LOG_ERROR("Cannot Get Actual Detection Settings!");
  684. return false;
  685. };
  686. isCompromised = false;
  687. if( m_pHardwareManager->getHardwareParams( tHwParams ) ) {
  688. if( tHwParams.AutostartMotors != ptr->flags.AutostartMotors ){
  689. tHwParams.AutostartMotors = ptr->flags.AutostartMotors; isCompromised = true;
  690. };
  691. if( tHwParams.BaseScanCount != ptr->flags.BaseScanCount ){
  692. tHwParams.BaseScanCount = ptr->flags.BaseScanCount; isCompromised = true;
  693. };
  694. if( tHwParams.BBinterlace != ptr->flags.BBinterlace ){
  695. tHwParams.BBinterlace = ptr->flags.BBinterlace; isCompromised = true;
  696. };
  697. if( tHwParams.BuildThByCore != ptr->flags.BuildThByCore ){
  698. tHwParams.BuildThByCore = ptr->flags.BuildThByCore; isCompromised = true;
  699. };
  700. if( tHwParams.DetectorsCount != ptr->flags.DetectorsCount ){
  701. tHwParams.DetectorsCount = ptr->flags.DetectorsCount; isCompromised = true;
  702. };
  703. if( tHwParams.DryContactMode1 != ptr->flags.DryContactMode1 ){
  704. tHwParams.DryContactMode1 = ptr->flags.DryContactMode1; isCompromised = true;
  705. };
  706. if( tHwParams.DryContactMode2 != ptr->flags.DryContactMode2 ){
  707. tHwParams.DryContactMode2 = ptr->flags.DryContactMode2; isCompromised = true;
  708. };
  709. if( tHwParams.DryContactStatus1 != ptr->flags.DryContactStatus1 ){
  710. tHwParams.DryContactStatus1 = ptr->flags.DryContactStatus1; isCompromised = true;
  711. };
  712. if( tHwParams.DryContactStatus2 != ptr->flags.DryContactStatus2 ){
  713. tHwParams.DryContactStatus2 = ptr->flags.DryContactStatus2; isCompromised = true;
  714. };
  715. if( tHwParams.HorizontalFreq != ptr->flags.HorizontalFreq ){
  716. tHwParams.HorizontalFreq = ptr->flags.HorizontalFreq; isCompromised = true;
  717. };
  718. if( tHwParams.HorizontalMotorOn != ptr->flags.HorizontalMotorOn ){
  719. tHwParams.HorizontalMotorOn = ptr->flags.HorizontalMotorOn; isCompromised = true;
  720. };
  721. if( tHwParams.LaserPulsesOn != ptr->flags.LaserPulsesOn ){
  722. tHwParams.LaserPulsesOn = ptr->flags.LaserPulsesOn; isCompromised = true;
  723. };
  724. if( tHwParams.MirrorScanCount != ptr->flags.MirrorScanCount ){
  725. tHwParams.MirrorScanCount = ptr->flags.MirrorScanCount; isCompromised = true;
  726. };
  727. if( tHwParams.QQinterlace != ptr->flags.QQinterlace ){
  728. tHwParams.QQinterlace = ptr->flags.QQinterlace; isCompromised = true;
  729. };
  730. if( isCompromised ) m_pHardwareManager->setHardwareParams( &tHwParams );
  731. } else {
  732. LOG_ERROR("Cannot Get Actual Hardware Settings!");
  733. return false;
  734. };
  735. return true;
  736. };
  737. return false;
  738. };
  739. // KIID 0-7 Master Study 8-15 - Master Work - 16-23 - Slave Study 24-31 - Slave Work
  740. thresholdNode *LRF_Kernel::getTreshold( uint32_t &size, uint8_t kID ){
  741. thresholdNode * retVal = 0;
  742. //if ( kID < 16 ) { // Local TH
  743. #ifdef TH_PER_MIRROR
  744. kID = (kID>39)?39:kID;
  745. #else
  746. kID = (kID>15)?15:kID;
  747. #endif
  748. #ifdef ONPC_TOTTAL_SIMULATOR
  749. if( m_pCurrentTH == NULL ) {
  750. m_pCurrentTH = (thresholdNode *) malloc( TH_SIZE_BYTES );
  751. };
  752. if( m_pCurrentTH != NULL ) {
  753. char fName[64];
  754. sprintf( fName, "sim_data_%d.raw", kID );
  755. if( fileExist( fName ) ) {
  756. FILE *fd = fopen( fName, "rb" );
  757. if( fd != NULL ) {
  758. uint32_t Size = TH_SIZE_NODES;
  759. fread( &Size, sizeof( uint32_t ), 1, fd );
  760. fread( m_pCurrentTH, sizeof( thresholdNode ), Size, fd );
  761. fclose( fd );
  762. };
  763. } else {
  764. for (int i = 0; i < TH_SIZE_NODES; i++) {
  765. m_pCurrentTH[i].values.min = i;
  766. m_pCurrentTH[i].values.max = i;
  767. m_pCurrentTH[i].values.cnt = 1;
  768. m_pCurrentTH[i].values.sbit = 0;
  769. };
  770. }
  771. m_bCurrentTH = kID;
  772. size = TH_SIZE_NODES;
  773. return m_pCurrentTH;
  774. };
  775. #else
  776. if( m_pCurrentTH != NULL ) {
  777. if ( m_bCurrentTH == kID ) {
  778. size = TH_SIZE_NODES;
  779. return m_pCurrentTH;
  780. } else {
  781. if( m_pHardwareManager != NULL ) {
  782. thresholdNode *ptr = m_pHardwareManager->getWorkingMemory();
  783. if( ptr!= NULL ){
  784. memcpy( m_pCurrentTH, ptr+(kID*TH_SIZE_NODES), TH_SIZE_BYTES );
  785. m_bCurrentTH = kID;
  786. size = TH_SIZE_NODES;
  787. return m_pCurrentTH;
  788. } else {
  789. size = 0;
  790. return NULL;
  791. };
  792. };
  793. };
  794. } else {
  795. if( m_pHardwareManager != NULL ){
  796. thresholdNode *ptr = m_pHardwareManager->getWorkingMemory();
  797. if( ptr!= NULL ){
  798. m_pCurrentTH = (thresholdNode *) malloc( TH_SIZE_BYTES );
  799. if( m_pCurrentTH != NULL ) {
  800. memcpy( m_pCurrentTH, ptr+(kID*TH_SIZE_NODES), TH_SIZE_BYTES );
  801. m_bCurrentTH = kID;
  802. size = TH_SIZE_NODES;
  803. return m_pCurrentTH;
  804. };
  805. };
  806. };
  807. };
  808. #endif
  809. size = 0;
  810. return NULL;
  811. };
  812. bool LRF_Kernel::reset( bool FullRestart ){
  813. if( FullRestart ) {
  814. //Shutting down Motors and Hardware
  815. //Starting up Motors and Hardware
  816. shutdown( true );
  817. } else {
  818. m_pHardwareManager->reset( true );
  819. };
  820. return true;
  821. };
  822. bool LRF_Kernel::shutdown( bool Restart ){
  823. if( Restart ) {
  824. LOG("Restarting the Kernel by SHUTDOWN!");
  825. m_bExitCause = 1;
  826. } else {
  827. LOG("Shutting down the Kernel by SHUTDOWN!");
  828. m_bExitCause = 0;
  829. }
  830. setStatus( TH_ENDED );
  831. return true;
  832. };
  833. void LRF_Kernel::forceDrawTH( uint16_t kMASK ){
  834. m_nForcedDrawTH |= kMASK;
  835. };
  836. #ifndef PRU_SLAVE //Just Master Stuff
  837. networkClientHandler *LRF_Kernel::attachNewClient( int SocketID ){
  838. LOG("Creating Client Handler!");
  839. LRF_ClientHandler *tal;
  840. tal = new LRF_ClientHandler( SocketID, this );
  841. if( tal!= NULL ) {
  842. LOG("Client Handler Created!");
  843. };
  844. return (networkClientHandler *) tal;
  845. };
  846. bool LRF_Kernel::detachClient( networkClientHandler *ptr ){
  847. LOG("Delete Client Handler!");
  848. if( ptr == NULL ) return false;
  849. delete( (LRF_ClientHandler *) ptr );
  850. return true;
  851. };
  852. bool LRF_Kernel::stopMotors( void ){
  853. #ifndef ONPC_TOTTAL_SIMULATOR
  854. if( m_pHardwareManager!= NULL ) {
  855. return m_pHardwareManager->stopMotors( false );
  856. };
  857. return false;
  858. #else
  859. return true;
  860. #endif
  861. };
  862. bool LRF_Kernel::startMotors( void ){
  863. #ifndef ONPC_TOTTAL_SIMULATOR
  864. if( m_pHardwareManager!= NULL ) {
  865. return m_pHardwareManager->startMotors( false );
  866. };
  867. return false;
  868. #else
  869. return true;
  870. #endif
  871. };
  872. bool LRF_Kernel::getRuntimeStatus( ALG_RUN_STATUS *ptr ){
  873. if( ptr == NULL ) return false;
  874. memset( ptr, 0, sizeof( ALG_RUN_STATUS ));
  875. ptr->ENABLED_LRF = m_nKernelFilter;
  876. if( m_pHardwareManager ) {
  877. HWM_CSTATUS tHWS;
  878. if( m_pHardwareManager->getRunTimeStat( &tHWS ) ){
  879. ptr->HW_STATUS = tHWS.HW_STATUS;
  880. ptr->DETECTING_LRF = tHWS._DETECTING_LRF;
  881. ptr->ENABLED_LRF = m_nKernelFilter;
  882. ptr->MASTER_FLIP_CNT = tHWS._FLIP_CNT;
  883. memcpy( ptr->MASTER_MP, tHWS._MP, sizeof(uint32_t) * 8 );
  884. memcpy( ptr->MASTER_GP, tHWS._GP, sizeof(uint32_t) * 8 );
  885. memcpy( ptr->MASTER_WP, tHWS._WP, sizeof(uint32_t) * 8 );
  886. };
  887. } else return false;
  888. if( m_pTWIC ) {
  889. TWIN_STATUS * tPPP = m_pTWIC->getSystemStatus();
  890. if (tPPP->VERSION_IS_SYNCED ) ptr->TWINC_STATUS = 1;
  891. if (tPPP->SETTINGS_IS_SYNCED ) ptr->TWINC_STATUS |= 2;
  892. if (tPPP->SHADOW_IS_SYNCED ) ptr->TWINC_STATUS |= 4;
  893. } else return false;
  894. return true;
  895. };
  896. #endif
  897. #undef LOG
  898. #undef LOG_ERROR