1、引入类库
引入雷塞的类文件库和dll,LTDMC.cs和LTDMC.dll


2、获取板卡基本信息
/// <summary>
/// 初始化卡
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Btn_Init_Click(object sender, EventArgs e)
{
short res;
// 初始化按钮
Btn_Init.Enabled = false;
Btn_Close.Enabled = true;
// 初始化卡
res = LTDMC.dmc_board_init();
if (res <=0 || res > 8)
{
Console.WriteLine("初始化轴卡失败");
return;
}
ushort cardNum = 0;
uint[] cardTypes = new uint[8];
ushort[] cardIds = new ushort[8];
// 获取卡号
res = LTDMC.dmc_get_CardInfList(ref cardNum, cardTypes, cardIds);
if (res == 0)
{
_CardNO = cardIds[0];
}
uint totalAxis = 0;
// 初始化卡轴号列表
res = LTDMC.dmc_get_total_axes(_CardNO, ref totalAxis);
if (res == 0)
{
comboBox_axis.Items.Clear();
for (ushort i = 0; i < totalAxis; i++)
{
axises[i] = i;
// 填充列表框的项
comboBox_axis.Items.Add($"{i}-Axis");
}
}
comboBox_axis.SelectedIndex= 0;
mb_init = true;
}
1.初始化轴卡
2.获取轴硬件id和固件版本
3.获取当前卡的轴数
(以上是一张卡的情况,所以CardTypeList[0]索引0位,如果存在多卡,则需要再做遍历)
3.关闭轴卡
/// <summary>
/// 关闭轴卡
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_close_Click(object sender, EventArgs e)
{
LTDMC.dmc_board_close();
}
4.运动参数设置
/// <summary>
/// 设置运动参数
/// </summary>
private void SetMoveParameters()
{
// 起始速度
double start = decimal.ToDouble(numericUpDown_startVel.Value);
// 运行速度
double speed = decimal.ToDouble(numericUpDown_vel.Value);
// 停止速度
double stop = decimal.ToDouble(numericUpDown_stopVel.Value);
// 获取加速时间
double acc = decimal.ToDouble(numericUpDown_acc.Value);
// 获取减速时间
double dec = decimal.ToDouble(numericUpDown_dec.Value);
// 获取S段时间
double s = decimal.ToDouble(numericUpDown_spara.Value);
// 获取当前选择的轴卡号
ushort axis = axises[comboBox_axis.SelectedIndex];
if (mb_init)
{
// 设置s段时间
short res = LTDMC.dmc_set_s_profile(_CardNO, axis, 0, s);
if (res != 0)
{
Console.WriteLine($"dmc_set_s_profile=={res}");
return;
}
// 设置单轴运动速度曲线
res = LTDMC.dmc_set_profile(_CardNO, axis, start, speed, acc, dec, stop);
if (res != 0)
{
Console.WriteLine($"dmc_set_profile=={res}");
return;
}
// 设置停止时间
res = LTDMC.dmc_set_dec_stop_time(_CardNO, axis, dec);
if (res != 0)
{
Console.WriteLine($"dmc_set_dec_stop_time=={res}");
return;
}
}
}
需要设置参数起始速度、运行速度、停止速度、加速时间、减速时间、S段时间(加减速时间)、停止时间,以上设置都必须继续当前轴的卡号_CardNO和轴号axis。
5.执行运动控制
/// <summary>
/// 执行运动功能实现
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_start_Click(object sender, EventArgs e)
{
// 设置运动参数
SetMoveParameters();
// 获取运行的距离
double dist = decimal.ToDouble(numericUpDown_movePos.Value);
// 获取运行模式
ushort mode = (ushort)(radioButton_rel.Checked ? 0 : 1);
if (mb_init)
{
// 执行运动(运行位置大于0 正向运动,小于0 负向运动)
short res = LTDMC.dmc_pmove(_CardNO, axises[comboBox_axis.SelectedIndex], (int)dist, mode);
if (res != 0)
{
Console.WriteLine($"dmc_pmove=={res}");
}
}
}
基于以上代码,运动需要设置相对运动或者绝对运行的模式选择mode,需要基于卡号_CardNO和轴号axises[comboBox_axis.SelectedIndex],最后结合运动的距离,直线运动执行。
