|
|
#include "lrfScanner.h"
#define LOG( X ) m_pLog->Log( "LRFSCAN", this, X )
#define LOG_ERROR( X ) m_pLog->Log( "LRFSCAN", this, _LOG_ERROR, X )
/*
void FinishUpPackage( U8 Size ){ UART_BUFFER[0] = 0x55; UART_BUFFER[1] = Size - 2; UART_BUFFER[2] = UART_ADDRESS; UART_BUFFER[Size] = CheckSum( Size ); PACKAGE_LENGTH = Size+1; UART_INDEX = 0; READY_TO_SEND = 1;} */
const uint16_t ScanSamples[ 13 ] = { 0, 3328, 3328, 0, 1664, 0, 1120, 0, 800, 0, 0, 0, 544 };
uint8_t F_P( uint8_t *arr, uint8_t num ){ uint16_t cnt = 0; arr[0] = 0x55; arr[1] = num;//;( num - 2 ) & 0x7F;
arr[2] = 3; // LRF firmware address
for( uint8_t i = 0; i< num; i++ ) cnt+= arr[i]; arr[num] = cnt & 0xFF; return num+1; };
LRF_techScanner::LRF_techScanner( config_setting_t * Settings, PATHFINDER_DBC * DBC ):basicDualThread(){ m_pLog = basicLogCollection::getInstance(); m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "LRFSCAN" ); m_pBLOG = objectPathfinderBinaryLog::getInstance(); m_bSpeedSPS = 8; //Default 8 spin per second
m_nPulseCount = ScanSamples[ m_bSpeedSPS ]; m_nByteCount = m_nPulseCount << 1; m_pDBC = DBC; m_bActiveBuffer = 0; m_nSampleID = 0; m_iOutFID = -1; m_iInFID = -1; m_bWMODE = LRF_WM_INIT; m_sUpdateThreadHandler = TH_PRE_INIT;};
LRF_techScanner::~LRF_techScanner(){ if( m_bWMODE == LRF_WM_DATA ) stopScan(); if( m_iOutFID >= 0 ) { close( m_iOutFID ); } if( m_iInFID >= 0 ) { close( m_iInFID ); } //if (m_pPort!=NULL) delete(m_pPort);
};
bool LRF_techScanner::create(){ LOG("CREATE"); if( m_pDBC == NULL ) { config_setting_t *pSetting; pSetting = config_setting_get_member(m_pSettings, "BOARD_SPEED" ); if ( pSetting == NULL ){ m_bSpeedSPS = 8; pSetting = config_setting_add( m_pSettings, "BOARD_SPEED", CONFIG_TYPE_INT ); config_setting_set_int( pSetting, m_bSpeedSPS ); basicSettingsProvider::getInstance()->compromise(); LOG("Board Speed not defined - using default (8)!"); } else { m_bSpeedSPS = config_setting_get_int(pSetting); }; basicSettingsProvider::getInstance()->validate(); } else { if( !m_pDBC->getSettingsLRF( &m_bSpeedSPS ) ) m_bSpeedSPS = 8; }; LOG("SETTINGS READ"); m_bInitComplete = HW_init( "/dev/ttyS0", 3000000);//4000000 );
if( m_bInitComplete ){ LOG("HW DONE"); m_bWMODE = LRF_WM_COMMAND; return basicDualThread::init(); }; return false;};
bool LRF_techScanner::HW_init( CSTR DevicePath, int Speed ){ char MsgBuffer[CLOG]; m_bInitComplete = false; LOG("HW Init"); sprintf( m_sDevicePath, "%s", DevicePath ); int result; sprintf( MsgBuffer, "Opening FD %s", m_sDevicePath ); LOG( MsgBuffer ); do { m_iInFID = open( m_sDevicePath, O_RDONLY | O_NOCTTY | O_NONBLOCK | O_NDELAY ); } while( m_iInFID == -1 && errno == EINTR); if( m_iInFID == -1 ) return false;
do{ result = fcntl( m_iInFID, F_SETFL, O_NONBLOCK ); } while( result == -1 && errno == EINTR );
if( result == -1 ){ int const error = errno; do{ result = close( m_iInFID ); }while( result == -1 && errno == EINTR ); errno = error; sprintf( MsgBuffer, "Error %d openning port: %s", errno, strerror (errno) ); LOG_ERROR( MsgBuffer ); return false; }; do{ m_iOutFID = open( DevicePath, O_WRONLY | O_NDELAY ); }while( m_iOutFID == -1 && errno == EINTR); if( m_iOutFID == -1 ) return false;
/* *** Configure Port *** */ memset( &m_sTTY, 0, sizeof( m_sTTY ) );
/* Error Handling */ do{ result = tcgetattr( m_iInFID, &m_sTTY ); }while( result == -1 && errno == EINTR );
if( result == -1 ){ int const error = errno; do{ result = close( m_iInFID ); }while( result == -1 && errno == EINTR ); errno = error; sprintf( MsgBuffer, "Error %d from tcgetattr: %s", errno, strerror (errno) ); LOG_ERROR( MsgBuffer ); return false; };
/* Set Baud Rate */ m_iLineSpeed = Speed; int RetVal = 0; switch( m_iLineSpeed ){ case 4000000: cfsetspeed (&m_sTTY, B4000000 ); break; case 3500000: cfsetspeed (&m_sTTY, B3500000 ); break; case 3000000: cfsetspeed (&m_sTTY, B3000000 ); break; case 2500000: cfsetspeed (&m_sTTY, B2500000 ); break; case 2000000: cfsetspeed (&m_sTTY, B2000000 ); break; case 1500000: cfsetspeed (&m_sTTY, B1500000 ); break; case 1152000: cfsetspeed (&m_sTTY, B1152000 ); break; case 1000000: cfsetspeed (&m_sTTY, B1000000 ); break; case 921600: cfsetspeed (&m_sTTY, B921600 ); break; case 460800: cfsetspeed (&m_sTTY, B460800 ); break; case 230400: cfsetspeed (&m_sTTY, B230400 ); break; case 115200: cfsetspeed (&m_sTTY, B115200 ); break; case 19200: cfsetspeed (&m_sTTY, B19200 ); break; case 9600: cfsetspeed (&m_sTTY, B9600 ); break; default:{ close( m_iInFID ); close( m_iOutFID ); sprintf( MsgBuffer, "Error cannot set speed as [%d] bod", m_iLineSpeed ); LOG_ERROR( MsgBuffer ); return false; }; }; if( RetVal == 0 ) {#ifdef DEBUG
sprintf( MsgBuffer, "speed set to [%d] bod", m_iLineSpeed ); LOG( MsgBuffer );#endif
} else { RetVal = cfgetispeed( &m_sTTY ); sprintf( MsgBuffer, "Error cannot set speed as [%d] bod. Current is: %d bod. ", m_iLineSpeed, RetVal ); LOG_ERROR( MsgBuffer ); return false; };
/* Setting other Port Stuff */
m_sTTY.c_cflag &= ~PARENB; // Make 8n1
m_sTTY.c_cflag &= ~CSTOPB; m_sTTY.c_cflag &= ~CSIZE; m_sTTY.c_cflag |= CS8; m_sTTY.c_cflag &= ~CRTSCTS; // no flow control
m_sTTY.c_lflag = 0; // no signaling chars, no echo, no canonical processing
m_sTTY.c_oflag = 0; // no remapping, no delays
m_sTTY.c_cc[VMIN] = 0; // read doesn't block
m_sTTY.c_cc[VTIME] = 1; // 0.1 seconds read timeout
m_sTTY.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
m_sTTY.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
m_sTTY.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
m_sTTY.c_oflag &= ~OPOST; // make raw
/* Flush Port, then applies attributes */ tcflush( m_iOutFID, TCIFLUSH );
do{ result = tcsetattr( m_iOutFID, TCSANOW, &m_sTTY ); }while( result == -1 && errno == EINTR ); if( result == -1 ){ int const error = errno; do{ result = close( m_iOutFID ); }while( result == -1 && errno == EINTR ); errno = error; sprintf( MsgBuffer, "Error %d from tcsetattr: %s", errno, strerror (errno) ); LOG_ERROR( MsgBuffer ); return false; }; /* Flush Port, then applies attributes */ tcflush( m_iInFID, TCIFLUSH ); do{ result = tcsetattr( m_iInFID, TCSANOW, &m_sTTY ); }while( result == -1 && errno == EINTR ); if( result == -1 ){ int const error = errno; do{ result = close( m_iInFID ); }while( result == -1 && errno == EINTR ); errno = error; sprintf( MsgBuffer, "Error %d from tcsetattr: %s", errno, strerror (errno) ); LOG_ERROR( MsgBuffer ); return false; }; return true;};
bool LRF_techScanner::setDeviceSPS( uint8_t S ){ config_setting_t *pSetting; m_bSpeedSPS = S; m_nPulseCount = ScanSamples[m_bSpeedSPS]; m_nByteCount = m_nPulseCount << 1; pSetting = config_setting_get_member(m_pSettings, "BOARD_SPEED" ); if ( pSetting == NULL ){ pSetting = config_setting_add( m_pSettings, "BOARD_SPEED", CONFIG_TYPE_INT ); }; config_setting_set_int( pSetting, m_bSpeedSPS ); basicSettingsProvider::getInstance()->compromise(); basicSettingsProvider::getInstance()->validate(); return setParams();};
void LRF_techScanner::m_fThreadAProc( void ){ m_bThreadAStatus = TH_WORKING; bool DataOK; uint8_t scanCounter = 0;
while( m_bThreadAStatus < TH_ENDED ){ DataOK = false; m_oMutex.lock(); if( m_bDataReady ) { DataOK = true; m_bDataReady = false; m_sEvent.begin_milliseconds = m_sScanSTS.milliseconds; m_sEvent.begin_seconds = m_sScanSTS.seconds; m_sEvent.end_milliseconds = m_sScanFTS.milliseconds; m_sEvent.end_seconds = m_sScanFTS.seconds; m_sEvent.sps = m_bSpeedSPS; m_sEvent.samples_count = m_nPulseCount; m_sEvent.samples = ( uint16_t * ) inBuffer[1 - m_bActiveBuffer]; }; m_oMutex.unlock(); if( !DataOK ){ SLEEP( 3 ); } else { scanCounter++; // Binary LOG
if( m_pBLOG != NULL ) { m_pBLOG->registerLFS( &m_sEvent ); }; // Database
if( ( scanCounter % m_bSpeedSPS ) == 0 ) { scanCounter = 0; int RetVal = pthread_join( m_sUpdateThreadHandler, NULL ); RetVal = pthread_create( &m_sUpdateThreadHandler, NULL, m_fUpdateShutNForget, ( void * ) this ); if( RetVal == -1 ) { LOG_ERROR("Thread Creation Faled."); }; }; }; };};
void *LRF_techScanner::m_fUpdateShutNForget( void * ptr ){ LRF_techScanner * Me; FILE * FD; if( ptr == NULL ) return NULL; Me = (LRF_techScanner *) ptr;#ifdef DEBUG_LRF_DATA
Me->m_pLog->Log( "LRFSCAN", Me, "Update" ); FD = fopen("data.out", "ab+"); if( FD!= NULL ) { fwrite( Me->m_sEvent.samples, 2, Me->m_sEvent.samples_count, FD ); fclose( FD ); };#endif
if( Me->m_pDBC != NULL ) { Me->m_pDBC->updateLRF_SCAN( &Me->m_sEvent ); }; return NULL;};
void LRF_techScanner::m_fThreadBProc( void ){ uint8_t inCommand[16]; uint16_t * m16Mask; uint16_t mBufferSize = 0; uint16_t SampleIndex = 0; char tBuff[256]; int RetVal; uint16_t idx; uint8_t parserIdx; uint8_t parserBuff[2]; uint16_t cSIZE; bool NoStopStage = true;
m16Mask = ( uint16_t * ) inCommand; m_bThreadBStatus = TH_WORKING; while( m_bThreadBStatus < TH_ENDED ){ switch( m_bWMODE ){ case LRF_WM_STOPING:{ if( NoStopStage ) { mBufferSize = 0; NoStopStage = false; }; RetVal = recptBlock( inCommand + mBufferSize, 1 ); if( RetVal < 0 ) { LOG_ERROR("COM Read Error!"); m_bThreadBStatus = TH_ENDED; continue; }; if( RetVal == 0 ) { usleep(1); continue; }; mBufferSize += RetVal; if( mBufferSize > 3 ) { if( ( *((uint16_t *)( inCommand )) == 0xFFFF ) && ( *((uint16_t *)( inCommand+2 )) == 0xAAAA ) ) { LOG("CMD_STOP ACK"); m_bWMODE = LRF_WM_COMMAND; cSIZE = 1; cDATA = false; mBufferSize = 0; m16Mask = ( uint16_t * ) inCommand; if( m_pDBC != NULL ) { m_pDBC->setLrfMode( 0 ); }; continue; }; memcpy( inCommand, inCommand+1, 3 ); mBufferSize = 3; }; break; }; case LRF_WM_DATA:{ RetVal = recptBlock( inBuffer[m_bActiveBuffer] + mBufferSize, cSIZE ); if( RetVal < 0 ) { LOG_ERROR("COM Read Error!"); m_bThreadBStatus = TH_ENDED; continue; }; if( RetVal == 0 ) { usleep(1); continue; }; mBufferSize += RetVal; if( cDATA ) { if( mBufferSize >= m_nByteCount ) { m_oMutex.lock(); getPFTS( &m_sScanFTS ); //memcpy( m_pScanData, inBuffer, m_nByteCount );
m_bActiveBuffer = 1 - m_bActiveBuffer; m16Mask = ( uint16_t * ) inBuffer[m_bActiveBuffer]; m_bDataReady = true; mBufferSize -= m_nByteCount; memcpy( inBuffer[m_bActiveBuffer], inBuffer[1-m_bActiveBuffer]+m_nByteCount, mBufferSize ); m_oMutex.unlock(); cSIZE = 1; cDATA = false; }; } else { if( mBufferSize > 1 ) { for( idx = 0; idx < (mBufferSize - 2); idx++ ){ if( *((uint16_t *)(inBuffer[m_bActiveBuffer]+idx)) == 0xFFFF ) { m_oMutex.lock(); getPFTS( &m_sScanSTS ); m_oMutex.unlock(); mBufferSize -= (idx+2); memcpy( inBuffer[m_bActiveBuffer], inBuffer[m_bActiveBuffer]+idx+2, mBufferSize ); cDATA = true; cSIZE = 256; continue; }; }; if( mBufferSize >=m_nByteCount ) { mBufferSize = 0; }; }; }; continue; }; case LRF_WM_COMMAND:{ RetVal = recptBlock( inCommand, 16 ); if( RetVal < 0 ) { // Error
LOG_ERROR("COM Read Error!"); m_bThreadBStatus = TH_ENDED; continue; }; if( RetVal == 0 ) { SLEEP( 5 ); continue; }; if( (*m16Mask) == 0xFFFF ){ switch( inCommand[2] ){ case CMD_GET_MODE:{ LOG("CMD_GET_MODE ACK"); break; }; case CMD_GET_PARAM:{ LOG("CMD_GET_PARAM ACK"); m_bSpeedSPS = inCommand[3]; m_nPulseCount = inCommand[4]; m_nPulseCount = ( m_nPulseCount << 8 ) | inCommand[5]; m_nByteCount = m_nPulseCount << 1; char tBuffer[256]; sprintf( tBuffer, "Working Params: Speed: %d Samples: %d", m_bSpeedSPS, m_nPulseCount ); LOG( tBuffer ); break; }; case CMD_START:{ LOG("CMD_START ACK"); cSIZE = 1; cDATA = false; parserIdx = 0; mBufferSize = 0; m16Mask = ( uint16_t * ) inBuffer[m_bActiveBuffer]; m_bWMODE = LRF_WM_DATA; if( m_pDBC != NULL ) { m_pDBC->setLrfMode( 1 ); }; break; }; case CMD_STOP:{ LOG("CMD_STOP ACK"); m_bWMODE = LRF_WM_COMMAND; if( m_pDBC != NULL ) { m_pDBC->setLrfMode( 0 ); }; break; }; case CMD_RESET:{ LOG("CMD_RESET ACK"); break; }; }; }; }; }; };};
bool LRF_techScanner::setParams(){ uint8_t Buffer[16]; //if( m_bStatus == 0 ) return false;
Buffer[0] = 0xFF; Buffer[1] = 0xFF; Buffer[2] = 7; Buffer[3] = CMD_SET_PARAM; Buffer[4] = m_bSpeedSPS; Buffer[5] = ( m_nPulseCount >> 8) & 0xFF; Buffer[6] = m_nPulseCount & 0xFF; LOG("Set Params"); sendBlock( Buffer, 7 ); return true;};
bool LRF_techScanner::reqParams(){ uint8_t Buffer[16]; Buffer[0] = 0xFF; Buffer[1] = 0xFF; Buffer[2] = 4; Buffer[3] = CMD_GET_PARAM; LOG("Ask Params"); sendBlock( Buffer, 4 ); return true;};
bool LRF_techScanner::reqMode(){ uint8_t Buffer[16]; Buffer[0] = 0xFF; Buffer[1] = 0xFF; Buffer[2] = 4; Buffer[3] = CMD_GET_MODE; LOG("Ask Working Mode"); sendBlock( Buffer, 4 ); return true;};
bool LRF_techScanner::startScan(){ uint8_t Buffer[16]; Buffer[0] = 0xFF; Buffer[1] = 0xFF; Buffer[2] = 4; Buffer[3] = CMD_START; LOG("Start Scan"); sendBlock( Buffer, 4 ); return true;};
bool LRF_techScanner::stopScan(){ uint8_t Buffer[16]; uint8_t cnt = 255; Buffer[0] = 0xFF; Buffer[1] = 0xFF; Buffer[2] = 4; Buffer[3] = CMD_STOP; LOG("Stop Scan"); m_bWMODE = LRF_WM_STOPING; while( m_bWMODE != LRF_WM_COMMAND && m_bThreadBStatus == TH_WORKING ){ sendBlock( Buffer, 4 ); SLEEP(200); }; return true;};
bool LRF_techScanner::resetDevice(){ uint8_t Buffer[16]; Buffer[0] = 0xFF; Buffer[1] = 0xFF; Buffer[2] = 4; Buffer[3] = CMD_RESET; LOG("Reset Scanner"); sendBlock( Buffer, 4 ); return true;};
int LRF_techScanner::sendBlock( unsigned char *Data, int DataSize ){ if(!m_bInitComplete) {#ifdef DEBUG
LOG( "Port is not Ready to Send" );#endif
return -1; }; int RetVal; do{ RetVal = write( m_iOutFID, Data, DataSize ); if((RetVal == -1 && (errno == EINTR || errno == EWOULDBLOCK))) usleep(100); } while (RetVal == -1 && (errno == EINTR || errno == EWOULDBLOCK));#ifdef DEBUG
char MsgBuffer[CLOG]; sprintf( MsgBuffer, "Sent %d byte(s) of %d", RetVal, DataSize ); LOG( MsgBuffer ); #ifdef _DEBUG_SERIAL_COMMUNICATION_
for( int i = 0; i < DataSize; i++ ){ sprintf( MsgBuffer, "0x%02X", Data[i] ); LOG( MsgBuffer ); }; #endif
#endif
return RetVal;};
int LRF_techScanner::recptBlock( unsigned char *Buffer, int BufferLength ){ if (!m_bInitComplete) {#ifdef DEBUG
LOG("Port is Not Ready to Read");#endif
return -1; }; int RetVal; do { RetVal = read( m_iInFID, Buffer, BufferLength ); if( RetVal == -1 && errno == EINTR ) usleep(100); } while (RetVal == -1 && errno == EINTR);
#ifdef __DEBUG
if( RetVal > 0 ){ char MsgBuffer[CLOG]; if( RetVal > 1 || RetVal == 0 ) { sprintf( MsgBuffer, "Received %d bytes", RetVal ); } else { sprintf( MsgBuffer, "Received 1 byte"); }; LOG( MsgBuffer ); };#endif
if( RetVal > 0) return RetVal; if( RetVal == -1) return 0; if( RetVal == 0 ){ errno = 0; return 0; }; /* r < -1, should never happen. */ errno = EIO; return 0;};
bool LRF_techScanner::isDead(){ return false;};
#undef LOG
#undef LOG_ERROR
|