using Community;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Model.MotionControl
{
///
/// 运行轨迹集群
///
public class TrajectoryCluster
{
public TrajectoryCluster(List mtmlist)
{
TrajectoryList = mtmlist;
}
#region 运行轨迹集合
///
/// 运行轨迹集合
///
public List TrajectoryList { get; set; }
#endregion
///
/// 轨迹集群运行中
///
public bool TrajectoryClusterRuning { get; set; }
#region 是否抬刀
private bool liftKnife;
///
/// 是否抬刀
///
public bool LiftKnife
{
get { return liftKnife; }
set
{
liftKnife = value;
}
}
#endregion
#region 抬刀高度
private int knifeHeight;
///
/// 抬刀高度
///
public int KnifeHeight
{
get { return knifeHeight; }
set
{
knifeHeight = value;
}
}
#endregion
#region 轨迹集合的连续插补运动
#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
///
/// 连续插补初始化
///
/// 需要插补运行的点位集合
private bool InterpolationSet()
{
try
{
MotionPointModel mpm = TrajectoryList[0].PointList[0];
CardNo = mpm.Shaft_X.MyCard.CardID;
AxisArray[0] = mpm.Shaft_X.Axis;
AxisArray[1] = mpm.Shaft_Y.Axis;
Crd = (ushort)TrajectoryList[0].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);
foreach (MotionTrajectoryModel mt in TrajectoryList)
{
//如果是直线轨迹
if (mt.TrajectoryTrype == 1)
{
//第四步、添加直线插补段
foreach (MotionPointModel mp in mt.PointList)
{
ret = LTSMC.smc_conti_line_unit(CardNo, Crd, AxisNum, AxisArray, new double[] { mp.ShaftPoint_X, mp.ShaftPoint_Y }, PositionModel, 0);
}
}
else if (mt.TrajectoryTrype == 2)
{
//第四步、添加圆弧插补段(3点圆弧)
double[] Target_Pos = new double[] { mt.PointList[2].ShaftPoint_X, mt.PointList[2].ShaftPoint_Y };//圆弧目标点位置
double[] Mid_Pos = new double[] { mt.PointList[1].ShaftPoint_X, mt.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);
return true;
}
catch (Exception)
{
return false;
}
}
///
/// 轨迹群运行步骤
///
GengralProcessEnum TrajectoryStep { get; set; }
///
/// 轨迹群运行
///
///
///
public bool TrajectoryListRun()
{
bool bl = false;
switch (TrajectoryStep)
{
case GengralProcessEnum.Start:
if (InterpolationSet())
{
//开始连续插补
ret = LTSMC.smc_conti_start_list(CardNo, Crd);
TrajectoryStep++;
}
break;
case GengralProcessEnum.Step1:
//获取坐标系的运行状态 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;
TrajectoryStep = GengralProcessEnum.Start;
}else if(state==5)
{
}
break;
}
return bl;
}
#endregion
}
}