|
dLife Home Page | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectdlife.robot.Controller
public abstract class Controller
Base class for controlling robots within the ControlCenter.
To control a robot (physical or simulated) you will create a sub-class of
Controller. The Controller class is an abstract
class that specifies three methods:
startUp(Robot): The startUp method is invoked when
the "Start Up" button in the ControlCenter is clicked and performs all of the
initialization for the Robot. This method's main job is to
create instances of the sensors and effectors that will be used to control
the robot.
step(): The step method is invoked over and over
again by the ControlCenter after the "Run" button is clicked. Clicking the
"Step" button will invoke the step method exactly once for each
click. The step method will typically check the robot's sensors,
decide on an action to take and issue commands to the robot's effectors.
shutDown(): The shutDown method is invoked when the
"Shut Down" button is clicked in the ControlCenter. The abstract
Controller class provides an empty definition of this method
which is sufficient for most sub-classes.
In addition, Controller sub-classes are required to have a
no-arg (default) constructor in order to be used with the ControlCenter. Very
typically this is accomplished by not defining any constructors in a
Controller sub-class. Note: if you do not define any
constructors in a class, Java automatically provides an empty default
constructor. Controller sub-classes usually do not require the
explicit definition of a constructor because all initialization can (and
should) be done in the startUp(Robot) method.
Below is an example of a very simple Controller that moves a
Hemisson robot forward until it detects an obstacle, at which point it simply
stops.
public class SimpleController extends Controller {
private HemissonProximitySensor hps;
private HemissonDifferentialDrive hdd;
public void startUp(Robot robot) {
// Create and add a ProximitySensor to the robot.
hps = new HemissonProximitySensor();
robot.addSensor(hps);
// Create and add a DifferentialDrive to the robot.
hdd = new HemissonDifferentialDrive();
robot.addEffector(hdd);
}
public void step() {
// Find the largest of the front 3 proximity sensor readings.
// Larger readings indicate closer obstacles.
int maxFront = hps.getMaximum(HemissonProximitySensor.FRONT_GROUP);
// If there is no obstacle move forward, else stop.
if (maxFront < 15) {
hdd.translate(0.5); // forward at half speed.
}
else {
hdd.translate(0); // stop.
}
}
}
Additional sample controllers can be found in the robot sub-packages of the
examples package.
Studying a few of these examples is a good way to get started. A few that
make particularly good starting points are:
| Constructor Summary | |
|---|---|
Controller()
|
|
| Method Summary | |
|---|---|
void |
shutDown()
This method is invoked when the Shut Down button in the ControlCenter is clicked. |
abstract void |
startUp(Robot robot)
This method is invoked automatically when the Start Up button in the ContolCenter is clicked. |
abstract void |
step()
This method is invoked either once when the Step button is clicked or periodically when the Run button is clicked. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public Controller()
| Method Detail |
|---|
public abstract void startUp(Robot robot)
robot - the Robot that this Controller will be controlling.public abstract void step()
public void shutDown()
|
dLife Home Page | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||