source: rtos_arduino/trunk/arduino_lib/libraries/ZumoShield/ZumoReflectanceSensorArray.h@ 232

Last change on this file since 232 was 232, checked in by ertl-honda, 8 years ago

Zumo用ライブラリの追加

File size: 18.1 KB
Line 
1/*! \file ZumoReflectanceSensorArray.h
2 *
3 * See the ZumoReflectanceSensorArray class reference for more information about
4 * this library.
5 *
6 * \class ZumoReflectanceSensorArray ZumoReflectanceSensorArray.h
7 * \brief Read from reflectance sensor array
8 *
9 * The ZumoReflectanceSensorArray library provides an interface for using a
10 * [Zumo Reflectance Sensor Array](http://www.pololu.com/product/1419) connected
11 * to a Zumo robot. The library provides access to the raw sensor values as well
12 * as to high level functions including calibration and line-tracking.
13 *
14 * For calibration, memory is allocated using the `malloc()` function. This
15 * conserves RAM: if all six sensors are calibrated with the emitters both on
16 * and off, a total of 48 bytes is dedicated to storing calibration values.
17 * However, for an application where only two sensors are used, and the
18 * emitters are always on during reads, only 4 bytes are required.
19 *
20 * Internally, this library uses all standard Arduino functions such as
21 * `micros()` for timing and `digitalRead()` for getting the sensor values, so
22 * it should work on all Arduinos without conflicting with other libraries.
23 *
24 * ### Calibration ###
25 *
26 * This library allows you to use the `calibrate()` method to easily calibrate
27 * your sensors for the particular conditions it will encounter. Calibrating
28 * your sensors can lead to substantially more reliable sensor readings, which
29 * in turn can help simplify your code. As such, we recommend you build a
30 * calibration phase into your Zumo's initialization routine. This can be as
31 * simple as a fixed duration over which you repeatedly call the `calibrate()`
32 * method.
33 *
34 * During this calibration phase, you will need to expose each of your
35 * reflectance sensors to the lightest and darkest readings they will encounter.
36 * For example, if your Zumo is programmed to be a line follower, you will want
37 * to slide it across the line during the calibration phase so the each sensor
38 * can get a reading of how dark the line is and how light the ground is (or you
39 * can program it to automatically turn back and forth to pass all of the
40 * sensors over the line). The **SensorCalibration** example included with this
41 * library demonstrates a calibration routine.
42 *
43 * ### Reading the sensors
44 *
45 *
46 * This library gives you a number of different ways to read the sensors.
47 *
48 * - You can request raw sensor values using the `read()` method.
49 *
50 * - You can request calibrated sensor values using the `readCalibrated()`
51 * method. Calibrated sensor values will always range from 0 to 1000, with the
52 * extreme values corresponding to the most and least reflective surfaces
53 * encountered during calibration.
54 *
55 * - For line-detection applications, you can request the line location using
56 * the `readLine()` method. This function provides calibrated values
57 * for each sensor and returns an integer that tells you where it thinks the
58 * line is.
59 *
60 * ### Class Inheritance ###
61 *
62 * The ZumoReflectanceSensorArray class is derived from the QTRSensorsRC class,
63 * which is in turn derived from the QTRSensors base class. The QTRSensorsRC and
64 * QTRSensors classes are part of the \ref QTRSensors.h "QTRSensors" library,
65 * which provides more general functionality for working with reflectance
66 * sensors and is included in the Zumo Shield libraries as a dependency for this
67 * library.
68 *
69 * We recommend using the ZumoReflectanceSensorArray library instead of
70 * the \ref QTRSensors.h "QTRSensors" library when programming an Arduino on a
71 * Zumo. For documentation specific to the %QTRSensors library, please see its
72 * [user's guide](http://www.pololu.com/docs/0J19) on Pololu's website.
73 */
74
75#ifndef ZumoReflectanceSensorArray_h
76#define ZumoReflectanceSensorArray_h
77
78#include <QTRSensors.h>
79#include <Arduino.h>
80
81#if defined(__AVR_ATmega32U4__)
82 // Arduino Leonardo
83 #define ZUMO_SENSOR_ARRAY_DEFAULT_EMITTER_PIN A4
84#else
85 // Arduino UNO and other ATmega328P/168 Arduinos
86 #define ZUMO_SENSOR_ARRAY_DEFAULT_EMITTER_PIN 2
87#endif
88
89class ZumoReflectanceSensorArray : public QTRSensorsRC
90{
91 public:
92
93 /*! \brief Minimal constructor.
94 *
95 * This version of the constructor performs no initialization. If it is used,
96 * the user must call init() before using the methods in this class.
97 */
98 ZumoReflectanceSensorArray() {}
99
100 /*! \brief Constructor; initializes with given emitter pin and defaults for
101 * other settings.
102 *
103 * \param emitterPin Pin that turns IR emitters on or off.
104 *
105 * This constructor calls `init(unsigned char emitterPin)` with the specified
106 * emitter pin and default values for other settings.
107 */
108 ZumoReflectanceSensorArray(unsigned char emitterPin)
109 {
110 init(emitterPin);
111 }
112
113 /*! \brief Constructor; initializes with all settings as given.
114 *
115 * \param pins Array of pin numbers for sensors.
116 * \param numSensors Number of sensors.
117 * \param timeout Maximum duration of reflectance reading in microseconds.
118 * \param emitterPin Pin that turns IR emitters on or off.
119 *
120 * This constructor calls `init(unsigned char * pins, unsigned char
121 * numSensors, unsigned int timeout, unsigned char emitterPin)` with all
122 * settings as given.
123 */
124 ZumoReflectanceSensorArray(unsigned char * pins, unsigned char numSensors, unsigned int timeout = 2000,
125 unsigned char emitterPin = ZUMO_SENSOR_ARRAY_DEFAULT_EMITTER_PIN)
126 {
127 QTRSensorsRC::init(pins, numSensors, timeout, emitterPin);
128 }
129
130 /*! \brief Initializes with given emitter pin and and defaults for other
131 * settings.
132 *
133 * \param emitterPin Pin that turns IR emitters on or off.
134 *
135 * This function initializes the ZumoReflectanceSensorArray object with the
136 * specified emitter pin. The other settings are set to default values: all
137 * six sensors on the array are active, and a timeout of 2000 microseconds is
138 * used.
139 *
140 * \a emitterPin is the Arduino digital pin that controls whether the IR LEDs
141 * are on or off. This pin is optional; if a valid pin is specified, the
142 * emitters will only be turned on during a reading. If \a emitterPin is not
143 * specified, the emitters will be controlled with pin 2 on the Uno (and other
144 * ATmega328/168 boards) or pin A4 on the Leonardo (and other ATmega32U4
145 * boards). (The "LED ON" jumper on the Zumo Reflectance Sensor Array must be
146 * configured correctly for this to work.) If the value `QTR_NO_EMITTER_PIN`
147 * (255) is used, you can leave the emitter pin disconnected and the IR
148 * emitters will always be on.
149 */
150 void init(unsigned char emitterPin = ZUMO_SENSOR_ARRAY_DEFAULT_EMITTER_PIN)
151 {
152 unsigned char sensorPins[] = { 4, A3, 11, A0, A2, 5 };
153 QTRSensorsRC::init(sensorPins, sizeof(sensorPins), 2000, emitterPin);
154 }
155
156 /*! \brief Initializes with all settings as given.
157 *
158 * \param pins Array of pin numbers for sensors.
159 * \param numSensors Number of sensors.
160 * \param timeout Maximum duration of reflectance reading in microseconds.
161 * \param emitterPin Pin that turns IR emitters on or off.
162 *
163 * This function initializes the ZumoReflectanceSensorArray object with all
164 * settings as given.
165 *
166 * The array \a pins should contain the Arduino digital pin numbers for each
167 * sensor.
168 *
169 * \a numSensors specifies the length of the \a pins array (the number of
170 * reflectance sensors you are using).
171 *
172 * \a timeout specifies the length of time in microseconds beyond which you
173 * consider the sensor reading completely black. That is to say, if the pulse
174 * length for a pin exceeds \a timeout, pulse timing will stop and the reading
175 * for that pin will be considered full black. It is recommended that you set
176 * \a timeout to be between 1000 and 3000 us, depending on factors like the
177 * height of your sensors and ambient lighting. This allows you to shorten the
178 * duration of a sensor-reading cycle while maintaining useful measurements of
179 * reflectance. If \a timeout is not specified, it defaults to 2000 us. (See
180 * the [product page](http://www.pololu.com/product/1419) for the Zumo
181 * Reflectance Sensor Array on Pololu's website for an overview of the
182 * sensors' principle of operation.)
183 *
184 * \a emitterPin is the Arduino digital pin that controls whether the IR LEDs
185 * are on or off. This pin is optional; if a valid pin is specified, the
186 * emitters will only be turned on during a reading. If \a emitterPin is not
187 * specified, the emitters will be controlled with pin 2 on the Uno (and other
188 * ATmega328/168 boards) or pin A4 on the Leonardo (and other ATmega32U4
189 * boards). (The corresponding connection should be made with the "LED ON"
190 * jumper on the Zumo Reflectance Sensor Array.) If the value
191 * `QTR_NO_EMITTER_PIN` (255) is used, you can leave the emitter pin
192 * disconnected and the IR emitters will always be on.
193 *
194 * This version of `%init()` can be useful if you only want to use a subset
195 * of the six reflectance sensors on the array. For example, using the
196 * outermost two sensors (on pins 4 and 5 by default) is usually enough for
197 * detecting the ring border in sumo competitions:
198 *
199 * ~~~{.ino}
200 * ZumoReflectanceSensorArray reflectanceSensors;
201 *
202 * ...
203 *
204 * reflectanceSensors.init((unsigned char[]) {4, 5}, 2);
205 * ~~~
206 *
207 * Alternatively, you can use \ref ZumoReflectanceSensorArray(unsigned char * pins, unsigned char numSensors, unsigned int timeout, unsigned char emitterPin)
208 * "a different constructor" to declare and initialize the object at the same
209 * time:
210 *
211 * ~~~{.ino}
212 *
213 * ZumoReflectanceSensorArray reflectanceSensors((unsigned char[]) {4, 5}, 2);
214 * ~~~
215 *
216 */
217 void init(unsigned char * pins, unsigned char numSensors, unsigned int timeout = 2000,
218 unsigned char emitterPin = ZUMO_SENSOR_ARRAY_DEFAULT_EMITTER_PIN)
219 {
220 QTRSensorsRC::init(pins, numSensors, timeout, emitterPin);
221 }
222};
223
224// documentation for inherited functions
225
226/*! \fn void QTRSensors::read(unsigned int *sensor_values, unsigned char readMode = QTR_EMITTERS_ON)
227\memberof ZumoReflectanceSensorArray
228 * \brief Reads the raw sensor values into an array.
229 *
230 * \param sensorValues Array to populate with sensor readings.
231 * \param readMode Read mode (`QTR_EMITTERS_OFF`, `QTR_EMITTERS_ON`, or
232 * `QTR_EMITTERS_ON_AND_OFF`).
233 *
234 * There **must** be space in the \a sensorValues array for as many values as
235 * there were sensors specified in the constructor. The values returned are
236 * measures of the reflectance in units of microseconds. They will be raw
237 * readings between 0 and the \a timeout argument (in units of microseconds)
238 * provided in the constructor (which defaults to 2000).
239 *
240 * The \a readMode argument specifies the kind of read that will be performed.
241 * Several options are defined:
242 *
243 * - `QTR_EMITTERS_OFF` specifies that the reading should be made without
244 * turning on the infrared (IR) emitters, in which case the reading represents
245 * ambient light levels near the sensor.
246 * - `QTR_EMITTERS_ON` specifies that the emitters should be turned on for the
247 * reading, which results in a measure of reflectance.
248 * - `QTR_EMITTERS_ON_AND_OFF` specifies that a reading should be made in both
249 * the on and off states. The values returned when this option is used are
250 * given by the formula **on + max &minus; off**, where **on** is the reading
251 * with the emitters on, **off** is the reading with the emitters off, and
252 * **max** is the maximum sensor reading. This option can reduce the amount of
253 * interference from uneven ambient lighting.
254 *
255 * Note that emitter control will only work if you specify a valid emitter pin
256 * in the constructor and make the corresponding connection (with the "LED ON"
257 * jumper or otherwise).
258 *
259 * The ZumoReflectanceSensorArray class inherits this function from the
260 * QTRSensors class.
261 */
262
263/*! \fn void QTRSensors::emittersOff()
264 * \brief Turns the IR LEDs off.
265 *
266 * This is mainly for use by the `read()` method, and calling this function
267 * before or after reading the sensors will have no effect on the readings, but
268 * you might wish to use it for testing purposes. This method will only do
269 * something if the emitter pin specified in the constructor is valid (i.e. not
270 * `QTR_NO_EMITTER_PIN`) and the corresponding connection is made.
271 *
272 * The ZumoReflectanceSensorArray class inherits this function from the
273 * QTRSensors class.
274 */
275
276/*! \fn void QTRSensors::emittersOn()
277 * \brief Turns the IR LEDs on.
278 * \copydetails emittersOff
279 */
280
281/*! \fn void QTRSensors::calibrate(unsigned char readMode = QTR_EMITTERS_ON)
282 * \brief Reads the sensors for calibration.
283 *
284 * \param readMode Read mode (`QTR_EMITTERS_OFF`, `QTR_EMITTERS_ON`, or
285 * `QTR_EMITTERS_ON_AND_OFF`).
286 *
287 * The sensor values read by this function are not returned; instead, the
288 * maximum and minimum values found over time are stored internally and used for
289 * the `readCalibrated()` method. You can access the calibration (i.e raw max
290 * and min sensor readings) through the public member pointers
291 * `calibratedMinimumOn`, `calibratedMaximumOn`, `calibratedMinimumOff`, and
292 * `calibratedMaximumOff`. Note that these pointers will point to arrays of
293 * length \a numSensors, as specified in the constructor, and they will only be
294 * allocated **after** `calibrate()` has been called. If you only calibrate with
295 * the emitters on, the calibration arrays that hold the off values will not be
296 * allocated.
297 *
298 * The ZumoReflectanceSensorArray class inherits this function from the
299 * QTRSensors class.
300 */
301
302/*! \fn void QTRSensors::resetCalibration()
303 * \brief Resets all calibration that has been done.
304 *
305 * This function discards the calibration values that have been previously
306 * recorded, resetting the min and max values.
307 *
308 * The ZumoReflectanceSensorArray class inherits this function from the
309 * QTRSensors class.
310 */
311
312/*! \fn void QTRSensors::readCalibrated(unsigned int *sensor_values, unsigned char readMode = QTR_EMITTERS_ON)
313 * \brief Returns sensor readings normalized to values between 0 and 1000.
314 *
315 * \param sensorValues Array to populate with sensor readings.
316 * \param readMode Read mode (`QTR_EMITTERS_OFF`, `QTR_EMITTERS_ON`, or
317 * `QTR_EMITTERS_ON_AND_OFF`).
318 *
319 * 0 corresponds to a reading that is less than or equal to the minimum value
320 * read by `calibrate()` and 1000 corresponds to a reading that is greater than
321 * or equal to the maximum value. Calibration values are stored separately for
322 * each sensor, so that differences in the sensors are accounted for
323 * automatically.
324 *
325 * The ZumoReflectanceSensorArray class inherits this function from the
326 * QTRSensors class.
327 */
328
329/*! \fn int QTRSensors::readLine(unsigned int *sensor_values, unsigned char readMode = QTR_EMITTERS_ON, unsigned char whiteLine = 0)
330 * \brief Returns an estimated position of a line under the sensor array.
331 *
332 * \param sensorValues Array to populate with sensor readings.
333 * \param readMode Read mode (`QTR_EMITTERS_OFF`, `QTR_EMITTERS_ON`, or
334 * `QTR_EMITTERS_ON_AND_OFF`).
335 * \param whiteLine 0 to detect a dark line on a light surface; 1 to detect
336 * a light line on a dark surface.
337 * \return An estimated line position.
338 *
339 * This function operates the same as `readCalibrated()`, but with a feature
340 * designed for line following: it returns an estimated position of the line.
341 * The estimate is made using a weighted average of the sensor indices
342 * multiplied by 1000, so that a return value of 0 indicates that the line is
343 * directly below sensor 0 (or was last seen by sensor 0 before being lost), a
344 * return value of 1000 indicates that the line is directly below sensor 1, 2000
345 * indicates that it's below sensor 2, etc. Intermediate values indicate that
346 * the line is between two sensors. The formula is:
347 *
348 * \f[
349 * \newcommand{sv}[1]{\mathtt{sensorValues[#1]}}
350 * \text{return value} =
351 * \frac{(0 \times \sv{0}) + (1000 \times \sv{1}) + (2000 \times \sv{2}) + \ldots}
352 * {\sv{0} + \sv{1} + \sv{2} + \ldots}
353 * \f]
354 *
355 * As long as your sensors aren't spaced too far apart relative to the line,
356 * this returned value is designed to be monotonic, which makes it great for use
357 * in closed-loop PID control. Additionally, this method remembers where it last
358 * saw the line, so if you ever lose the line to the left or the right, its line
359 * position will continue to indicate the direction you need to go to reacquire
360 * the line. For example, if sensor 5 is your rightmost sensor and you end up
361 * completely off the line to the left, this function will continue to return
362 * 5000.
363 *
364 * By default, this function assumes a dark line (high values) on a light
365 * background (low values). If your line is light on dark, set the optional
366 * second argument \a whiteLine to true. In this case, each sensor value will be
367 * replaced by the maximum possible value minus its actual value before the
368 * averaging.
369 *
370 * The ZumoReflectanceSensorArray class inherits this function from the
371 * QTRSensors class.
372 */
373
374
375// documentation for inherited member variables
376
377/*!
378 * \property unsigned int * QTRSensors::calibratedMinimumOn
379 * \brief The calibrated minimum values measured for each sensor, with emitters
380 * on.
381 *
382 * This pointer is unallocated and set to 0 until `calibrate()` is called, and
383 * then allocated to exactly the size required. Depending on the \a readMode
384 * argument to `calibrate()`, only the On or Off values might be allocated, as
385 * required. This variable is made public so that you can use the calibration
386 * values for your own calculations and do things like saving them to EEPROM,
387 * performing sanity checking, etc.
388 *
389 * The ZumoReflectanceSensorArray class inherits this variable from the
390 * QTRSensors class.
391 *
392 * \property unsigned int * QTRSensors::calibratedMaximumOn
393 * \brief The calibrated maximum values measured for each sensor, with emitters
394 * on.
395 * \copydetails QTRSensors::calibratedMinimumOn
396 *
397 * \property unsigned int * QTRSensors::calibratedMinimumOff
398 * \brief The calibrated minimum values measured for each sensor, with emitters
399 * off.
400 * \copydetails QTRSensors::calibratedMinimumOn
401 *
402 * \property unsigned int * QTRSensors::calibratedMaximumOff
403 * \brief The calibrated maximum values measured for each sensor, with emitters
404 * off.
405 * \copydetails QTRSensors::calibratedMinimumOn
406 */
407
408#endif
Note: See TracBrowser for help on using the repository browser.