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
966 lines
35 KiB
//----------------------------------------------------------------------------
|
|
// Dr.Frucht Systems Ltd.
|
|
// Copyright © 2000-2017. All Rights Reserved.
|
|
// Version 2.0 7/20/2017
|
|
// Last modification: 7/20/2017
|
|
// PROJECT:
|
|
// SUBSYSTEM:
|
|
// FILE: LRF_Kernel.cpp
|
|
// AUTHOR: gagua
|
|
// OVERVIEW
|
|
// ~~~~~~~~
|
|
// LRF_Kernel.cpp
|
|
//----------------------------------------------------------------------------
|
|
//
|
|
|
|
#include "LRF_SlimKernel.h"
|
|
#include "LRF_Detector.h"
|
|
#include "LRF_ClientHandler.h"
|
|
|
|
#define LOG( X ) m_pLog->Log( "SLKRNL", this, X )
|
|
#ifdef DEBUG
|
|
#define LOG_ERROR( X ) {\
|
|
char _tBfrErrMSG234[256];\
|
|
sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
|
|
m_pLog->Log( "SLKRNL", this, _LOG_ERROR, _tBfrErrMSG234 );\
|
|
}
|
|
#else
|
|
#define LOG_ERROR( X ) m_pLog->Log( "SLKRNL", this, _LOG_ERROR, X );
|
|
#endif
|
|
|
|
// Constructor implementation
|
|
#ifdef PRU_SLAVE
|
|
LRF_Kernel::LRF_Kernel( LRF_HardwareManager *HM ): basicThread(), basicEngine(){
|
|
#else
|
|
LRF_Kernel::LRF_Kernel( LRF_HardwareManager *HM ): basicThread(), networkServiceEngine(){
|
|
#endif
|
|
|
|
m_bExitCause = 0;
|
|
m_pLog = basicLogCollection::getInstance();
|
|
m_pEventHeap = basicEventHeap::getInstance();
|
|
m_pSettingsProvider = basicSettingsProvider::getInstance();
|
|
if( m_pSettingsProvider ) {
|
|
m_pSettings = m_pSettingsProvider->getSubNode( NULL, "KERNEL" );
|
|
if( m_pSettingsProvider->isCompromised() ) {
|
|
m_pSettingsProvider->validate();
|
|
};
|
|
} else {
|
|
m_pSettings = NULL;
|
|
};
|
|
m_pHardwareManager = HM;
|
|
m_nKernelFilter = 0x000000FF;
|
|
m_nFlipCounter = 0;
|
|
m_nFlipSavierID = 0;
|
|
m_pShadow = NULL;
|
|
m_pTWIC = NULL;
|
|
|
|
#ifndef PRU_SLAVE
|
|
m_pTracker = NULL;
|
|
m_pNetClientsServer = NULL;
|
|
m_nSST = 0;
|
|
#endif
|
|
m_pCurrentTH = NULL;
|
|
m_nCurrentTH = 0;
|
|
m_bCurrentTH = 0;
|
|
m_nForcedDrawTH = 0;
|
|
|
|
//m_eWM = WM_NONE;
|
|
m_nWidth = 800;
|
|
};
|
|
|
|
// Destructor implementation
|
|
LRF_Kernel::~LRF_Kernel(){
|
|
#ifdef DEBUG
|
|
LOG("Destructor");
|
|
LOG("End Main Thread");
|
|
#endif
|
|
WaitFinish( true );
|
|
#ifdef DEBUG
|
|
LOG("Deleting Components");
|
|
LOG("TWINC");
|
|
#endif
|
|
if( m_pTWIC ) delete( m_pTWIC );
|
|
#ifdef DEBUG
|
|
LOG("Twin Connector - Done");
|
|
LOG("SHADOW");
|
|
#endif
|
|
if( m_pShadow ) delete( m_pShadow );
|
|
#ifdef DEBUG
|
|
LOG("Shadow Mask - Done");
|
|
#endif
|
|
if ( m_pCurrentTH ) free( m_pCurrentTH );
|
|
#ifdef DEBUG
|
|
LOG("Destructor Finished");
|
|
#endif
|
|
};
|
|
|
|
#ifdef PRU_SLAVE
|
|
bool LRF_Kernel::create( LRF_HardwareManager *ptr ){
|
|
#else
|
|
bool LRF_Kernel::attachNetServer( networkServer *srvPort ){
|
|
if( srvPort != NULL ) {
|
|
m_pNetClientsServer = srvPort;
|
|
};
|
|
if( m_pNetClientsServer!= NULL ) {
|
|
return srvPort->attachEngine( this );
|
|
};
|
|
return false;
|
|
};
|
|
bool LRF_Kernel::create( LRF_HardwareManager *ptr, networkServer *srvPort ){
|
|
#endif
|
|
if( m_pSettings != NULL ) {
|
|
config_setting_t *pSetting;
|
|
int tVal;
|
|
char tBuffer[256];
|
|
LOG("Getting Settings ...");
|
|
#ifndef PRU_SLAVE
|
|
if (!config_setting_lookup_int(m_pSettings, KNL_SETTINGS_DETECTOR_SST, &tVal )){
|
|
tVal = m_nSST = 0;
|
|
pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_DETECTOR_SST, CONFIG_TYPE_INT);
|
|
config_setting_set_int(pSetting, tVal );
|
|
m_pSettingsProvider->compromise();
|
|
LOG("No Detector SST");
|
|
} else {
|
|
m_nSST = tVal;
|
|
sprintf( tBuffer, "Detector SST Read From Settings: %d", tVal );
|
|
LOG( tBuffer );
|
|
};
|
|
#endif
|
|
if (!config_setting_lookup_int(m_pSettings, KNL_SETTINGS_DETECTOR_WIDTH, &tVal )){
|
|
tVal = m_nWidth = 1000;
|
|
pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_DETECTOR_WIDTH, CONFIG_TYPE_INT);
|
|
config_setting_set_int(pSetting, tVal );
|
|
m_pSettingsProvider->compromise();
|
|
LOG("No Detector Width");
|
|
} else {
|
|
m_nWidth = tVal;
|
|
sprintf( tBuffer, "Detector Width Read From Settings: %d", tVal );
|
|
LOG( tBuffer );
|
|
};
|
|
if (!config_setting_lookup_int(m_pSettings, KNL_SETTINGS_FILTER_KERNELS, &tVal )){
|
|
tVal = m_nKernelFilter = 0x000000ff;
|
|
pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_FILTER_KERNELS, CONFIG_TYPE_INT);
|
|
config_setting_set_int(pSetting, tVal );
|
|
m_pSettingsProvider->compromise();
|
|
LOG("No Fltered Kernels Defined - All LRF Will Work");
|
|
} else {
|
|
m_nKernelFilter = tVal;
|
|
sprintf( tBuffer, "Filter Kernels Read From Settings: %d", tVal );
|
|
LOG( tBuffer );
|
|
};
|
|
if( m_pSettingsProvider->isCompromised() ) {
|
|
LOG("Save Settings");
|
|
m_pSettingsProvider->validate();
|
|
};
|
|
} else {
|
|
LOG_ERROR("Failed Read Config Settings!");
|
|
return false;
|
|
};
|
|
//m_eWM = WM_INIT;
|
|
setWM( WM_INIT );
|
|
m_nFlipCounter = 0;
|
|
m_nFlipSavierID = 0;
|
|
|
|
m_pShadow = new ShadowMask();
|
|
|
|
if( m_pShadow == NULL ) {
|
|
LOG_ERROR("Failed to create Shadow Mask!");
|
|
return false;
|
|
} else {
|
|
//Load Default Masks
|
|
if( m_pShadow->loadVertexes() ) LOG("Vertexeses Loadded!");
|
|
if( m_pShadow->loadRastered() ) LOG("Traces Loadded!");
|
|
};
|
|
|
|
if( m_pShadow->isActive() ) {
|
|
LOG("Shadow Mask is ACTIVE");
|
|
} else {
|
|
LOG("Shadow Mask is NOT ACTIVE");
|
|
};
|
|
|
|
if( m_pEventHeap == NULL ){
|
|
LOG_ERROR("No Event Heap attached!");
|
|
return false;
|
|
};
|
|
if( ptr != NULL ) {
|
|
m_pHardwareManager = ptr;
|
|
m_pHardwareManager->setKernelFilter( m_nKernelFilter );
|
|
};
|
|
if( m_pHardwareManager == NULL ) {
|
|
LOG_ERROR("No Hardware manager attached!");
|
|
return false;
|
|
};
|
|
|
|
#ifdef HIBERNATE_ENABLE
|
|
if( fileExist("hibernate.dat")) {
|
|
LOG("Loading Hibernated Data.");
|
|
if(!m_pHardwareManager->loadHibernatedTH("hibernate.dat")){
|
|
LOG_ERROR("Hibernate Data Load failure!");
|
|
};
|
|
};
|
|
#endif
|
|
m_lCycleCounter = 2;
|
|
#ifndef PRU_SLAVE
|
|
if( !attachNetServer( srvPort ) ) {
|
|
LOG_ERROR("No Network manager attached!");
|
|
return false;
|
|
};
|
|
#ifdef DEBUG
|
|
LOG( "Target Tracker about to be created" );
|
|
#endif
|
|
m_pTracker = new LRF_TargetTracker( m_nSST, m_nWidth );
|
|
if( m_pTracker == NULL ) {
|
|
LOG_ERROR("Error in starting Target Tracker!");
|
|
return false;
|
|
};
|
|
#ifdef DEBUG
|
|
LOG( "Created" );
|
|
#endif
|
|
#endif // !PRU_SLAVE
|
|
|
|
#ifdef DEBUG
|
|
LOG( "Twin Connector about to be created" );
|
|
#endif
|
|
m_pTWIC = new LRF_TwinConnector( m_pSettings );
|
|
if( m_pTWIC == NULL ) {
|
|
LOG_ERROR("Error creating TWIN object!");
|
|
return false;
|
|
} else {
|
|
#ifdef DEBUG
|
|
LOG( "TWIN object - OK" );
|
|
#endif
|
|
if( m_pTWIC->create() ){
|
|
#ifdef DEBUG
|
|
LOG( "TWIN create - OK" );
|
|
#endif
|
|
m_pTWIC->setKernel( this );
|
|
#ifndef PRU_SLAVE
|
|
m_pTWIC->attachTargetTracker( m_pTracker );
|
|
#endif
|
|
} else {
|
|
LOG_ERROR("Error in TWIN create!");
|
|
return false;
|
|
};
|
|
};
|
|
#ifdef DEBUG
|
|
LOG( "TWIN connector Created" );
|
|
#endif
|
|
|
|
#ifdef DEBUG
|
|
LOG( "Trhead about to be created" );
|
|
#endif
|
|
if( init() == false ){
|
|
LOG_ERROR("Error in starting Kernel Thread!");
|
|
return false;
|
|
};
|
|
#ifdef DEBUG
|
|
LOG( "Created" );
|
|
#endif
|
|
//m_eWM = WM_STUDY;
|
|
setWM( WM_STUDY );
|
|
return true;
|
|
};
|
|
|
|
void LRF_Kernel::WaitFinish( bool Force ){
|
|
if( Force ){
|
|
#ifdef DEBUG
|
|
LOG( "Thread End Forced" );
|
|
#endif
|
|
} else {
|
|
// 3 seconds sleep to settle down processes
|
|
SLEEP(3000);
|
|
};
|
|
waitThreadEnd( Force );
|
|
#ifdef DEBUG
|
|
if( Force ) {
|
|
LOG( "Thread forced to be Detached" );
|
|
} else {
|
|
LOG( "Thread is Detached by itself" );
|
|
};
|
|
#endif
|
|
};
|
|
/*
|
|
LRF3D_WORK_MODE LRF_Kernel::getWM( void ){
|
|
LRF3D_WORK_MODE rv;
|
|
m_mWMProtector.lock();
|
|
rv = m_eWM;
|
|
m_mWMProtector.unlock();
|
|
return rv;
|
|
};
|
|
|
|
void LRF_Kernel::setWM( LRF3D_WORK_MODE nMode ){
|
|
m_mWMProtector.lock();
|
|
m_eWM = nMode;
|
|
m_mWMProtector.unlock();
|
|
};
|
|
*/
|
|
|
|
/* Main Kernel Thread Procedure */
|
|
void LRF_Kernel::m_fThreadProc( void ){
|
|
char Buffer[256];
|
|
DT_STATUS tStatus = {0};
|
|
uint8_t bActiveTH = 0;
|
|
uint8_t bMeasuredTH = 0;
|
|
uint8_t bDrawedTH = 0;
|
|
|
|
#ifdef SIMULATE_TARGETS
|
|
uint8_t CCNT = 0;
|
|
uint16_t CNNT = 0;
|
|
#endif
|
|
|
|
m_bThreadStatus = TH_WORKING;
|
|
|
|
while( m_bThreadStatus < TH_ENDED ){
|
|
|
|
#ifdef SIMULATE_TARGETS
|
|
#ifdef SIMULATE_DOS_TARGETS
|
|
if( m_lCycleCounter % 10 == 0 ) {
|
|
#else
|
|
if( m_lCycleCounter % 1000 == 0 ) {
|
|
#endif
|
|
// Generate Target
|
|
/*CNNT++;
|
|
targetRecord cNode;
|
|
cNode.begin_id = ((TH_SIZE_NODES>>1)+CCNT*(TH_LINE_NODES>>3)+CNNT)%TH_SIZE_NODES;
|
|
cNode.end_id = (cNode.begin_id+TH_LINE_NODES)%TH_SIZE_NODES;
|
|
cNode.status = 0;
|
|
cNode.flags.active = 1;
|
|
cNode.flags.lrf = CCNT;
|
|
cNode.flags.w_cnt = 2;
|
|
|
|
cNode.range_min = 140;
|
|
cNode.range_max = cNode.range_min;
|
|
cNode.width = cNode.flags.w_cnt * cNode.range_min;
|
|
//cNode.width = ((4095>>3)*(8-CCNT))<<1;
|
|
cNode.flags.type = TARGET_WHITE;
|
|
m_pTracker->validateTarget( &cNode, m_pHardwareManager->isMaster() );
|
|
CCNT++;
|
|
CCNT &= 7;
|
|
*/
|
|
// Generate Target
|
|
CNNT++;
|
|
targetRecord cNode;
|
|
cNode.begin_id = ((TH_SIZE_NODES>>1)+CCNT*(TH_LINE_NODES>>3)+CNNT)%TH_SIZE_NODES;
|
|
cNode.end_id = (cNode.begin_id+TH_LINE_NODES)%TH_SIZE_NODES;
|
|
cNode.status = 0;
|
|
cNode.flags.active = 1;
|
|
cNode.flags.lrf = CCNT;
|
|
cNode.flags.w_cnt = 2;
|
|
cNode.width = ((4095>>3)*(8-CCNT))<<1;
|
|
cNode.flags.type = TARGET_WHITE;
|
|
m_pTracker->validateTarget( &cNode, m_pHardwareManager->isMaster() );
|
|
CCNT++;
|
|
CCNT &= 7;
|
|
};
|
|
#endif
|
|
|
|
if( !m_pHardwareManager->validateCores() ){
|
|
if( getWM() != WM_ERROR ){
|
|
setWM(WM_ERROR);
|
|
LOG_ERROR("PRU Core Trouble Detected!");
|
|
};
|
|
} else {
|
|
// PRU Cores are valid
|
|
bActiveTH = m_nKernelFilter & m_pHardwareManager->getDetectionStatus();
|
|
bMeasuredTH = m_nKernelFilter & m_pHardwareManager->getTHVisual();
|
|
switch( getWM() ) {
|
|
case WM_INIT:{
|
|
LOG("INIT");
|
|
if( m_pHardwareManager->getCoreStatus( tStatus ) ){
|
|
if( m_pHardwareManager->reset( false ) ){
|
|
//if( tStatus.mode == WM_DETECTOR ){
|
|
LOG("**********SWITCHING TO STUDY MODE**********");
|
|
|
|
setWM( WM_STUDY );
|
|
//};
|
|
};
|
|
};
|
|
break;
|
|
};
|
|
case WM_STUDY:{
|
|
if( bActiveTH ){
|
|
LOG("**********SWITCHING TO DETECTOR MODE**********");
|
|
setWM( WM_DETECTOR );
|
|
} else {
|
|
if( bMeasuredTH != bDrawedTH ){
|
|
LOG("**********SWITCHING TO BORDER MODE**********");
|
|
setWM( WM_BORDER );
|
|
};
|
|
};
|
|
break;
|
|
};
|
|
case WM_DETECTOR:{
|
|
bActiveTH = m_nKernelFilter & m_pHardwareManager->getTHVisual(); //m_pHardwareManager->getDetectionStatus();
|
|
if( bMeasuredTH != bDrawedTH ) {
|
|
LOG("**********SWITCHING TO BORDER MODE**********");
|
|
setWM( WM_BORDER );
|
|
continue;
|
|
};
|
|
volatile targetRecord *tNode = (volatile targetRecord *)m_pHardwareManager->getEventMemory();
|
|
if( tNode != NULL ) {
|
|
bool bActiveRecordsPresent = true;
|
|
while( bActiveRecordsPresent && ( m_bThreadStatus < TH_ENDED ) ) {
|
|
targetRecord cNode;
|
|
uint32_t cRange;
|
|
uint32_t cTrackID = 0;
|
|
EVENT_CONTAINER * tEvt = NULL;
|
|
uint8_t bFilter = m_nKernelFilter & m_pHardwareManager->getTargetFilter();
|
|
|
|
bActiveRecordsPresent = false;
|
|
for( int idx = 0; idx < MAX_TARGETS_COUNT; idx++ ){
|
|
// Filter Not Active Events
|
|
if ( tNode[idx].flags.active == 0 ) continue;
|
|
|
|
cNode.width = tNode[idx].width;
|
|
cNode.begin_id = tNode[idx].begin_id;
|
|
cNode.end_id = tNode[idx].end_id;
|
|
cNode.status = tNode[idx].status;
|
|
// Unlock record for PRU
|
|
tNode[idx].status = 0;
|
|
// Filter Events from Disabled Kernels -> goes to flip
|
|
//if( ((1 << cNode.flags.lrf ) & m_nKernelFilter ) == 0 ){
|
|
// // Abort checking this target since Kernel Disabled
|
|
// continue;
|
|
//};
|
|
// Filter Events from Disabled Kernels -> goes to flip
|
|
if( ((1 << cNode.flags.lrf ) & bFilter ) == 0 ){
|
|
// Abort checking this target since Kernel not Detecting
|
|
continue;
|
|
};
|
|
// Abort checking this target since no width
|
|
if( cNode.flags.w_cnt ){
|
|
cRange = cNode.width / cNode.flags.w_cnt;
|
|
#ifdef SHIFT_50
|
|
cRange += SHIFT_50;
|
|
cNode.width = cRange * cNode.flags.w_cnt;
|
|
#endif
|
|
} else continue;
|
|
|
|
bActiveRecordsPresent = true;
|
|
switch( cNode.flags.type ){
|
|
case TARGET_WHITE:{
|
|
if( m_pShadow != NULL ) {
|
|
if( m_pShadow->isActive() ) {
|
|
uint16_t cPointX, cPointY;
|
|
#ifdef PRU_SLAVE
|
|
makeXY( cNode.begin_id, cPointX, cPointY, false );
|
|
#else
|
|
makeXY( cNode.begin_id, cPointX, cPointY, true );
|
|
#endif
|
|
if( m_pShadow->isOutside( cPointX, cPointY )){
|
|
#ifdef PRU_SLAVE
|
|
m_pTWIC->validateTarget( &cNode );
|
|
#else
|
|
cTrackID = m_pTracker->validateTarget( &cNode, true );
|
|
#endif
|
|
};
|
|
} else { // Shadow is Disabled
|
|
#ifdef PRU_SLAVE
|
|
m_pTWIC->validateTarget( &cNode );
|
|
#else
|
|
cTrackID = m_pTracker->validateTarget( &cNode, true );
|
|
#endif
|
|
};
|
|
} else {
|
|
LOG_ERROR("NO SHADOW OBJECT!");
|
|
m_bThreadStatus = TH_ENDED;
|
|
continue;
|
|
};
|
|
break;
|
|
};
|
|
case TARGET_BLACK:{
|
|
#ifdef NO_BLACK_TARGETS
|
|
break;
|
|
};
|
|
#else
|
|
if( m_pShadow != NULL ) {
|
|
if( m_pShadow->isActive() ) {
|
|
uint16_t cPointX, cPointY;
|
|
#ifdef PRU_SLAVE
|
|
makeXY( cNode.begin_id, cPointX, cPointY, false );
|
|
#else
|
|
makeXY( cNode.begin_id, cPointX, cPointY, true );
|
|
#endif
|
|
if( m_pShadow->isOutside( cPointX, cPointY )){
|
|
#ifdef PRU_SLAVE
|
|
m_pTWIC->validateTarget( &cNode );
|
|
#else
|
|
cTrackID = m_pTracker->validateTarget( &cNode, true );
|
|
#endif
|
|
};
|
|
} else {
|
|
#ifdef PRU_SLAVE
|
|
m_pTWIC->validateTarget( &cNode );
|
|
#else
|
|
cTrackID = m_pTracker->validateTarget( &cNode, true );
|
|
#endif
|
|
};
|
|
} else {
|
|
LOG_ERROR("NO SHADOW OBJECT!");
|
|
m_bThreadStatus = TH_ENDED;
|
|
continue;
|
|
};
|
|
break;
|
|
};
|
|
|
|
#endif // NO_BLACK_TARGETS
|
|
// nohup sleep 30 & echo $! > pidfile
|
|
#ifndef IGNORE_PRU_SCAN
|
|
#ifndef PRU_SLAVE
|
|
case SCAN_COMPLETE: m_pTracker->ScanComplete(); break;
|
|
case SCAN_FROZEN: m_pTracker->ScanComplete(); break;
|
|
case MIRROR_FINISHED: m_pTracker->MirrorComplete(); break;
|
|
#endif
|
|
#endif //!IGNORE_PRU_SCAN
|
|
#ifndef IGNORE_PRU_FLIP
|
|
case FLIP_COMPLETE:{
|
|
CLIENT_EVENT *tData;
|
|
tData = (CLIENT_EVENT *) malloc(sizeof(CLIENT_EVENT));
|
|
m_nFlipCounter = cNode.begin_id;
|
|
if( tData!= NULL ) {
|
|
long long tMS = getMilliseconds();
|
|
tData->uts = tMS / 1000;
|
|
tData->ums = tMS % 1000;
|
|
tData->type = EVT_FLIP;
|
|
tData->a_th_node = cNode.width;
|
|
tData->th_id = m_nFlipCounter;
|
|
tEvt = createBasicEvent( 0, EVT_CLASS_SENSOR_3D, EVT_FLIP, NULL, NULL, tData );
|
|
} else {
|
|
tEvt = createBasicEvent( 0, EVT_CLASS_SENSOR_3D, EVT_FLIP, NULL, NULL, NULL );
|
|
};
|
|
break;
|
|
};
|
|
#endif //!IGNORE_PRU_FLIP
|
|
|
|
#ifndef IGNORE_ERROR_ANGLE
|
|
case ERROR_ANGLE: tEvt = createBasicEvent( 0, EVT_CLASS_SENSOR_3D, EVT_ERROR, NULL, NULL, NULL ); break;
|
|
#endif
|
|
};
|
|
if( tEvt != NULL) {
|
|
m_pEventHeap->broadcast( tEvt );
|
|
// Evt utilisation done by EventHeap thread
|
|
tEvt = NULL;
|
|
};
|
|
};
|
|
};
|
|
} else {
|
|
LOG_ERROR("No access to Event Memory");
|
|
m_bThreadStatus = TH_ENDED;
|
|
continue;
|
|
};
|
|
break;
|
|
};
|
|
case WM_BORDER:{
|
|
if( ( bMeasuredTH == 0 ) && ( m_nForcedDrawTH == 0 ) ){
|
|
LOG("No Measured No Force!");
|
|
bDrawedTH = bMeasuredTH;
|
|
if( bActiveTH ) {
|
|
LOG("**********SWITCHING TO DETECTOR MODE**********");
|
|
setWM( WM_DETECTOR );
|
|
} else {
|
|
LOG("**********SWITCHING TO STUDY MODE**********");
|
|
setWM( WM_STUDY );
|
|
};
|
|
} else {
|
|
if ( m_nForcedDrawTH ) {
|
|
LOG("Forced DRAW");
|
|
if( ( m_nForcedDrawTH & 0xFF00 ) != 0x00000 ) { // Force Remote TH Draw
|
|
#ifndef PRU_SLAVE
|
|
uint8_t chVal = ( m_nForcedDrawTH >> 8 ) & 0xFF;
|
|
// Force Local TH Draw
|
|
for ( uint8_t idx = 0; idx < 16; idx++ ) {
|
|
if( ( chVal & (1<<idx) ) != 0 ) {
|
|
if( m_pTWIC ){
|
|
if( m_pTWIC->requestRemoteDraw( idx ) ){
|
|
m_nForcedDrawTH &=~(1<<(idx+8)); // Clear Flag
|
|
break; // from for
|
|
};
|
|
};
|
|
};
|
|
};
|
|
#endif
|
|
} else {
|
|
// Force Local TH Draw
|
|
for ( uint8_t idx = 0; idx < 16; idx++ ) {
|
|
if( ( m_nForcedDrawTH & (1<<idx) ) != 0 ) {
|
|
if ( m_pTWIC->readyToConsumeTH() ) {
|
|
//Finished Build TH
|
|
uint32_t THsize;
|
|
thresholdNode *tPTR = getTreshold( THsize, idx );
|
|
if ( m_pTWIC->consumeTH( idx, tPTR, THsize ) ) {
|
|
m_nForcedDrawTH &=~(1<<idx); // Clear Flag
|
|
break; // from for
|
|
} else LOG("Error Consuming TH");
|
|
} else LOG("TWINC not ready to consume TH");
|
|
};
|
|
};
|
|
};
|
|
} else {
|
|
LOG("Measured!");
|
|
if( bDrawedTH != bMeasuredTH ) {
|
|
for( uint8_t idx = 0; idx < 8; idx++ ){
|
|
if( ( ( bMeasuredTH & (1<<idx) ) != 0 ) && ( ( bDrawedTH & (1<<idx) ) == 0 ) ){
|
|
sprintf( Buffer, "NEW [%d]TH READY!", idx );
|
|
LOG( Buffer );
|
|
if( m_pTWIC ){
|
|
if ( m_pTWIC->readyToConsumeTH() ) {
|
|
//Finished Build TH
|
|
uint32_t THsize;
|
|
thresholdNode *tPTR = getTreshold( THsize, idx );
|
|
if ( m_pTWIC->consumeTH( idx, tPTR, THsize ) ) {
|
|
//mark as consumed
|
|
bDrawedTH |= (1<<idx);
|
|
} else LOG("Error Consuming TH");
|
|
} else {
|
|
LOG("TWINC not ready to consume TH");
|
|
SLEEP(10);
|
|
break;
|
|
}
|
|
} else LOG("TWINC not exists!");
|
|
} else {
|
|
// If Th is cleared - remove old flag to consume newly build TH
|
|
if( ( bMeasuredTH & (1<<idx) ) == 0 ) bDrawedTH &= ~(1<<idx);
|
|
};
|
|
};
|
|
//bDrawedTH = bMeasuredTH;
|
|
};
|
|
};
|
|
if( ( m_nForcedDrawTH == 0 ) && ( bDrawedTH == bMeasuredTH ) ) {
|
|
if( bActiveTH ) {
|
|
LOG("**********SWITCHING TO DETECTOR MODE**********");
|
|
setWM( WM_DETECTOR );
|
|
} else {
|
|
LOG("**********SWITCHING TO STUDY MODE**********");
|
|
setWM( WM_STUDY );
|
|
};
|
|
}
|
|
};
|
|
break;
|
|
};
|
|
case WM_ERROR:{
|
|
if( m_pHardwareManager->validateCores() ){
|
|
LOG("**********SWITCHING TO INIT MODE**********");
|
|
setWM( WM_INIT );
|
|
}
|
|
break;
|
|
};
|
|
};
|
|
};
|
|
|
|
#ifdef ONPC_TOTTAL_SIMULATOR
|
|
//Simulation Stuff
|
|
//bActiveTH = 199;
|
|
#endif
|
|
SLEEP( SLEEP_KERNEL_INTERVAL );
|
|
|
|
if(( m_lCycleCounter >> 31 )!= 0 ) {
|
|
m_lCycleCounter = 0;
|
|
} else m_lCycleCounter++;
|
|
};
|
|
};
|
|
|
|
PLGS_3D_PARAMETERS LRF_Kernel::getDetectionParameters( void ){
|
|
PLGS_3D_PARAMETERS tParam = NULL;
|
|
if( m_pHardwareManager != NULL ){
|
|
tParam = (PLGS_3D_PARAMETERS) malloc(sizeof(LGS_3D_PARAMETERS));
|
|
if( tParam != NULL ){
|
|
memset( tParam, 0 , sizeof(LGS_3D_PARAMETERS));
|
|
/*TH_PARAMS tThParams;
|
|
if( m_pHardwareManager->getDetectionParams( tThParams ) ){
|
|
tParam->flags.BuildThByCore = tThParams.self_build_ena;
|
|
tParam->bSST = m_nSST;
|
|
tParam->nDelta = tThParams.delta;
|
|
tParam->nFlip = tThParams.flip_size;
|
|
tParam->nGPointSamples = tThParams.good_points;
|
|
tParam->nOffset = tThParams.offset;
|
|
tParam->nOrder = tThParams.order;
|
|
tParam->nSun = 0;
|
|
tParam->nWidth = m_nWidth;
|
|
};
|
|
//DT_STATUS tThStatus;
|
|
m_pHardwareManager->getHardwareParams( tParam->flags );
|
|
|
|
//if( m_pHardwareManager->getDetectionStatus( tThStatus ) ) {
|
|
// tParam->flags.LaserPulsesOn = 1;
|
|
//};
|
|
*/
|
|
if( !m_pHardwareManager->getParams( *tParam ) ) {
|
|
LOG_ERROR("getParams ERROR!");
|
|
free( tParam );
|
|
tParam = NULL;
|
|
}
|
|
#ifdef DEBUG
|
|
else {
|
|
char tBuf[123];
|
|
for( int i = 0; i < sizeof(HW_PARAMS); i++ ) {
|
|
sprintf( tBuf, "FLAGS_BIN[%d]: %d", i, tParam->flags_binary[i] );
|
|
LOG( tBuf );
|
|
};
|
|
sprintf( tBuf, "Sunbit: %d", tParam->flags.Sunbit_filter );
|
|
LOG( tBuf );
|
|
sprintf( tBuf, "CAN My ID: %d", tParam->flags.TGZ_canSRC );
|
|
LOG( tBuf );
|
|
sprintf( tBuf, "CAN TGZ ID: %d", tParam->flags.TGZ_canDST );
|
|
LOG( tBuf );
|
|
}
|
|
#endif
|
|
;
|
|
};
|
|
};
|
|
return tParam;
|
|
};
|
|
|
|
bool LRF_Kernel::setDetectionParameters( PLGS_3D_PARAMETERS ptr ){
|
|
bool isCompromised = false;
|
|
HW_PARAMS tHwParams;
|
|
TH_PARAMS tThParams;
|
|
if( m_pHardwareManager != NULL ){
|
|
if( m_pHardwareManager->getDetectionParams(tThParams)){
|
|
if( tThParams.delta != ptr->nDelta ){
|
|
tThParams.delta = ptr->nDelta; isCompromised = true;
|
|
};
|
|
if( tThParams.flip_size != ptr->nFlip ){
|
|
tThParams.flip_size = ptr->nFlip; isCompromised = true;
|
|
};
|
|
if( tThParams.good_points != ptr->nGPointSamples ){
|
|
tThParams.good_points = ptr->nGPointSamples; isCompromised = true;
|
|
};
|
|
if( tThParams.offset != ptr->nOffset ){
|
|
tThParams.offset = ptr->nOffset; isCompromised = true;
|
|
};
|
|
if( tThParams.order != ptr->nOrder ){
|
|
tThParams.order = ptr->nOrder; isCompromised = true;
|
|
};
|
|
if( tThParams.self_build_ena != ptr->flags.BuildThByCore ){
|
|
tThParams.self_build_ena = ptr->flags.BuildThByCore; isCompromised = true;
|
|
};
|
|
if( tThParams.master != ptr->flags.isMaster ) {
|
|
tThParams.master = ptr->flags.isMaster; isCompromised = true;
|
|
};
|
|
if( isCompromised ) m_pHardwareManager->setDetectionParams( &tThParams );
|
|
} else {
|
|
LOG_ERROR("Cannot Get Actual Detection Settings!");
|
|
return false;
|
|
};
|
|
isCompromised = false;
|
|
if( m_pHardwareManager->getHardwareParams( tHwParams ) ) {
|
|
if( tHwParams.AutostartMotors != ptr->flags.AutostartMotors ){
|
|
tHwParams.AutostartMotors = ptr->flags.AutostartMotors; isCompromised = true;
|
|
};
|
|
if( tHwParams.BaseScanCount != ptr->flags.BaseScanCount ){
|
|
tHwParams.BaseScanCount = ptr->flags.BaseScanCount; isCompromised = true;
|
|
};
|
|
if( tHwParams.BBinterlace != ptr->flags.BBinterlace ){
|
|
tHwParams.BBinterlace = ptr->flags.BBinterlace; isCompromised = true;
|
|
};
|
|
if( tHwParams.BuildThByCore != ptr->flags.BuildThByCore ){
|
|
tHwParams.BuildThByCore = ptr->flags.BuildThByCore; isCompromised = true;
|
|
};
|
|
if( tHwParams.DetectorsCount != ptr->flags.DetectorsCount ){
|
|
tHwParams.DetectorsCount = ptr->flags.DetectorsCount; isCompromised = true;
|
|
};
|
|
if( tHwParams.DryContactMode1 != ptr->flags.DryContactMode1 ){
|
|
tHwParams.DryContactMode1 = ptr->flags.DryContactMode1; isCompromised = true;
|
|
};
|
|
if( tHwParams.DryContactMode2 != ptr->flags.DryContactMode2 ){
|
|
tHwParams.DryContactMode2 = ptr->flags.DryContactMode2; isCompromised = true;
|
|
};
|
|
if( tHwParams.DryContactStatus1 != ptr->flags.DryContactStatus1 ){
|
|
tHwParams.DryContactStatus1 = ptr->flags.DryContactStatus1; isCompromised = true;
|
|
};
|
|
if( tHwParams.DryContactStatus2 != ptr->flags.DryContactStatus2 ){
|
|
tHwParams.DryContactStatus2 = ptr->flags.DryContactStatus2; isCompromised = true;
|
|
};
|
|
if( tHwParams.HorizontalFreq != ptr->flags.HorizontalFreq ){
|
|
tHwParams.HorizontalFreq = ptr->flags.HorizontalFreq; isCompromised = true;
|
|
};
|
|
if( tHwParams.HorizontalMotorOn != ptr->flags.HorizontalMotorOn ){
|
|
tHwParams.HorizontalMotorOn = ptr->flags.HorizontalMotorOn; isCompromised = true;
|
|
};
|
|
if( tHwParams.LaserPulsesOn != ptr->flags.LaserPulsesOn ){
|
|
tHwParams.LaserPulsesOn = ptr->flags.LaserPulsesOn; isCompromised = true;
|
|
};
|
|
if( tHwParams.MirrorScanCount != ptr->flags.MirrorScanCount ){
|
|
tHwParams.MirrorScanCount = ptr->flags.MirrorScanCount; isCompromised = true;
|
|
};
|
|
if( tHwParams.QQinterlace != ptr->flags.QQinterlace ){
|
|
tHwParams.QQinterlace = ptr->flags.QQinterlace; isCompromised = true;
|
|
};
|
|
if( isCompromised ) m_pHardwareManager->setHardwareParams( &tHwParams );
|
|
} else {
|
|
LOG_ERROR("Cannot Get Actual Hardware Settings!");
|
|
return false;
|
|
};
|
|
return true;
|
|
};
|
|
return false;
|
|
};
|
|
// KIID 0-7 Master Study 8-15 - Master Work - 16-23 - Slave Study 24-31 - Slave Work
|
|
thresholdNode *LRF_Kernel::getTreshold( uint32_t &size, uint8_t kID ){
|
|
thresholdNode * retVal = 0;
|
|
//if ( kID < 16 ) { // Local TH
|
|
#ifdef TH_PER_MIRROR
|
|
kID = (kID>39)?39:kID;
|
|
#else
|
|
kID = (kID>15)?15:kID;
|
|
#endif
|
|
|
|
#ifdef ONPC_TOTTAL_SIMULATOR
|
|
if( m_pCurrentTH == NULL ) {
|
|
m_pCurrentTH = (thresholdNode *) malloc( TH_SIZE_BYTES );
|
|
};
|
|
if( m_pCurrentTH != NULL ) {
|
|
char fName[64];
|
|
sprintf( fName, "sim_data_%d.raw", kID );
|
|
if( fileExist( fName ) ) {
|
|
FILE *fd = fopen( fName, "rb" );
|
|
if( fd != NULL ) {
|
|
uint32_t Size = TH_SIZE_NODES;
|
|
fread( &Size, sizeof( uint32_t ), 1, fd );
|
|
fread( m_pCurrentTH, sizeof( thresholdNode ), Size, fd );
|
|
fclose( fd );
|
|
};
|
|
} else {
|
|
for (int i = 0; i < TH_SIZE_NODES; i++) {
|
|
m_pCurrentTH[i].values.min = i;
|
|
m_pCurrentTH[i].values.max = i;
|
|
m_pCurrentTH[i].values.cnt = 1;
|
|
m_pCurrentTH[i].values.sbit = 0;
|
|
};
|
|
}
|
|
m_bCurrentTH = kID;
|
|
size = TH_SIZE_NODES;
|
|
return m_pCurrentTH;
|
|
};
|
|
#else
|
|
if( m_pCurrentTH != NULL ) {
|
|
if ( m_bCurrentTH == kID ) {
|
|
size = TH_SIZE_NODES;
|
|
return m_pCurrentTH;
|
|
} else {
|
|
if( m_pHardwareManager != NULL ) {
|
|
thresholdNode *ptr = m_pHardwareManager->getWorkingMemory();
|
|
if( ptr!= NULL ){
|
|
memcpy( m_pCurrentTH, ptr+(kID*TH_SIZE_NODES), TH_SIZE_BYTES );
|
|
m_bCurrentTH = kID;
|
|
size = TH_SIZE_NODES;
|
|
return m_pCurrentTH;
|
|
} else {
|
|
size = 0;
|
|
return NULL;
|
|
};
|
|
};
|
|
};
|
|
} else {
|
|
if( m_pHardwareManager != NULL ){
|
|
thresholdNode *ptr = m_pHardwareManager->getWorkingMemory();
|
|
if( ptr!= NULL ){
|
|
m_pCurrentTH = (thresholdNode *) malloc( TH_SIZE_BYTES );
|
|
if( m_pCurrentTH != NULL ) {
|
|
memcpy( m_pCurrentTH, ptr+(kID*TH_SIZE_NODES), TH_SIZE_BYTES );
|
|
m_bCurrentTH = kID;
|
|
size = TH_SIZE_NODES;
|
|
return m_pCurrentTH;
|
|
};
|
|
};
|
|
};
|
|
};
|
|
#endif
|
|
size = 0;
|
|
return NULL;
|
|
};
|
|
|
|
bool LRF_Kernel::reset( bool FullRestart ){
|
|
if( FullRestart ) {
|
|
//Shutting down Motors and Hardware
|
|
//Starting up Motors and Hardware
|
|
shutdown( true );
|
|
} else {
|
|
m_pHardwareManager->reset( true );
|
|
};
|
|
return true;
|
|
};
|
|
|
|
bool LRF_Kernel::shutdown( bool Restart ){
|
|
if( Restart ) {
|
|
LOG("Restarting the Kernel by SHUTDOWN!");
|
|
m_bExitCause = 1;
|
|
} else {
|
|
LOG("Shutting down the Kernel by SHUTDOWN!");
|
|
m_bExitCause = 0;
|
|
}
|
|
setStatus( TH_ENDED );
|
|
return true;
|
|
};
|
|
|
|
void LRF_Kernel::forceDrawTH( uint16_t kMASK ){
|
|
m_nForcedDrawTH |= kMASK;
|
|
};
|
|
|
|
#ifndef PRU_SLAVE //Just Master Stuff
|
|
networkClientHandler *LRF_Kernel::attachNewClient( int SocketID ){
|
|
LOG("Creating Client Handler!");
|
|
LRF_ClientHandler *tal;
|
|
tal = new LRF_ClientHandler( SocketID, this );
|
|
if( tal!= NULL ) {
|
|
LOG("Client Handler Created!");
|
|
};
|
|
return (networkClientHandler *) tal;
|
|
};
|
|
|
|
bool LRF_Kernel::detachClient( networkClientHandler *ptr ){
|
|
LOG("Delete Client Handler!");
|
|
if( ptr == NULL ) return false;
|
|
delete( (LRF_ClientHandler *) ptr );
|
|
return true;
|
|
};
|
|
|
|
bool LRF_Kernel::stopMotors( void ){
|
|
#ifndef ONPC_TOTTAL_SIMULATOR
|
|
if( m_pHardwareManager!= NULL ) {
|
|
return m_pHardwareManager->stopMotors( false );
|
|
};
|
|
return false;
|
|
#else
|
|
return true;
|
|
#endif
|
|
};
|
|
|
|
bool LRF_Kernel::startMotors( void ){
|
|
#ifndef ONPC_TOTTAL_SIMULATOR
|
|
if( m_pHardwareManager!= NULL ) {
|
|
return m_pHardwareManager->startMotors( false );
|
|
};
|
|
return false;
|
|
#else
|
|
return true;
|
|
#endif
|
|
};
|
|
bool LRF_Kernel::getRuntimeStatus( ALG_RUN_STATUS *ptr ){
|
|
if( ptr == NULL ) return false;
|
|
memset( ptr, 0, sizeof( ALG_RUN_STATUS ));
|
|
ptr->ENABLED_LRF = m_nKernelFilter;
|
|
if( m_pHardwareManager ) {
|
|
HWM_CSTATUS tHWS;
|
|
if( m_pHardwareManager->getRunTimeStat( &tHWS ) ){
|
|
ptr->HW_STATUS = tHWS.HW_STATUS;
|
|
ptr->DETECTING_LRF = tHWS._DETECTING_LRF;
|
|
ptr->ENABLED_LRF = m_nKernelFilter;
|
|
ptr->MASTER_FLIP_CNT = tHWS._FLIP_CNT;
|
|
memcpy( ptr->MASTER_MP, tHWS._MP, sizeof(uint32_t) * 8 );
|
|
memcpy( ptr->MASTER_GP, tHWS._GP, sizeof(uint32_t) * 8 );
|
|
memcpy( ptr->MASTER_WP, tHWS._WP, sizeof(uint32_t) * 8 );
|
|
};
|
|
} else return false;
|
|
if( m_pTWIC ) {
|
|
TWIN_STATUS * tPPP = m_pTWIC->getSystemStatus();
|
|
if (tPPP->VERSION_IS_SYNCED ) ptr->TWINC_STATUS = 1;
|
|
if (tPPP->SETTINGS_IS_SYNCED ) ptr->TWINC_STATUS |= 2;
|
|
if (tPPP->SHADOW_IS_SYNCED ) ptr->TWINC_STATUS |= 4;
|
|
} else return false;
|
|
return true;
|
|
};
|
|
#endif
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|