首页 > 编程语言 >Hiwin直线电机C#MPI控制程序<通讯设置、回原点设置、点动模式、点对点运动、依托Timer读取数据及数据采集>

Hiwin直线电机C#MPI控制程序<通讯设置、回原点设置、点动模式、点对点运动、依托Timer读取数据及数据采集>

时间:2022-09-27 15:12:01浏览次数:86  
标签:Hiwin C# StringBuilder pcom MPI int SetVarN new

一、通讯设定与连接√

说明与总结:

布尔量Connect为驱动器连接与否的标识符,启动时默认为false,即未连接状态;

 

     /*-----------------------------------------------*/
        /*                 连接驱动器               */
        /*-----------------------------------------------*/
        private void butt_Connect_Click(object sender, EventArgs e)
        {
            if (!Connect)//如果未连接,单击进行连接
            {
                MPI.setMFCmode();//setMFCmode:使用其他方法前,先使用此方法;
                MPI.disAllErrMsgBox(1);//disAllErrMsgBox:关闭所有通讯错误提示信息框;

                string path = "C:\\Thunder\\dce";

                string file ;
                if (combo_COM.SelectedIndex == 0)
                {
                    file = "D3.dce";
                }
                else 
                {
                    file = "D3COE.dce";
                }
                m_pcom = MPI.openDCE(new StringBuilder(path), new StringBuilder(file));//openDCE:建立 PC 与驱动器之间的通讯
                MPI.setComPar(0, 0, 5, 0, 0, 0, 0, 0, 100, 200, 100, m_pcom);//设定通讯参数,返回值为0表示方法执行成功
                //int setComPar(int port, int baudrate, int mode, int trid, int rcid,int canbaudrate,
                //int msgStand, int canpipelevel, int timeout,int locktime, int iternum, MDCE* pcom = NULL);

                if (Test_Connect())//如果Test_Connect()返回值为true,测试得到的结果为成功连接
                {
                    butt_Connect.Text = "断开连接";
                    MessageBox.Show(this, "连接成功!", "Communication", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Connect = true;
                    timer1.Enabled = true;//定时器开启
                }
                else//测试连接不成功
                {
                    timer1.Enabled = false;//定时器关闭
                    butt_Connect.Text = "连接";
                    MessageBox.Show(this, "连接失败!", "Communication", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Connect = false;
                }
            }
            else//如果已连接,单击则断开连接
            {
                timer1.Enabled = false;
                MotorEnabled(false); //MotorEnabled:激磁\解励磁电机
                MPI.deleteDCE(m_pcom, 0);//deleteDCE:结束PC与驱动器之间的通讯
                m_pcom = UIntPtr.Zero; 
                butt_Connect.Text = "Connect";
                MessageBox.Show(this, "Disconnect!", "Communication", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Connect = false;
            }
        }
        /*-----------------------------------------------*/
        /*                  测试连接                     */
        /*-----------------------------------------------*/
        private bool Test_Connect()
        {
            double vel, vel_temp;//最大速度Pt316的默认值vel_temp
            double setVel = 500.0;
            string vel_max = "Pt316";//Pt316最高速度
            unsafe
            {
                int x = MPI.GetVarN(new StringBuilder(vel_max), &vel_temp, 0, m_pcom);//GetVarN:取变量值
                Thread.Sleep(10);
                x = MPI.SetVarN(new StringBuilder(vel_max), setVel, 0, m_pcom);//赋值 (参数1 变量地址,参数2 变量值)
                Thread.Sleep(10);
                x = MPI.GetVarN(new StringBuilder(vel_max), &vel, 0, m_pcom);//取值
                Thread.Sleep(10);
                //MPI.SetVarN(new StringBuilder("Pt385"), 20, 0, m_pcom);//设置线性马达最大速度 20;单位:100mm/s
                //MPI.SetVarN(new StringBuilder("dVelCmd"), 100, 0, m_pcom);
            }
            if (vel == setVel)//如果最大速度写入成功,则改回默认速度并返回值为true
            {
                MPI.SetVarN(new StringBuilder(vel_max), vel_temp, 0, m_pcom);//将最大速度值更改未默认速度vel_temp
                Thread.Sleep(10);
                return true;
            }
            else 
            {
                return false;
            }
        }

二、电机使能/激磁√

 #region   MotorEnabled()电机使能函数
        /*-----------------------------------------------*/
        /*       使能,解使能电机                        */
        /*-----------------------------------------------*/
        private void MotorEnabled(bool enable)
        {
            unsafe
            {
                int sRdy = 0;
                string motor_ready = "S_RDY";
                string motor_enable = "X_en";

                MPI.getStateN(0, new StringBuilder(motor_ready), &sRdy, m_pcom);

                if (enable == true)
                {

                    MPI.SetVarN(new StringBuilder(motor_enable), 1, 0, m_pcom);
                    DateTime sDtime = DateTime.Now;
                    TimeSpan stTimeSpan = new TimeSpan(DateTime.Now.Ticks);
                    TimeSpan ltTimeSpan = new TimeSpan(DateTime.Now.Ticks);

                    while (MPI.getStateN(0, new StringBuilder(motor_ready), &sRdy, m_pcom) == 0)
                    {
                        if (sRdy == 1)
                            break;

                        ltTimeSpan = new TimeSpan(DateTime.Now.Ticks);
                        if (ltTimeSpan.TotalMilliseconds - stTimeSpan.TotalMilliseconds >= 1000 * 60)
                        {
                            Debug.WriteLine("Timeout");
                            break;
                        }
                        Thread.Sleep(10);
                    }
                    
                }
                else
                {

                    MPI.SetVarN(new StringBuilder(motor_enable), 0, 0, m_pcom);
                    DateTime sDtime = DateTime.Now;
                    TimeSpan stTimeSpan = new TimeSpan(DateTime.Now.Ticks);
                    TimeSpan ltTimeSpan = new TimeSpan(DateTime.Now.Ticks);

                    while (MPI.getStateN(0, new StringBuilder(motor_ready), &sRdy, m_pcom) == 0)
                    {
                        if (sRdy == 0)
                            break;

                        ltTimeSpan = new TimeSpan(DateTime.Now.Ticks);
                        if (ltTimeSpan.TotalMilliseconds - stTimeSpan.TotalMilliseconds >= 1000 * 60)
                        {
                            Debug.WriteLine("Timeout");
                            break;
                        }
                        Thread.Sleep(10);
                    }
                    
                }
            }
        }
        #endregion

 

 

三、点动调节设定√

 

        private void JogMove(int dir)
        {
            if (Connect == false)
                return;

            unsafe
            {
                string XvcJogDir = "X_vctr_jog_dir";

                try
                {
                    if (dir > 0)
                    {
                        MPI.SetVarN(new StringBuilder(XvcJogDir), 1, 0, m_pcom);
                    }
                    else if (dir < 0)
                    {
                        MPI.SetVarN(new StringBuilder(XvcJogDir), -1, 0, m_pcom);
                    }
                    else
                    {
                        MPI.SetVarN(new StringBuilder(XvcJogDir), 0, 0, m_pcom);
                    }
                }
                catch (Exception ex)
                {
                    MPI.SetVarN(new StringBuilder(XvcJogDir), 0, 0, m_pcom);
                    Debug.WriteLine(ex.Message);
                }
                finally
                { 

                }
            }
        }

 

四、回原点设定√

        private void btn_rHome_Click(object sender, EventArgs e)
        {
            if (!Connect)
                return;
            ChangeMode(0);
            unsafe
            {
                MPI.SetVarN(new StringBuilder("mTestRun"), 1, 0, m_pcom);    // 切换至内部位置模式
                MPI.SetVarN(new StringBuilder("Pt700"), 1, 0, m_pcom);       //设定回原点方法
                MPI.SetVarN(new StringBuilder("Pt705"), 10, 0, m_pcom);      //直线快速回原点   (单位: mm/s)
                MPI.SetVarN(new StringBuilder("Pt706"), 3, 0, m_pcom);       //直线慢速回原点   (单位: mm/s)
                MPI.SetVarN(new StringBuilder("Pt703"), 30, 0, m_pcom);      //回原点时间限制   (单位: 秒)
                MPI.SetVarN(new StringBuilder("Pt707"), 100, 0, m_pcom);     //回原点加速时间   单位ms
                MPI.SetVarN(new StringBuilder("Pt708"), 100, 0, m_pcom);     //回原点减速时间   单位ms
                MPI.SetVarN(new StringBuilder("Pt709"), 10, 0, m_pcom);      //回原点紧急减速时间   (单位: ms)
                MPI.SetVarN(new StringBuilder("Pt704"), 0, 0, m_pcom);       //原点偏移量       (单位: control unit)
                
                MPI.SetVarN(new StringBuilder("mHome"), 1, 0, m_pcom);       //执行回原点动作   //0-不执行 1-执行 2-暂停
            }
            
        }

 

五、点对点运动设定

 

        private void button_Move_Click(object sender, EventArgs e)
        {
            if (!Connect)
                return;

            if (!checkBox_con1.Checked || !checkBox_con2.Checked || !checkBox_con3.Checked || double.Parse(lab_Position.Text) > 10 || double.Parse(lab_Position.Text) < -10)
            {
                MessageBox.Show(this, "测试前请进行安全检查!", "安全检查", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            //点对点运动参数设定
            ChangeMode(1);//切换至点对点模式
            setAcc = double.Parse(textBox_acc.Text);//用户输入的加速度
            setAccT = double.Parse(textBox_acctime.Text);//用户输入的加速时间
            MPI.SetVarN(new StringBuilder("mTestRun"), 1, 0, m_pcom);//切换至位置模式
            MPI.SetVarN(new StringBuilder("Pt534"), setAccT, 0, m_pcom); //加速时间设置
            MPI.SetVarN(new StringBuilder("Pt537"), setAccT, 0, m_pcom);//减速时间设置            
            MPI.SetVarN(new StringBuilder("Pt585"), setAccT * g * setAcc, 0, m_pcom);//直线电机加速后达到的速度设置(单位: mm/s)

            string str_trg_ctrl_unit = textBox_trgPosCtrlUnit.Text;     ///输入运动位置 单位mm   (單位: control unit)
            str_trg_ctrl_unit.Trim();
            int.TryParse(str_trg_ctrl_unit, out trg_pos);  ////trg_pos  为SetVarN()中的位置量变量            

            uint.TryParse(textBox_Sam.Text, out SetSam);

            unsafe
            {
                try
                {
                    InvokeMoveButton(false);
                    button_Move.Text = "正在测试";
                    button_Move.Enabled = false;
                    InvokeShowInformation("");

                    arrayVel = new double[0];
                    //arrayCur = new double[0];/////删除后会累积每次数据
                    //arrayAcc = new double[0];
                    //arrayPos = new double[0];

                    Thread _thread0 = new Thread(() =>
                    {
                        Thread.Sleep(X_trg_delay);

                        double rev;
                        MPI.GetVarN(new StringBuilder("X_cntperunit"), &rev, 0, m_pcom);       ///每转一圈的count数或每移动100mm的count数
                        trg_pos = (int)(rev * trg_pos / 100);   //线性马达上位机文本框的mm数转count数  旋转电机根据传动比
                        string trage_pos = "X_trg";                   //////////X_trg为位置设置变量    count
                        MPI.SetVarN(new StringBuilder(trage_pos), trg_pos, 0, m_pcom);    /////设置位置,并开始运动  

                        while (true)
                        {
                            Thread.Sleep(3);
                            drivestate_temp = 0;
                            int drivState = 0;
                            string coin_state = "COIN";        ////定位完成输出信号
                            MPI.getStateN(0, new StringBuilder(coin_state), &drivState, m_pcom);
                            if (drivState == 1)
                            {
                                //OnCloseMTimer();////////退出微秒定时器
                                drivestate_temp = 1;
                                double Position = 0;
                                string enc_pos = "X_enc_pos";
                                MPI.GetVarN(new StringBuilder(enc_pos), &Position, 0, m_pcom);

                                break;
                            }
                            else
                                drivestate_temp = 0;
                        }
                    });
                    _thread0.Start();
                }
                catch (Exception exp)
                {
                    InvokeShowInformation(exp.Message);
                    throw exp;
                }
            }
        }

 

 

 

六、数据采集与曲线展示

        private void timer1_Tick(object sender, EventArgs e)
        {
            double Position, rev;
            string enc_pos = "X_enc_pos";
            string cntperuint = "X_cntperunit";

            double FbVelocity;//反馈速度接收变量
            string dfbvel = "dFbVel";//反馈速度地址

            unsafe
            {
                MPI.GetVarN(new StringBuilder(enc_pos), &Position, 0, m_pcom);
                MPI.GetVarN(new StringBuilder(cntperuint), &rev, 0, m_pcom);

                MPI.GetVarN(new StringBuilder(dfbvel), &FbVelocity, 0, m_pcom);    //读取反馈速度
                Thread.Sleep(10);
            }
            Position /= rev;
            Position *= 360;
            lab_Position.Text = string.Format("{0,-6:f}", Position);
            lbl_dFbVel.Text = string.Format("{0:f0}", FbVelocity);//显示反馈速度
        }

 

标签:Hiwin,C#,StringBuilder,pcom,MPI,int,SetVarN,new
From: https://www.cnblogs.com/legendY/p/16547430.html

相关文章

  • MyCAT实现MySQL读写分离
    MySQL中间件:用户连接到MySQL的中间件(代理),中间件接收用户的访问转发给后端的mysql数据库。MyCat:是MySQL的一个中间件软件,Mycat是一个开源的分布式数据库系统,是一个实现......
  • 误差修正ECM模型怎么分析?
    在宏观计量经济研究中,通常会使用VAR模型研究多个时间经济变量之间的数量关系情况,当数据不平稳但满足同阶单整时,通常使用协整检验研究长期均衡关系。与此同时,还可使用误差修......
  • # react 使用 redux 状态管理器
    react使用redux状态管理器学习资料英文文档:https://redux.js.org/中文文档:http://www.redux.org.cn/Github地址:https://github.com/reactjs/redux是什么redux......
  • Hbuilderx中mac链接android模拟器
    1、打开了mumu模拟器。2、hBuilderX=>运行=>abd路径设置=>只需要填写android模拟器端口7555(网易mumu模拟器对应的,每个模拟器有自己的,具体查看文档)3、./adbki......
  • 【code基础】顺序数组的去重
    掌握顺序数组的去重,对后续算法的去重剪枝,降低时间复杂度很有用,其基本思路如下:1.双指针,k和i。其中k指向不重复的位置,i为数组的遍历指针2.对数组进行遍历,其中i可以从1开......
  • cesium customShader实现建筑泛光效果
    在cesium1.87版本及以上,可使customShader用实现建筑泛光效果,代码如下//Cesium3dtile泛光效果实现(需版本1.87以上,支持customShader)constviewer=newCesium.Viewer(......
  • hbuilderx 编译报错cannot find module ‘yallist’
    更新后的Hbuilder X报错error:cannotfindmodule‘yallist’解决方案打开huilberx-工具-插件安装  卸载重装,重启hbuilderx。重新编译即可......
  • C#-等待异步函数执行结果
    另把你需要调用的异步函数变为普通的执行顺序,即非异步执行顺序经过查阅,只需要一句:varresult=Task.Run(async()=>awaityourAsyncMethod()).Result;对于没有返回......
  • c语言常见文件操作函数——文本流
    一、文件的打开关闭指向一个保存打开文件信息的结构体变量,该结构体类型为'FILE'。在'stdio.h'中声明:struct_iobuf{char*_ptr;int_cnt;char*_base;......
  • 解决pip is configured with locations that require TLS/SSL问题
    解决pipisconfiguredwithlocationsthatrequireTLS/SSL问题python3.7安装,解决pipisconfiguredwithlocationsthatrequireTLS/SSL问题1.安装相关依赖yum......