From f046bead3720ab373fc03fed1448aa542de7aeb6 Mon Sep 17 00:00:00 2001 From: Cyrille Nofficial Date: Thu, 16 Jun 2022 09:28:01 +0200 Subject: [PATCH] fix: compute duty with raw value in order to increase precision --- pkg/actuator/pca9685.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/pkg/actuator/pca9685.go b/pkg/actuator/pca9685.go index 2c7d3d0..ff5c093 100644 --- a/pkg/actuator/pca9685.go +++ b/pkg/actuator/pca9685.go @@ -58,11 +58,15 @@ func (c *Pca9685Controller) convertToDuty(percent float32) (gpio.Duty, error) { per := c.freq.Period().Microseconds() draw := float64(pw) / float64(per) - 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 + 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 { @@ -77,6 +81,8 @@ func (c *Pca9685Controller) Close() error { } func (c *Pca9685Controller) SetDuty(d gpio.Duty) error { + c.logr.Debugf("set duty %v -> %d (12bits value: %d)", d.String(), d, d>>12) + err := c.pin.PWM(d, c.freq) if err != nil { return fmt.Errorf("unable to set pwm value: %v", err)