2022-09-05 14:44:02 +00:00
|
|
|
package throttle
|
2019-12-27 14:38:12 +00:00
|
|
|
|
|
|
|
import (
|
2019-12-27 16:42:10 +00:00
|
|
|
"github.com/cyrilix/robocar-base/service"
|
2020-01-01 19:33:41 +00:00
|
|
|
"github.com/cyrilix/robocar-protobuf/go/events"
|
2022-09-05 13:30:26 +00:00
|
|
|
"github.com/cyrilix/robocar-throttle/pkg/brake"
|
|
|
|
"github.com/cyrilix/robocar-throttle/pkg/types"
|
2019-12-27 14:38:12 +00:00
|
|
|
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"
|
2019-12-27 16:42:10 +00:00
|
|
|
"sync"
|
2019-12-27 14:38:12 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-06-14 18:07:12 +00:00
|
|
|
func New(client mqtt.Client, throttleTopic, driveModeTopic, rcThrottleTopic, steeringTopic, throttleFeedbackTopic,
|
|
|
|
speedZoneTopic string,
|
|
|
|
maxValue types.Throttle, publishPilotFrequency int, opts ...Option) *Controller {
|
2022-09-05 13:30:26 +00:00
|
|
|
c := &Controller{
|
2019-12-27 16:42:10 +00:00
|
|
|
client: client,
|
|
|
|
throttleTopic: throttleTopic,
|
|
|
|
driveModeTopic: driveModeTopic,
|
|
|
|
rcThrottleTopic: rcThrottleTopic,
|
2022-09-05 14:44:02 +00:00
|
|
|
steeringTopic: steeringTopic,
|
2022-09-05 09:22:31 +00:00
|
|
|
throttleFeedbackTopic: throttleFeedbackTopic,
|
2023-06-14 18:07:12 +00:00
|
|
|
speedZoneTopic: speedZoneTopic,
|
2019-12-27 16:42:10 +00:00
|
|
|
maxThrottle: maxValue,
|
2020-01-01 19:33:41 +00:00
|
|
|
driveMode: events.DriveMode_USER,
|
2019-12-27 16:42:10 +00:00
|
|
|
publishPilotFrequency: publishPilotFrequency,
|
2023-06-14 18:07:12 +00:00
|
|
|
processor: &SteeringProcessor{minThrottle: 0.1, maxThrottle: maxValue},
|
2022-09-05 13:30:26 +00:00
|
|
|
brakeCtrl: &brake.DisabledController{},
|
2019-12-27 14:38:12 +00:00
|
|
|
}
|
2022-09-05 13:30:26 +00:00
|
|
|
for _, o := range opts {
|
|
|
|
o(c)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
2019-12-27 14:38:12 +00:00
|
|
|
|
2022-09-05 13:30:26 +00:00
|
|
|
type Option func(c *Controller)
|
|
|
|
|
|
|
|
func WithBrakeController(bc brake.Controller) Option {
|
|
|
|
return func(c *Controller) {
|
|
|
|
c.brakeCtrl = bc
|
|
|
|
}
|
2019-12-27 14:38:12 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 18:07:12 +00:00
|
|
|
func WithThrottleProcessor(p Processor) Option {
|
|
|
|
return func(c *Controller) {
|
|
|
|
c.processor = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 14:44:02 +00:00
|
|
|
type Controller struct {
|
2023-06-14 18:07:12 +00:00
|
|
|
client mqtt.Client
|
|
|
|
throttleTopic string
|
|
|
|
maxThrottle types.Throttle
|
|
|
|
processor Processor
|
2019-12-27 16:42:10 +00:00
|
|
|
|
|
|
|
muDriveMode sync.RWMutex
|
2020-01-01 19:33:41 +00:00
|
|
|
driveMode events.DriveMode
|
2019-12-27 16:42:10 +00:00
|
|
|
|
2022-09-05 14:44:02 +00:00
|
|
|
muSteering sync.RWMutex
|
2023-06-14 18:07:12 +00:00
|
|
|
steering types.Steering
|
2022-09-05 14:44:02 +00:00
|
|
|
|
2022-09-05 13:30:26 +00:00
|
|
|
brakeCtrl brake.Controller
|
2022-09-05 09:22:31 +00:00
|
|
|
|
|
|
|
cancel chan interface{}
|
|
|
|
publishPilotFrequency int
|
|
|
|
driveModeTopic, rcThrottleTopic, steeringTopic, throttleFeedbackTopic string
|
2023-06-14 18:07:12 +00:00
|
|
|
speedZoneTopic string
|
2019-12-27 14:38:12 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2019-12-27 16:42:10 +00:00
|
|
|
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))
|
2019-12-27 14:38:12 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2022-09-05 14:44:02 +00:00
|
|
|
c.onPublishPilotValue()
|
|
|
|
case <-c.cancel:
|
2022-09-05 14:44:02 +00:00
|
|
|
return nil
|
2019-12-27 14:38:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 14:44:02 +00:00
|
|
|
func (c *Controller) onPublishPilotValue() {
|
|
|
|
c.muDriveMode.RLock()
|
|
|
|
defer c.muDriveMode.RUnlock()
|
2019-12-27 16:42:10 +00:00
|
|
|
|
2022-09-05 14:44:02 +00:00
|
|
|
if c.driveMode != events.DriveMode_PILOT {
|
2019-12-27 16:42:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-14 18:07:12 +00:00
|
|
|
throttleFromSteering := c.processor.Process(c.readSteering())
|
2022-09-05 13:30:26 +00:00
|
|
|
|
2020-01-01 19:33:41 +00:00
|
|
|
throttleMsg := events.ThrottleMessage{
|
2022-09-05 13:30:26 +00:00
|
|
|
Throttle: float32(c.brakeCtrl.AdjustThrottle(throttleFromSteering)),
|
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 {
|
2022-09-05 14:44:02 +00:00
|
|
|
zap.S().Errorf("unable to marshal %v protobuf content: %v", throttleMsg.String(), err)
|
2020-01-01 19:33:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-05 14:44:02 +00:00
|
|
|
publish(c.client, c.throttleTopic, payload)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-06-14 18:07:12 +00:00
|
|
|
func (c *Controller) readSteering() types.Steering {
|
2022-09-05 14:44:02 +00:00
|
|
|
c.muSteering.RLock()
|
|
|
|
defer c.muSteering.RUnlock()
|
|
|
|
return c.steering
|
2019-12-27 16:42:10 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 14:44:02 +00:00
|
|
|
func (c *Controller) Stop() {
|
|
|
|
close(c.cancel)
|
2023-06-14 18:07:12 +00:00
|
|
|
service.StopService("throttle", c.client, c.driveModeTopic, c.rcThrottleTopic, c.steeringTopic,
|
|
|
|
c.throttleFeedbackTopic, c.speedZoneTopic)
|
2022-09-05 09:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) onThrottleFeedback(_ mqtt.Client, message mqtt.Message) {
|
|
|
|
var msg events.ThrottleMessage
|
|
|
|
err := proto.Unmarshal(message.Payload(), &msg)
|
|
|
|
if err != nil {
|
2022-09-05 13:30:26 +00:00
|
|
|
zap.S().Errorf("unable to unmarshal protobuf %T message: %v", &msg, err)
|
2022-09-05 09:22:31 +00:00
|
|
|
return
|
|
|
|
}
|
2022-09-05 13:30:26 +00:00
|
|
|
c.brakeCtrl.SetRealThrottle(types.Throttle(msg.GetThrottle()))
|
2019-12-27 16:42:10 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2022-09-05 13:30:26 +00:00
|
|
|
zap.S().Errorf("unable to unmarshal protobuf %T message: %v", &msg, err)
|
2020-01-01 19:33:41 +00:00
|
|
|
return
|
|
|
|
}
|
2019-12-27 16:42:10 +00:00
|
|
|
|
2022-09-05 14:44:02 +00:00
|
|
|
c.muDriveMode.Lock()
|
|
|
|
defer c.muDriveMode.Unlock()
|
|
|
|
c.driveMode = msg.GetDriveMode()
|
2019-12-27 16:42:10 +00:00
|
|
|
}
|
|
|
|
|
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()
|
2022-06-14 13:55:33 +00:00
|
|
|
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 13:30:26 +00:00
|
|
|
if types.Throttle(throttleMsg.GetThrottle()) > c.maxThrottle {
|
2022-09-05 14:44:02 +00:00
|
|
|
zap.S().Debugf("throttle upper that max value allowed, patch value from %v to %v", throttleMsg.GetThrottle(), c.maxThrottle)
|
2022-09-05 13:30:26 +00:00
|
|
|
throttleMsg.Throttle = float32(c.maxThrottle)
|
2022-06-15 17:22:30 +00:00
|
|
|
payloadPatched, err := proto.Marshal(&throttleMsg)
|
2022-06-14 13:55:33 +00:00
|
|
|
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-06-14 13:55:33 +00:00
|
|
|
}
|
2022-09-05 14:44:02 +00:00
|
|
|
publish(c.client, c.throttleTopic, payload)
|
2019-12-27 16:42:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 14:44:02 +00:00
|
|
|
func (c *Controller) onSteering(_ mqtt.Client, message mqtt.Message) {
|
|
|
|
var steeringMsg events.SteeringMessage
|
|
|
|
payload := message.Payload()
|
|
|
|
err := proto.Unmarshal(payload, &steeringMsg)
|
|
|
|
if err != nil {
|
|
|
|
zap.S().Errorf("unable to unmarshal steering message, skip value: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.muSteering.Lock()
|
|
|
|
defer c.muSteering.Unlock()
|
2023-06-14 18:07:12 +00:00
|
|
|
c.steering = types.Steering(steeringMsg.GetSteering())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) onSpeedZone(_ mqtt.Client, message mqtt.Message) {
|
|
|
|
var szMsg events.SpeedZoneMessage
|
|
|
|
payload := message.Payload()
|
|
|
|
err := proto.Unmarshal(payload, &szMsg)
|
|
|
|
if err != nil {
|
|
|
|
zap.S().Errorf("unable to unmarshal speedZone message, skip value: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.processor.SetSpeedZone(szMsg.GetSpeedZone())
|
2022-09-05 14:44:02 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 14:44:02 +00:00
|
|
|
var registerCallbacks = func(p *Controller) error {
|
2019-12-27 16:42:10 +00:00
|
|
|
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
|
|
|
|
}
|
2022-09-05 14:44:02 +00:00
|
|
|
|
|
|
|
err = service.RegisterCallback(p.client, p.steeringTopic, p.onSteering)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-05 09:22:31 +00:00
|
|
|
err = service.RegisterCallback(p.client, p.throttleFeedbackTopic, p.onThrottleFeedback)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-06-14 18:07:12 +00:00
|
|
|
err = service.RegisterCallback(p.client, p.speedZoneTopic, p.onSpeedZone)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-27 16:42:10 +00:00
|
|
|
return nil
|
2019-12-27 14:38:12 +00:00
|
|
|
}
|
2020-01-01 19:33:41 +00:00
|
|
|
|
2022-09-05 14:44:02 +00:00
|
|
|
var publish = func(client mqtt.Client, topic string, payload []byte) {
|
|
|
|
client.Publish(topic, 0, false, payload)
|
2020-01-01 19:33:41 +00:00
|
|
|
}
|