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.

963 lines
35 KiB

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