robocar-steering/pkg/steering/controller.go

146 lines
3.7 KiB
Go
Raw Normal View History

2022-08-20 19:33:45 +00:00
package steering
2020-02-08 21:35:47 +00:00
import (
"github.com/cyrilix/robocar-base/service"
"github.com/cyrilix/robocar-protobuf/go/events"
mqtt "github.com/eclipse/paho.mqtt.golang"
2021-10-12 17:47:33 +00:00
"go.uber.org/zap"
2022-01-03 17:07:56 +00:00
"google.golang.org/protobuf/proto"
2020-02-08 21:35:47 +00:00
"sync"
)
2022-08-22 11:12:12 +00:00
func NewController(client mqtt.Client, steeringTopic, driveModeTopic, rcSteeringTopic, tfSteeringTopic, objectsTopic string) *Controller {
2022-08-20 19:38:19 +00:00
return &Controller{
2020-02-08 21:35:47 +00:00
client: client,
steeringTopic: steeringTopic,
driveModeTopic: driveModeTopic,
rcSteeringTopic: rcSteeringTopic,
tfSteeringTopic: tfSteeringTopic,
2022-08-22 11:12:12 +00:00
objectsTopic: objectsTopic,
2020-02-08 21:35:47 +00:00
driveMode: events.DriveMode_USER,
}
}
2022-08-20 19:38:19 +00:00
type Controller struct {
2020-02-08 21:35:47 +00:00
client mqtt.Client
steeringTopic string
muDriveMode sync.RWMutex
driveMode events.DriveMode
2022-08-22 11:12:12 +00:00
cancel chan interface{}
driveModeTopic, rcSteeringTopic, tfSteeringTopic, objectsTopic string
muObjects sync.RWMutex
objects []*events.Object
debug bool
2020-02-08 21:35:47 +00:00
}
2022-08-20 19:38:19 +00:00
func (p *Controller) Start() error {
2020-02-08 21:35:47 +00:00
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
}
2022-08-20 19:38:19 +00:00
func (p *Controller) Stop() {
2020-02-08 21:35:47 +00:00
close(p.cancel)
service.StopService("throttle", p.client, p.driveModeTopic, p.rcSteeringTopic, p.tfSteeringTopic)
}
2022-08-22 11:12:12 +00:00
func (p *Controller) onObjects(_ mqtt.Client, message mqtt.Message) {
var msg events.ObjectsMessage
err := proto.Unmarshal(message.Payload(), &msg)
if err != nil {
zap.S().Errorf("unable to unmarshal protobuf %T message: %v", msg, err)
return
}
p.muObjects.Lock()
defer p.muObjects.Unlock()
p.objects = msg.GetObjects()
}
2022-08-20 19:38:19 +00:00
func (p *Controller) onDriveMode(_ mqtt.Client, message mqtt.Message) {
2020-02-08 21:35:47 +00:00
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()
}
2022-08-20 19:38:19 +00:00
func (p *Controller) onRCSteering(_ mqtt.Client, message mqtt.Message) {
2020-02-08 21:35:47 +00:00
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)
}
}
2022-08-20 19:38:19 +00:00
func (p *Controller) onTFSteering(_ mqtt.Client, message mqtt.Message) {
2020-02-08 21:35:47 +00:00
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)
}
}
2022-08-20 19:38:19 +00:00
var registerCallbacks = func(p *Controller) error {
2020-02-08 21:35:47 +00:00
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
}
2022-08-22 11:12:12 +00:00
err = service.RegisterCallback(p.client, p.objectsTopic, p.onObjects)
if err != nil {
return err
}
2020-02-08 21:35:47 +00:00
return nil
}
var publish = func(client mqtt.Client, topic string, payload *[]byte) {
client.Publish(topic, 0, false, *payload)
}