First implementation

This commit is contained in:
2019-12-27 18:23:08 +01:00
parent 1cf57702cc
commit b3de5ca724
211 changed files with 28458 additions and 0 deletions

39
actuator/pca9685.go Normal file
View File

@ -0,0 +1,39 @@
package actuator
import (
"log"
"periph.io/x/periph/conn/i2c/i2creg"
"periph.io/x/periph/conn/physic"
"periph.io/x/periph/experimental/devices/pca9685"
"periph.io/x/periph/host"
)
var (
device *pca9685.Dev
)
func init() {
log.Print("init pca9685 controller")
_, err := host.Init()
if err != nil {
log.Fatalf("unable to init host: %v", err)
}
log.Print("open i2c bus")
bus, err := i2creg.Open("")
if err != nil {
log.Fatalf("unable to init i2c bus: %v", err)
}
log.Print("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)
err = device.SetPwmFreq(60 * physic.Hertz)
if err != nil {
log.Fatalf("unable to set pwm frequency: %v", err)
}
log.Print("init done")
}

44
actuator/steering.go Normal file
View File

@ -0,0 +1,44 @@
package actuator
import (
"github.com/cyrilix/robocar-pca9685/util"
"log"
"periph.io/x/periph/conn/gpio"
"periph.io/x/periph/experimental/devices/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 {
log.Printf("unable to set throttle pwm value: %v", err)
}
}
// Set percent value steering
func (s *Steering) SetPercentValue(p float64) {
// map absolute angle to angle that vehicle can implement.
pulse := util.MapRange(p, LeftAngle, RightAngle, float64(s.leftPWM), float64(s.rightPWM))
s.SetPulse(pulse)
}
func NewSteering(channel, leftPWM, rightPWM int) *Steering {
t := Steering{
channel: channel,
dev: device,
leftPWM: leftPWM,
rightPWM: rightPWM,
}
return &t
}

53
actuator/throttle.go Normal file
View File

@ -0,0 +1,53 @@
package actuator
import (
"github.com/cyrilix/robocar-pca9685/util"
"log"
"periph.io/x/periph/conn/gpio"
"periph.io/x/periph/experimental/devices/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 {
log.Printf("unable to set throttle pwm value: %v", err)
}
}
// Set percent value throttle
func (t *Throttle) SetPercentValue(p float64) {
var pulse int
if p > 0 {
pulse = util.MapRange(p, 0, MaxThrottle, float64(t.zeroPulse), float64(t.maxPulse))
} else {
pulse = util.MapRange(p, MinThrottle, 0, float64(t.minPulse), float64(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,
}
log.Printf("send zero pulse to calibrate ESC: %v", zeroPulse)
t.SetPulse(zeroPulse)
return &t
}