Beispiel #1
0
        private void 示教路径_Click(object sender, EventArgs e)
        {
            if (示教路径.Text == "示教路径")
            {
                if (Usart.IsOpen == false)
                {
                    MessageBox.Show("请开启串口!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                //清空txt文档
                FileStream fs = new FileStream(RecieveTeachingPathInf, FileMode.Create, FileAccess.Write);

                fs.Close();

                示教路径.Text = "完成接收";
                usartMode = "示教路径";

                实时路径.Enabled        = false;
                TimeReadCom.Enabled = true;
            }
            else
            {
                PathClear = "示教路径";
                NoticeInformation.Text = "";
                示教路径.Text           = "示教路径";
                usartMode           = "";
                TimeReadCom.Enabled = false;
                实时路径.Enabled        = true;

                FileTxt.FileTxtCopy(RecieveTeachingPathInf, PathAllInf);

                viewControl.Refresh();
            }
        }
        //计算所绘制B样条曲线关键点路径长度,并存在txt文件中,用于切分姿态角度所用
        static public void CalculateBsplineInfo(string txtAdress)
        {
            //创建临时文件
            StreamWriter tempFile = new StreamWriter(Directory.GetCurrentDirectory() + "\\eeww.txt", true);

            //////////////////////////////////从TXT文档读取数据点/////////////////////////////////////
            //数据读取
            StreamReader  pathFile   = File.OpenText(txtAdress);
            List <string> tempString = new List <string>();

            while (!pathFile.EndOfStream)
            {
                tempString.Add(pathFile.ReadLine());
            }

            pathFile.Close();

            //如果点数小于2,不能规划出轨迹
            if (tempString.Count() < 2)
            {
                return;
            }

            List <PointF> tempPnts = new List <PointF>();

            Pen myPen = new Pen(Color.Black, 1);

            foreach (string str in tempString)
            {
                string[] sArray = str.Split(',');
                tempPnts.Add(new PointF(float.Parse(sArray[0]), float.Parse(sArray[1])));
            }

            System.Drawing.PointF[] tempPoint = tempPnts.ToArray();
            //////////////////////////////////////////////////////////////////////////////////////////////


            int num = tempPoint.Count();

            float[] pntLen = new float[num];
            //系数矩阵对角列
            float[] a = new float[num];
            //系数矩阵对角上列
            float[] b = new float[num];
            //系数矩阵对角下列
            float[] c = new float[num];

            //定义soluctionX、soluctionY为线性方程的解
            float[] soluctionX = new float[num];
            float[] soluctionY = new float[num];
            //定义dataX和dataY,用来存放inPoint里的X和Y坐标
            float[] dataX = new float[num];
            float[] dataY = new float[num];
            //定义controlPoint用来存放控制点
            PointF[] controlPoint = new PointF[num + 4];
            //存放画线的两个使用点
            PointF[] lines = new PointF[2];

            //初始化 a,b,c
            a[0]       = 18;
            a[num - 1] = 18;

            for (int i = 1; i < num - 1; i++)
            {
                a[i] = 4;
            }

            for (int i = 1; i < num - 1; i++)
            {
                b[i] = 1;
                c[i] = 1;
            }

            c[num - 1] = -9;
            b[0]       = -9;



            for (int i = 0; i < num; i++)
            {
                dataX[i] = 6.0f * tempPnts[i].X;
                dataY[i] = 6.0f * tempPnts[i].Y;
            }

            dataX[0]       *= 1.5f;
            dataY[0]       *= 1.5f;
            dataX[num - 1] *= 1.5f;
            dataY[num - 1] *= 1.5f;



            //计算outdataX,outdataY;

            Calculate math = new Calculate();

            //调用Matrix用追赶法求解线性方程
            math.Matrix(dataX, num, ref a, ref b, ref c, ref soluctionX);
            math.Matrix(dataY, num, ref a, ref b, ref c, ref soluctionY);



            controlPoint[num + 3].X = dataX[num - 1] / 9;
            controlPoint[num + 2].X = dataX[num - 1] / 9;
            controlPoint[0].X       = dataX[0] / 9;
            controlPoint[1].X       = dataX[0] / 9;


            for (int i = 0; i < num; i++)
            {
                controlPoint[i + 2].X = soluctionX[i];
            }

            controlPoint[num + 3].Y = dataY[num - 1] / 9;
            controlPoint[num + 2].Y = dataY[num - 1] / 9;
            controlPoint[0].Y       = dataY[0] / 9;
            controlPoint[1].Y       = dataY[0] / 9;


            for (int i = 0; i < num; i++)
            {
                controlPoint[i + 2].Y = soluctionY[i];
            }



            float threshold = 1.0f;

            while (true)
            {
                //计算型值点,画出曲线
                //从初始点开始
                lines[0] = tempPoint[0];

                pntLen[0] = 0.0f;

                int count = 1;

                float length = 0.0f;
                for (int i = 0; i < num + 1; i++)
                {
                    for (float u = 0.01f; u <= 1; u += 0.01f)
                    {
                        float b0 = 1.0f / 6 * (1 - u) * (1 - u) * (1 - u);
                        float b1 = 1.0f / 6 * (3 * u * u * u - 6 * u * u + 4);
                        float b2 = 1.0f / 6 * (-3 * u * u * u + 3 * u * u + 3 * u + 1);
                        float b3 = 1.0f / 6 * u * u * u;

                        lines[1].X = (b0 * controlPoint[i].X + b1 * controlPoint[i + 1].X + b2 * controlPoint[i + 2].X + b3 * controlPoint[i + 3].X);
                        lines[1].Y = (b0 * controlPoint[i].Y + b1 * controlPoint[i + 1].Y + b2 * controlPoint[i + 2].Y + b3 * controlPoint[i + 3].Y);

                        length += Bspline.CalculatePnt2Pnt(lines[0], lines[1]);

                        if (Bspline.CalculatePnt2Pnt(lines[1], tempPnts[count]) < threshold)
                        {
                            pntLen[count++] = length;
                        }



                        lines[0] = lines[1];
                    }
                }
                if (count == num - 1)
                {
                    length += Bspline.CalculatePnt2Pnt(lines[0], tempPoint[num - 1]);

                    pntLen[num - 1] = length;

                    break;
                }
                else
                {
                    threshold *= 2.0f;
                }
            }



            int ii = 0;

            foreach (string str in tempString)
            {
                tempFile.WriteLine(str + "," + pntLen[ii++].ToString());
            }

            tempFile.Close();

            //清空txt文档
            FileStream fs = new FileStream(txtAdress, FileMode.Create, FileAccess.Write);

            fs.Close();

            FileTxt.FileTxtCopy(Directory.GetCurrentDirectory() + "\\eeww.txt", txtAdress);

            //删除临时文件
            System.IO.File.Delete(Directory.GetCurrentDirectory() + "\\eeww.txt");
        }
        //将自由端点的b样条曲线分割为每段15cm的曲线
        static public void BsplineSegment(string BsplineWithoutDirTxt, string BsplineWithDirTxt)
        {
            //将端点长度写入文件,便于姿态角度拟合
            Bspline.CalculateBsplineInfo(BsplineWithoutDirTxt);
            //数据读取
            StreamReader pathFile = File.OpenText(BsplineWithoutDirTxt);

            List <string> tempString = new List <string>();

            while (!pathFile.EndOfStream)
            {
                tempString.Add(pathFile.ReadLine());
            }

            pathFile.Close();

            List <PointF> tempPnts       = new List <PointF>();
            List <PointF> posAnglePoints = new List <PointF>();

            foreach (string str in tempString)
            {
                string[] sArray = str.Split(',');
                tempPnts.Add(new PointF(float.Parse(sArray[0]), float.Parse(sArray[1])));
                posAnglePoints.Add(new PointF(float.Parse(sArray[3]), float.Parse(sArray[2])));
            }

            System.Drawing.PointF[] tempPoint = tempPnts.ToArray();

            System.Drawing.PointF[] posAnglePoint = posAnglePoints.ToArray();

            //创建临时文件
            StreamWriter posAngleFile = new StreamWriter(Directory.GetCurrentDirectory() + "\\23423.txt", false);
            //创建临时文件
            StreamWriter lengthFile = new StreamWriter(Directory.GetCurrentDirectory() + "\\325.txt", false);



            //创建临时文件
            StreamWriter lengthSegmentFile = new StreamWriter(Directory.GetCurrentDirectory() + "\\3424ds.txt", false);

            //每15cm分一段,计算端点切线方向 ,以无方向b样条的长度15cm一段进行切分

            Segment(tempPoint, BsplineWithDirTxt, lengthSegmentFile);

            lengthSegmentFile.Close();

            //数据读取
            StreamReader lengthSegmentFileR = File.OpenText(Directory.GetCurrentDirectory() + "\\3424ds.txt");


            //每15cm分一段,计算端点姿态方向
            Segment2(posAnglePoint, posAngleFile, lengthSegmentFileR);

            lengthSegmentFileR.Close();
            System.IO.File.Delete(Directory.GetCurrentDirectory() + "\\3424ds.txt");



            //计算每一段的长度
            Segment3(BsplineWithDirTxt, lengthFile);


            lengthFile.Close();
            posAngleFile.Close();

            //将三个文件合在一起///////////////////////////////////////////////////////////////////
            //数据读取
            StreamReader file1 = File.OpenText(BsplineWithDirTxt);

            List <string> tempString1 = new List <string>();

            while (!file1.EndOfStream)
            {
                tempString1.Add(file1.ReadLine());
            }

            file1.Close();

            //数据读取  posAngleFile
            StreamReader file2 = File.OpenText(Directory.GetCurrentDirectory() + "\\23423.txt");

            List <string> tempString2 = new List <string>();

            while (!file2.EndOfStream)
            {
                tempString2.Add(file2.ReadLine());
            }

            file2.Close();


            //数据读取  lengthFile
            StreamReader file3 = File.OpenText(Directory.GetCurrentDirectory() + "\\325.txt");

            List <string> tempString3 = new List <string>();

            while (!file3.EndOfStream)
            {
                tempString3.Add(file3.ReadLine());
            }

            file3.Close();



            Console.WriteLine(tempString1.Count());
            Console.WriteLine(tempString2.Count());
            Console.WriteLine(tempString3.Count());

            //创建临时文件
            StreamWriter tempAllInfoFile = new StreamWriter(Directory.GetCurrentDirectory() + "\\asdgfas.txt", true);

            for (int i = 0; i < tempString2.Count(); i++)
            {
                tempAllInfoFile.WriteLine(tempString1[i] + "," + tempString2[i] + "," + tempString3[i]);
            }


            tempAllInfoFile.Close();
            FileTxt.FileTxtCopy(Directory.GetCurrentDirectory() + "\\asdgfas.txt", BsplineWithDirTxt);


            //删除文件
            System.IO.File.Delete(Directory.GetCurrentDirectory() + "\\asdgfas.txt");

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



            lengthFile.Close();
            posAngleFile.Close();
            System.IO.File.Delete(Directory.GetCurrentDirectory() + "\\23423.txt");
            System.IO.File.Delete(Directory.GetCurrentDirectory() + "\\325.txt");
        }