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

feat: Add INA219 battery sensor #2380

Merged
merged 19 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 53 additions & 6 deletions documentation/builders/components/power/batterymonitor.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# Battery Monitor based on a ADS1015

> [!CAUTION]

Check failure on line 1 in documentation/builders/components/power/batterymonitor.md

View workflow job for this annotation

GitHub Actions / build

First line in a file should be a top-level heading

documentation/builders/components/power/batterymonitor.md:1 MD041/first-line-heading/first-line-h1 First line in a file should be a top-level heading [Context: "> [!CAUTION]"] https://github.com/DavidAnson/markdownlint/blob/v0.33.0/doc/md041.md
> Lithium and other batteries are dangerous and must be treated with care.
> Rechargeable Lithium Ion batteries are potentially hazardous and can
> present a serious **FIRE HAZARD** if damaged, defective or improperly used.
> Do not use this circuit to a lithium ion battery without expertise and
> training in handling and use of batteries of this type.
> present a serious **FIRE HAZARD** if damaged, defective, or improperly used.
> Do not use this circuit for a lithium-ion battery without the expertise and
> training in handling and using batteries of this type.
> Use appropriate test equipment and safety protocols during development.
> There is no warranty, this may not work as expected or at all!
> There is no warranty, this may not work as expected!

# Battery Monitor based on a ADS1015

Check failure on line 10 in documentation/builders/components/power/batterymonitor.md

View workflow job for this annotation

GitHub Actions / build

Headings should be surrounded by blank lines

documentation/builders/components/power/batterymonitor.md:10 MD022/blanks-around-headings Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "# Battery Monitor based on a ADS1015"] https://github.com/DavidAnson/markdownlint/blob/v0.33.0/doc/md022.md
The script in [src/jukebox/components/battery_monitor/batt_mon_i2c_ads1015/\_\_init\_\_.py](../../../../src/jukebox/components/battery_monitor/batt_mon_i2c_ads1015/__init__.py) is intended to read out the voltage of a single Cell LiIon Battery using a [CY-ADS1015 Board](https://www.adafruit.com/product/1083):

```text
Expand All @@ -31,3 +30,51 @@
>
> * the circuit is constantly draining the battery! (leak current up to: 2.1µA)
> * the time between sample needs to be a minimum 1sec with this high impedance voltage divider don't use the continuous conversion method!

# Battery Monitor based on an INA219

The script in [src/jukebox/components/battery_monitor/batt_mon_i2c_ina219/\_\_init\_\_.py](../../../../src/jukebox/components/battery_monitor/batt_mon_i2c_ina219/__init__.py) is intended to read out the voltage of a single cell or multiple LiIon Battery using a [INA219 Board](https://www.adafruit.com/product/904):

```text
3.3V
+
|
.----o----.
| | SDA
.-------------------------------o AIN o------
| | INA219 | SCL
| .----------o AOUT o------
--- | | |
Battery - Regulator + Raspi '----o----'
2.9V-4.2V| | |
| | |
=== === ===
```

# Configuration example

The battery monitoring is configured in the jukebox.yml file.

The "battmon" module has to be added to the modules setting.

Check failure on line 58 in documentation/builders/components/power/batterymonitor.md

View workflow job for this annotation

GitHub Actions / build

Trailing spaces

documentation/builders/components/power/batterymonitor.md:58:61 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1] https://github.com/DavidAnson/markdownlint/blob/v0.33.0/doc/md009.md

```text
saeugetier marked this conversation as resolved.
Show resolved Hide resolved
modules:
named:
# Do not change the order!
publishing: publishing
...
battmon: battery_monitor.batt_mon_i2c_ina219
```

Check failure on line 67 in documentation/builders/components/power/batterymonitor.md

View workflow job for this annotation

GitHub Actions / build

Fenced code blocks should be surrounded by blank lines

documentation/builders/components/power/batterymonitor.md:67 MD031/blanks-around-fences Fenced code blocks should be surrounded by blank lines [Context: "```"] https://github.com/DavidAnson/markdownlint/blob/v0.33.0/doc/md031.md
The battmon module needs further configuration:

```text
saeugetier marked this conversation as resolved.
Show resolved Hide resolved
battmon:
scale_to_phy_num: 1
scale_to_phy_denom: 0
warning_action:
all_clear_action:
```

The setting "scale_to_phy_denom" does not influence the INA219. However, the scale can be adjusted to fit multiple LiIon cells.


Check failure on line 80 in documentation/builders/components/power/batterymonitor.md

View workflow job for this annotation

GitHub Actions / build

Multiple consecutive blank lines

documentation/builders/components/power/batterymonitor.md:80 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2] https://github.com/DavidAnson/markdownlint/blob/v0.33.0/doc/md012.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# RPi-Jukebox-RFID Version 3
# Copyright (c) See file LICENSE in project root folder

import logging
import jukebox.plugs as plugs
import jukebox.cfghandler
from ina219 import INA219
from ina219 import DeviceRangeError
from components.battery_monitor import BatteryMonitorBase

logger = logging.getLogger('jb.battmon.ina219')

batt_mon = None


class battmon_ina219(BatteryMonitorBase.BattmonBase):
'''Battery Monitor based on a INA219
saeugetier marked this conversation as resolved.
Show resolved Hide resolved

See [Battery Monitor documentation](../../builders/components/power/batterymonitor.md)
'''

def __init__(self, cfg):
super().__init__(cfg, logger)

def init_batt_mon_hw(self, num: float, denom: float) -> None:
try:
self.adc = INA219(float(num) / 1000, busnum=1)
self.adc.configure(self.adc.RANGE_16V, self.adc.GAIN_AUTO, self.adc.ADC_32SAMP, self.adc.ADC_32SAMP)
except DeviceRangeError as e:
logger.error(f"Device range error: {e}")
raise
except Exception as e:
logger.error(f"Failed to initialize INA219: {e}")
raise

def get_batt_voltage(self) -> int:
try:
batt_voltage_mV = self.adc.supply_voltage() * 1000.0
return int(batt_voltage_mV)
except Exception as e:
logger.error(f"Failed to get supply voltage from INA219: {e}")
raise


@plugs.finalize
def finalize():
global batt_mon
cfg = jukebox.cfghandler.get_handler('jukebox')
batt_mon = battmon_ina219(cfg)
plugs.register(batt_mon, name='batt_mon')


@plugs.atexit
def atexit(**ignored_kwargs):
global batt_mon
batt_mon.status_thread.cancel()
Loading