forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabl-em4.go
233 lines (181 loc) Β· 5.51 KB
/
abl-em4.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package charger
// LICENSE
// Copyright (c) 2023 premultiply
// This module is NOT covered by the MIT license. All rights reserved.
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import (
"encoding/binary"
"fmt"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/modbus"
"github.com/evcc-io/evcc/util/sponsor"
)
const (
// per-unit registers
abl4Offset = 0x100
abl4RegCurrents = 0x3001 // 0.1A (3x)
abl4RegVoltages = 0x3007 // 0.1V (3x)
abl4RegPower = 0x300d // 1W
abl4RegEnergy = 0x300f // 0.01kWh
abl4RegStatus = 0x3031
abl4RegMaxCurrent = 0x3032 // 0.1A
)
// AblEm4 is an api.Charger implementation for ABL eM4 controller
type AblEm4 struct {
log *util.Logger
conn *modbus.Connection
base uint16
current uint16
}
func init() {
registry.Add("abl-em4", NewAblEm4FromConfig)
}
// NewAblEm4FromConfig creates an ABL eM4 charger from generic config
func NewAblEm4FromConfig(other map[string]interface{}) (api.Charger, error) {
cc := struct {
modbus.TcpSettings `mapstructure:",squash"`
Connector uint16
}{
TcpSettings: modbus.TcpSettings{
ID: 255, // default
},
Connector: 1,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
return NewAblEm4(cc.URI, cc.ID, cc.Connector)
}
// NewAblEm4 creates an ABL eM4 charger
func NewAblEm4(uri string, id uint8, connector uint16) (*AblEm4, error) {
conn, err := modbus.NewConnection(uri, "", "", 0, modbus.Tcp, id)
if err != nil {
return nil, err
}
if !sponsor.IsAuthorized() {
return nil, api.ErrSponsorRequired
}
log := util.NewLogger("abl-em4")
conn.Logger(log.TRACE)
wb := &AblEm4{
log: log,
conn: conn,
current: 60, // assume min current
}
wb.base = (connector - 1) * abl4Offset
// get initial state from charger
curr, err := wb.getCurrent()
if err != nil {
return nil, fmt.Errorf("current limit: %w", err)
}
if curr > 0 {
wb.current = curr
}
return wb, nil
}
func (wb *AblEm4) setCurrent(current uint16) error {
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, current)
_, err := wb.conn.WriteMultipleRegisters(wb.base+abl4RegMaxCurrent, 1, b)
return err
}
func (wb *AblEm4) getCurrent() (uint16, error) {
b, err := wb.conn.ReadHoldingRegisters(wb.base+abl4RegMaxCurrent, 1)
if err != nil {
return 0, err
}
return binary.BigEndian.Uint16(b), nil
}
// Status implements the api.Charger interface
func (wb *AblEm4) Status() (api.ChargeStatus, error) {
b, err := wb.conn.ReadHoldingRegisters(wb.base+abl4RegStatus, 1)
if err != nil {
return api.StatusNone, err
}
r := rune(b[1]>>4-0x0A) + 'A'
switch r {
case 'A', 'B', 'C':
return api.ChargeStatus(r), nil
default:
status, ok := ablStatus[b[1]]
if !ok {
status = string(r)
}
return api.StatusNone, fmt.Errorf("invalid status: %s", status)
}
}
// Enabled implements the api.Charger interface
func (wb *AblEm4) Enabled() (bool, error) {
curr, err := wb.getCurrent()
return curr > 0, err
}
// Enable implements the api.Charger interface
func (wb *AblEm4) Enable(enable bool) error {
var current uint16
if enable {
current = wb.current
}
return wb.setCurrent(current)
}
// MaxCurrent implements the api.Charger interface
func (wb *AblEm4) MaxCurrent(current int64) error {
return wb.MaxCurrentMillis(float64(current))
}
var _ api.ChargerEx = (*AblEm4)(nil)
// MaxCurrentMillis implements the api.ChargerEx interface
func (wb *AblEm4) MaxCurrentMillis(current float64) error {
if current < 6 {
return fmt.Errorf("invalid current %.1f", current)
}
wb.current = uint16(current * 10)
return wb.setCurrent(wb.current)
}
var _ api.Meter = (*AblEm4)(nil)
// currentPower implements the api.Meter interface
func (wb *AblEm4) CurrentPower() (float64, error) {
b, err := wb.conn.ReadHoldingRegisters(wb.base+abl4RegPower, 2)
if err != nil {
return 0, err
}
return float64(binary.BigEndian.Uint32(b)), nil
}
var _ api.MeterEnergy = (*AblEm4)(nil)
// totalEnergy implements the api.MeterEnergy interface
func (wb *AblEm4) TotalEnergy() (float64, error) {
b, err := wb.conn.ReadHoldingRegisters(wb.base+abl4RegEnergy, 2)
if err != nil {
return 0, err
}
return float64(binary.BigEndian.Uint32(b)) * 0.01, nil
}
// getPhaseValues returns 3 sequential register values
func (wb *AblEm4) getPhaseValues(reg uint16) (float64, float64, float64, error) {
b, err := wb.conn.ReadHoldingRegisters(reg, 6)
if err != nil {
return 0, 0, 0, err
}
var res [3]float64
for i := range res {
res[i] = float64(binary.BigEndian.Uint32(b[4*i:])) / 10
}
return res[0], res[1], res[2], nil
}
var _ api.PhaseCurrents = (*AblEm4)(nil)
// Currents implements the api.PhaseCurrents interface
func (wb *AblEm4) Currents() (float64, float64, float64, error) {
return wb.getPhaseValues(wb.base + abl4RegCurrents)
}
var _ api.PhaseVoltages = (*AblEm4)(nil)
// Voltages implements the api.PhaseVoltages interface
func (wb *AblEm4) Voltages() (float64, float64, float64, error) {
return wb.getPhaseValues(wb.base + abl4RegVoltages)
}