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

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



                 82     CHAPTER 3  ■  MOTION



                            The difference seen in the following class is that the interface is the actual format of the
                        command string in the method createCmd(). Also, because we’re used to dealing with servo
                        positions between 0–255, I left this in rather than requiring the worker program to keep track of
                        pulse widths between 750 and 2250 milliseconds.
                            The two fields in the class are cmds—a StringBuffer and a Boolean—to provide status to
                        any class using this object. Because the timing is determined by the LM32, I choose to use a
                        timer to signal the class for state change (busy=false). This is done via the setBusy() method
                        where a single task is set to be run at a specified time in the future (now + milliseconds in future).
                            The constructor calls the super constructor with the JSerialPort, and sets the maxPins field
                        to 32. The move() method converts the StringBuffer to a byte[] before sending out the serial
                        port via the Controller, execute method.




                        ■Note  I could have put in a parameter here for a delay in the call to execute(), but I wanted to show you
                        another way to do the same thing without tying up resources during a move.



                            The construction of the serial command has the following syntax:

                        "#" + channel (0-31)
                        + "P" + pulsewidth (750-1500milliseconds)
                        + "S" + speed(milliseconds for move)
                            You can string up to 32 commands together before you have to terminate it by appending
                        the following:
                        "T" + time for total move(milliseconds) + "\r"

                            The pulsewidth is the time in milliseconds that I have simplified via the getPw() method.
                        It will return a pulsewidth ranging from 750 to 1500 from a byte between 0 and 255.
                            Finally, the test program uses only two servos and moves them to positions 100 and 200,
                        respectively, during a 1-second timeframe. (See Example 3-12.)

                        Example 3-12. LM32.java

                        package com.scottpreston.javarobot.chapter3;

                        import java.util.Date;
                        import java.util.Timer;
                        import java.util.TimerTask;

                        import com.scottpreston.javarobot.chapter2.JSerialPort;
                        import com.scottpreston.javarobot.chapter2.SingleSerialPort;
                        import com.scottpreston.javarobot.chapter2.Utils;
   96   97   98   99   100   101   102   103   104   105   106