using System.Text.Json;
using System.Text.Json.Serialization;
using PLCSharp.Core.Tools;
using PLCSharp.Core.Common;
using Prism.Mvvm;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using static PLCSharp.VVMs.MotionController.InterpolationGroup;
namespace PLCSharp.VVMs.MotionController
{
///
/// 轴
///
public class Axis : BindableBase
{
///
/// 唯一标识
///
[Key]
public Guid ID { get; set; } = Guid.NewGuid();
///
/// ControllerID
///
public Guid ControllerID { get; set; }
private ushort _ControllerNumber;
///
/// 所属的控制序号
///
public ushort ControllerNumber
{
get { return _ControllerNumber; }
set { SetProperty(ref _ControllerNumber, value); }
}
private string _Name;
///
/// 点名称
///
public string Name
{
get { return _Name; }
set { SetProperty(ref _Name, value); }
}
private ushort _AxisNo;
///
/// 轴No
///
public ushort AxisNo
{
get { return _AxisNo; }
set { SetProperty(ref _AxisNo, value); }
}
private string _Prompt;
///
/// 提示
///
[NotMapped]
public string Prompt
{
get { return _Prompt; }
set { SetProperty(ref _Prompt, value); }
}
private string _Comment;
///
/// 备注
///
public string Comment
{
get { return _Comment; }
set { SetProperty(ref _Comment, value); }
}
private bool _ALM;
///
/// ALM
///
[NotMapped]
public bool ALM
{
get { return _ALM; }
set { SetProperty(ref _ALM, value); }
}
private bool _ORG;
///
/// ORG
///
[NotMapped]
public bool ORG
{
get { return _ORG; }
set { SetProperty(ref _ORG, value); }
}
private bool _ORGCreated;
///
/// ORGCreated
///
[NotMapped]
public bool ORGCreated
{
get { return _ORGCreated; }
set { SetProperty(ref _ORGCreated, value); }
}
private bool _PowerOn;
///
/// PowerOn
///
[NotMapped]
public bool PowerOn
{
get { return _PowerOn; }
set { SetProperty(ref _PowerOn, value); }
}
///
/// 创建ORG
///
/// 返回布尔值
public bool CreateORG()
{
if (Moving)
{
ErrorMessage = "运动中,请先停止";
return false;
}
if (Interlock.Home)
{
ErrorMessage = "回原锁定";
return false;
}
ORGCreated = false;
MotionFlow.Step = 1;
return true;
}
private bool _AbsoluteDone;
///
/// AbsoluteDone
///
[NotMapped]
public bool AbsoluteDone
{
get { return _AbsoluteDone; }
set { SetProperty(ref _AbsoluteDone, value); }
}
private bool _RelativeDone;
///
/// RelativeDone
///
[NotMapped]
public bool RelativeDone
{
get { return _RelativeDone; }
set { SetProperty(ref _RelativeDone, value); }
}
private bool _InterpolationDone;
///
/// 插补Done
///
[NotMapped]
public bool InterpolationDone
{
get { return _InterpolationDone; }
set { SetProperty(ref _InterpolationDone, value); }
}
private double _RelativeDistance;
///
/// RelativeDistance
///
[NotMapped]
public double RelativeDistance
{
get { return _RelativeDistance; }
set { SetProperty(ref _RelativeDistance, value); }
}
private AxisLock _Interlock = new();
///
/// Interlock
///
[NotMapped]
public AxisLock Interlock
{
get { return _Interlock; }
set { SetProperty(ref _Interlock, value); }
}
private string _ErrorMessage = "";
///
/// 错误Message
///
[NotMapped]
public string ErrorMessage
{
get { return _ErrorMessage; }
set
{
SetProperty(ref _ErrorMessage, value);
Error = value.Length > 0;
}
}
private bool _Error;
///
/// 错误
///
[NotMapped]
public bool Error
{
get { return _Error; }
set { SetProperty(ref _Error, value); }
}
///
/// 停止
///
public void Stop()
{
MotionFlow.Step = 101;
}
///
/// 运动Flow
///
[NotMapped]
public FlowModel MotionFlow { get; set; } = new();
///
/// Absolute运动
///
/// 返回布尔值
public bool AbsoluteMotion()
{
if (Moving)
{
ErrorMessage = "运动中,请先停止";
return false;
}
if (ORGCreated == false)
{
ErrorMessage = "请先回原点";
return false;
}
if (Interlock.Forward && Params.TargetPos > CommandPosition)
{
ErrorMessage = "正方向锁定";
return false;
}
if (Interlock.Backward && Params.TargetPos < CommandPosition)
{
ErrorMessage = "负方向锁定";
return false;
}
AbsoluteDone = false;
MotionFlow.Step = 11;
return true;
}
///
/// Relative运动
///
/// 返回布尔值
public bool RelativeMotion()
{
if (Moving)
{
ErrorMessage = "运动中,请先停止";
return false;
}
if (Interlock.Forward && Params.TargetDistance > 0)
{
ErrorMessage = "正方向锁定";
return false;
}
if (Interlock.Backward && Params.TargetDistance < 0)
{
ErrorMessage = "负方向锁定";
return false;
}
RelativeDone = false;
MotionFlow.Step = 21;
return true;
}
private bool _LimitP;
///
/// LimitP
///
[NotMapped]
public bool LimitP
{
get { return _LimitP; }
set { SetProperty(ref _LimitP, value); }
}
private bool _LimitN;
///
/// LimitN
///
[NotMapped]
public bool LimitN
{
get { return _LimitN; }
set { SetProperty(ref _LimitN, value); }
}
private bool _Moving;
///
/// Moving
///
[NotMapped]
public bool Moving
{
get { return _Moving; }
set { SetProperty(ref _Moving, value); }
}
private int _ErrCode;
///
/// ErrCode
///
[NotMapped]
public int ErrCode
{
get { return _ErrCode; }
set { SetProperty(ref _ErrCode, value); }
}
private double _CommandPosition;
///
/// Command位置
///
[NotMapped]
public double CommandPosition
{
get { return _CommandPosition; }
set { SetProperty(ref _CommandPosition, value); }
}
private double _EncoderPosition;
///
/// Encoder位置
///
[NotMapped]
public double EncoderPosition
{
get { return _EncoderPosition; }
set { SetProperty(ref _EncoderPosition, value); }
}
private double _Velocity;
///
/// Velocity
///
[NotMapped]
public double Velocity
{
get { return _Velocity; }
set { SetProperty(ref _Velocity, value); }
}
///
/// 序列化Params
///
[Column("Params")]
public string SerializedParams
{
get => JsonSerializer.Serialize(Params, JsonOptions.Default); // 自动序列化
set => Params = JsonSerializer.Deserialize(value, JsonOptions.Default); // 自动反序列化
}
private AxisParams _Params;
///
/// 参数集合
///
[NotMapped]
public AxisParams Params
{
get
{
_Params ??= new AxisParams();
return _Params;
}
set
{
SetProperty(ref _Params, value);
}
}
///
/// 插补GroupParams
///
[NotMapped]
public InterpolationGroupParams InterpolationGroupParams { get; set; }
///
/// Interpolations
///
[NotMapped]
public List Interpolations { get; set; }
///
/// 轴Params
///
public class AxisParams : BindableBase
{
private double _HomeLowVelocity = 10;
///
/// 回原低速
///
public double HomeLowVelocity
{
get { return _HomeLowVelocity; }
set { SetProperty(ref _HomeLowVelocity, value); }
}
private double _HomeHighVelocity = 100;
///
/// 回原高速
///
public double HomeHighVelocity
{
get { return _HomeHighVelocity; }
set { SetProperty(ref _HomeHighVelocity, value); }
}
private double _Equiv = 1000;
///
/// 脉冲当量
///
public double Equiv
{
get { return _Equiv; }
set { SetProperty(ref _Equiv, value); }
}
private ushort _PulseOutMode;
///
/// PulseOutMode
///
public ushort PulseOutMode
{
get { return _PulseOutMode; }
set { SetProperty(ref _PulseOutMode, 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 _Rate = 100;
///
/// Rate
///
[JsonIgnore]
public double Rate
{
get { return _Rate; }
set
{
if (value > 100)
value = 100;
else if (value < 1)
value = 1;
SetProperty(ref _Rate, value);
}
}
private double _StopVelocity;
///
/// 停止Velocity
///
public double StopVelocity
{
get { return _StopVelocity; }
set { SetProperty(ref _StopVelocity, 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 _TargetPos;
///
/// TargetPos
///
public double TargetPos
{
get { return _TargetPos; }
set { SetProperty(ref _TargetPos, value); }
}
private double _TargetDistance;
///
/// TargetDistance
///
public double TargetDistance
{
get { return _TargetDistance; }
set { SetProperty(ref _TargetDistance, value); }
}
private ushort _OrgLogic;
///
/// 原点电平
///
public ushort OrgLogic
{
get { return _OrgLogic; }
set { SetProperty(ref _OrgLogic, value); }
}
private ushort _ForwardLimitLogic;
///
/// ForwardLimitLogic
///
public ushort ForwardLimitLogic
{
get { return _ForwardLimitLogic; }
set { SetProperty(ref _ForwardLimitLogic, value); }
}
private ushort _ForwardLimitEnable;
///
/// ForwardLimit启用
///
public ushort ForwardLimitEnable
{
get { return _ForwardLimitEnable; }
set { SetProperty(ref _ForwardLimitEnable, value); }
}
private ushort _BackwardLimitLogic;
///
/// BackwardLimitLogic
///
public ushort BackwardLimitLogic
{
get { return _BackwardLimitLogic; }
set { SetProperty(ref _BackwardLimitLogic, value); }
}
private ushort _BackwardLimitEnable;
///
/// BackwardLimit启用
///
public ushort BackwardLimitEnable
{
get { return _BackwardLimitEnable; }
set { SetProperty(ref _BackwardLimitEnable, value); }
}
private ushort _ALMLogic;
///
/// ALMLogic
///
public ushort ALMLogic
{
get { return _ALMLogic; }
set { SetProperty(ref _ALMLogic, value); }
}
private ushort _ALMEnable;
///
/// ALM启用
///
public ushort ALMEnable
{
get { return _ALMEnable; }
set { SetProperty(ref _ALMEnable, value); }
}
private ushort _HomeDir;
///
/// 0-负向、1-正向
///
public ushort HomeDir
{
get { return _HomeDir; }
set { SetProperty(ref _HomeDir, value); }
}
private ushort _HomeMode;
///
/// 回原点方式
///
public ushort HomeMode
{
get { return _HomeMode; }
set { SetProperty(ref _HomeMode, value); }
}
}
///
/// 轴Lock
///
public class AxisLock : BindableBase
{
private bool _Home;
///
/// 回零
///
public bool Home
{
get { return _Home; }
set { SetProperty(ref _Home, value); }
}
private bool _Forward;
///
/// 正向
///
public bool Forward
{
get { return _Forward; }
set { SetProperty(ref _Forward, value); }
}
private bool _Backward;
///
/// 反向
///
public bool Backward
{
get { return _Backward; }
set { SetProperty(ref _Backward, value); }
}
}
}
}