OTSOxfordImpl.cpp 15 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. return true;
  248. }
  249. return false;
  250. }
  251. bool OxfordImpl::CollectXrayData(const long a_nAcTime, long* a_pnCounts, DWORD a_nBufferSize)
  252. {
  253. if (m_bInit)
  254. {
  255. if (!ManagedGlobals::oxfordController->CollectXrayPoint(a_nAcTime, a_pnCounts, a_nBufferSize))
  256. {
  257. LogErrorTrace(__FILE__, __LINE__, _T("CollectXrayData command failed."));
  258. return false;
  259. }
  260. return true;
  261. }
  262. return false;
  263. }
  264. bool OxfordImpl::CollectXrayDataAtPos(const double a_dPosX, const double a_dPosY, const long a_nAcTime, long* a_pnCounts, DWORD a_nBufferSize)
  265. {
  266. if (m_bInit)
  267. {
  268. if (!ManagedGlobals::oxfordController->CollectXrayPoint(a_dPosX, a_dPosY, a_nAcTime, a_pnCounts, a_nBufferSize))
  269. {
  270. LogErrorTrace(__FILE__, __LINE__, _T("CollectXrayDataAtPos command failed."));
  271. return false;
  272. }
  273. return true;
  274. }
  275. return false;
  276. }
  277. bool OxfordImpl::SetBeamPosition(const double a_dPosX, const double a_dPosY)
  278. {
  279. if ( m_bInit)
  280. {
  281. if (!ManagedGlobals::oxfordController->SetBeamPosition(a_dPosX, a_dPosY))
  282. {
  283. LogErrorTrace(__FILE__, __LINE__, _T("SetBeamPosition command failed."));
  284. return false;
  285. }
  286. return true;
  287. }
  288. return false;
  289. }
  290. bool OxfordImpl::SetScanSpeed(const long a_nMilliseconds)
  291. {
  292. if (m_bInit)
  293. {
  294. if (!ManagedGlobals::oxfordController->SetScanSpeed(a_nMilliseconds))
  295. {
  296. LogErrorTrace(__FILE__, __LINE__, _T("SetScanSpeed command failed."));
  297. return (int)OxfordWrapperErrorCode::COMMAND_FAILED;
  298. }
  299. return (int)OxfordWrapperErrorCode::SUCCEED;
  300. }
  301. return false;
  302. }
  303. bool OxfordImpl::GetImageSize(long& a_nWidth, long& a_nHeight)
  304. {
  305. if ( m_bInit)
  306. {
  307. if (!ManagedGlobals::oxfordController->GetImageSize(a_nWidth, a_nHeight))
  308. {
  309. LogErrorTrace(__FILE__, __LINE__, _T("GetImageSize command failed."));
  310. return (int)OxfordWrapperErrorCode::COMMAND_FAILED;
  311. }
  312. return (int)OxfordWrapperErrorCode::SUCCEED;
  313. }
  314. return false;
  315. }
  316. bool OxfordImpl::SetImageSize(const long a_nWidth, const long a_nHeight)
  317. {
  318. if (m_bInit)
  319. {
  320. if (!ManagedGlobals::oxfordController->SetImageSize(a_nWidth, a_nHeight))
  321. {
  322. LogErrorTrace(__FILE__, __LINE__, _T("SetImageSize command failed."));
  323. return (int)OxfordWrapperErrorCode::COMMAND_FAILED;
  324. }
  325. return (int)OxfordWrapperErrorCode::SUCCEED;
  326. }
  327. return false;
  328. }
  329. bool OxfordImpl::CollectImage(BYTE* a_pImageBits)
  330. {
  331. if (m_bInit)
  332. {
  333. if (!ManagedGlobals::oxfordController->CollectImage(a_pImageBits))
  334. {
  335. LogErrorTrace(__FILE__, __LINE__, _T("CollectImage command failed."));
  336. return (int)OxfordWrapperErrorCode::COMMAND_FAILED;
  337. }
  338. return (int)OxfordWrapperErrorCode::SUCCEED;
  339. }
  340. return false;
  341. }
  342. bool OxfordImpl::QuantifySpectrum(unsigned char* cResult)
  343. {
  344. if ( m_bInit)
  345. {
  346. if (!ManagedGlobals::oxfordController->QuantifySpectrum(cResult))
  347. {
  348. LogErrorTrace(__FILE__, __LINE__, _T("CollectImage command failed."));
  349. return (int)OxfordWrapperErrorCode::COMMAND_FAILED;
  350. }
  351. return (int)OxfordWrapperErrorCode::SUCCEED;
  352. }
  353. return false;
  354. }
  355. BOOL OxfordImpl::GetXRayByPoints(CPosXraysList& a_listXrayPois, DWORD a_nACTimeMS)
  356. {
  357. // do nothing if points list is empty
  358. if (a_listXrayPois.empty())
  359. {
  360. // points list is empty
  361. LogTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayByPoints: poits list is empty."));
  362. return TRUE;
  363. }
  364. // create array of BrukerSegment
  365. long nCollectCount = (long)a_listXrayPois.size();
  366. OxfordXrayData* segmentArray(new OxfordXrayData[nCollectCount]);
  367. for (int i = 0; i < nCollectCount; ++i)
  368. {
  369. CPoint poi = a_listXrayPois[i]->GetPosition();
  370. segmentArray[i].m_nPosX = poi.x;
  371. segmentArray[i].m_nPosY = poi.y;
  372. }
  373. if (!ManagedGlobals::oxfordController->CollectXrayPoints(a_nACTimeMS, segmentArray, nCollectCount, GENERALXRAYCHANNELS))
  374. {
  375. LogErrorTrace(__FILE__, __LINE__, _T("CollectXrayList command failed."));
  376. return (int)OxfordWrapperErrorCode::COMMAND_FAILED;
  377. }
  378. // get the specs for a_vXPoints
  379. for (int i = 0; i < nCollectCount; ++i)
  380. {
  381. // set spectrum data for the x-ray point
  382. a_listXrayPois[i]->SetXrayData((DWORD*)(segmentArray[i].m_pXrayData));
  383. if (m_bDoQuantification)
  384. {
  385. // quantify the spectrum
  386. CElementChemistriesList vElement = CElement::ExtractElementChemistrys(CControllerHelper::CharToString((const char*)segmentArray[i].m_strElementResult));
  387. a_listXrayPois[i]->SetElementQuantifyData(vElement);
  388. }
  389. }
  390. for (int i = 0; i < nCollectCount; ++i)
  391. {
  392. // check spectrum
  393. DWORD nTatolXrayCount = a_listXrayPois[i]->GetTotalCount();
  394. if (nTatolXrayCount < 20)
  395. {
  396. // captured an empty spectrum
  397. CPoint poi = a_listXrayPois[i]->GetPosition();
  398. // try to redo x-ray collection at the position
  399. static DWORD nChannelData[GENERALXRAYCHANNELS];
  400. memset(nChannelData, 0, sizeof(DWORD) * GENERALXRAYCHANNELS);
  401. if (!ManagedGlobals::oxfordController->CollectXrayPoint(a_nACTimeMS, (long*)nChannelData, GENERALXRAYCHANNELS))
  402. {
  403. // error
  404. CString s;
  405. s.Format(_T("Call CollectXRayPoint failed: index = %d(x:%d, y:%d))"), i, poi.x, poi.y);
  406. LogErrorTrace(__FILE__,__LINE__,s);
  407. return false;
  408. }
  409. // set spectrum with new spectrum
  410. a_listXrayPois[i]->SetXrayData(nChannelData);
  411. nTatolXrayCount = a_listXrayPois[i]->GetTotalCount();
  412. if (nTatolXrayCount < 20)
  413. {
  414. CString s1;
  415. s1.Format(_T("Single point spectrum still low count (%d counts), index = %d(x:%d, y:%d.) This could be caused by charging."),
  416. nTatolXrayCount, i, poi.x, poi.y);
  417. LogTrace(__FILE__, __LINE__, s1);
  418. }
  419. else
  420. {
  421. CString s2;
  422. s2.Format(_T("Single point spectrum collected successfully (%d counts), index = %d(x:%d, y:%d.)"),
  423. nTatolXrayCount, i, poi.x, poi.y);
  424. LogTrace(__FILE__, __LINE__, s2);
  425. }
  426. }
  427. }
  428. delete segmentArray;
  429. // ok return TRUE
  430. return TRUE;
  431. // error, return false
  432. return FALSE;
  433. }
  434. BOOL OxfordImpl::GetXRayByFeatures(CPosXraysList&a_listXrayPois, std::vector<BrukerFeature>& a_vFeatures, DWORD a_nXRayAQTime)
  435. {
  436. // do nothing if points list is empty
  437. if (a_listXrayPois.empty())
  438. {
  439. // points list is empty
  440. LogTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayByPoints: poits list is empty."));
  441. return TRUE;
  442. }
  443. if (a_listXrayPois.size() != a_vFeatures.size())
  444. {
  445. return FALSE;
  446. }
  447. // create array of BrukerSegment
  448. long nCollectCount = (long)a_listXrayPois.size();
  449. //convert the brukerfeature to oxfordfeature
  450. OxfordXrayData* features(new OxfordXrayData[nCollectCount]);
  451. int nTotalNum = 0;
  452. for (size_t i = 0; i < a_listXrayPois.size(); i++)
  453. {
  454. long nSegmentCount = (long)a_vFeatures[i].SegmentCount;
  455. features[i].m_nChordNum = nSegmentCount;
  456. long nPixelCount = 0;
  457. if (nSegmentCount > 0)
  458. {
  459. BrukerSegment* segs = a_vFeatures[i].pSegment;
  460. for (int j = 0; j < nSegmentCount; j++)
  461. {
  462. features[i].m_ChordList[j].m_nX = segs[j].XStart;
  463. features[i].m_ChordList[j].m_nY = segs[j].Y;
  464. features[i].m_ChordList[j].m_nLength = segs[j].XCount;
  465. nPixelCount += segs[j].XCount;
  466. }
  467. features[i].m_nPixelNum = nPixelCount;
  468. }
  469. else
  470. {
  471. // will generate according to the x-ray position
  472. // this shouldn't happen
  473. }
  474. nTotalNum += nPixelCount;
  475. }
  476. int a_nACTimeMS = a_nXRayAQTime / nTotalNum;
  477. if (!ManagedGlobals::oxfordController->CollectXrayArea(a_nXRayAQTime, features, nCollectCount, GENERALXRAYCHANNELS))
  478. {
  479. LogTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayArea failed"));
  480. return false;
  481. }
  482. // get the specs for a_vXPoints
  483. for (int i = 0; i < nCollectCount; ++i)
  484. {
  485. // set spectrum data for the x-ray point
  486. a_listXrayPois[i]->SetXrayData((DWORD*)(features[i].m_pXrayData));
  487. // set spectrum data for the x-ray point
  488. if (m_bDoQuantification)
  489. {
  490. // quantify the spectrum
  491. CElementChemistriesList vElement = CElement::ExtractElementChemistrys(CControllerHelper::CharToString((const char*)features[i].m_strElementResult));
  492. a_listXrayPois[i]->SetElementQuantifyData(vElement);
  493. }
  494. }
  495. for (int i = 0; i < nCollectCount; ++i)
  496. {
  497. // check spectrum
  498. DWORD nTatolXrayCount = a_listXrayPois[i]->GetTotalCount();
  499. if (nTatolXrayCount < 20)
  500. {
  501. // captured an empty spectrum
  502. CPoint poi = a_listXrayPois[i]->GetPosition();
  503. // try to redo x-ray collection at the position
  504. static DWORD nChannelData[GENERALXRAYCHANNELS];
  505. memset(nChannelData, 0, sizeof(DWORD) * GENERALXRAYCHANNELS);
  506. if (!ManagedGlobals::oxfordController->CollectXrayPoint(a_nXRayAQTime, (long*)nChannelData, GENERALXRAYCHANNELS))
  507. {
  508. LogErrorTrace(__FILE__, __LINE__, _T("CollectXrayData command failed."));
  509. return false;
  510. }
  511. // set spectrum with new spectrum
  512. a_listXrayPois[i]->SetXrayData(nChannelData);
  513. nTatolXrayCount = a_listXrayPois[i]->GetTotalCount();
  514. if (nTatolXrayCount < 20)
  515. {
  516. /*LogWarn(_T("Single point spectrum still low count (%d counts), index = %d(x:%d, y:%d.) This could be caused by charging."),
  517. nTatolXrayCount, i, poi.x, poi.y);*/
  518. }
  519. else
  520. {
  521. /*LogWarn(_T("Single point spectrum collected successfully (%d counts), index = %d(x:%d, y:%d.)"),
  522. nTatolXrayCount, i, poi.x, poi.y);*/
  523. }
  524. }
  525. }
  526. delete features;
  527. return TRUE;
  528. }
  529. }