From 0f17e4e6164874cf9e4b75aeb181dbe0c53e934c Mon Sep 17 00:00:00 2001 From: atbore-phx Date: Sat, 2 Mar 2024 17:07:07 +0100 Subject: [PATCH] feat(fronius): tuning --- pkg/fronius/configure.go | 21 +++++++++++++-------- pkg/fronius/schedule.go | 15 ++++++++------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/pkg/fronius/configure.go b/pkg/fronius/configure.go index ea34a30..b700be8 100644 --- a/pkg/fronius/configure.go +++ b/pkg/fronius/configure.go @@ -74,17 +74,22 @@ func Setdefaults(modbus_ip string) error { } func ForceCharge(modbus_ip string, power_prc int16) error { - regList := mdsc + if power_prc > 0 { + regList := mdsc - regList[StorCtl_Mod] = 2 // Limit Decharging - regList[OutWRte] = -100 * power_prc + regList[StorCtl_Mod] = 2 // Limit Decharging + regList[OutWRte] = -100 * power_prc - OpenModbusClient(modbus_ip) + OpenModbusClient(modbus_ip) - WriteFroniusModbusRegisters(regList) - ReadFroniusModbusRegisters(regList) - - ClosemodbusClient() + WriteFroniusModbusRegisters(regList) + ReadFroniusModbusRegisters(regList) + ClosemodbusClient() + } else if power_prc == 0 { + Setdefaults(modbus_ip) + } else { + panic(fmt.Errorf("someting goes wrong when force charging, percent of charging is negative: %d", power_prc)) + } return nil } diff --git a/pkg/fronius/schedule.go b/pkg/fronius/schedule.go index 0b1b78b..3afe8c0 100644 --- a/pkg/fronius/schedule.go +++ b/pkg/fronius/schedule.go @@ -9,25 +9,26 @@ func SetFroniusChargeBatteryMode(pw_forecast float64, pw_batt2charge float64, pw var ch_pc int16 = 0 time, _ := CheckTimeRange(start_hr, end_hr) - if !time || pw_batt2charge == 0 { // out of the time range => do not charge + if !time || pw_batt2charge == 0 { // out of the time range or battery 100% => do not charge fmt.Printf("Out time range start_time: %s - end_time: %s\n", start_hr, end_hr) Setdefaults(fronius_ip) } else { // in the time range pw_pv_net := pw_forecast - pw_consumption + // TODO: we know that from Estimate via Solar API, use that vaule instead. OpenModbusClient(fronius_ip) pw_max, _ := ReadFroniusModbusRegister(WChaMax) ClosemodbusClient() - if pw_pv_net > 0 { // there is pv power available + if pw_pv_net <= 0 { // net pv power is not enough => charge the diff + ch_pc = SetChargePower(float64(pw_max), -1*pw_pv_net, float64(max_charge)) + ForceCharge(fronius_ip, ch_pc) + } else { // there is pv power available pw_grid := pw_batt2charge - pw_pv_net - if pw_grid > 0 { // and it's less than battery capacity to charge => charge the the diff + if pw_grid > 0 { // it's less than the battery's capacity to charge => charge the the diff ch_pc = SetChargePower(float64(pw_max), pw_grid, float64(max_charge)) ForceCharge(fronius_ip, ch_pc) - } else { // or it is bigger than than battery capacity to charge => do not charge + } else { // it is bigger than than battery capacity to charge => do not charge Setdefaults(fronius_ip) } - } else { // or pv power is not enough => charge the diff - ch_pc = SetChargePower(float64(pw_max), -1*pw_pv_net, float64(max_charge)) - ForceCharge(fronius_ip, ch_pc) } } return ch_pc, nil