From 973ebb9df9e10ec5e0ad32a581f8355549ab6e93 Mon Sep 17 00:00:00 2001 From: Cyrille Nofficial Date: Mon, 13 Jun 2022 18:35:27 +0200 Subject: [PATCH] feat: asymetric throttle --- pkg/arduino/arduino.go | 11 ++++++++++- pkg/arduino/arduino_test.go | 8 ++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/pkg/arduino/arduino.go b/pkg/arduino/arduino.go index a9a72ea..cba5ccc 100644 --- a/pkg/arduino/arduino.go +++ b/pkg/arduino/arduino.go @@ -170,7 +170,16 @@ func (a *Part) processChannel2(v string) { } else if value > a.pwmThrottleConfig.Max { value = a.pwmThrottleConfig.Max } - a.throttle = ((float32(value)-MinPwmThrottle)/(MaxPwmThrottle-MinPwmThrottle))*2.0 - 1.0 + + throttle := 0. + if value > a.pwmThrottleConfig.Zero { + throttle = (float64(value) - float64(a.pwmThrottleConfig.Zero)) / float64(a.pwmThrottleConfig.Max-a.pwmThrottleConfig.Zero) + } + if value < a.pwmThrottleConfig.Zero { + throttle = -1. * (float64(a.pwmThrottleConfig.Zero) - float64(value)) / (float64(a.pwmThrottleConfig.Zero - a.pwmThrottleConfig.Min)) + } + + a.throttle = float32(throttle) } func (a *Part) processChannel3(v string) { diff --git a/pkg/arduino/arduino_test.go b/pkg/arduino/arduino_test.go index 24fb546..01a94c7 100644 --- a/pkg/arduino/arduino_test.go +++ b/pkg/arduino/arduino_test.go @@ -148,7 +148,7 @@ func TestArduinoPart_Update(t *testing.T) { {"Throttle: stop", fmt.Sprintf("12430,%d,%d,%d,%d,%d,%d,50,%d\n", channel1, 1450, channel3, channel4, channel5, channel6, distanceCm), defaultPwmThrottleConfig, - -0.03, -1., events.DriveMode_USER, false}, + 0.0, -1., events.DriveMode_USER, false}, {"Throttle: up", fmt.Sprintf("12435,%d,%d,%d,%d,%d,%d,50,%d\n", channel1, 1948, channel3, channel4, channel5, channel6, distanceCm), defaultPwmThrottleConfig, @@ -157,6 +157,10 @@ func TestArduinoPart_Update(t *testing.T) { fmt.Sprintf("12440,%d,%d,%d,%d,%d,%d,50,%d\n", channel1, 2998, channel3, channel4, channel5, channel6, distanceCm), defaultPwmThrottleConfig, 1., -1., events.DriveMode_USER, false}, + {"Throttle: zero not middle", + fmt.Sprintf("12440,%d,%d,%d,%d,%d,%d,50,%d\n", channel1, 1600, channel3, channel4, channel5, channel6, distanceCm), + PWMThrottleConfig{1000, 1700, 1500}, + 0.5, -1., events.DriveMode_USER, false}, } for _, c := range cases { @@ -169,7 +173,7 @@ func TestArduinoPart_Update(t *testing.T) { if err != nil { t.Error("unable to flush content") } - + a.pwmThrottleConfig = c.throttlePwmConfig time.Sleep(10 * time.Millisecond)