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 {
return err
}
period := 20000000.0
duty := gobot.FromScale(float64(^val), 0, 180.0)
period := 16666666.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)))
}
@ -359,7 +359,7 @@ func (b *BeagleboneAdaptor) pwmWrite(pin string, val byte) (err error) {
return
}
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)))
}
@ -384,10 +384,7 @@ func ensureSlot(slots, item string) (err error) {
if err != nil {
return err
}
err = fi.Sync()
if err != nil {
return err
}
fi.Sync()
scanner = bufio.NewScanner(fi)
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/run",
"/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/class/gpio/export",
"/sys/class/gpio/unexport",
@ -57,19 +58,19 @@ func TestBeagleboneAdaptor(t *testing.T) {
gobot.Assert(
t,
fs.Files["/sys/devices/ocp.3/pwm_test_P9_14.5/duty"].Contents,
"156862",
"343137",
)
a.ServoWrite("P9_14", 100)
gobot.Assert(
t,
fs.Files["/sys/devices/ocp.3/pwm_test_P9_14.5/period"].Contents,
"20000000",
"16666666",
)
gobot.Assert(
t,
fs.Files["/sys/devices/ocp.3/pwm_test_P9_14.5/duty"].Contents,
"17222222",
"1898148",
)
// Analog

View File

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