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

51





























                                                        Figure 3-6.
                                             Adding a second light sensor to Trusty


          Programming

          With two side-by-side light sensors, Trusty's algo rithm is pretty simple. If both sensors are on the line, we just drive forward. If
          only one of the sensors is on the line, we simply turn back to the line. If both sensors go off the line, we don't do anything; the
          robot must already be turning and should return to the line soon.

          This logic can be represented by a simple map from the sensor values to the motor settings, as shown in Table 3-1.

          Table 3-1. Mapping from Sensor Inputs to Motor Outputs

          Left Light   Right Light  Left Motor(A)  Right Motor(C)    Result
          Sensor (2)   Sensor (3)

          dark       dark       forward       forward          Go  forward

          dark       bright     stop          forward          Turn  left

          bright     dark       forward       stop             Turn  right

          bright     bright     -             -                No  change

          It's cumbersome to implement this algorithm in RCX Code. Figure 3-7 shows the program, which is based around two sensor
          watchers. But go ahead and try it out; it works well even if it looks kind of strange. In Chapter 4, I'll show you how this
          algorithm (as well as the single-light-sensor Trusty) can be reprogrammed in NQC.

          The program centers around two sensor watchers, one for each light sensor. Whenever either sensor sees light or dark, the
          value of the other is examined. Based on the values of the two sensors, the motors are set according to Table 3-1.
   57   58   59   60   61   62   63   64   65   66   67