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

Preston_5564C03.fm  Page 68  Wednesday, October 5, 2005  7:21 AM



                 68     CHAPTER 3  ■  MOTION



                            // stop
                            public void stop() throws Exception;
                            // forward
                            public void forward(int ms) throws Exception;
                            // reverse
                            public void reverse(int ms) throws Exception;
                            // pivot right
                            public void pivotRight(int ms) throws Exception;
                            // picot left
                            public void pivotLeft(int ms) throws Exception;
                            // setting speed of robot
                            public void setSpeed(int speed)throws Exception ;
                            // get speed of robot
                            public int getSpeed();



                        }



                        ■Note  The speed will not work in a hacked servo because it’s either full on or full off. There you will have
                        to delay the on-off cycles of your servo to something very fast. It might be difficult getting this to work and be
                        smooth given our baud rate. However, it will work well for an electronic speed control (ECS), but you might
                        want to adjust the speed to a higher resolution than 10.



                            In the SpeedDiffDrive class, I implement the JMotion interface and have a single field
                        speed which I defaulted to 5.
                            The setSpeed() method is the heart of the method as it sets the high values for the servo
                        controller as well as the low values. So, at a speed of 10, the high value would be 255, while at a
                        speed of 9 it would be 255 – 13 (12.7) = 242, and so on.
                            At the bottom of the class, I have to implement the methods from the interface that already
                        exist in the super-class. Why do we need to just create pass-through? Java does not support
                        multiple inheritance, so the compiler only sees the BasicDiffDrive’s method for the interface
                        and not the TimedDiffDrive class. (See Example 3-9.)

                        Example 3-9. SpeedDiffDrive.java
                        package com.scottpreston.javarobot.chapter3;


                        import com.scottpreston.javarobot.chapter2.JSerialPort;
                        import com.scottpreston.javarobot.chapter2.SingleSerialPort;

                        public class SpeedDiffDrive extends TimedDiffDrive implements JMotion{

                            // set initial speed
                            private int speed = 5;
   82   83   84   85   86   87   88   89   90   91   92