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.

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