You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Rs.SkyLine/Rs.SkyLine/Flow/SubFlow/UpCameraScanBarCodeFlow.cs

374 lines
18 KiB
C#

using HalconDotNet;
using Rs.Camera;
using Rs.Controls;
using Rs.Framework;
using Rs.Motion;
using Rs.MotionPlat.Commom;
using Rs.MotionPlat.Entitys.Trays;
using Rs.MotionPlat.Flow.Camera;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Rs.MotionPlat.Flow.SubFlow
{
enum EUpCameraScanBarCodeFlowStep
{
,
,
,
,
,
,
}
enum EUpCameraFixedScanBarCodeFlowStep
{
,
,
,
}
/// <summary>
/// 上相机扫码流程
/// </summary>
public class UpCameraScanBarCodeFlow
{
private UpCameraScanBarCodeFlow() { }
private static UpCameraScanBarCodeFlow instance;
public static UpCameraScanBarCodeFlow Instance
{
get
{
if(instance == null)
instance = new UpCameraScanBarCodeFlow();
return instance;
}
}
private EUpCameraScanBarCodeFlowStep step = EUpCameraScanBarCodeFlowStep.;
private EUpCameraFixedScanBarCodeFlowStep fixedStep = EUpCameraFixedScanBarCodeFlowStep.;
ErrorCode errCode = ErrorCode.Ok;
HObject[] grabImages= null;
Stopwatch timeout = new Stopwatch();
string logInfo = string.Empty;
List<VisionResult> vReslutList = new List<VisionResult>();
VisionResult singleResult;
bool exit = true;
ManualResetEvent grabFinishedEvent = new ManualResetEvent(true);
/// <summary>
/// /// 开始扫码,先把所有的穴位全部扫一遍,扫不到的最后再定点扫码,
/// 如果定位失败的穴位,即便没有扫到码,也不再扫码
/// </summary>
/// <param name="startPoint">拍照起始点</param>
/// <param name="endPoint">拍照终止点</param>
/// <param name="isReverse">是否反转拍照</param>
public void ScanMulti(int startSlotIndex,bool isReverse)
{
if(exit==false) return;
grabFinishedEvent.Reset();
SlotPoint startPoint = TrayPointManager.GetSlotPoint(ETrayType.Turnover, startSlotIndex);
SlotPoint endPoint = new SlotPoint();
exit = false;
step = EUpCameraScanBarCodeFlowStep.;
Task.Run(() => {
while (!exit)
{
if (MachineManage.Instance.MachineStatus == EMachineStatus.Stop)
{
Thread.Sleep(10);
continue;
}
switch (step)
{
case EUpCameraScanBarCodeFlowStep.:
if (!isReverse)
{
errCode = AxisControl.LoadX.MovePos(startPoint.X - 50, GlobalVar.FlyCameraSpeed);
if (errCode == ErrorCode.Ok || GlobalVar.VirtualAxis)
{
errCode = AxisControl.LoadY.MovePos(startPoint.Y, GlobalVar.FlyCameraSpeed);
if (errCode == ErrorCode.Ok || GlobalVar.VirtualAxis)
{
logInfo = GetClassName()+$"正向扫码,到扫码起始位";
MessageQueue.Instance.Insert(logInfo);
step = EUpCameraScanBarCodeFlowStep.;
}
else
{
MsgBox.ShowAxisAlarmDialog(AxisControl.LoadY, errCode);
}
}
else
{
MsgBox.ShowAxisAlarmDialog(AxisControl.LoadX, errCode);
}
}
else
{
errCode = AxisControl.LoadX.MovePos(startPoint.X + 50, GlobalVar.FlyCameraSpeed);
if (errCode == ErrorCode.Ok)
{
errCode = AxisControl.LoadY.MovePos(startPoint.Y, GlobalVar.FlyCameraSpeed);
if (errCode == ErrorCode.Ok)
{
logInfo = GetClassName() + $"反向扫码,到扫码起始位";
MessageQueue.Instance.Insert(logInfo);
step = EUpCameraScanBarCodeFlowStep.;
}
else
{
MsgBox.ShowAxisAlarmDialog(AxisControl.LoadY, errCode);
}
}
else
{
MsgBox.ShowAxisAlarmDialog(AxisControl.LoadX, errCode);
}
}
break;
case EUpCameraScanBarCodeFlowStep.:
if (Ops.IsStop(AxisAlias.LoadX, AxisAlias.LoadY) || GlobalVar.VirtualAxis)
{
//Thread.Sleep(1000);
List<double> grabPoints = new List<double>();
if (!isReverse)
{
endPoint = TrayPointManager.GetSlotPoint(ETrayType.Turnover, startSlotIndex + 7);
for (int i = startSlotIndex; i < startSlotIndex + 8; i++)
{
grabPoints.Add(TrayPointManager.GetSlotPoint(ETrayType.Turnover, i).X);
}
}
else
{
endPoint = TrayPointManager.GetSlotPoint(ETrayType.Turnover, startSlotIndex - 7);
for (int i = startSlotIndex; i > startSlotIndex - 8; i--)
{
grabPoints.Add(TrayPointManager.GetSlotPoint(ETrayType.Turnover, i).X);
}
}
ImageProcess.ClearAutoTrigger();
HikCamera.Instance.SetExposure("upCamera", GlobalVar.UpCameraExposureTime);
HikCamera.Instance.SetGain("upCamera", GlobalVar.UpCameraGain);
HikCamera.Instance.SetTrigger("upCamera", ETriggerMode.Auto);
AxisControl.LoadX.SetPosCompare(2, grabPoints.ToArray());
AxisControl.LoadX.CompareStatus(out short pstatu, out int pcount);
logInfo = GetClassName() + $"已运动到扫码起始位";
MessageQueue.Instance.Insert(logInfo);
step = EUpCameraScanBarCodeFlowStep.;
}
break;
case EUpCameraScanBarCodeFlowStep.:
if (!isReverse)
{
errCode = AxisControl.LoadX.MovePos(endPoint.X + 50, GlobalVar.FlyCameraSpeed);
if (errCode == ErrorCode.Ok || GlobalVar.VirtualAxis)
{
logInfo = GetClassName() + $"正向扫码,到扫码结束位";
MessageQueue.Instance.Insert(logInfo);
step = EUpCameraScanBarCodeFlowStep.;
}
else
{
MsgBox.ShowAxisAlarmDialog(AxisControl.LoadX, errCode);
}
}
else
{
errCode = AxisControl.LoadX.MovePos(endPoint.X - 50, GlobalVar.FlyCameraSpeed);
if (errCode == ErrorCode.Ok)
{
logInfo = GetClassName()+ $"反向扫码,到扫码结束位";
MessageQueue.Instance.Insert(logInfo);
step = EUpCameraScanBarCodeFlowStep.;
}
else
{
MsgBox.ShowAxisAlarmDialog(AxisControl.LoadX, errCode);
}
}
break;
case EUpCameraScanBarCodeFlowStep.:
if (Ops.IsStop(AxisAlias.LoadX, AxisAlias.LoadY) || GlobalVar.VirtualAxis)
{
logInfo = GetClassName() + $"已运动到扫码结束位";
MessageQueue.Instance.Insert(logInfo);
timeout.Restart();
step = EUpCameraScanBarCodeFlowStep.;
}
break;
case EUpCameraScanBarCodeFlowStep.:
grabImages = ImageProcess.GetAutoImage();
if(timeout.ElapsedMilliseconds<3000)
{
//AxisControl.LoadX.CompareStatus(out short pstatu, out int pcount);
if ((grabImages != null && grabImages.Length == 8) || GlobalVar.VirtualAxis)
{
grabImages = ImageRotate.Rotates(grabImages, "upCamera");
step = EUpCameraScanBarCodeFlowStep.;
}
}
else
{
//拍照超时
MessageQueue.Instance.Warn(GetClassName()+ "上相机拍照超时,重新拍照");
step = EUpCameraScanBarCodeFlowStep.;
}
break;
case EUpCameraScanBarCodeFlowStep.:
vReslutList.Clear();
int slotIndex = 0;
foreach (var img in grabImages)
{
int sIndex = 0;
if (!isReverse)
{
sIndex = startSlotIndex + slotIndex;
}
else
{
sIndex = startSlotIndex - slotIndex;
}
VisionResult vr = new VisionResult();
if (TurnoverTrayManager.Instance.Slot(sIndex).IsHasProduct)
{
vr = VisionManager.TurnoverTrayDumpProductOK(img,GlobalVar.EnableTurnoverTrayRecheck);
}
vr.SlotIndex = sIndex;
vReslutList.Add(vr);
slotIndex++;
}
exit = true;
grabFinishedEvent.Set();
break;
}
}
});
}
public void ScanSingle(int SlotIndex,bool needGo=true)
{
if (exit == false) return;
grabFinishedEvent.Reset();
HObject imageSingle = new HObject();
exit = false;
if(!needGo)
{
ImageProcess.ClearManualTrigger();
HikCamera.Instance.SetExposure("upCamera", GlobalVar.UpCameraExposureTime);
HikCamera.Instance.SetGain("upCamera",GlobalVar.UpCameraGain);
HikCamera.Instance.SetTrigger("upCamera", ETriggerMode.Manual);
AxisControl.LoadX.ComparePulse(2, false);
fixedStep = EUpCameraFixedScanBarCodeFlowStep.;
}
else
{
fixedStep = EUpCameraFixedScanBarCodeFlowStep.;
}
Task.Run(() => {
while (!exit)
{
if (MachineManage.Instance.MachineStatus == EMachineStatus.Stop)
{
Thread.Sleep(10);
continue;
}
switch (fixedStep)
{
case EUpCameraFixedScanBarCodeFlowStep.:
SlotPoint targetPoint = TrayPointManager.GetSlotPoint(ETrayType.Turnover, SlotIndex);
errCode = AxisControl.LoadX.MovePos(targetPoint.X, GlobalVar.FlyCameraSpeed);
if (errCode == ErrorCode.Ok || GlobalVar.VirtualAxis)
{
errCode = AxisControl.LoadY.MovePos(targetPoint.Y, GlobalVar.FlyCameraSpeed);
if (errCode == ErrorCode.Ok || GlobalVar.VirtualAxis)
{
logInfo = GetClassName() + $"到扫码起始位";
MessageQueue.Instance.Insert(logInfo);
fixedStep = EUpCameraFixedScanBarCodeFlowStep.;
}
else
{
MsgBox.ShowAxisAlarmDialog(AxisControl.LoadY, errCode);
}
}
else
{
MsgBox.ShowAxisAlarmDialog(AxisControl.LoadX, errCode);
}
break;
case EUpCameraFixedScanBarCodeFlowStep.:
if (Ops.IsStop(AxisAlias.LoadX, AxisAlias.LoadY) || GlobalVar.VirtualAxis)
{
Thread.Sleep(200);
ImageProcess.ClearManualTrigger();
HikCamera.Instance.SetTrigger("upCamera", ETriggerMode.Manual);
HikCamera.Instance.SetExposure("upCamera", GlobalVar.UpCameraExposureTime);
HikCamera.Instance.SetGain("upCamera", GlobalVar.UpCameraGain);
AxisControl.LoadX.ComparePulse(2, false);
logInfo = GetClassName() + $"已运动到扫码起始位";
MessageQueue.Instance.Insert(logInfo);
fixedStep = EUpCameraFixedScanBarCodeFlowStep.;
}
break;
case EUpCameraFixedScanBarCodeFlowStep.:
HObject image = ImageProcess.GetManualImage();
if(image!=null)
{
imageSingle = ImageRotate.Rotate(image, "upCamera");
HOperatorSet.WriteImage(imageSingle, "bmp", 0, "d://images//aaaa");
fixedStep = EUpCameraFixedScanBarCodeFlowStep.;
}
break;
case EUpCameraFixedScanBarCodeFlowStep.:
singleResult = VisionManager.TurnoverTrayDumpProductOK(imageSingle);
singleResult.SlotIndex = SlotIndex;
exit = true;
grabFinishedEvent.Set();
break;
}
}
});
}
public string GetClassName()
{
return "UpCameraScanBarCodeFlow-";
}
public List<VisionResult> Wait()
{
grabFinishedEvent.WaitOne();
return vReslutList;
}
public VisionResult WaitSingle()
{
grabFinishedEvent.WaitOne();
return singleResult;
}
public bool CheckResult(VisionResult vr)
{
if (vr.SearchModelOK)
return ((Math.Abs(vr.OffsetX) - GlobalVar.TurnoverTrayLocateXRange <= 0) && (Math.Abs(vr.OffsetY) - GlobalVar.TurnoverTrayLocateYRange <= 0) && (Math.Abs(vr.OffsetR) - GlobalVar.TurnoverTrayLocateRRange <= 0));
return false;
}
}
}