Locate.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. //恢复BeamShift的百分比值
  151. iSEM.SetBeamShiftX(pr.xs);
  152. Thread.Sleep(200);
  153. iSEM.SetBeamShiftY(pr.ys);
  154. Thread.Sleep(200);
  155. //恢复原来的放大倍数
  156. log.Info("恢复原来的放大倍数! " + pr.magCur);
  157. if (!iSEM.SetMagnification(pr.magCur))
  158. {
  159. log.Error("恢复放大倍数失败", false);
  160. return;
  161. }
  162. float cycle_time = iSEM.GetCycleTime();
  163. Thread.Sleep(100 + Convert.ToInt32(cycle_time));
  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. Thread.Sleep(200);
  203. iSEM.CmdFocusScanSpeed("CMD_SCANRATE5");
  204. //add by sun 2020-12-15 拍截面图调用蔡司接口时,要保证扫描速度5以上,自动亮度对比度功能关闭 end
  205. Thread.Sleep(200);
  206. float cycle_time = iSEM.GetCycleTime();
  207. Thread.Sleep(100 + Convert.ToInt32(cycle_time));
  208. return true;
  209. }
  210. public bool MoveRAxis(float value)
  211. {
  212. if (!iSEM.SetStageGotoR(value))
  213. {
  214. return false;
  215. }
  216. while (true)
  217. {
  218. Thread.Sleep(1000);
  219. if (iSEM.GetStageIs() == 0)
  220. {
  221. break;
  222. }
  223. }
  224. Thread.Sleep(200);
  225. iSEM.CmdFocusScanSpeed("CMD_SCANRATE5");//CmdFocusRate(5);
  226. //add by sun 2020-12-15 拍截面图调用蔡司接口时,要保证扫描速度5以上,自动亮度对比度功能关闭 end
  227. Thread.Sleep(200);
  228. float cycle_time = iSEM.GetCycleTime();
  229. Thread.Sleep(100 + Convert.ToInt32(cycle_time));
  230. return true;
  231. }
  232. public bool MoveZAxisByAbs(float value)
  233. {
  234. if (value < Z_Min || value > Z_Max)
  235. {
  236. log.Info("Z轴位置设置超出范围.", true);
  237. return false;
  238. }
  239. log.Info("将Z轴位置指定到:" + value.ToString(), true);
  240. if (!iSEM.SetStageGotoZ(value))
  241. {
  242. log.Error("样品台Z轴移到位置失败", false);
  243. return false;
  244. }
  245. while (true)
  246. {
  247. Thread.Sleep(1000);
  248. if (iSEM.GetStageIs() == 0)
  249. {
  250. break;
  251. }
  252. }
  253. Thread.Sleep(200);
  254. iSEM.CmdFocusScanSpeed("CMD_SCANRATE5");//CmdFocusRate(5);
  255. //add by sun 2020-12-15 拍截面图调用蔡司接口时,要保证扫描速度5以上,自动亮度对比度功能关闭 end
  256. Thread.Sleep(200);
  257. float cycle_time = iSEM.GetCycleTime();
  258. Thread.Sleep(100 + Convert.ToInt32(cycle_time));
  259. return true;
  260. }
  261. //移动到像素位置,这里要靠移动样品台实现,当位移量小于3um时,不予移动
  262. private bool MoveToPixByMoveStage()
  263. {
  264. float xc = prm.PositionX;
  265. float yc = prm.PositionY;
  266. //单位是m/pix
  267. float XpixSize = iSEM.GetPixelSize();
  268. log.Info("X像素尺寸=" + XpixSize.ToString() + "m/pixel", true);
  269. float YpixSize = iSEM.GetPixelSize();///(float)MParam.PixelSizeCor);
  270. log.Info("Y像素尺寸=" + YpixSize.ToString() + "m/pixel", true);
  271. Thread.Sleep(500);
  272. //0:width, 1:height
  273. int[] imageSize = iSEM.GetImageStore();
  274. int width = imageSize[0] / 2;
  275. int height = imageSize[1] / 2;
  276. log.Info("目标像素是(" + xc.ToString() + "," + yc.ToString() + ")", true);
  277. log.Info("中心像素是(" + width.ToString() + "," + height.ToString() + ")", true);
  278. float deltX = (xc - (float)width) * XpixSize;
  279. float deltY = (yc - (float)height) * YpixSize;
  280. log.Info("x位移量 = " + deltX.ToString() + "m", true);
  281. log.Info("y位移量 = " + deltY.ToString() + "m", true);
  282. float xpCur = iSEM.GetStageAtX();
  283. float ypCur = iSEM.GetStageAtY();
  284. log.Info("当前位置(" + xpCur.ToString() + "," + ypCur.ToString() + "),单位m", true);
  285. float xpNew = xpCur - deltX;
  286. float ypNew = ypCur + deltY;
  287. log.Info("目标位置(" + xpNew.ToString() + "," + ypNew.ToString() + "),单位m", true);
  288. log.Info("X方向移动样品台", true);
  289. if (!iSEM.SetStageGotoX(xpNew))
  290. {
  291. return false;
  292. }
  293. //判断是否移动完成
  294. while (true)
  295. {
  296. Thread.Sleep(1000);
  297. if (iSEM.GetStageIs() == 0)
  298. {
  299. break;
  300. }
  301. }
  302. log.Info("Y方向移动样品台", true);
  303. if (!iSEM.SetStageGotoY(ypNew))
  304. {
  305. return false;
  306. }
  307. //判断是否移动完成
  308. while (true)
  309. {
  310. Thread.Sleep(1000);
  311. if (iSEM.GetStageIs() == 0)
  312. {
  313. break;
  314. }
  315. }
  316. return true;
  317. }
  318. //移动到像素位置
  319. private bool MoveToPix()
  320. {
  321. float xc = prm.PositionX;
  322. float yc = prm.PositionY;
  323. //单位是m/pix
  324. float XpixSize = iSEM.GetPixelSize();
  325. log.Info("X像素尺寸=" + XpixSize.ToString() + "m/pixel", true);
  326. float YpixSize = (XpixSize / prm.PixelSize_Y_cur);
  327. log.Info("Y像素尺寸=" + YpixSize.ToString() + "m/pixel", true);
  328. Thread.Sleep(200);
  329. //0:width, 1:height
  330. int[] imageSize = iSEM.GetImageStore();
  331. int width = imageSize[0] / 2;
  332. int height = imageSize[1] / 2;
  333. log.Info("目标像素是(" + xc.ToString() + "," + yc.ToString() + ")", true);
  334. log.Info("中心像素是(" + width.ToString() + "," + height.ToString() + ")", true);
  335. float deltX = (xc - (float)width) * XpixSize;
  336. float deltY = (yc - (float)height) * YpixSize;
  337. log.Info("x位移量 = " + deltX.ToString() + "m", true);
  338. log.Info("y位移量 = " + deltY.ToString() + "m", true);
  339. float xpCur = iSEM.GetStageAtX();
  340. Thread.Sleep(200);
  341. float ypCur = iSEM.GetStageAtY();
  342. log.Info("当前位置(" + xpCur.ToString() + "," + ypCur.ToString() + "),单位m", true);
  343. float xpNew = xpCur - deltX;
  344. float ypNew = ypCur + deltY;
  345. log.Info("目标位置(" + xpNew.ToString() + "," + ypNew.ToString() + "),单位m", true);
  346. //计算最大偏移量
  347. float beamXCur = iSEM.GetBeamShiftX();//单位是%
  348. Thread.Sleep(200);
  349. iSEM.SetBeamShiftX(100);
  350. Thread.Sleep(200);
  351. float beamXMax = iSEM.GetBeamOffsetX();//单位是m
  352. Thread.Sleep(200);
  353. iSEM.SetBeamShiftX(beamXCur);
  354. Thread.Sleep(200);
  355. //计算光束偏移值:
  356. float beamX = iSEM.GetBeamOffsetX();
  357. log.Info("当前X方向光束偏移量=" + beamX.ToString() + "m", true);
  358. beamX = (-deltX + beamX);
  359. log.Info("X方向光束偏移量应为=" + beamX.ToString() + "m", true);
  360. if (((beamX <= beamXMax) && (beamX >= 0)) ||
  361. ((beamX >= -beamXMax) && (beamX < 0)))
  362. {
  363. log.Info("X方向移动光束", true);
  364. float beamXShift = beamX * 100 / beamXMax;
  365. log.Info("X方向光束偏移量应为=" + beamXShift.ToString() + "%", true);
  366. if (!iSEM.SetBeamShiftX(beamXShift))//if (!iSEM.SetBeamOffsetX(beamX))
  367. {
  368. log.Info("X方向光束偏移量" + beamX.ToString() + "m失败", true);
  369. return false;
  370. }
  371. }
  372. else if (deltX > 0.000003 || deltX < -0.000003)//大于3um使用移动样品台实现
  373. {
  374. log.Info("X方向移动样品台", true);
  375. if (!iSEM.SetStageGotoX(xpNew))
  376. {
  377. return false;
  378. }
  379. //判断是否移动完成
  380. while (true)
  381. {
  382. Thread.Sleep(1000);
  383. if (iSEM.GetStageIs() == 0)
  384. {
  385. break;
  386. }
  387. }
  388. }
  389. float beamY = iSEM.GetBeamOffsetY();
  390. log.Info("当前Y方向光束偏移量=" + beamY.ToString() + "m", true);
  391. beamY = (-deltY + beamY);
  392. log.Info("Y方向光束偏移量应为=" + beamY.ToString() + "m", true);
  393. //计算最大偏移量
  394. float beamYCur = iSEM.GetBeamShiftY();//单位是%
  395. Thread.Sleep(200);
  396. iSEM.SetBeamShiftY(100);
  397. Thread.Sleep(200);
  398. float beamYMax = iSEM.GetBeamOffsetY();//单位是m
  399. Thread.Sleep(200);
  400. iSEM.SetBeamShiftY(beamYCur);
  401. Thread.Sleep(200);
  402. if (((beamY <= beamYMax) && (beamY >= 0)) ||
  403. ((beamY >= -beamYMax) && (beamY < 0)))
  404. {
  405. log.Info("Y方向移动光束", true);
  406. float beamYShift = beamY * 100 / beamYMax;
  407. log.Info("Y方向光束偏移量" + beamYShift.ToString() + "%", true);
  408. //if(!iSEM.SetBeamOffsetY(beamY))
  409. if (!iSEM.SetBeamShiftY(beamYShift))
  410. {
  411. log.Info("Y方向光束偏移量" + beamY.ToString() + "m失败", true);
  412. return false;
  413. }
  414. }
  415. else if (deltY > 0.000003 || deltY < -0.000003)//大于3um使用移动样品台实现
  416. {
  417. log.Info("Y方向移动样品台", true);
  418. if (!iSEM.SetStageGotoY(ypNew))
  419. {
  420. return false;
  421. }
  422. //判断是否移动完成
  423. while (true)
  424. {
  425. Thread.Sleep(1000);
  426. if (iSEM.GetStageIs() == 0)
  427. {
  428. break;
  429. }
  430. }
  431. }
  432. return true;
  433. }
  434. public bool MoveToPixByMoveStage(float x, float y)
  435. {
  436. if (prm == null)
  437. {
  438. prm = new LocateParam();
  439. }
  440. this.prm.PositionX = x;
  441. this.prm.PositionY = y;
  442. MoveToPixByMoveStage();
  443. Thread.Sleep(200);
  444. Thread.Sleep(200);
  445. iSEM.CmdFocusScanSpeed("CMD_SCANRATE5");//CmdFocusRate(5);
  446. //add by sun 2020-12-15 拍截面图调用蔡司接口时,要保证扫描速度5以上,自动亮度对比度功能关闭 end
  447. Thread.Sleep(200);
  448. float cycle_time = iSEM.GetCycleTime();
  449. Thread.Sleep(100 + Convert.ToInt32(cycle_time));
  450. return true;
  451. }
  452. public bool MoveToPix(float x,float y,float pixelSize_Y_cur)
  453. {
  454. if (prm == null)
  455. {
  456. prm = new LocateParam();
  457. }
  458. this.prm.PositionX = x;
  459. this.prm.PositionY = y;
  460. this.prm.PixelSize_Y_cur = pixelSize_Y_cur;
  461. MoveToPix();
  462. Thread.Sleep(200);
  463. iSEM.CmdFocusScanSpeed("CMD_SCANRATE5");//CmdFocusRate(5);
  464. //add by sun 2020-12-15 拍截面图调用蔡司接口时,要保证扫描速度5以上,自动亮度对比度功能关闭 end
  465. Thread.Sleep(200);
  466. float cycle_time = iSEM.GetCycleTime();
  467. Thread.Sleep(100 + Convert.ToInt32(cycle_time));
  468. return true;
  469. }
  470. public LocateParam GetParam()
  471. {
  472. if (prm == null)
  473. {
  474. prm = new LocateParam();
  475. }
  476. return prm;
  477. }
  478. }
  479. }