TFLH/Model/MotionControl/MotionPointModel.cs
2026-08-06 09:50:46 +08:00

631 lines
18 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
{
/// <summary>
/// 运行点位
/// </summary>
public class MotionPointModel : NotificationObject
{
public int ID { get; set; }
private int pointNumber;
/// <summary>
/// 序号
/// </summary>
public int PointNumber
{
get { return pointNumber; }
set
{
pointNumber = value;
this.RaisePropertyChanged("Number");
}
}
/// <summary>
/// 机种ID
/// </summary>
public int MachineID { get; set; }
private string pointName;
/// <summary>
/// 点名称
/// </summary>
public string PointName
{
get { return pointName; }
set
{
pointName = value;
this.RaisePropertyChanged("PointName");
}
}
private int pointClass;
/// <summary>
/// 点位分类 1:点位分类1 2:点位分类2
/// </summary>
public int PointClass
{
get { return pointClass; }
set
{
pointClass = value;
this.RaisePropertyChanged("PointClass");
}
}
private int pointType;
/// <summary>
/// 点位类型 1:取料点 2:作用点 3:视觉点 4:待机点
/// </summary>
public int PointType
{
get { return pointType; }
set
{
pointType = value;
this.RaisePropertyChanged("PointType");
}
}
private double shaftPoint_X;
/// <summary>
/// X轴坐标点
/// </summary>
public double ShaftPoint_X
{
get { return shaftPoint_X; }
set
{
shaftPoint_X = value;
this.RaisePropertyChanged("ShaftPoint_X");
}
}
private double shaftPoint_Y;
/// <summary>
/// Y轴坐标点
/// </summary>
public double ShaftPoint_Y
{
get { return shaftPoint_Y; }
set
{
shaftPoint_Y = value;
this.RaisePropertyChanged("ShaftPoint_Y");
}
}
private double shaftPoint_Z;
/// <summary>
/// Z轴坐标点
/// </summary>
public double ShaftPoint_Z
{
get { return shaftPoint_Z; }
set
{
shaftPoint_Z = value;
this.RaisePropertyChanged("ShaftPoint_Z");
}
}
private double shaftPoint_U;
/// <summary>
/// U轴坐标点(旋转)
/// </summary>
public double ShaftPoint_U
{
get { return shaftPoint_U; }
set
{
shaftPoint_U = value;
this.RaisePropertyChanged("ShaftPoint_U");
}
}
private double speed_X;
/// <summary>
/// X轴移动速度
/// </summary>
public double Speed_X
{
get { return speed_X; }
set
{
speed_X = value;
this.RaisePropertyChanged("Speed_X");
}
}
private double speed_Y;
/// <summary>
/// Y轴移动速度
/// </summary>
public double Speed_Y
{
get { return speed_Y; }
set
{
speed_Y = value;
this.RaisePropertyChanged("Speed_Y");
}
}
private double speed_Z;
/// <summary>
/// Z轴移动速度
/// </summary>
public double Speed_Z
{
get { return speed_Z; }
set
{
speed_Z = value;
this.RaisePropertyChanged("Speed_Z");
}
}
private double speed_U;
/// <summary>
/// U轴移动速度
/// </summary>
public double Speed_U
{
get { return speed_U; }
set
{
speed_U = value;
this.RaisePropertyChanged("Speed_U");
}
}
private double speed_Gear;
/// <summary>
/// 速度档位(自动挡)
/// </summary>
public double Speed_Gear
{
get { return speed_Gear; }
set
{
speed_Gear = value;
this.RaisePropertyChanged("Speed_Gear");
}
}
private int shaftID_X;
/// <summary>
/// X轴ID
/// </summary>
public int ShaftID_X
{
get { return shaftID_X; }
set
{
shaftID_X = value;
this.RaisePropertyChanged("ShaftID_X");
}
}
private int shaftID_Y;
/// <summary>
/// Y轴ID
/// </summary>
public int ShaftID_Y
{
get { return shaftID_Y; }
set
{
shaftID_Y = value;
this.RaisePropertyChanged("ShaftID_Y");
}
}
private int shaftID_Z;
/// <summary>
/// Z轴ID
/// </summary>
public int ShaftID_Z
{
get { return shaftID_Z; }
set
{
shaftID_Z = value;
this.RaisePropertyChanged("ShaftID_Z");
}
}
private int shaftID_U;
/// <summary>
/// U轴ID
/// </summary>
public int ShaftID_U
{
get { return shaftID_U; }
set
{
shaftID_U = value;
this.RaisePropertyChanged("ShaftID_U");
}
}
/// <summary>
/// 点位进行中
/// </summary>
public bool DispensingRuning { get; set; }
#region 轴对象
/// <summary>
/// X轴
/// </summary>
public Shaft Shaft_X { get; set; }
/// <summary>
/// Y轴
/// </summary>
public Shaft Shaft_Y { get; set; }
/// <summary>
/// Z轴
/// </summary>
public Shaft Shaft_Z { get; set; }
/// <summary>
/// U轴
/// </summary>
public Shaft Shaft_U { get; set; }
#endregion
/// <summary>
/// Z轴是否要上升
/// </summary>
bool RiseZ { get; set; }
/// <summary>
/// 点位运行步骤
/// </summary>
public GengralProcessEnum PointRunStep { get; set; }
#region 实例化点位参数(包含插补参数)
/// <summary>
/// 此点位的轴是否在同一控制器
/// </summary>
public bool SameController { get; set; }
/// <summary>
/// 实例化点位参数(包含插补参数)
/// </summary>
/// <param name="AllShaftList">所有轴集合</param>
/// <param name="multiple">降速倍数</param>
public void GetInterpolationPer(ObservableCollection<Shaft> 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();
//轴对应的卡号一致
if (Shaft_X != null && Shaft_Y != null && (Shaft_X.CardID == Shaft_Y.CardID))
{
SameController = true;
}
else
{
SameController = false;
}
if (Shaft_X != null)
{
Shaft_X.GengrelMp = Coper(Shaft_X.Standbys);
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.Standbys);
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.Standbys);
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.Standbys);
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;
}
}
#endregion
#region 多轴同时运行
/// <summary>
/// 多轴同时运行
/// </summary>
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 多轴同时运行完成
/// <summary>
/// 多轴同时运行是否完成
/// </summary>
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 运行点位
/// <summary>
/// 运行点位
/// </summary>
/// <param name="AllShaftList">所有轴集合</param>
/// <param name="multiple">降速倍数</param>
/// <returns></returns>
public bool PointRun(ObservableCollection<Shaft> AllShaftList, int multiple = 1)
{
bool bl = 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.Standbys);
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)
{
PointRunStep = GengralProcessEnum.Start;
bl = true;
}
break;
}
}
catch (Exception)
{
string str = PointRunStep.ToString();
}
return bl;
}
#endregion
#region 停止点位运行
/// <summary>
/// 停止点位运行
/// </summary>
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 利用反射进行对象复制
/// <summary>
/// 利用反射进行对象复制
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="Coperobj"></param>
/// <returns></returns>
public T Coper<T>(T Coperobj)
{
T a = System.Activator.CreateInstance<T>();
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
}
}