Focus.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using NLog;
  2. using SmartSEMControl;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace MeasureThread
  10. {
  11. class Focus : IFocus
  12. {
  13. private Focusparam prm;
  14. private ISEMControl iSEM;
  15. private NLog.Logger log;
  16. public Focus( ISEMControl iSEM)
  17. {
  18. log = NLog.LogManager.GetCurrentClassLogger();
  19. this.iSEM = iSEM ?? throw new ArgumentNullException(nameof(iSEM));
  20. this.log = log ?? throw new ArgumentNullException(nameof(log));
  21. }
  22. public Focusparam GetParam()
  23. {
  24. if (prm == null)
  25. {
  26. prm = new Focusparam();
  27. }
  28. return prm;
  29. }
  30. public bool SetMagnification(float newMag)
  31. {
  32. bool mRetMag = iSEM.SetMagnification(newMag);
  33. //
  34. float set = newMag;
  35. Thread.Sleep(2000);
  36. float current = iSEM.GetMagnification();
  37. Thread.Sleep(200);
  38. while (Math.Abs(current - set) > 1)
  39. {
  40. iSEM.SetMagnification(set);
  41. Thread.Sleep(200);
  42. current = iSEM.GetMagnification();
  43. }
  44. return true;
  45. }
  46. public bool SetWorkingDistance(float newWdInMilimeter)
  47. {
  48. if (!iSEM.SetWorkingDistance(newWdInMilimeter/1000))//5.1mm 单位是米
  49. {
  50. log.Error("测量线程报错:重新设置到标准工作距离失败。", false);
  51. return false;
  52. }
  53. return true;
  54. }
  55. public bool DoFocusByNewMagnification(float newMag)
  56. {
  57. log.Warn("开始自动对焦!");
  58. // set magnification
  59. bool mRetMag = iSEM.SetMagnification(newMag);
  60. //
  61. float set = newMag;
  62. Thread.Sleep(1000);
  63. float current = iSEM.GetMagnification();
  64. Thread.Sleep(200);
  65. while (Math.Abs(current - set) > 1)
  66. {
  67. iSEM.SetMagnification(set);
  68. Thread.Sleep(200);
  69. current = iSEM.GetMagnification();
  70. }
  71. // set the window of focusing.
  72. iSEM.SetReduced(prm.reduceWindowPos.X, prm.reduceWindowPos.Y, (int)prm.reduceWinWidth, (int)prm.reduceWinHeight);
  73. iSEM.CmdFocusScanSpeed("CMD_SCANRATE4");//
  74. if (prm.IfAutoBrightnessAndContrast)
  75. {
  76. iSEM.SetAutoBright(100);
  77. Thread.Sleep(200);
  78. iSEM.SetAutoContrast(100);
  79. Thread.Sleep(200);
  80. iSEM.SetAutoVideoBrightnessAndContrast();
  81. Thread.Sleep(10000);//adjust the brightness and contrast automaticaly 10 second
  82. iSEM.SetAutoVideoOff();
  83. }
  84. else
  85. {
  86. iSEM.SetAutoVideoOff();
  87. Thread.Sleep(200);
  88. iSEM.SetBrightness(prm.brightness);//50.0f
  89. Thread.Sleep(200);
  90. iSEM.SetContrast(prm.contrast);//30.0f
  91. Thread.Sleep(200);
  92. }
  93. bool ifStig =false;
  94. if (newMag > 5000)
  95. {
  96. ifStig = true;
  97. }
  98. if (!ImageFocus1(ifStig))
  99. {
  100. log.Error("自动对焦失败,程序退出。", false);
  101. return false;
  102. }
  103. iSEM.CloseReduced();
  104. Thread.Sleep(200);
  105. log.Warn("自动对焦结束!"+"ifStig="+ifStig.ToString());
  106. return true;
  107. }
  108. public bool DoFocusBySetMainParameter()
  109. {
  110. log.Warn("开始自动对焦!");
  111. //set voltage and SemIprobe
  112. // bool mRet_VoltageFlag = iSEM.SetSEMVoltage(prm.voltage);
  113. // Thread.Sleep(500);
  114. //bool mRet_ElecFlag = iSEM.SetSEMIPROBE(prm.Iprobe / 1000000000);
  115. // Thread.Sleep(500);
  116. // set magnification
  117. bool mRetMag=iSEM.SetMagnification(prm.mag);
  118. Thread.Sleep(500);
  119. float current = iSEM.GetMagnification();
  120. while (Math.Abs(current - prm.mag) > 1)
  121. {
  122. iSEM.SetMagnification(prm.mag);
  123. Thread.Sleep(200);
  124. current = iSEM.GetMagnification();
  125. }
  126. //
  127. if (!TiltCorrection(prm.tiltCorrAngle))
  128. {
  129. return false;
  130. }
  131. float wd = iSEM.GetWorkingDistance();
  132. float offsetWd = Math.Abs(prm.workingDis - wd*1000);
  133. if (offsetWd > 3)//如果相差3mm以上,则重新设置,否则不动
  134. {
  135. log.Info("工作距离相差" + offsetWd + "mm");
  136. if (!iSEM.SetWorkingDistance(prm.workingDis / 1000))
  137. {
  138. return false;
  139. }
  140. }
  141. // set the window of focusing.
  142. iSEM.SetReduced(prm.reduceWindowPos.X, prm.reduceWindowPos.Y, (int)prm.reduceWinWidth, (int)prm.reduceWinHeight);
  143. iSEM.CmdFocusScanSpeed("CMD_SCANRATE4");//
  144. if (prm.IfAutoBrightnessAndContrast)
  145. {
  146. iSEM.SetAutoBright(100);
  147. Thread.Sleep(200);
  148. iSEM.SetAutoContrast(100);
  149. Thread.Sleep(200);
  150. iSEM.SetAutoVideoBrightnessAndContrast();
  151. Thread.Sleep(10000);
  152. iSEM.SetAutoVideoOff();
  153. }
  154. else
  155. {
  156. iSEM.SetAutoVideoOff();
  157. Thread.Sleep(200);
  158. iSEM.SetBrightness(prm.brightness);//50.0f
  159. Thread.Sleep(200);
  160. iSEM.SetContrast(prm.contrast);//30.0f
  161. Thread.Sleep(200);
  162. }
  163. bool ifStig = false;
  164. if (prm.mag > 5000)
  165. {
  166. ifStig = true;
  167. }
  168. else
  169. {
  170. ifStig = prm.ifStig;
  171. }
  172. if (!ImageFocus1(ifStig))
  173. {
  174. log.Error("自动对焦失败,程序退出。", false);
  175. return false;
  176. }
  177. iSEM.CloseReduced();
  178. Thread.Sleep(200);
  179. log.Warn("自动对焦结束!" + "ifStig=" + ifStig.ToString());
  180. return true;
  181. }
  182. public void setFocusParam(Focusparam param)
  183. {
  184. this.prm = param ;
  185. }
  186. //角度补偿
  187. public bool TiltCorrection(float a_fAngle)
  188. {
  189. float a = iSEM.GetTiltAngle();
  190. if (a != a_fAngle)
  191. {
  192. if (!iSEM.SetTiltAngleOn())
  193. {
  194. return false;
  195. }
  196. Thread.Sleep(200);
  197. //恢复原始状态
  198. if (!iSEM.SetTiltAngle(a_fAngle))
  199. {
  200. return false;
  201. }
  202. Thread.Sleep(200);
  203. }
  204. return true;
  205. }
  206. //执行自动对焦
  207. public bool ImageFocus1(Boolean Is_Stig)
  208. {
  209. #region 执行蔡司自动对焦程序
  210. //服务地址
  211. string snumstr = @"http://192.168.1.101:8123";
  212. bool normalized;
  213. // 调用方法;只对焦不消像散
  214. if (Is_Stig)
  215. {
  216. string focusstig = XmlRpcClient.autofocusstig(10, 3, 3, 3, 0.7, snumstr);//,normalized
  217. if (focusstig != "success")
  218. {
  219. return false;
  220. }
  221. }
  222. else
  223. {
  224. string onlyfocus = XmlRpcClient.autofocus(10, 3, 3, true, snumstr);//,normalized
  225. if (onlyfocus != "success")
  226. {
  227. return false;
  228. }
  229. }
  230. #endregion
  231. return true;
  232. }
  233. }
  234. }