using Community; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Model.MotionControl { public class Shaft : NotificationObject { #region 基本参数 public int ID { get; set; } /// /// 序号 /// public int SerialNumber { get; set; } private string inCode; /// /// 轴简码 /// public string InCode { get { return inCode; } set { inCode = value; this.RaisePropertyChanged("InCode"); } } private int goHomeOrder; /// /// 回原点顺序 /// public int GoHomeOrder { get { return goHomeOrder; } set { goHomeOrder = value; this.RaisePropertyChanged("GoHomeOrder"); } } private string shaftName; /// /// 轴名称 /// public string ShaftName { get { return shaftName; } set { shaftName = value; this.RaisePropertyChanged("ShaftName"); } } /// /// 控制卡ID /// public ushort CardID { get; set; } private ushort axis; /// /// 轴号 /// public ushort Axis { get { return axis; } set { axis = value; this.RaisePropertyChanged("Axis"); } } private bool toOrgin; /// /// 是否需要回原点 /// public bool ToOrgin { get { return toOrgin; } set { toOrgin = value; this.RaisePropertyChanged("ToOrgin"); } } /// /// 是否是LAN控制器的轴 /// public bool LanControl { get; set; } #endregion #region 已运行距离 /// /// 脉冲位移 /// public double PulseDir { get; set; } private double actualDir; /// /// 实际运行距离 /// public double ActualDir { get { return actualDir; } set { actualDir = value; this.RaisePropertyChanged("ActualDir"); } } private double conValue; /// /// 换算值 /// public double ConValue { get { return conValue; } set { conValue = value; this.RaisePropertyChanged("ConValue"); } } #endregion #region 脉冲输出模式 (步进:3;伺服:5) private ushort pulseOutMode; /// /// 脉冲输出模式 (步进:3;伺服:5) /// public ushort PulseOutMode { get { return pulseOutMode; } set { pulseOutMode = value; this.RaisePropertyChanged("PulseOutMode"); } } #endregion #region 编码器参数 private ushort encoderMode; /// /// 编码器脉冲模式0:非AB相(脉冲/方向)1:AB相1倍计数2:AB相2倍计数3:AB相3倍计数 /// public ushort EncoderMode { get { return encoderMode; } set { encoderMode = value; this.RaisePropertyChanged("EncoderMode"); } } #endregion #region 回原点参数 private HomePer home; /// /// 原点参数 /// public HomePer Home { get { return home; } set { home = value; this.RaisePropertyChanged("Home"); } } #endregion #region 运动参数 private MovementPer standbys; /// /// 默认运动参数(运行时不能更改) /// public MovementPer Standbys { get { return standbys; } set { standbys = value; this.RaisePropertyChanged("Standbys"); } } /// /// 通用运行参数 /// public MovementPer GengrelMp { get; set; } #endregion #region 限位/停止参数 private ushort hard_Enable; /// /// 硬件限位开启模式0:正负限位禁止1:正负限位允许2:正禁止,负允许3:正允许,负禁止 /// public ushort Hard_Enable { get { return hard_Enable; } set { hard_Enable = value; this.RaisePropertyChanged("Hard_Enable"); } } private ushort hard_Logic; /// /// 硬件限位有效电平 (0: 正负限位低电平有效/1: 正负限位高电平有效/2: 正限位低有效, 负限位高有效/3: 正限位高有效, 负限位低有效) /// public ushort Hard_Logic { get { return hard_Logic; } set { hard_Logic = value; this.RaisePropertyChanged("Hard_Logic"); } } private ushort hard_Mode; /// /// 硬件限位制动模式0:正负限位立即停止1:正负限位减速停止2:正立即停止,负减速停止3:正减速停止,负立即停止 /// public ushort Hard_Mode { get { return hard_Mode; } set { hard_Mode = value; this.RaisePropertyChanged("Hard_Mode"); } } private ushort soft_Enable; /// /// 软件限位使能状态 0:禁止1:允许 /// public ushort Soft_Enable { get { return soft_Enable; } set { soft_Enable = value; this.RaisePropertyChanged("Soft_Enable"); } } private ushort soft_Mode; /// /// 软件限位制动模式0:立即停止1:减速停止 /// public ushort Soft_Mode { get { return soft_Mode; } set { soft_Mode = value; this.RaisePropertyChanged("Soft_Mode"); } } private ushort emg_Enable; /// /// EMG急停触发减速停止 使能状态 /// public ushort Emg_Enable { get { return emg_Enable; } set { emg_Enable = value; this.RaisePropertyChanged("Emg_Enable"); } } private ushort emg_Logic; /// /// EMG急停触发减速停止 有效电平 /// public ushort Emg_Logic { get { return emg_Logic; } set { emg_Logic = value; this.RaisePropertyChanged("Emg_Logic"); } } #endregion #region 伺服参数 private bool isServo; /// /// 是否是伺服电机 /// public bool IsServo { get { return isServo; } set { isServo = value; this.RaisePropertyChanged("IsServo"); } } private short servoSwitch; /// /// 使能开关 0:开1:关 /// public short ServoSwitch { get { return servoSwitch; } set { servoSwitch = value; this.RaisePropertyChanged("ServoSwitch"); } } private ushort inp_Enable; /// /// INP使能(0:禁用1:允许) /// public ushort Inp_Enable { get { return inp_Enable; } set { inp_Enable = value; this.RaisePropertyChanged("INP_Enable"); } } private ushort inp_Logic; /// /// INP电平(0:低电平有效1:高电平有效) /// public ushort Inp_Logic { get { return inp_Logic; } set { inp_Logic = value; } } private ushort alm_Enable; /// /// ALM使能(0:禁用1:允许) /// public ushort Alm_Enable { get { return alm_Enable; } set { alm_Enable = value; this.RaisePropertyChanged("Alm_Enable"); } } private ushort alm_Logic; /// /// ALM信号电平(0:低电平有效1:高电平有效) /// public ushort Alm_Logic { get { return alm_Logic; } set { alm_Logic = value; this.RaisePropertyChanged("Alm_Logic"); } } #endregion #region 专用信号对象 /// /// 专用信号对象 /// public SpecialSignals ss { get; set; } #endregion #region 状态显示对象 private ShowInfo shaftState; /// /// 状态显示对象 /// public ShowInfo ShaftState { get { return shaftState; } set { shaftState = value; this.RaisePropertyChanged("ShaftState"); } } #endregion /// /// 回原点方法 /// public DelegateCommand GoHomeCommand { get; set; } public Shaft() { InCode = "-"; PulseDir = 0; ShaftState = new ShowInfo(); ShaftState.StatusBackground = ColorStr.Red; ShaftState.StatuInfo = "停止中"; ShaftState.StatusForeground = ColorStr.White; ss = new SpecialSignals(); Home = new HomePer(); Standbys = new MovementPer(); HomeStep = GengralProcessEnum.Start; GoHomeCommand = new DelegateCommand(); GoHomeCommand.ExecuteAction = new Action(GoHome); } private void GoHome(object obj) { org_Successful = true; } #region 初始化当前轴信息 /// /// 初始化当前轴信息 /// public void Initialize() { if (LanControl) { ServoInOut(); LTSMC.smc_set_pulse_outmode(CardID, Axis, PulseOutMode);//设置脉冲输出模式 LTSMC.smc_set_home_pin_logic(CardID, Axis, Home.Logic, 0); //设置回原有效电平 LTSMC.smc_set_alm_mode(CardID, Axis, alm_Enable, Alm_Logic, 0);//设置指定的ALM信号 LTSMC.smc_set_emg_mode(CardID, Axis, Emg_Enable, Emg_Logic); //设置EMG急停信号 LTSMC.smc_set_el_mode(CardID, Axis, Hard_Enable, Hard_Logic, 0); //设置硬件限位信号 LTSMC.smc_set_equiv(CardID, Axis, this.ConValue); //设置脉冲解析度 LTSMC.smc_set_home_position_unit(CardID, Axis, Home.ResetMode, 0);//原点完成后重置坐标方法(0:不重置/1:先重置再定位/2:定位后重置) LTSMC.smc_set_inp_mode(CardID, Axis, Inp_Enable, 0); //设置输入信号模式 LTSMC.smc_set_softlimit_unit(CardID, Axis, Soft_Enable, 0, Soft_Mode, 0, 0); //设置软件限位 } else { ServoInOut(); LTDMC.dmc_set_pulse_outmode(CardID, Axis, PulseOutMode); //设置脉冲输出模式 LTDMC.dmc_set_home_pin_logic(CardID, Axis, Home.Logic, 0); //设置回原有效电平 LTDMC.dmc_set_el_mode(CardID, Axis, Hard_Enable, Hard_Logic, 0); // 设置硬件限位信号 LTDMC.dmc_set_emg_mode(CardID, Axis, Emg_Enable, Emg_Logic); // //设置EMG急停信号 LTDMC.dmc_set_alm_mode(CardID, Axis, Alm_Enable, Alm_Logic, 0); //设置指定的ALM信号 } } #endregion #region 回原点运动 void HYD() /// /// 是否正在寻原点 /// public bool org_Successful { get; set; } DateTime HomeTime; /// /// 单张回原点步骤 /// public GengralProcessEnum HomeStep { get; set; } /// /// 回原点运动 /// /// 轴运动参数 public 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 (LanControl) { LTSMC.smc_set_home_profile_unit(CardID, Axis, Home.StartSpeed, Home.RunSpeed, Home.AccTime, Home.DecTime);//设置起始速度、运行速度、停止速度、加速时间、减速时间 LTSMC.smc_set_homemode(this.CardID, this.Axis, Home.Direction, Home.SpeedMode, Home.Mode, 0);//设置回零模式 LTSMC.smc_home_move(this.CardID, this.Axis);//启动回原点 } else { LTDMC.dmc_set_profile(this.CardID, this.Axis, Home.StartSpeed, Home.RunSpeed, Home.AccTime, Home.DecTime, Home.StopSpeed);//设置起始速度、运行速度、停止速度、加速时间、减速时间 LTDMC.dmc_set_homemode(this.CardID, this.Axis, Home.Direction, Home.SpeedMode, Home.Mode, 0);//设置回零模式 LTDMC.dmc_home_move(this.CardID, this.Axis);//启动回原点 } HomeStep++; } break; case GengralProcessEnum.Step1: ushort state = 0; LTSMC.smc_get_home_result(this.CardID, this.Axis, ref state); if (state == 1) { 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 void DZDC(MovementPer ssmp) { //如果处于运行中,则跳出 if (this.ShaftState.StatusBackground == ColorStr.Green) { return; } if (LanControl) { LTSMC.smc_set_profile_unit(CardID, Axis, ssmp.StartSpeed, ssmp.RunSpeed, ssmp.AccTime, ssmp.DecTime, ssmp.StopSpeed); //设置起始速度、运行速度、停止速度、加速时间、减速时间 LTSMC.smc_pmove_unit(CardID, Axis, ssmp.ActualDir, 1); //定长运动 1:绝对坐标模式 } else { LTDMC.dmc_set_profile(CardID, Axis, ssmp.StartSpeed, ssmp.RunSpeed, ssmp.AccTime, ssmp.DecTime, ssmp.StopSpeed); //设置起始速度、运行速度、停止速度、加速时间、减速时间 LTDMC.dmc_pmove(CardID, Axis, Convert.ToInt32(ssmp.ActualDir * ConValue), 1); //定长运动 1:绝对坐标模式 } } #endregion #region 1、获取轴运动状态 /// /// 获取轴运动状态 /// public void GetShaftStart() { try { short wh = 0; if (LanControl) { wh = LTSMC.smc_check_done(this.CardID, this.Axis); } else { wh = LTDMC.dmc_check_done(this.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、专用信号状态 /// /// 专用信号状态 /// public void SpecialStart() { lock (this) { try { string bit = ""; if (LanControl) { bit = Convert.ToInt64(Convert.ToString(LTSMC.smc_axis_io_status(this.CardID, this.Axis), 2)).ToString("0000000000000000"); } else { bit = Convert.ToInt32(Convert.ToString(LTDMC.dmc_axis_io_status(this.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、获取实际位移 /// /// 获取脉冲位移 并计算出实际移动距离 /// public void GetPluseDis() { try { if (LanControl) { double temp = 0; LTSMC.smc_get_position_unit(this.CardID, this.Axis, ref temp); this.ActualDir = Math.Round(temp, 2); } else { PulseDir = LTDMC.dmc_get_position(this.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、开启或关闭使能 /// /// 开启或关闭使能 /// public void ServoInOut() { try { if (IsServo) { if (LanControl) { ServoSwitch = LTSMC.smc_write_sevon_pin(CardID, Axis, 0); } else { ServoSwitch = LTDMC.dmc_write_sevon_pin(CardID, Axis, 0); } } } catch (Exception) { this.ShaftState.StatusBackground = ColorStr.Yellow; this.ShaftState.StatuInfo = "开启使能失败"; this.ShaftState.StatusForeground = ColorStr.Black; } } #endregion #region 5、对应轴归零 /// /// 对应轴归零 /// public void GoZero() { try { if (LanControl) { LTSMC.smc_set_position_unit(CardID, Axis, 0);//位置清零 } else { LTDMC.dmc_set_position(CardID, Axis, 0);//位置清零 } } catch (Exception) { this.ShaftState.StatusBackground = ColorStr.Yellow; this.ShaftState.StatuInfo = "位置清零失败"; this.ShaftState.StatusForeground = ColorStr.Black; } } #endregion #region 获取轴的所有状态(轴运动状态,专用信号状态,脉冲位移,使能开关) +GetAllStart() /// /// 获取轴的所有状态(轴运动状态,专用信号状态,脉冲位移,使能开关) /// public void GetAllStart() { SpecialStart(); if (this.ToOrgin)//如果原点都不用回 那也没必要监控位移 { GetPluseDis(); } } #endregion #region 连续运动 /// /// 连续运动 /// /// 方向参数 //public void ContinuousMove(ushort dir) //{ // //如果处于运行中,则跳出 // if (this.ShaftState.StatusBackground == ColorStr.Green) // { // return; // } // if (LanControl) // { // //连续运动 // LTSMC.smc_set_profile_unit(this.CardID, this.Axis, Home.StartSpeed, Home.RunSpeed, Home.AccTime, Home.DecTime, Home.StopSpeed);//设置起始速度、运行速度、停止速度、加速时间、减速时间 // LTSMC.smc_vmove(this.CardID, this.Axis, dir);//连续运动 // } // else // { // //连续运动 // LTDMC.dmc_set_profile(this.CardID, this.Axis, Home.StartSpeed, Home.RunSpeed, Home.AccTime, Home.DecTime, Home.StopSpeed);//设置起始速度、运行速度、停止速度、加速时间、减速时间 // LTDMC.dmc_vmove(this.CardID, this.Axis, dir);//连续运动 // } //} /// /// 连续运动 /// /// 方向参数 public void ContinuousMove(ushort dir) { //如果处于运行中,则跳出 if (this.ShaftState.StatusBackground == ColorStr.Green) { return; } if (GengrelMp == null) { if (LanControl) { //连续运动 LTSMC.smc_set_profile_unit(this.CardID, this.Axis, Home.StartSpeed, Home.RunSpeed, Home.AccTime, Home.DecTime, Home.StopSpeed);//设置起始速度、运行速度、停止速度、加速时间、减速时间 LTSMC.smc_vmove(this.CardID, this.Axis, dir);//连续运动 } else { //连续运动 LTDMC.dmc_set_profile(this.CardID, this.Axis, Home.StartSpeed, Home.RunSpeed, Home.AccTime, Home.DecTime, Home.StopSpeed);//设置起始速度、运行速度、停止速度、加速时间、减速时间 LTDMC.dmc_vmove(this.CardID, this.Axis, dir);//连续运动 } } else { if (LanControl) { //连续运动 LTSMC.smc_set_profile_unit(this.CardID, this.Axis, GengrelMp.StartSpeed, GengrelMp.RunSpeed, GengrelMp.AccTime, GengrelMp.DecTime, GengrelMp.StopSpeed);//设置起始速度、运行速度、停止速度、加速时间、减速时间 LTSMC.smc_vmove(this.CardID, this.Axis, dir);//连续运动 } else { //连续运动 LTDMC.dmc_set_profile(this.CardID, this.Axis, GengrelMp.StartSpeed, GengrelMp.RunSpeed, GengrelMp.AccTime, GengrelMp.DecTime, GengrelMp.StopSpeed);//设置起始速度、运行速度、停止速度、加速时间、减速时间 LTDMC.dmc_vmove(this.CardID, this.Axis, dir);//连续运动 } } } #endregion #region 停止运行 /// /// 停止运行 /// public void ShaftStop() { if (LanControl) { LTSMC.smc_stop(this.CardID, this.Axis, 1); } else { LTDMC.dmc_stop(this.CardID, this.Axis, 1); } } #endregion } }