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

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



                 62     CHAPTER 3  ■  MOTION



                            The constructor for this class takes a JSerialPort, which builds the MiniSSC. The setMotors()
                        method sets parameters regarding the position and movement of the SSC. This method is
                        followed by move(), which calls the MiniSSC method of the same name.
                            The four directional methods—forward(), reverse(), pivotLeft(), and pivotRight()—have
                        two functions: first, to check to see if the motors are connected in an inverted fashion (like the
                        CubeBot), and then to set the values of the motors, and secondly, to call the move method.
                        While I could have just called the ssc.move(), allowing me to extend and add a speed control
                        later, I could not have reused the method for later subclasses. However, if you don’t want speed
                        control, you can simplify this by just inserting the move positions directly in each of the four
                        directional methods.
                            The setters and accessor methods are included so classes can access the state variables of
                        right and left for the subclass TimedDiffDrive. Finally, in main() I test the class by sending the
                        robot forward for 2 seconds, and then stopping. (See Example 3-6.)

                        Example 3-6. BasicDiffDrive.java
                        package com.scottpreston.javarobot.chapter3;

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

                        public class BasicDiffDrive {

                            // drive will use MiniSSC
                            private MiniSsc ssc;

                            // left wheel hooked to pin 0
                            public static final int LEFT_WHEEL = 0;
                            // right wheel hooked to pin 1
                            public static final int RIGHT_WHEEL = 1;
                            // set all to neutral values
                            private int right = SSCProtocol.NEUTRAL;;
                            private int left = SSCProtocol.NEUTRAL;
                            private int rightHigh = SSCProtocol.MAX;
                            private int rightLow = SSCProtocol.MIN;
                            private int leftHigh = SSCProtocol.MAX;
                            private int leftLow = SSCProtocol.MIN;

                            // right will always be the one inverted can change this
                            private boolean motorsInverted = false;

                            // constructor takes JSerialPort
                            public BasicDiffDrive(JSerialPort serialPort) throws Exception {
                                // create MiniSSC
                                ssc = new MiniSsc(serialPort);
                            }
   76   77   78   79   80   81   82   83   84   85   86