AirQualityClick on A0 on Nucleo-F411RE
Diff: Air.cpp
- Revision:
- 0:964a928b323e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Air.cpp Tue Apr 14 11:36:17 2015 +0000 @@ -0,0 +1,98 @@ +#include "mbed.h" +#include "Air.h" + +Air::Air (PinName pin): _pin (pin) { +} + +/**************************************************************************/ +/*! +@brief Get the correction factor to correct for temperature and humidity + +@param[in] t The ambient air temperature +@param[in] h The relative humidity + +@return The calculated correction factor +*/ +/**************************************************************************/ +float Air::getCorrectionFactor(float t, float h) { + return CORA * (double)t * t - (double)CORB * t + CORC - ((double)h-33.)*CORD; +} + +/**************************************************************************/ +/*! +@brief Get the correction factor to correct for temperature and humidity +a +@return The sensor resistance in kOhm +*/ +/**************************************************************************/ +float Air::getResistance() { + double val = _pin.read(); + return ((1023./(double)val) * 5. - 1.)*RLOAD; +} + +/**************************************************************************/ +/*! +@brief Get the resistance of the sensor, ie. the measurement value corrected + for temp/hum + +@param[in] t The ambient air temperature +@param[in] h The relative humidity + +@return The corrected sensor resistance kOhm +*/ +/**************************************************************************/ +float Air::getCorrectedResistance(float t, float h) { + return getResistance()/getCorrectionFactor(t, h); +} + +/**************************************************************************/ +/*! +@brief Get the ppm of CO2 sensed (assuming only CO2 in the air) + +@return The ppm of CO2 in the air +*/ +/**************************************************************************/ +float Air::getPPM() { + return PARA * pow(((double)getResistance()/RZERO), -PARB); +} + +/**************************************************************************/ +/*! +@brief Get the ppm of CO2 sensed (assuming only CO2 in the air), corrected + for temp/hum + +@param[in] t The ambient air temperature +@param[in] h The relative humidity + +@return The ppm of CO2 in the air +*/ +/**************************************************************************/ +float Air::getCorrectedPPM(float t, float h) { + return PARA * pow(((double)getCorrectedResistance(t, h)/RZERO), -PARB); +} + +/**************************************************************************/ +/*! +@brief Get the resistance RZero of the sensor for calibration purposes + +@return The sensor resistance RZero in kOhm +*/ +/**************************************************************************/ +float Air::getRZero() { + return getResistance() * pow((ATMOCO2/PARA), (1./PARB)); +} + +/**************************************************************************/ +/*! +@brief Get the corrected resistance RZero of the sensor for calibration + purposes + +@param[in] t The ambient air temperature +@param[in] h The relative humidity + +@return The corrected sensor resistance RZero in kOhm +*/ +/**************************************************************************/ +float Air::getCorrectedRZero(float t, float h) { + return getCorrectedResistance(t, h) * pow((ATMOCO2/PARA), (1./PARB)); +}