liteon/Model/MotionControl/LeiSaiShaft.cs
2025-07-12 14:05:35 +08:00

433 lines
16 KiB
C#
Raw 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.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Model.MotionControl
{
public class LeiSaiShaft : FeiYuShaft
{
#region 初始化当前轴信息
/// <summary>
/// 初始化当前轴信息
/// </summary>
public override void Initialize()
{
if (MyCard.InterfaceType == 2)
{
ServoInOut();
LTSMC.smc_set_pulse_outmode(MyCard.CardID, Axis, PulseOutMode);//设置脉冲输出模式
LTSMC.smc_set_home_pin_logic(MyCard.CardID, Axis, MyHomePar.Logic, 0); //设置回原有效电平
LTSMC.smc_set_alm_mode(MyCard.CardID, Axis, Alm_Enable, Alm_Logic, 0);//设置指定的ALM信号
LTSMC.smc_set_emg_mode(MyCard.CardID, Axis, Emg_Enable, Emg_Logic); //设置EMG急停信号
LTSMC.smc_set_el_mode(MyCard.CardID, Axis, Hard_Enable, Hard_Logic, 0); //设置硬件限位信号
LTSMC.smc_set_equiv(MyCard.CardID, Axis, this.ConValue); //设置脉冲解析度
LTSMC.smc_set_home_position_unit(MyCard.CardID, Axis, MyHomePar.ResetMode, 0);//原点完成后重置坐标方法(0:不重置/1:先重置再定位/2:定位后重置)
LTSMC.smc_set_inp_mode(MyCard.CardID, Axis, Inp_Enable, 0); //设置输入信号模式
LTSMC.smc_set_softlimit_unit(MyCard.CardID, Axis, Soft_Enable, 0, Soft_Mode, 0, 0); //设置软件限位
GengrelMp = Coper(MyMotionPar);
}
else if (MyCard.InterfaceType == 0)
{
ServoInOut();
LTDMC.dmc_set_pulse_outmode(MyCard.CardID, Axis, PulseOutMode); //设置脉冲输出模式
LTDMC.dmc_set_home_pin_logic(MyCard.CardID, Axis, Home_Logic, 0); //设置回原有效电平
LTDMC.dmc_set_el_mode(MyCard.CardID, Axis, Hard_Enable, Hard_Logic, 0); // 设置硬件限位信号
LTDMC.dmc_set_emg_mode(MyCard.CardID, Axis, Emg_Enable, Emg_Logic); // //设置EMG急停信号
LTDMC.dmc_set_alm_mode(MyCard.CardID, Axis, Alm_Enable, Alm_Logic, 0); //设置指定的ALM信号
GengrelMp = Coper(MyMotionPar);
}
}
#endregion
#region 回原点运动 void HYD()
/// <summary>
/// 回原点运动
/// </summary>
/// <param name="sp">轴运动参数</param>
public override void HYD(InputSignal inEmg)
{
if (!org_Successful)
{
return;
}
else
if (inEmg.InputColor == ColorStr.Green)
{
ShaftStop();
return;
}
switch (HomeStep)
{
case GengralProcessEnum.Start:
if (this.ShaftState.StatusBackground == ColorStr.Red)
{
if (MyCard.InterfaceType == 2)
{
LTSMC.smc_set_home_profile_unit(MyCard.CardID, Axis, MyHomePar.StartSpeed, MyHomePar.RunSpeed, MyHomePar.AccTime, MyHomePar.DecTime);//设置起始速度、运行速度、停止速度、加速时间、减速时间
LTSMC.smc_set_homemode(MyCard.CardID, this.Axis, MyHomePar.Direction, MyHomePar.SpeedMode, MyHomePar.Mode, 0);//设置回零模式
LTSMC.smc_home_move(MyCard.CardID, this.Axis);//启动回原点
}
else if (MyCard.InterfaceType == 0)
{
LTDMC.dmc_set_profile(MyCard.CardID, this.Axis, MyHomePar.StartSpeed, MyHomePar.RunSpeed, MyHomePar.AccTime, MyHomePar.DecTime, MyHomePar.StopSpeed);//设置起始速度、运行速度、停止速度、加速时间、减速时间
LTDMC.dmc_set_homemode(MyCard.CardID, this.Axis, MyHomePar.Direction, MyHomePar.SpeedMode, MyHomePar.Mode, 0);//设置回零模式
LTDMC.dmc_home_move(MyCard.CardID, this.Axis);//启动回原点
}
HomeStep++;
}
break;
case GengralProcessEnum.Step1:
if (MyCard.InterfaceType == 2)
{
ushort state = 0;
LTSMC.smc_get_home_result(MyCard.CardID, this.Axis, ref state);
if (state == 1)
{
this.HomeTime = DateTime.Now; //记录当前回原点的开始时间
HomeStep++;
}
}
else if (MyCard.InterfaceType == 0)
{
if (this.ss.org == 1 && this.ShaftState.StatusBackground == ColorStr.Red)
{
this.HomeTime = DateTime.Now; //记录当前回原点的开始时间
HomeStep++;
}
}
break;
case GengralProcessEnum.Step2:
if (HomeTime.AddMilliseconds(500) < DateTime.Now)
{
this.GoZero(); //掉用归零方法
HomeStep++;
}
break;
case GengralProcessEnum.Step3:
if (this.ActualDir == 0)
{
HomeStep = GengralProcessEnum.Start;
org_Successful = false;
}
else
{
HomeStep--;
}
break;
}
}
#endregion
#region 单轴定长运动 void DZDC(SingleShaftPer sper)
/// <summary>
/// 单轴定长运动
/// </summary>
/// <param name="sper">运动参数</param>
public override void DZDC(MovementPer ssmp)
{
//如果处于运行中,则跳出
if (this.ShaftState.StatusBackground == ColorStr.Green)
{
return;
}
if (MyCard.InterfaceType == 2)
{
LTSMC.smc_set_profile_unit(MyCard.CardID, Axis, ssmp.StartSpeed, ssmp.RunSpeed, ssmp.AccTime, ssmp.DecTime, ssmp.StopSpeed); //设置起始速度、运行速度、停止速度、加速时间、减速时间
LTSMC.smc_pmove_unit(MyCard.CardID, Axis, ssmp.ActualDir, 1); //定长运动 1:绝对坐标模式
}
else if (MyCard.InterfaceType == 0)
{
LTDMC.dmc_set_profile(MyCard.CardID, Axis, ssmp.StartSpeed, ssmp.RunSpeed, ssmp.AccTime, ssmp.DecTime, ssmp.StopSpeed); //设置起始速度、运行速度、停止速度、加速时间、减速时间
LTDMC.dmc_pmove(MyCard.CardID, Axis, Convert.ToInt32(ssmp.ActualDir * ConValue), 1); //定长运动 1:绝对坐标模式
}
}
#endregion
#region 1、获取轴运动状态
/// <summary>
/// 获取轴运动状态
/// </summary>
public override void GetShaftStart()
{
try
{
short wh = 0;
if (MyCard.InterfaceType == 2)
{
wh = LTSMC.smc_check_done(MyCard.CardID, this.Axis);
}
else if (MyCard.InterfaceType == 0)
{
wh = LTDMC.dmc_check_done(MyCard.CardID, this.Axis);
}
if (wh == 0)
{
this.ShaftState.StatusBackground = ColorStr.Green; //运行中
this.ShaftState.StatuInfo = "运行中";
this.ShaftState.StatusForeground = ColorStr.White;
}
else
{
this.ShaftState.StatusBackground = ColorStr.Red; //停止中
this.ShaftState.StatuInfo = "停止中";
this.ShaftState.StatusForeground = ColorStr.White;
}
}
catch (Exception)
{
this.ShaftState.StatusBackground = ColorStr.Yellow; //停止中
this.ShaftState.StatuInfo = "轴运行状态异常";
this.ShaftState.StatusForeground = ColorStr.White;
}
}
#endregion
#region 2、专用信号状态
/// <summary>
/// 专用信号状态
/// </summary>
public override void SpecialStart()
{
lock (this)
{
try
{
string bit = "";
if (MyCard.InterfaceType == 2)
{
bit = Convert.ToInt64(Convert.ToString(LTSMC.smc_axis_io_status(MyCard.CardID, this.Axis), 2)).ToString("0000000000000000");
}
else if (MyCard.InterfaceType == 0)
{
bit = Convert.ToInt32(Convert.ToString(LTDMC.dmc_axis_io_status(MyCard.CardID, this.Axis), 2)).ToString("0000000000000000");
}
this.ss.alm = int.Parse(bit.Substring(15, 1));
this.ss.elN = int.Parse(bit.Substring(14, 1));
this.ss.elS = int.Parse(bit.Substring(13, 1));
this.ss.org = int.Parse(bit.Substring(11, 1));
if (this.ss.alm == 1)
{
this.ShaftState.StatusBackground = ColorStr.Yellow;
this.ShaftState.StatuInfo = "ALM异常";
this.ShaftState.StatusForeground = ColorStr.Black;
}
else
if (this.ss.elN == 1)
{
this.ShaftState.StatusBackground = ColorStr.Yellow;
this.ShaftState.StatuInfo = "正极限";
this.ShaftState.StatusForeground = ColorStr.Black;
}
else
if (this.ss.elS == 1)
{
this.ShaftState.StatusBackground = ColorStr.Yellow;
this.ShaftState.StatuInfo = "负极限";
this.ShaftState.StatusForeground = ColorStr.Black;
}
else
if (this.ss.org == 1)
{
this.ShaftState.StatusBackground = ColorStr.Red;
this.ShaftState.StatuInfo = "原点";
this.ShaftState.StatusForeground = ColorStr.Black;
}
else //获取运动状态
{
GetShaftStart();
}
}
catch (Exception)
{
this.ShaftState.StatusBackground = ColorStr.Yellow;
this.ShaftState.StatuInfo = "获取专用信号失败";
this.ShaftState.StatusForeground = ColorStr.Black;
}
}
}
#endregion
#region 3、获取实际位移
/// <summary>
/// 获取脉冲位移 并计算出实际移动距离
/// </summary>
public override void GetPluseDis()
{
try
{
if (MyCard.InterfaceType == 2)
{
double temp = 0;
LTSMC.smc_get_position_unit(MyCard.CardID, this.Axis, ref temp);
this.ActualDir = Math.Round(temp, 2);
}
else if (MyCard.InterfaceType == 0)
{
PulseDir = LTDMC.dmc_get_position(MyCard.CardID, this.Axis);
this.ActualDir = Math.Round((double)PulseDir / this.ConValue, 2);
}
}
catch (Exception)
{
this.ShaftState.StatusBackground = ColorStr.Yellow;
this.ShaftState.StatuInfo = "获取实时位移失败";
this.ShaftState.StatusForeground = ColorStr.Black;
}
}
#endregion
#region 4、开启或关闭使能
/// <summary>
/// 开启或关闭使能
/// </summary>
public override void ServoInOut()
{
try
{
if (IsServo)
{
if (MyCard.InterfaceType == 2)
{
ServoSwitch = LTSMC.smc_write_sevon_pin(MyCard.CardID, Axis, 0);
}
else if (MyCard.InterfaceType == 0)
{
ServoSwitch = LTDMC.dmc_write_sevon_pin(MyCard.CardID, Axis, 0);
}
}
}
catch (Exception)
{
this.ShaftState.StatusBackground = ColorStr.Yellow;
this.ShaftState.StatuInfo = "开启使能失败";
this.ShaftState.StatusForeground = ColorStr.Black;
}
}
#endregion
#region 5、对应轴归零
/// <summary>
/// 对应轴归零
/// </summary>
public override void GoZero()
{
try
{
if (MyCard.InterfaceType == 2)
{
LTSMC.smc_set_position_unit(MyCard.CardID, Axis, 0);//位置清零
}
else if (MyCard.InterfaceType == 0)
{
LTDMC.dmc_set_position(MyCard.CardID, Axis, 0);//位置清零
}
}
catch (Exception)
{
this.ShaftState.StatusBackground = ColorStr.Yellow;
this.ShaftState.StatuInfo = "位置清零失败";
this.ShaftState.StatusForeground = ColorStr.Black;
}
}
#endregion
#region 获取轴的所有状态(轴运动状态,专用信号状态,脉冲位移,使能开关) +GetAllStart()
/// <summary>
/// 获取轴的所有状态(轴运动状态,专用信号状态,脉冲位移,使能开关)
/// </summary>
public override void GetAllStart()
{
SpecialStart();
if (this.ToOrgin)//如果原点都不用回 那也没必要监控位移
{
GetPluseDis();
}
}
#endregion
#region 连续运动
/// <summary>
/// 连续运动
/// </summary>
/// <param name="dir">方向参数</param>
public override void ContinuousMove(ushort dir)
{
//如果处于运行中,则跳出
if (this.ShaftState.StatusBackground == ColorStr.Green)
{
return;
}
if (MyCard.InterfaceType == 2)
{
//连续运动
LTSMC.smc_set_profile_unit(MyCard.CardID, this.Axis, MyHomePar.StartSpeed, MyHomePar.RunSpeed, MyHomePar.AccTime, MyHomePar.DecTime, MyHomePar.StopSpeed);//设置起始速度、运行速度、停止速度、加速时间、减速时间
LTSMC.smc_vmove(MyCard.CardID, this.Axis, dir);//连续运动
}
else if (MyCard.InterfaceType == 0)
{
//连续运动
LTDMC.dmc_set_profile(MyCard.CardID, this.Axis, MyHomePar.StartSpeed, MyHomePar.RunSpeed, MyHomePar.AccTime, MyHomePar.DecTime, MyHomePar.StopSpeed);//设置起始速度、运行速度、停止速度、加速时间、减速时间
LTDMC.dmc_vmove(MyCard.CardID, this.Axis, dir);//连续运动
}
}
#endregion
#region 停止运行
/// <summary>
/// 停止运行
/// </summary>
public override void ShaftStop()
{
if (MyCard!=null)
{
if (MyCard.InterfaceType == 2)
{
LTSMC.smc_stop(MyCard.CardID, this.Axis, 1);
}
else if (MyCard.InterfaceType == 0)
{
LTDMC.dmc_stop(MyCard.CardID, this.Axis, 1);
}
}
}
#endregion
}
}