1
0
mirror of https://github.com/kidoman/embd synced 2024-06-07 03:27:48 +02:00

fix bug with negative currents and add Power

This commit is contained in:
Cullen Jennings 2016-05-22 09:33:55 -06:00
parent 69655b6c03
commit e87156263c
2 changed files with 30 additions and 6 deletions

View File

@ -23,7 +23,7 @@ func main() {
bus := embd.NewI2CBus(1) bus := embd.NewI2CBus(1)
ina := ina219.New(bus, 0x40, 0.1) ina := ina219.New(bus, 0x40, 0.001)
defer ina.Close() defer ina.Close()
for { for {
@ -42,7 +42,12 @@ func main() {
panic(err) panic(err)
} }
fmt.Printf("Shunt Voltage=%v Voltage=%v Current=%v\n", sv, v, c) p, err := ina.Power()
if err != nil {
panic(err)
}
fmt.Printf("Shunt Voltage=%v Voltage=%v Current=%v Power=%v \n", sv, v, c, p)
time.Sleep(500 * time.Millisecond) time.Sleep(500 * time.Millisecond)
} }

View File

@ -3,6 +3,7 @@
// interfcace. // interfcace.
// TODO - add options to config voltage range, sensitivity and averaging // TODO - add options to config voltage range, sensitivity and averaging
// TODO - add documentation
package ina219 package ina219
@ -54,7 +55,10 @@ func (d *INA219) setup() error {
d.mu.Lock() d.mu.Lock()
defer d.mu.Unlock() defer d.mu.Unlock()
config := uint16(0x219F) // 12, bit no integration, 32v bus range , 40mV shunt range //config := uint16(0x219F) // 12, bit no integration, 32v bus range , 40mV shunt range
//config := uint16(0x2777) // 12, bit 64x integration, 32v bus range , 40mV shunt range
config := uint16(0x2F77) // 12, bit 64x integration, 32v bus range , 80mV shunt range
err := d.Bus.WriteWordToReg(d.address, configReg, config) err := d.Bus.WriteWordToReg(d.address, configReg, config)
if err != nil { if err != nil {
return err return err
@ -62,7 +66,7 @@ func (d *INA219) setup() error {
calib := uint16(0.5 + 40.96/d.shuntResitance) calib := uint16(0.5 + 40.96/d.shuntResitance)
err = d.Bus.WriteWordToReg(d.address, calibReg, calib) // .1 ohm sense resitor err = d.Bus.WriteWordToReg(d.address, calibReg, calib)
if err != nil { if err != nil {
return err return err
} }
@ -84,7 +88,7 @@ func (d *INA219) ShuntVoltage() (float64, error) {
return math.NaN(), err return math.NaN(), err
} }
voltage := float64(v) / 100000.0 voltage := float64( int16(v) ) / 100000.0
return voltage, nil return voltage, nil
} }
@ -114,7 +118,22 @@ func (d *INA219) Current() (float64, error) {
return math.NaN(), err return math.NaN(), err
} }
current := float64(v) / 1000.0 current := float64( int16(v) ) / 1000.0
return current, nil
}
func (d *INA219) Power() (float64, error) {
if err := d.setup(); err != nil {
return math.NaN(), err
}
v, err := d.Bus.ReadWordFromReg(d.address, powerReg)
if err != nil {
return math.NaN(), err
}
current := float64( int16(v) ) / 50.0
return current, nil return current, nil
} }