This library is designed to work with devices like the LM335 temperature sensor. There are only two requirements for compatibility: 1) The device must be a sensor device that has an analog voltage output. 2) The physical quantity measured must vary linearly with the analog voltage. It uses a median filter to remove unwanted noise from ADC readings. The number of samples in the filter can be specified as an argument, as well as calibration points or a linear multiplier and coefficient.
LinearAnalogSensor.h@0:78eeca1c1944, 2013-08-07 (annotated)
- Committer:
- PennElectric
- Date:
- Wed Aug 07 19:37:12 2013 +0000
- Revision:
- 0:78eeca1c1944
Recent update with the names changed and variable number of samples.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
PennElectric | 0:78eeca1c1944 | 1 | /* Copyright (c) <2012> <P. Patel>, MIT License |
PennElectric | 0:78eeca1c1944 | 2 | * |
PennElectric | 0:78eeca1c1944 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
PennElectric | 0:78eeca1c1944 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
PennElectric | 0:78eeca1c1944 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
PennElectric | 0:78eeca1c1944 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
PennElectric | 0:78eeca1c1944 | 7 | * furnished to do so, subject to the following conditions: |
PennElectric | 0:78eeca1c1944 | 8 | * |
PennElectric | 0:78eeca1c1944 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
PennElectric | 0:78eeca1c1944 | 10 | * substantial portions of the Software. |
PennElectric | 0:78eeca1c1944 | 11 | * |
PennElectric | 0:78eeca1c1944 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
PennElectric | 0:78eeca1c1944 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
PennElectric | 0:78eeca1c1944 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
PennElectric | 0:78eeca1c1944 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
PennElectric | 0:78eeca1c1944 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
PennElectric | 0:78eeca1c1944 | 17 | */ |
PennElectric | 0:78eeca1c1944 | 18 | |
PennElectric | 0:78eeca1c1944 | 19 | // --------------------- Median Filtered Linear Temperature Sensor Reader ------------------------ |
PennElectric | 0:78eeca1c1944 | 20 | |
PennElectric | 0:78eeca1c1944 | 21 | #ifndef LINEAR_ANALOG |
PennElectric | 0:78eeca1c1944 | 22 | #define LINEAR_ANALOG |
PennElectric | 0:78eeca1c1944 | 23 | |
PennElectric | 0:78eeca1c1944 | 24 | #include "mbed.h" |
PennElectric | 0:78eeca1c1944 | 25 | /** This library is designed to work with devices like the LM335 temperature sensor. There are only |
PennElectric | 0:78eeca1c1944 | 26 | * two requirements for compatibility of a device with this library: 1) It must be a sensor device |
PennElectric | 0:78eeca1c1944 | 27 | * that has an analog voltage output. 2) The physical quantity measured must vary linearly with |
PennElectric | 0:78eeca1c1944 | 28 | * the analog voltage. The LM335 which creates an analog voltage proportional to temperature |
PennElectric | 0:78eeca1c1944 | 29 | * can work very well with this library using the nominal multiplier of 0.0050354 and offset of |
PennElectric | 0:78eeca1c1944 | 30 | * -273.15 or by providing two calibration points to the constuctor to generate a linear response. It |
PennElectric | 0:78eeca1c1944 | 31 | * samples the sensor 9 times and uses the median to minimize the effect of poor ADC readings and sudden |
PennElectric | 0:78eeca1c1944 | 32 | * changes. |
PennElectric | 0:78eeca1c1944 | 33 | * |
PennElectric | 0:78eeca1c1944 | 34 | * Example: |
PennElectric | 0:78eeca1c1944 | 35 | * @code |
PennElectric | 0:78eeca1c1944 | 36 | * LinearAnalogSensor mysensor(p20, 0.0050354, -273.15); // Setup a LM335 temp sensor on pin 20 |
PennElectric | 0:78eeca1c1944 | 37 | * DigitalOut myled(LED1); |
PennElectric | 0:78eeca1c1944 | 38 | * |
PennElectric | 0:78eeca1c1944 | 39 | * int main() { |
PennElectric | 0:78eeca1c1944 | 40 | * while(1) { |
PennElectric | 0:78eeca1c1944 | 41 | * // Light LED if filtered temperature is greater than 45 degrees Celsius |
PennElectric | 0:78eeca1c1944 | 42 | * if (mysensor > 45) myled = 1; |
PennElectric | 0:78eeca1c1944 | 43 | * else myled = 0; |
PennElectric | 0:78eeca1c1944 | 44 | * } |
PennElectric | 0:78eeca1c1944 | 45 | * } |
PennElectric | 0:78eeca1c1944 | 46 | * @endcode |
PennElectric | 0:78eeca1c1944 | 47 | */ |
PennElectric | 0:78eeca1c1944 | 48 | class LinearAnalogSensor { |
PennElectric | 0:78eeca1c1944 | 49 | public: |
PennElectric | 0:78eeca1c1944 | 50 | /** Create a calibrated temperature sensor object connected to an Analog input pin |
PennElectric | 0:78eeca1c1944 | 51 | * @param pin Analog input pin to which an LM335 or comparable linear temp sensor is connected |
PennElectric | 0:78eeca1c1944 | 52 | * @param numSamples Number of samples over which to apply a median filter |
PennElectric | 0:78eeca1c1944 | 53 | * @param temp1 Temperature (float in degrees C or F) of first calibration point |
PennElectric | 0:78eeca1c1944 | 54 | * @param read1 Mbed ADC reading (unsigned short) of first calibration point |
PennElectric | 0:78eeca1c1944 | 55 | * @param temp2 Temperature (float in degrees C or F) of second calibration point |
PennElectric | 0:78eeca1c1944 | 56 | * @param read2 Mbed ADC reading (unsigned short) of second calibration point |
PennElectric | 0:78eeca1c1944 | 57 | */ |
PennElectric | 0:78eeca1c1944 | 58 | LinearAnalogSensor(PinName pin, int numSamples, float temp1, unsigned short read1, float temp2, unsigned short read2); |
PennElectric | 0:78eeca1c1944 | 59 | /** Create a calibrated temperature sensor object connected to an Analog input pin |
PennElectric | 0:78eeca1c1944 | 60 | * @param pin Analog input pin to which an LM335 or comparable linear temp sensor is connected |
PennElectric | 0:78eeca1c1944 | 61 | * @param numSamples Number of samples over which to apply a median filter |
PennElectric | 0:78eeca1c1944 | 62 | * @param multiplier Conversion multiplier to go from ADC reading as unsigned short to temperature (change in degrees / change in unsigned short) |
PennElectric | 0:78eeca1c1944 | 63 | * @param offset Conversion offset (positive or negative) |
PennElectric | 0:78eeca1c1944 | 64 | */ |
PennElectric | 0:78eeca1c1944 | 65 | LinearAnalogSensor(PinName pin, int numSamples, float multiplier, float offset); |
PennElectric | 0:78eeca1c1944 | 66 | /** Returns a new median-filtered, converted reading from the sensor |
PennElectric | 0:78eeca1c1944 | 67 | * @returns New temperature (float) in degrees C or F as designated by the calibration points |
PennElectric | 0:78eeca1c1944 | 68 | */ |
PennElectric | 0:78eeca1c1944 | 69 | float read(); |
PennElectric | 0:78eeca1c1944 | 70 | /** Return the last calculated and stored temperature |
PennElectric | 0:78eeca1c1944 | 71 | * @returns Last stored temperature (float) in degrees C or F as designated by the calibration points |
PennElectric | 0:78eeca1c1944 | 72 | */ |
PennElectric | 0:78eeca1c1944 | 73 | float getLast(); |
PennElectric | 0:78eeca1c1944 | 74 | #ifdef MBED_OPERATORS |
PennElectric | 0:78eeca1c1944 | 75 | /** An operator shorthand for readTemp() to calculate filtered temperature |
PennElectric | 0:78eeca1c1944 | 76 | */ |
PennElectric | 0:78eeca1c1944 | 77 | operator float() { |
PennElectric | 0:78eeca1c1944 | 78 | return read(); |
PennElectric | 0:78eeca1c1944 | 79 | } |
PennElectric | 0:78eeca1c1944 | 80 | #endif |
PennElectric | 0:78eeca1c1944 | 81 | private: |
PennElectric | 0:78eeca1c1944 | 82 | AnalogIn _pin; |
PennElectric | 0:78eeca1c1944 | 83 | unsigned short * _arr; |
PennElectric | 0:78eeca1c1944 | 84 | float _reading; |
PennElectric | 0:78eeca1c1944 | 85 | float _multiplier; |
PennElectric | 0:78eeca1c1944 | 86 | int _numSamples; |
PennElectric | 0:78eeca1c1944 | 87 | float _offset; |
PennElectric | 0:78eeca1c1944 | 88 | }; |
PennElectric | 0:78eeca1c1944 | 89 | #endif |