robocar-pca9685/pkg/actuator/steering.go

53 lines
1.2 KiB
Go
Raw Normal View History

2019-12-27 17:23:08 +00:00
package actuator
import (
"github.com/cyrilix/robocar-pca9685/pkg/util"
2021-10-12 17:28:56 +00:00
"go.uber.org/zap"
"periph.io/x/conn/v3/gpio"
"periph.io/x/devices/v3/pca9685"
2019-12-27 17:23:08 +00:00
)
const (
LeftAngle = -1.
RightAngle = 1.
)
type Steering struct {
channel int
leftPWM, rightPWM, centerPWM int
dev *pca9685.Dev
2019-12-27 17:23:08 +00:00
}
func (s *Steering) SetPulse(pulse int) {
err := s.dev.SetPwm(s.channel, 0, gpio.Duty(pulse))
if err != nil {
2021-10-12 17:28:56 +00:00
zap.S().Warnf("unable to set steering pwm value: %v", err)
2019-12-27 17:23:08 +00:00
}
}
// SetPercentValue 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.
2022-03-29 17:47:22 +00:00
pulse := 0
if p > 0 {
pulse = util.MapRange(float64(p), 0, RightAngle, float64(s.centerPWM), float64(s.rightPWM))
} else if p < 0 {
pulse = util.MapRange(float64(p), LeftAngle, 0, float64(s.leftPWM), float64(s.centerPWM))
}
2022-02-25 18:16:59 +00:00
zap.S().Debugf("convert steering %v to %v", p, pulse)
2019-12-27 17:23:08 +00:00
s.SetPulse(pulse)
}
func NewSteering(channel, leftPWM, rightPWM, centerPWM int) *Steering {
2020-02-03 18:16:07 +00:00
s := Steering{
channel: channel,
dev: device,
leftPWM: leftPWM,
rightPWM: rightPWM,
centerPWM: centerPWM,
2019-12-27 17:23:08 +00:00
}
2020-02-03 18:16:07 +00:00
return &s
2019-12-27 17:23:08 +00:00
}