基于固高运动控制卡C语言开发的核心代码示例
一、基础开发环境配置
#include <stdio.h>
#include <stdlib.h>
#include "gts.h" // 固高运动控制卡C语言SDK头文件
// 定义轴参数结构体
typedef struct {
short axis_id; // 轴号(1-32)
double max_speed; // 最大速度(脉冲/秒)
double accel; // 加速度(脉冲/秒²)
double decel; // 减速度(脉冲/秒²)
} AxisParam;
// 全局变量
short g_card_num = 0; // 控制卡编号
AxisParam g_axes[4]; // 最多支持4轴
二、核心功能实现
1. 控制卡初始化
int GTS_Init() {
short sRtn;
// 打开控制卡
sRtn = GT_Open(&g_card_num, 0); // 0表示默认控制卡
if (sRtn != 0) {
printf("控制卡打开失败,错误码:%d\n", sRtn);
return -1;
}
// 复位控制卡
sRtn = GT_Reset(g_card_num);
if (sRtn != 0) {
printf("控制卡复位失败,错误码:%d\n", sRtn);
return -1;
}
// 加载配置文件(test.cfg需提前生成)
sRtn = GT_LoadConfig(g_card_num, "test.cfg");
if (sRtn != 0) {
printf("配置文件加载失败,错误码:%d\n", sRtn);
return -1;
}
// 清除报警和限位
for (int i = 0; i < 4; i++) {
sRtn = GT_ClrSts(g_card_num, (short)(g_axes[i].axis_id), 1);
if (sRtn != 0) {
printf("清除轴%d报警失败,错误码:%d\n", i+1, sRtn);
return -1;
}
}
return 0;
}
2. 点位运动控制
int GTS_MoveTo(int axis, double position) {
short sRtn;
TTrapPrm trap_prm;
// 切换到点位模式
sRtn = GT_PrfTrap(g_card_num, (short)(axis));
if (sRtn != 0) return -1;
// 设置运动参数
GT_GetTrapPrm(g_card_num, (short)(axis), &trap_prm);
trap_prm.acc = g_axes[axis-1].accel * 1000; // 转换为脉冲/秒²
trap_prm.dec = g_axes[axis-1].decel * 1000;
trap_prm.smoothTime = 30; // 平滑时间(ms)
GT_SetTrapPrm(g_card_num, (short)(axis), &trap_prm);
// 设置目标位置
GT_SetPos(g_card_num, (short)(axis), (long)position);
// 启动运动
sRtn = GT_Update(g_card_num, 1 << (axis-1));
if (sRtn != 0) return -1;
// 等待运动完成
do {
GT_GetSts(g_card_num, (short)(axis), &sts);
} while (sts & 0x400); // 检查运动状态
return 0;
}
3. 编码器位置读取
long GTS_ReadEncoder(int axis) {
long enc_pos = 0;
GT_GetEncPos(g_card_num, (short)(axis), &enc_pos);
return enc_pos;
}
// 示例:实时监控轴1位置
void MonitorEncoder() {
while (1) {
long pos = GTS_ReadEncoder(1);
printf("轴1编码器位置:%ld\n", pos);
Sleep(100); // 每100ms读取一次
}
}
4. 回零操作(Home捕获)
int GTS_HomeSearch(int axis) {
short sRtn;
long status, pos;
// 设置Home捕获模式
sRtn = GT_SetCaptureMode(axis, CAPTURE_HOME);
if (sRtn != 0) return -1;
// 启动运动寻找Home点
GT_SetVel(g_card_num, axis, 10); // 设置搜索速度
GT_SetPos(g_card_num, axis, SEARCH_HOME_DISTANCE); // 搜索距离
GT_Update(g_card_num, 1 << (axis-1));
// 等待捕获触发
do {
GT_GetCaptureStatus(axis, &status, &pos);
} while (status == 0);
printf("Home捕获位置:%ld\n", pos);
GT_ZeroPos(g_card_num, axis, 1); // 设置当前位置为零点
return 0;
}
三、多轴同步控制示例
// 四轴直线插补
void GTS_LinearInterpolation() {
short sRtn;
ushort axis_mask = 0;
// 选择参与插补的轴(1,2,3,4)
axis_mask |= (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3);
// 设置目标位置(单位:脉冲)
long target_pos[4] = {10000, -15000, 20000, -25000};
// 启动插补
sRtn = GT_LinearInterpolation(g_card_num, axis_mask, target_pos, 4, 5000);
if (sRtn != 0) {
printf("插补启动失败,错误码:%d\n", sRtn);
return;
}
// 等待插补完成
do {
GT_GetSts(g_card_num, 1, &sts);
} while (sts & 0x400);
printf("四轴插补完成\n");
}
四、错误处理与调试
// 错误码解析函数
const char* GTS_ErrorString(short code) {
switch(code) {
case 0: return "成功";
case -1: return "无效参数";
case -2: return "设备未打开";
case -3: return "超时错误";
case -4: return "通信错误";
default: return "未知错误";
}
}
// 示例:带错误检查的API调用
int Safe_GTS_Update() {
short sRtn = GT_Update(g_card_num, 0xFFFF);
if (sRtn != 0) {
printf("GT_Update失败:%s\n", GTS_ErrorString(sRtn));
return -1;
}
return 0;
}
参考代码 固高运动控制卡源代码 www.youwenfan.com/contentcsr/56638.html
五、编译与调试建议
-
编译环境:
- 使用Visual Studio 2019+,配置x64平台
- 添加固高SDK的
gts.h和gts.lib到工程 - 设置包含路径:
$(SolutionDir)include - 设置库路径:
$(SolutionDir)lib
-
调试技巧:
- 使用逻辑分析仪捕获SPI/I2C信号验证通信
- 通过
GT_DebugLog函数输出调试信息 - 在
GT_Config工具中验证轴参数配置
-
性能优化:
// 启用实时优先级线程 #include <windows.h> SetThreadPriority(GetCurrentThread(), THREAD_MODE_BACKGROUND_BEGIN);
六、扩展功能实现
1. 安全门控制
void GTS_SafetyDoorControl() {
// 读取安全门状态
int di_value = 0;
GT_GetDi(g_card_num, MC_GPI, &di_value);
if (di_value & 0x01) { // 门关闭信号
GT_AxisOn(g_card_num, 1); // 使能轴1
} else {
GT_AxisOff(g_card_num, 1); // 关闭轴1
}
}
2. 多任务调度
// 使用定时器实现周期性任务
VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) {
static int count = 0;
if (++count % 100 == 0) { // 每10秒执行
GTS_HomeSearch(1);
}
}
// 启动定时器
SetTimer(NULL, 1, 1000, TimerProc); // 1秒间隔
七、典型应用场景代码
场景1:自动上下料机械手
void AutoMaterialHandling() {
// 回零所有轴
for (int i = 1; i <= 4; i++) {
GTS_HomeSearch(i);
}
// 移动到取料位置
GTS_MoveTo(1, 5000); // X轴
GTS_MoveTo(2, -3000); // Y轴
// 抓取物料(控制电磁阀)
GT_SetDoBit(g_card_num, MC_GPO, 1, 1); // 打开电磁阀
// 移动到放料位置
GTS_LinearInterpolation();
}
八、注意事项
-
硬件连接:
- 确保编码器信号正确接入(差分信号A+/A-, B+/B-)
- 电源线与信号线分开走线,避免干扰
-
安全防护:
// 急停处理 void EmergencyStop() { GT_Stop(g_card_num); // 立即停止所有轴 GT_SetDoBit(g_card_num, MC_GPO, 2, 0); // 关闭输出 } -
固件升级:
// 使用GT_FirmwareUpdate函数升级固件 sRtn = GT_FirmwareUpdate(g_card_num, "firmware.bin");