void MotorPollThread() { try { while (true) { // take all pending comamnds IRobotCommand command = null; if (_commands.TryTake(out command, Timeout.Infinite, _cancel)) { // do it! command.Do(this); // anything to do after a command executed? PostCommand(); } } } catch (OperationCanceledException) { // normal exception when thread is stopped } catch (Exception ex) { // while (ex != null) { LcdConsole.WriteLine(ex.Message); ex = ex.InnerException; } throw; } }
public IRobotCommand Create(string inputLine, IRobot receiver) { if (string.IsNullOrEmpty(inputLine)) { return(null); } IRobotCommand parsedCommand = null; var upperCaseInput = inputLine.ToUpper().Trim(); if (upperCaseInput.StartsWith("PLACE")) { parsedCommand = ParsePositionCommand(upperCaseInput, receiver); } else if (upperCaseInput == "MOVE") { parsedCommand = new MoveCommand(receiver); } else if (upperCaseInput == "LEFT") { parsedCommand = new TurnLeftCommand(receiver); } else if (upperCaseInput == "RIGHT") { parsedCommand = new TurnRightCommand(receiver); } else if (upperCaseInput == "REPORT") { parsedCommand = new ReportCommand(receiver); } return(parsedCommand); }
public void ThenRobotPositionIsUpdatedAsExpected(int inputX, int inputY, bool hasResult) { var position = new Position { X = inputX, Y = inputY, Direction = 0 }; Robot = new Robot(new TableDimensions(TableSize, TableSize)); Command = new PlaceCommand(Robot, position); Command.Execute(); if (hasResult) { Robot.CurrentPosition().X.Should().Be(inputX); Robot.CurrentPosition().Y.Should().Be(inputY); } else { Robot.CurrentPosition().Should().Be(null); } }
void MotorPollThread() { while (_run) { // wait until there is something to do _changedWaitHandle.WaitOne(); // take all pending comamnds List <IRobotCommand> commands = DequeueAll(); for (int i = 0; i < commands.Count && _run; i++) { IRobotCommand command = commands[i]; if (command.GetType() == typeof(HandCommand)) { Do((HandCommand)command); } else if (command.GetType() == typeof(MoveCommand)) { Do((MoveCommand)command); } else { // unknown command } // wait for all motors to finish WaitHandle.WaitAll(_motorTasks); } } }
public void Queue(IRobotCommand command) { // make sure a single thread is changing values lock (_commands) { _commands.Add(command); } // notify worker thread _changedWaitHandle.Set(); }
private string GetCommandText(IRobotCommand cmd) { int roboIndex = SelectedRobotIndex + 1; if (cmd.CommandName == "Move" || cmd.CommandName == "Turn") { return($" Robot{roboIndex} - {SelectedCommand} - {Payload}"); } else { return($" Robot{roboIndex} - {SelectedCommand}"); } }
public List <List <IRobotCommand> > ConvertStringCommands(List <string> listOfCommands) { List <List <IRobotCommand> > commands = new List <List <IRobotCommand> >(); try { List <IRobotCommand> commandGroup = null; int counter = 0; foreach (string command in listOfCommands) { counter++; if (command.StartsWith(CommandNames.Place.Name)) { if (commandGroup != null) { commands.Add(commandGroup); commandGroup = null; } IRobotCommand placeCommand = GetPlaceRobotCommand(command); if (placeCommand != null) { commandGroup = new List <IRobotCommand>(); commandGroup.Add(placeCommand); continue; } } if (commandGroup != null) { if (!command.StartsWith(CommandNames.Place.Name)) { IRobotCommand robotCommand = CommandFactory.CreateCommand(command.ToUpper()); commandGroup.Add(robotCommand); } if (counter == listOfCommands.Count) { commands.Add(commandGroup); } } } } catch { } return(commands); }
private IRobotCommand GetPlaceRobotCommand(string command) { IRobotCommand robotCommand = null; try { string[] splitCmd = command.Split(' '); if (splitCmd != null && splitCmd.Length == 2) { string[] splitParams = splitCmd[1].Split(','); if (splitParams != null && splitParams.Length == 3) { uint x = 0; uint y = 0; if (uint.TryParse(splitParams[0], out x) && uint.TryParse(splitParams[1], out y)) { Direction direction = Directions.GetAll().Where(d => d.Name == splitParams[2].ToUpper()).FirstOrDefault(); if (direction != null) { robotCommand = new PlaceCommand() { Position = new Common.Data.MatPosition(new Common.Data.MatLocation(x, y), direction) }; } } } } } catch { } return(robotCommand); }
public Task <bool> ExecuteAsync(IRobotCommand command) { //find robot by index var robot = RobotRepository.Instance.Find(command.RobotIndex); var task = Task.Factory.StartNew(() => { if (command.CommandName == "Move") { robot.Move(command.Value); } else if (command.CommandName == "Turn") { robot.Turn(command.Value); } else { robot.Beep(); } return(true); }); return(task); }
public String ExecuteCommand(String command) { IRobotCommand robotCommand = commandFactory.GetCommand(command); return(robotCommand.Execute()); }
public static IRobotCommand CreateCommand(string commandName) { IRobotCommand command = GetCommands().FirstOrDefault(x => x.Command.Name.Equals(commandName)); return(command); }
static void Main(string[] args) { // for number conversion Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.InstalledUICulture; 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; }; LcdConsole.WriteLine("EV3Scanner 3.0"); float mainRatio = 1.667f; float secondaryRatio = 3.0f; float handRatio = 1f; #if DUMPLOGS StringBuilder logs = new StringBuilder(); #endif // stop here if (!run) { return; } // main loop LcdConsole.WriteLine("Starting..."); try { using (IRobot robot = new ScannerRobot() { }) { // apply settings robot.RatioSettings[RobotSetup.XPort] = mainRatio; robot.RatioSettings[RobotSetup.YPort] = secondaryRatio; robot.RatioSettings[RobotSetup.PenPort] = handRatio; // printer // robot.SpeedSettings[RobotSetup.XPort] = 64; // robot.SpeedSettings[RobotSetup.YPort] = 113; // adjust Y motor speed as X motor produces almost twice as much speed (0.56) // robot.SpeedSettings[RobotSetup.PenPort] = 127; // scanner robot.SpeedSettings[RobotSetup.XPort] = 16; robot.SpeedSettings[RobotSetup.YPort] = 113; // adjust Y motor speed as X motor produces almost twice as much speed (0.56) robot.SpeedSettings[RobotSetup.PenPort] = 127; // calibrate robot LcdConsole.WriteLine("Calibrating..."); robot.Calibrate(() => { return(!run); }); // 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]; // Enter the listening loop. while (run) { LcdConsole.WriteLine("Waiting for a connection... "); // blinking green Buttons.LedPattern(4); // Perform a blocking call to accept requests. // You could also user server.AcceptSocket() here. client = server.AcceptTcpClient(); LcdConsole.WriteLine("Connected!"); // turn off Buttons.LedPattern(0); // Get a stream object for reading and writing NetworkStream stream = client.GetStream(); // if robot is sending data, publish to network channel EventHandler <DataEventArgs> dataCB = (o, e) => { try { byte[] msg = System.Text.Encoding.ASCII.GetBytes(e.Data); stream.Write(msg, 0, msg.Length); } catch (IOException) { // disconnected robot.Off(); } }; robot.OnData += dataCB; #if DUMPLOGS // DEBUG byte[] logBuffer = Encoding.ASCII.GetBytes(logs.ToString()); client.GetStream().Write(logBuffer, 0, logBuffer.Length); #endif try { string data = null; int read; string message = ""; // Loop to receive all the data sent by the client. 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 { // get message type IRobotCommand command = RobotCommandFactory.Create(message); if (command != null) { robot.Queue(command); } // message = ""; } } } } catch (IOException) { LcdConsole.Clear(); LcdConsole.WriteLine("Disconnected!"); // stop sending data robot.OnData -= dataCB; } } } } catch (Exception ex) { /* #if DEBUG * // Set the TcpListener on port 13000. * int port = 13000; * * // TcpListener server = new TcpListener(port); * TcpListener debugServer = new TcpListener(IPAddress.Any, port); * * // Start listening for client requests. * debugServer.Start(); * * LcdConsole.WriteLine("Wait debugger"); * * // Perform a blocking call to accept requests. * // You could also user server.AcceptSocket() here. * using (TcpClient debugClient = debugServer.AcceptTcpClient()) * { * // Get a stream object for reading and writing * NetworkStream stream = debugClient.GetStream(); * * foreach (String it in Assembly.GetExecutingAssembly().GetManifestResourceNames()) * { * byte[] msg = System.Text.Encoding.ASCII.GetBytes(it + "\n"); * // Send back a response. * stream.Write(msg, 0, msg.Length); * } * * while (ex != null) * { * byte[] msg = System.Text.Encoding.ASCII.GetBytes(ex.Message + "\n"); * // Send back a response. * stream.Write(msg, 0, msg.Length); * * msg = System.Text.Encoding.ASCII.GetBytes(ex.StackTrace + "\n"); * // Send back a response. * stream.Write(msg, 0, msg.Length); * ex = ex.InnerException; * } * } * * // Stop listening for new clients. * debugServer.Stop(); #else * throw; #endif */ throw; } finally { // Stop listening for new clients. server.Stop(); } }
public void Queue(IRobotCommand command) { _commands.Add(command); }
public void Execute(IRobotCommand command, IStringResponse response = null) { command?.Execute(TheRobot, response); }
public void Add(IRobotCommand cmd) { _robotCommands.Add(cmd); }
public void Execute(IRobotCommand command) { command.Execute(_ioCommunicator); Console.WriteLine("{0} {1} sent to iRobot", DateTime.Now, command); }
/// <summary> /// Komut ekler /// </summary> /// <param name="command"></param> public void AddCommand(IRobotCommand command) { _commandList.Enqueue(command); }
/// <summary> /// Converts commands to string. /// </summary> /// <param name="command">The command.</param> /// <returns></returns> public string CommandToString(IRobotCommand command) { return(string.Empty); }