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

The readout results are wrong #1

Open
Manjo80 opened this issue Aug 11, 2023 · 0 comments
Open

The readout results are wrong #1

Manjo80 opened this issue Aug 11, 2023 · 0 comments

Comments

@Manjo80
Copy link

Manjo80 commented Aug 11, 2023

Hallo, maybe you could help me. I habe change thebq34100g1.py to use it with i2c because micropython has no working lib for smbus.

Is is "working" running without erros but the results I receive are not usable

Temperature: -273.15
Internal Temperature: -259.65
Voltage: 135
Current: 135
Power: 135
Capacity: 135
Full Capacity: 135
Design Capacity: 0
Cycle Count: 0
State of Charge: 0
Flags A: [0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
Flags B: [0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
Ctrl Status A: [0, 0, 0, 0, 0, 0, 0, 0]
Ctrl Status B: [0, 0, 0, 0, 0, 0, 0, 0]
Max Error: 0
Avg Time to Empty: 135
Avg Time to Full: 135
State of Health: 0
Qmax Time: 135
Learned Status: [0, 0, 0, 0, 0, 0, 0, 0]

this is the change bqbq34z100g1.py

import machine

class bq34z100g1(object):

    def __init__(self, address=0x69, bus=1):
        self._address = address
        self._bus = machine.I2C(bus, sda=machine.Pin(2), scl=machine.Pin(3), freq=400000)

    @staticmethod
    def log(msg):
        print(msg)

    @staticmethod
    def ftol(byte: int):
        liste = []
        liste.append(1) if byte & 1 > 0 else liste.append(0)
        liste.append(1) if byte & 2 > 0 else liste.append(0)
        liste.append(1) if byte & 4 > 0 else liste.append(0)
        liste.append(1) if byte & 8 > 0 else liste.append(0)
        liste.append(1) if byte & 16 > 0 else liste.append(0)
        liste.append(1) if byte & 32 > 0 else liste.append(0)
        liste.append(1) if byte & 64 > 0 else liste.append(0)
        liste.append(1) if byte & 128 > 0 else liste.append(0)
        return liste

    def openConfig(self):
        self._bus.writeto(self._address, bytes([0x00, 0x14, 0x04]))
        self._bus.writeto(self._address, bytes([0x00, 0x72, 0x36]))
        self._bus.writeto(self._address, bytes([0x61, 0x00]))

    def setConfig(self):
        self.openConfig()
        self._bus.writeto(self._address, bytes([0x3e, 0x30]))
        self._bus.writeto(self._address, bytes([0x3f, 0x00]))
        # self._bus.writeto(self._address, bytes([0x4a, 0x00]))
        # self._bus.writeto(self._address, bytes([0x4b, 0x00]))

    def _writeValue(self, cmd, text):
        try:
            self._bus.writeto(self._address, bytes([cmd, text >> 8, text & 0xFF]))
        except:
            bq34z100g1.log("Couldn't write to i2c bus")

    def _readValue(self, register: int, length: int = 2) -> int:
        try:
            if length == 2:
                data = self._bus.readfrom_mem(self._address, register, 2)
                return (data[0] << 8) | data[1]
            if length == 1:
                return self._bus.readfrom_mem(self._address, register, 1)[0]
            else:
                print("is not supported by now") # do again two times?
                return -1
        except:
            bq34z100g1.log("Could not read i2c bus")
            return -1

    def _readSignedValue(self, register: int, length: int = 2) -> int:
        value = self._readValue(register, length)
        if length == 2:
            if value <= 32767:
                return value
            else:
                return value - 65535
        if length == 1:
            if value <= 128:
                return value
            else:
                return value - 256

    def get_temperature(self):  # 0x0c
        """returns Temperature in °C"""
        return round((self._readValue(0x0C) * 0.1) - 273.15, 2)

    def get_internal_temperature(self):  # 0x2a
        """return internal Temperature in °C"""
        return round((self._readValue(0x2b) * 0.1) - 273.15, 2)

    def get_voltage(self):  # 0x08,0x09
        """return Voltage in mV"""
        return self._readValue(0x08)

    def get_current(self):  # 0x10,0x11
        """returns Current in mA"""
        return self._readSignedValue(0x10)

    def get_power(self):  # 0x26,0x27
        """returns current Power Usage"""
        return self._readSignedValue(0x26)

    def get_capacity(self):  # 0x04,0x05
        """returns Capacity in mAh"""
        return self._readValue(0x04)

    def get_full_capacity(self):  # 0x06,0x07
        """returns Capacity when full in mAh"""
        return self._readValue(0x06)

    def get_design_capacity(self):  # 0x0c,0x0d
        """returns Design Capacity in mAh"""
        return self._readValue(0x3C)

    def get_cycle_count(self):  # 0x2c
        """returns the amount of Cycles the Battery has run"""
        return self._readValue(0x2C, 1)

    def get_state_of_charge(self):  # 0x02
        """return State of Charge in %"""
        return self._readValue(0x02, 1)

    def get_flagsa(self):  #0x0e, 0x0f
        return str(str(self.ftol(int(self._readValue(0x0e, 1)))) + "\n" + str(self.ftol(int(self._readValue(0x0f, 1)))))

    def get_flagsb(self):  #0x12, 0x13
        return str(str(self.ftol(int(self._readValue(0x12, 1)))) + "\n" + str(self.ftol(int(self._readValue(0x13, 1)))))

    def get_ctrl_statusa(self):
        return self.ftol(int(self._readValue(0x00, 1)))

    def get_ctrl_statusb(self):
        return self.ftol(int(self._readValue(0x01, 1)))

    def get_max_error(self):
        return self._readValue(0x03, 1)

    def get_avg_time_to_empty(self):
        return self._readValue(0x18)

    def get_avg_time_to_full(self):  # 0x1a,0x1b
        return self._readValue(0x1A)

    def get_state_of_health(self):  # 0x2e,0x2f
        return self._readValue(0x2E)

    def get_qmax_time(self):
        return self._readValue(0x74)

    def get_learned_status(self):
        return self.ftol(int(self._readValue(0x63,1)))

and my test main.py

import time
import machine
from bq34z100g1 import bq34z100g1

i2c_sda = machine.Pin(2)
i2c_scl = machine.Pin(3)

i2c = machine.I2C(1, sda=i2c_sda, scl=i2c_scl, freq=400000)

battery_monitor = bq34z100g1(address=0x69, bus=1)

while True:
    temperature = battery_monitor.get_temperature()
    internal_temperature = battery_monitor.get_internal_temperature()
    voltage = battery_monitor.get_voltage()
    current = battery_monitor.get_current()
    power = battery_monitor.get_power()
    capacity = battery_monitor.get_capacity()
    full_capacity = battery_monitor.get_full_capacity()
    design_capacity = battery_monitor.get_design_capacity()
    cycle_count = battery_monitor.get_cycle_count()
    state_of_charge = battery_monitor.get_state_of_charge()
    flags_a = battery_monitor.get_flagsa()
    flags_b = battery_monitor.get_flagsb()
    ctrl_status_a = battery_monitor.get_ctrl_statusa()
    ctrl_status_b = battery_monitor.get_ctrl_statusb()
    max_error = battery_monitor.get_max_error()
    avg_time_to_empty = battery_monitor.get_avg_time_to_empty()
    avg_time_to_full = battery_monitor.get_avg_time_to_full()
    state_of_health = battery_monitor.get_state_of_health()
    qmax_time = battery_monitor.get_qmax_time()
    learned_status = battery_monitor.get_learned_status()
    
    print("Temperature:", temperature)
    print("Internal Temperature:", internal_temperature)
    print("Voltage:", voltage)
    print("Current:", current)
    print("Power:", power)
    print("Capacity:", capacity)
    print("Full Capacity:", full_capacity)
    print("Design Capacity:", design_capacity)
    print("Cycle Count:", cycle_count)
    print("State of Charge:", state_of_charge)
    print("Flags A:", flags_a)
    print("Flags B:", flags_b)
    print("Ctrl Status A:", ctrl_status_a)
    print("Ctrl Status B:", ctrl_status_b)
    print("Max Error:", max_error)
    print("Avg Time to Empty:", avg_time_to_empty)
    print("Avg Time to Full:", avg_time_to_full)
    print("State of Health:", state_of_health)
    print("Qmax Time:", qmax_time)
    print("Learned Status:", learned_status)
    
    time.sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant