OpenMarine

Full Version: OpenPlotter I2C 3.x.x - INA219 settings
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I have an INA219 that is working. However I need to change the gain settings.

In the screenshot below there are fields for "Sensor Settings" and "Magnitude Settings"


[attachment=1952]

In this screenshot below there is a place to enter settings. I don't know what I can set or the syntax.

[attachment=1951]


Second related question:

I have removed the 0.1 ohm surface mount resister from the board and substituted an external shunt. Is there a way to set the shunt resistance from settings?

Can anyone help with this?



TIA
i would be very interested in this as well...


best regards,
Tobias
(2022-11-12, 08:23 PM)Sailoog Wrote: [ -> ]could this help? https://forum.openmarine.net/showthread....2#pid24182

Kind of, but not really. Standard shunt on a INA219 is 100mOhm, i personally use a 7.5 mOhm (75 mV @ 10Amp) shunt at the moment but will switch to a 50 Amp shunt after testing. 
So set_calibration_16V_5A, with the ability to chose a custom shunt resistance would be great.

This here looks great, but i don't know where to apply those settings: https://forums.adafruit.com/viewtopic.ph...ml#p879003
I was working on this before I was interrupted by a Hurricane. It took a couple of days to get the power back on because the docks went underwater. I will start back on this very soon.

There is a lot more to configuring this sensor than you would think. A lot of settings interact with each other. It also depends if it is installed on the high side or the low side.

I have all the electronic test equipment to test things as I go along. I will post as things move along.
I have edited the title of this thread to be more explicit.

In older posts I have recommended to add these settings:


Quote:set_calibration_32V_2A
       """Configures to INA219 to be able to measure up to 32V and 2A of current. Counter
       overflow occurs at 3.2A.
       .. note:: These calculations assume a 0.1 shunt ohm resistor is present
       """
set_calibration_32V_1A
       """Configures to INA219 to be able to measure up to 32V and 1A of current. Counter overflow
       occurs at 1.3A.
       .. note:: These calculations assume a 0.1 ohm shunt resistor is present"""

set_calibration_16V_400mA
       """Configures to INA219 to be able to measure up to 16V and 400mA of current. Counter
       overflow occurs at 1.6A.
       .. note:: These calculations assume a 0.1 ohm shunt resistor is present"""
    
set_calibration_16V_5A
       """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"""

But after a deeper analysis of the code I can se that those pre-configured "calibration" settings seem just the most usual or expected uses of the module but there could be many more possible uses.

Those pre-configured uses are the combination of different values of these constants:

bus_voltage_range
gain
bus_adc_resolution
shunt_adc_resolution
mode

We could expose these constants and its fixed values as "sensor settings" in openplotter but we would also need to expose these private variables:

_current_lsb
_cal_value
_power_lsb

and here comes the problem because calculating these private variables is really complex for normal users. Here is an example of calculating them for "set_calibration_16V_5A"


Quote:       """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.

       # VBUS_MAX = 16V
       # 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 = 8.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 (uA per bit)
       self._current_lsb = 0.1524  # in milliamps

       # 5. Compute the calibration register
       # Cal = trunc (0.04096 / (Current_LSB * RSHUNT))
       # Cal = 13434 (0x347a)

       self._cal_value = 13434

       # 6. Calculate the power LSB
       # PowerLSB = 20 * CurrentLSB
       # PowerLSB = 0.003 (3.048mW per bit)
       self._power_lsb = 0.003048


We could do that but we would need a good documentation or at least values for all these variables for our own pre-configured settings.

This is the result of the html tool to calculate them in your example:

[Image: attachment.php?aid=1961]
but that result is for arduino and we should make sure those settings are the same as the python settings:

_cal_value
_current_lsb
_power_lsb

bus_voltage_range
gain
bus_adc_resolution
shunt_adc_resolution
mode

Does this all make sense to you?
I don't know if it makes sense YET!

Let's work through it.

I modified the adafruit INA219-config.html file to display more decimal places because some values were being truncated.

I ran the modified HTML file and started entering these values into the from your post.


In attached zip file:INA219-config.html  (sorry for the zip file it was, the post was too big with everything pasted in)
 
     Configures to INA219 to be able to measure up to 16V and 5000mA of current. 
     Counteroverflow 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.
       # VBUS_MAX = 16V
       # VSHUNT_MAX = 0.16 (Assumes Gain 3, 160mV) 

Ran into a problem, I couldn't enter a gain of 3. The Texas Instruments Data sheet doesn't list a gain of 3.
It is either a typo or Gain 3 is mapped to gain 4 in the python code. Note that Gain 4 really means "/4" in Texas Instrument (TI) lingo.
I entered GAIN 4 representing +/- 160 mv.
I made this table:

In attached zip file: gain mapping.jpg

Starting to enter values again, I skipped over sense resistance, choosing to let the HTML app calculate that value.

       # 1. Determine max possible current
       # MaxPossible_I = VSHUNT_MAX / RSHUNT
       # MaxPossible_I = 8.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)

I am little confused at this point:
   - The reference to "(Min = 15-bit, Max = 12-bit)" This is a 12-bit device.
   - The reference to "MinimumLSB = 0.0001529 (uA per bit)". This is an incredibly small number. The HTML app returns with 152 uA per bit, a more reasonable number.
   - MaximumLSB = 0.0012207 (uA per bit). Same problem, the HTML app returns with 1220 uA per bit.

       # 4. Choose an LSB between the min and max values
       #    (Preferrably a roundish number close to MinLSB)
       # CurrentLSB = 0.00016 (uA per bit)
       self._current_lsb = 0.1524  # in milliamps

I entered 152 in uA in the app.

Here is a table of the entries I made. The items highlighted in yellow are values that changed automatically when I entered the values on the left.

In attached zip file:Entered Values.jpg

Here is the output from the app:

In attached zip file:HTML Output 2022_11_14.jpg

So starting to make sense. 

The next thing to do is figure out how the JSON output from the HTML app would be mapped to python.
Then figure out how to expose the settings in the I2C settings for the INA219.
Then DOCUMENT procedure!
   Would need to include some schematics, including high & low side connections and what bus voltage means.
   Also need to calculate the wattage of the shunt resistor.

I have to run some errands right now. I will pick this back up tomorrow.
 
Figured out a little more by RTFM.

When I started asking questions is the previous thread (Announcing the release of I2C 3.2.0) i asked why I was getting null values.

Answering my own question:

Bus voltage, power and shunt voltage default to 0 (null) because the Calibration register defaults to 0 on powerup. These outputs will not output meaningful information until settings have been written to the Calibration register. (per Texas Instrument documentation)

Also, the INA219 doesn't know that Adafruit installed a 0.1 resister on the circuit board. So the shunt current value output is meaningless.

Another consideration:

If you are running any of the various INA219 SignalK plugins and are using the same I2C address in Open Plotter there isn't anything to prevent SignalK and OpenPlotter competing with each other to change the calibration register. Actually, there isn't anything to prevent the various SignalK plugins from competing with each other. This is one reason I would prefer to use OpenPlotter I2C because all of the addresses are managed in one place.

I learned this when I was trying various SignalK INA2i9 plugins and forgot to disable one before enabling the other.

I think most of the software (at least with my limited understanding of the code) uses the Adafruit drivers that reset the calibration register just before reading the data. I haven't figured out what effect this reset has on the internal resolution/averaging of the shunt voltage and bus voltage. Maybe, I should stop RTFM.[Image: smile.png]
This is what the python adafruit script does at startup:

Code:
     
       self._cal_value = 0
       self._current_lsb = 0
       self._power_lsb = 0
       self.set_calibration_32V_2A()

It sets to 0 the calibration values but immediately sets the default mode set_calibration_32V_2A with these values assuming there is a 0.1 shunt resistor:

Code:
self._cal_value = 4096
self._current_lsb = 0.1
self._power_lsb = 0.002

self.bus_voltage_range = BusVoltageRange.RANGE_32V
self.gain = Gain.DIV_8_320MV
self.bus_adc_resolution = ADCResolution.ADCRES_12BIT_1S
self.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_1S
self.mode = Mode.SANDBVOLT_CONTINUOUS

I will check the signal k plugins to see how they manage calibration and the possibility of checking from openplotter if they are competing for the device.

Currently openplotter users can only use the sensor for set_calibration_32V_2A with the built 0.1 shunt resistor, so I do not think anyone is using it. If you agree I could add these settings as "sensor settings" to openplotter:

_current_lsb:
   float values allowed
_cal_value:
   float values allowed
_power_lsb:
   float values allowed

bus_voltage_range:
    RANGE_16V
    RANGE_32V
gain:
    DIV_1_40MV
    DIV_2_80MV
    DIV_4_160MV
    DIV_8_320MV
bus_adc_resolution:
    ADCRES_9BIT_1S
    ADCRES_10BIT_1S
    ADCRES_11BIT_1S
    ADCRES_12BIT_1S
    ADCRES_12BIT_2S
    ADCRES_12BIT_4S
    ADCRES_12BIT_8S 
    ADCRES_12BIT_16S
    ADCRES_12BIT_32S
    ADCRES_12BIT_64S
    ADCRES_12BIT_128S
shunt_adc_resolution:
    same as bus_adc_resolution
mode:
    POWERDOWN
    SVOLT_TRIGGERED
    BVOLT_TRIGGERED
    SANDBVOLT_TRIGGERED
    ADCOFF
    SVOLT_CONTINUOUS
    BVOLT_CONTINUOUS
    SANDBVOLT_CONTINUOUS

The default value of these exposed variables will be set_calibration_16V_5A instead of set_calibration_32V_2A and you will be able to play with it using the html tool to calculate different modes.
Sounds great. I will test immediately when available.
Pages: 1 2