using PLCSharp.Core.Tools;
using Prism.Mvvm;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json;
using System.Text.Json.Serialization;
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 => JsonSerializer.Serialize(Interpolations, JsonOptions.Default); // 自动序列化
set
{
Interpolations = JsonSerializer.Deserialize>(value, JsonOptions.Default); // 自动反序列化
}
}
private ObservableCollection _Interpolations = [];
///
/// Interpolations
///
[NotMapped]
public ObservableCollection Interpolations
{
get { return _Interpolations; }
set { SetProperty(ref _Interpolations, value); }
}
///
/// 序列化Params
///
[Column("Params")]
public string SerializedParams
{
get => JsonSerializer.Serialize(Params, JsonOptions.Default); // 自动序列化
set => Params = JsonSerializer.Deserialize(value, JsonOptions.Default); // 自动反序列化
}
private InterpolationGroupParams _Params;
///
/// 参数集合
///
[NotMapped]
public InterpolationGroupParams Params
{
get
{
_Params ??= new InterpolationGroupParams();
return _Params;
}
set
{
SetProperty(ref _Params, value);
}
}
///
/// 插补GroupParams
///
public class InterpolationGroupParams : BindableBase
{
internal InterpolationGroupParams Snapshot() => (InterpolationGroupParams)MemberwiseClone();
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 方法
internal sealed class InterpolationRun(InterpolationGroup group)
{
internal readonly Axis X = group.AxisX;
internal readonly Axis Y = group.AxisY;
internal readonly InterpolationGroupParams Parameters = group.Params.Snapshot();
internal readonly List Segments = [.. group.Interpolations];
internal volatile bool Completed;
}
private readonly object _gate = new();
private InterpolationRun _run;
private bool _pending;
public void Run()
{
lock (_gate)
{
if (!_pending) return;
_pending = false;
var axis = _run.X;
lock (axis.InterpolationGate)
{
if (axis.MotionFlow.Step != 0 || _run.Y.MotionFlow.Step != 0)
{
axis.ErrorMessage = "插补轴忙,未启动插补";
return;
}
axis.Interpolations = _run.Segments;
axis.InterpolationGroupParams = _run.Parameters;
axis.InterpolationResult = _run;
axis.InterpolationDone = false;
axis.MotionFlow.Step = 31;
}
}
}
public bool Go()
{
lock (_gate)
{
if (_pending) return false;
if (_run != null)
{
lock (_run.X.InterpolationGate)
if (_run.X.InterpolationResult == _run && _run.X.MotionFlow.Step is 31 or 32)
return false;
}
_run = null;
if (AxisX == null || AxisY == null || Interpolations.Count == 0 ||
AxisX.MotionFlow.Step != 0 || AxisY.MotionFlow.Step != 0) return false;
_run = new InterpolationRun(this);
_pending = true;
return true;
}
}
public bool IsDone()
{
lock (_gate) return _run?.Completed == true;
}
public void Stop()
{
lock (_gate)
{
_pending = false;
(_run?.X ?? AxisX)?.Stop();
(_run?.Y ?? AxisY)?.Stop();
_run = null;
}
}
#endregion 方法
}
}