轨迹运动功能

int Row, Col; double[,] Date; int wether_loading_pointa = 0;
        public static double[] G_X; public static double[] G_Y; public static double[] G_Z; public static double[] posi_mode;
        private void button_datainput_Click(object sender, EventArgs e)
        {
            if (machine_origin == 0)
            {
                MessageBox.Show("原点未校准!");
                return;
            }
            OpenFileDialog openDlg = new OpenFileDialog();
            openDlg.Title = "打开数据文件";
            openDlg.Filter = "数据文件 (*.txt)|*.txt|All|*.*";
            bool result = false;
            string FilePath;
            string[] TempData;
            string[] TempArry;
            Row = 0;
            Col = 0;

            if (openDlg.ShowDialog() == DialogResult.OK)
            {
                FilePath = openDlg.FileName;
                TempData = System.IO.File.ReadAllLines(FilePath, System.Text.Encoding.Default);
                Row = TempData.GetLength(0);                                              //行数    
                TempArry = TempData[1].Split(',');
                Col = TempArry.GetLength(0);                                             //列数
                Date = new double[Row, Col];
                for (int i = 0; i < Row; i++)
                {
                    string[] Arry = TempData[i].Split(',');
                    for (int j = 0; j < Col; j++)
                    {
                        Date[i, j] = Convert.ToDouble(Arry[j]);
                    }
                }
                result = true;
            }
            if (result)
            {
                G_X = new double[Row]; G_Y = new double[Row]; G_Z = new double[Row];
                for (int j = 0; j < Row; j++)
                {
                    posi_mode[j] = Date[j, 0];//插补模式
                    G_X[j] = Date[j, 1]; G_Y[j] = Date[j, 2]; G_Z[j] = Date[j, 3];
                }
                wether_loading_pointa = 1;
                MessageBox.Show("路径读取完成!");
            }
            else
            {
                MessageBox.Show("路径读取错误!");
                return;
            }
            textBox_pointnum.Text = Convert.ToString(Row);  

        }
  private void button_run_Click(object sender, EventArgs e)
        {
            if (wether_link == 0)
            {
                MessageBox.Show("请先打开设备!");
                return;
            }
            if (SF == 0)
            {
                MessageBox.Show("伺服未开启!");
                return;
            }
            if (LTSMC.smc_check_done_multicoor(CardNum, 0) == 0)
            {
                MessageBox.Show("电机运动中,请稍后尝试!", "提示");
                return;
            }
            if (wether_loading_pointa == 0)
            {
                MessageBox.Show("未导入轨迹!", "提示");
                return;
            }

            for (int i = 0; i < axis_all.Length; i = i + 1)
            {
                LTSMC.smc_set_alm_mode(CardNum, axis_all[i], 0, 0, 0);//设置报警使能,关闭报警
                LTSMC.smc_write_sevon_pin(CardNum, axis_all[i], 0);//打开伺服使能               
            }
            double speed = decimal.ToDouble(run_speed.Value) * (fp / (2 * Math.PI));      //单位:unit/s
            ThreadPool.QueueUserWorkItem(
            delegate
            {
                int start_num;
                ushort[] AxisList = { 0, 1, 2 };
                LTSMC.smc_conti_set_lookahead_mode(CardNum, 0, 0, 0, 0, 0);
                LTSMC.smc_conti_open_list(CardNum, 0, 3, AxisList);
                LTSMC.smc_set_vector_profile_unit(CardNum, 0, speed, speed, acc / 2, dec / 2, speed);//设置插补运动速度参数
                LTSMC.smc_set_vector_s_profile(CardNum, 0, 0, acc / 2);//设置s曲线平滑时间
                //LTSMC.smc_conti_change_speed_ratio(CardNum, 0, 0.1);    //设置加工起始速度:50*0.1

                if (Row < 500)
                {
                    start_num = 0;
                    loadinging_interpolation(start_num, Row);
                    processing = 1;
                    LTSMC.smc_conti_start_list(CardNum, 0);//启动插补
                    LTSMC.smc_conti_close_list(CardNum, 0);//关闭插补缓冲区
                }
                else   
                {
                    start_num = 0;
                    loadinging_interpolation(start_num, 500);
                    int loaded_num = 500; int memory_num; int current_loading_num = 40;
                    processing = 1;
                    LTSMC.smc_conti_start_list(CardNum, 0);//启动插补
                    while (loaded_num < Row)
                    {
                        memory_num = LTSMC.smc_conti_remain_space(CardNum, 0);
                        if (memory_num >= current_loading_num)
                        {
                            if (Row - loaded_num >= current_loading_num)
                            {
                                start_num = loaded_num;
                                loadinging_interpolation(start_num, current_loading_num);
                                loaded_num = loaded_num + current_loading_num;
                            }
                            else
                            {
                                start_num = loaded_num;
                                loadinging_interpolation(start_num, Row - loaded_num);
                                loaded_num = Row;
                            }
                        }
                        else
                        {
                            Thread.Sleep(10);
                        }
                    }
                    LTSMC.smc_conti_close_list(CardNum, 0);//关闭插补缓冲区
                }
            }
            );
        }
        public void loadinging_interpolation(int start_num, int loading_point_num)
        {
            ushort[] AxisList = { 0, 1, 2 };
            for (int i = start_num; i < start_num + loading_point_num; i = i + 1)
                if (posi_mode[i] == 0)//直线插补运动模式
                {
                    LTSMC.smc_conti_line_unit(CardNum, 0, 3, AxisList, new double[] { Inverse.C1[i, 0], Inverse.C1[i, 1], Inverse.C1[i, 2] }, 0, 0);//运动模式,0: 相对坐标模式,跑相对坐标模式,首先回到原点
                }
                else
                {
                    MessageBox.Show("轨迹点格式错误!");
                }
        }

        //暂停
        int processing = 1;
        private void button_stop0_Click(object sender, EventArgs e)
        {
            if (processing == 1)
            {
                LTSMC.smc_conti_pause_list(CardNum, 0);
                button_stop0.Text = "继续";
                button_stop0.ForeColor = Color.Blue;
                processing = 0;
            }
            else
            {
                LTSMC.smc_conti_start_list(CardNum, 0);
                button_stop0.Text = "暂停";
                button_stop0.ForeColor = Color.Red;
                processing = 1;
            }
        }
        //停止
        private void button_stop1_Click(object sender, EventArgs e)
        {
            button_stop0.Text = "暂停";
            progressBar_processing.Value = 0;
            LTSMC.smc_set_pwm_output(CardNum, 0, 0, 0);
            wether_loading_pointa = 0;
            LTSMC.smc_conti_stop_list(CardNum, 0, 0);//停止模式, 0: 减速停止, 1: 立即停止
        }
        //程序关闭
        private void main_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result_zero = MessageBox.Show("机器是否已回零?", "提示:",
               MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result_zero == DialogResult.Yes)
            {  
                LTSMC.smc_write_outbit(CardNum, 17, 1);    //打开抱闸的IO(0,低电平。1,高电平打开抱闸)
                LTSMC.smc_conti_stop_list(CardNum, 0, 0);//停止模式, 0: 减速停止, 1: 立即停止              
                LTSMC.smc_set_pwm_output(CardNum, 0, 0, 0);
                LTSMC.smc_handwheel_set_mode(CardNum, 0, 0);//设置手轮运动模式,硬件或软件模式
                LTSMC.smc_handwheel_stop(CardNum);//手轮关闭
                for (int i = 0; i < axis_all.Length; i = i + 1)
                {
                    LTSMC.smc_write_sevon_pin(CardNum, axis_all[i], 1);//关闭伺服使能               
                }
                timer4.Enabled = true;
                timer4.Interval = 500;//执行间隔时间,单位为毫秒    
                timer4.Start();
                Application.ExitThread();
            }
            if (result_zero == DialogResult.No)
            {
                e.Cancel = true;
                DialogResult result_ifzero = MessageBox.Show("机器是否现在回零?", "提示:",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result_ifzero == DialogResult.Yes)
                {
                    comeback();
                }
                if (result_ifzero == DialogResult.No)
                {
                    this.Show();
                }
            }
        }

 

上一篇:定点移动与实验


下一篇:重置SMC与重置NVRAM(PRAM)