robocar-throttle/pkg/throttle/throttle.go

141 lines
3.7 KiB
Go
Raw Normal View History

2022-09-05 14:44:02 +00:00
package throttle
import (
"github.com/cyrilix/robocar-base/service"
2020-01-01 19:33:41 +00:00
"github.com/cyrilix/robocar-protobuf/go/events"
mqtt "github.com/eclipse/paho.mqtt.golang"
2021-10-12 19:56:42 +00:00
"go.uber.org/zap"
2022-01-03 15:56:33 +00:00
"google.golang.org/protobuf/proto"
"sync"
"time"
)
2022-09-05 14:44:02 +00:00
func New(client mqtt.Client, throttleTopic, driveModeTopic, rcThrottleTopic string, minValue, maxValue float32, publishPilotFrequency int) *Controller {
return &Controller{
client: client,
throttleTopic: throttleTopic,
driveModeTopic: driveModeTopic,
rcThrottleTopic: rcThrottleTopic,
minThrottle: minValue,
maxThrottle: maxValue,
2020-01-01 19:33:41 +00:00
driveMode: events.DriveMode_USER,
publishPilotFrequency: publishPilotFrequency,
}
}
2022-09-05 14:44:02 +00:00
type Controller struct {
client mqtt.Client
throttleTopic string
2020-01-01 19:33:41 +00:00
minThrottle, maxThrottle float32
muDriveMode sync.RWMutex
2020-01-01 19:33:41 +00:00
driveMode events.DriveMode
cancel chan interface{}
publishPilotFrequency int
driveModeTopic, rcThrottleTopic string
}
2022-09-05 14:44:02 +00:00
func (c *Controller) Start() error {
if err := registerCallbacks(c); err != nil {
2021-10-12 19:56:42 +00:00
zap.S().Errorf("unable to register callbacks: %v", err)
return err
}
2022-09-05 14:44:02 +00:00
c.cancel = make(chan interface{})
ticker := time.NewTicker(1 * time.Second / time.Duration(c.publishPilotFrequency))
for {
select {
case <-ticker.C:
2022-09-05 14:44:02 +00:00
c.onPublishPilotValue()
case <-c.cancel:
break
}
}
}
2022-09-05 14:44:02 +00:00
func (c *Controller) onPublishPilotValue() {
c.muDriveMode.RLock()
defer c.muDriveMode.RUnlock()
2022-09-05 14:44:02 +00:00
if c.driveMode != events.DriveMode_PILOT {
return
}
2020-01-01 19:33:41 +00:00
throttleMsg := events.ThrottleMessage{
2022-09-05 14:44:02 +00:00
Throttle: c.minThrottle,
2020-03-01 18:44:59 +00:00
Confidence: 1.0,
2020-01-01 19:33:41 +00:00
}
payload, err := proto.Marshal(&throttleMsg)
if err != nil {
2021-10-12 19:56:42 +00:00
zap.S().Errorf("unable to marshal %T protobuf content: %err", throttleMsg, err)
2020-01-01 19:33:41 +00:00
return
}
2022-09-05 14:44:02 +00:00
publish(c.client, c.throttleTopic, &payload)
}
2022-09-05 14:44:02 +00:00
func (c *Controller) Stop() {
close(c.cancel)
service.StopService("throttle", c.client, c.driveModeTopic, c.rcThrottleTopic)
}
2022-09-05 14:44:02 +00:00
func (c *Controller) onDriveMode(_ mqtt.Client, message mqtt.Message) {
2020-01-01 19:33:41 +00:00
var msg events.DriveModeMessage
err := proto.Unmarshal(message.Payload(), &msg)
if err != nil {
2021-10-12 19:56:42 +00:00
zap.S().Errorf("unable to unmarshal protobuf %T message: %v", msg, err)
2020-01-01 19:33:41 +00:00
return
}
2022-09-05 14:44:02 +00:00
c.muDriveMode.Lock()
defer c.muDriveMode.Unlock()
c.driveMode = msg.GetDriveMode()
}
2022-09-05 14:44:02 +00:00
func (c *Controller) onRCThrottle(_ mqtt.Client, message mqtt.Message) {
c.muDriveMode.RLock()
defer c.muDriveMode.RUnlock()
if c.driveMode == events.DriveMode_USER {
2020-01-01 19:33:41 +00:00
// 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
}
2022-06-15 17:22:30 +00:00
zap.S().Debugf("publish new throttle value from rc: %v", throttleMsg.GetThrottle())
2022-09-05 14:44:02 +00:00
if throttleMsg.GetThrottle() > c.maxThrottle {
zap.S().Debugf("throttle upper that max value allowed, patch value from %v to %v", throttleMsg.GetThrottle(), c.maxThrottle)
throttleMsg.Throttle = c.maxThrottle
2022-06-15 17:22:30 +00:00
payloadPatched, err := proto.Marshal(&throttleMsg)
if err != nil {
zap.S().Errorf("unable to marshall throttle msg: %v", err)
return
}
2022-09-05 14:44:02 +00:00
publish(c.client, c.throttleTopic, &payloadPatched)
2022-06-15 17:22:30 +00:00
return
}
2022-09-05 14:44:02 +00:00
publish(c.client, c.throttleTopic, &payload)
}
}
2022-09-05 14:44:02 +00:00
var registerCallbacks = func(p *Controller) error {
err := service.RegisterCallback(p.client, p.driveModeTopic, p.onDriveMode)
if err != nil {
return err
}
err = service.RegisterCallback(p.client, p.rcThrottleTopic, p.onRCThrottle)
if err != nil {
return err
}
return nil
}
2020-01-01 19:33:41 +00:00
var publish = func(client mqtt.Client, topic string, payload *[]byte) {
client.Publish(topic, 0, false, *payload)
}