From feb095d6b3603f2c15d2a7c0e2f07510a07a509e Mon Sep 17 00:00:00 2001 From: Gagua on MyIR master Date: Thu, 12 Nov 2020 00:27:14 +0000 Subject: [PATCH] PNG of Threshold finished --- lib/App/3dSERVER/LRF_HardwareManager.cpp | 41 ++- lib/App/3dSERVER/LRF_HardwareManager.h | 12 + lib/App/3dSERVER/LRF_Kernel.cpp | 44 +-- lib/App/3dSERVER/LRF_TwinConnector.cpp | 378 +++++++++++--------- lib/App/3dSERVER/LRF_TwinConnector.h | 24 +- lib/data/protocol/protocolInternalLRF3D.cpp | 59 +-- lib/data/protocol/protocolInternalLRF3D.h | 73 ++-- lib/hardware/soc/general/3DLRF.h | 2 +- 8 files changed, 347 insertions(+), 286 deletions(-) diff --git a/lib/App/3dSERVER/LRF_HardwareManager.cpp b/lib/App/3dSERVER/LRF_HardwareManager.cpp index 547256d6..051a6f41 100644 --- a/lib/App/3dSERVER/LRF_HardwareManager.cpp +++ b/lib/App/3dSERVER/LRF_HardwareManager.cpp @@ -37,6 +37,11 @@ LRF_HardwareManager::LRF_HardwareManager( config_setting_t *SNode ){ m_nDetectionFilter = 0; m_eSOC = SOC_UNKNOWN; +#ifdef IGNORE_PRU_FLIP + m_bStudyCompleteFlag = 0; + m_bDataFilledTH = 0; +#endif + #ifdef THREADED_FLIP m_pFlipThreadHandler = NULL; m_bWaitFirstFlip = true; @@ -1729,8 +1734,9 @@ bool LRF_HardwareManager::validateCores( void ){ uint32_t mCoreReq; mCoreReq = m_pSOC->getThCoreRequest(); switch( mCoreReq ) { + #ifndef IGNORE_PRU_FLIP___ case CORE3_REQ_FLIP_TH:{ - #ifdef THREADED_FLIP + #ifdef THREADED_FLIP if( m_pFlipMutex != NULL ) { if( 0 == pthread_mutex_trylock( m_pFlipMutex ) ){ if( !m_bWaitFirstFlip ) { @@ -1739,49 +1745,50 @@ bool LRF_HardwareManager::validateCores( void ){ pthread_mutex_unlock( m_pFlipMutex ); }; } else m_bWaitFirstFlip = false; - #ifdef DEBUG + #ifdef DEBUG LOG("Build Working Threshold Memories MT Mode!"); - #endif + #endif if( 0 != pthread_create( m_pFlipThreadHandler, NULL, m_fFlipThreadFunction, ( void * ) this ) ) { LOG_ERROR("Thread Creation Faled."); pthread_mutex_unlock( m_pFlipMutex ); }; - #ifdef DEBUG + #ifdef DEBUG LOG("TH Flip Thread Started"); - #endif + #endif } else { - #ifdef DEBUG + #ifdef DEBUG LOG("TH Flip Request Overlapped!"); - #endif + #endif }; } - #else //!THREADED_FLIP - #ifdef DEBUG + #else //!THREADED_FLIP + #ifdef DEBUG LOG("Build Working Threshold Memories NT Mode!"); - #endif + #endif if( performFlip() ){ - #ifdef DEBUG + #ifdef DEBUG LOG("CORE REQ Done."); - #endif + #endif } else { LOG_ERROR("Building Detector's TH Error!"); }; - #endif//!THREADED_FLIP + #endif//!THREADED_FLIP break; }; case CORE3_REQ_CLEAR_TH:{ - #ifdef DEBUG + #ifdef DEBUG LOG("Clear Threshold Memories"); - #endif + #endif if( cleanTH(0) ){ - #ifdef DEBUG + #ifdef DEBUG //LOG("CORE REQ Done."); - #endif + #endif } else { LOG_ERROR("Cleanup Detector's TH Error!"); } break; }; + #endif //!IGNORE_PRU_FLIP default:{ LOG_ERROR("Unrecognized Core Request!"); }; diff --git a/lib/App/3dSERVER/LRF_HardwareManager.h b/lib/App/3dSERVER/LRF_HardwareManager.h index 7cba7f06..ecaacdb1 100644 --- a/lib/App/3dSERVER/LRF_HardwareManager.h +++ b/lib/App/3dSERVER/LRF_HardwareManager.h @@ -38,6 +38,9 @@ #include "../../data/protocol/protocolTGZ.h" #endif +#define IGNORE_PRU_FLIP +#define IGNORE_PRU_SCAN + #define AM355X_SIGN_ZONE (0x44E10000) #define AM355X_SIGN_VAL (0x00000000) @@ -117,6 +120,11 @@ class LRF_HardwareManager { TH_PARAMS m_sCP_Detector; HW_PARAMS m_sHW_Params; +#ifdef IGNORE_PRU_FLIP + uint8_t m_bStudyCompleteFlag; // Used to clean Primary (learning TH) + uint8_t m_bDataFilledTH; +#endif + #ifndef PRU_SLAVE MO_STATUS m_sCS_Motors; canBus *m_pCanBus; @@ -205,6 +213,10 @@ class LRF_HardwareManager { bool saveHibernatedTH( CSTR fname = "hibernate.dat" ); bool loadHibernatedTH( CSTR fname = "hibernate.dat" ); #endif + +#ifdef IGNORE_PRU_FLIP + uint8_t getFilledTH( void ){ return m_bDataFilledTH; }; +#endif }; #endif // __LRF_HARDWAREMANAGER_H_INCL__ diff --git a/lib/App/3dSERVER/LRF_Kernel.cpp b/lib/App/3dSERVER/LRF_Kernel.cpp index 7e531436..02161e45 100644 --- a/lib/App/3dSERVER/LRF_Kernel.cpp +++ b/lib/App/3dSERVER/LRF_Kernel.cpp @@ -584,46 +584,40 @@ void LRF_Kernel::m_fThreadProc( void ){ }; break; #endif // NO_BLACK_TARGETS -// nohup sleep 30 & echo $! > pidfile +// nohup sleep 30 & echo $! > pidfile +#ifndef IGNORE_PRU_SCAN case SCAN_COMPLETE: -#ifdef PRU_SLAVE -#else + #ifndef PRU_SLAVE m_pTracker->ScanComplete(); -#endif + #endif sprintf( Buffer,"Scan Complete [%d,%d]", cNode.width, cNode.begin_id ); -#ifdef DEBUG - //LOG( Buffer ); -#endif + #ifdef DEBUG + LOG( Buffer ); + #endif tEvt = createBasicEvent( 0, EVT_CLASS_SENSOR, EVT_SCAN, Buffer, NULL ); break; case SCAN_FROZEN: + #ifndef PRU_SLAVE + m_pTracker->ScanComplete(); + #endif sprintf( Buffer,"Scan was Frozen", cNode.width, cNode.begin_id ); -#ifdef DEBUG - //LOG( Buffer ); -#endif + #ifdef DEBUG + LOG( Buffer ); + #endif tEvt = createBasicEvent( 0, EVT_CLASS_SENSOR, EVT_SCAN, Buffer, NULL ); -#ifdef PRU_SLAVE - //m_pTWIC->sendEvent( EVT_SCAN ); -#else - //if( m_pTWIC->haveTargets() ){ - // targetRecord *pTarget = NULL; - // while( ( tTarget = m_pTWIC->fetchTarget() ) != NULL ) { - // cTrackID = m_pTracker->validateTarget( pTarget, false ); - // }; - //}; - - m_pTracker->ScanComplete(); - -#endif break; +#endif //!IGNORE_PRU_SCAN +#ifndef IGNORE_PRU_FLIP case FLIP_COMPLETE: m_nFlipCounter = cNode.begin_id; sprintf( Buffer,"Flip Complete [%d,%d]", cNode.width, m_nFlipCounter ); -#ifdef DEBUG + #ifdef DEBUG LOG( Buffer ); -#endif + #endif tEvt = createBasicEvent( 0, EVT_CLASS_SENSOR, EVT_FLIP, Buffer, NULL ); break; +#endif //!IGNORE_PRU_FLIP + #ifndef PRU_SLAVE case MIRROR_FINISHED: //LOG("Mirror"); diff --git a/lib/App/3dSERVER/LRF_TwinConnector.cpp b/lib/App/3dSERVER/LRF_TwinConnector.cpp index 618c033e..1f3fa4c3 100644 --- a/lib/App/3dSERVER/LRF_TwinConnector.cpp +++ b/lib/App/3dSERVER/LRF_TwinConnector.cpp @@ -2,12 +2,14 @@ #include "LRF_Kernel.h" #include "LRF_TargetTracker.h" -#define STB_IMAGE_IMPLEMENTATION - #include "../../data/stbi/stb_image.h" - #define STB_IMAGE_WRITE_IMPLEMENTATION - #include "../../data/stbi/stb_image_write.h" - #undef STB_IMAGE_WRITE_IMPLEMENTATION -#undef STB_IMAGE_IMPLEMENTATION +#ifdef TWI_TH_USE_PICTURES + #define STB_IMAGE_IMPLEMENTATION + #include "../../data/stbi/stb_image.h" + #define STB_IMAGE_WRITE_IMPLEMENTATION + #include "../../data/stbi/stb_image_write.h" + #undef STB_IMAGE_WRITE_IMPLEMENTATION + #undef STB_IMAGE_IMPLEMENTATION +#endif #define LOG( X ) m_pLog->Log( "TWINC", this, X ) #ifdef DEBUG @@ -20,6 +22,18 @@ #define LOG_ERROR( X ) m_pLog->Log( "TWINC", this, _LOG_ERROR, X ); #endif +#define SLOG( X ) Me->m_pLog->Log( "TWINC", Me, X ) +#ifdef DEBUG + #define SLOG_ERROR( X ) {\ + char _tBfrErrMSG234[256];\ + sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\ + Me->m_pLog->Log( "TWINC", Me, _LOG_ERROR, _tBfrErrMSG234 );\ + } +#else + #define SLOG_ERROR( X ) Me->m_pLog->Log( "TWINC", Me, _LOG_ERROR, X ); +#endif + + LRF_TwinConnector::LRF_TwinConnector( config_setting_t *Settings ): basicThread(){ m_pLog = basicLogCollection::getInstance(); m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, (CSTR)"TWIN_3DLRF_CONNECTION" ); @@ -41,6 +55,12 @@ LRF_TwinConnector::LRF_TwinConnector( config_setting_t *Settings ): basicThread( LRF_TwinConnector::~LRF_TwinConnector(){ if( m_pPort != NULL ) delete( m_pPort ); + if (m_bDrawThreadStatus > TH_ARMED ) { + waitDrawThreadEnd( true ); + // JOIN in Wait function!! + //pthread_detach(m_sThreadHandler); + //pthread_join(m_sThreadHandler, NULL); + }; #ifdef PRU_SLAVE if( m_pMasterAddress ) free( m_pMasterAddress ); #else @@ -170,13 +190,105 @@ bool LRF_TwinConnector::create(){ return init(); }; +void *LRF_TwinConnector::m_fDrawThreadEntryPoint( void *ptr ){ + LRF_TwinConnector *Me; + if( ptr == NULL ) return NULL; + Me = (LRF_TwinConnector*) ptr; +#ifdef DEBUG + SLOG("TH Thread OK"); +#endif + if( Me->m_bDrawThreadStatus == TH_ARMED ) { +#ifdef DEBUG + SLOG("TH Image Thread ARMED!"); +#endif + Me->m_bDrawThreadStatus = TH_WORKING; + if( Me->m_pEngine!= NULL ) { + // Store Local TH Draws + thresholdNode *tMem; + uint32_t tSize; +#ifdef DEBUG + SLOG("Kernel is Defined!"); +#endif + for( uint8_t i=0; i<16; i++ ) { + // just in case if we must end work + if( Me->m_bDrawThreadStatus != TH_WORKING ) break; + tMem = Me->m_pEngine->getTreshold( tSize, i ); + if( tMem!= NULL ) { + char tBuffer[256]; +#ifdef TWI_TH_USE_PICTURES + sprintf( tBuffer, "data/th_data_%d.png", i ); + #ifdef DEBUG + SLOG("SAVE TH Image ..."); + SLOG( tBuffer ); + #endif + if( !Me->saveTHPicture( tMem, tBuffer, false )){ + SLOG_ERROR("TH PNG save failure!"); + } + #ifdef PRU_SLAVE + else { + Me->m_mStatusProtector.lock(); + Me->m_nStatus |= ( 1 << i ) << 16; + Me->m_mStatusProtector.unlock(); + } + #endif + ; + #ifdef DEBUG + SLOG("SAVE TH Image DONE!"); + #endif + SLEEP(10); +#else //TWI_TH_USE_PICTURES + sprintf( tBuffer, "data/th_data_%d.raw", i ); + #ifdef DEBUG + SLOG("SAVE TH Data ..."); + SLOG( tBuffer ); + #endif + FILE *fd; + fd = fopen( tBuffer, "wb+"); + if( fd != NULL ){ + uint32_t Size = TH_SIZE_NODES; + fwrite( &Size, sizeof( uint32_t ), 1, fd ); + fwrite( tMem, sizeof( thresholdNode ), Size, fd ); + fclose( fd ); + #ifdef PRU_SLAVE + Me->m_mStatusProtector.lock(); + Me->m_nStatus |= ( 1 << i ) << 16; + Me->m_mStatusProtector.unlock(); + #endif + } else { + SLOG_ERROR("TH RAW save failure!"); + }; + #ifdef DEBUG + SLOG("SAVE TH Data DONE!"); + #endif +#endif //TWI_TH_USE_PICTURES + }; + }; + }; + }; + Me->m_bDrawThreadStatus = TH_EXIT; + Me->m_mDrawProtector.unlock(); + pthread_exit( NULL ); + return NULL; +}; + +void LRF_TwinConnector::waitDrawThreadEnd( bool forced ){ + if( m_bDrawThreadStatus > TH_WORKING || m_bDrawThreadStatus < TH_ARMED ) return; + if( forced ) m_bDrawThreadStatus = TH_ENDED; + while( m_bDrawThreadStatus != TH_EXIT ){ + SLEEP(DEFAULT_THREAD_SLEEP_TIME); + }; + pthread_join( m_sDrawThreadHandler, NULL ); +}; + #ifdef PRU_SLAVE bool LRF_TwinConnector::packetHandler( protocolPackage *Package ){ if( Package == NULL ) return false; TWIN_COMMAND cCMD = (TWIN_COMMAND) Package->m_pData[3]; switch( cCMD ) { case TWC_GET_STATUS:{ - sendStatus(); + m_mStatusProtector.lock(); + sendStatus( m_nStatus ); + m_mStatusProtector.unlock(); break; }; }; @@ -187,19 +299,46 @@ bool LRF_TwinConnector::create(){ int ClientSocket = -1; struct sockaddr ClientAddress; socklen_t ClientAddressSize; - - m_bThreadStatus = TH_WORKING; - + + long long cMilliseconds; + long long cStatusTO = 0; + long long cDrawTO = 0; + + m_bThreadStatus = TH_WORKING; while( m_bThreadStatus < TH_ENDED ){ - if( m_bCCMD == TWC_NO_COMMAND && tCMD == NULL ){ - // No CMD is EXEC so check new cmds - CMD_PROTEC.lock(); - if( m_qCMD.size() > 0 ){ - tCMD = m_qCMD.front(); - m_qCMD.pop(); - }; - CMD_PROTEC.unlock(); - }; + + cMilliseconds = getMilliseconds(); + + // TH Saving procedure + if( cDrawTO == 0 ) { + cDrawTO = cMilliseconds; + } else if ( ( cMilliseconds - cDrawTO ) > TWI_DRAW_LOOP_MS ){ +#ifdef DEBUG + LOG("Try to Dump TH Images"); +#endif + if( m_mDrawProtector.try_lock() ) { +#ifdef DEBUG + LOG("Mutex Grabed"); +#endif + if( m_bDrawThreadStatus > TH_ARMED ) { + pthread_join( m_sDrawThreadHandler, NULL ); + }; + cDrawTO = cMilliseconds; + m_bDrawThreadStatus = TH_ARMED; + int RetVal = pthread_create( &m_sDrawThreadHandler, NULL, m_fDrawThreadEntryPoint, ( void * ) this ); + if( RetVal == -1 ) { + LOG_ERROR("Image Draw Thread Creation Failed."); + m_mDrawProtector.unlock(); + }; + } +#ifdef DEBUG + else { + LOG("Mutex NOT Grabed"); + } +#endif + ; + }; + if( m_pPort != NULL ){ if( m_pPort->getConnectionStatus() == CS_DISCONNECTED ){ m_bWorkingMode = TWI_STAGE_WAITING_CONNECTION; @@ -213,34 +352,19 @@ bool LRF_TwinConnector::create(){ delete( m_pPort ); m_pPort = NULL; continue; - }; - if( tCMD!= NULL ){ - if( m_bCCMD == TWC_NO_COMMAND ){ - // CMD not issued yet! - switch( tCMD->CMD ){ - case TWC_PUSH_TARGET:{ - break; - }; - }; - } else { - // Waiting CMD exe status - switch( m_bCCMD ){ - case TWC_SET_THDRAW:{ - - break; - }; - }; - if( m_bCCMD == TWC_NO_COMMAND && tCMD != NULL ){ - // The command is finished - Will fetch next command from queue - if( tCMD->PTR == NULL ) free( tCMD->PTR ); - free( tCMD ); - tCMD = NULL; - }; - }; - SLEEP( 5 ); } else { - SLEEP( 100 ); - }; + // Status Send procedure + if( cStatusTO == 0 ) { + cStatusTO = cMilliseconds; + } else if( ( cMilliseconds - cStatusTO ) > TWI_STATUS_LOOP_MS ){ + m_mStatusProtector.lock(); + if( !sendStatus( m_nStatus ) ){ + LOG_ERROR("Status Sending failure!"); + m_pPort->disconnect(); + }; + m_mStatusProtector.unlock(); + }; + }; } else { LOG("(re)Establish Connection"); m_pPort = new clientPort( m_pMasterAddress, m_nMasterPort, true ); @@ -283,7 +407,7 @@ bool LRF_TwinConnector::create(){ long long cMilliseconds; long long cStatusTO = 0; long long cDrawTO = 0; - + m_bThreadStatus = TH_WORKING; while( m_bThreadStatus < TH_ENDED ){ @@ -294,24 +418,30 @@ bool LRF_TwinConnector::create(){ if( cDrawTO == 0 ) { cDrawTO = cMilliseconds; } else if ( ( cMilliseconds - cDrawTO ) > TWI_DRAW_LOOP_MS ){ - cDrawTO = cMilliseconds; - if( m_pEngine!= NULL ) { - // Store Local TH Draws - thresholdNode *tMem; - uint32_t tSize; - for( uint8_t i=0; i<16; i++ ) { - // just in case if we must end work - if( m_bThreadStatus != TH_WORKING ) break; - tMem = m_pEngine->getTreshold( tSize, i ); - if( tMem!= NULL ) { - char tBuffer[256]; - sprintf( tBuffer, "data/th_data_%d.png", i ); - if( !saveTHPicture( tMem, tBuffer, false )){ - LOG_ERROR("TH PNG save failure!"); - }; - }; +#ifdef DEBUG + LOG("Try to Dump TH Images"); +#endif + if( m_mDrawProtector.try_lock() ) { +#ifdef DEBUG + LOG("Mutex Grabed"); +#endif + if( m_bDrawThreadStatus > TH_ARMED ) { + pthread_join( m_sDrawThreadHandler, NULL ); }; - }; + cDrawTO = cMilliseconds; + m_bDrawThreadStatus = TH_ARMED; + int RetVal = pthread_create( &m_sDrawThreadHandler, NULL, m_fDrawThreadEntryPoint, ( void * ) this ); + if( RetVal == -1 ) { + LOG_ERROR("Image Draw Thread Creation Failed."); + m_mDrawProtector.unlock(); + }; + } +#ifdef DEBUG + else { + LOG("Mutex NOT Grabed"); + } +#endif + ; }; // Port exists - SLAVE is connected if( m_pPort != NULL ){ @@ -321,94 +451,11 @@ bool LRF_TwinConnector::create(){ m_bWorkingMode = TWI_STAGE_WAITING_CONNECTION; delete( m_pPort ); m_pPort = NULL; + //Reset Flags + cStatusTO = 0; continue; }; - if( m_bCCMD == TWC_NO_COMMAND ){ - // No Current Request Issued - switch( m_bWorkingMode ) { - case TWI_STAGE_CONNECTED:{ - if( requestSoftwareVersion() ){ - m_bCCMD = TWC_ACK_VERSION; - }; - break; - }; - case TWI_STAGE_INIT_1:{ - if( requestCurrentSettings() ){ - m_bCCMD = TWC_ACK_CONFIG; - }; - break; - }; - case TWI_STAGE_INIT_2:{ - if( requestShadow() ){ - m_bCCMD = TWC_ACK_SHADOW; - }; - break; - }; - case TWI_STAGE_MAIN_LOOP:{ - if( ( cMilliseconds - cStatusTO ) > TWI_STATUS_LOOP_MS ) { - cStatusTO = cMilliseconds; - requestStatus(); - m_bCCMD = TWC_ACK_STATUS; - LOG("Req Status"); - } else { - SLEEP( TWI_SLOW_LOOP_SLEEP ); - } - break; - }; - }; - } else { - // Waiting CMD ACK status - if( getLastPackageType() == m_bCCMD ) { - switch( m_bCCMD ){ - case TWC_ACK_VERSION:{ - m_bWorkingMode = TWI_STAGE_INIT_1; - break; - }; - case TWC_ACK_CONFIG:{ - m_bWorkingMode = TWI_STAGE_INIT_2; - break; - }; - case TWC_ACK_SHADOW:{ - m_bWorkingMode = TWI_STAGE_MAIN_LOOP; - break; - }; - case TWC_ACK_STATUS:{ -#ifdef DEBUG - LOG("Registered Ack Status"); -#endif - //uint32_t nStatus = getStatus(); - if( ( m_nStatus & TWI_STATUS_TARGET_MASK ) > 0 ){ -#ifdef DEBUG - LOG("Have new Targets!"); -#endif - //m_pProtocol->setTData( m_pTargets ); - requestTargets(); - m_bCCMD = TWC_GET_TARGETS; - continue; - }; - break; - }; - case TWC_GET_TARGETS:{ - if( getLastPackageType() == TWC_TARGETS_DATA ){ - m_bCCMD = TWC_NO_COMMAND; -#ifdef DEBUG - LOG("Registered Ack Targets"); -#endif - if( m_pTracker != NULL ) { - uint8_t nCNT = (( m_nStatus & TWI_STATUS_TARGET_MASK ) >> 8 ); - for( uint8_t index = 0; index < nCNT; index++ ){ - m_pTracker->validateTarget( &m_pTargets[index], false ); - }; - - }; - }; - break; - }; - }; - m_bCCMD = TWC_NO_COMMAND; - }; - SLEEP( TWI_QUICK_LOOP_SLEEP ); - }; + SLEEP( TWI_SLOW_LOOP_SLEEP ); } else { // (re)Establish Connection ClientAddressSize = (socklen_t) sizeof( struct sockaddr_in ); @@ -416,7 +463,7 @@ bool LRF_TwinConnector::create(){ if ( ClientSocket == -1 ) { switch( errno ){ case EAGAIN:{ - SLEEP(10); + SLEEP( TWI_QUICK_LOOP_SLEEP ); continue; }; //case EWOULDBLOCK: me->Log(" The socket is marked non-blocking and no connections are present to be accepted."); break; @@ -526,7 +573,8 @@ bool LRF_TwinConnector::saveTHData( uint8_t kid, thresholdNode *ptr ){ return true; }; -bool LRF_TwinConnector::saveTHPicture( thresholdNode *ptr, CSTR fileName, bool monochrome ){ +#ifdef TWI_TH_USE_PICTURES +bool LRF_TwinConnector::saveTHPicture( thresholdNode *ptr, CSTR fileName, bool monochrome, bool agressive ){ uint8_t *tMemory = NULL; thresholdNode *cNode = NULL; uint8_t *cPixel = NULL; @@ -538,21 +586,23 @@ bool LRF_TwinConnector::saveTHPicture( thresholdNode *ptr, CSTR fileName, bool m }; tMemory = (uint8_t *) malloc( TH_SIZE_NODES * cSpace ); if( tMemory == NULL ){ - + LOG_ERROR("Image Memory Alloc Eror!"); return false; }; - -#ifndef PRU_SLAVE for( int j = 0; j < Q_SCAN_COUNT; j++ ) { for ( int i = 0; i < TH_LINE_NODES; i++ ) { - cNode = ptr+(j*i); - //cNode = ptr+( TH_LINE_NODES * j + i ); + //cNode = ptr+(j*i); + //cNode = &ptr[ ( TH_LINE_NODES * j ) + i ]; +#ifdef PRU_SLAVE + cNode = ptr+makeIds( i, j , false ); + cPixel = tMemory+( cSpace * makeIds( i, j , false )); #else - for( int j = (Q_SCAN_COUNT-1); j < -1; j++ ) { - for ( int i = 0; i < TH_LINE_NODES; i++ ) { - cNode = ptr+( TH_LINE_NODES * j + i ); -#endif - cPixel = tMemory+(cSpace*i*j); + cNode = ptr+makeIds( i, j , true ); + cPixel = tMemory+( cSpace * makeIds( i, j , false )); + //cNode = ptr + ( i * j ); + //cPixel = tMemory + ( cSpace * i * j ); +#endif + if( !agressive ) SLEEP(1); if( monochrome ) { #ifdef CHECK_SUN_BIT if( cNode->values.sbit ) { @@ -613,7 +663,11 @@ bool LRF_TwinConnector::saveTHPicture( thresholdNode *ptr, CSTR fileName, bool m if( RetVal == 0 ) return false; return true; }; +#endif //TWI_TH_USE_PICTURES + +#undef SLOG +#undef SLOG_ERROR #undef LOG #undef LOG_ERROR diff --git a/lib/App/3dSERVER/LRF_TwinConnector.h b/lib/App/3dSERVER/LRF_TwinConnector.h index a6987d84..152a380a 100644 --- a/lib/App/3dSERVER/LRF_TwinConnector.h +++ b/lib/App/3dSERVER/LRF_TwinConnector.h @@ -9,7 +9,9 @@ #include "../../data/net/serverPort.h" #endif -#define TWI_QUICK_LOOP_SLEEP 5 +//#define TWI_TH_USE_PICTURES + +#define TWI_QUICK_LOOP_SLEEP 15 #define TWI_SLOW_LOOP_SLEEP 100 #define TWI_STATUS_LOOP_MS 500 #define TWI_RECONNECT_LOOP_SLEEP 1000 @@ -54,6 +56,15 @@ class LRF_TargetTracker; uint32_t m_nStatus; basicMutex m_mStatusProtector; + bool m_bDrawByRAW; + bool m_bDrawMonochrome; + basicMutex m_mDrawProtector; + + pthread_t m_sDrawThreadHandler; + thredStatus m_bDrawThreadStatus; + static void * m_fDrawThreadEntryPoint(void *); + void waitDrawThreadEnd( bool forced = false ); + #ifdef PRU_SLAVE clientPort * m_pPort; TWIN_COMMAND_QUEUE m_qCMD; @@ -65,11 +76,16 @@ class LRF_TargetTracker; LRF_TargetTracker * m_pTracker; uint16_t m_nBindPort; CSTR m_pBindAddress; - //targetRecord m_pTargets[TWI_TARGET_QUEUE_SIZE]; + // Handshaking flags + bool m_bSettingsIsLoadded; + bool m_bVersionIsLoadded; + bool m_bShadowIsLoadded; +#endif +#ifdef TWI_TH_USE_PICTURES + bool saveTHPicture( thresholdNode *ptr, CSTR fileName, bool monochrome = false, bool agressive = false ); #endif - bool saveTHPicture( thresholdNode *ptr, CSTR fileName, bool monochrome ); protected: - void m_fThreadProc( void ); // Connection Handling Thread + void m_fThreadProc( void ); // Connection/THDraw Handling Thread virtual bool packetHandler( protocolPackage *Package ); public: explicit LRF_TwinConnector( config_setting_t *Settings = NULL ); diff --git a/lib/data/protocol/protocolInternalLRF3D.cpp b/lib/data/protocol/protocolInternalLRF3D.cpp index 13fe5474..778e2bc5 100644 --- a/lib/data/protocol/protocolInternalLRF3D.cpp +++ b/lib/data/protocol/protocolInternalLRF3D.cpp @@ -148,58 +148,19 @@ protocolPackage *protocolInternalLRF3D::packetExtractor( U8 *Buffer, size_t Buff protocolInternalLRF3D_SLAVE::protocolInternalLRF3D_SLAVE() : protocolInternalLRF3D() { m_pLog = basicLogCollection::getInstance(); - m_nTargetCount = 0; - memset( m_pTargets, 0, sizeof(targetRecord) * TWI_TARGET_QUEUE_SIZE ); - for( uint8_t i = 0; i < 16; i++ ) { - m_pThresholds[ i ] = (thresholdNode *) malloc( TH_SIZE_BYTES ); - if( m_pThresholds[ i ] != NULL ) { - memset( m_pThresholds[ i ], 0, TH_SIZE_BYTES ); - }; - }; }; protocolInternalLRF3D_SLAVE::~protocolInternalLRF3D_SLAVE(){ - for( uint8_t i = 0; i < 16; i++ ) { - if( m_pThresholds[ i ] != NULL ) free( m_pThresholds[ i ] ); - }; }; bool protocolInternalLRF3D_SLAVE::create( void ){ return protocolInternalLRF3D::create(); }; - bool protocolInternalLRF3D_SLAVE::setTrasholdData( uint8_t lrf_id, uint32_t *ptr ){ - if( ptr == NULL ) return false; - if( m_pThresholds[ lrf_id ] == NULL ) return false; - m_mData.lock(); - memcpy( m_pThresholds[ lrf_id ], ptr, TH_SIZE_BYTES ); - m_nStatus |= (( 1 << lrf_id ) << 8 ); - m_mData.unlock(); - return false; - }; - - bool protocolInternalLRF3D_SLAVE::enqueueTarget( targetRecord *ptr ){ - if( ptr == NULL ) return false; - m_mTargetProtector.lock(); - if( m_nTargetCount < TWI_TARGET_QUEUE_SIZE ) { - memcpy( &m_pTargets[m_nTargetCount], ptr, sizeof(targetRecord) ); - m_nTargetCount++; - m_nStatus = ( m_nStatus & 0x00fffffe ) | ( m_nTargetCount << 24 ); - } else { - memcpy( &m_pTargets[0], &m_pTargets[1], (TWI_TARGET_QUEUE_SIZE-1)*sizeof(targetRecord) ); - memcpy( &m_pTargets[TWI_TARGET_QUEUE_SIZE-1], ptr, sizeof(targetRecord) ); - m_nStatus |= 1; //Target Overflow flag - }; - m_mTargetProtector.unlock(); - return true; - }; - - bool protocolInternalLRF3D_SLAVE::sendTrasholdData( uint8_t lrf_id, uint32_t Idx ){ - if( m_pThresholds[ lrf_id ] == NULL ) return false; - uint32_t c_mask = (( 1 << lrf_id ) << 8 ); - if( ( m_nStatus & c_mask ) == 0 ) return false; + bool protocolInternalLRF3D_SLAVE::sendTrasholdData( thresholdNode *data, uint32_t dataIdx ){ + if( data == NULL ) return false; uint8_t Buffer[PROTOCOL_PARSER_BUFFER_SIZE]; - uint32_t node_index = Idx; + uint32_t node_index = dataIdx; uint32_t index = 8; Buffer[3] = TWC_TH_DATA; Buffer[4] = ( node_index >> 24 ) & 0xFF; @@ -207,10 +168,10 @@ protocolPackage *protocolInternalLRF3D::packetExtractor( U8 *Buffer, size_t Buff Buffer[6] = ( node_index >> 8 ) & 0xFF; Buffer[7] = ( node_index & 0xFF ); while ( ( index < ( TWI_MAX_PACKAGE_SIZE_BYTES - 5 ) ) && ( node_index < TH_SIZE_NODES ) ){ - Buffer[index++] = (m_pThresholds[ lrf_id ][ node_index ].data >> 24 ) & 0xFF;//0xFE; // to clear Sunbit data - Buffer[index++] = (m_pThresholds[ lrf_id ][ node_index ].data >> 16 ) & 0xFF; - Buffer[index++] = (m_pThresholds[ lrf_id ][ node_index ].data >> 8 ) & 0xFF; - Buffer[index++] = m_pThresholds[ lrf_id ][ node_index ].data & 0xFF; + Buffer[index++] = (data[ node_index ].data >> 24 ) & 0xFF;//0xFE; // to clear Sunbit data + Buffer[index++] = (data[ node_index ].data >> 16 ) & 0xFF; + Buffer[index++] = (data[ node_index ].data >> 8 ) & 0xFF; + Buffer[index++] = data[ node_index ].data & 0xFF; node_index++; }; #ifdef DEBUG @@ -218,7 +179,7 @@ protocolPackage *protocolInternalLRF3D::packetExtractor( U8 *Buffer, size_t Buff #endif return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, index ) ); }; - +/* bool protocolInternalLRF3D_SLAVE::sendTargets( void ){ uint8_t Buffer[PROTOCOL_PARSER_BUFFER_SIZE]; if( m_nTargetCount > 0 ) { @@ -263,8 +224,8 @@ protocolPackage *protocolInternalLRF3D::packetExtractor( U8 *Buffer, size_t Buff return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, 5 ) ); }; }; - - bool protocolInternalLRF3D_SLAVE::sendStatus( void ){ +*/ + bool protocolInternalLRF3D_SLAVE::sendStatus( uint32_t m_nStatus ){ if( m_oDataTransport == NULL ) return false; uint8_t Buffer[16]; Buffer[3] = TWC_ACK_STATUS; diff --git a/lib/data/protocol/protocolInternalLRF3D.h b/lib/data/protocol/protocolInternalLRF3D.h index 5ca1b260..422f6810 100644 --- a/lib/data/protocol/protocolInternalLRF3D.h +++ b/lib/data/protocol/protocolInternalLRF3D.h @@ -48,26 +48,44 @@ typedef struct S_TWIN_CMD_RECORD{ typedef std::queue TWIN_COMMAND_QUEUE; -#define TWI_STATUS_ERROR_MASK 0x000000ff -#define TWI_STATUS_TARGET_MASK 0x0000ff00 -#define TWI_STATUS_KERNEL_MASK 0xffff0000 +#define TWI_STATUS_ERROR_MASK 0x000000ff +#define TWI_STATUS_TARGET_MASK 0x0000ff00 +#define TWI_STATUS_KERNEL_MASK 0xffff0000 +#define TWI_STATUS_MASTER_KERNEL_MASK 0x00ff0000 +#define TWI_STATUS_SLAVE_KERNEL_MASK 0xff000000 -#define TWI_STATUS_KERNEL_00_RDY 0x00010000 -#define TWI_STATUS_KERNEL_01_RDY 0x00020000 -#define TWI_STATUS_KERNEL_02_RDY 0x00040000 -#define TWI_STATUS_KERNEL_03_RDY 0x00080000 -#define TWI_STATUS_KERNEL_04_RDY 0x00100000 -#define TWI_STATUS_KERNEL_05_RDY 0x00200000 -#define TWI_STATUS_KERNEL_06_RDY 0x00400000 -#define TWI_STATUS_KERNEL_07_RDY 0x00800000 -#define TWI_STATUS_KERNEL_08_RDY 0x01000000 -#define TWI_STATUS_KERNEL_09_RDY 0x02000000 -#define TWI_STATUS_KERNEL_10_RDY 0x04000000 -#define TWI_STATUS_KERNEL_11_RDY 0x08000000 -#define TWI_STATUS_KERNEL_12_RDY 0x10000000 -#define TWI_STATUS_KERNEL_13_RDY 0x20000000 -#define TWI_STATUS_KERNEL_14_RDY 0x40000000 -#define TWI_STATUS_KERNEL_15_RDY 0x80000000 +#define TWI_STATUS_KERNEL_00_RDY 0x00000001 +#define TWI_STATUS_KERNEL_01_RDY 0x00000002 +#define TWI_STATUS_KERNEL_02_RDY 0x00000004 +#define TWI_STATUS_KERNEL_03_RDY 0x00000008 +#define TWI_STATUS_KERNEL_04_RDY 0x00000010 +#define TWI_STATUS_KERNEL_05_RDY 0x00000020 +#define TWI_STATUS_KERNEL_06_RDY 0x00000040 +#define TWI_STATUS_KERNEL_07_RDY 0x00000080 +#define TWI_STATUS_KERNEL_08_RDY 0x00000100 +#define TWI_STATUS_KERNEL_09_RDY 0x00000200 +#define TWI_STATUS_KERNEL_10_RDY 0x00000400 +#define TWI_STATUS_KERNEL_11_RDY 0x00000800 +#define TWI_STATUS_KERNEL_12_RDY 0x00001000 +#define TWI_STATUS_KERNEL_13_RDY 0x00002000 +#define TWI_STATUS_KERNEL_14_RDY 0x00004000 +#define TWI_STATUS_KERNEL_15_RDY 0x00008000 +#define TWI_STATUS_KERNEL_16_RDY 0x00010000 +#define TWI_STATUS_KERNEL_17_RDY 0x00020000 +#define TWI_STATUS_KERNEL_18_RDY 0x00040000 +#define TWI_STATUS_KERNEL_19_RDY 0x00080000 +#define TWI_STATUS_KERNEL_20_RDY 0x00100000 +#define TWI_STATUS_KERNEL_21_RDY 0x00200000 +#define TWI_STATUS_KERNEL_22_RDY 0x00400000 +#define TWI_STATUS_KERNEL_23_RDY 0x00800000 +#define TWI_STATUS_KERNEL_24_RDY 0x01000000 +#define TWI_STATUS_KERNEL_25_RDY 0x02000000 +#define TWI_STATUS_KERNEL_26_RDY 0x04000000 +#define TWI_STATUS_KERNEL_27_RDY 0x08000000 +#define TWI_STATUS_KERNEL_28_RDY 0x10000000 +#define TWI_STATUS_KERNEL_29_RDY 0x20000000 +#define TWI_STATUS_KERNEL_30_RDY 0x40000000 +#define TWI_STATUS_KERNEL_31_RDY 0x80000000 class protocolInternalLRF3D: public protocolParser { private: @@ -93,18 +111,17 @@ class protocolInternalLRF3D: public protocolParser { private: basicLogCollection *m_pLog; protected: - thresholdNode *m_pThresholds[16]; // 8 - learning 8 - working + //thresholdNode *m_pThresholds[16]; // 8 - learning 8 - working basicMutex m_mData; - bool sendTrasholdData( uint8_t lrf_id, uint32_t dataIdx = 0 ); - bool sendTargets( void ); - bool sendStatus( void ); + bool sendTrasholdData( thresholdNode *data, uint32_t dataIdx = 0 ); + //bool sendTargets( void ); + bool sendStatus( uint32_t m_nStatus ); virtual bool packetHandler( protocolPackage *Package ){ return true; }; public: protocolInternalLRF3D_SLAVE(); virtual ~protocolInternalLRF3D_SLAVE(); virtual bool create( void ); bool resetChannel(void); - bool setTrasholdData( uint8_t lrf_id, uint32_t *ptr ); bool pushTarget( targetRecord *ptr ); }; #else //!PRU_SLAVE @@ -115,10 +132,10 @@ class protocolInternalLRF3D: public protocolParser { virtual bool packetHandler( protocolPackage *Package ){ return true; }; basicMutex m_mLastTypeProtector; TWIN_COMMAND m_bLastPackageType; - thresholdNode * m_pTrashold; - targetRecord m_pTargets[TWI_TARGET_QUEUE_SIZE]; - uint8_t m_nTargetCount; - basicMutex m_mTargetProtector; + //thresholdNode * m_pTrashold; + //targetRecord m_pTargets[TWI_TARGET_QUEUE_SIZE]; + //uint8_t m_nTargetCount; + //basicMutex m_mTargetProtector; public: protocolInternalLRF3D_MASTER(); virtual ~protocolInternalLRF3D_MASTER(); diff --git a/lib/hardware/soc/general/3DLRF.h b/lib/hardware/soc/general/3DLRF.h index 5f31a8fd..3b73b02a 100644 --- a/lib/hardware/soc/general/3DLRF.h +++ b/lib/hardware/soc/general/3DLRF.h @@ -661,7 +661,7 @@ enum { // x = sid % TH_LINE_NODES; // }; inline uint32_t makeIds( int x, int y, bool Master = true ){ - return (Master)?(288-y)*TH_LINE_NODES+x:y*TH_LINE_NODES+x; + return (Master)?(Q_SCAN_COUNT-y)*TH_LINE_NODES+x:y*TH_LINE_NODES+x; }; inline uint32_t interpolate(int color1, int color2, float progress) { int a1 = (color1 & 0xff000000) >> 24;