refactor: mv arduino package to pkg dir

This commit is contained in:
2021-10-12 18:02:24 +02:00
parent 164445b16c
commit 18b1bd13db
3 changed files with 6 additions and 7 deletions

261
pkg/arduino/arduino.go Normal file
View File

@ -0,0 +1,261 @@
package arduino
import (
"bufio"
"github.com/cyrilix/robocar-protobuf/go/events"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/golang/protobuf/proto"
"github.com/tarm/serial"
"go.uber.org/zap"
"io"
"regexp"
"strconv"
"strings"
"sync"
"time"
)
const (
MinPwmAngle = 960.0
MaxPwmAngle = 1980.0
MinPwmThrottle = 972.0
MaxPwmThrottle = 1954.0
)
var (
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<frequency>\d+),(?P<distance_cm>\d+)?`)
)
type Part struct {
client mqtt.Client
throttleTopic, steeringTopic, driveModeTopic, switchRecordTopic string
pubFrequency float64
serial io.Reader
mutex sync.Mutex
steering float32
throttle float32
ctrlRecord bool
driveMode events.DriveMode
cancel chan interface{}
}
func NewPart(client mqtt.Client, name string, baud int, throttleTopic, steeringTopic, driveModeTopic, switchRecordTopic string, pubFrequency float64) *Part {
c := &serial.Config{Name: name, Baud: baud}
s, err := serial.OpenPort(c)
if err != nil {
zap.S().Panicw("unable to open serial port: %v", err)
}
return &Part{
client: client,
serial: s,
throttleTopic: throttleTopic,
steeringTopic: steeringTopic,
driveModeTopic: driveModeTopic,
switchRecordTopic: switchRecordTopic,
pubFrequency: pubFrequency,
driveMode: events.DriveMode_INVALID,
cancel: make(chan interface{}),
}
}
func (a *Part) Start() error {
zap.S().Info("start arduino part")
go a.publishLoop()
for {
buff := bufio.NewReader(a.serial)
line, err := buff.ReadString('\n')
if err == io.EOF || line == "" {
zap.S().Error("remote connection closed")
break
}
if !serialLineRegex.MatchString(line) {
zap.S().Errorf("invalid line: '%v'", line)
continue
}
values := strings.Split(strings.TrimSuffix(strings.TrimSuffix(line, "\n"), "\r"), ",")
a.updateValues(values)
}
return nil
}
func (a *Part) updateValues(values []string) {
a.mutex.Lock()
defer a.mutex.Unlock()
a.processChannel1(values[1])
a.processChannel2(values[2])
a.processChannel3(values[3])
a.processChannel4(values[4])
a.processChannel5(values[5])
a.processChannel6(values[6])
}
func (a *Part) Stop() {
zap.S().Info("stop ArduinoPart")
close(a.cancel)
switch s := a.serial.(type) {
case io.ReadCloser:
if err := s.Close(); err != nil {
zap.S().Fatalf("unable to close serial port: %v", err)
}
}
}
func (a *Part) processChannel1(v string) {
zap.L().Debug("process new value for channel1", zap.String("value", v))
value, err := strconv.Atoi(v)
if err != nil {
zap.S().Errorf("invalid value for channel1, should be an int: %v", err)
}
if value < MinPwmAngle {
value = MinPwmAngle
} else if value > MaxPwmAngle {
value = MaxPwmAngle
}
a.steering = ((float32(value)-MinPwmAngle)/(MaxPwmAngle-MinPwmAngle))*2.0 - 1.0
}
func (a *Part) processChannel2(v string) {
zap.L().Debug("process new value for channel2", zap.String("value", v))
value, err := strconv.Atoi(v)
if err != nil {
zap.S().Errorf("invalid value for channel2, should be an int: %v", err)
}
if value < MinPwmThrottle {
value = MinPwmThrottle
} else if value > MaxPwmThrottle {
value = MaxPwmThrottle
}
a.throttle = ((float32(value)-MinPwmThrottle)/(MaxPwmThrottle-MinPwmThrottle))*2.0 - 1.0
}
func (a *Part) processChannel3(v string) {
zap.L().Debug("process new value for channel3", zap.String("value", v))
}
func (a *Part) processChannel4(v string) {
zap.L().Debug("process new value for channel4", zap.String("value", v))
}
func (a *Part) processChannel5(v string) {
zap.L().Debug("process new value for channel5", zap.String("value", v))
value, err := strconv.Atoi(v)
if err != nil {
zap.S().Errorf("invalid value for channel5, should be an int: %v", err)
}
if value < 1800 {
if !a.ctrlRecord {
zap.S().Infof("Update channel 5 with value %v, record: %v", true, false)
a.ctrlRecord = true
}
} else {
if a.ctrlRecord {
zap.S().Infof("Update channel 5 with value %v, record: %v", false, true)
a.ctrlRecord = false
}
}
}
func (a *Part) processChannel6(v string) {
zap.L().Debug("process new value for channel6", zap.String("value", v))
value, err := strconv.Atoi(v)
if err != nil {
zap.S().Errorf("invalid value for channel6, should be an int: %v", err)
return
}
if value > 1800 {
if a.driveMode != events.DriveMode_PILOT {
zap.S().Infof("Update channel 6 with value %v, new user_mode: %v", value, events.DriveMode_PILOT)
a.driveMode = events.DriveMode_PILOT
}
} else {
if a.driveMode != events.DriveMode_USER {
zap.S().Infof("Update channel 6 with value %v, new user_mode: %v", value, events.DriveMode_USER)
}
a.driveMode = events.DriveMode_USER
}
}
func (a *Part) publishLoop() {
ticker := time.NewTicker(time.Second / time.Duration(int(a.pubFrequency)))
for {
select {
case <-ticker.C:
a.publishValues()
case <-a.cancel:
ticker.Stop()
return
}
}
}
func (a *Part) publishValues() {
a.mutex.Lock()
defer a.mutex.Unlock()
a.publishThrottle()
a.publishSteering()
a.publishDriveMode()
a.publishSwitchRecord()
}
func (a *Part) publishThrottle() {
throttle := events.ThrottleMessage{
Throttle: a.throttle,
Confidence: 1.0,
}
throttleMessage, err := proto.Marshal(&throttle)
if err != nil {
zap.S().Errorf("unable to marshal protobuf throttle message: %v", err)
return
}
zap.L().Debug("throttle channel", zap.Float32("throttle", a.throttle))
publish(a.client, a.throttleTopic, &throttleMessage)
}
func (a *Part) publishSteering() {
steering := events.SteeringMessage{
Steering: a.steering,
Confidence: 1.0,
}
steeringMessage, err := proto.Marshal(&steering)
if err != nil {
zap.S().Errorf("unable to marshal protobuf steering message: %v", err)
return
}
zap.L().Debug("steering channel", zap.Float32("steering", a.steering))
publish(a.client, a.steeringTopic, &steeringMessage)
}
func (a *Part) publishDriveMode() {
dm := events.DriveModeMessage{
DriveMode: a.driveMode,
}
driveModeMessage, err := proto.Marshal(&dm)
if err != nil {
zap.S().Errorf("unable to marshal protobuf driveMode message: %v", err)
return
}
publish(a.client, a.driveModeTopic, &driveModeMessage)
}
func (a *Part) publishSwitchRecord() {
sr := events.SwitchRecordMessage{
Enabled: !a.ctrlRecord,
}
switchRecordMessage, err := proto.Marshal(&sr)
if err != nil {
zap.S().Errorf("unable to marshal protobuf SwitchRecord message: %v", err)
return
}
publish(a.client, a.switchRecordTopic, &switchRecordMessage)
}
var publish = func(client mqtt.Client, topic string, payload *[]byte) {
client.Publish(topic, 0, false, *payload)
}

287
pkg/arduino/arduino_test.go Normal file
View File

@ -0,0 +1,287 @@
package arduino
import (
"bufio"
"fmt"
"github.com/cyrilix/robocar-protobuf/go/events"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/golang/protobuf/proto"
"net"
"sync"
"testing"
"time"
)
func TestArduinoPart_Update(t *testing.T) {
oldPublish := publish
defer func() { publish = oldPublish }()
publish = func(client mqtt.Client, topic string, payload *[]byte) {}
ln, err := net.Listen("tcp", ":8080")
if err != nil {
t.Fatalf("unable to init connection for test")
}
defer func() {
if err := ln.Close(); err != nil {
t.Errorf("unable to close resource: %v", err)
}
}()
serialClient, err := net.Dial("tcp", "localhost:8080")
if err != nil {
t.Fatalf("unable to init connection for test")
}
defer func() {
if err := serialClient.Close(); err != nil {
t.Errorf("unable to close resource: %v", err)
}
}()
conn, err := ln.Accept()
if err != nil {
t.Fatalf("unable to init connection for test")
}
defer func() {
if err := conn.Close(); err != nil {
t.Errorf("unable to close resource: %v", err)
}
}()
a := Part{client: nil, serial: conn, pubFrequency: 100}
go func() {
err := a.Start()
if err != nil {
t.Errorf("unsable to start part: %v", err)
t.Fail()
}
}()
channel1, channel2, channel3, channel4, channel5, channel6, distanceCm := 678, 910, 1112, 1678, 1910, 112, 128
cases := []struct {
name, content string
expectedThrottle, expectedSteering float32
expectedDriveMode events.DriveMode
expectedSwitchRecord bool
}{
{"Good value",
fmt.Sprintf("12345,%d,%d,%d,%d,%d,%d,50,%d\n", channel1, channel2, channel3, channel4, channel5, channel6, distanceCm),
-1., -1., events.DriveMode_USER, false},
{"Unparsable line",
"12350,invalid line\n",
-1., -1., events.DriveMode_USER, false},
{"Switch record on",
fmt.Sprintf("12355,%d,%d,%d,%d,%d,%d,50,%d\n", channel1, channel2, channel3, channel4, 998, channel6, distanceCm),
-1., -1., events.DriveMode_USER, true},
{"Switch record off",
fmt.Sprintf("12360,%d,%d,%d,%d,%d,%d,50,%d\n", channel1, channel2, channel3, channel4, 1987, channel6, distanceCm),
-1., -1., events.DriveMode_USER, false},
{"Switch record off",
fmt.Sprintf("12365,%d,%d,%d,%d,%d,%d,50,%d\n", channel1, channel2, channel3, channel4, 1850, channel6, distanceCm),
-1., -1., events.DriveMode_USER, false},
{"Switch record on",
fmt.Sprintf("12370,%d,%d,%d,%d,%d,%d,50,%d\n", channel1, channel2, channel3, channel4, 1003, channel6, distanceCm),
-1., -1., events.DriveMode_USER, true},
{"DriveMode: user",
fmt.Sprintf("12375,%d,%d,%d,%d,%d,%d,50,%d\n", channel1, channel2, channel3, channel4, channel5, 998, distanceCm),
-1., -1., events.DriveMode_USER, false},
{"DriveMode: pilot",
fmt.Sprintf("12380,%d,%d,%d,%d,%d,%d,50,%d\n", channel1, channel2, channel3, channel4, channel5, 1987, distanceCm),
-1., -1., events.DriveMode_PILOT, false},
{"DriveMode: pilot",
fmt.Sprintf("12385,%d,%d,%d,%d,%d,%d,50,%d\n", channel1, channel2, channel3, channel4, channel5, 1850, distanceCm),
-1., -1., events.DriveMode_PILOT, false},
// DriveMode: user
{"DriveMode: user",
fmt.Sprintf("12390,%d,%d,%d,%d,%d,%d,50,%d\n", channel1, channel2, channel3, channel4, channel5, 1003, distanceCm),
-1., -1., events.DriveMode_USER, false},
{"Sterring: over left",
fmt.Sprintf("12395,%d,%d,%d,%d,%d,%d,50,%d\n", 99, channel2, channel3, channel4, channel5, channel6, distanceCm),
-1., -1., events.DriveMode_USER, false},
{"Sterring: left",
fmt.Sprintf("12400,%d,%d,%d,%d,%d,%d,50,%d\n", 998, channel2, channel3, channel4, channel5, channel6, distanceCm),
-1., -0.93, events.DriveMode_USER, false},
{"Sterring: middle",
fmt.Sprintf("12405,%d,%d,%d,%d,%d,%d,50,%d\n", 1450, channel2, channel3, channel4, channel5, channel6, distanceCm),
-1., -0.04, events.DriveMode_USER, false},
{"Sterring: right",
fmt.Sprintf("12410,%d,%d,%d,%d,%d,%d,50,%d\n", 1958, channel2, channel3, channel4, channel5, channel6, distanceCm),
-1., 0.96, events.DriveMode_USER, false},
{"Sterring: over right",
fmt.Sprintf("12415,%d,%d,%d,%d,%d,%d,50,%d\n", 2998, channel2, channel3, channel4, channel5, channel6, distanceCm),
-1., 1., events.DriveMode_USER, false},
{"Throttle: over down",
fmt.Sprintf("12420,%d,%d,%d,%d,%d,%d,50,%d\n", channel1, 99, channel3, channel4, channel5, channel6, distanceCm),
-1., -1., events.DriveMode_USER, false},
{"Throttle: down",
fmt.Sprintf("12425,%d,%d,%d,%d,%d,%d,50,%d\n", channel1, 998, channel3, channel4, channel5, channel6, distanceCm),
-0.95, -1., events.DriveMode_USER, false},
{"Throttle: stop",
fmt.Sprintf("12430,%d,%d,%d,%d,%d,%d,50,%d\n", channel1, 1450, channel3, channel4, channel5, channel6, distanceCm),
-0.03, -1., events.DriveMode_USER, false},
{"Throttle: up",
fmt.Sprintf("12435,%d,%d,%d,%d,%d,%d,50,%d\n", channel1, 1948, channel3, channel4, channel5, channel6, distanceCm),
0.99, -1., events.DriveMode_USER, false},
{"Throttle: over up",
fmt.Sprintf("12440,%d,%d,%d,%d,%d,%d,50,%d\n", channel1, 2998, channel3, channel4, channel5, channel6, distanceCm),
1., -1., events.DriveMode_USER, false},
}
for _, c := range cases {
w := bufio.NewWriter(serialClient)
_, err := w.WriteString(c.content)
if err != nil {
t.Errorf("unable to send test content: %v", c.content)
}
err = w.Flush()
if err != nil {
t.Error("unable to flush content")
}
time.Sleep(10 * time.Millisecond)
a.mutex.Lock()
if fmt.Sprintf("%0.2f", a.throttle) != fmt.Sprintf("%0.2f", c.expectedThrottle) {
t.Errorf("%s: bad throttle value, expected: %0.2f, actual: %.2f", c.name, c.expectedThrottle, a.throttle)
}
if fmt.Sprintf("%0.2f", a.steering) != fmt.Sprintf("%0.2f", c.expectedSteering) {
t.Errorf("%s: bad steering value, expected: %0.2f, actual: %.2f", c.name, c.expectedSteering, a.steering)
}
if a.driveMode != c.expectedDriveMode {
t.Errorf("%s: bad drive mode, expected: %v, actual:%v", c.name, c.expectedDriveMode, a.driveMode)
}
if a.ctrlRecord != c.expectedSwitchRecord {
t.Errorf("%s: bad switch record, expected: %v, actual:%v", c.name, c.expectedSwitchRecord, a.ctrlRecord)
}
a.mutex.Unlock()
}
}
func TestPublish(t *testing.T) {
oldPublish := publish
defer func() { publish = oldPublish }()
var muPublishedEvents sync.Mutex
pulishedEvents := make(map[string][]byte)
publish = func(client mqtt.Client, topic string, payload *[]byte) {
muPublishedEvents.Lock()
defer muPublishedEvents.Unlock()
pulishedEvents[topic] = *payload
}
ln, err := net.Listen("tcp", ":8080")
if err != nil {
t.Fatalf("unable to init connection for test")
}
defer ln.Close()
client, err := net.Dial("tcp", "localhost:8080")
if err != nil {
t.Fatalf("unable to init connection for test")
}
defer client.Close()
conn, err := ln.Accept()
if err != nil {
t.Fatalf("unable to init connection for test")
}
defer conn.Close()
pubFrequency := 100.
a := Part{
client: nil,
serial: conn,
pubFrequency: pubFrequency,
throttleTopic: "car/part/arduino/throttle",
steeringTopic: "car/part/arduino/steering",
driveModeTopic: "car/part/arduino/drive_mode",
switchRecordTopic: "car/part/arduino/switch_record",
cancel: make(chan interface{}),
}
go a.Start()
defer a.Stop()
cases := []struct {
throttle, steering float32
driveMode events.DriveMode
switchRecord bool
expectedThrottle events.ThrottleMessage
expectedSteering events.SteeringMessage
expectedDriveMode events.DriveModeMessage
expectedSwitchRecord events.SwitchRecordMessage
}{
{-1, 1, events.DriveMode_USER, true,
events.ThrottleMessage{Throttle: -1., Confidence: 1.},
events.SteeringMessage{Steering: 1.0, Confidence: 1.},
events.DriveModeMessage{DriveMode: events.DriveMode_USER},
events.SwitchRecordMessage{Enabled: false},
},
{0, 0, events.DriveMode_PILOT, false,
events.ThrottleMessage{Throttle: 0., Confidence: 1.},
events.SteeringMessage{Steering: 0., Confidence: 1.},
events.DriveModeMessage{DriveMode: events.DriveMode_PILOT},
events.SwitchRecordMessage{Enabled: true},
},
{0.87, -0.58, events.DriveMode_PILOT, false,
events.ThrottleMessage{Throttle: 0.87, Confidence: 1.},
events.SteeringMessage{Steering: -0.58, Confidence: 1.},
events.DriveModeMessage{DriveMode: events.DriveMode_PILOT},
events.SwitchRecordMessage{Enabled: true},
},
}
for _, c := range cases {
a.mutex.Lock()
a.throttle = c.throttle
a.steering = c.steering
a.driveMode = c.driveMode
a.ctrlRecord = c.switchRecord
a.mutex.Unlock()
time.Sleep(time.Second / time.Duration(int(pubFrequency)))
time.Sleep(500 * time.Millisecond)
var throttleMsg events.ThrottleMessage
muPublishedEvents.Lock()
unmarshalMsg(t, pulishedEvents["car/part/arduino/throttle"], &throttleMsg)
muPublishedEvents.Unlock()
if throttleMsg.String() != c.expectedThrottle.String() {
t.Errorf("msg(car/part/arduino/throttle): %v, wants %v", throttleMsg, c.expectedThrottle)
}
var steeringMsg events.SteeringMessage
muPublishedEvents.Lock()
unmarshalMsg(t, pulishedEvents["car/part/arduino/steering"], &steeringMsg)
muPublishedEvents.Unlock()
if steeringMsg.String() != c.expectedSteering.String() {
t.Errorf("msg(car/part/arduino/steering): %v, wants %v", steeringMsg, c.expectedSteering)
}
var driveModeMsg events.DriveModeMessage
muPublishedEvents.Lock()
unmarshalMsg(t, pulishedEvents["car/part/arduino/drive_mode"], &driveModeMsg)
muPublishedEvents.Unlock()
if driveModeMsg.String() != c.expectedDriveMode.String() {
t.Errorf("msg(car/part/arduino/drive_mode): %v, wants %v", driveModeMsg, c.expectedDriveMode)
}
var switchRecordMsg events.SwitchRecordMessage
muPublishedEvents.Lock()
unmarshalMsg(t, pulishedEvents["car/part/arduino/switch_record"], &switchRecordMsg)
muPublishedEvents.Unlock()
if switchRecordMsg.String() != c.expectedSwitchRecord.String() {
t.Errorf("msg(car/part/arduino/switch_record): %v, wants %v", switchRecordMsg, c.expectedSwitchRecord)
}
}
}
func unmarshalMsg(t *testing.T, payload []byte, msg proto.Message) {
err := proto.Unmarshal(payload, msg)
if err != nil {
t.Errorf("unable to unmarshal protobuf content to %T: %v", msg, err)
}
}