robocar-steering/pkg/steering/controller.go

124 lines
3.2 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-20 19:38:19 +00:00
func NewController(client mqtt.Client, steeringTopic, driveModeTopic, rcSteeringTopic, tfSteeringTopic string, debug bool) *Controller {
return &Controller{
2020-02-08 21:35:47 +00:00
client: client,
steeringTopic: steeringTopic,
driveModeTopic: driveModeTopic,
rcSteeringTopic: rcSteeringTopic,
tfSteeringTopic: tfSteeringTopic,
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
cancel chan interface{}
driveModeTopic, rcSteeringTopic, tfSteeringTopic string
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-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
}
return nil
}
var publish = func(client mqtt.Client, topic string, payload *[]byte) {
client.Publish(topic, 0, false, *payload)
}