Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sensor: battery voltage -> SoC conversions #72966

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

JordanYates
Copy link
Collaborator

Provide a mechanism for sensors that can only measure battery voltage to report a charge percentage through an open circuit voltage lookup table. 5 default tables are provided for different battery chemistries, application specific curves can easily be used instead.

This conversion is added to the voltage-divider driver and configured on the Thingy53 platform.

A custom linear interpolation function is provided to avoid any dependency on CMSIS-DSP.

@aurel32
Copy link
Collaborator

aurel32 commented May 18, 2024

Thanks @JordanYates, this is definitely a great addition to Zephyr, given it targets IoT, handling battery SoC is something important.

That said I wonder if the voltage divider is the right place for that. On one side not all voltage divider are used for battery sensing (but the battery feature is optional, so it's probably fine), and on the other side, not all voltage battery sensors are voltage dividers (for instance STM32 vref or vbat driver, but there are probably more on I2C or SPI bus, for instance the ina2xx). One way to solve that would be to add a battery sensor driver, that references a sensor device used to get the voltage, and then present it as a voltage / SoC gauge.

I am not 100% sure it could work, but if needed I can give it a try.

@JordanYates
Copy link
Collaborator Author

On one side not all voltage divider are used for battery sensing

That is a good point that I didn't consider, but as you say this feature is optional and should not affect those use cases.

and on the other side, not all voltage battery sensors are voltage divider

True, but this PR provides all the infrastructure needed to add the same conversion functionality with a minimum of effort.
Add the array to the config struct, populate it with the devicetree macro, call battery_soc_lookup with the voltage you would provide normally.

I generally try to avoid nested devices as they tend not to be as flexible as you'd hope when devices implement differing levels of functionality.

Add linear interpolation function that respects floating point rounding
rules (away from zero).

Signed-off-by: Jordan Yates <jordan@embeint.com>
Add tests for the linear interpolation math functions.

Signed-off-by: Jordan Yates <jordan@embeint.com>
Support additional properties from the linux `battery.yaml`.

The `ocv-capacity-table-0` definition differs from the Linux version as
we do not have a good way to generate variable length arrays in our
config structs, or a good way to separate out the percentage vs voltage
in the mapping.

We reduce the flexibility by enforcing a step size of 10%, which removes
the need for the percentage to be in the array and solves the variable
length problem.

To simplify the effort of defining these voltage curves, default curves
for a variety of chemistries have been added, extracted from datasheet
graphs of discharge curves from reputable manufacturers.

Signed-off-by: Jordan Yates <jordan@embeint.com>
Perform data conversion in the `fetch` call as documented in the sensor
API.

Signed-off-by: Jordan Yates <jordan@embeint.com>
Support SoC lookup through the use of the `ocv-capacity-table-0`
devicetree property.

Signed-off-by: Jordan Yates <jordan@embeint.com>
Define a battery curve for the lithium ion polymer battery shipped with
the board.

Signed-off-by: Jordan Yates <jordan@embeint.com>
@decsny
Copy link
Member

decsny commented May 20, 2024

I have the same concern about putting this within voltage divider, doesn't seem right, what alternatives are there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Devicetree Binding PR modifies or adds a Device Tree binding area: Sensors Sensors platform: nRF Nordic nRFx
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants