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

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: CalibrationData.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of CalibrationData.cpp
  13. //----------------------------------------------------------------------------
  14. // CalibrationData.cpp: implementation of the CCalibrationData class.
  15. //
  16. //////////////////////////////////////////////////////////////////////
  17. #include "stdafx.h"
  18. #include "Calibration.h"
  19. #include "CalibrationData.h"
  20. #ifdef _DEBUG
  21. #undef THIS_FILE
  22. static char THIS_FILE[]=__FILE__;
  23. #define new DEBUG_NEW
  24. #endif
  25. //////////////////////////////////////////////////////////////////////
  26. // Construction/Destruction
  27. //////////////////////////////////////////////////////////////////////
  28. static int g_nInFile = 1;
  29. static int g_nOutFile = 6;
  30. static int g_nReportFile = 7;
  31. static PARAMETER g_pVariable[] =
  32. {
  33. { _T("type"), IDC_EDIT_TYPE, FALSE, PT_INTEGER, 2, 0.0, NULL },
  34. { _T("test_file"), IDC_EDIT_TEST_FILE, FALSE, PT_STRING, 0, 0.0, _T("invalue.txt") },
  35. { _T("tolerance_x"), IDC_EDIT_TOLERANCE_X, FALSE, PT_DOUBLE, 0, 1e-4, NULL },
  36. { _T("tolerance_F"), IDC_EDIT_TOLERANCE_F, FALSE, PT_DOUBLE, 0, 1e-4, NULL },
  37. { _T("iterations"), IDC_EDIT_ITERATIONS, FALSE, PT_INTEGER, 1000, 0, NULL },
  38. { _T("arground"), IDC_EDIT_ARGROUND, FALSE, PT_DOUBLE, 0, 0.001, NULL },
  39. { _T("Out_file"), IDC_EDIT_OUT_FILE, FALSE, PT_STRING, 0, 0.0, _T("outvalue.txt") },
  40. { _T("Report_file"), IDC_EDIT_REPORT_FILE, FALSE, PT_STRING, 0, 0.0, _T("report.txt") },
  41. { _T("report"), IDC_EDIT_REPORT, FALSE, PT_INTEGER, 1, 0.0, NULL },
  42. { _T("NXc0"), IDC_EDIT_NXC0, FALSE, PT_INTEGER, 3, 0.0, NULL },
  43. { _T("Xc0_min"), IDC_EDIT_XC0_MIN, FALSE, PT_DOUBLE, 0, -65.5, NULL },
  44. { _T("Xc0_max"), IDC_EDIT_XC0_MAX, FALSE, PT_DOUBLE, 0, -25.5, NULL },
  45. { _T("NYc0"), IDC_EDIT_NYC0, FALSE, PT_INTEGER, 3, 0.0, NULL },
  46. { _T("Yc0_min"), IDC_EDIT_YC0_MIN, FALSE, PT_DOUBLE, 0, -70.5, NULL },
  47. { _T("Yc0_max"), IDC_EDIT_YC0_MAX, FALSE, PT_DOUBLE, 0, -30.5, NULL },
  48. { _T("NZc0"), IDC_EDIT_NZC0, FALSE, PT_INTEGER, 3, 0.0, NULL },
  49. { _T("Zc0_min"), IDC_EDIT_ZC0_MIN, FALSE, PT_DOUBLE, 0, -1, NULL },
  50. { _T("Zc0_max"), IDC_EDIT_ZC0_MAX, FALSE, PT_DOUBLE, 0, 1, NULL },
  51. { _T("NPsic0"), IDC_EDIT_NPSIC0, FALSE, PT_INTEGER, 4, 0.0, NULL },
  52. { _T("Psic0_min"), IDC_EDIT_PSIC0_MIN, FALSE, PT_DOUBLE, 0, 0.0, NULL },
  53. { _T("Psic0_max"), IDC_EDIT_PSIC0_MAX, FALSE, PT_DOUBLE, 0, 240.0, NULL },
  54. { _T("NThetac0"), IDC_EDIT_NTHETAC0, FALSE, PT_INTEGER, 3, 0.0, NULL },
  55. { _T("Thetac0_min"), IDC_EDIT_THETAC0_MIN, FALSE, PT_DOUBLE, 0, -10.0, NULL },
  56. { _T("Thetac0_max"), IDC_EDIT_THETAC0_MAX, FALSE, PT_DOUBLE, 0, 10.0, NULL },
  57. { _T("NPhic0"), IDC_EDIT_NPHIC0, FALSE, PT_INTEGER, 3, 0.0, NULL },
  58. { _T("Phic0_min"), IDC_EDIT_PHIC0_MIN, FALSE, PT_DOUBLE, 0, -10.0, NULL },
  59. { _T("Phic0_max"), IDC_EDIT_PHIC0_MAX, FALSE, PT_DOUBLE, 0, 10.0, NULL },
  60. { _T("Target_thetad(1)"), IDC_EDIT_TARGET_THETAD1, FALSE, PT_DOUBLE, 0, 0.0, NULL },
  61. { _T("Target_thetad(2)"), IDC_EDIT_TARGET_THETAD2, FALSE, PT_DOUBLE, 0, 0.0, NULL },
  62. { _T("Roc_s_msr"), IDC_EDIT_ROC_S_MSR, FALSE, PT_DOUBLE, 0, 68.0, NULL },
  63. { _T("Psic_s_msrd"), IDC_EDIT_PSIC_S_MSRD, FALSE, PT_DOUBLE, 0, 312.0, NULL }
  64. };
  65. static int g_nVariable = sizeof(g_pVariable) / sizeof(g_pVariable[0]);
  66. static LPCTSTR g_pFNExec = _T("Exec");
  67. static LPCTSTR g_pFNData = _T("Data");
  68. static LPCTSTR g_pFNParams = _T("Exec\\runvalue.txt");
  69. static LPCTSTR g_pFNIn = _T("invalue.txt");
  70. static LPCTSTR g_pFNOut = _T("outvalue.txt");
  71. static LPCTSTR g_pFNReport = _T("report.txt");
  72. CCalibrationData::CCalibrationData()
  73. {
  74. m_nVariable = g_nVariable;
  75. m_pVariable = new PARAMETER[m_nVariable];
  76. ::CopyMemory( m_pVariable, g_pVariable, sizeof(g_pVariable) );
  77. LoadParams( g_pFNParams );
  78. m_Thread.hEvent = ::CreateEvent( NULL, FALSE, FALSE, NULL );
  79. m_Thread.hThread = NULL;
  80. m_Thread.hWnd = NULL;
  81. }
  82. CCalibrationData::~CCalibrationData()
  83. {
  84. if( m_Thread.hThread )
  85. {
  86. ::SetEvent( m_Thread.hEvent );
  87. ::WaitForSingleObject( m_Thread.hThread, INFINITE );
  88. ::CloseHandle( m_Thread.hThread );
  89. }
  90. ::CloseHandle( m_Thread.hEvent );
  91. SaveParams( g_pFNParams );
  92. delete [] m_pVariable;
  93. }
  94. #define SKIP_BLANK( pBuffer ) { while( *pBuffer == ' ' || *pBuffer == '\t' ) pBuffer++; }
  95. #define SKIP_NON_BLANK( pBuffer ) { while( *pBuffer != ' ' && *pBuffer != '\t' && *pBuffer ) pBuffer++; }
  96. #define GET_WORD( pName, pBuffer ) \
  97. { SKIP_BLANK( pBuffer ); \
  98. LPSTR pBegin = pBuffer; \
  99. SKIP_NON_BLANK( pBuffer ); \
  100. _tcsncpy( pName, pBegin, pBuffer - pBegin ); \
  101. *(pName + (int)(pBuffer - pBegin) ) = 0; }
  102. BOOL CCalibrationData::LoadParams( CString fname )
  103. {
  104. CStdioFile file;
  105. if( file.Open( fname, CFile::modeRead | CFile::typeText, NULL ) )
  106. {
  107. CString line;
  108. while( file.ReadString( line ) )
  109. {
  110. LPSTR pBuffer = (LPSTR)(LPCTSTR)line;
  111. TCHAR pName[MAX_PATH];
  112. GET_WORD( pName, pBuffer );
  113. for( int i = 0; i < m_nVariable; i++ )
  114. {
  115. if( !_tcsicmp( pName, m_pVariable[i].pName ) )
  116. {
  117. GET_WORD( pName, pBuffer );
  118. switch( m_pVariable[i].nType )
  119. {
  120. case PT_INTEGER:
  121. m_pVariable[i].data_i = atoi( pName );
  122. m_pVariable[i].bInit = TRUE;
  123. break;
  124. case PT_DOUBLE:
  125. m_pVariable[i].data_d = atof( pName );
  126. m_pVariable[i].bInit = TRUE;
  127. break;
  128. case PT_STRING:
  129. _tcscpy( m_pVariable[i].data_s, pName );
  130. m_pVariable[i].bInit = TRUE;
  131. break;
  132. }
  133. break; // variable found
  134. }
  135. }
  136. }
  137. file.Close();
  138. return TRUE;
  139. }
  140. return FALSE;
  141. }
  142. BOOL CCalibrationData::SaveParams( CString fname )
  143. {
  144. CStdioFile file;
  145. if( file.Open( fname, CFile::modeCreate | CFile::modeWrite | CFile::typeText, NULL ) )
  146. {
  147. CString line;
  148. for( int i = 0; i < m_nVariable; i++ )
  149. {
  150. switch( m_pVariable[i].nType )
  151. {
  152. case PT_INTEGER:
  153. line.Format( _T("%s %d\n"), m_pVariable[i].pName, m_pVariable[i].data_i );
  154. break;
  155. case PT_DOUBLE:
  156. line.Format( _T("%s %f\n"), m_pVariable[i].pName, m_pVariable[i].data_d );
  157. break;
  158. case PT_STRING:
  159. line.Format( _T("%s %s\n"), m_pVariable[i].pName, m_pVariable[i].data_s );
  160. break;
  161. }
  162. file.WriteString( line );
  163. }
  164. file.Close();
  165. return TRUE;
  166. }
  167. return FALSE;
  168. }
  169. BOOL CCalibrationData::Execute( HWND hWnd, CString& name )
  170. {
  171. if( m_Thread.hThread )
  172. {
  173. if( ::WaitForSingleObject( m_Thread.hThread, 0 ) != WAIT_OBJECT_0 )
  174. return FALSE;
  175. ::CloseHandle( m_Thread.hThread );
  176. m_Thread.hThread = NULL;
  177. }
  178. SYSTEMTIME st;
  179. ::GetLocalTime( &st );
  180. CString dname;
  181. dname.Format( _T("%s"), g_pFNData );
  182. ::CreateDirectory( dname, NULL );
  183. // set name for store in logical relation
  184. name.Format( "%04hu_%02hu_%02hu_%02hu_%02hu_%02hu", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond );
  185. CString sdname;
  186. sdname.Format( _T("%s\\%s"), (LPCTSTR)dname, (LPCTSTR)name );
  187. ::CreateDirectory( sdname, NULL );
  188. CString fname;
  189. fname.Format( _T("%s\\%s"), (LPCTSTR)sdname, g_pFNIn );
  190. CString iname;
  191. iname.Format( _T("..\\%s"), fname );
  192. _tcscpy( m_pVariable[g_nInFile].data_s, iname );
  193. CString oname;
  194. oname.Format( _T("..\\%s\\%s"), sdname, g_pFNOut );
  195. _tcscpy( m_pVariable[g_nOutFile].data_s, oname );
  196. CString rname;
  197. rname.Format( _T("..\\%s\\%s"), sdname, g_pFNReport );
  198. _tcscpy( m_pVariable[g_nReportFile].data_s, rname );
  199. SaveParams( g_pFNParams );
  200. CStdioFile file;
  201. file.Open( fname, CFile::modeCreate | CFile::modeWrite | CFile::typeText );
  202. POSITION pos = m_DataList.GetHeadPosition();
  203. while( pos )
  204. {
  205. DATACALC dc = m_DataList.GetNext( pos );
  206. CString str;
  207. str.Format( _T("%8.2f %8.2f %8.2f %8.2f %8.2f %8.2f %8.2f\n"),
  208. dc.ma, dc.mr, dc.sa, dc.sr, dc.cp, dc.ct, dc.cz );
  209. file.WriteString( str );
  210. }
  211. file.Close();
  212. while( m_DataList.GetCount() )
  213. m_DataList.RemoveTail();
  214. m_Thread.hWnd = hWnd;
  215. DWORD dwThreadId;
  216. m_Thread.hThread = ::CreateThread( NULL, 0UL, ThreadProcCalibrationData, this, 0UL, &dwThreadId );
  217. return TRUE;
  218. }
  219. DWORD CCalibrationData::ThreadProc()
  220. {
  221. STARTUPINFO si;
  222. PROCESS_INFORMATION pi;
  223. ::ZeroMemory( &si, sizeof( STARTUPINFO ) );
  224. ::ZeroMemory( &pi, sizeof( PROCESS_INFORMATION ) );
  225. si.cb = sizeof( STARTUPINFO );
  226. si.dwFlags = STARTF_USESHOWWINDOW;
  227. si.wShowWindow = SW_SHOW;
  228. DWORD dwExitCode;
  229. if( ::CreateProcess( _T("exec\\camera_estimation.exe"), NULL, NULL,
  230. NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, _T("exec"), &si, &pi ) )
  231. {
  232. /* HANDLE hOutput = ::GetStdHandle( STD_OUTPUT_HANDLE );
  233. HANDLE hInput = ::GetStdHandle( STD_OUTPUT_HANDLE );
  234. HANDLE hPipe = ::CreatePipe( hOutput, hInput, NULL, 1UL );
  235. while( ::WaitForSingleObject( pi.hProces, 0UL )!=WAIT_OBJECT_0 )
  236. {
  237. }
  238. */
  239. HANDLE pHandles[] = { m_Thread.hEvent, pi.hProcess };
  240. switch( ::WaitForMultipleObjects( 2, pHandles, FALSE, INFINITE ) )
  241. {
  242. case WAIT_OBJECT_0 + 1:
  243. ::GetExitCodeProcess( pi.hProcess, &dwExitCode );
  244. ::PostMessage( m_Thread.hWnd, WM_CALIBRATION, TRUE, dwExitCode );
  245. break;
  246. case WAIT_OBJECT_0:
  247. ::TerminateProcess( pi.hProcess, (UINT)-1L );
  248. break;
  249. }
  250. }
  251. else
  252. ::PostMessage( m_Thread.hWnd, WM_CALIBRATION, FALSE, 0UL );
  253. if( pi.hThread )
  254. ::CloseHandle( pi.hThread );
  255. if( pi.hProcess )
  256. ::CloseHandle( pi.hProcess );
  257. return 0UL;
  258. }
  259. void CCalibrationData::GetFolderList( CStringList &list )
  260. {
  261. CString fpath = CString(g_pFNData) + _T("\\*.*");
  262. CFileFind finder;
  263. BOOL bWorking = finder.FindFile( fpath );
  264. while( bWorking )
  265. {
  266. bWorking = finder.FindNextFile();
  267. if( finder.IsDirectory() && finder.GetFileName()[0] != '.' )
  268. list.AddTail( finder.GetFileName() );
  269. }
  270. }
  271. void CCalibrationData::GetDataList( CString fname, CDataList &list )
  272. {
  273. CString fpath = CString(g_pFNData) + _T("\\*.*");
  274. CFileFind finder;
  275. BOOL bWorking = finder.FindFile( fpath );
  276. while( bWorking )
  277. {
  278. bWorking = finder.FindNextFile();
  279. if( finder.GetFilePath().Find( fname, 0 ) != -1 )
  280. {
  281. CString pname = finder.GetFilePath() + _T("\\") + g_pFNIn;
  282. CStdioFile file;
  283. if( file.Open( pname, CFile::modeRead | CFile::typeText ) )
  284. {
  285. DATACALC dc;
  286. CString str;
  287. while( file.ReadString( str ) )
  288. {
  289. LPTSTR pStr = (LPTSTR)(LPCTSTR)str;
  290. TCHAR pWord[MAX_PATH];
  291. GET_WORD( pWord, pStr ); dc.ma = atof( pWord );
  292. GET_WORD( pWord, pStr ); dc.mr = atof( pWord );
  293. GET_WORD( pWord, pStr ); dc.sa = atof( pWord );
  294. GET_WORD( pWord, pStr ); dc.sr = atof( pWord );
  295. GET_WORD( pWord, pStr ); dc.cp = atof( pWord );
  296. GET_WORD( pWord, pStr ); dc.ct = atof( pWord );
  297. GET_WORD( pWord, pStr ); dc.cz = atof( pWord );
  298. list.AddTail( dc );
  299. }
  300. file.Close();
  301. }
  302. }
  303. }
  304. }
  305. CString CCalibrationData::GetResultPath( CString fname )
  306. {
  307. CString fpath = CString(g_pFNData) + _T("\\*.*");
  308. CFileFind finder;
  309. BOOL bWorking = finder.FindFile( fpath );
  310. while( bWorking )
  311. {
  312. bWorking = finder.FindNextFile();
  313. if( finder.GetFilePath().Find( fname, 0 ) != -1 )
  314. {
  315. return finder.GetFilePath() + _T("\\") + g_pFNOut;
  316. }
  317. }
  318. return CString(_T(""));
  319. }
  320. CString CCalibrationData::GetReportPath( CString fname )
  321. {
  322. CString fpath = CString(g_pFNData) + _T("\\*.*");
  323. CFileFind finder;
  324. BOOL bWorking = finder.FindFile( fpath );
  325. while( bWorking )
  326. {
  327. bWorking = finder.FindNextFile();
  328. if( finder.GetFilePath().Find( fname, 0 ) != -1 )
  329. {
  330. return finder.GetFilePath() + _T("\\") + g_pFNReport;
  331. }
  332. }
  333. return CString(_T(""));
  334. }
  335. BOOL CCalibrationData::GetResult( CString fname, DATARES &drm, DATARES &drs )
  336. {
  337. CString fpath = CString(g_pFNData) + _T("\\*.*");
  338. CFileFind finder;
  339. BOOL bWorking = finder.FindFile( fpath );
  340. while( bWorking )
  341. {
  342. bWorking = finder.FindNextFile();
  343. if( finder.GetFilePath().Find( fname, 0 ) != -1 )
  344. {
  345. CString pname = finder.GetFilePath() + _T("\\") + g_pFNOut;
  346. CStdioFile file;
  347. if( file.Open( pname, CFile::modeRead | CFile::typeText ) )
  348. {
  349. CString str;
  350. BOOL bSuccess[] = { FALSE, FALSE };
  351. while( file.ReadString( str ) )
  352. {
  353. LPTSTR pStr = (LPTSTR)(LPCTSTR)str;
  354. TCHAR pWord[MAX_PATH];
  355. GET_WORD( pWord, pStr );
  356. if( !_tcscmp( pWord, _T("defm") ) )
  357. {
  358. GET_WORD( pWord, pStr ); drm.x = atof( pWord );
  359. GET_WORD( pWord, pStr ); drm.y = atof( pWord );
  360. GET_WORD( pWord, pStr ); drm.z = atof( pWord );
  361. GET_WORD( pWord, pStr ); drm.psi = atof( pWord );
  362. GET_WORD( pWord, pStr ); drm.theta = atof( pWord );
  363. GET_WORD( pWord, pStr ); drm.phi = atof( pWord );
  364. bSuccess[0] = TRUE;
  365. }
  366. else
  367. if( !_tcscmp( pWord, _T("defs") ) )
  368. {
  369. GET_WORD( pWord, pStr ); drs.x = atof( pWord );
  370. GET_WORD( pWord, pStr ); drs.y = atof( pWord );
  371. GET_WORD( pWord, pStr ); drs.z = atof( pWord );
  372. GET_WORD( pWord, pStr ); drs.psi = atof( pWord );
  373. GET_WORD( pWord, pStr ); drs.theta = atof( pWord );
  374. GET_WORD( pWord, pStr ); drs.phi = atof( pWord );
  375. bSuccess[1] = TRUE;
  376. }
  377. }
  378. file.Close();
  379. return bSuccess[0] && bSuccess[1]; // WARNING: needs to be checked in math
  380. }
  381. }
  382. }
  383. return FALSE;
  384. }
  385. void CCalibrationData::AddData( double ma, double mr, double sa, double sr, double cp, double ct, double cz )
  386. {
  387. DATACALC dc;
  388. dc.ma = ma;
  389. dc.mr = mr;
  390. dc.sa = sa;
  391. dc.sr = sr;
  392. dc.cp = cp;
  393. dc.ct = ct;
  394. dc.cz = cz;
  395. m_DataList.AddTail( dc );
  396. }