Skip to content

Commit

Permalink
Working Version needs calibration
Browse files Browse the repository at this point in the history
  • Loading branch information
markirb committed Dec 23, 2024
1 parent 8e8c2f8 commit 0b490e3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
4 changes: 2 additions & 2 deletions mos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
58 changes: 44 additions & 14 deletions src/BL0942/shelly_pm_bl0942.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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_);
Expand All @@ -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;

Expand All @@ -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_));
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/ShellyMini1PMGen3/shelly_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,19 @@ void CreatePeripherals(std::vector<std::unique_ptr<Input>> *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<std::unique_ptr<Component>> *comps,
std::vector<std::unique_ptr<mgos::hap::Accessory>> *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),
Expand Down

0 comments on commit 0b490e3

Please sign in to comment.