#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 运行速度,单位 ppsTacc 总加速时间,单位 sTdec 总减速时间,单位 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,WORDposi_mode)功 能:指定任意两轴以对称的梯形速度曲线做插补运动参 数:axis1 指定两轴插补的第一轴axis2 指定两轴插补的第二轴Dist1 指定 axis1 的位移值,单位:脉冲数Dist2 指定 axis2 的位移值,单位:脉冲数posi_mode 位移模式设定:0 表示相对位移,1 表示绝对位移返回值:无*/Dmc2410.d2410_t_line2(XAxis, inX, YAxis, inY, 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//当前步骤加1CurrentStep++;#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是否为trueif (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;}}}#endregionpublic enum RunStep{//等待运行RunReady,//前往取料口MoveToReclaimer,//取料口判断料是否存在ReclaimerWait,//取料口Z轴下降ReclaimerDown,//取料口抓取物体ReclaimerGrap,//取料口Z轴上升ReclaimerUp,//前往加工位MoveToProcess,//加工位检测ProcessCheck,//加工位Z轴下降1ProcessDown1,//加工位放下物体ProcessPutDown,//加工位Z轴上升1ProcessUp1,//加工位等待ProcessWait,//加工位Z轴下降2ProcessDown2,//加工位抓取物体ProcessGrab,//加工位Z轴上升2ProcessUp2,//前往出料口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; }}
如果觉得《雷赛运动控制卡例子》对你有帮助,请点赞、收藏,并留下你的观点哦!