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

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



                                                                                  CHAPTER 3  ■  MOTION    89



                            // helper method to get difference
                            private int getTotalDiff() {
                                int totalDiff = 0;
                                for (int i = 0; i < commands.size(); i++) {
                                    ServoPosition newPos = (ServoPosition) commands.get(i);
                                    ServoPosition curPos = (ServoPosition) servos.get(newPos.pin);
                                    int tmpDiff = Math.abs(newPos.pos - curPos.pos);
                                    totalDiff = totalDiff + tmpDiff;
                                }
                                return totalDiff;
                            }


                            private ServoPosition getServo(int id) {
                                return (ServoPosition) servos.get(id);
                            }


                            // sample program same as LM32
                            public static void main(String[] args) {
                                try {
                                    // get single serial port instance
                                    JSerialPort sPort = (JSerialPort) SingleSerialPort.getInstance(1);
                                    // create new LM32
                                    MiniSscGM miniSscGM = new MiniSscGM(sPort);
                                    // sets position for servo at pin 0
                                    miniSscGM.sscCmd(0, 100);
                                    // sets position for servo at pin 1
                                    miniSscGM.sscCmd(1, 200);
                                    // tells the servos to move there in 1 second.
                                    miniSscGM.move(1000);
                                    // close serial port
                                    sPort.close();
                                } catch (Exception e) {
                                    // print stack trace and exit
                                    e.printStackTrace();
                                    System.exit(1);
                                }

                            }
                        }


                        Section Summary
                        You can see from the GroupMoveProtocol that the LM32 has a lot of advantages over the
                        SSCProtocol when you want smoother movement or have a lot of servos you want to command
                        at once.
   103   104   105   106   107   108   109   110   111   112   113