Пример #1
0
        public void SampleThreadFunc()
        {
            if (StateManager.TcpState.IsClientConnected)
            {
                InterNetwork.GetInstance().AcquirePicture();
            }
            else
            {
                WriteToConsole("No client connected !");
            }
            while (true)
            {
                if (TcpIpFileManager.GetInstance().IsFileFresh)
                {
                    break;
                }
                else
                {
                    Thread.Sleep(100);
                }
            }
            //When the file is ready
            Image <Rgb, Byte> rawImage = new Image <Rgb, byte>((Bitmap)TcpIpFileManager.GetInstance().NowImage);

            WriteToPictureRaw(rawImage);
        }
Пример #2
0
        private void debug1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Image<Gray,byte> debugImage = new Image<Gray, byte>("1.jpg");
            //ImageViewerManager.Instance().ShowPicture(debugImage);
            SampleThreadFunc();
            ColorDetectResult result = Cv.DetectColor(new Image <Rgb, byte>(TcpIpFileManager.GetInstance().FilePath));

            TcpIpFileManager.GetInstance().IsFileFresh = false;
            WriteToConsole("Color Det= " + result.Result);
        }
Пример #3
0
        public static bool IsT1G1End()
        {
            if (!FinalSign)
            {
                return(false);
            }
            ColorDetectResult result = Cv.DetectColor(new Image <Rgb, byte>(TcpIpFileManager.GetInstance().FilePath));

            if (result.Result > 180)
            {
                while (true)
                {
                    FormMain.GetInstance().SampleThreadFunc();
                    Image <Rgb, Byte> rawImage   = new Image <Rgb, byte>((Bitmap)TcpIpFileManager.GetInstance().NowImage);
                    double[]          threshold1 = new double[]
                    {
                        200
                    };
                    double[] threshold2 = new double[]
                    {
                        100, 200, 300
                    };
                    CannyTextureAnalysisResult textureAnalysisResult = Cv.AutoCannyTextureAnalysis(rawImage, threshold1, threshold2, 0);
                    TcpIpFileManager.GetInstance().IsFileFresh       = false;
                    float threshold = 0.05f;
                    if (Math.Abs(textureAnalysisResult.Diff - 0.15) < threshold)
                    {
                        return(true);
                    }
                    else
                    {
                        if (textureAnalysisResult.Diff > threshold)
                        {
                            Arduino.GetInstance().Send(MotorDirection.Forward, 80, MotorDirection.Backward, 80, 300);
                        }
                        else if (textureAnalysisResult.Diff < -threshold)
                        {
                            Arduino.GetInstance().Send(MotorDirection.Backward, 80, MotorDirection.Forward, 80, 300);
                        }
                    }
                    while (StateManager.ArduinoState.IsBusy)
                    {
                        Thread.Sleep(100);
                    }
                }
            }
            else
            {
                return(false);
            }
        }
Пример #4
0
 public void G1T1ThreadFunc()
 {
     WriteToConsole("Ground 1 Task 1 begins to execute !");
     while (true)
     {
         SampleThreadFunc();
         Tdps.T1G1();
         TcpIpFileManager.GetInstance().IsFileFresh = false;
         while (StateManager.ArduinoState.IsBusy)
         {
             Thread.Sleep(100);
         }
         if (Tdps.IsT1G1End())
         {
             break;
         }
     }
     WriteToConsole("Ground 1 Task 1 finished !");
 }
Пример #5
0
        public static void T1G2()
        {
            Arduino.GetInstance().GoStraight(110);
            while (StateManager.ArduinoState.IsBusy)
            {
                Thread.Sleep(100);
            }
            FormMain.GetInstance().SampleThreadFunc();
            TcpIpFileManager.GetInstance().IsFileFresh = false;
            FormMain.GetInstance().SampleThreadFunc();
            ColorDetectResult result = Cv.DetectColor(new Image <Rgb, byte>(TcpIpFileManager.GetInstance().FilePath));

            TcpIpFileManager.GetInstance().IsFileFresh = false;
            Arduino.GetInstance().GoStraight(7);
            while (StateManager.ArduinoState.IsBusy)
            {
                Thread.Sleep(100);
            }
            //if (result.Result <= 130 &&  result.Result >= 60)							//Yellow Green
            //{
            //	FormMain.GetInstance().WriteToConsole("YELLOW");
            //	Arduino.GetInstance().PidTurn(90);
            //	while (StateManager.ArduinoState.IsBusy)
            //	{
            //		Thread.Sleep(100);
            //	}
            //	Arduino.GetInstance().GoStraight(180);
            //	while (true)
            //	{
            //		Arduino.GetInstance().GoStraight(3);
            //		while (StateManager.ArduinoState.IsBusy)
            //		{
            //			Thread.Sleep(100);
            //		}
            //		Arduino.GetInstance().GetDistance();
            //		float dis = Arduino.GetInstance().SonicDistance;
            //		if (dis > 40 && dis < 80)
            //		{
            //			break;
            //		}
            //	}
            //	while (StateManager.ArduinoState.IsBusy)
            //	{
            //		Thread.Sleep(100);
            //	}
            //	Arduino.GetInstance().PidTurn(-90);
            //	while (StateManager.ArduinoState.IsBusy)
            //	{
            //		Thread.Sleep(100);
            //	}

            //}
            //else if (result.Result <= 240)															//Blue
            //{
            //	FormMain.GetInstance().WriteToConsole("BLUE");
            //	Arduino.GetInstance().PidTurn(135);
            //	while (StateManager.ArduinoState.IsBusy)
            //	{
            //		Thread.Sleep(100);
            //	}
            //	Arduino.GetInstance().GoStraight(75);
            //	while (StateManager.ArduinoState.IsBusy)
            //	{
            //		Thread.Sleep(100);
            //	}
            //	Arduino.GetInstance().PidTurn(-55);
            //	while (StateManager.ArduinoState.IsBusy)
            //	{
            //		Thread.Sleep(100);
            //	}

            //	//Finish
            //	Arduino.GetInstance().GoStraight(160);
            //	while (true)
            //	{
            //		Arduino.GetInstance().GoStraight(3);
            //		while (StateManager.ArduinoState.IsBusy)
            //		{
            //			Thread.Sleep(100);
            //		}
            //		Arduino.GetInstance().GetDistance();
            //		float dis = Arduino.GetInstance().SonicDistance;
            //		if (dis > 40 && dis < 80)
            //		{
            //			break;
            //		}
            //	}
            //	Arduino.GetInstance().PidTurn(-80);


            //}
            if (result.Result <= 320)                                                                                                                                   //Violet
            {
                FormMain.GetInstance().WriteToConsole("VIOLET");
                Arduino.GetInstance().PidTurn(45);
                while (StateManager.ArduinoState.IsBusy)
                {
                    Thread.Sleep(100);
                }
                Arduino.GetInstance().GoStraight(75);
                while (StateManager.ArduinoState.IsBusy)
                {
                    Thread.Sleep(100);
                }
                Arduino.GetInstance().PidTurn(45);
                while (StateManager.ArduinoState.IsBusy)
                {
                    Thread.Sleep(100);
                }

                //Finish
                Arduino.GetInstance().GoStraight(160);
                while (true)
                {
                    Arduino.GetInstance().GoStraight(3);
                    while (StateManager.ArduinoState.IsBusy)
                    {
                        Thread.Sleep(100);
                    }
                    Arduino.GetInstance().GetDistance();
                    float dis = Arduino.GetInstance().SonicDistance;
                    if (dis > 40 && dis < 80)
                    {
                        break;
                    }
                }
                Arduino.GetInstance().PidTurn(-90);
            }
            else
            {
                throw new LogicErrorException();
            }
        }
Пример #6
0
        public static void T1G1()
        {
            Image <Rgb, Byte> rawImage = new Image <Rgb, byte>((Bitmap)TcpIpFileManager.GetInstance().NowImage);

            double[] threshold1 = new double[]
            {
                200
            };
            double[] threshold2 = new double[]
            {
                100, 200, 300
            };

            CannyTextureAnalysisResult textureAnalysisResult = Cv.AutoCannyTextureAnalysis(rawImage, threshold1, threshold2, 0);

            //[obsolote] Detect lines and then remove them from final image.
            //DetectLineResult lineResult = Cv.DetectLine(textureAnalysisResult.Img);
            //Image<Gray, Byte> outputImage = textureAnalysisResult.Img;
            //foreach (LineSegment2D line in lineResult.Line)
            //{
            //	outputImage.Draw(line, new Gray(0), 2);
            //}
            //CannyTextureAnalysisResult reduceLinesResult = new CannyTextureAnalysisResult(rawImage, "Reduce the lines");

            float diff = textureAnalysisResult.Diff;
            LinearRegressionResult lrResult = Cv.CalculateLinearRegression(textureAnalysisResult.Img);

            FormMain.GetInstance().WriteToConsole("Diff= " + diff + "   Slope= " + lrResult.Slope + "   Count= " + lrResult.Count + " CounterFinal= " + Counter);
            FormMain.GetInstance().WriteToPicture1(textureAnalysisResult.Img);

            //[obsolote] Generate nine patch graph.
            //NinePatchResult patchResult = Cv.CalculateNinePatch(textureAnalysisResult.Img);
            //int height = FormMain.GetInstance().GetPicture2Size().Height;
            //int width = FormMain.GetInstance().GetPicture2Size().Width;
            //byte[,,] patchImageData = new byte[height, width, 1];
            //for (var index0 = 0; index0 < 3; index0++)
            //{
            //	for (var index1 = 0; index1 < 3; index1++)
            //	{
            //		if (patchResult.Patch[index0,index1])
            //		{
            //			for (var y = height/3*index1; y < height/3*(index1+1); y++)
            //			{
            //				for (var x = width/3*index0; x < width/3*(index0+1); x++)
            //				{
            //					patchImageData[y, x, 0] = 255;
            //				}
            //			}
            //		}
            //	}
            //}
            //Image<Gray, Byte> patchImage = new Image<Gray, Byte>(patchImageData);
            //FormMain.GetInstance().WriteToPicture2(patchImage);

            //threshold
            float threshold = 0.05f;

            if (lrResult.Count < 2000)
            {
                Arduino.GetInstance().Send(MotorDirection.Backward, 80, MotorDirection.Backward, 80, 300);
                return;
            }
            if (Math.Abs(diff) > threshold)
            {
                countAcc = 0;
                if (diff > threshold)
                {
                    if (_previousDiff < -threshold)
                    {
                        Arduino.GetInstance().Send(MotorDirection.Forward, 35, MotorDirection.Backward, 35, 300);
                    }
                    else
                    {
                        Arduino.GetInstance().Send(MotorDirection.Forward, 80, MotorDirection.Backward, 100, 300);
                    }
                }
                else if (diff < -threshold)
                {
                    if (_previousDiff > threshold)
                    {
                        Arduino.GetInstance().Send(MotorDirection.Backward, 35, MotorDirection.Forward, 35, 300);
                    }
                    else
                    {
                        Arduino.GetInstance().Send(MotorDirection.Backward, 80, MotorDirection.Forward, 100, 300);
                    }
                }
            }
            else
            {
                if (lrResult.Slope < 0.1)
                {
                    countAcc++;
                    if (countAcc > 2)
                    {
                        if (FinalSign)
                        {
                            Counter++;
                            Arduino.GetInstance().Send(MotorDirection.Forward, 75, MotorDirection.Forward, 75, 1000);
                        }
                        else
                        {
                            Counter = Counter + 3;
                            Arduino.GetInstance().Send(MotorDirection.Forward, 75, MotorDirection.Forward, 75, 3000);
                        }
                    }
                    else
                    {
                        Counter++;
                        Arduino.GetInstance().Send(MotorDirection.Forward, 75, MotorDirection.Forward, 75, 1000);
                    }
                }
                else
                {
                    countAcc = 0;
                    Arduino.GetInstance().Send(MotorDirection.Forward, 75, MotorDirection.Forward, 75, 200);
                }
            }
            _previousDiff = diff;
        }
Пример #7
0
            private void Receive(object argClient)
            {
                var           client = (TcpManager.TcpClientWithGuid)argClient;
                NetworkStream ns     = client.TcpStream;
                StreamReader  sr     = new StreamReader(ns);

                while (true)
                {
                    string data;
                    try
                    {
                        data = sr.ReadLine();
                    }
                    catch (IOException e)
                    {
                        client.Stop();
                        StateManager.TcpState.IsClientConnected = false;
                        break;
                    }

                    if (data == null)
                    {
                        client.Stop();
                        StateManager.TcpState.IsClientConnected = false;
                        break;
                    }
                    string[] items = data.Split(Separators, StringSplitOptions.RemoveEmptyEntries);
                    //Get Picture
                    if (items[0] == "Picture")
                    {
                        long         contentLen = Convert.ToInt64(items[1]);
                        MemoryStream ms         = new MemoryStream();
                        long         size       = 0;
                        while (size < contentLen)
                        {
                            //Receive 256 bytes for every loop
                            byte[] bits = new byte[4096];
                            int    r    = client.TcpClient.GetStream().Read(bits, 0, bits.Length);
                            if (r <= 0)
                            {
                                break;
                            }
                            ms.Write(bits, 0, r);
                            size += r;
                        }
                        System.Drawing.Image img;
                        try
                        {
                            img = System.Drawing.Image.FromStream(ms);
                        }
                        catch (Exception e)
                        {
                            InterNetwork.GetInstance().AcquirePicture();
                            continue;
                        }
                        TcpIpFileManager.GetInstance().AddTempFile(img);
                    }
                    //Motor State Feedback
                    else if (items[0] == "Motor")
                    {
                        if (items[1] == "Finished")
                        {
                            StateManager.ArduinoState.IsBusy = false;
                        }
                        else if (items[1] == "GetCommand")
                        {
                            StateManager.ArduinoState.IsBusy = true;
                        }
                        else
                        {
                            throw new NotImplementedException();
                        }
                    }
                    //MPU data send back
                    else if (items[0] == "MPU")
                    {
                        if (items[1] == "Angle")
                        {
                            if (items[2] == "Z")
                            {
                                FormArduinoControlPanel.GetInstance().WriteToConsole("Z angle = " + items[3]);
                            }
                            else
                            {
                                throw new NotImplementedException();
                            }
                        }
                        else
                        {
                            throw new NotImplementedException();
                        }
                    }
                    //Get Distance Result
                    else if (items[0] == "DISTANCE")
                    {
                        FormArduinoControlPanel.GetInstance().WriteToConsole("Distance = " + items[1]);
                        Arduino.GetInstance().SonicDistance = Convert.ToSingle(items[1]);
                    }
                    //ELSE
                    else
                    {
                        throw new NotImplementedException();
                    }
                }
            }
Пример #8
0
 public static TcpIpFileManager GetInstance()
 {
     return(_instance ?? (_instance = new TcpIpFileManager()));
 }