Calculating error on line following robot (PID)

I am kind of new to control systems and I am currently making a line follower using PID loop. I am using 6 IR LED Photo-transistor pairs to detect the line but I am not sure how to calculate the error. I searched online and many people use error = set point - position(current), but I am struggling to figure out how to use the input from the 6 sensors to get a precise position.

PS- I tried the loop with digital sensor input and it worked (giving each case an error value) but I am planning to analog values of sensors.

The line width is approximately 1.7 cm and it is a white line on a black track. Also this project is for a competition, and I would really appreciate any advice on which micro controller to use (currently using STM32F401RE or Arduino Uno) or any other thing I can do to make the robot faster and better. Cheers!

• Can you provide more information on the line width and senors you want to use. Mar 17, 2018 at 22:47
• I am using either 6 or 5 TCRT5000 sensors which is basically an IR LED and a photo transistor combined together. Line is a white line on a black track. The approximate width of the line is 1.7 cm. I also have another question, does the distance between 2 sensors matter if it is in a reasonable range? Mar 17, 2018 at 23:29
• Can you also describe your sensor spatial dispersion wrt the line you try to follow ? Mar 19, 2018 at 12:32

I order for you to use a PID loop for line follow, you are going to need an error that is proportional to the distance you are from the line. That may not be possible with the sensors you are describing. The best you can do is when the light sensors are analog (their output is proportional the the amount of line they see and they have a beam width that is the same as the width of the line.

If that is the case, line the sensors up so they are spaced one beam width apart as follows:

L3-L2-L1-R1-R2-R3

Then error = 3*L3+2*L2+L1-R1-2*R2-3*R3

For more information and other techniques, here is an article on PID line following techniques with LEGO Mindstorm. Line follow is used a lot in FLL (First Lego League).

Follow-up: It looks like the sensor you are using (TCRT5000) can be used as an analog sensor. It is an active sensor so that will help with ambient light rejection. The algorithm described above for calculating error should work if you want to do PID control.

See the Application Note for the TCT5000 for more information on how to use the sensor. The section on 'Resolution' should help you set the sensor spacing. If possible, set the senor height so that the resolution is smaller then the line width; but, no so small that the line could fit within the "gap" between the sensors (ideal is no gap).