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

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



                 58     CHAPTER 3  ■  MOTION



                            // accessor
                            public int getMaxPin() {
                                return maxPin;
                            }
                            // setter
                            public void setMaxPin(int maxPin) {
                                this.maxPin = maxPin;
                            }

                        }
                            Now that the general super-class has been created, it’s time to create a specific class for the
                        MiniSSC-II. This class has no fields and simply calls the parent constructor and setMaxPin
                        method to limit the total pins to seven.
                            In the example program in main(), I call the same logic that composed the class SerialSSC
                        in Example 3-2. Move the servo through the range of motion in 5-byte increments. You’ll note
                        that its command structure is simpler (no bytes to create or cast), and you have error control
                        built in. (See Example 3-5.)


                        Example 3-5. MiniSsc.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 MiniSsc extends Ssc implements SSCProtocol {

                            // calls super and sets max pin to 7
                            public MiniSsc(JSerialPort serialPort) throws Exception {
                                super(serialPort);
                                setMaxPin(7);
                            }

                            // sample program
                            public static void main(String[] args) {
                                try {
                                    // get single serial port instance
                                    JSerialPort sPort = (JSerialPort) SingleSerialPort.getInstance(1);
                                    // create new miniSSc
                                    MiniSsc ssc = new MiniSsc(sPort);
   72   73   74   75   76   77   78   79   80   81   82