2019-12-01 21:35:19 +00:00
|
|
|
package arduino
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2020-01-01 16:12:18 +00:00
|
|
|
"github.com/cyrilix/robocar-protobuf/go/events"
|
|
|
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
2019-12-01 21:35:19 +00:00
|
|
|
"github.com/tarm/serial"
|
2021-10-12 15:55:58 +00:00
|
|
|
"go.uber.org/zap"
|
2022-01-03 09:24:06 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2019-12-01 21:35:19 +00:00
|
|
|
"io"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
MinPwmThrottle = 972.0
|
|
|
|
MaxPwmThrottle = 1954.0
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-08-18 16:22:24 +00:00
|
|
|
serialLineRegex = regexp.MustCompile(`(?P<timestamp>\d+),(?P<channel_1>\d+),(?P<channel_2>\d+),(?P<channel_3>\d+),(?P<channel_4>\d+),(?P<channel_5>\d+),(?P<channel_6>-?\d+),(?P<channel_7>\d+),(?P<channel_8>\d+),(?P<frequency>\d+)`)
|
2022-08-10 14:49:58 +00:00
|
|
|
DefaultPwmThrottle = PWMConfig{
|
|
|
|
Min: MinPwmThrottle,
|
|
|
|
Max: MaxPwmThrottle,
|
|
|
|
Middle: MinPwmThrottle + (MaxPwmThrottle-MinPwmThrottle)/2,
|
|
|
|
}
|
2019-12-01 21:35:19 +00:00
|
|
|
)
|
|
|
|
|
2019-12-18 22:21:52 +00:00
|
|
|
type Part struct {
|
2020-02-03 18:00:21 +00:00
|
|
|
client mqtt.Client
|
2020-01-27 18:21:26 +00:00
|
|
|
throttleTopic, steeringTopic, driveModeTopic, switchRecordTopic string
|
2020-02-03 18:00:21 +00:00
|
|
|
pubFrequency float64
|
|
|
|
serial io.Reader
|
|
|
|
mutex sync.Mutex
|
2022-08-10 14:49:58 +00:00
|
|
|
steering, secondarySteering float32
|
|
|
|
throttle, secondaryThrottle float32
|
2020-02-03 18:00:21 +00:00
|
|
|
ctrlRecord bool
|
|
|
|
driveMode events.DriveMode
|
|
|
|
cancel chan interface{}
|
2022-01-17 17:40:25 +00:00
|
|
|
|
2022-08-10 14:49:58 +00:00
|
|
|
useSecondaryRc bool
|
|
|
|
pwmSteeringConfig *PWMConfig
|
|
|
|
pwmSecondarySteeringConfig *PWMConfig
|
|
|
|
pwmThrottleConfig *PWMConfig
|
|
|
|
pwmSecondaryThrottleConfig *PWMConfig
|
2022-06-13 13:21:58 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 14:49:58 +00:00
|
|
|
type PWMConfig struct {
|
|
|
|
Min int
|
|
|
|
Max int
|
|
|
|
Middle int
|
2022-01-17 17:40:25 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 14:49:58 +00:00
|
|
|
func NewPWMConfig(min, max int) *PWMConfig {
|
|
|
|
return &PWMConfig{
|
|
|
|
Min: min,
|
|
|
|
Max: max,
|
|
|
|
Middle: min + (max-min)/2,
|
|
|
|
}
|
2019-12-01 21:35:19 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 14:49:58 +00:00
|
|
|
func NewAsymetricPWMConfig(min, max, middle int) *PWMConfig {
|
|
|
|
c := NewPWMConfig(min, max)
|
|
|
|
c.Middle = middle
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(p *Part)
|
|
|
|
|
|
|
|
func WithThrottleConfig(throttleConfig *PWMConfig) Option {
|
|
|
|
return func(p *Part) {
|
|
|
|
p.pwmThrottleConfig = throttleConfig
|
2022-01-17 17:40:25 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-13 13:21:58 +00:00
|
|
|
|
2022-08-10 14:49:58 +00:00
|
|
|
func WithSteeringConfig(steeringConfig *PWMConfig) Option {
|
|
|
|
return func(p *Part) {
|
|
|
|
p.pwmSteeringConfig = steeringConfig
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithSecondaryRC(throttleConfig, steeringConfig *PWMConfig) Option {
|
|
|
|
return func(p *Part) {
|
|
|
|
p.pwmSecondaryThrottleConfig = throttleConfig
|
|
|
|
p.pwmSecondarySteeringConfig = steeringConfig
|
|
|
|
}
|
2022-01-17 17:40:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewPart(client mqtt.Client, name string, baud int, throttleTopic, steeringTopic, driveModeTopic,
|
2022-08-10 14:49:58 +00:00
|
|
|
switchRecordTopic string, pubFrequency float64, options ...Option) *Part {
|
2019-12-01 21:35:19 +00:00
|
|
|
c := &serial.Config{Name: name, Baud: baud}
|
|
|
|
s, err := serial.OpenPort(c)
|
|
|
|
if err != nil {
|
2021-10-12 15:55:58 +00:00
|
|
|
zap.S().Panicw("unable to open serial port: %v", err)
|
2019-12-01 21:35:19 +00:00
|
|
|
}
|
2022-08-10 14:49:58 +00:00
|
|
|
p := &Part{
|
2020-02-03 18:00:21 +00:00
|
|
|
client: client,
|
|
|
|
serial: s,
|
|
|
|
throttleTopic: throttleTopic,
|
|
|
|
steeringTopic: steeringTopic,
|
|
|
|
driveModeTopic: driveModeTopic,
|
|
|
|
switchRecordTopic: switchRecordTopic,
|
|
|
|
pubFrequency: pubFrequency,
|
|
|
|
driveMode: events.DriveMode_INVALID,
|
|
|
|
cancel: make(chan interface{}),
|
2022-01-17 17:40:25 +00:00
|
|
|
|
2022-08-10 14:49:58 +00:00
|
|
|
pwmSteeringConfig: &DefaultPwmThrottle,
|
|
|
|
pwmSecondarySteeringConfig: &DefaultPwmThrottle,
|
|
|
|
pwmThrottleConfig: &DefaultPwmThrottle,
|
|
|
|
pwmSecondaryThrottleConfig: &DefaultPwmThrottle,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range options {
|
|
|
|
o(p)
|
2020-01-01 16:12:18 +00:00
|
|
|
}
|
2022-08-10 14:49:58 +00:00
|
|
|
return p
|
2019-12-01 21:35:19 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 22:21:52 +00:00
|
|
|
func (a *Part) Start() error {
|
2021-10-12 16:02:24 +00:00
|
|
|
zap.S().Info("start arduino part")
|
2019-12-01 21:35:19 +00:00
|
|
|
go a.publishLoop()
|
|
|
|
for {
|
|
|
|
buff := bufio.NewReader(a.serial)
|
|
|
|
line, err := buff.ReadString('\n')
|
|
|
|
if err == io.EOF || line == "" {
|
2021-10-12 16:02:24 +00:00
|
|
|
zap.S().Error("remote connection closed")
|
2019-12-01 21:35:19 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-03-29 17:23:28 +00:00
|
|
|
zap.L().Debug("raw line: %s", zap.String("raw", line))
|
2019-12-18 22:21:52 +00:00
|
|
|
if !serialLineRegex.MatchString(line) {
|
2021-10-12 16:02:24 +00:00
|
|
|
zap.S().Errorf("invalid line: '%v'", line)
|
2019-12-01 21:35:19 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
values := strings.Split(strings.TrimSuffix(strings.TrimSuffix(line, "\n"), "\r"), ",")
|
|
|
|
|
|
|
|
a.updateValues(values)
|
|
|
|
}
|
2019-12-18 22:21:52 +00:00
|
|
|
return nil
|
2019-12-01 21:35:19 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 22:21:52 +00:00
|
|
|
func (a *Part) updateValues(values []string) {
|
2019-12-01 21:35:19 +00:00
|
|
|
a.mutex.Lock()
|
|
|
|
defer a.mutex.Unlock()
|
2019-12-18 22:21:52 +00:00
|
|
|
a.processChannel1(values[1])
|
|
|
|
a.processChannel2(values[2])
|
|
|
|
a.processChannel3(values[3])
|
|
|
|
a.processChannel4(values[4])
|
|
|
|
a.processChannel5(values[5])
|
|
|
|
a.processChannel6(values[6])
|
2022-08-10 14:49:58 +00:00
|
|
|
a.processChannel7(values[7])
|
|
|
|
a.processChannel8(values[8])
|
2019-12-01 21:35:19 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 22:21:52 +00:00
|
|
|
func (a *Part) Stop() {
|
2021-10-12 16:02:24 +00:00
|
|
|
zap.S().Info("stop ArduinoPart")
|
2020-01-01 16:12:18 +00:00
|
|
|
close(a.cancel)
|
2019-12-01 21:35:19 +00:00
|
|
|
switch s := a.serial.(type) {
|
|
|
|
case io.ReadCloser:
|
|
|
|
if err := s.Close(); err != nil {
|
2021-10-12 16:02:24 +00:00
|
|
|
zap.S().Fatalf("unable to close serial port: %v", err)
|
2019-12-01 21:35:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-18 22:21:52 +00:00
|
|
|
func (a *Part) processChannel1(v string) {
|
2022-05-30 16:58:52 +00:00
|
|
|
zap.L().Debug("process new value for steering on channel1", zap.String("value", v))
|
2019-12-18 22:21:52 +00:00
|
|
|
value, err := strconv.Atoi(v)
|
2019-12-01 21:35:19 +00:00
|
|
|
if err != nil {
|
2022-05-30 16:58:52 +00:00
|
|
|
zap.S().Errorf("invalid steering value for channel1, should be an int: %v", err)
|
2019-12-01 21:35:19 +00:00
|
|
|
}
|
2022-08-10 14:49:58 +00:00
|
|
|
a.steering = convertPwmSteeringToPercent(value, a.pwmSteeringConfig)
|
2022-01-17 17:40:25 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 14:49:58 +00:00
|
|
|
func convertPwmSteeringToPercent(value int, c *PWMConfig) float32 {
|
|
|
|
if value < c.Min {
|
|
|
|
value = c.Min
|
|
|
|
} else if value > c.Max {
|
|
|
|
value = c.Max
|
2022-01-17 17:40:25 +00:00
|
|
|
}
|
2022-08-10 14:49:58 +00:00
|
|
|
if value == c.Middle {
|
2022-01-17 17:40:25 +00:00
|
|
|
return 0.
|
|
|
|
}
|
2022-08-10 14:49:58 +00:00
|
|
|
if value < c.Middle {
|
|
|
|
return (float32(value) - float32(c.Middle)) / float32(c.Middle-c.Min)
|
2019-12-01 21:35:19 +00:00
|
|
|
}
|
2022-01-17 17:40:25 +00:00
|
|
|
// middle < value < max
|
2022-08-10 14:49:58 +00:00
|
|
|
return (float32(value) - float32(c.Middle)) / float32(c.Max-c.Middle)
|
2019-12-01 21:35:19 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 22:21:52 +00:00
|
|
|
func (a *Part) processChannel2(v string) {
|
2022-05-30 16:58:52 +00:00
|
|
|
zap.L().Debug("process new throttle value on channel2", zap.String("value", v))
|
2019-12-18 22:21:52 +00:00
|
|
|
value, err := strconv.Atoi(v)
|
2019-12-01 21:35:19 +00:00
|
|
|
if err != nil {
|
2022-05-30 16:58:52 +00:00
|
|
|
zap.S().Errorf("invalid throttle value for channel2, should be an int: %v", err)
|
2019-12-01 21:35:19 +00:00
|
|
|
}
|
2022-06-13 13:21:58 +00:00
|
|
|
if value < a.pwmThrottleConfig.Min {
|
|
|
|
value = a.pwmThrottleConfig.Min
|
|
|
|
} else if value > a.pwmThrottleConfig.Max {
|
|
|
|
value = a.pwmThrottleConfig.Max
|
2019-12-01 21:35:19 +00:00
|
|
|
}
|
2022-06-13 16:35:27 +00:00
|
|
|
|
|
|
|
throttle := 0.
|
2022-08-10 14:49:58 +00:00
|
|
|
if value > a.pwmThrottleConfig.Middle {
|
|
|
|
throttle = (float64(value) - float64(a.pwmThrottleConfig.Middle)) / float64(a.pwmThrottleConfig.Max-a.pwmThrottleConfig.Middle)
|
2022-06-13 16:35:27 +00:00
|
|
|
}
|
2022-08-10 14:49:58 +00:00
|
|
|
if value < a.pwmThrottleConfig.Middle {
|
|
|
|
throttle = -1. * (float64(a.pwmThrottleConfig.Middle) - float64(value)) / (float64(a.pwmThrottleConfig.Middle - a.pwmThrottleConfig.Min))
|
2022-06-13 16:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
a.throttle = float32(throttle)
|
2019-12-01 21:35:19 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 22:21:52 +00:00
|
|
|
func (a *Part) processChannel3(v string) {
|
2021-10-12 15:55:58 +00:00
|
|
|
zap.L().Debug("process new value for channel3", zap.String("value", v))
|
2022-08-10 14:49:58 +00:00
|
|
|
value, err := strconv.Atoi(v)
|
|
|
|
if err != nil {
|
|
|
|
zap.S().Errorf("invalid throttle value for channel2, should be an int: %v", err)
|
|
|
|
}
|
|
|
|
a.useSecondaryRc = value > 1900
|
2019-12-01 21:35:19 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 22:21:52 +00:00
|
|
|
func (a *Part) processChannel4(v string) {
|
2021-10-12 15:55:58 +00:00
|
|
|
zap.L().Debug("process new value for channel4", zap.String("value", v))
|
2019-12-01 21:35:19 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 22:21:52 +00:00
|
|
|
func (a *Part) processChannel5(v string) {
|
2021-10-12 15:55:58 +00:00
|
|
|
zap.L().Debug("process new value for channel5", zap.String("value", v))
|
2019-12-18 22:21:52 +00:00
|
|
|
|
|
|
|
value, err := strconv.Atoi(v)
|
2019-12-01 21:35:19 +00:00
|
|
|
if err != nil {
|
2022-05-30 16:58:52 +00:00
|
|
|
zap.S().Errorf("invalid value for channel5 'record', should be an int: %v", err)
|
2019-12-01 21:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if value < 1800 {
|
2019-12-18 22:21:52 +00:00
|
|
|
if !a.ctrlRecord {
|
2021-10-12 15:55:58 +00:00
|
|
|
zap.S().Infof("Update channel 5 with value %v, record: %v", true, false)
|
2019-12-01 21:35:19 +00:00
|
|
|
a.ctrlRecord = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if a.ctrlRecord {
|
2021-10-12 15:55:58 +00:00
|
|
|
zap.S().Infof("Update channel 5 with value %v, record: %v", false, true)
|
2019-12-01 21:35:19 +00:00
|
|
|
a.ctrlRecord = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-18 22:21:52 +00:00
|
|
|
func (a *Part) processChannel6(v string) {
|
2021-10-12 15:55:58 +00:00
|
|
|
zap.L().Debug("process new value for channel6", zap.String("value", v))
|
2019-12-18 22:21:52 +00:00
|
|
|
value, err := strconv.Atoi(v)
|
2019-12-01 21:35:19 +00:00
|
|
|
if err != nil {
|
2022-05-30 16:58:52 +00:00
|
|
|
zap.S().Errorf("invalid value for channel6 'drive-mode', should be an int: %v", err)
|
2019-12-01 21:35:19 +00:00
|
|
|
return
|
|
|
|
}
|
2022-08-18 16:22:24 +00:00
|
|
|
if value < 0 {
|
|
|
|
// No value, ignore it
|
|
|
|
return
|
|
|
|
}
|
2019-12-01 21:35:19 +00:00
|
|
|
if value > 1800 {
|
2020-01-01 16:12:18 +00:00
|
|
|
if a.driveMode != events.DriveMode_PILOT {
|
2022-05-30 16:58:52 +00:00
|
|
|
zap.S().Infof("Update channel 6 'drive-mode' with value %v, new user_mode: %v", value, events.DriveMode_PILOT)
|
2020-01-01 16:12:18 +00:00
|
|
|
a.driveMode = events.DriveMode_PILOT
|
2019-12-01 21:35:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-01-01 16:12:18 +00:00
|
|
|
if a.driveMode != events.DriveMode_USER {
|
2022-05-30 16:58:52 +00:00
|
|
|
zap.S().Infof("Update channel 6 'drive-mode' with value %v, new user_mode: %v", value, events.DriveMode_USER)
|
2019-12-01 21:35:19 +00:00
|
|
|
}
|
2020-01-01 16:12:18 +00:00
|
|
|
a.driveMode = events.DriveMode_USER
|
2019-12-01 21:35:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-10 14:49:58 +00:00
|
|
|
func (a *Part) processChannel7(v string) {
|
|
|
|
zap.L().Debug("process new value for secondary steering on channel7", zap.String("value", v))
|
|
|
|
value, err := strconv.Atoi(v)
|
|
|
|
if err != nil {
|
|
|
|
zap.S().Errorf("invalid steering value for channel7, should be an int: %v", err)
|
|
|
|
}
|
|
|
|
a.secondarySteering = convertPwmSteeringToPercent(value, a.pwmSecondarySteeringConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Part) processChannel8(v string) {
|
|
|
|
zap.L().Debug("process new throttle value on channel8", zap.String("value", v))
|
|
|
|
value, err := strconv.Atoi(v)
|
|
|
|
if err != nil {
|
|
|
|
zap.S().Errorf("invalid throttle value for channel8, should be an int: %v", err)
|
|
|
|
}
|
|
|
|
if value < a.pwmSecondaryThrottleConfig.Min {
|
|
|
|
value = a.pwmSecondaryThrottleConfig.Min
|
|
|
|
} else if value > a.pwmSecondaryThrottleConfig.Max {
|
|
|
|
value = a.pwmSecondaryThrottleConfig.Max
|
|
|
|
}
|
|
|
|
a.secondaryThrottle = ((float32(value)-float32(a.pwmSecondaryThrottleConfig.Min))/float32(a.pwmSecondaryThrottleConfig.Max-a.pwmSecondaryThrottleConfig.Min))*2.0 - 1.0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Part) Throttle() float32 {
|
|
|
|
a.mutex.Lock()
|
|
|
|
defer a.mutex.Unlock()
|
|
|
|
if a.useSecondaryRc {
|
|
|
|
return a.secondaryThrottle
|
|
|
|
}
|
|
|
|
return a.throttle
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Part) Steering() float32 {
|
|
|
|
a.mutex.Lock()
|
|
|
|
defer a.mutex.Unlock()
|
|
|
|
if a.useSecondaryRc {
|
|
|
|
return a.secondarySteering
|
|
|
|
}
|
|
|
|
return a.steering
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Part) DriveMode() events.DriveMode {
|
|
|
|
a.mutex.Lock()
|
|
|
|
defer a.mutex.Unlock()
|
|
|
|
return a.driveMode
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Part) SwitchRecord() bool {
|
|
|
|
a.mutex.Lock()
|
|
|
|
defer a.mutex.Unlock()
|
|
|
|
return a.ctrlRecord
|
|
|
|
}
|
|
|
|
|
2019-12-18 22:21:52 +00:00
|
|
|
func (a *Part) publishLoop() {
|
2020-01-01 16:12:18 +00:00
|
|
|
ticker := time.NewTicker(time.Second / time.Duration(int(a.pubFrequency)))
|
|
|
|
|
2019-12-01 21:35:19 +00:00
|
|
|
for {
|
2020-01-01 16:12:18 +00:00
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2020-01-27 18:21:26 +00:00
|
|
|
a.publishValues()
|
2020-01-01 16:12:18 +00:00
|
|
|
case <-a.cancel:
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
}
|
2019-12-01 21:35:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-27 18:21:26 +00:00
|
|
|
func (a *Part) publishValues() {
|
|
|
|
a.publishThrottle()
|
|
|
|
a.publishSteering()
|
|
|
|
a.publishDriveMode()
|
|
|
|
a.publishSwitchRecord()
|
2020-01-01 16:12:18 +00:00
|
|
|
}
|
|
|
|
|
2020-01-27 18:21:26 +00:00
|
|
|
func (a *Part) publishThrottle() {
|
2020-01-01 16:12:18 +00:00
|
|
|
throttle := events.ThrottleMessage{
|
2022-08-10 14:49:58 +00:00
|
|
|
Throttle: a.Throttle(),
|
2020-01-01 16:12:18 +00:00
|
|
|
Confidence: 1.0,
|
|
|
|
}
|
|
|
|
throttleMessage, err := proto.Marshal(&throttle)
|
|
|
|
if err != nil {
|
2021-10-12 15:55:58 +00:00
|
|
|
zap.S().Errorf("unable to marshal protobuf throttle message: %v", err)
|
2020-01-01 16:12:18 +00:00
|
|
|
return
|
|
|
|
}
|
2021-10-12 15:55:58 +00:00
|
|
|
zap.L().Debug("throttle channel", zap.Float32("throttle", a.throttle))
|
2022-06-13 13:21:58 +00:00
|
|
|
publish(a.client, a.throttleTopic, throttleMessage)
|
2020-01-01 16:12:18 +00:00
|
|
|
}
|
|
|
|
|
2020-01-27 18:21:26 +00:00
|
|
|
func (a *Part) publishSteering() {
|
2020-01-01 16:12:18 +00:00
|
|
|
steering := events.SteeringMessage{
|
2022-08-10 14:49:58 +00:00
|
|
|
Steering: a.Steering(),
|
2020-01-01 16:12:18 +00:00
|
|
|
Confidence: 1.0,
|
|
|
|
}
|
|
|
|
steeringMessage, err := proto.Marshal(&steering)
|
|
|
|
if err != nil {
|
2021-10-12 15:55:58 +00:00
|
|
|
zap.S().Errorf("unable to marshal protobuf steering message: %v", err)
|
2020-01-01 16:12:18 +00:00
|
|
|
return
|
|
|
|
}
|
2021-10-12 15:55:58 +00:00
|
|
|
zap.L().Debug("steering channel", zap.Float32("steering", a.steering))
|
2022-06-13 13:21:58 +00:00
|
|
|
publish(a.client, a.steeringTopic, steeringMessage)
|
2020-01-01 16:12:18 +00:00
|
|
|
}
|
|
|
|
|
2020-01-27 18:21:26 +00:00
|
|
|
func (a *Part) publishDriveMode() {
|
2020-01-01 16:12:18 +00:00
|
|
|
dm := events.DriveModeMessage{
|
2022-08-10 14:49:58 +00:00
|
|
|
DriveMode: a.DriveMode(),
|
2020-01-01 16:12:18 +00:00
|
|
|
}
|
|
|
|
driveModeMessage, err := proto.Marshal(&dm)
|
|
|
|
if err != nil {
|
2021-10-12 15:55:58 +00:00
|
|
|
zap.S().Errorf("unable to marshal protobuf driveMode message: %v", err)
|
2020-01-01 16:12:18 +00:00
|
|
|
return
|
|
|
|
}
|
2022-06-13 13:21:58 +00:00
|
|
|
publish(a.client, a.driveModeTopic, driveModeMessage)
|
2020-01-01 16:12:18 +00:00
|
|
|
}
|
|
|
|
|
2020-01-27 18:21:26 +00:00
|
|
|
func (a *Part) publishSwitchRecord() {
|
2020-01-01 16:12:18 +00:00
|
|
|
sr := events.SwitchRecordMessage{
|
2022-08-10 14:49:58 +00:00
|
|
|
Enabled: !a.SwitchRecord(),
|
2020-01-01 16:12:18 +00:00
|
|
|
}
|
|
|
|
switchRecordMessage, err := proto.Marshal(&sr)
|
|
|
|
if err != nil {
|
2021-10-12 15:55:58 +00:00
|
|
|
zap.S().Errorf("unable to marshal protobuf SwitchRecord message: %v", err)
|
2020-01-01 16:12:18 +00:00
|
|
|
return
|
|
|
|
}
|
2022-06-13 13:21:58 +00:00
|
|
|
publish(a.client, a.switchRecordTopic, switchRecordMessage)
|
2020-01-01 16:12:18 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 13:21:58 +00:00
|
|
|
var publish = func(client mqtt.Client, topic string, payload []byte) {
|
|
|
|
client.Publish(topic, 0, false, payload)
|
2019-12-01 21:35:19 +00:00
|
|
|
}
|