Focus.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. Thread.Sleep(200);
  45. float cycle_time = iSEM.GetCycleTime();
  46. Thread.Sleep(Convert.ToInt32(cycle_time) + 100);
  47. return true;
  48. }
  49. public bool SetWorkingDistance(float newWdInMilimeter)
  50. {
  51. if (!iSEM.SetWorkingDistance(newWdInMilimeter/1000))//5.1mm 单位是米
  52. {
  53. log.Error("测量线程报错:重新设置到标准工作距离失败。", false);
  54. return false;
  55. }
  56. return true;
  57. }
  58. public bool DoFocusByNewMagnification(float newMag)
  59. {
  60. log.Warn("开始自动对焦!");
  61. // set magnification
  62. bool mRetMag = iSEM.SetMagnification(newMag);
  63. //
  64. float set = newMag;
  65. Thread.Sleep(2000);
  66. float current = iSEM.GetMagnification();
  67. Thread.Sleep(200);
  68. while (Math.Abs(current - set) > 1)
  69. {
  70. iSEM.SetMagnification(set);
  71. Thread.Sleep(200);
  72. current = iSEM.GetMagnification();
  73. }
  74. Thread.Sleep(200);
  75. float cycle_time = iSEM.GetCycleTime();
  76. Thread.Sleep(Convert.ToInt32(cycle_time) + 100);
  77. // set the window of focusing.
  78. iSEM.SetReduced(prm.reduceWindowPos.X, prm.reduceWindowPos.Y, (int)prm.reduceWinWidth, (int)prm.reduceWinHeight);
  79. iSEM.CmdFocusScanSpeed("CMD_SCANRATE4");//
  80. if (prm.IfAutoBrightnessAndContrast)
  81. {
  82. iSEM.SetAutoBright(100);
  83. Thread.Sleep(200);
  84. iSEM.SetAutoContrast(100);
  85. Thread.Sleep(200);
  86. iSEM.SetAutoVideoBrightnessAndContrast();
  87. Thread.Sleep(10000);//adjust the brightness and contrast automaticaly 10 second
  88. iSEM.SetAutoVideoOff();
  89. }
  90. else
  91. {
  92. iSEM.SetAutoVideoOff();
  93. Thread.Sleep(200);
  94. iSEM.SetBrightness(prm.brightness);//50.0f
  95. Thread.Sleep(200);
  96. iSEM.SetContrast(prm.contrast);//30.0f
  97. Thread.Sleep(200);
  98. }
  99. bool ifStig =false;
  100. if (newMag > 5000)
  101. {
  102. ifStig = true;
  103. }
  104. if (!ImageFocus1(ifStig))
  105. {
  106. log.Error("自动对焦失败", false);
  107. iSEM.CloseReduced();
  108. return false;
  109. }
  110. iSEM.CloseReduced();
  111. Thread.Sleep(200);
  112. log.Warn("自动对焦结束!"+"ifStig="+ifStig.ToString());
  113. return true;
  114. }
  115. public bool DoFocusBySetMainParameter()
  116. {
  117. log.Warn("开始自动对焦!");
  118. // set magnification
  119. bool mRetMag=iSEM.SetMagnification(prm.mag);
  120. Thread.Sleep(2000);
  121. float current = iSEM.GetMagnification();
  122. while (Math.Abs(current - prm.mag) > 1)
  123. {
  124. iSEM.SetMagnification(prm.mag);
  125. Thread.Sleep(200);
  126. current = iSEM.GetMagnification();
  127. }
  128. Thread.Sleep(200);
  129. float cycle_time = iSEM.GetCycleTime();
  130. Thread.Sleep(Convert.ToInt32(cycle_time) + 100);
  131. log.Info("角度补充为:"+ prm.tiltCorrAngle.ToString());
  132. if (!TiltCorrection(prm.tiltCorrAngle))
  133. {
  134. return false;
  135. }
  136. float wd = iSEM.GetWorkingDistance();
  137. float offsetWd = Math.Abs(prm.workingDis - wd*1000);
  138. if (offsetWd > 3)//如果相差3mm以上,则重新设置,否则不动
  139. {
  140. log.Info("工作距离相差" + offsetWd + "mm");
  141. if (!iSEM.SetWorkingDistance(prm.workingDis / 1000))
  142. {
  143. return false;
  144. }
  145. Thread.Sleep(1000);
  146. }
  147. // set the window of focusing.
  148. iSEM.SetReduced(prm.reduceWindowPos.X, prm.reduceWindowPos.Y, (int)prm.reduceWinWidth, (int)prm.reduceWinHeight);
  149. iSEM.CmdFocusScanSpeed("CMD_SCANRATE4");//
  150. if (prm.IfAutoBrightnessAndContrast)
  151. {
  152. iSEM.SetAutoBright(100);
  153. Thread.Sleep(200);
  154. iSEM.SetAutoContrast(100);
  155. Thread.Sleep(200);
  156. iSEM.SetAutoVideoBrightnessAndContrast();
  157. Thread.Sleep(10000);
  158. iSEM.SetAutoVideoOff();
  159. }
  160. else
  161. {
  162. iSEM.SetAutoVideoOff();
  163. Thread.Sleep(200);
  164. iSEM.SetBrightness(prm.brightness);//50.0f
  165. Thread.Sleep(200);
  166. iSEM.SetContrast(prm.contrast);//30.0f
  167. Thread.Sleep(200);
  168. }
  169. bool ifStig = false;
  170. if (prm.mag > 5000)
  171. {
  172. ifStig = true;
  173. }
  174. else
  175. {
  176. ifStig = prm.ifStig;
  177. }
  178. if (!ImageFocus1(ifStig))
  179. {
  180. log.Warn("自动对焦失败", false);
  181. iSEM.CloseReduced();
  182. return false;
  183. }
  184. iSEM.CloseReduced();
  185. Thread.Sleep(200);
  186. log.Warn("自动对焦结束!" + "ifStig=" + ifStig.ToString());
  187. return true;
  188. }
  189. public void setFocusParam(Focusparam param)
  190. {
  191. this.prm = param ;
  192. }
  193. //角度补偿
  194. public bool TiltCorrection(float a_fAngle)
  195. {
  196. float a = iSEM.GetTiltAngle();
  197. if (a != a_fAngle)
  198. {
  199. if (!iSEM.SetTiltAngleOn())
  200. {
  201. return false;
  202. }
  203. Thread.Sleep(200);
  204. //恢复原始状态
  205. if (!iSEM.SetTiltAngle(a_fAngle))
  206. {
  207. return false;
  208. }
  209. Thread.Sleep(2000);
  210. }
  211. return true;
  212. }
  213. //执行自动对焦
  214. public bool ImageFocus1(Boolean Is_Stig)
  215. {
  216. #region 执行蔡司自动对焦程序
  217. //服务地址
  218. string snumstr = @"http://192.168.1.101:8123";
  219. bool normalized;
  220. // 调用方法;只对焦不消像散
  221. if (Is_Stig)
  222. {
  223. string focusstig = XmlRpcClient.autofocusstig(10, 3, 3, 3, 0.7, snumstr);//,normalized
  224. if (focusstig != "success")
  225. {
  226. return false;
  227. }
  228. }
  229. else
  230. {
  231. string onlyfocus = XmlRpcClient.autofocus(10, 3, 3, true, snumstr);//,normalized
  232. if (onlyfocus != "success")
  233. {
  234. return false;
  235. }
  236. }
  237. #endregion
  238. return true;
  239. }
  240. }
  241. }