软限位设置例程

软限位设置例程
// SoftLimit.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

// 该例程仅用于功能演示,请保证安全的情况下使用

// 测试功能:软限位配置示例
// 测试平台:网络型运动控制器
// 测试环境:Windows
// 测试流程:
//           (1)初始化控制器
//           (2)配置软限位
//           (3)运动轴到查过软限位的目标位置
//           (4)等待轴软限位触发运动停止
//           (5)运动完成,关闭控制器
// 注意事项:
//           (1)本例程使用的“例程专用.xml”、“例程专用.cfg”,仅用于本例程
//           (2)实际使用时,需要使用MotionStudio生成网络配置xml

// 加载固高运动控制库头文件
#include "gxn.h"
// 动态加载固高运动控制gxn.lib库
#pragma comment(lib,"gxn.lib")

/**
 * @brief 指令出错打印函数
 * @param command 打印信息字符串
 * @param error 错误码
 * @return 错误码
*/
short CommandHandler(char* command, short error)
{
    printf("%s = %d\n", command, error);
    getchar();

    return error;
}

/**
 * @brief 初始化运动控制器(开卡 + 初始化网络拓扑 + 初始化轴)
 * @param core 需要初始化的核号,从1开始
 * @param axis 需要初始化的轴起始索引,从1开始
 * @param axisCount 需要初始化的轴数量,从起始索引axis开始计数,必须大于0
 * @return 0表示初始化成功,非0表示初始化失败
*/
short InitMc(short core,short axis,short axisCount)
{
    short rtn;
    short overTime;
    long status;

    // 打开运动控制器
    rtn = GTN_OpenCard(CHANNEL_PCIE,NULL,NULL);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_OpenCard",rtn);
    }
    printf("Open Card Success !\n");

    // 初始化网络
    // 注意:(1)“例程专用.xml”仅用于本例程
    //       (2)实际使用时,需要使用MotionStudio生成对应的网络配置文件
    // overTime:网络初始化超时时间,单位:秒
    overTime = 120;
    rtn = GTN_NetInit(NET_INIT_MODE_XML_STRICT,"例程专用.xml",overTime,&status);
    if ( 0 != rtn )
    {
        printf("status = %d\n",status);
        return CommandHandler("GTN_NetInit",rtn);
    }
    printf("Init Net Success !\n");

    // 加载配置文件到控制器
    // 注意:(1)“例程专用.cfg”仅用于本例程
    //       (2)实际使用时,需要使用MotionStudio生成对应的配置文件
    rtn = GTN_LoadConfig(core,"例程专用.cfg");
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_LoadConfig(\"例程专用.cfg\")",rtn);
    }

    // 清除轴状态
    rtn = GTN_ClrSts(core,axis,axisCount);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_ClrSts",rtn);
    }
    printf("Init Mc Config Success !\n");

    return rtn;
}

/**
 * @brief 启动轴点位运动到目标位置
 * @param core 核号,从1开始
 * @param profile 规划器索引,从1开始
 * @param targetVel 目标速度
 * @param targetPos 目标位置
 * @return 0表示成功,非0表示失败
*/
short TrapMove(short core,short profile,double targetVel,long targetPos)
{
    short rtn;
    TTrapPrm trap;
    memset(&trap,0,sizeof(trap));

    rtn = GTN_PrfTrap(core,profile);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_PrfTrap",rtn);
    }

    trap.acc = 10;
    trap.dec = 20;
    trap.velStart = 0;
    trap.smoothTime = 0;
    rtn = GTN_SetTrapPrm(core,profile,&trap);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_SetTrapPrm",rtn);
    }

    rtn = GTN_SetVel(core,profile,targetVel);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_SetVel",rtn);
    }

    rtn = GTN_SetPos(core,profile,targetPos);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_SetPos",rtn);
    }

    rtn = GTN_Update(core,1<<(profile-1));
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_Update",rtn);
    }

    return rtn;
}

int _tmain(int argc, _TCHAR* argv[])
{
    short rtn;                         // 指令返回值
    short core;                        // 需要执行例程的运动控制器核号
    short axis;                        // 需要初始化的轴起始索引号
    short axisCount;                   // 需要初始化的轴数量,从轴起始索引号开始算起

    // 初始化运动控制器
    // 开卡 + 初始化网络拓扑 + 初始化核1的1-8轴
    core = 1;
    axis = 1;
    axisCount = 8;
    rtn = InitMc(core,axis,axisCount);
    if ( 0 != rtn )
    {
        return rtn;
    }

    // 轴位置清零
    axis = 1;                          // 需要配置软限位的轴
    rtn = GTN_ZeroPos(core,axis);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_ZeroPos",rtn);
    }

    double positiveLimit;              // 轴正向软限位位置,单位:脉冲
    double negativeLimit;              // 轴负向软限位位置,单位:脉冲

    positiveLimit = 10000;
    negativeLimit = -10000;
    rtn = GTN_SetSoftLimitEx(core,axis,positiveLimit,negativeLimit);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_SetSoftLimitEx",rtn);
    }

    rtn = GTN_ClrSts(core,axis);
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_ClrSts",rtn);
    }

    double targetVel;                  // 轴点位运动目标速度
    long targetPos;                    // 轴点位运动目标位置

    targetVel = 10;
    targetPos = positiveLimit + 1000;
    rtn = TrapMove(core,axis,targetVel,targetPos);
    if ( 0 != rtn )
    {
        return rtn;
    }

    long sts;
    double prfPos;
    TLimitInfo limitInfo;
    do 
    {
        rtn =  GTN_GetSts(core,axis,&sts,1,0);
        rtn += GTN_GetPrfPos(core,axis,&prfPos,1,0);
        if ( 0 != rtn )
        {
            return CommandHandler("GTN_GetSts",rtn);
        }

        printf("Axis:%d status:0x%#x prfPos:%.3lf \r",axis,sts,prfPos);
    } while ( 0x400 == (sts&0x400) );

    printf("\nMotion Down !\n");

    rtn = GTN_GetLimitInfo(core,axis,&limitInfo);
    if ( 0 == rtn )
    {
        printf("hwLmtPositiveEnable:%d\n",limitInfo.hwLmtPositiveEnable);
        printf("hwLmtNegativeEnable:%d\n",limitInfo.hwLmtNegativeEnable);
        printf("swLmtPositiveEnable:%d\n",limitInfo.swLmtPositiveEnable);
        printf("swLmtNegativeEnable:%d\n",limitInfo.swLmtNegativeEnable);
        printf("hwLmtPositiveStatus:%d\n",limitInfo.hwLmtPositiveStatus);
        printf("hwLmtNegativeStatus:%d\n",limitInfo.hwLmtNegativeStatus);
        printf("swLmtPositiveStatus:%d\n",limitInfo.swLmtPositiveStatus);
        printf("swLmtNegativeStatus:%d\n",limitInfo.swLmtNegativeStatus);
    }

    // 关闭控制器
    rtn = GTN_Close();
    if ( 0 != rtn )
    {
        return CommandHandler("GTN_Close",rtn);
    }

    printf("Press Any Key To Exit !\n");
    getchar();

    return 0;
}