Beispiel #1
0
        public static void Main(string[] args)
        {
            ManualResetEvent terminateProgram = new ManualResetEvent(false);

            UltraSonicMode[] modes = { UltraSonicMode.Centimeter, UltraSonicMode.Inch, UltraSonicMode.Listen };
            int          modeIdx   = 0;
            ButtonEvents buts      = new ButtonEvents();
            var          sensor    = new EV3UltrasonicSensor(SensorPort.In1, modes[modeIdx]);

            LcdConsole.WriteLine("Use sonic on port1");
            LcdConsole.WriteLine("Up change mode");
            LcdConsole.WriteLine("Enter read");
            LcdConsole.WriteLine("Esc. terminate");
            buts.EscapePressed += () => {
                terminateProgram.Set();
            };
            buts.UpPressed += () => {
                modeIdx     = (modeIdx + 1) % modes.Length;
                sensor.Mode = modes[modeIdx];
                LcdConsole.WriteLine("Mode: " + modes[modeIdx]);
            };
            buts.EnterPressed += () => {
                LcdConsole.WriteLine(sensor.ReadAsString());
            };
            terminateProgram.WaitOne();
        }
Beispiel #2
0
        /// <summary>
        /// this method initializes all needed motors and sensors
        /// to control the robot accordingly
        /// </summary>
        public static void InitRobot()
        {
            AddEmergencyStopOption();

            LeftTrack          = new Motor(Constants.LEFT_TRACK_PORT);
            RightTrack         = new Motor(Constants.RIGHT_TRACK_PORT);
            GrapplerRiserMotor = new Motor(Constants.GRAPPLER_RISER_PORT);
            GrapplerWheelMotor = new Motor(Constants.GRAPPLER_WHEEL_PORT);

            ColorSensor      = new EV3ColorSensor(Constants.EV3_COLOR_SENSOR_PORT);
            IRSensor         = new EV3IRSensor(Constants.IR_SENSOR_PORT);
            UltraSonicSensor = new EV3UltrasonicSensor(Constants.ULTRASONIC_SENSOR_PORT);

            ColorSensor.Mode      = ColorMode.RGB;
            UltraSonicSensor.Mode = UltraSonicMode.Centimeter;

            GrapplerPosition = GrapplerPosition.Down;
            FoodState        = FoodState.Searching;

            IsInitialized = true;

            WaitToFullyBootProgram();

            CalibrizeGrappler();

            PrintEmptyLine();

            WaitForStartButtonPress();

            TeamMode = TeamMode.WinnieTeam;
            //InitColour();
        }
Beispiel #3
0
        public static void MainThread()
        {
            // All in one thread
            Init();
            SetColor(ConsoleColor.Red);
            LogFileWriter.WriteLine("");
            Log("New log file started.");
            try
            {
                TRobot.Writeln(PathForExe);
                if (IsRobot)
                {
                    Emergency.Start();
                    FinalizePRogram();
                    LogFileWriter = File.AppendText(LogFile);
                    BIOS_Beep();

                    // MonoBrick.EV3.Mailbox mail = new MonoBrick.EV3.Mailbox();
                    // Thread.Sleep(1000000);

                    /*string[] arr = MonoBrick.Bluetooth<BrickCommand, BrickReply>.GetPortNames();
                     * for (int i = 0; i < arr.Length; i++)
                     * {
                     *      Writeln(arr[i]);
                     * }
                     * Thread.Sleep(100000);*/

                    Motor TestMotor = new Motor(MotorPort.OutD);
                    Writeln(TestMotor.GetTachoCount().ToString());
                    TestMotor.SetSpeed(100);
                    Thread.Sleep(1000);
                    TestMotor.Brake();
                    Writeln(TestMotor.GetTachoCount().ToString());
                    // Thread.Sleep(100000);

                    // System.IO.Ports.SerialPort serialPort = new SerialPort();
                    string[] ARR = SerialPort.GetPortNames();
                    for (int i = 0; i < ARR.Length; i++)
                    {
                        Writeln($"Serial {ARR[i]}");
                    }

                    Writeln("Hello from Robot");

                    long A = 0;

                    TDirectionizer directionizer = new TDirectionizer(SensorPort.In2, SensorPort.In3);
                    Motor          Left          = new Motor(MotorPort.OutC);
                    Motor          Right         = new Motor(MotorPort.OutB);

                    /*while (true)
                     * {
                     *      int[] res = directionizer.Directionize();
                     *      LcdConsole.Clear();
                     *      TRobot.Writeln($"Motorl: {res[0]}; Motorr: {res[1]};");
                     *      Left.SetPower((sbyte)res[0]);
                     *      Right.SetPower((sbyte)res[1]);
                     *      Thread.Sleep(50);
                     * }*/

                    TMotivator          vec = new TMotivator(MotorPort.OutC, MotorPort.OutB);
                    EV3UltrasonicSensor ultrasonicSensor = new EV3UltrasonicSensor(SensorPort.In1, UltraSonicMode.Centimeter);

                    Motor Test = new Motor(MotorPort.OutB);
                    Writeln($"Prev tacho: {Test.GetTachoCount()}");
                    vec.MoveForward(50, 360);
                    Writeln($"Tacho: {Test.GetTachoCount()}");
                    Thread.Sleep(100000);


                    /*while (true)
                     * {
                     *      A++;
                     *      vec.MoveForward_mm(50, 300);
                     *      vec.TurnLeft(20, 180);
                     *      vec.MoveForward_mm(50, 300);
                     *      vec.TurnRight(20, 180);
                     *      if (A >= 4)
                     *              break;
                     * }*/
                }

                TField    Fld = Field_Generated_Class.Generate();
                TDijkstra djk = new TDijkstra(new TPoint(1, 1), new TPoint(2, 0), Fld);
                TRobot.Writeln(djk.Search_Way().ToString() + $" steps {djk.CountSteps}");
                djk.Way.Reverse();
                for (int i = 0; i < djk.Way.Count; i++)
                {
                    TRobot.Writeln($"[{djk.Way[i].Position.X}, {djk.Way[i].Position.Y}]");
                }
            }
            catch (Exception E)
            {
                string Str = ($"[EXCEPTION][{E.GetType().Name}]: " + E.Message);
                File.WriteAllLines(PathForExe + "last_exception.txt", new string[] { Str });
                TRobot.Writeln(Str);
            }
            TRobot.Writeln("Press any key to continue...");
            TRobot.Read();
            if (IsRobot)
            {
                for (MotorPort i = MotorPort.OutA; i <= MotorPort.OutD; i++)
                {
                    Motor mot = new Motor(i);
                    mot.Brake();
                    mot.SetPower(0);
                }
            }
            FinalizePRogram();
        }
Beispiel #4
0
        public static void Main(string[] args)
        {
            //AutoFin->false
            ManualResetEvent terminateProgram = new ManualResetEvent(false);

            //Motor_Rady
            LcdConsole.WriteLine("***Motor_Rady***");
            Motor motorA = new Motor(MotorPort.OutA);
            Motor motorD = new Motor(MotorPort.OutD);

            motorA.Off();
            motorD.Off();
            motorA.ResetTacho();
            motorD.ResetTacho();
            //Vehicle_Rady
            Vehicle    vehicle = new Vehicle(MotorPort.OutA, MotorPort.OutD);
            WaitHandle waitHandle;

            vehicle.ReverseLeft  = false;
            vehicle.ReverseRight = false;
            int b = 0;

            //Sensor_Rady
            LcdConsole.WriteLine("***Sensor_Rady***");
            //Touch_Rady
            var touchSensor1 = new EV3TouchSensor(SensorPort.In1);
            var touchSensor2 = new EV3TouchSensor(SensorPort.In4);
            //UltraSonic_Rady
            var UltraSonicSensor = new EV3UltrasonicSensor(SensorPort.In3, UltraSonicMode.Centimeter);
            //Color_Rady
            EventWaitHandle stopped = new ManualResetEvent(false);

            ColorMode[] modes   = { ColorMode.Color, ColorMode.Reflection, ColorMode.Ambient, ColorMode.Blue };
            int         modeIdx = 0;
            var         sensor  = new EV3ColorSensor(SensorPort.In2, ColorMode.Reflection);


            //Conection
            LcdConsole.WriteLine("***Conect_Rady***");
            //Http
            Encoding enc   = Encoding.UTF8;
            string   input = "Detect";
            string   url   = "http://nursinghomeexplorer.azurewebsites.net/index.php";
            string   param = "";
            //
            Hashtable ht = new Hashtable();

            ht["langpair"] = "#ja";
            ht["hl"]       = "en";
            ht["text"]     = HttpUtility.UrlEncode(input, enc);
            foreach (string k in ht.Keys)
            {
                param += String.Format("{0}={1}&", k, ht[k]);
            }
            byte[] data = Encoding.ASCII.GetBytes(param);
            //
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

            req.Method        = "POST";
            req.ContentType   = "application/x-www-form-urlencoded";
            req.ContentLength = data.Length;
            LcdConsole.WriteLine("***Conected!***");

            //timer set
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            long a = 0;

            //buts
            ButtonEvents buts = new ButtonEvents();

            LcdConsole.WriteLine("Up read BrightLine");
            LcdConsole.WriteLine("Down read Floor");
            //LcdConsole.WriteLine ("Left Exit program");
            //LcdConsole.WriteLine ("Right Exit program");
            LcdConsole.WriteLine("Enter change color mode");
            LcdConsole.WriteLine("Esc. StartRun");

            buts.UpPressed += () =>
            {
                LcdConsole.WriteLine("Sensor value: " + sensor.ReadAsString());
            };
            buts.DownPressed += () =>
            {
                LcdConsole.WriteLine("Raw sensor value: " + sensor.ReadRaw());
            };
            buts.EnterPressed += () =>
            {
                modeIdx     = (modeIdx + 1) % modes.Length;
                sensor.Mode = modes[modeIdx];
                LcdConsole.WriteLine("Sensor mode is set to: " + modes[modeIdx]);
            };
            //Left_Finish
            buts.LeftPressed += () => {
                terminateProgram.Set();
            };

            //Escape_StartRun
            buts.EscapePressed += () =>
            {
                LcdConsole.WriteLine("***StartRun***");
                stopped.Set();
                //loop_run on the line
                while (true)
                {
                    //touchsensor2 Pressed
                    if (touchSensor2.IsPressed() == true)
                    {
                        //motors stop
                        motorA.Brake();
                        motorD.Brake();

                        LcdConsole.WriteLine("***Stop***");

                        //timer start
                        sw.Start();

                        //after 5sec
                        Thread.Sleep(5000);
                        a = sw.ElapsedMilliseconds;

                        LcdConsole.WriteLine("***Stop_5sec***");

                        if (a >= 5000)
                        {
                            //timer stop
                            a = 0;
                            sw.Stop();
                            sw.Reset();

                            if (touchSensor2.IsPressed() == true)
                            {
                                LcdConsole.WriteLine("***Stop_ISPressed2***");

                                //
                                Stream reqStream = req.GetRequestStream();
                                LcdConsole.WriteLine("***Stop_GetReqStream***");
                                reqStream.Write(data, 0, data.Length);
                                LcdConsole.WriteLine("***Stop_write***");
                                reqStream.Close();
                                LcdConsole.WriteLine("***Stop_reqStream.close***");

                                break;
                            }
                            if (touchSensor1.IsPressed() == true)
                            {
                                //
                                Stream reqStream = req.GetRequestStream();
                                reqStream.Write(data, 0, data.Length);
                                reqStream.Close();

                                break;
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }

                    //touchsensor1 pressed
                    if (touchSensor1.IsPressed() == true)
                    {
                        motorA.Brake();
                        motorD.Brake();
                        LcdConsole.WriteLine("***Stop***");
                        //timer start
                        sw.Start();

                        //after 5sec
                        Thread.Sleep(5000);

                        a = sw.ElapsedMilliseconds;
                        if (a >= 5000)
                        {
                            //timer stop
                            a = 0;
                            sw.Stop();
                            sw.Reset();

                            if (touchSensor1.IsPressed() == true)
                            {
                                //
                                Stream reqStream = req.GetRequestStream();
                                reqStream.Write(data, 0, data.Length);
                                reqStream.Close();

                                break;
                            }
                            if (touchSensor2.IsPressed() == true)
                            {
                                //
                                Stream reqStream = req.GetRequestStream();
                                reqStream.Write(data, 0, data.Length);
                                reqStream.Close();

                                break;
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }
                    //Ultrasonic on
                    if (UltraSonicSensor.Read() <= 30)
                    {
                        motorA.Brake();
                        motorD.Brake();
                        LcdConsole.WriteLine("***Stop***");
                        //timer start
                        sw.Start();

                        //after 5sec
                        Thread.Sleep(5000);

                        a = sw.ElapsedMilliseconds;

                        if (a >= 5000)
                        {
                            //timer stop
                            a = 0;
                            sw.Stop();
                            sw.Reset();

                            if (UltraSonicSensor.Read() <= 30)
                            {
                                //
                                Stream reqStream = req.GetRequestStream();
                                reqStream.Write(data, 0, data.Length);
                                reqStream.Close();

                                break;
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }

                    b = sensor.Read();

                    if (b < 15)
                    {
                        vehicle.TurnRightForward(10, 100);
                    }
                    if (15 <= b && b < 60)
                    {
                        vehicle.TurnLeftForward(10, 60);
                    }

                    if (b >= 60)
                    {
                        waitHandle = vehicle.Forward(10, 0, true);
                    }
                }
            };
            terminateProgram.WaitOne();
            buts.LeftPressed += () => {
                terminateProgram.Set();
            };
        }