From 0b490e3ddb638b5cf402b9e8caecf9024400a1a1 Mon Sep 17 00:00:00 2001 From: Markus Kirberg Date: Mon, 23 Dec 2024 14:53:33 +0000 Subject: [PATCH] Working Version needs calibration --- mos.yml | 4 +- src/BL0942/shelly_pm_bl0942.cpp | 58 ++++++++++++++++++++------- src/ShellyMini1PMGen3/shelly_init.cpp | 10 +++++ 3 files changed, 56 insertions(+), 16 deletions(-) diff --git a/mos.yml b/mos.yml index ef650d24..b221555d 100644 --- a/mos.yml +++ b/mos.yml @@ -136,10 +136,10 @@ config_schema: - ["gains.phcalb", "f", 0, {title: ""}] - ["scales", "o", {title: "", abstract: true}] - - ["scales.voltage_scale", "f", 1, {title: ""}] #default register values are 0x40000 + - ["scales.voltage_scale", "f", 1, {title: ""}] - ["scales.current_scale", "f", 1, {title: ""}] - ["scales.aenergy_scale", "f", 1, {title: ""}] - - ["scales.apower_scale", "f", 1, {title: ""}] + - ["scales.apower_scale", "f", 1, {title: ""}] build_vars: # BLE disabled for most models. diff --git a/src/BL0942/shelly_pm_bl0942.cpp b/src/BL0942/shelly_pm_bl0942.cpp index 4d3529f6..b414440f 100644 --- a/src/BL0942/shelly_pm_bl0942.cpp +++ b/src/BL0942/shelly_pm_bl0942.cpp @@ -23,8 +23,8 @@ struct packet { uint8_t frame_header; - uint8_t i_rms[2]; - uint8_t rms[3]; + uint8_t i_rms[3]; + uint8_t v_rms[3]; uint8_t i_fast_rms[3]; uint8_t watt[3]; uint8_t cf_cnt[3]; @@ -89,10 +89,10 @@ Status BL0942PowerMeter::Init() { LOG(LL_INFO, ("BL0942 @ %d/%d", rx_pin_, tx_pin_)); this->WriteReg(BL_SOFT_RESET, 0x5a5a5a); - this->WriteReg(BL_USR_WRPROT, 0x550000); - this->WriteReg(BL_MODE, 0x001000); - this->WriteReg(BL_TPS_CTRL, 0xFF4700); - this->WriteReg(BL_I_FAST_RMS_CTRL, 0x1C1800); + //this->WriteReg(BL_USR_WRPROT, 0x550000); + //this->WriteReg(BL_MODE, 0x001000); + //this->WriteReg(BL_TPS_CTRL, 0xFF4700); + //this->WriteReg(BL_I_FAST_RMS_CTRL, 0x1C1800); return Status::OK(); } @@ -124,6 +124,7 @@ bool BL0942PowerMeter::WriteReg(uint8_t reg, uint32_t val) { } bool BL0942PowerMeter::ReadReg(uint8_t reg, uint8_t *rx_buf, size_t len) { + bool whole_packet = (len == 23); uint8_t tx_buf[2] = {BL_READ | BL_ADDR, reg}; mgos_uart_write(uart_no_, tx_buf, 2); mgos_uart_flush(uart_no_); @@ -134,9 +135,12 @@ bool BL0942PowerMeter::ReadReg(uint8_t reg, uint8_t *rx_buf, size_t len) { int read_len = mgos_uart_read(uart_no_, rx_buf, len); - uint8_t chksum = tx_buf[0] + tx_buf[1]; - for (int i = 0; i < len; i++) { + uint8_t chksum = + tx_buf[0] + + (whole_packet ? 0 : tx_buf[1]); // ignore tx_buf[1] when reading packet + for (int i = 0; i < len - 1; i++) { chksum += rx_buf[i]; + // LOG(LL_INFO, ("data %i:%02X", i, rx_buf[i])); } chksum ^= 0xFF; @@ -147,17 +151,43 @@ bool BL0942PowerMeter::ReadReg(uint8_t reg, uint8_t *rx_buf, size_t len) { return true; } -uint32_t convert_le24(uint8_t v[3]){ - return ((uint32_t)v[2] << 16) | (uint32_t)(v[1] << 8) | v[0]; +uint32_t convert_le24(uint8_t v[3]) { + return ((uint32_t) v[2] << 16) | (uint32_t) (v[1] << 8) | v[0]; +} + +uint32_t convert_le16(uint8_t v[2]) { + return (uint32_t) (v[1] << 8) | v[0]; } void BL0942PowerMeter::MeasureTimerCB() { packet rx_buf; - if (this->ReadReg(0xAA, (uint8_t *) rx_buf, sizeof(rx_buf))) { - if (rx_buf.frame_header == 0x55) { - // TODO preocess readings: - uint32_t cf = convert_le24(rx_buf.cf_count); + static uint32_t cf_cnt = 0; + if (this->ReadReg(0xAA, (uint8_t *) &rx_buf, sizeof(rx_buf))) { + if (rx_buf.frame_header == 0x55) { + uint32_t cf = convert_le24(rx_buf.cf_cnt); + cf = (cf_cnt & 0xFF000000) | cf; + if (cf_cnt > cf) { + cf += 0x1000000; + } + cf_cnt = cf; + + float wref = (3537 / (1.218 * 1.218 * 4)); + float vref = (73989 / (1.218 * 4)); + float iref = (305978 / (1.218)); + + float vo = convert_le24(rx_buf.v_rms) / vref; + float vi = convert_le24(rx_buf.i_rms) / iref; + int32_t wa_tmp = convert_le24(rx_buf.watt); + if(wa_tmp & 0x800000) { + wa_tmp |= 0xFF000000;} + float wa = wa_tmp / wref; + float fr = 1000000.0 / (float)convert_le16(rx_buf.frequency); + + apa_ = wa; + aea_ = cf / (wref * 3600 / (1638.4 * 256)); + + LOG(LL_INFO, ("vo: %.1f wa: %.2f i: %.2f fr: %.2f ae: %.2f", vo, wa, vi, fr, aea_)); } } } diff --git a/src/ShellyMini1PMGen3/shelly_init.cpp b/src/ShellyMini1PMGen3/shelly_init.cpp index f4df6ce7..113f88ff 100644 --- a/src/ShellyMini1PMGen3/shelly_init.cpp +++ b/src/ShellyMini1PMGen3/shelly_init.cpp @@ -54,9 +54,19 @@ void CreatePeripherals(std::vector> *inputs, InitSysBtn(BTN_GPIO, BTN_DOWN); } +void PrintCalibrationData() { + mgos_config_factory *c = &(mgos_sys_config.factory); + LOG(LL_INFO, ("calibration.done %Q", c->calib.done)); + mgos_config_scales *s = &c->calib.scales0; + LOG(LL_INFO, ("gains vs: %f cs: %f ps: %f es: %f", s->voltage_scale, + s->current_scale, s->apower_scale, s->aenergy_scale)); +} + void CreateComponents(std::vector> *comps, std::vector> *accs, HAPAccessoryServerRef *svr) { + void PrintCalibrationData(); + bool gdo_mode = mgos_sys_config_get_shelly_mode() == (int) Mode::kGarageDoor; if (gdo_mode) { hap::CreateHAPGDO(1, FindInput(1), FindInput(2), FindOutput(1),