mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-24 13:48:49 +08:00
adaptors(PWM): fix wrong duty cycle after kill program (#994)
This commit is contained in:
parent
c73f0f2385
commit
124499d9ce
@ -191,7 +191,7 @@ func (a *PWMPinsAdaptor) getDefaultInitializer() func(gobot.PWMPinner) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := setPeriod(pin, a.periodDefault, false); err != nil {
|
||||
if err := setPeriod(pin, a.periodDefault, a.adjustDutyOnSetPeriod); err != nil {
|
||||
return err
|
||||
}
|
||||
// period needs to be set >1 before all next statements
|
||||
|
@ -2,6 +2,7 @@ package adaptors
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -127,6 +128,8 @@ func TestPWMPinsFinalize(t *testing.T) {
|
||||
sys := system.NewAccesser()
|
||||
fs := sys.UseMockFilesystem(pwmMockPaths)
|
||||
a := NewPWMPinsAdaptor(sys, testPWMPinTranslator)
|
||||
fs.Files[pwmPeriodPath].Contents = "0"
|
||||
fs.Files[pwmDutyCyclePath].Contents = "0"
|
||||
// assert that finalize before connect is working
|
||||
gobottest.Assert(t, a.Finalize(), nil)
|
||||
// arrange
|
||||
@ -270,12 +273,15 @@ func Test_PWMPin(t *testing.T) {
|
||||
var tests = map[string]struct {
|
||||
mockPaths []string
|
||||
period string
|
||||
dutyCycle string
|
||||
translate func(string) (string, int, error)
|
||||
pin string
|
||||
wantErr string
|
||||
}{
|
||||
"pin_ok": {
|
||||
mockPaths: []string{pwmExportPath, pwmEnablePath, pwmPeriodPath, pwmDutyCyclePath, pwmPolarityPath},
|
||||
period: "0",
|
||||
dutyCycle: "0",
|
||||
translate: translator,
|
||||
pin: "33",
|
||||
},
|
||||
@ -293,18 +299,23 @@ func Test_PWMPin(t *testing.T) {
|
||||
wantErr: "SetEnabled(false) failed for id 44 with : /sys/devices/platform/ff680020.pwm/pwm/pwmchip3/pwm44/enable: no such file",
|
||||
},
|
||||
"init_setperiod_dutycycle_no_error": {
|
||||
mockPaths: []string{pwmExportPath, pwmEnablePath, pwmPeriodPath, pwmPolarityPath},
|
||||
mockPaths: []string{pwmExportPath, pwmEnablePath, pwmPeriodPath, pwmDutyCyclePath, pwmPolarityPath},
|
||||
period: "0",
|
||||
dutyCycle: "0",
|
||||
translate: translator,
|
||||
pin: "33",
|
||||
},
|
||||
"init_setperiod_error": {
|
||||
mockPaths: []string{pwmExportPath, pwmEnablePath},
|
||||
mockPaths: []string{pwmExportPath, pwmEnablePath, pwmDutyCyclePath},
|
||||
dutyCycle: "0",
|
||||
translate: translator,
|
||||
pin: "33",
|
||||
wantErr: "SetPeriod(10000000) failed for id 44 with : /sys/devices/platform/ff680020.pwm/pwm/pwmchip3/pwm44/period: no such file",
|
||||
},
|
||||
"init_setpolarity_error": {
|
||||
mockPaths: []string{pwmExportPath, pwmEnablePath, pwmPeriodPath, pwmDutyCyclePath},
|
||||
period: "0",
|
||||
dutyCycle: "0",
|
||||
translate: translator,
|
||||
pin: "33",
|
||||
wantErr: "SetPolarity(normal) failed for id 44 with : /sys/devices/platform/ff680020.pwm/pwm/pwmchip3/pwm44/polarity: no such file",
|
||||
@ -322,6 +333,9 @@ func Test_PWMPin(t *testing.T) {
|
||||
if tc.period != "" {
|
||||
fs.Files[pwmPeriodPath].Contents = tc.period
|
||||
}
|
||||
if tc.dutyCycle != "" {
|
||||
fs.Files[pwmDutyCyclePath].Contents = tc.dutyCycle
|
||||
}
|
||||
a := NewPWMPinsAdaptor(sys, tc.translate)
|
||||
if err := a.Connect(); err != nil {
|
||||
panic(err)
|
||||
@ -333,6 +347,9 @@ func Test_PWMPin(t *testing.T) {
|
||||
gobottest.Assert(t, err, nil)
|
||||
gobottest.Refute(t, got, nil)
|
||||
} else {
|
||||
if !strings.Contains(err.Error(), tc.wantErr) {
|
||||
log.Println(err.Error())
|
||||
}
|
||||
gobottest.Assert(t, strings.Contains(err.Error(), tc.wantErr), true)
|
||||
gobottest.Assert(t, got, nil)
|
||||
}
|
||||
|
@ -54,6 +54,8 @@ func TestPWM(t *testing.T) {
|
||||
}
|
||||
|
||||
a, fs := initTestAdaptorWithMockedFilesystem(mockPaths)
|
||||
fs.Files["/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm1/duty_cycle"].Contents = "0"
|
||||
fs.Files["/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm1/period"].Contents = "0"
|
||||
|
||||
gobottest.Assert(t, a.PwmWrite("P9_99", 175), errors.New("'P9_99' is not a valid id for a PWM pin"))
|
||||
_ = a.PwmWrite("P9_21", 175)
|
||||
|
@ -81,6 +81,9 @@ func TestFinalizeErrorAfterGPIO(t *testing.T) {
|
||||
|
||||
func TestFinalizeErrorAfterPWM(t *testing.T) {
|
||||
a, fs := initTestAdaptorWithMockedFilesystem()
|
||||
fs.Files["/sys/class/pwm/pwmchip0/pwm0/duty_cycle"].Contents = "0"
|
||||
fs.Files["/sys/class/pwm/pwmchip0/pwm0/period"].Contents = "0"
|
||||
|
||||
gobottest.Assert(t, a.Connect(), nil)
|
||||
gobottest.Assert(t, a.PwmWrite("PWM0", 100), nil)
|
||||
|
||||
@ -122,6 +125,9 @@ func TestProDigitalIO(t *testing.T) {
|
||||
|
||||
func TestPWM(t *testing.T) {
|
||||
a, fs := initTestAdaptorWithMockedFilesystem()
|
||||
fs.Files["/sys/class/pwm/pwmchip0/pwm0/duty_cycle"].Contents = "0"
|
||||
fs.Files["/sys/class/pwm/pwmchip0/pwm0/period"].Contents = "0"
|
||||
|
||||
_ = a.Connect()
|
||||
|
||||
err := a.PwmWrite("PWM0", 100)
|
||||
@ -130,17 +136,21 @@ func TestPWM(t *testing.T) {
|
||||
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/export"].Contents, "0")
|
||||
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/enable"].Contents, "1")
|
||||
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/duty_cycle"].Contents, "3921568")
|
||||
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/period"].Contents, "10000000") // pwmPeriodDefault
|
||||
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/polarity"].Contents, "normal")
|
||||
|
||||
err = a.ServoWrite("PWM0", 0)
|
||||
gobottest.Assert(t, err, nil)
|
||||
|
||||
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/duty_cycle"].Contents, "500000")
|
||||
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/period"].Contents, "10000000")
|
||||
|
||||
err = a.ServoWrite("PWM0", 180)
|
||||
gobottest.Assert(t, err, nil)
|
||||
|
||||
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/duty_cycle"].Contents, "2000000")
|
||||
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/period"].Contents, "10000000") // pwmPeriodDefault
|
||||
|
||||
gobottest.Assert(t, a.Finalize(), nil)
|
||||
}
|
||||
|
||||
|
@ -82,6 +82,8 @@ func TestDigitalIO(t *testing.T) {
|
||||
|
||||
func TestPWM(t *testing.T) {
|
||||
a, fs := initTestAdaptorWithMockedFilesystem(pwmMockPaths)
|
||||
fs.Files["/sys/class/pwm/pwmchip0/pwm0/duty_cycle"].Contents = "0"
|
||||
fs.Files["/sys/class/pwm/pwmchip0/pwm0/period"].Contents = "0"
|
||||
|
||||
err := a.PwmWrite("32", 100)
|
||||
gobottest.Assert(t, err, nil)
|
||||
@ -89,17 +91,21 @@ func TestPWM(t *testing.T) {
|
||||
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/export"].Contents, "0")
|
||||
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/enable"].Contents, "1")
|
||||
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/duty_cycle"].Contents, "3921568")
|
||||
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/period"].Contents, "10000000") // pwmPeriodDefault
|
||||
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/polarity"].Contents, "normal")
|
||||
|
||||
err = a.ServoWrite("32", 0)
|
||||
gobottest.Assert(t, err, nil)
|
||||
|
||||
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/duty_cycle"].Contents, "500000")
|
||||
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/period"].Contents, "10000000")
|
||||
|
||||
err = a.ServoWrite("32", 180)
|
||||
gobottest.Assert(t, err, nil)
|
||||
|
||||
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/duty_cycle"].Contents, "2000000")
|
||||
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/period"].Contents, "10000000")
|
||||
|
||||
gobottest.Assert(t, a.Finalize(), nil)
|
||||
}
|
||||
|
||||
@ -116,6 +122,8 @@ func TestFinalizeErrorAfterGPIO(t *testing.T) {
|
||||
|
||||
func TestFinalizeErrorAfterPWM(t *testing.T) {
|
||||
a, fs := initTestAdaptorWithMockedFilesystem(pwmMockPaths)
|
||||
fs.Files["/sys/class/pwm/pwmchip0/pwm0/duty_cycle"].Contents = "0"
|
||||
fs.Files["/sys/class/pwm/pwmchip0/pwm0/period"].Contents = "0"
|
||||
|
||||
gobottest.Assert(t, a.PwmWrite("32", 1), nil)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user