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

lego-energymeter.h

Go to the documentation of this file.
00001 /*!@addtogroup lego
00002  * @{
00003  * @defgroup legoem Energy Meter
00004  * Energy Meter
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: lego-energymeter.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 #ifndef __LEGOEM_DRIVER_H__
00013 #define __LEGOEM_DRIVER_H__
00014 
00015 /** \file lego-energymeter.h
00016  * \brief RobotC Energy Meter Driver
00017  *
00018  * lego-energymeter.h provides an API for the Lego Energy Meter.
00019  *
00020  * Changelog:
00021  * - 0.1: Initial release
00022  *
00023  * Credits :
00024  * - David Cosimano for sending me one of these.
00025  * - John Hansen for providing me with the specs.
00026  *
00027  * License: You may use this code as you wish, provided you give credit where its due.
00028  *
00029  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER. 
00030 
00031  * \author Xander Soldaat (mightor@gmail.com)
00032  * \date 22 August 2010
00033  * \version 0.1
00034  * \example lego-energymeter-test1.c
00035  */
00036 
00037 #pragma systemFile
00038 
00039 #ifndef __COMMON_H__
00040 #include "common.h"
00041 #endif
00042 
00043 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00044 /*
00045 <Address definitions>
00046 */
00047 #define LEGOEM_I2C_ADDR 0x04 /*!< Energy Meter I2C device address */
00048 #define LEGOEM_I2C_REG  0x0A /*!< Start of I2C registers that need to be read */
00049 #define LEGOEM_I2C_SIZE   14 /*!< Number of registers to read at once */
00050 
00051 
00052 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00053 // Function prototypes
00054 bool LEGOEMreadData(tSensors link, float &voltageIn, float &currentIn, float &voltageOut, float &currentOut, int &joule, float &wattIn, float &wattOut);
00055 
00056 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00057 // global variables
00058 tByteArray       LEGOEM_I2CRequest;    /*!< Array to hold I2C command data */
00059 tByteArray       LEGOEM_I2CReply;      /*!< Array to hold I2C reply data   */
00060 
00061 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00062 
00063 
00064 /**
00065  * Read a snapshot of the current register values.  They must all be read at once to
00066  * ensure data coherency.
00067  *
00068  * @param link the LEGO Energy Meter port number
00069  * @param voltageIn voltage level on input
00070  * @param currentIn current supplied on input
00071  * @param voltageOut voltage on output
00072  * @param currentOut current drawn on output
00073  * @param joule number of Joules stored on E-Meter
00074  * @param wattIn amount of Watts coming in
00075  * @param wattOut amount of Watts being consumed
00076  * @return true if no error occured, false if it did
00077  */
00078 bool LEGOEMreadData(tSensors link, float &voltageIn, float &currentIn, float &voltageOut, float &currentOut, int &joule, float &wattIn, float &wattOut) {
00079   memset(LEGOEM_I2CRequest, 0, sizeof(tByteArray));
00080 
00081   LEGOEM_I2CRequest[0] = 2;                // Message size
00082   LEGOEM_I2CRequest[1] = LEGOEM_I2C_ADDR;  // I2C Address
00083   LEGOEM_I2CRequest[2] = LEGOEM_I2C_REG;   // Value address
00084 
00085   if (!writeI2C(link, LEGOEM_I2CRequest, LEGOEM_I2CReply, LEGOEM_I2C_SIZE))
00086     return false;
00087 
00088   voltageIn  = (float)(LEGOEM_I2CReply[0]  + (LEGOEM_I2CReply[1]  << 8)) / 1000;
00089   currentIn  = (float)(LEGOEM_I2CReply[2]  + (LEGOEM_I2CReply[3]  << 8)) / 1000;
00090   voltageOut = (float)(LEGOEM_I2CReply[4]  + (LEGOEM_I2CReply[5]  << 8)) / 1000;
00091   currentOut = (float)(LEGOEM_I2CReply[6]  + (LEGOEM_I2CReply[7]  << 8)) / 1000;
00092   joule      =         LEGOEM_I2CReply[8]  + (LEGOEM_I2CReply[9]  << 8);
00093   wattIn     = (float)(LEGOEM_I2CReply[10] + (LEGOEM_I2CReply[11] << 8)) / 1000;
00094   wattOut    = (float)(LEGOEM_I2CReply[12] + (LEGOEM_I2CReply[13] << 8)) / 1000;
00095   return true;
00096 }
00097 
00098 #endif // __LEGOEM_DRIVER_H__
00099 
00100 /*
00101  * $Id: lego-energymeter.h 133 2013-03-10 15:15:38Z xander $
00102  */
00103 /* @} */
00104 /* @} */