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.

753 lines
26 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
5 years ago
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
5 years ago
5 years ago
​
5 years ago
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
5 years ago
5 years ago
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
5 years ago
5 years ago
5 years ago
​
​
​
​
5 years ago
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
5 years ago
​
​
​
​
5 years ago
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
5 years ago
​
5 years ago
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. LRF_Kernel::LRF_Kernel( LRF_HardwareManager *HM ): basicThread(), basicEngine(){
  30. m_bExitCause = 0;
  31. m_pLog = basicLogCollection::getInstance();
  32. m_pEventHeap = basicEventHeap::getInstance();
  33. m_pSettingsProvider = basicSettingsProvider::getInstance();
  34. if( m_pSettingsProvider ) {
  35. m_pSettings = m_pSettingsProvider->getSubNode( NULL, "KERNEL" );
  36. if( m_pSettingsProvider->isCompromised() ) {
  37. m_pSettingsProvider->validate();
  38. };
  39. } else {
  40. m_pSettings = NULL;
  41. };
  42. m_pHardwareManager = HM;
  43. m_nKernelFilter = 0x000000FF;
  44. m_nFlipCounter = 0;
  45. m_nFlipSavierID = 0;
  46. m_pShadow = NULL;
  47. m_pTWIC = NULL;
  48. #ifndef PRU_SLAVE
  49. m_pTracker = NULL;
  50. m_pNetClientsServer = NULL;
  51. m_nSST = 0;
  52. #endif
  53. m_pCurrentTH = NULL;
  54. m_nCurrentTH = 0;
  55. m_bCurrentTH = 0;
  56. m_eWM = WM_NONE;
  57. m_nWidth = 800;
  58. };
  59. // Destructor implementation
  60. LRF_Kernel::~LRF_Kernel(){
  61. #ifdef DEBUG
  62. LOG("Destructor");
  63. LOG("End Main Thread");
  64. #endif
  65. WaitFinish( true );
  66. #ifdef DEBUG
  67. LOG("Deleting Components");
  68. LOG("TWINC");
  69. #endif
  70. if( m_pTWIC ) delete( m_pTWIC );
  71. #ifdef DEBUG
  72. LOG("Twin Connector - Done");
  73. LOG("SHADOW");
  74. #endif
  75. if( m_pShadow ) delete( m_pShadow );
  76. #ifdef DEBUG
  77. LOG("Shadow Mask - Done");
  78. #endif
  79. #ifdef DEBUG
  80. LOG("Destructor Finished");
  81. #endif
  82. };
  83. #ifdef PRU_SLAVE
  84. bool LRF_Kernel::create( LRF_HardwareManager *ptr ){
  85. #else
  86. bool LRF_Kernel::attachNetServer( networkServer *srvPort ){
  87. if( srvPort != NULL ) {
  88. m_pNetClientsServer = srvPort;
  89. };
  90. if( m_pNetClientsServer!= NULL ) {
  91. return srvPort->attachEngine( (basicEngine *)this );
  92. };
  93. return false;
  94. };
  95. bool LRF_Kernel::create( LRF_HardwareManager *ptr, networkServer *srvPort ){
  96. #endif
  97. if( m_pSettings != NULL ) {
  98. config_setting_t *pSetting;
  99. int tVal;
  100. char tBuffer[256];
  101. LOG("Getting Settings ...");
  102. #ifndef PRU_SLAVE
  103. if (!config_setting_lookup_int(m_pSettings, KNL_SETTINGS_DETECTOR_SST, &tVal )){
  104. tVal = m_nSST = 0;
  105. pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_DETECTOR_SST, CONFIG_TYPE_INT);
  106. config_setting_set_int(pSetting, tVal );
  107. m_pSettingsProvider->compromise();
  108. LOG("No Detector SST");
  109. } else {
  110. m_nSST = tVal;
  111. sprintf( tBuffer, "Detector SST Read From Settings: %d", tVal );
  112. LOG( tBuffer );
  113. };
  114. #endif
  115. if (!config_setting_lookup_int(m_pSettings, KNL_SETTINGS_DETECTOR_WIDTH, &tVal )){
  116. tVal = m_nWidth = 1000;
  117. pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_DETECTOR_WIDTH, CONFIG_TYPE_INT);
  118. config_setting_set_int(pSetting, tVal );
  119. m_pSettingsProvider->compromise();
  120. LOG("No Detector Width");
  121. } else {
  122. m_nWidth = tVal;
  123. sprintf( tBuffer, "Detector Width Read From Settings: %d", tVal );
  124. LOG( tBuffer );
  125. };
  126. if (!config_setting_lookup_int(m_pSettings, KNL_SETTINGS_FILTER_KERNELS, &tVal )){
  127. tVal = m_nKernelFilter = 0x000000ff;
  128. pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_FILTER_KERNELS, CONFIG_TYPE_INT);
  129. config_setting_set_int(pSetting, tVal );
  130. m_pSettingsProvider->compromise();
  131. LOG("No Fltered Kernels Defined - All LRF Will Work");
  132. } else {
  133. m_nKernelFilter = tVal;
  134. sprintf( tBuffer, "Filter Kernels Read From Settings: %d", tVal );
  135. LOG( tBuffer );
  136. };
  137. if( m_pSettingsProvider->isCompromised() ) {
  138. LOG("Save Settings");
  139. m_pSettingsProvider->validate();
  140. };
  141. } else {
  142. LOG_ERROR("Failed Read Config Settings!");
  143. return false;
  144. };
  145. m_eWM = WM_INIT;
  146. m_nFlipCounter = 0;
  147. m_nFlipSavierID = 0;
  148. m_pShadow = new ShadowMask();
  149. if( m_pShadow == NULL ) {
  150. LOG_ERROR("Failed to create Shadow Mask!");
  151. return false;
  152. };
  153. if( m_pShadow->isActive() ) {
  154. LOG("Shadow Mask is ACTIVE");
  155. } else {
  156. LOG("Shadow Mask is NOT ACTIVE");
  157. }
  158. if( m_pEventHeap == NULL ){
  159. LOG_ERROR("No Event Heap attached!");
  160. return false;
  161. };
  162. if( ptr != NULL ) {
  163. m_pHardwareManager = ptr;
  164. m_pHardwareManager->setKernelFilter( m_nKernelFilter );
  165. };
  166. if( m_pHardwareManager == NULL ) {
  167. LOG_ERROR("No Hardware manager attached!");
  168. return false;
  169. };
  170. #ifdef HIBERNATE_ENABLE
  171. if( fileExist("hibernate.dat")) {
  172. LOG("Loading Hibernated Data.");
  173. if(!m_pHardwareManager->loadHibernatedTH("hibernate.dat")){
  174. LOG_ERROR("Hibernate Data Load failure!");
  175. };
  176. };
  177. #endif
  178. m_lCycleCounter = 2;
  179. #ifndef PRU_SLAVE
  180. if( !attachNetServer( srvPort ) ) {
  181. LOG_ERROR("No Network manager attached!");
  182. return false;
  183. };
  184. #ifdef DEBUG
  185. LOG( "Target Tracker about to be created" );
  186. #endif
  187. m_pTracker = new LRF_TargetTracker( m_nSST, m_nWidth );
  188. if( m_pTracker == NULL ) {
  189. LOG_ERROR("Error in starting Target Tracker!");
  190. return false;
  191. };
  192. #ifdef DEBUG
  193. LOG( "Created" );
  194. #endif
  195. #endif // !PRU_SLAVE
  196. #ifdef DEBUG
  197. LOG( "Twin Connector about to be created" );
  198. #endif
  199. m_pTWIC = new LRF_TwinConnector( m_pSettings );
  200. if( m_pTWIC == NULL ) {
  201. LOG_ERROR("Error creating TWIN object!");
  202. return false;
  203. } else {
  204. #ifdef DEBUG
  205. LOG( "TWIN object - OK" );
  206. #endif
  207. if( m_pTWIC->create() ){
  208. #ifdef DEBUG
  209. LOG( "TWIN create - OK" );
  210. #endif
  211. m_pTWIC->setKernel( this );
  212. #ifndef PRU_SLAVE
  213. m_pTWIC->attachTargetTracker( m_pTracker );
  214. #endif
  215. } else {
  216. LOG_ERROR("Error in TWIN create!");
  217. return false;
  218. };
  219. };
  220. #ifdef DEBUG
  221. LOG( "TWIN connector Created" );
  222. #endif
  223. #ifdef DEBUG
  224. LOG( "Trhead about to be created" );
  225. #endif
  226. if( init() == false ){
  227. LOG_ERROR("Error in starting Kernel Thread!");
  228. return false;
  229. };
  230. #ifdef DEBUG
  231. LOG( "Created" );
  232. #endif
  233. m_eWM = WM_STUDY;
  234. return true;
  235. };
  236. void LRF_Kernel::WaitFinish( bool Force ){
  237. if( Force ){
  238. #ifdef DEBUG
  239. LOG( "Thread End Forced" );
  240. #endif
  241. } else {
  242. // 3 seconds sleep to settle down processes
  243. SLEEP(3000);
  244. };
  245. waitThreadEnd( Force );
  246. #ifdef DEBUG
  247. if( Force ) {
  248. LOG( "Thread forced to be Detached" );
  249. } else {
  250. LOG( "Thread is Detached by itself" );
  251. };
  252. #endif
  253. };
  254. LRF_WORK_MODE LRF_Kernel::getWM( void ){
  255. LRF_WORK_MODE rv;
  256. m_mWMProtector.lock();
  257. rv = m_eWM;
  258. m_mWMProtector.unlock();
  259. return rv;
  260. };
  261. void LRF_Kernel::setWM( LRF_WORK_MODE nMode ){
  262. m_mWMProtector.lock();
  263. m_eWM = nMode;
  264. m_mWMProtector.unlock();
  265. };
  266. /* Main Kernel Thread Procedure */
  267. void LRF_Kernel::m_fThreadProc( void ){
  268. char Buffer[256];
  269. DT_STATUS tStatus = {0};
  270. uint8_t bActiveTH = 0;
  271. uint8_t bOldTH = 0;
  272. m_bThreadStatus = TH_WORKING;
  273. while( m_bThreadStatus < TH_ENDED ){
  274. if( !m_pHardwareManager->validateCores() ){
  275. if( getWM() != WM_ERROR ){
  276. setWM(WM_ERROR);
  277. LOG_ERROR("PRU Core Trouble Detected!");
  278. };
  279. } else {
  280. switch( getWM() ) {
  281. case WM_INIT:{
  282. if( m_pHardwareManager->getCoreStatus( tStatus ) ){
  283. m_pHardwareManager->reset( false );
  284. if( tStatus.mode == WM_DETECTOR ) setWM( WM_STUDY );
  285. };
  286. break;
  287. };
  288. case WM_STUDY:{
  289. bActiveTH = m_nKernelFilter & m_pHardwareManager->getDetectionStatus();
  290. if( bActiveTH ) setWM( WM_DETECTOR );
  291. break;
  292. };
  293. case WM_DETECTOR:{
  294. bActiveTH = m_nKernelFilter & m_pHardwareManager->getDetectionStatus();
  295. if( bActiveTH != bOldTH ) {
  296. setWM( WM_BORDER );
  297. continue;
  298. };
  299. volatile targetRecord *tNode = (volatile targetRecord *)m_pHardwareManager->getEventMemory();
  300. if( tNode != NULL ) {
  301. bool bActiveRecordsPresent = true;
  302. while( bActiveRecordsPresent && ( m_bThreadStatus < TH_ENDED ) ) {
  303. targetRecord cNode;
  304. uint32_t cRange;
  305. uint32_t cTrackID = 0;
  306. EVENT_CONTAINER * tEvt = NULL;
  307. bActiveRecordsPresent = false;
  308. for( int idx = 0; idx < MAX_TARGETS_COUNT; idx++ ){
  309. // Filter Not Active Events
  310. if ( tNode[idx].flags.active == 0 ) continue;
  311. cNode.width = tNode[idx].width;
  312. cNode.begin_id = tNode[idx].begin_id;
  313. cNode.end_id = tNode[idx].end_id;
  314. cNode.status = tNode[idx].status;
  315. // Unlock record for PRU
  316. tNode[idx].status = 0;
  317. // Filter Events from Disabled Kernels -> goes to flip
  318. if( ((1 << cNode.flags.lrf ) & m_nKernelFilter ) == 0 ){
  319. // Abort checking this target since Kernel Disabled
  320. continue;
  321. };
  322. // Abort checking this target since no width
  323. if( cNode.flags.w_cnt ){
  324. cRange = cNode.width / cNode.flags.w_cnt;
  325. #ifdef SHIFT_50
  326. cRange += SHIFT_50;
  327. cNode.width = cRange * cNode.flags.w_cnt;
  328. #endif
  329. } else continue;
  330. bActiveRecordsPresent = true;
  331. switch( cNode.flags.type ){
  332. case TARGET_WHITE:{
  333. if( m_pShadow != NULL ) {
  334. if( m_pShadow->isActive() ) {
  335. uint16_t cPointX, cPointY;
  336. #ifdef PRU_SLAVE
  337. makeXY( cNode.begin_id, cPointX, cPointY, false );
  338. #else
  339. makeXY( cNode.begin_id, cPointX, cPointY, true );
  340. #endif
  341. if( m_pShadow->isOutside( cPointX, cPointY )){
  342. #ifdef PRU_SLAVE
  343. m_pTWIC->validateTarget( &cNode );
  344. #else
  345. cTrackID = m_pTracker->validateTarget( &cNode, true );
  346. #endif
  347. };
  348. } else { // Shadow is Disabled
  349. #ifdef PRU_SLAVE
  350. m_pTWIC->validateTarget( &cNode );
  351. #else
  352. cTrackID = m_pTracker->validateTarget( &cNode, true );
  353. #endif
  354. };
  355. } else {
  356. LOG_ERROR("NO SHADOW OBJECT!");
  357. m_bThreadStatus = TH_ENDED;
  358. continue;
  359. };
  360. break;
  361. };
  362. case TARGET_BLACK:{
  363. #ifdef NO_BLACK_TARGETS
  364. break;
  365. };
  366. #else
  367. if( m_pShadow != NULL ) {
  368. if( m_pShadow->isActive() ) {
  369. uint16_t cPointX, cPointY;
  370. #ifdef PRU_SLAVE
  371. makeXY( cNode.begin_id, cPointX, cPointY, false );
  372. #else
  373. makeXY( cNode.begin_id, cPointX, cPointY, true );
  374. #endif
  375. if( m_pShadow->isOutside( cPointX, cPointY )){
  376. #ifdef PRU_SLAVE
  377. m_pTWIC->validateTarget( &cNode );
  378. #else
  379. cTrackID = m_pTracker->validateTarget( &cNode, true );
  380. #endif
  381. };
  382. } else {
  383. #ifdef PRU_SLAVE
  384. m_pTWIC->validateTarget( &cNode );
  385. #else
  386. cTrackID = m_pTracker->validateTarget( &cNode, true );
  387. #endif
  388. };
  389. } else {
  390. LOG_ERROR("NO SHADOW OBJECT!");
  391. m_bThreadStatus = TH_ENDED;
  392. continue;
  393. };
  394. break;
  395. };
  396. #endif // NO_BLACK_TARGETS
  397. // nohup sleep 30 & echo $! > pidfile
  398. #ifndef IGNORE_PRU_SCAN
  399. #ifndef PRU_SLAVE
  400. case SCAN_COMPLETE: m_pTracker->ScanComplete(); break;
  401. case SCAN_FROZEN: m_pTracker->ScanComplete(); break;
  402. case MIRROR_FINISHED: m_pTracker->MirrorComplete(); break;
  403. #endif
  404. #endif //!IGNORE_PRU_SCAN
  405. #ifndef IGNORE_PRU_FLIP
  406. case FLIP_COMPLETE:{
  407. CLIENT_EVENT *tData;
  408. tData = (CLIENT_EVENT *) malloc(sizeof(CLIENT_EVENT));
  409. m_nFlipCounter = cNode.begin_id;
  410. if( tData!= NULL ) {
  411. long long tMS = getMilliseconds();
  412. tData->uts = tMS / 1000;
  413. tData->ums = tMS % 1000;
  414. tData->type = EVT_FLIP;
  415. tData->a_th_node = cNode.width;
  416. tData->th_id = m_nFlipCounter;
  417. tEvt = createBasicEvent( 0, EVT_CLASS_SENSOR_3D, EVT_FLIP, NULL, NULL, tData );
  418. } else {
  419. tEvt = createBasicEvent( 0, EVT_CLASS_SENSOR_3D, EVT_FLIP, NULL, NULL, NULL );
  420. };
  421. break;
  422. };
  423. #endif //!IGNORE_PRU_FLIP
  424. #ifndef IGNORE_ERROR_ANGLE
  425. case ERROR_ANGLE: tEvt = createBasicEvent( 0, EVT_CLASS_SENSOR_3D, EVT_ERROR, NULL, NULL, NULL ); break;
  426. #endif
  427. };
  428. if( tEvt != NULL) {
  429. m_pEventHeap->broadcast( tEvt );
  430. // Evt utilisation done by EventHeap thread
  431. tEvt = NULL;
  432. };
  433. };
  434. };
  435. } else {
  436. LOG_ERROR("No access to Event Memory");
  437. m_bThreadStatus = TH_ENDED;
  438. continue;
  439. };
  440. break;
  441. };
  442. case WM_BORDER:{
  443. if( bActiveTH == 0 ){
  444. setWM( WM_STUDY );
  445. } else {
  446. for ( uint8_t idx = 0; idx < 8; idx++ ) {
  447. if( ( ( bActiveTH & (1<<idx) ) != 0 ) && ( ( bOldTH & (1<<idx) ) == 0 ) ){
  448. if( m_pTWIC ){
  449. if ( m_pTWIC->readyToConsumeTH() ) {
  450. //Finished Build TH
  451. uint32_t THsize;
  452. thresholdNode *tPTR = getTreshold( THsize, idx );
  453. if ( m_pTWIC->consumeTH( idx, tPTR, THsize ) ) {
  454. //mark as consumed
  455. bOldTH |= (1<<idx);
  456. };
  457. };
  458. };
  459. } else {
  460. // If Th is cleared - remove old flag to consume newly build TH
  461. if( ( bActiveTH & (1<<idx) ) == 0 ) bOldTH &= ~(1<<idx);
  462. };
  463. };
  464. setWM( WM_DETECTOR );
  465. };
  466. //bOldTH = bActiveTH; // marking just after consumning-saving
  467. break;
  468. };
  469. case WM_ERROR:{
  470. if( m_pHardwareManager->validateCores() ) setWM( WM_INIT );
  471. break;
  472. };
  473. };
  474. };
  475. SLEEP( SLEEP_KERNEL_INTERVAL );
  476. if(( m_lCycleCounter >> 31 )!= 0 ) {
  477. m_lCycleCounter = 0;
  478. } else m_lCycleCounter++;
  479. };
  480. };
  481. PLGS_3D_PARAMETERS LRF_Kernel::getDetectionParameters( void ){
  482. PLGS_3D_PARAMETERS tParam = NULL;
  483. if( m_pHardwareManager != NULL ){
  484. tParam = (PLGS_3D_PARAMETERS) malloc(sizeof(LGS_3D_PARAMETERS));
  485. memset( tParam, 0 , sizeof(LGS_3D_PARAMETERS));
  486. if( tParam != NULL ){
  487. /*TH_PARAMS tThParams;
  488. if( m_pHardwareManager->getDetectionParams( tThParams ) ){
  489. tParam->flags.BuildThByCore = tThParams.self_build_ena;
  490. tParam->bSST = m_nSST;
  491. tParam->nDelta = tThParams.delta;
  492. tParam->nFlip = tThParams.flip_size;
  493. tParam->nGPointSamples = tThParams.good_points;
  494. tParam->nOffset = tThParams.offset;
  495. tParam->nOrder = tThParams.order;
  496. tParam->nSun = 0;
  497. tParam->nWidth = m_nWidth;
  498. };
  499. //DT_STATUS tThStatus;
  500. m_pHardwareManager->getHardwareParams( tParam->flags );
  501. //if( m_pHardwareManager->getDetectionStatus( tThStatus ) ) {
  502. // tParam->flags.LaserPulsesOn = 1;
  503. //};
  504. */
  505. if( !m_pHardwareManager->getParams( *tParam ) ) {
  506. LOG_ERROR("getParams ERROR!");
  507. free( tParam );
  508. tParam = NULL;
  509. }
  510. #ifdef DEBUG
  511. else {
  512. char tBuf[123];
  513. for( int i = 0; i < sizeof(HW_PARAMS); i++ ) {
  514. sprintf( tBuf, "FLAGS_BIN[%d]: %d", i, tParam->flags_binary[i] );
  515. LOG( tBuf );
  516. };
  517. sprintf( tBuf, "Sunbit: %d", tParam->flags.Sunbit_filter );
  518. LOG( tBuf );
  519. sprintf( tBuf, "CAN My ID: %d", tParam->flags.TGZ_canSRC );
  520. LOG( tBuf );
  521. sprintf( tBuf, "CAN TGZ ID: %d", tParam->flags.TGZ_canDST );
  522. LOG( tBuf );
  523. }
  524. #endif
  525. ;
  526. };
  527. };
  528. return tParam;
  529. };
  530. bool LRF_Kernel::setDetectionParameters( PLGS_3D_PARAMETERS ptr ){
  531. bool isCompromised = false;
  532. HW_PARAMS tHwParams;
  533. TH_PARAMS tThParams;
  534. if( m_pHardwareManager != NULL ){
  535. if( m_pHardwareManager->getDetectionParams(tThParams)){
  536. if( tThParams.delta != ptr->nDelta ){
  537. tThParams.delta = ptr->nDelta; isCompromised = true;
  538. };
  539. if( tThParams.flip_size != ptr->nFlip ){
  540. tThParams.flip_size = ptr->nFlip; isCompromised = true;
  541. };
  542. if( tThParams.good_points != ptr->nGPointSamples ){
  543. tThParams.good_points = ptr->nGPointSamples; isCompromised = true;
  544. };
  545. if( tThParams.offset != ptr->nOffset ){
  546. tThParams.offset = ptr->nOffset; isCompromised = true;
  547. };
  548. if( tThParams.order != ptr->nOrder ){
  549. tThParams.order = ptr->nOrder; isCompromised = true;
  550. };
  551. if( tThParams.self_build_ena != ptr->flags.BuildThByCore ){
  552. tThParams.self_build_ena = ptr->flags.BuildThByCore; isCompromised = true;
  553. };
  554. if( tThParams.master != ptr->flags.isMaster ) {
  555. tThParams.master = ptr->flags.isMaster; isCompromised = true;
  556. };
  557. if( isCompromised ) m_pHardwareManager->setDetectionParams( &tThParams );
  558. } else {
  559. LOG_ERROR("Cannot Get Actual Detection Settings!");
  560. return false;
  561. };
  562. isCompromised = false;
  563. if( m_pHardwareManager->getHardwareParams( tHwParams ) ) {
  564. if( tHwParams.AutostartMotors != ptr->flags.AutostartMotors ){
  565. tHwParams.AutostartMotors = ptr->flags.AutostartMotors; isCompromised = true;
  566. };
  567. if( tHwParams.BaseScanCount != ptr->flags.BaseScanCount ){
  568. tHwParams.BaseScanCount = ptr->flags.BaseScanCount; isCompromised = true;
  569. };
  570. if( tHwParams.BBinterlace != ptr->flags.BBinterlace ){
  571. tHwParams.BBinterlace = ptr->flags.BBinterlace; isCompromised = true;
  572. };
  573. if( tHwParams.BuildThByCore != ptr->flags.BuildThByCore ){
  574. tHwParams.BuildThByCore = ptr->flags.BuildThByCore; isCompromised = true;
  575. };
  576. if( tHwParams.DetectorsCount != ptr->flags.DetectorsCount ){
  577. tHwParams.DetectorsCount = ptr->flags.DetectorsCount; isCompromised = true;
  578. };
  579. if( tHwParams.DryContactMode1 != ptr->flags.DryContactMode1 ){
  580. tHwParams.DryContactMode1 = ptr->flags.DryContactMode1; isCompromised = true;
  581. };
  582. if( tHwParams.DryContactMode2 != ptr->flags.DryContactMode2 ){
  583. tHwParams.DryContactMode2 = ptr->flags.DryContactMode2; isCompromised = true;
  584. };
  585. if( tHwParams.DryContactStatus1 != ptr->flags.DryContactStatus1 ){
  586. tHwParams.DryContactStatus1 = ptr->flags.DryContactStatus1; isCompromised = true;
  587. };
  588. if( tHwParams.DryContactStatus2 != ptr->flags.DryContactStatus2 ){
  589. tHwParams.DryContactStatus2 = ptr->flags.DryContactStatus2; isCompromised = true;
  590. };
  591. if( tHwParams.HorizontalFreq != ptr->flags.HorizontalFreq ){
  592. tHwParams.HorizontalFreq = ptr->flags.HorizontalFreq; isCompromised = true;
  593. };
  594. if( tHwParams.HorizontalMotorOn != ptr->flags.HorizontalMotorOn ){
  595. tHwParams.HorizontalMotorOn = ptr->flags.HorizontalMotorOn; isCompromised = true;
  596. };
  597. if( tHwParams.LaserPulsesOn != ptr->flags.LaserPulsesOn ){
  598. tHwParams.LaserPulsesOn = ptr->flags.LaserPulsesOn; isCompromised = true;
  599. };
  600. if( tHwParams.MirrorScanCount != ptr->flags.MirrorScanCount ){
  601. tHwParams.MirrorScanCount = ptr->flags.MirrorScanCount; isCompromised = true;
  602. };
  603. if( tHwParams.QQinterlace != ptr->flags.QQinterlace ){
  604. tHwParams.QQinterlace = ptr->flags.QQinterlace; isCompromised = true;
  605. };
  606. if( isCompromised ) m_pHardwareManager->setHardwareParams( &tHwParams );
  607. } else {
  608. LOG_ERROR("Cannot Get Actual Hardware Settings!");
  609. return false;
  610. };
  611. return true;
  612. };
  613. return false;
  614. };
  615. // KIID 0-7 Master Study 8-15 - Master Work - 16-23 - Slave Study 24-31 - Slave Work
  616. thresholdNode *LRF_Kernel::getTreshold( uint32_t &size, uint8_t kID ){
  617. thresholdNode * retVal = 0;
  618. //if ( kID < 16 ) { // Local TH
  619. #ifdef TH_PER_MIRROR
  620. kID = (kID>39)?39:kID;
  621. #else
  622. kID = (kID>15)?15:kID;
  623. #endif
  624. if( m_pCurrentTH != NULL ) {
  625. if ( m_bCurrentTH == kID ) {
  626. size = TH_SIZE_NODES;
  627. return m_pCurrentTH;
  628. } else {
  629. if (m_pHardwareManager != NULL) {
  630. thresholdNode *ptr = m_pHardwareManager->getWorkingMemory();
  631. if( ptr!= NULL ){
  632. memcpy( m_pCurrentTH, ptr+(kID*TH_SIZE_NODES), TH_SIZE_BYTES );
  633. m_bCurrentTH = kID;
  634. size = TH_SIZE_NODES;
  635. return m_pCurrentTH;
  636. } else {
  637. size = 0;
  638. return NULL;
  639. };
  640. };
  641. };
  642. } else {
  643. if (m_pHardwareManager != NULL) {
  644. thresholdNode *ptr = m_pHardwareManager->getWorkingMemory();
  645. if( ptr!= NULL ){
  646. m_pCurrentTH = (thresholdNode *) malloc( TH_SIZE_BYTES );
  647. if( m_pCurrentTH != NULL ) {
  648. memcpy( m_pCurrentTH, ptr+(kID*TH_SIZE_NODES), TH_SIZE_BYTES );
  649. m_bCurrentTH = kID;
  650. size = TH_SIZE_NODES;
  651. return m_pCurrentTH;
  652. };
  653. };
  654. };
  655. };
  656. size = 0;
  657. return NULL;
  658. };
  659. bool LRF_Kernel::reset( bool FullRestart ){
  660. if( FullRestart ) {
  661. //Shutting down Motors and Hardware
  662. //Starting up Motors and Hardware
  663. shutdown( true );
  664. } else {
  665. m_pHardwareManager->reset( true );
  666. };
  667. return true;
  668. };
  669. bool LRF_Kernel::shutdown( bool Restart ){
  670. if( Restart ) {
  671. LOG("Restarting the Kernel by SHUTDOWN!");
  672. m_bExitCause = 1;
  673. } else {
  674. LOG("Shutting down the Kernel by SHUTDOWN!");
  675. m_bExitCause = 0;
  676. }
  677. setStatus( TH_ENDED );
  678. return true;
  679. };
  680. #ifndef PRU_SLAVE //Just Master Stuff
  681. networkClientHandler *LRF_Kernel::attachNewClient( int SocketID ){
  682. LOG("Creating Client Handler!");
  683. LRF_ClientHandler *tal;
  684. tal = new LRF_ClientHandler( SocketID, this );
  685. if( tal!= NULL ) {
  686. LOG("Client Handler Created!");
  687. };
  688. return (networkClientHandler *) tal;
  689. };
  690. bool LRF_Kernel::detachClient( networkClientHandler *ptr ){
  691. LOG("Delete Client Handler!");
  692. if( ptr == NULL ) return false;
  693. delete( (LRF_ClientHandler *) ptr );
  694. return true;
  695. };
  696. bool LRF_Kernel::stopMotors( void ){
  697. #ifndef ONPC_TOTTAL_SIMULATOR
  698. if( m_pHardwareManager!= NULL ) {
  699. return m_pHardwareManager->stopMotors( false );
  700. };
  701. return false;
  702. #else
  703. return true;
  704. #endif
  705. };
  706. bool LRF_Kernel::startMotors( void ){
  707. #ifndef ONPC_TOTTAL_SIMULATOR
  708. if( m_pHardwareManager!= NULL ) {
  709. return m_pHardwareManager->startMotors( false );
  710. };
  711. return false;
  712. #else
  713. return true;
  714. #endif
  715. };
  716. #endif
  717. #undef LOG
  718. #undef LOG_ERROR