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.
971 lines
31 KiB
971 lines
31 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_Kernel.h"
|
|
#include "LRF_Detector.h"
|
|
#include "LRF_ClientHandler.h"
|
|
|
|
#define LOG( X ) m_pLog->Log( "Kernel", 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( "Kernel", this, _LOG_ERROR, _tBfrErrMSG234 );\
|
|
}
|
|
#else
|
|
#define LOG_ERROR( X ) m_pLog->Log( "Kernel", this, _LOG_ERROR, X );
|
|
#endif
|
|
|
|
// Constructor implementation
|
|
LRF_Kernel::LRF_Kernel( LRF_HardwareManager *HM ): basicThread(), networkServiceEngine(){
|
|
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;
|
|
|
|
#ifdef __EU_PROJECT__
|
|
m_pSafeShoreHandler = NULL;
|
|
m_bSafeShoreSupport = false;
|
|
m_pDefenderHandler = NULL;
|
|
m_bDefenderSupport = false;
|
|
m_pKafkaHandler = NULL;
|
|
m_bKafkaSupport = false;
|
|
#endif
|
|
#ifdef PRU_SLAVE
|
|
#else
|
|
m_pTracker = NULL;
|
|
m_pNetClientsServer = NULL;
|
|
m_nSST = 0;
|
|
#endif
|
|
m_pThreshold = NULL;
|
|
m_eWorkMode = WM_NONE;
|
|
m_nWidth = 800;
|
|
};
|
|
|
|
// Destructor implementation
|
|
LRF_Kernel::~LRF_Kernel(){
|
|
#ifdef DEBUG
|
|
LOG("Destructor");
|
|
#endif
|
|
WaitFinish(true);
|
|
#ifdef DEBUG
|
|
LOG("Deleting Components");
|
|
#endif
|
|
|
|
#ifdef __EU_PROJECT__
|
|
if( m_pSafeShoreHandler ) delete( m_pSafeShoreHandler );
|
|
#ifdef DEBUG
|
|
LOG("Safe Shore - Done");
|
|
#endif
|
|
if( m_pDefenderHandler ) delete( m_pDefenderHandler );
|
|
#ifdef DEBUG
|
|
LOG("Defender - Done");
|
|
#endif
|
|
if( m_pKafkaHandler ) delete( m_pKafkaHandler );
|
|
#ifdef DEBUG
|
|
LOG("KAFKA - Done");
|
|
#endif
|
|
#endif
|
|
if( m_pTWIC ) delete( m_pTWIC );
|
|
#ifdef DEBUG
|
|
LOG("Twin Connector - Done");
|
|
#endif
|
|
if( m_pShadow ) delete( m_pShadow );
|
|
#ifdef DEBUG
|
|
LOG("Shadow Mask - Done");
|
|
#endif
|
|
#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( (networkServiceEngine *)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
|
|
#ifdef __EU_PROJECT__
|
|
if( !config_setting_lookup_bool( m_pSettings, KNL_SETTINGS_SAFE_SHORE, &tVal ) ){
|
|
tVal = m_bSafeShoreSupport = true;
|
|
pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_SAFE_SHORE, CONFIG_TYPE_BOOL );
|
|
config_setting_set_bool( pSetting, tVal );
|
|
m_pSettingsProvider->compromise();
|
|
LOG("Safe-Shore support not defined");
|
|
} else {
|
|
m_bSafeShoreSupport = (bool)tVal;
|
|
sprintf( tBuffer, "Safe-Shore support Read From Settings: %d", tVal );
|
|
LOG( tBuffer );
|
|
};
|
|
if( !config_setting_lookup_bool( m_pSettings, KNL_SETTINGS_DEFENDER, &tVal ) ){
|
|
tVal = m_bDefenderSupport = true;
|
|
pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_DEFENDER, CONFIG_TYPE_BOOL );
|
|
config_setting_set_bool( pSetting, tVal );
|
|
m_pSettingsProvider->compromise();
|
|
LOG("Defender support not defined");
|
|
} else {
|
|
m_bDefenderSupport = (bool)tVal;
|
|
sprintf( tBuffer, "Defender support Read From Settings: %d", tVal );
|
|
LOG( tBuffer );
|
|
};
|
|
if( !config_setting_lookup_bool( m_pSettings, KNL_SETTINGS_KAFKA, &tVal ) ){
|
|
tVal = m_bKafkaSupport = true;
|
|
pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_KAFKA, CONFIG_TYPE_BOOL );
|
|
config_setting_set_bool( pSetting, tVal );
|
|
m_pSettingsProvider->compromise();
|
|
LOG("KAFKA support not defined");
|
|
} else {
|
|
m_bKafkaSupport = (bool)tVal;
|
|
sprintf( tBuffer, "KAFKA support Read From Settings: %d", tVal );
|
|
LOG( tBuffer );
|
|
};
|
|
#endif
|
|
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_eWorkMode = 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;
|
|
};
|
|
|
|
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 __EU_PROJECT__
|
|
if( m_bSafeShoreSupport ){
|
|
m_pSafeShoreHandler = new LRF_SafeShoreHandler( m_pSettings );
|
|
if( m_pSafeShoreHandler == NULL ) {
|
|
LOG_ERROR("SafeShore Protocol failed!");
|
|
};
|
|
};
|
|
if( m_bDefenderSupport ) {
|
|
m_pDefenderHandler = new LRF_DefenderHandler( m_pSettings );
|
|
if( m_pDefenderHandler == NULL ) {
|
|
LOG_ERROR("Defender Protocol failed!");
|
|
};
|
|
};
|
|
if( m_bKafkaSupport ) {
|
|
m_pKafkaHandler = new LRF_KafkaHandler( m_pSettings );
|
|
if( m_pKafkaHandler == NULL ) {
|
|
LOG_ERROR("Kafka Protocol failed!");
|
|
};
|
|
};
|
|
#endif
|
|
#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_eWorkMode = 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
|
|
};
|
|
|
|
/* Main Kernel Thread Procedure */
|
|
void LRF_Kernel::m_fThreadProc( void ){
|
|
#ifdef SIMULATE_TARGETS
|
|
uint8_t CCNT = 0;
|
|
uint16_t CNNT = 0;
|
|
#endif
|
|
char Buffer[256];
|
|
DT_STATUS tStatus = {0};
|
|
m_bThreadStatus = TH_WORKING;
|
|
while( m_bThreadStatus < TH_ENDED ){
|
|
if( !m_pHardwareManager->validateCores() ){
|
|
//Handle Error in PRU Cores
|
|
LOG_ERROR("PRU Core Trouble Detected!");
|
|
m_eWorkMode = WM_ERROR;
|
|
while( ( m_bThreadStatus < TH_ENDED ) && ( !m_pHardwareManager->validateCores()) ) SLEEP( 100 ); //Stop Kernel
|
|
#ifdef DEBUG
|
|
LOG( "Cores Became OK!" );
|
|
#endif
|
|
m_eWorkMode = WM_INIT;
|
|
};
|
|
#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.width = ((4095>>3)*(8-CCNT))<<1;
|
|
cNode.flags.type = TARGET_WHITE;
|
|
m_pTracker->validateTarget( &cNode, m_pHardwareManager->isMaster() );
|
|
CCNT++;
|
|
CCNT &= 7;
|
|
};
|
|
#endif
|
|
// Check Working Mode
|
|
if( tStatus.mode != WM_DETECTOR ){
|
|
//if( m_eWorkMode != WM_DETECTOR ){
|
|
if( m_pHardwareManager->getCoreStatus( tStatus ) ){
|
|
if( tStatus.mode == WM_DETECTOR ) {
|
|
// Detection Mode
|
|
m_eWorkMode = WM_DETECTOR;
|
|
#ifdef DEBUG
|
|
LOG( "Switching to detection mode!" );
|
|
#ifdef NO_BLACK_TARGETS
|
|
LOG( "B-Target Will be Ignored by Build" );
|
|
#endif
|
|
#endif
|
|
#ifdef __EU_PROJECT__
|
|
if( m_bSafeShoreSupport ){
|
|
if( m_pSafeShoreHandler != NULL ) {
|
|
m_pSafeShoreHandler->SetStatus( true );
|
|
};
|
|
};
|
|
if( m_bDefenderSupport ) {
|
|
if( m_pDefenderHandler != NULL ) {
|
|
m_pDefenderHandler->SetStatus( true );
|
|
};
|
|
};
|
|
if( m_bKafkaSupport ) {
|
|
if( m_pKafkaHandler != NULL ) {
|
|
m_pKafkaHandler->SetStatus( true );
|
|
};
|
|
};
|
|
#endif
|
|
thresholdNode *ptr;
|
|
ptr = m_pHardwareManager->getWorkingMemory();
|
|
if( ptr!= NULL ){
|
|
m_nThresholdSize = TH_SIZE_NODES;
|
|
m_pThreshold = (thresholdNode *) malloc( sizeof(thresholdNode)* m_nThresholdSize);
|
|
if( m_pThreshold!= NULL ) {
|
|
memcpy( m_pThreshold, ptr, m_nThresholdSize );
|
|
} else m_nThresholdSize = 0;
|
|
};
|
|
};
|
|
};
|
|
} else { //m_eWorkMode == WM_DETECTOR
|
|
// Readout Events
|
|
int index;
|
|
bool bActiveRecordsPresent;
|
|
targetRecord cNode;
|
|
volatile targetRecord *tNode;
|
|
tNode = (volatile targetRecord *) m_pHardwareManager->getEventMemory();
|
|
if( tNode != NULL ) {
|
|
//LOG( "Event Mem accessed OK");
|
|
EVENT_CONTAINER * tEvt = NULL;
|
|
uint32_t cRange;
|
|
uint32_t cTrackID = 0;
|
|
bActiveRecordsPresent = true;
|
|
while( bActiveRecordsPresent && ( m_bThreadStatus < TH_ENDED ) ) {
|
|
bActiveRecordsPresent = false;
|
|
for( index = 0; index < MAX_TARGETS_COUNT; index++){
|
|
// Copy record from MEM
|
|
//LOG("Checking Event Record");
|
|
cNode.width = tNode[index].width;
|
|
cNode.begin_id = tNode[index].begin_id;
|
|
cNode.end_id = tNode[index].end_id;
|
|
cNode.status = tNode[index].status;
|
|
// Unlock record for PRU
|
|
tNode[index].status = 0;
|
|
// Check by Working TH Complete for LRF
|
|
// if( cNode.flags.type == TARGET_WHITE || cNode.flags.type == TARGET_BLACK ) {
|
|
// // If not -> cancel analyze
|
|
// if((m_pHardwareManager->getTargetFilter() & ( 1 << cNode.flags.lrf )) == 0 ) continue;
|
|
// };
|
|
if( cNode.flags.active ){
|
|
// Filter Events from Disabled Kernels -> goes to flip
|
|
if( ((1 << cNode.flags.lrf ) & m_nKernelFilter ) == 0 ){
|
|
// Abort checking this target since Kernel Disabled
|
|
continue;
|
|
};
|
|
if( cNode.flags.w_cnt ){
|
|
cRange = cNode.width / cNode.flags.w_cnt;
|
|
#ifdef SHIFT_50
|
|
cRange += SHIFT_50;
|
|
// width must be updated accordingly
|
|
cNode.width = cRange * cNode.flags.w_cnt;
|
|
#endif
|
|
// Filter Target's Width in Da Tracker!
|
|
// if( m_nWidth > 0 ) {
|
|
// if( cNode.width < m_nWidth ) {
|
|
// Abort Detection of this target since small width
|
|
// continue;
|
|
// };
|
|
// };
|
|
};// else {
|
|
// Abort checking this target since no width
|
|
// continue;
|
|
//};
|
|
tEvt = NULL;
|
|
|
|
bActiveRecordsPresent = true;
|
|
|
|
switch( cNode.flags.type ){
|
|
case TARGET_WHITE:{
|
|
#ifndef SKIP_TARGET_LOG
|
|
#ifdef DEBUG
|
|
LOG( "Push W-Target Into Tracker" );
|
|
#endif
|
|
#endif
|
|
if( m_pShadow != NULL ) {
|
|
if( m_pShadow->isActive() ) {
|
|
uint16_t cPointX, cPointY;
|
|
makeXY( cNode.begin_id, cPointX, cPointY, m_pHardwareManager->isMaster() );
|
|
if( m_pShadow->isOutside( cPointX, cPointY )){
|
|
#ifdef PRU_SLAVE
|
|
#else
|
|
cTrackID = m_pTracker->validateTarget( &cNode, m_pHardwareManager->isMaster());
|
|
#endif
|
|
#ifndef SKIP_TARGET_LOG
|
|
#ifdef DEBUG
|
|
sprintf( Buffer, "Assigned ID [%d] area: %d CNT: %d bID: %d eID: %d",
|
|
cTrackID,
|
|
cNode.width,
|
|
cNode.flags.w_cnt,
|
|
cNode.begin_id,
|
|
cNode.end_id
|
|
);
|
|
LOG( Buffer );
|
|
#endif
|
|
#endif
|
|
} else {
|
|
#ifndef SKIP_TARGET_LOG
|
|
LOG( "Target Shadowed" );
|
|
#endif
|
|
};
|
|
} else { // Shadow is Disabled
|
|
#ifdef PRU_SLAVE
|
|
#else
|
|
cTrackID = m_pTracker->validateTarget( &cNode, m_pHardwareManager->isMaster());
|
|
#endif
|
|
#ifndef SKIP_TARGET_LOG
|
|
#ifdef DEBUG
|
|
sprintf( Buffer, "Assigned ID [%d] area: %d CNT: %d bID: %d eID: %d",
|
|
cTrackID,
|
|
cNode.width,
|
|
cNode.flags.w_cnt,
|
|
cNode.begin_id,
|
|
cNode.end_id
|
|
);
|
|
LOG( Buffer );
|
|
#endif
|
|
#endif
|
|
}
|
|
} else {
|
|
LOG_ERROR("NO SHADOW OBJECT!");
|
|
m_bThreadStatus = TH_ENDED;
|
|
continue;
|
|
}
|
|
break;
|
|
};
|
|
case TARGET_BLACK:{
|
|
#ifdef NO_BLACK_TARGETS
|
|
#ifndef SKIP_TARGET_LOG
|
|
#ifdef DEBUG
|
|
// LOG( "B-Target Ignored by Build" );
|
|
#endif
|
|
#endif
|
|
};
|
|
break;
|
|
#else
|
|
#ifndef SKIP_TARGET_LOG
|
|
#ifdef DEBUG
|
|
LOG( "Push B-Target Into Tracker" );
|
|
#endif
|
|
#endif
|
|
if( m_pShadow != NULL ) {
|
|
if( m_pShadow->isActive() ) {
|
|
|
|
uint16_t cPointX, cPointY;
|
|
makeXY( cNode.begin_id, cPointX, cPointY, m_pHardwareManager->isMaster() );
|
|
|
|
if( m_pShadow->isOutside( cPointX, cPointY )){
|
|
cTrackID = m_pTracker->validateTarget( &cNode, m_pHardwareManager->isMaster());
|
|
#ifndef SKIP_TARGET_LOG
|
|
#ifdef DEBUG
|
|
sprintf( Buffer, "Assigned ID [%d] area: %d CNT: %d bID: %d eID: %d",
|
|
cTrackID,
|
|
cNode.width,
|
|
cNode.flags.w_cnt,
|
|
cNode.begin_id,
|
|
cNode.end_id
|
|
);
|
|
LOG( Buffer );
|
|
#endif
|
|
#endif
|
|
};
|
|
} else { // Shadow is Disabled
|
|
cTrackID = m_pTracker->validateTarget( &cNode, m_pHardwareManager->isMaster());
|
|
#ifndef SKIP_TARGET_LOG
|
|
#ifdef DEBUG
|
|
sprintf( Buffer, "Assigned ID [%d] area: %d CNT: %d bID: %d eID: %d",
|
|
cTrackID,
|
|
cNode.width,
|
|
cNode.flags.w_cnt,
|
|
cNode.begin_id,
|
|
cNode.end_id
|
|
);
|
|
LOG( Buffer );
|
|
#endif
|
|
#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
|
|
case SCAN_COMPLETE:{
|
|
//CLIENT_EVENT *tData;
|
|
//tData = (CLIENT_EVENT *) malloc(sizeof(CLIENT_EVENT));
|
|
#ifndef PRU_SLAVE
|
|
m_pTracker->ScanComplete();
|
|
#endif
|
|
//sprintf( Buffer,"Scan Complete [%d,%d]", cNode.width, cNode.begin_id );
|
|
#ifdef DEBUG
|
|
//LOG( Buffer );
|
|
#endif
|
|
//if( tData!= NULL ) {
|
|
// long long tMS = getMilliseconds();
|
|
// tData->uts = tMS / 1000;
|
|
// tData->ums = tMS % 1000;
|
|
// tData->type = EVT_SCAN;
|
|
// tData->a_th_node = cNode.width;
|
|
// tData->th_id = cNode.begin_id;
|
|
// tEvt = createBasicEvent( 0, EVT_CLASS_SENSOR_3D, EVT_SCAN, NULL, Buffer, tData );
|
|
//} else {
|
|
// tEvt = createBasicEvent( 0, EVT_CLASS_SENSOR_3D, EVT_SCAN, NULL, 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
|
|
//tEvt = createBasicEvent( 0, EVT_CLASS_SENSOR_3D, EVT_SCAN, Buffer, NULL );
|
|
break;
|
|
#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;
|
|
sprintf( Buffer,"Flip Complete [%d,%d]", cNode.width, m_nFlipCounter );
|
|
#ifdef DEBUG
|
|
LOG( Buffer );
|
|
#endif
|
|
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, Buffer, tData );
|
|
} else {
|
|
tEvt = createBasicEvent( 0, EVT_CLASS_SENSOR_3D, EVT_FLIP, NULL, Buffer, NULL );
|
|
};
|
|
break;
|
|
};
|
|
#endif //!IGNORE_PRU_FLIP
|
|
|
|
#ifndef PRU_SLAVE
|
|
case MIRROR_FINISHED:
|
|
m_pTracker->MirrorComplete();
|
|
break;
|
|
#endif
|
|
|
|
#ifndef IGNORE_ERROR_ANGLE
|
|
case ERROR_ANGLE:
|
|
sprintf( Buffer,"Error Angle [%d,%d]", cNode.width, cNode.begin_id );
|
|
#ifdef DEBUG
|
|
LOG_ERROR( Buffer );
|
|
#endif
|
|
tEvt = createBasicEvent( 0, EVT_CLASS_SENSOR_3D, EVT_ERROR, Buffer, NULL );
|
|
break;
|
|
#endif
|
|
};
|
|
if( tEvt != NULL) {
|
|
m_pEventHeap->broadcast( tEvt );
|
|
// Evt utilisation done by EventHeap thread
|
|
tEvt = NULL;
|
|
};// else { LOG("No Event Created!" ); };
|
|
};
|
|
};
|
|
};
|
|
//LOG("End Event Check");
|
|
} else {
|
|
LOG_ERROR("No access to Event Memory");
|
|
m_bThreadStatus = TH_ENDED;
|
|
};
|
|
};
|
|
SLEEP( SLEEP_KERNEL_INTERVAL );
|
|
if(( m_lCycleCounter >> 31 )!= 0 ) {
|
|
m_lCycleCounter = 0;
|
|
} else m_lCycleCounter++;
|
|
};
|
|
};
|
|
|
|
bool LRF_Kernel::setMode( LRF3D_WORK_MODE nMode ){
|
|
if( m_eWorkMode == nMode ) return true;
|
|
|
|
#ifndef NO_MOTORS
|
|
if( m_eWorkMode == WM_DETECTOR || nMode == WM_DETECTOR ){
|
|
#endif
|
|
if( nMode == WM_BORDER ) {
|
|
LOG("Switched to Mode BORDER");
|
|
};
|
|
if( nMode == WM_DETECTOR ) {
|
|
LOG("Switched to Mode DETECTOR");
|
|
};
|
|
// No other cleints demand specoal mode
|
|
m_eWorkMode = nMode;
|
|
return true;
|
|
#ifndef NO_MOTORS
|
|
};
|
|
return false;
|
|
#endif
|
|
};
|
|
|
|
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));
|
|
memset( tParam, 0 , sizeof(LGS_3D_PARAMETERS));
|
|
if( tParam != NULL ){
|
|
/*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;
|
|
} 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 );
|
|
};
|
|
};
|
|
};
|
|
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;
|
|
};
|
|
|
|
thresholdNode *LRF_Kernel::getTreshold( uint32_t &size, uint8_t kID ){
|
|
if( m_pTWIC != NULL ) {
|
|
|
|
};
|
|
if( m_pHardwareManager!= NULL ) {
|
|
thresholdNode *ptr;
|
|
ptr = m_pHardwareManager->getWorkingMemory();
|
|
#ifdef TH_PER_MIRROR
|
|
kID = (kID>39)?39:kID;
|
|
#else
|
|
kID = (kID>15)?15:kID;
|
|
#endif
|
|
if( ptr!= NULL ){
|
|
m_nThresholdSize = TH_SIZE_NODES;
|
|
if( m_pThreshold == NULL ) {
|
|
//m_pThreshold = (thresholdNode *) malloc( sizeof(thresholdNode)* m_nThresholdSize );
|
|
m_pThreshold = (thresholdNode *) malloc( TH_SIZE_BYTES );
|
|
};
|
|
if( m_pThreshold != NULL ) {
|
|
//memcpy( m_pThreshold, ptr+(kID*TH_SIZE_NODES), sizeof(thresholdNode)* m_nThresholdSize );
|
|
memcpy( m_pThreshold, ptr+(kID*TH_SIZE_NODES), TH_SIZE_BYTES );
|
|
} else m_nThresholdSize = 0;
|
|
};
|
|
};
|
|
size = m_nThresholdSize;
|
|
return m_pThreshold;
|
|
};
|
|
|
|
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;
|
|
};
|
|
|
|
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;
|
|
};
|
|
|
|
#ifndef PRU_SLAVE //Just Master Stuff
|
|
|
|
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
|