1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-27 13:48:56 +08:00

adaptors(PWM): fix wrong duty cycle after kill program (#994)

This commit is contained in:
Thomas Kohler 2023-10-19 18:08:03 +02:00 committed by GitHub
parent c73f0f2385
commit 124499d9ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 3 deletions

View File

@ -191,7 +191,7 @@ func (a *PWMPinsAdaptor) getDefaultInitializer() func(gobot.PWMPinner) error {
return err return err
} }
} }
if err := setPeriod(pin, a.periodDefault, false); err != nil { if err := setPeriod(pin, a.periodDefault, a.adjustDutyOnSetPeriod); err != nil {
return err return err
} }
// period needs to be set >1 before all next statements // period needs to be set >1 before all next statements

View File

@ -2,6 +2,7 @@ package adaptors
import ( import (
"fmt" "fmt"
"log"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
@ -127,6 +128,8 @@ func TestPWMPinsFinalize(t *testing.T) {
sys := system.NewAccesser() sys := system.NewAccesser()
fs := sys.UseMockFilesystem(pwmMockPaths) fs := sys.UseMockFilesystem(pwmMockPaths)
a := NewPWMPinsAdaptor(sys, testPWMPinTranslator) a := NewPWMPinsAdaptor(sys, testPWMPinTranslator)
fs.Files[pwmPeriodPath].Contents = "0"
fs.Files[pwmDutyCyclePath].Contents = "0"
// assert that finalize before connect is working // assert that finalize before connect is working
gobottest.Assert(t, a.Finalize(), nil) gobottest.Assert(t, a.Finalize(), nil)
// arrange // arrange
@ -270,12 +273,15 @@ func Test_PWMPin(t *testing.T) {
var tests = map[string]struct { var tests = map[string]struct {
mockPaths []string mockPaths []string
period string period string
dutyCycle string
translate func(string) (string, int, error) translate func(string) (string, int, error)
pin string pin string
wantErr string wantErr string
}{ }{
"pin_ok": { "pin_ok": {
mockPaths: []string{pwmExportPath, pwmEnablePath, pwmPeriodPath, pwmDutyCyclePath, pwmPolarityPath}, mockPaths: []string{pwmExportPath, pwmEnablePath, pwmPeriodPath, pwmDutyCyclePath, pwmPolarityPath},
period: "0",
dutyCycle: "0",
translate: translator, translate: translator,
pin: "33", 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", 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": { "init_setperiod_dutycycle_no_error": {
mockPaths: []string{pwmExportPath, pwmEnablePath, pwmPeriodPath, pwmPolarityPath}, mockPaths: []string{pwmExportPath, pwmEnablePath, pwmPeriodPath, pwmDutyCyclePath, pwmPolarityPath},
period: "0",
dutyCycle: "0",
translate: translator, translate: translator,
pin: "33", pin: "33",
}, },
"init_setperiod_error": { "init_setperiod_error": {
mockPaths: []string{pwmExportPath, pwmEnablePath}, mockPaths: []string{pwmExportPath, pwmEnablePath, pwmDutyCyclePath},
dutyCycle: "0",
translate: translator, translate: translator,
pin: "33", pin: "33",
wantErr: "SetPeriod(10000000) failed for id 44 with : /sys/devices/platform/ff680020.pwm/pwm/pwmchip3/pwm44/period: no such file", wantErr: "SetPeriod(10000000) failed for id 44 with : /sys/devices/platform/ff680020.pwm/pwm/pwmchip3/pwm44/period: no such file",
}, },
"init_setpolarity_error": { "init_setpolarity_error": {
mockPaths: []string{pwmExportPath, pwmEnablePath, pwmPeriodPath, pwmDutyCyclePath}, mockPaths: []string{pwmExportPath, pwmEnablePath, pwmPeriodPath, pwmDutyCyclePath},
period: "0",
dutyCycle: "0",
translate: translator, translate: translator,
pin: "33", pin: "33",
wantErr: "SetPolarity(normal) failed for id 44 with : /sys/devices/platform/ff680020.pwm/pwm/pwmchip3/pwm44/polarity: no such file", 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 != "" { if tc.period != "" {
fs.Files[pwmPeriodPath].Contents = tc.period fs.Files[pwmPeriodPath].Contents = tc.period
} }
if tc.dutyCycle != "" {
fs.Files[pwmDutyCyclePath].Contents = tc.dutyCycle
}
a := NewPWMPinsAdaptor(sys, tc.translate) a := NewPWMPinsAdaptor(sys, tc.translate)
if err := a.Connect(); err != nil { if err := a.Connect(); err != nil {
panic(err) panic(err)
@ -333,6 +347,9 @@ func Test_PWMPin(t *testing.T) {
gobottest.Assert(t, err, nil) gobottest.Assert(t, err, nil)
gobottest.Refute(t, got, nil) gobottest.Refute(t, got, nil)
} else { } 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, strings.Contains(err.Error(), tc.wantErr), true)
gobottest.Assert(t, got, nil) gobottest.Assert(t, got, nil)
} }

View File

@ -54,6 +54,8 @@ func TestPWM(t *testing.T) {
} }
a, fs := initTestAdaptorWithMockedFilesystem(mockPaths) 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")) 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) _ = a.PwmWrite("P9_21", 175)

View File

@ -81,6 +81,9 @@ func TestFinalizeErrorAfterGPIO(t *testing.T) {
func TestFinalizeErrorAfterPWM(t *testing.T) { func TestFinalizeErrorAfterPWM(t *testing.T) {
a, fs := initTestAdaptorWithMockedFilesystem() 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.Connect(), nil)
gobottest.Assert(t, a.PwmWrite("PWM0", 100), nil) gobottest.Assert(t, a.PwmWrite("PWM0", 100), nil)
@ -122,6 +125,9 @@ func TestProDigitalIO(t *testing.T) {
func TestPWM(t *testing.T) { func TestPWM(t *testing.T) {
a, fs := initTestAdaptorWithMockedFilesystem() 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() _ = a.Connect()
err := a.PwmWrite("PWM0", 100) 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/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/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/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") gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/polarity"].Contents, "normal")
err = a.ServoWrite("PWM0", 0) err = a.ServoWrite("PWM0", 0)
gobottest.Assert(t, err, nil) 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/duty_cycle"].Contents, "500000")
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/period"].Contents, "10000000")
err = a.ServoWrite("PWM0", 180) err = a.ServoWrite("PWM0", 180)
gobottest.Assert(t, err, nil) 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/duty_cycle"].Contents, "2000000")
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/period"].Contents, "10000000") // pwmPeriodDefault
gobottest.Assert(t, a.Finalize(), nil) gobottest.Assert(t, a.Finalize(), nil)
} }

View File

@ -82,6 +82,8 @@ func TestDigitalIO(t *testing.T) {
func TestPWM(t *testing.T) { func TestPWM(t *testing.T) {
a, fs := initTestAdaptorWithMockedFilesystem(pwmMockPaths) 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) err := a.PwmWrite("32", 100)
gobottest.Assert(t, err, nil) 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/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/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/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") gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/polarity"].Contents, "normal")
err = a.ServoWrite("32", 0) err = a.ServoWrite("32", 0)
gobottest.Assert(t, err, nil) 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/duty_cycle"].Contents, "500000")
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/period"].Contents, "10000000")
err = a.ServoWrite("32", 180) err = a.ServoWrite("32", 180)
gobottest.Assert(t, err, nil) 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/duty_cycle"].Contents, "2000000")
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/period"].Contents, "10000000")
gobottest.Assert(t, a.Finalize(), nil) gobottest.Assert(t, a.Finalize(), nil)
} }
@ -116,6 +122,8 @@ func TestFinalizeErrorAfterGPIO(t *testing.T) {
func TestFinalizeErrorAfterPWM(t *testing.T) { func TestFinalizeErrorAfterPWM(t *testing.T) {
a, fs := initTestAdaptorWithMockedFilesystem(pwmMockPaths) 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) gobottest.Assert(t, a.PwmWrite("32", 1), nil)