Obviously, for a solar power system, I need to measure negative currents (current goes both out of and into the battery- when charging). I ran across this problem a couple of months ago and figured it out. I was using the Subfact Python library for the INA219.
There is also a similar bug in the subfact python library as in the Adafruit C++ library for the INA219 (fixed as of November 2013). They aren't calculating twos complement correctly, so negative currents aren't reported correctly.
The ProjectCuracao unit is plugged in at moment to a USB charger, hence the funny looking efficiency numbers. On my RasPiConnect control screen, you can clearly see the -6ma heading into the battery. Special thanks to "faraday" for finding the same issue and contributing to the fix.
def twosToInt( val, len):
# Convert twos compliment to integer
if(val & (1 << len - 1)):
val = val - (1<<len)
return val
Changes to subfact python library:
def getShuntVoltage_raw(self):
result = self.i2c.readList(self.__INA219_REG_SHUNTVOLTAGE,2)
if (result[0] >> 7 == 1):
testint = (result[0]*256 + result[1])
othernew = twosToInt( testint, 16)
return othernew
else:
return (result[0] << 8) | (result[1])
def getCurrent_raw(self):
result = self.i2c.readList(self.__INA219_REG_CURRENT,2)
if (result[0] >> 7 == 1):
testint = (result[0]*256 + result[1])
othernew = twosToInt( testint, 16)
return othernew
else:
return (result[0] << 8) | (result[1])
def getPower_raw(self):
result = self.i2c.readList(self.__INA219_REG_POWER,2)
if (result[0] >> 7 == 1):
testint = (result[0]*256 + result[1])
othernew = twosToInt( testint, 16)
return othernew
else:
return (result[0] << 8) | (result[1])
No comments:
Post a Comment