Différences entre versions de « Pololu-Senseur-QTR-Utiliser »

De MCHobby - Wiki
Sauter à la navigation Sauter à la recherche
Ligne 32 : Ligne 32 :
  
 
=== emittersOn() ===
 
=== emittersOn() ===
 +
<syntaxhighlight lang="C">void emittersOn()</syntaxhighlight>
  
{{traduction}}
+
Turn the IR LEDs 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. This method will only do something if the emitter pin specified in the constructor is not <font color="red">QTR_NO_EMITTER_PIN</font>.
  
 +
=== emittersOff() ===
 +
<syntaxhighlight lang="C">void emittersOff()</syntaxhighlight>
 +
 +
Turn the IR LEDs off. 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.
 +
 +
=== calibrate() ===
 +
<syntaxhighlight lang="C">void calibrate(unsigned char readMode = QTR_EMITTERS_ON)</syntaxhighlight>
 +
 +
Reads the sensors for calibration. The sensor values are not returned; instead, the maximum and minimum values found over time are stored internally and used for the '''readCalibrated()''' method. You can access the calibration (i.e raw max and min sensor readings) through the public member pointers '''calibratedMinimumOn''', '''calibratedMaximumOn''', '''calibratedMinimumOff''', and '''calibratedMaximumOff'''. Note that these pointers will point to arrays of length numSensors, as specified in the constructor, and they will only be allocated after '''calibrate()''' has been called. If you only calibrate with the emitters on, the calibration arrays that hold the off values will not be allocated.
 +
 +
=== readCalibrated() ===
 +
<syntaxhighlight lang="C">void readCalibrated(unsigned int *sensorValues, unsigned char readMode = QTR_EMITTERS_ON)</syntaxhighlight>
 +
 +
Returns sensor readings calibrated to a value between 0 and 1000, where 0 corresponds to a reading that is less than or equal to the minimum value read by '''calibrate()''' and 1000 corresponds to a reading that is greater than or equal to the maximum value. Calibration values are stored separately for each sensor, so that differences in the sensors are accounted for automatically.
 +
 +
=== readLine() ===
 +
<syntaxhighlight lang="C">unsigned int readLine(unsigned int *sensorValues, unsigned char readMode = QTR_EMITTERS_ON, unsigned char whiteLine = 0)</syntaxhighlight>
 +
 +
Operates the same as read calibrated, but with a feature designed for line following: this function returns an estimated position of the line. The estimate is made using a weighted average of the sensor indices multiplied by 1000, so that a return value of 0 indicates that the line is directly below sensor 0 (or was last seen by sensor 0 before being lost), a return value of 1000 indicates that the line is directly below sensor 1, 2000 indicates that it’s below sensor 2, etc. Intermediate values indicate that the line is between two sensors. The formula is:
 +
 +
<nowiki> 0*value0 + 1000*value1 + 2000*value2 + ...
 +
--------------------------------------------
 +
    value0  +  value1  +  value2 + ...</nowiki>
 +
 +
As long as your sensors aren’t spaced too far apart relative to the line, this returned value is designed to be monotonic, which makes it great for use in closed-loop PID control. Additionally, this method remembers where it last saw the line, so if you ever lose the line to the left or the right, it’s line position will continue to indicate the direction you need to go to reacquire the line. For example, if sensor 4 is your rightmost sensor and you end up completely off the line to the left, this function will continue to return 4000.
 +
 +
By default, this function assumes a dark line (high values) surrounded by white (low values). If your line is light on black, set the optional second argument whiteLine to true. In this case, each sensor value will be replaced by the maximum possible value minus its actual value before the averaging.
 +
 +
=== calibratedMinimumOn ===
 +
<syntaxhighlight lang="C">unsigned int* calibratedMinimumOn</syntaxhighlight>
 +
 +
The calibrated minimum values measured for each sensor, with emitters on. The pointers are unallocated and set to 0 until '''calibrate()''' is called, and then allocated to exactly the size required. Depending on the readMode argument to calibrate(), only the On or Off values may be allocated, as required. This and the following variables are made public so that you can use them for your own calculations and do things like saving the values to EEPROM, performing sanity checking, etc
 +
 +
=== calibratedMaximumOn ===
 +
<syntaxhighlight lang="C">unsigned int* calibratedMaximumOn</syntaxhighlight>
 +
 +
The calibrated maximum values measured for each sensor, with emitters on.
 +
 +
=== calibratedMinimumOff ===
 +
<syntaxhighlight lang="C">unsigned int* calibratedMinimumOff</syntaxhighlight>
 +
 +
The calibrated minimum values measured for each sensor, with emitters off.
 +
 +
=== calibratedMaximumOff ===
 +
<syntaxhighlight lang="C">unsigned int* calibratedMaximumOff</syntaxhighlight>
 +
 +
The calibrated maximum values measured for each sensor, with emitters off.
 +
 +
=== ~QTRSensors() - descructeur ===
 +
<syntaxhighlight lang="C">Destructor: ~QTRSensors()</syntaxhighlight>
 +
 +
The destructor for the QTRSensors class frees up memory allocated for the calibration arrays.
 +
 +
=== QTRSensorsRC() - constructeur ===
 +
<syntaxhighlight lang="C">Constructor: QTRSensorsRC()</syntaxhighlight>
 +
 +
This version of the constructor performs no initialization. If it is used, the user must call init() before using the methods in this class.
 +
 +
<syntaxhighlight lang="C">Constructor: QTRSensorsRC(unsigned char* digitalPins, unsigned char numSensors, unsigned int timeout = 2000, unsigned char emitterPin = QTR_NO_EMITTER_PIN);</syntaxhighlight>
 +
 +
This constructor just calls {{fname|init()}}, below.
 +
 +
<syntaxhighlight lang="C">void QTRSensorsRC::init(unsigned char* digitalPins, unsigned char numSensors, unsigned int timeout = 2000, unsigned char emitterPin = QTR_NO_EMITTER_PIN)</syntaxhighlight>
 +
 +
Initializes a QTR-RC (digital) sensor array.
 +
 +
* The array ''digitalPins'' should contain the Arduino digital pin numbers for each sensor.
 +
* ''numSensors'' specifies the length of the ''digitalPins'' array (the number of QTR-RC sensors you are using). numSensors must be no greater than 16.
 +
* ''timeout'' specifies the length of time in microseconds beyond which you consider the sensor reading completely black. That is to say, if the pulse length for a pin exceeds timeout, pulse timing will stop and the reading for that pin will be considered full black. It is recommended that you set timeout to be between 1000 and 3000 µs, depending on factors like the height of your sensors and ambient lighting. This allows you to shorten the duration of a sensor-reading cycle while maintaining useful measurements of reflectance.
 +
* ''emitterPin'' is the Arduino digital pin that controls whether the IR LEDs are on or off. This pin is optional and only exists on the 8A and 8RC QTR sensor arrays. If a valid pin is specified, the emitters will only be turned on during a reading. If the value <font color="red">QTR_NO_EMITTER_PIN</font> (255) is used, you can leave the emitter pin disconnected and the IR emitters will always be on.
 +
 +
=== QTRSensorsAnalog() - constructeur ===
 +
<syntaxhighlight lang="C">Constructor: QTRSensorsAnalog()</syntaxhighlight>
 +
 +
This version of the constructor performs no initialization. If this constructor is used, the user must call init() before using the methods in this class.
 +
 +
<syntaxhighlight lang="C">Constructor: QTRSensorsAnalog(unsigned char* analogPins, unsigned char numSensors, unsigned char numSamplesPerSensor = 4, unsigned char emitterPin = QTR_NO_EMITTER_PIN)</syntaxhighlight>
 +
 +
This constructor just calls {{fname|init()}}, below.
 +
 +
<syntaxhighlight lang="C">void init(unsigned char* analogPins, unsigned char numSensors, unsigned char numSamplesPerSensor = 4, unsigned char emitterPin = QTR_NO_EMITTER_PIN)</syntaxhighlight>
 +
 +
Initializes a QTR-A (analog) sensor array.
 +
 +
* The array ''pins'' should contain the Arduino analog input pin number for each sensor. For example, if pins is {0, 1, 7}, sensor 1 is on analog input 0, sensor 2 is on analog input 1, and sensor 3 is on analog input 7.
 +
* ''numSensors'' specifies the length of the analogPins array (the number of QTR-A sensors you are using). numSensors must be no greater than 16.
 +
* ''numSamplesPerSensor'' indicates the number of 10-bit analog samples to average per channel (per sensor) for each reading. The total number of analog-to-digital conversions performed will be equal to numSensors times numSamplesPerSensor. Increasing this parameter increases noise suppression at the cost of sample rate. This parameter must not exceed 64. Recommended value: 4.
 +
* ''emitterPin'' is the Arduino digital pin that controls whether the IR LEDs are on or off. This pin is optional and only exists on the 8A and 8RC QTR sensor arrays. If a valid pin is specified, the emitters will only be turned on during a reading. If the value QTR_NO_EMITTER_PIN (255) is used, you can leave the emitter pin disconnected and the IR emitters will always be on.
  
 
{{Pololu-Senseur-QTR-TRAILER}}
 
{{Pololu-Senseur-QTR-TRAILER}}

Version du 30 avril 2018 à 19:33

QTRSensor Command Reference

For QTR-xA sensors, you will want to instantiate a QTRSensorsAnalog object, and for QTR-xRC sensors you will want to instantiate a QTRSensorsRC object. Aside from the constructors, these two objects provide the same methods for reading sensor values (both classes are derived from the same abstract base class). The library provides access to the raw sensors values as well as to high level functions including calibration and line-tracking.

This section of the library defines an object for each of the two QTR sensor types, with the QTRSensorsAnalog class intended for use with QTR-xA sensors and the QTRSensorsRC class intended for use with QTR-xRC sensors. This library takes care of the differences between the QTR-xA and QTR-xRC sensors internally, providing you with a common interface to both sensors. The only external difference is in the constructors. This is achieved by having both of these classes derive from the abstract base class QTRSensors. This base class cannot be instantiated.

The QTRSensorsAnalog and QTRSensorsRC classes must be instantiated before they are used. This allows multiple QTR sensor arrays to be controlled independently as separate QTRSensors objects.

For calibration, memory is allocated using the malloc() command. This conserves RAM: if all eight sensors are calibrated with the emitters both on an off, a total of 64 bytes would be dedicated to storing calibration values. However, for an application where only three sensors are used, and the emitters are always on during reads, only 6 bytes are required.

Internally, this library uses all standard Arduino functions such as micros() for timing and analogRead() or digitalRead() for getting the sensor values, so it should work on all Arduinos without conflicting with other libraries.

Les fonctions

read()

void read(unsigned int *sensorValues, unsigned char readMode = QTR_EMITTERS_ON)

Reads the raw sensor values into an array. There MUST be space for as many values as there were sensors specified in the constructor. The values returned are a measure of the reflectance in units that depend on the type of sensor being used, with higher values corresponding to lower reflectance (a black surface or a void). QTR-xA sensors will return a raw value between 0 and 1023. QTR-xRC sensors will return a raw value between 0 and the timeout argument (in units of microseconds) provided in the constructor (which defaults to 2000).

The functions that read values from the sensors all take an argument readMode, which specifies the kind of read that will be performed. Several options are defined: QTR_EMITTERS_OFF specifies that the reading should be made without turning on the infrared (IR) emitters, in which case the reading represents ambient light levels near the sensor; QTR_EMITTERS_ON specifies that the emitters should be turned on for the reading, which results in a measure of reflectance; and QTR_EMITTERS_ON_AND_OFF specifies that a reading should be made in both the on and off states. The values returned when the QTR_EMITTERS_ON_AND_OFF option is used are given by on + max – off, where on is the reading with the emitters on, off is the reading with the emitters off, and max is the maximum sensor reading. This option can reduce the amount of interference from uneven ambient lighting. Note that emitter control will only work if you specify a valid emitter pin in the constructor.

Exemple d'utilisation:

unsigned int sensor_values[8];
sensors.read(sensor_values);

emittersOn()

void emittersOn()

Turn the IR LEDs 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. This method will only do something if the emitter pin specified in the constructor is not QTR_NO_EMITTER_PIN.

emittersOff()

void emittersOff()

Turn the IR LEDs off. 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.

calibrate()

void calibrate(unsigned char readMode = QTR_EMITTERS_ON)

Reads the sensors for calibration. The sensor values are not returned; instead, the maximum and minimum values found over time are stored internally and used for the readCalibrated() method. You can access the calibration (i.e raw max and min sensor readings) through the public member pointers calibratedMinimumOn, calibratedMaximumOn, calibratedMinimumOff, and calibratedMaximumOff. Note that these pointers will point to arrays of length numSensors, as specified in the constructor, and they will only be allocated after calibrate() has been called. If you only calibrate with the emitters on, the calibration arrays that hold the off values will not be allocated.

readCalibrated()

void readCalibrated(unsigned int *sensorValues, unsigned char readMode = QTR_EMITTERS_ON)

Returns sensor readings calibrated to a value between 0 and 1000, where 0 corresponds to a reading that is less than or equal to the minimum value read by calibrate() and 1000 corresponds to a reading that is greater than or equal to the maximum value. Calibration values are stored separately for each sensor, so that differences in the sensors are accounted for automatically.

readLine()

unsigned int readLine(unsigned int *sensorValues, unsigned char readMode = QTR_EMITTERS_ON, unsigned char whiteLine = 0)

Operates the same as read calibrated, but with a feature designed for line following: this function returns an estimated position of the line. The estimate is made using a weighted average of the sensor indices multiplied by 1000, so that a return value of 0 indicates that the line is directly below sensor 0 (or was last seen by sensor 0 before being lost), a return value of 1000 indicates that the line is directly below sensor 1, 2000 indicates that it’s below sensor 2, etc. Intermediate values indicate that the line is between two sensors. The formula is:

 0*value0 + 1000*value1 + 2000*value2 + ...
--------------------------------------------
     value0  +  value1  +  value2 + ...

As long as your sensors aren’t spaced too far apart relative to the line, this returned value is designed to be monotonic, which makes it great for use in closed-loop PID control. Additionally, this method remembers where it last saw the line, so if you ever lose the line to the left or the right, it’s line position will continue to indicate the direction you need to go to reacquire the line. For example, if sensor 4 is your rightmost sensor and you end up completely off the line to the left, this function will continue to return 4000.

By default, this function assumes a dark line (high values) surrounded by white (low values). If your line is light on black, set the optional second argument whiteLine to true. In this case, each sensor value will be replaced by the maximum possible value minus its actual value before the averaging.

calibratedMinimumOn

unsigned int* calibratedMinimumOn

The calibrated minimum values measured for each sensor, with emitters on. The pointers are unallocated and set to 0 until calibrate() is called, and then allocated to exactly the size required. Depending on the readMode argument to calibrate(), only the On or Off values may be allocated, as required. This and the following variables are made public so that you can use them for your own calculations and do things like saving the values to EEPROM, performing sanity checking, etc

calibratedMaximumOn

unsigned int* calibratedMaximumOn

The calibrated maximum values measured for each sensor, with emitters on.

calibratedMinimumOff

unsigned int* calibratedMinimumOff

The calibrated minimum values measured for each sensor, with emitters off.

calibratedMaximumOff

unsigned int* calibratedMaximumOff

The calibrated maximum values measured for each sensor, with emitters off.

~QTRSensors() - descructeur

Destructor: ~QTRSensors()

The destructor for the QTRSensors class frees up memory allocated for the calibration arrays.

QTRSensorsRC() - constructeur

Constructor: QTRSensorsRC()

This version of the constructor performs no initialization. If it is used, the user must call init() before using the methods in this class.

Constructor: QTRSensorsRC(unsigned char* digitalPins, unsigned char numSensors, unsigned int timeout = 2000, unsigned char emitterPin = QTR_NO_EMITTER_PIN);

This constructor just calls init(), below.

void QTRSensorsRC::init(unsigned char* digitalPins, unsigned char numSensors, unsigned int timeout = 2000, unsigned char emitterPin = QTR_NO_EMITTER_PIN)

Initializes a QTR-RC (digital) sensor array.

  • The array digitalPins should contain the Arduino digital pin numbers for each sensor.
  • numSensors specifies the length of the digitalPins array (the number of QTR-RC sensors you are using). numSensors must be no greater than 16.
  • timeout specifies the length of time in microseconds beyond which you consider the sensor reading completely black. That is to say, if the pulse length for a pin exceeds timeout, pulse timing will stop and the reading for that pin will be considered full black. It is recommended that you set timeout to be between 1000 and 3000 µs, depending on factors like the height of your sensors and ambient lighting. This allows you to shorten the duration of a sensor-reading cycle while maintaining useful measurements of reflectance.
  • emitterPin is the Arduino digital pin that controls whether the IR LEDs are on or off. This pin is optional and only exists on the 8A and 8RC QTR sensor arrays. If a valid pin is specified, the emitters will only be turned on during a reading. If the value QTR_NO_EMITTER_PIN (255) is used, you can leave the emitter pin disconnected and the IR emitters will always be on.

QTRSensorsAnalog() - constructeur

Constructor: QTRSensorsAnalog()

This version of the constructor performs no initialization. If this constructor is used, the user must call init() before using the methods in this class.

Constructor: QTRSensorsAnalog(unsigned char* analogPins, unsigned char numSensors, unsigned char numSamplesPerSensor = 4, unsigned char emitterPin = QTR_NO_EMITTER_PIN)

This constructor just calls init(), below.

void init(unsigned char* analogPins, unsigned char numSensors, unsigned char numSamplesPerSensor = 4, unsigned char emitterPin = QTR_NO_EMITTER_PIN)

Initializes a QTR-A (analog) sensor array.

  • The array pins should contain the Arduino analog input pin number for each sensor. For example, if pins is {0, 1, 7}, sensor 1 is on analog input 0, sensor 2 is on analog input 1, and sensor 3 is on analog input 7.
  • numSensors specifies the length of the analogPins array (the number of QTR-A sensors you are using). numSensors must be no greater than 16.
  • numSamplesPerSensor indicates the number of 10-bit analog samples to average per channel (per sensor) for each reading. The total number of analog-to-digital conversions performed will be equal to numSensors times numSamplesPerSensor. Increasing this parameter increases noise suppression at the cost of sample rate. This parameter must not exceed 64. Recommended value: 4.
  • emitterPin is the Arduino digital pin that controls whether the IR LEDs are on or off. This pin is optional and only exists on the 8A and 8RC QTR sensor arrays. If a valid pin is specified, the emitters will only be turned on during a reading. If the value QTR_NO_EMITTER_PIN (255) is used, you can leave the emitter pin disconnected and the IR emitters will always be on.

Basé sur "Arduino Library for the Pololu QTR Reflectance Sensors" de Pololu (www.pololu.com/docs/0J19/1) - Traduit en Français par shop.mchobby.be CC-BY-SA pour la traduction
Toute copie doit contenir ce crédit, lien vers cette page et la section "crédit de traduction". Traduit avec l'autorisation expresse de Pololu (www.pololu.com)

Based on "Arduino Library for the Pololu QTR Reflectance Sensors" from Pololu (www.pololu.com/docs/0J19/1) - Translated to French by shop.mchobby.be CC-BY-SA for the translation
Copies must includes this credit, link to this page and the section "crédit de traduction" (translation credit). Translated with the Pololu's authorization (www.pololu.com)