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

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



                 116    CHAPTER 4  ■  SENSORS



                            Now that you’re familiar with the hardware, I’ll create some software to get readings from
                        these using a BASIC Stamp. Afterward, I’ll create two Java classes.

                        Code Objective

                        The objective here is to get compass readings from a robot using one of the three compasses
                        described earlier.


                        Code Discussion
                        I’ll start with programming the BASIC Stamp. In the first part of this program (see Example 4-1),
                        I connect my three compasses to the different pins on the Stamp (1 and 2) for the Devantech,
                        (8, 9, 10, and 11) for the Vector, and (0, 1, 2, and 3) for the Dinsmore.
                            Second, I create my main program where I wait for specific commands: 100 to start reading,
                        101 for the Dinsmore, 102 for the Devantech, and 103 for the Vector.
                            Third, I create three separate subroutines for the different compasses. For the Devantech
                        compass, I just need to use the PULSIN to get the compass reading, For the Vector, I need to
                        send some signals and wait for a time before I can SHIFTIN the readings. And the Dinsmore
                        reads logic values from the input pins to return one of eight readings (N, E, S, W, NE, NW, SE,
                        SW). (See Figure 4-1.)


                        Example 4-1. Compass.bs2
                        ' {$STAMP BS2}
                        ' {$PBASIC 2.5}
                        ' {$PORT COM1}
                        ' cmd variable
                        cmd              VAR       Byte
                        N9600            CON       16468

                        ' CMPS03 COMPASS
                        cin              CON       12 'serial data out     GREEN (a)
                        headingC         VAR       Word 'heading

                        ' VECTOR COMPASS
                        sselect          CON       10 'select signal
                        sdo              CON       09 'serial data out
                        sclk             CON       11 'clock signal
                        rst              CON       8  'reset signal
                        headingV         VAR       Word 'heading

                        ' DINSMORE 1490 COMPASS
                        north            CON       0
                        east             CON       1
                        south            CON       2
                        west             CON       3
                        headingD         VAR       Byte
   130   131   132   133   134   135   136   137   138   139   140