2019-12-27 14:38:12 +00:00
|
|
|
package part
|
|
|
|
|
|
|
|
import (
|
2020-01-01 18:36:22 +00:00
|
|
|
"encoding/json"
|
2019-12-27 14:38:12 +00:00
|
|
|
"github.com/cyrilix/robocar-base/mqttdevice"
|
2019-12-27 16:42:10 +00:00
|
|
|
"github.com/cyrilix/robocar-base/service"
|
|
|
|
"github.com/cyrilix/robocar-base/types"
|
2019-12-27 14:38:12 +00:00
|
|
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
2020-01-01 18:36:22 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-12-27 16:42:10 +00:00
|
|
|
"sync"
|
2019-12-27 14:38:12 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-12-27 16:42:10 +00:00
|
|
|
func NewPart(client mqtt.Client, pub mqttdevice.Publisher, throttleTopic, driveModeTopic, rcThrottleTopic string,
|
|
|
|
minValue, maxValue float64, publishPilotFrequency int) *ThrottlePart {
|
2019-12-27 14:38:12 +00:00
|
|
|
return &ThrottlePart{
|
2019-12-27 16:42:10 +00:00
|
|
|
client: client,
|
|
|
|
pub: pub,
|
|
|
|
throttleTopic: throttleTopic,
|
|
|
|
driveModeTopic: driveModeTopic,
|
|
|
|
rcThrottleTopic: rcThrottleTopic,
|
|
|
|
minThrottle: minValue,
|
|
|
|
maxThrottle: maxValue,
|
|
|
|
driveMode: types.DriveModeUser,
|
|
|
|
publishPilotFrequency: publishPilotFrequency,
|
2019-12-27 14:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type ThrottlePart struct {
|
|
|
|
client mqtt.Client
|
|
|
|
pub mqttdevice.Publisher
|
|
|
|
throttleTopic string
|
|
|
|
minThrottle, maxThrottle float64
|
2019-12-27 16:42:10 +00:00
|
|
|
|
|
|
|
muDriveMode sync.RWMutex
|
|
|
|
driveMode types.DriveMode
|
|
|
|
|
|
|
|
cancel chan interface{}
|
|
|
|
publishPilotFrequency int
|
|
|
|
driveModeTopic, rcThrottleTopic string
|
2019-12-27 14:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ThrottlePart) Start() error {
|
2019-12-27 16:42:10 +00:00
|
|
|
if err := registerCallbacks(p); err != nil {
|
|
|
|
log.Printf("unable to rgeister callbacks: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-27 14:38:12 +00:00
|
|
|
p.cancel = make(chan interface{})
|
2019-12-27 16:42:10 +00:00
|
|
|
ticker := time.NewTicker(1 * time.Second / time.Duration(p.publishPilotFrequency))
|
2019-12-27 14:38:12 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2019-12-27 16:42:10 +00:00
|
|
|
p.publishPilotValue()
|
2019-12-27 14:38:12 +00:00
|
|
|
case <-p.cancel:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 16:42:10 +00:00
|
|
|
func (p *ThrottlePart) publishPilotValue() {
|
|
|
|
p.muDriveMode.RLock()
|
|
|
|
defer p.muDriveMode.RUnlock()
|
|
|
|
|
|
|
|
if p.driveMode != types.DriveModePilot {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.pub.Publish(p.throttleTopic, mqttdevice.NewMqttValue(types.Throttle{
|
|
|
|
Value: p.minThrottle,
|
|
|
|
Confidence: 1.0,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2019-12-27 14:38:12 +00:00
|
|
|
func (p *ThrottlePart) Stop() {
|
|
|
|
close(p.cancel)
|
2019-12-27 16:42:10 +00:00
|
|
|
service.StopService("throttle", p.client, p.driveModeTopic, p.rcThrottleTopic)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ThrottlePart) onDriveMode(_ mqtt.Client, message mqtt.Message) {
|
2020-01-01 18:36:22 +00:00
|
|
|
m := types.ParseString(string(message.Payload()))
|
2019-12-27 16:42:10 +00:00
|
|
|
|
|
|
|
p.muDriveMode.Lock()
|
|
|
|
defer p.muDriveMode.Unlock()
|
|
|
|
p.driveMode = m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ThrottlePart) onRCThrottle(_ mqtt.Client, message mqtt.Message) {
|
|
|
|
payload := message.Payload()
|
2020-01-01 18:36:22 +00:00
|
|
|
var throttle types.Throttle
|
|
|
|
err := json.Unmarshal(payload, &throttle)
|
2019-12-27 16:42:10 +00:00
|
|
|
if err != nil {
|
2020-01-01 18:36:22 +00:00
|
|
|
log.Errorf("unable to parse throttle json: %v", err)
|
2019-12-27 16:42:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.muDriveMode.RLock()
|
|
|
|
defer p.muDriveMode.RUnlock()
|
|
|
|
if p.driveMode == types.DriveModeUser {
|
2020-01-01 18:36:22 +00:00
|
|
|
p.pub.Publish(p.throttleTopic, mqttdevice.NewMqttValue(throttle))
|
2019-12-27 16:42:10 +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
|
2019-12-27 14:38:12 +00:00
|
|
|
}
|