robocar-pca9685/pkg/actuator/steering.go

45 lines
924 B
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 int
dev *pca9685.Dev
}
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
}
}
// 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 {
2020-02-03 18:16:07 +00:00
s := Steering{
2019-12-27 17:23:08 +00:00
channel: channel,
dev: device,
leftPWM: leftPWM,
rightPWM: rightPWM,
}
2020-02-03 18:16:07 +00:00
return &s
2019-12-27 17:23:08 +00:00
}