Skip to content

Commit

Permalink
adaptors(PWM): fix wrong duty cycle after kill program (#994)
Browse files Browse the repository at this point in the history
  • Loading branch information
gen2thomas committed Oct 19, 2023
1 parent c73f0f2 commit 124499d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
2 changes: 1 addition & 1 deletion platforms/adaptors/pwmpinsadaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 19 additions & 2 deletions platforms/adaptors/pwmpinsadaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package adaptors

import (
"fmt"
"log"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
},
Expand All @@ -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",
Expand All @@ -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)
Expand All @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions platforms/beaglebone/beaglebone_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions platforms/chip/chip_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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)
}

Expand Down
8 changes: 8 additions & 0 deletions platforms/upboard/up2/adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,30 @@ 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)

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)
}

Expand All @@ -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)

Expand Down

0 comments on commit 124499d

Please sign in to comment.