Locate.cs 18 KB

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