refactor: log with zap
This commit is contained in:
		@@ -1,7 +1,7 @@
 | 
			
		||||
package actuator
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	log "github.com/sirupsen/logrus"
 | 
			
		||||
	"go.uber.org/zap"
 | 
			
		||||
	"periph.io/x/conn/v3/i2c/i2creg"
 | 
			
		||||
	"periph.io/x/conn/v3/physic"
 | 
			
		||||
	"periph.io/x/devices/v3/pca9685"
 | 
			
		||||
@@ -13,27 +13,27 @@ var (
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	log.Info("init pca9685 controller")
 | 
			
		||||
	zap.S().Info("init pca9685 controller")
 | 
			
		||||
	_, err := host.Init()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatalf("unable to init host: %v", err)
 | 
			
		||||
		zap.S().Fatalf("unable to init host: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	log.Info("open i2c bus")
 | 
			
		||||
	zap.S().Info("open i2c bus")
 | 
			
		||||
	bus, err := i2creg.Open("")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatalf("unable to init i2c bus: %v", err)
 | 
			
		||||
		zap.S().Fatalf("unable to init i2c bus: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	log.Info("i2c bus opened")
 | 
			
		||||
	zap.S().Info("i2c bus opened")
 | 
			
		||||
 | 
			
		||||
	device, err = pca9685.NewI2C(bus, pca9685.I2CAddr)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatalf("unable to init pca9685 bus: %v", err)
 | 
			
		||||
		zap.S().Fatalf("unable to init pca9685 bus: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	log.Infof("set pwm frequency to %d", 60)
 | 
			
		||||
	zap.S().Infof("set pwm frequency to %d", 60)
 | 
			
		||||
	err = device.SetPwmFreq(60 * physic.Hertz)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Panicf("unable to set pwm frequency: %v", err)
 | 
			
		||||
		zap.S().Panicf("unable to set pwm frequency: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	log.Info("init done")
 | 
			
		||||
	zap.S().Info("init done")
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@ package actuator
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/cyrilix/robocar-pca9685/util"
 | 
			
		||||
	log "github.com/sirupsen/logrus"
 | 
			
		||||
	"go.uber.org/zap"
 | 
			
		||||
	"periph.io/x/conn/v3/gpio"
 | 
			
		||||
	"periph.io/x/devices/v3/pca9685"
 | 
			
		||||
)
 | 
			
		||||
@@ -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.Warningf("unable to set steering pwm value: %v", err)
 | 
			
		||||
		zap.S().Warnf("unable to set steering pwm value: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@ package actuator
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/cyrilix/robocar-pca9685/util"
 | 
			
		||||
	log "github.com/sirupsen/logrus"
 | 
			
		||||
	"go.uber.org/zap"
 | 
			
		||||
	"periph.io/x/conn/v3/gpio"
 | 
			
		||||
	"periph.io/x/devices/v3/pca9685"
 | 
			
		||||
)
 | 
			
		||||
@@ -21,7 +21,7 @@ type Throttle struct {
 | 
			
		||||
func (t *Throttle) SetPulse(pulse int) {
 | 
			
		||||
	err := t.dev.SetPwm(t.channel, 0, gpio.Duty(pulse))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Infof("unable to set throttle pwm value: %v", err)
 | 
			
		||||
		zap.S().Errorf("unable to set throttle pwm value: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -34,7 +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)
 | 
			
		||||
	zap.S().Debugf("set throttle to %v-> %v (%v, %v, %v, %v, %v)", p, pulse, LeftAngle, RightAngle, t.minPulse, t.maxPulse, t.zeroPulse)
 | 
			
		||||
	t.SetPulse(pulse)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -47,7 +47,7 @@ func NewThrottle(channel, zeroPulse, minPulse, maxPulse int) *Throttle {
 | 
			
		||||
		maxPulse:  maxPulse,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	log.Infof("send zero pulse to calibrate ESC")
 | 
			
		||||
	zap.S().Info("send zero pulse to calibrate ESC")
 | 
			
		||||
	t.SetPercentValue(0)
 | 
			
		||||
 | 
			
		||||
	return &t
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user