From 34c4433836ff89ef717331ae994682d77c9a7e1c Mon Sep 17 00:00:00 2001 From: Cyrille Nofficial Date: Mon, 5 Sep 2022 16:44:02 +0200 Subject: [PATCH] refactor: extract code used to compute throttle --- pkg/throttle/controller.go | 18 +++---- pkg/throttle/processor.go | 13 +++++ pkg/throttle/processor_test.go | 86 ++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 12 deletions(-) create mode 100644 pkg/throttle/processor.go create mode 100644 pkg/throttle/processor_test.go diff --git a/pkg/throttle/controller.go b/pkg/throttle/controller.go index 4c0e4e7..4bfc334 100644 --- a/pkg/throttle/controller.go +++ b/pkg/throttle/controller.go @@ -6,7 +6,6 @@ import ( mqtt "github.com/eclipse/paho.mqtt.golang" "go.uber.org/zap" "google.golang.org/protobuf/proto" - "math" "sync" "time" ) @@ -18,18 +17,19 @@ func New(client mqtt.Client, throttleTopic, driveModeTopic, rcThrottleTopic, ste driveModeTopic: driveModeTopic, rcThrottleTopic: rcThrottleTopic, steeringTopic: steeringTopic, - minThrottle: minValue, maxThrottle: maxValue, driveMode: events.DriveMode_USER, publishPilotFrequency: publishPilotFrequency, + steeringProcessor: &SteeringProcessor{minThrottle: minValue, maxThrottle: maxValue}, } } type Controller struct { - client mqtt.Client - throttleTopic string - minThrottle, maxThrottle float32 + client mqtt.Client + throttleTopic string + maxThrottle float32 + steeringProcessor *SteeringProcessor muDriveMode sync.RWMutex driveMode events.DriveMode @@ -69,7 +69,7 @@ func (c *Controller) onPublishPilotValue() { } throttleMsg := events.ThrottleMessage{ - Throttle: c.computeThrottle(), + Throttle: c.steeringProcessor.Process(c.readSteering()), Confidence: 1.0, } payload, err := proto.Marshal(&throttleMsg) @@ -82,12 +82,6 @@ func (c *Controller) onPublishPilotValue() { } -func (c *Controller) computeThrottle() float32 { - s := c.readSteering() - absSteering := math.Abs(float64(s)) - return c.minThrottle + float32(float64(c.maxThrottle-c.minThrottle)*(1-absSteering)) -} - func (c *Controller) readSteering() float32 { c.muSteering.RLock() defer c.muSteering.RUnlock() diff --git a/pkg/throttle/processor.go b/pkg/throttle/processor.go new file mode 100644 index 0000000..f90e805 --- /dev/null +++ b/pkg/throttle/processor.go @@ -0,0 +1,13 @@ +package throttle + +import "math" + +type SteeringProcessor struct { + minThrottle, maxThrottle float32 +} + +// Process compute throttle from steering value +func (sp *SteeringProcessor) Process(steering float32) float32 { + absSteering := math.Abs(float64(steering)) + return sp.minThrottle + float32(float64(sp.maxThrottle-sp.minThrottle)*(1-absSteering)) +} diff --git a/pkg/throttle/processor_test.go b/pkg/throttle/processor_test.go new file mode 100644 index 0000000..6fb80bc --- /dev/null +++ b/pkg/throttle/processor_test.go @@ -0,0 +1,86 @@ +package throttle + +import "testing" + +func TestSteeringProcessor_Process(t *testing.T) { + type fields struct { + minThrottle float32 + maxThrottle float32 + } + type args struct { + steering float32 + } + tests := []struct { + name string + fields fields + args args + want float32 + }{ + { + name: "steering straight", + fields: fields{ + minThrottle: 0.2, + maxThrottle: 0.5, + }, + args: args{ + steering: 0., + }, + want: 0.5, + }, + { + name: "steering full left should return min throttle", + fields: fields{ + minThrottle: 0.2, + maxThrottle: 0.5, + }, + args: args{ + steering: -1., + }, + want: 0.2, + }, + { + name: "steering full right should return min throttle", + fields: fields{ + minThrottle: 0.2, + maxThrottle: 0.5, + }, + args: args{ + steering: 1., + }, + want: 0.2, + }, + { + name: "steering mid-left should return intermediate throttle", + fields: fields{ + minThrottle: 0.3, + maxThrottle: 0.5, + }, + args: args{ + steering: -0.5, + }, + want: 0.4, + }, + { + name: "steering mid-right should return intermediate throttle", + fields: fields{ + minThrottle: 0.3, + maxThrottle: 0.5, + }, + args: args{ + steering: 0.5, + }, + want: 0.4, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + sp := &SteeringProcessor{ + minThrottle: tt.fields.minThrottle, + maxThrottle: tt.fields.maxThrottle, + } + if got := sp.Process(tt.args.steering); got != tt.want { + t.Errorf("Process() = %v, want %v", got, tt.want) + } + }) + } +}