using Prism.Mvvm;
using System;
namespace SKMC.Api.Motion.Model
{
///
/// 电机模型
///
public class MotionAxis : BindableBase
{
private long _id;
public long Id
{
get { return _id; }
set { _id = value; RaisePropertyChanged(); }
}
private string _type;
///
/// 电机类型, Closed: 闭环, Open: 开环
///
public string Type
{
get { return _type; }
set { _type = value; }
}
private string _code;
private short level;
///
/// 安全等级, 0: 低压电机, 1: 高压电机
///
public short Level
{
get { return level; }
set { level = value; }
}
///
/// 电机码
///
public string Code
{
get { return _code; }
set { _code = value; RaisePropertyChanged(); }
}
private string catalog;
///
/// 所属(模块)分类
///
public string Catalog
{
get { return catalog; }
set { catalog = value; RaisePropertyChanged(); }
}
private short _axisNo;
///
/// 电机序号
///
public short AxisNo
{
get { return _axisNo; }
set { _axisNo = value; RaisePropertyChanged(); }
}
private char _axisDir;
public char AxisDir
{
get { return _axisDir; }
set { _axisDir = value; RaisePropertyChanged(); }
}
///
/// 默认的安全检查函数, 用于电机调试时
///
public Func SafeChecker { get; set; }
private MotionAxisStatus _axisSts;
///
/// 电机状态
///
public MotionAxisStatus AxisSts
{
get { return _axisSts; }
set { _axisSts = value; RaisePropertyChanged(); }
}
///
/// 到位时允许的偏差 (mm)
///
public double InRange { get; set; }
///
/// 是否已停止
///
public bool IsStopped => _axisSts.IsBusy == 0 && _axisSts.IsDone == 1;
///
/// 是否上使能
///
public bool IsEnabled => _axisSts.IsEnable == 1;
///
/// 如果该轴出现异常, 是否会中断其他电机与IO控制
/// 如果需要对该轴出现异常后进行恢复处理, 为不影响到其他电机与IO控制, 这里需要设置为false
///
public bool CanInterruptAll { get; set; } = true;
///
/// 是否已回零
///
public bool IsHomed
{
get => _axisSts.IsHomed == 1;
set => _axisSts.IsHomed = value ? (byte)1 : (byte)0;
}
private string _name;
///
/// 名称/描述
///
public string Name
{
get { return _name; }
set { _name = value; RaisePropertyChanged(); }
}
private string _ptag;
///
/// 正方向运动标签, 默认→
///
public string PTag
{
get { return _ptag; }
set { _ptag = value; }
}
private string _ntag;
///
/// 负方向运动标签, 默认←
///
public string NTag
{
get { return _ntag; }
set { _ntag = value; }
}
private short _homeMod;
///
/// 驱动器回零模式
///
public short HomeMod
{
get { return _homeMod; }
set { _homeMod = value; RaisePropertyChanged(); }
}
private short _homeDir;
///
/// 回零方向
///
public short HomeDir
{
get { return _homeDir; }
set { _homeDir = value; RaisePropertyChanged(); }
}
private double _lowVel;
///
/// 回零低速
///
public double LowVel
{
get { return _lowVel; }
set { _lowVel = value; RaisePropertyChanged(); }
}
private double _highVel;
///
/// 回零高速
///
public double HighVel
{
get { return _highVel; }
set { _highVel = value; RaisePropertyChanged(); }
}
public string SerialNo { get => $"[{AxisNo}]{AxisDir}: {Code}"; }
public string SerialName { get => $"[{Code}]: {Name}"; }
private int _gearScale;
///
/// 电子齿轮比
///
public int GearScale
{
get { return _gearScale; }
set { _gearScale = value; }
}
private double _distanceR;
///
/// 电机每旋转一圈机构的移动距离, 可以是螺距导程, 也可以是传动轮周长
///
public double DistanceR
{
get { return _distanceR; }
set { _distanceR = value; }
}
private short _unitType;
// 单位类型, 0表示电机步距, 1表示实际距离
public short UnitType
{
get { return _unitType; }
set { _unitType = value; }
}
private string _unitName;
// 单位名称用于显示, 例如mm, °等
public string UnitName
{
get { return _unitName; }
set { _unitName = value; }
}
private string _moveType;
// 移动类型, L直线, R旋转
public string MoveType
{
get { return _moveType; }
set { _moveType = value; }
}
///
/// 距离(mm)对应的电机运动单位
///
public double DistanceUnit
{
get => _gearScale / _distanceR;
}
///
/// 当前ENC值对应的实际距离mm
///
public double Distance
{
get => _axisSts.ENC / DistanceUnit;
}
///
/// 离上一次记录的实际距离 mm
///
public double WatchDistance => _axisSts.WatchPos / DistanceUnit;
private double _prfUnit;
///
/// 运动目标值, 实际单位mm或者角度等
///
public double PrfUnit
{
get { return _prfUnit; }
set
{
_prfUnit = value / DistanceUnit;
AxisSts.PRF = value;
RaisePropertyChanged();
}
}
private double _encUnit;
///
/// 运动反馈值, 实际单位mm或者角度等
///
public double EncUnit
{
get { return _encUnit; }
set
{
_encUnit = value / DistanceUnit;
AxisSts.ENC = value;
if (PercentOn) Percent = (_encUnit - NegativeUnit) / (PositiveUnit - NegativeUnit);
RaisePropertyChanged();
}
}
///
/// 是否使用位置百分比
///
public bool PercentOn { get; set; }
///
/// 负限位位置(mm)
///
public double NegativeUnit { get; set; }
///
/// 正限位位置(mm)
///
public double PositiveUnit { get; set; }
private double percent;
///
/// 位置百分比, 负限位为0%, 正限位为100%
///
public double Percent
{
get { return percent; }
set { percent = value; RaisePropertyChanged(); }
}
///
/// 电机停止(中断)
///
public bool IsInterrupt { get; set; }
///
/// 被占用的handler
///
public int? LockHandler { get; set; }
///
/// 当前位置是否在偏移量之内
///
///
///
///
public bool IsEncInRange(double targetVal)
{
if (_encUnit == targetVal) return true;
else if (_encUnit > targetVal) return _encUnit - targetVal <= InRange;
else return targetVal - _encUnit <= InRange;
}
///
/// 当前位置是否在偏移量之内
///
///
///
///
public bool IsEncInRange(double targetVal, double offset)
{
if (_encUnit == targetVal) return true;
else if (_encUnit > targetVal) return _encUnit - targetVal <= offset;
else return targetVal - _encUnit <= offset;
}
}
}