refactor: extract code used to compute throttle
This commit is contained in:
parent
d9b0dbb07a
commit
34c4433836
@ -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()
|
||||
|
13
pkg/throttle/processor.go
Normal file
13
pkg/throttle/processor.go
Normal file
@ -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))
|
||||
}
|
86
pkg/throttle/processor_test.go
Normal file
86
pkg/throttle/processor_test.go
Normal file
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user