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

268 lines
8.6 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.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Model.MotionControl
{
/// <summary>
/// 运行轨迹集群
/// </summary>
public class TrajectoryCluster
{
public TrajectoryCluster(List<MotionTrajectoryModel> mtmlist)
{
TrajectoryList = mtmlist;
}
#region 运行轨迹集合
/// <summary>
/// 运行轨迹集合
/// </summary>
public List<MotionTrajectoryModel> TrajectoryList { get; set; }
#endregion
/// <summary>
/// 轨迹集群运行中
/// </summary>
public bool TrajectoryClusterRuning { get; set; }
#region 是否抬刀
private bool liftKnife;
/// <summary>
/// 是否抬刀
/// </summary>
public bool LiftKnife
{
get { return liftKnife; }
set
{
liftKnife = value;
}
}
#endregion
#region 抬刀高度
private int knifeHeight;
/// <summary>
/// 抬刀高度
/// </summary>
public int KnifeHeight
{
get { return knifeHeight; }
set
{
knifeHeight = value;
}
}
#endregion
#region 轨迹集合的连续插补运动
#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>
/// <param name="pointlist">需要插补运行的点位集合</param>
private bool InterpolationSet()
{
try
{
MotionPointModel mpm = TrajectoryList[0].PointList[0];
CardNo = mpm.Shaft_X.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;
}
}
/// <summary>
/// 轨迹群运行步骤
/// </summary>
GengralProcessEnum TrajectoryStep { get; set; }
/// <summary>
/// 轨迹群运行
/// </summary>
/// <param name="Trajectorylist"></param>
/// <returns></returns>
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
}
}