Black White Detection
The sensor is calibrated with regards to a black and a white value, which is used to determine the difference between black and white afterwards.
Black is typically calibrated to a value of 39 and white to a value of 60 in the environment we did the testing in.
Line Follower with Calibration
To make the car move more smoothly, we implemented proportional regulation of the speed on the two wheels. This means that we can regulate the power between the wheels instead of giving all the power to one wheel.
We ustilize the fact that the senser will not return either 39 or 60, as measured in the first test. It will return values from 39 to 60, and when it is excatly aligned with the line, it will return a value around 50. So we need to make the car go more to the left when the value is above 50 and more to the right, when it is below 50, and the closer it is to the outer values, the faster it should turn.
Our implementation:
int lightValue = sensor.light();
int error = lightValue - offset; float turn = kp * error; float powerA = tp + turn; float powerC = tp - turn; Car.forward((int)powerA, (int)powerC); Thread.sleep(10);
We have implemented the pseudocode from [PID], and tweak it to work with our robot.
Video of the new car can be found here.
ColorSensor with Calibration
The scheme from the BlackWhiteDetection-class is modified in such a way, that green know also is to be calibrated before the line follower begins to run. However it is important to notice that green is position right in between black and white value-wise. This means that it is not possible to use the same strategy, as for black and white, in this case. The black and white strategy checks for whether or not the given light sensor value is above or under a black and white threshold.
This black and white strategy is still used, but it is however combined with a new strategy for determining the color green. To detect green the program is simply calibrated to the color green, and if a value is the same as the calibrated value or of by a interval x, then it is the color green.
public boolean green() {
int interval = 1;
if(ls.readValue() == greenLightValue ||
ls.readValue() == greenLightValue+interval ||
ls.readValue() == greenLightValue-interval) {
return true;
}
return false;
}
To test the program we simply modified the line follower robot from above to stop in a "green zone", as described in the next section. 5
Line Follower that stops in a Goal Zone
It is in many ways a simple task to make a robot stop in a goal zone of the color green, since we have function for determining how the color green looks it is simply a matter of stopping the robot when it detects this color.
However as mentioned above the color green is very close to both black and white, which means the line follower can risk stopping on detecting one of these colors. To solve this problem we choose to create a counter which on 40 detections in a row of the color green would halt.
if(sensor.green()) {
i++;
if(i>=40) {
return;
}
} else {
i = 0;
}