2022-09-05 14:44:02 +00:00
|
|
|
package throttle
|
|
|
|
|
2022-09-05 13:30:26 +00:00
|
|
|
import (
|
2023-06-14 18:07:12 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"github.com/cyrilix/robocar-protobuf/go/events"
|
2022-09-05 13:30:26 +00:00
|
|
|
"github.com/cyrilix/robocar-throttle/pkg/types"
|
|
|
|
"math"
|
2023-06-14 18:07:12 +00:00
|
|
|
"os"
|
|
|
|
"sync"
|
2022-09-05 13:30:26 +00:00
|
|
|
)
|
2022-09-05 14:44:02 +00:00
|
|
|
|
2023-06-14 18:07:12 +00:00
|
|
|
type Processor interface {
|
|
|
|
// Process compute throttle from steering value
|
|
|
|
Process(steering types.Steering) types.Throttle
|
|
|
|
|
|
|
|
SetSpeedZone(sz events.SpeedZone)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSteeringProcessor(minThrottle, maxThrottle types.Throttle) *SteeringProcessor {
|
|
|
|
return &SteeringProcessor{
|
|
|
|
minThrottle: minThrottle,
|
|
|
|
maxThrottle: maxThrottle,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 14:44:02 +00:00
|
|
|
type SteeringProcessor struct {
|
2022-09-05 13:30:26 +00:00
|
|
|
minThrottle, maxThrottle types.Throttle
|
2022-09-05 14:44:02 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 18:07:12 +00:00
|
|
|
func (sp *SteeringProcessor) SetSpeedZone(_ events.SpeedZone) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-05 14:44:02 +00:00
|
|
|
// Process compute throttle from steering value
|
2023-06-14 18:07:12 +00:00
|
|
|
func (sp *SteeringProcessor) Process(steering types.Steering) types.Throttle {
|
2022-09-05 14:44:02 +00:00
|
|
|
absSteering := math.Abs(float64(steering))
|
2022-09-05 13:30:26 +00:00
|
|
|
return sp.minThrottle + types.Throttle(float64(sp.maxThrottle-sp.minThrottle)*(1-absSteering))
|
2022-09-05 14:44:02 +00:00
|
|
|
}
|
2023-06-14 18:07:12 +00:00
|
|
|
|
|
|
|
func NewSpeedZoneProcessor(slowThrottle, normalThrottle, fastThrottle types.Throttle,
|
|
|
|
moderateSteering, fullSteering float64) *SpeedZoneProcessor {
|
|
|
|
return &SpeedZoneProcessor{
|
|
|
|
muSz: sync.Mutex{},
|
|
|
|
speedZone: events.SpeedZone_UNKNOWN,
|
|
|
|
slowThrottle: slowThrottle,
|
|
|
|
normalThrottle: normalThrottle,
|
|
|
|
fastThrottle: fastThrottle,
|
|
|
|
moderateSteering: moderateSteering,
|
|
|
|
fullSteering: fullSteering,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type SpeedZoneProcessor struct {
|
|
|
|
muSz sync.Mutex
|
|
|
|
speedZone events.SpeedZone
|
|
|
|
slowThrottle, normalThrottle, fastThrottle types.Throttle
|
|
|
|
moderateSteering, fullSteering float64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sp *SpeedZoneProcessor) SpeedZone() events.SpeedZone {
|
|
|
|
sp.muSz.Lock()
|
|
|
|
defer sp.muSz.Unlock()
|
|
|
|
return sp.speedZone
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sp *SpeedZoneProcessor) SetSpeedZone(sz events.SpeedZone) {
|
|
|
|
sp.muSz.Lock()
|
|
|
|
defer sp.muSz.Unlock()
|
|
|
|
sp.speedZone = sz
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process compute throttle from steering value
|
|
|
|
func (sp *SpeedZoneProcessor) Process(steering types.Steering) types.Throttle {
|
|
|
|
st := math.Abs(float64(steering))
|
|
|
|
|
|
|
|
switch sp.SpeedZone() {
|
|
|
|
case events.SpeedZone_FAST:
|
|
|
|
if st >= sp.fullSteering {
|
|
|
|
return sp.slowThrottle
|
|
|
|
} else if st >= sp.moderateSteering {
|
|
|
|
return sp.normalThrottle
|
|
|
|
}
|
|
|
|
return sp.fastThrottle
|
|
|
|
case events.SpeedZone_NORMAL:
|
|
|
|
if st > sp.fullSteering {
|
|
|
|
return sp.slowThrottle
|
|
|
|
}
|
|
|
|
return sp.normalThrottle
|
|
|
|
case events.SpeedZone_SLOW:
|
|
|
|
return sp.slowThrottle
|
|
|
|
}
|
|
|
|
return sp.slowThrottle
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCustomSteeringProcessor(cfg *Config) *CustomSteeringProcessor {
|