Esempio n. 1
0
        public static void Main(string[] args)
        {
            EventWaitHandle stopped = new ManualResetEvent(false);

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

            LcdConsole.WriteLine("Use color on port1");
            LcdConsole.WriteLine("Up read value");
            LcdConsole.WriteLine("Down raw value");
            LcdConsole.WriteLine("Enter change mode");
            LcdConsole.WriteLine("Esc. terminate");
            buts.EscapePressed += () => {
                stopped.Set();
            };
            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]);
            };
            stopped.WaitOne();
        }
Esempio n. 2
0
        private async Task TestEV3Color()
        {
            //brick.Stop();
            //brick.SetTimeout(250);
            EV3ColorSensor nxtlight = new EV3ColorSensor(BrickPortSensor.PORT_S4, ColorSensorMode.Reflection);
            EV3TouchSensor touch    = new EV3TouchSensor(BrickPortSensor.PORT_S1);
            //brick.Stop();
            //brick.SetupSensors();
            RGBColor rgb;
            await Task.Delay(5000);

            for (int i = 0; i < nxtlight.NumberOfModes(); i++)
            {
                int count = 0;
                while ((count < 100) && !touch.IsPressed())
                {
                    //Debug.WriteLine(string.Format("NXT Touch, Raw: {0}, ReadASString: {1}, IsPressed: {2}, NumberNodes: {3}, SensorName: {4}", touch.ReadRaw(), touch.ReadAsString(), touch.IsPressed(), touch.NumberOfModes(), touch.GetSensorName()));
                    //Debug.WriteLine(string.Format("EV3 Touch, Raw: {0}, ReadASString: {1}, IsPressed: {2}, NumberNodes: {3}, SensorName: {4}", ev3Touch.ReadRaw(), ev3Touch.ReadAsString(), ev3Touch.IsPressed(), ev3Touch.NumberOfModes(), ev3Touch.GetSensorName()));
                    //Debug.WriteLine(string.Format("NXT Sound, Raw: {0}, ReadASString: {1}, NumberNodes: {2}, SensorName: {3}", sound.ReadRaw(), sound.ReadAsString(), sound.NumberOfModes(), sound.GetSensorName()));

                    //brick.UpdateValues();
                    Debug.WriteLine(string.Format("EV3 Color Sensor, Raw: {0}, ReadASString: {1}",
                                                  nxtlight.ReadRaw(), nxtlight.ReadAsString()));
                    rgb = nxtlight.ReadRGBColor();
                    Debug.WriteLine(string.Format("Color: {0}, Red: {1}, Green: {2}, Blue: {3}",
                                                  nxtlight.ReadColor(), rgb.Red, rgb.Green, rgb.Blue));
                    //brick.Stop();
                    //brick.Start();
                    //nxtlight.ColorMode = ColorSensorMode.Ambient;
                    await Task.Delay(1000);

                    //if ((touch.IsPressed()) && ev3Touch.IsPressed())
                    count++;
                    //nxtlight.ColorMode = ColorSensorMode.Color;
                }
                if (nxtlight.ColorMode == ColorSensorMode.Reflection)
                {
                    nxtlight.ColorMode = ColorSensorMode.Color;
                }
                else
                {
                    nxtlight.ColorMode = ColorSensorMode.Reflection;
                }
                //brick.SetupSensors();
                await Task.Delay(5000);
            }
        }
Esempio n. 3
0
        static private void TestEV3Color()
        {
            Console.WriteLine("EV3 sensor color test mode");
            EV3ColorSensor nxtlight = new EV3ColorSensor(_brick, SensorPort.Port2, ColorSensorMode.Green);
            EV3TouchSensor touch    = new EV3TouchSensor(_brick, SensorPort.Port1);
            RGBColor       rgb;

            Thread.Sleep(5000);
            for (int i = 0; i < nxtlight.NumberOfModes(); i++)
            {
                int count = 0;
                while ((count < 100) && !touch.IsPressed())
                {
                    Console.WriteLine($"EV3 Color Sensor, Raw: {nxtlight.ReadRaw()}, ReadASString: {nxtlight.ReadAsString()}");
                    rgb = nxtlight.ReadRGBColor();
                    Console.WriteLine($"Color: {nxtlight.ReadColor()}, Red: {rgb.Red}, Green: {rgb.Green}, Blue: {rgb.Blue}");
                    Thread.Sleep(1000);
                    count++;
                }
                nxtlight.SelectNextMode();
                Thread.Sleep(5000);
            }
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            ButtonEvents buttons = new ButtonEvents();
            TcpListener  server  = null;
            TcpClient    client  = null;
            bool         run     = true;

            buttons.EscapePressed += () =>
            {
                if (server != null)
                {
                    server.Stop();
                }
                if (client != null)
                {
                    client.Close();
                }

                run = false;
            };

            Log("KinematicServer 1.5");

            float mainRatio      = 46.667f;
            float secondaryRatio = 40.0f;
            float handRatio      = 8f;

#if DUMPLOGS
            StringBuilder logs = new StringBuilder();
#endif
            using (RobotMotors motors = new RobotMotors(MotorPort.OutA, MotorPort.OutB, MotorPort.OutC)
            {
                MainMotorRatio = mainRatio, SecondaryMotorRatio = secondaryRatio, HandMotorRatio = handRatio
            })
            {
                EV3TouchSensor sensor1 = new EV3TouchSensor(SensorPort.In1);
                EV3ColorSensor sensor2 = new EV3ColorSensor(SensorPort.In2);
                sensor2.Mode = ColorMode.Reflection;

                int sensor2max = -1;
                int pause      = 0;
                buttons.EnterReleased += () =>
                {
                    pause++;
                };
                int currentPause = 0;
                motors.Calibrate((RobotMotors.CalibrationSteps step) =>
                {
                    if (!run)
                    {
                        return(true);
                    }
                    switch (step)
                    {
                    case RobotMotors.CalibrationSteps.Main:
                        if (sensor1.IsPressed())
                        {
                            return(true);
                        }
                        break;

                    case RobotMotors.CalibrationSteps.Pause:
                        if (currentPause != pause)
                        {
                            currentPause = pause;
                            return(true);
                        }
                        break;

                    case RobotMotors.CalibrationSteps.Secondary:
                        int sensor2reading = sensor2.ReadRaw();
                        if (sensor2reading > 19 &&
                            sensor2reading < sensor2max)     // moving away from sweet spot (but remove noise)
                        {
#if DUMPLOGS
                            logs.Append(string.Format("{0}:{1}:{2}\n\n", motors.GetRawTacho(MotorPort.OutB), sensor2reading, sensor2max));
                            logs.Append("-----------------\n\n");
#endif
                            return(true);
                        }
                        else if (sensor2reading > sensor2max)
                        {
                            sensor2max = sensor2reading;      // keep going when it increases
                        }
#if DUMPLOGS
                        logs.Append(string.Format("{0}:{1}:{2}\n\n", motors.GetRawTacho(MotorPort.OutB), sensor2reading, sensor2max));
#endif
                        break;

                    case RobotMotors.CalibrationSteps.SecondaryReset:
                        sensor2max = -1;
#if DUMPLOGS
                        logs.Append("***********************\n\n");
#endif
                        break;
                    }

                    /*
                     * Lcd.Clear();
                     * int line = 0;
                     * Lcd.WriteText(Font.MediumFont, new Point(0, line), "Calibrating...", true);
                     * line += (int)(Font.MediumFont.maxHeight);
                     * Lcd.WriteText(Font.MediumFont, new Point(0, line), string.Format("Refl.: {0} / {1}", sensor2.ReadRaw(), sensor2max), true);
                     * line += (int)(Font.MediumFont.maxHeight);
                     * Lcd.WriteText(Font.MediumFont, new Point(0, line), string.Format("A: {0}", motors.GetRawTacho(MotorPort.OutA)), true);
                     * line += (int)(Font.MediumFont.maxHeight);
                     * Lcd.WriteText(Font.MediumFont, new Point(0, line), string.Format("B: {0}", motors.GetRawTacho(MotorPort.OutB)), true);
                     * line += (int)(Font.MediumFont.maxHeight);
                     * Lcd.Update();
                     */

                    return(false);
                });
            }
            // stop here
            if (!run)
            {
                return;
            }

            // main loop
            Lcd.Clear();
            Lcd.Update();
            Log("Starting...");
            try
            {
                using (RobotMotors motors = new RobotMotors(MotorPort.OutA, MotorPort.OutB, MotorPort.OutC)
                {
                    MainMotorRatio = mainRatio, SecondaryMotorRatio = secondaryRatio, HandMotorRatio = handRatio
                })
                {
                    // Set the TcpListener on port 13000.
                    Int32 port = 13000;

                    // TcpListener server = new TcpListener(port);
                    server = new TcpListener(IPAddress.Any, port);

                    // Start listening for client requests.
                    server.Start();

                    // Buffer for reading data
                    Byte[] bytes = new Byte[256];
                    String data  = null;

                    // Enter the listening loop.
                    while (run)
                    {
                        Log("Waiting for a connection... ");

                        // Perform a blocking call to accept requests.
                        // You could also user server.AcceptSocket() here.
                        client = server.AcceptTcpClient();
                        Log("Connected!");

#if DUMPLOGS
                        // DEBUG
                        byte[] logBuffer = Encoding.ASCII.GetBytes(logs.ToString());
                        client.GetStream().Write(logBuffer, 0, logBuffer.Length);
#endif
                        motors.MainMotorSpeed      = 64;
                        motors.SecondaryMotorSpeed = 64;
                        motors.HandMotorSpeed      = 127;
                        data = null;

                        // Get a stream object for reading and writing
                        NetworkStream stream = client.GetStream();

                        int    read;
                        string message = "";
                        // Loop to receive all the data sent by the client.
                        int commandCount = 0;
                        while (run && (read = stream.Read(bytes, 0, bytes.Length)) != 0)
                        {
                            // Translate data bytes to a ASCII string.
                            data = System.Text.Encoding.ASCII.GetString(bytes, 0, read);
                            for (int i = 0; i < read; i++)
                            {
                                char c = data[i];
                                if (c != '\0')
                                {
                                    message += c;
                                }
                                else
                                {
                                    String[] rawCommand = message.Split(';');
                                    // get message type
                                    switch (rawCommand[0])
                                    {
                                    case "UP":
                                        motors.Queue(new HandCommand(true));
                                        commandCount = 0;
                                        break;

                                    case "DWN":
                                        motors.Queue(new HandCommand(false));
                                        commandCount = 0;
                                        break;

                                    case "MOV":
                                        commandCount++;
                                        float mainRotation      = float.Parse(rawCommand[1]);
                                        float secondaryRotation = float.Parse(rawCommand[2]);

                                        motors.Queue(
                                            new MoveCommand {
                                            MainRotation      = (int)Math.Round(mainRotation, MidpointRounding.AwayFromZero),
                                            SecondaryRotation = (int)Math.Round(secondaryRotation, MidpointRounding.AwayFromZero)
                                        });
                                        break;
                                    }

                                    //
                                    Lcd.Clear();
                                    int line = 0;
                                    Lcd.WriteText(Font.MediumFont, new Point(0, line), string.Format("Count: {0}", commandCount), true);
                                    line += (int)(Font.MediumFont.maxHeight);
                                    Lcd.WriteText(Font.MediumFont, new Point(0, line), string.Format("Last: {0}", message), true);
                                    line += (int)(Font.MediumFont.maxHeight);
                                    Lcd.Update();

                                    //
                                    message = "";
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log(ex.Message);
                if (client.Connected)
                {
                    byte[] buffer = Encoding.ASCII.GetBytes(ex.Message);
                    client.GetStream().Write(buffer, 0, buffer.Length);
                }
                throw;
            }
            finally
            {
                // Stop listening for new clients.
                server.Stop();
            }
        }
Esempio n. 5
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();
            };
        }