Locate.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using NLog;
  8. using SmartSEMControl;
  9. namespace MeasureThread
  10. {
  11. class Locate : ILocate
  12. {
  13. struct PositionRecord
  14. {
  15. public float xs;
  16. public float ys;
  17. public float xpCur;
  18. public float ypCur;
  19. public float magCur;
  20. }
  21. private PositionRecord pr=new PositionRecord();
  22. NLog.Logger log;
  23. private LocateParam prm;
  24. private ISEMControl iSEM;
  25. const float Z_Min = 0;
  26. const float Z_Max = 0.05f;
  27. const float fSateyZ = (float)0.007;//单位是米
  28. public Locate(ISEMControl iSEM)
  29. {
  30. this.log = NLog.LogManager.GetCurrentClassLogger();
  31. this.iSEM = iSEM ?? throw new ArgumentNullException(nameof(iSEM));
  32. }
  33. public bool LocateCutHolePosition(MeasureData.CutHole currHole, bool ifTilt)
  34. {
  35. if (ifTilt)//如果是倾斜样品台,移动Z轴
  36. {
  37. //0、先移动Z轴到比较低的位置上。
  38. if (!MoveZAxis(fSateyZ, Locate.ZAxisDirection.down))
  39. {
  40. log.Error("测量线程报错:样品台Z轴移动失败,Z移动" + fSateyZ.ToString(), false);
  41. return false;
  42. }
  43. }
  44. //1、移动样品台到第一个观测点,先移动R轴,再移动XY轴
  45. if (!MoveRAxis(currHole.Position.R))
  46. {
  47. log.Error("测量线程报错:样品台R轴移动失败,R为" + currHole.Position.ToString(), false);
  48. //continue;
  49. return false;
  50. }
  51. //2、移动XY轴
  52. // lo.MoveToPixByMoveStage(currHole.Position.X,currHole.Position.Y)
  53. if (!iSEM.MoveStageXY(currHole.Position.X, currHole.Position.Y))
  54. {
  55. log.Error("测量线程报错:样品台XY轴移动失败,(X,Y)为"
  56. + currHole.Position.X.ToString() + ","
  57. + currHole.Position.Y.ToString() + ")", false);
  58. return false;
  59. }
  60. while (true)
  61. {
  62. Thread.Sleep(1000);
  63. if (iSEM.GetStageIs() == 0)
  64. {
  65. break;
  66. }
  67. }
  68. //判断是否停止进程
  69. //3、恢复Z轴
  70. if (ifTilt)
  71. {
  72. //恢复Z轴
  73. if (!MoveZAxisByAbs(currHole.Position.Z))
  74. {
  75. log.Error("测量线程报错:样品台Z轴移动失败,Z为" + currHole.Position.Z.ToString(), false);
  76. return false;
  77. }
  78. }
  79. //4、设置WD
  80. iSEM.SetWorkingDistance(currHole.Position.WD);
  81. Thread.Sleep(1000);
  82. return true;
  83. }
  84. public bool RotateStageByDeltaR(float deltaR)
  85. {
  86. float Zpos = iSEM.GetStageAtZ();
  87. Thread.Sleep(200);
  88. log.Info("测量线程:当前Z轴位置" + Zpos.ToString() + "m", true);
  89. Locate lo = new Locate(iSEM);
  90. //选择之前,先降Z轴
  91. lo.MoveZAxis(fSateyZ, Locate.ZAxisDirection.down);
  92. iSEM.SetStageDeltaR(deltaR);
  93. while (true)
  94. {
  95. Thread.Sleep(1000);
  96. if (iSEM.GetStageIs() == 0)
  97. {
  98. break;
  99. }
  100. }
  101. Thread.Sleep(1000);
  102. if (!MoveZAxisByAbs(Zpos))
  103. {
  104. log.Error("测量线程报错:样品台Z回到安全位置失败", false);
  105. return false;
  106. }
  107. return true;
  108. }
  109. public void RecordCurrentBeamShiftAndStagePosition()
  110. {
  111. #region 记录初始设置的BeamShift的百分比和样品台XY位置
  112. pr. xs = iSEM.GetBeamShiftX();
  113. Thread.Sleep(200);
  114. pr. ys = iSEM.GetBeamShiftY();
  115. Thread.Sleep(200);
  116. pr. xpCur = iSEM.GetStageAtX();
  117. Thread.Sleep(200);
  118. pr. ypCur = iSEM.GetStageAtY();
  119. Thread.Sleep(200);
  120. pr.magCur = iSEM.GetMagnification();
  121. Thread.Sleep(500);
  122. log.Info("原来的光束偏移量x = " + pr.xs.ToString()
  123. + "y=" + pr.ys.ToString() + ","
  124. + "原来样品台的位置x= " +pr. xpCur.ToString()
  125. + "y=" + pr.ypCur.ToString(), true);
  126. #endregion
  127. }
  128. public void RestoreLastBeamShiftAndStagePosition()
  129. {
  130. #region 恢复到拍照的状态
  131. log.Info("恢复到拍照的状态!");
  132. iSEM.SetStageGotoX(pr.xpCur);
  133. while (true)
  134. {
  135. Thread.Sleep(1000);
  136. if (iSEM.GetStageIs() == 0)
  137. {
  138. break;
  139. }
  140. }
  141. iSEM.SetStageGotoY(pr.ypCur);
  142. while (true)
  143. {
  144. Thread.Sleep(1000);
  145. if (iSEM.GetStageIs() == 0)
  146. {
  147. break;
  148. }
  149. }
  150. //恢复原来的放大倍数
  151. log.Info("恢复原来的放大倍数! " + pr.magCur);
  152. if (!iSEM.SetMagnification(pr.magCur))
  153. {
  154. log.Error("恢复放大倍数失败", false);
  155. return;
  156. }
  157. float cycle_time = iSEM.GetCycleTime();
  158. Thread.Sleep(100 + Convert.ToInt32(cycle_time));
  159. //恢复BeamShift的百分比值
  160. iSEM.SetBeamShiftX(pr.xs);
  161. Thread.Sleep(200);
  162. iSEM.SetBeamShiftY(pr.ys);
  163. Thread.Sleep(200);
  164. #endregion
  165. }
  166. public enum ZAxisDirection
  167. {
  168. up=0,
  169. down=1
  170. }
  171. public bool MoveZAxis(float value, ZAxisDirection d )
  172. {
  173. if (value < Z_Min || value> Z_Max)
  174. {
  175. log.Info("Z轴位置设置超出范围.", true);
  176. return false;
  177. }
  178. float curZ = iSEM.GetStageAtZ();
  179. float targetZ;
  180. if (d == ZAxisDirection.up)
  181. {
  182. targetZ = curZ + value;
  183. }
  184. else
  185. {
  186. targetZ = curZ - value;
  187. }
  188. log.Info("将Z轴位置指定到:" + value.ToString(), true);
  189. if (!iSEM.SetStageGotoZ(targetZ))
  190. {
  191. log.Error("样品台Z轴移到位置失败", false);
  192. return false;
  193. }
  194. while (true)
  195. {
  196. Thread.Sleep(1000);
  197. if (iSEM.GetStageIs() == 0)
  198. {
  199. break;
  200. }
  201. }
  202. return true;
  203. }
  204. public bool MoveRAxis(float value)
  205. {
  206. if (!iSEM.SetStageGotoR(value))
  207. {
  208. return false;
  209. }
  210. while (true)
  211. {
  212. Thread.Sleep(1000);
  213. if (iSEM.GetStageIs() == 0)
  214. {
  215. break;
  216. }
  217. }
  218. return true;
  219. }
  220. public bool MoveZAxisByAbs(float value)
  221. {
  222. if (value < Z_Min || value > Z_Max)
  223. {
  224. log.Info("Z轴位置设置超出范围.", true);
  225. return false;
  226. }
  227. log.Info("将Z轴位置指定到:" + value.ToString(), true);
  228. if (!iSEM.SetStageGotoZ(value))
  229. {
  230. log.Error("样品台Z轴移到位置失败", false);
  231. return false;
  232. }
  233. while (true)
  234. {
  235. Thread.Sleep(1000);
  236. if (iSEM.GetStageIs() == 0)
  237. {
  238. break;
  239. }
  240. }
  241. return true;
  242. }
  243. //移动到像素位置,这里要靠移动样品台实现,当位移量小于3um时,不予移动
  244. private bool MoveToPixByMoveStage()
  245. {
  246. float xc = prm.PositionX;
  247. float yc = prm.PositionY;
  248. //单位是m/pix
  249. float XpixSize = iSEM.GetPixelSize();
  250. log.Info("X像素尺寸=" + XpixSize.ToString() + "m/pixel", true);
  251. float YpixSize = iSEM.GetPixelSize();///(float)MParam.PixelSizeCor);
  252. log.Info("Y像素尺寸=" + YpixSize.ToString() + "m/pixel", true);
  253. Thread.Sleep(500);
  254. //0:width, 1:height
  255. int[] imageSize = iSEM.GetImageStore();
  256. int width = imageSize[0] / 2;
  257. int height = imageSize[1] / 2;
  258. log.Info("目标像素是(" + xc.ToString() + "," + yc.ToString() + ")", true);
  259. log.Info("中心像素是(" + width.ToString() + "," + height.ToString() + ")", true);
  260. float deltX = (xc - (float)width) * XpixSize;
  261. float deltY = (yc - (float)height) * YpixSize;
  262. log.Info("x位移量 = " + deltX.ToString() + "m", true);
  263. log.Info("y位移量 = " + deltY.ToString() + "m", true);
  264. float xpCur = iSEM.GetStageAtX();
  265. float ypCur = iSEM.GetStageAtY();
  266. log.Info("当前位置(" + xpCur.ToString() + "," + ypCur.ToString() + "),单位m", true);
  267. float xpNew = xpCur - deltX;
  268. float ypNew = ypCur + deltY;
  269. log.Info("目标位置(" + xpNew.ToString() + "," + ypNew.ToString() + "),单位m", true);
  270. log.Info("X方向移动样品台", true);
  271. if (!iSEM.SetStageGotoX(xpNew))
  272. {
  273. return false;
  274. }
  275. //判断是否移动完成
  276. while (true)
  277. {
  278. Thread.Sleep(1000);
  279. if (iSEM.GetStageIs() == 0)
  280. {
  281. break;
  282. }
  283. }
  284. log.Info("Y方向移动样品台", true);
  285. if (!iSEM.SetStageGotoY(ypNew))
  286. {
  287. return false;
  288. }
  289. //判断是否移动完成
  290. while (true)
  291. {
  292. Thread.Sleep(1000);
  293. if (iSEM.GetStageIs() == 0)
  294. {
  295. break;
  296. }
  297. }
  298. return true;
  299. }
  300. //移动到像素位置
  301. private bool MoveToPix()
  302. {
  303. float xc = prm.PositionX;
  304. float yc = prm.PositionY;
  305. //单位是m/pix
  306. float XpixSize = iSEM.GetPixelSize();
  307. log.Info("X像素尺寸=" + XpixSize.ToString() + "m/pixel", true);
  308. float YpixSize = (XpixSize / prm.PixelSize_Y_cur);
  309. log.Info("Y像素尺寸=" + YpixSize.ToString() + "m/pixel", true);
  310. Thread.Sleep(200);
  311. //0:width, 1:height
  312. int[] imageSize = iSEM.GetImageStore();
  313. int width = imageSize[0] / 2;
  314. int height = imageSize[1] / 2;
  315. log.Info("目标像素是(" + xc.ToString() + "," + yc.ToString() + ")", true);
  316. log.Info("中心像素是(" + width.ToString() + "," + height.ToString() + ")", true);
  317. float deltX = (xc - (float)width) * XpixSize;
  318. float deltY = (yc - (float)height) * YpixSize;
  319. log.Info("x位移量 = " + deltX.ToString() + "m", true);
  320. log.Info("y位移量 = " + deltY.ToString() + "m", true);
  321. float xpCur = iSEM.GetStageAtX();
  322. Thread.Sleep(200);
  323. float ypCur = iSEM.GetStageAtY();
  324. log.Info("当前位置(" + xpCur.ToString() + "," + ypCur.ToString() + "),单位m", true);
  325. float xpNew = xpCur - deltX;
  326. float ypNew = ypCur + deltY;
  327. log.Info("目标位置(" + xpNew.ToString() + "," + ypNew.ToString() + "),单位m", true);
  328. //计算最大偏移量
  329. float beamXCur = iSEM.GetBeamShiftX();//单位是%
  330. Thread.Sleep(200);
  331. iSEM.SetBeamShiftX(100);
  332. Thread.Sleep(200);
  333. float beamXMax = iSEM.GetBeamOffsetX();//单位是m
  334. Thread.Sleep(200);
  335. iSEM.SetBeamShiftX(beamXCur);
  336. Thread.Sleep(200);
  337. //计算光束偏移值:
  338. float beamX = iSEM.GetBeamOffsetX();
  339. log.Info("当前X方向光束偏移量=" + beamX.ToString() + "m", true);
  340. beamX = (-deltX + beamX);
  341. log.Info("X方向光束偏移量应为=" + beamX.ToString() + "m", true);
  342. if (((beamX <= beamXMax) && (beamX >= 0)) ||
  343. ((beamX >= -beamXMax) && (beamX < 0)))
  344. {
  345. log.Info("X方向移动光束", true);
  346. float beamXShift = beamX * 100 / beamXMax;
  347. log.Info("X方向光束偏移量应为=" + beamXShift.ToString() + "%", true);
  348. if (!iSEM.SetBeamShiftX(beamXShift))//if (!iSEM.SetBeamOffsetX(beamX))
  349. {
  350. log.Info("X方向光束偏移量" + beamX.ToString() + "m失败", true);
  351. return false;
  352. }
  353. }
  354. else if (deltX > 0.000003 || deltX < -0.000003)//大于3um使用移动样品台实现
  355. {
  356. log.Info("X方向移动样品台", true);
  357. if (!iSEM.SetStageGotoX(xpNew))
  358. {
  359. return false;
  360. }
  361. //判断是否移动完成
  362. while (true)
  363. {
  364. Thread.Sleep(1000);
  365. if (iSEM.GetStageIs() == 0)
  366. {
  367. break;
  368. }
  369. }
  370. }
  371. float beamY = iSEM.GetBeamOffsetY();
  372. log.Info("当前Y方向光束偏移量=" + beamY.ToString() + "m", true);
  373. beamY = (-deltY + beamY);
  374. log.Info("Y方向光束偏移量应为=" + beamY.ToString() + "m", true);
  375. //计算最大偏移量
  376. float beamYCur = iSEM.GetBeamShiftY();//单位是%
  377. Thread.Sleep(200);
  378. iSEM.SetBeamShiftY(100);
  379. Thread.Sleep(200);
  380. float beamYMax = iSEM.GetBeamOffsetY();//单位是m
  381. Thread.Sleep(200);
  382. iSEM.SetBeamShiftY(beamYCur);
  383. Thread.Sleep(200);
  384. if (((beamY <= beamYMax) && (beamY >= 0)) ||
  385. ((beamY >= -beamYMax) && (beamY < 0)))
  386. {
  387. log.Info("Y方向移动光束", true);
  388. float beamYShift = beamY * 100 / beamYMax;
  389. log.Info("Y方向光束偏移量" + beamYShift.ToString() + "%", true);
  390. //if(!iSEM.SetBeamOffsetY(beamY))
  391. if (!iSEM.SetBeamShiftY(beamYShift))
  392. {
  393. log.Info("Y方向光束偏移量" + beamY.ToString() + "m失败", true);
  394. return false;
  395. }
  396. }
  397. else if (deltY > 0.000003 || deltY < -0.000003)//大于3um使用移动样品台实现
  398. {
  399. log.Info("Y方向移动样品台", true);
  400. if (!iSEM.SetStageGotoY(ypNew))
  401. {
  402. return false;
  403. }
  404. //判断是否移动完成
  405. while (true)
  406. {
  407. Thread.Sleep(1000);
  408. if (iSEM.GetStageIs() == 0)
  409. {
  410. break;
  411. }
  412. }
  413. }
  414. return true;
  415. }
  416. public bool MoveToPixByMoveStage(float x, float y)
  417. {
  418. if (prm == null)
  419. {
  420. prm = new LocateParam();
  421. }
  422. this.prm.PositionX = x;
  423. this.prm.PositionY = y;
  424. MoveToPixByMoveStage();
  425. Thread.Sleep(200);
  426. return true;
  427. }
  428. public bool MoveToPix(float x,float y,float pixelSize_Y_cur)
  429. {
  430. if (prm == null)
  431. {
  432. prm = new LocateParam();
  433. }
  434. this.prm.PositionX = x;
  435. this.prm.PositionY = y;
  436. this.prm.PixelSize_Y_cur = pixelSize_Y_cur;
  437. MoveToPix();
  438. Thread.Sleep(200);
  439. return true;
  440. }
  441. public LocateParam GetParam()
  442. {
  443. if (prm == null)
  444. {
  445. prm = new LocateParam();
  446. }
  447. return prm;
  448. }
  449. }
  450. }