Merge pull request #32 from diebietse/fix_frequency
Some checks failed
build / inverter_gui_pipeline (push) Has been cancelled
Some checks failed
build / inverter_gui_pipeline (push) Has been cancelled
Add frequency clip check
This commit is contained in:
@@ -346,7 +346,7 @@ func (m *mk2Ser) dcDecode(frame []byte) {
|
||||
chargeC := m.applyScale(getUnsigned(frame[10:13]), ramVarIBat)
|
||||
m.info.BatCurrent = usedC - chargeC
|
||||
|
||||
m.info.OutFrequency = 10 / (m.applyScale(float64(frame[13]), ramVarInverterPeriod))
|
||||
m.info.OutFrequency = m.calcFreq(frame[13], ramVarInverterPeriod)
|
||||
logrus.Debugf("dcDecode %#v", m.info)
|
||||
|
||||
// Send L1 status request
|
||||
@@ -362,12 +362,8 @@ func (m *mk2Ser) acDecode(frame []byte) {
|
||||
m.info.InCurrent = m.applyScale(getSigned(frame[7:9]), ramVarIMains)
|
||||
m.info.OutVoltage = m.applyScale(getSigned(frame[9:11]), ramVarVInverter)
|
||||
m.info.OutCurrent = m.applyScale(getSigned(frame[11:13]), ramVarIInverter)
|
||||
m.info.InFrequency = m.calcFreq(frame[13], ramVarMainPeriod)
|
||||
|
||||
if frame[13] == 0xff {
|
||||
m.info.InFrequency = 0
|
||||
} else {
|
||||
m.info.InFrequency = 10 / (m.applyScale(float64(frame[13]), ramVarMainPeriod))
|
||||
}
|
||||
logrus.Debugf("acDecode %#v", m.info)
|
||||
|
||||
// Send status request
|
||||
@@ -376,6 +372,13 @@ func (m *mk2Ser) acDecode(frame []byte) {
|
||||
m.sendCommand(cmd)
|
||||
}
|
||||
|
||||
func (m *mk2Ser) calcFreq(data byte, scaleIndex int) float64 {
|
||||
if data == 0xff || data == 0x00 {
|
||||
return 0
|
||||
}
|
||||
return 10 / (m.applyScale(float64(data), scaleIndex))
|
||||
}
|
||||
|
||||
// Decode charge state of battery.
|
||||
func (m *mk2Ser) stateDecode(frame []byte) {
|
||||
m.info.ChargeState = m.applyScaleAndSign(frame[1:3], ramVarChargeState)
|
||||
|
||||
@@ -249,3 +249,63 @@ func Test_mk2Ser_scaleDecode(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_mk2Ser_calcFreq(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
scales []scaling
|
||||
data byte
|
||||
scaleIndex int
|
||||
want float64
|
||||
}{
|
||||
{
|
||||
name: "Calculate working low",
|
||||
scales: []scaling{
|
||||
{supported: false},
|
||||
},
|
||||
data: 0x01,
|
||||
scaleIndex: 0,
|
||||
want: 10,
|
||||
},
|
||||
{
|
||||
name: "Calculate working high",
|
||||
scales: []scaling{
|
||||
{
|
||||
supported: true,
|
||||
offset: 0,
|
||||
scale: 0.01,
|
||||
},
|
||||
},
|
||||
data: 0xFE,
|
||||
scaleIndex: 0,
|
||||
want: 3.937007874015748,
|
||||
},
|
||||
{
|
||||
name: "Calculate clip high",
|
||||
scales: []scaling{
|
||||
{supported: false},
|
||||
},
|
||||
data: 0xff,
|
||||
scaleIndex: 0,
|
||||
want: 0,
|
||||
},
|
||||
{
|
||||
name: "Calculate clip low",
|
||||
scales: []scaling{
|
||||
{supported: false},
|
||||
},
|
||||
data: 0x00,
|
||||
scaleIndex: 0,
|
||||
want: 0,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
m := &mk2Ser{
|
||||
scales: tt.scales,
|
||||
}
|
||||
got := m.calcFreq(tt.data, tt.scaleIndex)
|
||||
assert.InDelta(t, tt.want, got, testDelta)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user