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
{
///
/// 运行轨迹模型
///
public class MotionTrajectoryModel : NotificationObject
{
public int ID { get; set; }
///
/// 序号
///
public int SerialNumber { get; set; }
///
/// 上级ID
///
public int SuperiorID { get; set; }
#region 轨迹名称
private string traName;
///
/// 轨迹名称
///
public string TraName
{
get { return traName; }
set
{
traName = value;
this.RaisePropertyChanged("TraName");
}
}
#endregion
#region 分类(0、左运行轨迹 1、右运行轨迹)
private int classType;
///
/// 分类(0、左运行轨迹 1、右运行轨迹)
///
public int ClassType
{
get { return classType; }
set
{
classType = value;
this.RaisePropertyChanged("ClassType");
}
}
#endregion
#region 轨迹类型 0、单点 1、直线 2、圆弧 3、椭圆
private int trajectoryTrype;
///
/// 轨迹类型 0、单点 1、直线 2、圆弧 3、椭圆
///
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;
///
/// 轨迹类型名称
///
public string TrajectoryTrypeName
{
get { return trajectoryTrypeName; }
set
{
trajectoryTrypeName = value;
this.RaisePropertyChanged("TrajectoryTrypeName");
}
}
#endregion
#region 是否抬刀
private bool liftKnife;
///
/// 是否抬刀
///
public bool LiftKnife
{
get { return liftKnife; }
set
{
liftKnife = value;
this.RaisePropertyChanged("LiftKnife");
}
}
#endregion
#region 抬刀高度
private int knifeHeight;
///
/// 抬刀高度
///
public int KnifeHeight
{
get { return knifeHeight; }
set
{
knifeHeight = value;
this.RaisePropertyChanged("KnifeHeight");
}
}
#endregion
#region 点位集合
private ObservableCollection pointList;
///
/// 点位集合
///
public ObservableCollection PointList
{
get { return pointList; }
set
{
pointList = value;
this.RaisePropertyChanged("PointList");
}
}
#endregion
#region 插补参数
///
/// 控制器连接好
///
ushort CardNo { get; set; }
short ret; //返回错误码
///
/// 位置模式 0:相对模式,1:绝对模式
///
ushort PositionModel = 1;
///
/// 参与插补运动的坐标系
///
ushort Crd { get; set; }
///
/// 插补运动轴数为2
///
ushort AxisNum = 2;
///
/// 轴号数组
///
ushort[] AxisArray = new ushort[2];
///
/// 插补起始速度
///
double Min_Vel { get; set; }
///
/// 插补运动最大矢量速度3000unit/s
///
double Max_Vel { get; set; }
///
/// 插补停止速度
///
double Stop_Vel { get; set; }
///
/// 插补运动加速时间0.2s
///
double Tacc { get; set; }
///
/// 插补运动减速时间0.1s
///
double Tdec { get; set; }
///
/// 保留参数,固定值为0
///
ushort Smode = 0;
///
/// 平滑时间为0.05s
///
double Spara = 0.05;
///
/// 是否启用Blend 功能,0 不使用,1 使用
///
//ushort Blend = 1;
///
/// 终点坐标数组
///
double[] EndArray = new double[2]; //定义终点坐标
///
/// //圆弧方向,0:顺时针,1:逆时针
///
// ushort dir = 0;
//int cic = 0; //圆弧圈数
double[] cen = new double[2]; //定义圆心坐标
double[] Dist1 = new double[2]; //定义终点坐标
///
/// 定义连续插补使能参数,0 连续,1 前瞻运动
///
ushort mode = 1;
///
/// 定义插补段数:200 段
///
int LookaheadSegment = 200;
///
/// 定义轨迹误差:1unit
///
double PathError = 0.1;
///
/// 定义拐弯加速度:10000unit/s2
///
double LookaheadAcc = 10000;
///
/// 使能圆弧限速,0:不使用,1 使能
///
ushort ArcLimit = 1;
#endregion
///
/// 轨迹运行中
///
public bool TrajectoryRuning { get; set; }
///
/// 运行轨迹初始化
///
public bool TrajectotyInitialization(ObservableCollection 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;
}
}
///
/// 轨迹运行步骤
///
public GengralProcessEnum TrajectoryRunStep { get; set; }
///
/// 轨迹运行时间
///
DateTime TrajectoryRunTime { get; set; }
///
/// 轨迹运行
///
///
///
///
public bool TrajectoryRun(ObservableCollection 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;
}
///
/// 点位验证 (轴必须在同一个控制器上)
///
/// 所有轴集合
/// 运行降速倍数
private bool PointVerification(ObservableCollection 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;
}
}
}