记录一下前人编写的一种控制方法,点击按钮可以相对运动指定的距离,长按可以实现jog运动,很适合高精度设备调试。
主要学习固高和C#事件的使用方法,直接把函数粘贴上来吧!
private void btn_y_motion_MouseDown(object sender, MouseEventArgs e)
{
try
{
Button btn = sender as Button;
curr_btn_ = btn;
//判断是多少距离的按钮
int tag = Convert.ToInt32(btn.Tag);
//设置按钮颜色
SetBtnSelectedBackColor(CaAxes.kY, tag);
is_mouse_down_ = true;
is_mouse_up_ = false;
int num = 0;
while (num < 50) // 0.5s判断jog运动
{
if (is_mouse_up_)
{
return;
}
HBase.DelayMs(10);
num++;
}
is_jogging_ = true;
// 按下0.5s判断jog运动
while (is_jogging_)
{
if (is_mouse_up_)
{
return;
}
//判断方向
int pom = (e.Button == MouseButtons.Left) ? 1 : -1;
//判断要移动的距离
double pos = Math.Pow(10, tag - 3) * pom;
//获取当前位置
double curr_pos = sg_.mc.GetPos(sg_.settings.axesPmI[1].sn);
//判断限位
if (pos > 0 && (curr_pos + pos) > sg_.settings.axesPmI[1].forward_limit)
{
ClearBtnBackColor(CaAxes.kY);
is_mouse_down_ = false;
is_mouse_up_ = true;
is_jogging_ = false;
curr_btn_ = null;
sg_.mc.Stop(sg_.settings.axesPmI[1].sn);
DlgAlarm.Show("行程超出正向软限位!", 0);
break;
}
else if (pos < 0 && (curr_pos - pos) < sg_.settings.axesPmI[1].negative_limit)
{
ClearBtnBackColor(CaAxes.kY);
is_mouse_down_ = false;
is_mouse_up_ = true;
is_jogging_ = false;
curr_btn_ = null;
sg_.mc.Stop(sg_.settings.axesPmI[1].sn);
DlgAlarm.Show("行程超出负向软限位!", 0);
break;
}
is_jogging_ = true;
bool direction = (e.Button == MouseButtons.Left);
//启动jog,类似大多数,set参数update就好了
sg_.mc.Jog(sg_.settings.axesPmI[1].sn, GetJogMotionParam(1, tag), direction);
HBase.DelayMs(10);
}
}
catch (Exception ex)
{
DlgAlarm.Show(ex.Message, 1);
}
}
private void btn_y_motion_MouseUp(object sender, MouseEventArgs e)
{
try
{
Button btn = sender as Button;
if (is_jogging_)
{
//在运动就停止
sg_.mc.Stop(sg_.settings.axesPmI[1].sn);
}
else
{
//进到这里就是点位运动
int tag = Convert.ToInt32(btn.Tag);
int pom = (e.Button == MouseButtons.Left) ? 1 : -1;
double pos = Math.Pow(10, tag - 3) * pom;
double curr_pos = sg_.mc.GetPos(sg_.settings.axesPmI[1].sn);
if (pos > 0 && (curr_pos + pos) > sg_.settings.axesPmI[1].forward_limit)
{
ClearBtnBackColor(CaAxes.kY);
is_mouse_down_ = false;
is_mouse_up_ = true;
is_jogging_ = false;
curr_btn_ = null;
sg_.mc.Stop(sg_.settings.axesPmI[1].sn);
DlgAlarm.Show("行程超出正向软限位!", 0);
return;
}
else if (pos < 0 && (curr_pos - pos) < sg_.settings.axesPmI[1].negative_limit)
{
ClearBtnBackColor(CaAxes.kY);
is_mouse_down_ = false;
is_mouse_up_ = true;
is_jogging_ = false;
curr_btn_ = null;
sg_.mc.Stop(sg_.settings.axesPmI[1].sn);
DlgAlarm.Show("行程超出负向软限位!", 0);
return;
}
//相对运动指定距离
sg_.mc.MoveRel(sg_.settings.axesPmI[1].sn, sg_.settings.GetAxisMpPtp(1), pos);
}
ClearBtnBackColor(CaAxes.kY);
is_mouse_down_ = false;
is_mouse_up_ = true;
is_jogging_ = false;
curr_btn_ = null;
}
catch (Exception ex)
{
DlgAlarm.Show(ex.Message, 1);
}
}