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

LEGOEM-driver.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: LEGOEM-driver.h 46 2011-01-16 16:56:49Z xander $
00010  */
00011 
00012 #ifndef __LEGOEM_DRIVER_H__
00013 #define __LEGOEM_DRIVER_H__
00014 
00015 /** \file LEGOEM-driver.h
00016  * \brief RobotC Energy Meter Driver
00017  *
00018  * LEGOEM-driver.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 2.00 AND HIGHER.
00030  * \author Xander Soldaat (mightor@gmail.com)
00031  * \date 22 August 2010
00032  * \version 0.1
00033  * \example LEGOEM-test1.c
00034  */
00035 
00036 #pragma systemFile
00037 
00038 #ifndef __COMMON_H__
00039 #include "common.h"
00040 #endif
00041 
00042 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00043 /*
00044 <Address definitions>
00045 */
00046 #define LEGOEM_I2C_ADDR 0x04 /*!< Energy Meter I2C device address */
00047 #define LEGOEM_I2C_REG  0x0A /*!< Start of I2C registers that need to be read */
00048 #define LEGOEM_I2C_SIZE   14 /*!< Number of registers to read at once */
00049 
00050 
00051 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00052 // Function prototypes
00053 bool LEGOEMreadData(tSensors link, float &voltageIn, float &currentIn, float &voltageOut, float &currentOut, int &joule, float &wattIn, float &wattOut);
00054 
00055 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00056 // global variables
00057 tByteArray       LEGOEM_I2CRequest;    /*!< Array to hold I2C command data */
00058 tByteArray       LEGOEM_I2CReply;      /*!< Array to hold I2C reply data   */
00059 
00060 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00061 
00062 
00063 /**
00064  * Read a snapshot of the current register values.  They must all be read at once to
00065  * ensure data coherency.
00066  *
00067  * @param link the LEGO Energy Meter port number
00068  * @param voltageIn voltage level on input
00069  * @param currentIn current supplied on input
00070  * @param voltageOut voltage on output
00071  * @param currentOut current drawn on output
00072  * @param joule number of Joules stored on E-Meter
00073  * @param wattIn amount of Watts coming in
00074  * @param wattOut amount of Watts being consumed
00075  * @return true if no error occured, false if it did
00076  */
00077 bool LEGOEMreadData(tSensors link, float &voltageIn, float &currentIn, float &voltageOut, float &currentOut, int &joule, float &wattIn, float &wattOut) {
00078   memset(LEGOEM_I2CRequest, 0, sizeof(tByteArray));
00079 
00080   LEGOEM_I2CRequest[0] = 2;                // Message size
00081   LEGOEM_I2CRequest[1] = LEGOEM_I2C_ADDR;  // I2C Address
00082   LEGOEM_I2CRequest[2] = LEGOEM_I2C_REG;   // Value address
00083 
00084   if (!writeI2C(link, LEGOEM_I2CRequest, LEGOEM_I2C_SIZE))
00085     return false;
00086 
00087   if (!readI2C(link, LEGOEM_I2CReply, LEGOEM_I2C_SIZE))
00088     return false;
00089 
00090   voltageIn  = (float)(LEGOEM_I2CReply[0]  + (LEGOEM_I2CReply[1]  << 8)) / 1000;
00091   currentIn  = (float)(LEGOEM_I2CReply[2]  + (LEGOEM_I2CReply[3]  << 8)) / 1000;
00092   voltageOut = (float)(LEGOEM_I2CReply[4]  + (LEGOEM_I2CReply[5]  << 8)) / 1000;
00093   currentOut = (float)(LEGOEM_I2CReply[6]  + (LEGOEM_I2CReply[7]  << 8)) / 1000;
00094   joule      =         LEGOEM_I2CReply[8]  + (LEGOEM_I2CReply[9]  << 8);
00095   wattIn     = (float)(LEGOEM_I2CReply[10] + (LEGOEM_I2CReply[11] << 8)) / 1000;
00096   wattOut    = (float)(LEGOEM_I2CReply[12] + (LEGOEM_I2CReply[13] << 8)) / 1000;
00097   return true;
00098 }
00099 
00100 #endif // __LEGOEM_DRIVER_H__
00101 
00102 /*
00103  * $Id: LEGOEM-driver.h 46 2011-01-16 16:56:49Z xander $
00104  */
00105 /* @} */
00106 /* @} */