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

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



                 56     CHAPTER 3  ■  MOTION



                                try {
                                    // create serial port
                                    StandardSerialPort serialPort = new StandardSerialPort(1);
                                    // increment position by 5 each time in loop
                                    for (int pos = 0; pos < 255; pos = pos + 5) {
                                        // create byte array for ssc commands
                                        byte[] sscCmd = new byte[] { (byte) 255, 0, (byte) pos };
                                        // send byte array to serial port
                                        serialPort.write(sscCmd);
                                        // pause between commands
                                        Thread.sleep(100);
                                    }
                                    // close serail port
                                    serialPort.close();
                                } catch (Exception e) {
                                    // print stack trace and exit.
                                    e.printStackTrace();
                                    System.exit(1);
                                }
                            }
                        }
                            While the preceding class does just what I want it to, I thought that given all the work in the
                        coming sections and chapters it would be a good idea to standardize communication to the SSC
                        because its communication protocol is fixed. To do this, I created the interface SSCProtocol.java. It
                        has a single method defined, move(pin, pos). The method has the following parameters: pin—
                        the pin position of where the servo is plugged in; and pos—the position of the servo from 0 to 255.
                        I also added a few constants to simplify classes using this protocol. (See Example 3-3.)


                        Example 3-3. SSCProtocol.java
                        package com.scottpreston.javarobot.chapter3;

                        public interface SSCProtocol {

                            // maximum
                           public static final byte MAX = (byte) 255;
                           // neutral
                           public static final byte NEUTRAL = (byte)127;
                           // minimum
                           public static final byte MIN = (byte) 0;


                           /**
                            * @param pin - connector on the MiniSSC 0-7
                            * @param pos - byte from 0-255
                            */
                           public void move(int pin, int pos) throws Exception;
                        }
   70   71   72   73   74   75   76   77   78   79   80