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

Preston_5564C07.fm  Page 239  Monday, September 26, 2005  5:38 AM



                                                                               CHAPTER 7  ■  NAVIGATION  239



                        the robot will want to make. Its child class, DistanceVector, is used to help the programmer
                        differentiate between moving with units of distance or units of time.
                            The two public fields are heading and magnitude. The heading I chose to be an int for
                        degrees and the magnitude can be any double value. See Example 7-8.

                        Example 7-8. MotionVector

                        package com.scottpreston.javarobot.chapter7;

                        public class MotionVector extends Edge{

                            public int heading = 0;
                            public double magnitude = 0;

                            public MotionVector(int h, double seconds) {
                                heading = h;
                                magnitude = seconds;
                                weight= (int)seconds;
                            }

                            public MotionVector(String h, String seconds) throws Exception {
                                heading = new Integer(h).intValue();
                                magnitude = new Double(seconds).doubleValue();
                                weight= (int)magnitude;
                            }

                            public String toString() {
                                return "Heading: " + heading + " Seconds: " + magnitude;
                            }
                        }
                            The next class, DistanceVector, is basically the same as the MotionVector except that in
                        this navigation class I convert inches to seconds via a conversion based on a calibration of the
                        robot’s speed and surface. See Example 7-9.

                        Example 7-9. DistanceVector

                        package com.scottpreston.javarobot.chapter7;

                        public class DistanceVector extends MotionVector {

                            public DistanceVector(int h, double inches) {
                                super(h, inches);
                            }
   253   254   255   256   257   258   259   260   261   262   263