using gts;using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Windows.Forms;namespace GtsDemon2{ public partial class FrmMain : Form { public FrmMain() { InitializeComponent(); //mc.GT_Open(0,1); this.Load = FrmMain_Load; } private CancellationTokenSource cts = new CancellationTokenSource(); private void FrmMain_Load(object sender, EventArgs e) { short rtn; // 打开运动控制器 rtn = mc.GT_Open(0, 1); Commandhandler("GT_Open", rtn); // 复位 rtn = mc.GT_Reset(); Commandhandler("GT_Reset", rtn); //cmb_Axis.SelectedIndex = 0; timer1.Interval = 100; //定时器间隔100ms timer1.Start(); //启动定时器 } public void Commandhandler(string cmd, short rtn) {
if (rtn != 0) {
MessageBox.Show(cmd + " = " + rtn.ToString()); } } private void Ini_Click(object sender, EventArgs e) {
short rtn; rtn = mc.GT_AlarmOff(1); //1轴关报警 Commandhandler("GT_AlarmOff", rtn); rtn = mc.GT_LmtsOff(1, -1);//1轴关闭所有限位 Commandhandler("GT_LmtsOff", rtn); rtn = mc.GT_ClrSts(1, 1);//1轴状态刷新 Commandhandler("GT_ClrSts", rtn); rtn =mc.GT_AxisOn(1);//1轴使能 Commandhandler("GT_AxisOn", rtn); //以此处为原点 rtn = mc.GT_ZeroPos(1, 1); //需要位置清零的起始轴号,正整数。需要位置清零的轴数 } private void timer1_Tick(object sender, EventArgs e) {
short rtn,axis, run; int sts, space, seg; uint clk; //double prfPos; double[] prfPos = new double[2]; double[] enfPos = new double[2]; axis = 3; //axis = Convert.ToInt16(cmb_Axis.SelectedIndex + 1); //获取轴状态: //rtn = mc.GT_GetSts(1, out sts,1, out clk); //sts显示报警状态 rtn = mc.GT_GetSts(axis, out sts, 1, out clk); //sts显示报警状态 //读取规划位置。 //起始规划轴号,规划位置,读取的轴数,读取控制器时钟 //rtn = mc.GT_GetPrfPos(1, out prfPos, 1, out clk); rtn = mc.GT_GetPrfPos(axis, out prfPos[0], 2, out clk); //读取2个轴 rtn = mc.GT_CrdSpace(axis, out space, 0); rtn = mc.GT_CrdStatus(axis, out run, out seg, 0); lbl_Space.Text = space.ToString(); lbl_Seg.Text = seg.ToString(); //(short crd, short *pRun, long *pSegment, short fifo=0) //crd 坐标系号。正整数,取值范围:[1, 2]。 //pRun 读取插补运动状态。 //0:该坐标系的该 FIFO 没有在运动;1:该坐标系的该 FIFO 正在进行插补运动。 // pSegment读取当前已经完成的插补段数。 // 当重新建立坐标系或者调用 GT_CrdClear 指令后,该值会被清零。 //fifo 所要查询运动状态的插补缓存区号。正整数,取值范围:[0, 1],默认值为:0 lbl_XPrfPos.Text = Convert.ToInt32(prfPos[0]).ToString(); lbl_YPrfPos.Text = Convert.ToInt32(prfPos[1]).ToString(); //lbl_XPrfPos.Text = Convert.ToInt32(prfPos).ToString(); //读取规划位置 //读取编码器位置 /* 指令原型 short GT_GetEncPos (short encoder, double *pValue, short count=1, unsigned long *pClock=NULL) 指令说明 读取编码器位置。 指令类型 立即指令,调用后立即生效。 章节页码 108 指令参数 该指令共有 4 个参数,参数的详细信息如下。 encoder 编码器起始轴号。正整数。 对于 14 轴控制器,取值范围:[1, 14],硬件上没有编码器接口的轴资源,软件上读不到 编码器值。 pValue 编码器位置。 count 读取的轴数。默认为 1。 1 次最多可以读取 8 个编码器轴。 pClock 读取控制器时钟。 */ rtn = mc.GT_GetEncPos(axis, out enfPos[0], 2, out clk); lbl_XEncPos.Text = Convert.ToInt32(enfPos[0]).ToString(); lbl_YEncPos.Text = Convert.ToInt32(enfPos[1]).ToString(); } private void btn_Trap_Click(object sender, EventArgs e) {
short rtn = 0; uint clk = 0; double prfPos; //位置 mc.TTrapPrm trap; //结构体对象 double vel, acc, dec; int pos; vel = double.Parse(tb_Vel.Text.Trim()); acc = double.Parse(tb_Acc.Text.Trim()); dec = double.Parse(tb_Dec.Text.Trim()); pos = int.Parse(tb_Pos.Text.Trim()); rtn = mc.GT_PrfTrap(1); // 将 1 轴设为点位模式 Commandhandler("GT_PrfTrap", rtn); rtn = mc.GT_GetTrapPrm(1,out trap); //读取点位运动参数(需要读取所有运动参数到上位机变量) Commandhandler("GT_GetTrapPrm", rtn); trap.acc = acc; trap.dec = dec; //trap.acc = 1; //trap.dec = 1; trap.smoothTime = 20;//范围0-50 rtn = mc.GT_SetTrapPrm(1, ref trap); //设置点位运动参数 Commandhandler("GT_SetTrapPrm", rtn); rtn = mc.GT_SetVel(1, vel); //rtn = mc.GT_SetVel(1, 10); //10脉冲每毫秒 Commandhandler("GT_SetVel", rtn); rtn = mc.GT_GetPrfPos(1, out prfPos, 1, out clk);//获取当前位置 //从当前位置运动, 当前位置+1000 //rtn = mc.GT_SetPos(1, Convert.ToInt32(prfPos)+1000); //1轴 走到1000脉冲 //设置速度 rtn = mc.GT_SetPos(1, pos); Commandhandler("GT_SetPos", rtn); //1 << 0; // 0001 -> 0001 = 1 //1 << 1; // 0001 -> 0010 = 2 //1 << 2; // 0001 -> 0100 = 4 //按位指示需要启动点位运动或 Jog 运动的轴号,bit0~bit13 分别对应 1~14 轴。 // Axis 轴号,如果是1轴不偏移(bit0), 2轴偏移一位(000 0000 0000 0010) //给1号轴参数 rtn = mc.GT_Update(1 << (1 - 1)); //mc.GT_Update(1 << (Axis - 1)); Commandhandler("GT_Update", rtn); } private void btn_Stop_Click(object sender, EventArgs e) {
short rtn; short axis; axis = (short)(cmb_Axis.SelectedIndex + 1); //停止运动 //short GT_Stop(long mask, long option) //mask 按位指示需要停止运动的轴号或者坐标系号。当 bit 位为 1 时表示停止对应的轴或者坐标系。 //option 按位指示停止方式。当 bit 位为 0 时表示平滑停止对应的轴或坐标系, //当 bit 位为 1 时表示急停对应的轴或坐标系。 rtn = mc.GT_Stop(1 << (axis - 1), 0); } public short jog(short axis, double vel, double acc, double dec) {
short rtn; rtn = mc.GT_PrfJog(axis); Commandhandler("GT_PrfJog", rtn); mc.TJogPrm jogPrm; rtn += mc.GT_GetJogPrm(axis, out jogPrm); Commandhandler("GT_GetJogPrm", rtn); jogPrm.acc = acc; jogPrm.dec = dec; jogPrm.smooth = 0.5; rtn += mc.GT_SetJogPrm(axis, ref jogPrm); Commandhandler("GT_SetJogPrm", rtn); rtn += mc.GT_SetVel(axis, vel); Commandhandler("GT_SetVel", rtn); rtn += mc.GT_Update(1 << (axis - 1)); Commandhandler("GT_Update", rtn); return rtn; } private void btn_Crd_Click(object sender, EventArgs e) {
mc.TCrdPrm crd = new mc.TCrdPrm(); //定义坐标系 /* 坐标系与规划器的映射关系。Profile[0..7]对应规划轴 1~8,如果规划轴没有对 应到该坐标系,则 profile[x]的值为 0;如果对应到了 X 轴,则 profile[x]为 1,Y 轴对应 为 2,Z 轴对应为 3,A 轴对应为 4。不允许多个规划轴映射到相同坐标系的相同坐标轴, 也不允许把相同规划轴对应到不同的坐标系,否则该指令将会返回错误值。每个元素的 取值范围:[0, 4]。 */ short[] profile = new short[8]; //起始规划轴号 int[] originPos = new int[8]; //原点 short rtn; rtn = mc.GT_GetCrdPrm(1, out crd); //查询坐标系参数 crd.dimension = 2; //二维坐标系 crd.evenTime = 10; //最小匀速时间 单位: 毫秒ms 每个插补段的最小匀速段时间。取值范围:[0, 32767)。单位:ms。 crd.synAccMax = 10; //最大加速度 单位:pulse/ms² crd.synVelMax = 100; //最大速度 单位:pulse/ms for (short i = 0; i < 8; i++) {
profile[i] = 0; originPos[i] = 0; } profile[2] = 1; //3轴为x轴 profile[3] = 2; //4轴为y轴 crd.profile = profile; //设置xy对应轴号 crd.setOriginFlag = 1; //手动设置坐标原点 crd.originPos = originPos; //设置坐标原点偏移量 //short crd, ref TCrdPrm pCrdPrm, short crd 坐标系号,取值范围:[1, 2]。 rtn = mc.GT_SetCrdPrm(1, ref crd); //建立坐标系 //XY 平面二维直线插补。 /* short GT_LnXY (short crd, long x, long y, double synVel, double synAcc, double velEnd=0,short fifo=0) crd 坐标系号。正整数,取值范围:[1, 2]。 x 插补段 x 轴终点坐标值。取值范围:[-1073741823, 1073741823],单位:pulse。 y 插补段 y 轴终点坐标值。取值范围:[-1073741823, 1073741823],单位:pulse。 synVel 插补段的目标合成速度。取值范围:(0, 32767),单位:pulse/ms。 synAcc 插补段的合成加速度。取值范围:(0, 32767),单位:pulse/ms 2 。 velEnd 插补段的终点速度。取值范围:[0, 32767),单位:pulse/ms。该值只有在没有使用前瞻预 处理功能时才有意义,否则该值无效。默认值为:0。 fifo 插补缓存区号。取值范围:[0, 1],默认值为:0。 指令返回值 若返回值为 1: */ //(short crd, long x, long y, double synVel, double synAcc, double velEnd = 0,short fifo = 0) rtn = mc.GT_LnXY(1, 1000, 2000, 1, 1, 10, 0); //直线插补 rtn = mc.GT_LnXY(1, 2000, 2000, 1, 1, 0, 0); /* crd 坐标系号。正整数,取值范围:[1, 2]。 x 圆弧插补 x 轴的终点坐标值。取值范围:[-1073741823, 1073741823],单位:pulse。 y 圆弧插补 y 轴的终点坐标值。取值范围:[-1073741823, 1073741823],单位:pulse。 xCenter 圆弧插补的圆心 x 方向相对于起点位置的偏移量。 yCenter 圆弧插补的圆心 y 方向相对于起点位置的偏移量。 circleDir 圆弧的旋转方向。 0:顺时针圆弧。 1:逆时针圆弧。 synVel 插补段的目标合成速度。取值范围:(0, 32767),单位:pulse/ms。 synAcc 插补段的合成加速度。取值范围:(0, 32767),单位:pulse/ms 2 。 velEnd 插补段的终点速度。取值范围:[0, 32767),单位:pulse/ms。该值只有在没有使用前瞻 预处理功能时才有意义,否则该值无效。默认值为:0。 fifo 插补缓存区号。正整数,取值范围:[0, 1],默认值为:0。 指令返回值 若返回值为 1: (1) 检查当前坐标系是否映射了相关轴。 (2) 检查是否向 fifo1 中传递数据,若是,则检查 fifo0 是否使用并运动,若运动,则返 回错误。 (3) 检查相应的 fifo 是否已满。 其他返回值:请参照指令返回值列表。 (short crd, long x, long y, double xCenter, double yCenter, short circleDir,double synVel, double synAcc, double velEnd=0, short fifo=0) */ rtn = mc.GT_ArcXYC(1, 2000, 2000, 0, 1000, 1, 1, 1, 0, 0);//圆弧插补,x的偏移量是0,y轴偏移量1000,逆时针画圆circleDir = 1 rtn = mc.GT_CrdStart(1, 0); //启动插补运动 mask:从bit0到bit1对应坐标系号。0:不启动该坐标系,1:启动该坐标系 //option:从bit0到bit1对应缓存区编号。bit0 对应坐标系 1,bit1 对应坐标系 2。 0:启动坐标系中 FIFO0 的运动,1:启动坐标系中 FIFO1 的运动。 } //连续插补 private void continual_Click(object sender, EventArgs e) {
mc.TCrdPrm crd = new mc.TCrdPrm(); short[] profile = new short[8]; int[] originPos = new int[8]; short rtn; int space; rtn = mc.GT_GetCrdPrm(1, out crd); crd.dimension = 2; //二维坐标系 crd.evenTime = 10; //最小匀速时间 ms crd.synAccMax = 10; //最大加速度 pluse/ms^2 crd.synVelMax =