10 if (timeout > 32767) { timeout = 32767; }
49 if (!calibration.initialized)
51 uint16_t * oldMaximum = calibration.maximum;
52 calibration.maximum = (uint16_t *)realloc(calibration.maximum,
54 if (calibration.maximum ==
nullptr)
61 uint16_t * oldMinimum = calibration.minimum;
62 calibration.minimum = (uint16_t *)realloc(calibration.minimum,
64 if (calibration.minimum ==
nullptr)
75 calibration.maximum[i] = 0;
76 calibration.minimum[i] = _maxValue;
79 calibration.initialized =
true;
82 for (uint8_t j = 0; j < 10; j++)
84 read(sensorValues, mode);
89 if ((j == 0) || (sensorValues[i] > maxSensorValues[i]))
91 maxSensorValues[i] = sensorValues[i];
95 if ((j == 0) || (sensorValues[i] < minSensorValues[i]))
97 minSensorValues[i] = sensorValues[i];
107 if (minSensorValues[i] > calibration.maximum[i])
109 calibration.maximum[i] = minSensorValues[i];
114 if (maxSensorValues[i] < calibration.minimum[i])
116 calibration.minimum[i] = maxSensorValues[i];
127 readPrivate(sensorValues);
131 readPrivate(sensorValues);
136 readPrivate(sensorValues);
169 read(sensorValues, mode);
173 uint16_t calmin, calmax;
187 uint16_t denominator = calmax - calmin;
190 if (denominator != 0)
192 value = (((int32_t)sensorValues[i]) - calmin) * 1000 / denominator;
195 if (value < 0) { value = 0; }
196 else if (value > 1000) { value = 1000; }
198 sensorValues[i] = value;
216 uint16_t value = sensorValues[i];
217 if (invertReadings) { value = 1000 - value; }
220 if (value > 200) { onLine =
true; }
225 avg += (uint32_t)value * (i * 1000);
244 _lastPosition = avg / sum;
245 return _lastPosition;
249 LineSensors::~LineSensors()
257 void LineSensors::readPrivate(uint16_t * sensorValues)
259 FastGPIO::Pin<line0Pin>::setOutputHigh();
260 FastGPIO::Pin<line1Pin>::setOutputHigh();
261 FastGPIO::Pin<line2Pin>::setOutputHigh();
262 FastGPIO::Pin<line3Pin>::setOutputHigh();
263 FastGPIO::Pin<line4Pin>::setOutputHigh();
266 sensorValues[0] = _timeout;
267 sensorValues[1] = _timeout;
268 sensorValues[2] = _timeout;
269 sensorValues[3] = _timeout;
270 sensorValues[4] = _timeout;
273 uint16_t startTime = micros();
274 FastGPIO::Pin<line0Pin>::setInput();
275 FastGPIO::Pin<line1Pin>::setInput();
276 FastGPIO::Pin<line2Pin>::setInput();
277 FastGPIO::Pin<line3Pin>::setInput();
278 FastGPIO::Pin<line4Pin>::setInput();
285 time = micros() - startTime;
286 if (time >= _timeout)
291 if (!FastGPIO::Pin<line0Pin>::isInputHigh() && time < sensorValues[0]) { sensorValues[0] = time; }
292 if (!FastGPIO::Pin<line1Pin>::isInputHigh() && time < sensorValues[1]) { sensorValues[1] = time; }
293 if (!FastGPIO::Pin<line2Pin>::isInputHigh() && time < sensorValues[2]) { sensorValues[2] = time; }
294 if (!FastGPIO::Pin<line3Pin>::isInputHigh() && time < sensorValues[3]) { sensorValues[3] = time; }
295 if (!FastGPIO::Pin<line4Pin>::isInputHigh() && time < sensorValues[4]) { sensorValues[4] = time; }
297 __builtin_avr_delay_cycles(4);
void emittersOff()
Turns the IR LEDs off.
CalibrationData calibrationOn
Data from calibrating with emitters on.
void resetCalibration()
Resets all calibration that has been done.
void readCalibrated(uint16_t *sensorValues, LineSensorsReadMode mode=LineSensorsReadMode::On)
Reads the sensors and provides calibrated values between 0 and 1000.
static const uint8_t _sensorCount
The 3pi+ 32U4 has 5 line sensors.
void read(uint16_t *sensorValues, LineSensorsReadMode mode=LineSensorsReadMode::On)
Reads the raw sensor values into an array.
void emittersOn()
Turns the IR LEDs on.
CalibrationData calibrationOff
Data from calibrating with emitters off.
void setTimeout(uint16_t timeout)
Sets the timeout for RC sensors.
void calibrate(LineSensorsReadMode mode=LineSensorsReadMode::On)
Reads the sensors for calibration.
Top-level namespace for the Pololu3piPlus32U4 library.
LineSensorsReadMode
Emitter behavior when taking readings.
uint16_t * maximum
Highest readings seen during calibration.
bool initialized
Whether array pointers have been allocated and initialized.
uint16_t * minimum
Lowest readings seen during calibration.