using System; using System.Collections.Generic; using XP.Hardware.MotionControl.Abstractions; using XP.Hardware.MotionControl.Abstractions.Enums; using XP.Hardware.MotionControl.Config; namespace XP.Hardware.MotionControl.Implementations { /// /// 虚拟运动系统实现 | Simulated Motion System Implementation /// 根据 MotionControlConfig 创建虚拟轴实例,无需真实 PLC 硬件 /// Creates simulated axis instances from MotionControlConfig, no real PLC required /// public class SimulatedMotionSystem : IMotionSystem { private readonly Dictionary _linearAxes = new(); private readonly Dictionary _rotaryAxes = new(); private readonly ISafetyDoor _safetyDoor; private readonly IJoystick _joystick; private readonly IAxisReset _axisReset; /// /// 构造函数 | Constructor /// /// 运动控制配置 | Motion control configuration public SimulatedMotionSystem(MotionControlConfig config) { if (config == null) throw new ArgumentNullException(nameof(config)); // 创建直线轴 | Create linear axes if (config.LinearAxes.TryGetValue(AxisId.SourceZ, out var sz)) _linearAxes[AxisId.SourceZ] = new SimulatedLinearAxis(AxisId.SourceZ, sz.Min, sz.Max, sz.Origin); if (config.LinearAxes.TryGetValue(AxisId.DetectorZ, out var dz)) _linearAxes[AxisId.DetectorZ] = new SimulatedLinearAxis(AxisId.DetectorZ, dz.Min, dz.Max, dz.Origin); if (config.LinearAxes.TryGetValue(AxisId.StageX, out var sx)) _linearAxes[AxisId.StageX] = new SimulatedLinearAxis(AxisId.StageX, sx.Min, sx.Max, sx.Origin); if (config.LinearAxes.TryGetValue(AxisId.StageY, out var sy)) _linearAxes[AxisId.StageY] = new SimulatedLinearAxis(AxisId.StageY, sy.Min, sy.Max, sy.Origin); // 创建旋转轴 | Create rotary axes if (config.RotaryAxes.TryGetValue(RotaryAxisId.DetectorSwing, out var ds)) _rotaryAxes[RotaryAxisId.DetectorSwing] = new SimulatedRotaryAxis(RotaryAxisId.DetectorSwing, ds.Min, ds.Max, ds.Enabled); if (config.RotaryAxes.TryGetValue(RotaryAxisId.StageRotation, out var sr)) _rotaryAxes[RotaryAxisId.StageRotation] = new SimulatedRotaryAxis(RotaryAxisId.StageRotation, sr.Min, sr.Max, sr.Enabled); if (config.RotaryAxes.TryGetValue(RotaryAxisId.FixtureRotation, out var fr)) _rotaryAxes[RotaryAxisId.FixtureRotation] = new SimulatedRotaryAxis(RotaryAxisId.FixtureRotation, fr.Min, fr.Max, fr.Enabled); // 创建辅助设备 | Create helper devices _safetyDoor = new SimulatedSafetyDoor(); _joystick = new SimulatedJoystick(); _axisReset = new SimulatedAxisReset(); } /// public ISafetyDoor SafetyDoor => _safetyDoor; /// public IJoystick Joystick => _joystick; /// public IAxisReset AxisReset => _axisReset; /// public IReadOnlyDictionary LinearAxes => _linearAxes; /// public IReadOnlyDictionary RotaryAxes => _rotaryAxes; /// public ILinearAxis GetLinearAxis(AxisId axisId) { if (_linearAxes.TryGetValue(axisId, out var axis)) return axis; throw new KeyNotFoundException($"[Simulated] 未找到直线轴 {axisId} | Linear axis {axisId} not found"); } /// public IRotaryAxis GetRotaryAxis(RotaryAxisId axisId) { if (_rotaryAxes.TryGetValue(axisId, out var axis)) return axis; throw new KeyNotFoundException($"[Simulated] 未找到旋转轴 {axisId} | Rotary axis {axisId} not found"); } /// public void UpdateAllStatus() { // 虚拟运动系统无需从 PLC 轮询状态 | No PLC polling needed for simulated motion system } } }