using Community;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Model.MotionControl
{
///
/// 运行点位
///
public class MotionPointModel : NotificationObject
{
public int ID { get; set; }
private int pointNumber;
///
/// 序号
///
public int PointNumber
{
get { return pointNumber; }
set
{
pointNumber = value;
this.RaisePropertyChanged("Number");
}
}
///
/// 机种ID
///
public int MachineID { get; set; }
private string pointName;
///
/// 点名称
///
public string PointName
{
get { return pointName; }
set
{
pointName = value;
this.RaisePropertyChanged("PointName");
}
}
private int pointClass;
///
/// 点位分类 1:点位分类1 2:点位分类2
///
public int PointClass
{
get { return pointClass; }
set
{
pointClass = value;
this.RaisePropertyChanged("PointClass");
}
}
private int pointType;
///
/// 点位类型 1:取料点 2:作用点 3:视觉点 4:待机点
///
public int PointType
{
get { return pointType; }
set
{
pointType = value;
this.RaisePropertyChanged("PointType");
}
}
private double shaftPoint_X;
///
/// X轴坐标点
///
public double ShaftPoint_X
{
get { return shaftPoint_X; }
set
{
shaftPoint_X = value;
this.RaisePropertyChanged("ShaftPoint_X");
}
}
private double shaftPoint_Y;
///
/// Y轴坐标点
///
public double ShaftPoint_Y
{
get { return shaftPoint_Y; }
set
{
shaftPoint_Y = value;
this.RaisePropertyChanged("ShaftPoint_Y");
}
}
private double shaftPoint_Z;
///
/// Z轴坐标点
///
public double ShaftPoint_Z
{
get { return shaftPoint_Z; }
set
{
shaftPoint_Z = value;
this.RaisePropertyChanged("ShaftPoint_Z");
}
}
private double shaftPoint_U;
///
/// U轴坐标点(旋转)
///
public double ShaftPoint_U
{
get { return shaftPoint_U; }
set
{
shaftPoint_U = value;
this.RaisePropertyChanged("ShaftPoint_U");
}
}
private double speed_X;
///
/// X轴移动速度
///
public double Speed_X
{
get { return speed_X; }
set
{
speed_X = value;
this.RaisePropertyChanged("Speed_X");
}
}
private double speed_Y;
///
/// Y轴移动速度
///
public double Speed_Y
{
get { return speed_Y; }
set
{
speed_Y = value;
this.RaisePropertyChanged("Speed_Y");
}
}
private double speed_Z;
///
/// Z轴移动速度
///
public double Speed_Z
{
get { return speed_Z; }
set
{
speed_Z = value;
this.RaisePropertyChanged("Speed_Z");
}
}
private double speed_U;
///
/// U轴移动速度
///
public double Speed_U
{
get { return speed_U; }
set
{
speed_U = value;
this.RaisePropertyChanged("Speed_U");
}
}
private double speed_Gear;
///
/// 速度档位(自动挡)
///
public double Speed_Gear
{
get { return speed_Gear; }
set
{
speed_Gear = value;
this.RaisePropertyChanged("Speed_Gear");
}
}
private int shaftID_X;
///
/// X轴ID
///
public int ShaftID_X
{
get { return shaftID_X; }
set
{
shaftID_X = value;
this.RaisePropertyChanged("ShaftID_X");
}
}
private int shaftID_Y;
///
/// Y轴ID
///
public int ShaftID_Y
{
get { return shaftID_Y; }
set
{
shaftID_Y = value;
this.RaisePropertyChanged("ShaftID_Y");
}
}
private int shaftID_Z;
///
/// Z轴ID
///
public int ShaftID_Z
{
get { return shaftID_Z; }
set
{
shaftID_Z = value;
this.RaisePropertyChanged("ShaftID_Z");
}
}
private int shaftID_U;
///
/// U轴ID
///
public int ShaftID_U
{
get { return shaftID_U; }
set
{
shaftID_U = value;
this.RaisePropertyChanged("ShaftID_U");
}
}
///
/// 点位进行中
///
public bool DispensingRuning { get; set; }
#region 轴对象
///
/// X轴
///
public FeiYuShaft Shaft_X { get; set; }
///
/// Y轴
///
public FeiYuShaft Shaft_Y { get; set; }
///
/// Z轴
///
public FeiYuShaft Shaft_Z { get; set; }
///
/// U轴
///
public FeiYuShaft Shaft_U { get; set; }
#endregion
///
/// Z轴是否要上升
///
bool RiseZ { get; set; }
///
/// 点位运行步骤
///
public GengralProcessEnum PointRunStep { get; set; }
#region 实例化点位参数(包含插补参数)
///
/// 此点位的轴是否在同一控制器
///
public bool SameController { get; set; }
///
/// 实例化点位参数(包含插补参数)
///
/// 所有轴集合
/// 降速倍数
public void GetInterpolationPer(ObservableCollection AllShaftList, int multiple = 1)
{
Shaft_X = AllShaftList.Where(s => s.ID == this.ShaftID_X && this.ShaftID_X > 0).FirstOrDefault();
Shaft_Y = AllShaftList.Where(s => s.ID == this.ShaftID_Y && this.ShaftID_Y > 0).FirstOrDefault();
Shaft_Z = AllShaftList.Where(s => s.ID == this.ShaftID_Z && this.ShaftID_Z > 0).FirstOrDefault();
Shaft_U = AllShaftList.Where(s => s.ID == this.ShaftID_U && this.ShaftID_U > 0).FirstOrDefault();
this.Speed_X = Shaft_X != null ? this.Speed_Gear : 0;
this.Speed_Y = Shaft_Y != null ? this.Speed_Gear : 0;
this.Speed_Z = Shaft_Z != null ? this.Speed_Gear : 0;
this.Speed_U = Shaft_U != null ? this.Speed_Gear : 0;
//轴对应的卡号一致
if (Shaft_X != null && Shaft_Y != null && (Shaft_X.MyCard.ID == Shaft_Y.MyCard.ID))
{
SameController = true;
}
else
{
SameController = false;
}
if (Shaft_X != null)
{
Shaft_X.GengrelMp = Coper(Shaft_X.MyMotionPar);
Shaft_X.GengrelMp.ActualDir = Math.Round(this.ShaftPoint_X, 2);
Shaft_X.GengrelMp.StartSpeed = Math.Round(Shaft_X.GengrelMp.StartSpeed / multiple, 2);
Shaft_X.GengrelMp.RunSpeed = Math.Round(this.Speed_X / multiple, 2);
}
if (Shaft_Y != null)
{
Shaft_Y.GengrelMp = Coper(Shaft_Y.MyMotionPar);
Shaft_Y.GengrelMp.ActualDir = Math.Round(this.ShaftPoint_Y, 2);
Shaft_Y.GengrelMp.StartSpeed = Math.Round(Shaft_Y.GengrelMp.StartSpeed / multiple, 2);
Shaft_Y.GengrelMp.RunSpeed = Math.Round(this.Speed_Y / multiple, 2);
}
if (Shaft_Z != null)
{
Shaft_Z.GengrelMp = Coper(Shaft_Z.MyMotionPar);
Shaft_Z.GengrelMp.ActualDir = Math.Round(this.ShaftPoint_Z, 2);
Shaft_Z.GengrelMp.StartSpeed = Math.Round(Shaft_Z.GengrelMp.StartSpeed / multiple, 2);
Shaft_Z.GengrelMp.RunSpeed = Math.Round(this.Speed_Z / multiple, 2);
}
if (Shaft_U != null)
{
Shaft_U.GengrelMp = Coper(Shaft_U.MyMotionPar);
Shaft_U.GengrelMp.ActualDir = Math.Round(this.ShaftPoint_U, 2);
Shaft_U.GengrelMp.StartSpeed = Math.Round(Shaft_U.GengrelMp.StartSpeed / multiple, 2);
Shaft_U.GengrelMp.RunSpeed = Math.Round(this.Speed_U / multiple, 2);
}
//判断Z轴是否要上升(当前位置与目标位置相差1mm以上的 Z轴上升)
//if ((Shaft_X == null || Math.Abs(Shaft_X.GengrelMp.ActualDir - Shaft_X.ActualDir) < 1) && (Shaft_Y == null || Math.Abs(Shaft_Y.GengrelMp.ActualDir - Shaft_Y.ActualDir) < 1) && (Shaft_U == null || Math.Abs(Shaft_U.GengrelMp.ActualDir - Shaft_U.ActualDir) < 1))
//{
// RiseZ = false;
//}
//else
//{
// RiseZ = true;
//}
RiseZ = false;
}
#endregion
#region 多轴同时运行
///
/// 多轴同时运行
///
private void MoreShaftRun()
{
if (Shaft_X != null)
{
Shaft_X.DZDC(Shaft_X.GengrelMp);
}
if (Shaft_Y != null)
{
Shaft_Y.DZDC(Shaft_Y.GengrelMp);
}
if (Shaft_U != null)
{
Shaft_U.DZDC(Shaft_U.GengrelMp);
}
if (!RiseZ)
{
if (Shaft_Z != null)
{
Shaft_Z.DZDC(Shaft_Z.GengrelMp);
}
}
}
#endregion
#region 多轴同时运行完成
///
/// 多轴同时运行是否完成
///
private bool MoreShaftRrunOK()
{
if (Shaft_X != null)
{
if (Shaft_X.ActualDir != Math.Round(Shaft_X.GengrelMp.ActualDir, 2))
{
return false;
}
}
if (Shaft_Y != null)
{
if (Shaft_Y.ActualDir != Math.Round(Shaft_Y.GengrelMp.ActualDir, 2))
{
return false;
}
}
if (Shaft_U != null)
{
//if (Shaft_U.ActualDir != Math.Round(Shaft_U.GengrelMp.ActualDir, 2))
//{
// return false;
//}
//因为 360°除不尽 存在误差 故用此方式
if (Math.Abs(Shaft_U.ActualDir - Math.Round(Shaft_U.GengrelMp.ActualDir, 2)) >= 0.5)
{
return false;
}
}
if (!RiseZ)
{
if (Shaft_Z != null)
{
if (Shaft_Z.ActualDir != Math.Round(Shaft_Z.GengrelMp.ActualDir, 2))
{
return false;
}
}
}
return true;
}
#endregion
#region 运行点位
///
/// 运行点位
///
/// 所有轴集合
/// 降速倍数
///
public bool PointRun(ObservableCollection AllShaftList, int multiple = 1, int isAuto = 0)
{
bool bl = false;
//升降Z轴防呆,两个产品检知亮或都不亮时,才能走点位
if ((InputList[39].LightUp && !InputList[40].LightUp) || (!InputList[39].LightUp && InputList[40].LightUp))
{
//获取升降轴对象
FeiYuShaft shaft = AllShaftList.Where(w => w.InCode == "SJ" && w.ID == ShaftID_Z).FirstOrDefault();
//升降轴急停
if (shaft != null)
{
shaft.ShaftStop();
if (isAuto == 1) //手动
{
PointRunStep = GengralProcessEnum.Start;
}
else //自动,点位停止运行
{
PointRunStep = GengralProcessEnum.End;
return false;
}
}
}
try
{
switch (PointRunStep)
{
case GengralProcessEnum.Start:
GetInterpolationPer(AllShaftList, multiple);
if (Shaft_Z != null)
{
//判断Z是否需要上升
if (!RiseZ)
{
PointRunStep = GengralProcessEnum.Step2;
}
else
{
Shaft_Z.GengrelMp.ActualDir = 0;
Shaft_Z.GengrelMp.RunSpeed = this.Speed_Z;
Shaft_Z.DZDC(Shaft_Z.GengrelMp);
PointRunStep++;
}
}
else
{
PointRunStep = GengralProcessEnum.Step2;
}
break;
case GengralProcessEnum.Step1:
if (Shaft_Z == null || (Shaft_Z.ActualDir == 0))// && Shaft_Z.ss.org == 1
{
Shaft_Z.GengrelMp = Coper(Shaft_Z.MyMotionPar);
Shaft_Z.GengrelMp.ActualDir = Math.Round(this.ShaftPoint_Z, 2);
Shaft_Z.GengrelMp.StartSpeed = Math.Round(Shaft_Z.GengrelMp.StartSpeed / multiple, 2);
Shaft_Z.GengrelMp.RunSpeed = Math.Round(this.Speed_Z / multiple, 2);
PointRunStep++;
}
break;
case GengralProcessEnum.Step2:
if (MoreShaftRrunOK())
{
if (Shaft_Z != null)
{
Shaft_Z.DZDC(Shaft_Z.GengrelMp);
PointRunStep++;
}
else
{
PointRunStep = GengralProcessEnum.Start;
bl = true;
}
}
else
{
if (Shaft_Z == null || !RiseZ || (Shaft_Z.ActualDir == 0)) // && Shaft_Z.ss.org == 1
{
MoreShaftRun();
}
else
{
PointRunStep = GengralProcessEnum.Start;
}
}
break;
case GengralProcessEnum.Step3:
if (Shaft_Z == null || (Shaft_Z.ActualDir == Shaft_Z.GengrelMp.ActualDir && Shaft_Z.ShaftState.StatusBackground == ColorStr.Red))
{
PointRunStep = GengralProcessEnum.Start;
bl = true;
}
break;
}
}
catch (Exception)
{
//string str = PointRunStep.ToString();
}
return bl;
}
#endregion
#region 停止点位运行
///
/// 停止点位运行
///
public void PointStop()
{
if (Shaft_Z != null)
{
Shaft_Z.ShaftStop();
}
if (Shaft_X != null)
{
Shaft_X.ShaftStop();
}
if (Shaft_Y != null)
{
Shaft_Y.ShaftStop();
}
if (Shaft_U != null)
{
Shaft_U.ShaftStop();
}
PointRunStep = GengralProcessEnum.Start;
}
#endregion
#region 利用反射进行对象复制
///
/// 利用反射进行对象复制
///
///
///
///
public T Coper(T Coperobj)
{
T a = System.Activator.CreateInstance();
try
{
var Types = Coperobj.GetType();//获得类型
var Typed = a.GetType();
foreach (PropertyInfo sp in Types.GetProperties())//获得类型的属性字段
{
foreach (PropertyInfo dp in Typed.GetProperties())
{
if (dp.Name == sp.Name)//判断属性名是否相同
{
dp.SetValue(a, sp.GetValue(Coperobj, null), null);//获得s对象属性的值复制给d对象的属性
}
}
}
}
catch (Exception ex)
{
throw ex;
}
return a;
}
#endregion
///
/// 输入信号集合,用于防呆,防止撞机
///
public List InputList { get; set; }
}
}