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

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



                                                                                  CHAPTER 3  ■  MOTION    87



                        Example 3-14. MiniSscGM.java
                        package com.scottpreston.javarobot.chapter3;

                        import java.util.ArrayList;

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

                        public class MiniSscGM extends Ssc implements SSCProtocol, GroupMoveProtocol {

                            // store commands in list
                            private ArrayList commands = new ArrayList();
                            // store servos in list
                            private ArrayList servos = new ArrayList();

                            // constructor takes JSerialPort as parameter
                            public MiniSscGM(JSerialPort jSerialPort) throws Exception {
                                super(jSerialPort);
                                setMaxPin(7);
                                // create servos
                                createServos();
                            }
                            // add servos to list
                            private void createServos() throws Exception{
                                for (int i = 0; i < getMaxPin() + 1; i++) {
                                    ServoPosition svo = new ServoPosition(i, SSCProtocol.NEUTRAL);
                                    // index will be same as id.
                                    servos.add(svo);
                                }
                            }

                            public void sscCmd(int ch, int pos) throws Exception {
                                // calls overridden move method later in this class
                                move(ch, pos);
                            }

                            public void cmd(int ch, int pos, int spd) throws Exception {
                                // not going to implement the spd variable for the MiniSSC-II
                                ServoPosition svoPos = new ServoPosition(ch, pos);
                                commands.add(svoPos);
                            }

                            public void move(int time) throws Exception {
                                // all servo moves will have a minimum step-size of 3






                   97022d2480fe4a63cfdfa123a6e70098
   101   102   103   104   105   106   107   108   109   110   111