mirror of
https://github.com/kidoman/embd
synced 2024-12-31 09:01:37 +01:00
refactoring
This commit is contained in:
parent
2b67e89fad
commit
7d044a8f0e
@ -3,59 +3,49 @@ package servo
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
|
"github.com/kid0m4n/go-rpi/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A PWM interface implements access to a pwm controller.
|
// A PWM interface implements access to a pwm controller.
|
||||||
type PWM interface {
|
type PWM interface {
|
||||||
SetPwm(n int, onTime int, offTime int) error
|
SetPwm(channel int, onTime int, offTime int) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// A SERVO interface implements access to the servo.
|
type Servo struct {
|
||||||
type Servo interface {
|
PWM PWM
|
||||||
SetAngle(angle int) error
|
Freq int
|
||||||
|
Channel int
|
||||||
|
|
||||||
SetDebug(v bool)
|
Minms, Maxms float64
|
||||||
|
|
||||||
|
Debug bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type servo struct {
|
// New creates a new Servo interface.
|
||||||
pwm PWM
|
|
||||||
freq int
|
|
||||||
n int
|
|
||||||
|
|
||||||
minms, maxms float64
|
|
||||||
|
|
||||||
debug bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// New creates a new SERVO interface.
|
|
||||||
// pwm: instance of a PWM controller.
|
// pwm: instance of a PWM controller.
|
||||||
// freq: Frequency of pwm signal (typically 50Hz)
|
// freq: Frequency of pwm signal (typically 50Hz)
|
||||||
// n: PWM channel of the pwm controller to be used
|
// channel: PWM channel of the pwm controller to be used
|
||||||
// minms: Pulse width corresponding to servo position of 0deg
|
// minms: Pulse width corresponding to servo position of 0deg
|
||||||
// maxms: Pulse width corresponding to servo position of 180deg
|
// maxms: Pulse width corresponding to servo position of 180deg
|
||||||
func New(pwm PWM, freq int, n int, minms, maxms float64) Servo {
|
func New(pwm PWM, freq, channel int, minms, maxms float64) *Servo {
|
||||||
return &servo{
|
return &Servo{
|
||||||
pwm: pwm,
|
PWM: pwm,
|
||||||
freq: freq,
|
Freq: freq,
|
||||||
n: n,
|
Channel: channel,
|
||||||
minms: minms,
|
Minms: minms,
|
||||||
maxms: maxms,
|
Maxms: maxms,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetDebug is used to enable logging (debug mode).
|
|
||||||
func (s *servo) SetDebug(v bool) {
|
|
||||||
s.debug = v
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetAngle sets the servo angle.
|
// SetAngle sets the servo angle.
|
||||||
func (s *servo) SetAngle(angle int) error {
|
func (s *Servo) SetAngle(angle int) error {
|
||||||
ms := s.minms + float64(angle)*(s.maxms-s.minms)/180
|
us := util.Map(int64(angle), 0, 180, int64(s.Minms*1000), int64(s.Maxms*1000))
|
||||||
offTime := int(ms * float64(s.freq) * 4096 / 1000)
|
offTime := int(us) * s.Freq * 4096 / 1000000
|
||||||
|
|
||||||
if s.debug {
|
if s.Debug {
|
||||||
log.Printf("servo: given angle %v calculated ms %2.2f offTime %v", angle, ms, offTime)
|
log.Printf("servo: given angle %v calculated %v us offTime %v", angle, us, offTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.pwm.SetPwm(s.n, 0, offTime)
|
return s.PWM.SetPwm(s.Channel, 0, offTime)
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,6 @@ func main() {
|
|||||||
defer pwm.Close()
|
defer pwm.Close()
|
||||||
|
|
||||||
servo := servo.New(pwm, 50, 0, 1, 2.5)
|
servo := servo.New(pwm, 50, 0, 1, 2.5)
|
||||||
servo.SetDebug(true)
|
|
||||||
|
|
||||||
c := make(chan os.Signal, 1)
|
c := make(chan os.Signal, 1)
|
||||||
signal.Notify(c, os.Interrupt, os.Kill)
|
signal.Notify(c, os.Interrupt, os.Kill)
|
||||||
|
5
util/map.go
Normal file
5
util/map.go
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
func Map(x, inmin, inmax, outmin, outmax int64) int64 {
|
||||||
|
return (x-inmin)*(outmax-outmin)/(inmax-inmin) + outmin
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user