Page 56 - The Unofficial Guide to Lego Mindstorms Robots
P. 56

45


                 if I'm on the line, go straight forward
                 if I'm off the line, find the line and start over

          It's the "find the line" part that's difficult. When Trusty's light sensor goes off the black line, Trusty has no way of knowing if
          he's on the right or the l eft side of the line. Ideally, Trusty would turn back to the line and start going straight again. He would
          p roceed in a zigzag fashion along the line.

          State

          Even if Trusty doesn't know which side of the line he's on, he can make a pretty  good guess. If he knows he drove off the left
          si de of the line last time, it's a pretty good bet he'll drive off the right side the next time. Figure 3-2 shows a likely path as
          Trusty tries to stay on the line.












                                                        Figure 3-2.
                                               Trusty zigzags along the  black line

          With this in mind, Trusty's algorithm can be more specific:

                 if I'm on the line, go straight forward
                 if I'm off the line {
                        turn back the opposite direction from the way I turned last time
                        if I still don't find the line, turn farther back the other direction
                 }

          Another way of looking at this is to say that Trusty now has two possible states:

          1. Just turned left (turn right next)

          2. Just turned right (turn left next)

          By keeping track of this state, Trusty can figure out the best way to turn the next time he drives off the black line.

          Could I Please Have a Variable?

          Some kind of variable is needed if Trusty is to keep track of his state. Said another way, Trusty needs some kind of memory to
          remember  which way  he  last  turned.  This  highlights one of  the weak points  of RCX  Code (the  RIS programming
          environment), its lack of variables.
   51   52   53   54   55   56   57   58   59   60   61