2023-05-20 20:25:21 +08:00
|
|
|
//go:build example
|
2017-03-13 23:01:39 +08:00
|
|
|
// +build example
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2017-03-13 23:01:39 +08:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
2016-08-26 12:04:36 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"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"
|
2016-08-26 12:04:36 +08:00
|
|
|
)
|
|
|
|
|
2023-10-30 02:35:26 +08:00
|
|
|
const (
|
2016-08-26 12:04:36 +08:00
|
|
|
// Min pulse length out of 4096
|
|
|
|
servoMin = 150
|
|
|
|
// Max pulse length out of 4096
|
|
|
|
servoMax = 700
|
|
|
|
// Limiting the max this servo can rotate (in deg)
|
|
|
|
maxDegree = 180
|
|
|
|
// Number of degrees to increase per call
|
|
|
|
degIncrease = 10
|
|
|
|
yawDeg = 90
|
|
|
|
)
|
|
|
|
|
2023-10-30 02:35:26 +08:00
|
|
|
func main() {
|
|
|
|
r := raspi.NewAdaptor()
|
2016-10-16 01:55:43 +08:00
|
|
|
|
2016-08-26 12:04:36 +08:00
|
|
|
// Changing from the default 0x40 address because this configuration involves
|
|
|
|
// a Servo HAT stacked on top of a DC/Stepper Motor HAT on top of the Pi.
|
|
|
|
stackedHatAddr := 0x41
|
2023-10-30 02:35:26 +08:00
|
|
|
|
|
|
|
adaFruit := i2c.NewAdafruit2327Driver(r, i2c.WithAddress(stackedHatAddr))
|
|
|
|
|
|
|
|
work := func() {
|
|
|
|
gobot.Every(5*time.Second, func() {
|
|
|
|
adafruitServoMotorRunner(adaFruit)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("adaFruitBot",
|
|
|
|
[]gobot.Connection{r},
|
|
|
|
[]gobot.Device{adaFruit},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
|
|
|
robot.Start()
|
|
|
|
}
|
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
func adafruitServoMotorRunner(a *i2c.Adafruit2327Driver) error {
|
2023-10-30 02:35:26 +08:00
|
|
|
log.Printf("Servo Motor Run Loop...\n")
|
|
|
|
|
2016-08-26 12:04:36 +08:00
|
|
|
var channel byte = 1
|
|
|
|
deg := 90
|
|
|
|
|
|
|
|
// Do not need to set this every run loop
|
|
|
|
freq := 60.0
|
2023-11-16 03:51:52 +08:00
|
|
|
if err := a.SetServoMotorFreq(freq); err != nil {
|
2016-08-26 12:04:36 +08:00
|
|
|
log.Printf("%s", err.Error())
|
2023-11-16 03:51:52 +08:00
|
|
|
return err
|
2016-08-26 12:04:36 +08:00
|
|
|
}
|
|
|
|
// start in the middle of the 180-deg range
|
|
|
|
pulse := degree2pulse(deg)
|
2023-11-16 03:51:52 +08:00
|
|
|
if err := a.SetServoMotorPulse(channel, 0, pulse); err != nil {
|
2016-08-26 12:04:36 +08:00
|
|
|
log.Printf(err.Error())
|
2023-11-16 03:51:52 +08:00
|
|
|
return err
|
2016-08-26 12:04:36 +08:00
|
|
|
}
|
|
|
|
// INCR
|
|
|
|
pulse = degree2pulse(deg + degIncrease)
|
2023-11-16 03:51:52 +08:00
|
|
|
if err := a.SetServoMotorPulse(channel, 0, pulse); err != nil {
|
2016-08-26 12:04:36 +08:00
|
|
|
log.Printf(err.Error())
|
2023-11-16 03:51:52 +08:00
|
|
|
return err
|
2016-08-26 12:04:36 +08:00
|
|
|
}
|
2016-11-05 20:05:49 +08:00
|
|
|
time.Sleep(2000 * time.Millisecond)
|
2016-08-26 12:04:36 +08:00
|
|
|
// DECR
|
|
|
|
pulse = degree2pulse(deg - degIncrease)
|
2023-11-16 03:51:52 +08:00
|
|
|
if err := a.SetServoMotorPulse(channel, 0, pulse); err != nil {
|
2016-08-26 12:04:36 +08:00
|
|
|
log.Printf(err.Error())
|
2023-11-16 03:51:52 +08:00
|
|
|
return err
|
2016-08-26 12:04:36 +08:00
|
|
|
}
|
2023-11-16 03:51:52 +08:00
|
|
|
return nil
|
2016-08-26 12:04:36 +08:00
|
|
|
}
|
2016-10-16 01:55:43 +08:00
|
|
|
|
2023-10-30 02:35:26 +08:00
|
|
|
func degree2pulse(deg int) int32 {
|
|
|
|
pulse := servoMin
|
|
|
|
pulse += ((servoMax - servoMin) / maxDegree) * deg
|
|
|
|
return int32(pulse)
|
2016-08-26 12:04:36 +08:00
|
|
|
}
|