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.

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