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"
|
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 {
|
|
|
|
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 {
|
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
|
|
|
}
|