Robot.java
|
[top] |
|
import josx.platform.rcx.*;
public class Robot
{
public void go_forward()
{
Motor.A.forward();
Motor.C.forward();
}
public void go_backward()
{
Motor.A.backward();
Motor.C.backward();
}
public void stop()
{
Motor.A.stop();
Motor.C.stop();
}
public void pause(int delay)
{
try { Thread.sleep(delay); }
catch (InterruptedException ie) { }
}
}
|
RobotLR.java
|
[top] |
|
import josx.platform.rcx.*;
public class RobotLR extends Robot
{
Sensor touchSensor = Sensor.S1;
public RobotLR ()
{
touchSensor.addSensorListener(new TouchBackListener(this));
// As soon as the robot object is created
// the SensorListener will be added to the touchsensor.
}
public void go_right()
{
Motor.A.forward();
Motor.C.backward();
}
public void go_left()
{
Motor.A.backward();
Motor.C.forward();
}
}
|
RobotLine.java
|
[top] |
|
import josx.platform.rcx.*;
public class RobotLine extends RobotLR {
Sensor lightSensor = Sensor.S2;
public RobotLine ()
{
// As soon as the robot object is created
lightSensor.activate();
// the lamp of the lightsensor will be switched on
lightSensor.addSensorListener(new CourtListener(this));
// and the SensorListener will be added to the lightsensor.
}
public void go() {
TextLCD.print("LINE");
this.go_forward();
while (true) { }
}
public void correct_line() {
this.go_backward();
this.pause(150);
this.go_left();
this.pause(80);
this.go_forward();
}
}
|
TouchBackListener.java
|
[top] |
|
import josx.platform.rcx.Sensor;
import josx.platform.rcx.SensorListener;
public class TouchBackListener implements SensorListener
{
private RobotLR touchRobot;
public TouchBackListener(RobotLR touchRobot) {
this.touchRobot = touchRobot;
}
public void stateChanged(Sensor s, int oldValue, int newValue)
{
if (s.readBooleanValue())
{
touchRobot.go_backward();
touchRobot.pause(300);
touchRobot.go_forward();
}
}
}
|
CourtListener.java
|
[top] |
|
import josx.platform.rcx.Sensor;
import josx.platform.rcx.SensorListener;
public class CourtListener implements SensorListener {
private RobotLine lineRobot;
private int threshold;
public CourtListener(RobotLine lineRobot) {
this.lineRobot = lineRobot;
this.threshold = 0;
this.lineRobot.pause(300);
// wait 300ms for reasonable sensor values.
}
public void stateChanged(Sensor s, int oldValue, int newValue) {
if (threshold == 0) threshold = newValue;
// If there is no threshold available,
// it would be initialized once.
if (newValue > threshold + 10) {
lineRobot.correct_line();
} // If current lightsensor value is 10% over the threshold,
// the robot has to correct his course.
}
}
|
Test.java
|
[top] |
|
import josx.platform.rcx.*;
/**
* Sample introduction to techniques of OOP.
* @author T.Rinkens 25 Jul 2002
*
* DESCRIPTION
* Program that makes a simple robot follow the black line
* on the contructopedia test pad.
* Before pressing 'Run'-Button, the robot must be placed
* on the black line.
*/
class Test
{
static RobotLine robbi;
public static void main(String[] args)
{
robbi = new RobotLine();
robbi.go();
} // main
}// class
|