35 #include "QTRSensors.h"
41 void QTRSensors::init(
unsigned char *pins,
unsigned char numSensors,
42 unsigned char emitterPin)
44 calibratedMinimumOn=0;
45 calibratedMaximumOn=0;
46 calibratedMinimumOff=0;
47 calibratedMaximumOff=0;
51 if (numSensors > QTR_MAX_SENSORS)
52 _numSensors = QTR_MAX_SENSORS;
54 _numSensors = numSensors;
58 _pins = (
unsigned char*)malloc(
sizeof(
unsigned char)*_numSensors);
64 for (i = 0; i < _numSensors; i++)
69 _emitterPin = emitterPin;
81 void QTRSensors::read(
unsigned int *sensor_values,
unsigned char readMode)
83 unsigned int off_values[QTR_MAX_SENSORS];
86 if(readMode == QTR_EMITTERS_ON || readMode == QTR_EMITTERS_ON_AND_OFF)
91 readPrivate(sensor_values);
94 if(readMode == QTR_EMITTERS_ON_AND_OFF)
96 readPrivate(off_values);
98 for(i=0;i<_numSensors;i++)
100 sensor_values[i] += _maxValue - off_values[i];
110 void QTRSensors::emittersOff()
112 if (_emitterPin == QTR_NO_EMITTER_PIN)
114 pinMode(_emitterPin, OUTPUT);
115 digitalWrite(_emitterPin, LOW);
116 delayMicroseconds(200);
119 void QTRSensors::emittersOn()
121 if (_emitterPin == QTR_NO_EMITTER_PIN)
123 pinMode(_emitterPin, OUTPUT);
124 digitalWrite(_emitterPin, HIGH);
125 delayMicroseconds(200);
129 void QTRSensors::resetCalibration()
132 for(i=0;i<_numSensors;i++)
134 if(calibratedMinimumOn)
135 calibratedMinimumOn[i] = _maxValue;
136 if(calibratedMinimumOff)
137 calibratedMinimumOff[i] = _maxValue;
138 if(calibratedMaximumOn)
139 calibratedMaximumOn[i] = 0;
140 if(calibratedMaximumOff)
141 calibratedMaximumOff[i] = 0;
149 void QTRSensors::calibrate(
unsigned char readMode)
151 if(readMode == QTR_EMITTERS_ON_AND_OFF || readMode == QTR_EMITTERS_ON)
153 calibrateOnOrOff(&calibratedMinimumOn,
154 &calibratedMaximumOn,
159 if(readMode == QTR_EMITTERS_ON_AND_OFF || readMode == QTR_EMITTERS_OFF)
161 calibrateOnOrOff(&calibratedMinimumOff,
162 &calibratedMaximumOff,
167 void QTRSensors::calibrateOnOrOff(
unsigned int **calibratedMinimum,
168 unsigned int **calibratedMaximum,
169 unsigned char readMode)
172 unsigned int sensor_values[16];
173 unsigned int max_sensor_values[16];
174 unsigned int min_sensor_values[16];
177 if(*calibratedMaximum == 0)
179 *calibratedMaximum = (
unsigned int*)malloc(
sizeof(
unsigned int)*_numSensors);
182 if(*calibratedMaximum == 0)
188 for(i=0;i<_numSensors;i++)
189 (*calibratedMaximum)[i] = 0;
191 if(*calibratedMinimum == 0)
193 *calibratedMinimum = (
unsigned int*)malloc(
sizeof(
unsigned int)*_numSensors);
196 if(*calibratedMinimum == 0)
199 for(i=0;i<_numSensors;i++)
200 (*calibratedMinimum)[i] = _maxValue;
206 read(sensor_values,readMode);
207 for(i=0;i<_numSensors;i++)
210 if(j == 0 || max_sensor_values[i] < sensor_values[i])
211 max_sensor_values[i] = sensor_values[i];
214 if(j == 0 || min_sensor_values[i] > sensor_values[i])
215 min_sensor_values[i] = sensor_values[i];
220 for(i=0;i<_numSensors;i++)
222 if(min_sensor_values[i] > (*calibratedMaximum)[i])
223 (*calibratedMaximum)[i] = min_sensor_values[i];
224 if(max_sensor_values[i] < (*calibratedMinimum)[i])
225 (*calibratedMinimum)[i] = max_sensor_values[i];
235 void QTRSensors::readCalibrated(
unsigned int *sensor_values,
unsigned char readMode)
240 if(readMode == QTR_EMITTERS_ON_AND_OFF || readMode == QTR_EMITTERS_OFF)
241 if(!calibratedMinimumOff || !calibratedMaximumOff)
243 if(readMode == QTR_EMITTERS_ON_AND_OFF || readMode == QTR_EMITTERS_ON)
244 if(!calibratedMinimumOn || !calibratedMaximumOn)
248 read(sensor_values,readMode);
250 for(i=0;i<_numSensors;i++)
252 unsigned int calmin,calmax;
253 unsigned int denominator;
256 if(readMode == QTR_EMITTERS_ON)
258 calmax = calibratedMaximumOn[i];
259 calmin = calibratedMinimumOn[i];
261 else if(readMode == QTR_EMITTERS_OFF)
263 calmax = calibratedMaximumOff[i];
264 calmin = calibratedMinimumOff[i];
269 if(calibratedMinimumOff[i] < calibratedMinimumOn[i])
272 calmin = calibratedMinimumOn[i] + _maxValue - calibratedMinimumOff[i];
274 if(calibratedMaximumOff[i] < calibratedMaximumOn[i])
277 calmax = calibratedMaximumOn[i] + _maxValue - calibratedMaximumOff[i];
280 denominator = calmax - calmin;
284 x = (((
signed long)sensor_values[i]) - calmin)
285 * 1000 / denominator;
290 sensor_values[i] = x;
315 int QTRSensors::readLine(
unsigned int *sensor_values,
316 unsigned char readMode,
unsigned char white_line)
318 unsigned char i, on_line = 0;
323 readCalibrated(sensor_values, readMode);
328 for(i=0;i<_numSensors;i++) {
329 int value = sensor_values[i];
340 avg += (long)(value) * (i * 1000);
348 if(_lastValue < (_numSensors-1)*1000/2)
353 return (_numSensors-1)*1000;
357 _lastValue = avg/sum;
365 QTRSensorsRC::QTRSensorsRC()
367 calibratedMinimumOn = 0;
368 calibratedMaximumOn = 0;
369 calibratedMinimumOff = 0;
370 calibratedMaximumOff = 0;
374 QTRSensorsRC::QTRSensorsRC(
unsigned char* pins,
375 unsigned char numSensors,
unsigned int timeout,
unsigned char emitterPin)
377 calibratedMinimumOn = 0;
378 calibratedMaximumOn = 0;
379 calibratedMinimumOff = 0;
380 calibratedMaximumOff = 0;
383 init(pins, numSensors, timeout, emitterPin);
407 void QTRSensorsRC::init(
unsigned char* pins,
408 unsigned char numSensors,
unsigned int timeout,
unsigned char emitterPin)
410 QTRSensors::init(pins, numSensors, emitterPin);
424 void QTRSensorsRC::readPrivate(
unsigned int *sensor_values)
431 for(i = 0; i < _numSensors; i++)
433 sensor_values[i] = _maxValue;
434 digitalWrite(_pins[i], HIGH);
435 pinMode(_pins[i], OUTPUT);
438 delayMicroseconds(10);
440 for(i = 0; i < _numSensors; i++)
442 pinMode(_pins[i], INPUT);
443 digitalWrite(_pins[i], LOW);
446 unsigned long startTime = micros();
447 while (micros() - startTime < _maxValue)
449 unsigned int time = micros() - startTime;
450 for (i = 0; i < _numSensors; i++)
452 if (digitalRead(_pins[i]) == LOW && time < sensor_values[i])
453 sensor_values[i] = time;
461 QTRSensorsAnalog::QTRSensorsAnalog()
463 calibratedMinimumOn = 0;
464 calibratedMaximumOn = 0;
465 calibratedMinimumOff = 0;
466 calibratedMaximumOff = 0;
470 QTRSensorsAnalog::QTRSensorsAnalog(
unsigned char* pins,
471 unsigned char numSensors,
unsigned char numSamplesPerSensor,
472 unsigned char emitterPin)
474 calibratedMinimumOn = 0;
475 calibratedMaximumOn = 0;
476 calibratedMinimumOff = 0;
477 calibratedMaximumOff = 0;
480 init(pins, numSensors, numSamplesPerSensor, emitterPin);
507 void QTRSensorsAnalog::init(
unsigned char* pins,
508 unsigned char numSensors,
unsigned char numSamplesPerSensor,
509 unsigned char emitterPin)
511 QTRSensors::init(pins, numSensors, emitterPin);
513 _numSamplesPerSensor = numSamplesPerSensor;
526 void QTRSensorsAnalog::readPrivate(
unsigned int *sensor_values)
534 for(i = 0; i < _numSensors; i++)
535 sensor_values[i] = 0;
537 for (j = 0; j < _numSamplesPerSensor; j++)
539 for (i = 0; i < _numSensors; i++)
541 sensor_values[i] += analogRead(_pins[i]);
546 for (i = 0; i < _numSensors; i++)
547 sensor_values[i] = (sensor_values[i] + (_numSamplesPerSensor >> 1)) /
548 _numSamplesPerSensor;
552 QTRSensors::~QTRSensors()
556 if(calibratedMaximumOn)
557 free(calibratedMaximumOn);
558 if(calibratedMaximumOff)
559 free(calibratedMaximumOff);
560 if(calibratedMinimumOn)
561 free(calibratedMinimumOn);
562 if(calibratedMinimumOff)
563 free(calibratedMinimumOff);