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

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



                                                                                  CHAPTER 3  ■  MOTION    93



                                elbowPos = pos;
                                move(ELBOW_PIN, pos);
                            }


                            public void rest() throws Exception {
                                shoulder(SHOULDER_REST);
                                elbow(ELBOW_REST);
                            }

                            public static void main(String[] args) {
                                try {
                                    // get single serial port instance
                                    JSerialPort sPort = (JSerialPort) SingleSerialPort.getInstance(1);
                                    // create new BasicArm
                                    BasicArm arm = new BasicArm(sPort);
                                    // move to rest position
                                    arm.rest();
                                    // move elbow to 150
                                    arm.elbow(150);
                                    // move shoulder to 200
                                    arm.shoulder(200);
                                    // close serial port
                                    sPort.close();
                                } catch (Exception e) {
                                    // print stack trace and exit
                                    e.printStackTrace();
                                    System.exit(1);
                                }
                            }
                        }

                            The next class will formalize positions a little more than just byte 150 for the elbow and
                        byte 200 for the shoulder.


                        Code Objectives
                        The objective here is to make the positions easier to invoke, and to also simplify arm usage.

                        Code Discussion

                        The only field I will use in this class will be arm, of type BasicArm.
                            The constructor takes the JSerialPort and moves the arm to its rest position.
                            Of the two position methods, toA() and toB() encapsulate the positions of A and B in a
                        method so that you don’t have to remember them from within an invoking class. I pause between
                        the methods so that motion can stop since the movement is still jerky. (See Example 3-16.)
   107   108   109   110   111   112   113   114   115   116   117