robocar-throttle/pkg/part/part.go

123 lines
3.0 KiB
Go
Raw Permalink Normal View History

package part
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"
2020-01-01 19:33:41 +00:00
"github.com/golang/protobuf/proto"
2021-10-12 19:56:42 +00:00
"go.uber.org/zap"
"sync"
"time"
)
2020-01-01 19:33:41 +00:00
func NewPart(client mqtt.Client, throttleTopic, driveModeTopic, rcThrottleTopic string, minValue, maxValue float32, publishPilotFrequency int) *ThrottlePart {
return &ThrottlePart{
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,
}
}
type ThrottlePart 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
}
func (p *ThrottlePart) Start() error {
if err := registerCallbacks(p); err != nil {
2021-10-12 19:56:42 +00:00
zap.S().Errorf("unable to register callbacks: %v", err)
return err
}
p.cancel = make(chan interface{})
ticker := time.NewTicker(1 * time.Second / time.Duration(p.publishPilotFrequency))
for {
select {
case <-ticker.C:
2020-03-01 18:44:59 +00:00
p.onPublishPilotValue()
case <-p.cancel:
break
}
}
}
2020-03-01 18:44:59 +00:00
func (p *ThrottlePart) onPublishPilotValue() {
p.muDriveMode.RLock()
defer p.muDriveMode.RUnlock()
2020-01-01 19:33:41 +00:00
if p.driveMode != events.DriveMode_PILOT {
return
}
2020-01-01 19:33:41 +00:00
throttleMsg := events.ThrottleMessage{
2020-03-01 18:44:59 +00:00
Throttle: p.minThrottle,
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
}
publish(p.client, p.throttleTopic, &payload)
}
func (p *ThrottlePart) Stop() {
close(p.cancel)
service.StopService("throttle", p.client, p.driveModeTopic, p.rcThrottleTopic)
}
func (p *ThrottlePart) 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
}
p.muDriveMode.Lock()
defer p.muDriveMode.Unlock()
2020-01-01 19:33:41 +00:00
p.driveMode = msg.GetDriveMode()
}
func (p *ThrottlePart) onRCThrottle(_ mqtt.Client, message mqtt.Message) {
p.muDriveMode.RLock()
defer p.muDriveMode.RUnlock()
2020-01-01 19:33:41 +00:00
if p.driveMode == events.DriveMode_USER {
// Republish same content
payload := message.Payload()
publish(p.client, p.throttleTopic, &payload)
}
}
2020-03-01 18:44:59 +00:00
var registerCallbacks = func(p *ThrottlePart) 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)
}