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); } }
private static void AutoSampleTimerCallbackFunc(object state) { if (AutoSampleTimerCallbackFuncSign) { return; } if (StateManager.ArduinoState.IsBusy == true) { return; } AutoSampleTimerCallbackFuncSign = true; FormMain.GetInstance().SampleThreadFunc(); AutoSampleTimerCallbackFuncSign = false; }
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(); } }
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; }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(FormMain.GetInstance()); }