1
0
Fork 0
mirror of https://github.com/kidoman/embd synced 2025-07-03 03:47:33 +02:00

- support for servo blaster

- servo requires the underlying PWM to support a SetMicroseconds method
This commit is contained in:
Karan Misra 2014-01-09 02:35:18 +05:30
parent cce1943e34
commit 7c32e907e1
7 changed files with 124 additions and 19 deletions

View file

@ -149,6 +149,11 @@ func (d *PCA9685) SetPwm(channel, onTime, offTime int) (err error) {
return
}
func (d *PCA9685) SetMicroseconds(channel, us int) (err error) {
offTime := us * d.Freq * pwmControlPoints / 1000000
return d.SetPwm(channel, 0, offTime)
}
// Close stops the controller and resets mode and pwm controller registers.
func (d *PCA9685) Close() (err error) {
if err = d.setup(); err != nil {

View file

@ -0,0 +1,56 @@
// Package servoblaster allows interfacing with the software servoblaster driver.
//
// More details on ServoBlaster at: https://github.com/richardghirst/PiBits/tree/master/ServoBlaster
package servoblaster
import (
"fmt"
"log"
"os"
)
// ServoBlaster represents a software RPi PWM/PCM based servo control module.
type ServoBlaster struct {
initialized bool
fd *os.File
// Debug level.
Debug bool
}
// New creates a new ServoBlaster instance.
func New() *ServoBlaster {
return &ServoBlaster{}
}
func (d *ServoBlaster) setup() (err error) {
if d.initialized {
return
}
if d.fd, err = os.OpenFile("/dev/servoblaster", os.O_WRONLY, os.ModeExclusive); err != nil {
return
}
d.initialized = true
return
}
// SetMicroseconds sends a command to the PWM driver to generate a us wide pulse.
func (d *ServoBlaster) SetMicroseconds(channel, us int) (err error) {
if err = d.setup(); err != nil {
return
}
cmd := fmt.Sprintf("%v=%vus\n", channel, us)
if d.Debug {
log.Printf("servoblaster: sending command %q", cmd)
}
_, err = d.fd.WriteString(cmd)
return
}
// Close closes the open driver handle.
func (d *ServoBlaster) Close() (err error) {
if d.fd != nil {
err = d.fd.Close()
}
return
}