Page 84 - The Definitive Guide to Building Java Robots
P. 84

Preston_5564C03.fm  Page 65  Wednesday, October 5, 2005  7:21 AM



                                                                                  CHAPTER 3  ■  MOTION    65



                            // accessor
                            public int getRightHigh() {
                                return rightHigh;
                            }

                            // setter
                            public void setRightHigh(int rightHigh) {
                                this.rightHigh = rightHigh;
                            }
                            // accessor
                            public int getRightLow() {
                                return rightLow;
                            }

                            // setter
                            public void setRightLow(int rightLow) {
                                this.rightLow = rightLow;
                            }


                            // sample program
                            public static void main(String[] args) {
                                try {
                                    // get instance of SingleSerialPort
                                    JSerialPort sPort = (JSerialPort) SingleSerialPort.getInstance(1);
                                    // create instnace of BasicDiffDrive
                                    BasicDiffDrive diffDrive = new BasicDiffDrive(sPort);
                                    // move forward
                                    diffDrive.forward();
                                    // pause 2 seconds
                                    Utils.pause(2000);
                                    // stop
                                    diffDrive.stop();
                                    // close serial port
                                    sPort.close();
                                } catch (Exception e) {
                                    // print stack trace and exit
                                    e.printStackTrace();
                                    System.exit(1);
                                }
                            }
                        }

                            The next class simplifies movement a bit by adding the pause() and stop() methods for
                        you. By extending the BasicDiffDrive class and creating new methods with parameters to take
                        a millisecond argument, our robot can move in a particular direction during a given unit of
                        time. (See Example 3-7.)
   79   80   81   82   83   84   85   86   87   88   89