grovetemp: updated conversion formula for v1.2 compatibility

Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
Mihai Tudor Panu 2016-08-02 10:16:46 -07:00
parent 84a77730be
commit bda66a3d06
2 changed files with 19 additions and 6 deletions

View File

@ -33,7 +33,7 @@
using namespace upm;
GroveTemp::GroveTemp(unsigned int pin, float scale)
GroveTemp::GroveTemp(unsigned int pin, float scale, int r0, int b)
{
if ( !(m_aio = mraa_aio_init(pin)) ) {
throw std::invalid_argument(std::string(__FUNCTION__) +
@ -42,6 +42,8 @@ GroveTemp::GroveTemp(unsigned int pin, float scale)
}
m_name = "Temperature Sensor";
m_scale = scale;
m_r0 = r0;
m_b = b;
}
GroveTemp::~GroveTemp()
@ -55,8 +57,8 @@ int GroveTemp::value ()
if (a == -1.0) return -1;
// Apply scale factor after error check
a *= m_scale;
float r = (float)(1023.0-a)*10000.0/a;
float t = 1.0/(log(r/10000.0)/3975.0 + 1.0/298.15)-273.15;
float r = (float)(1023.0-a)*(float)m_r0/a;
float t = 1.0/(log(r/(float)m_r0)/(float)m_b + 1.0/298.15)-273.15;
return (int) round(t);
}

View File

@ -43,7 +43,11 @@ namespace upm {
*
* @brief API for the Grove Temperature Sensor
*
* Basic UPM module for the Grove temperature sensor on analog
* Basic UPM module for the Grove temperature sensor on analog. This sensor
* uses a thermistor to measure ambient temperature. The conversion formula has
* been updated to work with versions 1.1 and 1.2 of the sensor. For the older
* v1.0 sensor you will have to specify R0 and B values when initializing the
* device. The range of this sensor is -40 to 125 C and accuracy is +/- 1.5 C.
*
* @image html grovetemp.jpg
* @snippet grovetemp.cxx Interesting
@ -55,9 +59,13 @@ class GroveTemp: public Grove {
*
* @param pin Analog pin to use
* @param scale Scaling factor for raw analog value from the ADC,
* useful for mixed 3.3V/5V boards
* useful for mixed 3.3V/5V boards, default 1.0
* @param r0 zero power resistance, this is 100K (default) for
* v1.1-v1.2 and 10K for v1.0 of the sensor
* @param b thermistor nominal B constant, this is 4275 (default) for
* v1.1-v1.2 and 3975 for v1.0 of the sensor
*/
GroveTemp(unsigned int pin, float scale = 1.0);
GroveTemp(unsigned int pin, float scale = 1.0, int r0 = 100000, int b = 4275);
/**
* GroveTemp destructor
*/
@ -77,5 +85,8 @@ class GroveTemp: public Grove {
private:
mraa_aio_context m_aio;
float m_scale;
int m_r0;
int m_b;
};
}