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