package main
import (
"log"
"time"
"gobot.io/x/gobot"
"gobot.io/x/gobot/drivers/i2c"
"gobot.io/x/gobot/platforms/raspi"
)
var (
servoMin = 150
servoMax = 700
maxDegree = 180
degIncrease = 10
yawDeg = 90
)
func degree2pulse(deg int) int32 {
pulse := servoMin
pulse += ((servoMax - servoMin) / maxDegree) * deg
return int32(pulse)
}
func adafruitServoMotorRunner(a *i2c.AdafruitMotorHatDriver) (err error) {
log.Printf("Servo Motor Run Loop...\n")
stackedHatAddr := 0x41
var channel byte = 1
deg := 90
a.SetServoHatAddress(stackedHatAddr)
freq := 60.0
if err = a.SetServoMotorFreq(freq); err != nil {
log.Printf("%s", err.Error())
return
}
pulse := degree2pulse(deg)
if err = a.SetServoMotorPulse(channel, 0, pulse); err != nil {
log.Printf(err.Error())
return
}
pulse = degree2pulse(deg + degIncrease)
if err = a.SetServoMotorPulse(channel, 0, pulse); err != nil {
log.Printf(err.Error())
return
}
time.Sleep(2000 * time.Millisecond)
pulse = degree2pulse(deg - degIncrease)
if err = a.SetServoMotorPulse(channel, 0, pulse); err != nil {
log.Printf(err.Error())
return
}
return
}
func main() {
r := raspi.NewAdaptor()
adaFruit := i2c.NewAdafruitMotorHatDriver(r)
work := func() {
gobot.Every(5*time.Second, func() {
adafruitServoMotorRunner(adaFruit)
})
}
robot := gobot.NewRobot("adaFruitBot",
[]gobot.Connection{r},
[]gobot.Device{adaFruit},
work,
)
robot.Start()
}