Week 5


We experiment with the light sensor and complete the exercises for Lego Lab 4.

Duration of activity: 7 hours
Group members participating: Ole Rasmussen, Kasper Sørensen

Goal

To complete the exercises for Lego Lab 4.

We will experiment with the light sensor and in the process create several variations of a line follower robot.

Plan

Since our goal is to complete the exercises for this weeks lab the plan will be to complete each exercise in sequence:
  1. Black & White detection and LineFollower:
    Experiment with the BlackWhiteSensor class. Investigate ability to differentiate between black and white. 
  2. Color sensor:
    Creating a class capable of detecting black, white and green based on the BlackWhiteSensor class.
  3. LineFollower with Goal zone:
    We will try to enhance the LineFollowerCal to use our BlackWhiteGreenSensor and stop in a green goal zone.
  4. PID controlled LineFollower:
    Here we will try to enhance the LineFollower so that it can use PID control on the raw sensor values from a LightSensor.
  5. LineFollower using ColorSensor:
    Here we will experiment with the NXT ColorSensor and see if we are able to create a LineFollower using it.

Black & White detection and LineFollower

In this exercise we will test if the BlackWhiteSensor class can reliably differentiates between black and white after a calibration run. We will then try the LineFollowerCal application and see how it behaves.

With regards to differentiating between black and white the BlackWhiteSensor does a great job. Our test program can be seen in Figure 1. On paper (See Figure 2), the robot is able to detect without error what color is under the sensor.
Figure 1 - Our test program.
Figure 2 - Robot on paper. Detects black and white correctly.
Out of interest we tried lifting the robot to see how far from the paper it could differentiate between black and white. It turned out not to be very capable at a distance of around 5cm or more (see Figure 3) which was expected due to the line not being very thick.
Figure 3 - Robot lifted from the surface. Unable to detect correctly.
We also tried testing the robot above one of our computers where each surface (white, black) was much larger than on the paper (see Figure 4). It turned out that the robot had a much easier time distinguishing black from white even when lifted above 5cm
Figure 4 - Lifted above computer, still detects correctly.

Results

Based on the above observations we believe the robot will be able to follow a black line. It turned out we were right (see Figure 5).
Figure 5 - Primitive line follower

Color sensor

Having tested the BlackWhiteSensor class we will now try to enhance it so that it is able to detect one more color, namely green. We will call this class BlackWhiteGreenSensor.

First we checked what light values we would get from the sensor above surfaces of Black, White and Green.
The results can be seen on Figure 6 (the three arrows above the bar).

Figure 6 - Sensor values and interpretation of color for a given value.
We observed that the green color is situated between the readings of black and white colors. On the basis of this, we defined the BlackGreen and GreenWhite threshold and used these to determine what color is currently below the sensor. See Figure 6 for our implementation of this. One note: we changed the design of our class (compared to BlackWhiteSensor) such that instead of having three different methods isGreen(), isBlack() and isWhite() we simply return the current color under the sensor readColor() - we feel like this is a better design and allows for less clutter in the client code as can be seen on figure 6.
Figure 7 - Implementation of BlackWhiteGreenSensor
To test our new BlackWhiteGreenSensor we write a test program (See Figure 7). This program will display the color returned from the sensor on the LCD display. We observed that it correctly detects the colors if placed directly above them. However, if we placed the robot just at the edge of a black line, the robot would believe it detects a green color, since the light value returned is in this region (average between black and white).

Figure 8 - Test program for BlackWhiteGreenSensor

LineFollower with Goal zone

In this exercise we will use our newly implemented BlackWhiteGreenSensor to follow a line and stop in a green goal zone.

First we simply used the code from LineFollowerCal.java with the only modification that it will stop the robot when reading a single green value from the BlackWhiteGreenSensor. The result can be seen in figure 9. As expected, it will sometimes stop on the edge of a line, since it believes it is inside the goal zone. This is due to the fact that the sensor will read the color as green when its placed directly above the intersection between black and white.
Figure 9 - First attempt at following line with our BlackWhiteGreenSensor
In our second attempt we modified the program to only stop the robot if it reads several consecutive green values in a predefined period of time (like 500ms). The result of this modification can be seen in figure 10.
The reason that this works is that it will discard spontaneous green readings occurring when the sensor is directly above the edge of the line.
Figure 10 - Second and final attempt at following line with our BlackWhiteGreenSensor
The implementation of this line follower can be seen in Figure 11.

Figure 11 - Final code for our line follower using BlackWhiteGreenSensor

PID controlled LineFollower

We have constructed a PID line follower as can be seen in figure 12. The video of this robot can be seen in figure 13.
Figure 12 - Source code for PID line follower
Figure 13 - Final code for our line follower using BlackWhiteGreenSensor

By comparing the PID line follower (figure 13) to the bang-bang line follower (figure 10) we can clearly see that the PID follower is faster and smoother. It still oscillates a bit but we couldn't get it perfect.

LineFollower using ColorSensor

We didn't have time to do this exercise.

Conclusion

We have made all parts for this week unless the last exercise so next week we'll start on the week 6 exercises.