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

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



                                                                                  CHAPTER 3  ■  MOTION   103



                            // move leg backward
                            public String backward() {
                                return LM32.createCmd(horzServo.pin,horzServo.min,LM32.DEFAULT_SPEED);
                            }
                            // reset horz servo
                            public String neutralHorz() {
                                return LM32.createCmd(horzServo.pin,horzServo.neutral,LM32.DEFAULT_SPEED);
                            }

                            // reset vert servo
                            public String neutralVert(){
                                return LM32.createCmd(vertServo.pin,vertServo.neutral,LM32.DEFAULT_SPEED);
                            }

                            // reset both servos
                            public String neutral() {
                                return neutralVert() + neutralHorz();
                            }

                        }
                            The Hexapod class has as fields the LM32 as the worker class and six legs defined as
                        BasicLegs. I also defined two leg groups as ArrayList. Because of the gait I chose, I’ll move three
                        legs simultaneously. By placing them in a list, I can move all legs in this group with a single
                        command.
                            The fields UP, DOWN, and so on are enumerations. The int speed is the speed variable,
                        and MAX_SPEED represents what the minimum time should be for the group move of three legs.
                            The constructor takes, you guessed it, the JSerialPort and calls two methods init() and
                        setLegGroups(). The init method creates all leg positions for the six BasicLegs. The setLegGroups
                        method adds these to legGroup1 and legGroup2.
                            The forward() gait is a combination of eight separate commands. The gait commands are
                        created via the getTotalMove() method which comprises a StringBuffer. The method operates
                        by iterating through all the legs and (depending on the command) returns the string from the
                        BasicLeg.motion() method. This is repeated until all the legs are done. Then the group move is
                        executed in the time specified via the getSpeedInMs() method.
                            The getSpeedInMs() method uses an inverse relationship between the time of the leg
                        move and the speed. This is so that 10 is still fast and 1 is still slow. For example: the speed of
                        the robot at speed 10 is 2500 – 2500 + 250 milliseconds per move of a leg group, and at a speed
                        of 1 the speed of the robot is 2500 – 250 + 250 = 2500 milliseconds per leg.
                            In the forward() method with a millisecond parameter, it loops through calling forward
                        until the time is up. This is all a function of the speed of the robot and it even throws an exception
                        if the requested move time is less than the minimum time. (See Example 3-20.)
   117   118   119   120   121   122   123   124   125   126   127