From ccaf5415b4d1dbe256c98395a1258e0b98c2de2a Mon Sep 17 00:00:00 2001 From: tfarcraw <123211521@163.com> Date: Tue, 21 Jul 2026 22:53:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E8=A7=86=E8=A7=89=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PLCSharpWpf/Core/Common/FlowModel.cs | 14 +++++++ .../Algorithm/CaliperFindCircleHandler.cs | 12 ++++-- .../Algorithm/CaliperFindEdgeHandler.cs | 9 ++-- .../Algorithm/GrayTemplateMatchHandler.cs | 10 ++++- .../Algorithm/ORBMatchHandler.cs | 2 +- .../Algorithm/PointToPointDistance.xaml | 2 +- PLCSharpWpf/VVMs/Vision/VisionFunction.cs | 42 +++++++++++++++---- 7 files changed, 74 insertions(+), 17 deletions(-) diff --git a/PLCSharpWpf/Core/Common/FlowModel.cs b/PLCSharpWpf/Core/Common/FlowModel.cs index d3d1ec6..b760d5c 100644 --- a/PLCSharpWpf/Core/Common/FlowModel.cs +++ b/PLCSharpWpf/Core/Common/FlowModel.cs @@ -37,6 +37,18 @@ /// public bool Done { get; set; } /// + /// 流程是否正常结束(默认 false,正常结束时置 true) + /// + public bool IsSuccess { get; set; } + /// + /// 失败原因(非空表示异常结束) + /// + public string? ErrorState { get; set; } + /// + /// 单个步骤超时时间(秒),默认 5 秒 + /// + public double TimeoutSeconds { get; set; } = 5; + /// /// 默认构造函数,自动调用 Reset 初始化 /// public FlowModel() @@ -51,6 +63,8 @@ public void Reset() { Done = false; + IsSuccess = false; + ErrorState = null; Step = 0; StepTime = DateTime.Now; FlowStarTime = DateTime.Now; diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindCircleHandler.cs b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindCircleHandler.cs index 9df5565..68c4703 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindCircleHandler.cs +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindCircleHandler.cs @@ -23,7 +23,7 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm if (!item.DoubleParams.TryGetValue("CircleCenterX", out double cx) || !item.DoubleParams.TryGetValue("CircleCenterY", out double cy) || !item.DoubleParams.TryGetValue("CircleRadius", out double radius)) - throw new Exception("圆形ROI未配置,请先画圆!"); + { item.Flow.ErrorState = "圆形ROI未配置,请先画圆!"; return false; } // 读取卡尺参数 var threshold = item.IntParams.TryGetValue("Threshold", out int t) ? Math.Max(t, 1) : 30; @@ -180,7 +180,10 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm _ = func.RenderDrawAsync(); if (edgePoints.Count < 3) - throw new Exception($"仅找到 {edgePoints.Count} 个边缘点,至少需要3个!"); + { + item.Flow.ErrorState = $"仅找到 {edgePoints.Count} 个边缘点,至少需要3个!"; + return false; + } // RANSAC 拟合圆 var circle = FitCircleRansac(edgePoints, maxIterations: 200, inlierThreshold: inlierTh); @@ -193,7 +196,10 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm }); if (ransacInliers < minInliers) - throw new Exception($"内点数不足 ({ransacInliers} < {minInliers}),请调整 RANSAC 阈值或检查边缘点!"); + { + item.Flow.ErrorState = $"内点数不足 ({ransacInliers} < {minInliers}),请调整 RANSAC 阈值!"; + return false; + }; func.DrawCommands.Add(DrawCommand.Circle(circle.CenterX, circle.CenterY, circle.Radius, magenta, 2)); diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindEdgeHandler.cs b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindEdgeHandler.cs index 64511c5..0ee0e03 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindEdgeHandler.cs +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindEdgeHandler.cs @@ -14,7 +14,7 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm !item.DoubleParams.TryGetValue("LineStartY", out double sy) || !item.DoubleParams.TryGetValue("LineEndX", out double ex) || !item.DoubleParams.TryGetValue("LineEndY", out double ey)) - throw new Exception("卡尺线未配置,请先画线!"); + { item.Flow.ErrorState = "卡尺线未配置,请先画线!"; return false; } var threshold = item.IntParams.TryGetValue("Threshold", out int t) ? Math.Max(t, 1) : 30; var numCalipers = item.IntParams.TryGetValue("NumCalipers", out int nc) ? Math.Max(nc, 1) : 50; @@ -31,7 +31,7 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm double lineDx = ex - sx, lineDy = ey - sy; double lineLen = Math.Sqrt(lineDx * lineDx + lineDy * lineDy); - if (lineLen < 1) throw new Exception("直线太短!"); + if (lineLen < 1) { item.Flow.ErrorState = "直线太短!"; return false; } double ux = lineDx / lineLen, uy = lineDy / lineLen; double pdx = -uy, pdy = ux; double halfLen = caliperLength / 2.0; @@ -87,7 +87,10 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm } if (edgePoints.Count < 2) - throw new Exception($"仅找到 {edgePoints.Count} 个边缘点,至少需要2个!"); + { + item.Flow.ErrorState = $"仅找到 {edgePoints.Count} 个边缘点,至少需要2个!"; + return false; + } // RANSAC 内点 var inls = RansacInliers(edgePoints, inlierTh); diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/GrayTemplateMatchHandler.cs b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/GrayTemplateMatchHandler.cs index c322428..7aa0ea7 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/GrayTemplateMatchHandler.cs +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/GrayTemplateMatchHandler.cs @@ -72,7 +72,10 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm // ===== 2. 加载模板 ===== var tmpl = func.LocalImageDatas.FirstOrDefault(m => m.Name == templateName); if (tmpl?.Mat == null || tmpl.Mat.Empty()) - throw new Exception("模板提取失败,请先画选区截取模板!"); + { + item.Flow.ErrorState = "模板提取失败,请先画选区截取模板!"; + return false; + } templateRoi = tmpl.Mat.Clone(); // ===== 3. 灰度化 ===== @@ -98,7 +101,10 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm out bestScore, out bestAngle, out bestX, out bestY, out etW, out etH, out bestResult, ref totalTemplates); if (bestScore < scoreMin) - throw new Exception($"未找到可靠匹配(最佳分数 {bestScore:F3} < 阈值 {scoreMin})!"); + { + item.Flow.ErrorState = $"未找到可靠匹配(最佳分数 {bestScore:F3} < 阈值 {scoreMin})"; + return false; + } // GetRotationMatrix2D 正角度 = OpenCV 顺时针 → 取反统一为视觉方向 bestAngle = -bestAngle; diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/ORBMatchHandler.cs b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/ORBMatchHandler.cs index bd5ef42..5905d31 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/ORBMatchHandler.cs +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/ORBMatchHandler.cs @@ -51,7 +51,7 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm case "旋转矩形": if (!item.DoubleParams.TryGetValue("TemplateWidth", out templateW) || !item.DoubleParams.TryGetValue("TemplateHeight", out templateH)) - throw new Exception("旋转矩形模板参数缺失!"); + { item.Flow.ErrorState = "旋转矩形模板参数缺失!"; return false; } break; case "圆形": if (!item.DoubleParams.TryGetValue("TemplateRadius", out double radius)) diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/PointToPointDistance.xaml b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/PointToPointDistance.xaml index b1d87ab..a835620 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/PointToPointDistance.xaml +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/PointToPointDistance.xaml @@ -1,4 +1,4 @@ - RunItem(item)); + item.Flow.StepTime = DateTime.Now; + flow.Step++; + } + break; + case 3: + { + var item = VisionFlows[index]; + var task = flow.TaskBool; + if (task == null) { flow.Step--; break; } + if (task.IsCompleted) + { + if (task.Result) + { + flow.Step = 1; // 回到 case 1 取下一个 + index++; + } + else + { + item.Flow.ErrorState ??= "未知错误"; + flow.IsSuccess = false; + flow.Done = true; + } + } + else if (item.Flow.CheckStepTime(flow.TimeoutSeconds)) + { + item.Flow.ErrorState = $"执行超时(>{flow.TimeoutSeconds:F0}秒)"; + flow.IsSuccess = false; + flow.Done = true; + } } break; } @@ -531,7 +559,7 @@ namespace PLCSharp.VVMs.Vision /// public class VisionParams : BindableBase { - + private ObservableDictionary _ResultDoubles = []; /// /// 通用数值结果(如 ORB 匹配输出的 X/Y/角度偏移)