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:
parent
cce1943e34
commit
7c32e907e1
7 changed files with 124 additions and 19 deletions
|
@ -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 {
|
||||
|
|
56
controller/servoblaster/servoblaster.go
Normal file
56
controller/servoblaster/servoblaster.go
Normal 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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue