From 0d183e0f0bd63ecf3709a0761212e109d9d0e32e Mon Sep 17 00:00:00 2001 From: Cyrille Nofficial Date: Thu, 16 Jun 2022 15:24:56 +0200 Subject: [PATCH] fix(steering): reverse direction --- cmd/rc-pca9685/rc-pca9685.go | 2 ++ pkg/actuator/pca9685.go | 18 +++++++----------- pkg/actuator/pca9685_test.go | 6 ++++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/cmd/rc-pca9685/rc-pca9685.go b/cmd/rc-pca9685/rc-pca9685.go index 034c1c8..000c8a9 100644 --- a/cmd/rc-pca9685/rc-pca9685.go +++ b/cmd/rc-pca9685/rc-pca9685.go @@ -128,6 +128,7 @@ func main() { dev, throttleChannel, actuator.PWM(throttleMinPWM), actuator.PWM(throttleMaxPWM), actuator.PWM(throttleStoppedPWM), + -1., 1., freq, zap.S().With("actuator", "throttle"), ) @@ -139,6 +140,7 @@ func main() { dev, steeringChannel, actuator.PWM(steeringLeftPWM), actuator.PWM(steeringRightPWM), actuator.PWM(steeringCenterPWM), + 1., -1., freq, zap.S().With("actuator", "steering"), ) diff --git a/pkg/actuator/pca9685.go b/pkg/actuator/pca9685.go index ff5c093..79a52b9 100644 --- a/pkg/actuator/pca9685.go +++ b/pkg/actuator/pca9685.go @@ -49,9 +49,9 @@ func (c *Pca9685Controller) convertToDuty(percent float32) (gpio.Duty, error) { // map absolute angle to angle that vehicle can implement. pw := int(c.neutralPWM) if percent > 0 { - pw = util.MapRange(float64(percent), 0, MaxPercent, float64(c.neutralPWM), float64(c.maxPWM)) + pw = util.MapRange(float64(percent), 0, c.maxPercent, float64(c.neutralPWM), float64(c.maxPWM)) } else if percent < 0 { - pw = util.MapRange(float64(percent), MinPercent, 0, float64(c.minPWM), float64(c.neutralPWM)) + pw = util.MapRange(float64(percent), c.minPercent, 0, float64(c.minPWM), float64(c.neutralPWM)) } c.logr.Debugf("convert value %v to pw: %v", percent, pw) @@ -59,20 +59,13 @@ func (c *Pca9685Controller) convertToDuty(percent float32) (gpio.Duty, error) { draw := float64(pw) / float64(per) return gpio.Duty(int32(draw * float64(gpio.DutyMax))), nil - /* - d, err := gpio.ParseDuty(strconv.Itoa(int(dd))) - // d, err := gpio.ParseDuty(fmt.Sprintf("%.f%%", draw*100)) - if err != nil { - return 0, fmt.Errorf("unable to parse duty, probably bad compute: %w", err) - } - return d, nil - */ } type Pca9685Controller struct { logr *zap.SugaredLogger pin gpio.PinIO minPWM, maxPWM, neutralPWM PWM + minPercent, maxPercent float64 freq physic.Frequency } @@ -103,7 +96,8 @@ func (c *Pca9685Controller) SetPercentValue(p float32) error { return nil } -func NewPca9685Controller(device *pca9685.Dev, channel int, minPWM, maxPWM, neutralPWM PWM, freq physic.Frequency, logr *zap.SugaredLogger) (*Pca9685Controller, error) { +func NewPca9685Controller(device *pca9685.Dev, channel int, minPWM, maxPWM, neutralPWM PWM, minPercent, maxPercent float64, + freq physic.Frequency, logr *zap.SugaredLogger) (*Pca9685Controller, error) { p, err := device.CreatePin(channel) if err != nil { return nil, fmt.Errorf("unable to create pin for channel %v: %w", channel, err) @@ -113,6 +107,8 @@ func NewPca9685Controller(device *pca9685.Dev, channel int, minPWM, maxPWM, neut minPWM: minPWM, maxPWM: maxPWM, neutralPWM: neutralPWM, + minPercent: minPercent, + maxPercent: maxPercent, freq: freq, logr: logr, } diff --git a/pkg/actuator/pca9685_test.go b/pkg/actuator/pca9685_test.go index f6255e2..238e487 100644 --- a/pkg/actuator/pca9685_test.go +++ b/pkg/actuator/pca9685_test.go @@ -48,7 +48,7 @@ func Test_convertToDuty(t *testing.T) { centerPWM: 1500, freq: 60 * physic.Hertz, }, - want: "6%", + want: "12%", }, { name: "right", @@ -59,7 +59,7 @@ func Test_convertToDuty(t *testing.T) { centerPWM: 1500, freq: 60 * physic.Hertz, }, - want: "12%", + want: "6%", }, } for _, tt := range tests { @@ -71,6 +71,8 @@ func Test_convertToDuty(t *testing.T) { maxPWM: tt.args.rightPWM, neutralPWM: tt.args.centerPWM, freq: tt.args.freq, + minPercent: 1., + maxPercent: -1, } if got, err := c.convertToDuty(tt.args.percent); got.String() != tt.want {