Refactor pwm operations

This commit is contained in:
Adrian Zankich 2014-12-23 18:35:45 -08:00
parent 67cdddb7ec
commit 1b1c3ad9ce
3 changed files with 55 additions and 44 deletions

View File

@ -206,8 +206,8 @@ func (b *BeagleboneAdaptor) ServoWrite(pin string, val byte) (err error) {
if err != nil { if err != nil {
return err return err
} }
period := 20000000.0 period := 16666666.0
duty := gobot.FromScale(float64(^val), 0, 180.0) duty := (gobot.FromScale(float64(val), 0, 180.0) * 0.115) + 0.05
return b.pwmPins[i].pwmWrite(strconv.Itoa(int(period)), strconv.Itoa(int(period*duty))) return b.pwmPins[i].pwmWrite(strconv.Itoa(int(period)), strconv.Itoa(int(period*duty)))
} }
@ -359,7 +359,7 @@ func (b *BeagleboneAdaptor) pwmWrite(pin string, val byte) (err error) {
return return
} }
period := 500000.0 period := 500000.0
duty := gobot.FromScale(float64(^val), 0, 255.0) duty := gobot.FromScale(float64(val), 0, 255.0)
return b.pwmPins[i].pwmWrite(strconv.Itoa(int(period)), strconv.Itoa(int(period*duty))) return b.pwmPins[i].pwmWrite(strconv.Itoa(int(period)), strconv.Itoa(int(period*duty)))
} }
@ -384,10 +384,7 @@ func ensureSlot(slots, item string) (err error) {
if err != nil { if err != nil {
return err return err
} }
err = fi.Sync() fi.Sync()
if err != nil {
return err
}
scanner = bufio.NewScanner(fi) scanner = bufio.NewScanner(fi)
for scanner.Scan() { for scanner.Scan() {

View File

@ -23,6 +23,7 @@ func TestBeagleboneAdaptor(t *testing.T) {
"/sys/devices/ocp.3/pwm_test_P9_14.5", "/sys/devices/ocp.3/pwm_test_P9_14.5",
"/sys/devices/ocp.3/pwm_test_P9_14.5/run", "/sys/devices/ocp.3/pwm_test_P9_14.5/run",
"/sys/devices/ocp.3/pwm_test_P9_14.5/period", "/sys/devices/ocp.3/pwm_test_P9_14.5/period",
"/sys/devices/ocp.3/pwm_test_P9_14.5/polarity",
"/sys/devices/ocp.3/pwm_test_P9_14.5/duty", "/sys/devices/ocp.3/pwm_test_P9_14.5/duty",
"/sys/class/gpio/export", "/sys/class/gpio/export",
"/sys/class/gpio/unexport", "/sys/class/gpio/unexport",
@ -57,19 +58,19 @@ func TestBeagleboneAdaptor(t *testing.T) {
gobot.Assert( gobot.Assert(
t, t,
fs.Files["/sys/devices/ocp.3/pwm_test_P9_14.5/duty"].Contents, fs.Files["/sys/devices/ocp.3/pwm_test_P9_14.5/duty"].Contents,
"156862", "343137",
) )
a.ServoWrite("P9_14", 100) a.ServoWrite("P9_14", 100)
gobot.Assert( gobot.Assert(
t, t,
fs.Files["/sys/devices/ocp.3/pwm_test_P9_14.5/period"].Contents, fs.Files["/sys/devices/ocp.3/pwm_test_P9_14.5/period"].Contents,
"20000000", "16666666",
) )
gobot.Assert( gobot.Assert(
t, t,
fs.Files["/sys/devices/ocp.3/pwm_test_P9_14.5/duty"].Contents, fs.Files["/sys/devices/ocp.3/pwm_test_P9_14.5/duty"].Contents,
"17222222", "1898148",
) )
// Analog // Analog

View File

@ -1,9 +1,11 @@
package beaglebone package beaglebone
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"strings" "strings"
"time"
"github.com/hybridgroup/gobot/sysfs" "github.com/hybridgroup/gobot/sysfs"
) )
@ -15,8 +17,7 @@ type pwmPin struct {
// newPwmPin creates a new pwm pin with specified pin number // newPwmPin creates a new pwm pin with specified pin number
func newPwmPin(pinNum string, ocp string) (p *pwmPin, err error) { func newPwmPin(pinNum string, ocp string) (p *pwmPin, err error) {
var fi sysfs.File done := make(chan error, 0)
p = &pwmPin{ p = &pwmPin{
pinNum: strings.ToUpper(pinNum), pinNum: strings.ToUpper(pinNum),
} }
@ -28,36 +29,51 @@ func newPwmPin(pinNum string, ocp string) (p *pwmPin, err error) {
p.pwmDevice = pwmDevice[0] p.pwmDevice = pwmDevice[0]
for i := 0; i < 10; i++ { go func() {
fi, err = sysfs.OpenFile(fmt.Sprintf("%v/run", p.pwmDevice), os.O_RDWR|os.O_APPEND, 0666) for {
defer fi.Close() if _, err := sysfs.OpenFile(fmt.Sprintf("%v/period", p.pwmDevice), os.O_RDONLY, 0644); err == nil {
if err != nil && i == 9 { break
return }
} else {
break
} }
} for {
if fi, err := sysfs.OpenFile(fmt.Sprintf("%v/duty", p.pwmDevice), os.O_WRONLY|os.O_APPEND, 0644); err == nil {
defer fi.Close()
if _, err = fi.WriteString("0"); err != nil {
done <- err
}
fi.Sync()
break
}
}
for {
if fi, err := sysfs.OpenFile(fmt.Sprintf("%v/polarity", p.pwmDevice), os.O_WRONLY|os.O_APPEND, 0644); err == nil {
defer fi.Close()
if _, err = fi.WriteString("0"); err != nil {
done <- err
}
fi.Sync()
break
}
}
for {
if fi, err := sysfs.OpenFile(fmt.Sprintf("%v/run", p.pwmDevice), os.O_WRONLY|os.O_APPEND, 0644); err == nil {
defer fi.Close()
if _, err = fi.WriteString("1"); err != nil {
done <- err
}
fi.Sync()
break
}
}
done <- nil
}()
_, err = fi.WriteString("1") select {
if err != nil { case err = <-done:
return return p, err
case <-time.After(500 * time.Millisecond):
return p, errors.New("could not initialize pwm device")
} }
err = fi.Sync()
if err != nil {
return
}
for {
if _, err := sysfs.OpenFile(fmt.Sprintf("%v/period", p.pwmDevice), os.O_RDONLY, 0644); err == nil {
break
}
}
for {
if _, err := sysfs.OpenFile(fmt.Sprintf("%v/duty", p.pwmDevice), os.O_RDONLY, 0644); err == nil {
break
}
}
return
} }
// pwmWrite writes to a pwm pin with specified period and duty // pwmWrite writes to a pwm pin with specified period and duty
@ -67,8 +83,7 @@ func (p *pwmPin) pwmWrite(period string, duty string) (err error) {
if err != nil { if err != nil {
return return
} }
_, err = f1.WriteString(period) if _, err = f1.WriteString(period); err != nil {
if err != nil {
return return
} }
@ -77,10 +92,8 @@ func (p *pwmPin) pwmWrite(period string, duty string) (err error) {
if err != nil { if err != nil {
return return
} }
_, err = f2.WriteString(duty) _, err = f2.WriteString(duty)
if err != nil {
return
}
return return
} }