robocar-steering/pkg/part/part.go

125 lines
3.2 KiB
Go
Raw Normal View History

2020-02-08 21:35:47 +00:00
package part
import (
"github.com/cyrilix/robocar-base/service"
"github.com/cyrilix/robocar-protobuf/go/events"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/golang/protobuf/proto"
2021-10-12 17:47:33 +00:00
"go.uber.org/zap"
2020-02-08 21:35:47 +00:00
"sync"
)
func NewPart(client mqtt.Client, steeringTopic, driveModeTopic, rcSteeringTopic, tfSteeringTopic string, debug bool) *SteeringPart {
2020-02-08 21:35:47 +00:00
return &SteeringPart{
client: client,
steeringTopic: steeringTopic,
driveModeTopic: driveModeTopic,
rcSteeringTopic: rcSteeringTopic,
tfSteeringTopic: tfSteeringTopic,
driveMode: events.DriveMode_USER,
debug: debug,
2020-02-08 21:35:47 +00:00
}
}
type SteeringPart struct {
client mqtt.Client
steeringTopic string
muDriveMode sync.RWMutex
driveMode events.DriveMode
cancel chan interface{}
driveModeTopic, rcSteeringTopic, tfSteeringTopic string
debug bool
2020-02-08 21:35:47 +00:00
}
func (p *SteeringPart) Start() error {
if err := registerCallbacks(p); err != nil {
2021-10-12 17:47:33 +00:00
zap.S().Errorf("unable to rgeister callbacks: %v", err)
2020-02-08 21:35:47 +00:00
return err
}
p.cancel = make(chan interface{})
<-p.cancel
return nil
}
func (p *SteeringPart) Stop() {
close(p.cancel)
service.StopService("throttle", p.client, p.driveModeTopic, p.rcSteeringTopic, p.tfSteeringTopic)
}
func (p *SteeringPart) onDriveMode(_ mqtt.Client, message mqtt.Message) {
var msg events.DriveModeMessage
err := proto.Unmarshal(message.Payload(), &msg)
if err != nil {
2021-10-12 17:47:33 +00:00
zap.S().Errorf("unable to unmarshal protobuf %T message: %v", msg, err)
2020-02-08 21:35:47 +00:00
return
}
p.muDriveMode.Lock()
defer p.muDriveMode.Unlock()
p.driveMode = msg.GetDriveMode()
}
func (p *SteeringPart) onRCSteering(_ mqtt.Client, message mqtt.Message) {
p.muDriveMode.RLock()
defer p.muDriveMode.RUnlock()
if p.debug {
var evt events.SteeringMessage
err := proto.Unmarshal(message.Payload(), &evt)
if err != nil {
zap.S().Debugf("unable to unmarshal rc event: %v", err)
}else {
zap.S().Debugf("receive steering message from radio command: %0.00f", evt.GetSteering())
}
}
2020-02-08 21:35:47 +00:00
if p.driveMode == events.DriveMode_USER {
// Republish same content
payload := message.Payload()
publish(p.client, p.steeringTopic, &payload)
}
}
func (p *SteeringPart) onTFSteering(_ mqtt.Client, message mqtt.Message) {
p.muDriveMode.RLock()
defer p.muDriveMode.RUnlock()
if p.debug {
var evt events.SteeringMessage
err := proto.Unmarshal(message.Payload(), &evt)
if err != nil {
zap.S().Debugf("unable to unmarshal tensorflow event: %v", err)
}else {
zap.S().Debugf("receive steering message from tensorflow: %0.00f", evt.GetSteering())
}
}
2020-02-08 21:35:47 +00:00
if p.driveMode == events.DriveMode_PILOT {
// Republish same content
payload := message.Payload()
publish(p.client, p.steeringTopic, &payload)
}
}
var registerCallbacks = func(p *SteeringPart) error {
err := service.RegisterCallback(p.client, p.driveModeTopic, p.onDriveMode)
if err != nil {
return err
}
err = service.RegisterCallback(p.client, p.rcSteeringTopic, p.onRCSteering)
if err != nil {
return err
}
err = service.RegisterCallback(p.client, p.tfSteeringTopic, p.onTFSteering)
if err != nil {
return err
}
return nil
}
var publish = func(client mqtt.Client, topic string, payload *[]byte) {
client.Publish(topic, 0, false, *payload)
}