456 lines
16 KiB
C#
456 lines
16 KiB
C#
using Community;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Collections.ObjectModel;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Model.MotionControl
|
||
{
|
||
/// <summary>
|
||
/// 运行轨迹模型
|
||
/// </summary>
|
||
public class MotionTrajectoryModel : NotificationObject
|
||
{
|
||
public int ID { get; set; }
|
||
|
||
/// <summary>
|
||
/// 序号
|
||
/// </summary>
|
||
public int SerialNumber { get; set; }
|
||
/// <summary>
|
||
/// 上级ID
|
||
/// </summary>
|
||
public int SuperiorID { get; set; }
|
||
|
||
#region 轨迹名称
|
||
private string traName;
|
||
/// <summary>
|
||
/// 轨迹名称
|
||
/// </summary>
|
||
public string TraName
|
||
{
|
||
get { return traName; }
|
||
set
|
||
{
|
||
traName = value;
|
||
this.RaisePropertyChanged("TraName");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 分类(0、左运行轨迹 1、右运行轨迹)
|
||
private int classType;
|
||
/// <summary>
|
||
/// 分类(0、左运行轨迹 1、右运行轨迹)
|
||
/// </summary>
|
||
public int ClassType
|
||
{
|
||
get { return classType; }
|
||
set
|
||
{
|
||
classType = value;
|
||
this.RaisePropertyChanged("ClassType");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 轨迹类型 0、单点 1、直线 2、圆弧 3、椭圆
|
||
private int trajectoryTrype;
|
||
/// <summary>
|
||
/// 轨迹类型 0、单点 1、直线 2、圆弧 3、椭圆
|
||
/// </summary>
|
||
public int TrajectoryTrype
|
||
{
|
||
get { return trajectoryTrype; }
|
||
set
|
||
{
|
||
trajectoryTrype = value;
|
||
switch (value)
|
||
{
|
||
case 0:
|
||
TrajectoryTrypeName = "单点";
|
||
break;
|
||
case 1:
|
||
TrajectoryTrypeName = "直线";
|
||
break;
|
||
case 2:
|
||
TrajectoryTrypeName = "圆弧";
|
||
break;
|
||
case 3:
|
||
TrajectoryTrypeName = "椭圆";
|
||
break;
|
||
}
|
||
this.RaisePropertyChanged("TrajectoryTrype");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 轨迹类型名称
|
||
private string trajectoryTrypeName;
|
||
/// <summary>
|
||
/// 轨迹类型名称
|
||
/// </summary>
|
||
public string TrajectoryTrypeName
|
||
{
|
||
get { return trajectoryTrypeName; }
|
||
set
|
||
{
|
||
trajectoryTrypeName = value;
|
||
this.RaisePropertyChanged("TrajectoryTrypeName");
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 是否抬刀
|
||
private bool liftKnife;
|
||
/// <summary>
|
||
/// 是否抬刀
|
||
/// </summary>
|
||
public bool LiftKnife
|
||
{
|
||
get { return liftKnife; }
|
||
set
|
||
{
|
||
liftKnife = value;
|
||
this.RaisePropertyChanged("LiftKnife");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 抬刀高度
|
||
private int knifeHeight;
|
||
/// <summary>
|
||
/// 抬刀高度
|
||
/// </summary>
|
||
public int KnifeHeight
|
||
{
|
||
get { return knifeHeight; }
|
||
set
|
||
{
|
||
knifeHeight = value;
|
||
this.RaisePropertyChanged("KnifeHeight");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 点位集合
|
||
private ObservableCollection<MotionPointModel> pointList;
|
||
/// <summary>
|
||
/// 点位集合
|
||
/// </summary>
|
||
public ObservableCollection<MotionPointModel> PointList
|
||
{
|
||
get { return pointList; }
|
||
set
|
||
{
|
||
pointList = value;
|
||
this.RaisePropertyChanged("PointList");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 插补参数
|
||
/// <summary>
|
||
/// 控制器连接好
|
||
/// </summary>
|
||
ushort CardNo { get; set; }
|
||
short ret; //返回错误码
|
||
/// <summary>
|
||
/// 位置模式 0:相对模式,1:绝对模式
|
||
/// </summary>
|
||
ushort PositionModel = 1;
|
||
/// <summary>
|
||
/// 参与插补运动的坐标系
|
||
/// </summary>
|
||
ushort Crd { get; set; }
|
||
/// <summary>
|
||
/// 插补运动轴数为2
|
||
/// </summary>
|
||
ushort AxisNum = 2;
|
||
/// <summary>
|
||
/// 轴号数组
|
||
/// </summary>
|
||
ushort[] AxisArray = new ushort[2];
|
||
|
||
/// <summary>
|
||
/// 插补起始速度
|
||
/// </summary>
|
||
double Min_Vel { get; set; }
|
||
/// <summary>
|
||
/// 插补运动最大矢量速度3000unit/s
|
||
/// </summary>
|
||
double Max_Vel { get; set; }
|
||
/// <summary>
|
||
/// 插补停止速度
|
||
/// </summary>
|
||
double Stop_Vel { get; set; }
|
||
/// <summary>
|
||
/// 插补运动加速时间0.2s
|
||
/// </summary>
|
||
double Tacc { get; set; }
|
||
/// <summary>
|
||
/// 插补运动减速时间0.1s
|
||
/// </summary>
|
||
double Tdec { get; set; }
|
||
|
||
/// <summary>
|
||
/// 保留参数,固定值为0
|
||
/// </summary>
|
||
ushort Smode = 0;
|
||
/// <summary>
|
||
/// 平滑时间为0.05s
|
||
/// </summary>
|
||
double Spara = 0.05;
|
||
/// <summary>
|
||
/// 是否启用Blend 功能,0 不使用,1 使用
|
||
/// </summary>
|
||
//ushort Blend = 1;
|
||
/// <summary>
|
||
/// 终点坐标数组
|
||
/// </summary>
|
||
double[] EndArray = new double[2]; //定义终点坐标
|
||
/// <summary>
|
||
/// //圆弧方向,0:顺时针,1:逆时针
|
||
/// </summary>
|
||
// ushort dir = 0;
|
||
//int cic = 0; //圆弧圈数
|
||
double[] cen = new double[2]; //定义圆心坐标
|
||
double[] Dist1 = new double[2]; //定义终点坐标
|
||
/// <summary>
|
||
/// 定义连续插补使能参数,0 连续,1 前瞻运动
|
||
/// </summary>
|
||
ushort mode = 1;
|
||
/// <summary>
|
||
/// 定义插补段数:200 段
|
||
/// </summary>
|
||
int LookaheadSegment = 200;
|
||
/// <summary>
|
||
/// 定义轨迹误差:1unit
|
||
/// </summary>
|
||
double PathError = 0.1;
|
||
/// <summary>
|
||
/// 定义拐弯加速度:10000unit/s2
|
||
/// </summary>
|
||
double LookaheadAcc = 10000;
|
||
/// <summary>
|
||
/// 使能圆弧限速,0:不使用,1 使能
|
||
/// </summary>
|
||
ushort ArcLimit = 1;
|
||
|
||
#endregion
|
||
|
||
/// <summary>
|
||
/// 轨迹运行中
|
||
/// </summary>
|
||
public bool TrajectoryRuning { get; set; }
|
||
|
||
/// <summary>
|
||
/// 运行轨迹初始化
|
||
/// </summary>
|
||
public bool TrajectotyInitialization(ObservableCollection<FeiYuShaft> AllShaftList, int multiple = 1)
|
||
{
|
||
|
||
//点位验证及初始化
|
||
if (!PointVerification(AllShaftList, multiple))
|
||
{
|
||
//MyMessageBox.Show(TraName + "点位初始化失败!");
|
||
return false;
|
||
}
|
||
|
||
try
|
||
{
|
||
MotionPointModel mpm = PointList[0];
|
||
switch (TrajectoryTrype)
|
||
{
|
||
case 1://直线
|
||
CardNo = mpm.Shaft_X.MyCard.CardID;
|
||
AxisArray[0] = mpm.Shaft_X.Axis;
|
||
AxisArray[1] = mpm.Shaft_Y.Axis;
|
||
Crd = (ushort)ClassType;//坐标系分配
|
||
Min_Vel = Math.Round((mpm.Shaft_X.GengrelMp.StartSpeed + mpm.Shaft_Y.GengrelMp.StartSpeed) / 2, 2); //获取起始速度
|
||
Max_Vel = Math.Round((mpm.Shaft_X.GengrelMp.RunSpeed + mpm.Shaft_Y.GengrelMp.RunSpeed) / 2, 2);//获取运行速度
|
||
Stop_Vel = 0;
|
||
Tacc = 0.1;
|
||
Tdec = 0.1;
|
||
LookaheadAcc = Math.Round(Max_Vel / 3, 2);
|
||
//第一步、设置插补运动速度参数、S时间参数
|
||
ret = LTSMC.smc_set_vector_profile_unit(CardNo, Crd, Min_Vel, Max_Vel, Tacc, Tdec, Stop_Vel);
|
||
ret = LTSMC.smc_set_vector_s_profile(CardNo, Crd, Smode, Spara);
|
||
|
||
//第二步、设置前瞻参数
|
||
ret = LTSMC.smc_conti_set_lookahead_mode(CardNo, Crd, mode, LookaheadSegment, PathError, LookaheadAcc);
|
||
//第三步、打开连续插补
|
||
ret = LTSMC.smc_conti_open_list(CardNo, Crd, AxisNum, AxisArray);
|
||
|
||
//第四步、添加直线插补段
|
||
foreach (MotionPointModel mp in PointList)
|
||
{
|
||
ret = LTSMC.smc_conti_line_unit(CardNo, Crd, AxisNum, AxisArray, new double[] { mp.ShaftPoint_X, mp.ShaftPoint_Y }, PositionModel, 0);
|
||
}
|
||
//第五步、关闭连续插补缓冲区
|
||
ret = LTSMC.smc_conti_close_list(CardNo, Crd);
|
||
|
||
break;
|
||
case 2: //圆弧
|
||
//MotionPointModel mpm = PointList[0];
|
||
CardNo = mpm.Shaft_X.MyCard.CardID;
|
||
AxisArray[0] = mpm.Shaft_X.Axis;
|
||
AxisArray[1] = mpm.Shaft_Y.Axis;
|
||
Crd = (ushort)ClassType;//坐标系分配
|
||
Min_Vel = Math.Round((mpm.Shaft_X.GengrelMp.StartSpeed + mpm.Shaft_Y.GengrelMp.StartSpeed) / 2, 2); //获取起始速度
|
||
Max_Vel = Math.Round((mpm.Shaft_X.GengrelMp.RunSpeed + mpm.Shaft_Y.GengrelMp.RunSpeed) / 2, 2);//获取运行速度
|
||
Stop_Vel = 0;
|
||
Tacc = 0.1;
|
||
Tdec = 0.1;
|
||
LookaheadAcc = Math.Round(Max_Vel / 3, 2);
|
||
//第一步、设置插补运动速度参数、S时间参数
|
||
ret = LTSMC.smc_set_vector_profile_unit(CardNo, Crd, Min_Vel, Max_Vel, Tacc, Tdec, Stop_Vel);
|
||
ret = LTSMC.smc_set_vector_s_profile(CardNo, Crd, Smode, Spara);
|
||
//第二步、设置圆弧限速功能使能
|
||
ret = LTSMC.smc_set_arc_limit(CardNo, Crd, ArcLimit, 0, 0);
|
||
//第三步、设置前瞻参数
|
||
ret = LTSMC.smc_conti_set_lookahead_mode(CardNo, Crd, mode, LookaheadSegment, PathError, LookaheadAcc);
|
||
//第三步、打开连续插补
|
||
ret = LTSMC.smc_conti_open_list(CardNo, Crd, AxisNum, AxisArray);
|
||
|
||
//第四步、添加圆弧插补段(3点圆弧)
|
||
double[] Target_Pos = new double[] { PointList[2].ShaftPoint_X, PointList[2].ShaftPoint_Y };//圆弧目标点位置
|
||
double[] Mid_Pos = new double[] { PointList[1].ShaftPoint_X, PointList[1].ShaftPoint_Y };//圆弧中间点位置
|
||
ret = LTSMC.smc_conti_arc_move_3points_unit(CardNo, Crd, AxisNum, AxisArray, Target_Pos, Mid_Pos, 0, PositionModel, 0);
|
||
//第五步、关闭连续插补缓冲区
|
||
ret = LTSMC.smc_conti_close_list(CardNo, Crd);
|
||
break;
|
||
case 3: //椭圆
|
||
CardNo = PointList[0].Shaft_X.MyCard.CardID;
|
||
Crd = (ushort)ClassType;
|
||
break;
|
||
}
|
||
return true;
|
||
}
|
||
catch (Exception)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 轨迹运行步骤
|
||
/// </summary>
|
||
public GengralProcessEnum TrajectoryRunStep { get; set; }
|
||
/// <summary>
|
||
/// 轨迹运行时间
|
||
/// </summary>
|
||
DateTime TrajectoryRunTime { get; set; }
|
||
|
||
/// <summary>
|
||
/// 轨迹运行
|
||
/// </summary>
|
||
/// <param name="AllShaftList"></param>
|
||
/// <param name="multiple"></param>
|
||
/// <returns></returns>
|
||
public bool TrajectoryRun(ObservableCollection<FeiYuShaft> AllShaftList, int multiple = 1)
|
||
{
|
||
bool bl = false;
|
||
switch (TrajectoryRunStep)
|
||
{
|
||
case GengralProcessEnum.Start:
|
||
if (TrajectoryTrype > 0)
|
||
{
|
||
//先运行到整个轨迹的起始点位
|
||
if (PointList[0].PointRun(AllShaftList))
|
||
{
|
||
TrajectoryRunTime = DateTime.Now;
|
||
TrajectoryRunStep++;
|
||
|
||
}
|
||
}
|
||
else
|
||
{
|
||
|
||
}
|
||
break;
|
||
case GengralProcessEnum.Step1:
|
||
if (TrajectotyInitialization(AllShaftList, multiple))
|
||
{
|
||
//开始连续插补
|
||
ret = LTSMC.smc_conti_start_list(CardNo, Crd);
|
||
TrajectoryRunStep++;
|
||
}
|
||
else
|
||
{
|
||
bl = false;
|
||
TrajectoryRunStep = GengralProcessEnum.Start;
|
||
}
|
||
break;
|
||
case GengralProcessEnum.Step2:
|
||
if (TrajectoryTrype > 0)
|
||
{
|
||
//获取坐标系的运行状态 0:运动中,1:暂停中,2:正常停止,3:未启动,4:空闲,5:异常减速停止中
|
||
short state = LTSMC.smc_conti_get_run_state(CardNo, Crd);
|
||
if (state == 1)//暂停
|
||
{
|
||
LTSMC.smc_conti_start_list(CardNo, Crd);
|
||
}
|
||
else if (state == 2 || state == 4)
|
||
{
|
||
bl = true;
|
||
TrajectoryRunStep = GengralProcessEnum.Start;
|
||
}
|
||
else if (state == 5)
|
||
{
|
||
|
||
// MyMessageBox.Show("轨迹运行异常");
|
||
//bl = true;
|
||
//TrajectoryRunStep = GengralProcessEnum.Start;
|
||
}
|
||
}
|
||
break;
|
||
}
|
||
|
||
return bl;
|
||
}
|
||
/// <summary>
|
||
/// 点位验证 (轴必须在同一个控制器上)
|
||
/// </summary>
|
||
/// <param name="AllShaftList">所有轴集合</param>
|
||
/// <param name="multiple">运行降速倍数</param>
|
||
private bool PointVerification(ObservableCollection<FeiYuShaft> AllShaftList, int multiple = 1)
|
||
{
|
||
bool bl = true;
|
||
if (PointList == null && PointList.Count <= 0)
|
||
{
|
||
return bl;
|
||
}
|
||
|
||
int a = -1;
|
||
foreach (MotionPointModel mp in PointList)
|
||
{
|
||
mp.GetInterpolationPer(AllShaftList, multiple);
|
||
if (TrajectoryTrype > 0)
|
||
{
|
||
if (!mp.SameController)
|
||
{
|
||
bl = false;
|
||
}
|
||
else
|
||
{
|
||
if (a < 0)
|
||
{
|
||
a = mp.Shaft_X.MyCard.ID;
|
||
}
|
||
else if (a != mp.Shaft_X.MyCard.ID)
|
||
{
|
||
bl = false;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return bl;
|
||
}
|
||
|
||
}
|
||
}
|