Professional Documents
Culture Documents
/*
* Written by Ben Schmidel et al., October 4, 2010.
* Copyright (c) 2008-2012 Pololu Corporation. For more information, see
*
* http://www.pololu.com
* http://forum.pololu.com
* http://www.pololu.com/docs/0J19
*
* You may freely modify and share this code, as long as you keep this
* notice intact (including the two links above). Licensed under the
* Creative Commons BY-SA 3.0 license:
*
* http://creativecommons.org/licenses/by-sa/3.0/
*
* Disclaimer: To the extent permitted by law, Pololu provides this work
* without any warranty. It might be defective, in which case you agree
* to be responsible for all resulting costs and damages.
*/
#include <stdlib.h>
#include "QTRSensors.h"
#include <Arduino.h>
if (_pins == 0)
{
_pins = (unsigned char*)malloc(sizeof(unsigned char)*_numSensors);
if (_pins == 0)
return;
}
unsigned char i;
for (i = 0; i < _numSensors; i++)
{
_pins[i] = pins[i];
}
_emitterPin = emitterPin;
}
readPrivate(sensor_values);
emittersOff();
if(readMode == QTR_EMITTERS_ON_AND_OFF)
{
readPrivate(off_values);
for(i=0;i<_numSensors;i++)
{
sensor_values[i] += _maxValue - off_values[i];
}
}
}
// Turn the IR LEDs off and 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.
void QTRSensors::emittersOff()
{
if (_emitterPin == QTR_NO_EMITTER_PIN)
return;
pinMode(_emitterPin, OUTPUT);
digitalWrite(_emitterPin, LOW);
delayMicroseconds(200);
}
void QTRSensors::emittersOn()
{
if (_emitterPin == QTR_NO_EMITTER_PIN)
return;
pinMode(_emitterPin, OUTPUT);
digitalWrite(_emitterPin, HIGH);
delayMicroseconds(200);
}
for(i=0;i<_numSensors;i++)
(*calibratedMaximum)[i] = 0;
}
if(*calibratedMinimum == 0)
{
*calibratedMinimum = (unsigned int*)malloc(sizeof(unsigned
int)*_numSensors);
for(i=0;i<_numSensors;i++)
(*calibratedMinimum)[i] = _maxValue;
}
int j;
for(j=0;j<10;j++)
{
read(sensor_values,readMode);
for(i=0;i<_numSensors;i++)
{
// set the max we found THIS time
if(j == 0 || max_sensor_values[i] < sensor_values[i])
max_sensor_values[i] = sensor_values[i];
for(i=0;i<_numSensors;i++)
{
unsigned int calmin,calmax;
unsigned int denominator;
signed int x = 0;
if(denominator != 0)
x = (((signed long)sensor_values[i]) - calmin)
* 1000 / denominator;
if(x < 0)
x = 0;
else if(x > 1000)
x = 1000;
sensor_values[i] = x;
}
readCalibrated(sensor_values, readMode);
avg = 0;
sum = 0;
for(i=0;i<_numSensors;i++) {
int value = sensor_values[i];
if(white_line)
value = 1000-value;
if(!on_line)
{
// If it last read to the left of center, return 0.
if(_lastValue < (_numSensors-1)*1000/2)
return 0;
_lastValue = avg/sum;
return _lastValue;
}
// The array 'pins' contains the Arduino pin number for each sensor.
// 'emitterPin' is the Arduino pin that controls the IR LEDs on the 8RC
// modules. If you are using a 1RC (i.e. if there is no emitter pin),
// or if you just want the emitters on all the time and don't want to
// use an I/O pin to control it, use a value of 255 (QTR_NO_EMITTER_PIN).
void QTRSensorsRC::init(unsigned char* pins,
unsigned char numSensors, unsigned int timeout, unsigned char emitterPin)
{
QTRSensors::init(pins, numSensors, emitterPin);
_maxValue = timeout;
}
if (_pins == 0)
return;
// the array 'pins' contains the Arduino analog pin assignment for each
// sensor. For example, if pins is {0, 1, 7}, sensor 1 is on
// Arduino analog input 0, sensor 2 is on Arduino analog input 1,
// and sensor 3 is on Arduino analog input 7.
// 'emitterPin' is the Arduino pin that controls the IR LEDs on the 8RC
// modules. If you are using a 1RC (i.e. if there is no emitter pin),
// or if you just want the emitters on all the time and don't want to
// use an I/O pin to control it, use a value of 255 (QTR_NO_EMITTER_PIN).
void QTRSensorsAnalog::init(unsigned char* pins,
unsigned char numSensors, unsigned char numSamplesPerSensor,
unsigned char emitterPin)
{
QTRSensors::init(pins, numSensors, emitterPin);
_numSamplesPerSensor = numSamplesPerSensor;
_maxValue = 1023; // this is the maximum returned by the A/D conversion
}
if (_pins == 0)
return;