Mindstorms 3rd Party ROBOTC Drivers RobotC
[Home] [Download] [Submit a bug/suggestion] [ROBOTC Forums] [Blog] [Support this project]

hitechnic-barometer.h

Go to the documentation of this file.
00001 /*!@addtogroup HiTechnic
00002  * @{
00003  * @defgroup HTBM Barometric Sensor
00004  * HiTechnic Barometric Sensor
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: hitechnic-barometer.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 #ifndef __HTBM_H__
00013 #define __HTBM_H__
00014 /** \file hitechnic-barometer.h
00015  * \brief HiTechnic Barometric Sensor driver
00016  *
00017  * hitechnic-barometer.h provides an API for the HiTechnic Barometric Sensor.\n
00018  * Pressure values are between 0 and approx 31 inHG.\n
00019  * Temperature values are between -25C and +85C.
00020  *
00021  * Changelog:
00022  * - 0.1: Initial release
00023  * - 0.2: Replaced array structs with typedefs
00024  *
00025  * Credits:
00026  * - Big thanks to HiTechnic for providing me with the hardware necessary to write and test this.
00027  *
00028  * License: You may use this code as you wish, provided you give credit where its due.
00029  *
00030  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER. 
00031 
00032  * \author Xander Soldaat (xander_at_botbench.com)
00033  * \date 20 February 2011
00034  * \version 0.2
00035  * \example hitechnic-barometer-test1.c
00036  */
00037 
00038 #pragma systemFile
00039 
00040 #ifndef __COMMON_H__
00041 #include "common.h"
00042 #endif
00043 
00044 #define HTBM_I2C_ADDR  0x02      /*!< Barometric sensor device address */
00045 #define HTBM_OFFSET    0x42      /*!< Offset for data registers */
00046 #define HTBM_TEMP_HIGH 0x00      /*!< Temperature high byte */
00047 #define HTBM_TEMP_LOW  0x01      /*!< Temperature low byte */
00048 #define HTBM_PRES_HIGH 0x02      /*!< Pressure high byte */
00049 #define HTBM_PRES_LOW  0x03      /*!< Pressure low byte */
00050 
00051 const float mInHgtohPa = 0.03386389;
00052 const float mInHgtoPsi = 0.491098/1000;
00053 
00054 long HTBMreadMInHg(tSensors link);
00055 float HTBMreadhPa(tSensors link);
00056 float HTBMreadPsi(tSensors link);
00057 float HTBMreadTemp(tSensors link);
00058 float HTBMreadTempF(tSensors link);
00059 
00060 tByteArray HTBM_I2CRequest;    /*!< Array to hold I2C command data */
00061 tByteArray HTBM_I2CReply;      /*!< Array to hold I2C reply data */
00062 
00063 /**
00064  * Read the current atmospheric pressure in 1/1000th inch Hg (mercury)
00065  * @param link the HTBM port number
00066  * @return pressure in 1/1000th inch Hg (mercury), -255 if something went wrong
00067  */
00068 long HTBMreadMInHg(tSensors link) {
00069   memset(HTBM_I2CRequest, 0, sizeof(tByteArray));
00070 
00071   HTBM_I2CRequest[0] = 2;                       // Message size
00072   HTBM_I2CRequest[1] = HTBM_I2C_ADDR;           // I2C Address
00073   HTBM_I2CRequest[2] = HTBM_OFFSET + HTBM_PRES_HIGH; // Pressure high byte
00074 
00075   if (!writeI2C(link, HTBM_I2CRequest, HTBM_I2CReply, 2))
00076     return -255;
00077 
00078   return (HTBM_I2CReply[0] <<  8) + HTBM_I2CReply[1];
00079 }
00080 
00081 
00082 /**
00083  * Read the current atmospheric pressure in hecto Pascal
00084  * @param link the HTBM port number
00085  * @return pressure in hecto Pascal, -255 if something went wrong
00086  */
00087 float HTBMreadhPa(tSensors link) {
00088   return HTBMreadMInHg(link) * mInHgtohPa;
00089 }
00090 
00091 
00092 /**
00093  * Read the current atmospheric pressure in pounds per square inch
00094  * @param link the HTBM port number
00095  * @return pressure in pounds per square inch, -255 if something went wrong
00096  */
00097 float HTBMreadPsi(tSensors link) {
00098   return (float)HTBMreadMInHg(link) * mInHgtoPsi;
00099 }
00100 
00101 
00102 /**
00103  * Read the current air temperature in degrees Celcius
00104  * @param link the HTBM port number
00105  * @return current air temperature in degrees Celcius, -255 if something went wrong
00106  */
00107 float HTBMreadTemp(tSensors link) {
00108   float temp = 0.0;
00109   memset(HTBM_I2CRequest, 0, sizeof(tByteArray));
00110 
00111   HTBM_I2CRequest[0] = 2;                       // Message size
00112   HTBM_I2CRequest[1] = HTBM_I2C_ADDR;           // I2C Address
00113   HTBM_I2CRequest[2] = HTBM_OFFSET + HTBM_TEMP_HIGH; // Pressure high byte
00114 
00115   // Send the request
00116   if (!writeI2C(link, HTBM_I2CRequest, HTBM_I2CReply, 2))
00117     return -255;
00118 
00119   temp = (HTBM_I2CReply[0] <<  8) + HTBM_I2CReply[1];
00120   temp /= 10;
00121   return temp;
00122 }
00123 
00124 
00125 /**
00126  * Read the current air temperature in degrees Fahrenheit
00127  * @param link the HTBM port number
00128  * @return the current air temperature in degrees Fahrenheit, -255 if something went wrong
00129  */
00130 float HTBMreadTempF(tSensors link) {
00131   return (HTBMreadTemp(link) * 1.8) + 32;
00132 }
00133 
00134 
00135 #endif // __HTBM_H__
00136 
00137 /*
00138  * $Id: hitechnic-barometer.h 133 2013-03-10 15:15:38Z xander $
00139  */
00140 /* @} */
00141 /* @} */