Zumo32U4 library
Zumo32U4ProximitySensors.h
Go to the documentation of this file.
1 // Copyright Pololu Corporation. For more information, see http://www.pololu.com/
2 
5 #pragma once
6 
7 #include <stdint.h>
8 #include <string.h>
9 
12 static const uint8_t SENSOR_LEFT = 20;
13 
16 static const uint8_t SENSOR_FRONT = 22;
17 
20 static const uint8_t SENSOR_RIGHT = 4;
21 
24 static const uint8_t SENSOR_NO_PIN = 255;
25 
106 {
107 public:
108 
113  static const uint8_t defaultLineSensorEmitterPin = 11;
114 
122  {
123  clearAll();
124  }
125 
129  Zumo32U4ProximitySensors(uint8_t * pins, uint8_t numSensors,
130  uint8_t lineSensorEmitterPin = defaultLineSensorEmitterPin)
131  {
132  clearAll();
133  init(pins, numSensors, lineSensorEmitterPin);
134  }
135 
150  void initThreeSensors(uint8_t lineSensorEmitterPin = defaultLineSensorEmitterPin)
151  {
152  uint8_t defaultPins[] = { SENSOR_LEFT, SENSOR_FRONT, SENSOR_RIGHT };
153  init(defaultPins, sizeof(defaultPins), lineSensorEmitterPin);
154  }
155 
167  void initFrontSensor(uint8_t lineSensorEmitterPin = defaultLineSensorEmitterPin)
168  {
169  uint8_t pins[] = { SENSOR_FRONT };
170  init(pins, sizeof(pins), lineSensorEmitterPin);
171  }
172 
182  void init(uint8_t * pins, uint8_t numSensors,
183  uint8_t lineSensorEmitterPin = defaultLineSensorEmitterPin);
184 
188  uint8_t getNumSensors() const
189  {
190  return numSensors;
191  }
192 
193  /* LED parameters **********************************************************/
194 
200  static const uint16_t defaultPeriod = 420;
201 
213  static const uint16_t defaultPulseOnTimeUs = 421;
214 
238  static const uint16_t defaultPulseOffTimeUs = 578;
239 
251  void setPeriod(uint16_t period)
252  {
253  this->period = period;
254  }
255 
272  void setBrightnessLevels(uint16_t * levels, uint8_t levelCount);
273 
278  void setPulseOnTimeUs(uint16_t pulseOnTimeUs)
279  {
280  this->pulseOnTimeUs = pulseOnTimeUs;
281  }
282 
289  void setPulseOffTimeUs(uint16_t pulseOffTimeUs)
290  {
291  this->pulseOffTimeUs = pulseOffTimeUs;
292  }
293 
297  uint8_t getNumBrightnessLevels() const
298  {
299  return numLevels;
300  }
301 
302  /* Basic operations *********************************************************/
303 
311  void lineSensorEmittersOff();
312 
314  void pullupsOn();
315 
329  bool readBasic(uint8_t sensorNumber);
330 
331  /* Sensor reading ***********************************************************/
332 
368  void read();
369 
376  uint8_t countsWithLeftLeds(uint8_t sensorNumber) const;
377 
384  uint8_t countsWithRightLeds(uint8_t sensorNumber) const;
385 
386  /* Helper methods for the typical Zumo sensor setup ************************/
387 
394  uint8_t countsLeftWithLeftLeds() const
395  {
396  return countsWithLeftLeds(findIndexForPin(SENSOR_LEFT));
397  }
398 
405  uint8_t countsLeftWithRightLeds() const
406  {
407  return countsWithRightLeds(findIndexForPin(SENSOR_LEFT));
408  }
409 
416  uint8_t countsFrontWithLeftLeds() const
417  {
418  return countsWithLeftLeds(findIndexForPin(SENSOR_FRONT));
419  }
420 
427  uint8_t countsFrontWithRightLeds() const
428  {
429  return countsWithRightLeds(findIndexForPin(SENSOR_FRONT));
430  }
431 
438  uint8_t countsRightWithLeftLeds() const
439  {
440  return countsWithLeftLeds(findIndexForPin(SENSOR_RIGHT));
441  }
442 
449  uint8_t countsRightWithRightLeds() const
450  {
451  return countsWithRightLeds(findIndexForPin(SENSOR_RIGHT));
452  }
453 
463  {
464  return readBasic(findIndexForPin(SENSOR_LEFT));
465  }
466 
476  {
477  return readBasic(findIndexForPin(SENSOR_FRONT));
478  }
479 
489  {
490  return readBasic(findIndexForPin(SENSOR_RIGHT));
491  }
492 
493 private:
494 
495  void clearAll();
496  void prepareToRead();
497  uint8_t findIndexForPin(uint8_t pin) const;
498 
499  typedef struct SensorData
500  {
501  uint8_t pin;
502  uint8_t withLeftLeds;
503  uint8_t withRightLeds;
504  } SensorData;
505 
506  SensorData * dataArray;
507  uint8_t numSensors;
508 
509  uint8_t lineSensorEmitterPin;
510 
511  uint16_t * levelsArray;
512  uint8_t numLevels;
513 
514  uint16_t period;
515  uint16_t pulseOnTimeUs;
516  uint16_t pulseOffTimeUs;
517 };
static const uint8_t SENSOR_LEFT
The pin number for the standard pin that is used to read the left proximity sensor.
static const uint8_t SENSOR_FRONT
The pin number for the standard pin that is used to read the front proximity sensor.
static const uint8_t SENSOR_NO_PIN
A constant that can be used in place of a pin number to indicate that no pin should be used.
static const uint8_t SENSOR_RIGHT
The pin number for the standard pin that is used to read the right proximity sensor.
Gets readings from the three proximity sensors on the front sensor array.
void read()
Emits IR pulses and gets readings from the sensors.
static const uint16_t defaultPeriod
The default period for the infrared pulses.
Zumo32U4ProximitySensors(uint8_t *pins, uint8_t numSensors, uint8_t lineSensorEmitterPin=defaultLineSensorEmitterPin)
Constructor that takes pin arguments.
void init(uint8_t *pins, uint8_t numSensors, uint8_t lineSensorEmitterPin=defaultLineSensorEmitterPin)
Configures this object to use a custom set of pins.
uint8_t countsWithRightLeds(uint8_t sensorNumber) const
Returns the number of brightness levels for the right LEDs that activated the specified sensor.
uint8_t countsRightWithRightLeds() const
Returns the number of brightness levels for the right LEDs that activated the right proximity sensor.
void setBrightnessLevels(uint16_t *levels, uint8_t levelCount)
Sets the sequence of brightness levels used by read().
void setPulseOffTimeUs(uint16_t pulseOffTimeUs)
Sets the amount of time, in microseconds, that the read() function will leave the pulses off before g...
void initThreeSensors(uint8_t lineSensorEmitterPin=defaultLineSensorEmitterPin)
Configures this object to use all three proximity sensors.
void lineSensorEmittersOff()
Turns the IR emitters for the line sensors off.
bool readBasic(uint8_t sensorNumber)
Does a quick digital reading of the specified sensor without emitting any IR pulses.
static const uint16_t defaultPulseOffTimeUs
The default time to leave the infrared LEDs off between readings, in microseconds.
bool readBasicFront()
Does a quick digital reading of the front sensor.
static const uint16_t defaultPulseOnTimeUs
The default duration of the bursts of infrared pulses emitted, in microseconds.
uint8_t countsFrontWithRightLeds() const
Returns the number of brightness levels for the right LEDs that activated the front proximity sensor.
bool readBasicRight()
Does a quick digital reading of the right sensor.
uint8_t countsWithLeftLeds(uint8_t sensorNumber) const
Returns the number of brightness levels for the left LEDs that activated the specified sensor.
Zumo32U4ProximitySensors()
Minimal constructor.
static const uint8_t defaultLineSensorEmitterPin
The default line sensor emitter pin.
bool readBasicLeft()
Does a quick digital reading of the left sensor.
uint8_t countsFrontWithLeftLeds() const
Returns the number of brightness levels for the left LEDs that activated the front proximity sensor.
uint8_t countsRightWithLeftLeds() const
Returns the number of brightness levels for the left LEDs that activated the right proximity sensor.
uint8_t countsLeftWithRightLeds() const
Returns the number of brightness levels for the right LEDs that activated the left proximity sensor.
void setPeriod(uint16_t period)
Sets the period used for the IR pulses.
uint8_t getNumBrightnessLevels() const
Returns the number of brightness levels.
void initFrontSensor(uint8_t lineSensorEmitterPin=defaultLineSensorEmitterPin)
Configures this object to use just the front proximity sensor.
uint8_t getNumSensors() const
Returns the number of sensors.
uint8_t countsLeftWithLeftLeds() const
Returns the number of brightness levels for the left LEDs that activated the left proximity sensor.
void pullupsOn()
Sets each sensor pin to an input with pull-up resistors enabled.
void setPulseOnTimeUs(uint16_t pulseOnTimeUs)
Sets the duration, in microseconds, for each burst of IR pulses emitted by the read() function.