using Newtonsoft.Json; using PLCSharp.Core.Common; using Prism.Mvvm; using System.Collections.ObjectModel; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace PLCSharp.VVMs.MotionController { /// /// 插补Group /// public class InterpolationGroup : BindableBase { #region 属性 /// /// 唯一标识 /// [Key] public Guid ID { get; set; } = Guid.NewGuid(); /// /// 配方标识 /// public Guid RecipeID { get; set; } private string _Name; /// /// 点名称 /// public string Name { get { return _Name; } set { SetProperty(ref _Name, value); } } private Axis _AxisX; /// /// 轴X /// [NotMapped] public Axis AxisX { get { return _AxisX; } set { SetProperty(ref _AxisX, value); if (value != null) { AxisXName = value.Name; Params.AxisXNo = value.AxisNo; } else AxisXName = string.Empty; } } private string _AxisXName; /// /// 轴X名称 /// public string AxisXName { get { return _AxisXName; } set { SetProperty(ref _AxisXName, value); } } private Axis _AxisY; /// /// 轴Y /// [NotMapped] public Axis AxisY { get { return _AxisY; } set { SetProperty(ref _AxisY, value); if (value != null) { AxisYName = value.Name; Params.AxisYNo = value.AxisNo; } else AxisYName = string.Empty; } } private string _AxisYName; /// /// 轴Y名称 /// public string AxisYName { get { return _AxisYName; } set { SetProperty(ref _AxisYName, value); } } /// /// 序列化Interpolations /// [Column("Interpolations")] public string SerializedInterpolations { get => JsonConvert.SerializeObject(Interpolations); // 自动序列化 set { Interpolations = JsonConvert.DeserializeObject>(value); // 自动反序列化 } } private ObservableCollection _Interpolations = []; /// /// Interpolations /// [NotMapped] public ObservableCollection Interpolations { get { return _Interpolations; } set { SetProperty(ref _Interpolations, value); } } /// /// 序列化Params /// [Column("Params")] public string SerializedParams { get => JsonConvert.SerializeObject(Params); // 自动序列化 set => Params = JsonConvert.DeserializeObject(value); // 自动反序列化 } private InterpolationGroupParams _Params; /// /// 参数集合 /// [NotMapped] /// /// 配置项 /// public InterpolationGroupParams Params { get { _Params ??= new InterpolationGroupParams(); return _Params; } set { SetProperty(ref _Params, value); } } /// /// 插补GroupParams /// public class InterpolationGroupParams : BindableBase { private string _Prompt; /// /// 提示 /// [JsonIgnore] /// /// 提示 /// public string Prompt { get { return _Prompt; } set { SetProperty(ref _Prompt, value); } } private double _MinVelocity; /// /// 最小Velocity /// public double MinVelocity { get { return _MinVelocity; } set { SetProperty(ref _MinVelocity, value); } } private double _MaxVelocity = 1000; /// /// 最大Velocity /// public double MaxVelocity { get { return _MaxVelocity; } set { SetProperty(ref _MaxVelocity, value); } } private double _Acc = 0.1; /// /// 加速时间 /// public double Acc { get { return _Acc; } set { SetProperty(ref _Acc, value); } } private double _Dec = 0.1; /// /// Dec /// public double Dec { get { return _Dec; } set { SetProperty(ref _Dec, value); } } private double _StopVelocity; /// /// 停止Velocity /// public double StopVelocity { get { return _StopVelocity; } set { SetProperty(ref _StopVelocity, value); } } private ushort _Coordinate; /// /// 坐标系 /// public ushort Coordinate { get { return _Coordinate; } set { SetProperty(ref _Coordinate, value); } } private double _Ahead; /// /// Ahead /// public double Ahead { get { return _Ahead; } set { SetProperty(ref _Ahead, value); } } /// /// 轴XNo /// [JsonIgnore] public ushort AxisXNo { get; set; } /// /// 轴YNo /// [JsonIgnore] public ushort AxisYNo { get; set; } private ushort _OutBit; /// /// OutBit /// public ushort OutBit { get { return _OutBit; } set { SetProperty(ref _OutBit, value); } } private double _DelayValue; /// /// Delay值 /// public double DelayValue { get { return _DelayValue; } set { SetProperty(ref _DelayValue, value); } } } #endregion #region 方法 private readonly FlowModel flowModel = new(); /// /// 运行 /// public void Run() { if (AxisX == null || AxisY == null) return; switch (flowModel.Step) { case 1: AxisX.Interpolations = [.. Interpolations]; AxisX.InterpolationGroupParams = Params; AxisX.MotionFlow.Step = 31; flowModel.Step++; break; case 10: if (AxisX.InterpolationDone) { flowModel.Step = 0; flowModel.Done = true; } break; } } /// /// Go /// /// 返回布尔值 public bool Go() { flowModel.Done = false; if (AxisX == null || AxisY == null) { flowModel.Step = 0; return false; } if (Interpolations.Count < 1) { flowModel.Step = 0; return false; } flowModel.Step = 1; return true; } /// /// IsDone /// /// 返回布尔值 public bool IsDone() { return flowModel.Done; } /// /// 停止 /// public void Stop() { AxisX?.Stop(); AxisY?.Stop(); flowModel.Reset(); } #endregion } }