Pololu Robotics & Electronics
Menu
My account Comments or questions? About Pololu Contact Ordering information Distributors

Pololu Forum

Feed calibration values without actually calibrating QTR sensors

There is a similar question on this forum, yet it is not clearly answered. I want to speed up my development process by not needing to calibrate my line-following robot every time I upload my code to test something. Thus, I would like to feed calibration values without calling the calibrate function, for it to be instant, is this possible? And if so, how is it done? I don’t think it’s the matter of the topic, but just for the record, I am dealing with QTR-8RC sensor.

I created this function inside the QTR library and it works fine:

void QTRSensors::virtualCalibrate(uint16_t *maxSensorValues, uint16_t *minSensorValues, QTRReadMode mode = QTRReadMode::On) {
	// (Re)allocate and initialize the arrays if necessary.
	if (!calibrationOn.initialized) {
		uint16_t *oldMaximum = calibrationOn.maximum;
		calibrationOn.maximum = (uint16_t*) realloc(calibrationOn.maximum, sizeof(uint16_t) * _sensorCount);
		if (calibrationOn.maximum == nullptr) {
			// Memory allocation failed; don't continue.
			free(oldMaximum); // deallocate any memory used by old array
			return;
		}

		uint16_t *oldMinimum = calibrationOn.minimum;
		calibrationOn.minimum = (uint16_t*) realloc(calibrationOn.minimum, sizeof(uint16_t) * _sensorCount);
		if (calibrationOn.minimum == nullptr) {
			// Memory allocation failed; don't continue.
			free(oldMinimum); // deallocate any memory used by old array
			return;
		}

		// Initialize the max and min calibrated values to values that
		// will cause the first reading to update them.
		for (uint8_t i = 0; i < _sensorCount; i++) {
			calibrationOn.maximum[i] = 0;
			calibrationOn.minimum[i] = _maxValue;
		}

		calibrationOn.initialized = true;
	}

	// record the min and max calibration values
	for (uint8_t i = 0; i < _sensorCount; i++) {
		if (calibrationOn.maximum[i] < maxSensorValues[i]) {
			calibrationOn.maximum[i] = maxSensorValues[i];
		}

		if (calibrationOn.minimum[i] > minSensorValues[i]) {
			calibrationOn.minimum[i] = minSensorValues[i];
		}
	}
}

Although I would like to be able to do this outside the library (without changing the library’s code), how can this be easily done? And if it is not easily doable, is my solution sufficient enough?

Hello.

It sounds like you want to load your calibration values into the QTRSensors library without having to modify it. Is that correct? If so, probably the easiest way is to call calibrate() once to ensure that the calibration data arrays are allocated for calibrationOn and calibrationOff. Then, since those arrays are public, you can write your calibration values to those variables from the sketch (e.g. qtr.calibrationOn.minimum[i] = value).

- Amanda