|
|
|
@ -2,6 +2,13 @@ |
|
|
|
#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
|
|
|
|
|
|
|
|
#define LOG( X ) m_pLog->Log( "TWINC", this, X )
|
|
|
|
#ifdef DEBUG
|
|
|
|
#define LOG_ERROR( X ) {\
|
|
|
|
@ -16,9 +23,9 @@ |
|
|
|
LRF_TwinConnector::LRF_TwinConnector( config_setting_t *Settings ): basicThread(){ |
|
|
|
m_pLog = basicLogCollection::getInstance(); |
|
|
|
m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, (CSTR)"TWIN_3DLRF_CONNECTION" ); |
|
|
|
m_pProtocol = NULL; |
|
|
|
m_pPort = NULL; |
|
|
|
m_pEngine = NULL; |
|
|
|
m_nStatus = 0; |
|
|
|
#ifdef PRU_SLAVE
|
|
|
|
m_nMasterPort = 0; |
|
|
|
m_pMasterAddress = (CSTR) malloc( 16 ); |
|
|
|
@ -33,7 +40,6 @@ LRF_TwinConnector::LRF_TwinConnector( config_setting_t *Settings ): basicThread( |
|
|
|
|
|
|
|
LRF_TwinConnector::~LRF_TwinConnector(){ |
|
|
|
if( m_pPort != NULL ) delete( m_pPort ); |
|
|
|
if( m_pProtocol != NULL ) delete( m_pProtocol ); |
|
|
|
|
|
|
|
#ifdef PRU_SLAVE
|
|
|
|
if( m_pMasterAddress ) free( m_pMasterAddress ); |
|
|
|
@ -98,23 +104,23 @@ bool LRF_TwinConnector::create(){ |
|
|
|
return false; |
|
|
|
}; |
|
|
|
#ifdef PRU_SLAVE
|
|
|
|
m_pProtocol = new protocolInternalLRF3D_SLAVE(); |
|
|
|
if( m_pProtocol == NULL ){ |
|
|
|
LOG_ERROR("Error creating protocol!"); |
|
|
|
return false; |
|
|
|
}; |
|
|
|
if( !m_pProtocol->create() ) return false; |
|
|
|
//m_pProtocol = new protocolInternalLRF3D_SLAVE();
|
|
|
|
//if( m_pProtocol == NULL ){
|
|
|
|
// LOG_ERROR("Error creating protocol!");
|
|
|
|
// return false;
|
|
|
|
//};
|
|
|
|
if( !protocolInternalLRF3D_SLAVE::create() ) return false; |
|
|
|
#else
|
|
|
|
int RetCode; |
|
|
|
struct hostent * h_info; |
|
|
|
struct sockaddr_in * MainSocketAddress = NULL; |
|
|
|
|
|
|
|
m_pProtocol = new protocolInternalLRF3D_MASTER(); |
|
|
|
if( m_pProtocol == NULL ){ |
|
|
|
LOG_ERROR("Error creating protocol!"); |
|
|
|
return false; |
|
|
|
}; |
|
|
|
if( !m_pProtocol->create() ) return false; |
|
|
|
//m_pProtocol = new protocolInternalLRF3D_MASTER();
|
|
|
|
//if( m_pProtocol == NULL ){
|
|
|
|
// LOG_ERROR("Error creating protocol!");
|
|
|
|
// return false;
|
|
|
|
//};
|
|
|
|
if( !protocolInternalLRF3D_MASTER::create() ) return false; |
|
|
|
|
|
|
|
m_nMainSocket = socket( AF_INET, SOCK_STREAM, 0 ); |
|
|
|
if( m_nMainSocket == -1){ |
|
|
|
@ -165,6 +171,17 @@ bool LRF_TwinConnector::create(){ |
|
|
|
}; |
|
|
|
|
|
|
|
#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(); |
|
|
|
break; |
|
|
|
}; |
|
|
|
}; |
|
|
|
return true; |
|
|
|
}; |
|
|
|
void LRF_TwinConnector::m_fThreadProc( void ){ |
|
|
|
TWIN_CMD_REC * tCMD = NULL; |
|
|
|
int ClientSocket = -1; |
|
|
|
@ -228,19 +245,44 @@ bool LRF_TwinConnector::create(){ |
|
|
|
LOG("(re)Establish Connection"); |
|
|
|
m_pPort = new clientPort( m_pMasterAddress, m_nMasterPort, true ); |
|
|
|
if( m_pPort!= NULL ){ |
|
|
|
m_pProtocol->setTransport( m_pPort ); |
|
|
|
setTransport( m_pPort ); |
|
|
|
m_bWorkingMode = TWI_STAGE_CONNECTED; |
|
|
|
}; |
|
|
|
}; |
|
|
|
}; |
|
|
|
}; |
|
|
|
#else //Master
|
|
|
|
bool LRF_TwinConnector::packetHandler( protocolPackage *Package ){ |
|
|
|
if( Package == NULL ) return false; |
|
|
|
TWIN_COMMAND cCMD = (TWIN_COMMAND)Package->m_pData[3]; |
|
|
|
switch( cCMD ) { |
|
|
|
case TWC_ACK_STATUS:{ |
|
|
|
uint32_t tStatus; |
|
|
|
#ifdef DEBUG
|
|
|
|
LOG("Status ACK reseived"); |
|
|
|
#endif
|
|
|
|
tStatus = ( Package->m_pData[4] << 24 ) | ( Package->m_pData[5] << 16 ) | ( Package->m_pData[6] << 8 ) | Package->m_pData[7]; |
|
|
|
m_mStatusProtector.lock(); |
|
|
|
m_nStatus = tStatus; |
|
|
|
m_mStatusProtector.unlock(); |
|
|
|
break; |
|
|
|
}; |
|
|
|
case TWC_TH_DATA:{ |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
m_mLastTypeProtector.lock(); |
|
|
|
m_bLastPackageType = cCMD; |
|
|
|
m_mLastTypeProtector.unlock(); |
|
|
|
return true; |
|
|
|
}; |
|
|
|
void LRF_TwinConnector::m_fThreadProc( void ){ |
|
|
|
int ClientSocket = -1; |
|
|
|
struct sockaddr ClientAddress; |
|
|
|
socklen_t ClientAddressSize; |
|
|
|
long long cMilliseconds; |
|
|
|
long long cStatusTO = 0; |
|
|
|
long long cDrawTO = 0; |
|
|
|
|
|
|
|
m_bThreadStatus = TH_WORKING; |
|
|
|
|
|
|
|
@ -248,6 +290,29 @@ bool LRF_TwinConnector::create(){ |
|
|
|
|
|
|
|
cMilliseconds = getMilliseconds(); |
|
|
|
|
|
|
|
// TH Saving procedure
|
|
|
|
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!"); |
|
|
|
}; |
|
|
|
}; |
|
|
|
}; |
|
|
|
}; |
|
|
|
}; |
|
|
|
// Port exists - SLAVE is connected
|
|
|
|
if( m_pPort != NULL ){ |
|
|
|
// Check if still connected
|
|
|
|
@ -262,19 +327,19 @@ bool LRF_TwinConnector::create(){ |
|
|
|
// No Current Request Issued
|
|
|
|
switch( m_bWorkingMode ) { |
|
|
|
case TWI_STAGE_CONNECTED:{ |
|
|
|
if( m_pProtocol->requestSoftwareVersion() ){ |
|
|
|
if( requestSoftwareVersion() ){ |
|
|
|
m_bCCMD = TWC_ACK_VERSION; |
|
|
|
}; |
|
|
|
break; |
|
|
|
}; |
|
|
|
case TWI_STAGE_INIT_1:{ |
|
|
|
if( m_pProtocol->requestCurrentSettings() ){ |
|
|
|
if( requestCurrentSettings() ){ |
|
|
|
m_bCCMD = TWC_ACK_CONFIG; |
|
|
|
}; |
|
|
|
break; |
|
|
|
}; |
|
|
|
case TWI_STAGE_INIT_2:{ |
|
|
|
if( m_pProtocol->requestShadow() ){ |
|
|
|
if( requestShadow() ){ |
|
|
|
m_bCCMD = TWC_ACK_SHADOW; |
|
|
|
}; |
|
|
|
break; |
|
|
|
@ -282,18 +347,18 @@ bool LRF_TwinConnector::create(){ |
|
|
|
case TWI_STAGE_MAIN_LOOP:{ |
|
|
|
if( ( cMilliseconds - cStatusTO ) > TWI_STATUS_LOOP_MS ) { |
|
|
|
cStatusTO = cMilliseconds; |
|
|
|
m_pProtocol->requestStatus(); |
|
|
|
requestStatus(); |
|
|
|
m_bCCMD = TWC_ACK_STATUS; |
|
|
|
LOG("Req Status"); |
|
|
|
} else { |
|
|
|
SLEEP( TWI_SLOW_LOOP_SLEEP ); |
|
|
|
}; |
|
|
|
} |
|
|
|
break; |
|
|
|
}; |
|
|
|
}; |
|
|
|
} else { |
|
|
|
// Waiting CMD ACK status
|
|
|
|
if( m_pProtocol->getLastPackageType() == m_bCCMD ) { |
|
|
|
if( getLastPackageType() == m_bCCMD ) { |
|
|
|
switch( m_bCCMD ){ |
|
|
|
case TWC_ACK_VERSION:{ |
|
|
|
m_bWorkingMode = TWI_STAGE_INIT_1; |
|
|
|
@ -311,26 +376,26 @@ bool LRF_TwinConnector::create(){ |
|
|
|
#ifdef DEBUG
|
|
|
|
LOG("Registered Ack Status"); |
|
|
|
#endif
|
|
|
|
uint32_t nStatus = m_pProtocol->getStatus(); |
|
|
|
if( ( nStatus & TWI_STATUS_TARGET_MASK ) > 0 ){ |
|
|
|
//uint32_t nStatus = getStatus();
|
|
|
|
if( ( m_nStatus & TWI_STATUS_TARGET_MASK ) > 0 ){ |
|
|
|
#ifdef DEBUG
|
|
|
|
LOG("Have new Targets!"); |
|
|
|
#endif
|
|
|
|
//m_pProtocol->setTData( m_pTargets );
|
|
|
|
m_pProtocol->requestTargets(); |
|
|
|
requestTargets(); |
|
|
|
m_bCCMD = TWC_GET_TARGETS; |
|
|
|
continue; |
|
|
|
}; |
|
|
|
break; |
|
|
|
}; |
|
|
|
case TWC_GET_TARGETS:{ |
|
|
|
if( m_pProtocol->getLastPackageType() == TWC_TARGETS_DATA ){ |
|
|
|
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_pProtocol->getStatus() & TWI_STATUS_TARGET_MASK ) >> 8 ); |
|
|
|
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 ); |
|
|
|
}; |
|
|
|
@ -383,13 +448,21 @@ bool LRF_TwinConnector::create(){ |
|
|
|
m_pPort = new serverPort( ClientSocket ); |
|
|
|
}; |
|
|
|
if( m_pPort!= NULL ){ |
|
|
|
m_pProtocol->setTransport( m_pPort ); |
|
|
|
setTransport( m_pPort ); |
|
|
|
// Reset Stage
|
|
|
|
m_bWorkingMode = TWI_STAGE_CONNECTED; |
|
|
|
}; |
|
|
|
}; |
|
|
|
}; |
|
|
|
}; |
|
|
|
bool LRF_TwinConnector::attachTargetTracker( LRF_TargetTracker *ptr ){ |
|
|
|
if(ptr == NULL) return false; |
|
|
|
m_pTracker = ptr; |
|
|
|
return false; |
|
|
|
}; |
|
|
|
bool LRF_TwinConnector::updateSettings( void ){ |
|
|
|
return true; |
|
|
|
}; |
|
|
|
#endif //!PRU_SLAVE
|
|
|
|
|
|
|
|
bool LRF_TwinConnector::cleanTHData( uint8_t kid ){ |
|
|
|
@ -453,13 +526,93 @@ bool LRF_TwinConnector::saveTHData( uint8_t kid, thresholdNode *ptr ){ |
|
|
|
return true; |
|
|
|
}; |
|
|
|
|
|
|
|
bool LRF_TwinConnector::saveTHPicture( thresholdNode *ptr, CSTR fileName, bool monochrome ){ |
|
|
|
uint8_t *tMemory = NULL; |
|
|
|
thresholdNode *cNode = NULL; |
|
|
|
uint8_t *cPixel = NULL; |
|
|
|
uint8_t cSpace; |
|
|
|
if( monochrome ) { |
|
|
|
cSpace = 1; |
|
|
|
} else { |
|
|
|
cSpace = 4; |
|
|
|
}; |
|
|
|
tMemory = (uint8_t *) malloc( TH_SIZE_NODES * cSpace ); |
|
|
|
if( tMemory == NULL ){ |
|
|
|
|
|
|
|
return false; |
|
|
|
}; |
|
|
|
|
|
|
|
#ifndef PRU_SLAVE
|
|
|
|
bool LRF_TwinConnector::attachTargetTracker( LRF_TargetTracker *ptr ){ |
|
|
|
if(ptr == NULL) return false; |
|
|
|
m_pTracker = ptr; |
|
|
|
return false; |
|
|
|
}; |
|
|
|
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 );
|
|
|
|
#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); |
|
|
|
if( monochrome ) { |
|
|
|
#ifdef CHECK_SUN_BIT
|
|
|
|
if( cNode->values.sbit ) { |
|
|
|
cPixel[0] = 0; |
|
|
|
} else { |
|
|
|
cPixel[0] = (uint8_t)((((float) cNode->values.min ) * 255.f ) / 4096.f ); |
|
|
|
}; |
|
|
|
#else
|
|
|
|
cPixel[0] = (uint8_t)((((float) cNode->values.min ) * 255.f ) / 4096.f ); |
|
|
|
#endif
|
|
|
|
} else { |
|
|
|
#ifdef CHECK_SUN_BIT
|
|
|
|
if( cNode->values.sbit ) { |
|
|
|
// Set Red
|
|
|
|
cPixel[0] = 0x00; |
|
|
|
cPixel[1] = 0xff; |
|
|
|
cPixel[2] = 0x00; |
|
|
|
cPixel[3] = 0xff; |
|
|
|
} else { |
|
|
|
if( cNode->values.cnt == 0 ) { |
|
|
|
// Set Blue
|
|
|
|
cPixel[0] = 0x00; |
|
|
|
cPixel[1] = 0x00; |
|
|
|
cPixel[2] = 0xff; |
|
|
|
cPixel[3] = 0xff; |
|
|
|
} else { |
|
|
|
uint32_t tColor; |
|
|
|
float tVal = ((float) cNode->values.min ) / 4096.f; |
|
|
|
tColor = interpolate( 0x000000ff, 0xffffffff, tVal ); |
|
|
|
cPixel[0] = ( tColor >> 24 ) & 0xFF; |
|
|
|
cPixel[1] = ( tColor >> 16 ) & 0xFF; |
|
|
|
cPixel[2] = ( tColor >> 8 ) & 0xFF; |
|
|
|
cPixel[3] = tColor & 0xFF; |
|
|
|
}; |
|
|
|
}; |
|
|
|
#else
|
|
|
|
if( cNode->values.cnt == 0 ) { |
|
|
|
// Set Blue
|
|
|
|
cPixel[0] = 0x00; |
|
|
|
cPixel[1] = 0x00; |
|
|
|
cPixel[2] = 0xff; |
|
|
|
cPixel[3] = 0xff; |
|
|
|
} else { |
|
|
|
uint32_t tColor; |
|
|
|
float tVal = ((float) cNode->values.min ) / 4096.f; |
|
|
|
tColor = interpolate( 0x000000ff, 0xffffffff, tVal ); |
|
|
|
cPixel[0] = ( tColor >> 24 ) & 0xFF; |
|
|
|
cPixel[1] = ( tColor >> 16 ) & 0xFF; |
|
|
|
cPixel[2] = ( tColor >> 8 ) & 0xFF; |
|
|
|
cPixel[3] = tColor & 0xFF; |
|
|
|
}; |
|
|
|
#endif
|
|
|
|
}; |
|
|
|
}; |
|
|
|
}; |
|
|
|
int RetVal = stbi_write_png( fileName, TH_LINE_NODES, Q_SCAN_COUNT, cSpace, tMemory, TH_LINE_NODES * cSpace ); |
|
|
|
free( tMemory ); |
|
|
|
if( RetVal == 0 ) return false; |
|
|
|
return true; |
|
|
|
}; |
|
|
|
|
|
|
|
#undef LOG
|
|
|
|
#undef LOG_ERROR
|
|
|
|
|