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

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



                 96     CHAPTER 3  ■  MOTION



                        Example 3-17. ServoPosition2.java
                        package com.scottpreston.javarobot.chapter3;

                        public class ServoPosition2 extends ServoPosition {

                            // minimum position of arm
                            public int min;
                            // maximum position of arm
                            public int max;
                            // neutral position of arm
                            public int neutral;

                            public ServoPosition2(int pin) throws Exception {
                                super(pin,SSCProtocol.NEUTRAL);
                                min = SSCProtocol.MIN;
                                max = SSCProtocol.MAX;
                                neutral = SSCProtocol.NEUTRAL;
                            }
                            public ServoPosition2(int pin, int pos, int min, int max) throws Exception{
                                super(pin,pos);
                                if (min > 255 || min < 0) {
                                    throw new Exception("Minimum out of range, 0-255 only.");
                                }
                                if (max > 255 || max < 0) {
                                    throw new Exception("Maximum out of range, 0-255 only.");
                                }
                                this.min = min;
                                this.max = max;
                                this.neutral = pos;
                            }

                        }
                            The ComplexArm class in this example uses fields of type ServoPosition2. I named these
                        fields according to the limb they represent (shoulder1, shoulder2, elbow, and so on). In the
                        ArrayList servos I store these servo positions for later use. In lm32, I have an instance of the
                        worker class LM32.
                            The constructor takes the JSerialPort and calls the init() method. Init() creates new instances of
                        the servo positions and adds them to the ArrayList of servos (in a separate method for a simple
                        constructor).
                            The rest() method in ComplexArm is similar to BasicArm, except that rather than calling
                        each servo separately, I iterate through the list of servos, create the command, and then call
                        move() over a time of 1 second.
                            The posA() and posB() methods have specific positions for each servo, but instead of
                        jerking to one position and pausing, the movements are slow over a total time of 1 second.
                            The move() method checks the range by iteration through the list and checks pin limits.
                        Then it calls the LM32 move() command.
   110   111   112   113   114   115   116   117   118   119   120