雷赛运动控制卡例子

        #region 自动运行流程
        private void AutoRunProcess()
        {
            AddLog(0, "系统归零完成,等待触发");

            //初始化当前步骤
            CurrentStep = RunStep.RunReady;

            bool fRun = false;

            bool fOver = false;

            int Error = 0;

            int CycleTimes = 0;

            while (true)
            {
                //检测是否按下启动信号
                int start = Dmc2410.d2410_read_inbit(0, 2);

                if (start == 0)
                {
                    fRun = true;
                    CurrentStep = RunStep.MoveToReclaimer;
                }

                if (fRun)
                {
                    AddLog(0, "系统开始生产...");

                    MySQLHelper.Log("系统开始生产...", 0, CommonMethods.objAdmin.LoginName);

                    fRun = false;

                    SysStatus = 1;

                    while (true)
                    {
                        switch (CurrentStep)
                        {
                            case RunStep.MoveToReclaimer:

                                UpdateStep("1:前往取料口");

                                if (CycleTimes == 0)
                                {
                                    Dmc2410.d2410_set_position(XAxis, 0); // 指定轴号, 绝对位置值
                                    Dmc2410.d2410_set_position(YAxis, 0);
                                    //设置直线插补的参数
                                    /*
                                     void d2410_set_vector_profile(double Min_Vel,double Max_Vel,double Tacc,
                                    double Tdec)
                                    功 能:设定插补矢量运动曲线的起始速度、运行速度、加速时间、减速时间
                                    参 数:Min_Vel  起始速度,单位 pps  pps,pulse per second ,用来表示步进电机的转速
                                    Max_Vel  运行速度,单位 pps
                                    Tacc  总加速时间,单位 s
                                    Tdec 总减速时间,单位 s
                                     */
                                    Dmc2410.d2410_set_vector_profile(objConfig.VelMin, objConfig.VelMax, objConfig.Tac, objConfig.Tac);
                                    //指定任意两轴以对称的梯形速度曲线做插补运动
                                    /*
                                    void d2410_t_line2(WORD axis1,long Dist1,WORD axis2,long Dist2,WORD
                                    posi_mode)
                                    功 能:指定任意两轴以对称的梯形速度曲线做插补运动
                                    参 数:axis1 指定两轴插补的第一轴
                                    axis2 指定两轴插补的第二轴
                                    Dist1 指定 axis1 的位移值,单位:脉冲数
                                    Dist2 指定 axis2 的位移值,单位:脉冲数
                                    posi_mode 位移模式设定:0 表示相对位移,1 表示绝对位移
                                    返回值:无
                                     */
                                    Dmc2410.d2410_t_line2(XAxis, objConfig.OrginX, YAxis, objConfig.OrginY, 0);

                                }
                                else
                                {
                                    //设置直线插补的参数
                                    Dmc2410.d2410_set_vector_profile(objConfig.VelMin, objConfig.VelMax, objConfig.Tac, objConfig.Tac);

                                    Dmc2410.d2410_t_line2(XAxis, objConfig.OutletX, YAxis, objConfig.OutletY, 0);
                                }
                                /*
                                 WORD d2410_check_done(WORD axis)
                                功 能:检测指定轴的运动状态,停止或是在运行中。
                                参 数:axis  指定轴号
                                返回值:0 表示指定轴正在运行,1 表示指定轴已停止。
                                 */
                                while (Dmc2410.d2410_check_done(XAxis) == 0 || Dmc2410.d2410_check_done(YAxis) == 0)
                                {
                                    if (IsAutoStop)
                                    {
                                        fOver = true;
                                        break;
                                    }
                                    Thread.Sleep(10);
                                }

                                if (CycleTimes > 0)
                                {
                                    Invoke(new Action(() =>
                                    {
                                        this.pointCurve1.ClearAllPoints();
                                    }));
                                
                                }

                                break;
                            case RunStep.ReclaimerWait:

                                //实际项目在这里需要检测是否有料

                                UpdateStep("2:检测是否有料");

                                Thread.Sleep(2000);

                                break;
                            case RunStep.ReclaimerDown:

                                UpdateStep("3:取料口下降");

                                Dmc2410.d2410_set_position(ZAxis, 0);

                                Dmc2410.d2410_set_st_profile(ZAxis, objConfig.VelMin, objConfig.VelMax, objConfig.Tac, objConfig.Tac, objConfig.STac, objConfig.STac);
                                /*
                                 void d2410_s_pmove(WORD axis,long Dist,WORD posi_mode)
                                功 能:使指定轴以对称 S 形速度曲线做定长位移运动
                                参 数:axis  指定轴号
                                Dist  (绝对/相对)位移值,单位:脉冲数
                                posi_mode 位移模式设定:0 表示相对位移,1 表示绝对位移
                                 */
                                Dmc2410.d2410_s_pmove(ZAxis, objConfig.DownZAxis, 0);

                                while (Dmc2410.d2410_check_done(ZAxis) == 0)
                                {
                                    if (IsAutoStop)
                                    {
                                        fOver = true;
                                        break;
                                    }
                                    Thread.Sleep(10);
                                }
                                break;
                            case RunStep.ReclaimerGrap:

                                UpdateStep("4:取料口抓取");
                                /*
                                 void d2410_write_outbit (WORD cardno, WORD bitno,WORD on_off)
                                功 能:对指定控制卡的某一位输出口置位
                                参 数:cardno 指定控制卡号, 范围(0 - N - 1 ,N 为卡数)
                                Bitno 指定输出口位号(取值范围:1-20)
                                on_off 输出电平:0-表示输出低电平,1-表示输出高电平。
                                注 意:其中输出口 1 –12 初始状态可选, 当拨码开关 S1 对应的位设置为 OFF
                                后, 则输出口的电平逻辑会取反。
                                 */
                                //抓取物体
                                Dmc2410.d2410_write_outbit(0, 4, 0);

                                Thread.Sleep(500);

                                break;
                            case RunStep.ReclaimerUp:

                                UpdateStep("5:取料口上升");

                                Dmc2410.d2410_set_position(ZAxis, 0);

                                Dmc2410.d2410_set_st_profile(ZAxis, objConfig.VelMin, objConfig.VelMax, objConfig.Tac, objConfig.Tac, objConfig.STac, objConfig.STac);

                                Dmc2410.d2410_s_pmove(ZAxis, objConfig.LiftZAxis * (-1), 0);

                                while (Dmc2410.d2410_check_done(ZAxis) == 0)
                                {
                                    if (IsAutoStop)
                                    {
                                        fOver = true;
                                        break;
                                    }
                                    Thread.Sleep(10);
                                }

                                break;
                            case RunStep.MoveToProcess:

                                UpdateStep("6:前往加工位");

                                //设置直线插补的参数
                                Dmc2410.d2410_set_vector_profile(objConfig.VelMin, objConfig.VelMax, objConfig.Tac, objConfig.Tac);

                                Dmc2410.d2410_t_line2(XAxis, objConfig.ReclaimerX, YAxis, objConfig.ReclaimerY, 0);

                                while (Dmc2410.d2410_check_done(XAxis) == 0 || Dmc2410.d2410_check_done(YAxis) == 0)
                                {
                                    if (IsAutoStop)
                                    {
                                        fOver = true;
                                        break;
                                    }
                                    Thread.Sleep(10);
                                }

                                break;
                            case RunStep.ProcessCheck:

                                UpdateStep("7:检测是否有料");

                                //实际项目在这里需要检测是否有料

                                Thread.Sleep(2000);

                                break;
                            case RunStep.ProcessDown1:

                                UpdateStep("8:加工位下降I");

                                Dmc2410.d2410_set_position(ZAxis, 0);

                                Dmc2410.d2410_set_st_profile(ZAxis, objConfig.VelMin, objConfig.VelMax, objConfig.Tac, objConfig.Tac, objConfig.STac, objConfig.STac);

                                Dmc2410.d2410_s_pmove(ZAxis, objConfig.LiftZAxis, 0);

                                while (Dmc2410.d2410_check_done(ZAxis) == 0)
                                {
                                    if (IsAutoStop)
                                    {
                                        fOver = true;
                                        break;
                                    }
                                    Thread.Sleep(10);
                                }

                                break;
                            case RunStep.ProcessPutDown:

                                UpdateStep("9:加工位放下");

                                //放下物体
                                Dmc2410.d2410_write_outbit(0, 4, 1);

                                Thread.Sleep(500);

                                break;
                            case RunStep.ProcessUp1:

                                UpdateStep("10:加工位上升I");

                                Dmc2410.d2410_set_position(ZAxis, 0);

                                Dmc2410.d2410_set_st_profile(ZAxis, objConfig.VelMin, objConfig.VelMax, objConfig.Tac, objConfig.Tac, objConfig.STac, objConfig.STac);

                                Dmc2410.d2410_s_pmove(ZAxis, objConfig.LiftZAxis * (-1), 0);

                                while (Dmc2410.d2410_check_done(ZAxis) == 0)
                                {
                                    if (IsAutoStop)
                                    {
                                        fOver = true;
                                        break;
                                    }
                                    Thread.Sleep(10);
                                }

                                break;
                            case RunStep.ProcessWait:

                                UpdateStep("11:检测是否回来");

                                //实际项目在这里需要检测是否有料

                                Thread.Sleep(2000);

                                break;
                            case RunStep.ProcessDown2:

                                UpdateStep("12:加工位下降II");

                                Dmc2410.d2410_set_position(ZAxis, 0);

                                Dmc2410.d2410_set_st_profile(ZAxis, objConfig.VelMin, objConfig.VelMax, objConfig.Tac, objConfig.Tac, objConfig.STac, objConfig.STac);

                                Dmc2410.d2410_s_pmove(ZAxis, objConfig.LiftZAxis, 0);

                                while (Dmc2410.d2410_check_done(ZAxis) == 0)
                                {
                                    if (IsAutoStop)
                                    {
                                        fOver = true;
                                        break;
                                    }
                                    Thread.Sleep(10);
                                }

                                break;
                            case RunStep.ProcessGrab:
                                UpdateStep("13:加工位抓取");

                                //放下物体
                                Dmc2410.d2410_write_outbit(0, 4, 0);

                                Thread.Sleep(500);

                                break;
                            case RunStep.ProcessUp2:

                                UpdateStep("14:加工位上升II");

                                Dmc2410.d2410_set_position(ZAxis, 0);

                                Dmc2410.d2410_set_st_profile(ZAxis, objConfig.VelMin, objConfig.VelMax, objConfig.Tac, objConfig.Tac, objConfig.STac, objConfig.STac);

                                Dmc2410.d2410_s_pmove(ZAxis, objConfig.LiftZAxis * (-1), 0);

                                while (Dmc2410.d2410_check_done(ZAxis) == 0)
                                {
                                    if (IsAutoStop)
                                    {
                                        fOver = true;
                                        break;
                                    }
                                    Thread.Sleep(10);
                                }

                                break;
                            case RunStep.MoveToOutlet:

                                UpdateStep("15:前往出料口");

                                //设置直线插补的参数
                                Dmc2410.d2410_set_vector_profile(objConfig.VelMin, objConfig.VelMax, objConfig.Tac, objConfig.Tac);

                                Dmc2410.d2410_t_line2(XAxis, objConfig.ProcessX, YAxis, objConfig.ProcessY, 0);

                                while (Dmc2410.d2410_check_done(XAxis) == 0 || Dmc2410.d2410_check_done(YAxis) == 0)
                                {
                                    if (IsAutoStop)
                                    {
                                        fOver = true;
                                        break;
                                    }
                                    Thread.Sleep(10);
                                }

                                break;
                            case RunStep.OutletWait:

                                UpdateStep("16:出料口检测");

                                //实际项目在这里需要检测是否有料

                                Thread.Sleep(2000);


                                break;
                            case RunStep.OutletDown:

                                UpdateStep("17:出料口下降");

                                Dmc2410.d2410_set_position(ZAxis, 0);

                                Dmc2410.d2410_set_st_profile(ZAxis, objConfig.VelMin, objConfig.VelMax, objConfig.Tac, objConfig.Tac, objConfig.STac, objConfig.STac);

                                Dmc2410.d2410_s_pmove(ZAxis, objConfig.LiftZAxis, 0);

                                while (Dmc2410.d2410_check_done(ZAxis) == 0)
                                {
                                    if (IsAutoStop)
                                    {
                                        fOver = true;
                                        break;
                                    }
                                    Thread.Sleep(10);
                                }

                                break;
                            case RunStep.OutletPutDown:

                                UpdateStep("18:出料口放下");

                                //放下物体
                                Dmc2410.d2410_write_outbit(0, 4, 1);

                                Thread.Sleep(500);

                                break;
                            case RunStep.OutletUp:

                                UpdateStep("19:加工位上升");

                                Dmc2410.d2410_set_position(ZAxis, 0);

                                Dmc2410.d2410_set_st_profile(ZAxis, objConfig.VelMin, objConfig.VelMax, objConfig.Tac, objConfig.Tac, objConfig.STac, objConfig.STac);

                                Dmc2410.d2410_s_pmove(ZAxis, objConfig.LiftZAxis * (-1), 0);

                                while (Dmc2410.d2410_check_done(ZAxis) == 0)
                                {
                                    if (IsAutoStop)
                                    {
                                        fOver = true;
                                        break;
                                    }
                                    Thread.Sleep(10);
                                }
                                break;
                            case RunStep.RunReady:
                                break;
                            case RunStep.MaxStep:
                                break;
                            default:
                                break;
                        }

                        #region 检测是否有暂停信号
                        //检测是否有暂停信号

                        if (IsPause)
                        {
                            AddLog(0, "系统开始暂停");

                            MySQLHelper.Log("系统开始暂停", 0, CommonMethods.objAdmin.LoginName);

                            while (IsPause)
                            {
                                if (IsAutoStop)
                                {
                                    fOver = true;
                                    break;
                                }
                                Thread.Sleep(10);
                            }
                        }
                        #endregion

                        #region 检测是否有错误信号
                        //检测是否有错误信号
                        switch (Error)
                        {
                            //根据错误严重程序
                            case 1:
                                SysStatus = 2;
                                break;
                            case 2:
                                SysStatus = 2;
                                break;
                            default:
                                break;
                        }
                        #endregion

                        //当前步骤加1
                        CurrentStep++;

                        #region 检测是否为最后一步
                        //检测是否为最后一步
                        if (CurrentStep == RunStep.MaxStep)
                        {
                            CycleTimes++;

                            //更新当前产量
                            Invoke(new Action(() =>
                            {
                                this.lbl_CurrentProduct.Text = CycleTimes.ToString();
                            }));

                            //插入数据库(暂时传递一个随机结果到数据,后面可以改成视觉检测)
                            MySQLHelper.AddProduct(txt_LotNum.Text.Trim(), new Random().Next(0,1), CommonMethods.objAdmin.LoginName);

                            switch (this.objRunMode.CircleMode)
                            {
                                case CircleMode.OneAndHome:
                                    fOver = true;
                                    break;
                                case CircleMode.OneAndStop:
                                    fOver = true;
                                    break;
                                case CircleMode.CustomTime:
                                    //完成产量,正常结束
                                    if (CycleTimes == this.objRunMode.CircleTimes)
                                    {
                                        fOver = true;
                                    }
                                    else
                                    {
                                        CurrentStep = RunStep.MoveToReclaimer;
                                    }
                                    break;
                                default:
                                    break;
                            }
                        }
                        #endregion

                        //检测fOver是否为true
                        if (fOver)
                        {
                            break;
                        }
                    }
                }

                if (fOver)
                {
                    if (IsAutoStop)
                    {
                        AddLog(1, "自动运行异常停止");

                        MySQLHelper.Log("自动运行异常停止", 1, CommonMethods.objAdmin.LoginName);

                        Dmc2410.d2410_decel_stop(XAxis, 0.1);
                        Dmc2410.d2410_decel_stop(YAxis, 0.1);
                        Dmc2410.d2410_decel_stop(ZAxis, 0.1);

                        SysStatus = 2;

                        IsAutoStop = false;
                    }
                    else
                    {
                        AddLog(0, "自动运行正常,生产完成");

                        MySQLHelper.Log("自动运行正常,生产完成", 0, CommonMethods.objAdmin.LoginName);

                        SysStatus = 0;                  
                    }

                   //将电磁阀关闭
                    Dmc2410.d2410_write_outbit(0, 4, 1);


                    //清除曲线
                    AddCurveEnable = false;

                    Invoke(new Action(() =>
                    {
                        this.pointCurve1.ClearAllPoints();
                        this.pointCurve1.Refresh();
                    }));

                    //根据模式判断是否归零
                    if (objRunMode.CircleMode != CircleMode.OneAndStop)
                    {
                        HomeProcess();
                    }
                    break;
                }
            }


        }
        #endregion

    public enum RunStep
    {
        //等待运行
        RunReady,

        //前往取料口
        MoveToReclaimer,

        //取料口判断料是否存在
        ReclaimerWait,

        //取料口Z轴下降
        ReclaimerDown,

        //取料口抓取物体
        ReclaimerGrap,

        //取料口Z轴上升
        ReclaimerUp,

        //前往加工位
        MoveToProcess,

        //加工位检测
        ProcessCheck,

        //加工位Z轴下降1
        ProcessDown1,

        //加工位放下物体
        ProcessPutDown,

        //加工位Z轴上升1
        ProcessUp1,

        //加工位等待
        ProcessWait,

        //加工位Z轴下降2
        ProcessDown2,

        //加工位抓取物体
        ProcessGrab,

        //加工位Z轴上升2
        ProcessUp2,

        //前往出料口
        MoveToOutlet,

        //出料口等待
        OutletWait,

        //出料口Z轴下降
        OutletDown,

        //加工位放下物体
        OutletPutDown,

        //加工位Z轴上升
        OutletUp,

        //总步骤
        MaxStep,
    }
public   class ConfigInfo
    {
        //初始速度
        public double VelMin { get; set; }

        //运行速度
        public double  VelMax { get; set; }

        //加速时间
        public double Tac { get; set; }

        //S段时间
        public double  STac { get; set; }

        //X轴归零位置
        public int XAxisPosition { get; set; }

        //Y轴归零位置
        public int YAxisPosition { get; set; }

        //Z轴归零位置
        public int ZAxisPosition { get; set; }

        //取料口X轴
        public int ReclaimerXAxis { get; set; }

        //取料口Y轴
        public int ReclaimerYAxis { get; set; }

        //加工位X轴
        public int ProcessXAxis { get; set; }

        //加工位Y轴
        public int ProcessYAxis { get; set; }

        //出料口X轴
        public int OutletXAxis { get; set; }

        //出料口Y轴
        public int OutletYAxis { get; set; }

        //Z轴下降
        public int DownZAxis { get; set; }

        //Z轴上升
        public int LiftZAxis { get; set; }

        //自动锁屏
        public bool AutoLock { get; set; }

        //锁屏间隔
        public int LockPeriod { get; set; }

    }
    public class ConfigInfoExt : ConfigInfo
    {

        //当前处于原点,目标距离的X轴位移
        public int OrginX
        {
            get { return this.ReclaimerXAxis; }
        }

        //当前处于原点,目标距离的Y轴位移
        public int OrginY
        {
            get { return this.ReclaimerYAxis; }
        }

        //当前处于取料点,目标距离的X轴位移
        public int ReclaimerX
        {
            get { return this.ProcessXAxis-this.ReclaimerXAxis; }
        }

        //当前处于取料点,目标距离的Y轴位移
        public int ReclaimerY
        {
            get { return this.ProcessYAxis - this.ReclaimerYAxis; }
        }

        //当前处于加工处,目标距离的X轴位移
        public int ProcessX
        {
            get { return this.OutletXAxis - this.ProcessXAxis; }
        }

        //当前处于加工处,目标距离的Y轴位移
        public int ProcessY
        {
            get { return this.OutletYAxis - this.ProcessYAxis; }
        }


        //当前处于取料口,目标距离的X轴位移
        public int OutletX
        {
            get { return this.ReclaimerXAxis - this.OutletXAxis; }
        }

        //当前处于取料口,目标距离的Y轴位移
        public int OutletY
        {
            get { return this.ReclaimerYAxis - this.OutletYAxis; }
        }

    }
    public enum CircleMode
    {
        //循环一次归零
        OneAndHome,

        //循环一次停止
        OneAndStop,

        //自定义循环次数
        CustomTime
    }

    public class AutoRunMode
    {
        //循环模式
        public CircleMode CircleMode { get; set; }

        //循环次数
        public int CircleTimes { get; set; }

    }
  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘诺西亚的火山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值