This commit is contained in:
xhm\HP 2025-03-24 15:21:16 +08:00
parent 8aec9ba7fa
commit 447cf4326b
6 changed files with 363 additions and 71 deletions

View File

@ -930,6 +930,7 @@ namespace DH.Commons.Base
public class DetectionLable : NotifyProperty
{
private bool _selected = false;
private string _labelId;
private string _labelName;
private double _maxScore;

View File

@ -18,7 +18,7 @@ namespace DH.Commons.Base
private int _dataBit = 8;
private StopBits _stopBit = StopBits.One;
private Parity _parity = Parity.None;
private string _ip = "192.168.6.6";
private string _ip = "192.168.6.61";
private int _port = 502;
private AntList<PLCItem> _PLCItemList = new AntList<PLCItem>();
[Category("设备配置")]

View File

@ -45,12 +45,19 @@ namespace DH.Devices.PLC
TcpNet.DataFormat = HslCommunication.Core.DataFormat.ABCD;
TcpNet.Station = 1;
TcpNet.Series = XinJESeries.XD;
PLCItem itemSpeed = PLCItemList.FirstOrDefault(u => u.Name == "转盘速度");
if(itemSpeed== null)
{
return false;
}
OperateResult ret = TcpNet.ConnectServer();
if (ret.IsSuccess)
{
Connected = true;
MonitorPieces();
TurntableOpen(12000,true);
TurntableOpen(Convert.ToInt32(itemSpeed.Value) , true);
return true;
}
@ -58,6 +65,8 @@ namespace DH.Devices.PLC
{
return false;
}
}
catch
{
@ -438,6 +447,9 @@ namespace DH.Devices.PLC
private void MonitorPiecesImpl()
{
PLCItem pLCItem= PLCItemList.FirstOrDefault(u => u.Name == "产品计数");
if (pLCItem == null)
return;
DateTime startTime = DateTime.Now;
DateTime endTime = startTime;
TimeSpan timeSpan = endTime - startTime;
@ -448,7 +460,7 @@ namespace DH.Devices.PLC
Stopwatch sw = new Stopwatch();
uint tmpPieceNumber = 0;
sw.Start();
var ret = TcpNet.ReadUInt16("D1016");
sw.Stop();

View File

@ -136,9 +136,8 @@ namespace DH.Devices.Vision
// json = "{\"FastDetResult\":[{\"cls_id\":0,\"cls\":\"liewen\",\"fScore\":0.654843,\"rect\":[175,99,110,594]},{\"cls_id\":0,\"cls\":\"liewen\",\"fScore\":0.654589,\"rect\":[2608,19,104,661]},{\"cls_id\":0,\"cls\":\"liewen\",\"fScore\":0.654285,\"rect\":[1275,19,104,662]},{\"cls_id\":0,\"cls\":\"liewen\",\"fScore\":0.620762,\"rect\":[1510,95,107,600]},{\"cls_id\":0,\"cls\":\"liewen\",\"fScore\":0.617812,\"rect\":[2844,93,106,602]}]}";
//
Console.WriteLine("检测结果JSON" + json);
#pragma warning disable CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。
SegResult detResult = JsonConvert.DeserializeObject<SegResult>(json);
#pragma warning restore CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。
if (detResult == null)
{
return;
@ -176,7 +175,6 @@ namespace DH.Devices.Vision
MLResult mlResult = new MLResult();
Mat originMat=new Mat() ;
Mat detectMat= new Mat();
#pragma warning disable CS0168 // 声明了变量,但从未使用过
try
{
if (req.mImage == null)
@ -266,18 +264,17 @@ namespace DH.Devices.Vision
// 释放 Mat 资源
if (detectMat != null)
{
detectMat.Dispose();
#pragma warning disable CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。
detectMat = null;
#pragma warning restore CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。
}
if (originMat != null)
{
originMat.Dispose();
#pragma warning disable CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。
originMat = null;
#pragma warning restore CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。
}

View File

@ -206,60 +206,47 @@ namespace DH.Devices.Vision
{
//当前检测项的 过滤条件
//var conditionList = detectConfig.DetectionFilterList
// .Where(u => u.IsEnabled && u.LabelName == d.LabelName)
// .GroupBy(u => u.ResultState)
// .OrderBy(u => u.Key)
// .ToList();
//当前检测项的 过滤条件
//var conditionList = detectConfig.DetectionFilterList
// .Where(u => u.IsEnabled && u.LabelName == d.LabelName)
// .GroupBy(u => u.ResultState)
// .OrderBy(u => u.Key)
// .ToList();
// 当前检测项的 过滤条件
var conditionList = detectConfig.DetectionLableList
.Where(u=>u.LabelName == d.LabelName)
.GroupBy(u => u.ResultState)
.OrderBy(u => u.Key)
.ToList();
//if (conditionList.Count == 0)
//{
if (conditionList.Count == 0)
{
// d.FinalResult = d.LabelName.ToLower() == "ok"
// ? ResultState.OK
// : ResultState.DetectNG;
//}
//else
//{
// d.FinalResult = detectConfig.IsMixModel
// ? ResultState.A_NG
// : ResultState.OK;
d.FinalResult = d.LabelName.ToLower() == "ok"
? ResultState.OK
: ResultState.DetectNG;
}
else
{
d.FinalResult = detectConfig.IsMixModel
? ResultState.A_NG
: ResultState.OK;
//}
}
//foreach (IGrouping<ResultState, DetectionFilter> group in conditionList)
//{
// //bool b = group.ToList().Any(f =>
// //{
// // return f.FilterOperation(d);
// //});
foreach (IGrouping<ResultState, DetectionFilter> group in conditionList)
{
bool b = group.ToList().Any(f =>
{
return f.FilterOperation(d);
});
// //if (b)
// //{
// // d.FinalResult = group.Key;
// // break;
// //}
if (b)
{
d.FinalResult = group.Key;
break;
}
// if (group.Any(f => f.FilterOperation(d)))
// {
// d.FinalResult = group.Key;
// break;
// }
// //else
// //{
// // d.FinalResult = d.InferenceResult = ResultState.OK;
// //}
//}
}
});
#endregion
#region 5.NG

View File

@ -30,7 +30,9 @@ using System.Threading.Tasks;
using System.Windows.Forms;
using XKRS.UI.Device.Winform;
using static AntdUI.Math3D;
using static DH.Commons.Enums.EnumHelper;
using Camera = DHSoftware.Models.Camera;
using ResultState = DH.Commons.Base.ResultState;
namespace DHSoftware
{
@ -242,10 +244,10 @@ namespace DHSoftware
public List<Camera> HKCameras { get; } = new List<Camera>();
public List<HikVisionCamera> HKCameras { get; } = new List<HikVisionCamera>();
public List<Do3ThinkCamera> Cameras { get; } = new List<Do3ThinkCamera>();
public Dictionary<string, SimboObjectDetection> Dectection { get; } = new Dictionary<string, SimboObjectDetection>();
public XinJEPLCTcpNet PLC { get; } = new XinJEPLCTcpNet();
public XinJEPLCTcpNet PLC { get; } = XinJEPLCTcpNet.Instance;
SLDMotion sLDMotion = new SLDMotion();
private void MainWindow_Load(object sender, EventArgs e)
{
@ -261,7 +263,7 @@ namespace DHSoftware
foreach (var camera in HKCameras)
{
//var hk as HikVisionCamera;
//camera.CameraDisConnect();
camera.CameraDisConnect();
}
PLC.PLCDisConnect();
}
@ -299,6 +301,8 @@ namespace DHSoftware
public volatile int ProductNum_OK = 0;
private readonly object _cameraSummaryLock = new object();
public SimboVisionDriver? _visionEngine = null;
public PLCBase? _PLCConfig = null;
List<DetectionConfig> DetectionConfigs = new List<DetectionConfig>();
List<SimboStationMLEngineSet> SimboStationMLEngineList = new List<SimboStationMLEngineSet>();
Dictionary<string, HDevEngineTool> HalconToolDict = new Dictionary<string, HDevEngineTool>();
@ -306,8 +310,297 @@ namespace DHSoftware
public DateTime startTime;
private void HandleStartButton()
{
Cameras.Clear();
HKCameras.Clear();
Dectection.Clear();
_cameraRelatedDetectionDict = new();
//初始化相机 链接相机
if (ConfigModel.CameraBaseList.Count > 0)
{
for (int i = 0; i <ConfigModel.CameraBaseList.Count; i++)
{
var cameraBase = ConfigModel.CameraBaseList[i];
if(cameraBase.CamType == EnumCamType.Do3think)
{
Do3ThinkCamera cam = new Do3ThinkCamera();
cam.CameraName = cameraBase.CameraName;
cam.CameraIP = cameraBase.CameraIP;
cam.IsEnabled = cameraBase.IsEnabled;
Cameras.Add(cam);
//cam.CameraConnect();
cam.OnHImageOutput += OnCameraHImageOutput;
}
else if(cameraBase.CamType == EnumCamType.hik)
{
HikVisionCamera cam = new HikVisionCamera();
cam.CameraName = cameraBase.CameraName;
cam.CameraIP = cameraBase.CameraIP;
cam.IsEnabled=cameraBase.IsEnabled;
HKCameras.Add(cam);
// cam.CameraConnect();
cam.OnHImageOutput += OnCameraHImageOutput;
}
}
}
if(ConfigModel.PLCBaseList.Count > 0)
{
for (int i = 0; i < ConfigModel.PLCBaseList.Count; i++)
{
var plcBase = ConfigModel.PLCBaseList[i];
if(plcBase.PLCType==EnumPLCType.XC网口)
{
PLC.IP = plcBase.IP;
PLC.Enable = plcBase.Enable;
PLC.PLCName = plcBase.PLCName;
PLC.PLCItemList=plcBase.PLCItemList;
PLC.Port= plcBase.Port;
PLC.PLCConnect();
}
}
}
if (ConfigModel.DetectionList.Count > 0)
{
for (int i = 0; i < ConfigModel.DetectionList.Count; i++)
{
DetectionConfig detectionConfig = ConfigModel.DetectionList[i];
var detection = ConfigModel.DetectionList[i];
detectionConfig.CameraCollects = detection.CameraCollects;
detectionConfig.ModelconfThreshold = detection.ModelconfThreshold;
detectionConfig.ModelWidth = detection.ModelWidth;
detectionConfig.ModelHeight = detection.ModelHeight;
detectionConfig.In_lable_path = detection.In_lable_path;
detectionConfig.IsEnabled = detection.IsEnabled;
detectionConfig.ShowLocation.X = detection.ShowLocation.X;
detectionConfig.ShowLocation.Y = detection.ShowLocation.Y;
DetectionConfigs.Add(detectionConfig);
}
}
DetectionConfigs.ForEach(detection =>
{
detection.CameraCollects.ForEach(cam =>
{
List<string> Dets = new List<string>
{
detection.Id
};
if (!_cameraRelatedDetectionDict.ContainsKey(cam.CameraSourceId))
{
_cameraRelatedDetectionDict.Add(cam.CameraSourceId, Dets);
}
else
{
_cameraRelatedDetectionDict[cam.CameraSourceId].Add(detection.Id);
}
}
);
});
string inferenceDevice = "CPU";
//
_visionEngine = new SimboVisionDriver();
_visionEngine.DetectionConfigs = DetectionConfigs;
//初始化模型 加载模型
_visionEngine.Init();
CtrlVisionRunBase ctrlVisionRun = new CtrlVisionRunBase(_visionEngine);
tabImgDisplay.Controls.Add(ctrlVisionRun);
}
private static int currentRegister = 411; // 初始为 D411
private void StartProcess()
{
//计数清零
PieceCount = 0;
//吹气点位归置
currentRegister = 411;
if (_PLCConfig?.Enable == true)
{
PLC.CountToZero();
}
ConfigModel.CameraBaseList.ForEach(d =>
{
if (d is CameraBase cam)
{
cam.SnapshotCount = 0;
}
});
//PrepareBatchNO();
// isInPositionChecking = false;
//isFullTrayChecking = false;
//队列清空
// var temp = new List<ITriggerSet>();
// temp.AddRange(XKRSPLCConfig.TriggerConfigCollection);
// temp.AddRange(XKRSPLCConfig.SnapshotTriggerConfigCollection);
//temp.ForEach(t =>
//{
// var diskInfo = XKRSPLCConfig.DiskInfoList.FirstOrDefault(u => u.DiskName == t.DiskName);
// if (diskInfo != null)
// {
// t.AxisNum = diskInfo.DiskAxisNum;
// }
// else
// {
// t.AxisNum = -1;
// }
//});
//if (temp.Any(u => u.AxisNum < 0))
//{
// LogAsync(DateTime.Now, LogLevel.Error, "触发信号归属转盘未确认");
// return new ProcessResponse(false);
//}
// _diskInfoListInOrder = XKRSPLCConfig.DiskInfoList.OrderBy(u => u.DiskSequence).ToList();
// var axisNumList = _diskInfoListInOrder.Select(u => u.DiskAxisNum).ToList();
/// PrepareMLEngine();
// if (_PLCConfig?.Enable == true)
//挡料电机操作
// _PLC.FeedingMotor(_PLCConfig.CunToZeroSpeed, _PLCConfig.CunPos, _PLCConfig.CunSpeed, _PLCConfig.CunDirection);
//流程开启操作配置
// ProcessInitialAction();
// if (_PLC?Enabled == true)
//皮带
// _PLC.Belt(true);
//DeviceCollection.ForEach(d =>
//{
// if (d is CameraBase c)
// {
// c.OnHImageOutput -= OnCameraHImageOutput;
// c.OnHImageOutput += OnCameraHImageOutput;
// c.SnapshotCount = 0;
// }
//});
_productLists.Clear();
#region
//mOfflineImageTimer = new System.Timers.Timer();
//mOfflineImageTimer.Elapsed += OnEmitSerialPortAsync;
//mOfflineImageTimer.Interval = 1000;
//mOfflineImageTimer.Start();
#endregion
var settings = _visionEngine.DetectionConfigs.Where(u => u.IsEnabled && u.IsAddStation ).ToList();
if (settings != null)
{
settings = settings.Where(s => s.IsEnabled).ToList();
ProductBaseCount = settings.Count;
for (int i = 0; i < ProductBaseCount * ProductListMulti; i++)
{
ConcurrentDictionary<uint, ProductData> products = new ConcurrentDictionary<uint, ProductData>();
_productLists.Add(products);
}
}
// _MGSCameraList = DeviceCollection
//.OfType<MGSCameraDriver>() // 直接筛选出 MGSCameraDriver 类型的元素
//.Where(camera => camera.IConfig != null && camera.IConfig.IsEnabled) // 进一步筛选 IConfig 不为 null 且 IsEnabled 为 true
//.ToList();
// ProductBaseCount = _MGSCameraList.Count;
for (int i = 0; i < ProductBaseCount * ProductListMulti; i++)
{
ConcurrentDictionary<uint, ProductData> products = new ConcurrentDictionary<uint, ProductData>();
_productLists.Add(products);
}
// 转盘操作
// if (_PLC?.IIConfig?.IsEnabled == true)
// _PLC.TurntableOpen(XKRSPLCConfig.TurnSpeed, XKRSPLCConfig.TurnDirection);
Thread.Sleep(500);
//振动盘
//if (XKRSPLCConfig.IsEnableVibratory)
//{
// if (_PLC.IIConfig.IsEnabled)
// _PLC.Vibratory(true);
//}
//InitialOEEStatistic();
// MachineState = MachineState.Running;
}
private void PrepareMLEngine()
{
//if (_visionEngine == null)
//{
// _visionEngine = DeviceCollection.FirstOrDefault(u => u is VisionEngineBase) as VisionEngineBase;
//}
//if (_visionEngine == null)
//{
// throw new ProcessException($"未能获取检测设备");
//}
//相机模组
//_cameraRelatedDetectionDict = new();
//detectionList.ForEach(detection =>
//{
// detection.CameraCollects.ForEach(cam =>
// {
// List<string> Dets = new List<string>
// {
// detection.Id
// };
// if (!_cameraRelatedDetectionDict.ContainsKey(cam.CameraSourceId))
// {
// _cameraRelatedDetectionDict.Add(cam.CameraSourceId, Dets);
// }
// else
// {
// _cameraRelatedDetectionDict[cam.CameraSourceId].Add(detection.Id);
// }
// });
//});
//_visionEngine.OnDetectionWarningStop += VisionEngine_OnDetectionWarningStopAsync;
}
#if false
private void HandleStartButton2()
{
CurrentMachine = true;
//_visionEngine.Start();
//[Category("深度学习检测配置")]
//[DisplayName("检测标签定义集合")]
@ -502,22 +795,22 @@ namespace DHSoftware
{
detection.CameraCollects.ForEach(cam =>
{
List<string> Dets = new List<string>
{
List<string> Dets = new List<string>
{
detection.Id
};
if (!_cameraRelatedDetectionDict.ContainsKey(cam.CameraSourceId))
{
_cameraRelatedDetectionDict.Add(cam.CameraSourceId, Dets);
}
else
{
_cameraRelatedDetectionDict[cam.CameraSourceId].Add(detection.Id);
}
if (!_cameraRelatedDetectionDict.ContainsKey(cam.CameraSourceId))
{
_cameraRelatedDetectionDict.Add(cam.CameraSourceId, Dets);
}
else
{
_cameraRelatedDetectionDict[cam.CameraSourceId].Add(detection.Id);
}
}
}
);
});
string inferenceDevice = "CPU";
@ -650,6 +943,8 @@ namespace DHSoftware
});
}
}
#endif
private uint PieceCount = 0;
private List<ConcurrentDictionary<uint, ProductData>> _productLists = new List<ConcurrentDictionary<uint, ProductData>>();