1
0
mirror of https://github.com/kidoman/embd synced 2024-11-10 19:48:53 +01:00
embd/samples/servo.go

62 lines
862 B
Go
Raw Normal View History

2014-03-02 08:06:34 +01:00
// +build ignore
package main
import (
"flag"
"os"
"os/signal"
"time"
2014-03-02 20:21:23 +01:00
"github.com/kidoman/embd"
2014-02-10 00:35:41 +01:00
"github.com/kidoman/embd/controller/pca9685"
"github.com/kidoman/embd/motion/servo"
_ "github.com/kidoman/embd/host/all"
)
func main() {
flag.Parse()
2014-03-02 20:21:23 +01:00
if err := embd.InitI2C(); err != nil {
panic(err)
}
2014-03-02 20:21:23 +01:00
defer embd.CloseI2C()
2014-03-02 20:21:23 +01:00
bus := embd.NewI2CBus(1)
d := pca9685.New(bus, 0x41)
d.Freq = 50
defer d.Close()
pwm := d.ServoChannel(0)
servo := servo.New(pwm)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
turnTimer := time.Tick(500 * time.Millisecond)
left := true
servo.SetAngle(90)
defer func() {
servo.SetAngle(90)
}()
for {
select {
case <-turnTimer:
left = !left
switch left {
case true:
servo.SetAngle(70)
case false:
servo.SetAngle(110)
}
case <-c:
return
}
}
}