一、运动控制平台轴卡通用类的封装
1.代码
1.1 参数
public struct AxisParameter
{
public double acc;
public double dec;
public double velStart;
public short smoothTime;
public int searchDistance;
}
1.2 轴的通用功能接口
public interface IAxisTool
{
short AxisRest();
short AxisInit(short axis);
short AxisAbsMove(short axis, AxisParameter parameter, int pos, double vel);
short AxisRelativeMove(short axis, AxisParameter parameter, int pos, double vel);
void AxisGoHome(short axis, AxisParameter parameter, double vel, double homeVel);
short GetAxisPrfPos(short axis, out double prfPos);
short GetAxisPrfVel(short axis, out double prfVel);
short GetAxisEncPos(short axis, out double encPos);
short GetAxisEncVel(short axis, out double encVel);
short CloseAxis();
short StopAxis(short axis);
}
1.3 雷赛DMC3800轴卡
public class DMC3800AxisTool
{
public short AxisAbsMove(short axis, AxisParameter parameter)
{
Debug.WriteLine("雷赛DMC3800型号的相对运动功能");
return 1;
}
public short AxisAbsMove(short axis, AxisParameter parameter, int pos, double vel)
{
throw new NotImplementedException();
}
public short AxisGoHome(short axis, AxisParameter parameter, int pos, double vel, double homeVel)
{
throw new NotImplementedException();
}
public short AxisInit(short axis)
{
throw new NotImplementedException();
}
public short AxisRelativeMove(short axis, AxisParameter parameter, int pos, double vel)
{
throw new NotImplementedException();
}
public short GetAxisEncPos(short axis, out double encPos)
{
throw new NotImplementedException();
}
public short GetAxisEncVel(short axis, out double encVel)
{
throw new NotImplementedException();
}
public short GetAxisPrfPos(short axis, out double prfPos)
{
throw new NotImplementedException();
}
public short GetAxisPrfVel(short axis, out double prfVel)
{
throw new NotImplementedException();
}
}
1.4 三菱FX5U
public class FX5UAxisTool
{
public short AxisAbsMove(short axis, AxisParameter parameter)
{
Debug.WriteLine("三菱FX5U型号的相对运动功能");
return 1;
}
public short AxisAbsMove(short axis, AxisParameter parameter, int pos, double vel)
{
throw new NotImplementedException();
}
public short AxisGoHome(short axis, AxisParameter parameter, int pos, double vel, double homeVel)
{
throw new NotImplementedException();
}
public short AxisInit(short axis)
{
throw new NotImplementedException();
}
public short AxisRelativeMove(short axis, AxisParameter parameter, int pos, double vel)
{
throw new NotImplementedException();
}
public short GetAxisEncPos(short axis, out double encPos)
{
throw new NotImplementedException();
}
public short GetAxisEncVel(short axis, out double encVel)
{
throw new NotImplementedException();
}
public short GetAxisPrfPos(short axis, out double prfPos)
{
throw new NotImplementedException();
}
public short GetAxisPrfVel(short axis, out double prfVel)
{
throw new NotImplementedException();
}
}
1.5 固高GTS800
public class GTS800AxisTool : IAxisTool
{
bool flag= false;
public short AxisRest()
{
short rtn = GT_Reset();
return rtn;
}
public short AxisInit(short axis)
{
short rtn = 0;
rtn = GT_Open(0, 1);
if (rtn == 0)
{
flag = true;
rtn = GT_LoadConfig("GTS800-VIP-Home.cfg");
rtn = GT_ClrSts(axis, 1);
} else
{
flag = false;
}
return rtn;
}
public short AxisAbsMove(short axis, AxisParameter parameter, int pos, double vel)
{
short rtn;
if (flag)
{
GT_ClrSts(axis, 1);
rtn = GT_PrfTrap(axis);
TTrapPrm trap;
trap.acc = parameter.acc;
trap.dec= parameter.dec;
trap.velStart= parameter.velStart;
trap.smoothTime = parameter.smoothTime;
rtn = GT_SetTrapPrm(axis, ref trap);
rtn = GT_SetPos(axis, pos);
rtn = GT_SetVel(axis, vel);
rtn = GT_Update(1 << (axis -1));
} else
{
rtn = -1111;
}
return rtn;
}
public void AxisGoHome(short axis, AxisParameter parameter, double vel, double homeVel)
{
uint clk;
Task.Run(() =>
{
int searchDis = parameter.searchDistance;
searchDis = searchDis == 0 ? 805306368 : searchDis;
int offset = 5000;
int direction = searchDis > 0 ? 1 : -1;
TTrapPrm trap;
trap.acc = parameter.acc;
trap.dec = parameter.dec;
trap.velStart = parameter.velStart;
trap.smoothTime =parameter.smoothTime;
AxisAbsMove(axis, parameter, searchDis, vel);
int status;
do
{
GT_GetSts(axis, out status, 1, out clk);
} while ((status & 0x400) != 0);
var limitType = direction == 1 ? "正" : "负";
Debug.WriteLine($"触发{limitType}限位");
direction = -direction;
GT_ClrSts(axis, 1);
GT_SetCaptureMode(axis, CAPTURE_HOME);
AxisAbsMove(axis, parameter, direction * 805306368, vel);
short capture;
int originPos;
do
{
GT_GetCaptureStatus(axis, out capture, out originPos, 1, out clk);
} while (capture == 0);
int newPos = originPos + direction * offset;
AxisAbsMove(axis,parameter, newPos, vel);
do
{
GT_GetSts(axis, out status, 1, out clk);
} while ((status & 0x400) != 0);
GT_ClrSts(axis, 1);
AxisAbsMove(axis, parameter,originPos, homeVel);
double prfPos;
do
{
GT_GetPrfPos(axis, out prfPos, 1, out clk);
GT_GetSts(axis, out status, 1, out clk);
} while ((status & 0x400) != 0);
if (prfPos == originPos)
{
GT_ZeroPos(axis, 1);
}
});
}
public short AxisRelativeMove(short axis, AxisParameter parameter, int pos, double vel)
{
throw new NotImplementedException();
}
public short GetAxisEncPos(short axis, out double encPos)
{
short rtn = GT_GetEncPos(axis,out encPos, 1, out uint clk);
return rtn;
}
public short GetAxisEncVel(short axis, out double encVel)
{
short rtn = GT_GetEncVel(axis, out encVel, 1, out uint clk);
return rtn;
}
public short GetAxisPrfPos(short axis, out double prfPos)
{
short rtn = GT_GetPrfPos(axis, out prfPos, 1, out uint clk);
return rtn;
}
public short GetAxisPrfVel(short axis, out double prfVel)
{
short rtn = GT_GetPrfVel(axis, out prfVel, 1, out uint clk);
return rtn;
}
public short CloseAxis()
{
short rtn = GT_Close();
return rtn;
}
public short StopAxis(short axis)
{
short rtn = GT_Stop(1 << (axis - 1), 0);
return rtn;
}
}
1.7 工厂类
public class AxisToolFactory
{
public static IAxisTool CreateInstance(AxisType type)
{
IAxisTool axisTool = null;
switch (type)
{
case AxisType.DMC3800:
break;
case AxisType.GTS800:
axisTool = new GTS800AxisTool();
break;
case AxisType.FX5U:
break;
default:
break;
}
return axisTool;
}
}
public enum AxisType
{
DMC3800,
GTS800,
FX5U
}