robocar-arduino/cmd/rc-arduino/rc-arduino.go

57 lines
2.1 KiB
Go
Raw Normal View History

2019-12-01 21:35:19 +00:00
package main
import (
"flag"
"github.com/cyrilix/robocar-arduino/arduino"
2019-12-18 22:21:52 +00:00
"github.com/cyrilix/robocar-base/cli"
2020-02-03 18:00:21 +00:00
log "github.com/sirupsen/logrus"
2019-12-01 21:35:19 +00:00
"os"
)
const DefaultClientId = "robocar-arduino"
func main() {
2020-01-27 18:21:26 +00:00
var mqttBroker, username, password, clientId string
var throttleTopic, steeringTopic, driveModeTopic, switchRecordTopic string
2019-12-01 21:35:19 +00:00
var device string
var baud int
var pubFrequency float64
2019-12-18 22:21:52 +00:00
var debug bool
2019-12-01 21:35:19 +00:00
2019-12-18 22:21:52 +00:00
mqttQos := cli.InitIntFlag("MQTT_QOS", 0)
2019-12-01 21:35:19 +00:00
_, mqttRetain := os.LookupEnv("MQTT_RETAIN")
2019-12-18 22:21:52 +00:00
cli.InitMqttFlags(DefaultClientId, &mqttBroker, &username, &password, &clientId, &mqttQos, &mqttRetain)
2019-12-01 21:35:19 +00:00
flag.Float64Var(&pubFrequency, "mqtt-pub-frequency", 25., "Number of messages to publish per second")
2020-01-27 18:21:26 +00:00
flag.StringVar(&throttleTopic, "mqtt-topic-throttle", os.Getenv("MQTT_TOPIC_THROTTLE"), "Mqtt topic where to publish throttle values, use MQTT_TOPIC_THROTTLE if args not set")
flag.StringVar(&steeringTopic, "mqtt-topic-steering", os.Getenv("MQTT_TOPIC_STEERING"), "Mqtt topic where to publish steering values, use MQTT_TOPIC_STEERING if args not set")
flag.StringVar(&driveModeTopic, "mqtt-topic-drive-mode", os.Getenv("MQTT_TOPIC_DRIVE_MODE"), "Mqtt topic where to publish drive mode state, use MQTT_TOPIC_DRIVE_MODE if args not set")
flag.StringVar(&switchRecordTopic, "mqtt-topic-switch-record", os.Getenv("MQTT_TOPIC_SWITCH_RECORD"), "Mqtt topic where to publish switch record state, use MQTT_TOPIC_SWITCH_RECORD if args not set")
2019-12-01 21:35:19 +00:00
flag.StringVar(&device, "device", "/dev/serial0", "Serial device")
flag.IntVar(&baud, "baud", 115200, "Serial baud")
2019-12-18 22:21:52 +00:00
flag.BoolVar(&debug, "debug", false, "Display raw value to debug")
flag.Parse()
if len(os.Args) <= 1 {
flag.PrintDefaults()
os.Exit(1)
}
2019-12-01 21:35:19 +00:00
2020-02-03 18:00:21 +00:00
if debug {
log.SetLevel(log.DebugLevel)
}
client, err := cli.Connect(mqttBroker, username, password, clientId)
2020-01-27 18:21:26 +00:00
if err != nil {
log.Fatalf("unable to connect to mqtt broker: %v", err)
}
defer client.Disconnect(10)
2019-12-01 21:35:19 +00:00
2020-02-03 18:00:21 +00:00
a := arduino.NewPart(client, device, baud, throttleTopic, steeringTopic, driveModeTopic, switchRecordTopic, pubFrequency)
err = a.Start()
2019-12-18 22:21:52 +00:00
if err != nil {
log.Printf("unable to start service: %v", err)
}
2019-12-01 21:35:19 +00:00
}