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

一、通讯设定与连接√

说明与总结:

布尔量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);//显示反馈速度
        }

 

posted @ 2022-09-27 15:00  Chuanshuo96  阅读(420)  评论(0)    收藏  举报