跳转至

安全机制配置接口应用案例

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 连接控制器

配置相关接口函数:

函数名 功能
setFsLimit 设置正软限位
getFsLimit 读取正软限位
setRsLimit 设置负软限位
getRsLimit 读取负软限位
getFhLimit 读取正硬限位
getRhLimit 读物负硬限位
setIniConfig 设置单个/多个ini配置的值
getIniConfig 获取指定/全部的ini设置数据
getCoord 该接口将读取当前坐标值

可以通过读取/设置限位相关函数对机械进行正负限位操作,也可以直接用setIniConfig函数来对机械进行正负限位、最大速度限制,并通过getIniConfig查看其值,以及通过getCoord函数监测实时坐标位置。

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. 正负限位设置相关示例:
    void limitSetting()
    {
        weconcnc::CProxyIO *pIO = weconcnc::CProxyIO::getInstance(commApi);
        int32_t nAxis = 0;
        float fsLimit = 1000;
        float RsLimit = -1000;
    
        pIO->setFsLimit(nAxis, fsLimit);
        pIO->setRsLimit(nAxis, RsLimit);
    
        weconcnc::WECONCNC_ERROR_E ret = pIO->getFsLimit(nAxis, fsLimit);
        if(ret != weconcnc::WECONCNC_ERROR_SUCCESS) {
            std::cout << "get fsLimit failed!" << std::endl;
        }
        else {
            std::cout << "fsLimit is " << fsLimit << std::endl;
        }
        ret = pIO->getRsLimit(nAxis, RsLimit);
        if(ret != weconcnc::WECONCNC_ERROR_SUCCESS) {
            std::cout << "get RsLimit failed!" << std::endl;
        }
        else {
            std::cout << "RsLimit is " << RsLimit << std::endl;
        }
    
        bool fhLimit = false;
        bool rhLimit = false;
        ret = pIO->getRhLimit(nAxis, fhLimit);
        if(ret != weconcnc::WECONCNC_ERROR_SUCCESS) {
            std::cout << "get fhLimit failed!" << std::endl;
        }
        else {
            std::cout << "fhLimit is " << fhLimit << std::endl;
        }
        ret = pIO->getRhLimit(nAxis, rhLimit);
        if(ret != weconcnc::WECONCNC_ERROR_SUCCESS) {
            std::cout << "get rhLimit failed!" << std::endl;
        }
        else {
            std::cout << "rhLimit is " << rhLimit << std::endl;
        }
    }
    
  3. Ini配置设置相关示例:

    函数定义:

    ①setIniConfig

    定义: WECONCNC_ERROR_E setIniConfig(int32_t nAxis, INI_CONFIG_FIELD eField, std::string &sParam);

    WECONCNC_ERROR_E setIniConfig(int32_t nAxis, std::vector> &vecIniConfig);

    说明: 设置单个 /多个ini配置的值。

    参数:【IN】int32_t nAxis,轴号

    INI_CONFIG_FIELD eField,请参见INI_CONFIG_FIELD定义

    std::string sParam,参数的值

    std::vector> vecIniConfig 多组期望设置的参数与对应的值。

    返回值: WECONCNC_ERROR_E,请参见WECONCNC_ERROR_E定义

    ②getIniConfig

    定义: WECONCNC_ERROR_E getIniConfig(int32_t nAxis, INI_CONFIG_FIELD eField, std::string &sParam);

    WECONCNC_ERROR_E getIniConfig(int32_t nAxis, CNC_INI_CONFIG_T &stIniConfig);

    说明: 获取指定/全部的ini设置数据。

    参数: 【IN】int32_t nAxis,轴号

    INI_CONFIG_FIELD eField,请参见INI_CONFIG_FIELD定义

    【OUT】std::string sParam,参数的值

    CNC_INI_CONFIG_T stIniConfig,请参见CNC_INI_CONFIG_T定义。

    返回值:WECONCNC_ERROR_E,请参见WECONCNC_ERROR_E定义

    void iniConfig()
    {
        weconcnc::CProxySys *pSys =  weconcnc::CProxySys::getInstance(commApi);
        int32_t nAxis = 0;
        weconcnc::INI_CONFIG_FIELD maxlimit = weconcnc::INI_MAX_LIMIT;
        weconcnc::WECONCNC_ERROR_E ret;
        std::string sParammax = "1000";
        ret = pSys->setIniConfig(nAxis, maxlimit, sParammax);
        if(weconcnc::WECONCNC_ERROR_SUCCESS != ret) {
            std::cout << "set inimaxlimit config failed!" << std::endl;
        }
        else {
            std::cout << "set inimaxlimit config success!" << std::endl;
        }
    
        weconcnc::INI_CONFIG_FIELD minlimit = weconcnc::INI_MIN_LIMIT;
        std::string sParammin = "-1000";
        ret = pSys->setIniConfig(nAxis, minlimit, sParammin);
        if (weconcnc::WECONCNC_ERROR_SUCCESS != ret) {
            std::cout << "set iniminlimit config failed!" << std::endl;
        }
        else {
            std::cout << "set iniminlimit config success!" << std::endl;
        }
    
        weconcnc::INI_CONFIG_FIELD maxvel = weconcnc::INI_MAX_VEL;
        std::string sParamvel = "500";
        ret = pSys->setIniConfig(nAxis, maxvel, sParamvel);
        if (weconcnc::WECONCNC_ERROR_SUCCESS != ret) {
            std::cout << "set inimaxvel config failed!" << std::endl;
        }
        else {
            std::cout << "set inimaxvel config success!" << std::endl;
        }
    
        sParammax = "";
        pSys->getIniConfig(nAxis, maxlimit, sParammax);
        std::cout << "ini config value is " << sParammax << std::endl;
    }
    
    4. 通过getCoord获取实时坐标:
    void updateCoord()
    {
        weconcnc::COORDINATE_T coord;
        weconcnc::CProxyStatus *pStatus = weconcnc::CProxyStatus::getInstance(commApi);
        weconcnc::WECONCNC_ERROR_E ret = pStatus->getCoord(coord);
    
        if (ret != weconcnc::WECONCNC_ERROR_SUCCESS) {
            std::cout << "get coord failed!" << std::endl;
        }
        else {
            std::cout << "Coordinates: X=" << coord.getX() << "," << " Y=" << coord.getY() << "," << 
                " Z=" << coord.getZ() << std::endl;
        }
    }
    

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

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

    image18

  5. 相关函数接口

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

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

(2)设置正负软硬限位相关接口。

指令 说明
WAux_Direct_GetFsLimit 设置正软限位
WAux_Direct_SetFsLimit 读取正软限位
WAux_Direct_GetRsLimit 设置负软限位
WAux_Direct_SetRsLimit 读取负软限位
WAux_Direct_GetFwdIn 读取正硬限位
WAux_Direct_GetRevIn 读取负硬限位
  1. 设置正负限位示例
    #include <Waux.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;
        }
    
        float fsLimit = 0.0f;
        ret = WAux_Direct_GetFsLimit(handle, 0, &fsLimit);
        if (ERR_SUCCESS != ret) {
            std::cout << "WAux_Direct_GetFsLimit failed!" << std::endl;
        }
        else {
            std::cout << "fsLimit is " << fsLimit << std::endl;
        }
    
        ret = WAux_Direct_SetFsLimit(handle, 0, 1500.0f);
        if (ERR_SUCCESS != ret) {
            std::cout << "WAux_Direct_SetFsLimit failed!" << std::endl;
        }
        else {
            std::cout << "WAux_Direct_SetFsLimit success!" << std::endl;
        }
    
        float RsLimit = 0.0f;
        ret = WAux_Direct_GetRsLimit(handle, 0, &RsLimit);
        if (ERR_SUCCESS != ret) {
            std::cout << "WAux_Direct_GetRsLimit failed!" << std::endl;
        }
        else {
            std::cout << "RsLimit is " << RsLimit << std::endl;
        }
    
        ret = WAux_Direct_SetFsLimit(handle, 0, -1500.0f);
        if (ERR_SUCCESS != ret) {
            std::cout << "WAux_Direct_SetFsLimit failed!" << std::endl;
        }
        else {
            std::cout << "WAux_Direct_SetFsLimit success!" << std::endl;
        }
    
        int FwdIn = 0;
        ret = WAux_Direct_GetFwdIn(handle, 0, &FwdIn);
        if (ERR_SUCCESS != ret) {
            std::cout << "WAux_Direct_GetFwdIn failed!" << std::endl;
        }
        else {
            std::cout << "FwdIn is " << FwdIn << std::endl;
        }
    
        int RevIn = 0;
        ret = WAux_Direct_GetRevIn(handle, 0, &RevIn);
        if (ERR_SUCCESS != ret) {
            std::cout << "WAux_Direct_GetRevIn failed!" << std::endl;
        }
        else {
            std::cout << "RevIn is " << RevIn << std::endl;
        }
    }