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

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



                                                                               CHAPTER 7  ■  NAVIGATION  267



                        which the robot might want to navigate. The fourth field is an int[] called characteristic. This
                        field represents the four coordinate axes as they would be measured in this region. The array
                        {1,0,0,1} would represent readings in the north and west direction, but not long or out-of-range
                        readings in east and south.
                            The region has a constructor of a name and a size. In the constructor, I also add the center
                        waypoint since it will be needed later for navigation.
                            Outside of standard getter and accessor methods, I have three methods, one that will get
                        the point in the region by name, and two other methods that do point translations. The method
                        getScaledPoint() converts points taken in this region, say, from a sonar to a scaled point in the
                        region within the bounds of 100 × 100. The method getScaledMagnitude() converts the final
                        distance vector back to actual inches for any movement. See Example 7-16.


                        Example 7-16. Region.java
                        package com.scottpreston.javarobot.chapter7;

                        import java.awt.Point;
                        import java.util.ArrayList;

                        public class Region extends Vertex{

                            //list of way points in center
                            private ArrayList wayPoints = new ArrayList();
                            // start point in region absolute coordinates
                            private int size = 0;
                            // used to determine position within region N,E,S,W readings
                            private int[] characteristic = new int[]{0,0,0,0};

                            // constructor
                            public Region(String name, int size) {
                                super(name);
                                this.size = size;
                                // just add center point for later use.
                                addWayPoint(NavPoint.CENTER_POINT,50,50);
                            }

                            // navigation points
                            public void addWayPoint(NavPoint p) {
                                wayPoints.add(p);
                            }
                            public void addWayPoint(String name,int x, int y) {
                                addWayPoint(new NavPoint(name,x,y));
                            }
   281   282   283   284   285   286   287   288   289   290   291