Skip to content

Commit

Permalink
Fix cell volltages for 3S BMS and apply the same to temperature sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
dreadnought committed Jul 12, 2021
1 parent cddafd0 commit f3928a3
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions dalybms/daly_bms.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def _format_message(self, command, extra=""):
self.logger.debug("w %s" % message_bytes.hex())
return message_bytes

def _read_request(self, command, extra="", max_responses=1):
def _read_request(self, command, extra="", max_responses=1, return_list=False):
"""
Sends a read request to the BMS and reads the response. In case it fails, it retries 'max_responses' times.
Expand All @@ -79,7 +79,8 @@ def _read_request(self, command, extra="", max_responses=1):
response_data = self._read(
command=command,
extra=extra,
max_responses=max_responses)
max_responses=max_responses,
return_list=return_list)
if not response_data:
self.logger.debug("%x. try failed, retrying..." % (x + 1))
time.sleep(0.2)
Expand All @@ -90,7 +91,7 @@ def _read_request(self, command, extra="", max_responses=1):
return False
return response_data

def _read(self, command, extra="", max_responses=1):
def _read(self, command, extra="", max_responses=1, return_list=False):
self.logger.debug("-- %s ------------------------" % command)
if not self.serial.isOpen():
self.serial.open()
Expand Down Expand Up @@ -125,13 +126,12 @@ def _read(self, command, extra="", max_responses=1):
if x == max_responses:
break

if (max_responses) == 1:
if len(response_data) == 1:
return response_data[0]
else:
return False

return response_data
if return_list or len(response_data) > 1:
return response_data
elif len(response_data) == 1:
return response_data[0]
else:
return False

def get_soc(self, response_data=None):
# SOC of Total Voltage Current
Expand Down Expand Up @@ -236,18 +236,25 @@ def get_status(self, response_data=None):
self.status = data
return data

def _calc_cell_voltage_responses(self):

def _calc_num_responses(self, status_field, num_per_frame):
if not self.status:
self.logger.error("get_status has to be called at least once before calling get_cell_voltages")
return False

# each response message includes 3 cell voltages
if self.address == 8:
# via Bluetooth the BMS returns 16 frames, even when they don't have data
max_responses = 16
# via Bluetooth the BMS returns all frames, even when they don't have data
if status_field == 'cell_voltages':
max_responses = 16
elif status_field == 'temperatures':
max_responses = 3
else:
self.logger.error("unkonwn status_field %s" % status_field)
return False
else:
# via UART/USB the BMS returns only frames that have data
max_responses = math.ceil(self.status["cells"] / 3)
max_responses = math.ceil(self.status[status_field] / num_per_frame)
return max_responses

def _split_frames(self, response_data, status_field, structure):
Expand All @@ -266,10 +273,10 @@ def _split_frames(self, response_data, status_field, structure):

def get_cell_voltages(self, response_data=None):
if not response_data:
max_responses = self._calc_cell_voltage_responses()
max_responses = self._calc_num_responses(status_field="cells", num_per_frame=3)
if not max_responses:
return
response_data = self._read_request("95", max_responses=max_responses)
response_data = self._read_request("95", max_responses=max_responses, return_list=True)
if not response_data:
return False

Expand All @@ -281,7 +288,10 @@ def get_cell_voltages(self, response_data=None):
def get_temperatures(self, response_data=None):
# Sensor temperatures
if not response_data:
response_data = self._read_request("96", max_responses=3)
max_responses = self._calc_num_responses(status_field="temperature_sensors", num_per_frame=7)
if not max_responses:
return
response_data = self._read_request("96", max_responses=max_responses, return_list=True)
if not response_data:
return False

Expand Down

0 comments on commit f3928a3

Please sign in to comment.