2023-05-20 20:25:21 +08:00
|
|
|
//go:build example
|
2017-05-05 11:20:32 +08:00
|
|
|
// +build example
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2017-05-05 11:20:32 +08:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
2017-05-04 06:08:56 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-05-05 10:51:58 +08:00
|
|
|
"fmt"
|
2017-05-04 06:08:56 +08:00
|
|
|
"time"
|
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/drivers/i2c"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/raspi"
|
2017-05-04 06:08:56 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
r := raspi.NewAdaptor()
|
|
|
|
ina := i2c.NewINA3221Driver(r)
|
|
|
|
|
|
|
|
work := func() {
|
|
|
|
gobot.Every(5*time.Second, func() {
|
2017-05-05 10:51:58 +08:00
|
|
|
for _, ch := range []i2c.INA3221Channel{i2c.INA3221Channel1, i2c.INA3221Channel2, i2c.INA3221Channel3} {
|
|
|
|
val, err := ina.GetBusVoltage(ch)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("INA3221Channel %v Bus Voltage error: %v\n", ch, err)
|
|
|
|
}
|
|
|
|
fmt.Printf("INA3221Channel %v Bus Voltage: %fV\n", ch, val)
|
|
|
|
|
|
|
|
val, err = ina.GetShuntVoltage(ch)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("INA3221Channel %v Shunt Voltage error: %v\n", ch, err)
|
|
|
|
}
|
|
|
|
fmt.Printf("INA3221Channel %v Shunt Voltage: %fV\n", ch, val)
|
|
|
|
|
|
|
|
val, err = ina.GetCurrent(ch)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("INA3221Channel %v Current error: %v\n", ch, err)
|
|
|
|
}
|
|
|
|
fmt.Printf("INA3221Channel %v Current: %fmA\n", ch, val)
|
|
|
|
|
|
|
|
val, err = ina.GetLoadVoltage(ch)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("INA3221Channel %v Load Voltage error: %v\n", ch, err)
|
|
|
|
}
|
|
|
|
fmt.Printf("INA3221Channel %v Load Voltage: %fV\n", ch, val)
|
2017-05-04 06:08:56 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-05-05 10:51:58 +08:00
|
|
|
robot := gobot.NewRobot("INA3221 Robot",
|
2017-05-04 06:08:56 +08:00
|
|
|
[]gobot.Connection{r},
|
|
|
|
[]gobot.Device{ina},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := robot.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-05-04 06:08:56 +08:00
|
|
|
}
|