Compare commits

...

5 Commits

Author SHA1 Message Date
Cyrille Nofficial
419a1ddd72 refactor(cli): rewrite log configuration 2022-06-16 12:00:23 +02:00
Cyrille Nofficial
8312e7a3bd fix: publish patched payload 2022-06-16 09:10:32 +02:00
Cyrille Nofficial
41f99b7f57 build: use cache to build go binary 2022-06-15 17:13:34 +02:00
Cyrille Nofficial
8740f5eaf9 build: exclude binary from git history 2022-06-15 16:54:16 +02:00
Cyrille Nofficial
72e93141d9 feat: implement max throttle limiter on user drive
close robocars/robocar-setup#7
2022-06-14 15:55:33 +02:00
4 changed files with 55 additions and 37 deletions

View File

@ -26,11 +26,12 @@ image_build(){
printf "\n\nBuild go binary %s\n\n" "${BINARY}.${binary_suffix}"
CGO_ENABLED=0 GOOS=${GOOS} GOARCH=${GOARCH} GOARM=${GOARM} go build -mod vendor -a ${GOTAGS} -o "${BINARY}.${binary_suffix}" ./cmd/${BINARY}/
mkdir -p build
CGO_ENABLED=0 GOOS=${GOOS} GOARCH=${GOARCH} GOARM=${GOARM} go build -mod vendor ${GOTAGS} -o "build/${BINARY}.${binary_suffix}" ./cmd/${BINARY}/
buildah --os "$GOOS" --arch "$GOARCH" $VARIANT --name "$containerName" from gcr.io/distroless/static
buildah config --user 1234 "$containerName"
buildah copy "$containerName" "${BINARY}.${binary_suffix}" /go/bin/$BINARY
buildah copy "$containerName" "build/${BINARY}.${binary_suffix}" /go/bin/$BINARY
buildah config --entrypoint '["/go/bin/'$BINARY'"]' "${containerName}"
buildah commit --rm --manifest $IMAGE_NAME "${containerName}" "${containerName}"

View File

@ -18,7 +18,6 @@ func main() {
var mqttBroker, username, password, clientId string
var throttleTopic, driveModeTopic, rcThrottleTopic string
var minThrottle, maxThrottle float64
var debug bool
err := cli.SetFloat64DefaultValueFromEnv(&minThrottle, "THROTTLE_MIN", DefaultThrottleMin)
if err != nil {
@ -39,7 +38,7 @@ func main() {
flag.StringVar(&rcThrottleTopic, "mqtt-topic-rc-throttle", os.Getenv("MQTT_TOPIC_RC_THROTTLE"), "Mqtt topic that contains RC Throttle value, use MQTT_TOPIC_RC_THROTTLE if args not set")
flag.Float64Var(&minThrottle, "throttle-min", minThrottle, "Minimum throttle value, use THROTTLE_MIN if args not set")
flag.Float64Var(&maxThrottle, "throttle-max", maxThrottle, "Minimum throttle value, use THROTTLE_MAX if args not set")
flag.BoolVar(&debug, "debug", false, "Display raw value to debug")
logLevel := zap.LevelFlag("log", zap.InfoLevel, "log level")
flag.Parse()
if len(os.Args) <= 1 {
@ -48,11 +47,7 @@ func main() {
}
config := zap.NewDevelopmentConfig()
if debug {
config.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
} else {
config.Level = zap.NewAtomicLevelAt(zap.InfoLevel)
}
config.Level = zap.NewAtomicLevelAt(*logLevel)
lgr, err := config.Build()
if err != nil {
log.Fatalf("unable to init logger: %v", err)

View File

@ -98,9 +98,26 @@ func (p *ThrottlePart) onRCThrottle(_ mqtt.Client, message mqtt.Message) {
p.muDriveMode.RLock()
defer p.muDriveMode.RUnlock()
if p.driveMode == events.DriveMode_USER {
zap.S().Debug("publish new throttle value from rc")
// Republish same content
payload := message.Payload()
var throttleMsg events.ThrottleMessage
err := proto.Unmarshal(payload, &throttleMsg)
if err != nil {
zap.S().Errorf("unable to unmarshall throttle msg to check throttle value: %v", err)
return
}
zap.S().Debugf("publish new throttle value from rc: %v", throttleMsg.GetThrottle())
if throttleMsg.GetThrottle() > p.maxThrottle {
zap.S().Debugf("throttle upper that max value allowed, patch value from %v to %v", throttleMsg.GetThrottle(), p.maxThrottle)
throttleMsg.Throttle = p.maxThrottle
payloadPatched, err := proto.Marshal(&throttleMsg)
if err != nil {
zap.S().Errorf("unable to marshall throttle msg: %v", err)
return
}
publish(p.client, p.throttleTopic, &payloadPatched)
return
}
publish(p.client, p.throttleTopic, &payload)
}
}

View File

@ -38,47 +38,52 @@ func TestDefaultThrottle(t *testing.T) {
p := NewPart(nil, throttleTopic, driveModeTopic, rcThrottleTopic, minValue, 1., 200)
cases := []struct {
name string
maxThrottle float32
driveMode events.DriveModeMessage
rcThrottle events.ThrottleMessage
expectedThrottle events.ThrottleMessage
}{
{events.DriveModeMessage{DriveMode: events.DriveMode_USER}, events.ThrottleMessage{Throttle: 0.3, Confidence: 1.0}, events.ThrottleMessage{Throttle: 0.3, Confidence: 1.0}},
{events.DriveModeMessage{DriveMode: events.DriveMode_PILOT}, events.ThrottleMessage{Throttle: 0.5, Confidence: 1.0}, events.ThrottleMessage{Throttle: minValue, Confidence: 1.0}},
{events.DriveModeMessage{DriveMode: events.DriveMode_PILOT}, events.ThrottleMessage{Throttle: 0.4, Confidence: 1.0}, events.ThrottleMessage{Throttle: minValue, Confidence: 1.0}},
{events.DriveModeMessage{DriveMode: events.DriveMode_USER}, events.ThrottleMessage{Throttle: 0.5, Confidence: 1.0}, events.ThrottleMessage{Throttle: 0.5, Confidence: 1.0}},
{events.DriveModeMessage{DriveMode: events.DriveMode_USER}, events.ThrottleMessage{Throttle: 0.4, Confidence: 1.0}, events.ThrottleMessage{Throttle: 0.4, Confidence: 1.0}},
{events.DriveModeMessage{DriveMode: events.DriveMode_USER}, events.ThrottleMessage{Throttle: 0.6, Confidence: 1.0}, events.ThrottleMessage{Throttle: 0.6, Confidence: 1.0}},
{"test1", 1., events.DriveModeMessage{DriveMode: events.DriveMode_USER}, events.ThrottleMessage{Throttle: 0.3, Confidence: 1.0}, events.ThrottleMessage{Throttle: 0.3, Confidence: 1.0}},
{"test2", 1., events.DriveModeMessage{DriveMode: events.DriveMode_PILOT}, events.ThrottleMessage{Throttle: 0.5, Confidence: 1.0}, events.ThrottleMessage{Throttle: minValue, Confidence: 1.0}},
{"test3", 1., events.DriveModeMessage{DriveMode: events.DriveMode_PILOT}, events.ThrottleMessage{Throttle: 0.4, Confidence: 1.0}, events.ThrottleMessage{Throttle: minValue, Confidence: 1.0}},
{"test4", 1., events.DriveModeMessage{DriveMode: events.DriveMode_USER}, events.ThrottleMessage{Throttle: 0.5, Confidence: 1.0}, events.ThrottleMessage{Throttle: 0.5, Confidence: 1.0}},
{"test5", 1., events.DriveModeMessage{DriveMode: events.DriveMode_USER}, events.ThrottleMessage{Throttle: 0.4, Confidence: 1.0}, events.ThrottleMessage{Throttle: 0.4, Confidence: 1.0}},
{"test6", 1., events.DriveModeMessage{DriveMode: events.DriveMode_USER}, events.ThrottleMessage{Throttle: 0.6, Confidence: 1.0}, events.ThrottleMessage{Throttle: 0.6, Confidence: 1.0}},
{"limit max throttle on user mode", 0.4, events.DriveModeMessage{DriveMode: events.DriveMode_USER}, events.ThrottleMessage{Throttle: 0.6, Confidence: 1.0}, events.ThrottleMessage{Throttle: 0.4, Confidence: 1.0}},
}
go p.Start()
defer func() { close(p.cancel) }()
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
p.maxThrottle = c.maxThrottle
p.onDriveMode(nil, testtools.NewFakeMessageFromProtobuf(driveModeTopic, &c.driveMode))
p.onRCThrottle(nil, testtools.NewFakeMessageFromProtobuf(rcThrottleTopic, &c.rcThrottle))
p.onDriveMode(nil, testtools.NewFakeMessageFromProtobuf(driveModeTopic, &c.driveMode))
p.onRCThrottle(nil, testtools.NewFakeMessageFromProtobuf(rcThrottleTopic, &c.rcThrottle))
time.Sleep(10 * time.Millisecond)
time.Sleep(10 * time.Millisecond)
for i := 3; i >= 0; i-- {
for i := 3; i >= 0; i-- {
var msg events.ThrottleMessage
muEventsPublished.Lock()
err := proto.Unmarshal(eventsPublished[throttleTopic], &msg)
if err != nil {
t.Errorf("unable to unmarshall response: %v", err)
t.Fail()
}
muEventsPublished.Unlock()
var msg events.ThrottleMessage
muEventsPublished.Lock()
err := proto.Unmarshal(eventsPublished[throttleTopic], &msg)
if err != nil {
t.Errorf("unable to unmarshall response: %v", err)
t.Fail()
if msg.GetThrottle() != c.expectedThrottle.GetThrottle() {
t.Errorf("bad msg value for mode %v: %v, wants %v", c.driveMode, msg.GetThrottle(), c.expectedThrottle.GetThrottle())
}
if msg.GetConfidence() != 1. {
t.Errorf("bad throtlle confidence: %v, wants %v", msg.GetConfidence(), 1.)
}
time.Sleep(1 * time.Millisecond)
}
muEventsPublished.Unlock()
if msg.GetThrottle() != c.expectedThrottle.GetThrottle() {
t.Errorf("bad msg value for mode %v: %v, wants %v", c.driveMode, msg.GetThrottle(), c.expectedThrottle.GetThrottle())
}
if msg.GetConfidence() != 1. {
t.Errorf("bad throtlle confidence: %v, wants %v", msg.GetConfidence(), 1.)
}
time.Sleep(1 * time.Millisecond)
}
})
}
}