Refactor logs
This commit is contained in:
		| @@ -1,7 +1,7 @@ | |||||||
| package actuator | package actuator | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"log" | 	log "github.com/sirupsen/logrus" | ||||||
| 	"periph.io/x/periph/conn/i2c/i2creg" | 	"periph.io/x/periph/conn/i2c/i2creg" | ||||||
| 	"periph.io/x/periph/conn/physic" | 	"periph.io/x/periph/conn/physic" | ||||||
| 	"periph.io/x/periph/experimental/devices/pca9685" | 	"periph.io/x/periph/experimental/devices/pca9685" | ||||||
| @@ -13,27 +13,27 @@ var ( | |||||||
| ) | ) | ||||||
|  |  | ||||||
| func init() { | func init() { | ||||||
| 	log.Print("init pca9685 controller") | 	log.Info("init pca9685 controller") | ||||||
| 	_, err := host.Init() | 	_, err := host.Init() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("unable to init host: %v", err) | 		log.Fatalf("unable to init host: %v", err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	log.Print("open i2c bus") | 	log.Info("open i2c bus") | ||||||
| 	bus, err := i2creg.Open("") | 	bus, err := i2creg.Open("") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("unable to init i2c bus: %v", err) | 		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) | 	device, err = pca9685.NewI2C(bus, pca9685.I2CAddr) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("unable to init pca9685 bus: %v", err) | 		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) | 	err = device.SetPwmFreq(60 * physic.Hertz) | ||||||
| 	if err != nil { | 	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") | ||||||
| } | } | ||||||
|   | |||||||
| @@ -21,7 +21,7 @@ type Steering struct { | |||||||
| func (s *Steering) SetPulse(pulse int) { | func (s *Steering) SetPulse(pulse int) { | ||||||
| 	err := s.dev.SetPwm(s.channel, 0, gpio.Duty(pulse)) | 	err := s.dev.SetPwm(s.channel, 0, gpio.Duty(pulse)) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Infof("unable to set throttle pwm value: %v", err) | 		log.Warningf("unable to set steering pwm value: %v", err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -34,6 +34,7 @@ func (t *Throttle) SetPercentValue(p float32) { | |||||||
| 	} else { | 	} else { | ||||||
| 		pulse = util.MapRange(float64(p), MinThrottle, 0, float64(t.minPulse), float64(t.zeroPulse)) | 		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) | 	t.SetPulse(pulse) | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -46,7 +47,7 @@ func NewThrottle(channel, zeroPulse, minPulse, maxPulse int) *Throttle { | |||||||
| 		maxPulse:  maxPulse, | 		maxPulse:  maxPulse, | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	log.Printf("send zero pulse to calibrate ESC: %v", zeroPulse) | 	log.Infof("send zero pulse to calibrate ESC") | ||||||
| 	t.SetPulse(zeroPulse) | 	t.SetPulse(zeroPulse) | ||||||
|  |  | ||||||
| 	return &t | 	return &t | ||||||
|   | |||||||
| @@ -24,40 +24,42 @@ const ( | |||||||
|  |  | ||||||
| func main() { | func main() { | ||||||
| 	var mqttBroker, username, password, clientId, topicThrottle, topicSteering string | 	var mqttBroker, username, password, clientId, topicThrottle, topicSteering string | ||||||
|  | 	var debug bool | ||||||
|  |  | ||||||
| 	mqttQos := cli.InitIntFlag("MQTT_QOS", 0) | 	mqttQos := cli.InitIntFlag("MQTT_QOS", 0) | ||||||
| 	_, mqttRetain := os.LookupEnv("MQTT_RETAIN") | 	_, mqttRetain := os.LookupEnv("MQTT_RETAIN") | ||||||
|  |  | ||||||
| 	cli.InitMqttFlags(DefaultClientId, &mqttBroker, &username, &password, &clientId, &mqttQos, &mqttRetain) | 	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 | 	var throttleChannel, throttleStoppedPWM, throttleMinPWM, throttleMaxPWM int | ||||||
| 	if err := cli.SetIntDefaultValueFromEnv(&throttleChannel, "THROTTLE_CHANNEL", ThrottleChannel); err != nil { | 	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 { | 	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 { | 	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 { | 	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 | 	var steeringChannel, steeringLeftPWM, steeringRightPWM int | ||||||
| 	if err := cli.SetIntDefaultValueFromEnv(&steeringChannel, "STEERING_CHANNEL", SteeringChannel); err != nil { | 	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 { | 	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 { | 	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 | 	var updatePWMFrequency int | ||||||
| 	if err := cli.SetIntDefaultValueFromEnv(&updatePWMFrequency, "UPDATE_PWM_FREQUENCY", 25); err != nil { | 	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") | 	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) | 		os.Exit(1) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	if debug { | ||||||
|  | 		log.SetLevel(log.DebugLevel) | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	client, err := cli.Connect(mqttBroker, username, password, clientId) | 	client, err := cli.Connect(mqttBroker, username, password, clientId) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("unable to connect to mqtt bus: %v", err) | 		log.Fatalf("unable to connect to mqtt bus: %v", err) | ||||||
|   | |||||||
| @@ -67,9 +67,10 @@ func (p *Pca9685Part) onThrottleChange(_ MQTT.Client, message MQTT.Message) { | |||||||
| 	var throttle events.ThrottleMessage | 	var throttle events.ThrottleMessage | ||||||
| 	err := proto.Unmarshal(message.Payload(), &throttle) | 	err := proto.Unmarshal(message.Payload(), &throttle) | ||||||
| 	if err != nil { | 	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 | 		return | ||||||
| 	} | 	} | ||||||
|  | 	log.Debugf("new throttle value: %v", throttle.GetThrottle()) | ||||||
| 	p.muThrottle.Lock() | 	p.muThrottle.Lock() | ||||||
| 	defer p.muThrottle.Unlock() | 	defer p.muThrottle.Unlock() | ||||||
| 	p.throttleCtrl.SetPercentValue(throttle.GetThrottle()) | 	p.throttleCtrl.SetPercentValue(throttle.GetThrottle()) | ||||||
| @@ -79,7 +80,7 @@ func (p *Pca9685Part) onSteeringChange(_ MQTT.Client, message MQTT.Message) { | |||||||
| 	var steering events.SteeringMessage | 	var steering events.SteeringMessage | ||||||
| 	err := proto.Unmarshal(message.Payload(), &steering) | 	err := proto.Unmarshal(message.Payload(), &steering) | ||||||
| 	if err != nil { | 	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 | 		return | ||||||
| 	} | 	} | ||||||
| 	p.muSteering.Lock() | 	p.muSteering.Lock() | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user