?? gtcardapi.cpp
字號:
/* Copyright (c) 2006, lihaizhou All rights reserved.
華中科技大學國家數控系統工程技術研究中心
文件名稱:GtCardApi.h
摘 要:固高GT-400-SP-PCI-G運動控制卡操作類
運行環境:Windows2000/Xp
當前版本:0.1
修改描述:
修改作者:
修改日期:
原 作 者:李海洲
主 管 人:李海洲
完成日期:2006年02月19日 */
#include "stdafx.h"
#include "math.h"
#include "GtCardApi.h"
IMPLEMENT_DYNAMIC(Gt_Card_Api, CCncBase)
Gt_Card_Api::Gt_Card_Api()
{
memset(m_home_step , 0 ,sizeof(m_home_step));
error_msk = 0;
axis_mode = 0;//都是私有成員變量
acc_home_jog = 0.001;
speed_jog = 6;
speed_home = 6;
speed_move_step = 10;
acc_move_step = 0.001;
jerk_move_step = 0.0001;
for(int i=0; i<=Sys_Max_Axis_Num; i++)
{
m_axis_ratio[i] = 1.0;
}
}
Gt_Card_Api::~Gt_Card_Api()
{
}
// 功 能:運動控制器初始化
//
// 返回值: 非0表示有錯誤發生
// 0表示沒有有錯誤發生
int Gt_Card_Api::InitNCCard(void)
{
int i;
int nRet = 0;
if(GT_Open( ))//打開運動控制器設備
nRet = 1;
if(GT_Reset( ))//復位運動控制器
nRet = 1;
// if(GT_SwitchtoCardNo(1)) /* 將1 號卡設為當前卡(僅對于多卡系統,單卡系統可取消該行)*/
// nRet = 1;
if(GT_SetSmplTm(Sys_Control_Timer))//該函數設置運動控制器伺服周期。設定值的范圍為:48~1966.08 微秒運動控制器推薦使用的伺服周期是200 微秒。
nRet = 1;
for( i=1; i<=Sys_Max_Axis_Num; i++) //屏蔽每一軸的中斷
{
if(GT_Axis(i))
nRet = 1;
if(GT_SetIntrMsk(0))
nRet = 1;
if(GT_CtrlMode(1)) // 0表示為模擬電壓輸出模式,1 表示為脈沖輸
nRet = 1;
}
m_bIsCardInitialized = nRet ? FALSE : TRUE;
return nRet;
}
// 功 能: 檢測指定的軸是否是有效軸(1,2,3,4為有效軸)
//
// 返回值: 1表示無效軸
// 0表示有效軸
int Gt_Card_Api::check_axis_user(int axis)
{
int rtn = 1;
if((axis <= Sys_Max_Axis_Num)&&(axis > 0))
rtn = 0;
return rtn;
}
// 功 能: 設置指定軸的1um對應的脈沖數
//
// 參 數:axis的取值(1) axis <= 0 無效的參數
// (2) 1 , 2 , 3 ,4 時初始化指定的軸
// PerPlus的取值(1) 每轉脈沖數目
// PerGap的取值 (1) 每轉工作臺移動的距離
void Gt_Card_Api::set_axis_ratio(int axis,long PerPlus , long PerGap)
{
long tmpPos;
double tmpDub;
if(check_axis_user(axis))
return ;
tmpDub = PerPlus;
tmpDub /= (double)PerGap;
tmpPos = (long)(tmpDub *100000.0);
if(tmpPos != 0)
m_axis_ratio[axis] = tmpDub;
}
// 功 能:將指定軸對應的以um為單位的位置,轉換成以脈沖為單位的位置
//
// 參 數:axis的取值(1) axis <= 0 無效的參數
// (2) 1 , 2 , 3 ,4 時初始化指定的軸
// pos的取值 (1) 以um為單位的位置
//
// 返回值: 以脈沖為單位的位置
long Gt_Card_Api::cmd_to_motion_pos(int axis,long pos)
{
double tmpPos = m_axis_ratio[axis] * (double)pos;
return (long)tmpPos;
}
// 功 能:將指定軸對應的以脈沖為單位的位置,轉換成以um為單位的位置
//
// 參 數:axis的取值(1) axis <= 0 無效的參數
// (2) 1 , 2 , 3 ,4 時初始化指定的軸
// pos的取值 (1) 以um為單位的位置
//
// 返回值: 以um為單位的位置
{
double tmpPos = (double)pos / m_axis_ratio[axis];
return (long)tmpPos;
}
// 功 能:初始化軸指定的軸
//
// 參 數:axis的取值(1) axis <= 0 時初始化系統所有的軸
// (2) 1 , 2 , 3 ,4 時初始化指定的軸
//
// 返回值: 1表示有錯誤發生
// 0表示命令執行成功
// 功 能:控制使能或者關閉指定的軸
//
// 參 數: axis的取值 (1) axis <= 0 時使能或者關閉系統所有的軸
// (2) 1 , 2 , 3 ,4 時使能或者關閉指定的軸
//
// mode的取值 (1) Axis_On_Mode 打開軸驅動使能
// (2) Axis_Off_Mode 關閉軸驅動使能
//
// 返回值: 1表示有錯誤發生
// 0表示命令執行成功
int Gt_Card_Api::set_axis_on_off(int axis , int mode) //控制軸初始化函數
{
int i;
int k;
short rtn = 0;
if(axis > Sys_Max_Axis_Num)
return 1;
if(axis <= 0)
{
i = 1;
k = Sys_Max_Axis_Num;
}
else
{
i = axis;
k = axis;
}
for( ; i <= k ; i++)
{
if(GT_Axis(i))//設置第i 軸為當前軸
rtn = 1;
if(mode == Axis_On_Mode)
{
if(GT_AxisOn())//驅動使能
rtn = 1;
}
else
{
if(GT_AxisOff())//關閉軸驅動使能
rtn = 1;
}
}
return rtn;
}
// 功 能:設置指定的軸手動和回零時的運動參數
//
// 參 數: speed的取值(1) speed > 0 指定的軸向以speed的速度向正方向手動 (-16384 -- 16384)
// (2) speed < 0 指定的軸向以speed的速度向負方向手動
// (3) speed = 0 指定的軸停止運動
// acc的取值 (1) 指定的軸加速度 0 -- 16383
// 返回值: 1表示有錯誤發生
// 0表示命令執行成功
int Gt_Card_Api::set_jog_para(double speed , double acc)
{
int rtn = 0;
if(acc < 0.0 || acc > 16383.0)
acc_home_jog = 0.001;
else
acc_home_jog = acc;
if(speed < (-16384.0))
speed_jog = -16383.0;
else
if(speed > 16384.0)
speed_jog = 16383.0;
else
speed_jog = speed;
return rtn;
}
// 功 能:設置指定的軸手動和回零時的運動參數
//
// 參 數: speed1的取值(1) speed1 > 0 指定的軸向以speed1的速度向正方向回零 (-16384 -- 16384)
// (2) speed1 < 0 指定的軸向以speed1的速度向負方向回零
// (3) speed1 = 0 指定的軸停止運動
// acc的取值 (1) 指定的軸加速度 0 -- 16383
void Gt_Card_Api::set_home_para(double speed1 , double acc)
{
if(acc < 0.0 || acc > 16383.0)
acc_home_jog = 0.001;
else
acc_home_jog = acc;
if(speed1 < (-16384.0))
speed_home = -16383.0;
else
if(speed1 > 16384.0)
speed_home = 16383.0;
else
speed_home = speed1;
}
// 功 能:控制指定的軸回參考點
//
// 參 數: axis的取值 (1) axis <= 0 無效的參數
// (2) 1 , 2 , 3 ,4 時使指定的軸回參考點
//
// start_stop參數: 1 指定的軸回零
// 0 指定的回零動作停止
//
// 返回值: 1表示有錯誤發生
// 0表示命令執行成功
int Gt_Card_Api::set_axis_home(int axis,int start_stop)
{
int rtn = 0;
if(check_axis_user(axis))
return 1;
m_home_step[axis] = start_stop;
if(start_stop == 0)
{
if(GT_ClrSts())//清狀態
rtn = -1;
if(set_axis_stop(axis))
rtn = -1;
m_home_step[axis] = -1;
}
return rtn;
}
// 功 能:控制指定的軸進行回參考點動作,此函數需不斷執行,直到返回0或者-1
//
// 參 數: axis的取值 (1) axis <= 0 無效的參數
// (2) 1 , 2 , 3 ,4 時使指定的軸回參考點
//
// 返回值: 1 表示正在回零
// -1 表示回零失敗
// 0 表示回零完成
int Gt_Card_Api::set_axis_homeing(int axis)
{
int rtn = 1;
unsigned short status;
long actl_pos;
if(check_axis_user(axis))
return -1;
switch(m_home_step[axis])
{
case 0:
default:
break;
case 1:
if(GT_Axis(axis))//設置第i 軸為當前軸
rtn = -1;
if(GT_ClrSts())//清狀態
rtn = -1;
if(GT_CaptHome()) //設置捕獲Home
rtn = -1;
if(GT_PrflV()) //設置速度控制模式
rtn = -1;
axis_mode = 0;
if(GT_SetAcc(acc_home_jog))//設置加速度
rtn = 1;
if(GT_SetVel(speed_home)) // 設置速度為目標速度
rtn = 1;
if(GT_Update()) //刷新參數
rtn = -1;
m_home_step[axis] = 2;
break;
case 2:
if(GT_Axis(axis))//設置第i 軸為當前軸
rtn = -1;
if(GT_GetSts(&status))//讀取軸狀態值
rtn = -1;
if((status&0x8)) //等待Home 捕獲
{
m_home_step[axis] = 3;
}
break;
case 3:
if(GT_Axis(axis))//設置第i 軸為當前軸
rtn = -1;
if(GT_GetCapt(&actl_pos)) //讀取捕獲位置
rtn = -1;
if(GT_PrflS()) //設置S-曲線控制模式
rtn = 1;
axis_mode = 0;
if(GT_SetJerk(jerk_move_step))//設置加加速度
rtn = 1;
if(GT_SetMAcc(acc_move_step))//設置加速度為
rtn = 1;
if(GT_SetVel(speed_move_step)) // 設置速度為目標速度
rtn = 1;
if(GT_SetPos(actl_pos)) //設置捕獲位置為目標位置
rtn = -1;
if(GT_ClrSts()) //清狀態
rtn = -1;
if(GT_Update()) //刷新參數
rtn = -1;
m_home_step[axis] = 4;
break;
case 4:
if(check_axis_done(axis)) //等待運動完成
break;
if(GT_Axis(axis))//設置第i 軸為當前軸
rtn = -1;
if(GT_ZeroPos()) //清零
rtn = -1;
m_home_step[axis] = -1;
rtn = 0;
break;
}
return rtn;
}
// 功 能:控制指定的軸以某一速度一直運動
//
// 參 數: axis的取值 (1) axis <= 0 無效的參數
// (2) 1 , 2 , 3 ,4 時使指定的軸
// 返回值: 1表示有錯誤發生
// 0表示命令執行成功
int Gt_Card_Api::set_axis_jog(int axis)
{
int rtn = 0;
if(check_axis_user(axis))
return 1;
if(GT_Axis(axis))//設置第i 軸為當前軸
rtn = 1;
if(GT_PrflV()) //設置速度控制模式
rtn = 1;
axis_mode = 0;
if(GT_SetAcc(acc_home_jog))//設置加速度
rtn = 1;
if(GT_SetVel(speed_jog)) // 設置速度為目標速度
rtn = 1;
if(GT_Update()) //刷新參數
rtn = 1;
return rtn;
}
// 功 能:設置指定的軸步進或者單軸運動時的運動參數
//
// 參 數: speed的取值(1) speed > 0 指定的軸向以speed的速度運動0 -- 16383
// acc的取值 (1) 指定的軸加速度 0 -- 0.5
// jerk的取值 (1) 指定的軸加加速度 0 -- 0.5
void Gt_Card_Api::set_move_step_para(double speed ,double acc,double jerk)
{
if(jerk < 0.0 || jerk > 0.5)
jerk_move_step = 0.0001;
else
jerk_move_step = jerk;
if(acc < 0.0 || acc > 0.5)
acc_move_step = 0.001;
else
acc_move_step = acc;
if(speed < (0.0))
speed_move_step = 0.0;
else
if(speed > 16384.0)
speed_move_step = 16383.0;
else
speed_move_step = speed;
}
// 功 能:控制指定的軸以某一速度,以當前設定的目標位置為基礎,增量運行displacement指定的距離
//
//
// 參 數: axis的取值 (1) axis <= 0 無效的參數
// (2) 1 , 2 , 3 ,4 時使指定的軸
//
// displacement的取值(1) displacement > 0 指定的軸向以speed的速度向正方向運動
// (2) displacement < 0 指定的軸向以speed的速度向負方向運動
//
// speed的取值(1) speed > 0 指定的軸向以speed的速度運動0 -- 16383
// acc的取值 (1) 指定的軸加速度 0 -- 0.5
// jerk的取值 (1) 指定的軸加加速度 0 -- 0.5
//
// 返回值: 1表示有錯誤發生
// 0表示命令執行成功
int Gt_Card_Api::set_axis_step(int axis,long displacement)
{
int rtn = 0;
long actl_pos;
if(check_axis_user(axis))
return 1;
if(GT_Axis(axis))//設置第i 軸為當前軸
rtn = 1;
if(get_axis_den(axis)) //檢測指定軸是否在運動
return 1;
if(GT_PrflS()) //設置S-曲線控制模式
rtn = 1;
axis_mode = 0;
if(GT_SetJerk(jerk_move_step))//設置加加速度
rtn = 1;
if(GT_SetMAcc(acc_move_step))//設置加速度為
rtn = 1;
if(GT_SetVel(speed_move_step)) // 設置速度為目標速度
rtn = 1;
if(GT_GetPos(&actl_pos)) //Atl取得當前軸的實際位置
rtn = 1;
actl_pos += cmd_to_motion_pos(axis,actl_pos + displacement);
if(GT_SetPos(actl_pos)) //設置目標位置
rtn = 1;
if(GT_Update()) //刷新參數
rtn = 1;
return rtn;
}
// 功 能:控制指定的軸以某一速度,運動到指定的位置
//
//
// 參 數: axis的取值 (1) axis <= 0 無效的參數
// (2) 1 , 2 , 3 ,4 時使指定的軸
//
// position的取值(1) 指定的軸要運動到的位置
//
// speed的取值(1) speed > 0 指定的軸向以speed的速度運動
//
// 返回值: 1表示有錯誤發生
// 0表示命令執行成功
int Gt_Card_Api::set_axis_moveto(int axis,long position)
{
int rtn = 0;
if(check_axis_user(axis))
return 1;
if(GT_Axis(axis))//設置第i 軸為當前軸
rtn = 1;
if(get_axis_den(axis)) //檢測指定軸是否在運動
return 1;
if(GT_PrflS()) //設置S-曲線控制模式
rtn = 1;
axis_mode = 0;
if(GT_SetJerk(jerk_move_step))//設置加加速度
rtn = 1;
if(GT_SetMAcc(acc_move_step))//設置加速度為
rtn = 1;
if(GT_SetVel(speed_move_step)) // 設置速度為目標速度
rtn = 1;
position = cmd_to_motion_pos(axis , position);
if(GT_SetPos(position)) //設置捕獲位置為目標位置
rtn = 1;
if(GT_Update()) //刷新參數
rtn = 1;
return rtn;
}
// 功 能:平滑停止指定軸
//
//
// 參 數: axis的取值 (1) axis <= 0 平滑停止所有的軸
// (2) 1 , 2 , 3 ,4 時使指定的軸
//
// 返回值: 1表示有錯誤發生
// 0表示命令執行成功
int Gt_Card_Api::set_axis_stop(int axis)
{
int i;
int k;
int rtn = 0;
if(axis > Sys_Max_Axis_Num)
return 1;
if(axis <= 0)
{
i = 1;
k = Sys_Max_Axis_Num;
}
else
{
i = axis;
k = axis;
}
for( ; i <= k ; i++)
{
if(GT_Axis(i))//設置第i 軸為當前軸
{
rtn = 1;
}
if(GT_SmthStp())
{
rtn = 1;
}
if(GT_Update())
{
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -