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

Preston_5564C04.fm  Page 133  Wednesday, October 5, 2005  7:22 AM



                                                                                 CHAPTER 4  ■  SENSORS   133



                        ping_srf:
                          PULSOUT INIT1,5                      ' 10us init pulse
                          OUTPUT INIT1                         ' (delay)
                          RCTIME ECHO1,1,wDist1                ' measure echo time
                          wDist1=wDist1/convfac                ' convert to inches
                          SEROUT 16,N9600,[DEC wDist1,CR]
                          GOTO main


                        ping_6500:
                          PULSOUT INIT2,5                      ' 10us init pulse
                          OUTPUT INIT2                         ' (delay)
                          RCTIME ECHO2,1,wDist2                ' measure echo time
                          wDist2=wDist2/convfac                ' convert to inches
                          SEROUT 16,N9600,[DEC wDist2,CR]
                          GOTO main

                            Next is the DistanceStamp class, which reads the data from the distance.bs2 program in
                        Example 4-7. The fields of this class are similar to the CompassStamp with the exception of the
                        command names and the instance field distSensor, which enumerates the distance reading.

                        Example 4-7. DistanceStamp.java

                        package com.scottpreston.javarobot.chapter5;
                        import com.scottpreston.javarobot.chapter2.Controller;
                        import com.scottpreston.javarobot.chapter2.JSerialPort;
                        import com.scottpreston.javarobot.chapter2.SingleSerialPort;
                        public class DistanceStamp extends Controller {


                            // commands set in basic stmap program
                            public static final int CMD_INIT = 100;
                            public static final int CMD_IR = 101;
                            public static final int CMD_SRF = 102;
                            public static final int CMD_6500 = 103;

                            private int distSensor = CMD_SRF;


                            // constructor
                            public DistanceStamp(JSerialPort sPort) throws Exception {
                                super(sPort);
                            }

                            public int ping(int distSensor) throws Exception {
                                setDistSensor(distSensor);
                                return ping();
                            }
   147   148   149   150   151   152   153   154   155   156   157