Optimized
Fork of LinearAnalogSensors by
Revision 4:61e97a2029f8, committed 2016-08-13
- Comitter:
- wang1tao
- Date:
- Sat Aug 13 09:34:26 2016 +0000
- Parent:
- 3:ab7e37fda3a3
- Commit message:
- Changed for Multi-channel PH measurement
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AnalogPH.h Sat Aug 13 09:34:26 2016 +0000 @@ -0,0 +1,86 @@ +/* Copyright (c) <2012> <P. Patel>, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + + // --------------------- Median Filtered Linear Temperature Sensor Reader ------------------------ + +#ifndef ANALOGPH +#define ANALOGPH + +#include "mbed.h" +/** This library is designed to work with devices like the LM335 temperature sensor. There are only + * two requirements for compatibility of a device with this library: 1) It must be a sensor device + * that has an analog voltage output. 2) The physical quantity measured must vary linearly with + * the analog voltage. The LM335 which creates an analog voltage proportional to temperature + * can work very well with this library using the nominal multiplier of 0.0050354 and offset of + * -273.15 or by providing two calibration points to the constuctor to generate a linear response. It + * samples the sensor 9 times and uses the median to minimize the effect of poor ADC readings and sudden + * changes. + * + * Example: + * @code + * LinearTemp mysensor(p20, 0.0050354, -273.15); // Setup a LM335 temp sensor on pin 20 + * DigitalOut myled(LED1); + * + * int main() { + * while(1) { + * // Light LED if filtered temperature is greater than 45 degrees Celsius + * if (mysensor > 45) myled = 1; + * else myled = 0; + * } + * } + * @endcode + */ +class AnalogPHSensor { +public: + /** Create a calibrated temperature sensor object connected to an Analog input pin + * @param pin Analog input pin to which an LM335 or comparable linear temp sensor is connected + * @param temp1 Temperature (float in degrees C or F) of first calibration point + * @param read1 Mbed ADC reading (unsigned short) of first calibration point + * @param temp2 Temperature (float in degrees C or F) of second calibration point + * @param read2 Mbed ADC reading (unsigned short) of second calibration point + */ + AnalogPHSensor(PinName pin, float temp1, unsigned short read1, float temp2, unsigned short read2); + /** Create a calibrated temperature sensor object connected to an Analog input pin + * @param pin Analog input pin to which an LM335 or comparable linear temp sensor is connected + * @param multiplier Conversion multiplier to go from ADC reading as unsigned short to temperature (change in degrees / change in unsigned short) + * @param offset Conversion offset (positive or negative) + */ + AnalogPHSensor(PinName pin, float multiplier, float offset); + /** Returns a new median-filtered, converted reading from the sensor + * @returns New temperature (float) in degrees C or F as designated by the calibration points + */ + float readPH(); + /** Return the last calculated and stored temperature + * @returns Last stored PH + */ + float getPH(); + #ifdef MBED_OPERATORS + /** An operator shorthand for readPH() to calculate filtered PH + */ + operator float() { + return readPH(); + } + #endif +private: + AnalogIn _pin; + unsigned short _phArr[6]; + float _ph; + float _multiplier; + float _offset; +}; +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AnaolgPH.cpp Sat Aug 13 09:34:26 2016 +0000 @@ -0,0 +1,75 @@ +/* Copyright (c) <2012> <P. Patel>, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +// --------------------- Median Filtered Linear Temperature Sensor Reader ---------------------------- + +#include "AnalogPH.h" +#include "mbed.h" +#include "mDot.h" + +// Constructor using two calibration points to define linear multiplier and offset +AnalogPHSensor::AnalogPHSensor(PinName pin, float temp1, unsigned short read1, float temp2, unsigned short read2):_pin(pin) { + _ph = 0.0; // Zero the temperature + _multiplier = (temp2 - temp1) / (read2 - read1); // Calculate multiplier as slope + _offset = temp1 - (_multiplier * read1); // Calculate offset +} +// Constructor using user defined multiplier and offset +AnalogPHSensor::AnalogPHSensor(PinName pin, float multiplier, float offset):_pin(pin) { + _ph = 0.0; // Zero the temperature + _multiplier = multiplier; // Set multiplier + _offset = offset; // Set offset +} + +// Populates an array with ph readings, sorts, and returns median +float AnalogPHSensor::readPH() { + unsigned short i, minValue, maxValue; + + _ph = 0.0; + for (i = 0; i < 6; i++) { + _phArr[i] = _pin.read_u16(); + osDelay(100); + } + + //looking for the minValue and maxValue in the buffer + minValue = _phArr[0]; + maxValue = _phArr[0]; + for(i=1; i<6; i++){ + if(minValue > _phArr[i])minValue=_phArr[i]; + if(maxValue < _phArr[i])maxValue=_phArr[i]; + } + + //sum + for(i=0; i<6; i++) + { + _ph += (float)_phArr[i]; + } + + // minus max and min + _ph -= (float)minValue; + _ph -= (float)maxValue; + + //averaging + _ph = _ph/4.0; + + _ph = 3.0*_ph/(16.0*4096); + return _ph; +} +// Returns last calculated ph value +float AnalogPHSensor::getPH() { + return _ph; +} \ No newline at end of file
--- a/LinearTemp.cpp Fri Aug 12 03:32:33 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,75 +0,0 @@ -/* Copyright (c) <2012> <P. Patel>, MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -// --------------------- Median Filtered Linear Temperature Sensor Reader ---------------------------- - -#include "LinearTemp.h" -#include "mbed.h" -#include "mDot.h" - -// Constructor using two calibration points to define linear multiplier and offset -LinearTemp::LinearTemp(PinName pin, float temp1, unsigned short read1, float temp2, unsigned short read2):_pin(pin) { - _temp = 0.0; // Zero the temperature - _multiplier = (temp2 - temp1) / (read2 - read1); // Calculate multiplier as slope - _offset = temp1 - (_multiplier * read1); // Calculate offset -} -// Constructor using user defined multiplier and offset -LinearTemp::LinearTemp(PinName pin, float multiplier, float offset):_pin(pin) { - _temp = 0.0; // Zero the temperature - _multiplier = multiplier; // Set multiplier - _offset = offset; // Set offset -} - -// Populates an array with temperature readings, sorts, and returns median -float LinearTemp::readTemp() { - unsigned short i, tmp, minValue, maxValue; - - _temp = 0.0; - for (i = 0; i < 10; i++) { - _tempArr[i] = _pin.read_u16(); - osDelay(200); - } - - //looking for the minValue and maxValue in the buffer - minValue = _tempArr[0]; - maxValue = _tempArr[0]; - for(i=1; i<10; i++){ - if(minValue > _tempArr[i])minValue=_tempArr[i]; - if(maxValue < _tempArr[i])maxValue=_tempArr[i]; - } - - //sum - for(i=0; i<10; i++) - { - _temp += (float)_tempArr[i]; - } - - // minus max and min - _temp -= (float)minValue; - _temp -= (float)maxValue; - - //averaging - _temp = _temp/8.0; - - _temp = 3.0*_temp/(16.0*4096); - return _temp; -} -// Returns last calculated temperature value -float LinearTemp::getTemp() { - return _temp; -} \ No newline at end of file
--- a/LinearTemp.h Fri Aug 12 03:32:33 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,86 +0,0 @@ -/* Copyright (c) <2012> <P. Patel>, MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - - // --------------------- Median Filtered Linear Temperature Sensor Reader ------------------------ - -#ifndef LINEAR_TEMP -#define LINEAR_TEMP - -#include "mbed.h" -/** This library is designed to work with devices like the LM335 temperature sensor. There are only - * two requirements for compatibility of a device with this library: 1) It must be a sensor device - * that has an analog voltage output. 2) The physical quantity measured must vary linearly with - * the analog voltage. The LM335 which creates an analog voltage proportional to temperature - * can work very well with this library using the nominal multiplier of 0.0050354 and offset of - * -273.15 or by providing two calibration points to the constuctor to generate a linear response. It - * samples the sensor 9 times and uses the median to minimize the effect of poor ADC readings and sudden - * changes. - * - * Example: - * @code - * LinearTemp mysensor(p20, 0.0050354, -273.15); // Setup a LM335 temp sensor on pin 20 - * DigitalOut myled(LED1); - * - * int main() { - * while(1) { - * // Light LED if filtered temperature is greater than 45 degrees Celsius - * if (mysensor > 45) myled = 1; - * else myled = 0; - * } - * } - * @endcode - */ -class LinearTemp { -public: - /** Create a calibrated temperature sensor object connected to an Analog input pin - * @param pin Analog input pin to which an LM335 or comparable linear temp sensor is connected - * @param temp1 Temperature (float in degrees C or F) of first calibration point - * @param read1 Mbed ADC reading (unsigned short) of first calibration point - * @param temp2 Temperature (float in degrees C or F) of second calibration point - * @param read2 Mbed ADC reading (unsigned short) of second calibration point - */ - LinearTemp(PinName pin, float temp1, unsigned short read1, float temp2, unsigned short read2); - /** Create a calibrated temperature sensor object connected to an Analog input pin - * @param pin Analog input pin to which an LM335 or comparable linear temp sensor is connected - * @param multiplier Conversion multiplier to go from ADC reading as unsigned short to temperature (change in degrees / change in unsigned short) - * @param offset Conversion offset (positive or negative) - */ - LinearTemp(PinName pin, float multiplier, float offset); - /** Returns a new median-filtered, converted reading from the sensor - * @returns New temperature (float) in degrees C or F as designated by the calibration points - */ - float readTemp(); - /** Return the last calculated and stored temperature - * @returns Last stored temperature (float) in degrees C or F as designated by the calibration points - */ - float getTemp(); - #ifdef MBED_OPERATORS - /** An operator shorthand for readTemp() to calculate filtered temperature - */ - operator float() { - return readTemp(); - } - #endif -private: - AnalogIn _pin; - unsigned short _tempArr[10]; - float _temp; - float _multiplier; - float _offset; -}; -#endif \ No newline at end of file