1
0
mirror of https://github.com/kidoman/embd synced 2024-11-01 07:48:43 +01:00
embd/motion/servo/servo.go

47 lines
861 B
Go
Raw Normal View History

// Package servo allows control of servos using a PWM controller.
package servo
import (
"github.com/golang/glog"
2014-02-10 00:35:41 +01:00
"github.com/kidoman/embd/util"
)
const (
minus = 544
maxus = 2400
)
const (
// DefaultFreq represents the default (preferred) freq of a PWM doing servo duties.
DefaultFreq = 50
)
// A PWM interface implements access to a pwm controller.
type PWM interface {
SetMicroseconds(us int) error
}
2014-01-05 11:19:03 +01:00
type Servo struct {
PWM PWM
Minus, Maxus int
}
2014-01-05 11:19:03 +01:00
// New creates a new Servo interface.
func New(pwm PWM) *Servo {
2014-01-05 11:19:03 +01:00
return &Servo{
PWM: pwm,
Minus: minus,
Maxus: maxus,
}
}
// SetAngle sets the servo angle.
2014-01-05 11:19:03 +01:00
func (s *Servo) SetAngle(angle int) error {
us := util.Map(int64(angle), 0, 180, int64(s.Minus), int64(s.Maxus))
glog.V(1).Infof("servo: given angle %v calculated %v us", angle, us)
return s.PWM.SetMicroseconds(int(us))
}