Learning
OOP with robotics
(NEW: Support for Lego Mindstorms EV3. Consult EV3JLib)
In modern programming
courses, the concepts of object oriented programming play a major role,
especially when using Java. For beginners, teaching-by-examples is an
essential teaching paradigma. But the examples for the class design have
to be selected as concrete as possible in order to show that there is
close mapping between real objects and their abstract counterpart as software
objects. Modeling real objects into software objects let the student feel
the essence of OOP.
What can be more
concrete and touchable than a robot? It is well-known that robotics provides
a formidable playground for teaching OOP. The real robot can easily be
modeled in software and acquiring a robot means instantiating a class
object. Evidently the real robot has parts like motors, gears, arms,
actuators, sensors, etc. These objects correspond to instances of software
classes and are software components of the robot class (has-a-relationship).
The robot can be steered by commands like turning on motors and reading
sensors. In software these commands are represented by methods of the
robot classes. Finally the robot becomes more and more intelligent by expanding
its possibilites (class derivation, is-a-relationship).
|
|
|
Assembling the parts of a robot like motors and sensors can be modeled very closely by constructing a robot class in a object-oriented programming language. |
|
Since a few years
the Lego NXT brick is a good
choice for the teaching and hobby environment. The Lego kit is a good
compromise between performance and affordability. When shipped, the Lego
firmware runs on the brick's microprocessor. But there are alternatives:
One comes from leJOS and contains
a Java interpreter. The firmware is able to communicate via Bluetooth
with another device like a host computer or a mobile phone. When the
brick is started, the firmware listens for commands and reacts accordingly
(turns motors on, reports back values of sensors, etc.). Moreover programs
may be downloaded into the brick and executed standalone. Therefore two
distinct modes of operation the NXT are possible: Either the NXT is controlled
by a user program running on a host (we call this "direct mode")
or a user program is downloaded and runs standalone on the brick (we call
this "autonomous mode"). Both modes have their pros and cons,
both are important and widely used in commercial and technical robot applications.
The
NxtJLib Java Library
NxtJLib is
a Java library for the direct mode that runs on a host computer. A library
called NxtJLibMP with exactly the same functionality runs on Java-enabled
mobile phones. A third version NxtJLibA provides the same OOP functionalities on top of the NXJ library for standalone programs that are uploaded into the NXT brick ('A' stands for autonomous mode). The libraries NxtJLib/NxtJLIbMP for the host controlled mode are inspired by the leJOS direct command
software and use some low level code from the leJOS source distribution, but are otherwise independent of the leJOS distribution. Only bricks running the leJOS firmware are supported. The Bluetooth communication uses
the BlueCove library for J2SE, the standard JSR-82 implementation for
J2ME and the Android Bluetooth classes for Android smartphones. More than one NXT brick (with different Bluetooth names) may be
operated at the same time.
When the robot is working,
lot of events occur: The touch sensor bounces against a wall, sound is
detected, light is turned on, etc. The best way to handle these events
in software is by using the Java event model: An event method (callback)
is declared and registered. Whenever the event occurs, this method is
automatically invoked. The NxtJLib implements the event model for all
sensors by polling them with a internal thread at regular time intervals.
Instead of writing old fashioned programs by polling the sensors in the user code constantly,
the user may focus on modern event driven algorithms based on the structure: "Whenever something happens do the following...".
In few words:
NxtJLib inspires the student to write pleasent Java code with a clean
OOP design.
A
first example
The following program shows
the fundamental concept of NxtJLib. A robot is created and the Bluetooth
link established. Then two motors are created and assembled into to robot.
Hereafter the robot is used. In the end the robot is disconnected and
the program terminates.
// NxtDemo.java
import ch.aplu.nxt.*;
public class NxtDemo
{
public NxtDemo()
{
// Ask
for the name, create robot and connect
NxtRobot robot = new NxtRobot();
// Create two motors
Motor motA = new Motor(MotorPort.A);
Motor motB = new Motor(MotorPort.B);
// Assemble motors into robot
robot.addPart(motA);
robot.addPart(motB);
// Use motors
motA.forward();
motB.forward();
// Wait a moment
Tools.delay(5000);
// Disconnect and terminate
robot.exit();
}
public static void main(String[] args)
{
new NxtDemo();
}
}
Execute
using WebStart (NXT brick with leJOS firmware needed)
With
the Gidlet framework for mobile phones,
the program for a Java-enabled handy looks almost the same. Just add another
include line and move the code from the constructor to the main method:
// NxtDemoGidlet.java
import ch.aplu.gidlet.*;
import ch.aplu.nxt.*;
public class NxtDemoGidlet extends Gidlet
{
public void main()
{
// Same
code as above
}
}
Download
JAR/JAD files for installation on mobile phone (NXT brick needed)
In both applications
the parameterless constructor of NxtRobot is used. In this case an input
dialog is automatically displayed where the Bluetooth name can be entered.
The class design
of NxtJLib is clean and straightforward. A simplified class diagram
is shown below:
Complete
class diagram
|