固高卡使用
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
提示:这里可以添加本文要记录的大概内容:
固高GTS脉冲控制卡,函数封装类
提示:以下是本篇文章正文内容,下面案例可供参考
一、GTS400
固高脉冲卡DELL封装
二、使用步骤
1.头文件.h
代码如下(示例):
#ifndef GTSCTRL_H
#define GTSCTRL_H
#include <QObject>
#include "gts.h"
#include <QMutex>
#define GTS_RATE 1
#define AxisCount 2
#define gtsApp (GTSCtrl::getInstance())
//错误定义
enum class MotionError
{
SUCCESS = 0,//操作成功
MOTION_FAILED = 1,//操作失败
MOTION_STOPED = 2,//运动停止
MOTION_RUNNING = 3,//正在运动
MOTION_INIT_MOTION_FAILED = 4,//初始化失败
LOAD_CONFIG_FAILED = 5,//载入配置文件失败
EXCEPTION = 6//运行异常
};
enum class CAPTURE
{
HOME = 1,//Home信号
INDEX = 2//Index信号
};
class GTSCtrl : public QObject
{
Q_OBJECT
public:
GTSCtrl(QObject *parent=0);
~GTSCtrl();
static GTSCtrl* getInstance();
//轴回零状态
QList<bool> m_AxisHomeFlag = {false,false};
// 伺服使能
void gts_AxisOn(int axisIndex);
// 伺服关闭
void gts_AxisOff(int axisIndex);
// 带位置比较输出模式的绝对位移
void gts_AbsMoveWithCompare(int axisIndex, long aimPos/*绝对位置p*/, long vel/*运动速度(p/ms)*/,long PInterVal/*脉冲间隔,像素当量(脉冲/像素)*/,short fwith/*脉宽*/,bool block=true);
// 带位置比较输出模式的绝对位移
void gts_AbsMoveWithCompare(int axisIndex, long aimPos/*绝对位置p*/, long vel/*运动速度(p/ms)*/,long acc/*加速度(p/ms)*/, long PInterVal/*脉冲间隔,像素当量(脉冲/像素)*/, short fwith/*脉宽*/, bool block = true);
//位置清零
void gts_PosZero(int axisIndex);
//轴规划位置
void gts_SetPrfPos(int axisIndex,long axisPos);
//将轴设为点位模式 acc dac p/ms2
void gts_ProTrap(int axisIndex, double acc = 10, double dac = 10);
//将轴设为点位模式 acc dac p/ms2
void gts_ProTrap(int axisIndex, double velStart, double acc, double dac);
//读取点位运动参数
void gts_GetTrapPrm(int axisIndex,TTrapPrm* prm);
// 设置AXIS轴的目标位置
void gts_SetPos(int axisIndex,long posValue);
//获取轴的当前位置
void gts_GetPos(int axisIndex,long* posValue);
// 设置AXIS轴的目标速度
void gts_SetVel(int axisIndex,int velValue);
//启动AXIS轴的运动
void gts_Start(int axisIndex);
//停止AXIS轴的运动
void gts_Stop(int axisIndex);
// 读取AXIS轴的状态
void gts_GetSts(int axisIndex,long* sts);
// 读取AXIS轴的规划位置
void gts_GetPrfPos(int axisIndex,double* prfPos);
//AXIS轴是否规划停止
bool gts_IsPrfStop(int axisIndex);
//AXIS绝对位移 vel单位 pause/s 每秒脉冲个数
void gts_AbsMove(int axisIndex, double aimPos, double vel, double acc, bool block=false);
//设置AXIS加减速、速度
void gts_SetAccDec(int axisIndex,double acc,double dec);
//AXIS JOG dir==0 正向 dir==1反向 vel单位 pause/s 每秒脉冲个数
void gts_JogMove(int axisIndex, double vel,int dir);
//设置输出为有效电平
bool gts_SetOutputValid(int channel,int validValue=0);
//设置输出为无效电平
bool gts_SetOutputInValid(int channel,int invalidValue=1);
//检测输入信号是否有效,控制卡默认低电平有效
bool gts_IsInputValid(int channel, int validValue = 0, int Noise=50);
//拓展io输入有效 默认低电平有效
bool gts_isOn(int channel/*start 0*/,int exmdlIndex = 0,int validValue = 0, int Noise=5/*防噪声*/);
//拓展io输出 默认低电平有效
bool gts_SetOn(int channel, int exmdlIndex = 0, int validValue = 0);
//默认高电平有效
bool gts_SetOff(int channel, int exmdlIndex = 0, int validValue = 1);
//读取当前通用输出点状态,有效返回true 无效返回false
bool gts_GetGPO(int channel, int validValue=0);
//读取当前glink200输出点状态
bool gts_GetGlinkO(int channel, int exmdlIndex=0, int validValue =0);
//监控输入信号是否超时(输入通道,超时时间ms,有效电平0 1)
bool gts_IsInputTimeOut(int channel,int timeout=500,int validValue=0);
//AXIS回原点,返回值0-回原点成功;1-未找到原点;2-移动到原点失败
int gts_Home(int axisIndex);
//AXIS智能回原点
void gts_SmartHome(int axisIndex,long serachDistance);
//多轴智能回原点
void gts_2AxisMultiSmartHome(int *axisArr, long *serchDistanceArr);
void gts_3AxisMultiSmartHome(int *axisArr, long *serchDistanceArr);
//伺服电机运动到位
bool gts_ServoArrive(int axisIndex);
//多轴运动到位
bool gts_ServoArrive2(int axisIndex,int validValue=0);
//show errorMsg
void showErrorMsg(int axisIndex,QString errInfo);
//伺服限位报警,默认上升沿有效 type=0正限位 type=1 负限位
bool gts_LimitAlarm(int axisIndex,int type);
//伺服启动器报警,默认低电平有效
bool gts_ServoAlarm(int axisIndex);
// 清除故障报警状态
void gts_ClrSts(int axisIndex);
void gts_InCreMove(int axisIndex, double pauseNum/*要走的脉冲数*/, double vel,double acc,double dac);
void gts_InCreMove(int axisIndex, double pauseNum/*要走的脉冲数*/,
double velstart, double vel, double acc, double dac);
//设置pid
void setPID(int axisIndex,TPid pid);
//自动对角到位
bool gts_AutoFocusOK(int axis);
//*****************************************************************************
//轴卡状态
short gts_SUCCESS = 0;
//jog运动变量声明
TJogPrm jog;
///初始化运动控制卡
/// </summary>
/// <returns></returns>
MotionError InitCard();
/// <关闭运动控制卡>
/// </summary>
/// <returns></returns>
MotionError DeInitCard();
/// <获取指定控制器的锁存器的标志位>
/// // 指定控制器的锁存器的标志位
/// </summary>
/// <param name="axis">轴</param>
/// <param name="zero_flag">原点锁存器标志位</param>
/// <returns></returns>
MotionError GetEzZeroFlag(int axis, bool *zero_flag);
/// <以恒定的速度运动>
/// 以恒定的速度运动
/// </summary>
/// <param name="axis">轴号</param>
/// <param name="servo_velocity">速度</param>
/// <returns></returns>
MotionError StartAxisVelocityMotion(int axis, double servo_velocity);
/// <以恒定的速度运动>
/// 以恒定的速度运动
/// </summary>
/// <param name="axis">轴号</param>
/// <param name="servo_velocity">速度</param>
/// <param name="servo_acc">加速度</param>
/// <returns></returns>
MotionError StartAxisVelocityMotion(int axis, double servo_velocity, double servo_acc);
/// <单轴相对运动>
/// 单轴相对运动
/// </summary>
/// <param name="axis">轴号</param>
/// <param name="servo_distance">运动到的点位</param>
/// <param name="servo_velocity">运动的速度</param>
/// <returns></returns>
MotionError StartAxisDistanceMotion(int axis, double servo_distance, double servo_velocity);
/// <单轴绝对运动>
/// 单轴绝对运动
/// </summary>
/// <param name="axis">轴号</param>
/// <param name="servo_position">坐标</param>
/// <returns></returns>
MotionError StartAxisAbsMotion(int axis, int servo_position, double servo_velocity);
/// <单轴绝对运动>
/// 单轴绝对运动
/// </summary>
/// <param name="axis">轴号</param>
/// <param name="servo_position">位置</param>
/// <param name="servo_velocity">速度</param>
/// <param name="acc_time">加速度单位pulse/ms2</param>
/// <returns></returns>
MotionError StartAxisAbsMotion(int axis, int servo_position, double servo_velocity, double acc);
/// <单轴相对运动(指定点与加速)>
/// 单轴相对运动(指定点与加速)
/// </summary>
/// <param name="axis">轴号</param>
/// <param name="servo_distance">运动距离</param>
/// <param name="servo_velocity">运动速度</param>
/// <param name="acc_time">运动加速度</param>
/// <returns></returns>
MotionError StartAxisDistanceMotion(int axis, double servo_distance, double servo_velocity, double acc_time);
/// <单轴相对运动(指定点与加速)>
/// 单轴相对运动(指定点与加速)
/// </summary>
/// <param name="axis">轴号</param>
/// <param name="servo_distance">运动距离</param>
/// <param name="encode_pos">当前脉冲位置</param>
/// <param name="servo_velocity">运动速度</param>
/// <param name="acc_time">运动加速度</param>
/// <returns></returns>
MotionError StartAxisDistanceMotionWithCurPos(int axis, double servo_distance,double *encode_pos, double start_velocity, double servo_velocity, double acc_time);
/// <减速制动>
/// 开始减速直至减到起始速度后,停止运行
/// </summary>
/// <param name="axis">轴号</param>
/// <returns></returns>
MotionError DelStop(int axis);
/// <紧急制动>
/// 立即停止指定的运行轴,没有任何减速过程
/// </summary>
/// <param name="axis">轴号</param>
/// <returns></returns>
MotionError ImdStop(int axis);
/// <设置轴的规划位置位置脉冲值>
/// 设置控制器发出的指令计数器脉冲位置值
/// </summary>
/// <param name="axis">轴号</param>
/// <param name="pulse_command">脉冲</param>
/// <returns></returns>
MotionError SetCommandPosition(int axis, int pulse_command);
/// <获取轴的规划位置位置脉冲值>
/// 获取控制器发出的指令计数器脉冲位置值
/// </summary>
/// <param name="axis">轴号</param>
/// <param name="pulse_command">脉冲</param>
/// <returns></returns>
MotionError GetCommandPosition(int axis, double *pulse_command);
/// <设置当前位置脉冲值>
/// 设置外部机械反馈的计数器中脉冲位置值,该值是经过运动变比的数值
/// </summary>
/// <param name="axis">轴号</param>
/// <param name="encoder_position">脉冲位置</param>
/// <returns></returns>
MotionError SetEncoderPosition(int axis, int encoder_position);
/// <获取当前所处位置的脉冲值>
/// 获取当前所处位置的脉冲值
/// </summary>
/// <param name="axis">轴号</param>
/// <param name="encoder_position">脉冲位置</param>
/// <returns></returns>
MotionError GetEncoderPosition(int axis, double *encoder_position);
/// <获取轴状态>
/// 获取轴状态
/// </summary>
/// <param name="axis">轴号</param>
/// <param name="alarm_on">伺服报警信号</param>
/// <param name="positive_limit_on">正限位报警</param>
/// <param name="negative_limit_on">负限位报警</param>
/// <returns></returns>
MotionError GetAxisSate(int axis, bool *alarm_on, bool *positive_limit_on, bool *negative_limit_on);
/// <清除报警>
/// 清除报警
/// </summary>
/// <returns></returns>
MotionError ClearAxisSate();
/// <获取轴急停状态>
/// 获取轴急停状态
/// </summary>
/// <param name="emg_on">急停标志位</param>
/// <returns></returns>
MotionError GetEmgSate(int axis,bool *emg_on);
/// <获取轴运动完成状态>
/// 获取轴运动完成状态
/// </summary>
/// <param name="axis">轴号</param>
/// <returns></returns>
MotionError GetAxisMotionDoneState(int axis);
/// <获取轴运动编码器完成状态>
/// 获取轴运动编码器完成状态
/// </summary>
/// <param name="axis">轴号</param>
/// <returns></returns>
MotionError GetAxisEncMotionDoneState(int axis);
/// <设置捕获方式>
///
/// <param name="ez_logic">捕获方式</param>
/// <returns></returns>
MotionError SetEZMode(int axis, CAPTURE capture);
/// <获取原点信号的位置>
/// 获取原点信号的位置
/// </summary>
/// <param name="aixs">轴号</param>
/// <param name="Postion">位置</param>
/// <returns></returns>
MotionError GetEZMode(int axis, int *Postion);
/// <位置清零>
/// 位置清零
/// </summary>
/// <param name="axis">位置清零的起始轴号</param>
/// <param name="count">轴数</param>
/// <returns></returns>
MotionError ZeroPos(int axis, int count);
/// <设置和启动位置比较>
/// 设置启动位置比较
/// </summary>
/// <param name="axis">轴号</param>
/// <param name="startPos">起始位置</param>
/// <param name="repeatTime">重复次数</param>
/// <param name="inverval">间隔</param>
/// <returns></returns>
MotionError SetCompareData(int axis, int startPos, int repeatTime, int inverval);
/// <停止启动位置比较>
/// 停止启动位置比较
/// </summary>
/// <returns></returns>
MotionError StopCompare();
/// <获取位置比较输出个数>
/// 获取位置比较输出个数
/// </summary>
/// <param name="nStatus">0表示位置输出已完成,1表示还在进行</param>
/// <param name="nNumber">返回位置比较输出个数</param>
/// <returns></returns>
MotionError GetCompareData(short *nStatus, int *nNumber);
/// <清楚捕获信号>
/// 清楚捕获信号
/// </summary>
/// <param name="axis">轴号</param>
/// <returns></returns>
MotionError ClearEzFlag(int axis);
/// <获取输入信号>
/// 获取输入信号
/// </summary>
/// <param name="axis">轴号</param>
/// <returns></returns>
MotionError GetDiFlag(int diIndex, short diType, bool *Di_flag);
/// <设置高速口IO输出高低电平>
/// 设置高速口IO输出高低电平
/// </summary>
/// <param name="channel">输出的通道:1或者2</param>
/// <param name="level">电平状态,0或1</param>
///<param name="pulseType">输出电平或是脉冲,0为脉冲,1为电平</param>
///<param name="time">输出电平此参数无效,输出脉冲此参数是脉宽</param>
/// <returns></returns>
MotionError SetHighSpeedIO(short channel, short level, short pulseType, short time = 0);
/// <设置DAC12输出高低电平>
/// 设置DAC12输出高低电平
/// </summary>
/// <param name="level">电平状态,0为低电平或1为高电平</param>
/// <returns></returns>
MotionError SetDACLevel(short level);
/// <设置轴到位误差带>
/// 设置轴到位误差带
/// </summary>
/// <param name="axis">轴号</param>
/// <param name="band">误差带脉冲大小,单位:pulse</param>
/// <param name="time">误差带保持时间,单位:us</param>
/// <returns></returns>
MotionError SetAxisStopBand(short axis,int band,int time);
/// <获取轴到位误差带>
/// 获取轴到位误差带
/// </summary>
/// <param name="axis">轴号</param>
/// <param name="band">误差带脉冲大小,单位:pulse</param>
/// <param name="time">误差带保持时间,单位:us</param>
/// <returns></returns>
MotionError GetAxisBand(short axis, int* band, int* time);
/// <获取轴到位状态>
/// 获取轴到位状态
/// </summary>
/// <param name="axis">轴号</param>
/// <returns></returns>
MotionError GetAxisMotionArrivedState(int axis);
/// <设置输出信号>
/// 设置输出信号
/// </summary>
/// <param name="doIndex">输出信号的点位</param>
/// <returns></returns>
MotionError SetDoFlag(short doIndex, short doType, bool Do_flag);
/// <获取输出信号>
/// 获取输出信号
/// </summary>
/// <param name="doIndex">信号的点位</param>
/// <returns></returns>
MotionError GetDoFlag(short doIndex, short doType, bool *Do_flag);
/// <获取轴运动完成>
/// 获取轴运动完成
/// </summary>
/// <param name="axis">轴号</param>
/// <param name="targetPos">目标位置</param>
/// <returns></returns>
MotionError GetAxisRunDistanceDone(int axis, double targetPos);
/// <设置发送帧信号>
/// 获取轴运动完成
/// </summary>
/// <param name="targetPos">发出帧信号目标位置</param>
/// <returns></returns>
MotionError SetFrameSignal(double targetPos);
private:
static GTSCtrl* m_pInstance;
static QMutex m_mutex;
short m_bFlagAlarm= false; // 伺服报警标志
short m_bFlagMError= false; // 跟随误差越限标志
short m_bFlagPosLimit= false; // 正限位触发标志
short m_bFlagNegLimit = false; // 负限位触发标志
short m_bFlagSmoothStop = false; // 平滑停止标志
short m_bFlagAbruptStop = false; // 急停标志
short m_bFlagServoOn = false; // 伺服使能标志
short m_bFlagMotion = false; // 规划器运动标志
int m_nHomeVel;//home 速度
double m_curPos = 0;//当前位置
long m_lastAimPos=0; //目标位置
};
#endif // GTSCTRL_H
2.执行文件
代码如下(示例):
#include "gtsctrl.h"
#include <QDebug>
#include <QMessageBox>
#include <QThread>
#include <QTime>
#include "ExtMdl.h"
#include <QCoreApplication>
#include <QSettings>
#include "qapplication.h"
#include "qmath.h"
#pragma execution_character_set("utf-8")
#define SEARCH_HOME -200000000
#define HOME_OFFSET -2000
GTSCtrl* GTSCtrl::m_pInstance = NULL;
QMutex GTSCtrl::m_mutex;
void commandhandler(char *command, short error)
{
// 如果指令执行返回值为非0,说明指令执行错误,向屏幕输出错误结果
if (error)
{
//qDebug() << command << "=" << error;
}
}
GTSCtrl::GTSCtrl(QObject *parent)
: QObject(parent)
{
QString filePath = QCoreApplication::applicationDirPath() + "/System/Machine.ini";
QSettings moduleSetting(filePath, QSettings::IniFormat);
moduleSetting.setIniCodec("GB2312");
m_nHomeVel = moduleSetting.value("GTS/homeVel", 2).toInt();
//初始化板卡
short sRtn;
// 打开运动控制器
sRtn = GT_Open();
commandhandler("GT_Open", sRtn);
// 复位运动控制器
//sRtn = GT_Reset();
//commandhandler("GT_Reset", sRtn);
// 配置运动控制器
// 注意:配置文件取消了各轴的报警和限位
QString path = QCoreApplication::applicationDirPath() + "/GTS800.cfg";
QByteArray tmp = path.toLocal8Bit();
char* charPath = tmp.data();
sRtn = GT_LoadConfig(charPath);
commandhandler("GT_LoadConfig ", sRtn);
QThread::msleep(50);
// 清除各轴的报警和限位
sRtn = GT_ClrSts(1, 8);
commandhandler("GT_ClrSts", sRtn);
//关闭Z轴刹车
SetDoFlag(1,MC_GPO,false);
上电需要保持DAC11通道有5V电压,不然相机端不能接收信号
//short tmp = 16384;
tmp = 32767;
tmp = 0;
//GT_SetDac(11, &tmp);
//以下为io拓展模块操作
//连接IO扩展模块
//sRtn = GT_OpenExtMdl("gts.dll");
//commandhandler("GT_OpenExtMdl", sRtn);
//加载配置文件,配置IO扩展模块
//sRtn = GT_LoadExtConfig("extmdl.cfg");
//commandhandler("GT_LoadExtConfig", sRtn);
/*
TPid pid;
// 配置轴2为模拟量输出模式
sRtn = GT_CtrlMode(2, 0);
// 配置轴2报警输出无效
//sRtn = GT_AlarmOff(2);
// 配置轴1正负限位无效
//sRtn = GT_LmtsOff(2, -1);
// 配置完成后要更新状态
sRtn = GT_ClrSts(2);
// 获取当前pid参数
sRtn = GT_GetPid(1, 2, &pid);
// 调试阶段,只需修改一下kp到一个较小的值
//pid.kp = 0.15;
pid.kp = 2;
pid.kd = 2;
// 设置PID参数
sRtn = GT_SetPid(1, 2, &pid);
*/
SetAxisStopBand(1, 20, 1);
SetAxisStopBand(2, 10, 1);
short dac11 = 16384;
short dac12 = 0;
GT_SetDac(12, &dac12);
GT_SetDac(11, &dac11);
dac11 = 0;
dac11 = 32767;
}
GTSCtrl::~GTSCtrl()
{
//打开Z轴刹车
SetDoFlag(1, MC_GPO, true);
}
GTSCtrl* GTSCtrl::getInstance()
{
if (m_pInstance == NULL)
{
m_pInstance = new GTSCtrl();
}
return m_pInstance;
}
void GTSCtrl::gts_AxisOn(int axisIndex)
{
short sRtn;
sRtn = GT_AxisOn(axisIndex);
commandhandler("gts_AxisOn",sRtn);
}
void GTSCtrl::gts_PosZero(int axisIndex)
{
short sRtn;
sRtn = GT_ZeroPos(axisIndex);
commandhandler("gts_PosZero", sRtn);
}
void GTSCtrl::gts_SetPrfPos(int axisIndex, long axisPos)
{
short sRtn;
sRtn = GT_SetPrfPos(axisIndex,axisPos);
commandhandler("gts_SetPrfPos", sRtn);
}
void GTSCtrl::gts_ProTrap(int axisIndex, double acc, double dac)
{
short sRtn;
sRtn = GT_PrfTrap(axisIndex);
commandhandler("gts_ProTrap", sRtn);
TTrapPrm prm;
sRtn = GT_GetTrapPrm(axisIndex, &prm);
prm.acc = acc;
prm.dec = dac;
prm.velStart = 0;
prm.smoothTime = 20;
sRtn = GT_SetTrapPrm(axisIndex,&prm);
}
void GTSCtrl::gts_ProTrap(int axisIndex, double velStart,double acc, double dac)
{
short sRtn;
sRtn = GT_PrfTrap(axisIndex);
commandhandler("gts_ProTrap", sRtn);
TTrapPrm prm;
sRtn = GT_GetTrapPrm(axisIndex, &prm);
prm.acc = acc;
prm.dec = dac;
prm.velStart = velStart;
prm.smoothTime = 10;
sRtn = GT_SetTrapPrm(axisIndex, &prm);
}
void GTSCtrl::gts_GetTrapPrm(int axisIndex, TTrapPrm* prm)
{
short sRtn;
sRtn = GT_GetTrapPrm(axisIndex,prm);
commandhandler("gts_GetTrapPrm", sRtn);
}
void GTSCtrl::gts_SetPos(int axisIndex, long posValue)
{
short sRtn;
sRtn = GT_SetPos(axisIndex, posValue);
commandhandler("gts_SetPos", sRtn);
}
void GTSCtrl::gts_SetVel(int axisIndex, int velValue)
{
short sRtn;
sRtn = GT_SetVel(axisIndex, velValue);
commandhandler("gts_SetVel", sRtn);
}
void GTSCtrl::gts_Start(int axisIndex)
{
short sRtn;
sRtn = GT_Update(1<<(axisIndex-1));
commandhandler("gts_Start", sRtn);
}
void GTSCtrl::gts_GetSts(int axisIndex,long *sts)
{
short sRtn;
sRtn = GT_GetSts(axisIndex,sts);
long lAxisStatus = *sts;
// 伺服报警标志
if (lAxisStatus & 0x2)
{
m_bFlagAlarm = true;
showErrorMsg(axisIndex,"伺服报警");
}
else
{
m_bFlagAlarm = false;
}
// 跟随误差越限标志
if (lAxisStatus & 0x10)
{
m_bFlagMError = true;
showErrorMsg(axisIndex,"跟随误差越限");
}
else
{
m_bFlagMError = false;
}
// 正向限位
if (lAxisStatus & 0x20)
{
m_bFlagPosLimit = true;
showErrorMsg(axisIndex,"正限位触发");
}
else
{
m_bFlagPosLimit = false;
}
// 负向限位
if (lAxisStatus & 0x40)
{
m_bFlagNegLimit = true;
showErrorMsg(axisIndex,"负限位触发");
}
else
{
m_bFlagNegLimit = false;
}
// 平滑停止
if (lAxisStatus & 0x80)
{
m_bFlagSmoothStop = true;
showErrorMsg(axisIndex,"平滑停止触发");
}
else
{
m_bFlagSmoothStop = false;
}
// 急停标志
if (lAxisStatus & 0x100)
{
m_bFlagAbruptStop = true;
showErrorMsg(axisIndex,"急停触发");
}
else
{
m_bFlagAbruptStop = false;
}
// 伺服使能标志
if (lAxisStatus & 0x200)
{
m_bFlagServoOn = true;
showErrorMsg(axisIndex,"伺服使能");
}
else
{
m_bFlagServoOn = false;
}
// 规划器正在运动标志
if (lAxisStatus & 0x400)
{
m_bFlagMotion = true;
printf("规划器正在运动\n");
}
else
{
m_bFlagMotion = false;
printf("规划器已停止\n");
}
commandhandler("gts_GetSts", sRtn);
}
void GTSCtrl::gts_GetPrfPos(int axisIndex, double* prfPos)
{
QMutexLocker locker(&m_mutex); // 自解锁
short sRtn;
//m_mutex.lock();
sRtn = GT_GetPrfPos(axisIndex, prfPos);
(*prfPos) = (*prfPos) / GTS_RATE;
//m_mutex.unlock();
//commandhandler("gts_GetPrfPos", sRtn);
}
void GTSCtrl::gts_AxisOff(int axisIndex)
{
short sRtn;
sRtn = GT_AxisOff(axisIndex);
commandhandler("gts_AxisOff",sRtn);
}
void GTSCtrl::gts_AbsMoveWithCompare(int axisIndex, long aimPos/*绝对位置p*/, long vel/*运动速度(p/ms)*/,
long PInterVal/*脉冲间隔,像素当量(脉冲/像素)*/, short fwith/*脉宽*/, bool block)
{
StartAxisAbsMotion(axisIndex, (aimPos) / GTS_RATE, vel / GTS_RATE, 100);
if (block){
short sRtn, pSts;
long sts, pCount[2];
double prfPos;
while (true)
{
if (gtsApp->GetAxisMotionDoneState(axisIndex) == MotionError::MOTION_STOPED)
//if (gtsApp->GetAxisMotionArrivedState(axisIndex) == MotionError::MOTION_STOPED)
{
// 读取比较输出脉冲个数
sRtn = GT_CompareStatus(&pSts, pCount);
qDebug() << "状态=" << pSts << ",输出脉冲个数1=" << pCount[0]<< ",输出脉冲个数2=" << pCount[1];
gtsApp->StopCompare();
//DelStop(axisIndex);
break;
}
QThread::msleep(50);
}
}
}
void GTSCtrl::gts_AbsMoveWithCompare(int axisIndex, long aimPos/*绝对位置p*/, long vel/*运动速度(p/ms)*/,
long acc/*加速度(p/ms)*/,long PInterVal/*脉冲间隔,像素当量(脉冲/像素)*/, short fwith/*脉宽*/,
bool block)
{
StartAxisAbsMotion(axisIndex, (aimPos) / GTS_RATE, vel / GTS_RATE, acc / GTS_RATE);
if (block) {
short sRtn, pSts;
long sts, pCount[2];
double prfPos;
while (true)
{
if (gtsApp->GetAxisMotionDoneState(axisIndex) == MotionError::MOTION_STOPED)
//if (gtsApp->GetAxisMotionArrivedState(axisIndex) == MotionError::MOTION_STOPED)
{
// 读取比较输出脉冲个数
sRtn = GT_CompareStatus(&pSts, pCount);
qDebug() << "状态=" << pSts << ",输出脉冲个数1=" << pCount[0] << ",输出脉冲个数2=" << pCount[1];
gtsApp->StopCompare();
//DelStop(axisIndex);
break;
}
QThread::msleep(50);
}
//do
//{
// // 读取AXIS轴的状态
// sRtn = GT_GetSts(axisIndex, &sts);
//
//} while (sts & 0x400); // 等待AXIS轴规划停止
}
}
bool GTSCtrl::gts_IsPrfStop(int axisIndex)
{
long sts;
gts_GetSts(axisIndex,&sts);
if (sts & 0x400)
{
return false;
}
return true;
}
void GTSCtrl::gts_AbsMove(int axisIndex, double aimPos/*mm/s*/, double vel,double acc, bool block)
{
//gts_ProTrap(axisIndex);//点位运动模式
//long actPos;
//GT_GetPos(axisIndex,&actPos);
qDebug() << QStringLiteral("绝对位移->当前位置:") << actPos/GTS_RATE;
//gts_SetPos(axisIndex, aimPos/GTS_RATE);
qDebug() << QStringLiteral("绝对位移->目标位置:") << aimPos;
//gts_SetVel(axisIndex,vel/1000/GTS_RATE);//转换为 脉冲数/毫秒
//gts_Start(axisIndex);
StartAxisAbsMotion(axisIndex, aimPos / GTS_RATE, vel / GTS_RATE, acc );
//QThread::msleep(200);
if (block){
short sRtn, pSts;
long sts, pCount;
double prfPos;
while (true)
{
if (gtsApp->GetAxisMotionDoneState(axisIndex) == MotionError::MOTION_STOPED)
{
//DelStop(axisIndex);
break;
}
QThread::msleep(50);
}
//do
//{
// // 读取AXIS轴的状态
// sRtn = GT_GetSts(axisIndex, &sts);
//} while (sts & 0x400); // 等待AXIS轴规划停止
}
}
void GTSCtrl::gts_InCreMove(int axisIndex, double pauseNum/*要走的脉冲数*/, double vel, double acc, double dac)
{
gts_ProTrap(axisIndex,acc,dac);//点位运动模式
double actPos;
GT_GetEncPos((short)axisIndex, &actPos, 1);//获取当前轴的坐标
gts_SetPos(axisIndex, (actPos + pauseNum));
//qDebug() << "当前位置,"<<actPos<<"修正值,"<< pauseNum <<",目标位置" << actPos + pauseNum;
gts_SetVel(axisIndex, vel / 1000 / GTS_RATE);//转换为 脉冲数/毫秒
gts_Start(axisIndex);
}
void GTSCtrl::gts_InCreMove(int axisIndex, double pauseNum/*要走的脉冲数*/, double velstart,double vel, double acc, double dac)
{
QMutexLocker locker(&m_mutex); // 自解锁
//m_mutex.lock();
double velStart = velstart / 1000 / GTS_RATE;
gts_ProTrap(axisIndex, velStart, acc, dac);//点位运动模式
double actPos;
GT_GetEncPos((short)axisIndex, &actPos, 1);//获取当前轴的坐标
gts_SetPos(axisIndex, (actPos + pauseNum));
//qDebug() << "当前位置,"<<actPos<<"修正值,"<< pauseNum <<",目标位置" << actPos + pauseNum;
gts_SetVel(axisIndex, vel / 1000 / GTS_RATE);//转换为 脉冲数/毫秒
gts_Start(axisIndex);
//m_mutex.unlock();
}
void GTSCtrl::setPID(int axisIndex, TPid pid)
{
//short ret = GT_SetPid()
}
bool GTSCtrl::gts_AutoFocusOK(int axisIndex)
{
GT_GetPos(axisIndex, &m_lastAimPos);
GT_GetEncPos((short)axisIndex, &m_curPos, 1);
if (abs(m_curPos - m_lastAimPos)<10) {
return true;
}
return false;
}
void GTSCtrl::showErrorMsg(int axisIndex, QString errInfo)
{
QString strInfo = QString::number(axisIndex)+"号轴"+errInfo;
}
bool GTSCtrl::gts_SetOutputValid(int channel, int validValue)
{
ushort nRlt = GT_SetDoBit(MC_GPO,channel,validValue);
//commandhandler("gts_SetOutputValid",nRlt);
return true;
}
bool GTSCtrl::gts_IsInputValid(int channel, int validValue, int Noise)
{
short sRtn;
long lGpiValue;
sRtn = GT_GetDi(MC_GPI, &lGpiValue);
if ((lGpiValue & (1<<channel))==0 && validValue==0)
{
QThread::msleep(Noise);
if ((lGpiValue & (1 << channel)) == 0 && validValue == 0)
{
return true;
}
}
return false;
}
int GTSCtrl::gts_Home(int axisIndex)
{
short sRtn;
TTrapPrm trapPrm;
short capture;
long status, pos;
double prfPos, encPos, axisPrfPos, axisEncPos;
sRtn = GT_SetCaptureMode(axisIndex, CAPTURE_HOME);
sRtn = GT_PrfTrap(axisIndex);
// 读取点位模式运动参数
sRtn = GT_GetTrapPrm(axisIndex, &trapPrm);
trapPrm.acc = 0.25;
trapPrm.dec = 0.25;
// 设置点位模式运动参数
sRtn = GT_SetTrapPrm(axisIndex, &trapPrm);
// 设置点位模式目标速度,即回原点速度
sRtn = GT_SetVel(axisIndex, 2);
//test
double searchHome = 0;
GT_GetEncPos(axisIndex, &searchHome);
//sRtn = GT_SetPos(axisIndex, SEARCH_HOME);
sRtn = GT_SetPos(axisIndex, -searchHome);
commandhandler("gts_Home", sRtn);
// 启动运动
sRtn = GT_Update(axisIndex);
commandhandler("gts_Home", sRtn);
qDebug() << "Waiting for home signal..." << endl;
do
{
// 读取轴状态
sRtn = GT_GetSts(axisIndex, &status);
// 读取捕获状态
sRtn = GT_GetCaptureStatus(axisIndex, &capture, &pos);
// 读取规划位置
sRtn = GT_GetPrfPos(axisIndex, &prfPos);
// 读取编码器位置
sRtn = GT_GetEncPos(axisIndex, &encPos);
qDebug() << "capture =" << capture << ",prfPos=" << prfPos << ",encPos=" << encPos;
// 如果运动停止,返回出错信息
if (0 == (status & 0x400))
{
qDebug() << "no home found" << endl;
return 1;
}
// 等待捕获触发
} while (0 == capture);
// 显示捕获位置
printf("\ncapture pos = %ld\n", pos);
// 运动到"捕获位置+偏移量"
sRtn = GT_SetPos(axisIndex, pos + HOME_OFFSET);
commandhandler("gts_Home", sRtn);
// 在运动状态下更新目标位置
sRtn = GT_Update(axisIndex);
commandhandler("gts_Home", sRtn);
do
{
// 读取轴状态
sRtn = GT_GetSts(axisIndex, &status);
// 读取规划位置
sRtn = GT_GetPrfPos(axisIndex, &prfPos);
// 读取编码器位置
sRtn = GT_GetEncPos(axisIndex, &encPos);
printf("status=0x%-8lx prfPos=%-10.1lf encPos=%-10.1lf\r", status, prfPos, encPos);
// 等待运动停止
} while (status & 0x400);
// 检查是否到达"Home捕获位置+偏移量"
if (prfPos != pos + HOME_OFFSET)
{
qDebug() << "move to home pos error" << endl;
return 2;
}
qDebug() << "Home finish" << endl;
return 0;
}
bool GTSCtrl::gts_IsInputTimeOut(int channel, int timeout, int validValue)
{
short sRtn,sRtn2;
long lGpiValue;
int sts, sts2;
sRtn = GT_GetDi(MC_GPI, &lGpiValue);
sts = lGpiValue & (1 << channel);
QTime time;
time.start();
while(time.elapsed()<timeout)
{
sRtn2 = GT_GetDi(MC_GPI, &lGpiValue);
sts2 = lGpiValue & (1 << channel);
if (sts2 == validValue /*&& sts!=sts2*/)
{
return false;
}
}
return true;
}
bool GTSCtrl::gts_SetOutputInValid(int channel, int invalidValue/*=1*/)
{
ushort nRlt = GT_SetDoBit(MC_GPO, channel, invalidValue);
commandhandler("gts_SetOutputInValid", nRlt);
return true;
}
void GTSCtrl::gts_SmartHome(int axisIndex,long serachDistance)
{
short sRtn;
short gAxis = axisIndex;
THomeStatus tHomeSts;
short capture;
long pos;
//sRtn = GT_AlarmOff(gAxis);//轴报警无效
//sRtn = GT_LmtsOff(gAxis);//轴限位无效
sRtn = GT_LmtsOn(gAxis);
//sRtn = GT_LmtSns(3);限位触发取反
//sRtn = GT_EncSns(1);编码器取反
sRtn = GT_ClrSts(gAxis);//清除轴的报警和限位
sRtn = GT_ZeroPos(gAxis);
//设置smart home 回原点参数
THomePrm tHomePrm;
sRtn = GT_GetHomePrm(gAxis, &tHomePrm);
tHomePrm.mode = HOME_MODE_LIMIT_HOME;
//tHomePrm.mode = HOME_MODE_LIMIT_HOME;
//tHomePrm.mode = 11;
tHomePrm.moveDir = -1;
//tHomePrm.moveDir = 0;
tHomePrm.indexDir = 1;
//tHomePrm.edge = 1;
tHomePrm.edge = 0;
m_nHomeVel = 5;
tHomePrm.velHigh = m_nHomeVel/GTS_RATE;
qDebug() << "HomeVel=" << m_nHomeVel;
tHomePrm.velLow = 2;
tHomePrm.acc = 0.1;
tHomePrm.dec = 0.1;
tHomePrm.homeOffset = 0;
tHomePrm.searchHomeDistance = 0/*serachDistance*/;
tHomePrm.searchIndexDistance = 0;
tHomePrm.escapeStep = 0;
sRtn = GT_GoHome(gAxis, &tHomePrm);
do
{
sRtn = GT_GetHomeStatus(gAxis, &tHomeSts);
} while (tHomeSts.run /*capture == 0*/);
//gts_Stop(gAxis);
//QThread::msleep(100);
sRtn = GT_ZeroPos(gAxis);
long actPos;
sRtn = GT_GetPos(gAxis,&actPos);
qDebug() << QStringLiteral("回原点完成,当前位置:") << actPos << endl;
sRtn = GT_AlarmOn(gAxis);//轴报警有效
sRtn = GT_LmtsOn(gAxis);//轴限位有效
}
bool GTSCtrl::gts_ServoArrive(int axisIndex)//di 电机到位 取反
{
//捕获下降沿
short sRtn;
long lGpiValue;
//sRtn = GT_GetDi(MC_ARRIVE, &lGpiValue);
bool last = false;
bool now = false;
while (1)
{
last = now;
sRtn = GT_GetDi(MC_ARRIVE, &lGpiValue);
now = lGpiValue & (1 << axisIndex - 1);
if (now ==true /*&& now== false*/)//1->0
{
return true;
}
qDebug() << "0";
}
}
void GTSCtrl::gts_2AxisMultiSmartHome(int *axisArr, long *serchDistanceArr)
{
THomeStatus tHomeSts[2];
short sRtn;
for (int i=0; i<2; i++)
{
short gAxis = axisArr[i];
sRtn = GT_AlarmOff(gAxis);
sRtn = GT_LmtsOff(gAxis);
sRtn = GT_LmtSns(3);
//sRtn = GT_EncSns(1);编码器取反
sRtn = GT_ClrSts(gAxis);
//sRtn = GT_ZeroPos(gAxis);
//设置smart home 回原点参数
THomePrm tHomePrm;
sRtn = GT_GetHomePrm(gAxis, &tHomePrm);
tHomePrm.mode = HOME_MODE_HOME;
tHomePrm.moveDir = -1;
tHomePrm.indexDir = 1;
tHomePrm.edge = 0;
tHomePrm.velHigh = 100;
tHomePrm.velLow = 10;
tHomePrm.acc = 0.1;
tHomePrm.dec = 0.1;
tHomePrm.searchHomeDistance = serchDistanceArr[i];
tHomePrm.searchIndexDistance = 3000;
tHomePrm.escapeStep = 1000;
sRtn = GT_GoHome(gAxis, &tHomePrm);
}
do
{
sRtn = GT_GetHomeStatus(axisArr[0], &tHomeSts[0]);
sRtn = GT_GetHomeStatus(axisArr[1], &tHomeSts[1]);
} while (tHomeSts[0].run || tHomeSts[1].run);
for (int i=0; i<2; i++)
{
sRtn = GT_ZeroPos(axisArr[i]);
}
}
void GTSCtrl::gts_3AxisMultiSmartHome(int *axisArr, long *serchDistanceArr)
{
THomeStatus tHomeSts[3];
short sRtn;
for (int i = 0; i < 3; i++)
{
short gAxis = axisArr[i];
sRtn = GT_AlarmOff(gAxis);
sRtn = GT_LmtsOff(gAxis);
sRtn = GT_LmtSns(3);
//sRtn = GT_EncSns(1);编码器取反
sRtn = GT_ClrSts(gAxis);
//sRtn = GT_ZeroPos(gAxis);
//设置smart home 回原点参数
THomePrm tHomePrm;
sRtn = GT_GetHomePrm(gAxis, &tHomePrm);
tHomePrm.mode = HOME_MODE_HOME;
tHomePrm.moveDir = -1;
tHomePrm.indexDir = 1;
tHomePrm.edge = 0;
tHomePrm.velHigh = 100;
tHomePrm.velLow = 10;
tHomePrm.acc = 0.1;
tHomePrm.dec = 0.1;
tHomePrm.searchHomeDistance = serchDistanceArr[i];
tHomePrm.searchIndexDistance = 3000;
tHomePrm.escapeStep = 1000;
sRtn = GT_GoHome(gAxis, &tHomePrm);
}
do
{
sRtn = GT_GetHomeStatus(axisArr[0], &tHomeSts[0]);
sRtn = GT_GetHomeStatus(axisArr[1], &tHomeSts[1]);
sRtn = GT_GetHomeStatus(axisArr[1], &tHomeSts[1]);
} while (tHomeSts[0].run || tHomeSts[1].run || tHomeSts[2].run);
for (int i = 0; i < 3; i++)
{
sRtn = GT_ZeroPos(axisArr[3]);
}
}
bool GTSCtrl::gts_ServoArrive2(int axisIndex,int validValue)
{
short sRtn;
long lGpiValue;
bool now = 0;
sRtn = GT_GetDi(MC_ARRIVE, &lGpiValue);
now = lGpiValue & (1 << axisIndex - 1);
if (now == (bool)validValue)
{
return true;
}
return false;
}
bool GTSCtrl::gts_isOn(int channel, int exmdlIndex,int validValue, int Noise)//拓展io输入有效
{
short rtn;
unsigned short diValue;
//采集第一个(默认)模块数字量输入值
rtn = GT_GetExtIoValue(exmdlIndex, &diValue);
if ((diValue & (1<<channel)) == validValue)
{
QThread::msleep(Noise);
rtn = GT_GetExtIoValue(exmdlIndex, &diValue);
if ((diValue & (1 << channel)) == validValue)
{
return true;
}
}
return false;
}
bool GTSCtrl::gts_SetOn(int channel, int exmdlIndex, int validValue)
{
GT_SetExtIoBit(exmdlIndex, channel, validValue);
return true;
}
bool GTSCtrl::gts_SetOff(int channel, int exmdlIndex, int validValue)
{
GT_SetExtIoBit(exmdlIndex, channel, validValue);
return true;
}
void GTSCtrl::gts_JogMove(int axisIndex, double vel,int dir)
{
short sRtn;
TJogPrm jog;
// 将AXIS轴设为Jog模式
sRtn = GT_PrfJog(axisIndex);
// 读取Jog运动参数
sRtn = GT_GetJogPrm(axisIndex, &jog);
jog.acc = 0.0625;
jog.dec = 0.0625;
// 设置Jog运动参数
sRtn = GT_SetJogPrm(axisIndex, &jog);
// 设置AXIS轴的目标速度
if (dir == 0){
sRtn = GT_SetVel(axisIndex, vel/1000/GTS_RATE); //转换成pause/ms
}
else if(dir == 1){
sRtn = GT_SetVel(axisIndex, (-1)*vel / 1000 / GTS_RATE);//转换成pause/ms
}
// 启动AXIS轴的运动
sRtn = GT_Update(1 << (axisIndex-1));
}
void GTSCtrl::gts_GetPos(int axisIndex, long* posValue)
{
QMutexLocker locker(&m_mutex); // 自解锁
//m_mutex.lock();
GT_GetPos(axisIndex,posValue);
//m_mutex.unlock();
}
bool GTSCtrl::gts_GetGPO(int channel, int validValue/*=0*/)
{
short sRtn;
long value;
sRtn = GT_GetDo(MC_GPO,&value);
if ((value & (1<<(channel-1))) == validValue)
{
return true;
}
return false;
}
bool GTSCtrl::gts_GetGlinkO(int channel, int exmdlIndex/*=0*/, int validValue /*=0*/)
{
return true;
}
void GTSCtrl::gts_SetAccDec(int axisIndex,double acc, double dec)
{
TTrapPrm trapPrm;
GT_GetTrapPrm(axisIndex, &trapPrm);
trapPrm.acc = acc;
trapPrm.dec = dec;
GT_SetTrapPrm(axisIndex, &trapPrm);
}
void GTSCtrl::gts_Stop(int axisIndex)
{
GT_Stop(1<<(axisIndex-1),1<<(axisIndex-1));
}
bool GTSCtrl::gts_LimitAlarm(int axisIndex, int type)
{
long value;
GT_GetDi(type,&value);
if ((value & 1<<(axisIndex-1)))
{
return true;
}
return false;
}
bool GTSCtrl::gts_ServoAlarm(int axisIndex)
{
long value;
GT_GetDi(MC_ALARM,&value);
if ((value&1<<(axisIndex-1))==0)
{
return true;
}
return false;
}
void GTSCtrl::gts_ClrSts(int axisIndex)
{
short axis = axisIndex;
GT_ClrSts(axis);
}
//*******************************************************************************
MotionError GTSCtrl::InitCard()
{
short sRtn;
try
{
QMutexLocker locker(&m_mutex); // 自解锁
//m_mutex.lock();
//打开控制卡
sRtn = GT_Open();
if (sRtn != gts_SUCCESS) return MotionError::MOTION_INIT_MOTION_FAILED;
//载入配置文件
QString path = QCoreApplication::applicationDirPath() + "\\GTS800.cfg";
QByteArray tmp = path.toLocal8Bit();
char* charPath = tmp.data();
sRtn = GT_LoadConfig(charPath);
if (sRtn != gts_SUCCESS) return MotionError::LOAD_CONFIG_FAILED;
//清除各轴报警和限位(不在限位才能清楚报警)
sRtn = GT_ClrSts(1, 8);
if (sRtn != gts_SUCCESS) return MotionError::MOTION_INIT_MOTION_FAILED;
//轴使能
for (short i = 1; i <= AxisCount; i++)
{
sRtn = GT_AxisOn(i);
if (sRtn != 0) return MotionError::MOTION_INIT_MOTION_FAILED;
}
//m_mutex.unlock();
return MotionError::SUCCESS;
}
catch (...)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::DeInitCard()
{
try
{
QMutexLocker locker(&m_mutex); // 自解锁
//m_mutex.lock();
//关闭运动控制卡
if (GT_Close() != gts_SUCCESS)
{
//m_mutex.unlock();
return MotionError::MOTION_FAILED;
}
//m_mutex.unlock();
return MotionError::SUCCESS;
}
catch (...)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::GetEzZeroFlag(int axis, bool *zero_flag)
{
short result;
short Status;
long pValue;
unsigned long clk;
try
{
QMutexLocker locker(&m_mutex); // 自解锁
//m_mutex.lock();
GT_ClrSts(1, 8);
result = GT_GetCaptureStatus((short)axis, &Status, &pValue, 1, &clk);
//m_mutex.unlock();
*zero_flag = false;
if (result == gts_SUCCESS)
{
if (Status == 1)
*zero_flag = true;
return MotionError::SUCCESS;
}
return MotionError::MOTION_FAILED;
}
catch (...)
{
return MotionError::EXCEPTION;
}
}
//MotionError GTSCtrl::StartAxisVelocityMotion(int axis, double servo_velocity)
//{
// short result;
// long tmp;
// unsigned long clk;
// try
// {
// m_mutex.lock();
// //设置为jog模式
// GT_ClrSts(1, 8);
// GT_PrfJog((short)axis);
// //GT_GetJogMode((short)axis, &tmp, 1, &clk);
// //if (tmp != 0)
// //{
// // m_mutex.unlock();
// // return MotionError::MOTION_FAILED;
// //}
// jog.acc = 5;
// jog.dec = 5;
// jog.smooth = 15;
// GT_SetJogPrm((short)axis, &jog);
// GT_SetVel((short)axis, servo_velocity);
// result = GT_Update(1 << (axis - 1));
// m_mutex.unlock();
// if (result == gts_SUCCESS) return MotionError::SUCCESS;
// return MotionError::MOTION_FAILED;
// }
// catch (const std::exception&)
// {
// return MotionError::EXCEPTION;
// }
//}
MotionError GTSCtrl::StartAxisVelocityMotion(int axis, double servo_velocity, double servo_acc)
{
short result;
unsigned long clk;
TTrapPrm trap;
double encpos;
long tmp;
try
{
QMutexLocker locker(&m_mutex); // 自解锁
GT_ClrSts(1, 8);
GT_PrfTrap((short)axis);
GT_GetPrfMode((short)axis, &tmp, 1, &clk);
GT_GetPrfPos( (short)axis, &encpos, 1, &clk);//获取当前轴的坐标
if (servo_velocity > 0)
{
tmp = encpos + 173741823;
}
else
{
tmp = encpos - 173741823;
servo_velocity = abs(servo_velocity);
}
trap.acc = servo_acc;
trap.dec = servo_acc;
trap.smoothTime = 15;
trap.velStart = 0;
GT_SetTrapPrm((short)axis, &trap);//设置点位运动参数
GT_SetVel((short)axis, servo_velocity);//设置目标速度
result = GT_SetPos((short)axis, tmp);//设置目标位置
result = GT_Update(1 << (axis - 1));//更新轴运动
设置为jog模式
//GT_ClrSts(1, 8);
//GT_PrfJog((short)axis);
//jog.acc = servo_acc;
//jog.dec = servo_acc;
//GT_SetJogPrm((short)axis, &jog);
//GT_SetVel((short)axis, servo_velocity);
//result = GT_Update(1 << (axis - 1));
//m_mutex.unlock();
if (result == gts_SUCCESS) return MotionError::SUCCESS;
return MotionError::MOTION_FAILED;
}
catch (const std::exception&)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::StartAxisVelocityMotion(int axis, double servo_velocity)
{
short result;
unsigned long clk;
TTrapPrm trap;
double encpos;
long tmp;
try
{
QMutexLocker locker(&m_mutex); // 自解锁
//m_mutex.lock();
GT_ClrSts(1, 8);
GT_PrfTrap((short)axis);
GT_GetPrfMode((short)axis, &tmp, 1, &clk);
if (tmp != 0)
{
//m_mutex.unlock();
return MotionError::MOTION_FAILED;
}
trap.acc = 5;
trap.dec = 5;
trap.smoothTime = 15;
trap.velStart = 0;
GT_GetPrfPos((short)axis, &encpos, 1, &clk);//获取当前轴的坐标
GT_SetTrapPrm((short)axis, &trap);//设置点位运动参数
if (servo_velocity < 0)
{
tmp = encpos - 173741823;
}
else
{
tmp = encpos + 173741823;
}
GT_SetVel((short)axis,abs(servo_velocity));//设置目标速度
GT_SetPos((short)axis, tmp);//设置目标位置
result = GT_Update(1 << (axis - 1));//更新轴运动
//m_mutex.unlock();
if (result == gts_SUCCESS) return MotionError::SUCCESS;
return MotionError::MOTION_FAILED;
}
catch (const std::exception&)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::StartAxisDistanceMotion(int axis, double servo_distance, double servo_velocity)
{
short result;
unsigned long clk;
TTrapPrm trap;
double encpos;
long tmp;
try
{
QMutexLocker locker(&m_mutex); // 自解锁
//m_mutex.lock();
GT_ClrSts(1, 8);
GT_PrfTrap((short)axis);
GT_GetPrfMode((short)axis, &tmp, 1, &clk);
if (tmp != 0)
{
//m_mutex.unlock();
return MotionError::MOTION_FAILED;
}
trap.acc = 5;
trap.dec = 5;
trap.smoothTime = 15;
trap.velStart = 0;
GT_GetPrfPos((short)axis, &encpos, 1, &clk);//获取当前轴的坐标
GT_SetTrapPrm((short)axis, &trap);//设置点位运动参数
GT_SetVel((short)axis, servo_velocity);//设置目标速度
tmp = encpos + servo_distance;
GT_SetPos((short)axis, tmp);//设置目标位置
result = GT_Update(1 << (axis - 1));//更新轴运动
//m_mutex.unlock();
if (result == gts_SUCCESS) return MotionError::SUCCESS;
return MotionError::MOTION_FAILED;
}
catch (const std::exception&)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::StartAxisDistanceMotion(int axis, double servo_distance, double servo_velocity, double acc_time)
{
short result;
unsigned long clk;
TTrapPrm trap;
double encpos;
long tmp;
try
{
QMutexLocker locker(&m_mutex); // 自解锁
//m_mutex.lock();
GT_ClrSts(1, 8);
GT_PrfTrap((short)axis);
GT_GetPrfMode((short)axis, &tmp, 1, &clk);
if (tmp != 0) {
//m_mutex.unlock();
return MotionError::MOTION_FAILED;
}
trap.acc = acc_time;
trap.dec = acc_time;
trap.smoothTime = 15;
trap.velStart = 0;
GT_GetEncPos((short)axis, &encpos, 1, &clk);//获取当前轴的坐标
GT_SetTrapPrm((short)axis, &trap);//设置点位运动参数
GT_SetVel((short)axis, servo_velocity);//设置目标速度
tmp = encpos + servo_distance;
GT_SetPos((short)axis, tmp);//设置目标位置
result = GT_Update(1 << (axis - 1));//更新轴运动
//m_mutex.unlock();
if (result == gts_SUCCESS) return MotionError::SUCCESS;
return MotionError::MOTION_FAILED;
}
catch (const std::exception&)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::StartAxisDistanceMotionWithCurPos(int axis, double servo_distance, double *encode_pos, double start_velocity,double servo_velocity, double acc_time)
{
short result;
unsigned long clk;
TTrapPrm trap;
double encpos;
long tmp;
try
{
QMutexLocker locker(&m_mutex); // 自解锁
//m_mutex.lock();
GT_ClrSts(1, 8);
GT_PrfTrap((short)axis);
GT_GetPrfMode((short)axis, &tmp, 1, &clk);
if (tmp != 0) {
//m_mutex.unlock();
return MotionError::MOTION_FAILED;
}
trap.acc = acc_time;
trap.dec = acc_time;
trap.smoothTime = 15;
trap.velStart = start_velocity;
GT_GetEncPos((short)axis, &encpos, 1, &clk);//获取当前轴的坐标
*encode_pos = encpos;
GT_SetTrapPrm((short)axis, &trap);//设置点位运动参数
GT_SetVel((short)axis, servo_velocity);//设置目标速度
tmp = encpos + servo_distance;
GT_SetPos((short)axis, tmp);//设置目标位置
result = GT_Update(1 << (axis - 1));//更新轴运动
//m_mutex.unlock();
if (result == gts_SUCCESS) return MotionError::SUCCESS;
return MotionError::MOTION_FAILED;
}
catch (const std::exception&)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::StartAxisAbsMotion(int axis, int servo_position, double servo_velocity)
{
short result;
unsigned long clk;
TTrapPrm trap;
try
{
QMutexLocker locker(&m_mutex); // 自解锁
//m_mutex.lock();
GT_ClrSts(1, 8);
GT_PrfTrap((short)axis);
long tmp;
GT_GetPrfMode((short)axis, &tmp, 1, &clk);
if (tmp != 0) {
//m_mutex.unlock();
return MotionError::MOTION_FAILED;
}
trap.acc = 5;
trap.dec = 5;
trap.smoothTime = 15;
trap.velStart = 0;
GT_SetTrapPrm((short)axis, &trap);//设置点位运动参数
GT_SetVel((short)axis, servo_velocity);//设置目标速度
GT_SetPos((short)axis, servo_position);//设置目标位置
result = GT_Update(1 << (axis - 1));//更新轴运动
//m_mutex.unlock();
if (result == gts_SUCCESS) return MotionError::SUCCESS;
return MotionError::MOTION_FAILED;
}
catch (const std::exception&)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::StartAxisAbsMotion(int axis, int servo_position, double servo_velocity, double acc)
{
short result;
unsigned long clk;
TTrapPrm trap;
try
{
QMutexLocker locker(&m_mutex); // 自解锁
//m_mutex.lock();
GT_ClrSts(1, 8);
GT_PrfTrap((short)axis);
long tmp;
GT_GetPrfMode((short)axis, &tmp, 1, &clk);
if (tmp != 0) {
//m_mutex.unlock();
return MotionError::MOTION_FAILED;
}
trap.acc = acc;//这里指的是加速度不是加速时间 单位pulse/ms2
trap.dec = acc;
trap.smoothTime = 15;
trap.velStart = 0;
GT_SetTrapPrm((short)axis, &trap);//设置点位运动参数
//qDebug() << "当前轴:" + QString::number(axis) + "速度:" + QString::number(servo_velocity) + "加速度:" + QString::number(acc);
GT_SetVel((short)axis, servo_velocity);//设置目标速度
GT_SetPos((short)axis, servo_position);//设置目标位置
result = GT_Update(1 << (axis - 1));//更新轴运动
//m_mutex.unlock();
if (result == gts_SUCCESS) return MotionError::SUCCESS;
return MotionError::MOTION_FAILED;
}
catch (const std::exception&)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::DelStop(int axis)
{
short result;
try
{
QMutexLocker locker(&m_mutex); // 自解锁
//m_mutex.lock();
GT_SetStopDec((short)axis, 100, 30767);
result = GT_Stop(1 << (axis - 1), 0 << (axis - 1));
//m_mutex.unlock();
if (result == gts_SUCCESS) return MotionError::SUCCESS;
return MotionError::MOTION_FAILED;
}
catch (const std::exception&)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::ImdStop(int axis)
{
short result;
try
{
QMutexLocker locker(&m_mutex); // 自解锁
//m_mutex.lock();
GT_SetStopDec((short)axis, 50, 30767);
GT_SetStopIo((short)axis, 1, MC_ALARM, (short)axis);
result = GT_Stop(1 << (axis - 1), 1 << (axis - 1));
//m_mutex.unlock();
if (result == gts_SUCCESS) return MotionError::SUCCESS;
return MotionError::MOTION_FAILED;
}
catch (const std::exception&)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::SetCommandPosition(int axis, int pulse_command)
{
short result;
try
{
//m_mutex.lock();
QMutexLocker locker(&m_mutex); // 自解锁
result = GT_SetPrfPos((short)axis, pulse_command);
GT_SynchAxisPos(axis - 1);
//m_mutex.unlock();
if (result == gts_SUCCESS) return MotionError::SUCCESS;
return MotionError::MOTION_FAILED;
}
catch (const std::exception&)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::GetCommandPosition(int axis, double *pulse_command)
{
short result;
unsigned long clk;
try
{
QMutexLocker locker(&m_mutex); // 自解锁
//m_mutex.lock();
result = GT_GetPrfPos((short)axis, &*pulse_command, 1, &clk);
//m_mutex.unlock();
if (result == gts_SUCCESS) return MotionError::SUCCESS;
return MotionError::MOTION_FAILED;
}
catch (const std::exception&)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::SetEncoderPosition(int axis, int encoder_position)
{
short result;
try
{
//m_mutex.lock();
QMutexLocker locker(&m_mutex); // 自解锁
result = GT_SetEncPos((short)axis, encoder_position);
GT_SynchAxisPos(axis - 1);
//m_mutex.unlock();
if (result == gts_SUCCESS) return MotionError::SUCCESS;
return MotionError::MOTION_FAILED;
}
catch (const std::exception&)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::GetEncoderPosition(int axis, double *encoder_position)
{
short result;
unsigned long clk;
try
{
//m_mutex.lock();
QMutexLocker locker(&m_mutex); // 自解锁
result = GT_GetEncPos((short)axis, &*encoder_position,1, &clk);
GT_SynchAxisPos(axis - 1);
//m_mutex.unlock();
if (result == gts_SUCCESS) return MotionError::SUCCESS;
return MotionError::MOTION_FAILED;
}
catch (const std::exception&)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::GetAxisSate(int axis, bool *alarm_on, bool *positive_limit_on, bool *negative_limit_on)
{
short result;
unsigned long clk;
long AxisStatus;//轴状态
try
{
//m_mutex.lock();
QMutexLocker locker(&m_mutex); // 自解锁
GT_ClrSts(1, 8);
result = GT_GetSts((short)axis, &AxisStatus, 1, &clk);
//m_mutex.unlock();
if (result != gts_SUCCESS) return MotionError::MOTION_FAILED;
//伺服报警信号
if ((AxisStatus & 0x2) == 0)
{
*alarm_on = false;
}
else
{
*alarm_on = true;
}
//正限位
if ((AxisStatus & 0x20) == 0)
{
*positive_limit_on = false;
}
else
{
*positive_limit_on = true;
}
//负限位
if ((AxisStatus & 0x40) == 0)
{
*negative_limit_on = false;
}
else
{
*negative_limit_on = true;
}
return MotionError::SUCCESS;
}
catch (const std::exception&)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::ClearAxisSate()
{
short result;
try
{
QMutexLocker locker(&m_mutex); // 自解锁
//m_mutex.lock();
result = GT_ClrSts(1, 8);
//m_mutex.unlock();
if (result == gts_SUCCESS) return MotionError::SUCCESS;
return MotionError::MOTION_FAILED;
}
catch (const std::exception&)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::GetEmgSate(int axis,bool *emg_on)
{
short result;
long AxisStatus;
unsigned long clk;
//short axis = 1;
try
{
//m_mutex.lock();
QMutexLocker locker(&m_mutex); // 自解锁
result = GT_GetSts((short)axis, &AxisStatus, 1, &clk);
//m_mutex.unlock();
if (result != gts_SUCCESS) return MotionError::MOTION_FAILED;
if((AxisStatus & 0x100) == 0)
{
*emg_on = false;
}
else
{
*emg_on = true;
}
return MotionError::SUCCESS;
}
catch (const std::exception&)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::GetAxisMotionDoneState(int axis)
{
short result;
long AxisStatus;
unsigned long clk;
//short axis = 1;
try
{
QMutexLocker locker(&m_mutex); // 自解锁
//m_mutex.lock();
result = GT_GetSts((short)axis, &AxisStatus, 1, &clk);
//m_mutex.unlock();
if (result != gts_SUCCESS) return MotionError::MOTION_FAILED;
if ((AxisStatus & 0x400) != 0)
{
return MotionError::MOTION_RUNNING;
}
else
{
return MotionError::MOTION_STOPED;
}
}
catch (const std::exception&)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::SetEZMode(int axis, CAPTURE capture)
{
short result;
short Mode;
try
{
if (capture == CAPTURE::HOME)
{
Mode = 1;
}
else
{
Mode = 2;
}
//m_mutex.lock();
QMutexLocker locker(&m_mutex); // 自解锁
//GT_SetCaptureSense((short)axis, Mode, 0);
result = GT_SetCaptureMode((short)axis, Mode);
//m_mutex.unlock();
if (result == gts_SUCCESS) return MotionError::SUCCESS;
return MotionError::MOTION_FAILED;
}
catch (const std::exception&)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::GetEZMode(int axis, int *Postion)
{
short result;
short Status;
long tmp;
unsigned long clk;
try
{
//m_mutex.lock();
QMutexLocker locker(&m_mutex); // 自解锁
result = GT_GetCaptureStatus((short)axis, &Status, &tmp, 1, &clk);
//m_mutex.unlock();
if (result != gts_SUCCESS) return MotionError::MOTION_FAILED;
*Postion = tmp;
return MotionError::SUCCESS;
}
catch (const std::exception&)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::ZeroPos(int axis, int count)
{
short result;
try
{
QMutexLocker locker(&m_mutex); // 自解锁
//m_mutex.lock();
result = GT_ZeroPos((short)axis, (short)count);
//m_mutex.unlock();
if (result == gts_SUCCESS) return MotionError::SUCCESS;
return MotionError::MOTION_FAILED;
}
catch (const std::exception&)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::SetCompareData(int axis, int startPos, int repeatTime, int inverval)
{
short result;
try
{
long tmp = startPos;
QMutexLocker locker(&m_mutex); // 自解锁
//m_mutex.lock();
result = GT_CompareLinear((short)axis, 1, tmp, repeatTime, inverval, 5, 1);
//m_mutex.unlock();
if (result == gts_SUCCESS) return MotionError::SUCCESS;
return MotionError::MOTION_FAILED;
}
catch (const std::exception&)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::StopCompare()
{
short result;
try
{
//m_mutex.lock();
QMutexLocker locker(&m_mutex); // 自解锁
result = GT_CompareStop();
GT_2DCompareStop(0);
GT_2DCompareClear(0);
GT_2DCompareStop(1);
GT_2DCompareClear(1);
//m_mutex.unlock();
if (result == gts_SUCCESS) return MotionError::SUCCESS;
return MotionError::MOTION_FAILED;
}
catch (const std::exception&)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::GetCompareData(short *nStatus, int *nNumber)
{
short result;
long tmp;
try
{
QMutexLocker locker(&m_mutex); // 自解锁
//m_mutex.lock();
result = GT_CompareStatus(&*nStatus, &tmp);
//m_mutex.unlock();
if (result == gts_SUCCESS) return MotionError::SUCCESS;
return MotionError::MOTION_FAILED;
*nNumber = tmp;
}
catch (const std::exception&)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::ClearEzFlag(int axis)
{
short result;
try
{
//m_mutex.lock();
QMutexLocker locker(&m_mutex); // 自解锁
result = GT_ClearCaptureStatus((short)axis);
//m_mutex.unlock();
if (result == gts_SUCCESS) return MotionError::SUCCESS;
return MotionError::MOTION_FAILED;
}
catch (const std::exception&)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::GetDiFlag(int diIndex,short diType, bool *Di_flag)
{
short result;
long pValue;
unsigned long clk;
try
{
//m_mutex.lock();
QMutexLocker locker(&m_mutex); // 自解锁
GT_ClrSts(1, 8);
result = GT_GetDi(diType, &pValue);
//m_mutex.unlock();
*Di_flag = false;
if (result == gts_SUCCESS)
{
if (pValue &(1 << (diIndex -1)))
*Di_flag = true;
return MotionError::SUCCESS;
}
return MotionError::MOTION_FAILED;
}
catch (...)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::SetHighSpeedIO(short channel, short level, short pulseType, short time)
{
short result;
try
{
QMutexLocker locker(&m_mutex); // 自解锁
//m_mutex.lock();
if (level == 0)
{
result = GT_ComparePulse(0 << (channel - 1), pulseType, time);
}
else
{
result = GT_ComparePulse(1 << (channel - 1), pulseType, time);
}
//m_mutex.unlock();
return MotionError::SUCCESS;
}
catch (...)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::GetAxisEncMotionDoneState(int axis)
{
short result;
double Vel;
unsigned long clk;
//short axis = 1;
try
{
QMutexLocker locker(&m_mutex); // 自解锁
//m_mutex.lock();
result = GT_GetAxisEncVel(axis,&Vel);
//m_mutex.unlock();
if (abs(Vel) > 0)
{
return MotionError::MOTION_RUNNING;
}
else
{
return MotionError::MOTION_STOPED;
}
}
catch (const std::exception&)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::SetDACLevel(short level)
{
short result;
short tmp;
short tmp1;
try
{
QMutexLocker locker(&m_mutex); // 自解锁
//m_mutex.lock();
if (level == 0)
{
tmp = 0;
tmp1 = 32768;
result = GT_SetDac(12, &tmp);
qDebug() << "当前设置的Dac12的值为:" << QString::number(tmp);
//result = GT_SetDac(11, &tmp1);
short curDac;
result = GT_GetDac(12, &curDac);
qDebug() << "当前获取Dac12的值为:" << QString::number(curDac);
}
else
{
tmp = 32768;
tmp1 = 0;
result = GT_SetDac(12, &tmp);
qDebug() << "当前设置的Dac12的值为:" << QString::number(tmp);
//result = GT_SetDac(11, &tmp1);
short curDac;
result = GT_GetDac(12, &curDac);
qDebug() << "当前获取Dac12的值为:" << QString::number(curDac);
}
//m_mutex.unlock();
return MotionError::SUCCESS;
}
catch (...)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::SetAxisStopBand(short axis, int band, int time)
{
short result;
short tmp;
try
{
QMutexLocker locker(&m_mutex); // 自解锁
//m_mutex.lock();
result = GT_SetAxisBand(axis, band, time);
//m_mutex.unlock();
if (result != gts_SUCCESS) return MotionError::MOTION_FAILED;
return MotionError::SUCCESS;
}
catch (...)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::GetAxisBand(short axis, int* band, int* time)
{
short result;
long tmp1,tmp2;
try
{
QMutexLocker locker(&m_mutex); // 自解锁
//m_mutex.lock();
result = GT_GetAxisBand(axis, &tmp1, &tmp2);
//m_mutex.unlock();
if (result != gts_SUCCESS) return MotionError::MOTION_FAILED;
*band = tmp1;
*time = tmp2;
return MotionError::SUCCESS;
}
catch (...)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::GetAxisMotionArrivedState(int axis)
{
short result;
long AxisStatus;
unsigned long clk;
//short axis = 1;
try
{
QMutexLocker locker(&m_mutex); // 自解锁
//m_mutex.lock();
result = GT_GetSts((short)axis, &AxisStatus, 1, &clk);
//m_mutex.unlock();
if (result != gts_SUCCESS) return MotionError::MOTION_FAILED;
if ((0x800 != (AxisStatus & 0x800)))
{
return MotionError::MOTION_RUNNING;
}
else
{
return MotionError::MOTION_STOPED;
}
}
catch (const std::exception&)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::SetDoFlag(short doIndex, short doType, bool Do_flag)
{
short result;
long pValue;
unsigned long clk;
try
{
QMutexLocker locker(&m_mutex); // 自解锁
//int nDoIndex = -1;
GT_ClrSts( 1, 8);
GT_SetDoBit(doType, (doIndex), Do_flag);
//m_mutex.unlock();
return MotionError::MOTION_FAILED;
}
catch (...)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::GetDoFlag(short doIndex, short doType, bool *Do_flag)
{
short result;
long pValue;
unsigned long clk;
try
{
QMutexLocker locker(&m_mutex); // 自解锁
GT_ClrSts( 1, 8);
result = GT_GetDo( doType, &pValue);
//m_mutex.unlock();
*Do_flag = false;
if (result == gts_SUCCESS)
{
if (pValue &(1 << (doIndex - 1)))
*Do_flag = true;
return MotionError::SUCCESS;
}
return MotionError::MOTION_FAILED;
}
catch (...)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::GetAxisRunDistanceDone(int axis, double targetPos)
{
short result;
double dValue;
unsigned long clk;
double encoder_position , pulse_command;
try
{
QMutexLocker locker(&m_mutex); // 自解锁
//获取当前规划轴的位置
result = GT_GetPrfPos((short)axis, &pulse_command, 1, &clk);
//获取当前的编码器位置
result = GT_GetEncPos((short)axis, &encoder_position, 1, &clk);
//计算编码器位置和当前位置的坐标偏差
dValue = abs(targetPos - encoder_position);
if (dValue <= 2)
{
return MotionError::MOTION_STOPED;
}
dValue = abs(pulse_command - encoder_position);
if (dValue <= 2)
{
return MotionError::MOTION_STOPED;
}
return MotionError::MOTION_RUNNING;
}
catch (...)
{
return MotionError::EXCEPTION;
}
}
MotionError GTSCtrl::SetFrameSignal(double targetPos)
{
try
{
QMutexLocker locker(&m_mutex); // 自解锁
GT_2DCompareClear(1);
T2DCompareData dataBuf[1];
T2DComparePrm Prm;
short rtn = GT_2DCompareMode(1, COMPARE2D_MODE_1D); //HSIO0通道使用二维位置比较触发
Prm.encx = 1; //绑定比较轴X
Prm.ency = 1; //绑定比较轴Y
Prm.maxerr = 30; //位置比较允许误差
Prm.outputType = 0; //输出类型 0:脉冲;1:电平
Prm.source = 1; //比较源:0为规划器,1为编码器
Prm.startLevel = 0; // 起始电平方式,0:电平复位 ;1:电平取反
Prm.threshold = 0; //该参数设置为0
Prm.time = 30000; // 脉冲方式的脉冲宽度 us
rtn = GT_2DCompareSetPrm(1, &Prm); // 如果返回值7 请注意 配置的是脉冲计数还是编码器计数(看说明文档)
short sts, fifo, pfifo, pCount;
long Count = 0;
rtn = GT_2DCompareStatus(1, &sts, &Count, &fifo, &pfifo, &pCount);
dataBuf[0].px = targetPos; //保证位置数组中的数据在运动过程中均能达到触发位置
dataBuf[0].py = targetPos;
rtn = GT_2DCompareData(1, 1, dataBuf, fifo);
rtn = GT_2DCompareStart(1); // 启动二维位置比较输出 之后压入比较数据
if (rtn == gts_SUCCESS)
{
return MotionError::SUCCESS;
}
else
{
return MotionError::MOTION_FAILED;
}
}
catch (...)
{
return MotionError::EXCEPTION;
}
}
总结
提示:这里对文章进行总结: