LibPololu Question

Hi, I’ve been looking through the QRT sensor files and found something I don’t understand. It’s in the file “PololuQTRSensors.cpp”. This is the function:

void PololuQTRSensors::read(unsigned int *sensor_values, unsigned char readMode)
{
	unsigned int off_values[8];
	unsigned char i;
	
	if(readMode == QTR_EMITTERS_ON || readMode == QTR_EMITTERS_ON_AND_OFF)
		emittersOn();

	if (_type == QTR_RC)
	{
		((PololuQTRSensorsRC*)this)->readPrivate(sensor_values);
		emittersOff();
		if(readMode == QTR_EMITTERS_ON_AND_OFF)
			((PololuQTRSensorsRC*)this)->readPrivate(off_values);
	}
	else
	{
		((PololuQTRSensorsAnalog*)this)->readPrivate(sensor_values);
		emittersOff();
		if(readMode == QTR_EMITTERS_ON_AND_OFF)
			((PololuQTRSensorsRC*)this)->readPrivate(off_values);
	}

	if(readMode == QTR_EMITTERS_ON_AND_OFF)
	{
		for(i=0;i<_numSensors;i++)
		{
			sensor_values[i] += _maxValue - off_values[i];
		}
	}
}

For a non-RC sensor, we are turning on the emitters and taking an analog reading then turning off the emitters (if read_mode is on_and_off) and taking an RC reading of the sensors. Why is this??

Also, why do we calculate the sensor value that way? Sensor = Sensor + ( 1023 - off_value ). We are inversing the off_value and adding it to the on_value? Why not just subtract the the off_value from the on_value?

Denis.

Hello Denis,
It looks like you have identified a bug in the library. We will update the code to

      if(readMode == QTR_EMITTERS_ON_AND_OFF)
         ((PololuQTRSensorsAnalog*)this)->readPrivate(off_values);

in the next release of the library. For now, I recommend changing your own copy, recompiling, and running “make install”, if you want to use QTR-xA sensors with the ON_AND_OFF mode.

To answer your second question, the reason we do not simply subtract one value from the other is that the variables used for returning the sensor values are all of type “unsigned int”, so we can’t let them ever go negative.

Thanks for the note, and please let us know if you have any more questions!
-Paul