16. QTR Reflectance Sensors

The PololuQTRSensors class and the C functions in this section provide an interface for using Pololu’s QTR reflectance sensors together with the Orangutan. The library provides access to the raw sensors values as well as to high level functions including calibration and line-tracking.

We recommend not using this part of the library directly on the 3pi. Instead, we have provided an initialization function and convenient access functions through the Pololu3pi class. See Section 19 for details.

This section of the library defines an object for each of the two QTR sensor types, with the PololuQTRSensorsAnalog class intended for use with QTR-xA sensors and the PololuQTRSensorsRC class intended for use with QTR-xRC sensors. This library takes care of the differences between the QTR-xA and QTR-xRC sensors internally, providing you with a common interface to both sensors. The only external difference is in the constructors. This is achieved by having both of these classes derive from the abstract base class PololuQTRSensors. This base class cannot be instantiated.

The PololuQTRSensorsAnalog and PololuQTRSensorsRC classes are the only classes in the Pololu AVR library that must be instantiated before they are used. This allows multiple QTR sensor arrays to be controlled independently as separate PololuQTRSensors objects. The multiple independent array support is not available within the C environment, but multiple arrays can still be configured as a single array, as long as the total number of sensors does not exceed 8.

For calibration, memory is allocated using the malloc() command. This conserves RAM: if all eight sensors are calibrated with the emitters both on an off, a total of 64 bytes would be dedicated to storing calibration values. However, for an application where only three sensors are used, and the emitters are always on during reads, only 6 bytes are required.

Note that the PololuQTRSensorsRC class uses Timer2 during sensor reads to time the sensor pulses, so it might not work with code that uses Timer2 for other purposes. Once the sensor read is complete, Timer2 is restored to its original state; there are no restrictions on its use between sensor reads. The PololuQTRSensorsAnalog class does not use Timer2 at all, and all of the PololuQTRSensors code is compatible with the other Pololu AVR libraries.

Note: This library is implemented differently for use on Arduinos. Please install the Arduino-specific version if you want to use QTR sensors with the Arduino.

For a higher level overview of this library and example programs that show how this library can be used, please see Section 3.k of the Pololu AVR C/C++ Library User’s Guide.

Reference

C++ and methods are shown in red.

C functions are shown in green.

void PololuQTRSensors::read(unsigned int *sensorValues, unsigned char readMode = QTR_EMITTERS_ON)

void qtr_read(unsigned int *sensorValues, unsigned char readMode)

Reads the raw sensor values into an array. There MUST be space for as many values as there were sensors specified in the constructor. The values returned are a measure of the reflectance in units that depend on the type of sensor being used, with higher values corresponding to lower reflectance (a black surface or a void). QTR-xA sensors will return a raw value between 0 and 1023. QTR-xRC sensors will return a raw value between 0 and the timeout argument provided in the constructor (which defaults to 4000). The units will be in Timer2 counts, where Timer2 is running at the CPU clock divided by 8 (i.e. 2 MHz on a 16 MHz processor, or 2.5 MHz on a 20 MHz processor).

The functions that read values from the sensors all take an argument readMode, which specifies the kind of read that will be performed. Several options are defined: QTR_EMITTERS_OFF specifies that the reading should be made without turning on the infrared (IR) emitters, in which case the reading represents ambient light levels near the sensor; QTR_EMITTERS_ON specifies that the emitters should be turned on for the reading, which results in a measure of reflectance; and QTR_EMITTERS_ON_AND_OFF specifies that a reading should be made in both the on and off states. The values returned when the QTR_EMITTERS_ON_AND_OFF option is used are given by on + max – off, where on is the reading with the emitters on, off is the reading with the emitters off, and max is the maximum sensor reading. This option can reduce the amount of interference from uneven ambient lighting. Note that emitter control will only work if you specify a valid emitter pin in the constructor.

Example usage:

unsigned int sensor_values[8];
sensors.read(sensor_values);

void PololuQTRSensors::emittersOn()

void qtr_emitters_on()

Turn the IR LEDs on. This is mainly for use by the read method, and calling these functions before or after the reading the sensors will have no effect on the readings, but you may wish to use these for testing purposes. This method will only do something if a valid emitter pin was specified in the constructor.

void PololuQTRSensors::emittersOff()

void qtr_emitters_off()

Turn the IR LEDs off. This is mainly for use by the read method, and calling these functions before or after the reading the sensors will have no effect on the readings, but you may wish to use these for testing purposes.

void PololuQTRSensors::calibrate(unsigned char readMode = QTR_EMITTERS_ON)

void qtr_calibrate(unsigned char readMode)

Reads the sensors for calibration. The sensor values are not returned; instead, the maximum and minimum values found over time are stored internally and used for the readCalibrated() method. You can access the calibration (i.e raw max and min sensor readings) through the public member pointers calibratedMinimumOn, calibratedMaximumOn, calibratedMinimumOff, and calibratedMaximumOff. Note that these pointers will point to arrays of length numSensors, as specified in the constructor, and they will only be allocated after calibrate() has been called. If you only calibrate with the emitters on, the calibration arrays that hold the off values will not be allocated.

void PololuQTRSensors::readCalibrated(unsigned int *sensorValues, unsigned char readMode = QTR_EMITTERS_ON)

void qtr_read_calibrated(unsigned int *sensorValues, unsigned char readMode)

Returns sensor readings calibrated to a value between 0 and 1000, where 0 corresponds to a reading that is less than or equal to the minimum value read by calibrate() and 1000 corresponds to a reading that is greater than or equal to the maximum value. Calibration values are stored separately for each sensor, so that differences in the sensors are accounted for automatically.

unsigned int PololuQTRSensors::readLine(unsigned int *sensorValues, unsigned char readMode = QTR_EMITTERS_ON, unsigned char whiteLine = 0)

unsigned int qtr_read_line(unsigned int *sensorValues, unsigned char readMode)

Operates the same as read calibrated, but with a feature designed for line following: this function returns an estimated position of the line. The estimate is made using a weighted average of the sensor indices multiplied by 1000, so that a return value of 0 indicates that the line is directly below sensor 0, a return value of 1000 indicates that the line is directly below sensor 1, 2000 indicates that it’s below sensor 2, etc. Intermediate values indicate that the line is between two sensors. The formula is:

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

As long as your sensors aren’t spaced too far apart relative to the line, this returned value is designed to be monotonic, which makes it great for use in closed-loop PID control. Additionally, this method remembers where it last saw the line, so if you ever lose the line to the left or the right, it’s line position will continue to indicate the direction you need to go to reacquire the line. For example, if sensor 4 is your rightmost sensor and you end up completely off the line to the left, this function will continue to return 4000.

By default, this function assumes a dark line (high values) surrounded by white (low values). If your line is light on black, set the optional second argument whiteLine to true. In this case, each sensor value will be replaced by the maximum possible value minus its actual value before the averaging.

unsigned int* PololuQTRSensors::calibratedMinimumOn

unsigned int* qtr_calibrated_minimum_on()

The calibrated minumum values measured for each sensor, with emitters on. The pointers are unallocated and set to 0 until calibrate() is called, and then allocated to exactly the size required. Depending on the readMode argument to calibrate(), only the On or Off values may be allocated, as required. This and the following variables are made public so that you can use them for your own calculations and do things like saving the values to EEPROM, performing sanity checking, etc. The calibration values are available through function calls from C.

unsigned int* PololuQTRSensors::calibratedMaximumOn

unsigned int* qtr_calibrated_maximum_on()

The calibrated maximum values measured for each sensor, with emitters on.

unsigned int* PololuQTRSensors::calibratedMinimumOff

unsigned int* qtr_calibrated_minimum_off()

The calibrated minimum values measured for each sensor, with emitters off.

unsigned int* PololuQTRSensors::calibratedMaximumOff

unsigned int* qtr_calibrated_maximum_off()

The calibrated maximum values measured for each sensor, with emitters off.

PololuQTRSensors::~PololuQTRSensors()

The destructor for the PololuQTRSensors class frees up memory allocated for the calibration arrays. This feature is not available in C.

PololuQTRSensorsRC::PololuQTRSensorsRC()

This constructor performs no initialization. If it is used, the user must call init() before using the methods in this class.

PololuQTRSensorsRC::PololuQTRSensorsRC(unsigned char* pins, unsigned char numSensors, unsigned int timeout = 4000, unsigned char emitterPin = 255);

This constructor just calls init(), below.

void PololuQTRSensorsRC::init(unsigned char* pins, unsigned char numSensors, unsigned int timeout = 4000, unsigned char emitterPin = 255)

void qtr_rc_init(unsigned char* pins, unsigned char numSensors, unsigned int timeout, unsigned char emitterPin)

Initializes a QTR-RC (digital) sensor array.

The array pins should contain the pin numbers for each sensor, defined using the IO_* keywords provided by the library. For example, if pins is {IO_D3, IO_D6, IO_C1}, sensor 0 is on PD3, sensor 1 is on PD6, and sensor 2 is on PC1.

For ATmegaxx8-based controllers, the pin numbers are Arduino-compatible so you can define the pins array using Arduino pin numbers. For example, if pins is {3, 6, 15}, sensor 0 is on digital pin 3 or PD3, sensor 1 is on digital pin 6 or PD6, and sensor 2 is on digital pin 15 or PC1 (Arduino analog input 1). Digital pins 0 – 7 correpsond to port D pins PD0 – PD7, respectively. Digital pins 8 – 13 correspond to port B pins PB0 – PB5. Digital pins 14 – 19 correspond to port C pins PC0 – PC5, which are referred to in the Arduino environment as analog inputs 0 – 5.

numSensors specifies the length of the ‘pins’ array (the number of QTR-RC sensors you are using). numSensors must be no greater than 16.

timeout specifies the length of time in Timer2 counts beyond which you consider the sensor reading completely black. That is to say, if the pulse length for a pin exceeds timeout, pulse timing will stop and the reading for that pin will be considered full black. It is recommended that you set timeout to be between 1000 and 3000 us, depending on factors like the height of your sensors and ambient lighting. This allows you to shorten the duration of a sensor-reading cycle while maintaining useful measurements of reflectance. On a 16 MHz microcontroller, you can convert Timer2 counts to microseconds by dividing by 2 (2000 us = 4000 Timer2 counts = timeout of 4000). On a 20 MHz microcontroller, you can convert Timer2 counts to microseconds by dividing by 2.5 or multiplying by 0.4 (2000 us = 5000 Timer2 counts = timeout of 5000).

emitterPin is the Arduino digital pin that controls whether the IR LEDs are on or off. This pin is optional and only exists on the 8A and 8RC QTR sensor arrays. If a valid pin is specified, the emitters will only be turned on during a reading. If an invalid pin is specified (e.g. 255), the IR emitters will always be on.

PololuQTRSensorsAnalog::PololuQTRSensorsAnalog()

This constructor performs no initialization. If this constructor is used, the user must call init() before using the methods in this class.

PololuQTRSensorsAnalog::PololuQTRSensorsAnalog(unsigned char* analogPins, unsigned char numSensors, unsigned char numSamplesPerSensor = 4, unsigned char emitterPin = 255)

This constructor just calls init(), below.

void PololuQTRSensorsAnalog::init(unsigned char* analogPins, unsigned char numSensors, unsigned char numSamplesPerSensor = 4, unsigned char emitterPin = 255)

void qtr_analog_init(unsigned char* analogPins, unsigned char numSensors, unsigned char numSamplesPerSensor, unsigned char emitterPin)

Initializes a QTR-A (analog) sensor array.

The array pins contains the analog pin assignment for each sensor. For example, if pins is {0, 1, 7}, sensor 1 is on analog input 0, sensor 2 is on analog input 1, and sensor 3 is on analog input 7. The ATmegaxx8 has 8 total analog input channels (ADC0 – ADC7) that correspond to port C pins PC0 – PC5 and dedicated analog inputs ADC6 and ADC7. The Orangutan SVP and X2 also have 8 analog input channels (ADC0 – ADC7) but they correspond to port A pins PA0 – PA7. The analog channels provided by the Orangutan SVP’s auxiliary processor (channels A, B, C, and D) are not supported by this library.

numSensors specifies the length of the analogPins array (the number of QTR-A sensors you are using). numSensors must be no greater than 8.

numSamplesPerSensor indicates the number of 10-bit analog samples to average per channel (per sensor) for each reading. The total number of analog-to-digital conversions performed will be equal to numSensors times numSamplesPerSensor. Increasing this parameter increases noise suppression at the cost of sample rate. Recommended value: 4.

emitterPin is the digital pin (see qtr_rc_init(), above) that controls whether the IR LEDs are on or off. This pin is optional and only exists on the 8A and 8RC QTR sensor arrays. If a valid pin is specified, the emitters will only be turned on during a reading. If an invalid pin is specified (e.g. 255), the IR emitters will always be on.