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

Refactor battery object #16

Merged
merged 9 commits into from
May 16, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions create_build.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
dos2unix conf/serial-starter.d etc/dbus-serialbattery/dbus-serialbattery.py etc/dbus-serialbattery/service/run etc/dbus-serialbattery/service/log/run rc.local etc/dbus-serialbattery/LICENSE etc/dbus-serialbattery/README.md
tar -czvf venus-data.tar.gz --mode='a+rwX' conf/serial-starter.d etc/dbus-serialbattery/dbus-serialbattery.py etc/dbus-serialbattery/service/run etc/dbus-serialbattery/service/log/run rc.local etc/dbus-serialbattery/LICENSE etc/dbus-serialbattery/README.md
dos2unix conf/serial-starter.d etc/dbus-serialbattery/dbus-serialbattery.py etc/dbus-serialbattery/service/run etc/dbus-serialbattery/service/log/run rc.local etc/dbus-serialbattery/LICENSE etc/dbus-serialbattery/README.md etc/dbus-serialbattery/start-serialbattery.sh etc/dbus-serialbattery/battery.py etc/dbus-serialbattery/dbushelper.py etc/dbus-serialbattery/lttjdb.py etc/dbus-serialbattery/utils.py
tar -czvf venus-data.tar.gz --mode='a+rwX' conf/serial-starter.d etc/dbus-serialbattery/dbus-serialbattery.py etc/dbus-serialbattery/service/run etc/dbus-serialbattery/service/log/run rc.local etc/dbus-serialbattery/LICENSE etc/dbus-serialbattery/README.md etc/dbus-serialbattery/start-serialbattery.sh etc/dbus-serialbattery/battery.py etc/dbus-serialbattery/dbushelper.py etc/dbus-serialbattery/lttjdb.py etc/dbus-serialbattery/utils.py
150 changes: 150 additions & 0 deletions etc/dbus-serialbattery/battery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
import utils

# Constants - Need to dynamically get them in future
# Cell min/max voltages - used with the cell count to get the min/max battery voltage
MIN_CELL_VOLTAGE = 3.1
MAX_CELL_VOLTAGE = 3.45
# max battery charge/discharge current
MAX_BATTERY_CURRENT = 50.0
MAX_BATTERY_DISCHARGE_CURRENT = 60.0

class Protection(object):
# 2 = Alarm, 1 = Warning, 0 = Normal
def __init__(self):
self.voltage_high = None
self.voltage_low = None
self.soc_low = None
self.current_over = None
self.current_under = None
self.cell_imbalance = None
self.internal_failure = None
self.temp_high_charge = None
self.temp_low_charge = None
self.temp_high_discharge = None
self.temp_low_discharge = None


class Cell:
voltage = 0
balance = None

def __init__(self, balance):
self.balance = balance


class Battery(object):

def __init__(self, port, baud):
self.port = port
self.baud_rate = baud
self.role = 'battery'
self.type = 'Generic'

self.hardware_version = None
self.voltage = None
self.current = None
self.capacity_remain = None
self.capacity = None
self.cycles = None
self.production = None
self.protection = Protection()
self.version = None
self.soc = None
self.charge_fet = None
self.discharge_fet = None
self.cell_count = None
self.temp_censors = None
self.temp1 = None
self.temp2 = None
self.cells = []
self.control_charging = None
self.control_voltage = None
self.control_current = None
self.control_previous_total = None
self.control_previous_max = None
self.control_discharge_current = None
self.control_charge_current = None
self.control_allow_charge = None

def test_connection(self):
# Each driver must override this function to test if a connection can be made
# return false when fail, true if successful
return false

def get_settings(self):
# Each driver must override this function to read/set the battery settings
# It is called once after a successful connection by DbusHelper.setup_vedbus()
# Values: battery_type, version, hardware_version, min_battery_voltage, max_battery_voltage,
# MAX_BATTERY_CURRENT, MAX_BATTERY_DISCHARGE_CURRENT, cell_count, capacity
# return false when fail, true if successful
return false

def refresh_data(self):
# Each driver must override this function to read battery data and populate this class
# It is called each poll just before the data is published to vedbus
# return false when fail, true if successful
return false

def to_temp(self, sensor, value):
# Keep the temp value between -20 and 100 to handle sensor issues or no data.
# The BMS should have already protected before those limits have been reached.
if sensor == 1:
self.temp1 = min(max(value, -20), 100)
if sensor == 2:
self.temp2 = min(max(value, -20), 100)

def manage_charge_current(self):
# Start with the current values
charge_current = self.control_charge_current
discharge_current = self.control_discharge_current

# Change depending on the SOC values
if self.soc > 99:
self.control_allow_charge = False
else:
self.control_allow_charge = True
# Change depending on the SOC values
if 98 < self.soc <= 100:
self.control_charge_current = 1
elif 95 < self.soc <= 97:
self.control_charge_current = 4
elif 91 < self.soc <= 95:
self.control_charge_current = MAX_BATTERY_CURRENT/2
else:
self.control_charge_current = MAX_BATTERY_CURRENT

# Change depending on the SOC values
if self.soc <= 20:
self.control_discharge_current = 5
elif 20 < self.soc <= 30:
self.control_discharge_current = MAX_BATTERY_DISCHARGE_CURRENT/4
elif 30 < self.soc <= 35:
self.control_discharge_current = MAX_BATTERY_DISCHARGE_CURRENT/2
else:
self.control_discharge_current = MAX_BATTERY_DISCHARGE_CURRENT

def get_min_cell(self):
min_voltage = 9999
min_cell = None
for c in range(self.cell_count):
if min_voltage > self.cells[c].voltage:
min_voltage = self.cells[c].voltage
min_cell = c
return min_cell

def get_max_cell(self):
max_voltage = 0
max_cell = None
for c in range(self.cell_count):
if max_voltage < self.cells[c].voltage:
max_voltage = self.cells[c].voltage
max_cell = c
return max_cell

def get_balancing(self):
for c in range(self.cell_count):
if self.cells[c].balance:
return True
return False
Loading