Skip to content

Commit

Permalink
Merge pull request #24 from p0l0us/jk_bms_black_fix
Browse files Browse the repository at this point in the history
Fixing JK BMS black version.
  • Loading branch information
mr-manuel committed Mar 18, 2024
2 parents f07549d + fa137d9 commit c925a1e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
22 changes: 13 additions & 9 deletions etc/dbus-serialbattery/bms/jkbms_can.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
MAX_BATTERY_DISCHARGE_CURRENT,
MAX_CELL_VOLTAGE,
MIN_CELL_VOLTAGE,
JKBMS_CAN_CELL_COUNT,
zero_char,
)
from struct import unpack_from
Expand Down Expand Up @@ -49,12 +50,14 @@ def __del__(self):

MESSAGES_TO_READ = 100

# Changed from 0x0XF4 to 0x0XF5. See https://github.com/Louisvdw/dbus-serialbattery/issues/950
# B2A... Black is using 0x0XF4
# B2A... Silver is using 0x0XF5
# See https://github.com/Louisvdw/dbus-serialbattery/issues/950
CAN_FRAMES = {
BATT_STAT: 0x02F5,
CELL_VOLT: 0x04F5,
CELL_TEMP: 0x05F5,
ALM_INFO: 0x07F5,
BATT_STAT: [0x02F4, 0x02F5],
CELL_VOLT: [0x04F4, 0x04F5],
CELL_TEMP: [0x05F4, 0x05F5],
ALM_INFO: [0x07F4, 0x07F5],
}

def test_connection(self):
Expand All @@ -67,6 +70,7 @@ def get_settings(self):
# After successful connection get_settings will be call to set up the battery.
# Set the current limits, populate cell count, etc
# Return True if success, False for failure
self.cell_count = JKBMS_CAN_CELL_COUNT
self.max_battery_charge_current = MAX_BATTERY_CHARGE_CURRENT
self.max_battery_discharge_current = MAX_BATTERY_DISCHARGE_CURRENT
self.max_battery_voltage = MAX_CELL_VOLTAGE * self.cell_count
Expand Down Expand Up @@ -204,7 +208,7 @@ def read_serial_data_jkbms_CAN(self):
# print("message received")
messages_to_read -= 1
# print(messages_to_read)
if msg.arbitration_id == self.CAN_FRAMES[self.BATT_STAT]:
if msg.arbitration_id in self.CAN_FRAMES[self.BATT_STAT]:
voltage = unpack_from("<H", bytes([msg.data[0], msg.data[1]]))[0]
self.voltage = voltage / 10

Expand All @@ -222,7 +226,7 @@ def read_serial_data_jkbms_CAN(self):
# print(self.soc)
# print(self.time_to_go)

elif msg.arbitration_id == self.CAN_FRAMES[self.CELL_VOLT]:
elif msg.arbitration_id in self.CAN_FRAMES[self.CELL_VOLT]:
max_cell_volt = (
unpack_from("<H", bytes([msg.data[0], msg.data[1]]))[0] / 1000
)
Expand All @@ -249,14 +253,14 @@ def read_serial_data_jkbms_CAN(self):
self.cells[min_cell_nr - 1].voltage = min_cell_volt
self.cells[min_cell_nr - 1].balance = True

elif msg.arbitration_id == self.CAN_FRAMES[self.CELL_TEMP]:
elif msg.arbitration_id in self.CAN_FRAMES[self.CELL_TEMP]:
max_temp = unpack_from("<B", bytes([msg.data[0]]))[0] - 50
min_temp = unpack_from("<B", bytes([msg.data[2]]))[0] - 50
self.to_temp(1, max_temp if max_temp <= 100 else 100)
self.to_temp(2, min_temp if min_temp <= 100 else 100)
# print(max_temp)
# print(min_temp)
elif msg.arbitration_id == self.CAN_FRAMES[self.ALM_INFO]:
elif msg.arbitration_id in self.CAN_FRAMES[self.ALM_INFO]:
alarms = unpack_from(
"<L",
bytes([msg.data[0], msg.data[1], msg.data[2], msg.data[3]]),
Expand Down
8 changes: 7 additions & 1 deletion etc/dbus-serialbattery/config.default.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ LOGGING = INFO
MAX_BATTERY_CHARGE_CURRENT = 50.0
MAX_BATTERY_DISCHARGE_CURRENT = 60.0


; --------- Cell Voltages ---------
; Description:
; Cell min/max voltages which are used to calculate the min/max battery voltage
Expand Down Expand Up @@ -386,6 +385,13 @@ BATTERY_CAPACITY = 50
; Invert Battery Current. Default non-inverted. Set to -1 to invert
INVERT_CURRENT_MEASUREMENT = 1

; -- JK (Jikong) settings
; Predefines cell count for Jkbms_can
; The cell count should be auto-detected by identifying the highest cell number,
; but this process may be sometimes slow what could cause that cells voltage is not not
; updated in VenusOS. Try this workaround if you experience problems with cell voltage.
JKBMS_CAN_CELL_COUNT = 1

; -- ESC GreenMeter and Lipro device settings
GREENMETER_ADDRESS = 1
LIPRO_START_ADDRESS = 2
Expand Down
6 changes: 6 additions & 0 deletions etc/dbus-serialbattery/dbushelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,12 @@ def publish_dbus(self):
3,
)
except Exception:
exception_type, exception_object, exception_traceback = sys.exc_info()
file = exception_traceback.tb_frame.f_code.co_filename
line = exception_traceback.tb_lineno
logger.error(
f"Exception occurred: {repr(exception_object)} of type {exception_type} in {file} line #{line}"
)
pass

# Update TimeToGo and/or TimeToSoC
Expand Down
11 changes: 5 additions & 6 deletions etc/dbus-serialbattery/reinstall-local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ bluetooth_length=${#bms_array[@]}
# echo $bluetooth_length

# stop all dbus-blebattery services, if at least one exists
if [ -d "/service/dbus-blebattery.0" ]; then
if ls /service/dbus-blebattery.* 1> /dev/null 2>&1; then
echo "Killing old BLE battery services..."
svc -t /service/dbus-blebattery.*

# always remove existing blebattery services to cleanup
Expand Down Expand Up @@ -386,7 +387,8 @@ can_lenght=${#can_array[@]}
# echo $can_lenght

# stop all dbus-canbattery services, if at least one exists
if [ -d "/service/dbus-canbattery.0" ]; then
if ls /service/dbus-canbattery.* 1> /dev/null 2>&1; then
echo "Killing old CAN battery services..."
svc -t /service/dbus-canbattery.*

# always remove existing canbattery services to cleanup
Expand All @@ -395,10 +397,7 @@ if [ -d "/service/dbus-canbattery.0" ]; then
# kill all canbattery processes that remain
pkill -f "supervise dbus-canbattery.*"
pkill -f "multilog .* /var/log/dbus-canbattery.*"
pkill -f "python .*/dbus-serialbattery.py .*_Ble"

# kill opened bluetoothctl processes
pkill -f "^bluetoothctl "
pkill -f "python .*/dbus-serialbattery.py can.*"
fi


Expand Down
4 changes: 4 additions & 0 deletions etc/dbus-serialbattery/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def _get_list_from_config(

# save config values to constants


# --------- Battery Current limits ---------
MAX_BATTERY_CHARGE_CURRENT = float(config["DEFAULT"]["MAX_BATTERY_CHARGE_CURRENT"])
MAX_BATTERY_DISCHARGE_CURRENT = float(
Expand Down Expand Up @@ -250,6 +251,9 @@ def _get_list_from_config(
BATTERY_CAPACITY = float(config["DEFAULT"]["BATTERY_CAPACITY"])
INVERT_CURRENT_MEASUREMENT = int(config["DEFAULT"]["INVERT_CURRENT_MEASUREMENT"])

# -- JK BMS settings
JKBMS_CAN_CELL_COUNT = int(config["DEFAULT"]["JKBMS_CAN_CELL_COUNT"])

# -- ESC GreenMeter and Lipro device settings
GREENMETER_ADDRESS = int(config["DEFAULT"]["GREENMETER_ADDRESS"])
LIPRO_START_ADDRESS = int(config["DEFAULT"]["LIPRO_START_ADDRESS"])
Expand Down

0 comments on commit c925a1e

Please sign in to comment.