zl程序教程

您现在的位置是:首页 >  后端

当前栏目

【愚公系列】2023年04月 WPF运动控制平台-004.运动控制平台轴卡通用类的封装

WPF控制封装平台 系列 2023 愚公 04
2023-06-13 09:18:46 时间


一、运动控制平台轴卡通用类的封装

1.代码

1.1 参数

public struct AxisParameter
{
    public double acc; // 加速度
    public double dec; // 减速度
    public double velStart; // 启动速度
    public short smoothTime; // 平滑时间
    public int searchDistance; // 回原搜索距离
}

1.2 轴的通用功能接口

/// <summary>
/// 轴的通用功能接口
/// </summary>
public interface IAxisTool
{
    short AxisRest();

    /// <summary>
    /// 初始化轴卡
    /// </summary>
    /// <param name="axis">初始化轴号</param>
    /// <returns></returns>
    short AxisInit(short axis);
    /// <summary>
    /// 轴卡的绝对运动
    /// </summary>
    /// <param name="axis"></param>
    /// <param name="parameter"></param>
    /// <returns></returns>
    short AxisAbsMove(short axis, AxisParameter parameter, int pos, double vel);
    /// <summary>
    /// 轴卡的相对运动
    /// </summary>
    /// <param name="axis"></param>
    /// <param name="parameter"></param>
    /// <returns></returns>
    short AxisRelativeMove(short axis, AxisParameter parameter, int pos, double vel);
    /// <summary>
    /// 轴卡的回原
    /// </summary>
    /// <param name="axis"></param>
    /// <param name="parameter"></param>
    /// <returns></returns>
    void AxisGoHome(short axis, AxisParameter parameter, double vel, double homeVel);
    /// <summary>
    /// 获取规划位置
    /// </summary>
    /// <param name="axis"></param>
    /// <param name="encPos"></param>
    /// <returns></returns>
    short GetAxisPrfPos(short axis, out double prfPos);
    /// <summary>
    /// 获取规划的速度
    /// </summary>
    /// <param name="axis"></param>
    /// <param name="encPos"></param>
    /// <returns></returns>
    short GetAxisPrfVel(short axis, out double prfVel);
    /// <summary>
    /// 获取编码器反馈位置
    /// </summary>
    /// <param name="axis"></param>
    /// <param name="encPos"></param>
    /// <returns></returns>
    short GetAxisEncPos(short axis, out double encPos);
    /// <summary>
    /// 获取编码器反馈速度
    /// </summary>
    /// <param name="axis"></param>
    /// <param name="encPos"></param>
    /// <returns></returns>
    short GetAxisEncVel(short axis, out double encVel);

    /// <summary>
    /// 关闭轴卡功能
    /// </summary>
    /// <returns></returns>
    short CloseAxis();
    /// <summary>
    /// 停止对应轴功能
    /// </summary>
    /// <param name="axis"></param>
    /// <returns></returns>
    short StopAxis(short axis);
}

1.3 雷赛DMC3800轴卡

/// <summary>
/// 雷赛DMC3800轴卡的功能实现类
/// </summary>
public class DMC3800AxisTool
{
    public short AxisAbsMove(short axis, AxisParameter parameter)
    {
        Debug.WriteLine("雷赛DMC3800型号的相对运动功能");
        return 1;
    }

    public short AxisAbsMove(short axis, AxisParameter parameter, int pos, double vel)
    {
        throw new NotImplementedException();
    }

    public short AxisGoHome(short axis, AxisParameter parameter, int pos, double vel, double homeVel)
    {
        throw new NotImplementedException();
    }

    public short AxisInit(short axis)
    {
        throw new NotImplementedException();
    }

    public short AxisRelativeMove(short axis, AxisParameter parameter, int pos, double vel)
    {
        throw new NotImplementedException();
    }

    public short GetAxisEncPos(short axis, out double encPos)
    {
        throw new NotImplementedException();
    }

    public short GetAxisEncVel(short axis, out double encVel)
    {
        throw new NotImplementedException();
    }

    public short GetAxisPrfPos(short axis, out double prfPos)
    {
        throw new NotImplementedException();
    }

    public short GetAxisPrfVel(short axis, out double prfVel)
    {
        throw new NotImplementedException();
    }
}

1.4 三菱FX5U

/// <summary>
/// 三菱FX5U的运动控制功能实现类
/// </summary>
public class FX5UAxisTool
{
    public short AxisAbsMove(short axis, AxisParameter parameter)
    {
        Debug.WriteLine("三菱FX5U型号的相对运动功能");
        return 1;
    }

    public short AxisAbsMove(short axis, AxisParameter parameter, int pos, double vel)
    {
        throw new NotImplementedException();
    }

    public short AxisGoHome(short axis, AxisParameter parameter, int pos, double vel, double homeVel)
    {
        throw new NotImplementedException();
    }

    public short AxisInit(short axis)
    {
        throw new NotImplementedException();
    }

    public short AxisRelativeMove(short axis, AxisParameter parameter, int pos, double vel)
    {
        throw new NotImplementedException();
    }

    public short GetAxisEncPos(short axis, out double encPos)
    {
        throw new NotImplementedException();
    }

    public short GetAxisEncVel(short axis, out double encVel)
    {
        throw new NotImplementedException();
    }

    public short GetAxisPrfPos(short axis, out double prfPos)
    {
        throw new NotImplementedException();
    }

    public short GetAxisPrfVel(short axis, out double prfVel)
    {
        throw new NotImplementedException();
    }
}

1.5 固高GTS800

/// <summary>
/// 固高GTS800的运动控制器功能实现类
/// </summary>
public class GTS800AxisTool : IAxisTool
{
    bool flag= false;
    /// <summary>
    /// 复位轴卡的方法
    /// </summary>
    /// <returns></returns>
    public short AxisRest()
    {
        short rtn = GT_Reset();
        return rtn;
    }

    /// <summary>
    /// 轴的初始化功能
    /// </summary>
    /// <param name="axis"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public short AxisInit(short axis)
    {
        short rtn = 0;
        // 打开轴卡
        rtn = GT_Open(0, 1);
        if (rtn == 0) 
        {
            flag = true;
            // 加载轴卡配置
            rtn = GT_LoadConfig("GTS800-VIP-Home.cfg");
            // 清除轴卡的限位及报警状态
            rtn = GT_ClrSts(axis, 1);
            
        } else
        {
            flag = false;
        }

        return rtn;
    }
    /// <summary>
    /// 轴卡的绝对定位运动功能
    /// </summary>
    /// <param name="axis"></param>
    /// <param name="parameter"></param>
    /// <returns></returns>
    public short AxisAbsMove(short axis, AxisParameter parameter, int pos, double vel)
    {
        short rtn;
        if (flag) 
        {
            // 清除轴限位状态
            GT_ClrSts(axis, 1);
            // 指定规划轴为点位运动模式
            rtn = GT_PrfTrap(axis);
            // 设置点位运动参数
            TTrapPrm trap;
            trap.acc = parameter.acc;
            trap.dec= parameter.dec;
            trap.velStart= parameter.velStart;
            trap.smoothTime = parameter.smoothTime;
            rtn = GT_SetTrapPrm(axis, ref trap);
            // 设置目标位置
            rtn = GT_SetPos(axis, pos);
            // 设置目标速度
            rtn = GT_SetVel(axis, vel);
            // 启动点位运动
            rtn = GT_Update(1 << (axis -1));
        } else
        {
            rtn = -1111;
        }

        return rtn;
    }
    /// <summary>
    /// 轴卡的回原功能
    /// </summary>
    /// <param name="axis"></param>
    /// <param name="parameter"></param>
    /// <returns></returns>
    public void AxisGoHome(short axis, AxisParameter parameter, double vel, double homeVel)
    {
        uint clk;
        /*
            调用回原点指令,电机从所在位置以较高的速度往限位方向运动,如果
            碰到限位,则反方向运动并启动高速硬件捕获,在设定的搜索范围内搜索 Home,当触发 Home
            开关后,电机会以较低的速度运动到捕获的位置处(即 Home 开关)
         */

        // 以用户提供的距离进行点位运动搜索
        Task.Run(() =>
        {
            int searchDis = parameter.searchDistance;
            // 先获取界面输入搜索距离与偏移量
            searchDis = searchDis == 0 ? 805306368 : searchDis;
            int offset = 5000;
            // 获取当前运行的方向
            int direction = searchDis > 0 ? 1 : -1;

            TTrapPrm trap;
            trap.acc = parameter.acc;
            trap.dec = parameter.dec;
            trap.velStart = parameter.velStart;
            trap.smoothTime =parameter.smoothTime;
            AxisAbsMove(axis, parameter, searchDis, vel);

            int status;
            do
            {
                // 获取轴状态
                GT_GetSts(axis, out status, 1, out clk);
            } while ((status & 0x400) != 0);
            var limitType = direction == 1 ? "正" : "负";
            Debug.WriteLine($"触发{limitType}限位");
            / 反方向运行查找Home信号 //
            // 设置运行方向
            direction = -direction;
            // 清除轴限位状态
            GT_ClrSts(axis, 1);
            // 启动高速Home硬件捕获
            GT_SetCaptureMode(axis, CAPTURE_HOME);
            // 往反方向点位运动
            AxisAbsMove(axis, parameter, direction * 805306368, vel);

            short capture;
            int originPos;
            do
            {
                // 查询捕获Home的状态
                GT_GetCaptureStatus(axis, out capture, out originPos, 1, out clk);
            } while (capture == 0);
            // 触发Home后继续往前执行偏移量距离
            int newPos = originPos + direction * offset;
            // 点位运动到对应位置
            AxisAbsMove(axis,parameter, newPos, vel);
            // 等待运动到对应的位置
            do
            {
                // 获取轴状态
                GT_GetSts(axis, out status, 1, out clk);
            } while ((status & 0x400) != 0);

            // 清除轴限位状态
            GT_ClrSts(axis, 1);
            // 启动低速回原
            AxisAbsMove(axis, parameter,originPos, homeVel);
            double prfPos;
            do
            {
                // 获取规则位置:
                GT_GetPrfPos(axis, out prfPos, 1, out clk);
                // 获取轴状态
                GT_GetSts(axis, out status, 1, out clk);
            } while ((status & 0x400) != 0);

            if (prfPos == originPos)
            {
                // 设置机械原点
                GT_ZeroPos(axis, 1);

            }

        });
    }


    /// <summary>
    /// 轴卡的相对运动
    /// </summary>
    /// <param name="axis"></param>
    /// <param name="parameter"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public short AxisRelativeMove(short axis, AxisParameter parameter, int pos, double vel)
    {
        throw new NotImplementedException();
    }

    public short GetAxisEncPos(short axis, out double encPos)
    {
        short rtn = GT_GetEncPos(axis,out encPos, 1, out uint clk);
        return rtn;
    }

    public short GetAxisEncVel(short axis, out double encVel)
    {
        short rtn = GT_GetEncVel(axis, out encVel, 1, out uint clk);
        return rtn;
    }

    public short GetAxisPrfPos(short axis, out double prfPos)
    {
        short rtn = GT_GetPrfPos(axis, out prfPos, 1, out uint clk);
        return rtn;
    }

    public short GetAxisPrfVel(short axis, out double prfVel)
    {
        short rtn = GT_GetPrfVel(axis, out prfVel, 1, out uint clk);
        return rtn;
    }

    public short CloseAxis()
    {
        short rtn = GT_Close();

        return rtn;
    }

    public short StopAxis(short axis)
    {
        short rtn = GT_Stop(1 << (axis - 1), 0);

        return rtn;
    }
}

1.7 工厂类

/// <summary>
/// 轴的功能类
/// </summary>
public class AxisToolFactory
{
    /// <summary>
    /// 根据用户传递的轴卡或者PLC型号构建对应的实例对象
    /// </summary>
    /// <param name="type"></param>
    /// <returns></returns>
    public static IAxisTool CreateInstance(AxisType type)
    {
        IAxisTool axisTool = null;

        switch (type)
        {
            case AxisType.DMC3800:
                break;
            case AxisType.GTS800:
                axisTool = new GTS800AxisTool();
                break;
            case AxisType.FX5U:
                break;
            default:
                break;
        }

        return axisTool;
    }

}

public enum AxisType
{
    DMC3800,
    GTS800,
    FX5U
}