Before the command is sent, it is required to establish the connection with the robot and set the robot to status ready (ready to receive commands). Finally after the command execution the robot must be set to idle status. Then each command execution requires 4 steps listed below.
- Establish connection with the robot.
- Set the robot to ready status.
- Execute the command.
- Set the robot to idle status.
Take a look how to implement this, with Java 8 using lambdas.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RobotCommand.execute(command -> | |
command.cmd("MOVE") | |
.arg("-from 130") | |
.arg("-to 200") | |
.arg("-speed 5")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RobotCommand { | |
private String command; | |
private ArrayList<String> arguments; | |
protected RobotCommand() { | |
this.arguments = new ArrayList<String>(); | |
} | |
public RobotCommand cmd(String command) { | |
this.command = command; | |
return this; | |
} | |
public RobotCommand arg(String argument) { | |
this.arguments.add(argument); | |
return this; | |
} | |
public String getCommand() { | |
return command; | |
} | |
public List<String> getArguments() { | |
return arguments; | |
} | |
public static void execute(Consumer<RobotCommand> request) { | |
System.out.println("Establishing robot conection..."); | |
System.out.println("Set robot on status READY"); | |
RobotCommand robotCmd = new RobotCommand(); | |
request.accept(robotCmd); | |
StringBuilder params = new StringBuilder(); | |
robotCmd.getArguments().stream().forEach(arg -> params.append(arg + " ")); | |
System.out.println(String.format("Execute robot command: %s %s", robotCmd.getCommand(), params)); | |
System.out.println("Set robot on status IDLE"); | |
} | |
} |
You can find the complete code here
No comments:
Post a Comment