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.cpp@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 | #include "LinearAnalogSensor.h" |
PennElectric | 0:78eeca1c1944 | 22 | #include "mbed.h" |
PennElectric | 0:78eeca1c1944 | 23 | |
PennElectric | 0:78eeca1c1944 | 24 | // Constructor using two calibration points to define linear multiplier and offset |
PennElectric | 0:78eeca1c1944 | 25 | LinearAnalogSensor::LinearAnalogSensor(PinName pin, int numSamples, float temp1, unsigned short read1, float temp2, unsigned short read2):_pin(pin) { |
PennElectric | 0:78eeca1c1944 | 26 | _reading = 0.0; // Zero the temperature |
PennElectric | 0:78eeca1c1944 | 27 | _multiplier = (temp2 - temp1) / (read2 - read1); // Calculate multiplier as slope |
PennElectric | 0:78eeca1c1944 | 28 | _offset = temp1 - (_multiplier * read1); // Calculate offset |
PennElectric | 0:78eeca1c1944 | 29 | _numSamples = numSamples; |
PennElectric | 0:78eeca1c1944 | 30 | _arr = new unsigned short[numSamples]; |
PennElectric | 0:78eeca1c1944 | 31 | } |
PennElectric | 0:78eeca1c1944 | 32 | // Constructor using user defined multiplier and offset |
PennElectric | 0:78eeca1c1944 | 33 | LinearAnalogSensor::LinearAnalogSensor(PinName pin, int numSamples, float multiplier, float offset):_pin(pin) { |
PennElectric | 0:78eeca1c1944 | 34 | _reading = 0.0; // Zero the temperature |
PennElectric | 0:78eeca1c1944 | 35 | _multiplier = multiplier; // Set multiplier |
PennElectric | 0:78eeca1c1944 | 36 | _offset = offset; // Set offset |
PennElectric | 0:78eeca1c1944 | 37 | _numSamples = numSamples; |
PennElectric | 0:78eeca1c1944 | 38 | _arr = new unsigned short[numSamples]; |
PennElectric | 0:78eeca1c1944 | 39 | } |
PennElectric | 0:78eeca1c1944 | 40 | |
PennElectric | 0:78eeca1c1944 | 41 | // Populates an array with temperature readings, sorts, and returns median |
PennElectric | 0:78eeca1c1944 | 42 | float LinearAnalogSensor::read() { |
PennElectric | 0:78eeca1c1944 | 43 | memset(_arr, 0, _numSamples * sizeof(unsigned short)); |
PennElectric | 0:78eeca1c1944 | 44 | _arr[0] = _pin.read_u16(); |
PennElectric | 0:78eeca1c1944 | 45 | for (int i = 1; i < _numSamples; i++) { |
PennElectric | 0:78eeca1c1944 | 46 | _arr[i] = _pin.read_u16(); |
PennElectric | 0:78eeca1c1944 | 47 | unsigned short tmp = _arr[i]; |
PennElectric | 0:78eeca1c1944 | 48 | int j; |
PennElectric | 0:78eeca1c1944 | 49 | for (j = i - 1; j >= 0; j--) { |
PennElectric | 0:78eeca1c1944 | 50 | if (tmp>=_arr[j]) break; |
PennElectric | 0:78eeca1c1944 | 51 | _arr[j + 1] = _arr[j]; |
PennElectric | 0:78eeca1c1944 | 52 | } |
PennElectric | 0:78eeca1c1944 | 53 | _arr[j + 1] = tmp; |
PennElectric | 0:78eeca1c1944 | 54 | } |
PennElectric | 0:78eeca1c1944 | 55 | if (_numSamples % 2 == 0) { |
PennElectric | 0:78eeca1c1944 | 56 | _reading = (((_arr[_numSamples >> 1] + _arr[(_numSamples >> 1) - 1]) / (2.0)) * _multiplier) + _offset; |
PennElectric | 0:78eeca1c1944 | 57 | } |
PennElectric | 0:78eeca1c1944 | 58 | if (_numSamples % 2 == 1) { |
PennElectric | 0:78eeca1c1944 | 59 | _reading = (_arr[_numSamples >> 1] * _multiplier) + _offset; |
PennElectric | 0:78eeca1c1944 | 60 | } |
PennElectric | 0:78eeca1c1944 | 61 | return _reading; |
PennElectric | 0:78eeca1c1944 | 62 | } |
PennElectric | 0:78eeca1c1944 | 63 | // Returns last calculated temperature value |
PennElectric | 0:78eeca1c1944 | 64 | float LinearAnalogSensor::getLast() { |
PennElectric | 0:78eeca1c1944 | 65 | return _reading; |
PennElectric | 0:78eeca1c1944 | 66 | } |