Old fashion design for PC-104
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.
 
 
 
 
 

447 lines
14 KiB

//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright © 2000-2002. All Rights Reserved.
// Version 1.0 01/01/2000
// Last modification: 08/10/2003
// PROJECT:
// SUBSYSTEM:
// FILE: CalibrationData.cpp
// AUTHOR: Sergey Chizhevskiy
// OVERVIEW
// ~~~~~~~~
// Source file of CalibrationData.cpp
//----------------------------------------------------------------------------
// CalibrationData.cpp: implementation of the CCalibrationData class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Calibration.h"
#include "CalibrationData.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
static int g_nInFile = 1;
static int g_nOutFile = 6;
static int g_nReportFile = 7;
static PARAMETER g_pVariable[] =
{
{ _T("type"), IDC_EDIT_TYPE, FALSE, PT_INTEGER, 2, 0.0, NULL },
{ _T("test_file"), IDC_EDIT_TEST_FILE, FALSE, PT_STRING, 0, 0.0, _T("invalue.txt") },
{ _T("tolerance_x"), IDC_EDIT_TOLERANCE_X, FALSE, PT_DOUBLE, 0, 1e-4, NULL },
{ _T("tolerance_F"), IDC_EDIT_TOLERANCE_F, FALSE, PT_DOUBLE, 0, 1e-4, NULL },
{ _T("iterations"), IDC_EDIT_ITERATIONS, FALSE, PT_INTEGER, 1000, 0, NULL },
{ _T("arground"), IDC_EDIT_ARGROUND, FALSE, PT_DOUBLE, 0, 0.001, NULL },
{ _T("Out_file"), IDC_EDIT_OUT_FILE, FALSE, PT_STRING, 0, 0.0, _T("outvalue.txt") },
{ _T("Report_file"), IDC_EDIT_REPORT_FILE, FALSE, PT_STRING, 0, 0.0, _T("report.txt") },
{ _T("report"), IDC_EDIT_REPORT, FALSE, PT_INTEGER, 1, 0.0, NULL },
{ _T("NXc0"), IDC_EDIT_NXC0, FALSE, PT_INTEGER, 3, 0.0, NULL },
{ _T("Xc0_min"), IDC_EDIT_XC0_MIN, FALSE, PT_DOUBLE, 0, -65.5, NULL },
{ _T("Xc0_max"), IDC_EDIT_XC0_MAX, FALSE, PT_DOUBLE, 0, -25.5, NULL },
{ _T("NYc0"), IDC_EDIT_NYC0, FALSE, PT_INTEGER, 3, 0.0, NULL },
{ _T("Yc0_min"), IDC_EDIT_YC0_MIN, FALSE, PT_DOUBLE, 0, -70.5, NULL },
{ _T("Yc0_max"), IDC_EDIT_YC0_MAX, FALSE, PT_DOUBLE, 0, -30.5, NULL },
{ _T("NZc0"), IDC_EDIT_NZC0, FALSE, PT_INTEGER, 3, 0.0, NULL },
{ _T("Zc0_min"), IDC_EDIT_ZC0_MIN, FALSE, PT_DOUBLE, 0, -1, NULL },
{ _T("Zc0_max"), IDC_EDIT_ZC0_MAX, FALSE, PT_DOUBLE, 0, 1, NULL },
{ _T("NPsic0"), IDC_EDIT_NPSIC0, FALSE, PT_INTEGER, 4, 0.0, NULL },
{ _T("Psic0_min"), IDC_EDIT_PSIC0_MIN, FALSE, PT_DOUBLE, 0, 0.0, NULL },
{ _T("Psic0_max"), IDC_EDIT_PSIC0_MAX, FALSE, PT_DOUBLE, 0, 240.0, NULL },
{ _T("NThetac0"), IDC_EDIT_NTHETAC0, FALSE, PT_INTEGER, 3, 0.0, NULL },
{ _T("Thetac0_min"), IDC_EDIT_THETAC0_MIN, FALSE, PT_DOUBLE, 0, -10.0, NULL },
{ _T("Thetac0_max"), IDC_EDIT_THETAC0_MAX, FALSE, PT_DOUBLE, 0, 10.0, NULL },
{ _T("NPhic0"), IDC_EDIT_NPHIC0, FALSE, PT_INTEGER, 3, 0.0, NULL },
{ _T("Phic0_min"), IDC_EDIT_PHIC0_MIN, FALSE, PT_DOUBLE, 0, -10.0, NULL },
{ _T("Phic0_max"), IDC_EDIT_PHIC0_MAX, FALSE, PT_DOUBLE, 0, 10.0, NULL },
{ _T("Target_thetad(1)"), IDC_EDIT_TARGET_THETAD1, FALSE, PT_DOUBLE, 0, 0.0, NULL },
{ _T("Target_thetad(2)"), IDC_EDIT_TARGET_THETAD2, FALSE, PT_DOUBLE, 0, 0.0, NULL },
{ _T("Roc_s_msr"), IDC_EDIT_ROC_S_MSR, FALSE, PT_DOUBLE, 0, 68.0, NULL },
{ _T("Psic_s_msrd"), IDC_EDIT_PSIC_S_MSRD, FALSE, PT_DOUBLE, 0, 312.0, NULL }
};
static int g_nVariable = sizeof(g_pVariable) / sizeof(g_pVariable[0]);
static LPCTSTR g_pFNExec = _T("Exec");
static LPCTSTR g_pFNData = _T("Data");
static LPCTSTR g_pFNParams = _T("Exec\\runvalue.txt");
static LPCTSTR g_pFNIn = _T("invalue.txt");
static LPCTSTR g_pFNOut = _T("outvalue.txt");
static LPCTSTR g_pFNReport = _T("report.txt");
CCalibrationData::CCalibrationData()
{
m_nVariable = g_nVariable;
m_pVariable = new PARAMETER[m_nVariable];
::CopyMemory( m_pVariable, g_pVariable, sizeof(g_pVariable) );
LoadParams( g_pFNParams );
m_Thread.hEvent = ::CreateEvent( NULL, FALSE, FALSE, NULL );
m_Thread.hThread = NULL;
m_Thread.hWnd = NULL;
}
CCalibrationData::~CCalibrationData()
{
if( m_Thread.hThread )
{
::SetEvent( m_Thread.hEvent );
::WaitForSingleObject( m_Thread.hThread, INFINITE );
::CloseHandle( m_Thread.hThread );
}
::CloseHandle( m_Thread.hEvent );
SaveParams( g_pFNParams );
delete [] m_pVariable;
}
#define SKIP_BLANK( pBuffer ) { while( *pBuffer == ' ' || *pBuffer == '\t' ) pBuffer++; }
#define SKIP_NON_BLANK( pBuffer ) { while( *pBuffer != ' ' && *pBuffer != '\t' && *pBuffer ) pBuffer++; }
#define GET_WORD( pName, pBuffer ) \
{ SKIP_BLANK( pBuffer ); \
LPSTR pBegin = pBuffer; \
SKIP_NON_BLANK( pBuffer ); \
_tcsncpy( pName, pBegin, pBuffer - pBegin ); \
*(pName + (int)(pBuffer - pBegin) ) = 0; }
BOOL CCalibrationData::LoadParams( CString fname )
{
CStdioFile file;
if( file.Open( fname, CFile::modeRead | CFile::typeText, NULL ) )
{
CString line;
while( file.ReadString( line ) )
{
LPSTR pBuffer = (LPSTR)(LPCTSTR)line;
TCHAR pName[MAX_PATH];
GET_WORD( pName, pBuffer );
for( int i = 0; i < m_nVariable; i++ )
{
if( !_tcsicmp( pName, m_pVariable[i].pName ) )
{
GET_WORD( pName, pBuffer );
switch( m_pVariable[i].nType )
{
case PT_INTEGER:
m_pVariable[i].data_i = atoi( pName );
m_pVariable[i].bInit = TRUE;
break;
case PT_DOUBLE:
m_pVariable[i].data_d = atof( pName );
m_pVariable[i].bInit = TRUE;
break;
case PT_STRING:
_tcscpy( m_pVariable[i].data_s, pName );
m_pVariable[i].bInit = TRUE;
break;
}
break; // variable found
}
}
}
file.Close();
return TRUE;
}
return FALSE;
}
BOOL CCalibrationData::SaveParams( CString fname )
{
CStdioFile file;
if( file.Open( fname, CFile::modeCreate | CFile::modeWrite | CFile::typeText, NULL ) )
{
CString line;
for( int i = 0; i < m_nVariable; i++ )
{
switch( m_pVariable[i].nType )
{
case PT_INTEGER:
line.Format( _T("%s %d\n"), m_pVariable[i].pName, m_pVariable[i].data_i );
break;
case PT_DOUBLE:
line.Format( _T("%s %f\n"), m_pVariable[i].pName, m_pVariable[i].data_d );
break;
case PT_STRING:
line.Format( _T("%s %s\n"), m_pVariable[i].pName, m_pVariable[i].data_s );
break;
}
file.WriteString( line );
}
file.Close();
return TRUE;
}
return FALSE;
}
BOOL CCalibrationData::Execute( HWND hWnd, CString& name )
{
if( m_Thread.hThread )
{
if( ::WaitForSingleObject( m_Thread.hThread, 0 ) != WAIT_OBJECT_0 )
return FALSE;
::CloseHandle( m_Thread.hThread );
m_Thread.hThread = NULL;
}
SYSTEMTIME st;
::GetLocalTime( &st );
CString dname;
dname.Format( _T("%s"), g_pFNData );
::CreateDirectory( dname, NULL );
// set name for store in logical relation
name.Format( "%04hu_%02hu_%02hu_%02hu_%02hu_%02hu", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond );
CString sdname;
sdname.Format( _T("%s\\%s"), (LPCTSTR)dname, (LPCTSTR)name );
::CreateDirectory( sdname, NULL );
CString fname;
fname.Format( _T("%s\\%s"), (LPCTSTR)sdname, g_pFNIn );
CString iname;
iname.Format( _T("..\\%s"), fname );
_tcscpy( m_pVariable[g_nInFile].data_s, iname );
CString oname;
oname.Format( _T("..\\%s\\%s"), sdname, g_pFNOut );
_tcscpy( m_pVariable[g_nOutFile].data_s, oname );
CString rname;
rname.Format( _T("..\\%s\\%s"), sdname, g_pFNReport );
_tcscpy( m_pVariable[g_nReportFile].data_s, rname );
SaveParams( g_pFNParams );
CStdioFile file;
file.Open( fname, CFile::modeCreate | CFile::modeWrite | CFile::typeText );
POSITION pos = m_DataList.GetHeadPosition();
while( pos )
{
DATACALC dc = m_DataList.GetNext( pos );
CString str;
str.Format( _T("%8.2f %8.2f %8.2f %8.2f %8.2f %8.2f %8.2f\n"),
dc.ma, dc.mr, dc.sa, dc.sr, dc.cp, dc.ct, dc.cz );
file.WriteString( str );
}
file.Close();
while( m_DataList.GetCount() )
m_DataList.RemoveTail();
m_Thread.hWnd = hWnd;
DWORD dwThreadId;
m_Thread.hThread = ::CreateThread( NULL, 0UL, ThreadProcCalibrationData, this, 0UL, &dwThreadId );
return TRUE;
}
DWORD CCalibrationData::ThreadProc()
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
::ZeroMemory( &si, sizeof( STARTUPINFO ) );
::ZeroMemory( &pi, sizeof( PROCESS_INFORMATION ) );
si.cb = sizeof( STARTUPINFO );
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOW;
DWORD dwExitCode;
if( ::CreateProcess( _T("exec\\camera_estimation.exe"), NULL, NULL,
NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, _T("exec"), &si, &pi ) )
{
/* HANDLE hOutput = ::GetStdHandle( STD_OUTPUT_HANDLE );
HANDLE hInput = ::GetStdHandle( STD_OUTPUT_HANDLE );
HANDLE hPipe = ::CreatePipe( hOutput, hInput, NULL, 1UL );
while( ::WaitForSingleObject( pi.hProces, 0UL )!=WAIT_OBJECT_0 )
{
}
*/
HANDLE pHandles[] = { m_Thread.hEvent, pi.hProcess };
switch( ::WaitForMultipleObjects( 2, pHandles, FALSE, INFINITE ) )
{
case WAIT_OBJECT_0 + 1:
::GetExitCodeProcess( pi.hProcess, &dwExitCode );
::PostMessage( m_Thread.hWnd, WM_CALIBRATION, TRUE, dwExitCode );
break;
case WAIT_OBJECT_0:
::TerminateProcess( pi.hProcess, (UINT)-1L );
break;
}
}
else
::PostMessage( m_Thread.hWnd, WM_CALIBRATION, FALSE, 0UL );
if( pi.hThread )
::CloseHandle( pi.hThread );
if( pi.hProcess )
::CloseHandle( pi.hProcess );
return 0UL;
}
void CCalibrationData::GetFolderList( CStringList &list )
{
CString fpath = CString(g_pFNData) + _T("\\*.*");
CFileFind finder;
BOOL bWorking = finder.FindFile( fpath );
while( bWorking )
{
bWorking = finder.FindNextFile();
if( finder.IsDirectory() && finder.GetFileName()[0] != '.' )
list.AddTail( finder.GetFileName() );
}
}
void CCalibrationData::GetDataList( CString fname, CDataList &list )
{
CString fpath = CString(g_pFNData) + _T("\\*.*");
CFileFind finder;
BOOL bWorking = finder.FindFile( fpath );
while( bWorking )
{
bWorking = finder.FindNextFile();
if( finder.GetFilePath().Find( fname, 0 ) != -1 )
{
CString pname = finder.GetFilePath() + _T("\\") + g_pFNIn;
CStdioFile file;
if( file.Open( pname, CFile::modeRead | CFile::typeText ) )
{
DATACALC dc;
CString str;
while( file.ReadString( str ) )
{
LPTSTR pStr = (LPTSTR)(LPCTSTR)str;
TCHAR pWord[MAX_PATH];
GET_WORD( pWord, pStr ); dc.ma = atof( pWord );
GET_WORD( pWord, pStr ); dc.mr = atof( pWord );
GET_WORD( pWord, pStr ); dc.sa = atof( pWord );
GET_WORD( pWord, pStr ); dc.sr = atof( pWord );
GET_WORD( pWord, pStr ); dc.cp = atof( pWord );
GET_WORD( pWord, pStr ); dc.ct = atof( pWord );
GET_WORD( pWord, pStr ); dc.cz = atof( pWord );
list.AddTail( dc );
}
file.Close();
}
}
}
}
CString CCalibrationData::GetResultPath( CString fname )
{
CString fpath = CString(g_pFNData) + _T("\\*.*");
CFileFind finder;
BOOL bWorking = finder.FindFile( fpath );
while( bWorking )
{
bWorking = finder.FindNextFile();
if( finder.GetFilePath().Find( fname, 0 ) != -1 )
{
return finder.GetFilePath() + _T("\\") + g_pFNOut;
}
}
return CString(_T(""));
}
CString CCalibrationData::GetReportPath( CString fname )
{
CString fpath = CString(g_pFNData) + _T("\\*.*");
CFileFind finder;
BOOL bWorking = finder.FindFile( fpath );
while( bWorking )
{
bWorking = finder.FindNextFile();
if( finder.GetFilePath().Find( fname, 0 ) != -1 )
{
return finder.GetFilePath() + _T("\\") + g_pFNReport;
}
}
return CString(_T(""));
}
BOOL CCalibrationData::GetResult( CString fname, DATARES &drm, DATARES &drs )
{
CString fpath = CString(g_pFNData) + _T("\\*.*");
CFileFind finder;
BOOL bWorking = finder.FindFile( fpath );
while( bWorking )
{
bWorking = finder.FindNextFile();
if( finder.GetFilePath().Find( fname, 0 ) != -1 )
{
CString pname = finder.GetFilePath() + _T("\\") + g_pFNOut;
CStdioFile file;
if( file.Open( pname, CFile::modeRead | CFile::typeText ) )
{
CString str;
BOOL bSuccess[] = { FALSE, FALSE };
while( file.ReadString( str ) )
{
LPTSTR pStr = (LPTSTR)(LPCTSTR)str;
TCHAR pWord[MAX_PATH];
GET_WORD( pWord, pStr );
if( !_tcscmp( pWord, _T("defm") ) )
{
GET_WORD( pWord, pStr ); drm.x = atof( pWord );
GET_WORD( pWord, pStr ); drm.y = atof( pWord );
GET_WORD( pWord, pStr ); drm.z = atof( pWord );
GET_WORD( pWord, pStr ); drm.psi = atof( pWord );
GET_WORD( pWord, pStr ); drm.theta = atof( pWord );
GET_WORD( pWord, pStr ); drm.phi = atof( pWord );
bSuccess[0] = TRUE;
}
else
if( !_tcscmp( pWord, _T("defs") ) )
{
GET_WORD( pWord, pStr ); drs.x = atof( pWord );
GET_WORD( pWord, pStr ); drs.y = atof( pWord );
GET_WORD( pWord, pStr ); drs.z = atof( pWord );
GET_WORD( pWord, pStr ); drs.psi = atof( pWord );
GET_WORD( pWord, pStr ); drs.theta = atof( pWord );
GET_WORD( pWord, pStr ); drs.phi = atof( pWord );
bSuccess[1] = TRUE;
}
}
file.Close();
return bSuccess[0] && bSuccess[1]; // WARNING: needs to be checked in math
}
}
}
return FALSE;
}
void CCalibrationData::AddData( double ma, double mr, double sa, double sr, double cp, double ct, double cz )
{
DATACALC dc;
dc.ma = ma;
dc.mr = mr;
dc.sa = sa;
dc.sr = sr;
dc.cp = cp;
dc.ct = ct;
dc.cz = cz;
m_DataList.AddTail( dc );
}