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

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



                                                                               CHAPTER 7  ■  NAVIGATION  251


                        Code Objective

                        The code objective here is to give the robot the ability to find its start position, and then to navi-
                        gate with dead reckoning.


                        Code Discussion
                        Given my robot has two moving sonar, one fixed sonar, and a known environment (a Perfect
                        World), it will just have to perform two tasks. First, it will face north so it can align itself with the
                        walls of its environment. Second, it will need to take readings of the south and west walls to
                        determine my location. To perform this task, I’ll need to create a class for my movable sonar
                        called SonarServos.
                            The class has three instance fields, two to hold the positions of the servos and one for the
                        MiniSsc. The other static fields in the class are specific for Feynman5 and were obtained
                        through experimentation.
                            The constructor of the class takes a JSerialPort and is used to construct the MiniSsc class
                        responsible for moving the sonar.
                            The move() method takes two arguments: left and right. These raw positions move the
                        sonar. You can use this method when calibrating your robot for its AFT and FORE positions.
                            The moveLeft() and moveRight() methods take angles in degrees. Once again, I need to
                        convert the angles to match the N, E, S, W coordinate system. So 0 is in front of the robot, 90 is
                        to its right, and so on.
                            The left sonar only has valid angles from 180 to 360, while the right sonar only has valid
                        angles from 0 to 180. The methods take into account the FORE and AFT positions of the sonar
                        so that the robot moves the left or right sonar to the best approximation of the angle from the
                        byte resolution servo position.
                            In main(), the sonar moves to the front, to the back, and to the side of the robot. This will
                        validate that you have set the constants correctly. Then program the moves through angles
                        from 0 to 360. Here you can observe the robot moving one sonar at a time since the sonar are
                        each only capable of moving through 180 degrees. See Example 7-13.

                        Example 7-13. SonarServos.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.MiniSsc;

                        public class SonarServos {

                            public static final int LEFT_SONAR = 2;
                            public static final int RIGHT_SONAR = 3;
                            public static final int LEFT_AFT = 60;
                            public static final int LEFT_NEUTRAL = 150;
                            public static final int RIGHT_NEUTRAL = 110;
                            public static final int LEFT_FORE = 245;
   265   266   267   268   269   270   271   272   273   274   275