-
Notifications
You must be signed in to change notification settings - Fork 110
/
ina.go
348 lines (302 loc) · 9.06 KB
/
ina.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//go:build linux
// Package ina implements ina power sensors to measure voltage, current, and power
// INA219 datasheet: https://www.ti.com/lit/ds/symlink/ina219.pdf
// Example repo: https://github.com/periph/devices/blob/main/ina219/ina219.go
// INA226 datasheet: https://www.ti.com/lit/ds/symlink/ina226.pdf
// The voltage, current and power can be read as
// 16 bit big endian integers from their given registers.
// This value is multiplied by the register LSB to get the reading in nanounits.
// Voltage LSB: 1.25 mV for INA226, 4 mV for INA219
// Current LSB: maximum expected current of the system / (1 << 15)
// Power LSB: 25*CurrentLSB for INA226, 20*CurrentLSB for INA219
// The calibration register is programmed to measure current and power properly.
// The calibration register is set to: calibratescale / (currentLSB * senseResistor)
package ina
import (
"context"
"errors"
"fmt"
"strconv"
"sync"
"github.com/d2r2/go-i2c"
i2clog "github.com/d2r2/go-logger"
"go.viam.com/utils"
"go.viam.com/rdk/components/powersensor"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/resource"
)
const (
modelName219 = "ina219"
modelName226 = "ina226"
defaultI2Caddr = 0x40
configRegister = 0x00
shuntVoltageRegister = 0x01
busVoltageRegister = 0x02
powerRegister = 0x03
currentRegister = 0x04
calibrationRegister = 0x05
)
// values for inas in nano units so need to convert.
var (
senseResistor = toNano(0.1) // .1 ohm
maxCurrent219 = toNano(3.2) // 3.2 amp
maxCurrent226 = toNano(20) // 20 amp
)
// need to scale, making sure to not overflow int64.
var (
calibratescale219 = (toNano(1) * toNano(1) / 100000) << 12 // .04096 is internal fixed value for ina219
calibrateScale226 = (toNano(1) * toNano(1) / 100000) << 9 // .00512 is internal fixed value for ina226
)
var inaModels = []string{modelName219, modelName226}
// Config is used for converting config attributes.
type Config struct {
I2CBus string `json:"i2c_bus"`
I2cAddr int `json:"i2c_addr,omitempty"`
MaxCurrent float64 `json:"max_current_amps,omitempty"`
ShuntResistance float64 `json:"shunt_resistance,omitempty"`
}
// Validate ensures all parts of the config are valid.
func (conf *Config) Validate(path string) ([]string, error) {
var deps []string
if conf.I2CBus == "" {
return nil, resource.NewConfigValidationFieldRequiredError(path, "i2c_bus")
}
// The bus should be numeric. We store it as a string for consistency with other components,
// but we convert it to an int later, so let's check on that now.
if _, err := strconv.Atoi(conf.I2CBus); err != nil {
return nil, fmt.Errorf("i2c_bus must be numeric, not '%s': %w", conf.I2CBus, err)
}
return deps, nil
}
func init() {
for _, modelName := range inaModels {
localModelName := modelName
inaModel := resource.DefaultModelFamily.WithModel(modelName)
resource.RegisterComponent(
powersensor.API,
inaModel,
resource.Registration[powersensor.PowerSensor, *Config]{
Constructor: func(
ctx context.Context,
deps resource.Dependencies,
conf resource.Config,
logger logging.Logger,
) (powersensor.PowerSensor, error) {
newConf, err := resource.NativeConfig[*Config](conf)
if err != nil {
return nil, err
}
return newINA(conf.ResourceName(), newConf, logger, localModelName)
},
})
}
}
func newINA(
name resource.Name,
conf *Config,
logger logging.Logger,
modelName string,
) (powersensor.PowerSensor, error) {
err := i2clog.ChangePackageLogLevel("i2c", i2clog.InfoLevel)
if err != nil {
return nil, err
}
addr := conf.I2cAddr
if addr == 0 {
addr = defaultI2Caddr
logger.Infof("using i2c address : %d", defaultI2Caddr)
}
maxCurrent := toNano(conf.MaxCurrent)
if maxCurrent == 0 {
switch modelName {
case modelName219:
maxCurrent = maxCurrent219
logger.Info("using default max current 3.2A")
case modelName226:
maxCurrent = maxCurrent226
logger.Info("using default max current 20A")
}
}
resistance := toNano(conf.ShuntResistance)
if resistance == 0 {
resistance = senseResistor
logger.Info("using default resistor value 0.1 ohms")
}
busNumber, err := strconv.Atoi(conf.I2CBus)
if err != nil {
return nil, fmt.Errorf("non-numeric I2C bus number '%s': %w", conf.I2CBus, err)
}
s := &ina{
Named: name.AsNamed(),
logger: logger,
model: modelName,
bus: busNumber,
addr: byte(addr),
maxCurrent: maxCurrent,
resistance: resistance,
}
err = s.setCalibrationScale(modelName)
if err != nil {
return nil, err
}
return s, nil
}
// ina is a i2c sensor device that reports voltage, current and power.
type ina struct {
resource.Named
resource.AlwaysRebuild
resource.TriviallyCloseable
mu sync.Mutex
logger logging.Logger
model string
bus int
addr byte
currentLSB int64
powerLSB int64
cal uint16
maxCurrent int64
resistance int64
}
func (d *ina) setCalibrationScale(modelName string) error {
var calibratescale int64
d.currentLSB = d.maxCurrent / (1 << 15)
switch modelName {
case modelName219:
calibratescale = calibratescale219
d.powerLSB = (d.maxCurrent*20 + (1 << 14)) / (1 << 15)
case modelName226:
calibratescale = calibrateScale226
d.powerLSB = 25 * d.currentLSB
default:
return errors.New("ina model not supported")
}
// Calibration Register = calibration scale / (current LSB * Shunt Resistance)
// Where lsb is in Amps and resistance is in ohms.
// Calibration register is 16 bits.
cal := calibratescale / (d.currentLSB * d.resistance)
if cal >= (1 << 16) {
return fmt.Errorf("ina calibrate: calibration register value invalid %d", cal)
}
d.cal = uint16(cal)
return nil
}
func (d *ina) calibrate() error {
handle, err := i2c.NewI2C(d.addr, d.bus)
if err != nil {
d.logger.Errorf("can't open ina i2c: %s", err)
return err
}
defer utils.UncheckedErrorFunc(handle.Close)
// use the calibration result to set the scaling factor
// of the current and power registers for the maximum resolution
err = handle.WriteRegU16BE(calibrationRegister, d.cal)
if err != nil {
return err
}
// set the config register to all default values.
err = handle.WriteRegU16BE(configRegister, uint16(0x399F))
if err != nil {
return err
}
return nil
}
func (d *ina) Voltage(ctx context.Context, extra map[string]interface{}) (float64, bool, error) {
d.mu.Lock()
defer d.mu.Unlock()
handle, err := i2c.NewI2C(d.addr, d.bus)
if err != nil {
d.logger.CErrorf(ctx, "can't open ina i2c: %s", err)
return 0, false, err
}
defer utils.UncheckedErrorFunc(handle.Close)
bus, err := handle.ReadRegS16BE(busVoltageRegister)
if err != nil {
return 0, false, err
}
var voltage float64
switch d.model {
case modelName226:
// voltage is 1.25 mV/bit for the ina226
voltage = float64(bus) * 1.25e-3
case modelName219:
// lsb is 4mV, must shift right 3 bits
voltage = float64(bus>>3) * 4 / 1000
default:
}
isAC := false
return voltage, isAC, nil
}
func (d *ina) Current(ctx context.Context, extra map[string]interface{}) (float64, bool, error) {
d.mu.Lock()
defer d.mu.Unlock()
handle, err := i2c.NewI2C(d.addr, d.bus)
if err != nil {
d.logger.CErrorf(ctx, "can't open ina i2c: %s", err)
return 0, false, err
}
defer utils.UncheckedErrorFunc(handle.Close)
// Calibrate each time the current value is read, so if anything else is also writing to these registers
// we have the correct value.
err = d.calibrate()
if err != nil {
return 0, false, err
}
rawCur, err := handle.ReadRegS16BE(currentRegister)
if err != nil {
return 0, false, err
}
current := fromNano(float64(int64(rawCur) * d.currentLSB))
isAC := false
return current, isAC, nil
}
func (d *ina) Power(ctx context.Context, extra map[string]interface{}) (float64, error) {
d.mu.Lock()
defer d.mu.Unlock()
handle, err := i2c.NewI2C(d.addr, d.bus)
if err != nil {
d.logger.CErrorf(ctx, "can't open ina i2c handle: %s", err)
return 0, err
}
defer utils.UncheckedErrorFunc(handle.Close)
// Calibrate each time the power value is read, so if anything else is also writing to these registers
// we have the correct value.
err = d.calibrate()
if err != nil {
return 0, err
}
pow, err := handle.ReadRegS16BE(powerRegister)
if err != nil {
return 0, err
}
power := fromNano(float64(int64(pow) * d.powerLSB))
return power, nil
}
// Readings returns a map with voltage, current, power and isAC.
func (d *ina) Readings(ctx context.Context, extra map[string]interface{}) (map[string]interface{}, error) {
volts, isAC, err := d.Voltage(ctx, nil)
if err != nil {
d.logger.CErrorf(ctx, "failed to get voltage reading: %s", err.Error())
}
amps, _, err := d.Current(ctx, nil)
if err != nil {
d.logger.CErrorf(ctx, "failed to get current reading: %s", err.Error())
}
watts, err := d.Power(ctx, nil)
if err != nil {
d.logger.CErrorf(ctx, "failed to get power reading: %s", err.Error())
}
return map[string]interface{}{
"volts": volts,
"amps": amps,
"is_ac": isAC,
"watts": watts,
}, nil
}
func toNano(value float64) int64 {
nano := value * 1e9
return int64(nano)
}
func fromNano(value float64) float64 {
unit := value / 1e9
return unit
}