Java Reference
In-Depth Information
import
java.io.*;
public class
Controller
implements
Runnable{
Robot robot; // a reference to the robot
Thread thread
#
new
Thread(this, "Controller");
// a thread object
OccupancyMap map
#
new
OccupancyMap();
// the occupancy map
// the constructor
public
Controller(Robot robot) {
this
.robot
#
robot;
}
// starts the robot thread and the controller thread
public void
start() {
robot.start();
thread.start();
}
public void
run() {
String result; // the string that records a task result
double
position[]
#
new
double[3];
// the robot position
double
measures[]
#
new
double[360];
// the array of laser measures
int
numMeasures
#
0; // the number of laser measures
try
{
// initializes the communication with the robot
PipeInputStream pipeIn
#
new
PipeInputStream();
BufferedReader input
#
new
BufferedReader(
new
InputStreamReader(pipeIn));
PrintWriter output
#
new
PrintWriter(
new
PipeOutputStream(pipeIn),
true
);
robot.setOutput(output);
// requests the current robot position
robot.println("R1.GETPOS");
result
#
input.readLine();
parsePosition(result, position);
// requests a laser scan
robot.println("L1.SCAN");
result
#
input.readLine();
parseMeasures(result, measures);
drawLaserScan(position, measures);
this
.repaint();
// requests a backward translation of the robot
robot.println("P1.MOVEBW 60");
result
#
input.readLine();
}
catch
(IOException ioe) {ioe.printStackTrace();}
}