2021-01-15 10:12:21 +00:00
|
|
|
package gateway
|
2020-09-20 21:16:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2021-10-17 17:03:29 +00:00
|
|
|
"bytes"
|
2020-09-20 21:16:06 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"github.com/avast/retry-go"
|
|
|
|
"github.com/cyrilix/robocar-protobuf/go/events"
|
2021-01-15 10:12:21 +00:00
|
|
|
"github.com/cyrilix/robocar-simulator/pkg/simulator"
|
2021-10-17 17:03:29 +00:00
|
|
|
"github.com/disintegration/imaging"
|
2020-09-20 21:16:06 +00:00
|
|
|
"github.com/golang/protobuf/ptypes/timestamp"
|
2021-10-12 20:33:13 +00:00
|
|
|
"go.uber.org/zap"
|
2021-10-17 17:03:29 +00:00
|
|
|
"image"
|
|
|
|
"image/jpeg"
|
2020-09-20 21:16:06 +00:00
|
|
|
"io"
|
|
|
|
"net"
|
2021-10-17 17:03:29 +00:00
|
|
|
"strconv"
|
2021-01-15 10:31:37 +00:00
|
|
|
"sync"
|
2020-09-20 21:16:06 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-01-21 22:39:52 +00:00
|
|
|
type SimulatorSource interface {
|
|
|
|
FrameSource
|
|
|
|
SteeringSource
|
|
|
|
ThrottleSource
|
|
|
|
}
|
|
|
|
|
|
|
|
type FrameSource interface {
|
|
|
|
SubscribeFrame() <-chan *events.FrameMessage
|
|
|
|
}
|
|
|
|
type SteeringSource interface {
|
|
|
|
SubscribeSteering() <-chan *events.SteeringMessage
|
|
|
|
}
|
|
|
|
type ThrottleSource interface {
|
|
|
|
SubscribeThrottle() <-chan *events.ThrottleMessage
|
|
|
|
}
|
|
|
|
|
2021-10-17 17:03:29 +00:00
|
|
|
func New(addressSimulator string, car *simulator.CarConfigMsg, racer *simulator.RacerBioMsg, camera *simulator.CamConfigMsg) (*Gateway, error) {
|
2021-10-12 20:33:13 +00:00
|
|
|
l := zap.S().With("simulator", addressSimulator)
|
2021-01-15 10:12:21 +00:00
|
|
|
l.Info("run gateway from simulator")
|
2020-09-20 21:16:06 +00:00
|
|
|
|
2021-10-17 17:03:29 +00:00
|
|
|
imgHeight, err := strconv.Atoi(camera.ImgH)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid camera configuration, height should be a number: %w", err)
|
|
|
|
}
|
|
|
|
imgWidth, err := strconv.Atoi(camera.ImgW)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid camera configuration, width should be a number: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-09-20 21:16:06 +00:00
|
|
|
return &Gateway{
|
2021-02-16 16:46:10 +00:00
|
|
|
address: addressSimulator,
|
|
|
|
log: l,
|
|
|
|
frameSubscribers: make(map[chan<- *events.FrameMessage]interface{}),
|
|
|
|
steeringSubscribers: make(map[chan<- *events.SteeringMessage]interface{}),
|
|
|
|
throttleSubscribers: make(map[chan<- *events.ThrottleMessage]interface{}),
|
2021-02-09 18:14:29 +00:00
|
|
|
telemetrySubscribers: make(map[chan *simulator.TelemetryMsg]interface{}),
|
2021-02-16 16:46:10 +00:00
|
|
|
carSubscribers: make(map[chan *simulator.Msg]interface{}),
|
2021-10-12 20:13:44 +00:00
|
|
|
racerSubscribers: make(map[chan *simulator.Msg]interface{}),
|
|
|
|
cameraSubscribers: make(map[chan *simulator.Msg]interface{}),
|
2021-02-16 16:46:10 +00:00
|
|
|
carConfig: car,
|
2021-10-17 17:03:29 +00:00
|
|
|
imgWidth: imgWidth,
|
|
|
|
imgHeight: imgHeight,
|
2021-02-16 16:46:10 +00:00
|
|
|
racer: racer,
|
2021-10-12 20:13:44 +00:00
|
|
|
cameraConfig: camera,
|
2021-10-17 17:03:29 +00:00
|
|
|
}, nil
|
2020-09-20 21:16:06 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 20:33:13 +00:00
|
|
|
// Gateway is Simulator interface to events gateway frames into events topicFrame
|
2020-09-20 21:16:06 +00:00
|
|
|
type Gateway struct {
|
2020-11-29 17:42:33 +00:00
|
|
|
cancel chan interface{}
|
2020-09-20 21:16:06 +00:00
|
|
|
|
|
|
|
address string
|
2021-02-09 18:14:29 +00:00
|
|
|
|
|
|
|
muCommand sync.Mutex
|
2021-02-16 16:46:10 +00:00
|
|
|
muConn sync.Mutex
|
|
|
|
conn io.ReadWriteCloser
|
2021-01-15 10:31:37 +00:00
|
|
|
|
|
|
|
muControl sync.Mutex
|
|
|
|
lastControl *simulator.ControlMsg
|
2020-09-20 21:16:06 +00:00
|
|
|
|
2021-10-12 20:33:13 +00:00
|
|
|
log *zap.SugaredLogger
|
2021-01-21 22:39:52 +00:00
|
|
|
|
|
|
|
frameSubscribers map[chan<- *events.FrameMessage]interface{}
|
|
|
|
steeringSubscribers map[chan<- *events.SteeringMessage]interface{}
|
|
|
|
throttleSubscribers map[chan<- *events.ThrottleMessage]interface{}
|
2021-02-09 18:14:29 +00:00
|
|
|
|
|
|
|
telemetrySubscribers map[chan *simulator.TelemetryMsg]interface{}
|
2021-02-16 16:46:10 +00:00
|
|
|
carSubscribers map[chan *simulator.Msg]interface{}
|
2021-10-12 20:13:44 +00:00
|
|
|
racerSubscribers map[chan *simulator.Msg]interface{}
|
|
|
|
cameraSubscribers map[chan *simulator.Msg]interface{}
|
2021-02-09 18:14:29 +00:00
|
|
|
|
2021-10-12 20:13:44 +00:00
|
|
|
carConfig *simulator.CarConfigMsg
|
2021-10-17 17:03:29 +00:00
|
|
|
imgHeight int
|
|
|
|
imgWidth int
|
2021-10-12 20:13:44 +00:00
|
|
|
racer *simulator.RacerBioMsg
|
2021-02-16 16:46:10 +00:00
|
|
|
cameraConfig *simulator.CamConfigMsg
|
2021-10-17 17:03:29 +00:00
|
|
|
|
2020-09-20 21:16:06 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 16:04:42 +00:00
|
|
|
func (g *Gateway) Start() error {
|
|
|
|
g.log.Info("connect to simulator")
|
|
|
|
g.cancel = make(chan interface{})
|
2021-02-09 18:14:29 +00:00
|
|
|
|
|
|
|
go g.run()
|
2020-09-20 21:16:06 +00:00
|
|
|
|
2021-10-17 17:03:29 +00:00
|
|
|
|
2021-10-12 20:13:44 +00:00
|
|
|
err := g.writeRacerConfig()
|
2021-02-09 18:14:29 +00:00
|
|
|
if err != nil {
|
2021-10-12 20:13:44 +00:00
|
|
|
return fmt.Errorf("unable to configure racer to server: %v", err)
|
2021-02-09 18:14:29 +00:00
|
|
|
}
|
2021-10-17 17:03:29 +00:00
|
|
|
|
2021-02-16 16:46:10 +00:00
|
|
|
err = g.writeCameraConfig()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to configure camera to server: %v", err)
|
|
|
|
}
|
2020-09-20 21:16:06 +00:00
|
|
|
|
2021-10-17 17:03:29 +00:00
|
|
|
|
|
|
|
err = g.writeCarConfig()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to configure car to server: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
msgChan := g.subscribeTelemetryEvents()
|
2020-09-20 21:16:06 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-msgChan:
|
2021-10-17 17:03:29 +00:00
|
|
|
g.log.Debug("try to publish frame")
|
|
|
|
fr, err := g.publishFrame(msg)
|
|
|
|
if err != nil {
|
|
|
|
zap.S().Errorf("unable to publish frame, ignore event %v: %v", msg, err)
|
|
|
|
}
|
|
|
|
g.log.Debugf("frame published: %v", fr)
|
2021-02-01 16:04:42 +00:00
|
|
|
go g.publishInputSteering(msg, fr)
|
|
|
|
go g.publishInputThrottle(msg, fr)
|
|
|
|
case <-g.cancel:
|
2020-09-20 21:16:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 16:04:42 +00:00
|
|
|
func (g *Gateway) Stop() {
|
|
|
|
g.log.Info("close simulator gateway")
|
|
|
|
close(g.cancel)
|
2020-09-20 21:16:06 +00:00
|
|
|
|
2021-02-01 16:04:42 +00:00
|
|
|
if err := g.Close(); err != nil {
|
|
|
|
g.log.Warnf("unexpected error while simulator connection is closed: %v", err)
|
2020-09-20 21:16:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 16:04:42 +00:00
|
|
|
func (g *Gateway) Close() error {
|
|
|
|
for c := range g.frameSubscribers {
|
2021-01-21 22:39:52 +00:00
|
|
|
close(c)
|
|
|
|
}
|
2021-02-01 16:04:42 +00:00
|
|
|
for c := range g.steeringSubscribers {
|
2021-01-21 22:39:52 +00:00
|
|
|
close(c)
|
|
|
|
}
|
2021-02-01 16:04:42 +00:00
|
|
|
for c := range g.throttleSubscribers {
|
2021-01-21 22:39:52 +00:00
|
|
|
close(c)
|
|
|
|
}
|
2021-02-01 16:04:42 +00:00
|
|
|
if g.conn == nil {
|
|
|
|
g.log.Warn("no connection to close")
|
2020-09-20 21:16:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-02-01 16:04:42 +00:00
|
|
|
if err := g.conn.Close(); err != nil {
|
2020-09-20 21:16:06 +00:00
|
|
|
return fmt.Errorf("unable to close connection to simulator: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-09 18:14:29 +00:00
|
|
|
func (g *Gateway) run() {
|
2021-02-01 16:04:42 +00:00
|
|
|
err := g.connect()
|
2020-09-20 21:16:06 +00:00
|
|
|
if err != nil {
|
2021-02-01 16:04:42 +00:00
|
|
|
g.log.Panicf("unable to connect to simulator: %v", err)
|
2020-09-20 21:16:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = retry.Do(
|
2021-02-09 18:14:29 +00:00
|
|
|
func() error { return g.listen() },
|
2020-09-20 21:16:06 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2021-02-01 16:04:42 +00:00
|
|
|
g.log.Errorf("unable to connect to server: %v", err)
|
2020-09-20 21:16:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 16:04:42 +00:00
|
|
|
func (g *Gateway) connect() error {
|
|
|
|
g.muConn.Lock()
|
|
|
|
defer g.muConn.Unlock()
|
2021-01-15 10:31:37 +00:00
|
|
|
|
2021-02-01 16:04:42 +00:00
|
|
|
if g.conn != nil {
|
2021-01-15 10:31:37 +00:00
|
|
|
// already connected
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err := retry.Do(func() error {
|
2021-02-01 16:04:42 +00:00
|
|
|
g.log.Info("connect to simulator")
|
|
|
|
conn, err := connect(g.address)
|
2021-01-15 10:31:37 +00:00
|
|
|
if err != nil {
|
2021-02-01 16:04:42 +00:00
|
|
|
return fmt.Errorf("unable to connect to simulator at %v", g.address)
|
2021-01-15 10:31:37 +00:00
|
|
|
}
|
2021-02-01 16:04:42 +00:00
|
|
|
g.conn = conn
|
|
|
|
g.log.Info("connection success")
|
2021-01-15 10:31:37 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
retry.Delay(1*time.Second),
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-09 18:14:29 +00:00
|
|
|
func (g *Gateway) listen() error {
|
|
|
|
reader := bufio.NewReader(g.conn)
|
|
|
|
|
2020-09-20 21:16:06 +00:00
|
|
|
for {
|
|
|
|
rawLine, err := reader.ReadBytes('\n')
|
|
|
|
if err == io.EOF {
|
2021-02-01 16:04:42 +00:00
|
|
|
g.log.Info("Connection closed")
|
2020-09-20 21:16:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to read response: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-02-09 18:14:29 +00:00
|
|
|
var msg simulator.Msg
|
2020-09-20 21:16:06 +00:00
|
|
|
err = json.Unmarshal(rawLine, &msg)
|
|
|
|
if err != nil {
|
2021-02-01 16:04:42 +00:00
|
|
|
g.log.Errorf("unable to unmarshal simulator msg '%v': %v", string(rawLine), err)
|
2021-10-17 17:03:29 +00:00
|
|
|
continue
|
2020-09-20 21:16:06 +00:00
|
|
|
}
|
2021-02-09 18:14:29 +00:00
|
|
|
|
2021-10-17 17:03:29 +00:00
|
|
|
g.log.Debugf("new message of type %v", string(msg.MsgType))
|
2021-02-09 18:14:29 +00:00
|
|
|
switch msg.MsgType {
|
2021-10-17 17:03:29 +00:00
|
|
|
case "":
|
|
|
|
g.log.Debug("ping")
|
2021-02-09 18:14:29 +00:00
|
|
|
case simulator.MsgTypeTelemetry:
|
|
|
|
g.broadcastTelemetryMsg(rawLine)
|
|
|
|
case simulator.MsgTypeCarLoaded:
|
2021-10-17 17:03:29 +00:00
|
|
|
g.log.Debugf("car loaded: %v",string(rawLine))
|
|
|
|
if err := g.writeCarConfig(); err != nil{
|
|
|
|
zap.S().Errorf("unable to send car config: %v", err)
|
|
|
|
}
|
2021-02-09 18:14:29 +00:00
|
|
|
g.broadcastCarMsg(rawLine)
|
2021-02-16 16:46:10 +00:00
|
|
|
case simulator.MsgTypeRacerInfo:
|
|
|
|
g.broadcastRacerMsg(rawLine)
|
2021-02-09 18:14:29 +00:00
|
|
|
default:
|
2021-10-12 20:33:13 +00:00
|
|
|
g.log.Warnf("unmanaged simulator message: %v", string(rawLine))
|
2021-02-09 18:14:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Gateway) broadcastTelemetryMsg(rawLine []byte) {
|
2021-10-17 17:03:29 +00:00
|
|
|
var tMsg simulator.TelemetryMsg
|
|
|
|
err := json.Unmarshal(rawLine, &tMsg)
|
|
|
|
if err != nil {
|
|
|
|
g.log.Errorf("unable to unmarshal telemetry simulator msg '%v': %v", string(rawLine), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
g.log.Debugf("broadcast %d telemetry'", len(g.telemetrySubscribers))
|
2021-02-09 18:14:29 +00:00
|
|
|
for c := range g.telemetrySubscribers {
|
2021-10-17 17:03:29 +00:00
|
|
|
g.log.Debugf("broadcast telemetry ")
|
2021-02-09 18:14:29 +00:00
|
|
|
c <- &tMsg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Gateway) broadcastCarMsg(rawLine []byte) {
|
2021-10-17 17:03:29 +00:00
|
|
|
var tMsg simulator.Msg
|
|
|
|
err := json.Unmarshal(rawLine, &tMsg)
|
|
|
|
if err != nil {
|
|
|
|
g.log.Errorf("unable to unmarshal car simulator msg '%v': %v", string(rawLine), err)
|
|
|
|
return
|
|
|
|
}
|
2021-02-09 18:14:29 +00:00
|
|
|
for c := range g.carSubscribers {
|
|
|
|
c <- &tMsg
|
2020-09-20 21:16:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 16:46:10 +00:00
|
|
|
func (g *Gateway) broadcastRacerMsg(rawLine []byte) {
|
2021-10-17 17:03:29 +00:00
|
|
|
var tMsg simulator.Msg
|
|
|
|
err := json.Unmarshal(rawLine, &tMsg)
|
|
|
|
if err != nil {
|
|
|
|
g.log.Errorf("unable to unmarshal racer simulator msg '%v': %v", string(rawLine), err)
|
|
|
|
return
|
|
|
|
}
|
2021-02-16 16:46:10 +00:00
|
|
|
for c := range g.racerSubscribers {
|
|
|
|
c <- &tMsg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (g *Gateway) broadcastCameraMsg(rawLine []byte) {
|
2021-10-17 17:03:29 +00:00
|
|
|
var tMsg simulator.Msg
|
|
|
|
err := json.Unmarshal(rawLine, &tMsg)
|
|
|
|
if err != nil {
|
|
|
|
g.log.Errorf("unable to unmarshal camera simulator msg '%v': %v", string(rawLine), err)
|
|
|
|
return
|
|
|
|
}
|
2021-02-16 16:46:10 +00:00
|
|
|
for c := range g.cameraSubscribers {
|
|
|
|
c <- &tMsg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-17 17:03:29 +00:00
|
|
|
func (g *Gateway) publishFrame(msgSim *simulator.TelemetryMsg) (*events.FrameRef, error) {
|
2020-09-20 21:16:06 +00:00
|
|
|
now := time.Now()
|
2021-01-21 22:39:52 +00:00
|
|
|
frameRef := &events.FrameRef{
|
|
|
|
Name: "gateway",
|
|
|
|
Id: fmt.Sprintf("%d%03d", now.Unix(), now.Nanosecond()/1000/1000),
|
|
|
|
CreatedAt: ×tamp.Timestamp{
|
|
|
|
Seconds: now.Unix(),
|
|
|
|
Nanos: int32(now.Nanosecond()),
|
2020-09-20 21:16:06 +00:00
|
|
|
},
|
2021-01-21 22:39:52 +00:00
|
|
|
}
|
2021-10-17 17:03:29 +00:00
|
|
|
|
2021-01-21 22:39:52 +00:00
|
|
|
msg := &events.FrameMessage{
|
|
|
|
Id: frameRef,
|
2020-09-20 21:16:06 +00:00
|
|
|
Frame: msgSim.Image,
|
|
|
|
}
|
|
|
|
|
2021-10-17 17:03:29 +00:00
|
|
|
img, _, err := image.Decode(bytes.NewReader(msgSim.Image))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("bad image, skip simulator event: %w", err)
|
|
|
|
}
|
|
|
|
if img.Bounds().Dy() != g.imgHeight || img.Bounds().Dx() != g.imgWidth {
|
|
|
|
zap.S().Debugf("resize simulator image from (%vx%v) -> (%dx%d)", img.Bounds().Dx(), img.Bounds().Dy(), g.imgWidth, g.imgHeight)
|
|
|
|
img = imaging.Resize(img, g.imgWidth, g.imgHeight, imaging.NearestNeighbor)
|
|
|
|
var imgBuf bytes.Buffer
|
|
|
|
err = jpeg.Encode(&imgBuf, img, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to encode to peg resized image: %w", err)
|
|
|
|
}
|
|
|
|
msg.Frame = imgBuf.Bytes()
|
|
|
|
}
|
2021-02-01 16:04:42 +00:00
|
|
|
|
2021-10-17 17:03:29 +00:00
|
|
|
g.log.Debugf("events frame '%v/%v'", msg.Id.Name, msg.Id.Id)
|
2021-02-01 16:04:42 +00:00
|
|
|
for fs := range g.frameSubscribers {
|
2021-01-21 22:39:52 +00:00
|
|
|
fs <- msg
|
2020-09-20 21:16:06 +00:00
|
|
|
}
|
2021-10-17 17:03:29 +00:00
|
|
|
return frameRef, nil
|
2021-01-15 10:12:21 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 16:04:42 +00:00
|
|
|
func (g *Gateway) publishInputSteering(msgSim *simulator.TelemetryMsg, frameRef *events.FrameRef) {
|
2021-01-15 10:12:21 +00:00
|
|
|
steering := &events.SteeringMessage{
|
2021-01-21 22:39:52 +00:00
|
|
|
FrameRef: frameRef,
|
2021-01-15 10:31:37 +00:00
|
|
|
Steering: float32(msgSim.SteeringAngle),
|
2021-01-15 10:12:21 +00:00
|
|
|
Confidence: 1.0,
|
|
|
|
}
|
|
|
|
|
2021-10-12 20:33:13 +00:00
|
|
|
g.log.Debugf("events steering '%v'", steering.Steering)
|
2021-02-01 16:04:42 +00:00
|
|
|
for ss := range g.steeringSubscribers {
|
2021-01-21 22:39:52 +00:00
|
|
|
ss <- steering
|
2021-01-15 10:12:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 16:04:42 +00:00
|
|
|
func (g *Gateway) publishInputThrottle(msgSim *simulator.TelemetryMsg, frameRef *events.FrameRef) {
|
2021-01-21 22:39:52 +00:00
|
|
|
msg := &events.ThrottleMessage{
|
|
|
|
FrameRef: frameRef,
|
2021-01-15 10:31:37 +00:00
|
|
|
Throttle: float32(msgSim.Throttle),
|
2021-01-15 10:12:21 +00:00
|
|
|
Confidence: 1.0,
|
|
|
|
}
|
|
|
|
|
2021-10-12 20:33:13 +00:00
|
|
|
g.log.Debugf("events throttle '%v'", msg.Throttle)
|
2021-02-01 16:04:42 +00:00
|
|
|
for ts := range g.throttleSubscribers {
|
2021-01-21 22:39:52 +00:00
|
|
|
ts <- msg
|
2021-01-15 10:12:21 +00:00
|
|
|
}
|
2021-01-21 22:39:52 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 16:04:42 +00:00
|
|
|
func (g *Gateway) SubscribeFrame() <-chan *events.FrameMessage {
|
2021-01-21 22:39:52 +00:00
|
|
|
frameChan := make(chan *events.FrameMessage)
|
2021-02-01 16:04:42 +00:00
|
|
|
g.frameSubscribers[frameChan] = struct{}{}
|
2021-01-21 22:39:52 +00:00
|
|
|
return frameChan
|
|
|
|
}
|
2021-02-01 16:04:42 +00:00
|
|
|
func (g *Gateway) SubscribeSteering() <-chan *events.SteeringMessage {
|
2021-01-21 22:39:52 +00:00
|
|
|
steeringChan := make(chan *events.SteeringMessage)
|
2021-02-01 16:04:42 +00:00
|
|
|
g.steeringSubscribers[steeringChan] = struct{}{}
|
2021-01-21 22:39:52 +00:00
|
|
|
return steeringChan
|
|
|
|
}
|
2021-02-01 16:04:42 +00:00
|
|
|
func (g *Gateway) SubscribeThrottle() <-chan *events.ThrottleMessage {
|
2021-01-21 22:39:52 +00:00
|
|
|
throttleChan := make(chan *events.ThrottleMessage)
|
2021-02-01 16:04:42 +00:00
|
|
|
g.throttleSubscribers[throttleChan] = struct{}{}
|
2021-01-21 22:39:52 +00:00
|
|
|
return throttleChan
|
2020-09-20 21:16:06 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 18:14:29 +00:00
|
|
|
func (g *Gateway) subscribeTelemetryEvents() chan *simulator.TelemetryMsg {
|
|
|
|
telemetryChan := make(chan *simulator.TelemetryMsg)
|
|
|
|
g.telemetrySubscribers[telemetryChan] = struct{}{}
|
|
|
|
return telemetryChan
|
|
|
|
}
|
|
|
|
func (g *Gateway) unsubscribeTelemetryEvents(telemetryChan chan *simulator.TelemetryMsg) {
|
|
|
|
delete(g.telemetrySubscribers, telemetryChan)
|
|
|
|
close(telemetryChan)
|
|
|
|
}
|
|
|
|
func (g *Gateway) subscribeCarEvents() chan *simulator.Msg {
|
|
|
|
carChan := make(chan *simulator.Msg)
|
|
|
|
g.carSubscribers[carChan] = struct{}{}
|
|
|
|
return carChan
|
|
|
|
}
|
|
|
|
func (g *Gateway) unsubscribeCarEvents(carChan chan *simulator.Msg) {
|
|
|
|
delete(g.carSubscribers, carChan)
|
|
|
|
close(carChan)
|
|
|
|
}
|
|
|
|
|
2021-02-16 16:46:10 +00:00
|
|
|
func (g *Gateway) subscribeRacerEvents() chan *simulator.Msg {
|
|
|
|
racerChan := make(chan *simulator.Msg)
|
|
|
|
g.racerSubscribers[racerChan] = struct{}{}
|
|
|
|
return racerChan
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Gateway) unsubscribeRacerEvents(racerChan chan *simulator.Msg) {
|
|
|
|
delete(g.racerSubscribers, racerChan)
|
|
|
|
close(racerChan)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Gateway) subscribeCameraEvents() chan *simulator.Msg {
|
|
|
|
cameraChan := make(chan *simulator.Msg)
|
|
|
|
g.cameraSubscribers[cameraChan] = struct{}{}
|
|
|
|
return cameraChan
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Gateway) unsubscribeCameraEvents(cameraChan chan *simulator.Msg) {
|
|
|
|
delete(g.cameraSubscribers, cameraChan)
|
|
|
|
close(cameraChan)
|
|
|
|
}
|
|
|
|
|
2020-09-20 21:16:06 +00:00
|
|
|
var connect = func(address string) (io.ReadWriteCloser, error) {
|
|
|
|
conn, err := net.Dial("tcp", address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to connect to %v", address)
|
|
|
|
}
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
2021-02-01 16:04:42 +00:00
|
|
|
func (g *Gateway) WriteSteering(message *events.SteeringMessage) {
|
|
|
|
g.muControl.Lock()
|
|
|
|
defer g.muControl.Unlock()
|
|
|
|
g.initLastControlMsg()
|
2021-01-15 10:31:37 +00:00
|
|
|
|
2021-02-08 22:07:09 +00:00
|
|
|
g.lastControl.Steering = fmt.Sprintf("%.2f", message.Steering)
|
2021-10-17 17:03:29 +00:00
|
|
|
zap.S().Debugf("steering updated: %v", g.lastControl.Steering)
|
2021-02-01 16:04:42 +00:00
|
|
|
g.writeControlCommandToSimulator()
|
2021-01-15 10:31:37 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 16:04:42 +00:00
|
|
|
func (g *Gateway) writeControlCommandToSimulator() {
|
|
|
|
content, err := json.Marshal(g.lastControl)
|
2021-01-15 10:31:37 +00:00
|
|
|
if err != nil {
|
2021-02-01 16:04:42 +00:00
|
|
|
g.log.Errorf("unable to marshall control msg \"%#v\": %v", g.lastControl, err)
|
2021-01-15 10:31:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-09 18:14:29 +00:00
|
|
|
err = g.writeCommand(content)
|
2021-01-15 10:31:37 +00:00
|
|
|
if err != nil {
|
2021-02-09 18:14:29 +00:00
|
|
|
g.log.Errorf("unable to send control command to simulator: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Gateway) writeCommand(content []byte) error {
|
|
|
|
g.muCommand.Lock()
|
|
|
|
defer g.muCommand.Unlock()
|
|
|
|
|
|
|
|
if err := g.connect(); err != nil {
|
|
|
|
g.log.Errorf("unable to connect to simulator to send control command: %v", err)
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-12 20:33:13 +00:00
|
|
|
g.log.Debugf("write command to simulator: %v", string(content))
|
2021-02-09 18:14:29 +00:00
|
|
|
w := bufio.NewWriter(g.conn)
|
|
|
|
|
2021-10-17 17:03:29 +00:00
|
|
|
_, err := w.Write(content)
|
2021-02-09 18:14:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to write control msg \"%#v\" to simulator: %v", g.lastControl, err)
|
2021-01-15 10:31:37 +00:00
|
|
|
}
|
|
|
|
err = w.Flush()
|
|
|
|
if err != nil {
|
2021-02-09 18:14:29 +00:00
|
|
|
return fmt.Errorf("unable to flush control msg \"%#v\" to simulator: %v", g.lastControl, err)
|
2021-01-15 10:31:37 +00:00
|
|
|
}
|
2021-02-09 18:14:29 +00:00
|
|
|
return nil
|
2021-01-15 10:31:37 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 16:04:42 +00:00
|
|
|
func (g *Gateway) WriteThrottle(message *events.ThrottleMessage) {
|
|
|
|
g.muControl.Lock()
|
|
|
|
defer g.muControl.Unlock()
|
|
|
|
g.initLastControlMsg()
|
2021-01-15 10:31:37 +00:00
|
|
|
|
|
|
|
if message.Throttle > 0 {
|
2021-02-08 22:07:09 +00:00
|
|
|
g.lastControl.Throttle = fmt.Sprintf("%.2f", message.Throttle)
|
|
|
|
g.lastControl.Brake = "0.0"
|
2021-01-15 10:31:37 +00:00
|
|
|
} else {
|
2021-02-08 22:07:09 +00:00
|
|
|
g.lastControl.Throttle = "0.0"
|
2021-02-09 18:14:29 +00:00
|
|
|
g.lastControl.Brake = fmt.Sprintf("%.2f", -1*message.Throttle)
|
2021-01-15 10:31:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-17 17:03:29 +00:00
|
|
|
zap.S().Debugf("throttle updated: %v, brake: %v", g.lastControl.Throttle, g.lastControl.Brake)
|
2021-02-01 16:04:42 +00:00
|
|
|
g.writeControlCommandToSimulator()
|
2021-01-15 10:31:37 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 16:04:42 +00:00
|
|
|
func (g *Gateway) initLastControlMsg() {
|
|
|
|
if g.lastControl != nil {
|
2021-01-15 10:31:37 +00:00
|
|
|
return
|
|
|
|
}
|
2021-02-01 16:04:42 +00:00
|
|
|
g.lastControl = &simulator.ControlMsg{
|
2021-02-09 18:14:29 +00:00
|
|
|
MsgType: simulator.MsgTypeControl,
|
2021-02-08 22:07:09 +00:00
|
|
|
Steering: "0.0",
|
|
|
|
Throttle: "0.0",
|
|
|
|
Brake: "0.0",
|
2021-01-15 10:31:37 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-09 18:14:29 +00:00
|
|
|
|
|
|
|
func (g *Gateway) writeCarConfig() error {
|
|
|
|
carChan := g.subscribeCarEvents()
|
|
|
|
defer g.unsubscribeCarEvents(carChan)
|
|
|
|
|
|
|
|
g.log.Info("Send car configuration")
|
|
|
|
|
|
|
|
content, err := json.Marshal(g.carConfig)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to marshall car config msg \"%#v\": %v", g.lastControl, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = g.writeCommand(content)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to send car config to simulator: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-10-17 17:03:29 +00:00
|
|
|
select {
|
|
|
|
case msg := <-carChan:
|
|
|
|
g.log.Infof("Car loaded: %v", msg)
|
|
|
|
case <-time.Tick(250 * time.Millisecond):
|
|
|
|
g.log.Info("no response after car loading")
|
|
|
|
}
|
|
|
|
|
2021-02-09 18:14:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-02-16 16:46:10 +00:00
|
|
|
|
|
|
|
func (g *Gateway) writeRacerConfig() error {
|
|
|
|
racerChan := g.subscribeRacerEvents()
|
|
|
|
defer g.unsubscribeRacerEvents(racerChan)
|
|
|
|
|
|
|
|
g.log.Info("Send racer configuration")
|
|
|
|
|
|
|
|
content, err := json.Marshal(g.racer)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to marshall racer config msg \"%#v\": %v", g.lastControl, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = g.writeCommand(content)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to send racer config to simulator: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-10-12 20:13:44 +00:00
|
|
|
select {
|
|
|
|
case msg := <-racerChan:
|
|
|
|
g.log.Infof("Racer loaded: %v", msg)
|
|
|
|
case <-time.Tick(250 * time.Millisecond):
|
2021-10-17 17:03:29 +00:00
|
|
|
g.log.Info("no response after racer loading")
|
2021-02-16 16:46:10 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Gateway) writeCameraConfig() error {
|
|
|
|
cameraChan := g.subscribeCameraEvents()
|
|
|
|
defer g.unsubscribeCameraEvents(cameraChan)
|
|
|
|
|
|
|
|
g.log.Info("Send camera configuration")
|
|
|
|
|
|
|
|
content, err := json.Marshal(g.cameraConfig)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to marshall camera config msg \"%#v\": %v", g.lastControl, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = g.writeCommand(content)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to send camera config to simulator: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-10-12 20:13:44 +00:00
|
|
|
select {
|
2021-02-16 16:46:10 +00:00
|
|
|
case msg := <-cameraChan:
|
|
|
|
g.log.Infof("Camera configured: %v", msg)
|
2021-10-12 20:13:44 +00:00
|
|
|
case <-time.Tick(250 * time.Millisecond):
|
2021-10-17 17:03:29 +00:00
|
|
|
g.log.Info("no response after camera loading")
|
2021-02-16 16:46:10 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|