Page 245 - The Ultimate Palm Robot
P. 245
Color profile: Generic CMYK printer profile
Composite Default screen
Bots / The Ultimate Palm Robot/ Mukhar & Johnson / 222880-6 / Chapter 8
228 The Ultimate Palm Robot
Let’s start by looking at the constructor. The constructor here follows the
same basic outline as the gp_example class. Your class will need to pass a port
name and module address to the constructor. For Windows, the port name
will be “COM1” and the default moduleAddress for BrainStem is 2. One of the
first things the constructor does is create an instance of the acroname.jStem
class. This class is provided by Acroname, and you will find it in the \aJava
directory of the Acroname software. Next, the constructor attempts to open
the serial port. The CommPortIdentifier and SerialPort classes are part of the
Java Communications API. After the port is opened, the code gets the input
and output streams for the port and passes them to the jStem instance. After
the initialization is complete, the code uses the jStem instance and the module
address to create an instance of the jGP class. This class has the methods for
communicating with the BrainStem and is used by the other methods of the
AbstractBrainStem class.
Next is the readSensor(int) method. This method takes an int that identifies
the sensor to read. For the PPRK this argument should have the value 0, 1, or 2.
It returns the sensor reading as a float value between 0 and 1.0. The closer the
object is to the sensor, the closer the return value is to 1.0. If there is a problem
reading the sensor, the method returns –1.
Following the readSensor(int) method is the startServo(int, float) method.
This method takes a sensor number as the first argument and commands that
servo to rotate. The amount to rotate is given by the second argument, which
should be a number between –1.0 and +1.0. Values close to –1 or 1 will cause
the servo to rotate faster than values close to 0.
The final method is the stopServo(int) method. This method takes the servo
number as an argument and commands that servo to stop.
PPRK Programming with Java
Using our new base class, let’s write a Java program for the BrainStem. What
we will do is recreate the to_fro.leaf reflex program in Java. Recall that all this
program does is command the robot to move in one direction for two seconds,
then reverse that movement, and repeat indefinitely. Here’s the ToFro.java
source code:
public class ToFro extends AbstractBrainStem {
public ToFro(String portname, byte moduleAddress) {
super(portname, moduleAddress);
P:\010Comp\Bots\880-6\ch08.vp
Monday, May 12, 2003 1:16:48 PM