Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RCT: add battery control #18178

Merged
merged 12 commits into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,5 @@ replace gopkg.in/yaml.v3 => github.com/andig/yaml v0.0.0-20240531135838-1ff5761a
replace github.com/grid-x/modbus => github.com/evcc-io/modbus v0.0.0-20241027151224-722a7a5ae529

replace github.com/lorenzodonini/ocpp-go => github.com/evcc-io/ocpp-go v0.0.0-20241230132027-815870498cc3

replace github.com/mlnoga/rct => github.com/Maschga/rct v0.1.2-0.20250112193044-2980bbd48edf
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/Maschga/rct v0.1.2-0.20250112193044-2980bbd48edf h1:OxSUCmjj+ovko/IdC2755mGRNn2G3ksaOkp74Fm0QAI=
github.com/Maschga/rct v0.1.2-0.20250112193044-2980bbd48edf/go.mod h1:0lfd2mmBnBzIvuzYtdhG+2371u+cUfIxsYErm4P9KRI=
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver/v3 v3.3.0 h1:B8LGeaivUe71a5qox1ICM/JLl0NqZSW5CHyL+hmvYS0=
Expand Down Expand Up @@ -450,8 +452,6 @@ github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:F
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/mlnoga/rct v0.1.2-0.20240421173556-1c5b75037e2f h1:63hbvjkIpccli0EA60fqvzokvU1fH1ZPiNzlF/vUmvM=
github.com/mlnoga/rct v0.1.2-0.20240421173556-1c5b75037e2f/go.mod h1:0lfd2mmBnBzIvuzYtdhG+2371u+cUfIxsYErm4P9KRI=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down
56 changes: 49 additions & 7 deletions meter/rct.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package meter

import (
"encoding/binary"
"errors"
"fmt"
"math"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -52,14 +54,15 @@ func init() {
registry.Add("rct", NewRCTFromConfig)
}

//go:generate decorate -f decorateRCT -b *RCT -r api.Meter -t "api.MeterEnergy,TotalEnergy,func() (float64, error)" -t "api.Battery,Soc,func() (float64, error)" -t "api.BatteryCapacity,Capacity,func() float64"
//go:generate decorate -f decorateRCT -b *RCT -r api.Meter -t "api.MeterEnergy,TotalEnergy,func() (float64, error)" -t "api.Battery,Soc,func() (float64, error)" -t "api.BatteryController,SetBatteryMode,func(api.BatteryMode) error" -t "api.BatteryCapacity,Capacity,func() float64"

// NewRCTFromConfig creates an RCT from generic config
func NewRCTFromConfig(other map[string]interface{}) (api.Meter, error) {
cc := struct {
capacity `mapstructure:",squash"`
Uri, Usage string
Cache time.Duration
capacity `mapstructure:",squash"`
Uri, Usage string
MinSoc, MaxSoc int
Cache time.Duration
}{
Cache: time.Second,
}
Expand All @@ -72,13 +75,13 @@ func NewRCTFromConfig(other map[string]interface{}) (api.Meter, error) {
return nil, errors.New("missing usage")
}

return NewRCT(cc.Uri, cc.Usage, cc.Cache, cc.capacity.Decorator())
return NewRCT(cc.Uri, cc.Usage, cc.MinSoc, cc.MaxSoc, cc.Cache, cc.capacity.Decorator())
}

var rctMu sync.Mutex

// NewRCT creates an RCT meter
func NewRCT(uri, usage string, cache time.Duration, capacity func() float64) (api.Meter, error) {
func NewRCT(uri, usage string, minSoc, maxSoc int, cache time.Duration, capacity func() float64) (api.Meter, error) {
rctMu.Lock()
defer rctMu.Unlock()

Expand All @@ -105,11 +108,50 @@ func NewRCT(uri, usage string, cache time.Duration, capacity func() float64) (ap

// decorate api.BatterySoc
var batterySoc func() (float64, error)
var batteryMode func(api.BatteryMode) error
if usage == "battery" {
batterySoc = m.batterySoc

batteryMode = func(mode api.BatteryMode) error {
switch mode {
case api.BatteryNormal:
if err := m.conn.Write(rct.PowerMngSocStrategy, []byte{rct.SOCTargetInternal}); err != nil {
return err
}

if err := m.conn.Write(rct.BatterySoCTargetMin, m.floatVal(float32(minSoc)/100)); err != nil {
return err
}

return m.conn.Write(rct.PowerMngBatteryPowerExternW, m.floatVal(float32(0)))
andig marked this conversation as resolved.
Show resolved Hide resolved

case api.BatteryHold:
if err := m.conn.Write(rct.PowerMngSocStrategy, []byte{rct.SOCTargetInternal}); err != nil {
return err
}

return m.conn.Write(rct.BatterySoCTargetMin, m.floatVal(float32(maxSoc)/100))

case api.BatteryCharge:
if err := m.conn.Write(rct.PowerMngBatteryPowerExternW, m.floatVal(float32(-10_000))); err != nil {
andig marked this conversation as resolved.
Show resolved Hide resolved
return err
}

return m.conn.Write(rct.PowerMngSocStrategy, []byte{rct.SOCTargetExternal})

default:
return api.ErrNotAvailable
}
}
}

return decorateRCT(m, totalEnergy, batterySoc, capacity), nil
return decorateRCT(m, totalEnergy, batterySoc, batteryMode, capacity), nil
}

func (m *RCT) floatVal(f float32) []byte {
data := make([]byte, 4)
binary.BigEndian.PutUint32(data, math.Float32bits(f))
return data
}

// CurrentPower implements the api.Meter interface
Expand Down
94 changes: 89 additions & 5 deletions meter/rct_decorators.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions templates/definition/meter/rct-power.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,28 @@ products:
- brand: RCT
description:
generic: Power
capabilities: ["battery-control"]
params:
- name: usage
choice: ["grid", "pv", "battery"]
allinone: true
- name: host
- name: capacity
advanced: true
# battery control
- name: minsoc
type: int
default: 7
advanced: true
- name: maxsoc
type: int
default: 97
advanced: true
render: |
type: rct
uri: {{ .host }}
usage: {{ .usage }}
cache: 2s
capacity: {{ .capacity }} # kWh
minsoc: {{ .minsoc }}
maxsoc: {{ .maxsoc }}
Loading