robocar-pca9685/actuator/steering.go

45 lines
955 B
Go
Raw Normal View History

2019-12-27 17:23:08 +00:00
package actuator
import (
"github.com/cyrilix/robocar-pca9685/util"
2020-01-01 19:54:38 +00:00
log "github.com/sirupsen/logrus"
2019-12-27 17:23:08 +00:00
"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 {
2020-02-03 18:15:51 +00:00
log.Warningf("unable to set steering pwm value: %v", err)
2019-12-27 17:23:08 +00:00
}
}
// Set percent value steering
2020-01-01 19:54:38 +00:00
func (s *Steering) SetPercentValue(p float32) {
2019-12-27 17:23:08 +00:00
// map absolute angle to angle that vehicle can implement.
2020-01-01 19:54:38 +00:00
pulse := util.MapRange(float64(p), LeftAngle, RightAngle, float64(s.leftPWM), float64(s.rightPWM))
2019-12-27 17:23:08 +00:00
s.SetPulse(pulse)
}
func NewSteering(channel, leftPWM, rightPWM int) *Steering {
t := Steering{
channel: channel,
dev: device,
leftPWM: leftPWM,
rightPWM: rightPWM,
}
return &t
}