Files
EgtMachKernel/Simulator.cpp
T
Dario Sassi dd591f1853 EgtMachKernel :
- aggiunta gestione attivazione e stato di visualizzazione di una operazione.
2015-12-17 10:44:39 +00:00

306 lines
9.5 KiB
C++

//----------------------------------------------------------------------------
// EgalTech 2015-2015
//----------------------------------------------------------------------------
// File : Simulator.cpp Data : 19.10.15 Versione : 1.6j2
// Contenuto : Implementazione della classe Simulator.
//
//
//
// Modifiche : 19.10.15 DS Creazione modulo.
//
//
//----------------------------------------------------------------------------
//--------------------------- Include ----------------------------------------
#include "stdafx.h"
#include "Simulator.h"
#include "MachMgr.h"
#include "Machining.h"
#include "MachiningConst.h"
#include "/EgtDev/Include/EMkToolConst.h"
#include "/EgtDev/Include/EMkOperationConst.h"
using namespace std ;
//----------------------------------------------------------------------------
Simulator::Simulator( void)
{
m_pMchMgr = nullptr ;
m_pGeomDB = nullptr ;
m_dStep = 5.0 ;
m_nOpId = GDB_ID_NULL ;
m_nCLPathId = GDB_ID_NULL ;
m_nEntId = GDB_ID_NULL ;
m_dCoeff = 0 ;
m_AxesName.reserve( 8) ;
m_AxesVal.reserve( 8) ;
}
//----------------------------------------------------------------------------
Simulator::~Simulator( void)
{
}
//----------------------------------------------------------------------------
bool
Simulator::Init( MachMgr* pMchMgr)
{
// verifico ambiente
if ( pMchMgr == nullptr || pMchMgr->GetContextId() == 0 || pMchMgr->GetGeomDB() == nullptr)
return false ;
m_pMchMgr = pMchMgr ;
m_pGeomDB = m_pMchMgr->GetGeomDB() ;
return true ;
}
//----------------------------------------------------------------------------
bool
Simulator::Start( void)
{
// verifico ci sia una macchinata corrente
if ( m_pMchMgr == nullptr)
return false ;
int nMachGrp = m_pMchMgr->GetCurrMachGroup() ;
if ( nMachGrp == GDB_ID_NULL)
return false ;
// verifico la disposizione iniziale della macchinata
int nOpId = m_pMchMgr->GetFirstActiveOperation() ;
if ( m_pMchMgr->GetOperationType( nOpId) != OPER_DISP)
return false ;
// cerco la prima lavorazione
nOpId = m_pMchMgr->GetNextActiveOperation( nOpId) ;
while ( nOpId != GDB_ID_NULL && ! IsValidMachiningType( m_pMchMgr->GetOperationType( nOpId)))
nOpId = m_pMchMgr->GetNextActiveOperation( nOpId) ;
if ( nOpId == GDB_ID_NULL)
return false ;
m_nOpId = nOpId ;
// aggiornamenti legati al cambio di lavorazione (utensile e assi conseguenti)
if ( ! UpdateTool() || ! UpdateAxes())
return false ;
// porto la macchina in home
if ( ! GoHome())
return false ;
// determino il primo percorso di lavoro
int nClId = m_pGeomDB->GetFirstNameInGroup( m_nOpId, MCH_CL) ;
m_nCLPathId = m_pGeomDB->GetFirstGroupInGroup( nClId) ;
m_nEntId = m_pGeomDB->GetFirstInGroup( m_nCLPathId) ;
m_dCoeff = 0 ;
return true ;
}
//----------------------------------------------------------------------------
bool
Simulator::Move( int& nStatus)
{
// Verifiche
if ( m_pMchMgr == nullptr || m_pGeomDB == nullptr) {
nStatus = MCH_SIM_ERR ;
return false ;
}
// Se arrivato alla fine dell'interpolazione, recupero una nuova entità
if ( m_dCoeff > 0.999) {
m_nEntId = m_pGeomDB->GetNext( m_nEntId) ;
m_dCoeff = 0 ;
}
// Se arrivato alla fine di un percorso di lavoro, recupero un nuovo CLpath
while ( m_nEntId == GDB_ID_NULL) {
m_nCLPathId = m_pGeomDB->GetNextGroup( m_nCLPathId) ;
// se non ce ne sono altri, devo passare a una nuova lavorazione
if ( m_nCLPathId == GDB_ID_NULL)
break ;
m_nEntId = m_pGeomDB->GetFirstInGroup( m_nCLPathId) ;
m_dCoeff = 0 ;
}
// Se arrivato alla fine di una lavorazione, recupero la successiva
while ( m_nCLPathId == GDB_ID_NULL) {
m_nOpId = m_pMchMgr->GetNextActiveOperation( m_nOpId) ;
// se non ce ne sono altre, sono alla fine
if ( m_nOpId == GDB_ID_NULL) {
nStatus = MCH_SIM_END ;
return false ;
}
// aggiorno gli altri dati
int nClId = m_pGeomDB->GetFirstNameInGroup( m_nOpId, MCH_CL) ;
m_nCLPathId = m_pGeomDB->GetFirstGroupInGroup( nClId) ;
m_nEntId = m_pGeomDB->GetFirstInGroup( m_nCLPathId) ;
m_dCoeff = 0 ;
// aggiorno utensile e assi conseguenti
if ( ! UpdateTool() || ! UpdateAxes()) {
nStatus = MCH_SIM_ERR ;
return false ;
}
}
// Cerco prossima posizione su interpolazione corrente
CamData* pCamData = dynamic_cast<CamData*>( m_pGeomDB->GetUserObj( m_nEntId)) ;
if ( pCamData == nullptr) {
nStatus = MCH_SIM_ERR ;
return false ;
}
switch ( pCamData->GetAxesStatus()) {
case CamData::AS_OK :
break ;
case CamData::AS_OUTSTROKE :
nStatus = MCH_SIM_OUTSTROKE ;
return false ;
case CamData::AS_DIR_ERR :
nStatus = MCH_SIM_DIR_ERR ;
return false ;
default :
nStatus = MCH_SIM_ERR ;
return false ;
}
const DBLVECTOR& AxesEnd = pCamData->GetAxisVal() ;
// Verifico se movimento in rapido
bool bRapid = ( pCamData->GetFeed() < EPS_SMALL) ;
// Calcolo distanza di movimento
double dDist = 0 ;
for ( size_t i = 0 ; i < m_AxesName.size() ; ++ i)
dDist += ( AxesEnd[i] - m_AxesVal[i]) * ( AxesEnd[i] - m_AxesVal[i]) ;
dDist = sqrt( dDist) ;
m_dCoeff += ( bRapid ? 4 : 1) * m_dStep / dDist ;
if ( m_dCoeff > 1)
m_dCoeff = 1 ;
// Eseguo movimento
for ( size_t i = 0 ; i < m_AxesName.size() ; ++ i) {
double dVal = m_AxesVal[i] * ( 1 - m_dCoeff) + AxesEnd[i] * m_dCoeff ;
m_pMchMgr->SetAxisPos( m_AxesName[i], dVal) ;
}
// Se arrivato a fine interpolazione movimento, salvo posizioni e segnalo
if ( m_dCoeff > 0.999) {
m_AxesVal = AxesEnd ;
nStatus = MCH_SIM_END_STEP ;
}
// Altrimenti sto muovendomi in uno step
else
nStatus = MCH_SIM_OK ;
return true ;
}
//----------------------------------------------------------------------------
bool
Simulator::GetAxisInfoPos( int nI, string& sName, double& dVal)
{
// Verifiche
if ( m_pMchMgr == nullptr || m_pGeomDB == nullptr)
return false ;
// recupero i dati dell'asse
if ( nI < 0 || nI >= int( m_AxesName.size()))
return false ;
sName = m_AxesName[nI] ;
return m_pMchMgr->GetAxisPos( sName, dVal) ;
}
//----------------------------------------------------------------------------
bool
Simulator::GetToolInfo( string& sName, double& dSpeed)
{
// Verifiche
if ( m_pMchMgr == nullptr || m_pGeomDB == nullptr)
return false ;
// Assegno il nome dell'utensile
if ( m_sTool.empty())
return false ;
sName = m_sTool ;
// Recupero speed
Machining* pMch = dynamic_cast<Machining*>( m_pGeomDB->GetUserObj( m_nOpId)) ;
return ( pMch != nullptr && pMch->GetParam( MPA_TOOLSPEED, dSpeed)) ;
}
//----------------------------------------------------------------------------
bool
Simulator::GetMoveInfo( int& nGmove, double& dFeed)
{
// Verifiche
if ( m_pMchMgr == nullptr || m_pGeomDB == nullptr)
return false ;
// Recupero il movimento corrente
CamData* pCamData = dynamic_cast<CamData*>( m_pGeomDB->GetUserObj( m_nEntId)) ;
if ( pCamData == nullptr)
return false ;
// Assegno feed
dFeed = pCamData->GetFeed() ;
// Assegno tipo di movimento
if ( dFeed < EPS_SMALL)
nGmove = 0 ;
else
nGmove = 1 ;
return true ;
}
//----------------------------------------------------------------------------
bool
Simulator::SetStep( double dStep)
{
const double MIN_STEP = 0.1 ;
const double MAX_STEP = 100.0 ;
if ( dStep < MIN_STEP)
m_dStep = MIN_STEP ;
else if ( dStep > MAX_STEP)
m_dStep = MAX_STEP ;
else
m_dStep = dStep ;
return true ;
}
//----------------------------------------------------------------------------
bool
Simulator::Stop( void)
{
m_nOpId = GDB_ID_NULL ;
m_nCLPathId = GDB_ID_NULL ;
m_nEntId = GDB_ID_NULL ;
m_dCoeff = 0 ;
return true ;
}
//----------------------------------------------------------------------------
bool
Simulator::UpdateTool( void)
{
// Recupero l'utensile della lavorazione corrente
string sTool ;
Machining* pMch = dynamic_cast<Machining*>( m_pGeomDB->GetUserObj( m_nOpId)) ;
if ( pMch == nullptr || ! pMch->GetParam( MPA_TOOL, sTool))
return false ;
if ( ! m_pMchMgr->TdbSetCurrTool( sTool))
return false ;
// se cambiato, attivo l'utensile della lavorazione
if ( sTool != m_sTool) {
string sHead ; m_pMchMgr->TdbGetCurrToolParam( TPA_HEAD, sHead) ;
int nExit ; m_pMchMgr->TdbGetCurrToolParam( TPA_EXIT, nExit) ;
if ( ! m_pMchMgr->SetCalcTool( sTool, sHead, nExit))
return false ;
m_sTool = sTool ;
}
return true ;
}
//----------------------------------------------------------------------------
bool
Simulator::UpdateAxes( void)
{
// Macchina corrente
Machine* pMachine = m_pMchMgr->GetCurrMachine() ;
if ( pMachine == nullptr)
return false ;
// Carico i nomi degli assi macchina attivi
return pMachine->GetAllCurrAxesName( m_AxesName) ;
}
//----------------------------------------------------------------------------
bool
Simulator::GoHome( void)
{
// porto la macchina in home
m_pMchMgr->ResetAllAxesPos() ;
// assegno valori home degli assi macchina attivi
return m_pMchMgr->GetAllCalcAxesHomePos( m_AxesVal) ;
}