Page 294 - Hacking Roomba
P. 294

Chapter 13 — Giving Roomba a New Brain and Senses                    275



                             In order to measure a variable resistor like a photocell or potentiometer, you would usually cre-
                             ate a voltage divider with another fixed resistor and feed the output of that voltage divider to an
                             analog input of your microcontroller. The BS2 has no analog inputs, but it does have an inter-
                             esting function called RCTIME that records the amount of time it takes a capacitor to charge or
                             discharge through a resistor. The charge time to go from a digital LOW to HIGH is related to
                             the resistance value.The capacitors in parallel with the photocells and the resistors leading to the
                             BS2 pins are part of this timing circuit RCTIME needs. Because capacitors, resistors, and photo-
                             cells all have widely varying tolerances and are temperature sensitive, the values returned from
                             RCTIME cannot be used as absolute values, but rather as a mark of relative change. For details on
                             how RCTIME works, see the Basic Stamp language reference available from the Parallax site.
                             Listing 13-2 shows how to use RCTIME to measure the photocells. Only the front photocell
                             measurement is shown, but the back one is just the same. The code spins Roomba around every
                             so often, checking light levels. When it finds a darker area, it drives straight toward it. Even
                             with such a simple algorithm, Roomba will almost consistently find the darkest area of a room.
                             It never stops moving though, and still runs the BumpTurn algorithm so it avoids obstacles.

                             As a final bit of fun, you can now chase Roomba around with a flashlight.


                               Listing 13-2: Additions to BumpTurn to Make RoombaRoach

                               ‘ the pin where our light sensitive resistor is
                               LSRF        PIN  1
                               Main
                               ‘ ...everything else as in BumpTurn, then...

                               ‘ check light levels, find the dark
                                   FOR i = 1 TO 4
                                        oldresF = resF
                                        GOSUB Read_LightF
                                        IF resF > oldresF+10 THEN ‘ its getting darker
                                            GOSUB Go_Forward
                                        ELSE
                                            GOSUB Spin_Left
                                            PAUSE 500
                                        ENDIF
                                   NEXT
                                   GOTO Main
                               Read_LightF:
                                   HIGH LSRF  ‘ charge cap
                                   PAUSE 1    ‘ for 1 ms
                                   RCTIME LSRF, 1, resF
                                   DEBUG DEC ? resF  ‘ print out value to debug port
                               RETURN
   289   290   291   292   293   294   295   296   297   298   299