# Line following sensor calibration

hello !
first off all, ı’m sorry for my bad english

I want to make a robot following white line on a black surface. I use six of QTRx8A
sensors and arduino atmega328
pololu.com/docs/0J19/3 , i read this and functions are written. when I load program my arduino, my robot can follow line very well.
In arduino I used QTR-8a library and its codes. now I want to make a new robot using pic18f2550. but I have some problems :

1- I don’t know how I calibrate the sensors .
I can read sensors as analog and find maximum and minumum values for each sensor. but I don’t know how i find CALIBRATEDMAX AND CALIBRATEDMIN.

2- pololu.com/docs/0J19/3 in this document there is a formula for finding position

0*value0 + 1000*value1 + 2000*value2 + ...
--------------------------------------------
value0  +  value1  +  value2 + ...

to test this formula I looked at calibrated sensor values using arduino(serial monitor) , and used these values in this formula but I found difference results. my result and result on serial monitor (position) is not equal.

calibrated_max: 899 877 870 879 863 913
calibrated_min : 53 55 46 44 39 47
reading_sensor: 885 855 430 38 43 838
calibrated_sensor : 983 973 464 0 4 913
position =3243 when i calculate the position it is different

so i want to know whether the value(value0, value1…)in the formula is “calibrated_sensor value” !

my codes

#include <PololuQTRSensors.h>
#define NUM_SENSORS             6
#define NUM_SAMPLES_PER_SENSOR  6
#define EMITTER_PIN             12

PololuQTRSensorsAnalog qtra((unsigned char[]) {0, 1, 2, 3, 4, 5},
NUM_SENSORS, NUM_SAMPLES_PER_SENSOR, EMITTER_PIN);
unsigned int sensorValues[NUM_SENSORS];

void setup()
{
delay(500);
int i;
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
for (i = 0; i < 400; i++)
{
qtra.calibrate();
}
digitalWrite(13, LOW);

Serial.begin(9600);
for (i = 0; i < NUM_SENSORS; i++)
{
Serial.print(qtra.calibratedMinimumOn[i]);
Serial.print(' ');
}
Serial.println();

for (i = 0; i < NUM_SENSORS; i++)
{
Serial.print(qtra.calibratedMaximumOn[i]);
Serial.print(' ');
}
Serial.println();
Serial.println();
delay(1000);
}

void loop()
{
int i;
unsigned int position = qtra.readLine(sensorValues, QTR_EMITTERS_ON,1);

for (i = 0; i < NUM_SENSORS; i++)
{
Serial.print(sensorValues[i]);
Serial.print(' ');
}
Serial.println();

for (i = 0; i < NUM_SENSORS; i++)
{
Serial.print(sensorValues[i]);
Serial.print(' ');
}
Serial.println();

Serial.println(position);

delay(250);
}

Hello,

When I try it with your numbers, I get about 3200, consistent with your position value. You did not show your calculation or result, but I bet you are forgetting to invert the values (1000-x) because you are using the white line argument to readLine().

-Paul

my calculation was 1943. as you say i forgot to invert the values (1000-x) . but what is x ? x is raw sensor value or calibrated sensor value ?

1000*value1 + 2000*value2 + ...
--------------------------------------------
value0 + value1 + value2 + ...

in this formula i use calibrated sensor value as “value” . and i get 1943 .

You need to use 1000 minus the calibrated sensor value.

-Paul

Ok.
I get the right position value now . but sometimes there is some difference between the value which i calculate and the value which I read using OTR library.
my another proplem is how I find “calibrated maximum " and " calibrated minumum”. I can find maximums and minumums for each sensor.I think I must calibrate these values between 0 and 1000 . sometimes one of the maximum values i get is about 1012, 1020 etc…and what is calibration logic ?

Hello,

Those variables refer to the minimum and maximum raw values of the sensors, before adjusting for the calibration. So there is no problem with values over 1000. The values returned by readCalibrated, however, should always be between 0 and 1000.

-Paul

yes I know that the values returned by readCalibrated. I want to use my own codes for calibration. I want to ask how I calibrate these maximums and minumums using my own codes and not using readCalibrated . because I want to use PIC18f2550.

Hello,

Do you know that you can look at the source code yourself? Here is the part of readCalibrated that converts to calibrated values:

denominator = calmax - calmin;

signed int x = 0;
if(denominator != 0)
x = (((signed long)sensor_values[i]) - calmin)
* 1000 / denominator;
if(x < 0)
x = 0;
else if(x > 1000)
x = 1000;

-Paul