From d6494e6f773579ad84dda392c82a11fb00bed8b8 Mon Sep 17 00:00:00 2001 From: Hiroyuki Matsuo Date: Wed, 16 Nov 2016 17:21:18 +0900 Subject: [PATCH] Update Subfact_ina219.py to read current safely MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Accorindg to the official Arduino library (https://github.com/adafruit/Adafruit_INA219): (In Adafruit_INA219.cpp, line 406-409) Sometimes a sharp load will reset the INA219, which will reset the cal register, meaning CURRENT and POWER will not be available ... avoid this by always setting a cal value even if it's an unfortunate extra step So, I add the re-calibration code into Subfact_ina219.py --- Subfact_ina219.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Subfact_ina219.py b/Subfact_ina219.py index b95fbf7..8ed42ba 100644 --- a/Subfact_ina219.py +++ b/Subfact_ina219.py @@ -111,11 +111,12 @@ def twosToInt(self, val, len): return val def ina219SetCalibration_32V_2A(self): + self.ina219_calValue = 4096 self.ina219_currentDivider_mA = 10 # Current LSB = 100uA per bit (1000/100 = 10) self.ina219_powerDivider_mW = 2 # Power LSB = 1mW per bit (2/1) - + # Set Calibration register to 'Cal' calculated above - bytes = [(0x1000 >> 8) & 0xFF, 0x1000 & 0xFF] + bytes = [(self.ina219_calValue >> 8) & 0xFF, self.ina219_calValue & 0xFF] self.i2c.writeList(self.__INA219_REG_CALIBRATION, bytes) # Set Config register to take into account the settings above @@ -144,6 +145,13 @@ def getShuntVoltage_raw(self): return (result[0] << 8) | (result[1]) def getCurrent_raw(self): + # Sometimes a sharp load will reset the INA219, which will + # reset the cal register, meaning CURRENT and POWER will + # not be available ... avoid this by always setting a cal + # value even if it's an unfortunate extra step + bytes = [(self.ina219_calValue >> 8) & 0xFF, self.ina219_calValue & 0xFF] + self.i2c.writeList(self.__INA219_REG_CALIBRATION, bytes) + # Now we can safely read the CURRENT register! result = self.i2c.readList(self.__INA219_REG_CURRENT,2) if (result[0] >> 7 == 1): testint = (result[0]*256 + result[1])