From 447cf4326bb1a7a5d261abb588fdf726a1403946 Mon Sep 17 00:00:00 2001 From: "xhm\\HP" <1173131411@qq.com> Date: Mon, 24 Mar 2025 15:21:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DH.Commons/Base/DetectionConfig.cs | 1 + DH.Commons/Base/PLCBase.cs | 2 +- DH.Devices.PLC/XinJEPLCTcpNet.cs | 16 +- DH.Devices.Vision/SimboObjectDetection.cs | 13 +- DH.Devices.Vision/SimboVisionDriver.cs | 77 +++-- DHSoftware/MainWindow.cs | 325 +++++++++++++++++++++- 6 files changed, 363 insertions(+), 71 deletions(-) diff --git a/DH.Commons/Base/DetectionConfig.cs b/DH.Commons/Base/DetectionConfig.cs index 60f89a9..681f76e 100644 --- a/DH.Commons/Base/DetectionConfig.cs +++ b/DH.Commons/Base/DetectionConfig.cs @@ -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; diff --git a/DH.Commons/Base/PLCBase.cs b/DH.Commons/Base/PLCBase.cs index f4013f4..f23192e 100644 --- a/DH.Commons/Base/PLCBase.cs +++ b/DH.Commons/Base/PLCBase.cs @@ -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 _PLCItemList = new AntList(); [Category("设备配置")] diff --git a/DH.Devices.PLC/XinJEPLCTcpNet.cs b/DH.Devices.PLC/XinJEPLCTcpNet.cs index 8654207..ca4e862 100644 --- a/DH.Devices.PLC/XinJEPLCTcpNet.cs +++ b/DH.Devices.PLC/XinJEPLCTcpNet.cs @@ -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(); diff --git a/DH.Devices.Vision/SimboObjectDetection.cs b/DH.Devices.Vision/SimboObjectDetection.cs index 1c58026..ba3bedf 100644 --- a/DH.Devices.Vision/SimboObjectDetection.cs +++ b/DH.Devices.Vision/SimboObjectDetection.cs @@ -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(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 类型。 + } diff --git a/DH.Devices.Vision/SimboVisionDriver.cs b/DH.Devices.Vision/SimboVisionDriver.cs index bb4b5a8..7a99870 100644 --- a/DH.Devices.Vision/SimboVisionDriver.cs +++ b/DH.Devices.Vision/SimboVisionDriver.cs @@ -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 group in conditionList) - //{ - // //bool b = group.ToList().Any(f => - // //{ - // // return f.FilterOperation(d); - // //}); + foreach (IGrouping 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 diff --git a/DHSoftware/MainWindow.cs b/DHSoftware/MainWindow.cs index b7b72eb..f51e193 100644 --- a/DHSoftware/MainWindow.cs +++ b/DHSoftware/MainWindow.cs @@ -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 HKCameras { get; } = new List(); + public List HKCameras { get; } = new List(); public List Cameras { get; } = new List(); public Dictionary Dectection { get; } = new Dictionary(); - 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 DetectionConfigs = new List(); List SimboStationMLEngineList = new List(); Dictionary HalconToolDict = new Dictionary(); @@ -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 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 Dets = new List + { + 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(); + // 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 products = new ConcurrentDictionary(); + _productLists.Add(products); + } + } + + + // _MGSCameraList = DeviceCollection + //.OfType() // 直接筛选出 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 products = new ConcurrentDictionary(); + _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 Dets = new List + // { + // 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 Dets = new List + { + List Dets = new List { 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> _productLists = new List>();