using Community;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Model.MotionControl
{
///
/// 众为兴轴
///
public class ZmotionShaft : FeiYuShaft
{
#region 初始化当前轴信息
///
/// 初始化当前轴信息
///
public override void Initialize()
{
if (MyCard.InterfaceType == 2)
{
zmcaux.ZAux_Direct_SetUnits(MyCard.g_handle, Axis, (float)ConValue);
zmcaux.ZAux_Direct_SetDatumIn(MyCard.g_handle, Axis, SpecialIO.Org_IO);//原点in(0)
zmcaux.ZAux_Direct_SetAlmIn(MyCard.g_handle, Axis, SpecialIO.Alm_IO);//伺服报警in(1)
zmcaux.ZAux_Direct_SetFwdIn(MyCard.g_handle, Axis, SpecialIO.ElN_IO);//正限位in(2)
zmcaux.ZAux_Direct_SetRevIn(MyCard.g_handle, Axis, SpecialIO.ElS_IO); //负限位in(3)
zmcaux.ZAux_Direct_SetInvertIn(MyCard.g_handle, SpecialIO.Alm_IO, Alm_Logic);//高电平有效
switch (Hard_Logic)
{
case 0:
zmcaux.ZAux_Direct_SetInvertIn(MyCard.g_handle, SpecialIO.ElN_IO, 0);
zmcaux.ZAux_Direct_SetInvertIn(MyCard.g_handle, SpecialIO.ElS_IO, 0);
break;
case 1:
zmcaux.ZAux_Direct_SetInvertIn(MyCard.g_handle, SpecialIO.ElN_IO, 1);
zmcaux.ZAux_Direct_SetInvertIn(MyCard.g_handle, SpecialIO.ElS_IO, 1);
break;
case 2:
zmcaux.ZAux_Direct_SetInvertIn(MyCard.g_handle, SpecialIO.ElN_IO, 0);
zmcaux.ZAux_Direct_SetInvertIn(MyCard.g_handle, SpecialIO.ElS_IO, 1);
break;
case 3:
zmcaux.ZAux_Direct_SetInvertIn(MyCard.g_handle, SpecialIO.ElN_IO, 1);
zmcaux.ZAux_Direct_SetInvertIn(MyCard.g_handle, SpecialIO.ElS_IO, 0);
break;
}
zmcaux.ZAux_Direct_SetInvertIn(MyCard.g_handle, SpecialIO.Org_IO, MyHomePar.Logic);
//设置轴参数
zmcaux.ZAux_Direct_SetAtype(MyCard.g_handle, Axis, 7);
ServoInOut();
zmcaux.ZAux_Direct_SetParam(MyCard.g_handle, "INVERT_STEP", Axis, PulseOutMode);//脉冲输入模式
}
else
{
ADTMC.adt_set_pulse_equiv(MyCard.ID, Axis, ConValue);//设置脉冲当量
ADTMC.adt_set_pulse_mode(MyCard.ID, Axis, 1, 0, 0); //设置脉冲模式
ADTMC.adt_set_limit_mode(MyCard.ID, Axis, 1, 1, 0); //设置硬限位模式
ADTMC.adt_set_stop0_mode(MyCard.ID, Axis, 0, 0, 1);//设置原点信号模式
ADTMC.adt_set_stop1_mode(MyCard.ID, Axis, 0, 0, 1);//Z信号模式
ServoInOut();
}
}
#endregion
#region 回原点运动 void HYD()
///
/// 回原点运动
///
/// 轴运动参数
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)
{
//设置轴参数
zmcaux.ZAux_Direct_SetLspeed(MyCard.g_handle, Axis, (float)MyHomePar.StartSpeed);
zmcaux.ZAux_Direct_SetSpeed(MyCard.g_handle, Axis, (float)MyHomePar.RunSpeed);
zmcaux.ZAux_Direct_SetAccel(MyCard.g_handle, Axis, (float)MyHomePar.AccTime);
zmcaux.ZAux_Direct_SetDecel(MyCard.g_handle, Axis, (float)MyHomePar.DecTime);
zmcaux.ZAux_Direct_SetCreep(MyCard.g_handle, Axis, (float)MyHomePar.RunSpeed / 10);
zmcaux.ZAux_Direct_SetSramp(MyCard.g_handle, Axis, (float)MyHomePar.Sramp);
zmcaux.ZAux_Direct_Single_Datum(MyCard.g_handle, Axis, (int)MyHomePar.Mode);
}
else
{
Initialize();
ADTMC.HOMEPARAM homepar = new ADTMC.HOMEPARAM();
homepar.stop0_level = 0;
homepar.stop1_level = 0;
homepar.home_mode = MyHomePar.Direction == 0 ? ADTMC.HOMERMODE.SHMode28 : ADTMC.HOMERMODE.SHMode24;
homepar.vel_high = MyHomePar.RunSpeed;
homepar.vel_low = MyHomePar.StartSpeed;
homepar.org_offset = 0;
//ADTMC.adt_set_acc(MyCard.CardID, Axis, MyHomePar.AccTime);//将回零低速设为加速度
ADTMC.adt_set_acc(MyCard.CardID, Axis, MyHomePar.StartSpeed);//将回零低速设为加速度
ADTMC.adt_set_std_home_mode(MyCard.CardID, Axis, ref homepar);//设置回原点参数
ADTMC.adt_set_std_home_process(MyCard.CardID, Axis);//启动回原点
}
HomeStep++;
}
break;
case GengralProcessEnum.Step1:
if (MyCard.InterfaceType == 2)
{
uint state = 0;
zmcaux.ZAux_Direct_GetHomeStatus(MyCard.g_handle, this.Axis, ref state);
if (state == 1)
{
this.HomeTime = DateTime.Now; //记录当前回原点的开始时间
HomeStep++;
}
}
else
{
int res = ADTMC.adt_get_std_home_status(MyCard.CardID, Axis);
if (res == 0)
{
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)
///
/// 单轴定长运动
///
/// 运动参数
public override void DZDC(MovementPer ssmp)
{
//如果处于运行中,则跳出
if (this.ShaftState.StatusBackground == ColorStr.Green)
{
return;
}
if (MyCard.InterfaceType == 2)
{
zmcaux.ZAux_Direct_SetUnits(MyCard.g_handle, Axis, (float)ConValue);
zmcaux.ZAux_Direct_SetLspeed(MyCard.g_handle, Axis, (float)ssmp.StartSpeed);
zmcaux.ZAux_Direct_SetSpeed(MyCard.g_handle, Axis, (float)ssmp.RunSpeed);
zmcaux.ZAux_Direct_SetAccel(MyCard.g_handle, Axis, (float)ssmp.AccTime);
zmcaux.ZAux_Direct_SetDecel(MyCard.g_handle, Axis, (float)ssmp.DecTime);
zmcaux.ZAux_Direct_SetSramp(MyCard.g_handle, Axis, (float)ssmp.Sramp);
zmcaux.ZAux_Direct_Single_MoveAbs(MyCard.g_handle, Axis, (float)ssmp.ActualDir);
}
else
{
Initialize();
int model = 0;//0-S曲线加速 1-直线加速 2-指数函数加速 3-三角函数加速
ADTMC.adt_set_admode(MyCard.CardID, Axis, model);
ADTMC.adt_set_startv(MyCard.CardID, Axis, ssmp.StartSpeed);
ADTMC.adt_set_maxv(MyCard.CardID, Axis, ssmp.RunSpeed);
ADTMC.adt_set_acc(MyCard.CardID, Axis, ssmp.AccTime);
ADTMC.adt_pmove_unit(MyCard.CardID, Axis, ssmp.ActualDir, 1);
}
}
#endregion
#region 1、获取轴运动状态
///
/// 获取轴运动状态
///
public override void GetShaftStart()
{
try
{
int runstate = 0;
if (MyCard.InterfaceType == 2)
{
zmcaux.ZAux_Direct_GetIfIdle(MyCard.g_handle, Axis, ref runstate);
}
else
{
//Thread.Sleep(1);
//ADTMC.adt_get_axis_status(MyCard.CardID, Axis, out runstate);
uint res = 0;
ADTMC.adt_get_all_axis_move_status(MyCard.CardID, out res);
string bit = Convert.ToInt64(Convert.ToString(res, 2)).ToString("00000000");
List sl = bit.ToArray().ToList();
sl.Reverse();
runstate =Convert.ToInt32(sl[Axis - 1].ToString());
}
if (runstate == 1)
{
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、专用信号状态
///
/// 专用信号状态
///
public override void SpecialStart()
{
lock (this)
{
try
{
if (MyCard.InterfaceType == 2)
{
int axisstate = 0;
zmcaux.ZAux_Direct_GetAxisStatus(MyCard.g_handle, 0, ref axisstate);
this.ss.alm = (axisstate >> 22);//int.Parse(bit.Substring(22, 1));
this.ss.elN = (axisstate >> 4);//int.Parse(bit.Substring(4, 1));
this.ss.elS = (axisstate >> 5);// int.Parse(bit.Substring(5, 1));
this.ss.org = (axisstate >> 6);// int.Parse(bit.Substring(6, 1));
uint state = 0;
zmcaux.ZAux_Direct_GetIn(MyCard.g_handle, SpecialIO.Org_IO, ref state);//获取原点IO状态
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.White;
this.ShaftState.StatuInfo = "回原点中...";
this.ShaftState.StatusForeground = ColorStr.Black;
}
else
if (state == 1)
{
this.ShaftState.StatusBackground = ColorStr.White;
this.ShaftState.StatuInfo = "原点";
this.ShaftState.StatusForeground = ColorStr.Black;
}
else //获取运动状态
{
GetShaftStart();
}
}
else
{
int alm = 10;
int elN = 10;
int elS = 10;
int org = 10;
int emg = 10;
if (IsServo)
{
ADTMC.adt_read_inbit(MyCard.CardID, Axis - 1, out alm);
}
//ADTMC.adt_read_inbit(MyCard.CardID, (Axis + 3) * 2, out elN);
//ADTMC.adt_read_inbit(MyCard.CardID, ((Axis + 3) * 2) + 1, out elS);
//ADTMC.adt_read_inbit(MyCard.CardID, Axis + 15, out org);
//ADTMC.adt_read_inbit(MyCard.CardID, 36, out emg);
//ADTMC.adt_read_inbit(MyCard.CardID, SpecialIO.Alm_IO, out alm);
//ADTMC.adt_read_inbit(MyCard.CardID, SpecialIO.ElN_IO, out elN);
//ADTMC.adt_read_inbit(MyCard.CardID, SpecialIO.ElS_IO, out elS);
//ADTMC.adt_read_inbit(MyCard.CardID, SpecialIO.Emg_IO, out org);
//ADTMC.adt_read_inbit(MyCard.CardID, 36, out emg);
int res = 0;
ADTMC.adt_get_stopdata(MyCard.CardID, Axis,out res);
this.ss.alm = alm;
this.ss.elN = elN;
this.ss.elS = elS;
this.ss.org = org;
this.ss.emg = emg;
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.White;
this.ShaftState.StatuInfo = "回原点中...";
this.ShaftState.StatusForeground = ColorStr.Black;
}
else
if (this.ss.emg == 1)
{
this.ShaftState.StatusBackground = ColorStr.Yellow;
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、获取实际位移
///
/// 获取脉冲位移 并计算出实际移动距离
///
public override void GetPluseDis()
{
try
{
if (MyCard.InterfaceType == 2)
{
float weizhi = 0;
zmcaux.ZAux_Direct_GetDpos(MyCard.g_handle, Axis, ref weizhi);
this.ActualDir = Math.Round(weizhi, 2);
}
else
{
double dPose;
ADTMC.adt_get_target_pos_unit(MyCard.CardID, Axis, out dPose);
this.ActualDir = Math.Round(dPose, 2);
}
}
catch (Exception)
{
this.ShaftState.StatusBackground = ColorStr.Yellow;
this.ShaftState.StatuInfo = "获取实时位移失败";
this.ShaftState.StatusForeground = ColorStr.Black;
}
}
#endregion
#region 4、开启或关闭使能
///
/// 开启或关闭使能
///
public override void ServoInOut()
{
try
{
if (IsServo)
{
if (MyCard.InterfaceType == 2)
{
zmcaux.ZAux_Direct_SetOp(MyCard.g_handle, Axis * 2, 1); //IO 呈0,2,4,6,8,10分布
}
else
{
int index = ((Axis - 1) * 2 + 1);//未解之谜
//ADTMC.adt_read_outbit(MyCard.CardID, index, out int nStatus);
//int result = nStatus;
//if (result == 0 && OnOff == 1)
//{
ADTMC.adt_write_outbit(MyCard.CardID, index, 1);
//}
//else
//{
// ADTMC.adt_write_outbit(MyCard.CardID, index, 0);
//}
}
}
}
catch (Exception)
{
this.ShaftState.StatusBackground = ColorStr.Yellow;
this.ShaftState.StatuInfo = "开启使能失败";
this.ShaftState.StatusForeground = ColorStr.Black;
}
}
#endregion
#region 5、对应轴归零
///
/// 对应轴归零
///
public override void GoZero()
{
try
{
if (MyCard.InterfaceType == 2)
{
zmcaux.ZAux_Direct_SetDpos(MyCard.g_handle, Axis, 0);
}
else
{
ADTMC.adt_set_command_pos(MyCard.CardID, Axis, 0);
ADTMC.adt_set_actual_pos(MyCard.CardID, Axis, 0);
}
}
catch (Exception)
{
this.ShaftState.StatusBackground = ColorStr.Yellow;
this.ShaftState.StatuInfo = "位置清零失败";
this.ShaftState.StatusForeground = ColorStr.Black;
}
}
#endregion
#region 获取轴的所有状态(轴运动状态,专用信号状态,脉冲位移,使能开关) +GetAllStart()
///
/// 获取轴的所有状态(轴运动状态,专用信号状态,脉冲位移,使能开关)
///
public override void GetAllStart()
{
if (this.ToOrgin)//如果原点都不用回 没必要监控轴状态
{
}
SpecialStart();
GetPluseDis();
}
#endregion
#region 连续运动
///
/// 连续运动
///
/// 方向参数
public override void ContinuousMove(ushort dir)
{
//如果处于运行中,则跳出
if (this.ShaftState.StatusBackground == ColorStr.Green)
{
return;
}
//if (GengrelMp == null)
//{
if (MyCard.InterfaceType == 2)
{
//设置轴参数
zmcaux.ZAux_Direct_SetUnits(MyCard.g_handle, Axis, (float)ConValue);
zmcaux.ZAux_Direct_SetLspeed(MyCard.g_handle, Axis, (float)MyHomePar.StartSpeed);
zmcaux.ZAux_Direct_SetSpeed(MyCard.g_handle, Axis, (float)MyHomePar.RunSpeed);
zmcaux.ZAux_Direct_SetAccel(MyCard.g_handle, Axis, (float)MyHomePar.AccTime);
zmcaux.ZAux_Direct_SetDecel(MyCard.g_handle, Axis, (float)MyHomePar.DecTime);
zmcaux.ZAux_Direct_SetSramp(MyCard.g_handle, Axis, 0);
zmcaux.ZAux_Direct_Single_Vmove(MyCard.g_handle, Axis, dir);
}
else
{
int model = 0;//0-S曲线加速 1-直线加速 2-指数函数加速 3-三角函数加速
ADTMC.adt_set_admode(MyCard.CardID, Axis, model);
ADTMC.adt_set_startv(MyCard.CardID, Axis, MyHomePar.StartSpeed);
ADTMC.adt_set_maxv(MyCard.CardID, Axis, MyHomePar.RunSpeed);
ADTMC.adt_set_acc(MyCard.CardID, Axis, MyHomePar.AccTime);
ADTMC.adt_continue_move(MyCard.CardID, Axis, Math.Abs(dir - 1));//0:正向 ;1:负向
}
}
#endregion
#region 停止运行
///
/// 停止运行
///
public override void ShaftStop()
{
if (MyCard != null)
{
if (MyCard.InterfaceType == 2)
{
zmcaux.ZAux_Direct_Single_Cancel(MyCard.g_handle, Axis, 3);
}
else
{
ADTMC.adt_set_axis_stop(MyCard.CardID, Axis, 1);
}
}
}
#endregion
}
}