OTSOxfordImpl.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. #include "stdafx.h"
  2. #include "OTSOxfordImpl.h"
  3. #include "ControllerHelper.h"
  4. #include "COTSUtilityDllFunExport.h"
  5. #include "OxfordImplConst.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #endif
  9. #include <Oxford/OxfordWrapper/OxfordControllerWrapper.h>
  10. namespace OTSController
  11. {
  12. OxfordImpl::OxfordImpl(void)
  13. : m_bInit(false)
  14. {
  15. if (ManagedGlobals::oxfordController == nullptr)
  16. {
  17. ManagedGlobals::oxfordController = gcnew OxfordControllerWrapper();
  18. }
  19. }
  20. OxfordImpl::~OxfordImpl(void)
  21. {
  22. CloseClient();
  23. }
  24. void OxfordImpl::CloseClient(void)
  25. {
  26. ManagedGlobals::oxfordController->CloseClient();
  27. }
  28. bool OxfordImpl::Connect()
  29. {
  30. if (!m_bInit)
  31. {
  32. OxfordWrapperErrorCode r;
  33. if (!ManagedGlobals::oxfordController->Init())
  34. {
  35. r=OxfordWrapperErrorCode::CONTROLLER_INIT_FAILED;
  36. return false;
  37. }
  38. if (!ManagedGlobals::oxfordController->IsConnected())
  39. {
  40. r=OxfordWrapperErrorCode::CONTROLLER_NOT_CONNECTED;
  41. return false;
  42. }
  43. r=OxfordWrapperErrorCode::SUCCEED;
  44. m_bInit = true;
  45. }
  46. return m_bInit;
  47. }
  48. bool OxfordImpl::DisConnect()
  49. {
  50. if (m_bInit)
  51. {
  52. CloseClient();
  53. m_bInit = false;
  54. }
  55. return true;
  56. }
  57. bool OxfordImpl::IsConnected()
  58. {
  59. if (m_bInit)
  60. {
  61. if (!ManagedGlobals::oxfordController->IsConnected())
  62. {
  63. return false;
  64. }
  65. return true;
  66. }
  67. return false;
  68. }
  69. bool OxfordImpl::GetPositionXY(double& a_dPosX, double& a_dPosY)
  70. {
  71. if (m_bInit)
  72. {
  73. if (!ManagedGlobals::oxfordController->GetPositionXY(a_dPosX, a_dPosY))
  74. {
  75. LogErrorTrace(__FILE__, __LINE__, _T("GetPositionXY command failed."));
  76. return false;
  77. }
  78. return true;
  79. }
  80. return false;
  81. }
  82. bool OxfordImpl::SetPositionXY(double a_dPosX, double a_dPosY)
  83. {
  84. if (m_bInit)
  85. {
  86. if (!ManagedGlobals::oxfordController->SetPositionXY(a_dPosX, a_dPosY))
  87. {
  88. LogErrorTrace(__FILE__, __LINE__, _T("SetPositionXY command failed."));
  89. return false;
  90. }
  91. return true;
  92. }
  93. return false;
  94. }
  95. bool OxfordImpl::GetWorkingDistance(double& a_dWorkingDistance)
  96. {
  97. if ( m_bInit)
  98. {
  99. if (!ManagedGlobals::oxfordController->GetWorkingDistance(a_dWorkingDistance))
  100. {
  101. LogErrorTrace(__FILE__, __LINE__, _T("GetWorkingDistance command failed."));
  102. return false;
  103. }
  104. return true;
  105. }
  106. return false;
  107. }
  108. bool OxfordImpl::SetWorkingDistance(double a_dWorkingDistance)
  109. {
  110. if (m_bInit)
  111. {
  112. if (!ManagedGlobals::oxfordController->SetWorkingDistance(a_dWorkingDistance))
  113. {
  114. LogErrorTrace(__FILE__, __LINE__, _T("SetWorkingDistance command failed."));
  115. return false;
  116. }
  117. return true;
  118. }
  119. return false;
  120. }
  121. bool OxfordImpl::GetMagnification(double& a_dMagnification)
  122. {
  123. if (m_bInit)
  124. {
  125. if (!ManagedGlobals::oxfordController->GetMagnification(a_dMagnification))
  126. {
  127. LogErrorTrace(__FILE__, __LINE__, _T("GetMagnification command failed."));
  128. return false;
  129. }
  130. return true;
  131. }
  132. return false;
  133. }
  134. bool OxfordImpl::SetMagnification(double a_dMagnification)
  135. {
  136. if (m_bInit)
  137. {
  138. if (!ManagedGlobals::oxfordController->SetMagnification(a_dMagnification))
  139. {
  140. LogErrorTrace(__FILE__, __LINE__, _T("SetMagnification command failed."));
  141. return false;
  142. }
  143. return true;
  144. }
  145. return false;
  146. }
  147. bool OxfordImpl::GetHighVoltage(double& a_dHighVoltage)
  148. {
  149. if ( m_bInit)
  150. {
  151. if (!ManagedGlobals::oxfordController->GetHighVoltage(a_dHighVoltage))
  152. {
  153. LogErrorTrace(__FILE__, __LINE__, _T("GetHighVoltage command failed."));
  154. return false;
  155. }
  156. return true;
  157. }
  158. return false;
  159. }
  160. bool OxfordImpl::SetHighVoltage(double a_dHighVoltage)
  161. {
  162. if (m_bInit)
  163. {
  164. if (!ManagedGlobals::oxfordController->SetHighVoltage(a_dHighVoltage))
  165. {
  166. LogErrorTrace(__FILE__, __LINE__, _T("SetHighVoltage command failed."));
  167. return false;
  168. }
  169. return true;
  170. }
  171. return false;
  172. }
  173. bool OxfordImpl::GetBeamOn(bool& a_bBeamOn)
  174. {
  175. if (m_bInit)
  176. {
  177. if (!ManagedGlobals::oxfordController->GetBeamOn(a_bBeamOn))
  178. {
  179. LogErrorTrace(__FILE__, __LINE__, _T("GetBeamOn command failed."));
  180. return false;
  181. }
  182. return true;
  183. }
  184. return false;
  185. }
  186. bool OxfordImpl::SetBeamOn(bool a_bBeamOn)
  187. {
  188. if ( m_bInit)
  189. {
  190. if (!ManagedGlobals::oxfordController->SetBeamOn(a_bBeamOn))
  191. {
  192. LogErrorTrace(__FILE__, __LINE__, _T("SetBeamOn command failed."));
  193. return false;
  194. }
  195. return true;
  196. }
  197. return false;
  198. }
  199. bool OxfordImpl::GetBeamBlank(bool& a_bBeamBlank)
  200. {
  201. if ( m_bInit)
  202. {
  203. if (!ManagedGlobals::oxfordController->GetBeamBlank(a_bBeamBlank))
  204. {
  205. LogErrorTrace(__FILE__, __LINE__, _T("GetBeamBlank command failed."));
  206. return false;
  207. }
  208. return true;
  209. }
  210. return false;
  211. }
  212. bool OxfordImpl::SetBeamBlank(bool a_bBeamBlank)
  213. {
  214. if (m_bInit)
  215. {
  216. if (!ManagedGlobals::oxfordController->SetBeamBlank(a_bBeamBlank))
  217. {
  218. LogErrorTrace(__FILE__, __LINE__, _T("SetBeamBlank command failed."));
  219. return false;
  220. }
  221. return true;
  222. }
  223. return false;
  224. }
  225. bool OxfordImpl::GetExternal(bool& a_bExternal)
  226. {
  227. if (m_bInit)
  228. {
  229. if (!ManagedGlobals::oxfordController->GetExternalScan(a_bExternal))
  230. {
  231. LogErrorTrace(__FILE__, __LINE__, _T("GetExternal command failed."));
  232. return false;
  233. }
  234. return true;
  235. }
  236. return false;
  237. }
  238. bool OxfordImpl::SetExternal(bool a_bExternal)
  239. {
  240. if ( m_bInit)
  241. {
  242. if (!ManagedGlobals::oxfordController->SetExternalScan(a_bExternal))
  243. {
  244. LogErrorTrace(__FILE__, __LINE__, _T("SetExternal command failed."));
  245. return false;
  246. }
  247. LogWarn(__FILE__, __LINE__, _T("SetExternal: ") + CString(std::to_string(a_bExternal).c_str()));
  248. return true;
  249. }
  250. return false;
  251. }
  252. bool OxfordImpl::CollectXrayData(const long a_nAcTime, long* a_pnCounts, DWORD a_nBufferSize)
  253. {
  254. if (m_bInit)
  255. {
  256. if (!ManagedGlobals::oxfordController->CollectXrayPoint(a_nAcTime, a_pnCounts, a_nBufferSize))
  257. {
  258. LogErrorTrace(__FILE__, __LINE__, _T("CollectXrayData command failed."));
  259. return false;
  260. }
  261. return true;
  262. }
  263. return false;
  264. }
  265. bool OxfordImpl::CollectXrayDataAtPos(const double a_dPosX, const double a_dPosY, const long a_nAcTime, long* a_pnCounts, DWORD a_nBufferSize)
  266. {
  267. if (m_bInit)
  268. {
  269. if (!ManagedGlobals::oxfordController->CollectXrayPoint(a_dPosX, a_dPosY, a_nAcTime, a_pnCounts, a_nBufferSize))
  270. {
  271. LogErrorTrace(__FILE__, __LINE__, _T("CollectXrayDataAtPos command failed."));
  272. return false;
  273. }
  274. return true;
  275. }
  276. return false;
  277. }
  278. bool OxfordImpl::SetBeamPosition(const double a_dPosX, const double a_dPosY)
  279. {
  280. if ( m_bInit)
  281. {
  282. if (!ManagedGlobals::oxfordController->SetBeamPosition(a_dPosX, a_dPosY))
  283. {
  284. LogErrorTrace(__FILE__, __LINE__, _T("SetBeamPosition command failed."));
  285. return false;
  286. }
  287. return true;
  288. }
  289. return false;
  290. }
  291. bool OxfordImpl::SetScanSpeed(const long a_nMilliseconds)
  292. {
  293. if (m_bInit)
  294. {
  295. if (!ManagedGlobals::oxfordController->SetScanSpeed(a_nMilliseconds))
  296. {
  297. LogErrorTrace(__FILE__, __LINE__, _T("SetScanSpeed command failed."));
  298. return (int)OxfordWrapperErrorCode::COMMAND_FAILED;
  299. }
  300. return (int)OxfordWrapperErrorCode::SUCCEED;
  301. }
  302. return false;
  303. }
  304. bool OxfordImpl::GetImageSize(long& a_nWidth, long& a_nHeight)
  305. {
  306. if ( m_bInit)
  307. {
  308. if (!ManagedGlobals::oxfordController->GetImageSize(a_nWidth, a_nHeight))
  309. {
  310. LogErrorTrace(__FILE__, __LINE__, _T("GetImageSize command failed."));
  311. return (int)OxfordWrapperErrorCode::COMMAND_FAILED;
  312. }
  313. return (int)OxfordWrapperErrorCode::SUCCEED;
  314. }
  315. return false;
  316. }
  317. bool OxfordImpl::SetImageSize(const long a_nWidth, const long a_nHeight)
  318. {
  319. if (m_bInit)
  320. {
  321. if (!ManagedGlobals::oxfordController->SetImageSize(a_nWidth, a_nHeight))
  322. {
  323. LogErrorTrace(__FILE__, __LINE__, _T("SetImageSize command failed."));
  324. return (int)OxfordWrapperErrorCode::COMMAND_FAILED;
  325. }
  326. return (int)OxfordWrapperErrorCode::SUCCEED;
  327. }
  328. return false;
  329. }
  330. bool OxfordImpl::CollectImage(BYTE* a_pImageBits)
  331. {
  332. if (m_bInit)
  333. {
  334. if (!ManagedGlobals::oxfordController->CollectImage(a_pImageBits))
  335. {
  336. LogErrorTrace(__FILE__, __LINE__, _T("CollectImage command failed."));
  337. return (int)OxfordWrapperErrorCode::COMMAND_FAILED;
  338. }
  339. return (int)OxfordWrapperErrorCode::SUCCEED;
  340. }
  341. return false;
  342. }
  343. bool OxfordImpl::QuantifySpectrum(unsigned char* cResult)
  344. {
  345. if ( m_bInit)
  346. {
  347. if (!ManagedGlobals::oxfordController->QuantifySpectrum(cResult))
  348. {
  349. LogErrorTrace(__FILE__, __LINE__, _T("CollectImage command failed."));
  350. return (int)OxfordWrapperErrorCode::COMMAND_FAILED;
  351. }
  352. return (int)OxfordWrapperErrorCode::SUCCEED;
  353. }
  354. return false;
  355. }
  356. BOOL OxfordImpl::GetXRayByPoints(CPosXraysList& a_listXrayPois, DWORD a_nACTimeMS)
  357. {
  358. // do nothing if points list is empty
  359. if (a_listXrayPois.empty())
  360. {
  361. // points list is empty
  362. LogTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayByPoints: poits list is empty."));
  363. return TRUE;
  364. }
  365. // create array of BrukerSegment
  366. long nCollectCount = (long)a_listXrayPois.size();
  367. OxfordXrayData* segmentArray(new OxfordXrayData[nCollectCount]);
  368. for (int i = 0; i < nCollectCount; ++i)
  369. {
  370. CPoint poi = a_listXrayPois[i]->GetPosition();
  371. segmentArray[i].m_nPosX = poi.x;
  372. segmentArray[i].m_nPosY = poi.y;
  373. }
  374. if (!ManagedGlobals::oxfordController->CollectXrayPoints(a_nACTimeMS, segmentArray, nCollectCount, GENERALXRAYCHANNELS))
  375. {
  376. LogErrorTrace(__FILE__, __LINE__, _T("CollectXrayList command failed."));
  377. return (int)OxfordWrapperErrorCode::COMMAND_FAILED;
  378. }
  379. // get the specs for a_vXPoints
  380. for (int i = 0; i < nCollectCount; ++i)
  381. {
  382. // set spectrum data for the x-ray point
  383. a_listXrayPois[i]->SetXrayData((DWORD*)(segmentArray[i].m_pXrayData));
  384. if (m_bDoQuantification)
  385. {
  386. // quantify the spectrum
  387. CElementChemistriesList vElement = CElement::ExtractElementChemistrys(CControllerHelper::CharToString((const char*)segmentArray[i].m_strElementResult));
  388. a_listXrayPois[i]->SetElementQuantifyData(vElement);
  389. }
  390. }
  391. for (int i = 0; i < nCollectCount; ++i)
  392. {
  393. // check spectrum
  394. DWORD nTatolXrayCount = a_listXrayPois[i]->GetTotalCount();
  395. if (nTatolXrayCount < 20)
  396. {
  397. // captured an empty spectrum
  398. CPoint poi = a_listXrayPois[i]->GetPosition();
  399. // try to redo x-ray collection at the position
  400. static DWORD nChannelData[GENERALXRAYCHANNELS];
  401. memset(nChannelData, 0, sizeof(DWORD) * GENERALXRAYCHANNELS);
  402. if (!ManagedGlobals::oxfordController->CollectXrayPoint(a_nACTimeMS, (long*)nChannelData, GENERALXRAYCHANNELS))
  403. {
  404. // error
  405. CString s;
  406. s.Format(_T("Call CollectXRayPoint failed: index = %d(x:%d, y:%d))"), i, poi.x, poi.y);
  407. LogErrorTrace(__FILE__,__LINE__,s);
  408. return false;
  409. }
  410. // set spectrum with new spectrum
  411. a_listXrayPois[i]->SetXrayData(nChannelData);
  412. nTatolXrayCount = a_listXrayPois[i]->GetTotalCount();
  413. if (nTatolXrayCount < 20)
  414. {
  415. CString s1;
  416. s1.Format(_T("Single point spectrum still low count (%d counts), index = %d(x:%d, y:%d.) This could be caused by charging."),
  417. nTatolXrayCount, i, poi.x, poi.y);
  418. LogTrace(__FILE__, __LINE__, s1);
  419. }
  420. else
  421. {
  422. CString s2;
  423. s2.Format(_T("Single point spectrum collected successfully (%d counts), index = %d(x:%d, y:%d.)"),
  424. nTatolXrayCount, i, poi.x, poi.y);
  425. LogTrace(__FILE__, __LINE__, s2);
  426. }
  427. }
  428. }
  429. delete segmentArray;
  430. // ok return TRUE
  431. return TRUE;
  432. // error, return false
  433. return FALSE;
  434. }
  435. BOOL OxfordImpl::GetXRayByFeatures(CPosXraysList&a_listXrayPois, std::vector<BrukerFeature>& a_vFeatures, DWORD a_nXRayAQTime)
  436. {
  437. // do nothing if points list is empty
  438. if (a_listXrayPois.empty())
  439. {
  440. // points list is empty
  441. LogTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayByPoints: poits list is empty."));
  442. return TRUE;
  443. }
  444. if (a_listXrayPois.size() != a_vFeatures.size())
  445. {
  446. return FALSE;
  447. }
  448. // create array of BrukerSegment
  449. long nCollectCount = (long)a_listXrayPois.size();
  450. //convert the brukerfeature to oxfordfeature
  451. OxfordXrayData* features(new OxfordXrayData[nCollectCount]);
  452. int nTotalNum = 0;
  453. for (size_t i = 0; i < a_listXrayPois.size(); i++)
  454. {
  455. long nSegmentCount = (long)a_vFeatures[i].SegmentCount;
  456. features[i].m_nChordNum = nSegmentCount;
  457. long nPixelCount = 0;
  458. if (nSegmentCount > 0)
  459. {
  460. BrukerSegment* segs = a_vFeatures[i].pSegment;
  461. for (int j = 0; j < nSegmentCount; j++)
  462. {
  463. features[i].m_ChordList[j].m_nX = segs[j].XStart;
  464. features[i].m_ChordList[j].m_nY = segs[j].Y;
  465. features[i].m_ChordList[j].m_nLength = segs[j].XCount;
  466. nPixelCount += segs[j].XCount;
  467. }
  468. features[i].m_nPixelNum = nPixelCount;
  469. }
  470. else
  471. {
  472. // will generate according to the x-ray position
  473. // this shouldn't happen
  474. }
  475. nTotalNum += nPixelCount;
  476. }
  477. int a_nACTimeMS = a_nXRayAQTime / nTotalNum;
  478. if (!ManagedGlobals::oxfordController->CollectXrayArea(a_nXRayAQTime, features, nCollectCount, GENERALXRAYCHANNELS))
  479. {
  480. LogTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayArea failed"));
  481. return false;
  482. }
  483. // get the specs for a_vXPoints
  484. for (int i = 0; i < nCollectCount; ++i)
  485. {
  486. // set spectrum data for the x-ray point
  487. a_listXrayPois[i]->SetXrayData((DWORD*)(features[i].m_pXrayData));
  488. // set spectrum data for the x-ray point
  489. if (m_bDoQuantification)
  490. {
  491. // quantify the spectrum
  492. CElementChemistriesList vElement = CElement::ExtractElementChemistrys(CControllerHelper::CharToString((const char*)features[i].m_strElementResult));
  493. a_listXrayPois[i]->SetElementQuantifyData(vElement);
  494. }
  495. }
  496. for (int i = 0; i < nCollectCount; ++i)
  497. {
  498. // check spectrum
  499. DWORD nTatolXrayCount = a_listXrayPois[i]->GetTotalCount();
  500. if (nTatolXrayCount < 20)
  501. {
  502. // captured an empty spectrum
  503. CPoint poi = a_listXrayPois[i]->GetPosition();
  504. // try to redo x-ray collection at the position
  505. static DWORD nChannelData[GENERALXRAYCHANNELS];
  506. memset(nChannelData, 0, sizeof(DWORD) * GENERALXRAYCHANNELS);
  507. if (!ManagedGlobals::oxfordController->CollectXrayPoint(a_nXRayAQTime, (long*)nChannelData, GENERALXRAYCHANNELS))
  508. {
  509. LogErrorTrace(__FILE__, __LINE__, _T("CollectXrayData command failed."));
  510. return false;
  511. }
  512. // set spectrum with new spectrum
  513. a_listXrayPois[i]->SetXrayData(nChannelData);
  514. nTatolXrayCount = a_listXrayPois[i]->GetTotalCount();
  515. if (nTatolXrayCount < 20)
  516. {
  517. /*LogWarn(_T("Single point spectrum still low count (%d counts), index = %d(x:%d, y:%d.) This could be caused by charging."),
  518. nTatolXrayCount, i, poi.x, poi.y);*/
  519. }
  520. else
  521. {
  522. /*LogWarn(_T("Single point spectrum collected successfully (%d counts), index = %d(x:%d, y:%d.)"),
  523. nTatolXrayCount, i, poi.x, poi.y);*/
  524. }
  525. }
  526. }
  527. delete features;
  528. return TRUE;
  529. }
  530. }