robocar-pca9685/pkg/actuator/pca9685.go

40 lines
836 B
Go
Raw Normal View History

2019-12-27 17:23:08 +00:00
package actuator
import (
2021-10-12 17:28:56 +00:00
"go.uber.org/zap"
"periph.io/x/conn/v3/i2c/i2creg"
"periph.io/x/conn/v3/physic"
"periph.io/x/devices/v3/pca9685"
"periph.io/x/host/v3"
2019-12-27 17:23:08 +00:00
)
var (
device *pca9685.Dev
)
func init() {
2021-10-12 17:28:56 +00:00
zap.S().Info("init pca9685 controller")
2019-12-27 17:23:08 +00:00
_, err := host.Init()
if err != nil {
2021-10-12 17:28:56 +00:00
zap.S().Fatalf("unable to init host: %v", err)
2019-12-27 17:23:08 +00:00
}
2021-10-12 17:28:56 +00:00
zap.S().Info("open i2c bus")
2019-12-27 17:23:08 +00:00
bus, err := i2creg.Open("")
if err != nil {
2021-10-12 17:28:56 +00:00
zap.S().Fatalf("unable to init i2c bus: %v", err)
2019-12-27 17:23:08 +00:00
}
2021-10-12 17:28:56 +00:00
zap.S().Info("i2c bus opened")
2019-12-27 17:23:08 +00:00
device, err = pca9685.NewI2C(bus, pca9685.I2CAddr)
if err != nil {
2021-10-12 17:28:56 +00:00
zap.S().Fatalf("unable to init pca9685 bus: %v", err)
2019-12-27 17:23:08 +00:00
}
2021-10-12 17:28:56 +00:00
zap.S().Infof("set pwm frequency to %d", 60)
2019-12-27 17:23:08 +00:00
err = device.SetPwmFreq(60 * physic.Hertz)
if err != nil {
2021-10-12 17:28:56 +00:00
zap.S().Panicf("unable to set pwm frequency: %v", err)
2019-12-27 17:23:08 +00:00
}
2021-10-12 17:28:56 +00:00
zap.S().Info("init done")
2019-12-27 17:23:08 +00:00
}