370 lines
9.8 KiB
C#
370 lines
9.8 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// 插补Group
|
|
/// </summary>
|
|
public class InterpolationGroup : BindableBase
|
|
{
|
|
#region 属性
|
|
|
|
/// <summary>
|
|
/// 唯一标识
|
|
/// </summary>
|
|
[Key]
|
|
public Guid ID { get; set; } = Guid.NewGuid();
|
|
|
|
/// <summary>
|
|
/// 配方标识
|
|
/// </summary>
|
|
public Guid RecipeID { get; set; }
|
|
|
|
private string _Name;
|
|
|
|
/// <summary>
|
|
/// 点名称
|
|
/// </summary>
|
|
public string Name
|
|
{
|
|
get { return _Name; }
|
|
set { SetProperty(ref _Name, value); }
|
|
}
|
|
|
|
private Axis _AxisX;
|
|
|
|
/// <summary>
|
|
/// 轴X
|
|
/// </summary>
|
|
[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;
|
|
|
|
/// <summary>
|
|
/// 轴X名称
|
|
/// </summary>
|
|
public string AxisXName
|
|
{
|
|
get { return _AxisXName; }
|
|
set { SetProperty(ref _AxisXName, value); }
|
|
}
|
|
|
|
private Axis _AxisY;
|
|
|
|
/// <summary>
|
|
/// 轴Y
|
|
/// </summary>
|
|
[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;
|
|
|
|
/// <summary>
|
|
/// 轴Y名称
|
|
/// </summary>
|
|
public string AxisYName
|
|
{
|
|
get { return _AxisYName; }
|
|
set { SetProperty(ref _AxisYName, value); }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 序列化Interpolations
|
|
/// </summary>
|
|
[Column("Interpolations")]
|
|
public string SerializedInterpolations
|
|
{
|
|
get => JsonSerializer.Serialize(Interpolations, JsonOptions.Default); // 自动序列化
|
|
set
|
|
{
|
|
Interpolations = JsonSerializer.Deserialize<ObservableCollection<Interpolation>>(value, JsonOptions.Default); // 自动反序列化
|
|
}
|
|
}
|
|
|
|
private ObservableCollection<Interpolation> _Interpolations = [];
|
|
|
|
/// <summary>
|
|
/// Interpolations
|
|
/// </summary>
|
|
[NotMapped]
|
|
public ObservableCollection<Interpolation> Interpolations
|
|
{
|
|
get { return _Interpolations; }
|
|
set { SetProperty(ref _Interpolations, value); }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 序列化Params
|
|
/// </summary>
|
|
[Column("Params")]
|
|
public string SerializedParams
|
|
{
|
|
get => JsonSerializer.Serialize(Params, JsonOptions.Default); // 自动序列化
|
|
set => Params = JsonSerializer.Deserialize<InterpolationGroupParams>(value, JsonOptions.Default); // 自动反序列化
|
|
}
|
|
|
|
private InterpolationGroupParams _Params;
|
|
|
|
/// <summary>
|
|
/// 参数集合
|
|
/// </summary>
|
|
[NotMapped]
|
|
public InterpolationGroupParams Params
|
|
{
|
|
get
|
|
{
|
|
_Params ??= new InterpolationGroupParams();
|
|
|
|
return _Params;
|
|
}
|
|
set
|
|
{
|
|
SetProperty(ref _Params, value);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 插补GroupParams
|
|
/// </summary>
|
|
public class InterpolationGroupParams : BindableBase
|
|
{
|
|
internal InterpolationGroupParams Snapshot() => (InterpolationGroupParams)MemberwiseClone();
|
|
|
|
private string _Prompt;
|
|
|
|
/// <summary>
|
|
/// 提示
|
|
/// </summary>
|
|
[JsonIgnore]
|
|
public string Prompt
|
|
{
|
|
get { return _Prompt; }
|
|
set { SetProperty(ref _Prompt, value); }
|
|
}
|
|
|
|
private double _MinVelocity;
|
|
|
|
/// <summary>
|
|
/// 最小Velocity
|
|
/// </summary>
|
|
public double MinVelocity
|
|
{
|
|
get { return _MinVelocity; }
|
|
set { SetProperty(ref _MinVelocity, value); }
|
|
}
|
|
|
|
private double _MaxVelocity = 1000;
|
|
|
|
/// <summary>
|
|
/// 最大Velocity
|
|
/// </summary>
|
|
public double MaxVelocity
|
|
{
|
|
get { return _MaxVelocity; }
|
|
set { SetProperty(ref _MaxVelocity, value); }
|
|
}
|
|
|
|
private double _Acc = 0.1;
|
|
|
|
/// <summary>
|
|
/// 加速时间
|
|
/// </summary>
|
|
public double Acc
|
|
{
|
|
get { return _Acc; }
|
|
set { SetProperty(ref _Acc, value); }
|
|
}
|
|
|
|
private double _Dec = 0.1;
|
|
|
|
/// <summary>
|
|
/// Dec
|
|
/// </summary>
|
|
public double Dec
|
|
{
|
|
get { return _Dec; }
|
|
set { SetProperty(ref _Dec, value); }
|
|
}
|
|
|
|
private double _StopVelocity;
|
|
|
|
/// <summary>
|
|
/// 停止Velocity
|
|
/// </summary>
|
|
public double StopVelocity
|
|
{
|
|
get { return _StopVelocity; }
|
|
set { SetProperty(ref _StopVelocity, value); }
|
|
}
|
|
|
|
private ushort _Coordinate;
|
|
|
|
/// <summary>
|
|
/// 坐标系
|
|
/// </summary>
|
|
public ushort Coordinate
|
|
{
|
|
get { return _Coordinate; }
|
|
set { SetProperty(ref _Coordinate, value); }
|
|
}
|
|
|
|
private double _Ahead;
|
|
|
|
/// <summary>
|
|
/// Ahead
|
|
/// </summary>
|
|
public double Ahead
|
|
{
|
|
get { return _Ahead; }
|
|
set { SetProperty(ref _Ahead, value); }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 轴XNo
|
|
/// </summary>
|
|
[JsonIgnore]
|
|
public ushort AxisXNo { get; set; }
|
|
|
|
/// <summary>
|
|
/// 轴YNo
|
|
/// </summary>
|
|
[JsonIgnore]
|
|
public ushort AxisYNo { get; set; }
|
|
|
|
private ushort _OutBit;
|
|
|
|
/// <summary>
|
|
/// OutBit
|
|
/// </summary>
|
|
public ushort OutBit
|
|
{
|
|
get { return _OutBit; }
|
|
set { SetProperty(ref _OutBit, value); }
|
|
}
|
|
|
|
private double _DelayValue;
|
|
|
|
/// <summary>
|
|
/// Delay值
|
|
/// </summary>
|
|
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<Interpolation> 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 方法
|
|
}
|
|
} |