Templates for robot classes

Jabotics provides two class templates templates/en/Robot.tmpl and templates/en/Robot2.tmpl for the BlueJ IDE that can be found in the Jabotics installation directory. The first template has been designed for beginners, whereas the second template is aimed at more advanced users. In both cases the robot program is represented by a single class derived from org.jabotics.robot.en.XRobot. The robot class thereby gets access to the object factory, necessary to create the required control objects, and a number of constants that help to reduce the typing effort in method calls. Excerpts from the templates will be used to explain basic issues. Of course, the templates can be adapted to one's own requirements.

Class template for beginners

The template Robot.tmpl tries to keep the basic class structure as simple as possible. The class provides all control objects for motors, sensors, etc. as static members. The programmer can concentrate on implementing the main method with all robot instructions. This approach does not necessarily require real knowledge on object-oriented programming. The interface methods can simply be introduced as available "robot commands" that have to be sent to control objects with different responsibilities. The type of an object determines the available commands as described in the HTML documentation. If the robot class template with its numerous control objects still apears to be too complex to start from, it could be an option to deal with even simpler console applications first as described on the following page.

The template starts with an import statement to make all elements from org.jabotics.robot.en available. The robot class is derived from XRobot (placeholder for the class name in the template: $CLASSNAME):

import org.jabotics.robot.en.*;
public class $CLASSNAME extends XRobot

The provisioning of control objects reflects a robot in its standard configuration:

static final IMotor leftMotor = objectFactory.createMotor(EMotorPort.C);
static final IMotor rightMotor = objectFactory.createMotor(EMotorPort.B);
static final IPilot pilot = objectFactory.createPilot(EMotorPort.C, EMotorPort.B);
static final ITouchSensor enterButton = objectFactory.createTouchSensor(EButton.ENTER);
static final ITouchSensor escapeButton = objectFactory.createTouchSensor(EButton.ESCAPE);
static final ITouchSensor leftButton = objectFactory.createTouchSensor(EButton.LEFT);
static final ITouchSensor rightButton = objectFactory.createTouchSensor(EButton.RIGHT);
static final ITouchSensor touchSensor = objectFactory.createTouchSensor(ESensorPort.S1);
static final ISensor soundSensor = objectFactory.createSoundSensor(ESensorPort.S2);
static final ILightSensor lightSensor = objectFactory.createLightSensor(ESensorPort.S3);
static final ISensor ultrasonicSensor = objectFactory.createUltrasonicSensor(ESensorPort.S4);
static final ITimer timer = objectFactory.createTimer();
static final IDisplay display = objectFactory.createDisplay();
static final ISpeaker speaker = objectFactory.createSpeaker();

Beginners do not have to understand every detail but should be able to extract object names and types to work with the HTML documentation. The only method provided by the robot class is the main method, which already contains a sample implementation that has to be replaced by one's own instructions:

////////////////////////////////////////////////////////////////////////////////////////
// Replace the following sample code by your own instructions.
////////////////////////////////////////////////////////////////////////////////////////
timer.wait(2000); // time to get out of the way after starting the program
pilot.resetCounters();
pilot.setTargetSpeed(10);
pilot.moveStraight(FORWARD);
ultrasonicSensor.waitUntilBelowLimit(50);
pilot.stopMotion(WAIT);
float distance = pilot.getTachoCount(); // tacho count is being stored in new float variable "distance"
speaker.playBeepSequence();
display.clear();
display.write("Distance in cm:", 0, 0);
display.write(distance, 0, 1);
display.write("Press", 1, 3);
display.write("escape", 2, 4);
display.write("button!", 3, 5);
escapeButton.waitForReleaseEvent();

This excerpt gives a first impression on the usage of the control objects. The sample coding makes use of the abbreviating enumeration constants defined in the class XRobot like FORWARD and WAIT . Without these constants the method call for starting the forward motion would look like this:

pilot.moveStraight(EDirection.FORWARD);

Advanced template

Basic knowledge of object-oriented programming is a prerequisite for the usage of the template Robot2.tmpl. In the main method, an instance of the robot class is being created followed by a call to its run method with the true implementation of the program run:

public static void main(String[] args)
{
$CLASSNAME robot = new $CLASSNAME();
robot.run();
}

The template provides a sample implementation of the run method demonstrating the advanced possibilities of this approach. As part of this demonstration, the robot class not only derives from XRobot but also implements the interface ISensorListener, which allows the robot class instance to handle change event notifactions of the sensors in the callback method manageEvent:

public class $CLASSNAME extends XRobot implements ISensorListener

More detailed information can be found in the extensive comments of the class template and in the HTML documentation. It should be noted that the usage of callback methods requires some basic knowledge on concurrency in multithreaded applications.