2019-12-27 17:23:08 +00:00
|
|
|
package actuator
|
|
|
|
|
|
|
|
import (
|
2021-10-12 17:31:15 +00:00
|
|
|
"github.com/cyrilix/robocar-pca9685/pkg/util"
|
2021-10-12 17:28:56 +00:00
|
|
|
"go.uber.org/zap"
|
2021-09-01 19:34:31 +00:00
|
|
|
"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 {
|
2022-01-17 18:00:12 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-01-17 18:00:12 +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-01-17 18:00:12 +00:00
|
|
|
|
|
|
|
pulse := s.centerPWM
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-01-17 18:00:12 +00:00
|
|
|
func NewSteering(channel, leftPWM, rightPWM, centerPWM int) *Steering {
|
2020-02-03 18:16:07 +00:00
|
|
|
s := Steering{
|
2022-01-17 18:00:12 +00:00
|
|
|
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
|
|
|
}
|