From dca1c25f11c98ffc4c5f91ca9ee6f5fd7544c3a9 Mon Sep 17 00:00:00 2001 From: Cyrille Nofficial Date: Mon, 3 Feb 2020 19:15:51 +0100 Subject: [PATCH] Refactor logs --- actuator/pca9685.go | 14 +++++++------- actuator/steering.go | 2 +- actuator/throttle.go | 3 ++- cmd/rc-pca9685/rc-pca9685.go | 22 ++++++++++++++-------- part/part.go | 5 +++-- 5 files changed, 27 insertions(+), 19 deletions(-) diff --git a/actuator/pca9685.go b/actuator/pca9685.go index 10d4986..02a3969 100644 --- a/actuator/pca9685.go +++ b/actuator/pca9685.go @@ -1,7 +1,7 @@ package actuator import ( - "log" + log "github.com/sirupsen/logrus" "periph.io/x/periph/conn/i2c/i2creg" "periph.io/x/periph/conn/physic" "periph.io/x/periph/experimental/devices/pca9685" @@ -13,27 +13,27 @@ var ( ) func init() { - log.Print("init pca9685 controller") + log.Info("init pca9685 controller") _, err := host.Init() if err != nil { log.Fatalf("unable to init host: %v", err) } - log.Print("open i2c bus") + log.Info("open i2c bus") bus, err := i2creg.Open("") if err != nil { log.Fatalf("unable to init i2c bus: %v", err) } - log.Print("i2c bus opened") + log.Info("i2c bus opened") device, err = pca9685.NewI2C(bus, pca9685.I2CAddr) if err != nil { log.Fatalf("unable to init pca9685 bus: %v", err) } - log.Printf("set pwm frequency to %d", 60) + log.Infof("set pwm frequency to %d", 60) err = device.SetPwmFreq(60 * physic.Hertz) if err != nil { - log.Fatalf("unable to set pwm frequency: %v", err) + log.Panicf("unable to set pwm frequency: %v", err) } - log.Print("init done") + log.Info("init done") } diff --git a/actuator/steering.go b/actuator/steering.go index ea596dd..de7cebd 100644 --- a/actuator/steering.go +++ b/actuator/steering.go @@ -21,7 +21,7 @@ type Steering struct { func (s *Steering) SetPulse(pulse int) { err := s.dev.SetPwm(s.channel, 0, gpio.Duty(pulse)) if err != nil { - log.Infof("unable to set throttle pwm value: %v", err) + log.Warningf("unable to set steering pwm value: %v", err) } } diff --git a/actuator/throttle.go b/actuator/throttle.go index 0d6e946..9a5cf43 100644 --- a/actuator/throttle.go +++ b/actuator/throttle.go @@ -34,6 +34,7 @@ func (t *Throttle) SetPercentValue(p float32) { } else { pulse = util.MapRange(float64(p), MinThrottle, 0, float64(t.minPulse), float64(t.zeroPulse)) } + log.Debugf("set throttle to %v-> %v (%v, %v, %v, %v, %v)", p, pulse, LeftAngle, RightAngle, t.minPulse, t.maxPulse, t.zeroPulse) t.SetPulse(pulse) } @@ -46,7 +47,7 @@ func NewThrottle(channel, zeroPulse, minPulse, maxPulse int) *Throttle { maxPulse: maxPulse, } - log.Printf("send zero pulse to calibrate ESC: %v", zeroPulse) + log.Infof("send zero pulse to calibrate ESC") t.SetPulse(zeroPulse) return &t diff --git a/cmd/rc-pca9685/rc-pca9685.go b/cmd/rc-pca9685/rc-pca9685.go index 247aac9..585123d 100644 --- a/cmd/rc-pca9685/rc-pca9685.go +++ b/cmd/rc-pca9685/rc-pca9685.go @@ -24,40 +24,42 @@ const ( func main() { var mqttBroker, username, password, clientId, topicThrottle, topicSteering string + var debug bool mqttQos := cli.InitIntFlag("MQTT_QOS", 0) _, mqttRetain := os.LookupEnv("MQTT_RETAIN") cli.InitMqttFlags(DefaultClientId, &mqttBroker, &username, &password, &clientId, &mqttQos, &mqttRetain) + flag.BoolVar(&debug, "debug", false, "Display raw value to debug") var throttleChannel, throttleStoppedPWM, throttleMinPWM, throttleMaxPWM int if err := cli.SetIntDefaultValueFromEnv(&throttleChannel, "THROTTLE_CHANNEL", ThrottleChannel); err != nil { - log.Infof("unable to init throttleChannel arg: %v", err) + log.Warningf("unable to init throttleChannel arg: %v", err) } if err := cli.SetIntDefaultValueFromEnv(&throttleStoppedPWM, "THROTTLE_STOPPED_PWM", ThrottleStoppedPWM); err != nil { - log.Infof("unable to init throttleStoppedPWM arg: %v", err) + log.Warningf("unable to init throttleStoppedPWM arg: %v", err) } if err := cli.SetIntDefaultValueFromEnv(&throttleMinPWM, "THROTTLE_MIN_PWM", ThrottleMinPWM); err != nil { - log.Infof("unable to init throttleMinPWM arg: %v", err) + log.Warningf("unable to init throttleMinPWM arg: %v", err) } if err := cli.SetIntDefaultValueFromEnv(&throttleMaxPWM, "THROTTLE_MAX_PWM", ThrottleMaxPWM); err != nil { - log.Infof("unable to init throttleMaxPWM arg: %v", err) + log.Warningf("unable to init throttleMaxPWM arg: %v", err) } var steeringChannel, steeringLeftPWM, steeringRightPWM int if err := cli.SetIntDefaultValueFromEnv(&steeringChannel, "STEERING_CHANNEL", SteeringChannel); err != nil { - log.Infof("unable to init steeringChannel arg: %v", err) + log.Warningf("unable to init steeringChannel arg: %v", err) } if err := cli.SetIntDefaultValueFromEnv(&steeringLeftPWM, "STEERING_LEFT_PWM", SteeringLeftPWM); err != nil { - log.Infof("unable to init steeringLeftPWM arg: %v", err) + log.Warningf("unable to init steeringLeftPWM arg: %v", err) } if err := cli.SetIntDefaultValueFromEnv(&steeringRightPWM, "STEERING_RIGHT_PWM", SteeringRightPWM); err != nil { - log.Infof("unable to init steeringRightPWM arg: %v", err) + log.Warningf("unable to init steeringRightPWM arg: %v", err) } var updatePWMFrequency int if err := cli.SetIntDefaultValueFromEnv(&updatePWMFrequency, "UPDATE_PWM_FREQUENCY", 25); err != nil { - log.Infof("unable to init updatePWMFrequency arg: %v", err) + log.Warningf("unable to init updatePWMFrequency arg: %v", err) } flag.StringVar(&topicThrottle, "mqtt-topic-throttle", os.Getenv("MQTT_TOPIC_THROTTLE"), "Mqtt topic that contains throttle value, use MQTT_TOPIC_THROTTLE if args not set") @@ -77,6 +79,10 @@ func main() { os.Exit(1) } + if debug { + log.SetLevel(log.DebugLevel) + } + client, err := cli.Connect(mqttBroker, username, password, clientId) if err != nil { log.Fatalf("unable to connect to mqtt bus: %v", err) diff --git a/part/part.go b/part/part.go index d8d47b1..e1733b8 100644 --- a/part/part.go +++ b/part/part.go @@ -67,9 +67,10 @@ func (p *Pca9685Part) onThrottleChange(_ MQTT.Client, message MQTT.Message) { var throttle events.ThrottleMessage err := proto.Unmarshal(message.Payload(), &throttle) if err != nil { - log.Infof("[%v] unable to unmarshall throttle msg: %v", message.Topic(), err) + log.Warningf("[%v] unable to unmarshall throttle msg: %v", message.Topic(), err) return } + log.Debugf("new throttle value: %v", throttle.GetThrottle()) p.muThrottle.Lock() defer p.muThrottle.Unlock() p.throttleCtrl.SetPercentValue(throttle.GetThrottle()) @@ -79,7 +80,7 @@ func (p *Pca9685Part) onSteeringChange(_ MQTT.Client, message MQTT.Message) { var steering events.SteeringMessage err := proto.Unmarshal(message.Payload(), &steering) if err != nil { - log.Infof("[%v] unable to unmarshall steering msg: %v", message.Topic(), err) + log.Warningf("[%v] unable to unmarshal steering msg: %v", message.Topic(), err) return } p.muSteering.Lock()