提交位移台和对焦系统代码
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace HardwareControl.Core.AxisPlatform
|
||||
{
|
||||
/// <summary>
|
||||
/// AxisPlatform2.dll 的 P/Invoke 声明。
|
||||
/// 所有结构体按 C 端 #pragma pack(1) 布局,字段顺序与 axis_platform2.h 一致。
|
||||
/// </summary>
|
||||
internal static class AxisPlatformNative
|
||||
{
|
||||
private const string DllName = "AxisPlatform2.dll";
|
||||
|
||||
// 错误码,对应 AXIS2_ERR_*
|
||||
internal const int Ok = 0;
|
||||
internal const int ErrNullPtr = -1;
|
||||
internal const int ErrBufTooSmall = -2;
|
||||
internal const int ErrFrameTooShort = -3;
|
||||
internal const int ErrComOpenFailed = -4;
|
||||
internal const int ErrComNotOpen = -5;
|
||||
internal const int ErrComSendFailed = -6;
|
||||
internal const int ErrParseFailed = -7;
|
||||
internal const int ErrTimeout = -8;
|
||||
|
||||
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
|
||||
internal static extern IntPtr axis_init(
|
||||
[MarshalAs(UnmanagedType.LPStr)] string comPort,
|
||||
byte headType,
|
||||
IntPtr limits);
|
||||
|
||||
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern void axis_close(IntPtr handle);
|
||||
|
||||
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern int axis_x_move(IntPtr handle, short distance, ushort speed, ushort accel);
|
||||
|
||||
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern int axis_y_move(IntPtr handle, short distance, ushort speed, ushort accel);
|
||||
|
||||
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern int axis_z_move(IntPtr handle, short distance, ushort speed, ushort accel);
|
||||
|
||||
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern int axis_xyz_rel_move(
|
||||
IntPtr handle,
|
||||
ref Axis2MoveParams x,
|
||||
ref Axis2MoveParams y,
|
||||
ref Axis2MoveParams z);
|
||||
|
||||
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern int axis_xyz_abs_move(
|
||||
IntPtr handle,
|
||||
ref Axis2MoveParams x,
|
||||
ref Axis2MoveParams y,
|
||||
ref Axis2MoveParams z);
|
||||
|
||||
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern void axis_flush(IntPtr handle);
|
||||
|
||||
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern int axis_read_upload(IntPtr handle, out Axis2UploadData data);
|
||||
|
||||
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern IntPtr axis_get_error(IntPtr handle);
|
||||
|
||||
/// <summary>读取 DLL 内部记录的最后一次错误描述;无错误或句柄为空时返回空串。</summary>
|
||||
internal static string GetError(IntPtr handle)
|
||||
{
|
||||
if (handle == IntPtr.Zero)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
var pointer = axis_get_error(handle);
|
||||
return pointer == IntPtr.Zero
|
||||
? string.Empty
|
||||
: Marshal.PtrToStringAnsi(pointer) ?? string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>限位参数,对应 Axis2Limits(6 × int32)。</summary>
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
||||
internal struct Axis2Limits
|
||||
{
|
||||
public int XPosLimit;
|
||||
public int XNegLimit;
|
||||
public int YPosLimit;
|
||||
public int YNegLimit;
|
||||
public int ZPosLimit;
|
||||
public int ZNegLimit;
|
||||
}
|
||||
|
||||
/// <summary>单轴运动参数,对应 Axis2MoveParams(6 字节)。</summary>
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
||||
internal struct Axis2MoveParams
|
||||
{
|
||||
public short Distance;
|
||||
public ushort Speed;
|
||||
public ushort Accel;
|
||||
}
|
||||
|
||||
/// <summary>上传数据,对应 Axis2UploadData(13 字节载荷)。</summary>
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
||||
internal struct Axis2UploadData
|
||||
{
|
||||
public byte SysSta;
|
||||
public short XPos;
|
||||
public short YPos;
|
||||
public short ZPos;
|
||||
public byte XSta;
|
||||
public byte YSta;
|
||||
public byte ZSta;
|
||||
public byte Error;
|
||||
public byte HwVersion;
|
||||
public byte SwVersion;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
namespace HardwareControl.Core.AxisPlatform
|
||||
{
|
||||
/// <summary>
|
||||
/// 帧头类型,对应 AXIS_HEAD_*。
|
||||
/// </summary>
|
||||
public enum AxisPlatformHeadType : byte
|
||||
{
|
||||
/// <summary>台式自聚焦二维平台(0x7A)。</summary>
|
||||
Desktop2D = 0x7A,
|
||||
|
||||
/// <summary>传送带三维平台(0x7B)。</summary>
|
||||
Conveyor3D = 0x7B,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 位移台配置,对应 appsettings.json 的 "AxisStage" 节。
|
||||
/// 串口优先从系统参数 translation_com 读取,未配置时才使用 <see cref="ComPort"/>。
|
||||
/// </summary>
|
||||
public sealed class AxisStageOptions
|
||||
{
|
||||
public const string SectionName = "AxisStage";
|
||||
|
||||
/// <summary>
|
||||
/// 回退串口名(例如 "COM5"),仅当系统参数 translation_com 为空时使用。
|
||||
/// </summary>
|
||||
public string? ComPort { get; set; }
|
||||
|
||||
/// <summary>帧头类型,默认台式二维平台。</summary>
|
||||
public AxisPlatformHeadType FrameHead { get; set; } = AxisPlatformHeadType.Desktop2D;
|
||||
|
||||
/// <summary>未显式传参时使用的默认速度。</summary>
|
||||
public ushort DefaultSpeed { get; set; } = 50;
|
||||
|
||||
/// <summary>未显式传参时使用的默认加速度。</summary>
|
||||
public ushort DefaultAccel { get; set; } = 10;
|
||||
|
||||
/// <summary>限位参数;为 null 时不限位(向 DLL 传 NULL)。</summary>
|
||||
public AxisStageLimits? Limits { get; set; }
|
||||
|
||||
/// <summary>读取上传数据的默认超时,单位毫秒。</summary>
|
||||
public int ReadTimeoutMs { get; set; } = 1000;
|
||||
|
||||
/// <summary>读取上传数据失败时的重试间隔,单位毫秒。</summary>
|
||||
public int ReadRetryIntervalMs { get; set; } = 50;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 位移台限位参数(单位 mm)。仅在配置了 "Limits" 节时生效,
|
||||
/// 未配置的轴使用足够大的范围,等效于该轴不限位。
|
||||
/// </summary>
|
||||
public sealed class AxisStageLimits
|
||||
{
|
||||
public int XPositive { get; set; } = 1_000_000;
|
||||
public int XNegative { get; set; } = -1_000_000;
|
||||
public int YPositive { get; set; } = 1_000_000;
|
||||
public int YNegative { get; set; } = -1_000_000;
|
||||
public int ZPositive { get; set; } = 1_000_000;
|
||||
public int ZNegative { get; set; } = -1_000_000;
|
||||
|
||||
internal Axis2Limits ToNative() => new()
|
||||
{
|
||||
XPosLimit = XPositive,
|
||||
XNegLimit = XNegative,
|
||||
YPosLimit = YPositive,
|
||||
YNegLimit = YNegative,
|
||||
ZPosLimit = ZPositive,
|
||||
ZNegLimit = ZNegative,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace HardwareControl.Core.AxisPlatform
|
||||
{
|
||||
/// <summary>三轴坐标/位移,单位 mm。</summary>
|
||||
public readonly record struct AxisStagePoint(int X, int Y, int Z)
|
||||
{
|
||||
public static AxisStagePoint Zero => new(0, 0, 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
namespace HardwareControl.Core.AxisPlatform
|
||||
{
|
||||
/// <summary>
|
||||
/// 位移台上传帧(0xF5)的解析结果。
|
||||
/// </summary>
|
||||
/// <param name="XPosition">X 轴当前位置(mm)。</param>
|
||||
/// <param name="YPosition">Y 轴当前位置(mm)。</param>
|
||||
/// <param name="ZPosition">Z 轴当前位置(mm)。</param>
|
||||
/// <param name="XMoving">X 轴是否运动中。</param>
|
||||
/// <param name="YMoving">Y 轴是否运动中。</param>
|
||||
/// <param name="ZMoving">Z 轴是否运动中。</param>
|
||||
/// <param name="IsDebugMode">设备是否处于 Debug 模式(sys_sta 非 0)。</param>
|
||||
/// <param name="DeviceError">设备端错误码。</param>
|
||||
/// <param name="HardwareVersion">硬件版本号。</param>
|
||||
/// <param name="SoftwareVersion">软件版本号。</param>
|
||||
/// <param name="ReceivedAt">本机解析到该帧的时间。</param>
|
||||
public sealed record AxisStageStatus(
|
||||
int XPosition,
|
||||
int YPosition,
|
||||
int ZPosition,
|
||||
bool XMoving,
|
||||
bool YMoving,
|
||||
bool ZMoving,
|
||||
bool IsDebugMode,
|
||||
int DeviceError,
|
||||
int HardwareVersion,
|
||||
int SoftwareVersion,
|
||||
DateTimeOffset ReceivedAt)
|
||||
{
|
||||
/// <summary>是否有任意轴仍在运动。</summary>
|
||||
public bool AnyMoving => XMoving || YMoving || ZMoving;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace HardwareControl.Core.AxisPlatform
|
||||
{
|
||||
public class AxisStageStatusChangedEventArgs : EventArgs
|
||||
{
|
||||
public bool IsConnected { get; }
|
||||
public string Message { get; }
|
||||
|
||||
public AxisStageStatusChangedEventArgs(bool isConnected, string message)
|
||||
{
|
||||
IsConnected = isConnected;
|
||||
Message = message;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user