RobTillaart / INA226

Arduino library for INA226 power sensor
MIT License
54 stars 14 forks source link

float INA226::getCurrent() does not handle negative values properly #7

Closed RobTillaart closed 3 years ago

RobTillaart commented 3 years ago

@FrankTmmrmn

See begin of discussion in Pull Request #6

Essence of the problem

in my application - NiMh battery analyzer the current is positive when a load is connected but i don't get a negative number when I am charging.

RobTillaart commented 3 years ago

update: development branch created

analysis _read_register is returning a uint16_t, assigning it to float does not automatically interpret the sign bit ( = CSIGN - bit 15 - from Current Register 0x04). Code should check for sign bit similar as done for ShuntVoltage Register (0x01).

For positive values the float worked well, however for negative values it seems to interpret the value as a (large) positive value due to the sign bit. => BUG.

RobTillaart commented 3 years ago

@FrankTmmrmn

image

RobTillaart commented 3 years ago

Notice that the delta has some relative larger bumps The cause is that POWER uses less bits than the POWER2 calculation. For small values this gives a relative larger DELTA.

Might just need some more averaging...

INA.setAverage(7); smooths max

RobTillaart commented 3 years ago

@FrankTmmrmn Can you verify if the develop branch works?

FrankTmmrmn commented 3 years ago

Hi Rob I have compiled and loaded the code INA226_demo_2 here is the output of the serial monitor

POWER2 = busVoltage x current BUS CURRENT POWER POWER2 DELTA 26.692 761.300 659.20 20321.00 19661.80 26.681 760.000 684.20 20277.75 19593.55 26.674 762.500 676.70 20338.74 19662.04 26.695 760.000 626.70 20288.20 19661.50 26.660 762.500 569.20 20328.25 19759.05 26.656 760.000 599.20 20258.75 19659.55 26.651 761.300 631.70 20289.60 19657.90 26.647 758.800 594.20 20220.12 19625.92 26.644 760.000 589.20 20249.25 19660.05 26.666 760.000 606.70 20266.35 19659.65 26.747 571.300 2175.30 15280.85 13105.55 switch to charging 27.066 6494.900 1590.00 175792.57 174202.57 27.102 6496.100 1557.50 176060.54 174503.04 27.087 6493.600 1625.00 175895.39 174270.39 27.090 6494.900 1590.00 175946.84 174356.84 27.096 6493.600 1625.00 175952.20 174327.20 27.125 6496.100 1560.00 176206.71 174646.71 27.102 6493.600 1625.00 175992.79 174367.79

kind regards

Op zo 8 aug. 2021 om 13:00 schreef Rob Tillaart @.***>:

@FrankTmmrmn https://github.com/FrankTmmrmn Can you verify if the develop branch works?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/RobTillaart/INA226/issues/7#issuecomment-894779762, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVDRHO6JIUM2JR4IZZ7IZ4TT3ZPTZANCNFSM5BXDBFZA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email .

-- Kind regards

Frank Temmerman e: @.*** m: +32 (0)496 161 169 Skype: frank_temmerman http://www.eftico.com

RobTillaart commented 3 years ago

mmm does not look like solved.

1) Are you sure you have downloaded the develop branch? https://github.com/RobTillaart/INA226/tree/develop

I notice the header of the INA226_demo_2.ino sketch looks not updated as I added mW and mA etc. This could indicate you still use the master branch .

You might need to add a line Serial.println(INA226_LIB_VERSION); develop - 0.1.4, master is 0.1.3

2) Do you have multimeter values for current, to see what the actual values are? That would allow me to do some reverse reasoning that might help finding the cause.

3) Do you have a sketch of your hardware setup, so I might be able to recreate it (when time permits)

FrankTmmrmn commented 3 years ago

Rob cpp has the contents of the header file frank

Op ma 9 aug. 2021 om 12:48 schreef Rob Tillaart @.***>:

mmm does not look like solved.

  1. Are you sure you have downloaded the develop branch? https://github.com/RobTillaart/INA226/tree/develop

I notice the header of the INA226_demo_2.ino sketch looks not updated as I added mW and mA etc. This could indicate you

1.

Do you have multimeter values for current, to see what the actual values are? That would allow me to do some reverse reasoning that might help finding the cause. 2.

Do you have a sketch of your hardware setup, so I might be able to recreate it (when time permits)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/RobTillaart/INA226/issues/7#issuecomment-895124252, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVDRHO3HJCLMTOT4W6XNGFDT36W6PANCNFSM5BXDBFZA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email .

-- Kind regards

Frank Temmerman e: @.*** m: +32 (0)496 161 169 Skype: frank_temmerman http://www.eftico.com

FrankTmmrmn commented 3 years ago

sorry my mistake its only in the comment section that you mention the .h file twice seems to be working fine now Rob.

this is the output of the serial monitor

startup in charging mode

POWER2 = busVoltage x current V mA mW mW mW BUS CURRENT POWER POWER2 DELTA 27.081 -493.700 270.30 -13370.01 -13640.31 27.081 -493.700 270.30 -13370.01 -13640.31 27.081 -493.700 272.80 -13370.01 -13642.81 27.081 -493.700 275.30 -13370.01 -13645.31 27.061 -432.500 5151.40 -11703.99 -16855.39

switch to load 1 26.712 570.000 2122.80 15226.12 13103.33 26.710 570.000 2115.30 15224.70 13109.40 26.706 570.000 2112.80 15222.56 13109.76 26.705 570.000 2115.30 15221.85 13106.55 26.702 570.000 2110.30 15220.42 13110.12

switch to load 2 26.342 751.300 144.20 19791.12 19646.92 26.341 751.300 139.20 19790.18 19650.98 26.341 751.300 139.20 19790.18 19650.98 26.341 751.300 129.20 19790.18 19660.98 26.341 751.300 131.70 19790.18 19658.48 26.341 751.300 139.20 19790.18 19650.98

switch to load 3 26.082 1113.800 2845.60 29050.69 26205.09 26.082 1115.000 2858.10 29081.99 26223.89 26.082 1113.800 2850.60 29050.69 26200.09 26.082 1113.800 2843.10 29050.69 26207.59 26.081 1113.800 2840.60 29049.29 26208.69 26.081 1115.000 2853.10 29080.59 26227.50

switch to charging mode

26.870 -496.200 222.80 -13332.89 -13555.69 26.870 -496.200 232.80 -13332.89 -13565.69 26.870 -496.200 225.30 -13332.89 -13558.19 26.870 -496.200 225.30 -13332.89 -13558.19 26.870 -496.200 225.30 -13332.89 -13558.19 26.870 -496.200 237.80 -13332.89 -13570.69

Op ma 9 aug. 2021 om 15:13 schreef Frank Temmerman @.***>:

Rob cpp has the contents of the header file frank

Op ma 9 aug. 2021 om 12:48 schreef Rob Tillaart @.***

:

mmm does not look like solved.

  1. Are you sure you have downloaded the develop branch? https://github.com/RobTillaart/INA226/tree/develop

I notice the header of the INA226_demo_2.ino sketch looks not updated as I added mW and mA etc. This could indicate you

1.

Do you have multimeter values for current, to see what the actual values are? That would allow me to do some reverse reasoning that might help finding the cause. 2.

Do you have a sketch of your hardware setup, so I might be able to recreate it (when time permits)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/RobTillaart/INA226/issues/7#issuecomment-895124252, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVDRHO3HJCLMTOT4W6XNGFDT36W6PANCNFSM5BXDBFZA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email .

-- Kind regards

Frank Temmerman e: @.*** m: +32 (0)496 161 169 Skype: frank_temmerman http://www.eftico.com

-- Kind regards

Frank Temmerman e: @.*** m: +32 (0)496 161 169 Skype: frank_temmerman http://www.eftico.com

RobTillaart commented 3 years ago

It looks quite a bit better as the current is charging / draining dependent. I will merge it into the master as it is an improvement for sure.

In my tests the POWER and POWER2 were always (~) equal in size. Any idea why it isn't in your test runs?

RobTillaart commented 3 years ago

Merged into master. If there are no more questions / problems you can close this issue.

FrankTmmrmn commented 3 years ago

No idea at the moment Rob I didn't check any of the returned values with a calibrated meter, I will have to dig deeper in the different commands and the setup of the INA in general. First step was to get values out of the sensor and display them on the LCD and store everything on a SD card. I will look at it later, if i find something i will let you know gr Frank

Op ma 9 aug. 2021 om 16:35 schreef Rob Tillaart @.***>:

It looks quite a bit better as the current is charging / draining dependent. I will merge it into the master as it is an improvement for sure.

In my tests the POWER and POWER2 were always (~) equal in size. Any idea why it isn't in your test runs?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/RobTillaart/INA226/issues/7#issuecomment-895277714, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVDRHO23HTWT7WT2YBR33QTT37RUXANCNFSM5BXDBFZA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email .

-- Kind regards

Frank Temmerman e: @.*** m: +32 (0)496 161 169 Skype: frank_temmerman http://www.eftico.com

RobTillaart commented 3 years ago

OK, thanks again for pointing out the bug. For now I will close this issue and just create a new one if needed, or reopen this if it seems related.