Naive implementation that always return min value
This commit is contained in:
54
part/part.go
Normal file
54
part/part.go
Normal file
@ -0,0 +1,54 @@
|
||||
package part
|
||||
|
||||
import (
|
||||
"github.com/cyrilix/robocar-base/mqttdevice"
|
||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CommandValue struct {
|
||||
Value float64
|
||||
Confidence float64
|
||||
}
|
||||
type Steering CommandValue
|
||||
type Throttle CommandValue
|
||||
|
||||
func NewPart(pub mqttdevice.Publisher, throttleTopic string, minValue, maxValue float64) *ThrottlePart {
|
||||
return &ThrottlePart{
|
||||
pub: pub,
|
||||
throttleTopic: throttleTopic,
|
||||
minThrottle: minValue,
|
||||
maxThrottle: maxValue,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
type ThrottlePart struct {
|
||||
client mqtt.Client
|
||||
pub mqttdevice.Publisher
|
||||
throttleTopic string
|
||||
minThrottle, maxThrottle float64
|
||||
cancel chan interface{}
|
||||
}
|
||||
|
||||
func (p *ThrottlePart) Start() error {
|
||||
p.cancel = make(chan interface{})
|
||||
ticker := time.NewTicker(500 * time.Millisecond)
|
||||
for {
|
||||
|
||||
p.pub.Publish(p.throttleTopic, mqttdevice.NewMqttValue(Throttle{
|
||||
Value: p.minThrottle,
|
||||
Confidence: 1.0,
|
||||
}))
|
||||
|
||||
select {
|
||||
case <-ticker.C:
|
||||
case <-p.cancel:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ThrottlePart) Stop() {
|
||||
close(p.cancel)
|
||||
}
|
36
part/part_test.go
Normal file
36
part/part_test.go
Normal file
@ -0,0 +1,36 @@
|
||||
package part
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/cyrilix/robocar-base/testtools"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestDefaultThrottle(t *testing.T){
|
||||
throttleTopic := "topic/throttle"
|
||||
minValue := 0.56
|
||||
|
||||
pub := testtools.NewFakePublisher()
|
||||
p := NewPart(pub, throttleTopic, minValue, 1.)
|
||||
|
||||
go p.Start()
|
||||
defer p.Stop()
|
||||
|
||||
time.Sleep(1 * time.Millisecond)
|
||||
|
||||
mqttValue := pub.PublishedEvent(throttleTopic)
|
||||
var throttle Throttle
|
||||
err := json.Unmarshal(mqttValue, &throttle)
|
||||
if err != nil {
|
||||
t.Errorf("unable to unmarshall response: %v", err)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
if throttle.Value != minValue {
|
||||
t.Errorf("bad throttle value: %v, wants %v", throttle.Value, minValue)
|
||||
}
|
||||
if throttle.Confidence != 1. {
|
||||
t.Errorf("bad throtlle confidence: %v, wants %v", throttle.Confidence, 1.)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user