Focus.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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(Focusparam prm, ISEMControl iSEM)
  17. {
  18. log = NLog.LogManager.GetCurrentClassLogger();
  19. this.prm = prm ?? throw new ArgumentNullException(nameof(prm));
  20. this.iSEM = iSEM ?? throw new ArgumentNullException(nameof(iSEM));
  21. this.log = log ?? throw new ArgumentNullException(nameof(log));
  22. }
  23. public bool DoFocusByNewMagnification(float newMag)
  24. {
  25. // set magnification
  26. bool mRetMag = iSEM.SetMagnification(newMag);
  27. Thread.Sleep(500);
  28. //
  29. // set the window of focusing.
  30. iSEM.SetReduced(prm.reduceWindowPos.X, prm.reduceWindowPos.Y, (int)prm.reduceWinWidth, (int)prm.reduceWinHeight);
  31. iSEM.CmdFocusScanSpeed("CMD_SCANRATE5");//
  32. if (prm.IfAutoBrightnessAndContrast)
  33. {
  34. iSEM.SetAutoVideoBrightnessAndContrast();
  35. Thread.Sleep(5000);
  36. }
  37. else
  38. {
  39. iSEM.SetAutoVideoOff();
  40. Thread.Sleep(200);
  41. iSEM.SetBrightness(prm.brightness);//50.0f
  42. Thread.Sleep(200);
  43. iSEM.SetContrast(prm.contrast);//30.0f
  44. Thread.Sleep(200);
  45. }
  46. if (!ImageFocus1(true))
  47. {
  48. log.Error("自动对焦失败,程序退出。", false);
  49. return false;
  50. }
  51. iSEM.CloseReduced();
  52. Thread.Sleep(200);
  53. iSEM.SetAutoVideoBrightnessAndContrast();
  54. float cycle_time = iSEM.GetCycleTime();
  55. Thread.Sleep(1000 + Convert.ToInt32(cycle_time));
  56. return true;
  57. }
  58. public bool DoFocus()
  59. {
  60. //set voltage and SemIprobe
  61. bool mRet_VoltageFlag = iSEM.SetSEMVoltage(prm.voltage);
  62. Thread.Sleep(500);
  63. bool mRet_ElecFlag = iSEM.SetSEMIPROBE(prm.Iprobe / 1000000000);
  64. Thread.Sleep(500);
  65. // set magnification
  66. bool mRetMag=iSEM.SetMagnification(prm.mag);
  67. //float current = iSEM.GetMagnification();
  68. //while (Math.Abs(current - prm.mag) > 1)
  69. //{
  70. // iSEM.SetMagnification(prm.mag);
  71. // Thread.Sleep(200);
  72. // current = iSEM.GetMagnification();
  73. //}
  74. Thread.Sleep(500);
  75. //
  76. if (!TiltCorrection(prm.tiltCorrAngle))
  77. {
  78. return false;
  79. }
  80. float wd = iSEM.GetWorkingDistance();
  81. float offsetWd = Math.Abs(prm.workingDis - wd*1000);
  82. if (offsetWd > 0.5)//如果相差0.5mm以上,则重新设置,否则不动
  83. {
  84. if (!iSEM.SetWorkingDistance(prm.workingDis / 1000))
  85. {
  86. return false;
  87. }
  88. }
  89. // set the window of focusing.
  90. iSEM.SetReduced(prm .reduceWindowPos.X, prm.reduceWindowPos.Y, (int)prm.reduceWinWidth, (int)prm .reduceWinHeight);
  91. iSEM.CmdFocusScanSpeed("CMD_SCANRATE5");//
  92. if (prm.IfAutoBrightnessAndContrast)
  93. {
  94. iSEM.SetAutoVideoBrightnessAndContrast();
  95. Thread.Sleep(5000);
  96. }
  97. else
  98. {
  99. iSEM.SetAutoVideoOff();
  100. Thread.Sleep(200);
  101. iSEM.SetBrightness(prm.brightness);//50.0f
  102. Thread.Sleep(200);
  103. iSEM.SetContrast(prm.contrast);//30.0f
  104. Thread.Sleep(200);
  105. }
  106. if (!ImageFocus1(true))
  107. {
  108. log.Error("自动对焦失败,程序退出。", false);
  109. return false;
  110. }
  111. iSEM.CloseReduced();
  112. Thread.Sleep(200);
  113. iSEM.SetAutoVideoBrightnessAndContrast();
  114. float cycle_time = iSEM.GetCycleTime();
  115. Thread.Sleep(1000 + Convert.ToInt32(cycle_time));
  116. return true;
  117. }
  118. public void setFocusParam(Focusparam param)
  119. {
  120. prm = param;
  121. }
  122. //角度补偿
  123. public bool TiltCorrection(float a_fAngle)
  124. {
  125. if (!iSEM.SetTiltAngleOn())
  126. {
  127. return false;
  128. }
  129. Thread.Sleep(200);
  130. //恢复原始状态
  131. if (!iSEM.SetTiltAngle(a_fAngle))
  132. {
  133. return false;
  134. }
  135. return true;
  136. }
  137. //执行自动对焦
  138. public bool ImageFocus1(Boolean Is_Stig)
  139. {
  140. #region 执行蔡司自动对焦程序
  141. //服务地址
  142. string snumstr = @"http://192.168.1.101:8123";
  143. bool normalized;
  144. // 调用方法;只对焦不消像散
  145. if (Is_Stig)
  146. {
  147. string focusstig = XmlRpcClient.autofocusstig(10, 3, 3, 3, 0.7, snumstr);//,normalized
  148. if (focusstig != "success")
  149. {
  150. return false;
  151. }
  152. }
  153. else
  154. {
  155. string onlyfocus = XmlRpcClient.autofocus(10, 3, 3, true, snumstr);//,normalized
  156. if (onlyfocus != "success")
  157. {
  158. return false;
  159. }
  160. }
  161. #endregion
  162. return true;
  163. }
  164. }
  165. }