# 3.k. Pololu QTR Sensor Functions

#### Overview

This set of functions provides access to the QTR family of reflectance sensors, which come as single-sensor units (QTR-1A and QTR-1RC) or as 8-sensor arrays (QTR-8A and QTR-8RC). To initialize the set of sensors that you are using, choose either the qtr_analog_init() or qtr_rc_init() function, and specify the set of pins connected to the sensors that you will be using. The initialization may only be called once within the C environment, while C++ allows the sensors to be used in more complicated ways.

These functions are used by the 3pi support described in the 3pi Robot User’s Guide. We do not recommend using these functions directly on the 3pi unless you are adding additional sensors.

C++ and Arduino users: See Section 3 of Arduino Library for the Pololu QTR Reflectance Sensors for examples of this class in the Arduino environment, which is almost identical to C++. Please note that the Arduino version of this library is implemented differently from the Pololu AVR Library version, so make sure you download the version appropriate for your platform.

Complete documentation of these functions can be found in Section 16 of the Pololu AVR Library Command Reference.

#### Usage Notes

##### Calibration

This library allows you to use the calibrate() method to easily calibrate your sensors for the particular conditions it will encounter. Calibrating your sensors can lead to substantially more reliable sensor readings, which in turn can help simplify your code. As such, we recommend you build a calibration phase into your application’s initialization routine. This can be as simple as a fixed duration over which you repeated call the calibrate() method. During this calibration phase, you will need to expose each of your reflectance sensors to the lightest and darkest readings they will encounter. For example, if you have made a line follower, you will want to slide it across the line during the calibration phase so the each sensor can get a reading of how dark the line is and how light the ground is. A sample calibration routine would be:

#include <pololu/orangutan.h>

int main()
{
unsigned char qtr_rc_pins[] = {IO_C0, IO_C1, IO_C2};
qtr_rc_init(qtr_rc_pins, 3, 2000, 255);  // 800 us timeout, no emitter pin
// int qtr_analog_pins[] = {0, 1, 2};
// qtr_analog_init(qtr_analog_pins, 3, 10, IO_C0); // 10 samples, emitter pin is PC0

// optional: wait for some input from the user, such as  a button press

// then start calibration phase and move the sensors over both
// reflectance extremes they will encounter in your application:
int i;
for (i = 0; i < 250; i++)  // make the calibration take about 5 seconds
{
qtr_calibrate(QTR_EMITTERS_ON);
delay(20);
}

// optional: signal that the calibration phase is now over and wait for further
// input from the user, such as a button press

while (1)
{
// main body of program goes here
}

return 0;
}

This library gives you a number of different ways to read the sensors.

1. You can request raw sensor values using the read() method, which takes an optional argument that lets you perform the read with the IR emitters turned off (note that turning the emitters off is only supported by the QTR-8x reflectance sensor arrays).
2. You can request calibrated sensor values using the qtr_read_calibrated() function, which also takes an optional argument that lets you perform the read with the IR emitters turned off. Calibrated sensor values will always range from 0 to 1000, with 0 being as or more reflective (i.e. whiter) than the most reflective surface encountered during calibration, and 1000 being as or less reflective (i.e. blacker) than the least reflective surface encountered during calibration.
3. For line-detection applications, you can request the line location using the qtr_read_line() functions, which takes as optional parameters a boolean that indicates whether the line is white on a black background or black on a white background, and a boolean that indicates whether the IR emitters should be on or off during the measurement. qtr_read_line() provides calibrated values for each sensor and returns an integer that tells you where it thinks the line is. If you are using N sensors, a returned value of 0 means it thinks the line is on or to the outside of sensor 0, and a returned value of 1000 * (N-1) means it thinks the line is on or to the outside of sensor N-1. As you slide your sensors across the line, the line position will change monotonically from 0 to 1000 * (N-1), or vice versa. This line-position value can be used for closed-loop PID control.

A sample routine to obtain the sensor values and perform rudimentary line following would be:

void loop()  // call this routine repeatedly from your main program
{
unsigned int sensors[3];
// get calibrated sensor values returned in the sensors array,
// along with the line position.
// position will range from 0 to 2000, with 1000 corresponding
// to the line over the middle sensor

// if all three sensors see very low reflectance, take some
// appropriate action for this situation
if (sensors[0] > 750 && sensors[1] > 750 && sensors[2] > 750)
{
// do something.  Maybe this means we're at the edge of a
// course or about to fall off a table, in which case, we might
// want to stop moving, back up, and turn around.
return;
}

// compute our "error" from the line position.  We will make it so that the
// error is zero when the middle sensor is over the line, because this is
// our goal.  Error will range from -1000 to +1000.  If we have sensor 0 on
// the left and sensor 2 on the right,  a reading of -1000 means that we
// see the line on the left and a reading of +1000 means we see the line
// on the right.
int error = position - 1000;

int leftMotorSpeed = 100;
int rightMotorSpeed = 100;
if (error < -500)  // the line is on the left
leftMotorSpeed = 0;  // turn left
if (error > 500)  // the line is on the right
rightMotorSpeed = 0;  // turn right

// set motor speeds using the two motor speed variables above
}
##### PID Control

The integer value returned by qtr_read_line() can be easily converted into a measure of your position error for line-following applications, as was demonstrated in the previous code sample. The function used to generate this position/error value is designed to be monotonic, which means the value will almost always change in the same direction as you sweep your sensors across the line. This makes it a great quantity to use for PID control.

Explaining the nature of PID control is beyond the scope of this document, but Wikipedia has a very good article on the subject.

The following code gives a very simple example of PD control (I find the integral PID term is usually not necessary when it comes to line following). The specific nature of the constants will be determined by your particular application, but you should note that the derivative constant Kd is usually much bigger than the proportional constant Kp. This is because the derivative of the error is a much smaller quantity than the error itself, so in order to produce a meaningful correction it needs to be multiplied by a much larger constant.

int lastError = 0;

void loop()  // call this routine repeatedly from your main program
{
unsigned int sensors[3];
// get calibrated sensor values returned in the sensors array,
// along with the line position.
// position will range from 0 to 2000, with 1000 corresponding
// to the line over the middle sensor

// compute our "error" from the line position.  We will make it so
// that the error is zero when the middle sensor is over the line,
// because this is our goal.  Error will range from -1000 to +1000.
// If we have sensor 0 on the left and sensor 2 on the right,  a
// reading of -1000 means that we see the line on the left and a
// reading of +1000 means we see the line on the right.
int error = position - 1000;

// set the motor speed based on proportional and derivative PID terms
// KP is the a floating-point proportional constant (maybe start with a value around 0.1)
// KD is the floating-point derivative constant (maybe start with a value around 5)
// note that when doing PID, it's very important you get your signs right, or else the
// control loop will be unstable
int motorSpeed = KP * error + KD * (error - lastError);
lastError = error;

// M1 and M2 are base motor speeds.  That is to say, they are the
// speeds the motors should spin at if you are perfectly on the line
// with no error.  If your motors are well matched, M1 and M2 will be
// equal.  When you start testing your PID loop, it might help to start
// with small values for M1 and M2.  You can then increase the speed
// as you fine-tune your PID constants KP and KD.
int m1Speed = M1 + motorSpeed;
int m2Speed = M2 - motorSpeed;

// it might help to keep the speeds positive (this is optional)
// note that you might want to add a similiar line to keep the speeds from exceeding
// any maximum allowed value
if (m1Speed < 0)
m1Speed = 0;
if (m2Speed < 0)
m2Speed = 0;

// set motor speeds using the two motor speed variables above
}
(702) 262-6648
Same-day shipping, worldwide