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

MSPM-driver.h

Go to the documentation of this file.
00001 /*!@addtogroup mindsensors
00002  * @{
00003  * @defgroup mspm Power Meter Sensor
00004  * Power Meter Sensor
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: MSPM-driver.h 56 2011-05-30 19:47:12Z xander $
00010  */
00011 
00012 #ifndef __MSPM_H__
00013 #define __MSPM_H__
00014 
00015 /** \file MSPM-driver.h
00016  * \brief Mindsensors Power Meter Sensor
00017  *
00018  * MSPM-driver.h provides an API for the Mindsensors Power Meter Sensor.
00019  *
00020  * Changelog:
00021  *  - 0.1 Initial       release.
00022  *
00023  * Credits:
00024  * - Big thanks to Mindsensors for providing me with the hardware necessary to write and test this.
00025  *
00026  * License: You may use this code as you wish, provided you give credit where it's due.
00027  *
00028  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 2.00 AND HIGHER.
00029  * \author Xander Soldaat
00030  * \date 16 December 2009
00031  * \version 0.1
00032  * \example MSPM-test1.c
00033  */
00034 
00035 #pragma systemFile
00036 
00037 #ifndef __COMMON_H__
00038         #include "common.h"
00039 #endif
00040 
00041 //*******************************************************************************
00042 // REGISTER LOCATIONS AND COMMANDS for the Power Meter sensor
00043 //*******************************************************************************
00044 #define MSPM_I2C_ADDR     0x12  /*!< I2C address used by the PM */
00045 #define MSPM_CMD_REG            0x41  /*!< Register used for issuing commands */
00046 
00047 #define MSPM_PCURRENT     0x42  /*!< Present current reading in mA - 2 bytes */
00048 #define MSPM_PVOLTAGE           0x44  /*!< Present current reading in mV - 2 bytes */
00049 #define MSPM_CAPUSED                    0x46  /*!< Capacity used since last reset in mAh - 2 bytes */
00050 #define MSPM_TIME         0x56  /*!< Time since last reset  in ms - 4 bytes (long) */
00051 
00052 tByteArray MSPM_I2CRequest;       /*!< Array to hold I2C command data */
00053 tByteArray MSPM_I2CReply;         /*!< Array to hold I2C reply data */
00054 
00055 
00056 //*******************************************************************************
00057 // PUBLIC Power Meter functions
00058 //*******************************************************************************
00059 int MSPMreadCurrent(tSensors link, ubyte address = MSPM_I2C_ADDR);
00060 int MSPMreadVoltage(tSensors link, ubyte address = MSPM_I2C_ADDR);
00061 bool MSPMreadVoltageCurrent(tSensors link, int &voltage, int &current, ubyte address = MSPM_I2C_ADDR);
00062 long MSPMreadTime(tSensors link, ubyte address = MSPM_I2C_ADDR);
00063 bool MSPMresetCounters(tSensors link, ubyte address = MSPM_I2C_ADDR);
00064 
00065 //*******************************************************************************
00066 // INTERNAL USE ONLY - used by the above
00067 //*******************************************************************************
00068 bool _MSPMsendCommand(tSensors link, byte command, ubyte address);
00069 
00070 
00071 /**
00072  * This function sends a command to the Power Meter.
00073  *
00074  * Note: this is an internal function and should not be called directly.
00075  * @param link the sensor port number
00076  * @param command the command to be sent
00077  * @param address the I2C address to use, optional, defaults to 0x12
00078  * @return true if no error occured, false if it did
00079  */
00080 bool _MSPMsendCommand(tSensors link, byte command, ubyte address) {
00081   MSPM_I2CRequest[0] = 3;               // Message size
00082   MSPM_I2CRequest[1] = address;         // I2C Address
00083   MSPM_I2CRequest[2] = MSPM_CMD_REG;    // Register used for issuing commands
00084   MSPM_I2CRequest[3] = command;         // Command to be executed
00085 
00086   return writeI2C(link, MSPM_I2CRequest, 0);
00087 }
00088 
00089 
00090 /**
00091  * Return the present current measured.
00092  * @param link the MSPM port number
00093  * @param address the I2C address to use, optional, defaults to 0x12
00094  * @return the present current measured or -1 if an error occurred.
00095  */
00096 int MSPMreadCurrent(tSensors link, ubyte address) {
00097   memset(MSPM_I2CRequest, 0, sizeof(tByteArray));
00098 
00099   MSPM_I2CRequest[0] = 2;             // Message size
00100   MSPM_I2CRequest[1] = address;       // I2C Address
00101   MSPM_I2CRequest[2] = MSPM_PCURRENT; // Present current register
00102 
00103   if (!writeI2C(link, MSPM_I2CRequest, 2))
00104     return -1;
00105 
00106   if (!readI2C(link, MSPM_I2CReply, 2))
00107     return -1;
00108 
00109   return (MSPM_I2CReply[0] + (MSPM_I2CReply[1]<<8));
00110 }
00111 
00112 
00113 /**
00114  * Return the present voltage measured.
00115  * @param link the MSPM port number
00116  * @param address the I2C address to use, optional, defaults to 0x12
00117  * @return the present voltage measured or -1 if an error occurred.
00118  */
00119 int MSPMreadVoltage(tSensors link, ubyte address) {
00120   memset(MSPM_I2CRequest, 0, sizeof(tByteArray));
00121 
00122   MSPM_I2CRequest[0] = 2;             // Message size
00123   MSPM_I2CRequest[1] = address;       // I2C Address
00124   MSPM_I2CRequest[2] = MSPM_PVOLTAGE; // Present voltage register
00125 
00126   if (!writeI2C(link, MSPM_I2CRequest, 2))
00127     return -1;
00128 
00129   if (!readI2C(link, MSPM_I2CReply, 2))
00130     return -1;
00131 
00132   return (MSPM_I2CReply[0] + (MSPM_I2CReply[1]<<8));
00133 }
00134 
00135 
00136 /**
00137  * Return the present voltage and current measured.  This is a much more
00138  * efficient method of fetching both.
00139  * @param link the MSPM port number
00140  * @param voltage present voltage being measured
00141  * @param current present current being measured
00142  * @param address the I2C address to use, optional, defaults to 0x12
00143  * @return the present voltage measured or -1 if an error occurred.
00144  */
00145 bool MSPMreadVoltageCurrent(tSensors link, int &voltage, int &current, ubyte address) {
00146   memset(MSPM_I2CRequest, 0, sizeof(tByteArray));
00147 
00148   MSPM_I2CRequest[0] = 2;             // Message size
00149   MSPM_I2CRequest[1] = address;       // I2C Address
00150   MSPM_I2CRequest[2] = MSPM_PCURRENT; // Present current register
00151 
00152   if (!writeI2C(link, MSPM_I2CRequest, 4))
00153     return false;
00154 
00155   if (!readI2C(link, MSPM_I2CReply, 4))
00156     return false;
00157 
00158   current = MSPM_I2CReply[0] + (MSPM_I2CReply[1]<<8);
00159   voltage = MSPM_I2CReply[2] + (MSPM_I2CReply[3]<<8);
00160   return true;
00161 }
00162 
00163 /**
00164  * Return the time elapsed in ms since the last reset.
00165  * @param link the MSPM port number
00166  * @param address the I2C address to use, optional, defaults to 0x12
00167  * @return the time elapsed in ms since the last reset.
00168  */
00169 long MSPMreadTime(tSensors link, ubyte address) {
00170   memset(MSPM_I2CRequest, 0, sizeof(tByteArray));
00171 
00172   MSPM_I2CRequest[0] = 2;             // Message size
00173   MSPM_I2CRequest[1] = address;       // I2C Address
00174   MSPM_I2CRequest[2] = MSPM_TIME;     // Present voltage register
00175 
00176   if (!writeI2C(link, MSPM_I2CRequest, 4))
00177     return -1;
00178 
00179   if (!readI2C(link, MSPM_I2CReply, 4))
00180     return -1;
00181 
00182   return (((long)MSPM_I2CReply[3] << 24) + ((long)MSPM_I2CReply[2] << 16) + ((long)MSPM_I2CReply[1] <<  8) + (long)MSPM_I2CReply[0]);
00183   // return uByteToLong(MSPM_I2CReply[3], MSPM_I2CReply[2], MSPM_I2CReply[1], MSPM_I2CReply[0]);
00184 }
00185 
00186 
00187 /**
00188  * Reset all the counters.
00189  * @param link the MSPM port number
00190  * @param address the I2C address to use, optional, defaults to 0x12
00191  * @return true if no error occured, false if it did
00192  */
00193 bool MSPMresetCounters(tSensors link, ubyte address) {
00194   return _MSPMsendCommand(link, 'R', address);
00195 }
00196 
00197 
00198 #endif // __MSLL_H__
00199 
00200 /*
00201  * $Id: MSPM-driver.h 56 2011-05-30 19:47:12Z xander $
00202  */
00203 /* @} */
00204 /* @} */