tmp006: Added upm support for sensor TMP006

TMP006 is a infrared-thermopile sensor.

Signed-off-by: Norbert Wesp <nwesp@phytec.de>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
Norbert Wesp 2017-02-14 11:59:01 +01:00 committed by Mihai Tudor Panu
parent e83b8ef114
commit 8342b4c079
10 changed files with 628 additions and 0 deletions

View File

@ -75,3 +75,4 @@ yg1006.cxx YG1006Sample.java yg1006.js yg1006.py
sensortemplate.cxx SensorTemplateSample.java sensortemplate.js sensortemplate.py
p9813.cxx P9813Sample.java p9813.js p9813.py
tcs37727.cxx tcs37727.py
tmp006.cxx tmp006.py

View File

@ -335,6 +335,7 @@ add_example (abpdrrt005pg2a5)
add_example (lcdks)
add_example (mmc35240)
add_example (tcs37727)
add_example (tmp006)
# These are special cases where you specify example binary, source file and module(s)
include_directories (${PROJECT_SOURCE_DIR}/src)

69
examples/c++/tmp006.cxx Normal file
View File

@ -0,0 +1,69 @@
/* Author: Norbert Wesp <nwesp@phytec.de>
* Copyright (c) 2017 Phytec Messtechnik GmbH.
*
* 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.
*/
#include <unistd.h>
#include <iostream>
#include <signal.h>
#include "tmp006.hpp"
using namespace upm;
bool run = true;
void sig_handler(int sig)
{
if (sig == SIGINT)
run = false;
}
int main(int argc, char **argv)
{
signal(SIGINT, sig_handler);
//! [Interesting]
cout << "Initializing test-application..." << endl;
// Instantiate an TMP006 instance on bus 1
upm::TMP006 *mySensor = new upm::TMP006(1);
// activate periodic measurements
mySensor->setActive();
// update and print available values every second
while (run)
{
// Print out temperature value in °C
cout << "Temperature: " << mySensor->getTemperature(true) << " °C"
<< endl;
sleep(1);
}
cout << "Exiting test-application..." << endl;
delete mySensor;
//! [Interesting]
return 0;
}

60
examples/python/tmp006.py Normal file
View File

@ -0,0 +1,60 @@
#!/usr/bin/python
# Author: Norbert Wesp <nwesp@phytec.de>
# Copyright (c) 2017 Phytec Messtechnik GmbH.
#
# based on: tcs3414cs.py
#
# 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.
from __future__ import print_function
import time, sys, signal, atexit
from upm import pyupm_tmp006 as TMP006
def main():
# Instantiate the Infrared-Thermopile Sensor on I2C on bus 1
mySensor = TMP006.TMP006(1)
## Exit handlers ##
# This stops python from printing a stacktrace when you hit control-C
def SIGINTHandler(signum, frame):
raise SystemExit
# This lets you run code on exit,
# including functions from mySensor
def exitHandler():
print("Exiting")
sys.exit(0)
# Register exit handlers
atexit.register(exitHandler)
signal.signal(signal.SIGINT, SIGINTHandler)
# activate periodic measurements
mySensor.setActive();
# Print out temperature value and config-reg in hex every 0.5 seconds
while(1):
mySensor.getTemperature(True)
hex(mySensor.getConfig())
time.sleep(.5)
if __name__ == '__main__':
main()

View File

@ -0,0 +1,5 @@
set (libname "tmp006")
set (libdescription "IR-Termopile Sensor")
set (module_src ${libname}.cpp)
set (module_hpp ${libname}.hpp)
upm_module_init()

View File

@ -0,0 +1,19 @@
%module javaupm_tmp006
%include "../upm.i"
%{
#include "tmp006.hpp"
%}
%include "tmp006.hpp"
%pragma(java) jniclasscode=%{
static {
try {
System.loadLibrary("javaupm_tmp006");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load. \n" + e);
System.exit(1);
}
}
%}

View File

@ -0,0 +1,8 @@
%module jsupm_tmp006
%include "../upm.i"
%{
#include "tmp006.hpp"
%}
%include "tmp006.hpp"

15
src/tmp006/pyupm_tmp006.i Normal file
View File

@ -0,0 +1,15 @@
// Include doxygen-generated documentation
%include "pyupm_doxy2swig.i"
%module pyupm_tmp006
%include "../upm.i"
%feature("autodoc", "3");
#ifdef DOXYGEN
%include "tmp006_doc.i"
#endif
%include "tmp006.hpp"
%{
#include "tmp006.hpp"
%}

248
src/tmp006/tmp006.cpp Normal file
View File

@ -0,0 +1,248 @@
/*
* Author: Norbert Wesp <nwesp@phytec.de>
* Copyright (c) 2017 Phytec Messtechnik GmbH.
*
* based on: RIOT-driver tmp006 by Johann Fischer <j.fischer@phytec.de>
*
* 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.
*/
#include <iostream>
#include <string>
#include <stdexcept>
#include <unistd.h>
#include <stdlib.h>
#include <endian.h>
#include "tmp006.hpp"
using namespace upm;
TMP006::TMP006(int bus, uint8_t conv_rate, int devAddr) : m_i2ControlCtx(bus) {
uint8_t reg[2];
uint16_t tmp;
m_temperature = 0;
m_name = TMP006_NAME;;
m_controlAddr = devAddr;
m_bus = bus;
if (conv_rate > TMP006_CONFIG_CR_AS16) {
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_i2c_conv_rate() failed");
}
mraa::Result ret = m_i2ControlCtx.address(m_controlAddr);
if (ret != mraa::SUCCESS) {
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_i2c_address() failed");
}
if (checkID() != 0) {
/* sensor_id does not match! maybe wrong sensor chosen? */
throw std::invalid_argument(std::string(__FUNCTION__) +
": checkID() failed");
}
tmp = m_i2ControlCtx.readWordReg(TMP006_CONFIGURATION);
reg[0] = (tmp >> 8);
tmp = TMP006_CONFIG_CR(conv_rate);
reg[1] = (tmp >> 8);
tmp = ((uint16_t)reg[1] << 8) | reg[0];
ret = m_i2ControlCtx.writeWordReg(TMP006_CONFIGURATION, tmp);
if (ret != mraa::SUCCESS) {
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_i2c_write_word_data() failed");
}
}
int
TMP006::checkID(void)
{
uint8_t tmp[2];
uint16_t id;
int re = 0;
re = m_i2ControlCtx.readBytesReg(TMP006_DEVICE_ID_REG, tmp, 2);
if (re != 2) {
/* not enough bytes were read! */
return -1;
}
id = ((uint16_t)tmp[0] << 8) | tmp[1];
if (id != TMP006_DEVICE_ID) {
return -1;
}
return 0;
}
void
TMP006::resetSensor(void)
{
uint8_t reg[2];
uint16_t tmp = TMP006_RST_SOFT;
reg[0] = (tmp >> 8);
reg[1] = (tmp & 0x00ff);
tmp = ((uint16_t)reg[1] << 8) | reg[0];
mraa::Result ret = m_i2ControlCtx.writeWordReg(TMP006_CONFIGURATION, tmp);
if (ret != mraa::SUCCESS) {
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_i2c_write_word_data() failed");
}
}
void
TMP006::setActive(void)
{
uint8_t reg[2];
uint16_t tmp;
tmp = m_i2ControlCtx.readWordReg(TMP006_CONFIGURATION);
reg[0] = (tmp >> 8);
reg[1] = (tmp & 0x00ff);
reg[1] |= (TMP006_CONFIG_MOD(TMP006_CONFIG_MOD_CC) >> 8);
tmp = ((uint16_t)reg[0] << 8) | reg[1];
mraa::Result ret = m_i2ControlCtx.writeWordReg(TMP006_CONFIGURATION, tmp);
if (ret != mraa::SUCCESS) {
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_i2c_write_word_data() failed");
}
}
void
TMP006::setStandby(void)
{
uint8_t reg[2];
uint16_t tmp;
tmp = m_i2ControlCtx.readWordReg(TMP006_CONFIGURATION);
reg[0] = (tmp >> 8);
reg[1] = (tmp & 0x00ff);
reg[1] &= ~(TMP006_CONFIG_MOD(TMP006_CONFIG_MOD_CC) >> 8);
tmp = ((uint16_t)reg[0] << 8) | reg[1];
mraa::Result ret = m_i2ControlCtx.writeWordReg(TMP006_CONFIGURATION, tmp);
if (ret != mraa::SUCCESS) {
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_i2c_write_word_data() failed");
}
}
int
TMP006::sampleData(void)
{
uint8_t buf[2];
uint16_t rawVolt;
uint16_t rawTemp;
float tamb[1];
float tobj[1];
uint16_t tmp;
uint8_t drdy[1];
tmp = m_i2ControlCtx.readWordReg(TMP006_CONFIGURATION);
buf[0] = (tmp >> 8);
buf[1] = (tmp & 0x00ff);
*drdy = buf[0] & (TMP006_DRDY_DATA_RDY);
if(! (*drdy)) {
/* conversation in progress */
return -1;
}
tmp = m_i2ControlCtx.readWordReg(TMP006_SENSOR_VOLTAGE);
buf[0] = (tmp >> 8);
buf[1] = (tmp & 0x00ff);
rawVolt = ((uint16_t)buf[1] << 8) | buf[0];
tmp = m_i2ControlCtx.readWordReg(TMP006_LOCAL_TEMPERATURE);
buf[0] = (tmp >> 8);
buf[1] = (tmp & 0x00ff);
rawTemp = ((uint16_t)buf[1] << 8) | buf[0];
convert_data(rawVolt, rawTemp, tamb, tobj);
m_temperature = (uint16_t)*tobj;
return 0;
}
void
TMP006::convert_data(int16_t rawv,int16_t rawt, float *tamb, float *tobj)
{
/* calculate die temperature */
*tamb = (double)rawt / 128.0;
/* die temperature in Kelvin */
double tdie_k = *tamb + 273.15;
/* calculate sensor voltage */
double sens_v = (double)rawv * TMP006_CCONST_LSB_SIZE;
double tdiff = tdie_k - TMP006_CCONST_TREF;
double tdiff_pow2 = pow(tdiff, 2);
double s = TMP006_CCONST_S0 * (1 + TMP006_CCONST_A1 * tdiff
+ TMP006_CCONST_A2 * tdiff_pow2);
double v_os = TMP006_CCONST_B0 + TMP006_CCONST_B1 * tdiff
+ TMP006_CCONST_B2 * tdiff_pow2;
double f_obj = (sens_v-v_os) + TMP006_CCONST_C2 * pow((sens_v-v_os), 2);
double t = pow(pow(tdie_k, 4) + (f_obj / s), 0.25);
/* calculate object temperature in Celsius */
*tobj = (t - 273.15);
}
float
TMP006::getTemperature(int bSampleData)
{
if (bSampleData) {
if( (sampleData()) == -1) {
/* conversation in progress */
return (-273.2);
}
}
return (float) m_temperature;
}
uint16_t
TMP006::getConfig(void)
{
uint8_t buf[2];
uint16_t tmp;
tmp = m_i2ControlCtx.readWordReg(TMP006_CONFIGURATION);
buf[0] = (tmp >> 8);
buf[1] = (tmp & 0x00ff);
return ((uint16_t)buf[1] << 8) | buf[0];
}

202
src/tmp006/tmp006.hpp Normal file
View File

@ -0,0 +1,202 @@
/*
* Author: Norbert Wesp <nwesp@phytec.de>
* Copyright (c) 2017 Phytec Messtechnik GmbH.
*
* based on: RIOT-driver tmp006 by Johann Fischer <j.fischer@phytec.de>
*
* 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.
*/
#pragma once
#include <string>
#include <mraa/i2c.hpp>
#include <math.h>
#define TMP006_NAME "TMP006"
#define TMP006_I2C_ADDRESS 0x41
#define TMP006_MANUFACTURER_ID 0x5449
#define TMP006_DEVICE_ID 0x0067
#ifndef TMP006_CONVERSION_TIME
#define TMP006_CONVERSION_TIME 1E6 /**< Default Conversion Time in us */
#endif
/* TMP006 Register Map */
#define TMP006_SENSOR_VOLTAGE 0x00 /* Sensor voltage register */
#define TMP006_LOCAL_TEMPERATURE 0x01 /* Ambient temperature register */
#define TMP006_CONFIGURATION 0x02
#define TMP006_MANUFACTURER_ID_REG 0xFE
#define TMP006_DEVICE_ID_REG 0xFF
/* TMP006 configuration register bitmap */
#define TMP006_RST_SOFT (1 << 15)
#define TMP006_CONFIG_MOD_SHIFT 12
#define TMP006_CONFIG_MOD_MASK 0x7000
#define TMP006_CONFIG_MOD(x) (((uint16_t)(((uint16_t)(x))<<\
TMP006_CONFIG_MOD_SHIFT))\
&TMP006_CONFIG_MOD_MASK)
#define TMP006_CONFIG_MOD_CC 0x07
#define TMP006_CONFIG_MOD_OFF 0x00
#define TMP006_CONFIG_CR_SHIFT 9
#define TMP006_CONFIG_CR_MASK 0x0E00
#define TMP006_CONFIG_CR(x) (((uint16_t)(((uint16_t)(x))<<\
TMP006_CONFIG_CR_SHIFT))\
&TMP006_CONFIG_CR_MASK)
#define TMP006_CONFIG_CR_AS1 0x00 /* Conversion Time 0.25 s, AVG Samples: 1 */
#define TMP006_CONFIG_CR_AS2 0x01 /* Conversion Time 0.5 s, AVG Samples: 2 */
#define TMP006_CONFIG_CR_AS4 0x02 /* Conversion Time 1 s, AVG Samples: 4 */
#define TMP006_CONFIG_CR_AS8 0x03 /* Conversion Time 2 s, AVG Samples: 8 */
#define TMP006_CONFIG_CR_AS16 0x04 /* Conversion Time 4 s, AVG Samples: 16 */
#define TMP006_CONFIG_CR_DEF TMP006_CONFIG_CR_AS4 /**< Default for Testing */
#define TMP006_DRDY_EN (1 << 8)
#define TMP006_DRDY_DATA_RDY (1 << 7)
/* constant values for data conversion */
#ifndef TMP006_CCONST_S0
#define TMP006_CCONST_S0 6.4E-14 /* Calibration Factor */
#endif
#define TMP006_CCONST_A1 1.75E-3 /* Constant \f$a_{\mathrm{1}}\f$ */
#define TMP006_CCONST_A2 -1.678E-5 /* Constant \f$a_{\mathrm{2}}\f$ */
#define TMP006_CCONST_TREF 298.15 /* Constant \f$T_{\mathrm{REF}}\f$ */
#define TMP006_CCONST_B0 -2.94E-5 /* Constant \f$b_{\mathrm{0}}\f$ */
#define TMP006_CCONST_B1 -5.7E-7 /* Constant \f$b_{\mathrm{1}}\f$ */
#define TMP006_CCONST_B2 4.63E-9 /* Constant \f$b_{\mathrm{2}}\f$ */
#define TMP006_CCONST_C2 13.4 /* Constant \f$c_{\mathrm{2}}\f$ */
#define TMP006_CCONST_LSB_SIZE 156.25E-9 /* Sensor Voltage Register LSB Size */
namespace upm {
/**
* @brief TMP006 Infrared-Thermopile Sensor
* @defgroup tmp006 libupm-tmp006
* @ingroup ti i2c temp
*/
/**
* @library tmp006
* @sensor tmp006
* @comname TMP006 IR-Thermopile Sensor
* @type temp
* @man ti
* @web http://www.ti.com/product/TMP006/datasheet/abstract#SBOS5183423
* @con i2c
*
* @brief API for the TMP006 IR-Thermopile Sensor
*
* Description in web-link above:
* The TMP006 and TMP006B are fully integrated MEMs thermopile sensors that
* measure the temperature of an object without having to be in direct contact.
* The thermopile absorbs passive infrared energy from an object at wavelengths
* between 4 um to 16 um within the end-user defined field of view.
* The corresponding change in voltage across the thermopile is digitized and
* reported with the on-chip die thermal sensor measurement through an I2C- and
* SMBus-compatible interface. With this data, the target object temperature
* can be calculated by an external processor.
* The Infrared thermopile sensor is specified to operate from 40°C to +125°C.
* It is possible to measure an object temperature beyond the device operating
* range as long as the device itself does not exceed the operating temperature
* range (40°C to +125°C).
*
* @snippet tmp006.cxx Interesting
*/
class TMP006 {
public:
/**
*
* Instantiates an TMP006 object
*
* @param bus Number of the used bus
* @param conv_rate Conversation rate (allowed between 0 and 4)
* @param devAddr Address of the used I2C device
*/
TMP006 (int bus, uint8_t conv_rate=TMP006_CONFIG_CR_DEF,
int devAddr=TMP006_I2C_ADDRESS);
/**
* Check device_id of sensor
*
* @return 0 on succes
* -1 on error
*/
int checkID(void);
/**
* Performs a soft reset of the TMP006 device to ensure
* it is in a known state.
*/
void resetSensor(void);
/**
* Set active mode, this enables periodic measurements.
*/
void setActive(void);
/**
* Set standby mode, this disables periodic measurements.
*/
void setStandby(void);
/**
* Read out data of regs and call further function
* Also saves converted value to variable
*/
int sampleData(void);
/**
* Convert raw sensor values to temperature.
*
* @param rawv Object voltage value
* @param rawt Raw die temperature value
* @param tamp Converted ambient temperature
* @param tobj Converted object temperature
*/
void convert_data(int16_t rawv,int16_t rawt, float *tamb, float *tobj);
/**
* Get the calculated object temperature [degC]
*
* @param bSampleData Flag to read sensor
* @return The object temp in degC on success
* and -273.2 on 'conversation in progress'
*/
float getTemperature(int bSampleData = 0);
/**
* Get value of configuration reg
*/
uint16_t getConfig(void);
private:
std::string m_name;
int m_controlAddr;
int m_bus;
mraa::I2c m_i2ControlCtx;
int32_t m_temperature;
};
}