mirror of
https://github.com/kidoman/embd
synced 2025-07-03 11:57:38 +02:00
- Added pca9685 package 16-channel, 12-bit PWM controller with I2C
- Added servo package to control servos using pca9685 package
This commit is contained in:
parent
1a6f97f102
commit
e7503cfcd7
4 changed files with 443 additions and 0 deletions
46
samples/pca9685.go
Normal file
46
samples/pca9685.go
Normal file
|
@ -0,0 +1,46 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"time"
|
||||
|
||||
"github.com/kid0m4n/go-rpi/controller/pca9685"
|
||||
"github.com/kid0m4n/go-rpi/i2c"
|
||||
)
|
||||
|
||||
func main() {
|
||||
bus, err := i2c.NewBus(1)
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
||||
pca9685 := pca9685.New(bus, 0x41, 1000)
|
||||
pca9685.SetDebug(true)
|
||||
defer pca9685.Close()
|
||||
|
||||
if err := pca9685.SetPwm(15, 0, 1000); err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, os.Interrupt, os.Kill)
|
||||
|
||||
timer := time.Tick(2 * time.Second)
|
||||
sleeping := false
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-timer:
|
||||
sleeping = !sleeping
|
||||
if sleeping {
|
||||
pca9685.Sleep()
|
||||
} else {
|
||||
pca9685.Wake()
|
||||
}
|
||||
case <-c:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
51
samples/servo.go
Normal file
51
samples/servo.go
Normal file
|
@ -0,0 +1,51 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"time"
|
||||
|
||||
"github.com/kid0m4n/go-rpi/controller/pca9685"
|
||||
"github.com/kid0m4n/go-rpi/i2c"
|
||||
"github.com/kid0m4n/go-rpi/motion/servo"
|
||||
)
|
||||
|
||||
func main() {
|
||||
bus, err := i2c.NewBus(1)
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
||||
pwm := pca9685.New(bus, 0x42, 50)
|
||||
defer pwm.Close()
|
||||
|
||||
servo := servo.New(pwm, 50, 0, 1, 2.5)
|
||||
servo.SetDebug(true)
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue