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

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



                                                                               CHAPTER 7  ■  NAVIGATION  241



                            While inside the loop, the robot checks its current heading relative to the direction it wants
                        to face. I call this reading relHeading (short for relative heading), and to keep the degrees
                        always between 0 and 360, I added 360 to those relative headings less than zero. Once the robot
                        knows its relative heading, it can begin turning. If the relative heading is between 0 and 180,
                        then I tell it to pivot left. If the relative heading is between 180 and 360, then I tell it to pivot
                        right. Depending on how far away the robot is from its target position, I decrease the turn time.
                        Then once the accuracy is reached, I make sure the drive is stopped. I break out of the loop and
                        I reset the speed to 2.
                            The next methods in the class are two move() methods. One takes a DistanceVector and
                        the other takes a MotionVector. For the method taking the DistanceVector as a parameter,
                        inches get converted to seconds using the getSurfaceRate() method. It is important to take
                        measurements for this if you are not using an encoder. If you are using an encoder, then your
                        drive class will already have a mechanism for stopping you at a specified distance, so here you
                        would call that method from your drive class rather than do a conversion.
                            Finally, in the main() test method, the robot moves in a 3-foot square box in the directions
                        east, north, west, and south. In the end, it should be right back where it started, provided that
                        your calibrations are correct. (See Example 7-10.)

                        Example 7-10. Navigation.java
                        package com.scottpreston.javarobot.chapter7;

                        import com.scottpreston.javarobot.chapter2.JSerialPort;
                        import com.scottpreston.javarobot.chapter2.Utils;
                        import com.scottpreston.javarobot.chapter2.WebSerialClient;
                        import com.scottpreston.javarobot.chapter3.JMotion;
                        import com.scottpreston.javarobot.chapter3.SpeedDiffDrive;

                        public class Navigation {

                            // movement constants for raw movement
                            public static final int RAW_FWD = 0;
                            public static final int RAW_REV = 1;
                            public static final int RAW_RGT = 2;
                            public static final int RAW_LFT = 3;
                            // relative readings for 4 coordinate axes
                            public static final int REL_NORTH = 40;
                            public static final int REL_EAST = 100;
                            public static final int REL_SOUTH = 160;
                            public static final int REL_WEST = 255;
                            // surface constants
                            public static final int SURFACE_CEMENT = 1;
                            public static final int SURFACE_CARPET = 2;
                            // default speed
                            public static final int DEFAULT_SPEED = 25;







                   97022d2480fe4a63cfdfa123a6e70098
   255   256   257   258   259   260   261   262   263   264   265