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

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



                 254    CHAPTER 7  ■  NAVIGATION



                                } catch (Exception e) {
                                    e.printStackTrace();
                                    System.exit(1);
                                }
                            }
                        }
                            Now that the robot can move its sonar, it needs to look at four coordinate axes (N, E, S, and W),
                        and because the robot is in a perfect world and knows where the walls are located, it just needs
                        to take readings for west and south to give the robot its start position.
                            The Localization class extends Navigation. There is one field for the SonarServos class and
                        one for the robot radius during measurements of the robot’s position. This is needed because
                        the sonar servos are 12 inches from the center of the robot and the position of the robot will
                        always be relative to its center.
                            The next method, getStart(), changes the heading of the robot to north, and then moves
                        the sonar servos to the side so the robot can get the sonar reading to its left (west). Next, the
                        sonar servos moves AFT (south), and then takes the average of the two readings since both are
                        facing the same direction.
                            The next method describes how to calculate the start position if the robot is facing some
                        direction other than north. In this case, the program will need to know how to look at its
                        heading, and then move the sonar servos to their best approximation of the coordinate axis N,
                        E, S, and W.
                            To determine its heading, the method first calls its parents accessor to the NavStamp class
                        and then calls getCompass(). Next, the positions of the four axes need to be calculated based
                        on the heading of the robot. Here, the four coordinate axes are calculated by subtracting the
                        heading from the four coordinate axes angular values (0, 90, 180, and 270). For example, if the
                        robot is facing east, then its east position is in front of it or at 0 degrees. If the robot is facing
                        southeast, it’s –90 degrees, or to it’s left at 270, and so on. Don’t worry about the negative
                        numbers on the degrees, because the SonarServos will adjust this reading to the degree corre-
                        sponding from 0 to 360 degrees.




                        ■Note  The trigonometric functions can use either –90 or 270 to produce the correct number; however, it’s
                        easier to explain when talking about the range 0 to 360.



                            Next, depending on the heading of the robot, it will need to move its sonar to the corre-
                        sponding closest position. So, from 0 to 90 degrees, its best positions are to the south and west.
                        But while facing from 90 to 180 degrees, the west position is out of range of the left sonar, and
                        while I could make two readings for south and west with the right sonar, it’s not as efficient. So
                        instead I will take a north reading with the left sonar, and measure the west with the right. I
                        continue alternating what sonar takes what readings by recording the “bestReadings” for a
                        given heading for 180 to 270 degrees, and from 270 to 360 degrees.
                            Finally, at the end of the method, depending on the measurements taken, I either subtract
                        the north or the east reading from 100, since the room is a 100 × 100 grid. Then at the end I
                        adjust the readings based on the radius of the robot.
   268   269   270   271   272   273   274   275   276   277   278