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

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



                 104    CHAPTER 3  ■  MOTION



                        Example 3-20. Hexapod.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 Hexapod implements JMotion {

                            private LM32 lm32;

                            private BasicLeg leg1; // left front
                            private BasicLeg leg2; // left middle
                            private BasicLeg leg3; // left back
                            private BasicLeg leg4; // right front
                            private BasicLeg leg5; // right middle
                            private BasicLeg leg6; // right back

                            private ArrayList legGroup1 = new ArrayList();
                            private ArrayList legGroup2 = new ArrayList();

                            private static final int UP = 0;
                            private static final int DOWN = 1;
                            private static final int FORWARD = 2;
                            private static final int BACKWARD = 3;
                            private static final int NEUTRAL = 4;

                            private int speed = 5;
                            private static final int MIN_SPEED = 250;

                            public Hexapod(JSerialPort serialPort) throws Exception {

                                lm32 = new LM32(serialPort);
                                // two methods for clean constructor
                                init(); // init all legs
                                setLegGroups(); // set legs in groups

                            }

                            // create legs
                            private void init() throws Exception {
                                // 1st position vertical servo (up/down)
                                // 2nd position horzontal servo (forward/backward)
                                leg1 = new BasicLeg(new ServoPosition2(0, 127, 50, 200),
                                        new ServoPosition2(1, 127, 50, 200));
   118   119   120   121   122   123   124   125   126   127   128