From 124499d9cea72c5db66e0ee003a115902df25aa5 Mon Sep 17 00:00:00 2001 From: Thomas Kohler Date: Thu, 19 Oct 2023 18:08:03 +0200 Subject: [PATCH] adaptors(PWM): fix wrong duty cycle after kill program (#994) --- platforms/adaptors/pwmpinsadaptor.go | 2 +- platforms/adaptors/pwmpinsadaptor_test.go | 21 +++++++++++++++++-- .../beaglebone/beaglebone_adaptor_test.go | 2 ++ platforms/chip/chip_adaptor_test.go | 10 +++++++++ platforms/upboard/up2/adaptor_test.go | 8 +++++++ 5 files changed, 40 insertions(+), 3 deletions(-) diff --git a/platforms/adaptors/pwmpinsadaptor.go b/platforms/adaptors/pwmpinsadaptor.go index 11c2969bf..914771940 100644 --- a/platforms/adaptors/pwmpinsadaptor.go +++ b/platforms/adaptors/pwmpinsadaptor.go @@ -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 diff --git a/platforms/adaptors/pwmpinsadaptor_test.go b/platforms/adaptors/pwmpinsadaptor_test.go index 87f8f2f58..0421dcb0f 100644 --- a/platforms/adaptors/pwmpinsadaptor_test.go +++ b/platforms/adaptors/pwmpinsadaptor_test.go @@ -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) } diff --git a/platforms/beaglebone/beaglebone_adaptor_test.go b/platforms/beaglebone/beaglebone_adaptor_test.go index ed5e5a38f..b64c75a61 100644 --- a/platforms/beaglebone/beaglebone_adaptor_test.go +++ b/platforms/beaglebone/beaglebone_adaptor_test.go @@ -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) diff --git a/platforms/chip/chip_adaptor_test.go b/platforms/chip/chip_adaptor_test.go index 612b22164..4111d5482 100644 --- a/platforms/chip/chip_adaptor_test.go +++ b/platforms/chip/chip_adaptor_test.go @@ -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) } diff --git a/platforms/upboard/up2/adaptor_test.go b/platforms/upboard/up2/adaptor_test.go index 83ac48334..8b76f9766 100644 --- a/platforms/upboard/up2/adaptor_test.go +++ b/platforms/upboard/up2/adaptor_test.go @@ -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)