Page 246 - The Ultimate Palm Robot
P. 246

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






                                                   Chapter 8 Taking Control of the BrainStem Robot   229



                                     }

                                     public static void main(String[] args) {
                                       ToFro tf = new ToFro("COM1", (byte) 2);
                                       while (true) {
                                         tf.moveTo();
                                         tf.moveFro();
                                       }
                                     }

                                     void moveTo() {
                                       startServo(1,  1.0f);
                                       startServo(2, -1.0f);
                                       sleep(2);
                                     }

                                     void moveFro() {
                                       startServo(1, -1.0f);
                                       startServo(2,  1.0f);
                                       sleep(2);
                                     }

                                     void sleep(int seconds) {
                                       try { Thread.sleep(seconds*1000); } catch (Exception ignored) {}
                                     }
                                    }

                                      The ToFro class should be relatively straightforward. The main( ) method
                                    calls the constructor for the class, passing the communication port name and
                                    BrainStem module ID. The communication port name is, of course, the name
                                    on a Windows platform. If you are trying this code on a Mac or Unix platform,
                                    you will need to change to the appropriate name for your system. The con-
                                    structor calls the constructor of AbstractBrainStem, which sets up the serial
                                    port to communicate with the BrainStem. The code then starts an endless loop
                                    where it calls methods that move the robot in one direction for two seconds,
                                    and then in the opposite direction for two seconds.
                                      The next thing to do is to compile our program. To be able to do this, you
                                    will need some additional resources. The first thing you’ll need is the Java
                                    Communications API. We introduced the API earlier in this chapter. If you
                                    installed it at that time, you don’t need to do anything else. Otherwise,










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