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

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



                 92     CHAPTER 3  ■  MOTION



                        Example 3-15. BasicArm.java
                        package com.scottpreston.javarobot.chapter3;

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

                        public class BasicArm {

                            private MiniSsc ssc;
                            // set shoulder and elbow parameters
                            public static final int SHOULDER_PIN = 0;
                            public static final int SHOULDER_MAX = SSCProtocol.MAX;
                            public static final int SHOULDER_MIN = SSCProtocol.MIN;
                            public static final int SHOULDER_REST = 55;
                            public static final int ELBOW_PIN = 1;
                            public static final int ELBOW_MAX = SSCProtocol.MAX;
                            public static final int ELBOW_MIN = SSCProtocol.MIN;
                            public static final int ELBOW_REST = 65;
                            // instance variables of current position
                            private int shoulderPos = SSCProtocol.NEUTRAL;
                            private int elbowPos = SSCProtocol.NEUTRAL;

                            //constructor taking JSerialPort as parameter
                            public BasicArm(JSerialPort sPort) throws Exception {
                                ssc = new MiniSsc(sPort);
                            }

                            // passthrough to ssc
                            private void move(int pin, int pos) throws Exception {
                                ssc.move(pin, pos);
                            }

                            // move the shoulder
                            public void shoulder(int pos) throws Exception {
                                if (pos < SHOULDER_MIN || pos > SHOULDER_MAX) {
                                    throw new Exception("Out of shoulder range.");
                                }
                                shoulderPos = pos;
                                move(SHOULDER_PIN, pos);
                            }

                            // move the elbow
                            public void elbow(int pos) throws Exception {
                                if (pos < ELBOW_MIN || pos > ELBOW_MAX) {
                                    throw new Exception("Out of elbow range.");
                                }
   106   107   108   109   110   111   112   113   114   115   116