Driver for HP20X sensor
Dependents: 8-0_OneNet_IoT_demo
Revision 0:2ee0bc82e7b8, committed 2017-03-29
- Comitter:
- TaylorGy
- Date:
- Wed Mar 29 03:09:38 2017 +0000
- Commit message:
- driver for HP20x sensor
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/driver_mbed_HP20x.cpp Wed Mar 29 03:09:38 2017 +0000 @@ -0,0 +1,266 @@ +/* + * File name : HP20x_dev.cpp + * Description: Driver for I2C PRECISION BAROMETER AND ALTIMETER [HP206C] + * Author : Oliver Wang from Seeed studio + * Version : V0.1 + * Create Time: 2014/04 + * Change Log : +*/ + +/****************************************************************************/ +/*** Include files ***/ +/****************************************************************************/ +#include "driver_mbed_HP20x.h" +// #include <Wire.h> +// #include <Arduino.h> +#include "driver_mbed_KalmanFilter.h" +/****************************************************************************/ +/*** Local Variable ***/ +/****************************************************************************/ +HP20x_dev HP20x; +I2C i2c_HP20x(PB_3, PB_10); + +/****************************************************************************/ +/*** Class member Functions ***/ +/****************************************************************************/ +/* + **@ Function name: HP20x_dev + **@ Description: Constructor + **@ Input: none + **@ OutPut: none + **@ Retval: none +*/ +HP20x_dev::HP20x_dev() +{ + OSR_CFG = HP20X_CONVERT_OSR1024; + OSR_ConvertTime = 25; +} + +/* + **@ Function name: begin + **@ Description: Initialize HP20x_dev + **@ Input: none + **@ OutPut: none + **@ Retval: none +*/ +void HP20x_dev::begin() +{ + // Wire.begin(); + /* Reset HP20x_dev */ + HP20x.HP20X_IIC_WriteCmd(HP20X_SOFT_RST); +} + +/* + **@ Function name: isAvailable + **@ Description: Indicate whether it's available + **@ Input: none + **@ OutPut: none + **@ Retval: uchar +*/ +uchar HP20x_dev::isAvailable() +{ + uchar ret = HP20x.HP20X_IIC_ReadReg(REG_PARA); + return ret; +} +/* + **@ Function name: ReadTemperature + **@ Description: Read Temperature from HP20x_dev + **@ Input: + **@ OutPut: + **@ Retval: +*/ +ulong HP20x_dev::ReadTemperature(void) +{ + + HP20X_IIC_WriteCmd(HP20X_WR_CONVERT_CMD|OSR_CFG); //ADC convert + + wait_ms(int(OSR_ConvertTime)); //difference OSR_CFG will be difference OSR_ConvertTime + HP20X_IIC_WriteCmd(HP20X_READ_T); + ulong Temperature = HP20X_IIC_ReadData(); + return Temperature; +} + +/* + **@ Function name: ReadPressure + **@ Description: Read Pressure value + **@ Input: + **@ OutPut: + **@ Retval: value +*/ + +ulong HP20x_dev::ReadPressure(void) +{ + HP20X_IIC_WriteCmd(HP20X_WR_CONVERT_CMD|OSR_CFG); + wait_ms(int(OSR_ConvertTime)); + HP20X_IIC_WriteCmd(HP20X_READ_P); + ulong Pressure = HP20X_IIC_ReadData(); + return Pressure; +} + +/* + **@ Function name: ReadAltitude + **@ Description: Read Pressure value + **@ Input: + **@ OutPut: + **@ Retval: value +*/ +ulong HP20x_dev::ReadAltitude(void) +{ + HP20X_IIC_WriteCmd(HP20X_READ_A); + ulong Altitude = HP20X_IIC_ReadData(); + return Altitude; +} + +/* +void ReadPressureAndTemperature(void) +{ + HP20X_IIC_WriteCmd(HP20X_WR_CONVERT_CMD|OSR_CFG); + Timer_Delayxms(OSR_ConvertTime*2); + HP20X_IIC_WriteCmd(HP20X_READ_PT); + + Temperature=HP20X_IIC_ReadData(); + + Pressure=HP20X_IIC_ReadData3byte(); +} + +void IIC_ReadAltitudeAndTemperature(void) +{ + + HP20X_IIC_WriteCmd(HP20X_WR_CONVERT_CMD|OSR_CFG); + Timer_Delayxms(OSR_ConvertTime*2); + HP20X_IIC_WriteCmd(HP20X_READ_AT); + + Temperature=HP20X_IIC_ReadData(); + IIC_ACK(); + Altitude=HP20X_IIC_ReadData3byte(); + IIC_NoAck(); + IIC_Stop(); + +}*/ +/****************************************************************************/ +/*** Local Functions ***/ +/****************************************************************************/ + +/* + **@ Function name: HP20X_IIC_WriteCmd + **@ Description: + **@ Input: + **@ OutPut: + **@ Retval: +*/ +void HP20x_dev::HP20X_IIC_WriteCmd(uchar uCmd) +{ + /* Port to arduino */ + // Wire.beginTransmission(HP20X_I2C_DEV_ID); + // Wire.write(uCmd); + // Wire.endTransmission(); + char cmd = uCmd; + i2c_HP20x.write(HP20X_I2C_DEV_ID, &cmd, 1); +} + +/* + **@ Function name: HP20X_IIC_ReadReg + **@ Description: + **@ Input: + **@ OutPut: + **@ Retval: +*/ +uchar HP20x_dev::HP20X_IIC_ReadReg(uchar bReg) +{ + /* Port to arduino */ + char Temp; + + /* Send a register reading command */ + HP20X_IIC_WriteCmd(bReg|HP20X_RD_REG_MODE); + + // Wire.requestFrom(HP20X_I2C_DEV_ID, 1); + // while(Wire.available()) + // { + // Temp = Wire.read(); + // } + i2c_HP20x.read(HP20X_I2C_DEV_ID, &Temp, 1); + + return Temp; +} +/* + **@ Function name: HP20X_IIC_WriteReg + **@ Description: + **@ Input: + **@ OutPut: + **@ Retval: +*/ +void HP20x_dev::HP20X_IIC_WriteReg(uchar bReg,uchar bData) +{ + // Wire.beginTransmission(HP20X_I2C_DEV_ID); + // Wire.write(bReg|HP20X_WR_REG_MODE); + // Wire.write(bData); + // Wire.endTransmission(); + char cmd[2]; + cmd[0] = bReg|HP20X_WR_REG_MODE; + cmd[1] = bData; + i2c_HP20x.write(HP20X_I2C_DEV_ID, cmd, 2); +} + + +/* + **@ Function name: HP20X_IIC_ReadData + **@ Description: + **@ Input: + **@ OutPut: + **@ Retval: +*/ +ulong HP20x_dev::HP20X_IIC_ReadData(void) +{ + /* Port to arduino */ + ulong Temp = HP20X_IIC_ReadData3byte(); + return Temp; +} + +/* + **@ Function name: HP20X_IIC_ReadData3byte + **@ Description: + **@ Input: + **@ OutPut: + **@ Retval: +*/ +ulong HP20x_dev::HP20X_IIC_ReadData3byte(void) +{ + ulong TempData = 0; + char tmpArray[3]; + + /* Require three bytes from slave */ + // Wire.requestFrom(HP20X_I2C_DEV_ID, 3); + + // while(Wire.available()) // slave may send less than requested + // { + // uchar c = Wire.read(); // receive a byte as character + // tmpArray[cnt] = (ulong)c; + // cnt++; + // } + i2c_HP20x.read(HP20X_I2C_DEV_ID, tmpArray, 3); + + /* MSB */ + TempData = tmpArray[0]<<16 | tmpArray[1]<<8 | tmpArray[2]; + + + if(TempData&0x800000) + { + TempData|=0xff000000; + } + + /* // 24 bit to 32 bit + if(TempData&0x800000) + { + // 1:minus + TempData |= 0x80000000; + TempData &= 0xff7fffff; + } + else + { + // 0:plus + //do noting + } */ + return TempData; +} + +/**************************************END OF FILE**************************************/ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/driver_mbed_HP20x.h Wed Mar 29 03:09:38 2017 +0000 @@ -0,0 +1,106 @@ +/* + * File name : HP20x_dev.h + * Description: Driver for I2C PRECISION BAROMETER AND ALTIMETER [HP206C] + * Author : Oliver Wang from Seeed studio + * Version : V0.1 + * Create Time: 2014/04 + * Change Log : +*/ +#ifndef _HP20X_DEV_H +#define _HP20X_DEV_H +/****************************************************************************/ +/*** Including Files ***/ +/****************************************************************************/ +// #include <Wire.h> +// #include <Arduino.h> +#include "mbed.h" +/****************************************************************************/ +/*** Macro Definitions ***/ +/****************************************************************************/ +typedef unsigned int uint; +typedef unsigned char uchar; +typedef unsigned long ulong; + +// #define HP20X_I2C_DEV_ID (0xEC)>>1 //CSB PIN is VDD level(address is 0x76) +// #define HP20X_I2C_DEV_ID2 (0XEE)>>1 //CSB PIN is GND level(address is 0x77) + +const int HP20X_I2C_DEV_ID = 0xEC; +const int HP20X_I2C_DEV_ID2 = 0xEE; + +#define HP20X_SOFT_RST 0x06 +#define HP20X_WR_CONVERT_CMD 0x40 +#define HP20X_CONVERT_OSR4096 0<<2 +#define HP20X_CONVERT_OSR2048 1<<2 +#define HP20X_CONVERT_OSR1024 2<<2 +#define HP20X_CONVERT_OSR512 3<<2 +#define HP20X_CONVERT_OSR256 4<<2 +#define HP20X_CONVERT_OSR128 5<<2 + +#define HP20X_READ_P 0x30 //read_p command +#define HP20X_READ_A 0x31 //read_a command +#define HP20X_READ_T 0x32 //read_t command +#define HP20X_READ_PT 0x10 //read_pt command +#define HP20X_READ_AT 0x11 //read_at command +#define HP20X_READ_CAL 0X28 //RE-CAL ANALOG + +#define HP20X_WR_REG_MODE 0xC0 +#define HP20X_RD_REG_MODE 0x80 + +#define ERR_WR_DEVID_NACK 0x01 +#define ERR_RD_DEVID_NACK 0x02 +#define ERR_WR_REGADD_NACK 0x04 +#define ERR_WR_REGCMD_NACK 0x08 +#define ERR_WR_DATA_NACK 0x10 +#define ERR_RD_DATA_MISMATCH 0x20 + +#define I2C_DID_WR_MASK 0xFE +#define I2C_DID_RD_MASK 0x01 + +#define T_WIN_EN 0X01 +#define PA_WIN_EN 0X02 +#define T_TRAV_EN 0X04 +#define PA_TRAV_EN 0X08 +#define PA_RDY_EN 0X20 +#define T_RDY_EN 0X10 + +#define T_WIN_CFG 0X01 +#define PA_WIN_CFG 0X02 +#define PA_MODE_P 0X00 +#define PA_MODE_A 0X40 + +#define T_TRAV_CFG 0X04 + +#define OK_HP20X_DEV 0X80 //HP20x_dev successfully initialized +#define REG_PARA 0X0F //Status register + +/****************************************************************************/ +/*** Class Definitions ***/ +/****************************************************************************/ +class HP20x_dev +{ + /* Public variables and functions */ + public: + uchar OSR_CFG; + uint OSR_ConvertTime; + /* Constructor */ + HP20x_dev(); + void begin(); + uchar isAvailable(); + + /* Read sensor data */ + ulong ReadTemperature(void); + ulong ReadPressure(void); + ulong ReadAltitude(void); + + /* Private variables and functions */ + private: + /* Write a command to HP20x */ + void HP20X_IIC_WriteCmd(uchar uCmd); + /* Read register value */ + uchar HP20X_IIC_ReadReg(uchar bReg); + void HP20X_IIC_WriteReg(uchar bReg,uchar bData); + ulong HP20X_IIC_ReadData(void); + ulong HP20X_IIC_ReadData3byte(void); +}; +extern HP20x_dev HP20x; +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/driver_mbed_KalmanFilter.cpp Wed Mar 29 03:09:38 2017 +0000 @@ -0,0 +1,93 @@ +/* + * File name : KalmanFilter.cpp + * Description: Kalman Filter class + * Author : Oliver Wang from Seeed studio + * Version : V0.1 + * Create Time: 2014/04 + * Change Log : +*/ + +/****************************************************************************/ +/*** Include files ***/ +/****************************************************************************/ +// #include <Arduino.h> +#include <driver_mbed_KalmanFilter.h> +// #include <inttypes.h> +#include <stdlib.h> +#include <stdio.h> + +AnalogIn ain(D0); +/* random number table */ +float Rand_Table[100]={ +0.5377,1.8339,-2.2588,0.8622,0.3188,-1.3077,-0.4336,0.342,3.5784, +2.7694,-1.3499,3.0349,0.7254,-0.0631,0.7147,-0.2050,-0.1241,1.4897, +1.4090,1.4172,0.6715,-1.2075,0.7172,1.6302,0.4889,1.0347,0.7269, +-0.3034,0.2939,-0.7873,0.8884,-1.1471,-1.0689,-0.8095,-2.9443,1.4384, +0.3252,-0.7549,1.3703,-1.7115,-0.1022,-0.2414,0.3192,0.3129,-0.8649, +-0.0301,-0.1649,0.6277,1.0933,1.1093,-0.8637,0.0774,-1.2141,-1.1135, +-0.0068,1.5326,-0.7697,0.3714,-0.2256,1.1174,-1.0891,0.0326,0.5525, +1.1006,1.5442,0.0859,-1.4916,-0.7423,-1.0616,2.3505,-0.6156,0.7481, +-0.1924,0.8886,-0.7648,-1.4023,-1.4224,0.4882,-0.1774,-0.1961,1.4193, +0.2916,0.1978,1.5877,-0.8045,0.6966,0.8351,-0.2437,0.2157,-1.1658, +-1.1480,0.1049,0.7223,2.5855,-0.6669,0.1873,-0.0825,-1.9330,-0.439, +-1.7947}; + +/* Extern variables */ +KalmanFilter kalmanFilter; + +KalmanFilter::KalmanFilter() +{ + X_pre = 0; + P_pre = 0; + X_post = 0; + P_post = 0; + K_cur = 0; +} + +float KalmanFilter::Gaussian_Noise_Cov(void) +{ + int index = 0; + float tmp[10]={0.0}; + float average = 0.0; + float sum = 0.0; + /* Initialize random number generator */ + srand((int)ain.read()); + /* Get random number */ + for(int i=0; i<10; i++) + { + index = (int)rand()%100; + tmp[i] = Rand_Table[index]; + sum += tmp[i]; + } + + /* Calculate average */ + average = sum/10; + + /* Calculate Variance */ + float Variance = 0.0; + for(int j = 0; j < 10; j++) + { + Variance += (tmp[j]-average)*(tmp[j]-average); + } + Variance/=10.0; + + return Variance; +} + +float KalmanFilter::Filter(float origin) +{ + float modelNoise = 0.0; + float observeNoise = 0.0; + + /* Get model and observe Noise */ + modelNoise = Gaussian_Noise_Cov(); + observeNoise = Gaussian_Noise_Cov(); + + /* Algorithm */ + X_pre = X_post; + P_pre = P_post + modelNoise; + K_cur = P_pre/(P_pre + observeNoise); + P_post = (1 - K_cur)*P_pre; + X_post = X_pre + K_cur*(origin - X_pre); + return X_post; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/driver_mbed_KalmanFilter.h Wed Mar 29 03:09:38 2017 +0000 @@ -0,0 +1,38 @@ +/* + * File name : kalmanFilter.h + * Description: + * Author : Oliver Wang from Seeed studio + * Version : V0.1 + * Create Time: 2014/04 + * Change Log : +*/ + +#ifndef _KALMANFILTER_H +#define _KALMANFILTER_H +/****************************************************************************/ +/*** Include files ***/ +/****************************************************************************/ +// #include <Arduino.h> +// #include <inttypes.h> +#include "mbed.h" +/****************************************************************************/ +/*** Local variables ***/ +/****************************************************************************/ + + +/****************************************************************************/ +/*** Class Definitions ***/ +/****************************************************************************/ +class KalmanFilter +{ + public: + KalmanFilter(); + float Filter(float); + private: + /* variables */ + float X_pre, X_post, P_pre, P_post, K_cur; + float Gaussian_Noise_Cov(void); + +}; +extern KalmanFilter kalmanFilter; +#endif \ No newline at end of file