Pololu3piPlus32U4 library
Pololu3piPlus32U4LineSensors.h
Go to the documentation of this file.
1 // Copyright (C) Pololu Corporation. See www.pololu.com for details.
2 
4 
5 #pragma once
6 
7 #include <Arduino.h>
8 #include <FastGPIO.h>
9 
10 namespace Pololu3piPlus32U4
11 {
12 
14 enum class LineSensorsReadMode : uint8_t {
17  Off,
18 
21  On,
22 
27  Manual
28 };
29 
37 {
38 public:
40  static const uint8_t _sensorCount = 5;
41 
43  static const uint16_t defaultTimeout = 4000;
44 
45  static const uint8_t emitterPin = 11;
46  static const uint8_t line0Pin = 12;
47  static const uint8_t line1Pin = A0;
48  static const uint8_t line2Pin = A2;
49  static const uint8_t line3Pin = A3;
50  static const uint8_t line4Pin = A4;
51 
52  ~LineSensors();
53 
68  void setTimeout(uint16_t timeout);
69 
75  uint16_t getTimeout() { return _timeout; }
76 
104 
106  void resetCalibration();
107 
137  void read(uint16_t * sensorValues, LineSensorsReadMode mode = LineSensorsReadMode::On);
138 
159  void readCalibrated(uint16_t * sensorValues, LineSensorsReadMode mode = LineSensorsReadMode::On);
160 
204  uint16_t readLineBlack(uint16_t * sensorValues, LineSensorsReadMode mode = LineSensorsReadMode::On)
205  {
206  return readLinePrivate(sensorValues, mode, false);
207  }
208 
229  uint16_t readLineWhite(uint16_t * sensorValues, LineSensorsReadMode mode = LineSensorsReadMode::On)
230  {
231  return readLinePrivate(sensorValues, mode, true);
232  }
233 
234 
239  {
241  bool initialized = false;
243  uint16_t * minimum = nullptr;
245  uint16_t * maximum = nullptr;
246  };
247 
256 
259 
262 
264 
266  void emittersOn()
267  {
268  FastGPIO::Pin<emitterPin>::setOutputHigh();
269  }
270 
272  void emittersOff()
273  {
274  FastGPIO::Pin<emitterPin>::setInput();
275  }
276 
277 private:
278 
279  // Handles the actual calibration, including (re)allocating and
280  // initializing the storage for the calibration values if necessary.
281  void calibrateOnOrOff(CalibrationData & calibration, LineSensorsReadMode mode);
282 
283  void readPrivate(uint16_t * sensorValues);
284 
285  uint16_t readLinePrivate(uint16_t * sensorValues, LineSensorsReadMode mode, bool invertReadings);
286 
287  uint16_t _timeout = defaultTimeout;
288  uint16_t _maxValue = defaultTimeout; // the maximum value returned by readPrivate()
289  uint16_t _lastPosition = 0;
290 };
291 
292 }
Gets readings from the five reflectance sensors on the bottom of the 3pi+ 32U4.
static const uint16_t defaultTimeout
Default timeout for RC sensors (in microseconds).
void emittersOff()
Turns the IR LEDs off.
CalibrationData calibrationOn
Data from calibrating with emitters on.
uint16_t readLineWhite(uint16_t *sensorValues, LineSensorsReadMode mode=LineSensorsReadMode::On)
Reads the sensors, provides calibrated values, and returns an estimated white line position.
uint16_t getTimeout()
Returns the timeout.
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.
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.
uint16_t readLineBlack(uint16_t *sensorValues, LineSensorsReadMode mode=LineSensorsReadMode::On)
Reads the sensors, provides calibrated values, and returns an estimated black line position.
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.