35 #include "QTRSensors.h"
41 void QTRSensors::init(
unsigned char *pins,
unsigned char numSensors,
42 unsigned char emitterPin)
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];
112 if (_emitterPin == QTR_NO_EMITTER_PIN)
114 pinMode(_emitterPin, OUTPUT);
115 digitalWrite(_emitterPin, LOW);
116 delayMicroseconds(200);
121 if (_emitterPin == QTR_NO_EMITTER_PIN)
123 pinMode(_emitterPin, OUTPUT);
124 digitalWrite(_emitterPin, HIGH);
125 delayMicroseconds(200);
132 for(i=0;i<_numSensors;i++)
151 if(readMode == QTR_EMITTERS_ON_AND_OFF || readMode == QTR_EMITTERS_ON)
159 if(readMode == QTR_EMITTERS_ON_AND_OFF || readMode == QTR_EMITTERS_OFF)
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];
240 if(readMode == QTR_EMITTERS_ON_AND_OFF || readMode == QTR_EMITTERS_OFF)
243 if(readMode == QTR_EMITTERS_ON_AND_OFF || readMode == QTR_EMITTERS_ON)
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)
261 else if(readMode == QTR_EMITTERS_OFF)
280 denominator = calmax - calmin;
284 x = (((
signed long)sensor_values[i]) - calmin)
285 * 1000 / denominator;
290 sensor_values[i] = x;
316 unsigned char readMode,
unsigned char white_line)
318 unsigned char i, on_line = 0;
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()
374 QTRSensorsRC::QTRSensorsRC(
unsigned char* pins,
375 unsigned char numSensors,
unsigned int timeout,
unsigned char emitterPin)
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()
470 QTRSensorsAnalog::QTRSensorsAnalog(
unsigned char* pins,
471 unsigned char numSensors,
unsigned char numSamplesPerSensor,
472 unsigned char emitterPin)
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()