C# 固高指定距离运动、jog运动

本文详细描述了一种使用C#编写的控制方法,涉及按钮事件处理,如点击和长按功能,以及在高精度设备调试中如何实现相对运动和Jog运动,同时关注了限位条件的检查。
摘要由CSDN通过智能技术生成

记录一下前人编写的一种控制方法,点击按钮可以相对运动指定的距离,长按可以实现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);
            }
        }

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值