robocar-pca9685/actuator/pca9685.go

40 lines
844 B
Go
Raw Permalink Normal View History

2019-12-27 17:23:08 +00:00
package actuator
import (
2020-02-03 18:15:51 +00:00
log "github.com/sirupsen/logrus"
2019-12-27 17:23:08 +00:00
"periph.io/x/periph/conn/i2c/i2creg"
"periph.io/x/periph/conn/physic"
"periph.io/x/periph/experimental/devices/pca9685"
"periph.io/x/periph/host"
)
var (
device *pca9685.Dev
)
func init() {
2020-02-03 18:15:51 +00:00
log.Info("init pca9685 controller")
2019-12-27 17:23:08 +00:00
_, err := host.Init()
if err != nil {
log.Fatalf("unable to init host: %v", err)
}
2020-02-03 18:15:51 +00:00
log.Info("open i2c bus")
2019-12-27 17:23:08 +00:00
bus, err := i2creg.Open("")
if err != nil {
log.Fatalf("unable to init i2c bus: %v", err)
}
2020-02-03 18:15:51 +00:00
log.Info("i2c bus opened")
2019-12-27 17:23:08 +00:00
device, err = pca9685.NewI2C(bus, pca9685.I2CAddr)
if err != nil {
log.Fatalf("unable to init pca9685 bus: %v", err)
}
2020-02-03 18:15:51 +00:00
log.Infof("set pwm frequency to %d", 60)
2019-12-27 17:23:08 +00:00
err = device.SetPwmFreq(60 * physic.Hertz)
if err != nil {
2020-02-03 18:15:51 +00:00
log.Panicf("unable to set pwm frequency: %v", err)
2019-12-27 17:23:08 +00:00
}
2020-02-03 18:15:51 +00:00
log.Info("init done")
2019-12-27 17:23:08 +00:00
}