From 359efd7e605dea7c0e07169491285b20f9a6a8ca Mon Sep 17 00:00:00 2001 From: "wallarug (ardupilot)" Date: Wed, 2 Oct 2019 21:43:06 +1000 Subject: [PATCH 1/6] commit of 16V 5A (note - example changed) --- adafruit_ina219.py | 79 +++++++++++++++++++++++++++++++++++ examples/ina219_simpletest.py | 9 ++-- 2 files changed, 84 insertions(+), 4 deletions(-) diff --git a/adafruit_ina219.py b/adafruit_ina219.py index a9ac25a..45e280b 100644 --- a/adafruit_ina219.py +++ b/adafruit_ina219.py @@ -476,3 +476,82 @@ def set_calibration_16V_400mA(self): # pylint: disable=invalid-name self.bus_adc_resolution = ADCResolution.ADCRES_12BIT_1S self.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_1S self.mode = Mode.SANDBVOLT_CONTINUOUS + + def set_calibration_16V_5A(self): + # Calibration which uses the highest precision for + # current measurement (0.1mA), at the expense of + # only supporting 16V at 5000mA max. + + # VBUS_MAX = 16V + # VSHUNT_MAX = 0.08 (Assumes Gain 2, 80mV) + # RSHUNT = 0.02 (Resistor value in ohms) + + # 1. Determine max possible current + # MaxPossible_I = VSHUNT_MAX / RSHUNT + # MaxPossible_I = 4.0A + + # 2. Determine max expected current + # MaxExpected_I = 5.0A + + # 3. Calculate possible range of LSBs (Min = 15-bit, Max = 12-bit) + # MinimumLSB = MaxExpected_I/32767 + # MinimumLSB = 0.0001529 (uA per bit) + # MaximumLSB = MaxExpected_I/4096 + # MaximumLSB = 0.0012207 (uA per bit) + + # 4. Choose an LSB between the min and max values + # (Preferrably a roundish number close to MinLSB) + # CurrentLSB = 0.00016 (50uA per bit) + self._current_lsb = 0.1524 # in milliamps + + # 5. Compute the calibration register + # Cal = trunc (0.04096 / (Current_LSB * RSHUNT)) + # Cal = 26869 (0x68f5) + + self._cal_value = 26869 + + # 6. Calculate the power LSB + # PowerLSB = 20 * CurrentLSB + # PowerLSB = 0.001 (1mW per bit) + self._power_lsb = 0.003048 + + # 7. Compute the maximum current and shunt voltage values before overflow + # + # Max_Current = Current_LSB * 32767 + # Max_Current = 1.63835A before overflow + # + # If Max_Current > Max_Possible_I then + # Max_Current_Before_Overflow = MaxPossible_I + # Else + # Max_Current_Before_Overflow = Max_Current + # End If + # + # Max_Current_Before_Overflow = MaxPossible_I + # Max_Current_Before_Overflow = 0.4 + # + # Max_ShuntVoltage = Max_Current_Before_Overflow * RSHUNT + # Max_ShuntVoltage = 0.04V + # + # If Max_ShuntVoltage >= VSHUNT_MAX + # Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX + # Else + # Max_ShuntVoltage_Before_Overflow = Max_ShuntVoltage + # End If + # + # Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX + # Max_ShuntVoltage_Before_Overflow = 0.04V + + # 8. Compute the Maximum Power + # MaximumPower = Max_Current_Before_Overflow * VBUS_MAX + # MaximumPower = 0.4 * 16V + # MaximumPower = 6.4W + + # Set Calibration register to 'Cal' calcutated above + self._raw_calibration = self._cal_value + + # Set Config register to take into account the settings above + self.bus_voltage_range = BusVoltageRange.RANGE_16V + self.gain = Gain.DIV_2_80MV + self.bus_adc_resolution = ADCResolution.ADCRES_12BIT_1S + self.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_1S + self.mode = Mode.SANDBVOLT_CONTINUOUS diff --git a/examples/ina219_simpletest.py b/examples/ina219_simpletest.py index d0dbfbe..9485d9a 100644 --- a/examples/ina219_simpletest.py +++ b/examples/ina219_simpletest.py @@ -7,7 +7,8 @@ i2c_bus = board.I2C() -ina219 = INA219(i2c_bus) +ina219 = INA219(i2c_bus, 0x41) +ina219.set_calibration_16V_5A() print("ina219 test") @@ -21,10 +22,10 @@ print("") # optional : change configuration to use 32 samples averaging for both bus voltage and shunt voltage -ina219.bus_adc_resolution = ADCResolution.ADCRES_12BIT_32S -ina219.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_32S +#ina219.bus_adc_resolution = ADCResolution.ADCRES_12BIT_32S +#ina219.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_32S # optional : change voltage range to 16V -ina219.bus_voltage_range = BusVoltageRange.RANGE_16V +#ina219.bus_voltage_range = BusVoltageRange.RANGE_16V # measure and display loop while True: From e179b04d556a7745de6ce64ff17afb5fb297b197 Mon Sep 17 00:00:00 2001 From: "wallarug (ardupilot)" Date: Wed, 2 Oct 2019 21:47:48 +1000 Subject: [PATCH 2/6] updated example with power --- examples/ina219_simpletest.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/ina219_simpletest.py b/examples/ina219_simpletest.py index 9485d9a..c9be821 100644 --- a/examples/ina219_simpletest.py +++ b/examples/ina219_simpletest.py @@ -32,12 +32,14 @@ bus_voltage = ina219.bus_voltage # voltage on V- (load side) shunt_voltage = ina219.shunt_voltage # voltage between V+ and V- across the shunt current = ina219.current # current in mA + power = ina219.power # power in watts # INA219 measure bus voltage on the load side. So PSU voltage = bus_voltage + shunt_voltage print("PSU Voltage: {:6.3f} V".format(bus_voltage + shunt_voltage)) print("Shunt Voltage: {:9.6f} V".format(shunt_voltage)) print("Load Voltage: {:6.3f} V".format(bus_voltage)) print("Current: {:9.6f} A".format(current/1000)) + print("Power: {:9.6f} W".format(power)) print("") time.sleep(2) From f43322dbe274a87f7aeeabf20e904888c3dd35e9 Mon Sep 17 00:00:00 2001 From: "wallarug (ardupilot)" Date: Wed, 2 Oct 2019 21:58:03 +1000 Subject: [PATCH 3/6] added Gain.AUTO. Updated values for 16V_5A --- adafruit_ina219.py | 37 ++++++----------------------------- examples/ina219_simpletest.py | 2 +- 2 files changed, 7 insertions(+), 32 deletions(-) diff --git a/adafruit_ina219.py b/adafruit_ina219.py index 45e280b..2b82a7d 100644 --- a/adafruit_ina219.py +++ b/adafruit_ina219.py @@ -71,6 +71,7 @@ class Gain: DIV_2_80MV = 0x01 # shunt prog. gain set to /2, 80 mV range DIV_4_160MV = 0x02 # shunt prog. gain set to /4, 160 mV range DIV_8_320MV = 0x03 # shunt prog. gain set to /8, 320 mV range + AUTO = -1 # shunt prog. gain set to automatic class ADCResolution: """Constants for ``bus_adc_resolution`` or ``shunt_adc_resolution``""" @@ -501,14 +502,14 @@ def set_calibration_16V_5A(self): # 4. Choose an LSB between the min and max values # (Preferrably a roundish number close to MinLSB) - # CurrentLSB = 0.00016 (50uA per bit) + # CurrentLSB = 0.00016 (uA per bit) self._current_lsb = 0.1524 # in milliamps # 5. Compute the calibration register # Cal = trunc (0.04096 / (Current_LSB * RSHUNT)) - # Cal = 26869 (0x68f5) + # Cal = 13434 (0x347a) - self._cal_value = 26869 + self._cal_value = 13434 # 6. Calculate the power LSB # PowerLSB = 20 * CurrentLSB @@ -517,41 +518,15 @@ def set_calibration_16V_5A(self): # 7. Compute the maximum current and shunt voltage values before overflow # - # Max_Current = Current_LSB * 32767 - # Max_Current = 1.63835A before overflow - # - # If Max_Current > Max_Possible_I then - # Max_Current_Before_Overflow = MaxPossible_I - # Else - # Max_Current_Before_Overflow = Max_Current - # End If - # - # Max_Current_Before_Overflow = MaxPossible_I - # Max_Current_Before_Overflow = 0.4 - # - # Max_ShuntVoltage = Max_Current_Before_Overflow * RSHUNT - # Max_ShuntVoltage = 0.04V - # - # If Max_ShuntVoltage >= VSHUNT_MAX - # Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX - # Else - # Max_ShuntVoltage_Before_Overflow = Max_ShuntVoltage - # End If - # - # Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX - # Max_ShuntVoltage_Before_Overflow = 0.04V - # 8. Compute the Maximum Power - # MaximumPower = Max_Current_Before_Overflow * VBUS_MAX - # MaximumPower = 0.4 * 16V - # MaximumPower = 6.4W + # # Set Calibration register to 'Cal' calcutated above self._raw_calibration = self._cal_value # Set Config register to take into account the settings above self.bus_voltage_range = BusVoltageRange.RANGE_16V - self.gain = Gain.DIV_2_80MV + self.gain = Gain.AUTO self.bus_adc_resolution = ADCResolution.ADCRES_12BIT_1S self.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_1S self.mode = Mode.SANDBVOLT_CONTINUOUS diff --git a/examples/ina219_simpletest.py b/examples/ina219_simpletest.py index c9be821..4fc61b9 100644 --- a/examples/ina219_simpletest.py +++ b/examples/ina219_simpletest.py @@ -39,7 +39,7 @@ print("Shunt Voltage: {:9.6f} V".format(shunt_voltage)) print("Load Voltage: {:6.3f} V".format(bus_voltage)) print("Current: {:9.6f} A".format(current/1000)) - print("Power: {:9.6f} W".format(power)) + print("Power: {:9.6f} mW".format(power*1000)) print("") time.sleep(2) From d1e68beb5773beb2e79958bf4d8700c1ae9727e9 Mon Sep 17 00:00:00 2001 From: wallarug Date: Wed, 2 Oct 2019 22:00:17 +1000 Subject: [PATCH 4/6] Restored Original Test --- examples/ina219_simpletest.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/examples/ina219_simpletest.py b/examples/ina219_simpletest.py index 4fc61b9..d0dbfbe 100644 --- a/examples/ina219_simpletest.py +++ b/examples/ina219_simpletest.py @@ -7,8 +7,7 @@ i2c_bus = board.I2C() -ina219 = INA219(i2c_bus, 0x41) -ina219.set_calibration_16V_5A() +ina219 = INA219(i2c_bus) print("ina219 test") @@ -22,24 +21,22 @@ print("") # optional : change configuration to use 32 samples averaging for both bus voltage and shunt voltage -#ina219.bus_adc_resolution = ADCResolution.ADCRES_12BIT_32S -#ina219.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_32S +ina219.bus_adc_resolution = ADCResolution.ADCRES_12BIT_32S +ina219.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_32S # optional : change voltage range to 16V -#ina219.bus_voltage_range = BusVoltageRange.RANGE_16V +ina219.bus_voltage_range = BusVoltageRange.RANGE_16V # measure and display loop while True: bus_voltage = ina219.bus_voltage # voltage on V- (load side) shunt_voltage = ina219.shunt_voltage # voltage between V+ and V- across the shunt current = ina219.current # current in mA - power = ina219.power # power in watts # INA219 measure bus voltage on the load side. So PSU voltage = bus_voltage + shunt_voltage print("PSU Voltage: {:6.3f} V".format(bus_voltage + shunt_voltage)) print("Shunt Voltage: {:9.6f} V".format(shunt_voltage)) print("Load Voltage: {:6.3f} V".format(bus_voltage)) print("Current: {:9.6f} A".format(current/1000)) - print("Power: {:9.6f} mW".format(power*1000)) print("") time.sleep(2) From 1e702827bf2c6cc40c399260751b0a086b6f2b9a Mon Sep 17 00:00:00 2001 From: wallarug Date: Wed, 2 Oct 2019 22:04:10 +1000 Subject: [PATCH 5/6] Updated some values. Removed AUTO. --- adafruit_ina219.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/adafruit_ina219.py b/adafruit_ina219.py index 2b82a7d..53aa681 100644 --- a/adafruit_ina219.py +++ b/adafruit_ina219.py @@ -71,7 +71,6 @@ class Gain: DIV_2_80MV = 0x01 # shunt prog. gain set to /2, 80 mV range DIV_4_160MV = 0x02 # shunt prog. gain set to /4, 160 mV range DIV_8_320MV = 0x03 # shunt prog. gain set to /8, 320 mV range - AUTO = -1 # shunt prog. gain set to automatic class ADCResolution: """Constants for ``bus_adc_resolution`` or ``shunt_adc_resolution``""" @@ -484,12 +483,12 @@ def set_calibration_16V_5A(self): # only supporting 16V at 5000mA max. # VBUS_MAX = 16V - # VSHUNT_MAX = 0.08 (Assumes Gain 2, 80mV) + # VSHUNT_MAX = 0.16 (Assumes Gain 3, 160mV) # RSHUNT = 0.02 (Resistor value in ohms) # 1. Determine max possible current # MaxPossible_I = VSHUNT_MAX / RSHUNT - # MaxPossible_I = 4.0A + # MaxPossible_I = 8.0A # 2. Determine max expected current # MaxExpected_I = 5.0A @@ -513,7 +512,7 @@ def set_calibration_16V_5A(self): # 6. Calculate the power LSB # PowerLSB = 20 * CurrentLSB - # PowerLSB = 0.001 (1mW per bit) + # PowerLSB = 0.003 (3.048mW per bit) self._power_lsb = 0.003048 # 7. Compute the maximum current and shunt voltage values before overflow @@ -526,7 +525,7 @@ def set_calibration_16V_5A(self): # Set Config register to take into account the settings above self.bus_voltage_range = BusVoltageRange.RANGE_16V - self.gain = Gain.AUTO + self.gain = Gain.DIV_4_160MV self.bus_adc_resolution = ADCResolution.ADCRES_12BIT_1S self.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_1S self.mode = Mode.SANDBVOLT_CONTINUOUS From 9d22307779fb4c7c477e09226caf6b58f9328a02 Mon Sep 17 00:00:00 2001 From: wallarug Date: Thu, 3 Oct 2019 08:59:52 +1000 Subject: [PATCH 6/6] update for travis errors --- adafruit_ina219.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/adafruit_ina219.py b/adafruit_ina219.py index 53aa681..3009371 100644 --- a/adafruit_ina219.py +++ b/adafruit_ina219.py @@ -477,7 +477,11 @@ def set_calibration_16V_400mA(self): # pylint: disable=invalid-name self.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_1S self.mode = Mode.SANDBVOLT_CONTINUOUS - def set_calibration_16V_5A(self): + def set_calibration_16V_5A(self): # pylint: disable=invalid-name + """Configures to INA219 to be able to measure up to 16V and 5000mA of current. Counter + overflow occurs at 8.0A. + + .. note:: These calculations assume a 0.02 ohm shunt resistor is present""" # Calibration which uses the highest precision for # current measurement (0.1mA), at the expense of # only supporting 16V at 5000mA max.