using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MainShell.Common;
using SemiconductorVisionAlgorithm.SemiParams;
namespace MainShell.Motion
{
#region 数据结构
///
/// 逼近对位的单轴配置
///
public class ApproachAlignmentAxis
{
public ApproachAlignmentAxis(string axisName, double toleranceValue)
{
if (string.IsNullOrWhiteSpace(axisName))
{
throw new ArgumentNullException(nameof(axisName));
}
if (toleranceValue <= 0)
{
throw new ArgumentException("Tolerance value must be positive.", nameof(toleranceValue));
}
AxisName = axisName;
ToleranceValue = toleranceValue;
}
///
/// 轴名称(如 "X", "Y")
///
public string AxisName { get; }
///
/// 该轴的允许误差(单位与轴位置一致,通常为 mm)
///
public double ToleranceValue { get; }
///
/// 轴的描述信息(用于日志)
///
public string Description { get; set; }
}
public enum CenterRecognitionType
{
Template,
EdgeCircle
}
public class CenterRecognitionParameters
{
public CenterRecognitionParameters()
{
MinScore = 0.8d;
TemplatePath = "Template/Default";
Type = CenterRecognitionType.Template;
}
public string TemplatePath { get; set; }
public double MinScore { get; set; }
public bool UseRoi { get; set; }
public string RoiName { get; set; }
public CenterRecognitionType Type { get; set; }
public Rectangle1 rectangle { get; set; }
}
///
/// 逼近对位请求参数
///
public class ApproachAlignmentRequest
{
private IReadOnlyList _axes;
public ApproachAlignmentRequest(IEnumerable axes, CameraType camera = CameraType.TopPositionCamera)
{
if (axes == null)
{
throw new ArgumentNullException(nameof(axes));
}
var axesList = axes.ToList();
if (axesList.Count == 0)
{
throw new ArgumentException("At least one axis is required.", nameof(axes));
}
Axes = axesList.AsReadOnly();
Camera = camera;
MaxIterations = 5;
MoveTimeoutMilliseconds = 30000;
RecognitionTimeoutMilliseconds = 100000;
RecognitionParameters = new CenterRecognitionParameters();
}
///
/// 逼近轴配置列表
///
public IReadOnlyList Axes
{
get { return _axes; }
private set { _axes = value; }
}
///
/// 相机标识(用于采图和识别)
///
public CameraType Camera { get; set; }
///
/// 最大循环次数(默认 5)
///
public int MaxIterations { get; set; }
///
/// 轴运动的超时时间(毫秒,默认 30000)
///
public int MoveTimeoutMilliseconds { get; set; }
///
/// 识别中心的超时时间(毫秒,默认 10000)
///
public int RecognitionTimeoutMilliseconds { get; set; }
public CenterRecognitionParameters RecognitionParameters { get; set; }
}
///
/// 逼近对位的结果
///
public class ApproachAlignmentResult
{
public ApproachAlignmentResult()
{
FinalErrors = new Dictionary();
FinalAxisPositions = new Dictionary();
}
///
/// 是否成功(所有轴都在允许误差内)
///
public bool Succeeded { get; set; }
///
/// 实际完成的迭代次数
///
public int CompletedIterations { get; set; }
///
/// 最终的每轴误差(轴名 -> 误差值)
///
public Dictionary FinalErrors { get; }
///
/// 最终的轴位置(轴名 -> 位置值,用于调试)
///
public Dictionary FinalAxisPositions { get; }
///
/// 异常信息(如果失败)
///
public Exception Exception { get; set; }
///
/// 获取出错消息
///
public string Message => Exception?.Message ?? (Succeeded ? "Approach alignment succeeded." : "Approach alignment failed.");
}
///
/// 坐标转换结果
///
public class CoordinateTransformResult
{
///
/// 转换后的目标点坐标
///
public System.Windows.Point TargetPoint { get; set; }
///
/// 转换是否成功
///
public bool Succeeded { get; set; }
///
/// 错误或警告信息
///
public string Message { get; set; }
///
/// 转换过程中的异常
///
public Exception Exception { get; set; }
}
#endregion
#region 接口定义
///
/// 中心识别器接口:通过图像算法识别目标中心
///
public interface ICenterRecognizer
{
///
/// 识别图像中心点
///
/// 相机标识(用于指定采图源)
/// 识别超时时间(毫秒)
/// 取消令牌
/// 中心点 (CenterX, CenterY) 或 null 表示识别失败
Task<(double CenterX, double CenterY)?> RecognizeCenterAsync(
CameraType camera,
int timeoutMilliseconds,
CancellationToken cancellationToken = default(CancellationToken));
Task<(double CenterX, double CenterY)?> RecognizeCenterAsync(
CameraType camera,
int timeoutMilliseconds,
CenterRecognitionParameters parameters,
CancellationToken cancellationToken = default(CancellationToken));
}
///
/// 坐标转换器接口:将图像中心点转换为各轴的目标位置
///
public interface ICoordinateTransformer
{
///
/// 将图像中心点坐标转换为轴位置
///
/// 图像中心 X 坐标
/// 图像中心 Y 坐标
/// 目标轴列表
/// 转换结果,包含各轴的目标位置
Task TransformAsync(
System.Windows. Point ruler, System.Windows.Point pixel, string cameraName);
}
#endregion
}