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

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



                                                                                  CHAPTER 3  ■  MOTION    77



                            public void setServoTiming(int stepSize, int moveDelay)
                        throws Exception {
                                // ensure will work
                                if (stepSize < MIN_STEP_SIZE) {
                                    throw new Exception("Step size not possible at 9600 baud.");
                                }
                                if (moveDelay < (stepSize * MIN_DELAY_SIZE)) {
                                    throw new Exception("Move delay not practical for given step size.");
                                }
                                this.stepSize = stepSize;
                                this.moveDelay = moveDelay;
                            }

                            public int getMoveDelay() {
                                return moveDelay;
                            }

                            public int getStepSize() {
                                return stepSize;
                            }

                            // sample program
                            public static void main(String[] args) {
                                try {
                                    // get instance of SingleSerialPort
                                    JSerialPort sPort = (JSerialPort)SingleSerialPort.getInstance(1);
                                    // create instance of PanTilt
                                    PanTilt pt = new PanTilt(sPort);
                                    // pan left until exception is thrown
                                    while (true) {
                                        try {
                                        pt.moveLeft();
                                        } catch (Exception e) {
                                            break;
                                        }
                                    }
                                    // pan right
                                    while (true) {
                                        try {
                                        pt.moveRight();
                                        } catch (Exception e) {
                                            break;
                                        }
                                    }
                                    // reset head
                                    pt.reset();
                                    // tilt up
   91   92   93   94   95   96   97   98   99   100   101