C#与雷赛运动控制卡的使用(二) | 您所在的位置:网站首页 › rhx32w1024wifi控制卡 › C#与雷赛运动控制卡的使用(二) |
去年写过一篇关于C#实现马达运动控制卡编程的博客,很多小伙伴看了希望共享下代码,后来自己觉得写得不够完善就删掉了,这次准备重新写一篇完善点的,实现代码后续也会上传资源,觉得有用的可以去下载。 C#是一门面向对象的编程语言,所以在编写马达控制程序的时候也要有面向对象的思维,所以在此我们可以将马达看成是一类对象,而机台上的每个马达就是这个马达类的具体对象。一个简单的类大致包括了字段,属性和方法,需要创建一个马达类,首先得知道马达具备哪些基本的属性,而我们使用马达来实现哪些功能,怎么控制这个马达,就是一些方法。所以在动手敲代码之前,先来确定这个马达类中的字段,属性和方法。 做过PLC的应该都知道,开发运动控制程序,应先确定马达周边的硬件配置及自身的基本参数,下面就先确定马达硬件配置。 1. 基本属性 马达编号,轴1.2.3..... 脉冲当量(一个脉冲的直线位移) mm/pulse 需根据设备具体情况计算 public int AxisNo { get; private set; } public float Gain { get; private set; }2.三个感应器是否存在以及状态(需视具体机台定义) /// /// 以下三个属性用于区分是否存在原点 正负极限 /// private bool OrgExist; private bool PLmtExist; private bool NLmtExist; public bool Origin { get; private set; } //原点 public bool PLimit { get; private set; } //正极限 public bool NLimit { get; private set; } //负极限3.马达基本运行参数以及初始化 起始速度 加减速时间 #region 马达运行基本参数 private int StartSpeed; private double AccDecTime; public void InitPara(int _startSpd, double _accDecTime) { this.StartSpeed = _startSpd; this.AccDecTime = _accDecTime; }4. 自定义的属性(安全代码 运行中 目标位置 繁忙 定位到达) //安全代码 public int SafeCode { get; private set; } //运行中(自定义) public bool IsRunning { get; private set; } //目标位置 private int TargetPos; //忙(调用马达函数获取) private bool Busy; //定位到达 private bool Arrive;5. 创建马达实例(马达编号,脉冲当量 三个默认参数(感应器是否存在)) public MotorSys(int _axisNo, float _gain, bool orgExist = true, bool plimitExist = true, bool nlimitExist = true)6. 马达运行过程中需实时监测马达当前位置,安全信息,以及报警,是否到达极限等 //获取当前位置 private int GetPosPulse() //获取马达内部信号 极限 报警等 public void GetSignal() //实时检测马达运行安全 public int CheckSafe() { if (this.PLmtExist && this.PLimit) this.SafeCode = 21; else if (this.NLmtExist && this.NLimit) this.SafeCode = 22; else this.SafeCode = 0; return this.SafeCode; }7.马达实际运动方法大致可分为连续运动 相对距离运动 绝对定位 多轴插补 实时监测是否到位 减速停止等等 //复位 public void GoHome(int homeSpd, HomeDir dir) //连续运动 public void ContinueMove(int MoveSpd,MoveDir dir) //绝对定位 public void AbsGo(float pos, int moveSpd) //相对位移 public void RelGo(float dis, int moveSpd) //多轴插补 绝对定位 public static void MulAxisAbs(MotorSys[] motors, float[] pos, int moveSpd) //多轴插补 相对位移 public static void MulAxisRel(MotionSys[] motors, float[] dist, int moveSpd) //减速停止 public void DecelStop() |
CopyRight 2018-2019 实验室设备网 版权所有 |