跳转至

连续点动、增量点动控制案例

1.新建项目并附加函数库

(1)在VS2017菜单“文件”→“新建”→ “项目”,启动创建项目向导。

image1

  1. 选择开发语言为“Visual C++”和程序类型“空项目”。
    image2

  2. 找到厂家提供的SDK库,路径如下(64位release库为例)。
    image3

    image4

函数库具体路径如下

image5

image6

  1. 复制【cnc_sdk】到项目目录。

image7

为了保证可以在VS调试器中运行,将相关平台的dll库复制到代码目录(如,项目名称为CncSdkDemo,则放在【{你的工程目录}\CncSdkDemo\CncSdkDemo】目录下),这里以64位平台为例

(将【{你的工程目录}\CncSdkDemo\CncSdkDemo\cnc_sdk\lib\x64】下的“CncApi.dll”和“CncApid.dll”复制到【{你的工程目录}\CncSdkDemo\CncSdkDemo\

image8

(5)进入项目的属性页面。

image9 在【VC++目录】选项中配置【包含目录】、【库目录】
image10 image11 image12 在【链接器】-【输入】中配置【附加依赖项】,需要用到CncApi.lib

image13 【确认】并【应用】
image14 2.编程

(1)【源文件】新建一个main.cpp
image15 image16 (2)查看函数手册,熟悉相关函数接口

image17 连接控制器接口:

函数名 功能
connect 连接控制器

运动参数配置相关接口:

函数名 功能
setEstop 该接口设置当前系统的急停状态
setEnable 该接口设置当前系统的使能状态
setFeedOverride 该接口设置当前系统进给率
stopProgram 停止当前系统正在运行的程序

点动控制相关接口:

函数名 功能
continuousJog 连续点动控制
incrementJog 增量点动控制

3.机械增量、连续点动运动

  1. 连接控制器
    void connect()
    {
        weconcnc::CCommApi *commApi = new weconcnc::CCommApi();
        std::string ipAddress = "192.168.54.98";
        uint16_t port = 9995;
        weconcnc::WECONCNC_ERROR_E result = commApi->connect(ipAddress, port);
    }
    
  2. 确保机械处于使能状态,即s返回值为4
    void updateMotionState()
    {
        weconcnc::CProxyStatus* pStatus = weconcnc::CProxyStatus::getInstance(pComm);
        weconcnc::WECONCNC_ERROR_E ret;
        weconcnc::MACHINE_STATE_E motionState;
        ret = pStatus->getMotionState(motionState);
        if (ret != weconcnc::WECONCNC_ERROR_SUCCESS) {
            std::cout << "get motion state failed!" << std::endl;
        }
        else {
            std::cout << "Motion State: " << motionState << std::endl;
        }
    }
    
    如果处于未使能状态,可以通过设置急停和使能方法改变状态
    void on_setEnableButton_clicked()
    {
        weconcnc::CProxyMotion *proxyMotion = weconcnc::CProxyMotion::getInstance(pComm);
        weconcnc::WECONCNC_ERROR_E ret = weconcnc::WECONCNC_ERROR_SUCCESS;
        ret = proxyMotion->setEnable(true);
    }
    
    void on_setEstopButton_clicked()
    {
        weconcnc::CProxyMotion *proxyMotion = weconcnc::CProxyMotion::getInstance(pComm);
        weconcnc::WECONCNC_ERROR_E ret = weconcnc::WECONCNC_ERROR_SUCCESS;
        bool bEnable = true;
        ret = proxyMotion->setEstop(bEnable);
    }
    
  3. 调用点动控制接口控制机械移动
    void on_Jog1Button_pressed()
    {
        weconcnc::CProxyMotion *proxyMotion = weconcnc::CProxyMotion::getInstance(pComm);
        weconcnc::WECONCNC_ERROR_E ret = weconcnc::WECONCNC_ERROR_SUCCESS;
        int32_t  nAxis = 0;
        int32_t dSpeed = 1000;
        double dDistance = 100;
    
        if (isIncrementMode == false){
            ret = proxyMotion->continuousJog(nAxis, dSpeed);
        } else {
            ret = proxyMotion->incrementJog(nAxis, dSpeed, dDistance);
        }
    }
    

4.使用Waux.h中的接口实现方式

  1. 声明用到的头文件(Waux.h)和定义控制器连接

    image18

  2. 相关函数接口

    (1)链接控制器,获取链接句柄。

指令 说明
WAux_OpenCom 串口连接控制器
WAux_OpenEth 以太网连接控制器
WAux_OpenPci PCI 卡连接
WAux_FastOpen 与控制器建立连接,指定连接的等待时间.

(2)点动相关接口。

指令 说明
WAux_Direct_Single_Move 增量点动
WAux_Direct_Single_Vmove 连续点动
  1. 增量连续点动示例
    #include <Waux.h>
    #include <winsock2.h>
    int main()
    {
        WSADATA wsaData;
        int iResult;
    
        /* 初始化 Winsock */
        iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
        if (iResult != 0) {
            printf("WSAStartup failed with error: %d\n", iResult);
        }
    
    
        char pIp[] = "192.168.54.98";
        WMC_HANDLE handle = NULL;
        int ret = WAux_OpenEth(pIp, &handle);
        if (ERR_SUCCESS != ret) {
            std::cout << "WAux_OpenEth failed!" << std::endl;
        }
        else {
            std::cout << "WAux_OpenEth success!" << std::endl;
        }
        int nAxis = 0;
        int idir = 0;
        bool isIncrementMode = false;
        float fdistance = 10;
        if (isIncrementMode == true) {
            ret = WAux_Direct_Single_Move(handle, nAxis, fdistance);
        }
        else {
            ret = WAux_Direct_Single_Vmove(handle, nAxis, idir);
        }
        if (ERR_SUCCESS != ret) {
            std::cout << "Move failed!" << std::endl;
        }
        else {
            std::cout << "Move success" << std::endl;
        }
    }