From 14d5c605e7686e8fa5283e07624c3360dcbb9b3b Mon Sep 17 00:00:00 2001 From: Cyrille Nofficial Date: Thu, 18 Aug 2022 18:22:24 +0200 Subject: [PATCH] fix(channel6): ignore 'no value' signal for drive mode --- pkg/arduino/arduino.go | 6 +++++- pkg/arduino/arduino_test.go | 14 ++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/pkg/arduino/arduino.go b/pkg/arduino/arduino.go index 4d12768..6e356de 100644 --- a/pkg/arduino/arduino.go +++ b/pkg/arduino/arduino.go @@ -21,7 +21,7 @@ const ( ) var ( - serialLineRegex = regexp.MustCompile(`(?P\d+),(?P\d+),(?P\d+),(?P\d+),(?P\d+),(?P\d+),(?P\d+),(?P\d+),(?P\d+),(?P\d+)`) + serialLineRegex = regexp.MustCompile(`(?P\d+),(?P\d+),(?P\d+),(?P\d+),(?P\d+),(?P\d+),(?P-?\d+),(?P\d+),(?P\d+),(?P\d+)`) DefaultPwmThrottle = PWMConfig{ Min: MinPwmThrottle, Max: MaxPwmThrottle, @@ -255,6 +255,10 @@ func (a *Part) processChannel6(v string) { zap.S().Errorf("invalid value for channel6 'drive-mode', should be an int: %v", err) return } + if value < 0 { + // No value, ignore it + return + } if value > 1800 { if a.driveMode != events.DriveMode_PILOT { zap.S().Infof("Update channel 6 'drive-mode' with value %v, new user_mode: %v", value, events.DriveMode_PILOT) diff --git a/pkg/arduino/arduino_test.go b/pkg/arduino/arduino_test.go index ba9c1c8..7fa2f16 100644 --- a/pkg/arduino/arduino_test.go +++ b/pkg/arduino/arduino_test.go @@ -81,9 +81,9 @@ func TestArduinoPart_Update(t *testing.T) { fmt.Sprintf("12345,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, channel2, channel3, channel4, channel5, channel6, channel7, channel8), defaultPwmThrottleConfig, -1., -1., events.DriveMode_USER, false}, - {"Unparsable line", + {"Invalid line", "12350,invalid line\n", defaultPwmThrottleConfig, - -1., -1., events.DriveMode_USER, false}, + -1., -1., events.DriveMode_INVALID, false}, {"Switch record on", fmt.Sprintf("12355,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, channel2, channel3, channel4, 998, channel6, channel7, channel8), defaultPwmThrottleConfig, -1., -1., events.DriveMode_USER, true}, @@ -150,6 +150,15 @@ func TestArduinoPart_Update(t *testing.T) { {"Use 2nd rc: use channels 7 and 8", fmt.Sprintf("12440,%d,%d,%d,%d,%d,%d,%d,%d,50\n", 1000, 1000, 1950, channel4, channel5, channel6, 2000, 2008), defaultPwmThrottleConfig, 1., 1, events.DriveMode_USER, false}, + {"Drive Mode: user", + fmt.Sprintf("12430,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, channel6, channel3, channel4, channel5, 900, channel7, channel8), + defaultPwmThrottleConfig, -1., -1., events.DriveMode_USER, false}, + {"Drive Mode: pilot", + fmt.Sprintf("12430,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, channel6, channel3, channel4, channel5, 1950, channel7, channel8), + defaultPwmThrottleConfig, -1., -1., events.DriveMode_PILOT, false}, + {"Drive Mode: no value", + fmt.Sprintf("12430,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, channel6, channel3, channel4, channel5, -1, channel7, channel8), + defaultPwmThrottleConfig, -1., -1., events.DriveMode_INVALID, false}, } for _, c := range cases { @@ -165,6 +174,7 @@ func TestArduinoPart_Update(t *testing.T) { } a.pwmThrottleConfig = c.throttlePwmConfig + a.driveMode = events.DriveMode_INVALID time.Sleep(10 * time.Millisecond) a.mutex.Lock()