Skip to content

Commit

Permalink
Introduce a configurable tx_timeout to handle reluctant BMS responses (
Browse files Browse the repository at this point in the history
  • Loading branch information
syssi authored Jul 18, 2022
1 parent e2555bd commit 47480ad
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 1 deletion.
5 changes: 5 additions & 0 deletions components/jbd_bms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

CONF_JBD_BMS_ID = "jbd_bms_id"
CONF_ENABLE_FAKE_TRAFFIC = "enable_fake_traffic"
CONF_RX_TIMEOUT = "rx_timeout"

jbd_bms_ns = cg.esphome_ns.namespace("jbd_bms")
JbdBms = jbd_bms_ns.class_("JbdBms", cg.PollingComponent, uart.UARTDevice)
Expand All @@ -18,6 +19,9 @@
{
cv.GenerateID(): cv.declare_id(JbdBms),
cv.Optional(CONF_ENABLE_FAKE_TRAFFIC, default=False): cv.boolean,
cv.Optional(
CONF_RX_TIMEOUT, default="150ms"
): cv.positive_time_period_milliseconds,
}
)
.extend(cv.polling_component_schema("2s"))
Expand All @@ -31,3 +35,4 @@ async def to_code(config):
await uart.register_uart_device(var, config)

cg.add(var.set_enable_fake_traffic(config[CONF_ENABLE_FAKE_TRAFFIC]))
cg.add(var.set_rx_timeout(config[CONF_RX_TIMEOUT]))
3 changes: 2 additions & 1 deletion components/jbd_bms/jbd_bms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void JbdBms::setup() { this->send_command_(JBD_CMD_READ, JBD_CMD_HWVER); }
void JbdBms::loop() {
const uint32_t now = millis();

if (now - this->last_byte_ > 50) {
if (now - this->last_byte_ > this->rx_timeout_) {
this->rx_buffer_.clear();
this->last_byte_ = now;
}
Expand Down Expand Up @@ -344,6 +344,7 @@ void JbdBms::dump_config() { // NOLINT(google-readability-function-size,readabi
LOG_SENSOR("", "Cell Voltage 30", this->cells_[29].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 31", this->cells_[30].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 32", this->cells_[31].cell_voltage_sensor_);
ESP_LOGCONFIG(TAG, " RX timeout: %d ms", this->rx_timeout_);
}
float JbdBms::get_setup_priority() const {
// After UART bus
Expand Down
2 changes: 2 additions & 0 deletions components/jbd_bms/jbd_bms.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class JbdBms : public uart::UARTDevice, public PollingComponent {
device_model_text_sensor_ = device_model_text_sensor;
}
void set_enable_fake_traffic(bool enable_fake_traffic) { enable_fake_traffic_ = enable_fake_traffic; }
void set_rx_timeout(uint16_t rx_timeout) { rx_timeout_ = rx_timeout; }
void write_register(uint8_t address, uint16_t value);

protected:
Expand Down Expand Up @@ -160,6 +161,7 @@ class JbdBms : public uart::UARTDevice, public PollingComponent {
std::vector<uint8_t> rx_buffer_;
uint32_t last_byte_{0};
uint32_t last_send_{0};
uint16_t rx_timeout_{150};
bool enable_fake_traffic_;

void on_jbd_bms_data_(const uint8_t &function, const std::vector<uint8_t> &data);
Expand Down
1 change: 1 addition & 0 deletions esp32-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ uart:
direction: BOTH

jbd_bms:
rx_timeout: 150ms

binary_sensor:
- platform: jbd_bms
Expand Down
1 change: 1 addition & 0 deletions esp8266-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ uart:
direction: BOTH

jbd_bms:
rx_timeout: 150ms

binary_sensor:
- platform: jbd_bms
Expand Down

0 comments on commit 47480ad

Please sign in to comment.