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

Preston_5564C04.fm  Page 121  Wednesday, October 5, 2005  7:22 AM



                                                                                 CHAPTER 4  ■  SENSORS   121



                            public int getDevantech() throws Exception{
                                return getHeading(CMD_DEVANTECH);
                            }
                            public int getVector() throws Exception{
                                return getHeading(CMD_VECTOR);
                            }

                            // since differnt delay for each compass
                            private int getCompassDelay() {
                                int delay = 0;
                                if (compass == CMD_DINSMORE) {
                                    delay = 50;
                                }
                                if (compass == CMD_DEVANTECH) {
                                    delay = 150;
                                }
                                if (compass == CMD_VECTOR) {
                                    delay = 250;
                                }
                                return delay;
                            }

                            public int getCompass() {
                                return compass;
                            }


                            public void setCompass(int compass) {
                                this.compass = compass;
                            }

                            public static void main(String[] args) {
                                try {
                                    // since i am testing at my desk and not on my robot
                                    CompassStamp s = new CompassStamp(SingleSerialPort.getInstance(1));
                                    // since devantech is default
                                    System.out.println("Devantech Heading = " + s.getHeading());
                                    // getting specific heading
                                    System.out
                                            .println("Vector Heading = " + s.getHeading(CMD_VECTOR));
                                    // using a setter
                                    s.setCompass(CompassStamp.CMD_DISMORE);
                                    // getting dinsmore heading
                                    System.out.println("Dinsmore Heading = " + s.getHeading());
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    System.exit(1);

                                }
                            }
                        }
   135   136   137   138   139   140   141   142   143   144   145