Focus.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 DoFocus()
  24. {
  25. //set voltage and SemIprobe
  26. bool mRet_VoltageFlag = iSEM.SetSEMVoltage(prm.voltage);
  27. Thread.Sleep(500);
  28. bool mRet_ElecFlag = iSEM.SetSEMIPROBE(prm.Iprobe / 1000000000);
  29. Thread.Sleep(500);
  30. // set magnification
  31. bool mRetMag=iSEM.SetMagnification(prm.mag);
  32. //float current = iSEM.GetMagnification();
  33. //while (Math.Abs(current - prm.mag) > 1)
  34. //{
  35. // iSEM.SetMagnification(prm.mag);
  36. // Thread.Sleep(200);
  37. // current = iSEM.GetMagnification();
  38. //}
  39. Thread.Sleep(500);
  40. //
  41. if (!TiltCorrection(54.0f))
  42. {
  43. return false;
  44. }
  45. if (!iSEM.SetWorkingDistance(prm.workingDis/1000))
  46. {
  47. //log.Error("测量线程报错:重新设置到标准工作距离失败。", false);
  48. return false;
  49. }
  50. // set the window of focusing.
  51. iSEM.SetReduced(402, 128, 400, 400);
  52. iSEM.CmdFocusScanSpeed(prm.ScanSpeed);//
  53. if (prm.IfAutoBrightnessAndContrast)
  54. {
  55. iSEM.SetAutoVideoBrightnessAndContrast();
  56. Thread.Sleep(5000);
  57. }
  58. else
  59. {
  60. iSEM.SetAutoVideoOff();
  61. Thread.Sleep(200);
  62. iSEM.SetBrightness(prm.brightness);//50.0f
  63. Thread.Sleep(200);
  64. iSEM.SetContrast(prm.contrast);//30.0f
  65. Thread.Sleep(200);
  66. }
  67. if (!ImageFocus1(true))
  68. {
  69. log.Error("测量线程报错:拉直操作自动对焦失败,程序退出。", false);
  70. return false;
  71. }
  72. iSEM.CloseReduced();
  73. Thread.Sleep(200);
  74. iSEM.SetAutoVideoBrightnessAndContrast();
  75. float cycle_time = iSEM.GetCycleTime();
  76. Thread.Sleep(100 + Convert.ToInt32(cycle_time));
  77. return true;
  78. }
  79. public void setFocusParam(Focusparam param)
  80. {
  81. prm = param;
  82. }
  83. //角度补偿
  84. public bool TiltCorrection(float a_fAngle)
  85. {
  86. if (!iSEM.SetTiltAngleOn())
  87. {
  88. return false;
  89. }
  90. Thread.Sleep(200);
  91. //恢复原始状态
  92. if (!iSEM.SetTiltAngle(a_fAngle))
  93. {
  94. return false;
  95. }
  96. return true;
  97. }
  98. //执行自动对焦
  99. public bool ImageFocus1(Boolean Is_Stig)
  100. {
  101. #region 执行蔡司自动对焦程序
  102. //服务地址
  103. string snumstr = @"http://192.168.1.101:8123";
  104. bool normalized;
  105. // 调用方法;只对焦不消像散
  106. if (Is_Stig)
  107. {
  108. string focusstig = XmlRpcClient.autofocusstig(10, 3, 3, 3, 0.7, snumstr);//,normalized
  109. if (focusstig != "success")
  110. {
  111. return false;
  112. }
  113. }
  114. else
  115. {
  116. string onlyfocus = XmlRpcClient.autofocus(10, 3, 3, true, snumstr);//,normalized
  117. if (onlyfocus != "success")
  118. {
  119. return false;
  120. }
  121. }
  122. #endregion
  123. return true;
  124. }
  125. }
  126. }