refactor: move packages to pkg directory
This commit is contained in:
		
							
								
								
									
										39
									
								
								pkg/actuator/pca9685.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								pkg/actuator/pca9685.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,39 @@
 | 
			
		||||
package actuator
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"go.uber.org/zap"
 | 
			
		||||
	"periph.io/x/conn/v3/i2c/i2creg"
 | 
			
		||||
	"periph.io/x/conn/v3/physic"
 | 
			
		||||
	"periph.io/x/devices/v3/pca9685"
 | 
			
		||||
	"periph.io/x/host/v3"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	device *pca9685.Dev
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	zap.S().Info("init pca9685 controller")
 | 
			
		||||
	_, err := host.Init()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		zap.S().Fatalf("unable to init host: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	zap.S().Info("open i2c bus")
 | 
			
		||||
	bus, err := i2creg.Open("")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		zap.S().Fatalf("unable to init i2c bus: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	zap.S().Info("i2c bus opened")
 | 
			
		||||
 | 
			
		||||
	device, err = pca9685.NewI2C(bus, pca9685.I2CAddr)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		zap.S().Fatalf("unable to init pca9685 bus: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	zap.S().Infof("set pwm frequency to %d", 60)
 | 
			
		||||
	err = device.SetPwmFreq(60 * physic.Hertz)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		zap.S().Panicf("unable to set pwm frequency: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	zap.S().Info("init done")
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										44
									
								
								pkg/actuator/steering.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								pkg/actuator/steering.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,44 @@
 | 
			
		||||
package actuator
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/cyrilix/robocar-pca9685/pkg/util"
 | 
			
		||||
	"go.uber.org/zap"
 | 
			
		||||
	"periph.io/x/conn/v3/gpio"
 | 
			
		||||
	"periph.io/x/devices/v3/pca9685"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	LeftAngle  = -1.
 | 
			
		||||
	RightAngle = 1.
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Steering struct {
 | 
			
		||||
	channel           int
 | 
			
		||||
	leftPWM, rightPWM int
 | 
			
		||||
	dev               *pca9685.Dev
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (s *Steering) SetPulse(pulse int) {
 | 
			
		||||
	err := s.dev.SetPwm(s.channel, 0, gpio.Duty(pulse))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		zap.S().Warnf("unable to set steering pwm value: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Set percent value steering
 | 
			
		||||
func (s *Steering) SetPercentValue(p float32) {
 | 
			
		||||
	// map absolute angle to angle that vehicle can implement.
 | 
			
		||||
	pulse := util.MapRange(float64(p), LeftAngle, RightAngle, float64(s.leftPWM), float64(s.rightPWM))
 | 
			
		||||
	s.SetPulse(pulse)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewSteering(channel, leftPWM, rightPWM int) *Steering {
 | 
			
		||||
	s := Steering{
 | 
			
		||||
		channel:  channel,
 | 
			
		||||
		dev:      device,
 | 
			
		||||
		leftPWM:  leftPWM,
 | 
			
		||||
		rightPWM: rightPWM,
 | 
			
		||||
	}
 | 
			
		||||
	return &s
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										54
									
								
								pkg/actuator/throttle.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								pkg/actuator/throttle.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,54 @@
 | 
			
		||||
package actuator
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/cyrilix/robocar-pca9685/pkg/util"
 | 
			
		||||
	"go.uber.org/zap"
 | 
			
		||||
	"periph.io/x/conn/v3/gpio"
 | 
			
		||||
	"periph.io/x/devices/v3/pca9685"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	MinThrottle = -1
 | 
			
		||||
	MaxThrottle = 1
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Throttle struct {
 | 
			
		||||
	channel                       int
 | 
			
		||||
	zeroPulse, minPulse, maxPulse int
 | 
			
		||||
	dev                           *pca9685.Dev
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (t *Throttle) SetPulse(pulse int) {
 | 
			
		||||
	err := t.dev.SetPwm(t.channel, 0, gpio.Duty(pulse))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		zap.S().Errorf("unable to set throttle pwm value: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Set percent value throttle
 | 
			
		||||
func (t *Throttle) SetPercentValue(p float32) {
 | 
			
		||||
	var pulse int
 | 
			
		||||
	if p > 0 {
 | 
			
		||||
		pulse = util.MapRange(float64(p), 0, MaxThrottle, float64(t.zeroPulse), float64(t.maxPulse))
 | 
			
		||||
	} else {
 | 
			
		||||
		pulse = util.MapRange(float64(p), MinThrottle, 0, float64(t.minPulse), float64(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)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewThrottle(channel, zeroPulse, minPulse, maxPulse int) *Throttle {
 | 
			
		||||
	t := Throttle{
 | 
			
		||||
		channel:   channel,
 | 
			
		||||
		dev:       device,
 | 
			
		||||
		zeroPulse: zeroPulse,
 | 
			
		||||
		minPulse:  minPulse,
 | 
			
		||||
		maxPulse:  maxPulse,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	zap.S().Info("send zero pulse to calibrate ESC")
 | 
			
		||||
	t.SetPercentValue(0)
 | 
			
		||||
 | 
			
		||||
	return &t
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user