Notebook 04

print

Lab Notebook

light_sensor.png

Date: 23/02 2012
Group members participating: Falk, Jeppe and Jakob
Duration of activity: 4 hours

Goal

The goal of the lab session is to use the light sensor to make the robot follow a black line using different techniques and make the robot detect different colors.

Plan

  1. Mount the light sensor on the robot and make a program BWSensorDriver.java that uses the BlackWhiteSensor.java class and test the sensors light and dark detection.
  2. Try out the LineFollowerCal.java line follower program.
  3. Make a program ColorSensor.java based on the BlackWhiteSensor.java and make it detect three different colors: black, blue and white.
  4. Use the ColorSensor.java to make a line follower program StopInGoal.java, that follows a black line and stops in a blue goal zone.
  5. Try to make a smoother and faster line follower program using a PID regulator [5] and only one light sensor.

Results

Black White Detection

The light sensor was mounted accordingly to the manual 9797 page 32-34.

The way to calibrate the light sensor using the BlackWhiteSensor.java class is to first placing the robot over a black surface and pressing enter and then placing it on a white surface and pressing enter. The class uses the mean value between the values for black and white and uses this to detect light and dark.
The program BWSensorDriver.java that uses BlackWhiteSensor.java was created, and it writes the value to the display.
The program is good for detection black and white but it can not detect colors since it uses a threshold the result is always either black or white.

Line Follower with Calibration

The LineFollowerCal.java program makes the robot follow a line quite good. The program calibrates using the BlackWhiteSensor.java and starts to follow the line as soon as it is calibrated. If it can not detect any black in its vicinity it goes around in circels.
There is very little fluctuation in the robots driving and the oscillations becomes very small. This is probably because there is no sleep between the readings as it has been the case in previous assignments.

ColorSensor with Calibration

color_sensor_setup

The BlackWhiteSensor.java was used as the basis of a program: ColorSensor.java, that calibrates black, white and blue using the raw value of the sensor. The readings of the raw values was chosen as opposed to the percentage value to get more accurate readings and to get a larger span of measurement to easier to distinguish the colors.
The program divides the area of measurement into three areas using the read raw values as thresholds, instead of the two areas in the BlackWhiteSensor.java.
To use the ColorSensor.java program the CSDriver.java was created which prints the values to the display.

The following figure illustrates the relationship between the different colors and their respective raw values and how the program divide area of measurement.

color_relations.png

The following table is from a calibration of the three colors performed indoors in normal lighting measured on colored lego bricks on their smooth surface.

Color Raw value
White 401
Blue 583
Black 642

The program proved well to distinguish the three colors.

Line Follower that stops in a Goal Zone

Based on the LineFollowerCal.java program the StopInGoal.java that uses the ColorSensor.java was constructed. The program uses one light sensor and makes the robot travel along the one side of the black line and stop when it gets to a blue area.
The challenge with this approach is that when the sensor measures right between black and white it will measure a value similar to blue, so the naive implementation of making the robot stop when the light sensor reads blue does not work. To overcome this problem the the light sensor has to read a blue value 20 times in a row. Since the time between each reading is 10ms the light sensor has to read blue 200ms in a row. There is of course still a small chance that this is fulfilled before the robot reaches the blue zone, if the robot would drive completely straight where the light sensor reads exactly between black and white (=blue), but the program has not proven to be good enough for this to happen.

...
final int consMeas = 20;
...
while (! Button.ESCAPE.isPressed()) {
    LCD.drawInt(sensor.light(),4,10,2);
    LCD.refresh();
 
    if ( sensor.black() ) {
        Car.forward(power, 0);
        count = 0;
    } else if ( sensor.white() ) {
        Car.forward(0, power);
        count = 0;
    } else if ( sensor.blue() ) {
        count++;
        if (count > consMeas) {
            count = 0;
            break;
        }
    }
    Thread.sleep(10);
}
...

The value 20 (consMeas) can seem a bit magical, and maybe it is. The value 5 was too small, and with the value 50 the robot drove quite far into the blue zone, and 20 is just a choice between those two scenarios, but 30 would probably be a good value as well.
This implementation works nice on a straight line, but it does not perform well on a narrow or curved line.


As it can be seen in the above video the robot does not stop until the light sensor has read several blue values.

PID Line Follower

The description and pseudo code from A PID Controller For Lego Mindstorms Robots [5] was used to make a PID regulated line follower program: PIDLineFollower.java. The program uses a modifed version of the BlackWhiteSensor.java to read and calibrate the sensor values: BWSensorModified.java.
After implementing PIDLineFollower.java there were some problems with the constants (Kp, Ki, Kd) and the robot turned around in circels and the turn value increased to well over 10000. After following the approach int the "Tuning A PID Controller Without Complex Math" [5] section, the Kp value was sat to a lower value. This made the robot follow the line but oscillate quite a bit.

After this we keept on tweaking the Kp value until we got a satisfiable result. Some of the other terms needed a lot of tweaking as well but we ran out of time at the lab session.

The result behavior of the robot can be seen in the following video.

Conclusion

During the assignment we succeded in making a robot with a mounted light sensor detect different colors and in making the robot follow a black line.
Completing the assignments 1 to 3 in the plan went well and was completed without any major glitches. Excercise 4, where we were supposed to make a wall following robot that stops when entering a blue area, was a bit more challenging. We choose to implement the program using only one sensor rather than the more trivial (in theory) implementation with two sensors, because we wanted to see if it could be done. It turned out that it could be done fairly easy. Our idea was to count measures where the robot measured in the blue range and ensure that the robot was in the blue area for some time to elliminate the short readings of blue between reading black and white. We implemented this idea with another if else clause that checks if the read value is in the range of the blue color.
This approach turned out to work fine, as soon as we determined the number of measures needed to be counted to ensure that the robot was in the blue area.

The last excercise proved a bit more troublesome. We had som problems in the beginning with the implementation of the approach descriped in [5] but after we followed the term tweaking part of it we got the robot to follow a line. We ran out of time before we got to tweak all the termes correctly so the behavior of the robot wasn't that pretty. Besides we only made the robot follow a straight line, if we have had more time or perhaps had begun tweaking the different terms earlier maybe we could have gotten a better result.

References

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License