CameraTaskThread.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /**************************************************************************************************
  2. 版本所有(C), 2020-2022, 北京普达迪泰
  3. ***************************************************************************************************
  4. 文 件 名: CameraTaskThread.cpp
  5. 功能描述: 相机标定处理
  6. 版 本 号: 1.00初版
  7. 作 者: HYF
  8. 生成日期; 2021-12-20
  9. 最近修改:
  10. 函数列表:
  11. 修改历史:
  12. 1.日 期 :2021-12-20
  13. 作 者 : HYF
  14. 修改内容 : 创建文件
  15. **************************************************************************************************/
  16. #include "pch.h"
  17. #include "IDPMSComm.h"
  18. #include "CameraTaskThread.h"
  19. IMPLEMENT_DYNCREATE(CameraTaskThread, CUIThread)
  20. BEGIN_MESSAGE_MAP(CameraTaskThread, CWinThread)
  21. // {{AFX_MSG_MAP(DynamicTaskThread)
  22. // NOTE - the ClassWizard will add and remove mapping macros here.
  23. ON_THREAD_MESSAGE(WM_SINGLE_CAM_CONFIG, OnSingleCamConfig)
  24. ON_THREAD_MESSAGE(WM_DOUBLE_CAM_CONFIG_STEP1, OnDoubleCamCofigStep1)
  25. ON_THREAD_MESSAGE(WM_DOUBLE_CAM_CONFIG_STEP2, OnDoubleCamCofigStep2)
  26. //}}AFX_MSG_MAP
  27. END_MESSAGE_MAP()
  28. CameraTaskThread::CameraTaskThread(void)
  29. {
  30. }
  31. CameraTaskThread::~CameraTaskThread(void)
  32. {
  33. }
  34. BOOL CameraTaskThread::InitInstance()
  35. {
  36. return TRUE;
  37. }
  38. /**************************************************************************************************
  39. 函 数 名 :OnSingleCamConfig
  40. 功能描述 :单相机标定
  41. 输入参数 :略
  42. 输出参数 :无
  43. 返 回 值 :void
  44. **************************************************************************************************/
  45. void CameraTaskThread::OnSingleCamConfig(WPARAM wParam, LPARAM lParam)
  46. {
  47. CString szCommand;
  48. BOOL bRet = TRUE;
  49. int iRet = 0;
  50. DMSResponse *pRes = NULL;
  51. int iSizeRecevied = 0;
  52. int iCam = (int)wParam;
  53. CString szPhotoPath;
  54. // 获取像片路径
  55. bRet = this->GetPhotoPath(szPhotoPath);
  56. if (!bRet) {
  57. LOG::E(_T("像片路径不存在"));
  58. this->PostMsg(WM_FAILURE, pRes);
  59. return;
  60. }
  61. // IDPMS通讯器
  62. IDPMSComm *pComm = new IDPMSComm();
  63. bRet = pComm->Open();
  64. if (!bRet) {
  65. this->PostMsg(WM_START_IDPMS, pRes);
  66. delete pComm;
  67. return;
  68. }
  69. // 1.结束当前工程
  70. // szCommand.Format("IDPMS-CD:SET0003");
  71. // iRet = pComm->SendCommand(szCommand, pRes);
  72. // if (iRet <= 0) {
  73. // pComm->Close();
  74. // this->PostMsg(WM_CAM_CONFIG_FAILURE, pRes);
  75. // delete pComm;
  76. // return;
  77. // }
  78. // 2. 删除之前的工程文件夹
  79. CString szCameraFolder;
  80. szCameraFolder.Format("C:\\PD\\IDPMS\\工程\\%d号相机", iCam);
  81. Utils::DeleteFolder(szCameraFolder);
  82. // 3.新建工程
  83. szCommand.Format("IDCMD0017(PDTL=%d号模板,PDSV=%d号相机,PDDT= false)", iCam, iCam);
  84. iRet = pComm->SendCommand(szCommand, pRes);
  85. if (iRet <= 0) {
  86. pComm->Close();
  87. this->PostMsg(WM_CAM_CONFIG_FAILURE, pRes);
  88. delete pComm;
  89. return;
  90. }
  91. /* 4.读取路径下相机相片
  92. szCommand.Format("IDPMS-CD:SET0008");
  93. bRet = pComm->SendCommand(szCommand, pRes);
  94. if (!bRet) {
  95. pComm->Close();
  96. this->PostMsg(WM_CAM_CONFIG_FAILURE, pRes);
  97. return;
  98. }
  99. */
  100. // // 5.读取当前路径下的全部相片
  101. // szCommand.Format("IDPMS-CD:SET1701:C:/PD/2号/外参");
  102. // bRet = pComm->SendCommand(szCommand, pRes);
  103. // if (!bRet) {
  104. // pComm->Close();
  105. // this->PostMsg(WM_CAM_CONFIG_FAILURE, pRes);
  106. //delete pComm;
  107. // return;
  108. // }
  109. // 6.读取当前路径下所有相片
  110. szCommand.Format("IDPMS-CD:SET1701:%s", szPhotoPath);
  111. iRet = pComm->SendCommand(szCommand, pRes, 1);
  112. if (iRet <= 0) {
  113. pComm->Close();
  114. this->PostMsg(WM_CAM_CONFIG_FAILURE, pRes);
  115. delete pComm;
  116. return;
  117. }
  118. //// 5.读取当前路径下的全部相片
  119. //szCommand.Format("IDPMS-CD:SET1701:D:/BD");
  120. //iRet = pComm->SendCommand(szCommand, pRes);
  121. //if (iRet <= 0) {
  122. // pComm->Close();
  123. // this->PostMsg(WM_CAM_CONFIG_FAILURE, pRes);
  124. // delete pComm;
  125. // return;
  126. //}
  127. // 6.开始自动测量
  128. szCommand.Format("IDCMD0016(PDST=true,PDGO=true,PDCE=true,PDWN=false,PDFP=false,PDSS=true)");
  129. iRet = pComm->SendCommand(szCommand, pRes, _T("CPDSimpleBundleStats"));
  130. if (iRet <= 0) {
  131. pComm->Close();
  132. this->PostMsg(WM_CAM_CONFIG_FAILURE, pRes);
  133. delete pComm;
  134. return;
  135. }
  136. // 关闭通讯器
  137. pComm->Close();
  138. this->PostMsg(WM_CAM_CONFIG_SUCCESS, pRes);
  139. return;
  140. }
  141. /**************************************************************************************************
  142. 函 数 名 :GetPhotoPath
  143. 功能描述 :获取最新像片路径
  144. 输入参数 :略
  145. 输出参数 :无
  146. 返 回 值 :void
  147. **************************************************************************************************/
  148. BOOL CameraTaskThread::GetPhotoPath(CString &szTargetPath)
  149. {
  150. CString szPhotoDir = AppConfig::GetString(_T("Directory"), _T("PhotoDir"));
  151. if (szPhotoDir.IsEmpty()) return FALSE;
  152. CFileFind finder;
  153. CString szSearchDir;
  154. szSearchDir.Format("%s%s", szPhotoDir, _T("\\*.*"));
  155. BOOL bFound = finder.FindFile(szSearchDir);
  156. CTime time0 = 0;
  157. CString szTargetDir;
  158. while (bFound) {
  159. bFound = finder.FindNextFile();
  160. CString szPath = finder.GetFilePath();
  161. if (finder.IsDirectory() && !finder.IsDots()) {
  162. CString szDirName = finder.GetFileName();
  163. CFileStatus fileStatus;
  164. CFile::GetStatus(szPhotoDir + "\\" + szDirName, fileStatus);
  165. CTime time1 = fileStatus.m_ctime;
  166. if (time0 < time1) {
  167. szTargetDir = szDirName;
  168. time0 = time1;
  169. }
  170. }
  171. }
  172. if (szTargetDir.IsEmpty()) return FALSE;
  173. szTargetPath.Append(szPhotoDir);
  174. szTargetPath.Append(_T("\\"));
  175. szTargetPath.Append(szTargetDir);
  176. return TRUE;
  177. }
  178. /**************************************************************************************************
  179. 函 数 名 :GetPhotoPath
  180. 功能描述 :获取最新像片路径
  181. 输入参数 :略
  182. 输出参数 :无
  183. 返 回 值 :void
  184. **************************************************************************************************/
  185. BOOL CameraTaskThread::GetFileName(CString &szPath, CString szPrefix, CString &szTargetFile)
  186. {
  187. if (szPath.IsEmpty()) return FALSE;
  188. int iPrefixLen = szPrefix.GetLength();
  189. CString szTName;
  190. CFileFind ff;
  191. CString szFilter;
  192. szFilter.Format("%s\\%s*.TDX", szPath, szPrefix);
  193. BOOL bFound = ff.FindFile(szFilter);
  194. CTime time0 = 0;
  195. CString szTargetDir;
  196. int iTIdex = 0;
  197. while (bFound) {
  198. bFound = ff.FindNextFile();
  199. CString szPath = ff.GetFilePath();
  200. if (!ff.IsDirectory() && !ff.IsDots()) {
  201. CString szFileTitle = ff.GetFileTitle();
  202. CString strIdx = szFileTitle.Right(szFileTitle.GetLength()- iPrefixLen);
  203. int idex = _ttoi(strIdx);
  204. if (idex > iTIdex) {
  205. szTName.Format("%s", szFileTitle);
  206. iTIdex = idex;
  207. }
  208. }
  209. }
  210. if (szTName.IsEmpty()) return FALSE;
  211. szTargetFile.Empty();
  212. szTargetFile.Append(szTName);
  213. return TRUE;
  214. }
  215. /**************************************************************************************************
  216. 函 数 名 :OnDoubleCamCofigStep1
  217. 功能描述 :双相机外参标定:建立工程
  218. 输入参数 :略
  219. 输出参数 :无
  220. 返 回 值 :void
  221. **************************************************************************************************/
  222. void CameraTaskThread::OnDoubleCamCofigStep1(WPARAM wParam, LPARAM lParam)
  223. {
  224. CString szCommand;
  225. BOOL bRet = TRUE;
  226. int iRet = 0;
  227. DMSResponse *pRes = NULL;
  228. int iSizeRecevied = 0;
  229. // IDPMS通讯器
  230. IDPMSComm *pComm = new IDPMSComm();
  231. bRet = pComm->Open();
  232. if (!bRet) {
  233. this->PostMsg(WM_START_IDPMS, pRes);
  234. delete pComm;
  235. return;
  236. }
  237. // 1.结束当前工程
  238. szCommand.Format("IDPMS-CD:SET0003");
  239. iRet = pComm->SendCommand(szCommand, pRes);
  240. if (iRet <= 0) {
  241. pComm->Close();
  242. this->PostMsg(WM_CAM_CONFIG_FAILURE, pRes);
  243. delete pComm;
  244. return;
  245. }
  246. // 2.删除之前的工程文件
  247. CString szCameraFolder;
  248. szCameraFolder.Format("C:\\PD\\IDPMS\\工程\\%s_%s_双相机", m_szEquipTypeCode, m_szEquipCode);
  249. Utils::DeleteFolder(szCameraFolder);
  250. // 3. Copy相机驱动文件
  251. CString szSourceCam;
  252. CString szTargetCam;
  253. szSourceCam.Format("C:\\PD\\IDPMS\\工程\\1号相机\\BDAData\\PDDT-IO50M-0001-7103HKFDJ-25.cam");
  254. szTargetCam.Format("C:\\Program Files\\PD\\IDPMS\\PDTemplate\\双相机\\PDDT-IO50M-0001-7103HKFDJ-25.cam");
  255. CopyFile(szSourceCam, szTargetCam, FALSE);
  256. szSourceCam.Format("C:\\PD\\IDPMS\\工程\\2号相机\\BDAData\\PDDT-IO50M-0002-7103HKFDJ-25.cam");
  257. szTargetCam.Format("C:\\Program Files\\PD\\IDPMS\\PDTemplate\\双相机\\PDDT-IO50M-0002-7103HKFDJ-25.cam");
  258. CopyFile(szSourceCam, szTargetCam, FALSE);
  259. // 4.双相机工程建立
  260. szCommand.Format("IDCMD0017(PDTL=双相机,PDSV=%s_%s_双相机,PDDT= false)", m_szEquipTypeCode, m_szEquipCode);
  261. iRet = pComm->SendCommand(szCommand, pRes);
  262. if (iRet <= 0) {
  263. pComm->Close();
  264. this->PostMsg(WM_CAM_CONFIG_FAILURE, pRes);
  265. delete pComm;
  266. return;
  267. }
  268. // 关闭通讯器
  269. pComm->Close();
  270. delete pComm;
  271. // 向视图发送完成消息
  272. this->PostMsg(WM_CAM_CONFIG_SUCCESS, pRes);
  273. }
  274. /**************************************************************************************************
  275. 函 数 名 :OnDoubleCamCofigStep2
  276. 功能描述 :双相机外参标定:外参标定
  277. 输入参数 :略
  278. 输出参数 :无
  279. 返 回 值 :void
  280. **************************************************************************************************/
  281. void CameraTaskThread::OnDoubleCamCofigStep2(WPARAM wParam, LPARAM lParam)
  282. {
  283. CString szCommand;
  284. BOOL bRet = TRUE;
  285. BOOL iRet = 0;
  286. DMSResponse *pRes = NULL;
  287. int iSizeRecevied = 0;
  288. // 打开通讯器
  289. IDPMSComm *pComm = new IDPMSComm();
  290. bRet = pComm->Open();
  291. if (!bRet) {
  292. this->PostMsg(WM_START_IDPMS, pRes);
  293. delete pComm;
  294. return;
  295. }
  296. // 1.选择机架文件作为模板
  297. szCommand.Format("IDPMS-CD:SET1401:PDFN=机架");
  298. iRet = pComm->SendCommand(szCommand, pRes);
  299. if (iRet <= 0) {
  300. pComm->Close();
  301. this->PostMsg(WM_CAM_CONFIG_FAILURE, pRes);
  302. delete pComm;
  303. return;
  304. }
  305. // 2.拍摄一次
  306. szCommand.Format("IDPMS-CD:SET2901");
  307. iRet = pComm->SendCommand(szCommand, pRes);
  308. if (iRet <= 0) {
  309. pComm->Close();
  310. this->PostMsg(WM_CAM_CONFIG_FAILURE, pRes);
  311. delete pComm;
  312. return;
  313. }
  314. Sleep(1000);
  315. // 3.选择机架上建立坐标点
  316. CString szFileFind;
  317. CString szTargetPath ;
  318. szTargetPath.Format("C:\\PD\\IDPMS\\工程\\%s_%s_双相机", m_szEquipTypeCode, m_szEquipCode);
  319. bRet = GetFileName(szTargetPath, _T("TRI3D E"), szFileFind);
  320. if (!bRet) {
  321. pComm->Close();
  322. this->PostMsg(WM_CAM_CONFIG_FAILURE, pRes);
  323. delete pComm;
  324. return;
  325. }
  326. szCommand.Format("IDCMD0025(PDFN=%s,PDMP=flase, PDDP=flase, PDCP=flase, PDPL=C1 C2 C3)", szFileFind);
  327. iRet = pComm->SendCommand(szCommand, pRes);
  328. if (iRet <= 0) {
  329. pComm->Close();
  330. this->PostMsg(WM_CAM_CONFIG_FAILURE, pRes);
  331. delete pComm;
  332. return;
  333. }
  334. // 4.将机架上建立坐标点拷贝到RO3D
  335. szCommand.Format("IDCMD0032(PDFN=RO3D,PDMP=true, PDDP=true, PDCP=true, PDAP=flase)");
  336. iRet = pComm->SendCommand(szCommand, pRes);
  337. if (iRet <= 0) {
  338. pComm->Close();
  339. this->PostMsg(WM_CAM_CONFIG_FAILURE, pRes);
  340. delete pComm;
  341. return;
  342. }
  343. // 4.设置RO3D为驱动
  344. szCommand.Format("IDCMD0004(PDFN=RO3D)");
  345. iRet = pComm->SendCommand(szCommand, pRes);
  346. if (iRet <= 0) {
  347. pComm->Close();
  348. this->PostMsg(WM_CAM_CONFIG_FAILURE, pRes);
  349. delete pComm;
  350. return;
  351. }
  352. // 3.轴对准
  353. szCommand.Format("IDPMS-CD:SET3701:p1=C1:p2=C2:p3=C3:p1x=Y:p1y=Z:p1z=X:p2type=type:p3type=type:sys=frame");
  354. iRet = pComm->SendCommand(szCommand, pRes);
  355. if (iRet <= 0) {
  356. pComm->Close();
  357. this->PostMsg(WM_CAM_CONFIG_FAILURE, pRes);
  358. delete pComm;
  359. return;
  360. }
  361. // 3.双相机快速标定
  362. //szCommand.Format("IDPMS-CD:SET2701");
  363. //iRet = pComm->SendCommand(szCommand);
  364. //if (iRet <= 0) {
  365. // szResult.Format("%s", pRes);
  366. // pComm->Close();
  367. // return FALSE;
  368. //}
  369. // 关闭通讯器
  370. pComm->Close();
  371. delete pComm;
  372. // 向视图发送完成消息
  373. this->PostMsg(WM_CAM_CONFIG_SUCCESS, pRes);
  374. }