Focus.cs 8.6 KB

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