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

@ -9,43 +9,35 @@ import (
// A PWM interface implements access to a pwm controller.
type PWM interface {
SetPwm(channel int, onTime int, offTime int) error
SetMicroseconds(channel int, us int) error
}
type Servo struct {
PWM PWM
Freq int
Channel int
Minms, Maxms float64
Minus, Maxus int
Debug bool
}
// New creates a new Servo interface.
// pwm: instance of a PWM controller.
// freq: Frequency of pwm signal (typically 50Hz)
// channel: PWM channel of the pwm controller to be used
// minms: Pulse width corresponding to servo position of 0deg
// maxms: Pulse width corresponding to servo position of 180deg
func New(pwm PWM, freq, channel int, minms, maxms float64) *Servo {
func New(pwm PWM, channel int, minus, maxus int) *Servo {
return &Servo{
PWM: pwm,
Freq: freq,
Channel: channel,
Minms: minms,
Maxms: maxms,
Minus: minus,
Maxus: maxus,
}
}
// SetAngle sets the servo angle.
func (s *Servo) SetAngle(angle int) error {
us := util.Map(int64(angle), 0, 180, int64(s.Minms*1000), int64(s.Maxms*1000))
offTime := int(us) * s.Freq * 4096 / 1000000
us := util.Map(int64(angle), 0, 180, int64(s.Minus), int64(s.Maxus))
if s.Debug {
log.Printf("servo: given angle %v calculated %v us offTime %v", angle, us, offTime)
log.Printf("servo: given angle %v calculated %v us", angle, us)
}
return s.PWM.SetPwm(s.Channel, 0, offTime)
return s.PWM.SetMicroseconds(s.Channel, int(us))
}