static void Main(string[] args) { try { EV3Messenger messenger = new EV3Messenger(); if (messenger.Connect(PORT)) { Console.WriteLine("Connected"); } else { throw new InvalidOperationException("Connection failed"); } int ctop = Console.CursorTop; int cleft = Console.CursorLeft; while (true) { GamePadState state = GamePad.GetState(PlayerIndex.One); float stateX = state.ThumbSticks.Left.X; float stateY = state.ThumbSticks.Left.Y; int baseSpeed = (int)(stateY * SCALE); int correction = Math.Abs((int)(stateX * CORRECTION_SCALE)); int leftSpeed = baseSpeed; int rightSpeed = baseSpeed; if (stateX > 0) { leftSpeed += correction; rightSpeed -= correction; } else if (stateX < 0) { leftSpeed -= correction; rightSpeed += correction; } Console.SetCursorPosition(cleft, ctop); Console.Write(" "); Console.SetCursorPosition(cleft, ctop); Console.Write(leftSpeed.ToString() + " " + rightSpeed.ToString()); messenger.SendMessage("left", (float)leftSpeed); messenger.SendMessage("right", (float)rightSpeed); Thread.Sleep(50); // sleep so the brick can keep up } } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); Console.ReadKey(); } }
public Connection(string port) { Messenger = new EV3Messenger(); if (!Messenger.Connect(port)) throw new EV3CommunicationException( "Connection with EV3 failed"); this.Status = RobotStatus.Empty; this.LastColor = Color.White; this.LastColorIsRead = true; ReadTimer = new Timer(50); ReadTimer.Elapsed += new ElapsedEventHandler(ReadTimer_Elapsed); ReadTimer.Start(); }
static void Main(string[] args) { messenger = new EV3Messenger(); Timer ReadTimer = new Timer(50); ReadTimer.Elapsed += new ElapsedEventHandler(ReadTimer_Elapsed); ReadTimer.Start(); if (messenger.Connect("com40")) { Console.Write("Connected."); } else { throw new NotSupportedException("Connection error"); } while (true) { Console.WriteLine(lastmessage); } }
static void Main(string[] args) { int ctop; int cleft; //Timer readTimer = new Timer(new TimerCallback(Timer); try { EV3Messenger messenger = new EV3Messenger(); if (messenger.Connect("com40")) { Console.WriteLine("Connected."); ctop = Console.CursorTop; cleft = Console.CursorLeft; } else throw new InvalidOperationException("Connection error."); while (true) { EV3Message licht = messenger.ReadMessage(); GamePadState state = GamePad.GetState(PlayerIndex.One); int left = 0; int right = 0; int baseSpeed = (int)(state.Triggers.Left * 60); int baseSpeed = (int)(state.Triggers.Left * 40); int message = baseSpeed * 100; message += baseSpeed; if (state.ThumbSticks.Left.X >= 0) right = (int)(state.ThumbSticks.Left.X * 30); else if (state.ThumbSticks.Left.X <= 0) left = (int)Math.Abs(state.ThumbSticks.Left.X * 30); right = (int)(state.ThumbSticks.Left.X * 50); else if (state.ThumbSticks.Left.X <= 0) left = (int)Math.Abs(state.ThumbSticks.Left.X * 50); right *= 100; message += left; message += right; if (state.Buttons.A == ButtonState.Pressed) message = -1; int turretspeed = (int)(state.ThumbSticks.Right.X * 20); Console.SetCursorPosition(cleft, ctop); Console.Write(" "); Console.SetCursorPosition(cleft, ctop); Console.Write(message.ToString() + " " + turretspeed.ToString()); Console.Write(message.ToString() + " " + turretspeed.ToString() + " " + licht.ToString()); messenger.SendMessage("sturen", (float)message); messenger.SendMessage("turret", (float)turretspeed); //Thread.Sleep(new TimeSpan(1000)); Thread.Sleep(50); /* Brick can be slow processing bluetooth commands and queues them, * so simply add a delay of 50ms in each loop in the sender and the * brick can handle messages fine, brick has been tested without * any noticable delay and brick 'code' doesn't need waits. */ } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Console.WriteLine("Connection closed."); Console.ReadKey(); } }