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

mindsensors-pressure.h

Go to the documentation of this file.
00001 /*!@addtogroup mindsensors
00002  * @{
00003  * @defgroup ppsv3 Pressure Sensor V3
00004  * PPS-v3 Sensor
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: mindsensors-pressure.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 #ifndef __MSPPS_H__
00013 #define __MSPPS_H__
00014 /** \file mindsensors-pressure.h
00015  * \brief Mindsensors PPS-v3 driver
00016  *
00017  * mindsensors-pressure.h provides an API for the Mindsensors PPS-v3 Pressure Sensor driver
00018  *
00019  * Changelog:
00020  * - 0.1: Initial release
00021  *
00022  * Credits:
00023  * - Big thanks to Mindsensors for providing me with the hardware necessary to write and test this.
00024  *
00025  * License: You may use this code as you wish, provided you give credit where its due.
00026  *
00027  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER. 
00028 
00029  * \author Xander Soldaat (xander_at_botbench.com)
00030  * \date 01 January 2012
00031  * \version 0.1
00032  * \example mindsensors-pressure-test1.c
00033  */
00034 
00035 #pragma systemFile
00036 
00037 #ifndef __COMMON_H__
00038 #include "common.h"
00039 #endif
00040 
00041 #define MSPPS_I2C_ADDR       0x18  /*!< MSPPS I2C device address */
00042 
00043 #define MSPPS_CMD            0x41  /*!< MSPPS command register */
00044 
00045 #define MSPPS_UNIT           0x42  /*!< Configure measurement unit */
00046 #define MSPPS_PRESS_ABS      0x43  /*!< Read the absolute pressure */
00047 #define MSPPS_PRESS_GAUGE    0x45  /*!< Read the gauge pressure */
00048 #define MSPPS_PRESS_REF      0x47  /*!< Read or write the reference pressure */
00049 
00050 #define MSPPS_UNIT_PSI       0x50   /*!< PSI unit */
00051 #define MSPPS_UNIT_MB        0x62   /*!< Millibar unit */
00052 #define MSPPS_UNIT_KPA       0x6B   /*!< Kilo Pascal unit */
00053 
00054 #define MSPSS_CMD_SETREF     0x44    /*!< Set reference pressure to current absolute pressure */
00055 
00056 // Prototypes
00057 bool MSPPSsendCmd(tSensors link, ubyte command);
00058 bool MSPPSsetUnit(tSensors link, ubyte unit);
00059 long MSPPSreadPressure(tSensors link, ubyte reg);
00060 long MSPPSreadAbsPressure(tSensors link);
00061 long MSPPSreadGaugePressure(tSensors link);
00062 long MSPPSreadRefPressure(tSensors link);
00063 bool MSPPSsetRefPressure(tSensors link);
00064 bool MSPPSsetRefPressure(tSensors link, int refpressure);
00065 
00066 // Handy defines to shortcut some actions
00067 #define MSPPSsetUnitPSI(X)  MSPPSsetUnit(X, MSPPS_UNIT_PSI)
00068 #define MSPPSsetUnitmB(X)   MSPPSsetUnit(X, MSPPS_UNIT_MB)
00069 #define MSPPSsetUnitkPa(X)  MSPPSsetUnit(X, MSPPS_UNIT_KPA)
00070 
00071 tByteArray MSPPS_I2CRequest;       /*!< Array to hold I2C command data */
00072 tByteArray MSPPS_I2CReply;         /*!< Array to hold I2C reply data */
00073 
00074 
00075 /**
00076  * Read the pressure from the sensor and return it in the
00077  * unit the sensor has been configured for.<br>
00078  * It is better to use MSPPSreadAbsPressure(), MSPPSreadGaugePressure()
00079  * or MSPPSreadRefPressure() instead
00080  * @param link the sensor port number
00081  * @param reg Specific pressure register to read.
00082  * @return the absolute pressure.
00083  */
00084 long MSPPSreadPressure(tSensors link, ubyte reg)
00085 {
00086   memset(MSPPS_I2CRequest, 0, sizeof(tByteArray));
00087 
00088   MSPPS_I2CRequest[0] = 2;               // Number of bytes in I2C command
00089   MSPPS_I2CRequest[1] = MSPPS_I2C_ADDR;   // I2C address of accel sensor
00090   MSPPS_I2CRequest[2] = reg;     // Set write address to sensor mode register
00091 
00092   if (!writeI2C(link, MSPPS_I2CRequest, MSPPS_I2CReply, 2))
00093     return 0;
00094 
00095   return ((long)MSPPS_I2CReply[1] * 256) + MSPPS_I2CReply[0];
00096 }
00097 
00098 
00099 /**
00100  * Read the absolute pressure from the sensor and return it in the
00101  * unit the sensor has been configured for.
00102  * @param link the sensor port number
00103  * @return the absolute pressure.
00104  */
00105 long MSPPSreadAbsPressure(tSensors link)
00106 {
00107   return MSPPSreadPressure(link, MSPPS_PRESS_ABS);
00108 }
00109 
00110 
00111 /**
00112  * Read the gauge pressure from the sensor and return it in the
00113  * unit the sensor has been configured for.  This is the absolute
00114  * pressure minus the reference pressure.
00115  * @param link the sensor port number
00116  * @return the gauge pressure.
00117  */
00118 long MSPPSreadGaugePressure(tSensors link)
00119 {
00120   return MSPPSreadPressure(link, MSPPS_PRESS_GAUGE);
00121 }
00122 
00123 
00124 /**
00125  * Read the reference pressure from the sensor and return it in the
00126  * unit specified.
00127  * @param link the sensor port number
00128  * @return the reference pressure.
00129  */
00130 long MSPPSreadRefPressure(tSensors link)
00131 {
00132   return MSPPSreadPressure(link, MSPPS_PRESS_REF);
00133 }
00134 
00135 
00136 /**
00137  * Set the reference pressure to the value specified.
00138  * @param link the sensor port number
00139  * @param refpressure the value the ref pressure register should be set to.
00140  * @return true if no error occured, false if it did
00141  */
00142 bool MSPPSsetRefPressure(tSensors link, int refpressure)
00143 {
00144   memset(MSPPS_I2CRequest, 0, sizeof(tByteArray));
00145 
00146   MSPPS_I2CRequest[0] = 4;                         // Number of bytes in I2C command
00147   MSPPS_I2CRequest[1] = MSPPS_I2C_ADDR;            // I2C address of accel sensor
00148   MSPPS_I2CRequest[2] = MSPPS_PRESS_REF;           // Set write address to sensor mode register
00149   MSPPS_I2CRequest[3] = refpressure & 0xFF;         // Command to be sent to the sensor
00150   MSPPS_I2CRequest[4] = (refpressure << 8) & 0xFF;  // Command to be sent to the sensor
00151 
00152   return writeI2C(link, MSPPS_I2CRequest);
00153 }
00154 
00155 
00156 /**
00157  * Set the reference pressure to the current absolute pressure value.
00158  * @param link the sensor port number
00159  * @return true if no error occured, false if it did
00160  */
00161 bool MSPPSsetRefPressure(tSensors link)
00162 {
00163   return MSPPSsendCmd(link, MSPSS_CMD_SETREF);
00164 }
00165 
00166 
00167 /**
00168  * Send a command to the sensor
00169  * @param link the sensor port number
00170  * @param command the command to be sent
00171  * @return true if no error occured, false if it did
00172  */
00173 bool MSPPSsendCmd(tSensors link, ubyte command) {
00174   memset(MSPPS_I2CRequest, 0, sizeof(tByteArray));
00175 
00176   MSPPS_I2CRequest[0] = 3;               // Number of bytes in I2C command
00177   MSPPS_I2CRequest[1] = MSPPS_I2C_ADDR;   // I2C address of accel sensor
00178   MSPPS_I2CRequest[2] = MSPPS_CMD;        // Set write address to sensor mode register
00179   MSPPS_I2CRequest[3] = command;         // Command to be sent to the sensor
00180 
00181   return writeI2C(link, MSPPS_I2CRequest);
00182 }
00183 
00184 
00185 /**
00186  * Set the unit of measurement to the one specified.
00187  * @param link the sensor port number
00188  * @param unit the unit of measurement to be used.
00189  * @return true if no error occured, false if it did
00190  */
00191 bool MSPPSsetUnit(tSensors link, ubyte unit) {
00192   memset(MSPPS_I2CRequest, 0, sizeof(tByteArray));
00193 
00194   MSPPS_I2CRequest[0] = 3;               // Number of bytes in I2C command
00195   MSPPS_I2CRequest[1] = MSPPS_I2C_ADDR;   // I2C address of accel sensor
00196   MSPPS_I2CRequest[2] = MSPPS_UNIT;        // Set write address to sensor mode register
00197   MSPPS_I2CRequest[3] = unit;         // Command to be sent to the sensor
00198 
00199   return writeI2C(link, MSPPS_I2CRequest);
00200 }
00201 
00202 #endif //__MSPPS_H__
00203 
00204 /*
00205  * $Id: mindsensors-pressure.h 133 2013-03-10 15:15:38Z xander $
00206  */
00207 /* @} */
00208 /* @} */