123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- using NLog;
- using SmartSEMControl;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace MeasureThread
- {
- class Focus : IFocus
- {
- private Focusparam prm;
- private ISEMControl iSEM;
- private string svrIP;
- private string svrPort;
- private NLog.Logger log;
- public Focus( ISEMControl iSEM,string a_svrIp,string a_svrPort)
- {
- log = NLog.LogManager.GetCurrentClassLogger();
-
- this.iSEM = iSEM ?? throw new ArgumentNullException(nameof(iSEM));
- this.log = log ?? throw new ArgumentNullException(nameof(log));
- svrIP = a_svrIp;
- svrPort = a_svrPort;
- }
- public Focusparam GetParam()
- {
- if (prm == null)
- {
- prm = new Focusparam();
- }
- return prm;
- }
- public bool SetMagnification(float newMag)
- {
- bool mRetMag = iSEM.SetMagnification(newMag);
- //
- float set = newMag;
- Thread.Sleep(2000);
- float current = iSEM.GetMagnification();
- Thread.Sleep(200);
- while (Math.Abs(current - set) > 1)
- {
- iSEM.SetMagnification(set);
- Thread.Sleep(200);
- current = iSEM.GetMagnification();
- }
- Thread.Sleep(200);
- float cycle_time = iSEM.GetCycleTime();
- Thread.Sleep(Convert.ToInt32(cycle_time) + 100);
- return true;
- }
- public bool SetWorkingDistance(float newWdInMilimeter)
- {
-
- if (!iSEM.SetWorkingDistance(newWdInMilimeter/1000))//5.1mm 单位是米
- {
- log.Error("测量线程报错:重新设置到标准工作距离失败。", false);
- return false;
- }
- return true;
- }
-
- public bool DoFocusByNewMagnification(float newMag)
- {
- log.Warn("开始自动对焦!");
- // set magnification
- bool mRetMag = iSEM.SetMagnification(newMag);
-
- //
- float set = newMag;
- Thread.Sleep(2000);
- float current = iSEM.GetMagnification();
- Thread.Sleep(200);
- while (Math.Abs(current - set) > 1)
- {
- iSEM.SetMagnification(set);
- Thread.Sleep(200);
- current = iSEM.GetMagnification();
- }
- Thread.Sleep(200);
- float cycle_time = iSEM.GetCycleTime();
- Thread.Sleep(Convert.ToInt32(cycle_time) + 100);
- // set the window of focusing.
- iSEM.SetReduced(prm.reduceWindowPos.X, prm.reduceWindowPos.Y, (int)prm.reduceWinWidth, (int)prm.reduceWinHeight);
- iSEM.CmdFocusScanSpeed("CMD_SCANRATE4");//
- if (prm.IfAutoBrightnessAndContrast)
- {
- iSEM.SetAutoBright(100);
- Thread.Sleep(200);
- iSEM.SetAutoContrast(100);
- Thread.Sleep(200);
- iSEM.SetAutoVideoBrightnessAndContrast();
- Thread.Sleep(10000);//adjust the brightness and contrast automaticaly 10 second
- iSEM.SetAutoVideoOff();
- }
- else
- {
- iSEM.SetAutoVideoOff();
- Thread.Sleep(200);
- iSEM.SetBrightness(prm.brightness);//50.0f
- Thread.Sleep(200);
- iSEM.SetContrast(prm.contrast);//30.0f
- Thread.Sleep(200);
- }
-
- bool ifStig =false;
- if (newMag > 5000)
- {
- ifStig = true;
- }
- if (!ImageFocus1(ifStig,svrIP,svrPort))
- {
- log.Error("自动对焦失败", false);
- iSEM.CloseReduced();
- return false;
- }
- iSEM.CloseReduced();
- Thread.Sleep(200);
- log.Warn("自动对焦结束!"+"ifStig="+ifStig.ToString());
- return true;
- }
- public bool DoFocusBySetMainParameter()
- {
- log.Warn("开始自动对焦!");
-
- // set magnification
- bool mRetMag=iSEM.SetMagnification(prm.mag);
-
- Thread.Sleep(2000);
- float current = iSEM.GetMagnification();
- while (Math.Abs(current - prm.mag) > 1)
- {
- iSEM.SetMagnification(prm.mag);
- Thread.Sleep(200);
- current = iSEM.GetMagnification();
- }
- Thread.Sleep(200);
- float cycle_time = iSEM.GetCycleTime();
- Thread.Sleep(Convert.ToInt32(cycle_time) + 100);
- log.Info("角度补充为:"+ prm.tiltCorrAngle.ToString());
- if (!TiltCorrection(prm.tiltCorrAngle))
- {
- return false;
- }
- float wd = iSEM.GetWorkingDistance();
- float offsetWd = Math.Abs(prm.workingDis - wd*1000);
- if (offsetWd > 3)//如果相差3mm以上,则重新设置,否则不动
- {
- log.Info("工作距离相差" + offsetWd + "mm");
- if (!iSEM.SetWorkingDistance(prm.workingDis / 1000))
- {
- return false;
- }
- Thread.Sleep(1000);
- }
-
- // set the window of focusing.
- iSEM.SetReduced(prm.reduceWindowPos.X, prm.reduceWindowPos.Y, (int)prm.reduceWinWidth, (int)prm.reduceWinHeight);
- iSEM.CmdFocusScanSpeed("CMD_SCANRATE4");//
- if (prm.IfAutoBrightnessAndContrast)
- {
- iSEM.SetAutoBright(100);
- Thread.Sleep(200);
- iSEM.SetAutoContrast(100);
- Thread.Sleep(200);
- iSEM.SetAutoVideoBrightnessAndContrast();
- Thread.Sleep(10000);
- iSEM.SetAutoVideoOff();
- }
- else
- {
- iSEM.SetAutoVideoOff();
- Thread.Sleep(200);
- iSEM.SetBrightness(prm.brightness);//50.0f
- Thread.Sleep(200);
- iSEM.SetContrast(prm.contrast);//30.0f
- Thread.Sleep(200);
- }
-
- bool ifStig = false;
- if (prm.mag > 5000)
- {
- ifStig = true;
- }
- else
- {
- ifStig = prm.ifStig;
- }
- if (!ImageFocus1(ifStig,svrIP,svrPort))
- {
- log.Warn("自动对焦失败", false);
- iSEM.CloseReduced();
- return false;
- }
- iSEM.CloseReduced();
- Thread.Sleep(200);
- log.Warn("自动对焦结束!" + "ifStig=" + ifStig.ToString());
- return true;
- }
- public void setFocusParam(Focusparam param)
- {
- this.prm = param ;
-
- }
- //角度补偿
- public bool TiltCorrection(float a_fAngle)
- {
- float a = iSEM.GetTiltAngle();
- if (a != a_fAngle)
- {
- if (!iSEM.SetTiltAngleOn())
- {
- return false;
- }
- Thread.Sleep(200);
- //恢复原始状态
- if (!iSEM.SetTiltAngle(a_fAngle))
- {
- return false;
- }
- Thread.Sleep(2000);
- }
-
- return true;
- }
- //执行自动对焦
- public bool ImageFocus1(Boolean Is_Stig,string serverIp,string serverport)
- {
-
- #region 执行蔡司自动对焦程序
- //服务地址
- string snumstr = @"http://"+serverIp+":"+serverport;
- bool normalized;
-
- // 调用方法;只对焦不消像散
- if (Is_Stig)
- {
- string focusstig = XmlRpcClient.autofocusstig(10, 3, 3, 3, 0.7, snumstr);//,normalized
- if (focusstig != "success")
- {
- return false;
- }
- }
- else
- {
- string onlyfocus = XmlRpcClient.autofocus(10, 3, 3, true, snumstr);//,normalized
- if (onlyfocus != "success")
- {
- return false;
- }
- }
- #endregion
-
- return true;
- }
- }
- }
|