Page 243 - The Ultimate Palm Robot
P. 243

Color profile: Generic CMYK printer profile
           Composite  Default screen
                                                      Bots / The Ultimate Palm Robot/ Mukhar & Johnson / 222880-6 / Chapter 8






                    226     The Ultimate Palm Robot



                                 The gp_example.java has a doExample( ) method that actually reads and
                               writes data from and to the serial port. Here are some of the methods used:

                                        /* display Analog 0 */
                                        gp.AnalogIn((byte)0)); //returns a float
                                        /* move the servos a bit */
                                        gp.ServoAbs((byte)0, 0.5f); //move servo 0
                                        gp.ServoAbs((byte)0, -0.5f);

                               The AbstractBrainStem class

                               What we will do first is follow the gp_example class to develop our own base
                               class that can be used by any Java programs to communicate with the
                               BrainStem. We’ll write this class to do a couple of things for us:

                                   ❏ Create the jStem and jGP objects
                                   ❏ Provide a method for commanding a servo
                                   ❏ Provide a method for reading a sensor

                                  With these basic tasks provided by the base class, we don’t need to re-create
                               that code for every Java class we write for the BrainStem. When we write a
                               Java program for the BrainStem, we extend our base class and our program
                               gets the ability to read the sensors and command the servos.
                                  We’ll show the source code first, and then talk about what the class does. To
                               save space, we’re not going to show every bit of the class; you can find the
                               complete source code for this class in the \OtherApplications\ToFro direc-
                               tory on the CD-ROM. Here’s the base class:

                               public class AbstractBrainStem {
                                 jStem stem;
                                 jGP gp;

                                 public AbstractBrainStem(String portname, byte moduleAddress) {
                                   InputStream inputStream;
                                   OutputStream outputStream;

                                   stem = new jStem();

                                   try {
                                     CommPortIdentifier portId =










           P:\010Comp\Bots\880-6\ch08.vp
           Monday, May 12, 2003 1:16:48 PM
   238   239   240   241   242   243   244   245   246   247   248