public async Task Setup() { // Set brick _brick = new Brick(); // Set Smacker ;) _smackMotor = new Motor(_brick, Iot.Device.BrickPi3.Models.BrickPortMotor.PortB); //_smackMotor.PropertyChanged += HandleTachoPropertyChangedEvent; // Set ultrasonic _sonic = new EV3UltraSonicSensor(_brick, SensorPort.Port1, UltraSonicMode.Centimeter); _sonic.PropertyChanged += HandleSonicSmackPropertyChangedEvent; // Set movement motors _leftMotor = new Motor(_brick, Iot.Device.BrickPi3.Models.BrickPortMotor.PortC); _rightMotor = new Motor(_brick, Iot.Device.BrickPi3.Models.BrickPortMotor.PortD); // Gyro _gyro = new EV3GyroSensor(_brick, SensorPort.Port3); _gyro.Mode = GyroMode.Angle; //_gyro.PropertyChanged += HandleGyroMovementPropertyChangedEvent; // Color _color = new EV3ColorSensor(_brick, SensorPort.Port2, ColorSensorMode.Color, 20); _color.PropertyChanged += HandleMovementColorPropertyChangedEvent; // Touch _touch = new EV3TouchSensor(_brick, SensorPort.Port4, 20); _touch.PropertyChanged += HandleHit; }
private async Task TestMultipleSensorsTouchCSSoud() { NXTTouchSensor touch = new NXTTouchSensor(brick, BrickPortSensor.PORT_S2); EV3TouchSensor ev3Touch = new EV3TouchSensor(brick, BrickPortSensor.PORT_S1, 20); NXTSoundSensor sound = new NXTSoundSensor(brick, BrickPortSensor.PORT_S4); NXTColorSensor nxtlight = new NXTColorSensor(brick, BrickPortSensor.PORT_S3); RGBColor rgb; bool bwait = true; while (bwait) { 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())); Debug.WriteLine(string.Format("NXT Color Sensor, Raw: {0}, ReadASString: {1}, NumberNodes: {2}, SensorName: {3}", nxtlight.ReadRaw(), nxtlight.ReadAsString(), nxtlight.NumberOfModes(), nxtlight.GetSensorName())); rgb = nxtlight.ReadRGBColor(); Debug.WriteLine(string.Format("Color: {0}, Red: {1}, Green: {2}, Blue: {3}", nxtlight.ReadColor(), rgb.Red, rgb.Green, rgb.Blue)); // Debug.WriteLine(string.Format("raw {0}", nxtlight.ReadTest())); await Task.Delay(300); if ((touch.IsPressed()) && ev3Touch.IsPressed()) { bwait = false; } } }
public ScannerRobot() : base(new MotorPort[] { RobotSetup.XPort, RobotSetup.YPort, RobotSetup.PenPort }) { _resetSensor = new EV3TouchSensor(RobotSetup.XResetPort); _colorSensor = new EV3ColorSensor(RobotSetup.ColorPort, ColorMode.RGB); _scanThread = new Thread(ScanPollThread); _scanThread.IsBackground = true; _scanThread.Start(); }
private void TestTouch() { EV3TouchSensor touch = new EV3TouchSensor(brick, BrickPortSensor.PORT_S1); //NXTTouchSensor touch = new NXTTouchSensor(brick, BrickPortSensor.PORT_S2); int count = 0; while (count < 100) { Debug.WriteLine(string.Format("NXT Touch, IsPRessed: {0}, ReadAsString: {1}, Selected mode: {2}", touch.IsPressed(), touch.ReadAsString(), touch.SelectedMode())); Task.Delay(300).Wait();; } }
private static void TestTouch() { Console.WriteLine("Running 100 reads on EV3 touch sensor on port 1."); EV3TouchSensor touch = new EV3TouchSensor(_brick, SensorPort.Port1); // Alternative to test NXT touch sensor // NXTTouchSensor touch = new NXTTouchSensor(brick, BrickPortSensor.PORT_S2); int count = 0; while (count < 100) { Console.WriteLine($"NXT Touch, IsPRessed: {touch.IsPressed()}, ReadAsString: {touch.ReadAsString()}, Selected mode: {touch.SelectedMode()}"); Task.Delay(300).Wait(); } }
private static void TestNXTCS() { Console.WriteLine("Run NXT Color sensor test on port 4. Press the EV3 touch sensor on port 1 to stop the test."); NXTColorSensor nxtlight = new NXTColorSensor(_brick, SensorPort.Port4); EV3TouchSensor touch = new EV3TouchSensor(_brick, SensorPort.Port1); RGBColor rgb; while (!touch.IsPressed()) { Console.WriteLine($"NXT Color Sensor, Raw: {nxtlight.ReadRaw()}, ReadASString: {nxtlight.ReadAsString()}, NumberNodes: {nxtlight.SelectedMode()}"); rgb = nxtlight.ReadRGBColor(); Console.WriteLine($"Color: {nxtlight.ReadColor()}, Red: {rgb.Red}, Green: {rgb.Green}, Blue: {rgb.Blue}"); Thread.Sleep(300); } }
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); } }
private async Task LunchFollowMe() { EV3TouchSensor touch = new EV3TouchSensor(BrickPortSensor.PORT_S1); NXTUltraSonicSensor ultra = new NXTUltraSonicSensor(BrickPortSensor.PORT_S3, UltraSonicMode.Centimeter); robot = new Vehicule(BrickPortMotor.PORT_B, BrickPortMotor.PORT_C); while (!touch.IsPressed()) { int valultra = ultra.Value; if ((valultra < 70) && (valultra != 0)) { Debug.WriteLine($"Taking picture, distance {valultra} cm"); await MakePicture(); } await Task.Delay(10); } Debug.Write("end of face tracking and detection"); }
private static void TestMultipleSensorsTouchCSSoud() { NXTTouchSensor touch = new NXTTouchSensor(_brick, SensorPort.Port2); EV3TouchSensor ev3Touch = new EV3TouchSensor(_brick, SensorPort.Port1, 20); NXTSoundSensor sound = new NXTSoundSensor(_brick, SensorPort.Port4); NXTColorSensor nxtlight = new NXTColorSensor(_brick, SensorPort.Port3); RGBColor rgb; while (!((touch.IsPressed()) && ev3Touch.IsPressed())) { Console.WriteLine($"NXT Touch, Raw: {touch.ReadRaw()}, ReadASString: {touch.ReadAsString()}, IsPressed: {touch.IsPressed()}, NumberNodes: {touch.NumberOfModes()}, SensorName: {touch.GetSensorName()}"); Console.WriteLine($"EV3 Touch, Raw: {ev3Touch.ReadRaw()}, ReadASString: {ev3Touch.ReadAsString()}, IsPressed: {ev3Touch.IsPressed()}, NumberNodes: {ev3Touch.NumberOfModes()}, SensorName: {ev3Touch.GetSensorName()}"); Console.WriteLine($"NXT Sound, Raw: {sound.ReadRaw()}, ReadASString: {sound.ReadAsString()}, NumberNodes: {sound.NumberOfModes()}, SensorName: {sound.GetSensorName()}"); Console.WriteLine($"NXT Color Sensor, Raw: {nxtlight.ReadRaw()}, ReadASString: {nxtlight.ReadAsString()}, NumberNodes: {nxtlight.NumberOfModes()}, SensorName: {nxtlight.GetSensorName()}"); rgb = nxtlight.ReadRGBColor(); Console.WriteLine($"Color: {nxtlight.ReadColor()}, Red: {rgb.Red}, Green: {rgb.Green}, Blue: {rgb.Blue}"); Thread.Sleep(300); } }
public static void Main(string[] args) { ManualResetEvent terminateProgram = new ManualResetEvent(false); var touchSensor = new EV3TouchSensor(SensorPort.In1); //svar touchSensor = new NXTTouchSensor(SensorPort.In1); ButtonEvents buts = new ButtonEvents(); LcdConsole.WriteLine("Use touch on port1"); LcdConsole.WriteLine("Up read"); LcdConsole.WriteLine("Down read raw"); LcdConsole.WriteLine("Esc. terminate"); buts.EscapePressed += () => { terminateProgram.Set(); }; buts.UpPressed += () => { LcdConsole.WriteLine("Sensor value:" + touchSensor.ReadAsString()); }; buts.DownPressed += () => { LcdConsole.WriteLine("Raw sensor value: " + touchSensor.ReadRaw()); }; terminateProgram.WaitOne(); }
public async Task TestTouch() { EV3TouchSensor ev3Touch = new EV3TouchSensor(_brick, SensorPort.Port4, 20); ev3Touch.PropertyChanged += HandlePropertyChangedEvent; while (false) { if (ev3Touch.IsPressed()) { Console.WriteLine($"Pressed!"); _smackMotor.SetSpeed(255); Thread.Sleep(1000); _smackMotor.SetSpeed(-255); Thread.Sleep(1000); _smackMotor.SetSpeed(0); } await Task.Delay(100); } _smackMotor.SetSpeed(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); } }
private static void Main() { BrickConsole.WriteLine("Start!"); var speaker = new Speaker(10); speaker.Beep(); var tank = new Tank(MotorPort.OutD, MotorPort.OutA); var irSensorWrapper = new IRSensorLockWrapper(new EV3IRSensor(SensorPort.In2)); var colorSensor = new EV3ColorSensor(SensorPort.In1, ColorMode.Color); var touchSensor = new EV3TouchSensor(SensorPort.In3); var buttons = new ButtonEvents(); buttons.EscapePressed += () => { tank.Stop(); BrickConsole.WriteLine("End!"); speaker.Buzz(); Environment.Exit(0); }; var remote = new RemoteControl(irSensorWrapper, IRChannel.One); remote.ButtonsReleased += btn => { tank.Stop(); BrickConsole.WriteLine("TankState: {0}", tank); }; remote.ButtonsPressed += btn => { switch (btn) { case RemoteButton.LeftUp: tank.StartForward(speed); break; case RemoteButton.LeftDown: tank.StartBackward(speed); break; case RemoteButton.RightUp: tank.StartSpinLeft(speed); break; case RemoteButton.RightDown: tank.StartSpinRight(speed); break; case RemoteButton.LeftUp | RemoteButton.LeftDown: BrickConsole.WriteLine("Color: {0}", colorSensor.ReadColor()); break; case RemoteButton.RightUp | RemoteButton.RightDown: BrickConsole.WriteLine("TouchSensor.IsPressed: {0}", touchSensor.IsPressed()); break; case RemoteButton.LeftUp | RemoteButton.RightUp: BrickConsole.WriteLine("Distance: {0} cm", irSensorWrapper.Do(x => x.ReadDistance())); break; case RemoteButton.Beacon: BrickConsole.WriteLine("Color: {0}", colorSensor.ReadColor()); BrickConsole.WriteLine("Distance: {0} cm", irSensorWrapper.Do(x => x.ReadDistance())); BrickConsole.WriteLine("TouchSensor.IsPressed: {0}", touchSensor.IsPressed()); break; } }; Thread.Sleep(Timeout.Infinite); }
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(); }; }
const int MS_TIME_DOWN = 2700; // Time in ms untill the arm is down public Hefvork(MotorPort hefMotorPort, SensorPort limitSensorPort) { _hefMotor = new Motor(hefMotorPort); _limitSensor = new EV3TouchSensor(limitSensorPort); }
const int MS_TIME_DOWN = 2700; // Time in ms untill the arm is down public Hefvork() { _hefMotor = new Motor(Constants.HEFVORK_MOTOR_PORT); _limitSensor = new EV3TouchSensor(Constants.HEFVORK_STOP_SENSOR_PORT); }
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(); } }
public PrinterRobot() : base(new MotorPort[] { RobotSetup.XPort, RobotSetup.YPort, RobotSetup.PenPort }) { _resetSensor = new EV3TouchSensor(RobotSetup.XResetPort); }