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 (
|
|
|
|
MinThrottle = -1
|
|
|
|
MaxThrottle = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
type Throttle struct {
|
|
|
|
channel int
|
|
|
|
zeroPulse, minPulse, maxPulse int
|
|
|
|
dev *pca9685.Dev
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Throttle) SetPulse(pulse int) {
|
|
|
|
err := t.dev.SetPwm(t.channel, 0, gpio.Duty(pulse))
|
|
|
|
if err != nil {
|
2021-10-12 17:28:56 +00:00
|
|
|
zap.S().Errorf("unable to set throttle pwm value: %v", err)
|
2019-12-27 17:23:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set percent value throttle
|
2020-01-01 19:54:38 +00:00
|
|
|
func (t *Throttle) SetPercentValue(p float32) {
|
2019-12-27 17:23:08 +00:00
|
|
|
var pulse int
|
|
|
|
if p > 0 {
|
2020-01-01 19:54:38 +00:00
|
|
|
pulse = util.MapRange(float64(p), 0, MaxThrottle, float64(t.zeroPulse), float64(t.maxPulse))
|
2019-12-27 17:23:08 +00:00
|
|
|
} else {
|
2020-01-01 19:54:38 +00:00
|
|
|
pulse = util.MapRange(float64(p), MinThrottle, 0, float64(t.minPulse), float64(t.zeroPulse))
|
2019-12-27 17:23:08 +00:00
|
|
|
}
|
2021-10-12 17:28:56 +00:00
|
|
|
zap.S().Debugf("set throttle to %v-> %v (%v, %v, %v, %v, %v)", p, pulse, LeftAngle, RightAngle, t.minPulse, t.maxPulse, t.zeroPulse)
|
2019-12-27 17:23:08 +00:00
|
|
|
t.SetPulse(pulse)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewThrottle(channel, zeroPulse, minPulse, maxPulse int) *Throttle {
|
|
|
|
t := Throttle{
|
|
|
|
channel: channel,
|
|
|
|
dev: device,
|
|
|
|
zeroPulse: zeroPulse,
|
|
|
|
minPulse: minPulse,
|
|
|
|
maxPulse: maxPulse,
|
|
|
|
}
|
|
|
|
|
2021-10-12 17:28:56 +00:00
|
|
|
zap.S().Info("send zero pulse to calibrate ESC")
|
2020-02-03 18:16:07 +00:00
|
|
|
t.SetPercentValue(0)
|
2019-12-27 17:23:08 +00:00
|
|
|
|
|
|
|
return &t
|
|
|
|
}
|