QTRSensors library
|
This library allows you to use the QTRSensors::calibrate() method to easily calibrate your sensors for the particular conditions it will encounter. Calibrating your sensors can lead to substantially more reliable sensor readings, which in turn can help simplify your code. As such, we recommend you build a calibration phase into your application’s initialization routine. This can be as simple as a fixed duration over which you repeatedly call the calibrate()
method. During this calibration phase, you will need to expose each of your reflectance sensors to the lightest and darkest readings they will encounter. For example, if you have made a line follower, you will want to slide it across the line during the calibration phase so the each sensor can get a reading of how dark the line is and how light the ground is. A sample calibration routine would be:
This library gives you a number of different ways to read the sensors. All of these take an optional mode
argument that determines how the IR emitters behave during the reading. (The options available depend on what sensor board you have; see QTRReadMode for details.)
A sample routine to obtain the sensor values and perform rudimentary line following would be:
The integer value returned by QTRSensors::readLineWhite() or QTRSensors::readLineBlack() can be easily converted into a measure of your position error for line-following applications, as was demonstrated in the previous code sample. The function used to generate this position/error value is designed to be monotonic, which means the value will almost always change in the same direction as you sweep your sensors across the line. This makes it a great quantity to use for PID (proportional–integral–derivative) control.
Explaining PID control is beyond the scope of this document, but Wikipedia has a good article on the subject.
The following code gives a very simple example of PD (proportional–derivative) control (we find the integral term is usually not necessary for line following). The specific constants will be determined by your particular application, but note that the derivative constant Kd is usually much bigger than the proportional constant Kp. This is because the derivative (rate of change) of the error is a much smaller quantity than the error itself, so in order to produce a meaningful correction, it needs to be multiplied by a much larger constant.