# #Robotics Programming a Self Calibrating Line Following Robot   It has been a while since I wrote an article for my robotics portal, so I thought I would write about a topic I have been mentoring a student recently.

This concept for this article is how to create a line following robot that is self calibrating. When a robot is self calibrating, the requirement for taking light readings and updating the code when changing venues or light conditions change is avoided.

The techniques in this article work best with the Proportional Two Sensor Line Follower as discussed in the #Robotics Programming a Line Following Robot – Part 2 article.

Another benefit of the self calibrating coding is that when you use two sensors you don’t have to worry if the sensors are not matched (ie. they don’t have read same or similar values for white and black).

The idea behind self calibrating is to track both the minimum and maximum values for each sensor, so that when you get a reading you can “normalise” the value. The normalised value uses mathematical ratios to convert the raw reading to a value between 0 and 100 where 0 is the minimum value (black) and 100 is the maximum value (white). The self calibrating part is that we continuously update the minimum and maximum values based on the readings returned.

This technique works regardless of whether the readings you get are in the range of 0 to 100 or something else. For example: when reading raw RGB values from a Lego EV3 Colour Sensor with EV3 Basic, the values can be in the range of 0 to 800 approximately.

Below are two examples with minimum, maximum and reading values, and we need to calculate the normalised values. To calculate the normalised value for a reading you need to shift the raw value to start from zero and then use ratios and multiplying by 100 to convert the value into a percentage.

Normalised Value = 100.0 * (Reading – Minimum) / (Maximum – Minimum)

For Example 1:

Normalised Value = 100.0 * (30 – 10) / (50 – 10) = 100.0 * 20 / 40 = 2000.0 / 40 = 50.0%

For Example 2:

Normalised Value = 100.0 * (200 – 50) / (650 – 50) = 100.0 * 150 / 600 = 15000.0 / 600 = 25.0%

Once you have the normalised values, you can use them to line follow as well as check for black or white. For example: Less than 25% is black and over 75% is white.

### Starting Maximum and Minimum Values

To set the initial values for minimum and maximum values, we can take readings from the sensors when the program is first started. Assuming the robot is started with the sensors over white, we can read the starting maximum value.

For the minimum value we can estimate a starting value by dividing the maximum value by a factor that can be based on approximate reading values for black and white.

The idea is get the starting minimum value close to but still higher than the actual black reading. It will be updated as discussed in the section below.

The code to set these starting values should be at the beginning of the program before the main program loop.

### Updating Maximum and Minimum Values

Now that we have starting values for the maximum and minimum values, we can make the code self calibrating by updating these values inside the main program loop before we perform the normalisation step (as per the formula shown above).

Basically, if you see a blacker black, change the minimum value and if you see a whiter white, change the maximum value. So…

• if the reading is more than the current maximum, set the maximum to the reading.
• If the reading is less than the current minimum, set the minimum to the reading.

This will allow the robot to automatically adjust to changing lighting conditions.

### Notes

Here are some other points to take into account:

• You need to track the Maximum, Minimum, Reading and Normalised values for each sensor. Name the variables you use to include Left or L and Right or R so you know which sensor the variables are for.
• Handling the Silver Strip: The concept above will update the maximum value whenever it sees a larger reading value. The reading for the silver strip used in Robocup Junior at the entrance to the spill area rescue zone is usually significantly higher than the value for white. Your code will need to check if the reading is significantly higher than the current maximum value and if it is skip the updating of the maximum value. This will stop the silver strip affecting the normalisation process.
• If you are using RGB readings, you can use this normalisation process for each of the 3 colours. For the line following you can choose a single colour, such as Red or Green to control line following while at the same time take the three normalised readings to look for colours. If you are seeing more green than red or blue, then you know you are on a green shortcut square.
• If you are using EV3 Basic or another language that stores the RGB readings as an array, you can process all the self calibration and normalisation code inside a For i = 0 to 2 loop, where i is 0 = Red, 1 = Green and 2 = Blue.
• If you pick the robot up, this will affect the minimum value as reading will be a blacker black. It would be best to restart the program each time you pick up the robot. Make sure that the sensors are over white when you start the program to get the initial maximum value correct.

For more information on robotics and the EV3 Basic extensions to Microsoft Small Basic, check out the following links:

Hope you find this information useful.

David

29-Jun-2021: Highlight the notes about handling the silver strip in Robocup Junior Australia Line Rescue.