Focus.cs 7.2 KB

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