工控网首页
>

应用设计

>

正运动技术运动控制卡应用开发教程之Python

正运动技术运动控制卡应用开发教程之Python

众所周知,Python作为一门面向对象的新兴开发语言,具有完善的基础代码库,实用性与代码可读性强,被越来越多的人应用于智能装备的运动控制

今天正运动技术与大家分享一下运动控制卡应用开发教程之Python。

在正式学习之前,我们先了解一下正运动技术的运动控制卡ECI2418和ECI2618。这两款产品分别是4轴,6轴运动控制卡。

image.png

ECI2418支持4轴脉冲输入与编码器反馈,板载24点输入,16点输出,2AD,2DA,支持手轮接口,其中特定输出支持高速PWM控制。

image.png

ECI2618支持6轴脉冲输入与编码器反馈,板载24点输入,16点输出,2AD,2DA,支持手轮接口,其中特定输出支持高速PWM控制。

image.png

ECI2418,ECI2618均使用同一套API函数,均支持C、C++、C#、LabView、Python、Delphi等开发语言,支持VC6.0、VB6.0、Qt、.Net等平台,支持Windows、Linux、WinCE、iMac等操作系统。

以下是Python语言的开发流程。

Python语言的使用环境

操作系统环境:win7_64x 

Python开发运行环境:PyCharm2019.2

Python解释器版本:Python 3.7.4(32bit)

1、新建项目。

打开Pycharm软件进行操作,点击Create New Project新建项目。

image.png

2、设置Python项目存放路径。

此过程主要包含三步:

选择Python项目→选择Python项目将存放的路径→创建Python项目。

image.png

3、新建Python文件。

在Python项目中新建Python文件,右键CratPython文件夹,选择“New→PythonFile”,创建新的Python 文件。

image.png

选择“New→Python File”

创建新的Python 文件

4、将Python动态库复制到Python项目中。

image.png

5、模块导入并加载动态链接库。

首先把Python中的两个模块导入(platform和ctypes模块),其中ctypes模块提供和C语言兼容的数据类型,能够很方便地调用动态链接库中输出的C接口函数。

import platform

import ctypes

#运行环境判断

systype = platform.system()

if systype == 'Windows':

    if platform.architecture()[0] == '64bit':

        zauxdll = ctypes.WinDLL('./zauxdll64.dll')

        print('Windows x64')

    else:

        zauxdll = ctypes.WinDLL('./zauxdll.dll')

        print('Windows x86')

        

elif systype == 'Darwin':

    zmcdll = ctypes.CDLL('./zmotion.dylib')

    print("macOS")

elif systype == 'Linux':

    zmcdll = ctypes.CDLL('./libzmotion.so')

    print("Linux")

else:

    print("Not Supported!!")

6、通过加载导入的动态库链接库,调用ZMotion PC函数手册中的函数。

1)使用操作。

首先根据控制器连接方式用连接函数连接控制器,输出控制器句柄,利用控制器的句柄我们就可以对库函数进行操作。

即“打开PC函数手册→搜索想要的函数功能→查看函数说明→通过刚才加载的动态链接库返回的zauxdll对象进行调用”。

image.png

2)通过ip连接函数接口返回的控制器句柄handle,对控制器的句柄handle操作。

image.png

#####控制器连接#####

    def connect(self, ip, console=[]):

        if self.handle.value is not None:

            self.disconnect()

        ip_bytes = ip.encode('utf-8')

        p_ip = ctypes.c_char_p(ip_bytes)

        print("Connecting to", ip, "...")

        ret = zauxdll.ZAux_OpenEth(p_ip, ctypes.pointer(self.handle))

        msg = "Connected"

        if ret == 0:

            msg = ip + " Connected"

            self.sys_ip = ip

            self.is_connected = True

        else:

            msg = "Connection Failed, Error " + str(ret)

            self.is_connected = False

        console.append(msg)

        console.append(self.sys_info)

        return ret

    # 断开连接

    def disconnect(self):

        ret = zauxdll.ZAux_Close(self.handle)

        self.is_connected = False

        return ret

3)轴参数设置。

#####轴参数设置####

    # 设置轴类型

    def set_atype(self, iaxis, iValue):

        ret = zauxdll.ZAux_Direct_SetAtype(self.handle, iaxis, iValue)

        if ret == 0:

            print("Set Axis (", iaxis, ") Atype:", iValue)

        else:

            print("Set Axis (", iaxis, ") Atype fail!")

        return ret

    # 设置脉冲当量

    def set_units(self, iaxis, iValue):

        ret = zauxdll.ZAux_Direct_SetUnits(self.handle, iaxis, ctypes.c_float(iValue))

        if ret == 0:

            print("Set Axis (", iaxis, ") Units:", iValue)

        else:

            print("Set Axis (", iaxis, ") Units fail!")

        return ret

    # 设置轴加速度

    def set_accel(self, iaxis, iValue):

        ret = zauxdll.ZAux_Direct_SetAccel(self.handle, iaxis, ctypes.c_float(iValue))

        if ret == 0:

            print("Set Axis (", iaxis, ") Accel:", iValue)

        else:

            print("Set Accel (", iaxis, ") Accel fail!")

        return ret

    # 设置轴减速度

    def set_decel(self, iaxis, iValue):

        ret = zauxdll.ZAux_Direct_SetDecel(self.handle, iaxis, ctypes.c_float(iValue))

        if ret == 0:

            print("Set Axis (", iaxis, ") Decel:", iValue)

        else:

            print("Set Axis (", iaxis, ") Decel fail!")

        return ret

    # 设置轴运行速度

    def set_speed(self, iaxis, iValue):

        ret = zauxdll.ZAux_Direct_SetAccel(self.handle, iaxis, ctypes.c_float(iValue))

        if ret == 0:

            print("Set Axis (", iaxis, ") Speed:", iValue)

        else:

            print("Set Axis (", iaxis, ") Speed fail!")

        return ret

4)轴参数读取。

#####轴参数读取####

    # 读取轴类型

    def get_atype(self, iaxis):

        iValue = (ctypes.c_int)()

        ret = zauxdll.ZAux_Direct_GetAtype(self.handle, iaxis, ctypes.byref(iValue))

        if ret == 0:

            print("Get Axis (", iaxis, ") Atype:", iValue.value)

        else:

            print("Get Axis (", iaxis, ") Atype fail!")

        return ret

    # 读取轴脉冲当量

    def get_units(self, iaxis):

        iValue = (ctypes.c_float)()

        ret = zauxdll.ZAux_Direct_GetUnits(self.handle, iaxis, ctypes.byref(iValue))

        if ret == 0:

            print("Get Axis (", iaxis, ") Units:", iValue.value)

        else:

            print("Get Axis (", iaxis, ") Units fail!")

        return ret

    # 读取轴加速度

    def get_accel(self, iaxis):

        iValue = (ctypes.c_float)()

        ret = zauxdll.ZAux_Direct_GetAccel(self.handle, iaxis, ctypes.byref(iValue))

        if ret == 0:

            print("Get Axis (", iaxis, ") Accel:",  iValue.value)

        else:

            print("Get Axis (", iaxis, ") Accel fail!")

        return ret

    # 读取轴减速度

    def get_decel(self, iaxis):

        iValue = (ctypes.c_float)()

        ret = zauxdll.ZAux_Direct_GetDecel(self.handle, iaxis, ctypes.byref(iValue))

        if ret == 0:

            print("Get Axis (", iaxis, ") Decel:",  iValue.value)

        else:

            print("Get Axis (", iaxis, ") Decel fail!")

        return ret

    # 读取轴运行速度

    def get_speed(self, iaxis):

        iValue = (ctypes.c_float)()

        ret = zauxdll.ZAux_Direct_GetSpeed(self.handle, iaxis, ctypes.byref(iValue))

        if ret == 0:

            print("Get Axis (", iaxis, ") Speed:",  iValue.value)

        else:

            print("Get Axis (", iaxis, ") Speed fail!")

        return ret

5)单轴运动。

 #####运动调用####

    # 直线运动

    def move(self, iaxis, iValue):

        ret = zauxdll.ZAux_Direct_Single_Move(self.handle, iaxis, iValue)

        if ret == 0:

            print("Axis (", iaxis, ") Move:",iValue)

        else:

            print("Axis (", iaxis, ") Move Fail")

        return ret

6)运行程序,输出结果。

调用封装好的函数→运行→输出程序运行结果。

#####功能使用####

zaux = ZMCWrapper()

# 连接控制器ip   默认192.168.0.11

zaux.connect("192.168.0.11")  

# 设置轴0参数

zaux.set_atype(0, 1)

zaux.set_units(0, 100)

zaux.set_accel(0, 1000)

zaux.set_decel(0, 1000)

zaux.set_speed(0, 1000)

# 获取轴0参数

zaux.get_atype(0)

zaux.get_units(0)

zaux.get_accel(0)

zaux.get_decel(0)

zaux.get_speed(0)

# 运动

zaux.move(0, 100)  # 轴0直线运动移动100

image.png

运行并输出程序运行结果

正运动技术运动控制卡应用开发教程之Python就分享到这里,更多精彩内容,请关注我们的公众号。

 

本文由正运动小助手原创,欢迎大家转载,共同学习,一起提高中国智能制造水平。文章版权归正运动技术所有,如有转载请注明文章来源。

审核编辑(
王静
)
投诉建议

提交

查看更多评论
其他资讯

查看更多

正运动技术自主研发运动控制实时软核MotionRT7软件安装流程

Windows实时运动控制软核(六):LOCAL高速接口测试之Matlab

Windows实时运动控制软核(五):LOCAL高速接口测试之VC6.0

Windows实时运动控制软核(四):LOCAL高速接口测试之VB.NET

Windows实时运动控制软核(三):LOCAL高速接口测试之C++